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 update_specials_menu(inst);
1210 gtk_widget_show(inst->restartitem);
1214 static gint timer_trigger(gpointer data)
1216 long now = GPOINTER_TO_INT(data);
1220 if (run_timers(now, &next)) {
1221 ticks = next - GETTICKCOUNT();
1222 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1223 GINT_TO_POINTER(next));
1227 * Never let a timer resume. If we need another one, we've
1228 * asked for it explicitly above.
1233 void timer_change_notify(long next)
1238 gtk_timeout_remove(timer_id);
1240 ticks = next - GETTICKCOUNT();
1242 ticks = 1; /* just in case */
1244 timer_id = gtk_timeout_add(ticks, timer_trigger,
1245 GINT_TO_POINTER(next));
1248 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1251 * We must process exceptional notifications before ordinary
1252 * readability ones, or we may go straight past the urgent
1255 if (condition & GDK_INPUT_EXCEPTION)
1256 select_result(sourcefd, 4);
1257 if (condition & GDK_INPUT_READ)
1258 select_result(sourcefd, 1);
1259 if (condition & GDK_INPUT_WRITE)
1260 select_result(sourcefd, 2);
1263 void destroy(GtkWidget *widget, gpointer data)
1268 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1270 struct gui_data *inst = (struct gui_data *)data;
1271 term_set_focus(inst->term, event->in);
1272 term_update(inst->term);
1273 show_mouseptr(inst, 1);
1277 void set_busy_status(void *frontend, int status)
1279 struct gui_data *inst = (struct gui_data *)frontend;
1280 inst->busy_status = status;
1281 update_mouseptr(inst);
1285 * set or clear the "raw mouse message" mode
1287 void set_raw_mouse_mode(void *frontend, int activate)
1289 struct gui_data *inst = (struct gui_data *)frontend;
1290 activate = activate && !inst->cfg.no_mouse_rep;
1291 send_raw_mouse = activate;
1292 update_mouseptr(inst);
1295 void request_resize(void *frontend, int w, int h)
1297 struct gui_data *inst = (struct gui_data *)frontend;
1298 int large_x, large_y;
1299 int offset_x, offset_y;
1301 GtkRequisition inner, outer;
1304 * This is a heinous hack dreamed up by the gnome-terminal
1305 * people to get around a limitation in gtk. The problem is
1306 * that in order to set the size correctly we really need to be
1307 * calling gtk_window_resize - but that needs to know the size
1308 * of the _whole window_, not the drawing area. So what we do
1309 * is to set an artificially huge size request on the drawing
1310 * area, recompute the resulting size request on the window,
1311 * and look at the difference between the two. That gives us
1312 * the x and y offsets we need to translate drawing area size
1313 * into window size for real, and then we call
1314 * gtk_window_resize.
1318 * We start by retrieving the current size of the whole window.
1319 * Adding a bit to _that_ will give us a value we can use as a
1320 * bogus size request which guarantees to be bigger than the
1321 * current size of the drawing area.
1323 get_window_pixels(inst, &large_x, &large_y);
1327 #if GTK_CHECK_VERSION(2,0,0)
1328 gtk_widget_set_size_request(inst->area, large_x, large_y);
1330 gtk_widget_set_usize(inst->area, large_x, large_y);
1332 gtk_widget_size_request(inst->area, &inner);
1333 gtk_widget_size_request(inst->window, &outer);
1335 offset_x = outer.width - inner.width;
1336 offset_y = outer.height - inner.height;
1338 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1339 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1342 * Now we must set the size request on the drawing area back to
1343 * something sensible before we commit the real resize. Best
1344 * way to do this, I think, is to set it to what the size is
1345 * really going to end up being.
1347 #if GTK_CHECK_VERSION(2,0,0)
1348 gtk_widget_set_size_request(inst->area, area_x, area_y);
1350 gtk_widget_set_usize(inst->area, area_x, area_y);
1351 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1354 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1356 #if GTK_CHECK_VERSION(2,0,0)
1357 gtk_window_resize(GTK_WINDOW(inst->window),
1358 area_x + offset_x, area_y + offset_y);
1360 gdk_window_resize(inst->window->window,
1361 area_x + offset_x, area_y + offset_y);
1365 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1367 gboolean success[1];
1369 inst->cols[n].red = r * 0x0101;
1370 inst->cols[n].green = g * 0x0101;
1371 inst->cols[n].blue = b * 0x0101;
1373 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1374 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1375 FALSE, TRUE, success);
1377 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1381 void set_window_background(struct gui_data *inst)
1383 if (inst->area && inst->area->window)
1384 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1385 if (inst->window && inst->window->window)
1386 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1389 void palette_set(void *frontend, int n, int r, int g, int b)
1391 struct gui_data *inst = (struct gui_data *)frontend;
1394 if (n > NALLCOLOURS)
1396 real_palette_set(inst, n, r, g, b);
1398 /* Default Background changed. Ensure space between text area and
1399 * window border is redrawn */
1400 set_window_background(inst);
1401 draw_backing_rect(inst);
1402 gtk_widget_queue_draw(inst->area);
1406 void palette_reset(void *frontend)
1408 struct gui_data *inst = (struct gui_data *)frontend;
1409 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1410 static const int ww[] = {
1411 256, 257, 258, 259, 260, 261,
1412 0, 8, 1, 9, 2, 10, 3, 11,
1413 4, 12, 5, 13, 6, 14, 7, 15
1415 gboolean success[NALLCOLOURS];
1418 assert(lenof(ww) == NCFGCOLOURS);
1420 if (!inst->colmap) {
1421 inst->colmap = gdk_colormap_get_system();
1423 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1426 for (i = 0; i < NCFGCOLOURS; i++) {
1427 inst->cols[ww[i]].red = inst->cfg.colours[i][0] * 0x0101;
1428 inst->cols[ww[i]].green = inst->cfg.colours[i][1] * 0x0101;
1429 inst->cols[ww[i]].blue = inst->cfg.colours[i][2] * 0x0101;
1432 for (i = 0; i < NEXTCOLOURS; i++) {
1434 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1435 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1436 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1437 inst->cols[i+16].blue = b ? b + 0x2828 + 0x3737 : 0;
1439 int shade = i - 216;
1440 shade = shade * 0x0a0a + 0x0808;
1441 inst->cols[i+16].red = inst->cols[i+16].green =
1442 inst->cols[i+16].blue = shade;
1446 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1447 FALSE, TRUE, success);
1448 for (i = 0; i < NALLCOLOURS; i++) {
1450 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1451 appname, i, inst->cfg.colours[i][0],
1452 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1455 /* Since Default Background may have changed, ensure that space
1456 * between text area and window border is refreshed. */
1457 set_window_background(inst);
1459 draw_backing_rect(inst);
1460 gtk_widget_queue_draw(inst->area);
1464 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1465 * do this before we start using cut buffers.
1467 void init_cutbuffers()
1469 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1470 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, "", 0);
1471 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1472 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, "", 0);
1473 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1474 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, "", 0);
1475 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1476 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, "", 0);
1477 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1478 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, "", 0);
1479 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1480 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, "", 0);
1481 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1482 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, "", 0);
1483 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1484 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, "", 0);
1487 /* Store the data in a cut-buffer. */
1488 void store_cutbuffer(char * ptr, int len)
1490 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1491 XRotateBuffers(GDK_DISPLAY(), 1);
1492 XStoreBytes(GDK_DISPLAY(), ptr, len);
1495 /* Retrieve data from a cut-buffer.
1496 * Returned data needs to be freed with XFree().
1498 char * retrieve_cutbuffer(int * nbytes)
1501 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1502 if (*nbytes <= 0 && ptr != 0) {
1509 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1511 struct gui_data *inst = (struct gui_data *)frontend;
1512 if (inst->pasteout_data)
1513 sfree(inst->pasteout_data);
1514 if (inst->pasteout_data_ctext)
1515 sfree(inst->pasteout_data_ctext);
1516 if (inst->pasteout_data_utf8)
1517 sfree(inst->pasteout_data_utf8);
1520 * Set up UTF-8 and compound text paste data. This only happens
1521 * if we aren't in direct-to-font mode using the D800 hack.
1523 if (!inst->direct_to_font) {
1524 wchar_t *tmp = data;
1529 inst->pasteout_data_utf8 = snewn(len*6, char);
1530 inst->pasteout_data_utf8_len = len*6;
1531 inst->pasteout_data_utf8_len =
1532 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1533 inst->pasteout_data_utf8_len,
1534 CS_UTF8, NULL, NULL, 0);
1535 if (inst->pasteout_data_utf8_len == 0) {
1536 sfree(inst->pasteout_data_utf8);
1537 inst->pasteout_data_utf8 = NULL;
1539 inst->pasteout_data_utf8 =
1540 sresize(inst->pasteout_data_utf8,
1541 inst->pasteout_data_utf8_len + 1, char);
1542 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1546 * Now let Xlib convert our UTF-8 data into compound text.
1548 list[0] = inst->pasteout_data_utf8;
1549 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1550 XCompoundTextStyle, &tp) == 0) {
1551 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1552 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1553 inst->pasteout_data_ctext_len = tp.nitems;
1556 inst->pasteout_data_ctext = NULL;
1557 inst->pasteout_data_ctext_len = 0;
1560 inst->pasteout_data_utf8 = NULL;
1561 inst->pasteout_data_utf8_len = 0;
1562 inst->pasteout_data_ctext = NULL;
1563 inst->pasteout_data_ctext_len = 0;
1566 inst->pasteout_data = snewn(len*6, char);
1567 inst->pasteout_data_len = len*6;
1568 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1569 data, len, inst->pasteout_data,
1570 inst->pasteout_data_len,
1572 if (inst->pasteout_data_len == 0) {
1573 sfree(inst->pasteout_data);
1574 inst->pasteout_data = NULL;
1576 inst->pasteout_data =
1577 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1580 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1582 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1583 inst->input_event_time)) {
1584 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1585 GDK_SELECTION_TYPE_STRING, 1);
1586 if (inst->pasteout_data_ctext)
1587 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1588 compound_text_atom, 1);
1589 if (inst->pasteout_data_utf8)
1590 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1591 utf8_string_atom, 1);
1595 term_deselect(inst->term);
1598 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1599 guint info, guint time_stamp, gpointer data)
1601 struct gui_data *inst = (struct gui_data *)data;
1602 if (seldata->target == utf8_string_atom)
1603 gtk_selection_data_set(seldata, seldata->target, 8,
1604 inst->pasteout_data_utf8,
1605 inst->pasteout_data_utf8_len);
1606 else if (seldata->target == compound_text_atom)
1607 gtk_selection_data_set(seldata, seldata->target, 8,
1608 inst->pasteout_data_ctext,
1609 inst->pasteout_data_ctext_len);
1611 gtk_selection_data_set(seldata, seldata->target, 8,
1612 inst->pasteout_data, inst->pasteout_data_len);
1615 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1618 struct gui_data *inst = (struct gui_data *)data;
1620 term_deselect(inst->term);
1621 if (inst->pasteout_data)
1622 sfree(inst->pasteout_data);
1623 if (inst->pasteout_data_ctext)
1624 sfree(inst->pasteout_data_ctext);
1625 if (inst->pasteout_data_utf8)
1626 sfree(inst->pasteout_data_utf8);
1627 inst->pasteout_data = NULL;
1628 inst->pasteout_data_len = 0;
1629 inst->pasteout_data_ctext = NULL;
1630 inst->pasteout_data_ctext_len = 0;
1631 inst->pasteout_data_utf8 = NULL;
1632 inst->pasteout_data_utf8_len = 0;
1636 void request_paste(void *frontend)
1638 struct gui_data *inst = (struct gui_data *)frontend;
1640 * In Unix, pasting is asynchronous: all we can do at the
1641 * moment is to call gtk_selection_convert(), and when the data
1642 * comes back _then_ we can call term_do_paste().
1645 if (!inst->direct_to_font) {
1647 * First we attempt to retrieve the selection as a UTF-8
1648 * string (which we will convert to the correct code page
1649 * before sending to the session, of course). If that
1650 * fails, selection_received() will be informed and will
1651 * fall back to an ordinary string.
1653 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1655 inst->input_event_time);
1658 * If we're in direct-to-font mode, we disable UTF-8
1659 * pasting, and go straight to ordinary string data.
1661 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1662 GDK_SELECTION_TYPE_STRING,
1663 inst->input_event_time);
1667 gint idle_paste_func(gpointer data); /* forward ref */
1669 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1670 guint time, gpointer data)
1672 struct gui_data *inst = (struct gui_data *)data;
1676 int length, count, ret;
1677 int free_list_required = 0;
1678 int free_required = 0;
1681 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1683 * Failed to get a UTF-8 selection string. Try compound
1686 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1688 inst->input_event_time);
1692 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1694 * Failed to get UTF-8 or compound text. Try an ordinary
1697 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1698 GDK_SELECTION_TYPE_STRING,
1699 inst->input_event_time);
1704 * If we have data, but it's not of a type we can deal with,
1705 * we have to ignore the data.
1707 if (seldata->length > 0 &&
1708 seldata->type != GDK_SELECTION_TYPE_STRING &&
1709 seldata->type != compound_text_atom &&
1710 seldata->type != utf8_string_atom)
1714 * If we have no data, try looking in a cut buffer.
1716 if (seldata->length <= 0) {
1717 text = retrieve_cutbuffer(&length);
1720 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1721 * source, so use that as a de-facto standard. */
1722 charset = CS_ISO8859_1;
1726 * Convert COMPOUND_TEXT into UTF-8.
1728 if (seldata->type == compound_text_atom) {
1729 tp.value = seldata->data;
1730 tp.encoding = (Atom) seldata->type;
1731 tp.format = seldata->format;
1732 tp.nitems = seldata->length;
1733 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1735 if (ret != 0 || count != 1) {
1737 * Compound text failed; fall back to STRING.
1739 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1740 GDK_SELECTION_TYPE_STRING,
1741 inst->input_event_time);
1745 length = strlen(list[0]);
1747 free_list_required = 1;
1749 text = (char *)seldata->data;
1750 length = seldata->length;
1751 charset = (seldata->type == utf8_string_atom ?
1752 CS_UTF8 : inst->ucsdata.line_codepage);
1756 if (inst->pastein_data)
1757 sfree(inst->pastein_data);
1759 inst->pastein_data = snewn(length, wchar_t);
1760 inst->pastein_data_len = length;
1761 inst->pastein_data_len =
1762 mb_to_wc(charset, 0, text, length,
1763 inst->pastein_data, inst->pastein_data_len);
1765 term_do_paste(inst->term);
1767 if (term_paste_pending(inst->term))
1768 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1770 if (free_list_required)
1771 XFreeStringList(list);
1776 gint idle_paste_func(gpointer data)
1778 struct gui_data *inst = (struct gui_data *)data;
1780 if (term_paste_pending(inst->term))
1781 term_paste(inst->term);
1783 gtk_idle_remove(inst->term_paste_idle_id);
1789 void get_clip(void *frontend, wchar_t ** p, int *len)
1791 struct gui_data *inst = (struct gui_data *)frontend;
1794 *p = inst->pastein_data;
1795 *len = inst->pastein_data_len;
1799 static void set_window_titles(struct gui_data *inst)
1802 * We must always call set_icon_name after calling set_title,
1803 * since set_title will write both names. Irritating, but such
1806 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1807 if (!inst->cfg.win_name_always)
1808 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1811 void set_title(void *frontend, char *title)
1813 struct gui_data *inst = (struct gui_data *)frontend;
1814 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1815 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1816 set_window_titles(inst);
1819 void set_icon(void *frontend, char *title)
1821 struct gui_data *inst = (struct gui_data *)frontend;
1822 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1823 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1824 set_window_titles(inst);
1827 void set_sbar(void *frontend, int total, int start, int page)
1829 struct gui_data *inst = (struct gui_data *)frontend;
1830 if (!inst->cfg.scrollbar)
1832 inst->sbar_adjust->lower = 0;
1833 inst->sbar_adjust->upper = total;
1834 inst->sbar_adjust->value = start;
1835 inst->sbar_adjust->page_size = page;
1836 inst->sbar_adjust->step_increment = 1;
1837 inst->sbar_adjust->page_increment = page/2;
1838 inst->ignore_sbar = TRUE;
1839 gtk_adjustment_changed(inst->sbar_adjust);
1840 inst->ignore_sbar = FALSE;
1843 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1845 struct gui_data *inst = (struct gui_data *)data;
1847 if (!inst->cfg.scrollbar)
1849 if (!inst->ignore_sbar)
1850 term_scroll(inst->term, 1, (int)adj->value);
1853 void sys_cursor(void *frontend, int x, int y)
1856 * This is meaningless under X.
1861 * This is still called when mode==BELL_VISUAL, even though the
1862 * visual bell is handled entirely within terminal.c, because we
1863 * may want to perform additional actions on any kind of bell (for
1864 * example, taskbar flashing in Windows).
1866 void do_beep(void *frontend, int mode)
1868 if (mode != BELL_VISUAL)
1872 int char_width(Context ctx, int uc)
1875 * Under X, any fixed-width font really _is_ fixed-width.
1876 * Double-width characters will be dealt with using a separate
1877 * font. For the moment we can simply return 1.
1882 Context get_ctx(void *frontend)
1884 struct gui_data *inst = (struct gui_data *)frontend;
1885 struct draw_ctx *dctx;
1887 if (!inst->area->window)
1890 dctx = snew(struct draw_ctx);
1892 dctx->gc = gdk_gc_new(inst->area->window);
1896 void free_ctx(Context ctx)
1898 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1899 /* struct gui_data *inst = dctx->inst; */
1900 GdkGC *gc = dctx->gc;
1906 * Draw a line of text in the window, at given character
1907 * coordinates, in given attributes.
1909 * We are allowed to fiddle with the contents of `text'.
1911 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1912 unsigned long attr, int lattr)
1914 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1915 struct gui_data *inst = dctx->inst;
1916 GdkGC *gc = dctx->gc;
1917 int ncombining, combining;
1918 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1919 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
1921 if (attr & TATTR_COMBINING) {
1927 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
1928 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
1929 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
1934 if (inst->cfg.bold_colour && (attr & ATTR_BOLD)) {
1935 if (nfg < 16) nfg |= 8;
1936 else if (nfg >= 256) nfg |= 1;
1938 if (inst->cfg.bold_colour && (attr & ATTR_BLINK)) {
1939 if (nbg < 16) nbg |= 8;
1940 else if (nbg >= 256) nbg |= 1;
1942 if ((attr & TATTR_ACTCURS) && !monochrome) {
1947 fontid = shadow = 0;
1949 if (attr & ATTR_WIDE) {
1956 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1957 if (inst->fonts[fontid | 1])
1963 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1965 if (x >= inst->term->cols)
1967 if (x + len*2*widefactor > inst->term->cols)
1968 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1976 r.x = x*inst->font_width+inst->cfg.window_border;
1977 r.y = y*inst->font_height+inst->cfg.window_border;
1978 r.width = rlen*widefactor*inst->font_width;
1979 r.height = inst->font_height;
1980 gdk_gc_set_clip_rectangle(gc, &r);
1983 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1984 gdk_draw_rectangle(inst->pixmap, gc, 1,
1985 x*inst->font_width+inst->cfg.window_border,
1986 y*inst->font_height+inst->cfg.window_border,
1987 rlen*widefactor*inst->font_width, inst->font_height);
1989 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1996 wcs = snewn(len*ncombining+1, wchar_t);
1997 for (i = 0; i < len*ncombining; i++) {
2001 if (inst->fonts[fontid] == NULL) {
2003 * The font for this contingency does not exist.
2004 * Typically this means we've been given ATTR_WIDE
2005 * character and have no wide font. So we display
2006 * nothing at all; such is life.
2008 } else if (inst->fontinfo[fontid].is_wide) {
2010 * At least one version of gdk_draw_text_wc() has a
2011 * weird bug whereby it reads `len' elements of the
2012 * input string, but only draws `len/2'. Hence I'm
2013 * going to make its input array twice as long as it
2014 * theoretically needs to be, and pass in twice the
2015 * actual number of characters. If a fixed gdk actually
2016 * takes the doubled length seriously, then (a) the
2017 * array will stand scrutiny up to the full length, (b)
2018 * the spare elements of the array are full of zeroes
2019 * which will probably be an empty glyph in the font,
2020 * and (c) the clip rectangle should prevent it causing
2023 gwcs = snewn(len*2+1, GdkWChar);
2024 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2026 * FIXME: when we have a wide-char equivalent of
2027 * from_unicode, use it instead of this.
2029 for (combining = 0; combining < ncombining; combining++) {
2030 for (i = 0; i <= len; i++)
2031 gwcs[i] = wcs[i + combining];
2032 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2033 x*inst->font_width+inst->cfg.window_border,
2034 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2037 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2038 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2039 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2044 gcs = snewn(len+1, gchar);
2045 for (combining = 0; combining < ncombining; combining++) {
2046 wc_to_mb(inst->fontinfo[fontid].charset, 0,
2047 wcs + combining, len, gcs, len, ".", NULL, NULL);
2048 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2049 x*inst->font_width+inst->cfg.window_border,
2050 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2053 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2054 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2055 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2063 if (attr & ATTR_UNDER) {
2064 int uheight = inst->fonts[0]->ascent + 1;
2065 if (uheight >= inst->font_height)
2066 uheight = inst->font_height - 1;
2067 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
2068 y*inst->font_height + uheight + inst->cfg.window_border,
2069 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
2070 y*inst->font_height + uheight + inst->cfg.window_border);
2073 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2075 * I can't find any plausible StretchBlt equivalent in the
2076 * X server, so I'm going to do this the slow and painful
2077 * way. This will involve repeated calls to
2078 * gdk_draw_pixmap() to stretch the text horizontally. It's
2079 * O(N^2) in time and O(N) in network bandwidth, but you
2080 * try thinking of a better way. :-(
2083 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2084 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2085 x*inst->font_width+inst->cfg.window_border + 2*i,
2086 y*inst->font_height+inst->cfg.window_border,
2087 x*inst->font_width+inst->cfg.window_border + 2*i+1,
2088 y*inst->font_height+inst->cfg.window_border,
2089 len * widefactor * inst->font_width - i, inst->font_height);
2092 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2094 /* Now stretch vertically, in the same way. */
2095 if ((lattr & LATTR_MODE) == LATTR_BOT)
2099 for (i = 0; i < inst->font_height; i+=2) {
2100 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2101 x*inst->font_width+inst->cfg.window_border,
2102 y*inst->font_height+inst->cfg.window_border+dt*i+db,
2103 x*inst->font_width+inst->cfg.window_border,
2104 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
2105 len * widefactor * inst->font_width, inst->font_height-i-1);
2111 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2112 unsigned long attr, int lattr)
2114 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2115 struct gui_data *inst = dctx->inst;
2116 GdkGC *gc = dctx->gc;
2119 do_text_internal(ctx, x, y, text, len, attr, lattr);
2121 if (attr & ATTR_WIDE) {
2127 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2129 if (x >= inst->term->cols)
2131 if (x + len*2*widefactor > inst->term->cols)
2132 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2136 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2137 x*inst->font_width+inst->cfg.window_border,
2138 y*inst->font_height+inst->cfg.window_border,
2139 x*inst->font_width+inst->cfg.window_border,
2140 y*inst->font_height+inst->cfg.window_border,
2141 len*widefactor*inst->font_width, inst->font_height);
2144 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2145 unsigned long attr, int lattr)
2147 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2148 struct gui_data *inst = dctx->inst;
2149 GdkGC *gc = dctx->gc;
2151 int active, passive, widefactor;
2153 if (attr & TATTR_PASCURS) {
2154 attr &= ~TATTR_PASCURS;
2158 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
2159 attr &= ~TATTR_ACTCURS;
2163 do_text_internal(ctx, x, y, text, len, attr, lattr);
2165 if (attr & TATTR_COMBINING)
2168 if (attr & ATTR_WIDE) {
2174 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2176 if (x >= inst->term->cols)
2178 if (x + len*2*widefactor > inst->term->cols)
2179 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2183 if (inst->cfg.cursor_type == 0) {
2185 * An active block cursor will already have been done by
2186 * the above do_text call, so we only need to do anything
2190 gdk_gc_set_foreground(gc, &inst->cols[261]);
2191 gdk_draw_rectangle(inst->pixmap, gc, 0,
2192 x*inst->font_width+inst->cfg.window_border,
2193 y*inst->font_height+inst->cfg.window_border,
2194 len*widefactor*inst->font_width-1, inst->font_height-1);
2198 int startx, starty, dx, dy, length, i;
2202 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2203 char_width = 2*inst->font_width;
2205 char_width = inst->font_width;
2207 if (inst->cfg.cursor_type == 1) {
2208 uheight = inst->fonts[0]->ascent + 1;
2209 if (uheight >= inst->font_height)
2210 uheight = inst->font_height - 1;
2212 startx = x * inst->font_width + inst->cfg.window_border;
2213 starty = y * inst->font_height + inst->cfg.window_border + uheight;
2216 length = len * widefactor * char_width;
2219 if (attr & TATTR_RIGHTCURS)
2220 xadjust = char_width - 1;
2221 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2222 starty = y * inst->font_height + inst->cfg.window_border;
2225 length = inst->font_height;
2228 gdk_gc_set_foreground(gc, &inst->cols[261]);
2230 for (i = 0; i < length; i++) {
2232 gdk_draw_point(inst->pixmap, gc, startx, starty);
2237 } else if (active) {
2238 gdk_draw_line(inst->pixmap, gc, startx, starty,
2239 startx + (length-1) * dx, starty + (length-1) * dy);
2240 } /* else no cursor (e.g., blinked off) */
2243 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2244 x*inst->font_width+inst->cfg.window_border,
2245 y*inst->font_height+inst->cfg.window_border,
2246 x*inst->font_width+inst->cfg.window_border,
2247 y*inst->font_height+inst->cfg.window_border,
2248 len*widefactor*inst->font_width, inst->font_height);
2251 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2254 * Truly hideous hack: GTK doesn't allow us to set the mouse
2255 * cursor foreground and background colours unless we've _also_
2256 * created our own cursor from bitmaps. Therefore, I need to
2257 * load the `cursor' font and draw glyphs from it on to
2258 * pixmaps, in order to construct my cursors with the fg and bg
2259 * I want. This is a gross hack, but it's more self-contained
2260 * than linking in Xlib to find the X window handle to
2261 * inst->area and calling XRecolorCursor, and it's more
2262 * futureproof than hard-coding the shapes as bitmap arrays.
2264 static GdkFont *cursor_font = NULL;
2265 GdkPixmap *source, *mask;
2267 GdkColor cfg = { 0, 65535, 65535, 65535 };
2268 GdkColor cbg = { 0, 0, 0, 0 };
2269 GdkColor dfg = { 1, 65535, 65535, 65535 };
2270 GdkColor dbg = { 0, 0, 0, 0 };
2273 gint lb, rb, wid, asc, desc, w, h, x, y;
2275 if (cursor_val == -2) {
2276 gdk_font_unref(cursor_font);
2280 if (cursor_val >= 0 && !cursor_font)
2281 cursor_font = gdk_font_load("cursor");
2284 * Get the text extent of the cursor in question. We use the
2285 * mask character for this, because it's typically slightly
2286 * bigger than the main character.
2288 if (cursor_val >= 0) {
2290 text[0] = (char)cursor_val + 1;
2291 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2292 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2298 source = gdk_pixmap_new(NULL, w, h, 1);
2299 mask = gdk_pixmap_new(NULL, w, h, 1);
2302 * Draw the mask character on the mask pixmap.
2304 gc = gdk_gc_new(mask);
2305 gdk_gc_set_foreground(gc, &dbg);
2306 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2307 if (cursor_val >= 0) {
2309 text[0] = (char)cursor_val + 1;
2310 gdk_gc_set_foreground(gc, &dfg);
2311 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2316 * Draw the main character on the source pixmap.
2318 gc = gdk_gc_new(source);
2319 gdk_gc_set_foreground(gc, &dbg);
2320 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2321 if (cursor_val >= 0) {
2323 text[0] = (char)cursor_val;
2324 gdk_gc_set_foreground(gc, &dfg);
2325 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2330 * Create the cursor.
2332 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2337 gdk_pixmap_unref(source);
2338 gdk_pixmap_unref(mask);
2343 void modalfatalbox(char *p, ...)
2346 fprintf(stderr, "FATAL ERROR: ");
2348 vfprintf(stderr, p, ap);
2350 fputc('\n', stderr);
2354 void cmdline_error(char *p, ...)
2357 fprintf(stderr, "%s: ", appname);
2359 vfprintf(stderr, p, ap);
2361 fputc('\n', stderr);
2365 char *get_x_display(void *frontend)
2367 return gdk_get_display();
2370 long get_windowid(void *frontend)
2372 struct gui_data *inst = (struct gui_data *)frontend;
2373 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2376 static void help(FILE *fp) {
2378 "pterm option summary:\n"
2380 " --display DISPLAY Specify X display to use (note '--')\n"
2381 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2382 " -fn FONT Normal text font\n"
2383 " -fb FONT Bold text font\n"
2384 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2385 " -sl LINES Number of lines of scrollback\n"
2386 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2387 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2388 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2389 " -T TITLE Window title\n"
2390 " -ut, +ut Do(default) or do not update utmp\n"
2391 " -ls, +ls Do(default) or do not make shell a login shell\n"
2392 " -sb, +sb Do(default) or do not display a scrollbar\n"
2393 " -log PATH Log all output to a file\n"
2394 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2395 " -xrm RESOURCE-STRING Set an X resource\n"
2396 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2397 ) < 0 || fflush(fp) < 0) {
2398 perror("output error");
2403 int do_cmdline(int argc, char **argv, int do_everything,
2404 struct gui_data *inst, Config *cfg)
2410 * Macros to make argument handling easier. Note that because
2411 * they need to call `continue', they cannot be contained in
2412 * the usual do {...} while (0) wrapper to make them
2413 * syntactically single statements; hence it is not legal to
2414 * use one of these macros as an unbraced statement between
2417 #define EXPECTS_ARG { \
2418 if (--argc <= 0) { \
2420 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2425 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2427 while (--argc > 0) {
2432 * Shameless cheating. Debian requires all X terminal
2433 * emulators to support `-T title'; but
2434 * cmdline_process_param will eat -T (it means no-pty) and
2435 * complain that pterm doesn't support it. So, in pterm
2436 * only, we convert -T into -title.
2438 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2442 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2443 do_everything ? 1 : -1, cfg);
2446 cmdline_error("option \"%s\" requires an argument", p);
2447 } else if (ret == 2) {
2448 --argc, ++argv; /* skip next argument */
2450 } else if (ret == 1) {
2454 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2457 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2458 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2460 } else if (!strcmp(p, "-fb")) {
2463 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2464 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2466 } else if (!strcmp(p, "-fw")) {
2469 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2470 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2472 } else if (!strcmp(p, "-fwb")) {
2475 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2476 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2478 } else if (!strcmp(p, "-cs")) {
2481 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2482 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2484 } else if (!strcmp(p, "-geometry")) {
2485 int flags, x, y, w, h;
2489 flags = XParseGeometry(val, &x, &y, &w, &h);
2490 if (flags & WidthValue)
2492 if (flags & HeightValue)
2495 if (flags & (XValue | YValue)) {
2498 inst->gotpos = TRUE;
2499 inst->gravity = ((flags & XNegative ? 1 : 0) |
2500 (flags & YNegative ? 2 : 0));
2503 } else if (!strcmp(p, "-sl")) {
2506 cfg->savelines = atoi(val);
2508 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2509 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2510 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2515 if (!gdk_color_parse(val, &col)) {
2517 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2521 index = (!strcmp(p, "-fg") ? 0 :
2522 !strcmp(p, "-bg") ? 2 :
2523 !strcmp(p, "-bfg") ? 1 :
2524 !strcmp(p, "-bbg") ? 3 :
2525 !strcmp(p, "-cfg") ? 4 :
2526 !strcmp(p, "-cbg") ? 5 : -1);
2527 assert(index != -1);
2528 cfg->colours[index][0] = col.red / 256;
2529 cfg->colours[index][1] = col.green / 256;
2530 cfg->colours[index][2] = col.blue / 256;
2533 } else if (use_pty_argv && !strcmp(p, "-e")) {
2534 /* This option swallows all further arguments. */
2540 pty_argv = snewn(argc+1, char *);
2542 for (i = 0; i < argc; i++)
2543 pty_argv[i] = argv[i];
2544 pty_argv[argc] = NULL;
2545 break; /* finished command-line processing */
2547 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2550 } else if (!strcmp(p, "-title")) {
2553 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2554 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2556 } else if (!strcmp(p, "-log")) {
2559 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2560 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2561 cfg->logtype = LGTYP_DEBUG;
2563 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2565 cfg->stamp_utmp = 0;
2567 } else if (!strcmp(p, "-ut")) {
2569 cfg->stamp_utmp = 1;
2571 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2573 cfg->login_shell = 0;
2575 } else if (!strcmp(p, "-ls")) {
2577 cfg->login_shell = 1;
2579 } else if (!strcmp(p, "-nethack")) {
2581 cfg->nethack_keypad = 1;
2583 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2587 } else if (!strcmp(p, "-sb")) {
2591 } else if (!strcmp(p, "-name")) {
2595 } else if (!strcmp(p, "-xrm")) {
2597 provide_xrm_string(val);
2599 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2603 } else if (!strcmp(p, "-pgpfp")) {
2607 } else if(p[0] != '-' && (!do_everything ||
2608 process_nonoption_arg(p, cfg))) {
2613 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2621 * This function retrieves the character set encoding of a font. It
2622 * returns the character set without the X11 hack (in case the user
2623 * asks to use the font's own encoding).
2625 static int set_font_info(struct gui_data *inst, int fontid)
2627 GdkFont *font = inst->fonts[fontid];
2628 XFontStruct *xfs = GDK_FONT_XFONT(font);
2629 Display *disp = GDK_FONT_XDISPLAY(font);
2630 Atom charset_registry, charset_encoding;
2631 unsigned long registry_ret, encoding_ret;
2632 int retval = CS_NONE;
2634 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2635 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2636 inst->fontinfo[fontid].charset = CS_NONE;
2637 inst->fontinfo[fontid].is_wide = 0;
2638 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2639 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2641 reg = XGetAtomName(disp, (Atom)registry_ret);
2642 enc = XGetAtomName(disp, (Atom)encoding_ret);
2644 char *encoding = dupcat(reg, "-", enc, NULL);
2645 retval = inst->fontinfo[fontid].charset =
2646 charset_from_xenc(encoding);
2647 /* FIXME: when libcharset supports wide encodings fix this. */
2648 if (!strcasecmp(encoding, "iso10646-1")) {
2649 inst->fontinfo[fontid].is_wide = 1;
2654 * Hack for X line-drawing characters: if the primary
2655 * font is encoded as ISO-8859-anything, and has valid
2656 * glyphs in the first 32 char positions, it is assumed
2657 * that those glyphs are the VT100 line-drawing
2660 * Actually, we'll hack even harder by only checking
2661 * position 0x19 (vertical line, VT100 linedrawing
2662 * `x'). Then we can check it easily by seeing if the
2663 * ascent and descent differ.
2665 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2666 int lb, rb, wid, asc, desc;
2671 gdk_string_extents(inst->fonts[fontid], text,
2672 &lb, &rb, &wid, &asc, &desc);
2674 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2684 int uxsel_input_add(int fd, int rwx) {
2686 if (rwx & 1) flags |= GDK_INPUT_READ;
2687 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2688 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2689 return gdk_input_add(fd, flags, fd_input_func, NULL);
2692 void uxsel_input_remove(int id) {
2693 gdk_input_remove(id);
2696 char *guess_derived_font_name(GdkFont *font, int bold, int wide)
2698 XFontStruct *xfs = GDK_FONT_XFONT(font);
2699 Display *disp = GDK_FONT_XDISPLAY(font);
2700 Atom fontprop = XInternAtom(disp, "FONT", False);
2702 if (XGetFontProperty(xfs, fontprop, &ret)) {
2703 char *name = XGetAtomName(disp, (Atom)ret);
2704 if (name && name[0] == '-') {
2706 char *dupname, *extrafree = NULL, *ret;
2710 p = q = dupname = dupstr(name); /* skip initial minus */
2713 while (*p && nstr < lenof(strings)) {
2716 strings[nstr++] = p+1;
2721 if (nstr < lenof(strings))
2722 return NULL; /* XLFD was malformed */
2725 strings[2] = "bold";
2728 /* 4 is `wideness', which obviously may have changed. */
2729 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2730 strings[4] = strings[5] = "*";
2731 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
2734 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
2735 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
2736 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
2737 "-", strings[ 9], "-", strings[10], "-", strings[11],
2738 "-", strings[12], NULL);
2748 void setup_fonts_ucs(struct gui_data *inst)
2755 gdk_font_unref(inst->fonts[0]);
2757 gdk_font_unref(inst->fonts[1]);
2759 gdk_font_unref(inst->fonts[2]);
2761 gdk_font_unref(inst->fonts[3]);
2763 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2764 if (!inst->fonts[0]) {
2765 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2766 inst->cfg.font.name);
2769 font_charset = set_font_info(inst, 0);
2771 if (inst->cfg.shadowbold) {
2772 inst->fonts[1] = NULL;
2774 if (inst->cfg.boldfont.name[0]) {
2775 name = inst->cfg.boldfont.name;
2778 name = guess_derived_font_name(inst->fonts[0], TRUE, FALSE);
2781 inst->fonts[1] = name ? gdk_font_load(name) : NULL;
2782 if (inst->fonts[1]) {
2783 set_font_info(inst, 1);
2784 } else if (!guessed) {
2785 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2786 inst->cfg.boldfont.name);
2793 if (inst->cfg.widefont.name[0]) {
2794 name = inst->cfg.widefont.name;
2797 name = guess_derived_font_name(inst->fonts[0], FALSE, TRUE);
2800 inst->fonts[2] = name ? gdk_font_load(name) : NULL;
2801 if (inst->fonts[2]) {
2802 set_font_info(inst, 2);
2803 } else if (!guessed) {
2804 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2805 inst->cfg.widefont.name);
2811 if (inst->cfg.shadowbold) {
2812 inst->fonts[3] = NULL;
2814 if (inst->cfg.wideboldfont.name[0]) {
2815 name = inst->cfg.wideboldfont.name;
2819 * Here we have some choices. We can widen the bold font,
2820 * bolden the wide font, or widen and bolden the standard
2821 * font. Try them all, in that order!
2823 if (inst->cfg.widefont.name[0])
2824 name = guess_derived_font_name(inst->fonts[2], TRUE, FALSE);
2825 else if (inst->cfg.boldfont.name[0])
2826 name = guess_derived_font_name(inst->fonts[1], FALSE, TRUE);
2828 name = guess_derived_font_name(inst->fonts[0], TRUE, TRUE);
2831 inst->fonts[3] = name ? gdk_font_load(name) : NULL;
2832 if (inst->fonts[3]) {
2833 set_font_info(inst, 3);
2834 } else if (!guessed) {
2835 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n", appname,
2836 inst->cfg.wideboldfont.name);
2843 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2844 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2846 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2847 inst->cfg.utf8_override, font_charset,
2851 void set_geom_hints(struct gui_data *inst)
2854 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2855 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2856 geom.max_width = geom.max_height = -1;
2857 geom.base_width = 2*inst->cfg.window_border;
2858 geom.base_height = 2*inst->cfg.window_border;
2859 geom.width_inc = inst->font_width;
2860 geom.height_inc = inst->font_height;
2861 geom.min_aspect = geom.max_aspect = 0;
2862 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2863 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2864 GDK_HINT_RESIZE_INC);
2867 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2869 struct gui_data *inst = (struct gui_data *)data;
2870 term_clrsb(inst->term);
2873 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2875 struct gui_data *inst = (struct gui_data *)data;
2876 term_pwron(inst->term, TRUE);
2878 ldisc_send(inst->ldisc, NULL, 0, 0);
2881 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2883 struct gui_data *inst = (struct gui_data *)data;
2884 term_copyall(inst->term);
2887 void special_menuitem(GtkMenuItem *item, gpointer data)
2889 struct gui_data *inst = (struct gui_data *)data;
2890 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2894 inst->back->special(inst->backhandle, code);
2897 void about_menuitem(GtkMenuItem *item, gpointer data)
2899 struct gui_data *inst = (struct gui_data *)data;
2900 about_box(inst->window);
2903 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2905 struct gui_data *inst = (struct gui_data *)data;
2906 showeventlog(inst->eventlogstuff, inst->window);
2909 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2911 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2912 static const int ww[] = {
2913 256, 257, 258, 259, 260, 261,
2914 0, 8, 1, 9, 2, 10, 3, 11,
2915 4, 12, 5, 13, 6, 14, 7, 15
2917 struct gui_data *inst = (struct gui_data *)data;
2918 char *title = dupcat(appname, " Reconfiguration", NULL);
2919 Config cfg2, oldcfg;
2922 assert(lenof(ww) == NCFGCOLOURS);
2924 if (inst->reconfiguring)
2927 inst->reconfiguring = TRUE;
2929 cfg2 = inst->cfg; /* structure copy */
2931 if (do_config_box(title, &cfg2, 1,
2932 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2934 oldcfg = inst->cfg; /* structure copy */
2935 inst->cfg = cfg2; /* structure copy */
2937 /* Pass new config data to the logging module */
2938 log_reconfig(inst->logctx, &cfg2);
2940 * Flush the line discipline's edit buffer in the case
2941 * where local editing has just been disabled.
2944 ldisc_send(inst->ldisc, NULL, 0, 0);
2945 /* Pass new config data to the terminal */
2946 term_reconfig(inst->term, &cfg2);
2947 /* Pass new config data to the back end */
2949 inst->back->reconfig(inst->backhandle, &cfg2);
2952 * Just setting inst->cfg is sufficient to cause colour
2953 * setting changes to appear on the next ESC]R palette
2954 * reset. But we should also check whether any colour
2955 * settings have been changed, and revert the ones that
2956 * have to the new default, on the assumption that the user
2957 * is most likely to want an immediate update.
2959 for (i = 0; i < NCFGCOLOURS; i++) {
2960 if (oldcfg.colours[i][0] != cfg2.colours[i][0] ||
2961 oldcfg.colours[i][1] != cfg2.colours[i][1] ||
2962 oldcfg.colours[i][2] != cfg2.colours[i][2]) {
2963 real_palette_set(inst, ww[i], cfg2.colours[i][0],
2965 cfg2.colours[i][2]);
2968 * If the default background has changed, we must
2969 * repaint the space in between the window border
2970 * and the text area.
2973 set_window_background(inst);
2974 draw_backing_rect(inst);
2980 * If the scrollbar needs to be shown, hidden, or moved
2981 * from one end to the other of the window, do so now.
2983 if (oldcfg.scrollbar != cfg2.scrollbar) {
2985 gtk_widget_show(inst->sbar);
2987 gtk_widget_hide(inst->sbar);
2989 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2990 gtk_box_reorder_child(inst->hbox, inst->sbar,
2991 cfg2.scrollbar_on_left ? 0 : 1);
2995 * Change the window title, if required.
2997 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2998 set_title(inst, cfg2.wintitle);
2999 set_window_titles(inst);
3002 * Redo the whole tangled fonts and Unicode mess if
3005 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
3006 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
3007 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
3008 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
3009 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
3010 oldcfg.vtmode != cfg2.vtmode ||
3011 oldcfg.shadowbold != cfg2.shadowbold) {
3012 setup_fonts_ucs(inst);
3018 * Resize the window.
3020 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
3021 oldcfg.window_border != cfg2.window_border || need_size) {
3022 set_geom_hints(inst);
3023 request_resize(inst, cfg2.width, cfg2.height);
3026 * The above will have caused a call to term_size() for
3027 * us if it happened. If the user has fiddled with only
3028 * the scrollback size, the above will not have
3029 * happened and we will need an explicit term_size()
3032 if (oldcfg.savelines != cfg2.savelines)
3033 term_size(inst->term, inst->term->rows, inst->term->cols,
3037 term_invalidate(inst->term);
3040 * We do an explicit full redraw here to ensure the window
3041 * border has been redrawn as well as the text area.
3043 gtk_widget_queue_draw(inst->area);
3046 inst->reconfiguring = FALSE;
3049 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3052 * Re-execing ourself is not an exact science under Unix. I do
3053 * the best I can by using /proc/self/exe if available and by
3054 * assuming argv[0] can be found on $PATH if not.
3056 * Note that we also have to reconstruct the elements of the
3057 * original argv which gtk swallowed, since the user wants the
3058 * new session to appear on the same X display as the old one.
3066 * Collect the arguments with which to re-exec ourself.
3068 va_start(ap, fd_to_close);
3069 n = 2; /* progname and terminating NULL */
3070 n += inst->ngtkargs;
3071 while (va_arg(ap, char *) != NULL)
3075 args = snewn(n, char *);
3076 args[0] = inst->progname;
3078 for (i = 0; i < inst->ngtkargs; i++)
3079 args[i+1] = inst->gtkargvstart[i];
3082 va_start(ap, fd_to_close);
3083 while ((args[i++] = va_arg(ap, char *)) != NULL);
3089 * Do the double fork.
3102 } else if (pid2 > 0) {
3104 * First child has successfully forked second child. My
3105 * Work Here Is Done. Note the use of _exit rather than
3106 * exit: the latter appears to cause destroy messages
3107 * to be sent to the X server. I suspect gtk uses
3114 * If we reach here, we are the second child, so we now
3115 * actually perform the exec.
3117 if (fd_to_close >= 0)
3120 execv("/proc/self/exe", args);
3121 execvp(inst->progname, args);
3127 waitpid(pid, &status, 0);
3132 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3134 struct gui_data *inst = (struct gui_data *)gdata;
3136 * For this feature we must marshal cfg and (possibly) pty_argv
3137 * into a byte stream, create a pipe, and send this byte stream
3138 * to the child through the pipe.
3145 if (pipe(pipefd) < 0) {
3150 size = sizeof(inst->cfg);
3151 if (use_pty_argv && pty_argv) {
3152 for (i = 0; pty_argv[i]; i++)
3153 size += strlen(pty_argv[i]) + 1;
3156 data = snewn(size, char);
3157 memcpy(data, &inst->cfg, sizeof(inst->cfg));
3158 if (use_pty_argv && pty_argv) {
3159 int p = sizeof(inst->cfg);
3160 for (i = 0; pty_argv[i]; i++) {
3161 strcpy(data + p, pty_argv[i]);
3162 p += strlen(pty_argv[i]) + 1;
3167 sprintf(option, "---[%d,%d]", pipefd[0], size);
3168 fcntl(pipefd[0], F_SETFD, 0);
3169 fork_and_exec_self(inst, pipefd[1], option, NULL);
3173 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3176 perror("write to pipe");
3181 int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3183 int fd, i, ret, size;
3186 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3187 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3191 data = snewn(size, char);
3193 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3196 perror("read from pipe");
3198 } else if (i < size) {
3199 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3204 memcpy(cfg, data, sizeof(Config));
3205 if (use_pty_argv && size > sizeof(Config)) {
3209 while (i < size && data[i]) i++;
3211 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3218 pty_argv = snewn(n+1, char *);
3224 while (i < size && data[i]) i++;
3227 pty_argv[n++] = dupstr(p);
3234 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3236 struct gui_data *inst = (struct gui_data *)data;
3238 fork_and_exec_self(inst, -1, NULL);
3241 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3243 struct gui_data *inst = (struct gui_data *)data;
3246 logevent(inst, "----- Session restarted -----");
3247 term_pwron(inst->term, FALSE);
3248 start_backend(inst);
3249 inst->exited = FALSE;
3253 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3255 struct gui_data *inst = (struct gui_data *)data;
3256 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3258 fork_and_exec_self(inst, -1, "-load", str, NULL);
3261 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3263 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3268 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3270 struct gui_data *inst = (struct gui_data *)data;
3271 struct sesslist sesslist;
3274 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3275 (GtkCallback)gtk_widget_destroy, NULL);
3277 get_sesslist(&sesslist, TRUE);
3278 for (i = 1; i < sesslist.nsessions; i++) {
3279 GtkWidget *menuitem =
3280 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3281 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3282 gtk_widget_show(menuitem);
3283 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3284 dupstr(sesslist.sessions[i]));
3285 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3286 GTK_SIGNAL_FUNC(saved_session_menuitem),
3288 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3289 GTK_SIGNAL_FUNC(saved_session_freedata),
3292 get_sesslist(&sesslist, FALSE); /* free up */
3295 void update_specials_menu(void *frontend)
3297 struct gui_data *inst = (struct gui_data *)frontend;
3299 const struct telnet_special *specials;
3302 specials = inst->back->get_specials(inst->backhandle);
3306 /* I believe this disposes of submenus too. */
3307 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3308 (GtkCallback)gtk_widget_destroy, NULL);
3311 GtkWidget *menu = inst->specialsmenu;
3312 /* A lame "stack" for submenus that will do for now. */
3313 GtkWidget *saved_menu = NULL;
3315 for (i = 0; nesting > 0; i++) {
3316 GtkWidget *menuitem = NULL;
3317 switch (specials[i].code) {
3319 assert (nesting < 2);
3320 saved_menu = menu; /* XXX lame stacking */
3321 menu = gtk_menu_new();
3322 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3323 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3324 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3325 gtk_widget_show(menuitem);
3332 menu = saved_menu; /* XXX lame stacking */
3337 menuitem = gtk_menu_item_new();
3340 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3341 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3342 GINT_TO_POINTER(specials[i].code));
3343 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3344 GTK_SIGNAL_FUNC(special_menuitem), inst);
3348 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3349 gtk_widget_show(menuitem);
3352 gtk_widget_show(inst->specialsitem1);
3353 gtk_widget_show(inst->specialsitem2);
3355 gtk_widget_hide(inst->specialsitem1);
3356 gtk_widget_hide(inst->specialsitem2);
3360 static void start_backend(struct gui_data *inst)
3362 extern Backend *select_backend(Config *cfg);
3366 inst->back = select_backend(&inst->cfg);
3368 error = inst->back->init((void *)inst, &inst->backhandle,
3369 &inst->cfg, inst->cfg.host, inst->cfg.port,
3370 &realhost, inst->cfg.tcp_nodelay,
3371 inst->cfg.tcp_keepalives);
3374 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3375 inst->cfg.host, error);
3376 inst->exited = TRUE;
3377 fatal_message_box(inst->window, msg);
3382 if (inst->cfg.wintitle[0]) {
3383 set_title(inst, inst->cfg.wintitle);
3384 set_icon(inst, inst->cfg.wintitle);
3386 char *title = make_default_wintitle(realhost);
3387 set_title(inst, title);
3388 set_icon(inst, title);
3393 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3395 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3398 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3401 gtk_widget_hide(inst->restartitem);
3404 int pt_main(int argc, char **argv)
3406 extern int cfgbox(Config *cfg);
3407 struct gui_data *inst;
3410 * Create an instance structure and initialise to zeroes
3412 inst = snew(struct gui_data);
3413 memset(inst, 0, sizeof(*inst));
3414 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3415 inst->busy_status = BUSY_NOT;
3417 /* defer any child exit handling until we're ready to deal with
3419 block_signal(SIGCHLD, 1);
3421 inst->progname = argv[0];
3423 * Copy the original argv before letting gtk_init fiddle with
3424 * it. It will be required later.
3428 inst->gtkargvstart = snewn(argc-1, char *);
3429 for (i = 1; i < argc; i++)
3430 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3432 gtk_init(&argc, &argv);
3433 inst->ngtkargs = oldargc - argc;
3436 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3437 read_dupsession_data(inst, &inst->cfg, argv[1]);
3438 /* Splatter this argument so it doesn't clutter a ps listing */
3439 memset(argv[1], 0, strlen(argv[1]));
3441 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
3442 exit(1); /* pre-defaults pass to get -class */
3443 do_defaults(NULL, &inst->cfg);
3444 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
3445 exit(1); /* post-defaults, do everything */
3447 cmdline_run_saved(&inst->cfg);
3449 if (!cfg_launchable(&inst->cfg) && !cfgbox(&inst->cfg))
3450 exit(0); /* config box hit Cancel */
3453 if (!compound_text_atom)
3454 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3455 if (!utf8_string_atom)
3456 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3458 setup_fonts_ucs(inst);
3461 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3464 * Set up the colour map.
3466 palette_reset(inst);
3468 inst->width = inst->cfg.width;
3469 inst->height = inst->cfg.height;
3471 inst->area = gtk_drawing_area_new();
3472 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3473 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3474 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
3475 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3476 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3477 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3479 * We always create the scrollbar; it remains invisible if
3480 * unwanted, so we can pop it up quickly if it suddenly becomes
3483 if (inst->cfg.scrollbar_on_left)
3484 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3485 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3486 if (!inst->cfg.scrollbar_on_left)
3487 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3489 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3491 set_geom_hints(inst);
3493 gtk_widget_show(inst->area);
3494 if (inst->cfg.scrollbar)
3495 gtk_widget_show(inst->sbar);
3497 gtk_widget_hide(inst->sbar);
3498 gtk_widget_show(GTK_WIDGET(inst->hbox));
3501 int x = inst->xpos, y = inst->ypos;
3503 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3504 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3505 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3506 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3507 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3510 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3511 GTK_SIGNAL_FUNC(destroy), inst);
3512 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3513 GTK_SIGNAL_FUNC(delete_window), inst);
3514 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3515 GTK_SIGNAL_FUNC(key_event), inst);
3516 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3517 GTK_SIGNAL_FUNC(key_event), inst);
3518 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3519 GTK_SIGNAL_FUNC(focus_event), inst);
3520 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3521 GTK_SIGNAL_FUNC(focus_event), inst);
3522 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3523 GTK_SIGNAL_FUNC(configure_area), inst);
3524 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3525 GTK_SIGNAL_FUNC(expose_area), inst);
3526 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3527 GTK_SIGNAL_FUNC(button_event), inst);
3528 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3529 GTK_SIGNAL_FUNC(button_event), inst);
3530 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3531 GTK_SIGNAL_FUNC(motion_event), inst);
3532 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3533 GTK_SIGNAL_FUNC(selection_received), inst);
3534 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3535 GTK_SIGNAL_FUNC(selection_get), inst);
3536 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3537 GTK_SIGNAL_FUNC(selection_clear), inst);
3538 if (inst->cfg.scrollbar)
3539 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3540 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3541 gtk_widget_add_events(GTK_WIDGET(inst->area),
3542 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3543 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3544 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3546 gtk_widget_show(inst->window);
3548 set_window_background(inst);
3551 * Set up the Ctrl+rightclick context menu.
3554 GtkWidget *menuitem;
3556 extern const int use_event_log, new_session, saved_sessions;
3558 inst->menu = gtk_menu_new();
3560 #define MKMENUITEM(title, func) do { \
3561 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3562 gtk_menu_item_new(); \
3563 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3564 gtk_widget_show(menuitem); \
3566 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3567 GTK_SIGNAL_FUNC(func), inst); \
3570 MKMENUITEM("New Session", new_session_menuitem);
3571 MKMENUITEM("Restart Session", restart_session_menuitem);
3572 inst->restartitem = menuitem;
3573 gtk_widget_hide(inst->restartitem);
3574 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3575 if (saved_sessions) {
3576 inst->sessionsmenu = gtk_menu_new();
3577 /* sessionsmenu will be updated when it's invoked */
3578 /* XXX is this the right way to do dynamic menus in Gtk? */
3579 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3580 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3581 inst->sessionsmenu);
3583 MKMENUITEM(NULL, NULL);
3584 MKMENUITEM("Change Settings", change_settings_menuitem);
3585 MKMENUITEM(NULL, NULL);
3587 MKMENUITEM("Event Log", event_log_menuitem);
3588 MKMENUITEM("Special Commands", NULL);
3589 inst->specialsmenu = gtk_menu_new();
3590 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3591 inst->specialsitem1 = menuitem;
3592 MKMENUITEM(NULL, NULL);
3593 inst->specialsitem2 = menuitem;
3594 gtk_widget_hide(inst->specialsitem1);
3595 gtk_widget_hide(inst->specialsitem2);
3596 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3597 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3598 MKMENUITEM("Copy All", copy_all_menuitem);
3599 MKMENUITEM(NULL, NULL);
3600 s = dupcat("About ", appname, NULL);
3601 MKMENUITEM(s, about_menuitem);
3606 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3607 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3608 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3609 inst->blankcursor = make_mouse_ptr(inst, -1);
3610 make_mouse_ptr(inst, -2); /* clean up cursor font */
3611 inst->currcursor = inst->textcursor;
3612 show_mouseptr(inst, 1);
3614 inst->eventlogstuff = eventlogstuff_new();
3616 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3617 inst->logctx = log_init(inst, &inst->cfg);
3618 term_provide_logctx(inst->term, inst->logctx);
3622 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3624 start_backend(inst);
3626 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3628 /* now we're reday to deal with the child exit handler being
3630 block_signal(SIGCHLD, 0);
3633 * Block SIGPIPE: if we attempt Duplicate Session or similar
3634 * and it falls over in some way, we certainly don't want
3635 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3636 * this _after_ (at least pterm) forks off its child process,
3637 * since the child wants SIGPIPE handled in the usual way.
3639 block_signal(SIGPIPE, 1);
3641 inst->exited = FALSE;