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 */
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)}
36 /* Colours come in two flavours: configurable, and xterm-extended. */
37 #define NCFGCOLOURS (lenof(((Config *)0)->colours))
38 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
39 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
41 GdkAtom compound_text_atom, utf8_string_atom;
43 extern char **pty_argv; /* declared in pty.c */
44 extern int use_pty_argv;
47 * Timers are global across all sessions (even if we were handling
48 * multiple sessions, which we aren't), so the current timer ID is
51 static guint timer_id = 0;
54 GtkWidget *window, *area, *sbar;
56 GtkAdjustment *sbar_adjust;
57 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
59 GtkWidget *sessionsmenu;
61 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
66 int xpos, ypos, gotpos, gravity;
67 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
68 GdkColor cols[NALLCOLOURS];
70 wchar_t *pastein_data;
73 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
74 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
75 int font_width, font_height;
80 guint term_paste_idle_id;
83 char wintitle[sizeof(((Config *)0)->wintitle)];
84 char icontitle[sizeof(((Config *)0)->wintitle)];
85 int master_fd, master_func_id;
92 struct unicode_data ucsdata;
95 char *progname, **gtkargvstart;
97 guint32 input_event_time; /* Timestamp of the most recent input event. */
103 struct gui_data *inst;
106 static int send_raw_mouse;
108 static char *app_name = "pterm";
110 static void start_backend(struct gui_data *inst);
112 char *x_get_default(const char *key)
114 return XGetDefault(GDK_DISPLAY(), app_name, key);
117 void connection_fatal(void *frontend, char *p, ...)
119 struct gui_data *inst = (struct gui_data *)frontend;
124 msg = dupvprintf(p, ap);
127 fatal_message_box(inst->window, msg);
129 if (inst->cfg.close_on_exit == FORCE_ON)
134 * Default settings that are specific to pterm.
136 FontSpec platform_default_fontspec(const char *name)
139 if (!strcmp(name, "Font"))
140 strcpy(ret.name, "fixed");
146 Filename platform_default_filename(const char *name)
149 if (!strcmp(name, "LogFileName"))
150 strcpy(ret.path, "putty.log");
156 char *platform_default_s(const char *name)
158 if (!strcmp(name, "SerialLine"))
159 return dupstr("/dev/ttyS0");
163 int platform_default_i(const char *name, int def)
165 if (!strcmp(name, "CloseOnExit"))
166 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
167 if (!strcmp(name, "WinNameAlways"))
168 return 0; /* X natively supports icon titles, so use 'em by default */
172 void ldisc_update(void *frontend, int echo, int edit)
175 * This is a stub in pterm. If I ever produce a Unix
176 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
177 * then it will require some termios manoeuvring analogous to
178 * that in the Windows plink.c, but here it's meaningless.
182 char *get_ttymode(void *frontend, const char *mode)
184 struct gui_data *inst = (struct gui_data *)frontend;
185 return term_get_ttymode(inst->term, mode);
188 int from_backend(void *frontend, int is_stderr, const char *data, int len)
190 struct gui_data *inst = (struct gui_data *)frontend;
191 return term_data(inst->term, is_stderr, data, len);
194 int from_backend_untrusted(void *frontend, const char *data, int len)
196 struct gui_data *inst = (struct gui_data *)frontend;
197 return term_data_untrusted(inst->term, data, len);
200 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
202 struct gui_data *inst = (struct gui_data *)p->frontend;
204 ret = cmdline_get_passwd_input(p, in, inlen);
206 ret = term_get_userpass_input(inst->term, p, in, inlen);
210 void logevent(void *frontend, const char *string)
212 struct gui_data *inst = (struct gui_data *)frontend;
214 log_eventlog(inst->logctx, string);
216 logevent_dlg(inst->eventlogstuff, string);
219 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
221 struct gui_data *inst = (struct gui_data *)frontend;
224 return inst->font_height;
226 return inst->font_width;
230 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
231 * into a cooked one (SELECT, EXTEND, PASTE).
233 * In Unix, this is not configurable; the X button arrangement is
234 * rock-solid across all applications, everyone has a three-button
235 * mouse or a means of faking it, and there is no need to switch
236 * buttons around at all.
238 static Mouse_Button translate_button(Mouse_Button button)
240 /* struct gui_data *inst = (struct gui_data *)frontend; */
242 if (button == MBT_LEFT)
244 if (button == MBT_MIDDLE)
246 if (button == MBT_RIGHT)
248 return 0; /* shouldn't happen */
252 * Return the top-level GtkWindow associated with a particular
253 * front end instance.
255 void *get_window(void *frontend)
257 struct gui_data *inst = (struct gui_data *)frontend;
262 * Minimise or restore the window in response to a server-side
265 void set_iconic(void *frontend, int iconic)
268 * GTK 1.2 doesn't know how to do this.
270 #if GTK_CHECK_VERSION(2,0,0)
271 struct gui_data *inst = (struct gui_data *)frontend;
273 gtk_window_iconify(GTK_WINDOW(inst->window));
275 gtk_window_deiconify(GTK_WINDOW(inst->window));
280 * Move the window in response to a server-side request.
282 void move_window(void *frontend, int x, int y)
284 struct gui_data *inst = (struct gui_data *)frontend;
286 * I assume that when the GTK version of this call is available
287 * we should use it. Not sure how it differs from the GDK one,
290 #if GTK_CHECK_VERSION(2,0,0)
291 gtk_window_move(GTK_WINDOW(inst->window), x, y);
293 gdk_window_move(inst->window->window, x, y);
298 * Move the window to the top or bottom of the z-order in response
299 * to a server-side request.
301 void set_zorder(void *frontend, int top)
303 struct gui_data *inst = (struct gui_data *)frontend;
305 gdk_window_raise(inst->window->window);
307 gdk_window_lower(inst->window->window);
311 * Refresh the window in response to a server-side request.
313 void refresh_window(void *frontend)
315 struct gui_data *inst = (struct gui_data *)frontend;
316 term_invalidate(inst->term);
320 * Maximise or restore the window in response to a server-side
323 void set_zoomed(void *frontend, int zoomed)
326 * GTK 1.2 doesn't know how to do this.
328 #if GTK_CHECK_VERSION(2,0,0)
329 struct gui_data *inst = (struct gui_data *)frontend;
331 gtk_window_maximize(GTK_WINDOW(inst->window));
333 gtk_window_unmaximize(GTK_WINDOW(inst->window));
338 * Report whether the window is iconic, for terminal reports.
340 int is_iconic(void *frontend)
342 struct gui_data *inst = (struct gui_data *)frontend;
343 return !gdk_window_is_viewable(inst->window->window);
347 * Report the window's position, for terminal reports.
349 void get_window_pos(void *frontend, int *x, int *y)
351 struct gui_data *inst = (struct gui_data *)frontend;
353 * I assume that when the GTK version of this call is available
354 * we should use it. Not sure how it differs from the GDK one,
357 #if GTK_CHECK_VERSION(2,0,0)
358 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
360 gdk_window_get_position(inst->window->window, x, y);
365 * Report the window's pixel size, for terminal reports.
367 void get_window_pixels(void *frontend, int *x, int *y)
369 struct gui_data *inst = (struct gui_data *)frontend;
371 * I assume that when the GTK version of this call is available
372 * we should use it. Not sure how it differs from the GDK one,
375 #if GTK_CHECK_VERSION(2,0,0)
376 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
378 gdk_window_get_size(inst->window->window, x, y);
383 * Return the window or icon title.
385 char *get_window_title(void *frontend, int icon)
387 struct gui_data *inst = (struct gui_data *)frontend;
388 return icon ? inst->icontitle : inst->wintitle;
391 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
393 struct gui_data *inst = (struct gui_data *)data;
394 if (!inst->exited && inst->cfg.warn_on_close) {
395 if (!reallyclose(inst))
401 static void update_mouseptr(struct gui_data *inst)
403 switch (inst->busy_status) {
405 if (!inst->mouseptr_visible) {
406 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
407 } else if (send_raw_mouse) {
408 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
410 gdk_window_set_cursor(inst->area->window, inst->textcursor);
413 case BUSY_WAITING: /* XXX can we do better? */
415 /* We always display these cursors. */
416 gdk_window_set_cursor(inst->area->window, inst->waitcursor);
423 static void show_mouseptr(struct gui_data *inst, int show)
425 if (!inst->cfg.hide_mouseptr)
427 inst->mouseptr_visible = show;
428 update_mouseptr(inst);
431 void draw_backing_rect(struct gui_data *inst)
433 GdkGC *gc = gdk_gc_new(inst->area->window);
434 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
435 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
436 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
437 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
441 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
443 struct gui_data *inst = (struct gui_data *)data;
444 int w, h, need_size = 0;
447 * See if the terminal size has changed, in which case we must
448 * let the terminal know.
450 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
451 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
452 if (w != inst->width || h != inst->height) {
453 inst->cfg.width = inst->width = w;
454 inst->cfg.height = inst->height = h;
459 gdk_pixmap_unref(inst->pixmap);
463 inst->pixmap = gdk_pixmap_new(widget->window,
464 (inst->cfg.width * inst->font_width +
465 2*inst->cfg.window_border),
466 (inst->cfg.height * inst->font_height +
467 2*inst->cfg.window_border), -1);
469 draw_backing_rect(inst);
471 if (need_size && inst->term) {
472 term_size(inst->term, h, w, inst->cfg.savelines);
476 term_invalidate(inst->term);
481 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
483 struct gui_data *inst = (struct gui_data *)data;
486 * Pass the exposed rectangle to terminal.c, which will call us
487 * back to do the actual painting.
490 gdk_draw_pixmap(widget->window,
491 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
493 event->area.x, event->area.y,
494 event->area.x, event->area.y,
495 event->area.width, event->area.height);
500 #define KEY_PRESSED(k) \
501 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
503 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
505 struct gui_data *inst = (struct gui_data *)data;
507 wchar_t ucsoutput[2];
508 int ucsval, start, end, special, use_ucsoutput;
510 /* Remember the timestamp. */
511 inst->input_event_time = event->time;
513 /* By default, nothing is generated. */
515 special = use_ucsoutput = FALSE;
518 * If Alt is being released after typing an Alt+numberpad
519 * sequence, we should generate the code that was typed.
521 * Note that we only do this if more than one key was actually
522 * pressed - I don't think Alt+NumPad4 should be ^D or that
523 * Alt+NumPad3 should be ^C, for example. There's no serious
524 * inconvenience in having to type a zero before a single-digit
527 if (event->type == GDK_KEY_RELEASE &&
528 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
529 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
530 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
532 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
534 output[0] = inst->alt_keycode;
539 if (event->type == GDK_KEY_PRESS) {
543 printf("keypress: keyval = %04x, state = %08x; string =",
544 event->keyval, event->state);
545 for (i = 0; event->string[i]; i++)
546 printf(" %02x", (unsigned char) event->string[i]);
552 * NYI: Compose key (!!! requires Unicode faff before even trying)
556 * If Alt has just been pressed, we start potentially
557 * accumulating an Alt+numberpad code. We do this by
558 * setting alt_keycode to -1 (nothing yet but plausible).
560 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
561 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
562 inst->alt_keycode = -1;
563 inst->alt_digits = 0;
564 goto done; /* this generates nothing else */
568 * If we're seeing a numberpad key press with Mod1 down,
569 * consider adding it to alt_keycode if that's sensible.
570 * Anything _else_ with Mod1 down cancels any possibility
571 * of an ALT keycode: we set alt_keycode to -2.
573 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
575 switch (event->keyval) {
576 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
577 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
578 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
579 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
580 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
581 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
582 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
583 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
584 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
585 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
588 inst->alt_keycode = -2; /* it's invalid */
591 printf("Adding digit %d to keycode %d", digit,
594 if (inst->alt_keycode == -1)
595 inst->alt_keycode = digit; /* one-digit code */
597 inst->alt_keycode = inst->alt_keycode * 10 + digit;
600 printf(" gives new code %d\n", inst->alt_keycode);
602 /* Having used this digit, we now do nothing more with it. */
608 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
611 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
612 term_scroll(inst->term, 0, -inst->cfg.height/2);
615 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
616 term_scroll(inst->term, 0, -1);
619 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
620 term_scroll(inst->term, 0, +inst->cfg.height/2);
623 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
624 term_scroll(inst->term, 0, +1);
629 * Neither does Shift-Ins.
631 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
637 use_ucsoutput = FALSE;
639 /* ALT+things gives leading Escape. */
641 strncpy(output+1, event->string, 31);
642 if (!*event->string &&
643 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
644 ucsoutput[0] = '\033';
645 ucsoutput[1] = ucsval;
646 use_ucsoutput = TRUE;
650 end = strlen(output);
652 if (event->state & GDK_MOD1_MASK) {
654 if (end == 1) end = 0;
658 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
659 if (!event->string[0] && event->keyval == '`' &&
660 (event->state & GDK_CONTROL_MASK)) {
662 use_ucsoutput = FALSE;
666 /* Control-Break is the same as Control-C */
667 if (event->keyval == GDK_Break &&
668 (event->state & GDK_CONTROL_MASK)) {
670 use_ucsoutput = FALSE;
675 /* We handle Return ourselves, because it needs to be flagged as
676 * special to ldisc. */
677 if (event->keyval == GDK_Return) {
679 use_ucsoutput = FALSE;
684 /* Control-2, Control-Space and Control-@ are NUL */
685 if (!event->string[0] &&
686 (event->keyval == ' ' || event->keyval == '2' ||
687 event->keyval == '@') &&
688 (event->state & (GDK_SHIFT_MASK |
689 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
691 use_ucsoutput = FALSE;
695 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
696 if (!event->string[0] && event->keyval == ' ' &&
697 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
698 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
700 use_ucsoutput = FALSE;
704 /* We don't let GTK tell us what Backspace is! We know better. */
705 if (event->keyval == GDK_BackSpace &&
706 !(event->state & GDK_SHIFT_MASK)) {
707 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
708 use_ucsoutput = FALSE;
712 /* For Shift Backspace, do opposite of what is configured. */
713 if (event->keyval == GDK_BackSpace &&
714 (event->state & GDK_SHIFT_MASK)) {
715 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
716 use_ucsoutput = FALSE;
721 /* Shift-Tab is ESC [ Z */
722 if (event->keyval == GDK_ISO_Left_Tab ||
723 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
724 end = 1 + sprintf(output+1, "\033[Z");
725 use_ucsoutput = FALSE;
729 * NetHack keypad mode.
731 if (inst->cfg.nethack_keypad) {
733 switch (event->keyval) {
734 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
735 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
736 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
737 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
738 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
739 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
740 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
741 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
742 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
746 if (event->state & GDK_CONTROL_MASK)
748 else if (event->state & GDK_SHIFT_MASK)
752 use_ucsoutput = FALSE;
758 * Application keypad mode.
760 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
762 switch (event->keyval) {
763 case GDK_Num_Lock: xkey = 'P'; break;
764 case GDK_KP_Divide: xkey = 'Q'; break;
765 case GDK_KP_Multiply: xkey = 'R'; break;
766 case GDK_KP_Subtract: xkey = 'S'; break;
768 * Keypad + is tricky. It covers a space that would
769 * be taken up on the VT100 by _two_ keys; so we
770 * let Shift select between the two. Worse still,
771 * in xterm function key mode we change which two...
774 if (inst->cfg.funky_type == FUNKY_XTERM) {
775 if (event->state & GDK_SHIFT_MASK)
779 } else if (event->state & GDK_SHIFT_MASK)
784 case GDK_KP_Enter: xkey = 'M'; break;
785 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
786 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
787 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
788 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
789 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
790 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
791 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
792 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
793 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
794 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
795 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
798 if (inst->term->vt52_mode) {
799 if (xkey >= 'P' && xkey <= 'S')
800 end = 1 + sprintf(output+1, "\033%c", xkey);
802 end = 1 + sprintf(output+1, "\033?%c", xkey);
804 end = 1 + sprintf(output+1, "\033O%c", xkey);
805 use_ucsoutput = FALSE;
811 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
812 * for integer decimal nn.)
814 * We also deal with the weird ones here. Linux VCs replace F1
815 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
816 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
821 switch (event->keyval) {
823 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
826 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
829 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
832 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
835 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
838 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
841 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
844 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
847 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
850 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
883 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
884 case GDK_Home: case GDK_KP_Home:
887 case GDK_Insert: case GDK_KP_Insert:
890 case GDK_Delete: case GDK_KP_Delete:
893 case GDK_End: case GDK_KP_End:
896 case GDK_Page_Up: case GDK_KP_Page_Up:
899 case GDK_Page_Down: case GDK_KP_Page_Down:
903 /* Reorder edit keys to physical order */
904 if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
905 code = "\0\2\1\4\5\3\6"[code];
907 if (inst->term->vt52_mode && code > 0 && code <= 6) {
908 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
909 use_ucsoutput = FALSE;
913 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
914 code >= 11 && code <= 34) {
915 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
917 switch (event->keyval) {
918 case GDK_F1: index = 0; break;
919 case GDK_F2: index = 1; break;
920 case GDK_F3: index = 2; break;
921 case GDK_F4: index = 3; break;
922 case GDK_F5: index = 4; break;
923 case GDK_F6: index = 5; break;
924 case GDK_F7: index = 6; break;
925 case GDK_F8: index = 7; break;
926 case GDK_F9: index = 8; break;
927 case GDK_F10: index = 9; break;
928 case GDK_F11: index = 10; break;
929 case GDK_F12: index = 11; break;
931 if (event->state & GDK_SHIFT_MASK) index += 12;
932 if (event->state & GDK_CONTROL_MASK) index += 24;
933 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
934 use_ucsoutput = FALSE;
937 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
938 code >= 1 && code <= 6) {
939 char codes[] = "HL.FIG";
944 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
946 use_ucsoutput = FALSE;
949 if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
950 code >= 11 && code <= 24) {
956 if (inst->term->vt52_mode)
957 end = 1 + sprintf(output+1,
958 "\x1B%c", code + 'P' - 11 - offt);
960 end = 1 + sprintf(output+1,
961 "\x1BO%c", code + 'P' - 11 - offt);
962 use_ucsoutput = FALSE;
965 if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
966 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
967 use_ucsoutput = FALSE;
970 if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
971 if (inst->term->vt52_mode)
972 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
974 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
975 use_ucsoutput = FALSE;
978 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
979 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
980 use_ucsoutput = FALSE;
984 end = 1 + sprintf(output+1, "\x1B[%d~", code);
985 use_ucsoutput = FALSE;
991 * Cursor keys. (This includes the numberpad cursor keys,
992 * if we haven't already done them due to app keypad mode.)
994 * Here we also process un-numlocked un-appkeypadded KP5,
995 * which sends ESC [ G.
999 switch (event->keyval) {
1000 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1001 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1002 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1003 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1004 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1008 * The arrow keys normally do ESC [ A and so on. In
1009 * app cursor keys mode they do ESC O A instead.
1010 * Ctrl toggles the two modes.
1012 if (inst->term->vt52_mode) {
1013 end = 1 + sprintf(output+1, "\033%c", xkey);
1014 } else if (!inst->term->app_cursor_keys ^
1015 !(event->state & GDK_CONTROL_MASK)) {
1016 end = 1 + sprintf(output+1, "\033O%c", xkey);
1018 end = 1 + sprintf(output+1, "\033[%c", xkey);
1020 use_ucsoutput = FALSE;
1029 if (end-start > 0) {
1030 #ifdef KEY_DEBUGGING
1032 printf("generating sequence:");
1033 for (i = start; i < end; i++)
1034 printf(" %02x", (unsigned char) output[i]);
1040 * For special control characters, the character set
1041 * should never matter.
1043 output[end] = '\0'; /* NUL-terminate */
1045 ldisc_send(inst->ldisc, output+start, -2, 1);
1046 } else if (!inst->direct_to_font) {
1047 if (!use_ucsoutput) {
1049 * The stuff we've just generated is assumed to be
1050 * ISO-8859-1! This sounds insane, but `man
1051 * XLookupString' agrees: strings of this type
1052 * returned from the X server are hardcoded to
1053 * 8859-1. Strictly speaking we should be doing
1054 * this using some sort of GtkIMContext, which (if
1055 * we're lucky) would give us our data directly in
1056 * Unicode; but that's not supported in GTK 1.2 as
1057 * far as I can tell, and it's poorly documented
1058 * even in 2.0, so it'll have to wait.
1061 lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
1065 * We generated our own Unicode key data from the
1066 * keysym, so use that instead.
1069 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1073 * In direct-to-font mode, we just send the string
1074 * exactly as we received it.
1077 ldisc_send(inst->ldisc, output+start, end-start, 1);
1080 show_mouseptr(inst, 0);
1081 term_seen_key_event(inst->term);
1087 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1089 struct gui_data *inst = (struct gui_data *)data;
1090 int shift, ctrl, alt, x, y, button, act;
1092 /* Remember the timestamp. */
1093 inst->input_event_time = event->time;
1095 show_mouseptr(inst, 1);
1097 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
1098 term_scroll(inst->term, 0, -5);
1101 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
1102 term_scroll(inst->term, 0, +5);
1106 shift = event->state & GDK_SHIFT_MASK;
1107 ctrl = event->state & GDK_CONTROL_MASK;
1108 alt = event->state & GDK_MOD1_MASK;
1110 if (event->button == 3 && ctrl) {
1111 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1112 event->button, event->time);
1116 if (event->button == 1)
1118 else if (event->button == 2)
1119 button = MBT_MIDDLE;
1120 else if (event->button == 3)
1123 return FALSE; /* don't even know what button! */
1125 switch (event->type) {
1126 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1127 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1128 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1129 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1130 default: return FALSE; /* don't know this event type */
1133 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
1134 act != MA_CLICK && act != MA_RELEASE)
1135 return TRUE; /* we ignore these in raw mouse mode */
1137 x = (event->x - inst->cfg.window_border) / inst->font_width;
1138 y = (event->y - inst->cfg.window_border) / inst->font_height;
1140 term_mouse(inst->term, button, translate_button(button), act,
1141 x, y, shift, ctrl, alt);
1146 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1148 struct gui_data *inst = (struct gui_data *)data;
1149 int shift, ctrl, alt, x, y, button;
1151 /* Remember the timestamp. */
1152 inst->input_event_time = event->time;
1154 show_mouseptr(inst, 1);
1156 shift = event->state & GDK_SHIFT_MASK;
1157 ctrl = event->state & GDK_CONTROL_MASK;
1158 alt = event->state & GDK_MOD1_MASK;
1159 if (event->state & GDK_BUTTON1_MASK)
1161 else if (event->state & GDK_BUTTON2_MASK)
1162 button = MBT_MIDDLE;
1163 else if (event->state & GDK_BUTTON3_MASK)
1166 return FALSE; /* don't even know what button! */
1168 x = (event->x - inst->cfg.window_border) / inst->font_width;
1169 y = (event->y - inst->cfg.window_border) / inst->font_height;
1171 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1172 x, y, shift, ctrl, alt);
1177 void frontend_keypress(void *handle)
1179 struct gui_data *inst = (struct gui_data *)handle;
1182 * If our child process has exited but not closed, terminate on
1189 void notify_remote_exit(void *frontend)
1191 struct gui_data *inst = (struct gui_data *)frontend;
1194 if (!inst->exited &&
1195 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1196 inst->exited = TRUE;
1197 if (inst->cfg.close_on_exit == FORCE_ON ||
1198 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1199 gtk_main_quit(); /* just go */
1201 ldisc_free(inst->ldisc);
1205 inst->back->free(inst->backhandle);
1206 inst->backhandle = NULL;
1208 term_provide_resize_fn(inst->term, NULL, NULL);
1209 update_specials_menu(inst);
1211 gtk_widget_show(inst->restartitem);
1215 static gint timer_trigger(gpointer data)
1217 long now = GPOINTER_TO_INT(data);
1221 if (run_timers(now, &next)) {
1222 ticks = next - GETTICKCOUNT();
1223 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1224 GINT_TO_POINTER(next));
1228 * Never let a timer resume. If we need another one, we've
1229 * asked for it explicitly above.
1234 void timer_change_notify(long next)
1239 gtk_timeout_remove(timer_id);
1241 ticks = next - GETTICKCOUNT();
1243 ticks = 1; /* just in case */
1245 timer_id = gtk_timeout_add(ticks, timer_trigger,
1246 GINT_TO_POINTER(next));
1249 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1252 * We must process exceptional notifications before ordinary
1253 * readability ones, or we may go straight past the urgent
1256 if (condition & GDK_INPUT_EXCEPTION)
1257 select_result(sourcefd, 4);
1258 if (condition & GDK_INPUT_READ)
1259 select_result(sourcefd, 1);
1260 if (condition & GDK_INPUT_WRITE)
1261 select_result(sourcefd, 2);
1264 void destroy(GtkWidget *widget, gpointer data)
1269 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1271 struct gui_data *inst = (struct gui_data *)data;
1272 term_set_focus(inst->term, event->in);
1273 term_update(inst->term);
1274 show_mouseptr(inst, 1);
1278 void set_busy_status(void *frontend, int status)
1280 struct gui_data *inst = (struct gui_data *)frontend;
1281 inst->busy_status = status;
1282 update_mouseptr(inst);
1286 * set or clear the "raw mouse message" mode
1288 void set_raw_mouse_mode(void *frontend, int activate)
1290 struct gui_data *inst = (struct gui_data *)frontend;
1291 activate = activate && !inst->cfg.no_mouse_rep;
1292 send_raw_mouse = activate;
1293 update_mouseptr(inst);
1296 void request_resize(void *frontend, int w, int h)
1298 struct gui_data *inst = (struct gui_data *)frontend;
1299 int large_x, large_y;
1300 int offset_x, offset_y;
1302 GtkRequisition inner, outer;
1305 * This is a heinous hack dreamed up by the gnome-terminal
1306 * people to get around a limitation in gtk. The problem is
1307 * that in order to set the size correctly we really need to be
1308 * calling gtk_window_resize - but that needs to know the size
1309 * of the _whole window_, not the drawing area. So what we do
1310 * is to set an artificially huge size request on the drawing
1311 * area, recompute the resulting size request on the window,
1312 * and look at the difference between the two. That gives us
1313 * the x and y offsets we need to translate drawing area size
1314 * into window size for real, and then we call
1315 * gtk_window_resize.
1319 * We start by retrieving the current size of the whole window.
1320 * Adding a bit to _that_ will give us a value we can use as a
1321 * bogus size request which guarantees to be bigger than the
1322 * current size of the drawing area.
1324 get_window_pixels(inst, &large_x, &large_y);
1328 #if GTK_CHECK_VERSION(2,0,0)
1329 gtk_widget_set_size_request(inst->area, large_x, large_y);
1331 gtk_widget_set_usize(inst->area, large_x, large_y);
1333 gtk_widget_size_request(inst->area, &inner);
1334 gtk_widget_size_request(inst->window, &outer);
1336 offset_x = outer.width - inner.width;
1337 offset_y = outer.height - inner.height;
1339 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1340 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1343 * Now we must set the size request on the drawing area back to
1344 * something sensible before we commit the real resize. Best
1345 * way to do this, I think, is to set it to what the size is
1346 * really going to end up being.
1348 #if GTK_CHECK_VERSION(2,0,0)
1349 gtk_widget_set_size_request(inst->area, area_x, area_y);
1350 _gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1351 gtk_window_resize(GTK_WINDOW(inst->window),
1352 area_x + offset_x, area_y + offset_y);
1354 gtk_widget_set_usize(inst->area, area_x, area_y);
1355 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1356 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1357 gdk_window_resize(inst->window->window,
1358 area_x + offset_x, area_y + offset_y);
1362 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1364 gboolean success[1];
1366 inst->cols[n].red = r * 0x0101;
1367 inst->cols[n].green = g * 0x0101;
1368 inst->cols[n].blue = b * 0x0101;
1370 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1371 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1372 FALSE, TRUE, success);
1374 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1378 void set_window_background(struct gui_data *inst)
1380 if (inst->area && inst->area->window)
1381 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1382 if (inst->window && inst->window->window)
1383 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1386 void palette_set(void *frontend, int n, int r, int g, int b)
1388 struct gui_data *inst = (struct gui_data *)frontend;
1391 if (n > NALLCOLOURS)
1393 real_palette_set(inst, n, r, g, b);
1395 /* Default Background changed. Ensure space between text area and
1396 * window border is redrawn */
1397 set_window_background(inst);
1398 draw_backing_rect(inst);
1399 gtk_widget_queue_draw(inst->area);
1403 void palette_reset(void *frontend)
1405 struct gui_data *inst = (struct gui_data *)frontend;
1406 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1407 static const int ww[] = {
1408 256, 257, 258, 259, 260, 261,
1409 0, 8, 1, 9, 2, 10, 3, 11,
1410 4, 12, 5, 13, 6, 14, 7, 15
1412 gboolean success[NALLCOLOURS];
1415 assert(lenof(ww) == NCFGCOLOURS);
1417 if (!inst->colmap) {
1418 inst->colmap = gdk_colormap_get_system();
1420 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1423 for (i = 0; i < NCFGCOLOURS; i++) {
1424 inst->cols[ww[i]].red = inst->cfg.colours[i][0] * 0x0101;
1425 inst->cols[ww[i]].green = inst->cfg.colours[i][1] * 0x0101;
1426 inst->cols[ww[i]].blue = inst->cfg.colours[i][2] * 0x0101;
1429 for (i = 0; i < NEXTCOLOURS; i++) {
1431 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1432 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1433 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1434 inst->cols[i+16].blue = b ? b + 0x2828 + 0x3737 : 0;
1436 int shade = i - 216;
1437 shade = shade * 0x0a0a + 0x0808;
1438 inst->cols[i+16].red = inst->cols[i+16].green =
1439 inst->cols[i+16].blue = shade;
1443 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1444 FALSE, TRUE, success);
1445 for (i = 0; i < NALLCOLOURS; i++) {
1447 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1448 appname, i, inst->cfg.colours[i][0],
1449 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1452 /* Since Default Background may have changed, ensure that space
1453 * between text area and window border is refreshed. */
1454 set_window_background(inst);
1456 draw_backing_rect(inst);
1457 gtk_widget_queue_draw(inst->area);
1461 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1462 * do this before we start using cut buffers.
1464 void init_cutbuffers()
1466 unsigned char empty[] = "";
1467 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1468 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1469 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1470 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1471 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1472 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1473 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1474 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1475 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1476 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1477 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1478 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1479 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1480 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1481 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1482 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1485 /* Store the data in a cut-buffer. */
1486 void store_cutbuffer(char * ptr, int len)
1488 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1489 XRotateBuffers(GDK_DISPLAY(), 1);
1490 XStoreBytes(GDK_DISPLAY(), ptr, len);
1493 /* Retrieve data from a cut-buffer.
1494 * Returned data needs to be freed with XFree().
1496 char * retrieve_cutbuffer(int * nbytes)
1499 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1500 if (*nbytes <= 0 && ptr != 0) {
1507 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1509 struct gui_data *inst = (struct gui_data *)frontend;
1510 if (inst->pasteout_data)
1511 sfree(inst->pasteout_data);
1512 if (inst->pasteout_data_ctext)
1513 sfree(inst->pasteout_data_ctext);
1514 if (inst->pasteout_data_utf8)
1515 sfree(inst->pasteout_data_utf8);
1518 * Set up UTF-8 and compound text paste data. This only happens
1519 * if we aren't in direct-to-font mode using the D800 hack.
1521 if (!inst->direct_to_font) {
1522 wchar_t *tmp = data;
1527 inst->pasteout_data_utf8 = snewn(len*6, char);
1528 inst->pasteout_data_utf8_len = len*6;
1529 inst->pasteout_data_utf8_len =
1530 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1531 inst->pasteout_data_utf8_len,
1532 CS_UTF8, NULL, NULL, 0);
1533 if (inst->pasteout_data_utf8_len == 0) {
1534 sfree(inst->pasteout_data_utf8);
1535 inst->pasteout_data_utf8 = NULL;
1537 inst->pasteout_data_utf8 =
1538 sresize(inst->pasteout_data_utf8,
1539 inst->pasteout_data_utf8_len + 1, char);
1540 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1544 * Now let Xlib convert our UTF-8 data into compound text.
1546 list[0] = inst->pasteout_data_utf8;
1547 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1548 XCompoundTextStyle, &tp) == 0) {
1549 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1550 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1551 inst->pasteout_data_ctext_len = tp.nitems;
1554 inst->pasteout_data_ctext = NULL;
1555 inst->pasteout_data_ctext_len = 0;
1558 inst->pasteout_data_utf8 = NULL;
1559 inst->pasteout_data_utf8_len = 0;
1560 inst->pasteout_data_ctext = NULL;
1561 inst->pasteout_data_ctext_len = 0;
1564 inst->pasteout_data = snewn(len*6, char);
1565 inst->pasteout_data_len = len*6;
1566 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1567 data, len, inst->pasteout_data,
1568 inst->pasteout_data_len,
1570 if (inst->pasteout_data_len == 0) {
1571 sfree(inst->pasteout_data);
1572 inst->pasteout_data = NULL;
1574 inst->pasteout_data =
1575 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1578 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1580 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1581 inst->input_event_time)) {
1582 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1583 GDK_SELECTION_TYPE_STRING, 1);
1584 if (inst->pasteout_data_ctext)
1585 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1586 compound_text_atom, 1);
1587 if (inst->pasteout_data_utf8)
1588 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1589 utf8_string_atom, 1);
1593 term_deselect(inst->term);
1596 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1597 guint info, guint time_stamp, gpointer data)
1599 struct gui_data *inst = (struct gui_data *)data;
1600 if (seldata->target == utf8_string_atom)
1601 gtk_selection_data_set(seldata, seldata->target, 8,
1602 (unsigned char *)inst->pasteout_data_utf8,
1603 inst->pasteout_data_utf8_len);
1604 else if (seldata->target == compound_text_atom)
1605 gtk_selection_data_set(seldata, seldata->target, 8,
1606 (unsigned char *)inst->pasteout_data_ctext,
1607 inst->pasteout_data_ctext_len);
1609 gtk_selection_data_set(seldata, seldata->target, 8,
1610 (unsigned char *)inst->pasteout_data,
1611 inst->pasteout_data_len);
1614 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1617 struct gui_data *inst = (struct gui_data *)data;
1619 term_deselect(inst->term);
1620 if (inst->pasteout_data)
1621 sfree(inst->pasteout_data);
1622 if (inst->pasteout_data_ctext)
1623 sfree(inst->pasteout_data_ctext);
1624 if (inst->pasteout_data_utf8)
1625 sfree(inst->pasteout_data_utf8);
1626 inst->pasteout_data = NULL;
1627 inst->pasteout_data_len = 0;
1628 inst->pasteout_data_ctext = NULL;
1629 inst->pasteout_data_ctext_len = 0;
1630 inst->pasteout_data_utf8 = NULL;
1631 inst->pasteout_data_utf8_len = 0;
1635 void request_paste(void *frontend)
1637 struct gui_data *inst = (struct gui_data *)frontend;
1639 * In Unix, pasting is asynchronous: all we can do at the
1640 * moment is to call gtk_selection_convert(), and when the data
1641 * comes back _then_ we can call term_do_paste().
1644 if (!inst->direct_to_font) {
1646 * First we attempt to retrieve the selection as a UTF-8
1647 * string (which we will convert to the correct code page
1648 * before sending to the session, of course). If that
1649 * fails, selection_received() will be informed and will
1650 * fall back to an ordinary string.
1652 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1654 inst->input_event_time);
1657 * If we're in direct-to-font mode, we disable UTF-8
1658 * pasting, and go straight to ordinary string data.
1660 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1661 GDK_SELECTION_TYPE_STRING,
1662 inst->input_event_time);
1666 gint idle_paste_func(gpointer data); /* forward ref */
1668 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1669 guint time, gpointer data)
1671 struct gui_data *inst = (struct gui_data *)data;
1675 int length, count, ret;
1676 int free_list_required = 0;
1677 int free_required = 0;
1680 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1682 * Failed to get a UTF-8 selection string. Try compound
1685 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1687 inst->input_event_time);
1691 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1693 * Failed to get UTF-8 or compound text. Try an ordinary
1696 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1697 GDK_SELECTION_TYPE_STRING,
1698 inst->input_event_time);
1703 * If we have data, but it's not of a type we can deal with,
1704 * we have to ignore the data.
1706 if (seldata->length > 0 &&
1707 seldata->type != GDK_SELECTION_TYPE_STRING &&
1708 seldata->type != compound_text_atom &&
1709 seldata->type != utf8_string_atom)
1713 * If we have no data, try looking in a cut buffer.
1715 if (seldata->length <= 0) {
1716 text = retrieve_cutbuffer(&length);
1719 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1720 * source, so use that as a de-facto standard. */
1721 charset = CS_ISO8859_1;
1725 * Convert COMPOUND_TEXT into UTF-8.
1727 if (seldata->type == compound_text_atom) {
1728 tp.value = seldata->data;
1729 tp.encoding = (Atom) seldata->type;
1730 tp.format = seldata->format;
1731 tp.nitems = seldata->length;
1732 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1734 if (ret != 0 || count != 1) {
1736 * Compound text failed; fall back to STRING.
1738 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1739 GDK_SELECTION_TYPE_STRING,
1740 inst->input_event_time);
1744 length = strlen(list[0]);
1746 free_list_required = 1;
1748 text = (char *)seldata->data;
1749 length = seldata->length;
1750 charset = (seldata->type == utf8_string_atom ?
1751 CS_UTF8 : inst->ucsdata.line_codepage);
1755 if (inst->pastein_data)
1756 sfree(inst->pastein_data);
1758 inst->pastein_data = snewn(length, wchar_t);
1759 inst->pastein_data_len = length;
1760 inst->pastein_data_len =
1761 mb_to_wc(charset, 0, text, length,
1762 inst->pastein_data, inst->pastein_data_len);
1764 term_do_paste(inst->term);
1766 if (term_paste_pending(inst->term))
1767 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1769 if (free_list_required)
1770 XFreeStringList(list);
1775 gint idle_paste_func(gpointer data)
1777 struct gui_data *inst = (struct gui_data *)data;
1779 if (term_paste_pending(inst->term))
1780 term_paste(inst->term);
1782 gtk_idle_remove(inst->term_paste_idle_id);
1788 void get_clip(void *frontend, wchar_t ** p, int *len)
1790 struct gui_data *inst = (struct gui_data *)frontend;
1793 *p = inst->pastein_data;
1794 *len = inst->pastein_data_len;
1798 static void set_window_titles(struct gui_data *inst)
1801 * We must always call set_icon_name after calling set_title,
1802 * since set_title will write both names. Irritating, but such
1805 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1806 if (!inst->cfg.win_name_always)
1807 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1810 void set_title(void *frontend, char *title)
1812 struct gui_data *inst = (struct gui_data *)frontend;
1813 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1814 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1815 set_window_titles(inst);
1818 void set_icon(void *frontend, char *title)
1820 struct gui_data *inst = (struct gui_data *)frontend;
1821 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1822 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1823 set_window_titles(inst);
1826 void set_sbar(void *frontend, int total, int start, int page)
1828 struct gui_data *inst = (struct gui_data *)frontend;
1829 if (!inst->cfg.scrollbar)
1831 inst->sbar_adjust->lower = 0;
1832 inst->sbar_adjust->upper = total;
1833 inst->sbar_adjust->value = start;
1834 inst->sbar_adjust->page_size = page;
1835 inst->sbar_adjust->step_increment = 1;
1836 inst->sbar_adjust->page_increment = page/2;
1837 inst->ignore_sbar = TRUE;
1838 gtk_adjustment_changed(inst->sbar_adjust);
1839 inst->ignore_sbar = FALSE;
1842 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1844 struct gui_data *inst = (struct gui_data *)data;
1846 if (!inst->cfg.scrollbar)
1848 if (!inst->ignore_sbar)
1849 term_scroll(inst->term, 1, (int)adj->value);
1852 void sys_cursor(void *frontend, int x, int y)
1855 * This is meaningless under X.
1860 * This is still called when mode==BELL_VISUAL, even though the
1861 * visual bell is handled entirely within terminal.c, because we
1862 * may want to perform additional actions on any kind of bell (for
1863 * example, taskbar flashing in Windows).
1865 void do_beep(void *frontend, int mode)
1867 if (mode != BELL_VISUAL)
1871 int char_width(Context ctx, int uc)
1874 * Under X, any fixed-width font really _is_ fixed-width.
1875 * Double-width characters will be dealt with using a separate
1876 * font. For the moment we can simply return 1.
1881 Context get_ctx(void *frontend)
1883 struct gui_data *inst = (struct gui_data *)frontend;
1884 struct draw_ctx *dctx;
1886 if (!inst->area->window)
1889 dctx = snew(struct draw_ctx);
1891 dctx->gc = gdk_gc_new(inst->area->window);
1895 void free_ctx(Context ctx)
1897 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1898 /* struct gui_data *inst = dctx->inst; */
1899 GdkGC *gc = dctx->gc;
1905 * Draw a line of text in the window, at given character
1906 * coordinates, in given attributes.
1908 * We are allowed to fiddle with the contents of `text'.
1910 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1911 unsigned long attr, int lattr)
1913 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1914 struct gui_data *inst = dctx->inst;
1915 GdkGC *gc = dctx->gc;
1916 int ncombining, combining;
1917 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1918 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
1920 if (attr & TATTR_COMBINING) {
1926 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
1927 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
1928 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
1933 if (inst->cfg.bold_colour && (attr & ATTR_BOLD)) {
1934 if (nfg < 16) nfg |= 8;
1935 else if (nfg >= 256) nfg |= 1;
1937 if (inst->cfg.bold_colour && (attr & ATTR_BLINK)) {
1938 if (nbg < 16) nbg |= 8;
1939 else if (nbg >= 256) nbg |= 1;
1941 if ((attr & TATTR_ACTCURS) && !monochrome) {
1946 fontid = shadow = 0;
1948 if (attr & ATTR_WIDE) {
1955 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1956 if (inst->fonts[fontid | 1])
1962 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1964 if (x >= inst->term->cols)
1966 if (x + len*2*widefactor > inst->term->cols)
1967 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1975 r.x = x*inst->font_width+inst->cfg.window_border;
1976 r.y = y*inst->font_height+inst->cfg.window_border;
1977 r.width = rlen*widefactor*inst->font_width;
1978 r.height = inst->font_height;
1979 gdk_gc_set_clip_rectangle(gc, &r);
1982 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1983 gdk_draw_rectangle(inst->pixmap, gc, 1,
1984 x*inst->font_width+inst->cfg.window_border,
1985 y*inst->font_height+inst->cfg.window_border,
1986 rlen*widefactor*inst->font_width, inst->font_height);
1988 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1995 wcs = snewn(len*ncombining+1, wchar_t);
1996 for (i = 0; i < len*ncombining; i++) {
2000 if (inst->fonts[fontid] == NULL && (fontid & 2)) {
2002 * We've been given ATTR_WIDE, but have no wide font.
2003 * Fall back to the non-wide font.
2008 if (inst->fonts[fontid] == NULL) {
2010 * The font for this contingency does not exist. So we
2011 * display nothing at all; such is life.
2013 } else if (inst->fontinfo[fontid].is_wide) {
2015 * At least one version of gdk_draw_text_wc() has a
2016 * weird bug whereby it reads `len' elements of the
2017 * input string, but only draws `len/2'. Hence I'm
2018 * going to make its input array twice as long as it
2019 * theoretically needs to be, and pass in twice the
2020 * actual number of characters. If a fixed gdk actually
2021 * takes the doubled length seriously, then (a) the
2022 * array will stand scrutiny up to the full length, (b)
2023 * the spare elements of the array are full of zeroes
2024 * which will probably be an empty glyph in the font,
2025 * and (c) the clip rectangle should prevent it causing
2028 gwcs = snewn(len*2+1, GdkWChar);
2029 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2031 * FIXME: when we have a wide-char equivalent of
2032 * from_unicode, use it instead of this.
2034 for (combining = 0; combining < ncombining; combining++) {
2035 for (i = 0; i <= len; i++)
2036 gwcs[i] = wcs[i + combining];
2037 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2038 x*inst->font_width+inst->cfg.window_border,
2039 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2042 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2043 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2044 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2049 gcs = snewn(len+1, gchar);
2050 for (combining = 0; combining < ncombining; combining++) {
2051 wc_to_mb(inst->fontinfo[fontid].charset, 0,
2052 wcs + combining, len, gcs, len, ".", NULL, NULL);
2053 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2054 x*inst->font_width+inst->cfg.window_border,
2055 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2058 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2059 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2060 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2068 if (attr & ATTR_UNDER) {
2069 int uheight = inst->fonts[0]->ascent + 1;
2070 if (uheight >= inst->font_height)
2071 uheight = inst->font_height - 1;
2072 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
2073 y*inst->font_height + uheight + inst->cfg.window_border,
2074 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
2075 y*inst->font_height + uheight + inst->cfg.window_border);
2078 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2080 * I can't find any plausible StretchBlt equivalent in the
2081 * X server, so I'm going to do this the slow and painful
2082 * way. This will involve repeated calls to
2083 * gdk_draw_pixmap() to stretch the text horizontally. It's
2084 * O(N^2) in time and O(N) in network bandwidth, but you
2085 * try thinking of a better way. :-(
2088 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2089 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2090 x*inst->font_width+inst->cfg.window_border + 2*i,
2091 y*inst->font_height+inst->cfg.window_border,
2092 x*inst->font_width+inst->cfg.window_border + 2*i+1,
2093 y*inst->font_height+inst->cfg.window_border,
2094 len * widefactor * inst->font_width - i, inst->font_height);
2097 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2099 /* Now stretch vertically, in the same way. */
2100 if ((lattr & LATTR_MODE) == LATTR_BOT)
2104 for (i = 0; i < inst->font_height; i+=2) {
2105 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2106 x*inst->font_width+inst->cfg.window_border,
2107 y*inst->font_height+inst->cfg.window_border+dt*i+db,
2108 x*inst->font_width+inst->cfg.window_border,
2109 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
2110 len * widefactor * inst->font_width, inst->font_height-i-1);
2116 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2117 unsigned long attr, int lattr)
2119 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2120 struct gui_data *inst = dctx->inst;
2121 GdkGC *gc = dctx->gc;
2124 do_text_internal(ctx, x, y, text, len, attr, lattr);
2126 if (attr & ATTR_WIDE) {
2132 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2134 if (x >= inst->term->cols)
2136 if (x + len*2*widefactor > inst->term->cols)
2137 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2141 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2142 x*inst->font_width+inst->cfg.window_border,
2143 y*inst->font_height+inst->cfg.window_border,
2144 x*inst->font_width+inst->cfg.window_border,
2145 y*inst->font_height+inst->cfg.window_border,
2146 len*widefactor*inst->font_width, inst->font_height);
2149 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2150 unsigned long attr, int lattr)
2152 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2153 struct gui_data *inst = dctx->inst;
2154 GdkGC *gc = dctx->gc;
2156 int active, passive, widefactor;
2158 if (attr & TATTR_PASCURS) {
2159 attr &= ~TATTR_PASCURS;
2163 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
2164 attr &= ~TATTR_ACTCURS;
2168 do_text_internal(ctx, x, y, text, len, attr, lattr);
2170 if (attr & TATTR_COMBINING)
2173 if (attr & ATTR_WIDE) {
2179 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2181 if (x >= inst->term->cols)
2183 if (x + len*2*widefactor > inst->term->cols)
2184 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2188 if (inst->cfg.cursor_type == 0) {
2190 * An active block cursor will already have been done by
2191 * the above do_text call, so we only need to do anything
2195 gdk_gc_set_foreground(gc, &inst->cols[261]);
2196 gdk_draw_rectangle(inst->pixmap, gc, 0,
2197 x*inst->font_width+inst->cfg.window_border,
2198 y*inst->font_height+inst->cfg.window_border,
2199 len*widefactor*inst->font_width-1, inst->font_height-1);
2203 int startx, starty, dx, dy, length, i;
2207 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2208 char_width = 2*inst->font_width;
2210 char_width = inst->font_width;
2212 if (inst->cfg.cursor_type == 1) {
2213 uheight = inst->fonts[0]->ascent + 1;
2214 if (uheight >= inst->font_height)
2215 uheight = inst->font_height - 1;
2217 startx = x * inst->font_width + inst->cfg.window_border;
2218 starty = y * inst->font_height + inst->cfg.window_border + uheight;
2221 length = len * widefactor * char_width;
2224 if (attr & TATTR_RIGHTCURS)
2225 xadjust = char_width - 1;
2226 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2227 starty = y * inst->font_height + inst->cfg.window_border;
2230 length = inst->font_height;
2233 gdk_gc_set_foreground(gc, &inst->cols[261]);
2235 for (i = 0; i < length; i++) {
2237 gdk_draw_point(inst->pixmap, gc, startx, starty);
2242 } else if (active) {
2243 gdk_draw_line(inst->pixmap, gc, startx, starty,
2244 startx + (length-1) * dx, starty + (length-1) * dy);
2245 } /* else no cursor (e.g., blinked off) */
2248 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2249 x*inst->font_width+inst->cfg.window_border,
2250 y*inst->font_height+inst->cfg.window_border,
2251 x*inst->font_width+inst->cfg.window_border,
2252 y*inst->font_height+inst->cfg.window_border,
2253 len*widefactor*inst->font_width, inst->font_height);
2256 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2259 * Truly hideous hack: GTK doesn't allow us to set the mouse
2260 * cursor foreground and background colours unless we've _also_
2261 * created our own cursor from bitmaps. Therefore, I need to
2262 * load the `cursor' font and draw glyphs from it on to
2263 * pixmaps, in order to construct my cursors with the fg and bg
2264 * I want. This is a gross hack, but it's more self-contained
2265 * than linking in Xlib to find the X window handle to
2266 * inst->area and calling XRecolorCursor, and it's more
2267 * futureproof than hard-coding the shapes as bitmap arrays.
2269 static GdkFont *cursor_font = NULL;
2270 GdkPixmap *source, *mask;
2272 GdkColor cfg = { 0, 65535, 65535, 65535 };
2273 GdkColor cbg = { 0, 0, 0, 0 };
2274 GdkColor dfg = { 1, 65535, 65535, 65535 };
2275 GdkColor dbg = { 0, 0, 0, 0 };
2278 gint lb, rb, wid, asc, desc, w, h, x, y;
2280 if (cursor_val == -2) {
2281 gdk_font_unref(cursor_font);
2285 if (cursor_val >= 0 && !cursor_font) {
2286 cursor_font = gdk_font_load("cursor");
2288 gdk_font_ref(cursor_font);
2292 * Get the text extent of the cursor in question. We use the
2293 * mask character for this, because it's typically slightly
2294 * bigger than the main character.
2296 if (cursor_val >= 0) {
2298 text[0] = (char)cursor_val + 1;
2299 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2300 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2306 source = gdk_pixmap_new(NULL, w, h, 1);
2307 mask = gdk_pixmap_new(NULL, w, h, 1);
2310 * Draw the mask character on the mask pixmap.
2312 gc = gdk_gc_new(mask);
2313 gdk_gc_set_foreground(gc, &dbg);
2314 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2315 if (cursor_val >= 0) {
2317 text[0] = (char)cursor_val + 1;
2318 gdk_gc_set_foreground(gc, &dfg);
2319 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2324 * Draw the main character on the source pixmap.
2326 gc = gdk_gc_new(source);
2327 gdk_gc_set_foreground(gc, &dbg);
2328 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2329 if (cursor_val >= 0) {
2331 text[0] = (char)cursor_val;
2332 gdk_gc_set_foreground(gc, &dfg);
2333 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2338 * Create the cursor.
2340 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2345 gdk_pixmap_unref(source);
2346 gdk_pixmap_unref(mask);
2351 void modalfatalbox(char *p, ...)
2354 fprintf(stderr, "FATAL ERROR: ");
2356 vfprintf(stderr, p, ap);
2358 fputc('\n', stderr);
2362 void cmdline_error(char *p, ...)
2365 fprintf(stderr, "%s: ", appname);
2367 vfprintf(stderr, p, ap);
2369 fputc('\n', stderr);
2373 char *get_x_display(void *frontend)
2375 return gdk_get_display();
2378 long get_windowid(void *frontend)
2380 struct gui_data *inst = (struct gui_data *)frontend;
2381 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2384 static void help(FILE *fp) {
2386 "pterm option summary:\n"
2388 " --display DISPLAY Specify X display to use (note '--')\n"
2389 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2390 " -fn FONT Normal text font\n"
2391 " -fb FONT Bold text font\n"
2392 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2393 " -sl LINES Number of lines of scrollback\n"
2394 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2395 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2396 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2397 " -T TITLE Window title\n"
2398 " -ut, +ut Do(default) or do not update utmp\n"
2399 " -ls, +ls Do(default) or do not make shell a login shell\n"
2400 " -sb, +sb Do(default) or do not display a scrollbar\n"
2401 " -log PATH Log all output to a file\n"
2402 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2403 " -xrm RESOURCE-STRING Set an X resource\n"
2404 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2405 ) < 0 || fflush(fp) < 0) {
2406 perror("output error");
2411 int do_cmdline(int argc, char **argv, int do_everything,
2412 struct gui_data *inst, Config *cfg)
2418 * Macros to make argument handling easier. Note that because
2419 * they need to call `continue', they cannot be contained in
2420 * the usual do {...} while (0) wrapper to make them
2421 * syntactically single statements; hence it is not legal to
2422 * use one of these macros as an unbraced statement between
2425 #define EXPECTS_ARG { \
2426 if (--argc <= 0) { \
2428 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2433 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2435 while (--argc > 0) {
2440 * Shameless cheating. Debian requires all X terminal
2441 * emulators to support `-T title'; but
2442 * cmdline_process_param will eat -T (it means no-pty) and
2443 * complain that pterm doesn't support it. So, in pterm
2444 * only, we convert -T into -title.
2446 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2450 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2451 do_everything ? 1 : -1, cfg);
2454 cmdline_error("option \"%s\" requires an argument", p);
2455 } else if (ret == 2) {
2456 --argc, ++argv; /* skip next argument */
2458 } else if (ret == 1) {
2462 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2465 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2466 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2468 } else if (!strcmp(p, "-fb")) {
2471 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2472 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2474 } else if (!strcmp(p, "-fw")) {
2477 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2478 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2480 } else if (!strcmp(p, "-fwb")) {
2483 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2484 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2486 } else if (!strcmp(p, "-cs")) {
2489 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2490 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2492 } else if (!strcmp(p, "-geometry")) {
2498 flags = XParseGeometry(val, &x, &y, &w, &h);
2499 if (flags & WidthValue)
2500 cfg->width = (int)w;
2501 if (flags & HeightValue)
2502 cfg->height = (int)h;
2504 if (flags & (XValue | YValue)) {
2507 inst->gotpos = TRUE;
2508 inst->gravity = ((flags & XNegative ? 1 : 0) |
2509 (flags & YNegative ? 2 : 0));
2512 } else if (!strcmp(p, "-sl")) {
2515 cfg->savelines = atoi(val);
2517 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2518 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2519 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2524 if (!gdk_color_parse(val, &col)) {
2526 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2530 index = (!strcmp(p, "-fg") ? 0 :
2531 !strcmp(p, "-bg") ? 2 :
2532 !strcmp(p, "-bfg") ? 1 :
2533 !strcmp(p, "-bbg") ? 3 :
2534 !strcmp(p, "-cfg") ? 4 :
2535 !strcmp(p, "-cbg") ? 5 : -1);
2536 assert(index != -1);
2537 cfg->colours[index][0] = col.red / 256;
2538 cfg->colours[index][1] = col.green / 256;
2539 cfg->colours[index][2] = col.blue / 256;
2542 } else if (use_pty_argv && !strcmp(p, "-e")) {
2543 /* This option swallows all further arguments. */
2549 pty_argv = snewn(argc+1, char *);
2551 for (i = 0; i < argc; i++)
2552 pty_argv[i] = argv[i];
2553 pty_argv[argc] = NULL;
2554 break; /* finished command-line processing */
2556 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2559 } else if (!strcmp(p, "-title")) {
2562 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2563 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2565 } else if (!strcmp(p, "-log")) {
2568 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2569 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2570 cfg->logtype = LGTYP_DEBUG;
2572 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2574 cfg->stamp_utmp = 0;
2576 } else if (!strcmp(p, "-ut")) {
2578 cfg->stamp_utmp = 1;
2580 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2582 cfg->login_shell = 0;
2584 } else if (!strcmp(p, "-ls")) {
2586 cfg->login_shell = 1;
2588 } else if (!strcmp(p, "-nethack")) {
2590 cfg->nethack_keypad = 1;
2592 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2596 } else if (!strcmp(p, "-sb")) {
2600 } else if (!strcmp(p, "-name")) {
2604 } else if (!strcmp(p, "-xrm")) {
2606 provide_xrm_string(val);
2608 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2612 } else if (!strcmp(p, "-pgpfp")) {
2616 } else if(p[0] != '-' && (!do_everything ||
2617 process_nonoption_arg(p, cfg))) {
2622 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2630 * This function retrieves the character set encoding of a font. It
2631 * returns the character set without the X11 hack (in case the user
2632 * asks to use the font's own encoding).
2634 static int set_font_info(struct gui_data *inst, int fontid)
2636 GdkFont *font = inst->fonts[fontid];
2637 XFontStruct *xfs = GDK_FONT_XFONT(font);
2638 Display *disp = GDK_FONT_XDISPLAY(font);
2639 Atom charset_registry, charset_encoding;
2640 unsigned long registry_ret, encoding_ret;
2641 int retval = CS_NONE;
2643 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2644 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2645 inst->fontinfo[fontid].charset = CS_NONE;
2646 inst->fontinfo[fontid].is_wide = 0;
2647 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2648 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2650 reg = XGetAtomName(disp, (Atom)registry_ret);
2651 enc = XGetAtomName(disp, (Atom)encoding_ret);
2653 char *encoding = dupcat(reg, "-", enc, NULL);
2654 retval = inst->fontinfo[fontid].charset =
2655 charset_from_xenc(encoding);
2656 /* FIXME: when libcharset supports wide encodings fix this. */
2657 if (!strcasecmp(encoding, "iso10646-1")) {
2658 inst->fontinfo[fontid].is_wide = 1;
2663 * Hack for X line-drawing characters: if the primary
2664 * font is encoded as ISO-8859-anything, and has valid
2665 * glyphs in the first 32 char positions, it is assumed
2666 * that those glyphs are the VT100 line-drawing
2669 * Actually, we'll hack even harder by only checking
2670 * position 0x19 (vertical line, VT100 linedrawing
2671 * `x'). Then we can check it easily by seeing if the
2672 * ascent and descent differ.
2674 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2675 int lb, rb, wid, asc, desc;
2680 gdk_string_extents(inst->fonts[fontid], text,
2681 &lb, &rb, &wid, &asc, &desc);
2683 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2693 int uxsel_input_add(int fd, int rwx) {
2695 if (rwx & 1) flags |= GDK_INPUT_READ;
2696 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2697 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2699 return gdk_input_add(fd, flags, fd_input_func, NULL);
2704 void uxsel_input_remove(int id) {
2706 gdk_input_remove(id);
2709 char *guess_derived_font_name(GdkFont *font, int bold, int wide)
2711 XFontStruct *xfs = GDK_FONT_XFONT(font);
2712 Display *disp = GDK_FONT_XDISPLAY(font);
2713 Atom fontprop = XInternAtom(disp, "FONT", False);
2715 if (XGetFontProperty(xfs, fontprop, &ret)) {
2716 char *name = XGetAtomName(disp, (Atom)ret);
2717 if (name && name[0] == '-') {
2719 char *dupname, *extrafree = NULL, *ret;
2723 p = q = dupname = dupstr(name); /* skip initial minus */
2726 while (*p && nstr < lenof(strings)) {
2729 strings[nstr++] = p+1;
2734 if (nstr < lenof(strings))
2735 return NULL; /* XLFD was malformed */
2738 strings[2] = "bold";
2741 /* 4 is `wideness', which obviously may have changed. */
2742 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2743 strings[4] = strings[5] = "*";
2744 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
2747 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
2748 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
2749 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
2750 "-", strings[ 9], "-", strings[10], "-", strings[11],
2751 "-", strings[12], NULL);
2761 void setup_fonts_ucs(struct gui_data *inst)
2768 gdk_font_unref(inst->fonts[0]);
2770 gdk_font_unref(inst->fonts[1]);
2772 gdk_font_unref(inst->fonts[2]);
2774 gdk_font_unref(inst->fonts[3]);
2776 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2777 if (!inst->fonts[0]) {
2778 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2779 inst->cfg.font.name);
2782 gdk_font_ref(inst->fonts[0]);
2783 font_charset = set_font_info(inst, 0);
2785 if (inst->cfg.shadowbold) {
2786 inst->fonts[1] = NULL;
2788 if (inst->cfg.boldfont.name[0]) {
2789 name = inst->cfg.boldfont.name;
2792 name = guess_derived_font_name(inst->fonts[0], TRUE, FALSE);
2795 inst->fonts[1] = name ? gdk_font_load(name) : NULL;
2796 if (inst->fonts[1]) {
2797 gdk_font_ref(inst->fonts[1]);
2798 set_font_info(inst, 1);
2799 } else if (!guessed) {
2800 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2801 inst->cfg.boldfont.name);
2808 if (inst->cfg.widefont.name[0]) {
2809 name = inst->cfg.widefont.name;
2812 name = guess_derived_font_name(inst->fonts[0], FALSE, TRUE);
2815 inst->fonts[2] = name ? gdk_font_load(name) : NULL;
2816 if (inst->fonts[2]) {
2817 gdk_font_ref(inst->fonts[2]);
2818 set_font_info(inst, 2);
2819 } else if (!guessed) {
2820 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2821 inst->cfg.widefont.name);
2827 if (inst->cfg.shadowbold) {
2828 inst->fonts[3] = NULL;
2830 if (inst->cfg.wideboldfont.name[0]) {
2831 name = inst->cfg.wideboldfont.name;
2835 * Here we have some choices. We can widen the bold font,
2836 * bolden the wide font, or widen and bolden the standard
2837 * font. Try them all, in that order!
2839 if (inst->cfg.widefont.name[0])
2840 name = guess_derived_font_name(inst->fonts[2], TRUE, FALSE);
2841 else if (inst->cfg.boldfont.name[0])
2842 name = guess_derived_font_name(inst->fonts[1], FALSE, TRUE);
2844 name = guess_derived_font_name(inst->fonts[0], TRUE, TRUE);
2847 inst->fonts[3] = name ? gdk_font_load(name) : NULL;
2848 if (inst->fonts[3]) {
2849 gdk_font_ref(inst->fonts[3]);
2850 set_font_info(inst, 3);
2851 } else if (!guessed) {
2852 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n", appname,
2853 inst->cfg.wideboldfont.name);
2860 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2861 if (!inst->font_width) {
2862 /* Maybe this is a 16-bit font? If so, GDK 2 actually expects a
2863 * pointer to an XChar2b. This is pretty revolting. Can Pango do
2864 * this more neatly even for server-side fonts?
2869 inst->font_width = gdk_text_width(inst->fonts[0],
2870 (const gchar *)&space, 2);
2872 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2874 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2875 inst->cfg.utf8_override, font_charset,
2879 void set_geom_hints(struct gui_data *inst)
2882 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2883 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2884 geom.max_width = geom.max_height = -1;
2885 geom.base_width = 2*inst->cfg.window_border;
2886 geom.base_height = 2*inst->cfg.window_border;
2887 geom.width_inc = inst->font_width;
2888 geom.height_inc = inst->font_height;
2889 geom.min_aspect = geom.max_aspect = 0;
2890 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2891 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2892 GDK_HINT_RESIZE_INC);
2895 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2897 struct gui_data *inst = (struct gui_data *)data;
2898 term_clrsb(inst->term);
2901 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2903 struct gui_data *inst = (struct gui_data *)data;
2904 term_pwron(inst->term, TRUE);
2906 ldisc_send(inst->ldisc, NULL, 0, 0);
2909 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2911 struct gui_data *inst = (struct gui_data *)data;
2912 term_copyall(inst->term);
2915 void special_menuitem(GtkMenuItem *item, gpointer data)
2917 struct gui_data *inst = (struct gui_data *)data;
2918 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2922 inst->back->special(inst->backhandle, code);
2925 void about_menuitem(GtkMenuItem *item, gpointer data)
2927 struct gui_data *inst = (struct gui_data *)data;
2928 about_box(inst->window);
2931 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2933 struct gui_data *inst = (struct gui_data *)data;
2934 showeventlog(inst->eventlogstuff, inst->window);
2937 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2939 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2940 static const int ww[] = {
2941 256, 257, 258, 259, 260, 261,
2942 0, 8, 1, 9, 2, 10, 3, 11,
2943 4, 12, 5, 13, 6, 14, 7, 15
2945 struct gui_data *inst = (struct gui_data *)data;
2946 char *title = dupcat(appname, " Reconfiguration", NULL);
2947 Config cfg2, oldcfg;
2950 assert(lenof(ww) == NCFGCOLOURS);
2952 if (inst->reconfiguring)
2955 inst->reconfiguring = TRUE;
2957 cfg2 = inst->cfg; /* structure copy */
2959 if (do_config_box(title, &cfg2, 1,
2960 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2962 oldcfg = inst->cfg; /* structure copy */
2963 inst->cfg = cfg2; /* structure copy */
2965 /* Pass new config data to the logging module */
2966 log_reconfig(inst->logctx, &cfg2);
2968 * Flush the line discipline's edit buffer in the case
2969 * where local editing has just been disabled.
2972 ldisc_send(inst->ldisc, NULL, 0, 0);
2973 /* Pass new config data to the terminal */
2974 term_reconfig(inst->term, &cfg2);
2975 /* Pass new config data to the back end */
2977 inst->back->reconfig(inst->backhandle, &cfg2);
2980 * Just setting inst->cfg is sufficient to cause colour
2981 * setting changes to appear on the next ESC]R palette
2982 * reset. But we should also check whether any colour
2983 * settings have been changed, and revert the ones that
2984 * have to the new default, on the assumption that the user
2985 * is most likely to want an immediate update.
2987 for (i = 0; i < NCFGCOLOURS; i++) {
2988 if (oldcfg.colours[i][0] != cfg2.colours[i][0] ||
2989 oldcfg.colours[i][1] != cfg2.colours[i][1] ||
2990 oldcfg.colours[i][2] != cfg2.colours[i][2]) {
2991 real_palette_set(inst, ww[i], cfg2.colours[i][0],
2993 cfg2.colours[i][2]);
2996 * If the default background has changed, we must
2997 * repaint the space in between the window border
2998 * and the text area.
3001 set_window_background(inst);
3002 draw_backing_rect(inst);
3008 * If the scrollbar needs to be shown, hidden, or moved
3009 * from one end to the other of the window, do so now.
3011 if (oldcfg.scrollbar != cfg2.scrollbar) {
3013 gtk_widget_show(inst->sbar);
3015 gtk_widget_hide(inst->sbar);
3017 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
3018 gtk_box_reorder_child(inst->hbox, inst->sbar,
3019 cfg2.scrollbar_on_left ? 0 : 1);
3023 * Change the window title, if required.
3025 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
3026 set_title(inst, cfg2.wintitle);
3027 set_window_titles(inst);
3030 * Redo the whole tangled fonts and Unicode mess if
3033 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
3034 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
3035 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
3036 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
3037 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
3038 oldcfg.vtmode != cfg2.vtmode ||
3039 oldcfg.shadowbold != cfg2.shadowbold) {
3040 setup_fonts_ucs(inst);
3046 * Resize the window.
3048 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
3049 oldcfg.window_border != cfg2.window_border || need_size) {
3050 set_geom_hints(inst);
3051 request_resize(inst, cfg2.width, cfg2.height);
3054 * The above will have caused a call to term_size() for
3055 * us if it happened. If the user has fiddled with only
3056 * the scrollback size, the above will not have
3057 * happened and we will need an explicit term_size()
3060 if (oldcfg.savelines != cfg2.savelines)
3061 term_size(inst->term, inst->term->rows, inst->term->cols,
3065 term_invalidate(inst->term);
3068 * We do an explicit full redraw here to ensure the window
3069 * border has been redrawn as well as the text area.
3071 gtk_widget_queue_draw(inst->area);
3074 inst->reconfiguring = FALSE;
3077 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3080 * Re-execing ourself is not an exact science under Unix. I do
3081 * the best I can by using /proc/self/exe if available and by
3082 * assuming argv[0] can be found on $PATH if not.
3084 * Note that we also have to reconstruct the elements of the
3085 * original argv which gtk swallowed, since the user wants the
3086 * new session to appear on the same X display as the old one.
3094 * Collect the arguments with which to re-exec ourself.
3096 va_start(ap, fd_to_close);
3097 n = 2; /* progname and terminating NULL */
3098 n += inst->ngtkargs;
3099 while (va_arg(ap, char *) != NULL)
3103 args = snewn(n, char *);
3104 args[0] = inst->progname;
3106 for (i = 0; i < inst->ngtkargs; i++)
3107 args[i+1] = inst->gtkargvstart[i];
3110 va_start(ap, fd_to_close);
3111 while ((args[i++] = va_arg(ap, char *)) != NULL);
3117 * Do the double fork.
3130 } else if (pid2 > 0) {
3132 * First child has successfully forked second child. My
3133 * Work Here Is Done. Note the use of _exit rather than
3134 * exit: the latter appears to cause destroy messages
3135 * to be sent to the X server. I suspect gtk uses
3142 * If we reach here, we are the second child, so we now
3143 * actually perform the exec.
3145 if (fd_to_close >= 0)
3148 execv("/proc/self/exe", args);
3149 execvp(inst->progname, args);
3155 waitpid(pid, &status, 0);
3160 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3162 struct gui_data *inst = (struct gui_data *)gdata;
3164 * For this feature we must marshal cfg and (possibly) pty_argv
3165 * into a byte stream, create a pipe, and send this byte stream
3166 * to the child through the pipe.
3173 if (pipe(pipefd) < 0) {
3178 size = sizeof(inst->cfg);
3179 if (use_pty_argv && pty_argv) {
3180 for (i = 0; pty_argv[i]; i++)
3181 size += strlen(pty_argv[i]) + 1;
3184 data = snewn(size, char);
3185 memcpy(data, &inst->cfg, sizeof(inst->cfg));
3186 if (use_pty_argv && pty_argv) {
3187 int p = sizeof(inst->cfg);
3188 for (i = 0; pty_argv[i]; i++) {
3189 strcpy(data + p, pty_argv[i]);
3190 p += strlen(pty_argv[i]) + 1;
3195 sprintf(option, "---[%d,%d]", pipefd[0], size);
3196 fcntl(pipefd[0], F_SETFD, 0);
3197 fork_and_exec_self(inst, pipefd[1], option, NULL);
3201 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3204 perror("write to pipe");
3209 int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3211 int fd, i, ret, size;
3214 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3215 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3219 data = snewn(size, char);
3221 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3224 perror("read from pipe");
3226 } else if (i < size) {
3227 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3232 memcpy(cfg, data, sizeof(Config));
3233 if (use_pty_argv && size > sizeof(Config)) {
3237 while (i < size && data[i]) i++;
3239 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3246 pty_argv = snewn(n+1, char *);
3252 while (i < size && data[i]) i++;
3255 pty_argv[n++] = dupstr(p);
3262 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3264 struct gui_data *inst = (struct gui_data *)data;
3266 fork_and_exec_self(inst, -1, NULL);
3269 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3271 struct gui_data *inst = (struct gui_data *)data;
3274 logevent(inst, "----- Session restarted -----");
3275 term_pwron(inst->term, FALSE);
3276 start_backend(inst);
3277 inst->exited = FALSE;
3281 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3283 struct gui_data *inst = (struct gui_data *)data;
3284 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3286 fork_and_exec_self(inst, -1, "-load", str, NULL);
3289 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3291 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3296 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3298 struct gui_data *inst = (struct gui_data *)data;
3299 struct sesslist sesslist;
3302 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3303 (GtkCallback)gtk_widget_destroy, NULL);
3305 get_sesslist(&sesslist, TRUE);
3306 for (i = 1; i < sesslist.nsessions; i++) {
3307 GtkWidget *menuitem =
3308 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3309 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3310 gtk_widget_show(menuitem);
3311 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3312 dupstr(sesslist.sessions[i]));
3313 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3314 GTK_SIGNAL_FUNC(saved_session_menuitem),
3316 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3317 GTK_SIGNAL_FUNC(saved_session_freedata),
3320 get_sesslist(&sesslist, FALSE); /* free up */
3323 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3327 #if GTK_CHECK_VERSION(2,0,0)
3335 gtk_widget_realize(window);
3336 iconpm = gdk_pixmap_create_from_xpm_d(window->window, NULL,
3337 NULL, (gchar **)icon[0]);
3338 gdk_window_set_icon(window->window, NULL, iconpm, NULL);
3340 #if GTK_CHECK_VERSION(2,0,0)
3342 for (n = 0; n < n_icon; n++) {
3344 g_list_append(iconlist,
3345 gdk_pixbuf_new_from_xpm_data((const gchar **)
3348 gdk_window_set_icon_list(window->window, iconlist);
3352 void update_specials_menu(void *frontend)
3354 struct gui_data *inst = (struct gui_data *)frontend;
3356 const struct telnet_special *specials;
3359 specials = inst->back->get_specials(inst->backhandle);
3363 /* I believe this disposes of submenus too. */
3364 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3365 (GtkCallback)gtk_widget_destroy, NULL);
3368 GtkWidget *menu = inst->specialsmenu;
3369 /* A lame "stack" for submenus that will do for now. */
3370 GtkWidget *saved_menu = NULL;
3372 for (i = 0; nesting > 0; i++) {
3373 GtkWidget *menuitem = NULL;
3374 switch (specials[i].code) {
3376 assert (nesting < 2);
3377 saved_menu = menu; /* XXX lame stacking */
3378 menu = gtk_menu_new();
3379 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3380 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3381 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3382 gtk_widget_show(menuitem);
3389 menu = saved_menu; /* XXX lame stacking */
3394 menuitem = gtk_menu_item_new();
3397 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3398 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3399 GINT_TO_POINTER(specials[i].code));
3400 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3401 GTK_SIGNAL_FUNC(special_menuitem), inst);
3405 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3406 gtk_widget_show(menuitem);
3409 gtk_widget_show(inst->specialsitem1);
3410 gtk_widget_show(inst->specialsitem2);
3412 gtk_widget_hide(inst->specialsitem1);
3413 gtk_widget_hide(inst->specialsitem2);
3417 static void start_backend(struct gui_data *inst)
3419 extern Backend *select_backend(Config *cfg);
3423 inst->back = select_backend(&inst->cfg);
3425 error = inst->back->init((void *)inst, &inst->backhandle,
3426 &inst->cfg, inst->cfg.host, inst->cfg.port,
3427 &realhost, inst->cfg.tcp_nodelay,
3428 inst->cfg.tcp_keepalives);
3431 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3432 inst->cfg.host, error);
3433 inst->exited = TRUE;
3434 fatal_message_box(inst->window, msg);
3439 if (inst->cfg.wintitle[0]) {
3440 set_title(inst, inst->cfg.wintitle);
3441 set_icon(inst, inst->cfg.wintitle);
3443 char *title = make_default_wintitle(realhost);
3444 set_title(inst, title);
3445 set_icon(inst, title);
3450 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3452 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3455 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3458 gtk_widget_hide(inst->restartitem);
3461 int pt_main(int argc, char **argv)
3463 extern int cfgbox(Config *cfg);
3464 struct gui_data *inst;
3467 * Create an instance structure and initialise to zeroes
3469 inst = snew(struct gui_data);
3470 memset(inst, 0, sizeof(*inst));
3471 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3472 inst->busy_status = BUSY_NOT;
3474 /* defer any child exit handling until we're ready to deal with
3476 block_signal(SIGCHLD, 1);
3478 inst->progname = argv[0];
3480 * Copy the original argv before letting gtk_init fiddle with
3481 * it. It will be required later.
3485 inst->gtkargvstart = snewn(argc-1, char *);
3486 for (i = 1; i < argc; i++)
3487 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3489 gtk_init(&argc, &argv);
3490 inst->ngtkargs = oldargc - argc;
3493 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3494 read_dupsession_data(inst, &inst->cfg, argv[1]);
3495 /* Splatter this argument so it doesn't clutter a ps listing */
3496 memset(argv[1], 0, strlen(argv[1]));
3498 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
3499 exit(1); /* pre-defaults pass to get -class */
3500 do_defaults(NULL, &inst->cfg);
3501 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
3502 exit(1); /* post-defaults, do everything */
3504 cmdline_run_saved(&inst->cfg);
3506 if (!cfg_launchable(&inst->cfg) && !cfgbox(&inst->cfg))
3507 exit(0); /* config box hit Cancel */
3510 if (!compound_text_atom)
3511 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3512 if (!utf8_string_atom)
3513 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3515 setup_fonts_ucs(inst);
3518 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3521 * Set up the colour map.
3523 palette_reset(inst);
3525 inst->width = inst->cfg.width;
3526 inst->height = inst->cfg.height;
3528 inst->area = gtk_drawing_area_new();
3529 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3530 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3531 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
3532 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3533 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3534 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3536 * We always create the scrollbar; it remains invisible if
3537 * unwanted, so we can pop it up quickly if it suddenly becomes
3540 if (inst->cfg.scrollbar_on_left)
3541 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3542 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3543 if (!inst->cfg.scrollbar_on_left)
3544 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3546 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3548 set_geom_hints(inst);
3550 gtk_widget_show(inst->area);
3551 if (inst->cfg.scrollbar)
3552 gtk_widget_show(inst->sbar);
3554 gtk_widget_hide(inst->sbar);
3555 gtk_widget_show(GTK_WIDGET(inst->hbox));
3558 int x = inst->xpos, y = inst->ypos;
3560 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3561 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3562 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3563 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3564 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3567 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3568 GTK_SIGNAL_FUNC(destroy), inst);
3569 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3570 GTK_SIGNAL_FUNC(delete_window), inst);
3571 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3572 GTK_SIGNAL_FUNC(key_event), inst);
3573 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3574 GTK_SIGNAL_FUNC(key_event), inst);
3575 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3576 GTK_SIGNAL_FUNC(focus_event), inst);
3577 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3578 GTK_SIGNAL_FUNC(focus_event), inst);
3579 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3580 GTK_SIGNAL_FUNC(configure_area), inst);
3581 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3582 GTK_SIGNAL_FUNC(expose_area), inst);
3583 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3584 GTK_SIGNAL_FUNC(button_event), inst);
3585 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3586 GTK_SIGNAL_FUNC(button_event), inst);
3587 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3588 GTK_SIGNAL_FUNC(motion_event), inst);
3589 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3590 GTK_SIGNAL_FUNC(selection_received), inst);
3591 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3592 GTK_SIGNAL_FUNC(selection_get), inst);
3593 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3594 GTK_SIGNAL_FUNC(selection_clear), inst);
3595 if (inst->cfg.scrollbar)
3596 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3597 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3598 gtk_widget_add_events(GTK_WIDGET(inst->area),
3599 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3600 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3601 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3604 extern const char *const *const main_icon[];
3605 extern const int n_main_icon;
3606 set_window_icon(inst->window, main_icon, n_main_icon);
3609 gtk_widget_show(inst->window);
3611 set_window_background(inst);
3614 * Set up the Ctrl+rightclick context menu.
3617 GtkWidget *menuitem;
3619 extern const int use_event_log, new_session, saved_sessions;
3621 inst->menu = gtk_menu_new();
3623 #define MKMENUITEM(title, func) do { \
3624 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3625 gtk_menu_item_new(); \
3626 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3627 gtk_widget_show(menuitem); \
3629 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3630 GTK_SIGNAL_FUNC(func), inst); \
3633 MKMENUITEM("New Session", new_session_menuitem);
3634 MKMENUITEM("Restart Session", restart_session_menuitem);
3635 inst->restartitem = menuitem;
3636 gtk_widget_hide(inst->restartitem);
3637 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3638 if (saved_sessions) {
3639 inst->sessionsmenu = gtk_menu_new();
3640 /* sessionsmenu will be updated when it's invoked */
3641 /* XXX is this the right way to do dynamic menus in Gtk? */
3642 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3643 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3644 inst->sessionsmenu);
3646 MKMENUITEM(NULL, NULL);
3647 MKMENUITEM("Change Settings", change_settings_menuitem);
3648 MKMENUITEM(NULL, NULL);
3650 MKMENUITEM("Event Log", event_log_menuitem);
3651 MKMENUITEM("Special Commands", NULL);
3652 inst->specialsmenu = gtk_menu_new();
3653 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3654 inst->specialsitem1 = menuitem;
3655 MKMENUITEM(NULL, NULL);
3656 inst->specialsitem2 = menuitem;
3657 gtk_widget_hide(inst->specialsitem1);
3658 gtk_widget_hide(inst->specialsitem2);
3659 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3660 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3661 MKMENUITEM("Copy All", copy_all_menuitem);
3662 MKMENUITEM(NULL, NULL);
3663 s = dupcat("About ", appname, NULL);
3664 MKMENUITEM(s, about_menuitem);
3669 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3670 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3671 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3672 inst->blankcursor = make_mouse_ptr(inst, -1);
3673 make_mouse_ptr(inst, -2); /* clean up cursor font */
3674 inst->currcursor = inst->textcursor;
3675 show_mouseptr(inst, 1);
3677 inst->eventlogstuff = eventlogstuff_new();
3679 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3680 inst->logctx = log_init(inst, &inst->cfg);
3681 term_provide_logctx(inst->term, inst->logctx);
3685 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3687 start_backend(inst);
3689 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3691 /* now we're reday to deal with the child exit handler being
3693 block_signal(SIGCHLD, 0);
3696 * Block SIGPIPE: if we attempt Duplicate Session or similar
3697 * and it falls over in some way, we certainly don't want
3698 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3699 * this _after_ (at least pterm) forks off its child process,
3700 * since the child wants SIGPIPE handled in the usual way.
3702 block_signal(SIGPIPE, 1);
3704 inst->exited = FALSE;