2 * gtkwin.c: the main code that runs a PuTTY terminal emulator and
3 * backend in a GTK window.
18 #include <sys/types.h>
21 #include <gdk/gdkkeysyms.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
33 #define CAT2(x,y) x ## y
34 #define CAT(x,y) CAT2(x,y)
35 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
37 /* Colours come in two flavours: configurable, and xterm-extended. */
38 #define NCFGCOLOURS (lenof(((Config *)0)->colours))
39 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
40 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
42 GdkAtom compound_text_atom, utf8_string_atom;
44 extern char **pty_argv; /* declared in pty.c */
45 extern int use_pty_argv;
48 * Timers are global across all sessions (even if we were handling
49 * multiple sessions, which we aren't), so the current timer ID is
52 static guint timer_id = 0;
55 GtkWidget *window, *area, *sbar;
57 GtkAdjustment *sbar_adjust;
58 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
60 GtkWidget *sessionsmenu;
62 unifont *fonts[4]; /* normal, bold, wide, widebold */
63 int xpos, ypos, gotpos, gravity;
64 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
65 GdkColor cols[NALLCOLOURS];
67 wchar_t *pastein_data;
70 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
71 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
72 int font_width, font_height;
77 guint term_paste_idle_id;
78 guint term_exit_idle_id;
81 char wintitle[sizeof(((Config *)0)->wintitle)];
82 char icontitle[sizeof(((Config *)0)->wintitle)];
83 int master_fd, master_func_id;
90 struct unicode_data ucsdata;
93 char *progname, **gtkargvstart;
95 guint32 input_event_time; /* Timestamp of the most recent input event. */
101 struct gui_data *inst;
104 static int send_raw_mouse;
106 static char *app_name = "pterm";
108 static void start_backend(struct gui_data *inst);
110 char *x_get_default(const char *key)
112 return XGetDefault(GDK_DISPLAY(), app_name, key);
115 void connection_fatal(void *frontend, char *p, ...)
117 struct gui_data *inst = (struct gui_data *)frontend;
122 msg = dupvprintf(p, ap);
125 fatal_message_box(inst->window, msg);
127 if (inst->cfg.close_on_exit == FORCE_ON)
132 * Default settings that are specific to pterm.
134 FontSpec platform_default_fontspec(const char *name)
137 if (!strcmp(name, "Font"))
138 strcpy(ret.name, "server:fixed");
144 Filename platform_default_filename(const char *name)
147 if (!strcmp(name, "LogFileName"))
148 strcpy(ret.path, "putty.log");
154 char *platform_default_s(const char *name)
156 if (!strcmp(name, "SerialLine"))
157 return dupstr("/dev/ttyS0");
161 int platform_default_i(const char *name, int def)
163 if (!strcmp(name, "CloseOnExit"))
164 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
165 if (!strcmp(name, "WinNameAlways"))
166 return 0; /* X natively supports icon titles, so use 'em by default */
170 void ldisc_update(void *frontend, int echo, int edit)
173 * This is a stub in pterm. If I ever produce a Unix
174 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
175 * then it will require some termios manoeuvring analogous to
176 * that in the Windows plink.c, but here it's meaningless.
180 char *get_ttymode(void *frontend, const char *mode)
182 struct gui_data *inst = (struct gui_data *)frontend;
183 return term_get_ttymode(inst->term, mode);
186 int from_backend(void *frontend, int is_stderr, const char *data, int len)
188 struct gui_data *inst = (struct gui_data *)frontend;
189 return term_data(inst->term, is_stderr, data, len);
192 int from_backend_untrusted(void *frontend, const char *data, int len)
194 struct gui_data *inst = (struct gui_data *)frontend;
195 return term_data_untrusted(inst->term, data, len);
198 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
200 struct gui_data *inst = (struct gui_data *)p->frontend;
202 ret = cmdline_get_passwd_input(p, in, inlen);
204 ret = term_get_userpass_input(inst->term, p, in, inlen);
208 void logevent(void *frontend, const char *string)
210 struct gui_data *inst = (struct gui_data *)frontend;
212 log_eventlog(inst->logctx, string);
214 logevent_dlg(inst->eventlogstuff, string);
217 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
219 struct gui_data *inst = (struct gui_data *)frontend;
222 return inst->font_height;
224 return inst->font_width;
228 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
229 * into a cooked one (SELECT, EXTEND, PASTE).
231 * In Unix, this is not configurable; the X button arrangement is
232 * rock-solid across all applications, everyone has a three-button
233 * mouse or a means of faking it, and there is no need to switch
234 * buttons around at all.
236 static Mouse_Button translate_button(Mouse_Button button)
238 /* struct gui_data *inst = (struct gui_data *)frontend; */
240 if (button == MBT_LEFT)
242 if (button == MBT_MIDDLE)
244 if (button == MBT_RIGHT)
246 return 0; /* shouldn't happen */
250 * Return the top-level GtkWindow associated with a particular
251 * front end instance.
253 void *get_window(void *frontend)
255 struct gui_data *inst = (struct gui_data *)frontend;
260 * Minimise or restore the window in response to a server-side
263 void set_iconic(void *frontend, int iconic)
266 * GTK 1.2 doesn't know how to do this.
268 #if GTK_CHECK_VERSION(2,0,0)
269 struct gui_data *inst = (struct gui_data *)frontend;
271 gtk_window_iconify(GTK_WINDOW(inst->window));
273 gtk_window_deiconify(GTK_WINDOW(inst->window));
278 * Move the window in response to a server-side request.
280 void move_window(void *frontend, int x, int y)
282 struct gui_data *inst = (struct gui_data *)frontend;
284 * I assume that when the GTK version of this call is available
285 * we should use it. Not sure how it differs from the GDK one,
288 #if GTK_CHECK_VERSION(2,0,0)
289 gtk_window_move(GTK_WINDOW(inst->window), x, y);
291 gdk_window_move(inst->window->window, x, y);
296 * Move the window to the top or bottom of the z-order in response
297 * to a server-side request.
299 void set_zorder(void *frontend, int top)
301 struct gui_data *inst = (struct gui_data *)frontend;
303 gdk_window_raise(inst->window->window);
305 gdk_window_lower(inst->window->window);
309 * Refresh the window in response to a server-side request.
311 void refresh_window(void *frontend)
313 struct gui_data *inst = (struct gui_data *)frontend;
314 term_invalidate(inst->term);
318 * Maximise or restore the window in response to a server-side
321 void set_zoomed(void *frontend, int zoomed)
324 * GTK 1.2 doesn't know how to do this.
326 #if GTK_CHECK_VERSION(2,0,0)
327 struct gui_data *inst = (struct gui_data *)frontend;
329 gtk_window_maximize(GTK_WINDOW(inst->window));
331 gtk_window_unmaximize(GTK_WINDOW(inst->window));
336 * Report whether the window is iconic, for terminal reports.
338 int is_iconic(void *frontend)
340 struct gui_data *inst = (struct gui_data *)frontend;
341 return !gdk_window_is_viewable(inst->window->window);
345 * Report the window's position, for terminal reports.
347 void get_window_pos(void *frontend, int *x, int *y)
349 struct gui_data *inst = (struct gui_data *)frontend;
351 * I assume that when the GTK version of this call is available
352 * we should use it. Not sure how it differs from the GDK one,
355 #if GTK_CHECK_VERSION(2,0,0)
356 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
358 gdk_window_get_position(inst->window->window, x, y);
363 * Report the window's pixel size, for terminal reports.
365 void get_window_pixels(void *frontend, int *x, int *y)
367 struct gui_data *inst = (struct gui_data *)frontend;
369 * I assume that when the GTK version of this call is available
370 * we should use it. Not sure how it differs from the GDK one,
373 #if GTK_CHECK_VERSION(2,0,0)
374 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
376 gdk_window_get_size(inst->window->window, x, y);
381 * Return the window or icon title.
383 char *get_window_title(void *frontend, int icon)
385 struct gui_data *inst = (struct gui_data *)frontend;
386 return icon ? inst->icontitle : inst->wintitle;
389 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
391 struct gui_data *inst = (struct gui_data *)data;
392 if (!inst->exited && inst->cfg.warn_on_close) {
393 if (!reallyclose(inst))
399 static void update_mouseptr(struct gui_data *inst)
401 switch (inst->busy_status) {
403 if (!inst->mouseptr_visible) {
404 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
405 } else if (send_raw_mouse) {
406 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
408 gdk_window_set_cursor(inst->area->window, inst->textcursor);
411 case BUSY_WAITING: /* XXX can we do better? */
413 /* We always display these cursors. */
414 gdk_window_set_cursor(inst->area->window, inst->waitcursor);
421 static void show_mouseptr(struct gui_data *inst, int show)
423 if (!inst->cfg.hide_mouseptr)
425 inst->mouseptr_visible = show;
426 update_mouseptr(inst);
429 void draw_backing_rect(struct gui_data *inst)
431 GdkGC *gc = gdk_gc_new(inst->area->window);
432 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
433 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
434 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
435 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
439 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
441 struct gui_data *inst = (struct gui_data *)data;
442 int w, h, need_size = 0;
445 * See if the terminal size has changed, in which case we must
446 * let the terminal know.
448 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
449 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
450 if (w != inst->width || h != inst->height) {
451 inst->cfg.width = inst->width = w;
452 inst->cfg.height = inst->height = h;
457 gdk_pixmap_unref(inst->pixmap);
461 inst->pixmap = gdk_pixmap_new(widget->window,
462 (inst->cfg.width * inst->font_width +
463 2*inst->cfg.window_border),
464 (inst->cfg.height * inst->font_height +
465 2*inst->cfg.window_border), -1);
467 draw_backing_rect(inst);
469 if (need_size && inst->term) {
470 term_size(inst->term, h, w, inst->cfg.savelines);
474 term_invalidate(inst->term);
479 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
481 struct gui_data *inst = (struct gui_data *)data;
484 * Pass the exposed rectangle to terminal.c, which will call us
485 * back to do the actual painting.
488 gdk_draw_pixmap(widget->window,
489 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
491 event->area.x, event->area.y,
492 event->area.x, event->area.y,
493 event->area.width, event->area.height);
498 #define KEY_PRESSED(k) \
499 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
501 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
503 struct gui_data *inst = (struct gui_data *)data;
505 wchar_t ucsoutput[2];
506 int ucsval, start, end, special, use_ucsoutput;
508 /* Remember the timestamp. */
509 inst->input_event_time = event->time;
511 /* By default, nothing is generated. */
513 special = use_ucsoutput = FALSE;
516 * If Alt is being released after typing an Alt+numberpad
517 * sequence, we should generate the code that was typed.
519 * Note that we only do this if more than one key was actually
520 * pressed - I don't think Alt+NumPad4 should be ^D or that
521 * Alt+NumPad3 should be ^C, for example. There's no serious
522 * inconvenience in having to type a zero before a single-digit
525 if (event->type == GDK_KEY_RELEASE &&
526 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
527 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
528 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
530 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
532 output[0] = inst->alt_keycode;
537 if (event->type == GDK_KEY_PRESS) {
541 printf("keypress: keyval = %04x, state = %08x; string =",
542 event->keyval, event->state);
543 for (i = 0; event->string[i]; i++)
544 printf(" %02x", (unsigned char) event->string[i]);
550 * NYI: Compose key (!!! requires Unicode faff before even trying)
554 * If Alt has just been pressed, we start potentially
555 * accumulating an Alt+numberpad code. We do this by
556 * setting alt_keycode to -1 (nothing yet but plausible).
558 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
559 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
560 inst->alt_keycode = -1;
561 inst->alt_digits = 0;
562 goto done; /* this generates nothing else */
566 * If we're seeing a numberpad key press with Mod1 down,
567 * consider adding it to alt_keycode if that's sensible.
568 * Anything _else_ with Mod1 down cancels any possibility
569 * of an ALT keycode: we set alt_keycode to -2.
571 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
573 switch (event->keyval) {
574 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
575 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
576 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
577 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
578 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
579 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
580 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
581 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
582 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
583 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
586 inst->alt_keycode = -2; /* it's invalid */
589 printf("Adding digit %d to keycode %d", digit,
592 if (inst->alt_keycode == -1)
593 inst->alt_keycode = digit; /* one-digit code */
595 inst->alt_keycode = inst->alt_keycode * 10 + digit;
598 printf(" gives new code %d\n", inst->alt_keycode);
600 /* Having used this digit, we now do nothing more with it. */
606 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
609 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
610 term_scroll(inst->term, 0, -inst->cfg.height/2);
613 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
614 term_scroll(inst->term, 0, -1);
617 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
618 term_scroll(inst->term, 0, +inst->cfg.height/2);
621 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
622 term_scroll(inst->term, 0, +1);
627 * Neither does Shift-Ins.
629 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
635 use_ucsoutput = FALSE;
637 /* ALT+things gives leading Escape. */
639 strncpy(output+1, event->string, 31);
640 if (!*event->string &&
641 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
642 ucsoutput[0] = '\033';
643 ucsoutput[1] = ucsval;
644 use_ucsoutput = TRUE;
648 end = strlen(output);
650 if (event->state & GDK_MOD1_MASK) {
652 if (end == 1) end = 0;
656 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
657 if (!event->string[0] && event->keyval == '`' &&
658 (event->state & GDK_CONTROL_MASK)) {
660 use_ucsoutput = FALSE;
664 /* Control-Break sends a Break special to the backend */
665 if (event->keyval == GDK_Break &&
666 (event->state & GDK_CONTROL_MASK)) {
668 inst->back->special(inst->backhandle, TS_BRK);
672 /* We handle Return ourselves, because it needs to be flagged as
673 * special to ldisc. */
674 if (event->keyval == GDK_Return) {
676 use_ucsoutput = FALSE;
681 /* Control-2, Control-Space and Control-@ are NUL */
682 if (!event->string[0] &&
683 (event->keyval == ' ' || event->keyval == '2' ||
684 event->keyval == '@') &&
685 (event->state & (GDK_SHIFT_MASK |
686 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
688 use_ucsoutput = FALSE;
692 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
693 if (!event->string[0] && event->keyval == ' ' &&
694 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
695 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
697 use_ucsoutput = FALSE;
701 /* We don't let GTK tell us what Backspace is! We know better. */
702 if (event->keyval == GDK_BackSpace &&
703 !(event->state & GDK_SHIFT_MASK)) {
704 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
705 use_ucsoutput = FALSE;
709 /* For Shift Backspace, do opposite of what is configured. */
710 if (event->keyval == GDK_BackSpace &&
711 (event->state & GDK_SHIFT_MASK)) {
712 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
713 use_ucsoutput = FALSE;
718 /* Shift-Tab is ESC [ Z */
719 if (event->keyval == GDK_ISO_Left_Tab ||
720 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
721 end = 1 + sprintf(output+1, "\033[Z");
722 use_ucsoutput = FALSE;
724 /* And normal Tab is Tab, if the keymap hasn't already told us.
725 * (Curiously, at least one version of the MacOS 10.5 X server
726 * doesn't translate Tab for us. */
727 if (event->keyval == GDK_Tab && end <= 1) {
733 * NetHack keypad mode.
735 if (inst->cfg.nethack_keypad) {
737 switch (event->keyval) {
738 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
739 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
740 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
741 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
742 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
743 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
744 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
745 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
746 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
750 if (event->state & GDK_CONTROL_MASK)
752 else if (event->state & GDK_SHIFT_MASK)
756 use_ucsoutput = FALSE;
762 * Application keypad mode.
764 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
766 switch (event->keyval) {
767 case GDK_Num_Lock: xkey = 'P'; break;
768 case GDK_KP_Divide: xkey = 'Q'; break;
769 case GDK_KP_Multiply: xkey = 'R'; break;
770 case GDK_KP_Subtract: xkey = 'S'; break;
772 * Keypad + is tricky. It covers a space that would
773 * be taken up on the VT100 by _two_ keys; so we
774 * let Shift select between the two. Worse still,
775 * in xterm function key mode we change which two...
778 if (inst->cfg.funky_type == FUNKY_XTERM) {
779 if (event->state & GDK_SHIFT_MASK)
783 } else if (event->state & GDK_SHIFT_MASK)
788 case GDK_KP_Enter: xkey = 'M'; break;
789 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
790 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
791 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
792 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
793 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
794 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
795 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
796 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
797 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
798 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
799 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
802 if (inst->term->vt52_mode) {
803 if (xkey >= 'P' && xkey <= 'S')
804 end = 1 + sprintf(output+1, "\033%c", xkey);
806 end = 1 + sprintf(output+1, "\033?%c", xkey);
808 end = 1 + sprintf(output+1, "\033O%c", xkey);
809 use_ucsoutput = FALSE;
815 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
816 * for integer decimal nn.)
818 * We also deal with the weird ones here. Linux VCs replace F1
819 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
820 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
825 switch (event->keyval) {
827 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
830 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
833 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
836 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
839 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
842 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
845 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
848 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
851 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
854 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
887 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
888 case GDK_Home: case GDK_KP_Home:
891 case GDK_Insert: case GDK_KP_Insert:
894 case GDK_Delete: case GDK_KP_Delete:
897 case GDK_End: case GDK_KP_End:
900 case GDK_Page_Up: case GDK_KP_Page_Up:
903 case GDK_Page_Down: case GDK_KP_Page_Down:
907 /* Reorder edit keys to physical order */
908 if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
909 code = "\0\2\1\4\5\3\6"[code];
911 if (inst->term->vt52_mode && code > 0 && code <= 6) {
912 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
913 use_ucsoutput = FALSE;
917 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
918 code >= 11 && code <= 34) {
919 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
921 switch (event->keyval) {
922 case GDK_F1: index = 0; break;
923 case GDK_F2: index = 1; break;
924 case GDK_F3: index = 2; break;
925 case GDK_F4: index = 3; break;
926 case GDK_F5: index = 4; break;
927 case GDK_F6: index = 5; break;
928 case GDK_F7: index = 6; break;
929 case GDK_F8: index = 7; break;
930 case GDK_F9: index = 8; break;
931 case GDK_F10: index = 9; break;
932 case GDK_F11: index = 10; break;
933 case GDK_F12: index = 11; break;
935 if (event->state & GDK_SHIFT_MASK) index += 12;
936 if (event->state & GDK_CONTROL_MASK) index += 24;
937 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
938 use_ucsoutput = FALSE;
941 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
942 code >= 1 && code <= 6) {
943 char codes[] = "HL.FIG";
948 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
950 use_ucsoutput = FALSE;
953 if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
954 code >= 11 && code <= 24) {
960 if (inst->term->vt52_mode)
961 end = 1 + sprintf(output+1,
962 "\x1B%c", code + 'P' - 11 - offt);
964 end = 1 + sprintf(output+1,
965 "\x1BO%c", code + 'P' - 11 - offt);
966 use_ucsoutput = FALSE;
969 if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
970 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
971 use_ucsoutput = FALSE;
974 if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
975 if (inst->term->vt52_mode)
976 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
978 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
979 use_ucsoutput = FALSE;
982 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
983 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
984 use_ucsoutput = FALSE;
988 end = 1 + sprintf(output+1, "\x1B[%d~", code);
989 use_ucsoutput = FALSE;
995 * Cursor keys. (This includes the numberpad cursor keys,
996 * if we haven't already done them due to app keypad mode.)
998 * Here we also process un-numlocked un-appkeypadded KP5,
999 * which sends ESC [ G.
1003 switch (event->keyval) {
1004 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1005 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1006 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1007 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1008 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1012 * The arrow keys normally do ESC [ A and so on. In
1013 * app cursor keys mode they do ESC O A instead.
1014 * Ctrl toggles the two modes.
1016 if (inst->term->vt52_mode) {
1017 end = 1 + sprintf(output+1, "\033%c", xkey);
1018 } else if (!inst->term->app_cursor_keys ^
1019 !(event->state & GDK_CONTROL_MASK)) {
1020 end = 1 + sprintf(output+1, "\033O%c", xkey);
1022 end = 1 + sprintf(output+1, "\033[%c", xkey);
1024 use_ucsoutput = FALSE;
1033 if (end-start > 0) {
1034 #ifdef KEY_DEBUGGING
1036 printf("generating sequence:");
1037 for (i = start; i < end; i++)
1038 printf(" %02x", (unsigned char) output[i]);
1044 * For special control characters, the character set
1045 * should never matter.
1047 output[end] = '\0'; /* NUL-terminate */
1049 ldisc_send(inst->ldisc, output+start, -2, 1);
1050 } else if (!inst->direct_to_font) {
1051 if (!use_ucsoutput) {
1053 * The stuff we've just generated is assumed to be
1054 * ISO-8859-1! This sounds insane, but `man
1055 * XLookupString' agrees: strings of this type
1056 * returned from the X server are hardcoded to
1057 * 8859-1. Strictly speaking we should be doing
1058 * this using some sort of GtkIMContext, which (if
1059 * we're lucky) would give us our data directly in
1060 * Unicode; but that's not supported in GTK 1.2 as
1061 * far as I can tell, and it's poorly documented
1062 * even in 2.0, so it'll have to wait.
1065 lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
1069 * We generated our own Unicode key data from the
1070 * keysym, so use that instead.
1073 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1077 * In direct-to-font mode, we just send the string
1078 * exactly as we received it.
1081 ldisc_send(inst->ldisc, output+start, end-start, 1);
1084 show_mouseptr(inst, 0);
1085 term_seen_key_event(inst->term);
1091 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1092 GdkEventType type, guint ebutton, guint state,
1093 gdouble ex, gdouble ey)
1095 int shift, ctrl, alt, x, y, button, act;
1097 /* Remember the timestamp. */
1098 inst->input_event_time = timestamp;
1100 show_mouseptr(inst, 1);
1102 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1103 term_scroll(inst->term, 0, -5);
1106 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1107 term_scroll(inst->term, 0, +5);
1111 shift = state & GDK_SHIFT_MASK;
1112 ctrl = state & GDK_CONTROL_MASK;
1113 alt = state & GDK_MOD1_MASK;
1115 if (ebutton == 3 && ctrl) {
1116 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1117 ebutton, timestamp);
1123 else if (ebutton == 2)
1124 button = MBT_MIDDLE;
1125 else if (ebutton == 3)
1128 return FALSE; /* don't even know what button! */
1131 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1132 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1133 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1134 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1135 default: return FALSE; /* don't know this event type */
1138 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
1139 act != MA_CLICK && act != MA_RELEASE)
1140 return TRUE; /* we ignore these in raw mouse mode */
1142 x = (ex - inst->cfg.window_border) / inst->font_width;
1143 y = (ey - inst->cfg.window_border) / inst->font_height;
1145 term_mouse(inst->term, button, translate_button(button), act,
1146 x, y, shift, ctrl, alt);
1151 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1153 struct gui_data *inst = (struct gui_data *)data;
1154 return button_internal(inst, event->time, event->type, event->button,
1155 event->state, event->x, event->y);
1158 #if GTK_CHECK_VERSION(2,0,0)
1160 * In GTK 2, mouse wheel events have become a new type of event.
1161 * This handler translates them back into button-4 and button-5
1162 * presses so that I don't have to change my old code too much :-)
1164 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1166 struct gui_data *inst = (struct gui_data *)data;
1169 if (event->direction == GDK_SCROLL_UP)
1171 else if (event->direction == GDK_SCROLL_DOWN)
1176 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1177 button, event->state, event->x, event->y);
1181 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1183 struct gui_data *inst = (struct gui_data *)data;
1184 int shift, ctrl, alt, x, y, button;
1186 /* Remember the timestamp. */
1187 inst->input_event_time = event->time;
1189 show_mouseptr(inst, 1);
1191 shift = event->state & GDK_SHIFT_MASK;
1192 ctrl = event->state & GDK_CONTROL_MASK;
1193 alt = event->state & GDK_MOD1_MASK;
1194 if (event->state & GDK_BUTTON1_MASK)
1196 else if (event->state & GDK_BUTTON2_MASK)
1197 button = MBT_MIDDLE;
1198 else if (event->state & GDK_BUTTON3_MASK)
1201 return FALSE; /* don't even know what button! */
1203 x = (event->x - inst->cfg.window_border) / inst->font_width;
1204 y = (event->y - inst->cfg.window_border) / inst->font_height;
1206 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1207 x, y, shift, ctrl, alt);
1212 void frontend_keypress(void *handle)
1214 struct gui_data *inst = (struct gui_data *)handle;
1217 * If our child process has exited but not closed, terminate on
1224 static gint idle_exit_func(gpointer data)
1226 struct gui_data *inst = (struct gui_data *)data;
1229 if (!inst->exited &&
1230 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1231 inst->exited = TRUE;
1232 if (inst->cfg.close_on_exit == FORCE_ON ||
1233 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1234 gtk_main_quit(); /* just go */
1236 ldisc_free(inst->ldisc);
1240 inst->back->free(inst->backhandle);
1241 inst->backhandle = NULL;
1243 term_provide_resize_fn(inst->term, NULL, NULL);
1244 update_specials_menu(inst);
1246 gtk_widget_show(inst->restartitem);
1249 gtk_idle_remove(inst->term_exit_idle_id);
1253 void notify_remote_exit(void *frontend)
1255 struct gui_data *inst = (struct gui_data *)frontend;
1257 inst->term_exit_idle_id = gtk_idle_add(idle_exit_func, inst);
1260 static gint timer_trigger(gpointer data)
1262 long now = GPOINTER_TO_INT(data);
1266 if (run_timers(now, &next)) {
1267 ticks = next - GETTICKCOUNT();
1268 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1269 GINT_TO_POINTER(next));
1273 * Never let a timer resume. If we need another one, we've
1274 * asked for it explicitly above.
1279 void timer_change_notify(long next)
1284 gtk_timeout_remove(timer_id);
1286 ticks = next - GETTICKCOUNT();
1288 ticks = 1; /* just in case */
1290 timer_id = gtk_timeout_add(ticks, timer_trigger,
1291 GINT_TO_POINTER(next));
1294 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1297 * We must process exceptional notifications before ordinary
1298 * readability ones, or we may go straight past the urgent
1301 if (condition & GDK_INPUT_EXCEPTION)
1302 select_result(sourcefd, 4);
1303 if (condition & GDK_INPUT_READ)
1304 select_result(sourcefd, 1);
1305 if (condition & GDK_INPUT_WRITE)
1306 select_result(sourcefd, 2);
1309 void destroy(GtkWidget *widget, gpointer data)
1314 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1316 struct gui_data *inst = (struct gui_data *)data;
1317 term_set_focus(inst->term, event->in);
1318 term_update(inst->term);
1319 show_mouseptr(inst, 1);
1323 void set_busy_status(void *frontend, int status)
1325 struct gui_data *inst = (struct gui_data *)frontend;
1326 inst->busy_status = status;
1327 update_mouseptr(inst);
1331 * set or clear the "raw mouse message" mode
1333 void set_raw_mouse_mode(void *frontend, int activate)
1335 struct gui_data *inst = (struct gui_data *)frontend;
1336 activate = activate && !inst->cfg.no_mouse_rep;
1337 send_raw_mouse = activate;
1338 update_mouseptr(inst);
1341 void request_resize(void *frontend, int w, int h)
1343 struct gui_data *inst = (struct gui_data *)frontend;
1344 int large_x, large_y;
1345 int offset_x, offset_y;
1347 GtkRequisition inner, outer;
1350 * This is a heinous hack dreamed up by the gnome-terminal
1351 * people to get around a limitation in gtk. The problem is
1352 * that in order to set the size correctly we really need to be
1353 * calling gtk_window_resize - but that needs to know the size
1354 * of the _whole window_, not the drawing area. So what we do
1355 * is to set an artificially huge size request on the drawing
1356 * area, recompute the resulting size request on the window,
1357 * and look at the difference between the two. That gives us
1358 * the x and y offsets we need to translate drawing area size
1359 * into window size for real, and then we call
1360 * gtk_window_resize.
1364 * We start by retrieving the current size of the whole window.
1365 * Adding a bit to _that_ will give us a value we can use as a
1366 * bogus size request which guarantees to be bigger than the
1367 * current size of the drawing area.
1369 get_window_pixels(inst, &large_x, &large_y);
1373 #if GTK_CHECK_VERSION(2,0,0)
1374 gtk_widget_set_size_request(inst->area, large_x, large_y);
1376 gtk_widget_set_usize(inst->area, large_x, large_y);
1378 gtk_widget_size_request(inst->area, &inner);
1379 gtk_widget_size_request(inst->window, &outer);
1381 offset_x = outer.width - inner.width;
1382 offset_y = outer.height - inner.height;
1384 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1385 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1388 * Now we must set the size request on the drawing area back to
1389 * something sensible before we commit the real resize. Best
1390 * way to do this, I think, is to set it to what the size is
1391 * really going to end up being.
1393 #if GTK_CHECK_VERSION(2,0,0)
1394 gtk_widget_set_size_request(inst->area, area_x, area_y);
1395 gtk_window_resize(GTK_WINDOW(inst->window),
1396 area_x + offset_x, area_y + offset_y);
1398 gtk_widget_set_usize(inst->area, area_x, area_y);
1399 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1401 * I can no longer remember what this call to
1402 * gtk_container_dequeue_resize_handler is for. It was
1403 * introduced in r3092 with no comment, and the commit log
1404 * message was uninformative. I'm _guessing_ its purpose is to
1405 * prevent gratuitous resize processing on the window given
1406 * that we're about to resize it anyway, but I have no idea
1407 * why that's so incredibly vital.
1409 * I've tried removing the call, and nothing seems to go
1410 * wrong. I've backtracked to r3092 and tried removing the
1411 * call there, and still nothing goes wrong. So I'm going to
1412 * adopt the working hypothesis that it's superfluous; I won't
1413 * actually remove it from the GTK 1.2 code, but I won't
1414 * attempt to replicate its functionality in the GTK 2 code
1417 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1418 gdk_window_resize(inst->window->window,
1419 area_x + offset_x, area_y + offset_y);
1423 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1425 gboolean success[1];
1427 inst->cols[n].red = r * 0x0101;
1428 inst->cols[n].green = g * 0x0101;
1429 inst->cols[n].blue = b * 0x0101;
1431 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1432 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1433 FALSE, TRUE, success);
1435 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1439 void set_window_background(struct gui_data *inst)
1441 if (inst->area && inst->area->window)
1442 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1443 if (inst->window && inst->window->window)
1444 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1447 void palette_set(void *frontend, int n, int r, int g, int b)
1449 struct gui_data *inst = (struct gui_data *)frontend;
1452 if (n > NALLCOLOURS)
1454 real_palette_set(inst, n, r, g, b);
1456 /* Default Background changed. Ensure space between text area and
1457 * window border is redrawn */
1458 set_window_background(inst);
1459 draw_backing_rect(inst);
1460 gtk_widget_queue_draw(inst->area);
1464 void palette_reset(void *frontend)
1466 struct gui_data *inst = (struct gui_data *)frontend;
1467 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1468 static const int ww[] = {
1469 256, 257, 258, 259, 260, 261,
1470 0, 8, 1, 9, 2, 10, 3, 11,
1471 4, 12, 5, 13, 6, 14, 7, 15
1473 gboolean success[NALLCOLOURS];
1476 assert(lenof(ww) == NCFGCOLOURS);
1478 if (!inst->colmap) {
1479 inst->colmap = gdk_colormap_get_system();
1481 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1484 for (i = 0; i < NCFGCOLOURS; i++) {
1485 inst->cols[ww[i]].red = inst->cfg.colours[i][0] * 0x0101;
1486 inst->cols[ww[i]].green = inst->cfg.colours[i][1] * 0x0101;
1487 inst->cols[ww[i]].blue = inst->cfg.colours[i][2] * 0x0101;
1490 for (i = 0; i < NEXTCOLOURS; i++) {
1492 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1493 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1494 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1495 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1497 int shade = i - 216;
1498 shade = shade * 0x0a0a + 0x0808;
1499 inst->cols[i+16].red = inst->cols[i+16].green =
1500 inst->cols[i+16].blue = shade;
1504 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1505 FALSE, TRUE, success);
1506 for (i = 0; i < NALLCOLOURS; i++) {
1508 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1509 appname, i, inst->cfg.colours[i][0],
1510 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1513 /* Since Default Background may have changed, ensure that space
1514 * between text area and window border is refreshed. */
1515 set_window_background(inst);
1516 if (inst->area && inst->area->window) {
1517 draw_backing_rect(inst);
1518 gtk_widget_queue_draw(inst->area);
1522 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1523 * do this before we start using cut buffers.
1525 void init_cutbuffers()
1527 unsigned char empty[] = "";
1528 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1529 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1530 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1531 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1532 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1533 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1534 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1535 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1536 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1537 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1538 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1539 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1540 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1541 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1542 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1543 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1546 /* Store the data in a cut-buffer. */
1547 void store_cutbuffer(char * ptr, int len)
1549 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1550 XRotateBuffers(GDK_DISPLAY(), 1);
1551 XStoreBytes(GDK_DISPLAY(), ptr, len);
1554 /* Retrieve data from a cut-buffer.
1555 * Returned data needs to be freed with XFree().
1557 char * retrieve_cutbuffer(int * nbytes)
1560 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1561 if (*nbytes <= 0 && ptr != 0) {
1568 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1570 struct gui_data *inst = (struct gui_data *)frontend;
1571 if (inst->pasteout_data)
1572 sfree(inst->pasteout_data);
1573 if (inst->pasteout_data_ctext)
1574 sfree(inst->pasteout_data_ctext);
1575 if (inst->pasteout_data_utf8)
1576 sfree(inst->pasteout_data_utf8);
1579 * Set up UTF-8 and compound text paste data. This only happens
1580 * if we aren't in direct-to-font mode using the D800 hack.
1582 if (!inst->direct_to_font) {
1583 wchar_t *tmp = data;
1588 inst->pasteout_data_utf8 = snewn(len*6, char);
1589 inst->pasteout_data_utf8_len = len*6;
1590 inst->pasteout_data_utf8_len =
1591 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1592 inst->pasteout_data_utf8_len,
1593 CS_UTF8, NULL, NULL, 0);
1594 if (inst->pasteout_data_utf8_len == 0) {
1595 sfree(inst->pasteout_data_utf8);
1596 inst->pasteout_data_utf8 = NULL;
1598 inst->pasteout_data_utf8 =
1599 sresize(inst->pasteout_data_utf8,
1600 inst->pasteout_data_utf8_len + 1, char);
1601 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1605 * Now let Xlib convert our UTF-8 data into compound text.
1607 list[0] = inst->pasteout_data_utf8;
1608 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1609 XCompoundTextStyle, &tp) == 0) {
1610 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1611 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1612 inst->pasteout_data_ctext_len = tp.nitems;
1615 inst->pasteout_data_ctext = NULL;
1616 inst->pasteout_data_ctext_len = 0;
1619 inst->pasteout_data_utf8 = NULL;
1620 inst->pasteout_data_utf8_len = 0;
1621 inst->pasteout_data_ctext = NULL;
1622 inst->pasteout_data_ctext_len = 0;
1625 inst->pasteout_data = snewn(len*6, char);
1626 inst->pasteout_data_len = len*6;
1627 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1628 data, len, inst->pasteout_data,
1629 inst->pasteout_data_len,
1631 if (inst->pasteout_data_len == 0) {
1632 sfree(inst->pasteout_data);
1633 inst->pasteout_data = NULL;
1635 inst->pasteout_data =
1636 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1639 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1641 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1642 inst->input_event_time)) {
1643 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
1644 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1645 GDK_SELECTION_TYPE_STRING, 1);
1646 if (inst->pasteout_data_ctext)
1647 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1648 compound_text_atom, 1);
1649 if (inst->pasteout_data_utf8)
1650 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1651 utf8_string_atom, 1);
1655 term_deselect(inst->term);
1658 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1659 guint info, guint time_stamp, gpointer data)
1661 struct gui_data *inst = (struct gui_data *)data;
1662 if (seldata->target == utf8_string_atom)
1663 gtk_selection_data_set(seldata, seldata->target, 8,
1664 (unsigned char *)inst->pasteout_data_utf8,
1665 inst->pasteout_data_utf8_len);
1666 else if (seldata->target == compound_text_atom)
1667 gtk_selection_data_set(seldata, seldata->target, 8,
1668 (unsigned char *)inst->pasteout_data_ctext,
1669 inst->pasteout_data_ctext_len);
1671 gtk_selection_data_set(seldata, seldata->target, 8,
1672 (unsigned char *)inst->pasteout_data,
1673 inst->pasteout_data_len);
1676 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1679 struct gui_data *inst = (struct gui_data *)data;
1681 term_deselect(inst->term);
1682 if (inst->pasteout_data)
1683 sfree(inst->pasteout_data);
1684 if (inst->pasteout_data_ctext)
1685 sfree(inst->pasteout_data_ctext);
1686 if (inst->pasteout_data_utf8)
1687 sfree(inst->pasteout_data_utf8);
1688 inst->pasteout_data = NULL;
1689 inst->pasteout_data_len = 0;
1690 inst->pasteout_data_ctext = NULL;
1691 inst->pasteout_data_ctext_len = 0;
1692 inst->pasteout_data_utf8 = NULL;
1693 inst->pasteout_data_utf8_len = 0;
1697 void request_paste(void *frontend)
1699 struct gui_data *inst = (struct gui_data *)frontend;
1701 * In Unix, pasting is asynchronous: all we can do at the
1702 * moment is to call gtk_selection_convert(), and when the data
1703 * comes back _then_ we can call term_do_paste().
1706 if (!inst->direct_to_font) {
1708 * First we attempt to retrieve the selection as a UTF-8
1709 * string (which we will convert to the correct code page
1710 * before sending to the session, of course). If that
1711 * fails, selection_received() will be informed and will
1712 * fall back to an ordinary string.
1714 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1716 inst->input_event_time);
1719 * If we're in direct-to-font mode, we disable UTF-8
1720 * pasting, and go straight to ordinary string data.
1722 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1723 GDK_SELECTION_TYPE_STRING,
1724 inst->input_event_time);
1728 gint idle_paste_func(gpointer data); /* forward ref */
1730 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1731 guint time, gpointer data)
1733 struct gui_data *inst = (struct gui_data *)data;
1737 int length, count, ret;
1738 int free_list_required = 0;
1739 int free_required = 0;
1742 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1744 * Failed to get a UTF-8 selection string. Try compound
1747 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1749 inst->input_event_time);
1753 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1755 * Failed to get UTF-8 or compound text. Try an ordinary
1758 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1759 GDK_SELECTION_TYPE_STRING,
1760 inst->input_event_time);
1765 * If we have data, but it's not of a type we can deal with,
1766 * we have to ignore the data.
1768 if (seldata->length > 0 &&
1769 seldata->type != GDK_SELECTION_TYPE_STRING &&
1770 seldata->type != compound_text_atom &&
1771 seldata->type != utf8_string_atom)
1775 * If we have no data, try looking in a cut buffer.
1777 if (seldata->length <= 0) {
1778 text = retrieve_cutbuffer(&length);
1781 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1782 * source, so use that as a de-facto standard. */
1783 charset = CS_ISO8859_1;
1787 * Convert COMPOUND_TEXT into UTF-8.
1789 if (seldata->type == compound_text_atom) {
1790 tp.value = seldata->data;
1791 tp.encoding = (Atom) seldata->type;
1792 tp.format = seldata->format;
1793 tp.nitems = seldata->length;
1794 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1796 if (ret != 0 || count != 1) {
1798 * Compound text failed; fall back to STRING.
1800 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1801 GDK_SELECTION_TYPE_STRING,
1802 inst->input_event_time);
1806 length = strlen(list[0]);
1808 free_list_required = 1;
1810 text = (char *)seldata->data;
1811 length = seldata->length;
1812 charset = (seldata->type == utf8_string_atom ?
1813 CS_UTF8 : inst->ucsdata.line_codepage);
1817 if (inst->pastein_data)
1818 sfree(inst->pastein_data);
1820 inst->pastein_data = snewn(length, wchar_t);
1821 inst->pastein_data_len = length;
1822 inst->pastein_data_len =
1823 mb_to_wc(charset, 0, text, length,
1824 inst->pastein_data, inst->pastein_data_len);
1826 term_do_paste(inst->term);
1828 if (term_paste_pending(inst->term))
1829 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1831 if (free_list_required)
1832 XFreeStringList(list);
1837 gint idle_paste_func(gpointer data)
1839 struct gui_data *inst = (struct gui_data *)data;
1841 if (term_paste_pending(inst->term))
1842 term_paste(inst->term);
1844 gtk_idle_remove(inst->term_paste_idle_id);
1850 void get_clip(void *frontend, wchar_t ** p, int *len)
1852 struct gui_data *inst = (struct gui_data *)frontend;
1855 *p = inst->pastein_data;
1856 *len = inst->pastein_data_len;
1860 static void set_window_titles(struct gui_data *inst)
1863 * We must always call set_icon_name after calling set_title,
1864 * since set_title will write both names. Irritating, but such
1867 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1868 if (!inst->cfg.win_name_always)
1869 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1872 void set_title(void *frontend, char *title)
1874 struct gui_data *inst = (struct gui_data *)frontend;
1875 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1876 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1877 set_window_titles(inst);
1880 void set_icon(void *frontend, char *title)
1882 struct gui_data *inst = (struct gui_data *)frontend;
1883 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1884 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1885 set_window_titles(inst);
1888 void set_sbar(void *frontend, int total, int start, int page)
1890 struct gui_data *inst = (struct gui_data *)frontend;
1891 if (!inst->cfg.scrollbar)
1893 inst->sbar_adjust->lower = 0;
1894 inst->sbar_adjust->upper = total;
1895 inst->sbar_adjust->value = start;
1896 inst->sbar_adjust->page_size = page;
1897 inst->sbar_adjust->step_increment = 1;
1898 inst->sbar_adjust->page_increment = page/2;
1899 inst->ignore_sbar = TRUE;
1900 gtk_adjustment_changed(inst->sbar_adjust);
1901 inst->ignore_sbar = FALSE;
1904 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1906 struct gui_data *inst = (struct gui_data *)data;
1908 if (!inst->cfg.scrollbar)
1910 if (!inst->ignore_sbar)
1911 term_scroll(inst->term, 1, (int)adj->value);
1914 void sys_cursor(void *frontend, int x, int y)
1917 * This is meaningless under X.
1922 * This is still called when mode==BELL_VISUAL, even though the
1923 * visual bell is handled entirely within terminal.c, because we
1924 * may want to perform additional actions on any kind of bell (for
1925 * example, taskbar flashing in Windows).
1927 void do_beep(void *frontend, int mode)
1929 if (mode == BELL_DEFAULT)
1933 int char_width(Context ctx, int uc)
1936 * Under X, any fixed-width font really _is_ fixed-width.
1937 * Double-width characters will be dealt with using a separate
1938 * font. For the moment we can simply return 1.
1940 * FIXME: but is that also true of Pango?
1945 Context get_ctx(void *frontend)
1947 struct gui_data *inst = (struct gui_data *)frontend;
1948 struct draw_ctx *dctx;
1950 if (!inst->area->window)
1953 dctx = snew(struct draw_ctx);
1955 dctx->gc = gdk_gc_new(inst->area->window);
1959 void free_ctx(Context ctx)
1961 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1962 /* struct gui_data *inst = dctx->inst; */
1963 GdkGC *gc = dctx->gc;
1969 * Draw a line of text in the window, at given character
1970 * coordinates, in given attributes.
1972 * We are allowed to fiddle with the contents of `text'.
1974 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1975 unsigned long attr, int lattr)
1977 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1978 struct gui_data *inst = dctx->inst;
1979 GdkGC *gc = dctx->gc;
1980 int ncombining, combining;
1981 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
1982 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
1984 if (attr & TATTR_COMBINING) {
1990 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
1991 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
1992 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
1997 if (inst->cfg.bold_colour && (attr & ATTR_BOLD)) {
1998 if (nfg < 16) nfg |= 8;
1999 else if (nfg >= 256) nfg |= 1;
2001 if (inst->cfg.bold_colour && (attr & ATTR_BLINK)) {
2002 if (nbg < 16) nbg |= 8;
2003 else if (nbg >= 256) nbg |= 1;
2005 if ((attr & TATTR_ACTCURS) && !monochrome) {
2010 fontid = shadow = 0;
2012 if (attr & ATTR_WIDE) {
2019 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
2026 if (!inst->fonts[fontid]) {
2029 * Fall back through font ids with subsets of this one's
2030 * set bits, in order.
2032 for (i = fontid; i-- > 0 ;) {
2034 continue; /* some other bit is set */
2035 if (inst->fonts[i]) {
2040 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2043 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2045 if (x >= inst->term->cols)
2047 if (x + len*2*widefactor > inst->term->cols)
2048 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2056 r.x = x*inst->font_width+inst->cfg.window_border;
2057 r.y = y*inst->font_height+inst->cfg.window_border;
2058 r.width = rlen*widefactor*inst->font_width;
2059 r.height = inst->font_height;
2060 gdk_gc_set_clip_rectangle(gc, &r);
2063 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
2064 gdk_draw_rectangle(inst->pixmap, gc, 1,
2065 x*inst->font_width+inst->cfg.window_border,
2066 y*inst->font_height+inst->cfg.window_border,
2067 rlen*widefactor*inst->font_width, inst->font_height);
2069 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2074 * FIXME: this length is hardwired on the assumption that
2075 * conversions from wide to multibyte characters will
2076 * never generate more than 10 bytes for a single wide
2079 gcs = snewn(len*10+1, gchar);
2081 for (combining = 0; combining < ncombining; combining++) {
2082 int mblen = wc_to_mb(inst->fonts[fontid]->real_charset, 0,
2083 text + combining, len, gcs, len*10+1, ".",
2085 unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid],
2086 x*inst->font_width+inst->cfg.window_border,
2087 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2088 gcs, mblen, widefactor > 1, bold, inst->font_width);
2094 if (attr & ATTR_UNDER) {
2095 int uheight = inst->fonts[0]->ascent + 1;
2096 if (uheight >= inst->font_height)
2097 uheight = inst->font_height - 1;
2098 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
2099 y*inst->font_height + uheight + inst->cfg.window_border,
2100 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
2101 y*inst->font_height + uheight + inst->cfg.window_border);
2104 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2106 * I can't find any plausible StretchBlt equivalent in the
2107 * X server, so I'm going to do this the slow and painful
2108 * way. This will involve repeated calls to
2109 * gdk_draw_pixmap() to stretch the text horizontally. It's
2110 * O(N^2) in time and O(N) in network bandwidth, but you
2111 * try thinking of a better way. :-(
2114 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2115 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2116 x*inst->font_width+inst->cfg.window_border + 2*i,
2117 y*inst->font_height+inst->cfg.window_border,
2118 x*inst->font_width+inst->cfg.window_border + 2*i+1,
2119 y*inst->font_height+inst->cfg.window_border,
2120 len * widefactor * inst->font_width - i, inst->font_height);
2123 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2125 /* Now stretch vertically, in the same way. */
2126 if ((lattr & LATTR_MODE) == LATTR_BOT)
2130 for (i = 0; i < inst->font_height; i+=2) {
2131 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2132 x*inst->font_width+inst->cfg.window_border,
2133 y*inst->font_height+inst->cfg.window_border+dt*i+db,
2134 x*inst->font_width+inst->cfg.window_border,
2135 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
2136 len * widefactor * inst->font_width, inst->font_height-i-1);
2142 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2143 unsigned long attr, int lattr)
2145 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2146 struct gui_data *inst = dctx->inst;
2147 GdkGC *gc = dctx->gc;
2150 do_text_internal(ctx, x, y, text, len, attr, lattr);
2152 if (attr & ATTR_WIDE) {
2158 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2160 if (x >= inst->term->cols)
2162 if (x + len*2*widefactor > inst->term->cols)
2163 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2167 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2168 x*inst->font_width+inst->cfg.window_border,
2169 y*inst->font_height+inst->cfg.window_border,
2170 x*inst->font_width+inst->cfg.window_border,
2171 y*inst->font_height+inst->cfg.window_border,
2172 len*widefactor*inst->font_width, inst->font_height);
2175 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2176 unsigned long attr, int lattr)
2178 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2179 struct gui_data *inst = dctx->inst;
2180 GdkGC *gc = dctx->gc;
2182 int active, passive, widefactor;
2184 if (attr & TATTR_PASCURS) {
2185 attr &= ~TATTR_PASCURS;
2189 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
2190 attr &= ~TATTR_ACTCURS;
2194 do_text_internal(ctx, x, y, text, len, attr, lattr);
2196 if (attr & TATTR_COMBINING)
2199 if (attr & ATTR_WIDE) {
2205 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2207 if (x >= inst->term->cols)
2209 if (x + len*2*widefactor > inst->term->cols)
2210 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2214 if (inst->cfg.cursor_type == 0) {
2216 * An active block cursor will already have been done by
2217 * the above do_text call, so we only need to do anything
2221 gdk_gc_set_foreground(gc, &inst->cols[261]);
2222 gdk_draw_rectangle(inst->pixmap, gc, 0,
2223 x*inst->font_width+inst->cfg.window_border,
2224 y*inst->font_height+inst->cfg.window_border,
2225 len*widefactor*inst->font_width-1, inst->font_height-1);
2229 int startx, starty, dx, dy, length, i;
2233 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2234 char_width = 2*inst->font_width;
2236 char_width = inst->font_width;
2238 if (inst->cfg.cursor_type == 1) {
2239 uheight = inst->fonts[0]->ascent + 1;
2240 if (uheight >= inst->font_height)
2241 uheight = inst->font_height - 1;
2243 startx = x * inst->font_width + inst->cfg.window_border;
2244 starty = y * inst->font_height + inst->cfg.window_border + uheight;
2247 length = len * widefactor * char_width;
2250 if (attr & TATTR_RIGHTCURS)
2251 xadjust = char_width - 1;
2252 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2253 starty = y * inst->font_height + inst->cfg.window_border;
2256 length = inst->font_height;
2259 gdk_gc_set_foreground(gc, &inst->cols[261]);
2261 for (i = 0; i < length; i++) {
2263 gdk_draw_point(inst->pixmap, gc, startx, starty);
2268 } else if (active) {
2269 gdk_draw_line(inst->pixmap, gc, startx, starty,
2270 startx + (length-1) * dx, starty + (length-1) * dy);
2271 } /* else no cursor (e.g., blinked off) */
2274 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2275 x*inst->font_width+inst->cfg.window_border,
2276 y*inst->font_height+inst->cfg.window_border,
2277 x*inst->font_width+inst->cfg.window_border,
2278 y*inst->font_height+inst->cfg.window_border,
2279 len*widefactor*inst->font_width, inst->font_height);
2282 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2285 * Truly hideous hack: GTK doesn't allow us to set the mouse
2286 * cursor foreground and background colours unless we've _also_
2287 * created our own cursor from bitmaps. Therefore, I need to
2288 * load the `cursor' font and draw glyphs from it on to
2289 * pixmaps, in order to construct my cursors with the fg and bg
2290 * I want. This is a gross hack, but it's more self-contained
2291 * than linking in Xlib to find the X window handle to
2292 * inst->area and calling XRecolorCursor, and it's more
2293 * futureproof than hard-coding the shapes as bitmap arrays.
2295 static GdkFont *cursor_font = NULL;
2296 GdkPixmap *source, *mask;
2298 GdkColor cfg = { 0, 65535, 65535, 65535 };
2299 GdkColor cbg = { 0, 0, 0, 0 };
2300 GdkColor dfg = { 1, 65535, 65535, 65535 };
2301 GdkColor dbg = { 0, 0, 0, 0 };
2304 gint lb, rb, wid, asc, desc, w, h, x, y;
2306 if (cursor_val == -2) {
2307 gdk_font_unref(cursor_font);
2311 if (cursor_val >= 0 && !cursor_font) {
2312 cursor_font = gdk_font_load("cursor");
2314 gdk_font_ref(cursor_font);
2318 * Get the text extent of the cursor in question. We use the
2319 * mask character for this, because it's typically slightly
2320 * bigger than the main character.
2322 if (cursor_val >= 0) {
2324 text[0] = (char)cursor_val + 1;
2325 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2326 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2332 source = gdk_pixmap_new(NULL, w, h, 1);
2333 mask = gdk_pixmap_new(NULL, w, h, 1);
2336 * Draw the mask character on the mask pixmap.
2338 gc = gdk_gc_new(mask);
2339 gdk_gc_set_foreground(gc, &dbg);
2340 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2341 if (cursor_val >= 0) {
2343 text[0] = (char)cursor_val + 1;
2344 gdk_gc_set_foreground(gc, &dfg);
2345 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2350 * Draw the main character on the source pixmap.
2352 gc = gdk_gc_new(source);
2353 gdk_gc_set_foreground(gc, &dbg);
2354 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2355 if (cursor_val >= 0) {
2357 text[0] = (char)cursor_val;
2358 gdk_gc_set_foreground(gc, &dfg);
2359 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2364 * Create the cursor.
2366 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2371 gdk_pixmap_unref(source);
2372 gdk_pixmap_unref(mask);
2377 void modalfatalbox(char *p, ...)
2380 fprintf(stderr, "FATAL ERROR: ");
2382 vfprintf(stderr, p, ap);
2384 fputc('\n', stderr);
2388 void cmdline_error(char *p, ...)
2391 fprintf(stderr, "%s: ", appname);
2393 vfprintf(stderr, p, ap);
2395 fputc('\n', stderr);
2399 char *get_x_display(void *frontend)
2401 return gdk_get_display();
2404 long get_windowid(void *frontend)
2406 struct gui_data *inst = (struct gui_data *)frontend;
2407 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2410 static void help(FILE *fp) {
2412 "pterm option summary:\n"
2414 " --display DISPLAY Specify X display to use (note '--')\n"
2415 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2416 " -fn FONT Normal text font\n"
2417 " -fb FONT Bold text font\n"
2418 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2419 " -sl LINES Number of lines of scrollback\n"
2420 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2421 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2422 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2423 " -T TITLE Window title\n"
2424 " -ut, +ut Do(default) or do not update utmp\n"
2425 " -ls, +ls Do(default) or do not make shell a login shell\n"
2426 " -sb, +sb Do(default) or do not display a scrollbar\n"
2427 " -log PATH Log all output to a file\n"
2428 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2429 " -xrm RESOURCE-STRING Set an X resource\n"
2430 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2431 ) < 0 || fflush(fp) < 0) {
2432 perror("output error");
2437 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2438 struct gui_data *inst, Config *cfg)
2444 * Macros to make argument handling easier. Note that because
2445 * they need to call `continue', they cannot be contained in
2446 * the usual do {...} while (0) wrapper to make them
2447 * syntactically single statements; hence it is not legal to
2448 * use one of these macros as an unbraced statement between
2451 #define EXPECTS_ARG { \
2452 if (--argc <= 0) { \
2454 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2459 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2461 while (--argc > 0) {
2466 * Shameless cheating. Debian requires all X terminal
2467 * emulators to support `-T title'; but
2468 * cmdline_process_param will eat -T (it means no-pty) and
2469 * complain that pterm doesn't support it. So, in pterm
2470 * only, we convert -T into -title.
2472 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2476 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2477 do_everything ? 1 : -1, cfg);
2480 cmdline_error("option \"%s\" requires an argument", p);
2481 } else if (ret == 2) {
2482 --argc, ++argv; /* skip next argument */
2484 } else if (ret == 1) {
2488 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2491 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2492 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2494 } else if (!strcmp(p, "-fb")) {
2497 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2498 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2500 } else if (!strcmp(p, "-fw")) {
2503 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2504 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2506 } else if (!strcmp(p, "-fwb")) {
2509 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2510 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2512 } else if (!strcmp(p, "-cs")) {
2515 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2516 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2518 } else if (!strcmp(p, "-geometry")) {
2524 flags = XParseGeometry(val, &x, &y, &w, &h);
2525 if (flags & WidthValue)
2526 cfg->width = (int)w;
2527 if (flags & HeightValue)
2528 cfg->height = (int)h;
2530 if (flags & (XValue | YValue)) {
2533 inst->gotpos = TRUE;
2534 inst->gravity = ((flags & XNegative ? 1 : 0) |
2535 (flags & YNegative ? 2 : 0));
2538 } else if (!strcmp(p, "-sl")) {
2541 cfg->savelines = atoi(val);
2543 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2544 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2545 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2550 if (!gdk_color_parse(val, &col)) {
2552 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2556 index = (!strcmp(p, "-fg") ? 0 :
2557 !strcmp(p, "-bg") ? 2 :
2558 !strcmp(p, "-bfg") ? 1 :
2559 !strcmp(p, "-bbg") ? 3 :
2560 !strcmp(p, "-cfg") ? 4 :
2561 !strcmp(p, "-cbg") ? 5 : -1);
2562 assert(index != -1);
2563 cfg->colours[index][0] = col.red / 256;
2564 cfg->colours[index][1] = col.green / 256;
2565 cfg->colours[index][2] = col.blue / 256;
2568 } else if (use_pty_argv && !strcmp(p, "-e")) {
2569 /* This option swallows all further arguments. */
2575 pty_argv = snewn(argc+1, char *);
2577 for (i = 0; i < argc; i++)
2578 pty_argv[i] = argv[i];
2579 pty_argv[argc] = NULL;
2580 break; /* finished command-line processing */
2582 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2585 } else if (!strcmp(p, "-title")) {
2588 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2589 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2591 } else if (!strcmp(p, "-log")) {
2594 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2595 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2596 cfg->logtype = LGTYP_DEBUG;
2598 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2600 cfg->stamp_utmp = 0;
2602 } else if (!strcmp(p, "-ut")) {
2604 cfg->stamp_utmp = 1;
2606 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2608 cfg->login_shell = 0;
2610 } else if (!strcmp(p, "-ls")) {
2612 cfg->login_shell = 1;
2614 } else if (!strcmp(p, "-nethack")) {
2616 cfg->nethack_keypad = 1;
2618 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2622 } else if (!strcmp(p, "-sb")) {
2626 } else if (!strcmp(p, "-name")) {
2630 } else if (!strcmp(p, "-xrm")) {
2632 provide_xrm_string(val);
2634 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2638 } else if (!strcmp(p, "-pgpfp")) {
2642 } else if(p[0] != '-' && (!do_everything ||
2643 process_nonoption_arg(p, cfg,
2649 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2656 int uxsel_input_add(int fd, int rwx) {
2658 if (rwx & 1) flags |= GDK_INPUT_READ;
2659 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2660 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2662 return gdk_input_add(fd, flags, fd_input_func, NULL);
2665 void uxsel_input_remove(int id) {
2666 gdk_input_remove(id);
2669 void setup_fonts_ucs(struct gui_data *inst)
2672 unifont_destroy(inst->fonts[0]);
2674 unifont_destroy(inst->fonts[1]);
2676 unifont_destroy(inst->fonts[2]);
2678 unifont_destroy(inst->fonts[3]);
2680 inst->fonts[0] = unifont_create(inst->area, inst->cfg.font.name,
2682 inst->cfg.shadowboldoffset,
2683 inst->cfg.shadowbold);
2684 if (!inst->fonts[0]) {
2685 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2686 inst->cfg.font.name);
2690 if (inst->cfg.shadowbold || !inst->cfg.boldfont.name[0]) {
2691 inst->fonts[1] = NULL;
2693 inst->fonts[1] = unifont_create(inst->area, inst->cfg.boldfont.name,
2695 inst->cfg.shadowboldoffset,
2696 inst->cfg.shadowbold);
2697 if (!inst->fonts[1]) {
2698 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2699 inst->cfg.boldfont.name);
2704 if (inst->cfg.widefont.name[0]) {
2705 inst->fonts[2] = unifont_create(inst->area, inst->cfg.widefont.name,
2707 inst->cfg.shadowboldoffset,
2708 inst->cfg.shadowbold);
2709 if (!inst->fonts[2]) {
2710 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2711 inst->cfg.widefont.name);
2715 inst->fonts[2] = NULL;
2718 if (inst->cfg.shadowbold || !inst->cfg.wideboldfont.name[0]) {
2719 inst->fonts[3] = NULL;
2721 inst->fonts[3] = unifont_create(inst->area,
2722 inst->cfg.wideboldfont.name, TRUE,
2723 TRUE, inst->cfg.shadowboldoffset,
2724 inst->cfg.shadowbold);
2725 if (!inst->fonts[3]) {
2726 fprintf(stderr, "%s: unable to load wide bold font \"%s\"\n", appname,
2727 inst->cfg.boldfont.name);
2732 inst->font_width = inst->fonts[0]->width;
2733 inst->font_height = inst->fonts[0]->height;
2735 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2736 inst->cfg.utf8_override,
2737 inst->fonts[0]->public_charset,
2741 void set_geom_hints(struct gui_data *inst)
2744 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2745 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2746 geom.max_width = geom.max_height = -1;
2747 geom.base_width = 2*inst->cfg.window_border;
2748 geom.base_height = 2*inst->cfg.window_border;
2749 geom.width_inc = inst->font_width;
2750 geom.height_inc = inst->font_height;
2751 geom.min_aspect = geom.max_aspect = 0;
2752 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2753 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2754 GDK_HINT_RESIZE_INC);
2757 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2759 struct gui_data *inst = (struct gui_data *)data;
2760 term_clrsb(inst->term);
2763 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2765 struct gui_data *inst = (struct gui_data *)data;
2766 term_pwron(inst->term, TRUE);
2768 ldisc_send(inst->ldisc, NULL, 0, 0);
2771 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2773 struct gui_data *inst = (struct gui_data *)data;
2774 term_copyall(inst->term);
2777 void special_menuitem(GtkMenuItem *item, gpointer data)
2779 struct gui_data *inst = (struct gui_data *)data;
2780 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2784 inst->back->special(inst->backhandle, code);
2787 void about_menuitem(GtkMenuItem *item, gpointer data)
2789 struct gui_data *inst = (struct gui_data *)data;
2790 about_box(inst->window);
2793 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2795 struct gui_data *inst = (struct gui_data *)data;
2796 showeventlog(inst->eventlogstuff, inst->window);
2799 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2801 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2802 static const int ww[] = {
2803 256, 257, 258, 259, 260, 261,
2804 0, 8, 1, 9, 2, 10, 3, 11,
2805 4, 12, 5, 13, 6, 14, 7, 15
2807 struct gui_data *inst = (struct gui_data *)data;
2808 char *title = dupcat(appname, " Reconfiguration", NULL);
2809 Config cfg2, oldcfg;
2812 assert(lenof(ww) == NCFGCOLOURS);
2814 if (inst->reconfiguring)
2817 inst->reconfiguring = TRUE;
2819 cfg2 = inst->cfg; /* structure copy */
2821 if (do_config_box(title, &cfg2, 1,
2822 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2824 oldcfg = inst->cfg; /* structure copy */
2825 inst->cfg = cfg2; /* structure copy */
2827 /* Pass new config data to the logging module */
2828 log_reconfig(inst->logctx, &cfg2);
2830 * Flush the line discipline's edit buffer in the case
2831 * where local editing has just been disabled.
2834 ldisc_send(inst->ldisc, NULL, 0, 0);
2835 /* Pass new config data to the terminal */
2836 term_reconfig(inst->term, &cfg2);
2837 /* Pass new config data to the back end */
2839 inst->back->reconfig(inst->backhandle, &cfg2);
2842 * Just setting inst->cfg is sufficient to cause colour
2843 * setting changes to appear on the next ESC]R palette
2844 * reset. But we should also check whether any colour
2845 * settings have been changed, and revert the ones that
2846 * have to the new default, on the assumption that the user
2847 * is most likely to want an immediate update.
2849 for (i = 0; i < NCFGCOLOURS; i++) {
2850 if (oldcfg.colours[i][0] != cfg2.colours[i][0] ||
2851 oldcfg.colours[i][1] != cfg2.colours[i][1] ||
2852 oldcfg.colours[i][2] != cfg2.colours[i][2]) {
2853 real_palette_set(inst, ww[i], cfg2.colours[i][0],
2855 cfg2.colours[i][2]);
2858 * If the default background has changed, we must
2859 * repaint the space in between the window border
2860 * and the text area.
2863 set_window_background(inst);
2864 draw_backing_rect(inst);
2870 * If the scrollbar needs to be shown, hidden, or moved
2871 * from one end to the other of the window, do so now.
2873 if (oldcfg.scrollbar != cfg2.scrollbar) {
2875 gtk_widget_show(inst->sbar);
2877 gtk_widget_hide(inst->sbar);
2879 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2880 gtk_box_reorder_child(inst->hbox, inst->sbar,
2881 cfg2.scrollbar_on_left ? 0 : 1);
2885 * Change the window title, if required.
2887 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2888 set_title(inst, cfg2.wintitle);
2889 set_window_titles(inst);
2892 * Redo the whole tangled fonts and Unicode mess if
2895 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2896 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2897 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2898 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
2899 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
2900 oldcfg.vtmode != cfg2.vtmode ||
2901 oldcfg.shadowbold != cfg2.shadowbold) {
2902 setup_fonts_ucs(inst);
2908 * Resize the window.
2910 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2911 oldcfg.window_border != cfg2.window_border || need_size) {
2912 set_geom_hints(inst);
2913 request_resize(inst, cfg2.width, cfg2.height);
2916 * The above will have caused a call to term_size() for
2917 * us if it happened. If the user has fiddled with only
2918 * the scrollback size, the above will not have
2919 * happened and we will need an explicit term_size()
2922 if (oldcfg.savelines != cfg2.savelines)
2923 term_size(inst->term, inst->term->rows, inst->term->cols,
2927 term_invalidate(inst->term);
2930 * We do an explicit full redraw here to ensure the window
2931 * border has been redrawn as well as the text area.
2933 gtk_widget_queue_draw(inst->area);
2936 inst->reconfiguring = FALSE;
2939 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
2942 * Re-execing ourself is not an exact science under Unix. I do
2943 * the best I can by using /proc/self/exe if available and by
2944 * assuming argv[0] can be found on $PATH if not.
2946 * Note that we also have to reconstruct the elements of the
2947 * original argv which gtk swallowed, since the user wants the
2948 * new session to appear on the same X display as the old one.
2956 * Collect the arguments with which to re-exec ourself.
2958 va_start(ap, fd_to_close);
2959 n = 2; /* progname and terminating NULL */
2960 n += inst->ngtkargs;
2961 while (va_arg(ap, char *) != NULL)
2965 args = snewn(n, char *);
2966 args[0] = inst->progname;
2968 for (i = 0; i < inst->ngtkargs; i++)
2969 args[i+1] = inst->gtkargvstart[i];
2972 va_start(ap, fd_to_close);
2973 while ((args[i++] = va_arg(ap, char *)) != NULL);
2979 * Do the double fork.
2992 } else if (pid2 > 0) {
2994 * First child has successfully forked second child. My
2995 * Work Here Is Done. Note the use of _exit rather than
2996 * exit: the latter appears to cause destroy messages
2997 * to be sent to the X server. I suspect gtk uses
3004 * If we reach here, we are the second child, so we now
3005 * actually perform the exec.
3007 if (fd_to_close >= 0)
3010 execv("/proc/self/exe", args);
3011 execvp(inst->progname, args);
3017 waitpid(pid, &status, 0);
3022 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3024 struct gui_data *inst = (struct gui_data *)gdata;
3026 * For this feature we must marshal cfg and (possibly) pty_argv
3027 * into a byte stream, create a pipe, and send this byte stream
3028 * to the child through the pipe.
3035 if (pipe(pipefd) < 0) {
3040 size = sizeof(inst->cfg);
3041 if (use_pty_argv && pty_argv) {
3042 for (i = 0; pty_argv[i]; i++)
3043 size += strlen(pty_argv[i]) + 1;
3046 data = snewn(size, char);
3047 memcpy(data, &inst->cfg, sizeof(inst->cfg));
3048 if (use_pty_argv && pty_argv) {
3049 int p = sizeof(inst->cfg);
3050 for (i = 0; pty_argv[i]; i++) {
3051 strcpy(data + p, pty_argv[i]);
3052 p += strlen(pty_argv[i]) + 1;
3057 sprintf(option, "---[%d,%d]", pipefd[0], size);
3058 fcntl(pipefd[0], F_SETFD, 0);
3059 fork_and_exec_self(inst, pipefd[1], option, NULL);
3063 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3066 perror("write to pipe");
3071 int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3073 int fd, i, ret, size;
3076 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3077 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3081 data = snewn(size, char);
3083 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3086 perror("read from pipe");
3088 } else if (i < size) {
3089 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3094 memcpy(cfg, data, sizeof(Config));
3095 if (use_pty_argv && size > sizeof(Config)) {
3099 while (i < size && data[i]) i++;
3101 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3108 pty_argv = snewn(n+1, char *);
3114 while (i < size && data[i]) i++;
3117 pty_argv[n++] = dupstr(p);
3124 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3126 struct gui_data *inst = (struct gui_data *)data;
3128 fork_and_exec_self(inst, -1, NULL);
3131 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3133 struct gui_data *inst = (struct gui_data *)data;
3136 logevent(inst, "----- Session restarted -----");
3137 term_pwron(inst->term, FALSE);
3138 start_backend(inst);
3139 inst->exited = FALSE;
3143 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3145 struct gui_data *inst = (struct gui_data *)data;
3146 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3148 fork_and_exec_self(inst, -1, "-load", str, NULL);
3151 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3153 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3158 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3160 struct gui_data *inst = (struct gui_data *)data;
3161 struct sesslist sesslist;
3164 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3165 (GtkCallback)gtk_widget_destroy, NULL);
3167 get_sesslist(&sesslist, TRUE);
3168 for (i = 1; i < sesslist.nsessions; i++) {
3169 GtkWidget *menuitem =
3170 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3171 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3172 gtk_widget_show(menuitem);
3173 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3174 dupstr(sesslist.sessions[i]));
3175 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3176 GTK_SIGNAL_FUNC(saved_session_menuitem),
3178 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3179 GTK_SIGNAL_FUNC(saved_session_freedata),
3182 get_sesslist(&sesslist, FALSE); /* free up */
3185 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3189 GdkBitmap *iconmask;
3190 #if GTK_CHECK_VERSION(2,0,0)
3198 gtk_widget_realize(window);
3199 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3200 NULL, (gchar **)icon[0]);
3201 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3203 #if GTK_CHECK_VERSION(2,0,0)
3205 for (n = 0; n < n_icon; n++) {
3207 g_list_append(iconlist,
3208 gdk_pixbuf_new_from_xpm_data((const gchar **)
3211 gdk_window_set_icon_list(window->window, iconlist);
3215 void update_specials_menu(void *frontend)
3217 struct gui_data *inst = (struct gui_data *)frontend;
3219 const struct telnet_special *specials;
3222 specials = inst->back->get_specials(inst->backhandle);
3226 /* I believe this disposes of submenus too. */
3227 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3228 (GtkCallback)gtk_widget_destroy, NULL);
3231 GtkWidget *menu = inst->specialsmenu;
3232 /* A lame "stack" for submenus that will do for now. */
3233 GtkWidget *saved_menu = NULL;
3235 for (i = 0; nesting > 0; i++) {
3236 GtkWidget *menuitem = NULL;
3237 switch (specials[i].code) {
3239 assert (nesting < 2);
3240 saved_menu = menu; /* XXX lame stacking */
3241 menu = gtk_menu_new();
3242 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3243 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3244 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3245 gtk_widget_show(menuitem);
3252 menu = saved_menu; /* XXX lame stacking */
3257 menuitem = gtk_menu_item_new();
3260 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3261 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3262 GINT_TO_POINTER(specials[i].code));
3263 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3264 GTK_SIGNAL_FUNC(special_menuitem), inst);
3268 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3269 gtk_widget_show(menuitem);
3272 gtk_widget_show(inst->specialsitem1);
3273 gtk_widget_show(inst->specialsitem2);
3275 gtk_widget_hide(inst->specialsitem1);
3276 gtk_widget_hide(inst->specialsitem2);
3280 static void start_backend(struct gui_data *inst)
3282 extern Backend *select_backend(Config *cfg);
3286 inst->back = select_backend(&inst->cfg);
3288 error = inst->back->init((void *)inst, &inst->backhandle,
3289 &inst->cfg, inst->cfg.host, inst->cfg.port,
3290 &realhost, inst->cfg.tcp_nodelay,
3291 inst->cfg.tcp_keepalives);
3294 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3295 inst->cfg.host, error);
3296 inst->exited = TRUE;
3297 fatal_message_box(inst->window, msg);
3302 if (inst->cfg.wintitle[0]) {
3303 set_title(inst, inst->cfg.wintitle);
3304 set_icon(inst, inst->cfg.wintitle);
3306 char *title = make_default_wintitle(realhost);
3307 set_title(inst, title);
3308 set_icon(inst, title);
3313 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3315 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3318 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3321 gtk_widget_hide(inst->restartitem);
3324 int pt_main(int argc, char **argv)
3326 extern int cfgbox(Config *cfg);
3327 struct gui_data *inst;
3330 * Create an instance structure and initialise to zeroes
3332 inst = snew(struct gui_data);
3333 memset(inst, 0, sizeof(*inst));
3334 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3335 inst->busy_status = BUSY_NOT;
3337 /* defer any child exit handling until we're ready to deal with
3339 block_signal(SIGCHLD, 1);
3341 inst->progname = argv[0];
3343 * Copy the original argv before letting gtk_init fiddle with
3344 * it. It will be required later.
3348 inst->gtkargvstart = snewn(argc-1, char *);
3349 for (i = 1; i < argc; i++)
3350 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3352 gtk_init(&argc, &argv);
3353 inst->ngtkargs = oldargc - argc;
3356 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3357 read_dupsession_data(inst, &inst->cfg, argv[1]);
3358 /* Splatter this argument so it doesn't clutter a ps listing */
3359 memset(argv[1], 0, strlen(argv[1]));
3361 /* By default, we bring up the config dialog, rather than launching
3362 * a session. This gets set to TRUE if something happens to change
3363 * that (e.g., a hostname is specified on the command-line). */
3364 int allow_launch = FALSE;
3365 if (do_cmdline(argc, argv, 0, &allow_launch, inst, &inst->cfg))
3366 exit(1); /* pre-defaults pass to get -class */
3367 do_defaults(NULL, &inst->cfg);
3368 if (do_cmdline(argc, argv, 1, &allow_launch, inst, &inst->cfg))
3369 exit(1); /* post-defaults, do everything */
3371 cmdline_run_saved(&inst->cfg);
3374 allow_launch = TRUE;
3376 if ((!allow_launch || !cfg_launchable(&inst->cfg)) &&
3377 !cfgbox(&inst->cfg))
3378 exit(0); /* config box hit Cancel */
3381 if (!compound_text_atom)
3382 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3383 if (!utf8_string_atom)
3384 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3386 inst->area = gtk_drawing_area_new();
3388 setup_fonts_ucs(inst);
3391 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3394 * Set up the colour map.
3396 palette_reset(inst);
3398 inst->width = inst->cfg.width;
3399 inst->height = inst->cfg.height;
3401 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3402 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3403 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
3404 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3405 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3406 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3408 * We always create the scrollbar; it remains invisible if
3409 * unwanted, so we can pop it up quickly if it suddenly becomes
3412 if (inst->cfg.scrollbar_on_left)
3413 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3414 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3415 if (!inst->cfg.scrollbar_on_left)
3416 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3418 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3420 set_geom_hints(inst);
3422 gtk_widget_show(inst->area);
3423 if (inst->cfg.scrollbar)
3424 gtk_widget_show(inst->sbar);
3426 gtk_widget_hide(inst->sbar);
3427 gtk_widget_show(GTK_WIDGET(inst->hbox));
3430 int x = inst->xpos, y = inst->ypos;
3432 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3433 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3434 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3435 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3436 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3439 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3440 GTK_SIGNAL_FUNC(destroy), inst);
3441 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3442 GTK_SIGNAL_FUNC(delete_window), inst);
3443 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3444 GTK_SIGNAL_FUNC(key_event), inst);
3445 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3446 GTK_SIGNAL_FUNC(key_event), inst);
3447 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3448 GTK_SIGNAL_FUNC(focus_event), inst);
3449 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3450 GTK_SIGNAL_FUNC(focus_event), inst);
3451 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3452 GTK_SIGNAL_FUNC(configure_area), inst);
3453 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3454 GTK_SIGNAL_FUNC(expose_area), inst);
3455 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3456 GTK_SIGNAL_FUNC(button_event), inst);
3457 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3458 GTK_SIGNAL_FUNC(button_event), inst);
3459 #if GTK_CHECK_VERSION(2,0,0)
3460 gtk_signal_connect(GTK_OBJECT(inst->area), "scroll_event",
3461 GTK_SIGNAL_FUNC(scroll_event), inst);
3463 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3464 GTK_SIGNAL_FUNC(motion_event), inst);
3465 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3466 GTK_SIGNAL_FUNC(selection_received), inst);
3467 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3468 GTK_SIGNAL_FUNC(selection_get), inst);
3469 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3470 GTK_SIGNAL_FUNC(selection_clear), inst);
3471 if (inst->cfg.scrollbar)
3472 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3473 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3474 gtk_widget_add_events(GTK_WIDGET(inst->area),
3475 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3476 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3477 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3480 extern const char *const *const main_icon[];
3481 extern const int n_main_icon;
3482 set_window_icon(inst->window, main_icon, n_main_icon);
3485 gtk_widget_show(inst->window);
3487 set_window_background(inst);
3490 * Set up the Ctrl+rightclick context menu.
3493 GtkWidget *menuitem;
3495 extern const int use_event_log, new_session, saved_sessions;
3497 inst->menu = gtk_menu_new();
3499 #define MKMENUITEM(title, func) do { \
3500 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3501 gtk_menu_item_new(); \
3502 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3503 gtk_widget_show(menuitem); \
3505 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3506 GTK_SIGNAL_FUNC(func), inst); \
3509 MKMENUITEM("New Session", new_session_menuitem);
3510 MKMENUITEM("Restart Session", restart_session_menuitem);
3511 inst->restartitem = menuitem;
3512 gtk_widget_hide(inst->restartitem);
3513 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3514 if (saved_sessions) {
3515 inst->sessionsmenu = gtk_menu_new();
3516 /* sessionsmenu will be updated when it's invoked */
3517 /* XXX is this the right way to do dynamic menus in Gtk? */
3518 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3519 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3520 inst->sessionsmenu);
3522 MKMENUITEM(NULL, NULL);
3523 MKMENUITEM("Change Settings", change_settings_menuitem);
3524 MKMENUITEM(NULL, NULL);
3526 MKMENUITEM("Event Log", event_log_menuitem);
3527 MKMENUITEM("Special Commands", NULL);
3528 inst->specialsmenu = gtk_menu_new();
3529 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3530 inst->specialsitem1 = menuitem;
3531 MKMENUITEM(NULL, NULL);
3532 inst->specialsitem2 = menuitem;
3533 gtk_widget_hide(inst->specialsitem1);
3534 gtk_widget_hide(inst->specialsitem2);
3535 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3536 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3537 MKMENUITEM("Copy All", copy_all_menuitem);
3538 MKMENUITEM(NULL, NULL);
3539 s = dupcat("About ", appname, NULL);
3540 MKMENUITEM(s, about_menuitem);
3545 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3546 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3547 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3548 inst->blankcursor = make_mouse_ptr(inst, -1);
3549 make_mouse_ptr(inst, -2); /* clean up cursor font */
3550 inst->currcursor = inst->textcursor;
3551 show_mouseptr(inst, 1);
3553 inst->eventlogstuff = eventlogstuff_new();
3555 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3556 inst->logctx = log_init(inst, &inst->cfg);
3557 term_provide_logctx(inst->term, inst->logctx);
3561 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3563 start_backend(inst);
3565 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3567 /* now we're reday to deal with the child exit handler being
3569 block_signal(SIGCHLD, 0);
3572 * Block SIGPIPE: if we attempt Duplicate Session or similar
3573 * and it falls over in some way, we certainly don't want
3574 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3575 * this _after_ (at least pterm) forks off its child process,
3576 * since the child wants SIGPIPE handled in the usual way.
3578 block_signal(SIGPIPE, 1);
3580 inst->exited = FALSE;