Almost _all_ of the final connection-layer loop, in both SSH1 and
[u/mdw/putty] / unix / pterm.c
CommitLineData
f7f27309 1/*
2 * pterm - a fusion of the PuTTY terminal emulator with a Unix pty
3 * back end, all running as a GTK application. Wish me luck.
4 */
5
90cfd8f4 6#define _GNU_SOURCE
7
f7f27309 8#include <string.h>
d64838e1 9#include <assert.h>
f7f27309 10#include <stdlib.h>
90cfd8f4 11#include <string.h>
8caf7aed 12#include <signal.h>
1709795f 13#include <stdio.h>
f7f27309 14#include <time.h>
054d8535 15#include <errno.h>
16#include <fcntl.h>
17#include <unistd.h>
90cfd8f4 18#include <sys/types.h>
19#include <sys/wait.h>
f7f27309 20#include <gtk/gtk.h>
a57cd64b 21#include <gdk/gdkkeysyms.h>
c5e438ec 22#include <gdk/gdkx.h>
23#include <X11/Xlib.h>
24#include <X11/Xutil.h>
3aac0d35 25#include <X11/Xatom.h>
f7f27309 26
1709795f 27#define PUTTY_DO_GLOBALS /* actually _define_ globals */
2dc6356a 28
1709795f 29#include "putty.h"
887035a5 30#include "terminal.h"
1709795f 31
f7f27309 32#define CAT2(x,y) x ## y
33#define CAT(x,y) CAT2(x,y)
34#define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
35
d64838e1 36#define NCOLOURS (lenof(((Config *)0)->colours))
37
8c161662 38GdkAtom compound_text_atom, utf8_string_atom;
39
56801e3d 40extern char **pty_argv; /* declared in pty.c */
41extern int use_pty_argv;
42
39934deb 43/*
44 * Timers are global across all sessions (even if we were handling
45 * multiple sessions, which we aren't), so the current timer ID is
46 * a global variable.
47 */
48static guint timer_id = 0;
49
d64838e1 50struct gui_data {
12d200b1 51 GtkWidget *window, *area, *sbar;
6a5e84dd 52 GtkBox *hbox;
53 GtkAdjustment *sbar_adjust;
e3be8de5 54 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
55 *restartitem;
56801e3d 56 GtkWidget *sessionsmenu;
d64838e1 57 GdkPixmap *pixmap;
006238cb 58 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
2dc6356a 59 struct {
60 int charset;
61 int is_wide;
006238cb 62 } fontinfo[4];
aca2a702 63 int xpos, ypos, gotpos, gravity;
57e636ca 64 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
d64838e1 65 GdkColor cols[NCOLOURS];
66 GdkColormap *colmap;
e6346999 67 wchar_t *pastein_data;
085f4a68 68 int direct_to_font;
e6346999 69 int pastein_data_len;
48f1bc44 70 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
71 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
83616aab 72 int font_width, font_height;
56b9b9a7 73 int width, height;
88e6b9ca 74 int ignore_sbar;
b3530065 75 int mouseptr_visible;
0f660c8f 76 guint term_paste_idle_id;
c33c3d76 77 int alt_keycode;
045f707b 78 int alt_digits;
12d200b1 79 char wintitle[sizeof(((Config *)0)->wintitle)];
16891265 80 char icontitle[sizeof(((Config *)0)->wintitle)];
74aca06d 81 int master_fd, master_func_id;
b9d7bcad 82 void *ldisc;
6b78788a 83 Backend *back;
84 void *backhandle;
5def7522 85 Terminal *term;
a8327734 86 void *logctx;
74aca06d 87 int exited;
21d2b241 88 struct unicode_data ucsdata;
3ea863a3 89 Config cfg;
8eed910d 90 void *eventlogstuff;
56801e3d 91 char *progname, **gtkargvstart;
92 int ngtkargs;
d0a039b1 93 guint32 input_event_time; /* Timestamp of the most recent input event. */
a8327734 94};
95
96struct draw_ctx {
97 GdkGC *gc;
98 struct gui_data *inst;
d64838e1 99};
100
d64838e1 101static int send_raw_mouse;
102
c5e438ec 103static char *app_name = "pterm";
104
e3be8de5 105static void start_backend(struct gui_data *inst);
106
c85623f9 107char *x_get_default(const char *key)
c5e438ec 108{
109 return XGetDefault(GDK_DISPLAY(), app_name, key);
110}
111
3f935d5b 112void connection_fatal(void *frontend, char *p, ...)
113{
fbf6cb3b 114 struct gui_data *inst = (struct gui_data *)frontend;
3f935d5b 115
116 va_list ap;
117 char *msg;
118 va_start(ap, p);
119 msg = dupvprintf(p, ap);
120 va_end(ap);
121 inst->exited = TRUE;
122 fatal_message_box(inst->window, msg);
123 sfree(msg);
124 if (inst->cfg.close_on_exit == FORCE_ON)
125 cleanup_exit(1);
126}
127
5a9eb105 128/*
129 * Default settings that are specific to pterm.
130 */
9a30e26b 131FontSpec platform_default_fontspec(const char *name)
5a9eb105 132{
9a30e26b 133 FontSpec ret;
5a9eb105 134 if (!strcmp(name, "Font"))
9a30e26b 135 strcpy(ret.name, "fixed");
136 else
137 *ret.name = '\0';
138 return ret;
139}
140
141Filename platform_default_filename(const char *name)
142{
143 Filename ret;
144 if (!strcmp(name, "LogFileName"))
145 strcpy(ret.path, "putty.log");
146 else
147 *ret.path = '\0';
148 return ret;
149}
150
151char *platform_default_s(const char *name)
152{
5a9eb105 153 return NULL;
154}
155
c85623f9 156int platform_default_i(const char *name, int def)
5a9eb105 157{
158 if (!strcmp(name, "CloseOnExit"))
c888fed4 159 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
6e785e0c 160 if (!strcmp(name, "WinNameAlways"))
161 return 0; /* X natively supports icon titles, so use 'em by default */
5a9eb105 162 return def;
163}
164
b9d7bcad 165void ldisc_update(void *frontend, int echo, int edit)
1709795f 166{
167 /*
168 * This is a stub in pterm. If I ever produce a Unix
169 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
170 * then it will require some termios manoeuvring analogous to
171 * that in the Windows plink.c, but here it's meaningless.
172 */
173}
174
fbf6cb3b 175int from_backend(void *frontend, int is_stderr, const char *data, int len)
176{
177 struct gui_data *inst = (struct gui_data *)frontend;
178 return term_data(inst->term, is_stderr, data, len);
179}
180
cbe2d68f 181void logevent(void *frontend, const char *string)
1709795f 182{
fbf6cb3b 183 struct gui_data *inst = (struct gui_data *)frontend;
8eed910d 184
185 log_eventlog(inst->logctx, string);
186
187 logevent_dlg(inst->eventlogstuff, string);
1709795f 188}
189
a8327734 190int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
e9aef757 191{
a8327734 192 struct gui_data *inst = (struct gui_data *)frontend;
193
e9aef757 194 if (which)
195 return inst->font_height;
196 else
197 return inst->font_width;
198}
199
1709795f 200/*
201 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
202 * into a cooked one (SELECT, EXTEND, PASTE).
203 *
204 * In Unix, this is not configurable; the X button arrangement is
205 * rock-solid across all applications, everyone has a three-button
206 * mouse or a means of faking it, and there is no need to switch
207 * buttons around at all.
208 */
c387aa9f 209static Mouse_Button translate_button(Mouse_Button button)
1709795f 210{
a8327734 211 /* struct gui_data *inst = (struct gui_data *)frontend; */
212
1709795f 213 if (button == MBT_LEFT)
214 return MBT_SELECT;
215 if (button == MBT_MIDDLE)
216 return MBT_PASTE;
217 if (button == MBT_RIGHT)
218 return MBT_EXTEND;
219 return 0; /* shouldn't happen */
220}
221
222/*
5bf9955d 223 * Return the top-level GtkWindow associated with a particular
224 * front end instance.
225 */
226void *get_window(void *frontend)
227{
fbf6cb3b 228 struct gui_data *inst = (struct gui_data *)frontend;
5bf9955d 229 return inst->window;
230}
231
232/*
1709795f 233 * Minimise or restore the window in response to a server-side
234 * request.
235 */
a8327734 236void set_iconic(void *frontend, int iconic)
1709795f 237{
16891265 238 /*
239 * GTK 1.2 doesn't know how to do this.
240 */
241#if GTK_CHECK_VERSION(2,0,0)
a8327734 242 struct gui_data *inst = (struct gui_data *)frontend;
16891265 243 if (iconic)
244 gtk_window_iconify(GTK_WINDOW(inst->window));
245 else
246 gtk_window_deiconify(GTK_WINDOW(inst->window));
247#endif
1709795f 248}
249
250/*
251 * Move the window in response to a server-side request.
252 */
a8327734 253void move_window(void *frontend, int x, int y)
1709795f 254{
a8327734 255 struct gui_data *inst = (struct gui_data *)frontend;
16891265 256 /*
257 * I assume that when the GTK version of this call is available
258 * we should use it. Not sure how it differs from the GDK one,
259 * though.
260 */
261#if GTK_CHECK_VERSION(2,0,0)
262 gtk_window_move(GTK_WINDOW(inst->window), x, y);
263#else
264 gdk_window_move(inst->window->window, x, y);
265#endif
1709795f 266}
267
268/*
269 * Move the window to the top or bottom of the z-order in response
270 * to a server-side request.
271 */
a8327734 272void set_zorder(void *frontend, int top)
1709795f 273{
a8327734 274 struct gui_data *inst = (struct gui_data *)frontend;
16891265 275 if (top)
276 gdk_window_raise(inst->window->window);
277 else
278 gdk_window_lower(inst->window->window);
1709795f 279}
280
281/*
282 * Refresh the window in response to a server-side request.
283 */
a8327734 284void refresh_window(void *frontend)
1709795f 285{
a8327734 286 struct gui_data *inst = (struct gui_data *)frontend;
5def7522 287 term_invalidate(inst->term);
1709795f 288}
289
290/*
291 * Maximise or restore the window in response to a server-side
292 * request.
293 */
a8327734 294void set_zoomed(void *frontend, int zoomed)
1709795f 295{
16891265 296 /*
297 * GTK 1.2 doesn't know how to do this.
298 */
299#if GTK_CHECK_VERSION(2,0,0)
a8327734 300 struct gui_data *inst = (struct gui_data *)frontend;
16891265 301 if (iconic)
302 gtk_window_maximize(GTK_WINDOW(inst->window));
303 else
304 gtk_window_unmaximize(GTK_WINDOW(inst->window));
305#endif
1709795f 306}
307
308/*
309 * Report whether the window is iconic, for terminal reports.
310 */
a8327734 311int is_iconic(void *frontend)
1709795f 312{
a8327734 313 struct gui_data *inst = (struct gui_data *)frontend;
16891265 314 return !gdk_window_is_viewable(inst->window->window);
1709795f 315}
316
317/*
318 * Report the window's position, for terminal reports.
319 */
a8327734 320void get_window_pos(void *frontend, int *x, int *y)
1709795f 321{
a8327734 322 struct gui_data *inst = (struct gui_data *)frontend;
16891265 323 /*
324 * I assume that when the GTK version of this call is available
325 * we should use it. Not sure how it differs from the GDK one,
326 * though.
327 */
328#if GTK_CHECK_VERSION(2,0,0)
329 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
330#else
331 gdk_window_get_position(inst->window->window, x, y);
332#endif
1709795f 333}
334
335/*
336 * Report the window's pixel size, for terminal reports.
337 */
a8327734 338void get_window_pixels(void *frontend, int *x, int *y)
1709795f 339{
a8327734 340 struct gui_data *inst = (struct gui_data *)frontend;
16891265 341 /*
342 * I assume that when the GTK version of this call is available
343 * we should use it. Not sure how it differs from the GDK one,
344 * though.
345 */
346#if GTK_CHECK_VERSION(2,0,0)
347 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
348#else
349 gdk_window_get_size(inst->window->window, x, y);
350#endif
1709795f 351}
352
353/*
354 * Return the window or icon title.
355 */
a8327734 356char *get_window_title(void *frontend, int icon)
1709795f 357{
a8327734 358 struct gui_data *inst = (struct gui_data *)frontend;
5446ac11 359 return icon ? inst->icontitle : inst->wintitle;
1709795f 360}
f7f27309 361
f7f27309 362gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
363{
abb6895f 364 struct gui_data *inst = (struct gui_data *)data;
6b0eeb4e 365 if (!inst->exited && inst->cfg.warn_on_close) {
fbf6cb3b 366 if (!reallyclose(inst))
abb6895f 367 return TRUE;
368 }
f7f27309 369 return FALSE;
370}
371
a8327734 372static void show_mouseptr(struct gui_data *inst, int show)
57e636ca 373{
3ea863a3 374 if (!inst->cfg.hide_mouseptr)
57e636ca 375 show = 1;
376 if (show)
377 gdk_window_set_cursor(inst->area->window, inst->currcursor);
378 else
379 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
b3530065 380 inst->mouseptr_visible = show;
57e636ca 381}
382
db26b3af 383void draw_backing_rect(struct gui_data *inst)
384{
385 GdkGC *gc = gdk_gc_new(inst->area->window);
386 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
387 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
388 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
389 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
390 gdk_gc_unref(gc);
391}
392
f7f27309 393gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
394{
395 struct gui_data *inst = (struct gui_data *)data;
88e6b9ca 396 int w, h, need_size = 0;
f7f27309 397
e7ec3df1 398 /*
399 * See if the terminal size has changed, in which case we must
400 * let the terminal know.
401 */
3ea863a3 402 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
403 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
56b9b9a7 404 if (w != inst->width || h != inst->height) {
56b9b9a7 405 inst->cfg.width = inst->width = w;
406 inst->cfg.height = inst->height = h;
88e6b9ca 407 need_size = 1;
408 }
e7ec3df1 409
410 if (inst->pixmap) {
411 gdk_pixmap_unref(inst->pixmap);
412 inst->pixmap = NULL;
6a5e84dd 413 }
f7f27309 414
e7ec3df1 415 inst->pixmap = gdk_pixmap_new(widget->window,
416 (inst->cfg.width * inst->font_width +
417 2*inst->cfg.window_border),
418 (inst->cfg.height * inst->font_height +
419 2*inst->cfg.window_border), -1);
420
db26b3af 421 draw_backing_rect(inst);
e7ec3df1 422
423 if (need_size && inst->term) {
3ea863a3 424 term_size(inst->term, h, w, inst->cfg.savelines);
88e6b9ca 425 }
426
e7ec3df1 427 if (inst->term)
428 term_invalidate(inst->term);
429
f7f27309 430 return TRUE;
431}
432
433gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
434{
a8327734 435 struct gui_data *inst = (struct gui_data *)data;
f7f27309 436
437 /*
1709795f 438 * Pass the exposed rectangle to terminal.c, which will call us
439 * back to do the actual painting.
f7f27309 440 */
6a5e84dd 441 if (inst->pixmap) {
442 gdk_draw_pixmap(widget->window,
443 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
444 inst->pixmap,
445 event->area.x, event->area.y,
446 event->area.x, event->area.y,
447 event->area.width, event->area.height);
448 }
1709795f 449 return TRUE;
f7f27309 450}
451
452#define KEY_PRESSED(k) \
453 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
454
455gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
456{
a8327734 457 struct gui_data *inst = (struct gui_data *)data;
a57cd64b 458 char output[32];
f5dd8adb 459 wchar_t ucsoutput[2];
460 int ucsval, start, end, special, use_ucsoutput;
f7f27309 461
d0a039b1 462 /* Remember the timestamp. */
463 inst->input_event_time = event->time;
464
c33c3d76 465 /* By default, nothing is generated. */
466 end = start = 0;
f5dd8adb 467 special = use_ucsoutput = FALSE;
c33c3d76 468
469 /*
470 * If Alt is being released after typing an Alt+numberpad
471 * sequence, we should generate the code that was typed.
045f707b 472 *
473 * Note that we only do this if more than one key was actually
474 * pressed - I don't think Alt+NumPad4 should be ^D or that
475 * Alt+NumPad3 should be ^C, for example. There's no serious
476 * inconvenience in having to type a zero before a single-digit
477 * character code.
c33c3d76 478 */
479 if (event->type == GDK_KEY_RELEASE &&
480 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
481 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
045f707b 482 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
c33c3d76 483#ifdef KEY_DEBUGGING
484 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
485#endif
486 output[0] = inst->alt_keycode;
487 end = 1;
488 goto done;
489 }
490
1709795f 491 if (event->type == GDK_KEY_PRESS) {
a57cd64b 492#ifdef KEY_DEBUGGING
493 {
494 int i;
495 printf("keypress: keyval = %04x, state = %08x; string =",
496 event->keyval, event->state);
497 for (i = 0; event->string[i]; i++)
498 printf(" %02x", (unsigned char) event->string[i]);
499 printf("\n");
500 }
501#endif
502
503 /*
c33c3d76 504 * NYI: Compose key (!!! requires Unicode faff before even trying)
a57cd64b 505 */
506
6a5e84dd 507 /*
c33c3d76 508 * If Alt has just been pressed, we start potentially
509 * accumulating an Alt+numberpad code. We do this by
510 * setting alt_keycode to -1 (nothing yet but plausible).
511 */
512 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
513 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
514 inst->alt_keycode = -1;
045f707b 515 inst->alt_digits = 0;
c33c3d76 516 goto done; /* this generates nothing else */
517 }
518
519 /*
520 * If we're seeing a numberpad key press with Mod1 down,
521 * consider adding it to alt_keycode if that's sensible.
522 * Anything _else_ with Mod1 down cancels any possibility
523 * of an ALT keycode: we set alt_keycode to -2.
524 */
525 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
526 int digit = -1;
527 switch (event->keyval) {
528 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
529 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
530 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
531 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
532 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
533 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
534 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
535 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
536 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
537 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
538 }
539 if (digit < 0)
540 inst->alt_keycode = -2; /* it's invalid */
541 else {
542#ifdef KEY_DEBUGGING
543 printf("Adding digit %d to keycode %d", digit,
544 inst->alt_keycode);
545#endif
546 if (inst->alt_keycode == -1)
547 inst->alt_keycode = digit; /* one-digit code */
548 else
549 inst->alt_keycode = inst->alt_keycode * 10 + digit;
045f707b 550 inst->alt_digits++;
c33c3d76 551#ifdef KEY_DEBUGGING
552 printf(" gives new code %d\n", inst->alt_keycode);
553#endif
554 /* Having used this digit, we now do nothing more with it. */
555 goto done;
556 }
557 }
558
559 /*
6a5e84dd 560 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
561 * at all.
562 */
563 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
3ea863a3 564 term_scroll(inst->term, 0, -inst->cfg.height/2);
6a5e84dd 565 return TRUE;
566 }
153580da 567 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
568 term_scroll(inst->term, 0, -1);
569 return TRUE;
570 }
6a5e84dd 571 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
3ea863a3 572 term_scroll(inst->term, 0, +inst->cfg.height/2);
6a5e84dd 573 return TRUE;
574 }
153580da 575 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
576 term_scroll(inst->term, 0, +1);
577 return TRUE;
578 }
6a5e84dd 579
2a2c1973 580 /*
581 * Neither does Shift-Ins.
582 */
583 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
a8327734 584 request_paste(inst);
2a2c1973 585 return TRUE;
586 }
587
3ec1de33 588 special = FALSE;
f5dd8adb 589 use_ucsoutput = FALSE;
3ec1de33 590
a57cd64b 591 /* ALT+things gives leading Escape. */
592 output[0] = '\033';
593 strncpy(output+1, event->string, 31);
f5dd8adb 594 if (!*event->string &&
595 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
596 ucsoutput[0] = '\033';
597 ucsoutput[1] = ucsval;
598 use_ucsoutput = TRUE;
599 end = 2;
600 } else {
601 output[31] = '\0';
602 end = strlen(output);
603 }
5c0ea258 604 if (event->state & GDK_MOD1_MASK) {
605 start = 0;
606 if (end == 1) end = 0;
607 } else
608 start = 1;
a57cd64b 609
610 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
611 if (!event->string[0] && event->keyval == '`' &&
612 (event->state & GDK_CONTROL_MASK)) {
613 output[1] = '\x1C';
f5dd8adb 614 use_ucsoutput = FALSE;
a57cd64b 615 end = 2;
616 }
617
618 /* Control-Break is the same as Control-C */
619 if (event->keyval == GDK_Break &&
620 (event->state & GDK_CONTROL_MASK)) {
621 output[1] = '\003';
f5dd8adb 622 use_ucsoutput = FALSE;
a57cd64b 623 end = 2;
3ec1de33 624 special = TRUE;
625 }
626
627 /* We handle Return ourselves, because it needs to be flagged as
628 * special to ldisc. */
629 if (event->keyval == GDK_Return) {
630 output[1] = '\015';
f5dd8adb 631 use_ucsoutput = FALSE;
3ec1de33 632 end = 2;
633 special = TRUE;
a57cd64b 634 }
635
636 /* Control-2, Control-Space and Control-@ are NUL */
637 if (!event->string[0] &&
638 (event->keyval == ' ' || event->keyval == '2' ||
639 event->keyval == '@') &&
640 (event->state & (GDK_SHIFT_MASK |
641 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
642 output[1] = '\0';
f5dd8adb 643 use_ucsoutput = FALSE;
a57cd64b 644 end = 2;
645 }
646
647 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
648 if (!event->string[0] && event->keyval == ' ' &&
649 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
650 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
651 output[1] = '\240';
f5dd8adb 652 use_ucsoutput = FALSE;
a57cd64b 653 end = 2;
654 }
655
656 /* We don't let GTK tell us what Backspace is! We know better. */
657 if (event->keyval == GDK_BackSpace &&
658 !(event->state & GDK_SHIFT_MASK)) {
3ea863a3 659 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
f5dd8adb 660 use_ucsoutput = FALSE;
a57cd64b 661 end = 2;
3ec1de33 662 special = TRUE;
a57cd64b 663 }
e8e8d6e2 664 /* For Shift Backspace, do opposite of what is configured. */
665 if (event->keyval == GDK_BackSpace &&
666 (event->state & GDK_SHIFT_MASK)) {
3ea863a3 667 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
f5dd8adb 668 use_ucsoutput = FALSE;
e8e8d6e2 669 end = 2;
3ec1de33 670 special = TRUE;
e8e8d6e2 671 }
a57cd64b 672
673 /* Shift-Tab is ESC [ Z */
674 if (event->keyval == GDK_ISO_Left_Tab ||
675 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
676 end = 1 + sprintf(output+1, "\033[Z");
f5dd8adb 677 use_ucsoutput = FALSE;
a57cd64b 678 }
679
680 /*
8b1fcd56 681 * NetHack keypad mode.
682 */
3ea863a3 683 if (inst->cfg.nethack_keypad) {
8b1fcd56 684 char *keys = NULL;
685 switch (event->keyval) {
686 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
687 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
688 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
689 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
690 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
691 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
692 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
693 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
694 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
695 }
696 if (keys) {
697 end = 2;
698 if (event->state & GDK_SHIFT_MASK)
699 output[1] = keys[1];
700 else
701 output[1] = keys[0];
f5dd8adb 702 use_ucsoutput = FALSE;
8b1fcd56 703 goto done;
704 }
705 }
706
707 /*
a57cd64b 708 * Application keypad mode.
709 */
3ea863a3 710 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
a57cd64b 711 int xkey = 0;
712 switch (event->keyval) {
713 case GDK_Num_Lock: xkey = 'P'; break;
714 case GDK_KP_Divide: xkey = 'Q'; break;
715 case GDK_KP_Multiply: xkey = 'R'; break;
716 case GDK_KP_Subtract: xkey = 'S'; break;
717 /*
718 * Keypad + is tricky. It covers a space that would
719 * be taken up on the VT100 by _two_ keys; so we
720 * let Shift select between the two. Worse still,
721 * in xterm function key mode we change which two...
722 */
723 case GDK_KP_Add:
5789bc94 724 if (inst->cfg.funky_type == FUNKY_XTERM) {
a57cd64b 725 if (event->state & GDK_SHIFT_MASK)
726 xkey = 'l';
727 else
728 xkey = 'k';
729 } else if (event->state & GDK_SHIFT_MASK)
730 xkey = 'm';
731 else
732 xkey = 'l';
733 break;
734 case GDK_KP_Enter: xkey = 'M'; break;
735 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
736 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
737 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
738 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
739 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
740 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
741 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
742 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
743 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
744 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
745 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
746 }
747 if (xkey) {
5def7522 748 if (inst->term->vt52_mode) {
a57cd64b 749 if (xkey >= 'P' && xkey <= 'S')
750 end = 1 + sprintf(output+1, "\033%c", xkey);
751 else
752 end = 1 + sprintf(output+1, "\033?%c", xkey);
753 } else
754 end = 1 + sprintf(output+1, "\033O%c", xkey);
f5dd8adb 755 use_ucsoutput = FALSE;
a57cd64b 756 goto done;
757 }
758 }
759
760 /*
761 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
762 * for integer decimal nn.)
763 *
764 * We also deal with the weird ones here. Linux VCs replace F1
765 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
766 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
767 * respectively.
768 */
769 {
770 int code = 0;
771 switch (event->keyval) {
772 case GDK_F1:
773 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
774 break;
775 case GDK_F2:
776 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
777 break;
778 case GDK_F3:
779 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
780 break;
781 case GDK_F4:
782 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
783 break;
784 case GDK_F5:
785 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
786 break;
787 case GDK_F6:
788 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
789 break;
790 case GDK_F7:
791 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
792 break;
793 case GDK_F8:
794 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
795 break;
796 case GDK_F9:
797 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
798 break;
799 case GDK_F10:
800 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
801 break;
802 case GDK_F11:
803 code = 23;
804 break;
805 case GDK_F12:
806 code = 24;
807 break;
808 case GDK_F13:
809 code = 25;
810 break;
811 case GDK_F14:
812 code = 26;
813 break;
814 case GDK_F15:
815 code = 28;
816 break;
817 case GDK_F16:
818 code = 29;
819 break;
820 case GDK_F17:
821 code = 31;
822 break;
823 case GDK_F18:
824 code = 32;
825 break;
826 case GDK_F19:
827 code = 33;
828 break;
829 case GDK_F20:
830 code = 34;
831 break;
832 }
833 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
834 case GDK_Home: case GDK_KP_Home:
835 code = 1;
836 break;
837 case GDK_Insert: case GDK_KP_Insert:
838 code = 2;
839 break;
840 case GDK_Delete: case GDK_KP_Delete:
841 code = 3;
842 break;
843 case GDK_End: case GDK_KP_End:
844 code = 4;
845 break;
846 case GDK_Page_Up: case GDK_KP_Page_Up:
847 code = 5;
848 break;
849 case GDK_Page_Down: case GDK_KP_Page_Down:
850 code = 6;
851 break;
852 }
853 /* Reorder edit keys to physical order */
5789bc94 854 if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
a57cd64b 855 code = "\0\2\1\4\5\3\6"[code];
856
5def7522 857 if (inst->term->vt52_mode && code > 0 && code <= 6) {
a57cd64b 858 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
f5dd8adb 859 use_ucsoutput = FALSE;
a57cd64b 860 goto done;
861 }
862
5789bc94 863 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
a57cd64b 864 code >= 11 && code <= 34) {
865 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
866 int index = 0;
867 switch (event->keyval) {
868 case GDK_F1: index = 0; break;
869 case GDK_F2: index = 1; break;
870 case GDK_F3: index = 2; break;
871 case GDK_F4: index = 3; break;
872 case GDK_F5: index = 4; break;
873 case GDK_F6: index = 5; break;
874 case GDK_F7: index = 6; break;
875 case GDK_F8: index = 7; break;
876 case GDK_F9: index = 8; break;
877 case GDK_F10: index = 9; break;
878 case GDK_F11: index = 10; break;
879 case GDK_F12: index = 11; break;
880 }
881 if (event->state & GDK_SHIFT_MASK) index += 12;
882 if (event->state & GDK_CONTROL_MASK) index += 24;
883 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
f5dd8adb 884 use_ucsoutput = FALSE;
a57cd64b 885 goto done;
886 }
5789bc94 887 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
a57cd64b 888 code >= 1 && code <= 6) {
889 char codes[] = "HL.FIG";
890 if (code == 3) {
891 output[1] = '\x7F';
892 end = 2;
893 } else {
894 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
895 }
f5dd8adb 896 use_ucsoutput = FALSE;
a57cd64b 897 goto done;
898 }
5789bc94 899 if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
887035a5 900 code >= 11 && code <= 24) {
a57cd64b 901 int offt = 0;
902 if (code > 15)
903 offt++;
904 if (code > 21)
905 offt++;
5def7522 906 if (inst->term->vt52_mode)
a57cd64b 907 end = 1 + sprintf(output+1,
908 "\x1B%c", code + 'P' - 11 - offt);
909 else
910 end = 1 + sprintf(output+1,
911 "\x1BO%c", code + 'P' - 11 - offt);
f5dd8adb 912 use_ucsoutput = FALSE;
a57cd64b 913 goto done;
914 }
5789bc94 915 if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
a57cd64b 916 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
f5dd8adb 917 use_ucsoutput = FALSE;
a57cd64b 918 goto done;
919 }
5789bc94 920 if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
5def7522 921 if (inst->term->vt52_mode)
a57cd64b 922 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
923 else
924 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
f5dd8adb 925 use_ucsoutput = FALSE;
a57cd64b 926 goto done;
927 }
3ea863a3 928 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
a57cd64b 929 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
f5dd8adb 930 use_ucsoutput = FALSE;
a57cd64b 931 goto done;
932 }
933 if (code) {
934 end = 1 + sprintf(output+1, "\x1B[%d~", code);
f5dd8adb 935 use_ucsoutput = FALSE;
a57cd64b 936 goto done;
937 }
938 }
939
940 /*
941 * Cursor keys. (This includes the numberpad cursor keys,
942 * if we haven't already done them due to app keypad mode.)
943 *
944 * Here we also process un-numlocked un-appkeypadded KP5,
945 * which sends ESC [ G.
946 */
947 {
948 int xkey = 0;
949 switch (event->keyval) {
950 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
951 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
952 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
953 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
954 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
955 }
956 if (xkey) {
957 /*
958 * The arrow keys normally do ESC [ A and so on. In
959 * app cursor keys mode they do ESC O A instead.
960 * Ctrl toggles the two modes.
961 */
5def7522 962 if (inst->term->vt52_mode) {
a57cd64b 963 end = 1 + sprintf(output+1, "\033%c", xkey);
5def7522 964 } else if (!inst->term->app_cursor_keys ^
a57cd64b 965 !(event->state & GDK_CONTROL_MASK)) {
966 end = 1 + sprintf(output+1, "\033O%c", xkey);
967 } else {
968 end = 1 + sprintf(output+1, "\033[%c", xkey);
969 }
f5dd8adb 970 use_ucsoutput = FALSE;
a57cd64b 971 goto done;
972 }
973 }
c33c3d76 974 goto done;
975 }
a57cd64b 976
c33c3d76 977 done:
a57cd64b 978
c33c3d76 979 if (end-start > 0) {
a57cd64b 980#ifdef KEY_DEBUGGING
c33c3d76 981 int i;
982 printf("generating sequence:");
983 for (i = start; i < end; i++)
984 printf(" %02x", (unsigned char) output[i]);
985 printf("\n");
a57cd64b 986#endif
c33c3d76 987
3ec1de33 988 if (special) {
989 /*
990 * For special control characters, the character set
991 * should never matter.
992 */
993 output[end] = '\0'; /* NUL-terminate */
e3be8de5 994 if (inst->ldisc)
995 ldisc_send(inst->ldisc, output+start, -2, 1);
3ec1de33 996 } else if (!inst->direct_to_font) {
f5dd8adb 997 if (!use_ucsoutput) {
998 /*
999 * The stuff we've just generated is assumed to be
1000 * ISO-8859-1! This sounds insane, but `man
1001 * XLookupString' agrees: strings of this type
1002 * returned from the X server are hardcoded to
1003 * 8859-1. Strictly speaking we should be doing
1004 * this using some sort of GtkIMContext, which (if
1005 * we're lucky) would give us our data directly in
1006 * Unicode; but that's not supported in GTK 1.2 as
1007 * far as I can tell, and it's poorly documented
1008 * even in 2.0, so it'll have to wait.
1009 */
e3be8de5 1010 if (inst->ldisc)
1011 lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
1012 end-start, 1);
f5dd8adb 1013 } else {
1014 /*
1015 * We generated our own Unicode key data from the
1016 * keysym, so use that instead.
1017 */
e3be8de5 1018 if (inst->ldisc)
1019 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
f5dd8adb 1020 }
facd762c 1021 } else {
1022 /*
1023 * In direct-to-font mode, we just send the string
1024 * exactly as we received it.
1025 */
e3be8de5 1026 if (inst->ldisc)
1027 ldisc_send(inst->ldisc, output+start, end-start, 1);
facd762c 1028 }
2dc6356a 1029
a8327734 1030 show_mouseptr(inst, 0);
5def7522 1031 term_seen_key_event(inst->term);
1709795f 1032 }
1033
1034 return TRUE;
f7f27309 1035}
1036
e6346999 1037gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1038{
1039 struct gui_data *inst = (struct gui_data *)data;
1040 int shift, ctrl, alt, x, y, button, act;
1041
d0a039b1 1042 /* Remember the timestamp. */
1043 inst->input_event_time = event->time;
1044
a8327734 1045 show_mouseptr(inst, 1);
57e636ca 1046
bab6e572 1047 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
5def7522 1048 term_scroll(inst->term, 0, -5);
bab6e572 1049 return TRUE;
1050 }
1051 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
5def7522 1052 term_scroll(inst->term, 0, +5);
bab6e572 1053 return TRUE;
1054 }
1055
e6346999 1056 shift = event->state & GDK_SHIFT_MASK;
1057 ctrl = event->state & GDK_CONTROL_MASK;
1058 alt = event->state & GDK_MOD1_MASK;
47e4e735 1059
1060 if (event->button == 3 && ctrl) {
1061 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1062 event->button, event->time);
1063 return TRUE;
1064 }
1065
e6346999 1066 if (event->button == 1)
1067 button = MBT_LEFT;
1068 else if (event->button == 2)
1069 button = MBT_MIDDLE;
1070 else if (event->button == 3)
1071 button = MBT_RIGHT;
1072 else
1073 return FALSE; /* don't even know what button! */
1074
1075 switch (event->type) {
1076 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1077 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1078 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1079 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1080 default: return FALSE; /* don't know this event type */
1081 }
1082
3ea863a3 1083 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
e6346999 1084 act != MA_CLICK && act != MA_RELEASE)
1085 return TRUE; /* we ignore these in raw mouse mode */
1086
3ea863a3 1087 x = (event->x - inst->cfg.window_border) / inst->font_width;
1088 y = (event->y - inst->cfg.window_border) / inst->font_height;
e6346999 1089
fc5b0934 1090 term_mouse(inst->term, button, translate_button(button), act,
1091 x, y, shift, ctrl, alt);
e6346999 1092
1093 return TRUE;
1094}
1095
1096gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1097{
1098 struct gui_data *inst = (struct gui_data *)data;
1099 int shift, ctrl, alt, x, y, button;
1100
d0a039b1 1101 /* Remember the timestamp. */
1102 inst->input_event_time = event->time;
1103
a8327734 1104 show_mouseptr(inst, 1);
57e636ca 1105
e6346999 1106 shift = event->state & GDK_SHIFT_MASK;
1107 ctrl = event->state & GDK_CONTROL_MASK;
1108 alt = event->state & GDK_MOD1_MASK;
1109 if (event->state & GDK_BUTTON1_MASK)
1110 button = MBT_LEFT;
1111 else if (event->state & GDK_BUTTON2_MASK)
1112 button = MBT_MIDDLE;
1113 else if (event->state & GDK_BUTTON3_MASK)
1114 button = MBT_RIGHT;
1115 else
1116 return FALSE; /* don't even know what button! */
1117
3ea863a3 1118 x = (event->x - inst->cfg.window_border) / inst->font_width;
1119 y = (event->y - inst->cfg.window_border) / inst->font_height;
e6346999 1120
fc5b0934 1121 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1122 x, y, shift, ctrl, alt);
e6346999 1123
1124 return TRUE;
1125}
1126
b9d7bcad 1127void frontend_keypress(void *handle)
90cfd8f4 1128{
b9d7bcad 1129 struct gui_data *inst = (struct gui_data *)handle;
1130
90cfd8f4 1131 /*
1132 * If our child process has exited but not closed, terminate on
1133 * any keypress.
1134 */
1135 if (inst->exited)
1136 exit(0);
1137}
1138
39934deb 1139void notify_remote_exit(void *frontend)
f7f27309 1140{
39934deb 1141 struct gui_data *inst = (struct gui_data *)frontend;
74aca06d 1142 int exitcode;
a0e16eb1 1143
3f935d5b 1144 if (!inst->exited &&
1145 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
74aca06d 1146 inst->exited = TRUE;
1147 if (inst->cfg.close_on_exit == FORCE_ON ||
1148 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1149 exit(0); /* just go. */
e3be8de5 1150 if (inst->ldisc) {
1151 ldisc_free(inst->ldisc);
1152 inst->ldisc = NULL;
1153 }
1154 if (inst->back) {
1155 inst->back->free(inst->backhandle);
1156 inst->backhandle = NULL;
1157 inst->back = NULL;
1158 update_specials_menu(inst);
1159 }
1160 gtk_widget_show(inst->restartitem);
a0e16eb1 1161 }
39934deb 1162}
f7f27309 1163
39934deb 1164static gint timer_trigger(gpointer data)
1165{
1166 long now = GPOINTER_TO_INT(data);
1167 long next;
1168 long ticks;
1169
1170 if (run_timers(now, &next)) {
1171 ticks = next - GETTICKCOUNT();
1172 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1173 GINT_TO_POINTER(next));
1174 }
1175
1176 /*
1177 * Never let a timer resume. If we need another one, we've
1178 * asked for it explicitly above.
1179 */
1180 return FALSE;
1181}
1182
1183void timer_change_notify(long next)
1184{
1185 long ticks;
1186
1187 if (timer_id)
1188 gtk_timeout_remove(timer_id);
1189
1190 ticks = next - GETTICKCOUNT();
1191 if (ticks <= 0)
1192 ticks = 1; /* just in case */
1193
1194 timer_id = gtk_timeout_add(ticks, timer_trigger,
1195 GINT_TO_POINTER(next));
f7f27309 1196}
1197
74aca06d 1198void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
054d8535 1199{
3f935d5b 1200 /*
1201 * We must process exceptional notifications before ordinary
1202 * readability ones, or we may go straight past the urgent
1203 * marker.
1204 */
1205 if (condition & GDK_INPUT_EXCEPTION)
1206 select_result(sourcefd, 4);
1207 if (condition & GDK_INPUT_READ)
1208 select_result(sourcefd, 1);
1209 if (condition & GDK_INPUT_WRITE)
1210 select_result(sourcefd, 2);
054d8535 1211}
1212
f7f27309 1213void destroy(GtkWidget *widget, gpointer data)
1214{
1215 gtk_main_quit();
1216}
1217
1218gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1219{
a8327734 1220 struct gui_data *inst = (struct gui_data *)data;
5def7522 1221 inst->term->has_focus = event->in;
5def7522 1222 term_update(inst->term);
a8327734 1223 show_mouseptr(inst, 1);
1709795f 1224 return FALSE;
1225}
1226
1227/*
1228 * set or clear the "raw mouse message" mode
1229 */
a8327734 1230void set_raw_mouse_mode(void *frontend, int activate)
1709795f 1231{
a8327734 1232 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1233 activate = activate && !inst->cfg.no_mouse_rep;
d64838e1 1234 send_raw_mouse = activate;
1235 if (send_raw_mouse)
57e636ca 1236 inst->currcursor = inst->rawcursor;
d64838e1 1237 else
57e636ca 1238 inst->currcursor = inst->textcursor;
a8327734 1239 show_mouseptr(inst, inst->mouseptr_visible);
1709795f 1240}
1241
a8327734 1242void request_resize(void *frontend, int w, int h)
1709795f 1243{
a8327734 1244 struct gui_data *inst = (struct gui_data *)frontend;
51b13830 1245 int large_x, large_y;
1246 int offset_x, offset_y;
1247 int area_x, area_y;
1248 GtkRequisition inner, outer;
1249
1250 /*
1251 * This is a heinous hack dreamed up by the gnome-terminal
1252 * people to get around a limitation in gtk. The problem is
1253 * that in order to set the size correctly we really need to be
1254 * calling gtk_window_resize - but that needs to know the size
1255 * of the _whole window_, not the drawing area. So what we do
1256 * is to set an artificially huge size request on the drawing
1257 * area, recompute the resulting size request on the window,
1258 * and look at the difference between the two. That gives us
1259 * the x and y offsets we need to translate drawing area size
1260 * into window size for real, and then we call
1261 * gtk_window_resize.
1262 */
1263
1264 /*
1265 * We start by retrieving the current size of the whole window.
1266 * Adding a bit to _that_ will give us a value we can use as a
1267 * bogus size request which guarantees to be bigger than the
1268 * current size of the drawing area.
1269 */
a8327734 1270 get_window_pixels(inst, &large_x, &large_y);
51b13830 1271 large_x += 32;
1272 large_y += 32;
1273
1274#if GTK_CHECK_VERSION(2,0,0)
1275 gtk_widget_set_size_request(inst->area, large_x, large_y);
1276#else
1277 gtk_widget_set_usize(inst->area, large_x, large_y);
1278#endif
1279 gtk_widget_size_request(inst->area, &inner);
1280 gtk_widget_size_request(inst->window, &outer);
1281
1282 offset_x = outer.width - inner.width;
1283 offset_y = outer.height - inner.height;
1284
3ea863a3 1285 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1286 area_y = inst->font_height * h + 2*inst->cfg.window_border;
51b13830 1287
1288 /*
1289 * Now we must set the size request on the drawing area back to
1290 * something sensible before we commit the real resize. Best
1291 * way to do this, I think, is to set it to what the size is
1292 * really going to end up being.
1293 */
1294#if GTK_CHECK_VERSION(2,0,0)
1295 gtk_widget_set_size_request(inst->area, area_x, area_y);
1296#else
1297 gtk_widget_set_usize(inst->area, area_x, area_y);
56b9b9a7 1298 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
51b13830 1299#endif
1300
56b9b9a7 1301 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1302
51b13830 1303#if GTK_CHECK_VERSION(2,0,0)
1304 gtk_window_resize(GTK_WINDOW(inst->window),
1305 area_x + offset_x, area_y + offset_y);
1306#else
1307 gdk_window_resize(inst->window->window,
1308 area_x + offset_x, area_y + offset_y);
1309#endif
1709795f 1310}
1311
a8327734 1312static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
26b8e7cc 1313{
1314 gboolean success[1];
1315
1316 inst->cols[n].red = r * 0x0101;
1317 inst->cols[n].green = g * 0x0101;
1318 inst->cols[n].blue = b * 0x0101;
1319
56b9b9a7 1320 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
26b8e7cc 1321 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1322 FALSE, FALSE, success);
1323 if (!success[0])
a4e8ffb0 1324 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
26b8e7cc 1325 n, r, g, b);
1326}
1327
a8327734 1328void set_window_background(struct gui_data *inst)
7428c509 1329{
1330 if (inst->area && inst->area->window)
1331 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1332 if (inst->window && inst->window->window)
1333 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1334}
1335
a8327734 1336void palette_set(void *frontend, int n, int r, int g, int b)
1709795f 1337{
a8327734 1338 struct gui_data *inst = (struct gui_data *)frontend;
26b8e7cc 1339 static const int first[21] = {
1340 0, 2, 4, 6, 8, 10, 12, 14,
1341 1, 3, 5, 7, 9, 11, 13, 15,
1342 16, 17, 18, 20, 22
1343 };
a8327734 1344 real_palette_set(inst, first[n], r, g, b);
26b8e7cc 1345 if (first[n] >= 18)
a8327734 1346 real_palette_set(inst, first[n] + 1, r, g, b);
7428c509 1347 if (first[n] == 18)
a8327734 1348 set_window_background(inst);
1709795f 1349}
26b8e7cc 1350
a8327734 1351void palette_reset(void *frontend)
1709795f 1352{
a8327734 1353 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1354 /* This maps colour indices in inst->cfg to those used in inst->cols. */
26b8e7cc 1355 static const int ww[] = {
1356 6, 7, 8, 9, 10, 11, 12, 13,
1357 14, 15, 16, 17, 18, 19, 20, 21,
1358 0, 1, 2, 3, 4, 5
1359 };
1360 gboolean success[NCOLOURS];
1361 int i;
1362
1363 assert(lenof(ww) == NCOLOURS);
1364
1365 if (!inst->colmap) {
1366 inst->colmap = gdk_colormap_get_system();
1367 } else {
1368 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1369 }
1370
1371 for (i = 0; i < NCOLOURS; i++) {
3ea863a3 1372 inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
1373 inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
1374 inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
26b8e7cc 1375 }
1376
1377 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1378 FALSE, FALSE, success);
1379 for (i = 0; i < NCOLOURS; i++) {
1380 if (!success[i])
a4e8ffb0 1381 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1382 appname, i, inst->cfg.colours[i][0],
1383 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
26b8e7cc 1384 }
7428c509 1385
a8327734 1386 set_window_background(inst);
1709795f 1387}
1388
3aac0d35 1389/* Ensure that all the cut buffers exist - according to the ICCCM, we must
1390 * do this before we start using cut buffers.
1391 */
1392void init_cutbuffers()
1393{
1394 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1395 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, "", 0);
1396 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1397 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, "", 0);
1398 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1399 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, "", 0);
1400 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1401 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, "", 0);
1402 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1403 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, "", 0);
1404 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1405 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, "", 0);
1406 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1407 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, "", 0);
1408 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1409 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, "", 0);
1410}
1411
1412/* Store the data in a cut-buffer. */
1413void store_cutbuffer(char * ptr, int len)
1414{
1415 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1416 XRotateBuffers(GDK_DISPLAY(), 1);
1417 XStoreBytes(GDK_DISPLAY(), ptr, len);
1418}
1419
1420/* Retrieve data from a cut-buffer.
1421 * Returned data needs to be freed with XFree().
1422 */
1423char * retrieve_cutbuffer(int * nbytes)
1424{
1425 char * ptr;
1426 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
05971030 1427 if (*nbytes <= 0 && ptr != 0) {
3aac0d35 1428 XFree(ptr);
1429 ptr = 0;
1430 }
1431 return ptr;
1432}
1433
a8327734 1434void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1709795f 1435{
a8327734 1436 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1437 if (inst->pasteout_data)
1438 sfree(inst->pasteout_data);
48f1bc44 1439 if (inst->pasteout_data_ctext)
1440 sfree(inst->pasteout_data_ctext);
2dc6356a 1441 if (inst->pasteout_data_utf8)
1442 sfree(inst->pasteout_data_utf8);
1443
facd762c 1444 /*
48f1bc44 1445 * Set up UTF-8 and compound text paste data. This only happens
1446 * if we aren't in direct-to-font mode using the D800 hack.
facd762c 1447 */
085f4a68 1448 if (!inst->direct_to_font) {
2dc6356a 1449 wchar_t *tmp = data;
1450 int tmplen = len;
48f1bc44 1451 XTextProperty tp;
1452 char *list[1];
facd762c 1453
3d88e64d 1454 inst->pasteout_data_utf8 = snewn(len*6, char);
facd762c 1455 inst->pasteout_data_utf8_len = len*6;
2dc6356a 1456 inst->pasteout_data_utf8_len =
1457 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1458 inst->pasteout_data_utf8_len,
1459 CS_UTF8, NULL, NULL, 0);
085f4a68 1460 if (inst->pasteout_data_utf8_len == 0) {
1461 sfree(inst->pasteout_data_utf8);
1462 inst->pasteout_data_utf8 = NULL;
1463 } else {
1464 inst->pasteout_data_utf8 =
3d88e64d 1465 sresize(inst->pasteout_data_utf8,
48f1bc44 1466 inst->pasteout_data_utf8_len + 1, char);
1467 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1468 }
1469
1470 /*
1471 * Now let Xlib convert our UTF-8 data into compound text.
1472 */
1473 list[0] = inst->pasteout_data_utf8;
1474 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1475 XCompoundTextStyle, &tp) == 0) {
1476 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1477 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1478 inst->pasteout_data_ctext_len = tp.nitems;
1479 XFree(tp.value);
d1929fa4 1480 } else {
1481 inst->pasteout_data_ctext = NULL;
1482 inst->pasteout_data_ctext_len = 0;
1483 }
facd762c 1484 } else {
1485 inst->pasteout_data_utf8 = NULL;
1486 inst->pasteout_data_utf8_len = 0;
48f1bc44 1487 inst->pasteout_data_ctext = NULL;
1488 inst->pasteout_data_ctext_len = 0;
2dc6356a 1489 }
1490
3d88e64d 1491 inst->pasteout_data = snewn(len*6, char);
085f4a68 1492 inst->pasteout_data_len = len*6;
21d2b241 1493 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1494 data, len, inst->pasteout_data,
1495 inst->pasteout_data_len,
1496 NULL, NULL, NULL);
085f4a68 1497 if (inst->pasteout_data_len == 0) {
1498 sfree(inst->pasteout_data);
1499 inst->pasteout_data = NULL;
1500 } else {
1501 inst->pasteout_data =
3d88e64d 1502 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
085f4a68 1503 }
e6346999 1504
3aac0d35 1505 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1506
e6346999 1507 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1508 inst->input_event_time)) {
e6346999 1509 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1510 GDK_SELECTION_TYPE_STRING, 1);
48f1bc44 1511 if (inst->pasteout_data_ctext)
1512 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1513 compound_text_atom, 1);
facd762c 1514 if (inst->pasteout_data_utf8)
1515 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
8c161662 1516 utf8_string_atom, 1);
e6346999 1517 }
c076966d 1518
1519 if (must_deselect)
1520 term_deselect(inst->term);
e6346999 1521}
1522
1523void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1524 guint info, guint time_stamp, gpointer data)
1525{
a8327734 1526 struct gui_data *inst = (struct gui_data *)data;
8c161662 1527 if (seldata->target == utf8_string_atom)
2dc6356a 1528 gtk_selection_data_set(seldata, seldata->target, 8,
1529 inst->pasteout_data_utf8,
1530 inst->pasteout_data_utf8_len);
48f1bc44 1531 else if (seldata->target == compound_text_atom)
1532 gtk_selection_data_set(seldata, seldata->target, 8,
1533 inst->pasteout_data_ctext,
1534 inst->pasteout_data_ctext_len);
2dc6356a 1535 else
1536 gtk_selection_data_set(seldata, seldata->target, 8,
1537 inst->pasteout_data, inst->pasteout_data_len);
e6346999 1538}
1539
1540gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1541 gpointer data)
1542{
a8327734 1543 struct gui_data *inst = (struct gui_data *)data;
d0a039b1 1544
5def7522 1545 term_deselect(inst->term);
e6346999 1546 if (inst->pasteout_data)
1547 sfree(inst->pasteout_data);
48f1bc44 1548 if (inst->pasteout_data_ctext)
1549 sfree(inst->pasteout_data_ctext);
2dc6356a 1550 if (inst->pasteout_data_utf8)
1551 sfree(inst->pasteout_data_utf8);
e6346999 1552 inst->pasteout_data = NULL;
1553 inst->pasteout_data_len = 0;
48f1bc44 1554 inst->pasteout_data_ctext = NULL;
1555 inst->pasteout_data_ctext_len = 0;
2dc6356a 1556 inst->pasteout_data_utf8 = NULL;
1557 inst->pasteout_data_utf8_len = 0;
e6346999 1558 return TRUE;
1559}
1560
a8327734 1561void request_paste(void *frontend)
e6346999 1562{
a8327734 1563 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1564 /*
1565 * In Unix, pasting is asynchronous: all we can do at the
1566 * moment is to call gtk_selection_convert(), and when the data
1567 * comes back _then_ we can call term_do_paste().
1568 */
2dc6356a 1569
085f4a68 1570 if (!inst->direct_to_font) {
facd762c 1571 /*
1572 * First we attempt to retrieve the selection as a UTF-8
1573 * string (which we will convert to the correct code page
1574 * before sending to the session, of course). If that
1575 * fails, selection_received() will be informed and will
1576 * fall back to an ordinary string.
1577 */
1578 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1579 utf8_string_atom,
1580 inst->input_event_time);
facd762c 1581 } else {
1582 /*
1583 * If we're in direct-to-font mode, we disable UTF-8
1584 * pasting, and go straight to ordinary string data.
1585 */
1586 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1587 GDK_SELECTION_TYPE_STRING,
1588 inst->input_event_time);
facd762c 1589 }
e6346999 1590}
1591
0f660c8f 1592gint idle_paste_func(gpointer data); /* forward ref */
1593
e6346999 1594void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
27651070 1595 guint time, gpointer data)
e6346999 1596{
a8327734 1597 struct gui_data *inst = (struct gui_data *)data;
48f1bc44 1598 XTextProperty tp;
1599 char **list;
1600 char *text;
1601 int length, count, ret;
1602 int free_list_required = 0;
3aac0d35 1603 int free_required = 0;
48f1bc44 1604 int charset;
a8327734 1605
8c161662 1606 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
2dc6356a 1607 /*
eaa07514 1608 * Failed to get a UTF-8 selection string. Try compound
1609 * text next.
1610 */
1611 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1612 compound_text_atom,
1613 inst->input_event_time);
eaa07514 1614 return;
1615 }
1616
1617 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1618 /*
1619 * Failed to get UTF-8 or compound text. Try an ordinary
2dc6356a 1620 * string.
1621 */
1622 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1623 GDK_SELECTION_TYPE_STRING,
1624 inst->input_event_time);
2dc6356a 1625 return;
1626 }
1627
1628 /*
3aac0d35 1629 * If we have data, but it's not of a type we can deal with,
1630 * we have to ignore the data.
2dc6356a 1631 */
3aac0d35 1632 if (seldata->length > 0 &&
1633 seldata->type != GDK_SELECTION_TYPE_STRING &&
1634 seldata->type != compound_text_atom &&
1635 seldata->type != utf8_string_atom)
1636 return;
48f1bc44 1637
1638 /*
3aac0d35 1639 * If we have no data, try looking in a cut buffer.
1640 */
1641 if (seldata->length <= 0) {
1642 text = retrieve_cutbuffer(&length);
1643 if (length == 0)
1644 return;
1645 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1646 * source, so use that as a de-facto standard. */
1647 charset = CS_ISO8859_1;
1648 free_required = 1;
1649 } else {
76129c71 1650 /*
1651 * Convert COMPOUND_TEXT into UTF-8.
1652 */
1653 if (seldata->type == compound_text_atom) {
1654 tp.value = seldata->data;
1655 tp.encoding = (Atom) seldata->type;
1656 tp.format = seldata->format;
1657 tp.nitems = seldata->length;
1658 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1659 &list, &count);
1660 if (ret != 0 || count != 1) {
1661 /*
1662 * Compound text failed; fall back to STRING.
1663 */
1664 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1665 GDK_SELECTION_TYPE_STRING,
1666 inst->input_event_time);
1667 return;
1668 }
1669 text = list[0];
1670 length = strlen(list[0]);
1671 charset = CS_UTF8;
1672 free_list_required = 1;
1673 } else {
1674 text = (char *)seldata->data;
1675 length = seldata->length;
1676 charset = (seldata->type == utf8_string_atom ?
1677 CS_UTF8 : inst->ucsdata.line_codepage);
48f1bc44 1678 }
3aac0d35 1679 }
48f1bc44 1680
e6346999 1681 if (inst->pastein_data)
1682 sfree(inst->pastein_data);
1683
48f1bc44 1684 inst->pastein_data = snewn(length, wchar_t);
1685 inst->pastein_data_len = length;
2dc6356a 1686 inst->pastein_data_len =
48f1bc44 1687 mb_to_wc(charset, 0, text, length,
2dc6356a 1688 inst->pastein_data, inst->pastein_data_len);
e6346999 1689
5def7522 1690 term_do_paste(inst->term);
0f660c8f 1691
5def7522 1692 if (term_paste_pending(inst->term))
0f660c8f 1693 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
48f1bc44 1694
1695 if (free_list_required)
1696 XFreeStringList(list);
3aac0d35 1697 if (free_required)
1698 XFree(text);
0f660c8f 1699}
1700
1701gint idle_paste_func(gpointer data)
1702{
1703 struct gui_data *inst = (struct gui_data *)data;
1704
5def7522 1705 if (term_paste_pending(inst->term))
1706 term_paste(inst->term);
0f660c8f 1707 else
1708 gtk_idle_remove(inst->term_paste_idle_id);
1709
1710 return TRUE;
1709795f 1711}
1712
0f660c8f 1713
a8327734 1714void get_clip(void *frontend, wchar_t ** p, int *len)
1709795f 1715{
a8327734 1716 struct gui_data *inst = (struct gui_data *)frontend;
1717
1709795f 1718 if (p) {
e6346999 1719 *p = inst->pastein_data;
1720 *len = inst->pastein_data_len;
1709795f 1721 }
1722}
1723
6e785e0c 1724static void set_window_titles(struct gui_data *inst)
1725{
1726 /*
1727 * We must always call set_icon_name after calling set_title,
1728 * since set_title will write both names. Irritating, but such
1729 * is life.
1730 */
1731 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1732 if (!inst->cfg.win_name_always)
1733 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1734}
1735
a8327734 1736void set_title(void *frontend, char *title)
1709795f 1737{
a8327734 1738 struct gui_data *inst = (struct gui_data *)frontend;
12d200b1 1739 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1740 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
6e785e0c 1741 set_window_titles(inst);
1709795f 1742}
1743
a8327734 1744void set_icon(void *frontend, char *title)
1709795f 1745{
a8327734 1746 struct gui_data *inst = (struct gui_data *)frontend;
16891265 1747 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1748 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
6e785e0c 1749 set_window_titles(inst);
1709795f 1750}
1751
a8327734 1752void set_sbar(void *frontend, int total, int start, int page)
1709795f 1753{
a8327734 1754 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1755 if (!inst->cfg.scrollbar)
330f49be 1756 return;
6a5e84dd 1757 inst->sbar_adjust->lower = 0;
1758 inst->sbar_adjust->upper = total;
1759 inst->sbar_adjust->value = start;
1760 inst->sbar_adjust->page_size = page;
1761 inst->sbar_adjust->step_increment = 1;
1762 inst->sbar_adjust->page_increment = page/2;
88e6b9ca 1763 inst->ignore_sbar = TRUE;
6a5e84dd 1764 gtk_adjustment_changed(inst->sbar_adjust);
88e6b9ca 1765 inst->ignore_sbar = FALSE;
6a5e84dd 1766}
1767
1768void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1769{
a8327734 1770 struct gui_data *inst = (struct gui_data *)data;
1771
3ea863a3 1772 if (!inst->cfg.scrollbar)
330f49be 1773 return;
88e6b9ca 1774 if (!inst->ignore_sbar)
5def7522 1775 term_scroll(inst->term, 1, (int)adj->value);
1709795f 1776}
1777
a8327734 1778void sys_cursor(void *frontend, int x, int y)
1709795f 1779{
1780 /*
1781 * This is meaningless under X.
1782 */
1783}
1784
0ce89525 1785/*
1786 * This is still called when mode==BELL_VISUAL, even though the
1787 * visual bell is handled entirely within terminal.c, because we
1788 * may want to perform additional actions on any kind of bell (for
1789 * example, taskbar flashing in Windows).
1790 */
a8327734 1791void beep(void *frontend, int mode)
1709795f 1792{
0ce89525 1793 if (mode != BELL_VISUAL)
1794 gdk_beep();
1709795f 1795}
1796
2102eb8a 1797int char_width(Context ctx, int uc)
1709795f 1798{
1799 /*
1800 * Under X, any fixed-width font really _is_ fixed-width.
1801 * Double-width characters will be dealt with using a separate
1802 * font. For the moment we can simply return 1.
1803 */
1804 return 1;
1805}
1806
a8327734 1807Context get_ctx(void *frontend)
1709795f 1808{
a8327734 1809 struct gui_data *inst = (struct gui_data *)frontend;
1810 struct draw_ctx *dctx;
1811
d64838e1 1812 if (!inst->area->window)
1813 return NULL;
a8327734 1814
3d88e64d 1815 dctx = snew(struct draw_ctx);
a8327734 1816 dctx->inst = inst;
1817 dctx->gc = gdk_gc_new(inst->area->window);
1818 return dctx;
1709795f 1819}
1820
1821void free_ctx(Context ctx)
1822{
a8327734 1823 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1824 /* struct gui_data *inst = dctx->inst; */
1825 GdkGC *gc = dctx->gc;
1709795f 1826 gdk_gc_unref(gc);
a8327734 1827 sfree(dctx);
1709795f 1828}
1829
1830/*
1831 * Draw a line of text in the window, at given character
1832 * coordinates, in given attributes.
1833 *
1834 * We are allowed to fiddle with the contents of `text'.
1835 */
36566009 1836void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1a675941 1837 unsigned long attr, int lattr)
1709795f 1838{
a8327734 1839 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1840 struct gui_data *inst = dctx->inst;
1841 GdkGC *gc = dctx->gc;
c6958dfe 1842 int ncombining, combining;
006238cb 1843 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
d64838e1 1844
c6958dfe 1845 if (attr & TATTR_COMBINING) {
1846 ncombining = len;
1847 len = 1;
1848 } else
1849 ncombining = 1;
1850
37ca32ed 1851 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1852 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
1853 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1854 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
d64838e1 1855 if (attr & ATTR_REVERSE) {
1856 t = nfg;
1857 nfg = nbg;
1858 nbg = t;
1859 }
3ea863a3 1860 if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
37ca32ed 1861 nfg |= 1;
3ea863a3 1862 if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
37ca32ed 1863 nbg |= 1;
d64838e1 1864 if (attr & TATTR_ACTCURS) {
1865 nfg = NCOLOURS-2;
1866 nbg = NCOLOURS-1;
1867 }
1868
5bf50ab2 1869 fontid = shadow = 0;
006238cb 1870
1871 if (attr & ATTR_WIDE) {
1872 widefactor = 2;
1873 fontid |= 2;
1874 } else {
1875 widefactor = 1;
1876 }
1877
3ea863a3 1878 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
006238cb 1879 if (inst->fonts[fontid | 1])
1880 fontid |= 1;
5bf50ab2 1881 else
1882 shadow = 1;
1883 }
1884
36566009 1885 if ((lattr & LATTR_MODE) != LATTR_NORM) {
f34df346 1886 x *= 2;
5def7522 1887 if (x >= inst->term->cols)
614bb468 1888 return;
006238cb 1889 if (x + len*2*widefactor > inst->term->cols)
1890 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
623f81b7 1891 rlen = len * 2;
1892 } else
1893 rlen = len;
1894
1895 {
1896 GdkRectangle r;
1897
3ea863a3 1898 r.x = x*inst->font_width+inst->cfg.window_border;
1899 r.y = y*inst->font_height+inst->cfg.window_border;
006238cb 1900 r.width = rlen*widefactor*inst->font_width;
623f81b7 1901 r.height = inst->font_height;
1902 gdk_gc_set_clip_rectangle(gc, &r);
f34df346 1903 }
1904
d64838e1 1905 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
83616aab 1906 gdk_draw_rectangle(inst->pixmap, gc, 1,
3ea863a3 1907 x*inst->font_width+inst->cfg.window_border,
1908 y*inst->font_height+inst->cfg.window_border,
006238cb 1909 rlen*widefactor*inst->font_width, inst->font_height);
6a5e84dd 1910
d64838e1 1911 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2dc6356a 1912 {
1913 GdkWChar *gwcs;
1914 gchar *gcs;
1915 wchar_t *wcs;
1916 int i;
1917
c6958dfe 1918 wcs = snewn(len*ncombining+1, wchar_t);
1919 for (i = 0; i < len*ncombining; i++) {
36566009 1920 wcs[i] = text[i];
2dc6356a 1921 }
1922
006238cb 1923 if (inst->fonts[fontid] == NULL) {
1924 /*
1925 * The font for this contingency does not exist.
1926 * Typically this means we've been given ATTR_WIDE
1927 * character and have no wide font. So we display
1928 * nothing at all; such is life.
1929 */
1930 } else if (inst->fontinfo[fontid].is_wide) {
aa153a7f 1931 /*
1932 * At least one version of gdk_draw_text_wc() has a
1933 * weird bug whereby it reads `len' elements of the
1934 * input string, but only draws `len/2'. Hence I'm
1935 * going to make its input array twice as long as it
1936 * theoretically needs to be, and pass in twice the
1937 * actual number of characters. If a fixed gdk actually
1938 * takes the doubled length seriously, then (a) the
1939 * array will stand scrutiny up to the full length, (b)
1940 * the spare elements of the array are full of zeroes
1941 * which will probably be an empty glyph in the font,
1942 * and (c) the clip rectangle should prevent it causing
1943 * trouble anyway.
1944 */
3d88e64d 1945 gwcs = snewn(len*2+1, GdkWChar);
aa153a7f 1946 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2dc6356a 1947 /*
1948 * FIXME: when we have a wide-char equivalent of
1949 * from_unicode, use it instead of this.
1950 */
c6958dfe 1951 for (combining = 0; combining < ncombining; combining++) {
1952 for (i = 0; i <= len; i++)
1953 gwcs[i] = wcs[i + combining];
36566009 1954 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
c6958dfe 1955 x*inst->font_width+inst->cfg.window_border,
36566009 1956 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1957 gwcs, len*2);
c6958dfe 1958 if (shadow)
1959 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
1960 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
1961 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1962 gwcs, len*2);
1963 }
2dc6356a 1964 sfree(gwcs);
1965 } else {
3d88e64d 1966 gcs = snewn(len+1, gchar);
c6958dfe 1967 for (combining = 0; combining < ncombining; combining++) {
1968 wc_to_mb(inst->fontinfo[fontid].charset, 0,
1969 wcs + combining, len, gcs, len, ".", NULL, NULL);
36566009 1970 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
c6958dfe 1971 x*inst->font_width+inst->cfg.window_border,
36566009 1972 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1973 gcs, len);
c6958dfe 1974 if (shadow)
1975 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1976 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
1977 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1978 gcs, len);
1979 }
2dc6356a 1980 sfree(gcs);
1981 }
1982 sfree(wcs);
1983 }
d64838e1 1984
1985 if (attr & ATTR_UNDER) {
1986 int uheight = inst->fonts[0]->ascent + 1;
83616aab 1987 if (uheight >= inst->font_height)
1988 uheight = inst->font_height - 1;
3ea863a3 1989 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
1990 y*inst->font_height + uheight + inst->cfg.window_border,
1991 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
1992 y*inst->font_height + uheight + inst->cfg.window_border);
d64838e1 1993 }
1994
36566009 1995 if ((lattr & LATTR_MODE) != LATTR_NORM) {
f34df346 1996 /*
1997 * I can't find any plausible StretchBlt equivalent in the
1998 * X server, so I'm going to do this the slow and painful
1999 * way. This will involve repeated calls to
2000 * gdk_draw_pixmap() to stretch the text horizontally. It's
2001 * O(N^2) in time and O(N) in network bandwidth, but you
2002 * try thinking of a better way. :-(
2003 */
2004 int i;
006238cb 2005 for (i = 0; i < len * widefactor * inst->font_width; i++) {
f34df346 2006 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 2007 x*inst->font_width+inst->cfg.window_border + 2*i,
2008 y*inst->font_height+inst->cfg.window_border,
2009 x*inst->font_width+inst->cfg.window_border + 2*i+1,
2010 y*inst->font_height+inst->cfg.window_border,
7a216d7f 2011 len * widefactor * inst->font_width - i, inst->font_height);
f34df346 2012 }
2013 len *= 2;
36566009 2014 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
f34df346 2015 int dt, db;
2016 /* Now stretch vertically, in the same way. */
36566009 2017 if ((lattr & LATTR_MODE) == LATTR_BOT)
f34df346 2018 dt = 0, db = 1;
2019 else
2020 dt = 1, db = 0;
2021 for (i = 0; i < inst->font_height; i+=2) {
2022 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 2023 x*inst->font_width+inst->cfg.window_border,
2024 y*inst->font_height+inst->cfg.window_border+dt*i+db,
7a216d7f 2025 x*inst->font_width+inst->cfg.window_border,
3ea863a3 2026 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
7a216d7f 2027 len * widefactor * inst->font_width, inst->font_height-i-1);
f34df346 2028 }
2029 }
1a675941 2030 }
2031}
2032
36566009 2033void do_text(Context ctx, int x, int y, wchar_t *text, int len,
1a675941 2034 unsigned long attr, int lattr)
2035{
a8327734 2036 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2037 struct gui_data *inst = dctx->inst;
2038 GdkGC *gc = dctx->gc;
006238cb 2039 int widefactor;
1a675941 2040
2041 do_text_internal(ctx, x, y, text, len, attr, lattr);
2042
006238cb 2043 if (attr & ATTR_WIDE) {
2044 widefactor = 2;
2045 } else {
2046 widefactor = 1;
2047 }
2048
36566009 2049 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1a675941 2050 x *= 2;
5def7522 2051 if (x >= inst->term->cols)
1a675941 2052 return;
006238cb 2053 if (x + len*2*widefactor > inst->term->cols)
2054 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
f34df346 2055 len *= 2;
2056 }
2057
d64838e1 2058 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 2059 x*inst->font_width+inst->cfg.window_border,
2060 y*inst->font_height+inst->cfg.window_border,
2061 x*inst->font_width+inst->cfg.window_border,
2062 y*inst->font_height+inst->cfg.window_border,
006238cb 2063 len*widefactor*inst->font_width, inst->font_height);
1709795f 2064}
2065
36566009 2066void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
1709795f 2067 unsigned long attr, int lattr)
2068{
a8327734 2069 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2070 struct gui_data *inst = dctx->inst;
2071 GdkGC *gc = dctx->gc;
2072
a6db1e95 2073 int active, passive, widefactor;
d64838e1 2074
1709795f 2075 if (attr & TATTR_PASCURS) {
2076 attr &= ~TATTR_PASCURS;
d64838e1 2077 passive = 1;
2078 } else
2079 passive = 0;
3ea863a3 2080 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
1a675941 2081 attr &= ~TATTR_ACTCURS;
a6db1e95 2082 active = 1;
2083 } else
2084 active = 0;
1a675941 2085 do_text_internal(ctx, x, y, text, len, attr, lattr);
2086
ac059c2e 2087 if (attr & TATTR_COMBINING)
2088 len = 1;
2089
006238cb 2090 if (attr & ATTR_WIDE) {
2091 widefactor = 2;
2092 } else {
2093 widefactor = 1;
2094 }
2095
36566009 2096 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1a675941 2097 x *= 2;
5def7522 2098 if (x >= inst->term->cols)
1a675941 2099 return;
006238cb 2100 if (x + len*2*widefactor > inst->term->cols)
2101 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1a675941 2102 len *= 2;
2103 }
2104
3ea863a3 2105 if (inst->cfg.cursor_type == 0) {
1a675941 2106 /*
2107 * An active block cursor will already have been done by
2108 * the above do_text call, so we only need to do anything
2109 * if it's passive.
2110 */
2111 if (passive) {
2112 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
2113 gdk_draw_rectangle(inst->pixmap, gc, 0,
3ea863a3 2114 x*inst->font_width+inst->cfg.window_border,
2115 y*inst->font_height+inst->cfg.window_border,
35100bc3 2116 len*widefactor*inst->font_width-1, inst->font_height-1);
1a675941 2117 }
2118 } else {
2119 int uheight;
2120 int startx, starty, dx, dy, length, i;
2121
2122 int char_width;
2123
36566009 2124 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
1a675941 2125 char_width = 2*inst->font_width;
2126 else
2127 char_width = inst->font_width;
2128
3ea863a3 2129 if (inst->cfg.cursor_type == 1) {
1a675941 2130 uheight = inst->fonts[0]->ascent + 1;
2131 if (uheight >= inst->font_height)
2132 uheight = inst->font_height - 1;
2133
3ea863a3 2134 startx = x * inst->font_width + inst->cfg.window_border;
2135 starty = y * inst->font_height + inst->cfg.window_border + uheight;
1a675941 2136 dx = 1;
2137 dy = 0;
35100bc3 2138 length = len * widefactor * char_width;
1a675941 2139 } else {
2140 int xadjust = 0;
2141 if (attr & TATTR_RIGHTCURS)
2142 xadjust = char_width - 1;
3ea863a3 2143 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2144 starty = y * inst->font_height + inst->cfg.window_border;
1a675941 2145 dx = 0;
2146 dy = 1;
2147 length = inst->font_height;
2148 }
2149
d64838e1 2150 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1a675941 2151 if (passive) {
2152 for (i = 0; i < length; i++) {
2153 if (i % 2 == 0) {
2154 gdk_draw_point(inst->pixmap, gc, startx, starty);
2155 }
2156 startx += dx;
2157 starty += dy;
2158 }
a6db1e95 2159 } else if (active) {
1a675941 2160 gdk_draw_line(inst->pixmap, gc, startx, starty,
2161 startx + (length-1) * dx, starty + (length-1) * dy);
a6db1e95 2162 } /* else no cursor (e.g., blinked off) */
d64838e1 2163 }
1a675941 2164
2165 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 2166 x*inst->font_width+inst->cfg.window_border,
2167 y*inst->font_height+inst->cfg.window_border,
2168 x*inst->font_width+inst->cfg.window_border,
2169 y*inst->font_height+inst->cfg.window_border,
006238cb 2170 len*widefactor*inst->font_width, inst->font_height);
1709795f 2171}
2172
a8327734 2173GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
7798fa56 2174{
2175 /*
2176 * Truly hideous hack: GTK doesn't allow us to set the mouse
2177 * cursor foreground and background colours unless we've _also_
2178 * created our own cursor from bitmaps. Therefore, I need to
2179 * load the `cursor' font and draw glyphs from it on to
2180 * pixmaps, in order to construct my cursors with the fg and bg
2181 * I want. This is a gross hack, but it's more self-contained
2182 * than linking in Xlib to find the X window handle to
2183 * inst->area and calling XRecolorCursor, and it's more
2184 * futureproof than hard-coding the shapes as bitmap arrays.
2185 */
2186 static GdkFont *cursor_font = NULL;
2187 GdkPixmap *source, *mask;
2188 GdkGC *gc;
2189 GdkColor cfg = { 0, 65535, 65535, 65535 };
2190 GdkColor cbg = { 0, 0, 0, 0 };
2191 GdkColor dfg = { 1, 65535, 65535, 65535 };
2192 GdkColor dbg = { 0, 0, 0, 0 };
2193 GdkCursor *ret;
2194 gchar text[2];
2195 gint lb, rb, wid, asc, desc, w, h, x, y;
2196
57e636ca 2197 if (cursor_val == -2) {
7798fa56 2198 gdk_font_unref(cursor_font);
2199 return NULL;
2200 }
2201
57e636ca 2202 if (cursor_val >= 0 && !cursor_font)
7798fa56 2203 cursor_font = gdk_font_load("cursor");
2204
2205 /*
2206 * Get the text extent of the cursor in question. We use the
2207 * mask character for this, because it's typically slightly
2208 * bigger than the main character.
2209 */
57e636ca 2210 if (cursor_val >= 0) {
2211 text[1] = '\0';
2212 text[0] = (char)cursor_val + 1;
2213 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2214 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2215 } else {
2216 w = h = 1;
2217 x = y = 0;
2218 }
7798fa56 2219
2220 source = gdk_pixmap_new(NULL, w, h, 1);
2221 mask = gdk_pixmap_new(NULL, w, h, 1);
2222
2223 /*
2224 * Draw the mask character on the mask pixmap.
2225 */
7798fa56 2226 gc = gdk_gc_new(mask);
2227 gdk_gc_set_foreground(gc, &dbg);
2228 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
57e636ca 2229 if (cursor_val >= 0) {
2230 text[1] = '\0';
2231 text[0] = (char)cursor_val + 1;
2232 gdk_gc_set_foreground(gc, &dfg);
2233 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2234 }
7798fa56 2235 gdk_gc_unref(gc);
2236
2237 /*
2238 * Draw the main character on the source pixmap.
2239 */
7798fa56 2240 gc = gdk_gc_new(source);
2241 gdk_gc_set_foreground(gc, &dbg);
2242 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
57e636ca 2243 if (cursor_val >= 0) {
2244 text[1] = '\0';
2245 text[0] = (char)cursor_val;
2246 gdk_gc_set_foreground(gc, &dfg);
2247 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2248 }
7798fa56 2249 gdk_gc_unref(gc);
2250
2251 /*
2252 * Create the cursor.
2253 */
2254 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2255
2256 /*
2257 * Clean up.
2258 */
2259 gdk_pixmap_unref(source);
2260 gdk_pixmap_unref(mask);
2261
2262 return ret;
2263}
2264
1709795f 2265void modalfatalbox(char *p, ...)
2266{
2267 va_list ap;
2268 fprintf(stderr, "FATAL ERROR: ");
2269 va_start(ap, p);
2270 vfprintf(stderr, p, ap);
2271 va_end(ap);
2272 fputc('\n', stderr);
2273 exit(1);
f7f27309 2274}
2275
46a3419b 2276void cmdline_error(char *p, ...)
2277{
2278 va_list ap;
a4e8ffb0 2279 fprintf(stderr, "%s: ", appname);
46a3419b 2280 va_start(ap, p);
2281 vfprintf(stderr, p, ap);
2282 va_end(ap);
2283 fputc('\n', stderr);
2284 exit(1);
2285}
2286
a8327734 2287char *get_x_display(void *frontend)
755a6d84 2288{
2289 return gdk_get_display();
2290}
2291
7af753e6 2292long get_windowid(void *frontend)
2293{
fbf6cb3b 2294 struct gui_data *inst = (struct gui_data *)frontend;
7af753e6 2295 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2296}
2297
96add444 2298static void help(FILE *fp) {
2299 if(fprintf(fp,
2300"pterm option summary:\n"
2301"\n"
2302" --display DISPLAY Specify X display to use (note '--')\n"
2303" -name PREFIX Prefix when looking up resources (default: pterm)\n"
2304" -fn FONT Normal text font\n"
2305" -fb FONT Bold text font\n"
aca2a702 2306" -geometry GEOMETRY Position and size of window (size in characters)\n"
96add444 2307" -sl LINES Number of lines of scrollback\n"
2308" -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2309" -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2310" -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2311" -T TITLE Window title\n"
2312" -ut, +ut Do(default) or do not update utmp\n"
2313" -ls, +ls Do(default) or do not make shell a login shell\n"
2314" -sb, +sb Do(default) or do not display a scrollbar\n"
2315" -log PATH Log all output to a file\n"
2316" -nethack Map numeric keypad to hjklyubn direction keys\n"
2317" -xrm RESOURCE-STRING Set an X resource\n"
2318" -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2319 ) < 0 || fflush(fp) < 0) {
2320 perror("output error");
2321 exit(1);
2322 }
2323}
2324
aca2a702 2325int do_cmdline(int argc, char **argv, int do_everything,
2326 struct gui_data *inst, Config *cfg)
f7f27309 2327{
6169c758 2328 int err = 0;
56801e3d 2329 char *val;
f7f27309 2330
faec60ed 2331 /*
b89ee4f3 2332 * Macros to make argument handling easier. Note that because
2333 * they need to call `continue', they cannot be contained in
2334 * the usual do {...} while (0) wrapper to make them
2335 * syntactically single statements; hence it is not legal to
2336 * use one of these macros as an unbraced statement between
2337 * `if' and `else'.
faec60ed 2338 */
b89ee4f3 2339#define EXPECTS_ARG { \
faec60ed 2340 if (--argc <= 0) { \
2341 err = 1; \
a4e8ffb0 2342 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
b89ee4f3 2343 continue; \
faec60ed 2344 } else \
2345 val = *++argv; \
b89ee4f3 2346}
2347#define SECOND_PASS_ONLY { if (!do_everything) continue; }
faec60ed 2348
6169c758 2349 while (--argc > 0) {
2350 char *p = *++argv;
46a3419b 2351 int ret;
2352
fb9bf82f 2353 /*
2354 * Shameless cheating. Debian requires all X terminal
2355 * emulators to support `-T title'; but
2356 * cmdline_process_param will eat -T (it means no-pty) and
2357 * complain that pterm doesn't support it. So, in pterm
2358 * only, we convert -T into -title.
2359 */
2360 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2361 !strcmp(p, "-T"))
2362 p = "-title";
2363
46a3419b 2364 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2365 do_everything ? 1 : -1, cfg);
2366
2367 if (ret == -2) {
2368 cmdline_error("option \"%s\" requires an argument", p);
2369 } else if (ret == 2) {
2370 --argc, ++argv; /* skip next argument */
2371 continue;
2372 } else if (ret == 1) {
2373 continue;
2374 }
2375
8e20db05 2376 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
faec60ed 2377 EXPECTS_ARG;
2378 SECOND_PASS_ONLY;
9a30e26b 2379 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2380 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
faec60ed 2381
2382 } else if (!strcmp(p, "-fb")) {
2383 EXPECTS_ARG;
2384 SECOND_PASS_ONLY;
9a30e26b 2385 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2386 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
faec60ed 2387
006238cb 2388 } else if (!strcmp(p, "-fw")) {
2389 EXPECTS_ARG;
2390 SECOND_PASS_ONLY;
9a30e26b 2391 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2392 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
006238cb 2393
2394 } else if (!strcmp(p, "-fwb")) {
2395 EXPECTS_ARG;
2396 SECOND_PASS_ONLY;
9a30e26b 2397 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2398 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
006238cb 2399
2dc6356a 2400 } else if (!strcmp(p, "-cs")) {
2401 EXPECTS_ARG;
2402 SECOND_PASS_ONLY;
3ea863a3 2403 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2404 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2dc6356a 2405
8e20db05 2406 } else if (!strcmp(p, "-geometry")) {
2407 int flags, x, y, w, h;
2408 EXPECTS_ARG;
2409 SECOND_PASS_ONLY;
2410
2411 flags = XParseGeometry(val, &x, &y, &w, &h);
2412 if (flags & WidthValue)
3ea863a3 2413 cfg->width = w;
8e20db05 2414 if (flags & HeightValue)
3ea863a3 2415 cfg->height = h;
8e20db05 2416
aca2a702 2417 if (flags & (XValue | YValue)) {
2418 inst->xpos = x;
2419 inst->ypos = y;
2420 inst->gotpos = TRUE;
2421 inst->gravity = ((flags & XNegative ? 1 : 0) |
2422 (flags & YNegative ? 2 : 0));
2423 }
8e20db05 2424
2425 } else if (!strcmp(p, "-sl")) {
2426 EXPECTS_ARG;
2427 SECOND_PASS_ONLY;
3ea863a3 2428 cfg->savelines = atoi(val);
8e20db05 2429
2430 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2431 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
b60ca991 2432 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
8e20db05 2433 GdkColor col;
2434
2435 EXPECTS_ARG;
2436 SECOND_PASS_ONLY;
2437 if (!gdk_color_parse(val, &col)) {
2438 err = 1;
a4e8ffb0 2439 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2440 appname, val);
8e20db05 2441 } else {
2442 int index;
2443 index = (!strcmp(p, "-fg") ? 0 :
2444 !strcmp(p, "-bg") ? 2 :
2445 !strcmp(p, "-bfg") ? 1 :
2446 !strcmp(p, "-bbg") ? 3 :
b60ca991 2447 !strcmp(p, "-cfg") ? 4 :
8e20db05 2448 !strcmp(p, "-cbg") ? 5 : -1);
2449 assert(index != -1);
3ea863a3 2450 cfg->colours[index][0] = col.red / 256;
2451 cfg->colours[index][1] = col.green / 256;
2452 cfg->colours[index][2] = col.blue / 256;
8e20db05 2453 }
2454
1d009ae7 2455 } else if (use_pty_argv && !strcmp(p, "-e")) {
faec60ed 2456 /* This option swallows all further arguments. */
2457 if (!do_everything)
2458 break;
2459
6169c758 2460 if (--argc > 0) {
2461 int i;
3d88e64d 2462 pty_argv = snewn(argc+1, char *);
6169c758 2463 ++argv;
2464 for (i = 0; i < argc; i++)
2465 pty_argv[i] = argv[i];
2466 pty_argv[argc] = NULL;
2467 break; /* finished command-line processing */
2468 } else
a4e8ffb0 2469 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2470 appname);
faec60ed 2471
486c8271 2472 } else if (!strcmp(p, "-title")) {
faec60ed 2473 EXPECTS_ARG;
2474 SECOND_PASS_ONLY;
3ea863a3 2475 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2476 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
faec60ed 2477
2478 } else if (!strcmp(p, "-log")) {
2479 EXPECTS_ARG;
2480 SECOND_PASS_ONLY;
9a30e26b 2481 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2482 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
3ea863a3 2483 cfg->logtype = LGTYP_DEBUG;
faec60ed 2484
8e20db05 2485 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
faec60ed 2486 SECOND_PASS_ONLY;
3ea863a3 2487 cfg->stamp_utmp = 0;
faec60ed 2488
8e20db05 2489 } else if (!strcmp(p, "-ut")) {
faec60ed 2490 SECOND_PASS_ONLY;
3ea863a3 2491 cfg->stamp_utmp = 1;
faec60ed 2492
8e20db05 2493 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
faec60ed 2494 SECOND_PASS_ONLY;
3ea863a3 2495 cfg->login_shell = 0;
faec60ed 2496
8e20db05 2497 } else if (!strcmp(p, "-ls")) {
2498 SECOND_PASS_ONLY;
3ea863a3 2499 cfg->login_shell = 1;
8e20db05 2500
faec60ed 2501 } else if (!strcmp(p, "-nethack")) {
2502 SECOND_PASS_ONLY;
3ea863a3 2503 cfg->nethack_keypad = 1;
faec60ed 2504
8e20db05 2505 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2506 SECOND_PASS_ONLY;
3ea863a3 2507 cfg->scrollbar = 0;
8e20db05 2508
2509 } else if (!strcmp(p, "-sb")) {
faec60ed 2510 SECOND_PASS_ONLY;
3ea863a3 2511 cfg->scrollbar = 0;
faec60ed 2512
2513 } else if (!strcmp(p, "-name")) {
2514 EXPECTS_ARG;
2515 app_name = val;
0ac15bdc 2516
2517 } else if (!strcmp(p, "-xrm")) {
2518 EXPECTS_ARG;
2519 provide_xrm_string(val);
2520
96add444 2521 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2522 help(stdout);
2523 exit(0);
2524
46a3419b 2525 } else if(p[0] != '-' && (!do_everything ||
2526 process_nonoption_arg(p, cfg))) {
2527 /* do nothing */
2528
edc73959 2529 } else {
2530 err = 1;
a4e8ffb0 2531 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
330f49be 2532 }
6169c758 2533 }
2534
faec60ed 2535 return err;
2536}
2537
0f33f9d1 2538static void block_signal(int sig, int block_it) {
2539 sigset_t ss;
2540
2541 sigemptyset(&ss);
2542 sigaddset(&ss, sig);
2543 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2544 perror("sigprocmask");
2545 exit(1);
2546 }
2547}
2548
facd762c 2549/*
2550 * This function retrieves the character set encoding of a font. It
2551 * returns the character set without the X11 hack (in case the user
2552 * asks to use the font's own encoding).
2553 */
2554static int set_font_info(struct gui_data *inst, int fontid)
2dc6356a 2555{
2556 GdkFont *font = inst->fonts[fontid];
2557 XFontStruct *xfs = GDK_FONT_XFONT(font);
2558 Display *disp = GDK_FONT_XDISPLAY(font);
2559 Atom charset_registry, charset_encoding;
2560 unsigned long registry_ret, encoding_ret;
facd762c 2561 int retval = CS_NONE;
2562
2dc6356a 2563 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2564 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2565 inst->fontinfo[fontid].charset = CS_NONE;
2566 inst->fontinfo[fontid].is_wide = 0;
2567 if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
2568 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2569 char *reg, *enc;
2570 reg = XGetAtomName(disp, (Atom)registry_ret);
2571 enc = XGetAtomName(disp, (Atom)encoding_ret);
2572 if (reg && enc) {
2573 char *encoding = dupcat(reg, "-", enc, NULL);
facd762c 2574 retval = inst->fontinfo[fontid].charset =
2575 charset_from_xenc(encoding);
2dc6356a 2576 /* FIXME: when libcharset supports wide encodings fix this. */
facd762c 2577 if (!strcasecmp(encoding, "iso10646-1")) {
2dc6356a 2578 inst->fontinfo[fontid].is_wide = 1;
facd762c 2579 retval = CS_UTF8;
2580 }
2dc6356a 2581
2582 /*
2583 * Hack for X line-drawing characters: if the primary
2584 * font is encoded as ISO-8859-anything, and has valid
2585 * glyphs in the first 32 char positions, it is assumed
2586 * that those glyphs are the VT100 line-drawing
2587 * character set.
2588 *
2589 * Actually, we'll hack even harder by only checking
2590 * position 0x19 (vertical line, VT100 linedrawing
2591 * `x'). Then we can check it easily by seeing if the
2592 * ascent and descent differ.
2593 */
2594 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2595 int lb, rb, wid, asc, desc;
2596 gchar text[2];
2597
2598 text[1] = '\0';
2599 text[0] = '\x12';
2600 gdk_string_extents(inst->fonts[fontid], text,
2601 &lb, &rb, &wid, &asc, &desc);
2602 if (asc != desc)
2603 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2604 }
2605
2dc6356a 2606 sfree(encoding);
2607 }
2608 }
facd762c 2609
2610 return retval;
2dc6356a 2611}
2612
74aca06d 2613int uxsel_input_add(int fd, int rwx) {
2614 int flags = 0;
2615 if (rwx & 1) flags |= GDK_INPUT_READ;
2616 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2617 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2618 return gdk_input_add(fd, flags, fd_input_func, NULL);
2619}
2620
2621void uxsel_input_remove(int id) {
2622 gdk_input_remove(id);
2623}
2624
f87534ba 2625char *guess_derived_font_name(GdkFont *font, int bold, int wide)
2626{
2627 XFontStruct *xfs = GDK_FONT_XFONT(font);
2628 Display *disp = GDK_FONT_XDISPLAY(font);
2629 Atom fontprop = XInternAtom(disp, "FONT", False);
2630 unsigned long ret;
2631 if (XGetFontProperty(xfs, fontprop, &ret)) {
2632 char *name = XGetAtomName(disp, (Atom)ret);
2633 if (name && name[0] == '-') {
2634 char *strings[13];
2635 char *dupname, *extrafree = NULL, *ret;
2636 char *p, *q;
2637 int nstr;
2638
2639 p = q = dupname = dupstr(name); /* skip initial minus */
2640 nstr = 0;
2641
2642 while (*p && nstr < lenof(strings)) {
2643 if (*p == '-') {
2644 *p = '\0';
2645 strings[nstr++] = p+1;
2646 }
2647 p++;
2648 }
2649
2650 if (nstr < lenof(strings))
2651 return NULL; /* XLFD was malformed */
2652
2653 if (bold)
2654 strings[2] = "bold";
2655
2656 if (wide) {
2657 /* 4 is `wideness', which obviously may have changed. */
2658 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2659 strings[4] = strings[5] = "*";
2660 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
2661 }
2662
2663 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
2664 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
2665 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
2666 "-", strings[ 9], "-", strings[10], "-", strings[11],
2667 "-", strings[12], NULL);
2668 sfree(extrafree);
2669 sfree(dupname);
2670
2671 return ret;
2672 }
2673 }
2674 return NULL;
2675}
2676
56b9b9a7 2677void setup_fonts_ucs(struct gui_data *inst)
2678{
2679 int font_charset;
f87534ba 2680 char *name;
2681 int guessed;
56b9b9a7 2682
2683 if (inst->fonts[0])
2684 gdk_font_unref(inst->fonts[0]);
2685 if (inst->fonts[1])
2686 gdk_font_unref(inst->fonts[1]);
2687 if (inst->fonts[2])
2688 gdk_font_unref(inst->fonts[2]);
2689 if (inst->fonts[3])
2690 gdk_font_unref(inst->fonts[3]);
2691
2692 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2693 if (!inst->fonts[0]) {
2694 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2695 inst->cfg.font.name);
2696 exit(1);
2697 }
2698 font_charset = set_font_info(inst, 0);
f87534ba 2699
bf133a73 2700 if (inst->cfg.shadowbold) {
2701 inst->fonts[1] = NULL;
f87534ba 2702 } else {
bf133a73 2703 if (inst->cfg.boldfont.name[0]) {
2704 name = inst->cfg.boldfont.name;
2705 guessed = FALSE;
2706 } else {
2707 name = guess_derived_font_name(inst->fonts[0], TRUE, FALSE);
2708 guessed = TRUE;
2709 }
2710 inst->fonts[1] = name ? gdk_font_load(name) : NULL;
2711 if (inst->fonts[1]) {
2712 set_font_info(inst, 1);
2713 } else if (!guessed) {
2714 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2715 inst->cfg.boldfont.name);
2716 exit(1);
2717 }
2718 if (guessed)
2719 sfree(name);
f87534ba 2720 }
f87534ba 2721
56b9b9a7 2722 if (inst->cfg.widefont.name[0]) {
f87534ba 2723 name = inst->cfg.widefont.name;
2724 guessed = FALSE;
2725 } else {
2726 name = guess_derived_font_name(inst->fonts[0], FALSE, TRUE);
2727 guessed = TRUE;
2728 }
2729 inst->fonts[2] = name ? gdk_font_load(name) : NULL;
2730 if (inst->fonts[2]) {
56b9b9a7 2731 set_font_info(inst, 2);
f87534ba 2732 } else if (!guessed) {
2733 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2734 inst->cfg.widefont.name);
2735 exit(1);
2736 }
2737 if (guessed)
2738 sfree(name);
2739
bf133a73 2740 if (inst->cfg.shadowbold) {
2741 inst->fonts[3] = NULL;
f87534ba 2742 } else {
bf133a73 2743 if (inst->cfg.wideboldfont.name[0]) {
2744 name = inst->cfg.wideboldfont.name;
2745 guessed = FALSE;
2746 } else {
2747 /*
2748 * Here we have some choices. We can widen the bold font,
2749 * bolden the wide font, or widen and bolden the standard
2750 * font. Try them all, in that order!
2751 */
2752 if (inst->cfg.widefont.name[0])
2753 name = guess_derived_font_name(inst->fonts[2], TRUE, FALSE);
2754 else if (inst->cfg.boldfont.name[0])
2755 name = guess_derived_font_name(inst->fonts[1], FALSE, TRUE);
2756 else
2757 name = guess_derived_font_name(inst->fonts[0], TRUE, TRUE);
2758 guessed = TRUE;
2759 }
2760 inst->fonts[3] = name ? gdk_font_load(name) : NULL;
2761 if (inst->fonts[3]) {
2762 set_font_info(inst, 3);
2763 } else if (!guessed) {
2764 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n", appname,
2765 inst->cfg.wideboldfont.name);
2766 exit(1);
2767 }
2768 if (guessed)
2769 sfree(name);
f87534ba 2770 }
56b9b9a7 2771
2772 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2773 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2774
6ac7f054 2775 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2776 inst->cfg.utf8_override, font_charset,
3900c2d6 2777 inst->cfg.vtmode);
56b9b9a7 2778}
2779
2780void set_geom_hints(struct gui_data *inst)
2781{
2782 GdkGeometry geom;
2783 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2784 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2785 geom.max_width = geom.max_height = -1;
2786 geom.base_width = 2*inst->cfg.window_border;
2787 geom.base_height = 2*inst->cfg.window_border;
2788 geom.width_inc = inst->font_width;
2789 geom.height_inc = inst->font_height;
2790 geom.min_aspect = geom.max_aspect = 0;
2791 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2792 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2793 GDK_HINT_RESIZE_INC);
2794}
2795
47e4e735 2796void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2797{
2798 struct gui_data *inst = (struct gui_data *)data;
2799 term_clrsb(inst->term);
2800}
2801
2802void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2803{
2804 struct gui_data *inst = (struct gui_data *)data;
2805 term_pwron(inst->term);
e3be8de5 2806 if (inst->ldisc)
2807 ldisc_send(inst->ldisc, NULL, 0, 0);
47e4e735 2808}
2809
d9e5c333 2810void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2811{
2812 struct gui_data *inst = (struct gui_data *)data;
2813 term_copyall(inst->term);
2814}
2815
47e4e735 2816void special_menuitem(GtkMenuItem *item, gpointer data)
2817{
2818 struct gui_data *inst = (struct gui_data *)data;
f7f9fb5c 2819 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2820 "user-data"));
47e4e735 2821
e3be8de5 2822 if (inst->back)
2823 inst->back->special(inst->backhandle, code);
47e4e735 2824}
2825
2826void about_menuitem(GtkMenuItem *item, gpointer data)
2827{
7718480a 2828 struct gui_data *inst = (struct gui_data *)data;
2829 about_box(inst->window);
47e4e735 2830}
2831
8eed910d 2832void event_log_menuitem(GtkMenuItem *item, gpointer data)
2833{
2834 struct gui_data *inst = (struct gui_data *)data;
2835 showeventlog(inst->eventlogstuff, inst->window);
2836}
2837
56b9b9a7 2838void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2839{
2840 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2841 static const int ww[] = {
2842 6, 7, 8, 9, 10, 11, 12, 13,
2843 14, 15, 16, 17, 18, 19, 20, 21,
2844 0, 1, 2, 3, 4, 5
2845 };
2846 struct gui_data *inst = (struct gui_data *)data;
2847 char *title = dupcat(appname, " Reconfiguration", NULL);
2848 Config cfg2, oldcfg;
2849 int i, need_size;
2850
2851 cfg2 = inst->cfg; /* structure copy */
2852
2853 if (do_config_box(title, &cfg2, 1)) {
2854
2855 oldcfg = inst->cfg; /* structure copy */
2856 inst->cfg = cfg2; /* structure copy */
2857
2858 /* Pass new config data to the logging module */
2859 log_reconfig(inst->logctx, &cfg2);
2860 /*
2861 * Flush the line discipline's edit buffer in the case
2862 * where local editing has just been disabled.
2863 */
e3be8de5 2864 if (inst->ldisc)
2865 ldisc_send(inst->ldisc, NULL, 0, 0);
56b9b9a7 2866 /* Pass new config data to the terminal */
2867 term_reconfig(inst->term, &cfg2);
2868 /* Pass new config data to the back end */
e3be8de5 2869 if (inst->back)
2870 inst->back->reconfig(inst->backhandle, &cfg2);
56b9b9a7 2871
2872 /*
2873 * Just setting inst->cfg is sufficient to cause colour
2874 * setting changes to appear on the next ESC]R palette
2875 * reset. But we should also check whether any colour
2876 * settings have been changed, and revert the ones that
2877 * have to the new default, on the assumption that the user
2878 * is most likely to want an immediate update.
2879 */
2880 for (i = 0; i < NCOLOURS; i++) {
2881 if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
2882 oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
db26b3af 2883 oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2]) {
56b9b9a7 2884 real_palette_set(inst, i, cfg2.colours[ww[i]][0],
2885 cfg2.colours[ww[i]][1],
2886 cfg2.colours[ww[i]][2]);
db26b3af 2887
2888 /*
2889 * If the default background has changed, we must
2890 * repaint the space in between the window border
2891 * and the text area.
2892 */
2893 if (i == 18) {
2894 set_window_background(inst);
2895 draw_backing_rect(inst);
2896 }
2897 }
56b9b9a7 2898 }
2899
2900 /*
2901 * If the scrollbar needs to be shown, hidden, or moved
2902 * from one end to the other of the window, do so now.
2903 */
2904 if (oldcfg.scrollbar != cfg2.scrollbar) {
2905 if (cfg2.scrollbar)
2906 gtk_widget_show(inst->sbar);
2907 else
2908 gtk_widget_hide(inst->sbar);
2909 }
2910 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2911 gtk_box_reorder_child(inst->hbox, inst->sbar,
2912 cfg2.scrollbar_on_left ? 0 : 1);
2913 }
2914
2915 /*
2916 * Change the window title, if required.
2917 */
2918 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2919 set_title(inst, cfg2.wintitle);
6e785e0c 2920 set_window_titles(inst);
56b9b9a7 2921
2922 /*
2923 * Redo the whole tangled fonts and Unicode mess if
2924 * necessary.
2925 */
2926 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2927 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2928 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2929 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
3900c2d6 2930 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
bf133a73 2931 oldcfg.vtmode != cfg2.vtmode ||
2932 oldcfg.shadowbold != cfg2.shadowbold) {
56b9b9a7 2933 setup_fonts_ucs(inst);
2934 need_size = 1;
2935 } else
2936 need_size = 0;
2937
2938 /*
2939 * Resize the window.
2940 */
2941 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2942 oldcfg.window_border != cfg2.window_border || need_size) {
2943 set_geom_hints(inst);
2944 request_resize(inst, cfg2.width, cfg2.height);
ec9f4fc6 2945 } else {
2946 /*
2947 * The above will have caused a call to term_size() for
2948 * us if it happened. If the user has fiddled with only
2949 * the scrollback size, the above will not have
2950 * happened and we will need an explicit term_size()
2951 * here.
2952 */
2953 if (oldcfg.savelines != cfg2.savelines)
2954 term_size(inst->term, inst->term->rows, inst->term->cols,
2955 cfg2.savelines);
2956 }
56b9b9a7 2957
2958 term_invalidate(inst->term);
db26b3af 2959
2960 /*
2961 * We do an explicit full redraw here to ensure the window
2962 * border has been redrawn as well as the text area.
2963 */
2964 gtk_widget_queue_draw(inst->area);
56b9b9a7 2965 }
2966 sfree(title);
2967}
2968
56801e3d 2969void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
2970{
2971 /*
2972 * Re-execing ourself is not an exact science under Unix. I do
2973 * the best I can by using /proc/self/exe if available and by
2974 * assuming argv[0] can be found on $PATH if not.
2975 *
2976 * Note that we also have to reconstruct the elements of the
2977 * original argv which gtk swallowed, since the user wants the
2978 * new session to appear on the same X display as the old one.
2979 */
2980 char **args;
2981 va_list ap;
2982 int i, n;
2983 int pid;
2984
2985 /*
2986 * Collect the arguments with which to re-exec ourself.
2987 */
2988 va_start(ap, fd_to_close);
2989 n = 2; /* progname and terminating NULL */
2990 n += inst->ngtkargs;
2991 while (va_arg(ap, char *) != NULL)
2992 n++;
2993 va_end(ap);
2994
2995 args = snewn(n, char *);
2996 args[0] = inst->progname;
2997 args[n-1] = NULL;
2998 for (i = 0; i < inst->ngtkargs; i++)
2999 args[i+1] = inst->gtkargvstart[i];
3000
3001 i++;
3002 va_start(ap, fd_to_close);
3003 while ((args[i++] = va_arg(ap, char *)) != NULL);
3004 va_end(ap);
3005
3006 assert(i == n);
3007
3008 /*
3009 * Do the double fork.
3010 */
3011 pid = fork();
3012 if (pid < 0) {
3013 perror("fork");
3014 return;
3015 }
3016
3017 if (pid == 0) {
3018 int pid2 = fork();
3019 if (pid2 < 0) {
3020 perror("fork");
3021 _exit(1);
3022 } else if (pid2 > 0) {
3023 /*
3024 * First child has successfully forked second child. My
3025 * Work Here Is Done. Note the use of _exit rather than
3026 * exit: the latter appears to cause destroy messages
3027 * to be sent to the X server. I suspect gtk uses
3028 * atexit.
3029 */
3030 _exit(0);
3031 }
3032
3033 /*
3034 * If we reach here, we are the second child, so we now
3035 * actually perform the exec.
3036 */
3037 if (fd_to_close >= 0)
3038 close(fd_to_close);
3039
3040 execv("/proc/self/exe", args);
3041 execvp(inst->progname, args);
3042 perror("exec");
3043 _exit(127);
3044
3045 } else {
3046 int status;
3047 waitpid(pid, &status, 0);
3048 }
3049
3050}
3051
3052void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3053{
3054 struct gui_data *inst = (struct gui_data *)gdata;
3055 /*
3056 * For this feature we must marshal cfg and (possibly) pty_argv
3057 * into a byte stream, create a pipe, and send this byte stream
3058 * to the child through the pipe.
3059 */
3060 int i, ret, size;
3061 char *data;
3062 char option[80];
3063 int pipefd[2];
3064
3065 if (pipe(pipefd) < 0) {
3066 perror("pipe");
3067 return;
3068 }
3069
3070 size = sizeof(inst->cfg);
3071 if (use_pty_argv && pty_argv) {
3072 for (i = 0; pty_argv[i]; i++)
3073 size += strlen(pty_argv[i]) + 1;
3074 }
3075
3076 data = snewn(size, char);
3077 memcpy(data, &inst->cfg, sizeof(inst->cfg));
3078 if (use_pty_argv && pty_argv) {
3079 int p = sizeof(inst->cfg);
3080 for (i = 0; pty_argv[i]; i++) {
3081 strcpy(data + p, pty_argv[i]);
3082 p += strlen(pty_argv[i]) + 1;
3083 }
3084 assert(p == size);
3085 }
3086
3087 sprintf(option, "---[%d,%d]", pipefd[0], size);
3088 fcntl(pipefd[0], F_SETFD, 0);
3089 fork_and_exec_self(inst, pipefd[1], option, NULL);
3090 close(pipefd[0]);
3091
7ffdbc1a 3092 i = ret = 0;
56801e3d 3093 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3094 i += ret;
3095 if (ret < 0)
3096 perror("write to pipe");
3097 close(pipefd[1]);
3098 sfree(data);
3099}
3100
3101int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3102{
3103 int fd, i, ret, size;
3104 char *data;
3105
3106 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3107 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3108 exit(1);
3109 }
3110
3111 data = snewn(size, char);
7ffdbc1a 3112 i = ret = 0;
56801e3d 3113 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3114 i += ret;
3115 if (ret < 0) {
3116 perror("read from pipe");
3117 exit(1);
3118 } else if (i < size) {
3119 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3120 appname);
3121 exit(1);
3122 }
3123
3124 memcpy(cfg, data, sizeof(Config));
3125 if (use_pty_argv && size > sizeof(Config)) {
3126 int n = 0;
3127 i = sizeof(Config);
3128 while (i < size) {
3129 while (i < size && data[i]) i++;
3130 if (i >= size) {
3131 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3132 appname);
3133 exit(1);
3134 }
3135 i++;
3136 n++;
3137 }
3138 pty_argv = snewn(n+1, char *);
3139 pty_argv[n] = NULL;
3140 n = 0;
3141 i = sizeof(Config);
3142 while (i < size) {
3143 char *p = data + i;
3144 while (i < size && data[i]) i++;
3145 assert(i < size);
3146 i++;
3147 pty_argv[n++] = dupstr(p);
3148 }
3149 }
3150
3151 return 0;
3152}
3153
3154void new_session_menuitem(GtkMenuItem *item, gpointer data)
3155{
3156 struct gui_data *inst = (struct gui_data *)data;
3157
3158 fork_and_exec_self(inst, -1, NULL);
3159}
3160
e3be8de5 3161void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3162{
3163 struct gui_data *inst = (struct gui_data *)data;
3164
3165 if (!inst->back) {
3166 logevent(inst, "----- Session restarted -----");
3167 start_backend(inst);
3168 inst->exited = FALSE;
3169 }
3170}
3171
56801e3d 3172void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3173{
3174 struct gui_data *inst = (struct gui_data *)data;
3175 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3176
3177 fork_and_exec_self(inst, -1, "-load", str, NULL);
3178}
3179
3180void saved_session_freedata(GtkMenuItem *item, gpointer data)
3181{
3182 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3183
3184 sfree(str);
3185}
3186
47e4e735 3187void update_specials_menu(void *frontend)
3188{
fbf6cb3b 3189 struct gui_data *inst = (struct gui_data *)frontend;
47e4e735 3190
3191 const struct telnet_special *specials;
3192
e3be8de5 3193 if (inst->back)
3194 specials = inst->back->get_specials(inst->backhandle);
3195 else
3196 specials = NULL;
3197
6f2d0cde 3198 /* I believe this disposes of submenus too. */
47e4e735 3199 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3200 (GtkCallback)gtk_widget_destroy, NULL);
3201 if (specials) {
3202 int i;
6f2d0cde 3203 GtkWidget *menu = inst->specialsmenu;
3204 /* A lame "stack" for submenus that will do for now. */
3205 GtkWidget *saved_menu = NULL;
3206 int nesting = 1;
3207 for (i = 0; nesting > 0; i++) {
3208 GtkWidget *menuitem = NULL;
3209 switch (specials[i].code) {
3210 case TS_SUBMENU:
3211 assert (nesting < 2);
3212 saved_menu = menu; /* XXX lame stacking */
3213 menu = gtk_menu_new();
3214 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3215 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3216 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3217 gtk_widget_show(menuitem);
3218 menuitem = NULL;
3219 nesting++;
3220 break;
3221 case TS_EXITMENU:
3222 nesting--;
3223 if (nesting) {
3224 menu = saved_menu; /* XXX lame stacking */
3225 saved_menu = NULL;
3226 }
3227 break;
3228 case TS_SEP:
3229 menuitem = gtk_menu_item_new();
3230 break;
3231 default:
47e4e735 3232 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3233 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
f7f9fb5c 3234 GINT_TO_POINTER(specials[i].code));
47e4e735 3235 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3236 GTK_SIGNAL_FUNC(special_menuitem), inst);
6f2d0cde 3237 break;
3238 }
3239 if (menuitem) {
3240 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3241 gtk_widget_show(menuitem);
3242 }
47e4e735 3243 }
3244 gtk_widget_show(inst->specialsitem1);
3245 gtk_widget_show(inst->specialsitem2);
3246 } else {
3247 gtk_widget_hide(inst->specialsitem1);
3248 gtk_widget_hide(inst->specialsitem2);
3249 }
3250}
3251
e3be8de5 3252static void start_backend(struct gui_data *inst)
faec60ed 3253{
1d009ae7 3254 extern Backend *select_backend(Config *cfg);
e3be8de5 3255 char *realhost;
3256 const char *error;
3257
3258 inst->back = select_backend(&inst->cfg);
3259
3260 error = inst->back->init((void *)inst, &inst->backhandle,
3261 &inst->cfg, inst->cfg.host, inst->cfg.port,
3262 &realhost, inst->cfg.tcp_nodelay,
3263 inst->cfg.tcp_keepalives);
3264
3265 if (error) {
3266 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3267 inst->cfg.host, error);
3268 inst->exited = TRUE;
3269 fatal_message_box(inst->window, msg);
3270 sfree(msg);
3271 exit(0);
3272 }
3273
3274 if (inst->cfg.wintitle[0]) {
3275 set_title(inst, inst->cfg.wintitle);
3276 set_icon(inst, inst->cfg.wintitle);
3277 } else {
3278 char *title = make_default_wintitle(realhost);
3279 set_title(inst, title);
3280 set_icon(inst, title);
3281 sfree(title);
3282 }
3283 inst->back->provide_logctx(inst->backhandle, inst->logctx);
e3be8de5 3284
3285 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3286
3287 inst->ldisc =
3288 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3289 inst);
3290
3291 gtk_widget_hide(inst->restartitem);
3292}
3293
3294int pt_main(int argc, char **argv)
3295{
1d009ae7 3296 extern int cfgbox(Config *cfg);
a8327734 3297 struct gui_data *inst;
faec60ed 3298
56801e3d 3299 /*
3300 * Create an instance structure and initialise to zeroes
3301 */
3302 inst = snew(struct gui_data);
3303 memset(inst, 0, sizeof(*inst));
3304 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3305
0f33f9d1 3306 /* defer any child exit handling until we're ready to deal with
3307 * it */
3308 block_signal(SIGCHLD, 1);
3309
56801e3d 3310 inst->progname = argv[0];
7428c509 3311 /*
56801e3d 3312 * Copy the original argv before letting gtk_init fiddle with
3313 * it. It will be required later.
7428c509 3314 */
56801e3d 3315 {
3316 int i, oldargc;
3317 inst->gtkargvstart = snewn(argc-1, char *);
3318 for (i = 1; i < argc; i++)
3319 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3320 oldargc = argc;
3321 gtk_init(&argc, &argv);
3322 inst->ngtkargs = oldargc - argc;
3323 }
7428c509 3324
56801e3d 3325 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3326 read_dupsession_data(inst, &inst->cfg, argv[1]);
3327 /* Splatter this argument so it doesn't clutter a ps listing */
3328 memset(argv[1], 0, strlen(argv[1]));
3329 } else {
3330 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
3331 exit(1); /* pre-defaults pass to get -class */
3332 do_defaults(NULL, &inst->cfg);
3333 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
3334 exit(1); /* post-defaults, do everything */
9dc625f0 3335
56801e3d 3336 cmdline_run_saved(&inst->cfg);
46a3419b 3337
56801e3d 3338 if (!*inst->cfg.host && !cfgbox(&inst->cfg))
3339 exit(0); /* config box hit Cancel */
3340 }
1d009ae7 3341
8c161662 3342 if (!compound_text_atom)
3343 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3344 if (!utf8_string_atom)
3345 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
dd72dfa3 3346
56b9b9a7 3347 setup_fonts_ucs(inst);
3aac0d35 3348 init_cutbuffers();
1709795f 3349
12d200b1 3350 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3351
16891265 3352 /*
3353 * Set up the colour map.
3354 */
a8327734 3355 palette_reset(inst);
16891265 3356
56b9b9a7 3357 inst->width = inst->cfg.width;
3358 inst->height = inst->cfg.height;
3359
f7f27309 3360 inst->area = gtk_drawing_area_new();
3361 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3ea863a3 3362 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3363 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
56b9b9a7 3364 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3365 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
6a5e84dd 3366 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
56b9b9a7 3367 /*
3368 * We always create the scrollbar; it remains invisible if
3369 * unwanted, so we can pop it up quickly if it suddenly becomes
3370 * desirable.
3371 */
3372 if (inst->cfg.scrollbar_on_left)
3373 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
88e6b9ca 3374 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
56b9b9a7 3375 if (!inst->cfg.scrollbar_on_left)
3376 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
6a5e84dd 3377
12d200b1 3378 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
f7f27309 3379
56b9b9a7 3380 set_geom_hints(inst);
f7f27309 3381
aca2a702 3382 gtk_widget_show(inst->area);
3383 if (inst->cfg.scrollbar)
3384 gtk_widget_show(inst->sbar);
56b9b9a7 3385 else
3386 gtk_widget_hide(inst->sbar);
aca2a702 3387 gtk_widget_show(GTK_WIDGET(inst->hbox));
3388
3389 if (inst->gotpos) {
3390 int x = inst->xpos, y = inst->ypos;
3391 GtkRequisition req;
3392 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3393 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3394 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3395 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3396 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3397 }
3398
12d200b1 3399 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
f7f27309 3400 GTK_SIGNAL_FUNC(destroy), inst);
12d200b1 3401 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
f7f27309 3402 GTK_SIGNAL_FUNC(delete_window), inst);
12d200b1 3403 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
f7f27309 3404 GTK_SIGNAL_FUNC(key_event), inst);
c33c3d76 3405 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3406 GTK_SIGNAL_FUNC(key_event), inst);
12d200b1 3407 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
f7f27309 3408 GTK_SIGNAL_FUNC(focus_event), inst);
12d200b1 3409 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
f7f27309 3410 GTK_SIGNAL_FUNC(focus_event), inst);
3411 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3412 GTK_SIGNAL_FUNC(configure_area), inst);
3413 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3414 GTK_SIGNAL_FUNC(expose_area), inst);
e6346999 3415 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3416 GTK_SIGNAL_FUNC(button_event), inst);
3417 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3418 GTK_SIGNAL_FUNC(button_event), inst);
3419 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3420 GTK_SIGNAL_FUNC(motion_event), inst);
3421 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3422 GTK_SIGNAL_FUNC(selection_received), inst);
3423 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3424 GTK_SIGNAL_FUNC(selection_get), inst);
3425 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3426 GTK_SIGNAL_FUNC(selection_clear), inst);
3ea863a3 3427 if (inst->cfg.scrollbar)
330f49be 3428 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3429 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
f7f27309 3430 gtk_widget_add_events(GTK_WIDGET(inst->area),
e6346999 3431 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3432 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
57e636ca 3433 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
f7f27309 3434
12d200b1 3435 gtk_widget_show(inst->window);
f7f27309 3436
a8327734 3437 set_window_background(inst);
7428c509 3438
47e4e735 3439 /*
3440 * Set up the Ctrl+rightclick context menu.
3441 */
3442 {
3443 GtkWidget *menuitem;
3444 char *s;
56801e3d 3445 extern const int use_event_log, new_session, saved_sessions;
47e4e735 3446
3447 inst->menu = gtk_menu_new();
3448
3449#define MKMENUITEM(title, func) do { \
3450 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3451 gtk_menu_item_new(); \
3452 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3453 gtk_widget_show(menuitem); \
3454 if (func != NULL) \
3455 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3456 GTK_SIGNAL_FUNC(func), inst); \
3457} while (0)
56801e3d 3458 if (new_session)
3459 MKMENUITEM("New Session", new_session_menuitem);
e3be8de5 3460 MKMENUITEM("Restart Session", restart_session_menuitem);
3461 inst->restartitem = menuitem;
3462 gtk_widget_hide(inst->restartitem);
56801e3d 3463 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3464 if (saved_sessions) {
3465 struct sesslist sesslist;
3466 int i;
3467
3468 inst->sessionsmenu = gtk_menu_new();
3469
3470 get_sesslist(&sesslist, TRUE);
3471 for (i = 1; i < sesslist.nsessions; i++) {
3472 menuitem = gtk_menu_item_new_with_label(sesslist.sessions[i]);
3473 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3474 gtk_widget_show(menuitem);
3475 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3476 dupstr(sesslist.sessions[i]));
3477 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3478 GTK_SIGNAL_FUNC(saved_session_menuitem),
3479 inst);
3480 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3481 GTK_SIGNAL_FUNC(saved_session_freedata),
3482 inst);
3483 }
3484 get_sesslist(&sesslist, FALSE);
3485
3486 MKMENUITEM("Saved Sessions", NULL);
3487 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3488 inst->sessionsmenu);
3489 }
3490 MKMENUITEM(NULL, NULL);
56b9b9a7 3491 MKMENUITEM("Change Settings", change_settings_menuitem);
3492 MKMENUITEM(NULL, NULL);
8eed910d 3493 if (use_event_log)
3494 MKMENUITEM("Event Log", event_log_menuitem);
47e4e735 3495 MKMENUITEM("Special Commands", NULL);
3496 inst->specialsmenu = gtk_menu_new();
3497 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3498 inst->specialsitem1 = menuitem;
3499 MKMENUITEM(NULL, NULL);
3500 inst->specialsitem2 = menuitem;
533b1743 3501 gtk_widget_hide(inst->specialsitem1);
3502 gtk_widget_hide(inst->specialsitem2);
47e4e735 3503 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3504 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
d9e5c333 3505 MKMENUITEM("Copy All", copy_all_menuitem);
47e4e735 3506 MKMENUITEM(NULL, NULL);
3507 s = dupcat("About ", appname, NULL);
3508 MKMENUITEM(s, about_menuitem);
3509 sfree(s);
3510#undef MKMENUITEM
3511 }
3512
a8327734 3513 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3514 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3515 inst->blankcursor = make_mouse_ptr(inst, -1);
3516 make_mouse_ptr(inst, -2); /* clean up cursor font */
57e636ca 3517 inst->currcursor = inst->textcursor;
a8327734 3518 show_mouseptr(inst, 1);
d64838e1 3519
8eed910d 3520 inst->eventlogstuff = eventlogstuff_new();
3521
21d2b241 3522 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3ea863a3 3523 inst->logctx = log_init(inst, &inst->cfg);
a8327734 3524 term_provide_logctx(inst->term, inst->logctx);
887035a5 3525
74aca06d 3526 uxsel_init();
3527
3f935d5b 3528 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3529
e3be8de5 3530 start_backend(inst);
755a6d84 3531
b9d7bcad 3532 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
755a6d84 3533
0f33f9d1 3534 /* now we're reday to deal with the child exit handler being
3535 * called */
3536 block_signal(SIGCHLD, 0);
74aca06d 3537
755e29c6 3538 /*
3539 * Block SIGPIPE: if we attempt Duplicate Session or similar
3540 * and it falls over in some way, we certainly don't want
3541 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3542 * this _after_ (at least pterm) forks off its child process,
3543 * since the child wants SIGPIPE handled in the usual way.
3544 */
3545 block_signal(SIGPIPE, 1);
3546
74aca06d 3547 inst->exited = FALSE;
3548
f7f27309 3549 gtk_main();
3550
3551 return 0;
3552}