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 sends a Break special to the backend */
667 if (event->keyval == GDK_Break &&
668 (event->state & GDK_CONTROL_MASK)) {
670 inst->back->special(inst->backhandle, TS_BRK);
674 /* We handle Return ourselves, because it needs to be flagged as
675 * special to ldisc. */
676 if (event->keyval == GDK_Return) {
678 use_ucsoutput = FALSE;
683 /* Control-2, Control-Space and Control-@ are NUL */
684 if (!event->string[0] &&
685 (event->keyval == ' ' || event->keyval == '2' ||
686 event->keyval == '@') &&
687 (event->state & (GDK_SHIFT_MASK |
688 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
690 use_ucsoutput = FALSE;
694 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
695 if (!event->string[0] && event->keyval == ' ' &&
696 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
697 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
699 use_ucsoutput = FALSE;
703 /* We don't let GTK tell us what Backspace is! We know better. */
704 if (event->keyval == GDK_BackSpace &&
705 !(event->state & GDK_SHIFT_MASK)) {
706 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
707 use_ucsoutput = FALSE;
711 /* For Shift Backspace, do opposite of what is configured. */
712 if (event->keyval == GDK_BackSpace &&
713 (event->state & GDK_SHIFT_MASK)) {
714 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
715 use_ucsoutput = FALSE;
720 /* Shift-Tab is ESC [ Z */
721 if (event->keyval == GDK_ISO_Left_Tab ||
722 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
723 end = 1 + sprintf(output+1, "\033[Z");
724 use_ucsoutput = FALSE;
726 /* And normal Tab is Tab, if the keymap hasn't already told us.
727 * (Curiously, at least one version of the MacOS 10.5 X server
728 * doesn't translate Tab for us. */
729 if (event->keyval == GDK_Tab && end <= 1) {
735 * NetHack keypad mode.
737 if (inst->cfg.nethack_keypad) {
739 switch (event->keyval) {
740 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
741 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
742 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
743 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
744 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
745 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
746 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
747 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
748 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
752 if (event->state & GDK_CONTROL_MASK)
754 else if (event->state & GDK_SHIFT_MASK)
758 use_ucsoutput = FALSE;
764 * Application keypad mode.
766 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
768 switch (event->keyval) {
769 case GDK_Num_Lock: xkey = 'P'; break;
770 case GDK_KP_Divide: xkey = 'Q'; break;
771 case GDK_KP_Multiply: xkey = 'R'; break;
772 case GDK_KP_Subtract: xkey = 'S'; break;
774 * Keypad + is tricky. It covers a space that would
775 * be taken up on the VT100 by _two_ keys; so we
776 * let Shift select between the two. Worse still,
777 * in xterm function key mode we change which two...
780 if (inst->cfg.funky_type == FUNKY_XTERM) {
781 if (event->state & GDK_SHIFT_MASK)
785 } else if (event->state & GDK_SHIFT_MASK)
790 case GDK_KP_Enter: xkey = 'M'; break;
791 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
792 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
793 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
794 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
795 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
796 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
797 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
798 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
799 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
800 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
801 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
804 if (inst->term->vt52_mode) {
805 if (xkey >= 'P' && xkey <= 'S')
806 end = 1 + sprintf(output+1, "\033%c", xkey);
808 end = 1 + sprintf(output+1, "\033?%c", xkey);
810 end = 1 + sprintf(output+1, "\033O%c", xkey);
811 use_ucsoutput = FALSE;
817 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
818 * for integer decimal nn.)
820 * We also deal with the weird ones here. Linux VCs replace F1
821 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
822 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
827 switch (event->keyval) {
829 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
832 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
835 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
838 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
841 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
844 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
847 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
850 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
853 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
856 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
889 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
890 case GDK_Home: case GDK_KP_Home:
893 case GDK_Insert: case GDK_KP_Insert:
896 case GDK_Delete: case GDK_KP_Delete:
899 case GDK_End: case GDK_KP_End:
902 case GDK_Page_Up: case GDK_KP_Page_Up:
905 case GDK_Page_Down: case GDK_KP_Page_Down:
909 /* Reorder edit keys to physical order */
910 if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
911 code = "\0\2\1\4\5\3\6"[code];
913 if (inst->term->vt52_mode && code > 0 && code <= 6) {
914 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
915 use_ucsoutput = FALSE;
919 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
920 code >= 11 && code <= 34) {
921 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
923 switch (event->keyval) {
924 case GDK_F1: index = 0; break;
925 case GDK_F2: index = 1; break;
926 case GDK_F3: index = 2; break;
927 case GDK_F4: index = 3; break;
928 case GDK_F5: index = 4; break;
929 case GDK_F6: index = 5; break;
930 case GDK_F7: index = 6; break;
931 case GDK_F8: index = 7; break;
932 case GDK_F9: index = 8; break;
933 case GDK_F10: index = 9; break;
934 case GDK_F11: index = 10; break;
935 case GDK_F12: index = 11; break;
937 if (event->state & GDK_SHIFT_MASK) index += 12;
938 if (event->state & GDK_CONTROL_MASK) index += 24;
939 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
940 use_ucsoutput = FALSE;
943 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
944 code >= 1 && code <= 6) {
945 char codes[] = "HL.FIG";
950 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
952 use_ucsoutput = FALSE;
955 if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
956 code >= 11 && code <= 24) {
962 if (inst->term->vt52_mode)
963 end = 1 + sprintf(output+1,
964 "\x1B%c", code + 'P' - 11 - offt);
966 end = 1 + sprintf(output+1,
967 "\x1BO%c", code + 'P' - 11 - offt);
968 use_ucsoutput = FALSE;
971 if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
972 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
973 use_ucsoutput = FALSE;
976 if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
977 if (inst->term->vt52_mode)
978 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
980 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
981 use_ucsoutput = FALSE;
984 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
985 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
986 use_ucsoutput = FALSE;
990 end = 1 + sprintf(output+1, "\x1B[%d~", code);
991 use_ucsoutput = FALSE;
997 * Cursor keys. (This includes the numberpad cursor keys,
998 * if we haven't already done them due to app keypad mode.)
1000 * Here we also process un-numlocked un-appkeypadded KP5,
1001 * which sends ESC [ G.
1005 switch (event->keyval) {
1006 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1007 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1008 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1009 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1010 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1014 * The arrow keys normally do ESC [ A and so on. In
1015 * app cursor keys mode they do ESC O A instead.
1016 * Ctrl toggles the two modes.
1018 if (inst->term->vt52_mode) {
1019 end = 1 + sprintf(output+1, "\033%c", xkey);
1020 } else if (!inst->term->app_cursor_keys ^
1021 !(event->state & GDK_CONTROL_MASK)) {
1022 end = 1 + sprintf(output+1, "\033O%c", xkey);
1024 end = 1 + sprintf(output+1, "\033[%c", xkey);
1026 use_ucsoutput = FALSE;
1035 if (end-start > 0) {
1036 #ifdef KEY_DEBUGGING
1038 printf("generating sequence:");
1039 for (i = start; i < end; i++)
1040 printf(" %02x", (unsigned char) output[i]);
1046 * For special control characters, the character set
1047 * should never matter.
1049 output[end] = '\0'; /* NUL-terminate */
1051 ldisc_send(inst->ldisc, output+start, -2, 1);
1052 } else if (!inst->direct_to_font) {
1053 if (!use_ucsoutput) {
1055 * The stuff we've just generated is assumed to be
1056 * ISO-8859-1! This sounds insane, but `man
1057 * XLookupString' agrees: strings of this type
1058 * returned from the X server are hardcoded to
1059 * 8859-1. Strictly speaking we should be doing
1060 * this using some sort of GtkIMContext, which (if
1061 * we're lucky) would give us our data directly in
1062 * Unicode; but that's not supported in GTK 1.2 as
1063 * far as I can tell, and it's poorly documented
1064 * even in 2.0, so it'll have to wait.
1067 lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
1071 * We generated our own Unicode key data from the
1072 * keysym, so use that instead.
1075 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1079 * In direct-to-font mode, we just send the string
1080 * exactly as we received it.
1083 ldisc_send(inst->ldisc, output+start, end-start, 1);
1086 show_mouseptr(inst, 0);
1087 term_seen_key_event(inst->term);
1093 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1095 struct gui_data *inst = (struct gui_data *)data;
1096 int shift, ctrl, alt, x, y, button, act;
1098 /* Remember the timestamp. */
1099 inst->input_event_time = event->time;
1101 show_mouseptr(inst, 1);
1103 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
1104 term_scroll(inst->term, 0, -5);
1107 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
1108 term_scroll(inst->term, 0, +5);
1112 shift = event->state & GDK_SHIFT_MASK;
1113 ctrl = event->state & GDK_CONTROL_MASK;
1114 alt = event->state & GDK_MOD1_MASK;
1116 if (event->button == 3 && ctrl) {
1117 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1118 event->button, event->time);
1122 if (event->button == 1)
1124 else if (event->button == 2)
1125 button = MBT_MIDDLE;
1126 else if (event->button == 3)
1129 return FALSE; /* don't even know what button! */
1131 switch (event->type) {
1132 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1133 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1134 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1135 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1136 default: return FALSE; /* don't know this event type */
1139 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
1140 act != MA_CLICK && act != MA_RELEASE)
1141 return TRUE; /* we ignore these in raw mouse mode */
1143 x = (event->x - inst->cfg.window_border) / inst->font_width;
1144 y = (event->y - inst->cfg.window_border) / inst->font_height;
1146 term_mouse(inst->term, button, translate_button(button), act,
1147 x, y, shift, ctrl, alt);
1152 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1154 struct gui_data *inst = (struct gui_data *)data;
1155 int shift, ctrl, alt, x, y, button;
1157 /* Remember the timestamp. */
1158 inst->input_event_time = event->time;
1160 show_mouseptr(inst, 1);
1162 shift = event->state & GDK_SHIFT_MASK;
1163 ctrl = event->state & GDK_CONTROL_MASK;
1164 alt = event->state & GDK_MOD1_MASK;
1165 if (event->state & GDK_BUTTON1_MASK)
1167 else if (event->state & GDK_BUTTON2_MASK)
1168 button = MBT_MIDDLE;
1169 else if (event->state & GDK_BUTTON3_MASK)
1172 return FALSE; /* don't even know what button! */
1174 x = (event->x - inst->cfg.window_border) / inst->font_width;
1175 y = (event->y - inst->cfg.window_border) / inst->font_height;
1177 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1178 x, y, shift, ctrl, alt);
1183 void frontend_keypress(void *handle)
1185 struct gui_data *inst = (struct gui_data *)handle;
1188 * If our child process has exited but not closed, terminate on
1195 void notify_remote_exit(void *frontend)
1197 struct gui_data *inst = (struct gui_data *)frontend;
1200 if (!inst->exited &&
1201 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1202 inst->exited = TRUE;
1203 if (inst->cfg.close_on_exit == FORCE_ON ||
1204 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1205 gtk_main_quit(); /* just go */
1207 ldisc_free(inst->ldisc);
1211 inst->back->free(inst->backhandle);
1212 inst->backhandle = NULL;
1214 term_provide_resize_fn(inst->term, NULL, NULL);
1215 update_specials_menu(inst);
1217 gtk_widget_show(inst->restartitem);
1221 static gint timer_trigger(gpointer data)
1223 long now = GPOINTER_TO_INT(data);
1227 if (run_timers(now, &next)) {
1228 ticks = next - GETTICKCOUNT();
1229 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1230 GINT_TO_POINTER(next));
1234 * Never let a timer resume. If we need another one, we've
1235 * asked for it explicitly above.
1240 void timer_change_notify(long next)
1245 gtk_timeout_remove(timer_id);
1247 ticks = next - GETTICKCOUNT();
1249 ticks = 1; /* just in case */
1251 timer_id = gtk_timeout_add(ticks, timer_trigger,
1252 GINT_TO_POINTER(next));
1255 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1258 * We must process exceptional notifications before ordinary
1259 * readability ones, or we may go straight past the urgent
1262 if (condition & GDK_INPUT_EXCEPTION)
1263 select_result(sourcefd, 4);
1264 if (condition & GDK_INPUT_READ)
1265 select_result(sourcefd, 1);
1266 if (condition & GDK_INPUT_WRITE)
1267 select_result(sourcefd, 2);
1270 void destroy(GtkWidget *widget, gpointer data)
1275 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1277 struct gui_data *inst = (struct gui_data *)data;
1278 term_set_focus(inst->term, event->in);
1279 term_update(inst->term);
1280 show_mouseptr(inst, 1);
1284 void set_busy_status(void *frontend, int status)
1286 struct gui_data *inst = (struct gui_data *)frontend;
1287 inst->busy_status = status;
1288 update_mouseptr(inst);
1292 * set or clear the "raw mouse message" mode
1294 void set_raw_mouse_mode(void *frontend, int activate)
1296 struct gui_data *inst = (struct gui_data *)frontend;
1297 activate = activate && !inst->cfg.no_mouse_rep;
1298 send_raw_mouse = activate;
1299 update_mouseptr(inst);
1302 void request_resize(void *frontend, int w, int h)
1304 struct gui_data *inst = (struct gui_data *)frontend;
1305 int large_x, large_y;
1306 int offset_x, offset_y;
1308 GtkRequisition inner, outer;
1311 * This is a heinous hack dreamed up by the gnome-terminal
1312 * people to get around a limitation in gtk. The problem is
1313 * that in order to set the size correctly we really need to be
1314 * calling gtk_window_resize - but that needs to know the size
1315 * of the _whole window_, not the drawing area. So what we do
1316 * is to set an artificially huge size request on the drawing
1317 * area, recompute the resulting size request on the window,
1318 * and look at the difference between the two. That gives us
1319 * the x and y offsets we need to translate drawing area size
1320 * into window size for real, and then we call
1321 * gtk_window_resize.
1325 * We start by retrieving the current size of the whole window.
1326 * Adding a bit to _that_ will give us a value we can use as a
1327 * bogus size request which guarantees to be bigger than the
1328 * current size of the drawing area.
1330 get_window_pixels(inst, &large_x, &large_y);
1334 #if GTK_CHECK_VERSION(2,0,0)
1335 gtk_widget_set_size_request(inst->area, large_x, large_y);
1337 gtk_widget_set_usize(inst->area, large_x, large_y);
1339 gtk_widget_size_request(inst->area, &inner);
1340 gtk_widget_size_request(inst->window, &outer);
1342 offset_x = outer.width - inner.width;
1343 offset_y = outer.height - inner.height;
1345 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1346 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1349 * Now we must set the size request on the drawing area back to
1350 * something sensible before we commit the real resize. Best
1351 * way to do this, I think, is to set it to what the size is
1352 * really going to end up being.
1354 #if GTK_CHECK_VERSION(2,0,0)
1355 gtk_widget_set_size_request(inst->area, area_x, area_y);
1356 _gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1357 gtk_window_resize(GTK_WINDOW(inst->window),
1358 area_x + offset_x, area_y + offset_y);
1360 gtk_widget_set_usize(inst->area, area_x, area_y);
1361 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1362 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1363 gdk_window_resize(inst->window->window,
1364 area_x + offset_x, area_y + offset_y);
1368 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1370 gboolean success[1];
1372 inst->cols[n].red = r * 0x0101;
1373 inst->cols[n].green = g * 0x0101;
1374 inst->cols[n].blue = b * 0x0101;
1376 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1377 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1378 FALSE, TRUE, success);
1380 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1384 void set_window_background(struct gui_data *inst)
1386 if (inst->area && inst->area->window)
1387 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1388 if (inst->window && inst->window->window)
1389 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1392 void palette_set(void *frontend, int n, int r, int g, int b)
1394 struct gui_data *inst = (struct gui_data *)frontend;
1397 if (n > NALLCOLOURS)
1399 real_palette_set(inst, n, r, g, b);
1401 /* Default Background changed. Ensure space between text area and
1402 * window border is redrawn */
1403 set_window_background(inst);
1404 draw_backing_rect(inst);
1405 gtk_widget_queue_draw(inst->area);
1409 void palette_reset(void *frontend)
1411 struct gui_data *inst = (struct gui_data *)frontend;
1412 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1413 static const int ww[] = {
1414 256, 257, 258, 259, 260, 261,
1415 0, 8, 1, 9, 2, 10, 3, 11,
1416 4, 12, 5, 13, 6, 14, 7, 15
1418 gboolean success[NALLCOLOURS];
1421 assert(lenof(ww) == NCFGCOLOURS);
1423 if (!inst->colmap) {
1424 inst->colmap = gdk_colormap_get_system();
1426 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1429 for (i = 0; i < NCFGCOLOURS; i++) {
1430 inst->cols[ww[i]].red = inst->cfg.colours[i][0] * 0x0101;
1431 inst->cols[ww[i]].green = inst->cfg.colours[i][1] * 0x0101;
1432 inst->cols[ww[i]].blue = inst->cfg.colours[i][2] * 0x0101;
1435 for (i = 0; i < NEXTCOLOURS; i++) {
1437 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1438 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1439 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1440 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1442 int shade = i - 216;
1443 shade = shade * 0x0a0a + 0x0808;
1444 inst->cols[i+16].red = inst->cols[i+16].green =
1445 inst->cols[i+16].blue = shade;
1449 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1450 FALSE, TRUE, success);
1451 for (i = 0; i < NALLCOLOURS; i++) {
1453 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1454 appname, i, inst->cfg.colours[i][0],
1455 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1458 /* Since Default Background may have changed, ensure that space
1459 * between text area and window border is refreshed. */
1460 set_window_background(inst);
1462 draw_backing_rect(inst);
1463 gtk_widget_queue_draw(inst->area);
1467 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1468 * do this before we start using cut buffers.
1470 void init_cutbuffers()
1472 unsigned char empty[] = "";
1473 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1474 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1475 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1476 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1477 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1478 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1479 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1480 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1481 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1482 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1483 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1484 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1485 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1486 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1487 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1488 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1491 /* Store the data in a cut-buffer. */
1492 void store_cutbuffer(char * ptr, int len)
1494 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1495 XRotateBuffers(GDK_DISPLAY(), 1);
1496 XStoreBytes(GDK_DISPLAY(), ptr, len);
1499 /* Retrieve data from a cut-buffer.
1500 * Returned data needs to be freed with XFree().
1502 char * retrieve_cutbuffer(int * nbytes)
1505 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1506 if (*nbytes <= 0 && ptr != 0) {
1513 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1515 struct gui_data *inst = (struct gui_data *)frontend;
1516 if (inst->pasteout_data)
1517 sfree(inst->pasteout_data);
1518 if (inst->pasteout_data_ctext)
1519 sfree(inst->pasteout_data_ctext);
1520 if (inst->pasteout_data_utf8)
1521 sfree(inst->pasteout_data_utf8);
1524 * Set up UTF-8 and compound text paste data. This only happens
1525 * if we aren't in direct-to-font mode using the D800 hack.
1527 if (!inst->direct_to_font) {
1528 wchar_t *tmp = data;
1533 inst->pasteout_data_utf8 = snewn(len*6, char);
1534 inst->pasteout_data_utf8_len = len*6;
1535 inst->pasteout_data_utf8_len =
1536 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1537 inst->pasteout_data_utf8_len,
1538 CS_UTF8, NULL, NULL, 0);
1539 if (inst->pasteout_data_utf8_len == 0) {
1540 sfree(inst->pasteout_data_utf8);
1541 inst->pasteout_data_utf8 = NULL;
1543 inst->pasteout_data_utf8 =
1544 sresize(inst->pasteout_data_utf8,
1545 inst->pasteout_data_utf8_len + 1, char);
1546 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1550 * Now let Xlib convert our UTF-8 data into compound text.
1552 list[0] = inst->pasteout_data_utf8;
1553 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1554 XCompoundTextStyle, &tp) == 0) {
1555 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1556 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1557 inst->pasteout_data_ctext_len = tp.nitems;
1560 inst->pasteout_data_ctext = NULL;
1561 inst->pasteout_data_ctext_len = 0;
1564 inst->pasteout_data_utf8 = NULL;
1565 inst->pasteout_data_utf8_len = 0;
1566 inst->pasteout_data_ctext = NULL;
1567 inst->pasteout_data_ctext_len = 0;
1570 inst->pasteout_data = snewn(len*6, char);
1571 inst->pasteout_data_len = len*6;
1572 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1573 data, len, inst->pasteout_data,
1574 inst->pasteout_data_len,
1576 if (inst->pasteout_data_len == 0) {
1577 sfree(inst->pasteout_data);
1578 inst->pasteout_data = NULL;
1580 inst->pasteout_data =
1581 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1584 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1586 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1587 inst->input_event_time)) {
1588 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1589 GDK_SELECTION_TYPE_STRING, 1);
1590 if (inst->pasteout_data_ctext)
1591 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1592 compound_text_atom, 1);
1593 if (inst->pasteout_data_utf8)
1594 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1595 utf8_string_atom, 1);
1599 term_deselect(inst->term);
1602 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1603 guint info, guint time_stamp, gpointer data)
1605 struct gui_data *inst = (struct gui_data *)data;
1606 if (seldata->target == utf8_string_atom)
1607 gtk_selection_data_set(seldata, seldata->target, 8,
1608 (unsigned char *)inst->pasteout_data_utf8,
1609 inst->pasteout_data_utf8_len);
1610 else if (seldata->target == compound_text_atom)
1611 gtk_selection_data_set(seldata, seldata->target, 8,
1612 (unsigned char *)inst->pasteout_data_ctext,
1613 inst->pasteout_data_ctext_len);
1615 gtk_selection_data_set(seldata, seldata->target, 8,
1616 (unsigned char *)inst->pasteout_data,
1617 inst->pasteout_data_len);
1620 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1623 struct gui_data *inst = (struct gui_data *)data;
1625 term_deselect(inst->term);
1626 if (inst->pasteout_data)
1627 sfree(inst->pasteout_data);
1628 if (inst->pasteout_data_ctext)
1629 sfree(inst->pasteout_data_ctext);
1630 if (inst->pasteout_data_utf8)
1631 sfree(inst->pasteout_data_utf8);
1632 inst->pasteout_data = NULL;
1633 inst->pasteout_data_len = 0;
1634 inst->pasteout_data_ctext = NULL;
1635 inst->pasteout_data_ctext_len = 0;
1636 inst->pasteout_data_utf8 = NULL;
1637 inst->pasteout_data_utf8_len = 0;
1641 void request_paste(void *frontend)
1643 struct gui_data *inst = (struct gui_data *)frontend;
1645 * In Unix, pasting is asynchronous: all we can do at the
1646 * moment is to call gtk_selection_convert(), and when the data
1647 * comes back _then_ we can call term_do_paste().
1650 if (!inst->direct_to_font) {
1652 * First we attempt to retrieve the selection as a UTF-8
1653 * string (which we will convert to the correct code page
1654 * before sending to the session, of course). If that
1655 * fails, selection_received() will be informed and will
1656 * fall back to an ordinary string.
1658 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1660 inst->input_event_time);
1663 * If we're in direct-to-font mode, we disable UTF-8
1664 * pasting, and go straight to ordinary string data.
1666 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1667 GDK_SELECTION_TYPE_STRING,
1668 inst->input_event_time);
1672 gint idle_paste_func(gpointer data); /* forward ref */
1674 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1675 guint time, gpointer data)
1677 struct gui_data *inst = (struct gui_data *)data;
1681 int length, count, ret;
1682 int free_list_required = 0;
1683 int free_required = 0;
1686 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1688 * Failed to get a UTF-8 selection string. Try compound
1691 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1693 inst->input_event_time);
1697 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1699 * Failed to get UTF-8 or compound text. Try an ordinary
1702 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1703 GDK_SELECTION_TYPE_STRING,
1704 inst->input_event_time);
1709 * If we have data, but it's not of a type we can deal with,
1710 * we have to ignore the data.
1712 if (seldata->length > 0 &&
1713 seldata->type != GDK_SELECTION_TYPE_STRING &&
1714 seldata->type != compound_text_atom &&
1715 seldata->type != utf8_string_atom)
1719 * If we have no data, try looking in a cut buffer.
1721 if (seldata->length <= 0) {
1722 text = retrieve_cutbuffer(&length);
1725 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1726 * source, so use that as a de-facto standard. */
1727 charset = CS_ISO8859_1;
1731 * Convert COMPOUND_TEXT into UTF-8.
1733 if (seldata->type == compound_text_atom) {
1734 tp.value = seldata->data;
1735 tp.encoding = (Atom) seldata->type;
1736 tp.format = seldata->format;
1737 tp.nitems = seldata->length;
1738 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1740 if (ret != 0 || count != 1) {
1742 * Compound text failed; fall back to STRING.
1744 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1745 GDK_SELECTION_TYPE_STRING,
1746 inst->input_event_time);
1750 length = strlen(list[0]);
1752 free_list_required = 1;
1754 text = (char *)seldata->data;
1755 length = seldata->length;
1756 charset = (seldata->type == utf8_string_atom ?
1757 CS_UTF8 : inst->ucsdata.line_codepage);
1761 if (inst->pastein_data)
1762 sfree(inst->pastein_data);
1764 inst->pastein_data = snewn(length, wchar_t);
1765 inst->pastein_data_len = length;
1766 inst->pastein_data_len =
1767 mb_to_wc(charset, 0, text, length,
1768 inst->pastein_data, inst->pastein_data_len);
1770 term_do_paste(inst->term);
1772 if (term_paste_pending(inst->term))
1773 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1775 if (free_list_required)
1776 XFreeStringList(list);
1781 gint idle_paste_func(gpointer data)
1783 struct gui_data *inst = (struct gui_data *)data;
1785 if (term_paste_pending(inst->term))
1786 term_paste(inst->term);
1788 gtk_idle_remove(inst->term_paste_idle_id);
1794 void get_clip(void *frontend, wchar_t ** p, int *len)
1796 struct gui_data *inst = (struct gui_data *)frontend;
1799 *p = inst->pastein_data;
1800 *len = inst->pastein_data_len;
1804 static void set_window_titles(struct gui_data *inst)
1807 * We must always call set_icon_name after calling set_title,
1808 * since set_title will write both names. Irritating, but such
1811 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1812 if (!inst->cfg.win_name_always)
1813 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1816 void set_title(void *frontend, char *title)
1818 struct gui_data *inst = (struct gui_data *)frontend;
1819 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1820 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1821 set_window_titles(inst);
1824 void set_icon(void *frontend, char *title)
1826 struct gui_data *inst = (struct gui_data *)frontend;
1827 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1828 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1829 set_window_titles(inst);
1832 void set_sbar(void *frontend, int total, int start, int page)
1834 struct gui_data *inst = (struct gui_data *)frontend;
1835 if (!inst->cfg.scrollbar)
1837 inst->sbar_adjust->lower = 0;
1838 inst->sbar_adjust->upper = total;
1839 inst->sbar_adjust->value = start;
1840 inst->sbar_adjust->page_size = page;
1841 inst->sbar_adjust->step_increment = 1;
1842 inst->sbar_adjust->page_increment = page/2;
1843 inst->ignore_sbar = TRUE;
1844 gtk_adjustment_changed(inst->sbar_adjust);
1845 inst->ignore_sbar = FALSE;
1848 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1850 struct gui_data *inst = (struct gui_data *)data;
1852 if (!inst->cfg.scrollbar)
1854 if (!inst->ignore_sbar)
1855 term_scroll(inst->term, 1, (int)adj->value);
1858 void sys_cursor(void *frontend, int x, int y)
1861 * This is meaningless under X.
1866 * This is still called when mode==BELL_VISUAL, even though the
1867 * visual bell is handled entirely within terminal.c, because we
1868 * may want to perform additional actions on any kind of bell (for
1869 * example, taskbar flashing in Windows).
1871 void do_beep(void *frontend, int mode)
1873 if (mode == BELL_DEFAULT)
1877 int char_width(Context ctx, int uc)
1880 * Under X, any fixed-width font really _is_ fixed-width.
1881 * Double-width characters will be dealt with using a separate
1882 * font. For the moment we can simply return 1.
1887 Context get_ctx(void *frontend)
1889 struct gui_data *inst = (struct gui_data *)frontend;
1890 struct draw_ctx *dctx;
1892 if (!inst->area->window)
1895 dctx = snew(struct draw_ctx);
1897 dctx->gc = gdk_gc_new(inst->area->window);
1901 void free_ctx(Context ctx)
1903 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1904 /* struct gui_data *inst = dctx->inst; */
1905 GdkGC *gc = dctx->gc;
1911 * Draw a line of text in the window, at given character
1912 * coordinates, in given attributes.
1914 * We are allowed to fiddle with the contents of `text'.
1916 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1917 unsigned long attr, int lattr)
1919 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1920 struct gui_data *inst = dctx->inst;
1921 GdkGC *gc = dctx->gc;
1922 int ncombining, combining;
1923 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1924 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
1926 if (attr & TATTR_COMBINING) {
1932 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
1933 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
1934 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
1939 if (inst->cfg.bold_colour && (attr & ATTR_BOLD)) {
1940 if (nfg < 16) nfg |= 8;
1941 else if (nfg >= 256) nfg |= 1;
1943 if (inst->cfg.bold_colour && (attr & ATTR_BLINK)) {
1944 if (nbg < 16) nbg |= 8;
1945 else if (nbg >= 256) nbg |= 1;
1947 if ((attr & TATTR_ACTCURS) && !monochrome) {
1952 fontid = shadow = 0;
1954 if (attr & ATTR_WIDE) {
1961 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1962 if (inst->fonts[fontid | 1])
1968 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1970 if (x >= inst->term->cols)
1972 if (x + len*2*widefactor > inst->term->cols)
1973 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1981 r.x = x*inst->font_width+inst->cfg.window_border;
1982 r.y = y*inst->font_height+inst->cfg.window_border;
1983 r.width = rlen*widefactor*inst->font_width;
1984 r.height = inst->font_height;
1985 gdk_gc_set_clip_rectangle(gc, &r);
1988 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1989 gdk_draw_rectangle(inst->pixmap, gc, 1,
1990 x*inst->font_width+inst->cfg.window_border,
1991 y*inst->font_height+inst->cfg.window_border,
1992 rlen*widefactor*inst->font_width, inst->font_height);
1994 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2001 wcs = snewn(len*ncombining+1, wchar_t);
2002 for (i = 0; i < len*ncombining; i++) {
2006 if (inst->fonts[fontid] == NULL && (fontid & 2)) {
2008 * We've been given ATTR_WIDE, but have no wide font.
2009 * Fall back to the non-wide font.
2014 if (inst->fonts[fontid] == NULL) {
2016 * The font for this contingency does not exist. So we
2017 * display nothing at all; such is life.
2019 } else if (inst->fontinfo[fontid].is_wide) {
2021 * At least one version of gdk_draw_text_wc() has a
2022 * weird bug whereby it reads `len' elements of the
2023 * input string, but only draws `len/2'. Hence I'm
2024 * going to make its input array twice as long as it
2025 * theoretically needs to be, and pass in twice the
2026 * actual number of characters. If a fixed gdk actually
2027 * takes the doubled length seriously, then (a) the
2028 * array will stand scrutiny up to the full length, (b)
2029 * the spare elements of the array are full of zeroes
2030 * which will probably be an empty glyph in the font,
2031 * and (c) the clip rectangle should prevent it causing
2034 gwcs = snewn(len*2+1, GdkWChar);
2035 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2037 * FIXME: when we have a wide-char equivalent of
2038 * from_unicode, use it instead of this.
2040 for (combining = 0; combining < ncombining; combining++) {
2041 for (i = 0; i <= len; i++)
2042 gwcs[i] = wcs[i + combining];
2043 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2044 x*inst->font_width+inst->cfg.window_border,
2045 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2048 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2049 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2050 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2055 gcs = snewn(len+1, gchar);
2056 for (combining = 0; combining < ncombining; combining++) {
2057 wc_to_mb(inst->fontinfo[fontid].charset, 0,
2058 wcs + combining, len, gcs, len, ".", NULL, NULL);
2059 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2060 x*inst->font_width+inst->cfg.window_border,
2061 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2064 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2065 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2066 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2074 if (attr & ATTR_UNDER) {
2075 int uheight = inst->fonts[0]->ascent + 1;
2076 if (uheight >= inst->font_height)
2077 uheight = inst->font_height - 1;
2078 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
2079 y*inst->font_height + uheight + inst->cfg.window_border,
2080 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
2081 y*inst->font_height + uheight + inst->cfg.window_border);
2084 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2086 * I can't find any plausible StretchBlt equivalent in the
2087 * X server, so I'm going to do this the slow and painful
2088 * way. This will involve repeated calls to
2089 * gdk_draw_pixmap() to stretch the text horizontally. It's
2090 * O(N^2) in time and O(N) in network bandwidth, but you
2091 * try thinking of a better way. :-(
2094 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2095 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2096 x*inst->font_width+inst->cfg.window_border + 2*i,
2097 y*inst->font_height+inst->cfg.window_border,
2098 x*inst->font_width+inst->cfg.window_border + 2*i+1,
2099 y*inst->font_height+inst->cfg.window_border,
2100 len * widefactor * inst->font_width - i, inst->font_height);
2103 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2105 /* Now stretch vertically, in the same way. */
2106 if ((lattr & LATTR_MODE) == LATTR_BOT)
2110 for (i = 0; i < inst->font_height; i+=2) {
2111 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2112 x*inst->font_width+inst->cfg.window_border,
2113 y*inst->font_height+inst->cfg.window_border+dt*i+db,
2114 x*inst->font_width+inst->cfg.window_border,
2115 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
2116 len * widefactor * inst->font_width, inst->font_height-i-1);
2122 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2123 unsigned long attr, int lattr)
2125 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2126 struct gui_data *inst = dctx->inst;
2127 GdkGC *gc = dctx->gc;
2130 do_text_internal(ctx, x, y, text, len, attr, lattr);
2132 if (attr & ATTR_WIDE) {
2138 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2140 if (x >= inst->term->cols)
2142 if (x + len*2*widefactor > inst->term->cols)
2143 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2147 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2148 x*inst->font_width+inst->cfg.window_border,
2149 y*inst->font_height+inst->cfg.window_border,
2150 x*inst->font_width+inst->cfg.window_border,
2151 y*inst->font_height+inst->cfg.window_border,
2152 len*widefactor*inst->font_width, inst->font_height);
2155 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2156 unsigned long attr, int lattr)
2158 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2159 struct gui_data *inst = dctx->inst;
2160 GdkGC *gc = dctx->gc;
2162 int active, passive, widefactor;
2164 if (attr & TATTR_PASCURS) {
2165 attr &= ~TATTR_PASCURS;
2169 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
2170 attr &= ~TATTR_ACTCURS;
2174 do_text_internal(ctx, x, y, text, len, attr, lattr);
2176 if (attr & TATTR_COMBINING)
2179 if (attr & ATTR_WIDE) {
2185 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2187 if (x >= inst->term->cols)
2189 if (x + len*2*widefactor > inst->term->cols)
2190 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2194 if (inst->cfg.cursor_type == 0) {
2196 * An active block cursor will already have been done by
2197 * the above do_text call, so we only need to do anything
2201 gdk_gc_set_foreground(gc, &inst->cols[261]);
2202 gdk_draw_rectangle(inst->pixmap, gc, 0,
2203 x*inst->font_width+inst->cfg.window_border,
2204 y*inst->font_height+inst->cfg.window_border,
2205 len*widefactor*inst->font_width-1, inst->font_height-1);
2209 int startx, starty, dx, dy, length, i;
2213 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2214 char_width = 2*inst->font_width;
2216 char_width = inst->font_width;
2218 if (inst->cfg.cursor_type == 1) {
2219 uheight = inst->fonts[0]->ascent + 1;
2220 if (uheight >= inst->font_height)
2221 uheight = inst->font_height - 1;
2223 startx = x * inst->font_width + inst->cfg.window_border;
2224 starty = y * inst->font_height + inst->cfg.window_border + uheight;
2227 length = len * widefactor * char_width;
2230 if (attr & TATTR_RIGHTCURS)
2231 xadjust = char_width - 1;
2232 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2233 starty = y * inst->font_height + inst->cfg.window_border;
2236 length = inst->font_height;
2239 gdk_gc_set_foreground(gc, &inst->cols[261]);
2241 for (i = 0; i < length; i++) {
2243 gdk_draw_point(inst->pixmap, gc, startx, starty);
2248 } else if (active) {
2249 gdk_draw_line(inst->pixmap, gc, startx, starty,
2250 startx + (length-1) * dx, starty + (length-1) * dy);
2251 } /* else no cursor (e.g., blinked off) */
2254 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2255 x*inst->font_width+inst->cfg.window_border,
2256 y*inst->font_height+inst->cfg.window_border,
2257 x*inst->font_width+inst->cfg.window_border,
2258 y*inst->font_height+inst->cfg.window_border,
2259 len*widefactor*inst->font_width, inst->font_height);
2262 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2265 * Truly hideous hack: GTK doesn't allow us to set the mouse
2266 * cursor foreground and background colours unless we've _also_
2267 * created our own cursor from bitmaps. Therefore, I need to
2268 * load the `cursor' font and draw glyphs from it on to
2269 * pixmaps, in order to construct my cursors with the fg and bg
2270 * I want. This is a gross hack, but it's more self-contained
2271 * than linking in Xlib to find the X window handle to
2272 * inst->area and calling XRecolorCursor, and it's more
2273 * futureproof than hard-coding the shapes as bitmap arrays.
2275 static GdkFont *cursor_font = NULL;
2276 GdkPixmap *source, *mask;
2278 GdkColor cfg = { 0, 65535, 65535, 65535 };
2279 GdkColor cbg = { 0, 0, 0, 0 };
2280 GdkColor dfg = { 1, 65535, 65535, 65535 };
2281 GdkColor dbg = { 0, 0, 0, 0 };
2284 gint lb, rb, wid, asc, desc, w, h, x, y;
2286 if (cursor_val == -2) {
2287 gdk_font_unref(cursor_font);
2291 if (cursor_val >= 0 && !cursor_font) {
2292 cursor_font = gdk_font_load("cursor");
2294 gdk_font_ref(cursor_font);
2298 * Get the text extent of the cursor in question. We use the
2299 * mask character for this, because it's typically slightly
2300 * bigger than the main character.
2302 if (cursor_val >= 0) {
2304 text[0] = (char)cursor_val + 1;
2305 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2306 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2312 source = gdk_pixmap_new(NULL, w, h, 1);
2313 mask = gdk_pixmap_new(NULL, w, h, 1);
2316 * Draw the mask character on the mask pixmap.
2318 gc = gdk_gc_new(mask);
2319 gdk_gc_set_foreground(gc, &dbg);
2320 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2321 if (cursor_val >= 0) {
2323 text[0] = (char)cursor_val + 1;
2324 gdk_gc_set_foreground(gc, &dfg);
2325 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2330 * Draw the main character on the source pixmap.
2332 gc = gdk_gc_new(source);
2333 gdk_gc_set_foreground(gc, &dbg);
2334 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2335 if (cursor_val >= 0) {
2337 text[0] = (char)cursor_val;
2338 gdk_gc_set_foreground(gc, &dfg);
2339 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2344 * Create the cursor.
2346 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2351 gdk_pixmap_unref(source);
2352 gdk_pixmap_unref(mask);
2357 void modalfatalbox(char *p, ...)
2360 fprintf(stderr, "FATAL ERROR: ");
2362 vfprintf(stderr, p, ap);
2364 fputc('\n', stderr);
2368 void cmdline_error(char *p, ...)
2371 fprintf(stderr, "%s: ", appname);
2373 vfprintf(stderr, p, ap);
2375 fputc('\n', stderr);
2379 char *get_x_display(void *frontend)
2381 return gdk_get_display();
2384 long get_windowid(void *frontend)
2386 struct gui_data *inst = (struct gui_data *)frontend;
2387 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2390 static void help(FILE *fp) {
2392 "pterm option summary:\n"
2394 " --display DISPLAY Specify X display to use (note '--')\n"
2395 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2396 " -fn FONT Normal text font\n"
2397 " -fb FONT Bold text font\n"
2398 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2399 " -sl LINES Number of lines of scrollback\n"
2400 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2401 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2402 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2403 " -T TITLE Window title\n"
2404 " -ut, +ut Do(default) or do not update utmp\n"
2405 " -ls, +ls Do(default) or do not make shell a login shell\n"
2406 " -sb, +sb Do(default) or do not display a scrollbar\n"
2407 " -log PATH Log all output to a file\n"
2408 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2409 " -xrm RESOURCE-STRING Set an X resource\n"
2410 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2411 ) < 0 || fflush(fp) < 0) {
2412 perror("output error");
2417 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2418 struct gui_data *inst, Config *cfg)
2424 * Macros to make argument handling easier. Note that because
2425 * they need to call `continue', they cannot be contained in
2426 * the usual do {...} while (0) wrapper to make them
2427 * syntactically single statements; hence it is not legal to
2428 * use one of these macros as an unbraced statement between
2431 #define EXPECTS_ARG { \
2432 if (--argc <= 0) { \
2434 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2439 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2441 while (--argc > 0) {
2446 * Shameless cheating. Debian requires all X terminal
2447 * emulators to support `-T title'; but
2448 * cmdline_process_param will eat -T (it means no-pty) and
2449 * complain that pterm doesn't support it. So, in pterm
2450 * only, we convert -T into -title.
2452 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2456 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2457 do_everything ? 1 : -1, cfg);
2460 cmdline_error("option \"%s\" requires an argument", p);
2461 } else if (ret == 2) {
2462 --argc, ++argv; /* skip next argument */
2464 } else if (ret == 1) {
2468 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2471 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2472 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2474 } else if (!strcmp(p, "-fb")) {
2477 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2478 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2480 } else if (!strcmp(p, "-fw")) {
2483 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2484 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2486 } else if (!strcmp(p, "-fwb")) {
2489 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2490 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2492 } else if (!strcmp(p, "-cs")) {
2495 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2496 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2498 } else if (!strcmp(p, "-geometry")) {
2504 flags = XParseGeometry(val, &x, &y, &w, &h);
2505 if (flags & WidthValue)
2506 cfg->width = (int)w;
2507 if (flags & HeightValue)
2508 cfg->height = (int)h;
2510 if (flags & (XValue | YValue)) {
2513 inst->gotpos = TRUE;
2514 inst->gravity = ((flags & XNegative ? 1 : 0) |
2515 (flags & YNegative ? 2 : 0));
2518 } else if (!strcmp(p, "-sl")) {
2521 cfg->savelines = atoi(val);
2523 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2524 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2525 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2530 if (!gdk_color_parse(val, &col)) {
2532 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2536 index = (!strcmp(p, "-fg") ? 0 :
2537 !strcmp(p, "-bg") ? 2 :
2538 !strcmp(p, "-bfg") ? 1 :
2539 !strcmp(p, "-bbg") ? 3 :
2540 !strcmp(p, "-cfg") ? 4 :
2541 !strcmp(p, "-cbg") ? 5 : -1);
2542 assert(index != -1);
2543 cfg->colours[index][0] = col.red / 256;
2544 cfg->colours[index][1] = col.green / 256;
2545 cfg->colours[index][2] = col.blue / 256;
2548 } else if (use_pty_argv && !strcmp(p, "-e")) {
2549 /* This option swallows all further arguments. */
2555 pty_argv = snewn(argc+1, char *);
2557 for (i = 0; i < argc; i++)
2558 pty_argv[i] = argv[i];
2559 pty_argv[argc] = NULL;
2560 break; /* finished command-line processing */
2562 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2565 } else if (!strcmp(p, "-title")) {
2568 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2569 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2571 } else if (!strcmp(p, "-log")) {
2574 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2575 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2576 cfg->logtype = LGTYP_DEBUG;
2578 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2580 cfg->stamp_utmp = 0;
2582 } else if (!strcmp(p, "-ut")) {
2584 cfg->stamp_utmp = 1;
2586 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2588 cfg->login_shell = 0;
2590 } else if (!strcmp(p, "-ls")) {
2592 cfg->login_shell = 1;
2594 } else if (!strcmp(p, "-nethack")) {
2596 cfg->nethack_keypad = 1;
2598 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2602 } else if (!strcmp(p, "-sb")) {
2606 } else if (!strcmp(p, "-name")) {
2610 } else if (!strcmp(p, "-xrm")) {
2612 provide_xrm_string(val);
2614 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2618 } else if (!strcmp(p, "-pgpfp")) {
2622 } else if(p[0] != '-' && (!do_everything ||
2623 process_nonoption_arg(p, cfg,
2629 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2637 * This function retrieves the character set encoding of a font. It
2638 * returns the character set without the X11 hack (in case the user
2639 * asks to use the font's own encoding).
2641 static int set_font_info(struct gui_data *inst, int fontid)
2643 GdkFont *font = inst->fonts[fontid];
2644 XFontStruct *xfs = GDK_FONT_XFONT(font);
2645 Display *disp = GDK_FONT_XDISPLAY(font);
2646 Atom charset_registry, charset_encoding;
2647 unsigned long registry_ret, encoding_ret;
2648 int retval = CS_NONE;
2650 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2651 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2652 inst->fontinfo[fontid].charset = CS_NONE;
2653 inst->fontinfo[fontid].is_wide = 0;
2654 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2655 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2657 reg = XGetAtomName(disp, (Atom)registry_ret);
2658 enc = XGetAtomName(disp, (Atom)encoding_ret);
2660 char *encoding = dupcat(reg, "-", enc, NULL);
2661 retval = inst->fontinfo[fontid].charset =
2662 charset_from_xenc(encoding);
2663 /* FIXME: when libcharset supports wide encodings fix this. */
2664 if (!strcasecmp(encoding, "iso10646-1")) {
2665 inst->fontinfo[fontid].is_wide = 1;
2670 * Hack for X line-drawing characters: if the primary
2671 * font is encoded as ISO-8859-anything, and has valid
2672 * glyphs in the first 32 char positions, it is assumed
2673 * that those glyphs are the VT100 line-drawing
2676 * Actually, we'll hack even harder by only checking
2677 * position 0x19 (vertical line, VT100 linedrawing
2678 * `x'). Then we can check it easily by seeing if the
2679 * ascent and descent differ.
2681 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2682 int lb, rb, wid, asc, desc;
2687 gdk_string_extents(inst->fonts[fontid], text,
2688 &lb, &rb, &wid, &asc, &desc);
2690 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2700 int uxsel_input_add(int fd, int rwx) {
2702 if (rwx & 1) flags |= GDK_INPUT_READ;
2703 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2704 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2706 return gdk_input_add(fd, flags, fd_input_func, NULL);
2709 void uxsel_input_remove(int id) {
2710 gdk_input_remove(id);
2713 char *guess_derived_font_name(GdkFont *font, int bold, int wide)
2715 XFontStruct *xfs = GDK_FONT_XFONT(font);
2716 Display *disp = GDK_FONT_XDISPLAY(font);
2717 Atom fontprop = XInternAtom(disp, "FONT", False);
2719 if (XGetFontProperty(xfs, fontprop, &ret)) {
2720 char *name = XGetAtomName(disp, (Atom)ret);
2721 if (name && name[0] == '-') {
2723 char *dupname, *extrafree = NULL, *ret;
2727 p = q = dupname = dupstr(name); /* skip initial minus */
2730 while (*p && nstr < lenof(strings)) {
2733 strings[nstr++] = p+1;
2738 if (nstr < lenof(strings))
2739 return NULL; /* XLFD was malformed */
2742 strings[2] = "bold";
2745 /* 4 is `wideness', which obviously may have changed. */
2746 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2747 strings[4] = strings[5] = "*";
2748 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
2751 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
2752 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
2753 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
2754 "-", strings[ 9], "-", strings[10], "-", strings[11],
2755 "-", strings[12], NULL);
2765 void setup_fonts_ucs(struct gui_data *inst)
2772 gdk_font_unref(inst->fonts[0]);
2774 gdk_font_unref(inst->fonts[1]);
2776 gdk_font_unref(inst->fonts[2]);
2778 gdk_font_unref(inst->fonts[3]);
2780 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2781 if (!inst->fonts[0]) {
2782 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2783 inst->cfg.font.name);
2786 gdk_font_ref(inst->fonts[0]);
2787 font_charset = set_font_info(inst, 0);
2789 if (inst->cfg.shadowbold) {
2790 inst->fonts[1] = NULL;
2792 if (inst->cfg.boldfont.name[0]) {
2793 name = inst->cfg.boldfont.name;
2796 name = guess_derived_font_name(inst->fonts[0], TRUE, FALSE);
2799 inst->fonts[1] = name ? gdk_font_load(name) : NULL;
2800 if (inst->fonts[1]) {
2801 gdk_font_ref(inst->fonts[1]);
2802 set_font_info(inst, 1);
2803 } else if (!guessed) {
2804 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2805 inst->cfg.boldfont.name);
2812 if (inst->cfg.widefont.name[0]) {
2813 name = inst->cfg.widefont.name;
2816 name = guess_derived_font_name(inst->fonts[0], FALSE, TRUE);
2819 inst->fonts[2] = name ? gdk_font_load(name) : NULL;
2820 if (inst->fonts[2]) {
2821 gdk_font_ref(inst->fonts[2]);
2822 set_font_info(inst, 2);
2823 } else if (!guessed) {
2824 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2825 inst->cfg.widefont.name);
2831 if (inst->cfg.shadowbold) {
2832 inst->fonts[3] = NULL;
2834 if (inst->cfg.wideboldfont.name[0]) {
2835 name = inst->cfg.wideboldfont.name;
2839 * Here we have some choices. We can widen the bold font,
2840 * bolden the wide font, or widen and bolden the standard
2841 * font. Try them all, in that order!
2843 if (inst->cfg.widefont.name[0])
2844 name = guess_derived_font_name(inst->fonts[2], TRUE, FALSE);
2845 else if (inst->cfg.boldfont.name[0])
2846 name = guess_derived_font_name(inst->fonts[1], FALSE, TRUE);
2848 name = guess_derived_font_name(inst->fonts[0], TRUE, TRUE);
2851 inst->fonts[3] = name ? gdk_font_load(name) : NULL;
2852 if (inst->fonts[3]) {
2853 gdk_font_ref(inst->fonts[3]);
2854 set_font_info(inst, 3);
2855 } else if (!guessed) {
2856 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n", appname,
2857 inst->cfg.wideboldfont.name);
2864 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2865 if (!inst->font_width) {
2866 /* Maybe this is a 16-bit font? If so, GDK 2 actually expects a
2867 * pointer to an XChar2b. This is pretty revolting. Can Pango do
2868 * this more neatly even for server-side fonts?
2873 inst->font_width = gdk_text_width(inst->fonts[0],
2874 (const gchar *)&space, 2);
2876 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2878 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2879 inst->cfg.utf8_override, font_charset,
2883 void set_geom_hints(struct gui_data *inst)
2886 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2887 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2888 geom.max_width = geom.max_height = -1;
2889 geom.base_width = 2*inst->cfg.window_border;
2890 geom.base_height = 2*inst->cfg.window_border;
2891 geom.width_inc = inst->font_width;
2892 geom.height_inc = inst->font_height;
2893 geom.min_aspect = geom.max_aspect = 0;
2894 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2895 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2896 GDK_HINT_RESIZE_INC);
2899 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2901 struct gui_data *inst = (struct gui_data *)data;
2902 term_clrsb(inst->term);
2905 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2907 struct gui_data *inst = (struct gui_data *)data;
2908 term_pwron(inst->term, TRUE);
2910 ldisc_send(inst->ldisc, NULL, 0, 0);
2913 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2915 struct gui_data *inst = (struct gui_data *)data;
2916 term_copyall(inst->term);
2919 void special_menuitem(GtkMenuItem *item, gpointer data)
2921 struct gui_data *inst = (struct gui_data *)data;
2922 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2926 inst->back->special(inst->backhandle, code);
2929 void about_menuitem(GtkMenuItem *item, gpointer data)
2931 struct gui_data *inst = (struct gui_data *)data;
2932 about_box(inst->window);
2935 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2937 struct gui_data *inst = (struct gui_data *)data;
2938 showeventlog(inst->eventlogstuff, inst->window);
2941 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2943 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2944 static const int ww[] = {
2945 256, 257, 258, 259, 260, 261,
2946 0, 8, 1, 9, 2, 10, 3, 11,
2947 4, 12, 5, 13, 6, 14, 7, 15
2949 struct gui_data *inst = (struct gui_data *)data;
2950 char *title = dupcat(appname, " Reconfiguration", NULL);
2951 Config cfg2, oldcfg;
2954 assert(lenof(ww) == NCFGCOLOURS);
2956 if (inst->reconfiguring)
2959 inst->reconfiguring = TRUE;
2961 cfg2 = inst->cfg; /* structure copy */
2963 if (do_config_box(title, &cfg2, 1,
2964 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2966 oldcfg = inst->cfg; /* structure copy */
2967 inst->cfg = cfg2; /* structure copy */
2969 /* Pass new config data to the logging module */
2970 log_reconfig(inst->logctx, &cfg2);
2972 * Flush the line discipline's edit buffer in the case
2973 * where local editing has just been disabled.
2976 ldisc_send(inst->ldisc, NULL, 0, 0);
2977 /* Pass new config data to the terminal */
2978 term_reconfig(inst->term, &cfg2);
2979 /* Pass new config data to the back end */
2981 inst->back->reconfig(inst->backhandle, &cfg2);
2984 * Just setting inst->cfg is sufficient to cause colour
2985 * setting changes to appear on the next ESC]R palette
2986 * reset. But we should also check whether any colour
2987 * settings have been changed, and revert the ones that
2988 * have to the new default, on the assumption that the user
2989 * is most likely to want an immediate update.
2991 for (i = 0; i < NCFGCOLOURS; i++) {
2992 if (oldcfg.colours[i][0] != cfg2.colours[i][0] ||
2993 oldcfg.colours[i][1] != cfg2.colours[i][1] ||
2994 oldcfg.colours[i][2] != cfg2.colours[i][2]) {
2995 real_palette_set(inst, ww[i], cfg2.colours[i][0],
2997 cfg2.colours[i][2]);
3000 * If the default background has changed, we must
3001 * repaint the space in between the window border
3002 * and the text area.
3005 set_window_background(inst);
3006 draw_backing_rect(inst);
3012 * If the scrollbar needs to be shown, hidden, or moved
3013 * from one end to the other of the window, do so now.
3015 if (oldcfg.scrollbar != cfg2.scrollbar) {
3017 gtk_widget_show(inst->sbar);
3019 gtk_widget_hide(inst->sbar);
3021 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
3022 gtk_box_reorder_child(inst->hbox, inst->sbar,
3023 cfg2.scrollbar_on_left ? 0 : 1);
3027 * Change the window title, if required.
3029 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
3030 set_title(inst, cfg2.wintitle);
3031 set_window_titles(inst);
3034 * Redo the whole tangled fonts and Unicode mess if
3037 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
3038 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
3039 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
3040 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
3041 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
3042 oldcfg.vtmode != cfg2.vtmode ||
3043 oldcfg.shadowbold != cfg2.shadowbold) {
3044 setup_fonts_ucs(inst);
3050 * Resize the window.
3052 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
3053 oldcfg.window_border != cfg2.window_border || need_size) {
3054 set_geom_hints(inst);
3055 request_resize(inst, cfg2.width, cfg2.height);
3058 * The above will have caused a call to term_size() for
3059 * us if it happened. If the user has fiddled with only
3060 * the scrollback size, the above will not have
3061 * happened and we will need an explicit term_size()
3064 if (oldcfg.savelines != cfg2.savelines)
3065 term_size(inst->term, inst->term->rows, inst->term->cols,
3069 term_invalidate(inst->term);
3072 * We do an explicit full redraw here to ensure the window
3073 * border has been redrawn as well as the text area.
3075 gtk_widget_queue_draw(inst->area);
3078 inst->reconfiguring = FALSE;
3081 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3084 * Re-execing ourself is not an exact science under Unix. I do
3085 * the best I can by using /proc/self/exe if available and by
3086 * assuming argv[0] can be found on $PATH if not.
3088 * Note that we also have to reconstruct the elements of the
3089 * original argv which gtk swallowed, since the user wants the
3090 * new session to appear on the same X display as the old one.
3098 * Collect the arguments with which to re-exec ourself.
3100 va_start(ap, fd_to_close);
3101 n = 2; /* progname and terminating NULL */
3102 n += inst->ngtkargs;
3103 while (va_arg(ap, char *) != NULL)
3107 args = snewn(n, char *);
3108 args[0] = inst->progname;
3110 for (i = 0; i < inst->ngtkargs; i++)
3111 args[i+1] = inst->gtkargvstart[i];
3114 va_start(ap, fd_to_close);
3115 while ((args[i++] = va_arg(ap, char *)) != NULL);
3121 * Do the double fork.
3134 } else if (pid2 > 0) {
3136 * First child has successfully forked second child. My
3137 * Work Here Is Done. Note the use of _exit rather than
3138 * exit: the latter appears to cause destroy messages
3139 * to be sent to the X server. I suspect gtk uses
3146 * If we reach here, we are the second child, so we now
3147 * actually perform the exec.
3149 if (fd_to_close >= 0)
3152 execv("/proc/self/exe", args);
3153 execvp(inst->progname, args);
3159 waitpid(pid, &status, 0);
3164 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3166 struct gui_data *inst = (struct gui_data *)gdata;
3168 * For this feature we must marshal cfg and (possibly) pty_argv
3169 * into a byte stream, create a pipe, and send this byte stream
3170 * to the child through the pipe.
3177 if (pipe(pipefd) < 0) {
3182 size = sizeof(inst->cfg);
3183 if (use_pty_argv && pty_argv) {
3184 for (i = 0; pty_argv[i]; i++)
3185 size += strlen(pty_argv[i]) + 1;
3188 data = snewn(size, char);
3189 memcpy(data, &inst->cfg, sizeof(inst->cfg));
3190 if (use_pty_argv && pty_argv) {
3191 int p = sizeof(inst->cfg);
3192 for (i = 0; pty_argv[i]; i++) {
3193 strcpy(data + p, pty_argv[i]);
3194 p += strlen(pty_argv[i]) + 1;
3199 sprintf(option, "---[%d,%d]", pipefd[0], size);
3200 fcntl(pipefd[0], F_SETFD, 0);
3201 fork_and_exec_self(inst, pipefd[1], option, NULL);
3205 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3208 perror("write to pipe");
3213 int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3215 int fd, i, ret, size;
3218 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3219 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3223 data = snewn(size, char);
3225 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3228 perror("read from pipe");
3230 } else if (i < size) {
3231 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3236 memcpy(cfg, data, sizeof(Config));
3237 if (use_pty_argv && size > sizeof(Config)) {
3241 while (i < size && data[i]) i++;
3243 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3250 pty_argv = snewn(n+1, char *);
3256 while (i < size && data[i]) i++;
3259 pty_argv[n++] = dupstr(p);
3266 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3268 struct gui_data *inst = (struct gui_data *)data;
3270 fork_and_exec_self(inst, -1, NULL);
3273 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3275 struct gui_data *inst = (struct gui_data *)data;
3278 logevent(inst, "----- Session restarted -----");
3279 term_pwron(inst->term, FALSE);
3280 start_backend(inst);
3281 inst->exited = FALSE;
3285 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3287 struct gui_data *inst = (struct gui_data *)data;
3288 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3290 fork_and_exec_self(inst, -1, "-load", str, NULL);
3293 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3295 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3300 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3302 struct gui_data *inst = (struct gui_data *)data;
3303 struct sesslist sesslist;
3306 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3307 (GtkCallback)gtk_widget_destroy, NULL);
3309 get_sesslist(&sesslist, TRUE);
3310 for (i = 1; i < sesslist.nsessions; i++) {
3311 GtkWidget *menuitem =
3312 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3313 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3314 gtk_widget_show(menuitem);
3315 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3316 dupstr(sesslist.sessions[i]));
3317 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3318 GTK_SIGNAL_FUNC(saved_session_menuitem),
3320 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3321 GTK_SIGNAL_FUNC(saved_session_freedata),
3324 get_sesslist(&sesslist, FALSE); /* free up */
3327 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3331 GdkBitmap *iconmask;
3332 #if GTK_CHECK_VERSION(2,0,0)
3340 gtk_widget_realize(window);
3341 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3342 NULL, (gchar **)icon[0]);
3343 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3345 #if GTK_CHECK_VERSION(2,0,0)
3347 for (n = 0; n < n_icon; n++) {
3349 g_list_append(iconlist,
3350 gdk_pixbuf_new_from_xpm_data((const gchar **)
3353 gdk_window_set_icon_list(window->window, iconlist);
3357 void update_specials_menu(void *frontend)
3359 struct gui_data *inst = (struct gui_data *)frontend;
3361 const struct telnet_special *specials;
3364 specials = inst->back->get_specials(inst->backhandle);
3368 /* I believe this disposes of submenus too. */
3369 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3370 (GtkCallback)gtk_widget_destroy, NULL);
3373 GtkWidget *menu = inst->specialsmenu;
3374 /* A lame "stack" for submenus that will do for now. */
3375 GtkWidget *saved_menu = NULL;
3377 for (i = 0; nesting > 0; i++) {
3378 GtkWidget *menuitem = NULL;
3379 switch (specials[i].code) {
3381 assert (nesting < 2);
3382 saved_menu = menu; /* XXX lame stacking */
3383 menu = gtk_menu_new();
3384 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3385 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3386 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3387 gtk_widget_show(menuitem);
3394 menu = saved_menu; /* XXX lame stacking */
3399 menuitem = gtk_menu_item_new();
3402 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3403 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3404 GINT_TO_POINTER(specials[i].code));
3405 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3406 GTK_SIGNAL_FUNC(special_menuitem), inst);
3410 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3411 gtk_widget_show(menuitem);
3414 gtk_widget_show(inst->specialsitem1);
3415 gtk_widget_show(inst->specialsitem2);
3417 gtk_widget_hide(inst->specialsitem1);
3418 gtk_widget_hide(inst->specialsitem2);
3422 static void start_backend(struct gui_data *inst)
3424 extern Backend *select_backend(Config *cfg);
3428 inst->back = select_backend(&inst->cfg);
3430 error = inst->back->init((void *)inst, &inst->backhandle,
3431 &inst->cfg, inst->cfg.host, inst->cfg.port,
3432 &realhost, inst->cfg.tcp_nodelay,
3433 inst->cfg.tcp_keepalives);
3436 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3437 inst->cfg.host, error);
3438 inst->exited = TRUE;
3439 fatal_message_box(inst->window, msg);
3444 if (inst->cfg.wintitle[0]) {
3445 set_title(inst, inst->cfg.wintitle);
3446 set_icon(inst, inst->cfg.wintitle);
3448 char *title = make_default_wintitle(realhost);
3449 set_title(inst, title);
3450 set_icon(inst, title);
3455 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3457 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3460 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3463 gtk_widget_hide(inst->restartitem);
3466 int pt_main(int argc, char **argv)
3468 extern int cfgbox(Config *cfg);
3469 struct gui_data *inst;
3472 * Create an instance structure and initialise to zeroes
3474 inst = snew(struct gui_data);
3475 memset(inst, 0, sizeof(*inst));
3476 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3477 inst->busy_status = BUSY_NOT;
3479 /* defer any child exit handling until we're ready to deal with
3481 block_signal(SIGCHLD, 1);
3483 inst->progname = argv[0];
3485 * Copy the original argv before letting gtk_init fiddle with
3486 * it. It will be required later.
3490 inst->gtkargvstart = snewn(argc-1, char *);
3491 for (i = 1; i < argc; i++)
3492 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3494 gtk_init(&argc, &argv);
3495 inst->ngtkargs = oldargc - argc;
3498 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3499 read_dupsession_data(inst, &inst->cfg, argv[1]);
3500 /* Splatter this argument so it doesn't clutter a ps listing */
3501 memset(argv[1], 0, strlen(argv[1]));
3503 /* By default, we bring up the config dialog, rather than launching
3504 * a session. This gets set to TRUE if something happens to change
3505 * that (e.g., a hostname is specified on the command-line). */
3506 int allow_launch = FALSE;
3507 if (do_cmdline(argc, argv, 0, &allow_launch, inst, &inst->cfg))
3508 exit(1); /* pre-defaults pass to get -class */
3509 do_defaults(NULL, &inst->cfg);
3510 if (do_cmdline(argc, argv, 1, &allow_launch, inst, &inst->cfg))
3511 exit(1); /* post-defaults, do everything */
3513 cmdline_run_saved(&inst->cfg);
3516 allow_launch = TRUE;
3518 if ((!allow_launch || !cfg_launchable(&inst->cfg)) &&
3519 !cfgbox(&inst->cfg))
3520 exit(0); /* config box hit Cancel */
3523 if (!compound_text_atom)
3524 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3525 if (!utf8_string_atom)
3526 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3528 setup_fonts_ucs(inst);
3531 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3534 * Set up the colour map.
3536 palette_reset(inst);
3538 inst->width = inst->cfg.width;
3539 inst->height = inst->cfg.height;
3541 inst->area = gtk_drawing_area_new();
3542 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3543 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3544 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
3545 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3546 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3547 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3549 * We always create the scrollbar; it remains invisible if
3550 * unwanted, so we can pop it up quickly if it suddenly becomes
3553 if (inst->cfg.scrollbar_on_left)
3554 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3555 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3556 if (!inst->cfg.scrollbar_on_left)
3557 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3559 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3561 set_geom_hints(inst);
3563 gtk_widget_show(inst->area);
3564 if (inst->cfg.scrollbar)
3565 gtk_widget_show(inst->sbar);
3567 gtk_widget_hide(inst->sbar);
3568 gtk_widget_show(GTK_WIDGET(inst->hbox));
3571 int x = inst->xpos, y = inst->ypos;
3573 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3574 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3575 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3576 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3577 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3580 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3581 GTK_SIGNAL_FUNC(destroy), inst);
3582 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3583 GTK_SIGNAL_FUNC(delete_window), inst);
3584 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3585 GTK_SIGNAL_FUNC(key_event), inst);
3586 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3587 GTK_SIGNAL_FUNC(key_event), inst);
3588 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3589 GTK_SIGNAL_FUNC(focus_event), inst);
3590 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3591 GTK_SIGNAL_FUNC(focus_event), inst);
3592 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3593 GTK_SIGNAL_FUNC(configure_area), inst);
3594 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3595 GTK_SIGNAL_FUNC(expose_area), inst);
3596 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3597 GTK_SIGNAL_FUNC(button_event), inst);
3598 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3599 GTK_SIGNAL_FUNC(button_event), inst);
3600 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3601 GTK_SIGNAL_FUNC(motion_event), inst);
3602 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3603 GTK_SIGNAL_FUNC(selection_received), inst);
3604 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3605 GTK_SIGNAL_FUNC(selection_get), inst);
3606 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3607 GTK_SIGNAL_FUNC(selection_clear), inst);
3608 if (inst->cfg.scrollbar)
3609 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3610 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3611 gtk_widget_add_events(GTK_WIDGET(inst->area),
3612 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3613 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3614 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3617 extern const char *const *const main_icon[];
3618 extern const int n_main_icon;
3619 set_window_icon(inst->window, main_icon, n_main_icon);
3622 gtk_widget_show(inst->window);
3624 set_window_background(inst);
3627 * Set up the Ctrl+rightclick context menu.
3630 GtkWidget *menuitem;
3632 extern const int use_event_log, new_session, saved_sessions;
3634 inst->menu = gtk_menu_new();
3636 #define MKMENUITEM(title, func) do { \
3637 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3638 gtk_menu_item_new(); \
3639 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3640 gtk_widget_show(menuitem); \
3642 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3643 GTK_SIGNAL_FUNC(func), inst); \
3646 MKMENUITEM("New Session", new_session_menuitem);
3647 MKMENUITEM("Restart Session", restart_session_menuitem);
3648 inst->restartitem = menuitem;
3649 gtk_widget_hide(inst->restartitem);
3650 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3651 if (saved_sessions) {
3652 inst->sessionsmenu = gtk_menu_new();
3653 /* sessionsmenu will be updated when it's invoked */
3654 /* XXX is this the right way to do dynamic menus in Gtk? */
3655 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3656 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3657 inst->sessionsmenu);
3659 MKMENUITEM(NULL, NULL);
3660 MKMENUITEM("Change Settings", change_settings_menuitem);
3661 MKMENUITEM(NULL, NULL);
3663 MKMENUITEM("Event Log", event_log_menuitem);
3664 MKMENUITEM("Special Commands", NULL);
3665 inst->specialsmenu = gtk_menu_new();
3666 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3667 inst->specialsitem1 = menuitem;
3668 MKMENUITEM(NULL, NULL);
3669 inst->specialsitem2 = menuitem;
3670 gtk_widget_hide(inst->specialsitem1);
3671 gtk_widget_hide(inst->specialsitem2);
3672 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3673 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3674 MKMENUITEM("Copy All", copy_all_menuitem);
3675 MKMENUITEM(NULL, NULL);
3676 s = dupcat("About ", appname, NULL);
3677 MKMENUITEM(s, about_menuitem);
3682 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3683 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3684 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3685 inst->blankcursor = make_mouse_ptr(inst, -1);
3686 make_mouse_ptr(inst, -2); /* clean up cursor font */
3687 inst->currcursor = inst->textcursor;
3688 show_mouseptr(inst, 1);
3690 inst->eventlogstuff = eventlogstuff_new();
3692 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3693 inst->logctx = log_init(inst, &inst->cfg);
3694 term_provide_logctx(inst->term, inst->logctx);
3698 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3700 start_backend(inst);
3702 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3704 /* now we're reday to deal with the child exit handler being
3706 block_signal(SIGCHLD, 0);
3709 * Block SIGPIPE: if we attempt Duplicate Session or similar
3710 * and it falls over in some way, we certainly don't want
3711 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3712 * this _after_ (at least pterm) forks off its child process,
3713 * since the child wants SIGPIPE handled in the usual way.
3715 block_signal(SIGPIPE, 1);
3717 inst->exited = FALSE;