2 * gtkwin.c: the main code that runs a PuTTY terminal emulator and
3 * backend in a GTK window.
18 #include <sys/types.h>
21 #include <gdk/gdkkeysyms.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
33 #define CAT2(x,y) x ## y
34 #define CAT(x,y) CAT2(x,y)
35 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
37 #if GTK_CHECK_VERSION(2,0,0)
38 ASSERT(sizeof(long) <= sizeof(gsize));
39 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
40 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
42 ASSERT(sizeof(long) <= sizeof(gpointer));
43 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
44 #define GPOINTER_TO_LONG(p) ((long)(p))
47 /* Colours come in two flavours: configurable, and xterm-extended. */
48 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
49 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
51 GdkAtom compound_text_atom, utf8_string_atom;
53 extern char **pty_argv; /* declared in pty.c */
54 extern int use_pty_argv;
57 * Timers are global across all sessions (even if we were handling
58 * multiple sessions, which we aren't), so the current timer ID is
61 static guint timer_id = 0;
64 GtkWidget *window, *area, *sbar;
66 GtkAdjustment *sbar_adjust;
67 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
69 GtkWidget *sessionsmenu;
71 unifont *fonts[4]; /* normal, bold, wide, widebold */
72 int xpos, ypos, gotpos, gravity;
73 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
74 GdkColor cols[NALLCOLOURS];
76 wchar_t *pastein_data;
79 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
80 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
81 int font_width, font_height;
86 guint term_paste_idle_id;
87 guint term_exit_idle_id;
92 int master_fd, master_func_id;
99 struct unicode_data ucsdata;
102 char *progname, **gtkargvstart;
104 guint32 input_event_time; /* Timestamp of the most recent input event. */
106 /* Cached things out of conf that we refer to a lot */
112 static void cache_conf_values(struct gui_data *inst)
114 inst->bold_colour = conf_get_int(inst->conf, CONF_bold_colour);
115 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
116 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
121 struct gui_data *inst;
124 static int send_raw_mouse;
126 static char *app_name = "pterm";
128 static void start_backend(struct gui_data *inst);
130 char *x_get_default(const char *key)
132 return XGetDefault(GDK_DISPLAY(), app_name, key);
135 void connection_fatal(void *frontend, char *p, ...)
137 struct gui_data *inst = (struct gui_data *)frontend;
142 msg = dupvprintf(p, ap);
145 fatal_message_box(inst->window, msg);
147 if (conf_get_int(inst->conf, CONF_close_on_exit) == FORCE_ON)
152 * Default settings that are specific to pterm.
154 FontSpec platform_default_fontspec(const char *name)
157 if (!strcmp(name, "Font"))
158 strcpy(ret.name, "server:fixed");
164 Filename platform_default_filename(const char *name)
167 if (!strcmp(name, "LogFileName"))
168 strcpy(ret.path, "putty.log");
174 char *platform_default_s(const char *name)
176 if (!strcmp(name, "SerialLine"))
177 return dupstr("/dev/ttyS0");
181 int platform_default_i(const char *name, int def)
183 if (!strcmp(name, "CloseOnExit"))
184 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
185 if (!strcmp(name, "WinNameAlways"))
186 return 0; /* X natively supports icon titles, so use 'em by default */
190 /* Dummy routine, only required in plink. */
191 void ldisc_update(void *frontend, int echo, int edit)
195 char *get_ttymode(void *frontend, const char *mode)
197 struct gui_data *inst = (struct gui_data *)frontend;
198 return term_get_ttymode(inst->term, mode);
201 int from_backend(void *frontend, int is_stderr, const char *data, int len)
203 struct gui_data *inst = (struct gui_data *)frontend;
204 return term_data(inst->term, is_stderr, data, len);
207 int from_backend_untrusted(void *frontend, const char *data, int len)
209 struct gui_data *inst = (struct gui_data *)frontend;
210 return term_data_untrusted(inst->term, data, len);
213 int from_backend_eof(void *frontend)
215 return TRUE; /* do respond to incoming EOF with outgoing */
218 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
220 struct gui_data *inst = (struct gui_data *)p->frontend;
222 ret = cmdline_get_passwd_input(p, in, inlen);
224 ret = term_get_userpass_input(inst->term, p, in, inlen);
228 void logevent(void *frontend, const char *string)
230 struct gui_data *inst = (struct gui_data *)frontend;
232 log_eventlog(inst->logctx, string);
234 logevent_dlg(inst->eventlogstuff, string);
237 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
239 struct gui_data *inst = (struct gui_data *)frontend;
242 return inst->font_height;
244 return inst->font_width;
248 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
249 * into a cooked one (SELECT, EXTEND, PASTE).
251 * In Unix, this is not configurable; the X button arrangement is
252 * rock-solid across all applications, everyone has a three-button
253 * mouse or a means of faking it, and there is no need to switch
254 * buttons around at all.
256 static Mouse_Button translate_button(Mouse_Button button)
258 /* struct gui_data *inst = (struct gui_data *)frontend; */
260 if (button == MBT_LEFT)
262 if (button == MBT_MIDDLE)
264 if (button == MBT_RIGHT)
266 return 0; /* shouldn't happen */
270 * Return the top-level GtkWindow associated with a particular
271 * front end instance.
273 void *get_window(void *frontend)
275 struct gui_data *inst = (struct gui_data *)frontend;
280 * Minimise or restore the window in response to a server-side
283 void set_iconic(void *frontend, int iconic)
286 * GTK 1.2 doesn't know how to do this.
288 #if GTK_CHECK_VERSION(2,0,0)
289 struct gui_data *inst = (struct gui_data *)frontend;
291 gtk_window_iconify(GTK_WINDOW(inst->window));
293 gtk_window_deiconify(GTK_WINDOW(inst->window));
298 * Move the window in response to a server-side request.
300 void move_window(void *frontend, int x, int y)
302 struct gui_data *inst = (struct gui_data *)frontend;
304 * I assume that when the GTK version of this call is available
305 * we should use it. Not sure how it differs from the GDK one,
308 #if GTK_CHECK_VERSION(2,0,0)
309 gtk_window_move(GTK_WINDOW(inst->window), x, y);
311 gdk_window_move(inst->window->window, x, y);
316 * Move the window to the top or bottom of the z-order in response
317 * to a server-side request.
319 void set_zorder(void *frontend, int top)
321 struct gui_data *inst = (struct gui_data *)frontend;
323 gdk_window_raise(inst->window->window);
325 gdk_window_lower(inst->window->window);
329 * Refresh the window in response to a server-side request.
331 void refresh_window(void *frontend)
333 struct gui_data *inst = (struct gui_data *)frontend;
334 term_invalidate(inst->term);
338 * Maximise or restore the window in response to a server-side
341 void set_zoomed(void *frontend, int zoomed)
344 * GTK 1.2 doesn't know how to do this.
346 #if GTK_CHECK_VERSION(2,0,0)
347 struct gui_data *inst = (struct gui_data *)frontend;
349 gtk_window_maximize(GTK_WINDOW(inst->window));
351 gtk_window_unmaximize(GTK_WINDOW(inst->window));
356 * Report whether the window is iconic, for terminal reports.
358 int is_iconic(void *frontend)
360 struct gui_data *inst = (struct gui_data *)frontend;
361 return !gdk_window_is_viewable(inst->window->window);
365 * Report the window's position, for terminal reports.
367 void get_window_pos(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_position(GTK_WINDOW(inst->window), x, y);
378 gdk_window_get_position(inst->window->window, x, y);
383 * Report the window's pixel size, for terminal reports.
385 void get_window_pixels(void *frontend, int *x, int *y)
387 struct gui_data *inst = (struct gui_data *)frontend;
389 * I assume that when the GTK version of this call is available
390 * we should use it. Not sure how it differs from the GDK one,
393 #if GTK_CHECK_VERSION(2,0,0)
394 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
396 gdk_window_get_size(inst->window->window, x, y);
401 * Return the window or icon title.
403 char *get_window_title(void *frontend, int icon)
405 struct gui_data *inst = (struct gui_data *)frontend;
406 return icon ? inst->icontitle : inst->wintitle;
409 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
411 struct gui_data *inst = (struct gui_data *)data;
412 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
413 if (!reallyclose(inst))
419 static void update_mouseptr(struct gui_data *inst)
421 switch (inst->busy_status) {
423 if (!inst->mouseptr_visible) {
424 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
425 } else if (send_raw_mouse) {
426 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
428 gdk_window_set_cursor(inst->area->window, inst->textcursor);
431 case BUSY_WAITING: /* XXX can we do better? */
433 /* We always display these cursors. */
434 gdk_window_set_cursor(inst->area->window, inst->waitcursor);
441 static void show_mouseptr(struct gui_data *inst, int show)
443 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
445 inst->mouseptr_visible = show;
446 update_mouseptr(inst);
449 void draw_backing_rect(struct gui_data *inst)
451 GdkGC *gc = gdk_gc_new(inst->area->window);
452 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
453 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
454 inst->width * inst->font_width + 2*inst->window_border,
455 inst->height * inst->font_height + 2*inst->window_border);
459 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
461 struct gui_data *inst = (struct gui_data *)data;
462 int w, h, need_size = 0;
465 * See if the terminal size has changed, in which case we must
466 * let the terminal know.
468 w = (event->width - 2*inst->window_border) / inst->font_width;
469 h = (event->height - 2*inst->window_border) / inst->font_height;
470 if (w != inst->width || h != inst->height) {
473 conf_set_int(inst->conf, CONF_width, inst->width);
474 conf_set_int(inst->conf, CONF_height, inst->height);
479 gdk_pixmap_unref(inst->pixmap);
483 inst->pixmap = gdk_pixmap_new(widget->window,
484 (w * inst->font_width + 2*inst->window_border),
485 (h * inst->font_height + 2*inst->window_border), -1);
487 draw_backing_rect(inst);
489 if (need_size && inst->term) {
490 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
494 term_invalidate(inst->term);
499 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
501 struct gui_data *inst = (struct gui_data *)data;
504 * Pass the exposed rectangle to terminal.c, which will call us
505 * back to do the actual painting.
508 gdk_draw_pixmap(widget->window,
509 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
511 event->area.x, event->area.y,
512 event->area.x, event->area.y,
513 event->area.width, event->area.height);
518 #define KEY_PRESSED(k) \
519 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
521 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
523 struct gui_data *inst = (struct gui_data *)data;
525 wchar_t ucsoutput[2];
526 int ucsval, start, end, special, output_charset, use_ucsoutput;
528 /* Remember the timestamp. */
529 inst->input_event_time = event->time;
531 /* By default, nothing is generated. */
533 special = use_ucsoutput = FALSE;
534 output_charset = CS_ISO8859_1;
537 * If Alt is being released after typing an Alt+numberpad
538 * sequence, we should generate the code that was typed.
540 * Note that we only do this if more than one key was actually
541 * pressed - I don't think Alt+NumPad4 should be ^D or that
542 * Alt+NumPad3 should be ^C, for example. There's no serious
543 * inconvenience in having to type a zero before a single-digit
546 if (event->type == GDK_KEY_RELEASE &&
547 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
548 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
549 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
551 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
554 * FIXME: we might usefully try to do something clever here
555 * about interpreting the generated key code in a way that's
556 * appropriate to the line code page.
558 output[0] = inst->alt_keycode;
563 if (event->type == GDK_KEY_PRESS) {
567 printf("keypress: keyval = %04x, state = %08x; string =",
568 event->keyval, event->state);
569 for (i = 0; event->string[i]; i++)
570 printf(" %02x", (unsigned char) event->string[i]);
576 * NYI: Compose key (!!! requires Unicode faff before even trying)
580 * If Alt has just been pressed, we start potentially
581 * accumulating an Alt+numberpad code. We do this by
582 * setting alt_keycode to -1 (nothing yet but plausible).
584 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
585 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
586 inst->alt_keycode = -1;
587 inst->alt_digits = 0;
588 goto done; /* this generates nothing else */
592 * If we're seeing a numberpad key press with Mod1 down,
593 * consider adding it to alt_keycode if that's sensible.
594 * Anything _else_ with Mod1 down cancels any possibility
595 * of an ALT keycode: we set alt_keycode to -2.
597 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
599 switch (event->keyval) {
600 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
601 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
602 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
603 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
604 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
605 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
606 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
607 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
608 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
609 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
612 inst->alt_keycode = -2; /* it's invalid */
615 printf("Adding digit %d to keycode %d", digit,
618 if (inst->alt_keycode == -1)
619 inst->alt_keycode = digit; /* one-digit code */
621 inst->alt_keycode = inst->alt_keycode * 10 + digit;
624 printf(" gives new code %d\n", inst->alt_keycode);
626 /* Having used this digit, we now do nothing more with it. */
632 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
635 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
636 term_scroll(inst->term, 0, -inst->height/2);
639 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
640 term_scroll(inst->term, 0, -1);
643 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
644 term_scroll(inst->term, 0, +inst->height/2);
647 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
648 term_scroll(inst->term, 0, +1);
653 * Neither does Shift-Ins.
655 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
661 use_ucsoutput = FALSE;
663 /* ALT+things gives leading Escape. */
665 #if !GTK_CHECK_VERSION(2,0,0)
667 * In vanilla X, and hence also GDK 1.2, the string received
668 * as part of a keyboard event is assumed to be in
669 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
670 * but it's true: see the man page for XLookupString(3) for
673 output_charset = CS_ISO8859_1;
674 strncpy(output+1, event->string, lenof(output)-1);
677 * GDK 2.0 arranges to have done some translation for us: in
678 * GDK 2.0, event->string is encoded in the current locale.
680 * (However, it's also deprecated; we really ought to be
681 * using a GTKIMContext.)
683 * So we use the standard C library function mbstowcs() to
684 * convert from the current locale into Unicode; from there
685 * we can convert to whatever PuTTY is currently working in.
686 * (In fact I convert straight back to UTF-8 from
687 * wide-character Unicode, for the sake of simplicity: that
688 * way we can still use exactly the same code to manipulate
689 * the string, such as prefixing ESC.)
691 output_charset = CS_UTF8;
693 wchar_t widedata[32];
698 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
699 event->string, strlen(event->string),
700 widedata, lenof(widedata)-1);
703 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
704 CS_UTF8, NULL, NULL, 0);
705 output[1+ulen] = '\0';
710 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
711 ucsoutput[0] = '\033';
712 ucsoutput[1] = ucsval;
713 use_ucsoutput = TRUE;
716 output[lenof(output)-1] = '\0';
717 end = strlen(output);
719 if (event->state & GDK_MOD1_MASK) {
721 if (end == 1) end = 0;
725 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
726 if (!output[1] && event->keyval == '`' &&
727 (event->state & GDK_CONTROL_MASK)) {
729 use_ucsoutput = FALSE;
733 /* Control-Break sends a Break special to the backend */
734 if (event->keyval == GDK_Break &&
735 (event->state & GDK_CONTROL_MASK)) {
737 inst->back->special(inst->backhandle, TS_BRK);
741 /* We handle Return ourselves, because it needs to be flagged as
742 * special to ldisc. */
743 if (event->keyval == GDK_Return) {
745 use_ucsoutput = FALSE;
750 /* Control-2, Control-Space and Control-@ are NUL */
752 (event->keyval == ' ' || event->keyval == '2' ||
753 event->keyval == '@') &&
754 (event->state & (GDK_SHIFT_MASK |
755 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
757 use_ucsoutput = FALSE;
761 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
762 if (!output[1] && event->keyval == ' ' &&
763 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
764 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
766 output_charset = CS_ISO8859_1;
767 use_ucsoutput = FALSE;
771 /* We don't let GTK tell us what Backspace is! We know better. */
772 if (event->keyval == GDK_BackSpace &&
773 !(event->state & GDK_SHIFT_MASK)) {
774 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
776 use_ucsoutput = FALSE;
780 /* For Shift Backspace, do opposite of what is configured. */
781 if (event->keyval == GDK_BackSpace &&
782 (event->state & GDK_SHIFT_MASK)) {
783 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
785 use_ucsoutput = FALSE;
790 /* Shift-Tab is ESC [ Z */
791 if (event->keyval == GDK_ISO_Left_Tab ||
792 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
793 end = 1 + sprintf(output+1, "\033[Z");
794 use_ucsoutput = FALSE;
796 /* And normal Tab is Tab, if the keymap hasn't already told us.
797 * (Curiously, at least one version of the MacOS 10.5 X server
798 * doesn't translate Tab for us. */
799 if (event->keyval == GDK_Tab && end <= 1) {
805 * NetHack keypad mode.
807 if (conf_get_int(inst->conf, CONF_nethack_keypad)) {
809 switch (event->keyval) {
810 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
811 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
812 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
813 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
814 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
815 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
816 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
817 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
818 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
822 if (event->state & GDK_CONTROL_MASK)
824 else if (event->state & GDK_SHIFT_MASK)
828 use_ucsoutput = FALSE;
834 * Application keypad mode.
836 if (inst->term->app_keypad_keys &&
837 !conf_get_int(inst->conf, CONF_no_applic_k)) {
839 switch (event->keyval) {
840 case GDK_Num_Lock: xkey = 'P'; break;
841 case GDK_KP_Divide: xkey = 'Q'; break;
842 case GDK_KP_Multiply: xkey = 'R'; break;
843 case GDK_KP_Subtract: xkey = 'S'; break;
845 * Keypad + is tricky. It covers a space that would
846 * be taken up on the VT100 by _two_ keys; so we
847 * let Shift select between the two. Worse still,
848 * in xterm function key mode we change which two...
851 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
852 if (event->state & GDK_SHIFT_MASK)
856 } else if (event->state & GDK_SHIFT_MASK)
861 case GDK_KP_Enter: xkey = 'M'; break;
862 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
863 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
864 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
865 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
866 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
867 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
868 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
869 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
870 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
871 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
872 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
875 if (inst->term->vt52_mode) {
876 if (xkey >= 'P' && xkey <= 'S')
877 end = 1 + sprintf(output+1, "\033%c", xkey);
879 end = 1 + sprintf(output+1, "\033?%c", xkey);
881 end = 1 + sprintf(output+1, "\033O%c", xkey);
882 use_ucsoutput = FALSE;
888 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
889 * for integer decimal nn.)
891 * We also deal with the weird ones here. Linux VCs replace F1
892 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
893 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
898 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
899 switch (event->keyval) {
901 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
904 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
907 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
910 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
913 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
916 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
919 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
922 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
925 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
928 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
961 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
962 case GDK_Home: case GDK_KP_Home:
965 case GDK_Insert: case GDK_KP_Insert:
968 case GDK_Delete: case GDK_KP_Delete:
971 case GDK_End: case GDK_KP_End:
974 case GDK_Page_Up: case GDK_KP_Page_Up:
977 case GDK_Page_Down: case GDK_KP_Page_Down:
981 /* Reorder edit keys to physical order */
982 if (funky_type == FUNKY_VT400 && code <= 6)
983 code = "\0\2\1\4\5\3\6"[code];
985 if (inst->term->vt52_mode && code > 0 && code <= 6) {
986 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
987 use_ucsoutput = FALSE;
991 if (funky_type == FUNKY_SCO && /* SCO function keys */
992 code >= 11 && code <= 34) {
993 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
995 switch (event->keyval) {
996 case GDK_F1: index = 0; break;
997 case GDK_F2: index = 1; break;
998 case GDK_F3: index = 2; break;
999 case GDK_F4: index = 3; break;
1000 case GDK_F5: index = 4; break;
1001 case GDK_F6: index = 5; break;
1002 case GDK_F7: index = 6; break;
1003 case GDK_F8: index = 7; break;
1004 case GDK_F9: index = 8; break;
1005 case GDK_F10: index = 9; break;
1006 case GDK_F11: index = 10; break;
1007 case GDK_F12: index = 11; break;
1009 if (event->state & GDK_SHIFT_MASK) index += 12;
1010 if (event->state & GDK_CONTROL_MASK) index += 24;
1011 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1012 use_ucsoutput = FALSE;
1015 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1016 code >= 1 && code <= 6) {
1017 char codes[] = "HL.FIG";
1022 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1024 use_ucsoutput = FALSE;
1027 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1028 code >= 11 && code <= 24) {
1034 if (inst->term->vt52_mode)
1035 end = 1 + sprintf(output+1,
1036 "\x1B%c", code + 'P' - 11 - offt);
1038 end = 1 + sprintf(output+1,
1039 "\x1BO%c", code + 'P' - 11 - offt);
1040 use_ucsoutput = FALSE;
1043 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1044 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1045 use_ucsoutput = FALSE;
1048 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1049 if (inst->term->vt52_mode)
1050 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1052 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1053 use_ucsoutput = FALSE;
1056 if ((code == 1 || code == 4) &&
1057 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1058 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1059 use_ucsoutput = FALSE;
1063 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1064 use_ucsoutput = FALSE;
1070 * Cursor keys. (This includes the numberpad cursor keys,
1071 * if we haven't already done them due to app keypad mode.)
1073 * Here we also process un-numlocked un-appkeypadded KP5,
1074 * which sends ESC [ G.
1078 switch (event->keyval) {
1079 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1080 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1081 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1082 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1083 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1086 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1087 event->state & GDK_CONTROL_MASK);
1088 use_ucsoutput = FALSE;
1097 if (end-start > 0) {
1098 #ifdef KEY_DEBUGGING
1100 printf("generating sequence:");
1101 for (i = start; i < end; i++)
1102 printf(" %02x", (unsigned char) output[i]);
1108 * For special control characters, the character set
1109 * should never matter.
1111 output[end] = '\0'; /* NUL-terminate */
1113 ldisc_send(inst->ldisc, output+start, -2, 1);
1114 } else if (!inst->direct_to_font) {
1115 if (!use_ucsoutput) {
1117 lpage_send(inst->ldisc, output_charset, output+start,
1121 * We generated our own Unicode key data from the
1122 * keysym, so use that instead.
1125 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1129 * In direct-to-font mode, we just send the string
1130 * exactly as we received it.
1133 ldisc_send(inst->ldisc, output+start, end-start, 1);
1136 show_mouseptr(inst, 0);
1137 term_seen_key_event(inst->term);
1143 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1144 GdkEventType type, guint ebutton, guint state,
1145 gdouble ex, gdouble ey)
1147 int shift, ctrl, alt, x, y, button, act;
1149 /* Remember the timestamp. */
1150 inst->input_event_time = timestamp;
1152 show_mouseptr(inst, 1);
1154 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1155 term_scroll(inst->term, 0, -5);
1158 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1159 term_scroll(inst->term, 0, +5);
1163 shift = state & GDK_SHIFT_MASK;
1164 ctrl = state & GDK_CONTROL_MASK;
1165 alt = state & GDK_MOD1_MASK;
1167 if (ebutton == 3 && ctrl) {
1168 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1169 ebutton, timestamp);
1175 else if (ebutton == 2)
1176 button = MBT_MIDDLE;
1177 else if (ebutton == 3)
1180 return FALSE; /* don't even know what button! */
1183 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1184 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1185 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1186 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1187 default: return FALSE; /* don't know this event type */
1190 if (send_raw_mouse && !(shift && conf_get_int(inst->conf,
1191 CONF_mouse_override)) &&
1192 act != MA_CLICK && act != MA_RELEASE)
1193 return TRUE; /* we ignore these in raw mouse mode */
1195 x = (ex - inst->window_border) / inst->font_width;
1196 y = (ey - inst->window_border) / inst->font_height;
1198 term_mouse(inst->term, button, translate_button(button), act,
1199 x, y, shift, ctrl, alt);
1204 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1206 struct gui_data *inst = (struct gui_data *)data;
1207 return button_internal(inst, event->time, event->type, event->button,
1208 event->state, event->x, event->y);
1211 #if GTK_CHECK_VERSION(2,0,0)
1213 * In GTK 2, mouse wheel events have become a new type of event.
1214 * This handler translates them back into button-4 and button-5
1215 * presses so that I don't have to change my old code too much :-)
1217 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1219 struct gui_data *inst = (struct gui_data *)data;
1222 if (event->direction == GDK_SCROLL_UP)
1224 else if (event->direction == GDK_SCROLL_DOWN)
1229 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1230 button, event->state, event->x, event->y);
1234 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1236 struct gui_data *inst = (struct gui_data *)data;
1237 int shift, ctrl, alt, x, y, button;
1239 /* Remember the timestamp. */
1240 inst->input_event_time = event->time;
1242 show_mouseptr(inst, 1);
1244 shift = event->state & GDK_SHIFT_MASK;
1245 ctrl = event->state & GDK_CONTROL_MASK;
1246 alt = event->state & GDK_MOD1_MASK;
1247 if (event->state & GDK_BUTTON1_MASK)
1249 else if (event->state & GDK_BUTTON2_MASK)
1250 button = MBT_MIDDLE;
1251 else if (event->state & GDK_BUTTON3_MASK)
1254 return FALSE; /* don't even know what button! */
1256 x = (event->x - inst->window_border) / inst->font_width;
1257 y = (event->y - inst->window_border) / inst->font_height;
1259 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1260 x, y, shift, ctrl, alt);
1265 void frontend_keypress(void *handle)
1267 struct gui_data *inst = (struct gui_data *)handle;
1270 * If our child process has exited but not closed, terminate on
1277 static gint idle_exit_func(gpointer data)
1279 struct gui_data *inst = (struct gui_data *)data;
1280 int exitcode, close_on_exit;
1282 if (!inst->exited &&
1283 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1284 inst->exited = TRUE;
1285 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1286 if (close_on_exit == FORCE_ON ||
1287 (close_on_exit == AUTO && exitcode == 0))
1288 gtk_main_quit(); /* just go */
1290 ldisc_free(inst->ldisc);
1294 inst->back->free(inst->backhandle);
1295 inst->backhandle = NULL;
1297 term_provide_resize_fn(inst->term, NULL, NULL);
1298 update_specials_menu(inst);
1300 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1303 gtk_idle_remove(inst->term_exit_idle_id);
1307 void notify_remote_exit(void *frontend)
1309 struct gui_data *inst = (struct gui_data *)frontend;
1311 inst->term_exit_idle_id = gtk_idle_add(idle_exit_func, inst);
1314 static gint timer_trigger(gpointer data)
1316 long now = GPOINTER_TO_LONG(data);
1320 if (run_timers(now, &next)) {
1321 ticks = next - GETTICKCOUNT();
1322 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1323 LONG_TO_GPOINTER(next));
1327 * Never let a timer resume. If we need another one, we've
1328 * asked for it explicitly above.
1333 void timer_change_notify(long next)
1338 gtk_timeout_remove(timer_id);
1340 ticks = next - GETTICKCOUNT();
1342 ticks = 1; /* just in case */
1344 timer_id = gtk_timeout_add(ticks, timer_trigger,
1345 LONG_TO_GPOINTER(next));
1348 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1351 * We must process exceptional notifications before ordinary
1352 * readability ones, or we may go straight past the urgent
1355 if (condition & GDK_INPUT_EXCEPTION)
1356 select_result(sourcefd, 4);
1357 if (condition & GDK_INPUT_READ)
1358 select_result(sourcefd, 1);
1359 if (condition & GDK_INPUT_WRITE)
1360 select_result(sourcefd, 2);
1363 void destroy(GtkWidget *widget, gpointer data)
1368 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1370 struct gui_data *inst = (struct gui_data *)data;
1371 term_set_focus(inst->term, event->in);
1372 term_update(inst->term);
1373 show_mouseptr(inst, 1);
1377 void set_busy_status(void *frontend, int status)
1379 struct gui_data *inst = (struct gui_data *)frontend;
1380 inst->busy_status = status;
1381 update_mouseptr(inst);
1385 * set or clear the "raw mouse message" mode
1387 void set_raw_mouse_mode(void *frontend, int activate)
1389 struct gui_data *inst = (struct gui_data *)frontend;
1390 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1391 send_raw_mouse = activate;
1392 update_mouseptr(inst);
1395 void request_resize(void *frontend, int w, int h)
1397 struct gui_data *inst = (struct gui_data *)frontend;
1398 int large_x, large_y;
1399 int offset_x, offset_y;
1401 GtkRequisition inner, outer;
1404 * This is a heinous hack dreamed up by the gnome-terminal
1405 * people to get around a limitation in gtk. The problem is
1406 * that in order to set the size correctly we really need to be
1407 * calling gtk_window_resize - but that needs to know the size
1408 * of the _whole window_, not the drawing area. So what we do
1409 * is to set an artificially huge size request on the drawing
1410 * area, recompute the resulting size request on the window,
1411 * and look at the difference between the two. That gives us
1412 * the x and y offsets we need to translate drawing area size
1413 * into window size for real, and then we call
1414 * gtk_window_resize.
1418 * We start by retrieving the current size of the whole window.
1419 * Adding a bit to _that_ will give us a value we can use as a
1420 * bogus size request which guarantees to be bigger than the
1421 * current size of the drawing area.
1423 get_window_pixels(inst, &large_x, &large_y);
1427 #if GTK_CHECK_VERSION(2,0,0)
1428 gtk_widget_set_size_request(inst->area, large_x, large_y);
1430 gtk_widget_set_usize(inst->area, large_x, large_y);
1432 gtk_widget_size_request(inst->area, &inner);
1433 gtk_widget_size_request(inst->window, &outer);
1435 offset_x = outer.width - inner.width;
1436 offset_y = outer.height - inner.height;
1438 area_x = inst->font_width * w + 2*inst->window_border;
1439 area_y = inst->font_height * h + 2*inst->window_border;
1442 * Now we must set the size request on the drawing area back to
1443 * something sensible before we commit the real resize. Best
1444 * way to do this, I think, is to set it to what the size is
1445 * really going to end up being.
1447 #if GTK_CHECK_VERSION(2,0,0)
1448 gtk_widget_set_size_request(inst->area, area_x, area_y);
1449 gtk_window_resize(GTK_WINDOW(inst->window),
1450 area_x + offset_x, area_y + offset_y);
1452 gtk_widget_set_usize(inst->area, area_x, area_y);
1453 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1455 * I can no longer remember what this call to
1456 * gtk_container_dequeue_resize_handler is for. It was
1457 * introduced in r3092 with no comment, and the commit log
1458 * message was uninformative. I'm _guessing_ its purpose is to
1459 * prevent gratuitous resize processing on the window given
1460 * that we're about to resize it anyway, but I have no idea
1461 * why that's so incredibly vital.
1463 * I've tried removing the call, and nothing seems to go
1464 * wrong. I've backtracked to r3092 and tried removing the
1465 * call there, and still nothing goes wrong. So I'm going to
1466 * adopt the working hypothesis that it's superfluous; I won't
1467 * actually remove it from the GTK 1.2 code, but I won't
1468 * attempt to replicate its functionality in the GTK 2 code
1471 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1472 gdk_window_resize(inst->window->window,
1473 area_x + offset_x, area_y + offset_y);
1477 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1479 gboolean success[1];
1481 inst->cols[n].red = r * 0x0101;
1482 inst->cols[n].green = g * 0x0101;
1483 inst->cols[n].blue = b * 0x0101;
1485 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1486 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1487 FALSE, TRUE, success);
1489 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1493 void set_window_background(struct gui_data *inst)
1495 if (inst->area && inst->area->window)
1496 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1497 if (inst->window && inst->window->window)
1498 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1501 void palette_set(void *frontend, int n, int r, int g, int b)
1503 struct gui_data *inst = (struct gui_data *)frontend;
1506 if (n > NALLCOLOURS)
1508 real_palette_set(inst, n, r, g, b);
1510 /* Default Background changed. Ensure space between text area and
1511 * window border is redrawn */
1512 set_window_background(inst);
1513 draw_backing_rect(inst);
1514 gtk_widget_queue_draw(inst->area);
1518 void palette_reset(void *frontend)
1520 struct gui_data *inst = (struct gui_data *)frontend;
1521 /* This maps colour indices in inst->conf to those used in inst->cols. */
1522 static const int ww[] = {
1523 256, 257, 258, 259, 260, 261,
1524 0, 8, 1, 9, 2, 10, 3, 11,
1525 4, 12, 5, 13, 6, 14, 7, 15
1527 gboolean success[NALLCOLOURS];
1530 assert(lenof(ww) == NCFGCOLOURS);
1532 if (!inst->colmap) {
1533 inst->colmap = gdk_colormap_get_system();
1535 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1538 for (i = 0; i < NCFGCOLOURS; i++) {
1539 inst->cols[ww[i]].red =
1540 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
1541 inst->cols[ww[i]].green =
1542 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
1543 inst->cols[ww[i]].blue =
1544 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
1547 for (i = 0; i < NEXTCOLOURS; i++) {
1549 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1550 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1551 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1552 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1554 int shade = i - 216;
1555 shade = shade * 0x0a0a + 0x0808;
1556 inst->cols[i+16].red = inst->cols[i+16].green =
1557 inst->cols[i+16].blue = shade;
1561 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1562 FALSE, TRUE, success);
1563 for (i = 0; i < NALLCOLOURS; i++) {
1565 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1567 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
1568 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
1569 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
1572 /* Since Default Background may have changed, ensure that space
1573 * between text area and window border is refreshed. */
1574 set_window_background(inst);
1575 if (inst->area && inst->area->window) {
1576 draw_backing_rect(inst);
1577 gtk_widget_queue_draw(inst->area);
1581 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1582 * do this before we start using cut buffers.
1584 void init_cutbuffers()
1586 unsigned char empty[] = "";
1587 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1588 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1589 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1590 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1591 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1592 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1593 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1594 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1595 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1596 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1597 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1598 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1599 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1600 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1601 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1602 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1605 /* Store the data in a cut-buffer. */
1606 void store_cutbuffer(char * ptr, int len)
1608 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1609 XRotateBuffers(GDK_DISPLAY(), 1);
1610 XStoreBytes(GDK_DISPLAY(), ptr, len);
1613 /* Retrieve data from a cut-buffer.
1614 * Returned data needs to be freed with XFree().
1616 char * retrieve_cutbuffer(int * nbytes)
1619 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1620 if (*nbytes <= 0 && ptr != 0) {
1627 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1629 struct gui_data *inst = (struct gui_data *)frontend;
1630 if (inst->pasteout_data)
1631 sfree(inst->pasteout_data);
1632 if (inst->pasteout_data_ctext)
1633 sfree(inst->pasteout_data_ctext);
1634 if (inst->pasteout_data_utf8)
1635 sfree(inst->pasteout_data_utf8);
1638 * Set up UTF-8 and compound text paste data. This only happens
1639 * if we aren't in direct-to-font mode using the D800 hack.
1641 if (!inst->direct_to_font) {
1642 const wchar_t *tmp = data;
1647 inst->pasteout_data_utf8 = snewn(len*6, char);
1648 inst->pasteout_data_utf8_len = len*6;
1649 inst->pasteout_data_utf8_len =
1650 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1651 inst->pasteout_data_utf8_len,
1652 CS_UTF8, NULL, NULL, 0);
1653 if (inst->pasteout_data_utf8_len == 0) {
1654 sfree(inst->pasteout_data_utf8);
1655 inst->pasteout_data_utf8 = NULL;
1657 inst->pasteout_data_utf8 =
1658 sresize(inst->pasteout_data_utf8,
1659 inst->pasteout_data_utf8_len + 1, char);
1660 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1664 * Now let Xlib convert our UTF-8 data into compound text.
1666 list[0] = inst->pasteout_data_utf8;
1667 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1668 XCompoundTextStyle, &tp) == 0) {
1669 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1670 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1671 inst->pasteout_data_ctext_len = tp.nitems;
1674 inst->pasteout_data_ctext = NULL;
1675 inst->pasteout_data_ctext_len = 0;
1678 inst->pasteout_data_utf8 = NULL;
1679 inst->pasteout_data_utf8_len = 0;
1680 inst->pasteout_data_ctext = NULL;
1681 inst->pasteout_data_ctext_len = 0;
1684 inst->pasteout_data = snewn(len*6, char);
1685 inst->pasteout_data_len = len*6;
1686 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1687 data, len, inst->pasteout_data,
1688 inst->pasteout_data_len,
1690 if (inst->pasteout_data_len == 0) {
1691 sfree(inst->pasteout_data);
1692 inst->pasteout_data = NULL;
1694 inst->pasteout_data =
1695 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1698 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1700 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1701 inst->input_event_time)) {
1702 #if GTK_CHECK_VERSION(2,0,0)
1703 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
1705 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1706 GDK_SELECTION_TYPE_STRING, 1);
1707 if (inst->pasteout_data_ctext)
1708 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1709 compound_text_atom, 1);
1710 if (inst->pasteout_data_utf8)
1711 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1712 utf8_string_atom, 1);
1716 term_deselect(inst->term);
1719 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1720 guint info, guint time_stamp, gpointer data)
1722 struct gui_data *inst = (struct gui_data *)data;
1723 if (seldata->target == utf8_string_atom)
1724 gtk_selection_data_set(seldata, seldata->target, 8,
1725 (unsigned char *)inst->pasteout_data_utf8,
1726 inst->pasteout_data_utf8_len);
1727 else if (seldata->target == compound_text_atom)
1728 gtk_selection_data_set(seldata, seldata->target, 8,
1729 (unsigned char *)inst->pasteout_data_ctext,
1730 inst->pasteout_data_ctext_len);
1732 gtk_selection_data_set(seldata, seldata->target, 8,
1733 (unsigned char *)inst->pasteout_data,
1734 inst->pasteout_data_len);
1737 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1740 struct gui_data *inst = (struct gui_data *)data;
1742 term_deselect(inst->term);
1743 if (inst->pasteout_data)
1744 sfree(inst->pasteout_data);
1745 if (inst->pasteout_data_ctext)
1746 sfree(inst->pasteout_data_ctext);
1747 if (inst->pasteout_data_utf8)
1748 sfree(inst->pasteout_data_utf8);
1749 inst->pasteout_data = NULL;
1750 inst->pasteout_data_len = 0;
1751 inst->pasteout_data_ctext = NULL;
1752 inst->pasteout_data_ctext_len = 0;
1753 inst->pasteout_data_utf8 = NULL;
1754 inst->pasteout_data_utf8_len = 0;
1758 void request_paste(void *frontend)
1760 struct gui_data *inst = (struct gui_data *)frontend;
1762 * In Unix, pasting is asynchronous: all we can do at the
1763 * moment is to call gtk_selection_convert(), and when the data
1764 * comes back _then_ we can call term_do_paste().
1767 if (!inst->direct_to_font) {
1769 * First we attempt to retrieve the selection as a UTF-8
1770 * string (which we will convert to the correct code page
1771 * before sending to the session, of course). If that
1772 * fails, selection_received() will be informed and will
1773 * fall back to an ordinary string.
1775 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1777 inst->input_event_time);
1780 * If we're in direct-to-font mode, we disable UTF-8
1781 * pasting, and go straight to ordinary string data.
1783 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1784 GDK_SELECTION_TYPE_STRING,
1785 inst->input_event_time);
1789 gint idle_paste_func(gpointer data); /* forward ref */
1791 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1792 guint time, gpointer data)
1794 struct gui_data *inst = (struct gui_data *)data;
1798 int length, count, ret;
1799 int free_list_required = 0;
1800 int free_required = 0;
1803 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1805 * Failed to get a UTF-8 selection string. Try compound
1808 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1810 inst->input_event_time);
1814 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1816 * Failed to get UTF-8 or compound text. Try an ordinary
1819 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1820 GDK_SELECTION_TYPE_STRING,
1821 inst->input_event_time);
1826 * If we have data, but it's not of a type we can deal with,
1827 * we have to ignore the data.
1829 if (seldata->length > 0 &&
1830 seldata->type != GDK_SELECTION_TYPE_STRING &&
1831 seldata->type != compound_text_atom &&
1832 seldata->type != utf8_string_atom)
1836 * If we have no data, try looking in a cut buffer.
1838 if (seldata->length <= 0) {
1839 text = retrieve_cutbuffer(&length);
1842 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1843 * source, so use that as a de-facto standard. */
1844 charset = CS_ISO8859_1;
1848 * Convert COMPOUND_TEXT into UTF-8.
1850 if (seldata->type == compound_text_atom) {
1851 tp.value = seldata->data;
1852 tp.encoding = (Atom) seldata->type;
1853 tp.format = seldata->format;
1854 tp.nitems = seldata->length;
1855 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1857 if (ret != 0 || count != 1) {
1859 * Compound text failed; fall back to STRING.
1861 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1862 GDK_SELECTION_TYPE_STRING,
1863 inst->input_event_time);
1867 length = strlen(list[0]);
1869 free_list_required = 1;
1871 text = (char *)seldata->data;
1872 length = seldata->length;
1873 charset = (seldata->type == utf8_string_atom ?
1874 CS_UTF8 : inst->ucsdata.line_codepage);
1878 if (inst->pastein_data)
1879 sfree(inst->pastein_data);
1881 inst->pastein_data = snewn(length, wchar_t);
1882 inst->pastein_data_len = length;
1883 inst->pastein_data_len =
1884 mb_to_wc(charset, 0, text, length,
1885 inst->pastein_data, inst->pastein_data_len);
1887 term_do_paste(inst->term);
1889 if (term_paste_pending(inst->term))
1890 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1892 if (free_list_required)
1893 XFreeStringList(list);
1898 gint idle_paste_func(gpointer data)
1900 struct gui_data *inst = (struct gui_data *)data;
1902 if (term_paste_pending(inst->term))
1903 term_paste(inst->term);
1905 gtk_idle_remove(inst->term_paste_idle_id);
1911 void get_clip(void *frontend, wchar_t ** p, int *len)
1913 struct gui_data *inst = (struct gui_data *)frontend;
1916 *p = inst->pastein_data;
1917 *len = inst->pastein_data_len;
1921 static void set_window_titles(struct gui_data *inst)
1924 * We must always call set_icon_name after calling set_title,
1925 * since set_title will write both names. Irritating, but such
1928 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1929 if (!conf_get_int(inst->conf, CONF_win_name_always))
1930 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1933 void set_title(void *frontend, char *title)
1935 struct gui_data *inst = (struct gui_data *)frontend;
1936 sfree(inst->wintitle);
1937 inst->wintitle = dupstr(title);
1938 set_window_titles(inst);
1941 void set_icon(void *frontend, char *title)
1943 struct gui_data *inst = (struct gui_data *)frontend;
1944 sfree(inst->icontitle);
1945 inst->icontitle = dupstr(title);
1946 set_window_titles(inst);
1949 void set_title_and_icon(void *frontend, char *title, char *icon)
1951 struct gui_data *inst = (struct gui_data *)frontend;
1952 sfree(inst->wintitle);
1953 inst->wintitle = dupstr(title);
1954 sfree(inst->icontitle);
1955 inst->icontitle = dupstr(icon);
1956 set_window_titles(inst);
1959 void set_sbar(void *frontend, int total, int start, int page)
1961 struct gui_data *inst = (struct gui_data *)frontend;
1962 if (!conf_get_int(inst->conf, CONF_scrollbar))
1964 inst->sbar_adjust->lower = 0;
1965 inst->sbar_adjust->upper = total;
1966 inst->sbar_adjust->value = start;
1967 inst->sbar_adjust->page_size = page;
1968 inst->sbar_adjust->step_increment = 1;
1969 inst->sbar_adjust->page_increment = page/2;
1970 inst->ignore_sbar = TRUE;
1971 gtk_adjustment_changed(inst->sbar_adjust);
1972 inst->ignore_sbar = FALSE;
1975 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1977 struct gui_data *inst = (struct gui_data *)data;
1979 if (!conf_get_int(inst->conf, CONF_scrollbar))
1981 if (!inst->ignore_sbar)
1982 term_scroll(inst->term, 1, (int)adj->value);
1985 void sys_cursor(void *frontend, int x, int y)
1988 * This is meaningless under X.
1993 * This is still called when mode==BELL_VISUAL, even though the
1994 * visual bell is handled entirely within terminal.c, because we
1995 * may want to perform additional actions on any kind of bell (for
1996 * example, taskbar flashing in Windows).
1998 void do_beep(void *frontend, int mode)
2000 if (mode == BELL_DEFAULT)
2004 int char_width(Context ctx, int uc)
2007 * Under X, any fixed-width font really _is_ fixed-width.
2008 * Double-width characters will be dealt with using a separate
2009 * font. For the moment we can simply return 1.
2011 * FIXME: but is that also true of Pango?
2016 Context get_ctx(void *frontend)
2018 struct gui_data *inst = (struct gui_data *)frontend;
2019 struct draw_ctx *dctx;
2021 if (!inst->area->window)
2024 dctx = snew(struct draw_ctx);
2026 dctx->gc = gdk_gc_new(inst->area->window);
2030 void free_ctx(Context ctx)
2032 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2033 /* struct gui_data *inst = dctx->inst; */
2034 GdkGC *gc = dctx->gc;
2040 * Draw a line of text in the window, at given character
2041 * coordinates, in given attributes.
2043 * We are allowed to fiddle with the contents of `text'.
2045 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2046 unsigned long attr, int lattr)
2048 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2049 struct gui_data *inst = dctx->inst;
2050 GdkGC *gc = dctx->gc;
2051 int ncombining, combining;
2052 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2053 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
2055 if (attr & TATTR_COMBINING) {
2061 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2062 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2063 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2068 if (inst->bold_colour && (attr & ATTR_BOLD)) {
2069 if (nfg < 16) nfg |= 8;
2070 else if (nfg >= 256) nfg |= 1;
2072 if (inst->bold_colour && (attr & ATTR_BLINK)) {
2073 if (nbg < 16) nbg |= 8;
2074 else if (nbg >= 256) nbg |= 1;
2076 if ((attr & TATTR_ACTCURS) && !monochrome) {
2081 fontid = shadow = 0;
2083 if (attr & ATTR_WIDE) {
2090 if ((attr & ATTR_BOLD) && !inst->bold_colour) {
2097 if (!inst->fonts[fontid]) {
2100 * Fall back through font ids with subsets of this one's
2101 * set bits, in order.
2103 for (i = fontid; i-- > 0 ;) {
2105 continue; /* some other bit is set */
2106 if (inst->fonts[i]) {
2111 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2114 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2116 if (x >= inst->term->cols)
2118 if (x + len*2*widefactor > inst->term->cols)
2119 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2127 r.x = x*inst->font_width+inst->window_border;
2128 r.y = y*inst->font_height+inst->window_border;
2129 r.width = rlen*widefactor*inst->font_width;
2130 r.height = inst->font_height;
2131 gdk_gc_set_clip_rectangle(gc, &r);
2134 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
2135 gdk_draw_rectangle(inst->pixmap, gc, 1,
2136 x*inst->font_width+inst->window_border,
2137 y*inst->font_height+inst->window_border,
2138 rlen*widefactor*inst->font_width, inst->font_height);
2140 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2141 for (combining = 0; combining < ncombining; combining++) {
2142 unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid],
2143 x*inst->font_width+inst->window_border,
2144 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2145 text + combining, len, widefactor > 1,
2146 bold, inst->font_width);
2149 if (attr & ATTR_UNDER) {
2150 int uheight = inst->fonts[0]->ascent + 1;
2151 if (uheight >= inst->font_height)
2152 uheight = inst->font_height - 1;
2153 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->window_border,
2154 y*inst->font_height + uheight + inst->window_border,
2155 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2156 y*inst->font_height + uheight + inst->window_border);
2159 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2161 * I can't find any plausible StretchBlt equivalent in the
2162 * X server, so I'm going to do this the slow and painful
2163 * way. This will involve repeated calls to
2164 * gdk_draw_pixmap() to stretch the text horizontally. It's
2165 * O(N^2) in time and O(N) in network bandwidth, but you
2166 * try thinking of a better way. :-(
2169 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2170 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2171 x*inst->font_width+inst->window_border + 2*i,
2172 y*inst->font_height+inst->window_border,
2173 x*inst->font_width+inst->window_border + 2*i+1,
2174 y*inst->font_height+inst->window_border,
2175 len * widefactor * inst->font_width - i, inst->font_height);
2178 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2180 /* Now stretch vertically, in the same way. */
2181 if ((lattr & LATTR_MODE) == LATTR_BOT)
2185 for (i = 0; i < inst->font_height; i+=2) {
2186 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2187 x*inst->font_width+inst->window_border,
2188 y*inst->font_height+inst->window_border+dt*i+db,
2189 x*inst->font_width+inst->window_border,
2190 y*inst->font_height+inst->window_border+dt*(i+1),
2191 len * widefactor * inst->font_width, inst->font_height-i-1);
2197 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2198 unsigned long attr, int lattr)
2200 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2201 struct gui_data *inst = dctx->inst;
2202 GdkGC *gc = dctx->gc;
2205 do_text_internal(ctx, x, y, text, len, attr, lattr);
2207 if (attr & ATTR_WIDE) {
2213 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2215 if (x >= inst->term->cols)
2217 if (x + len*2*widefactor > inst->term->cols)
2218 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2222 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2223 x*inst->font_width+inst->window_border,
2224 y*inst->font_height+inst->window_border,
2225 x*inst->font_width+inst->window_border,
2226 y*inst->font_height+inst->window_border,
2227 len*widefactor*inst->font_width, inst->font_height);
2230 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2231 unsigned long attr, int lattr)
2233 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2234 struct gui_data *inst = dctx->inst;
2235 GdkGC *gc = dctx->gc;
2237 int active, passive, widefactor;
2239 if (attr & TATTR_PASCURS) {
2240 attr &= ~TATTR_PASCURS;
2244 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
2245 attr &= ~TATTR_ACTCURS;
2249 do_text_internal(ctx, x, y, text, len, attr, lattr);
2251 if (attr & TATTR_COMBINING)
2254 if (attr & ATTR_WIDE) {
2260 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2262 if (x >= inst->term->cols)
2264 if (x + len*2*widefactor > inst->term->cols)
2265 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2269 if (inst->cursor_type == 0) {
2271 * An active block cursor will already have been done by
2272 * the above do_text call, so we only need to do anything
2276 gdk_gc_set_foreground(gc, &inst->cols[261]);
2277 gdk_draw_rectangle(inst->pixmap, gc, 0,
2278 x*inst->font_width+inst->window_border,
2279 y*inst->font_height+inst->window_border,
2280 len*widefactor*inst->font_width-1, inst->font_height-1);
2284 int startx, starty, dx, dy, length, i;
2288 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2289 char_width = 2*inst->font_width;
2291 char_width = inst->font_width;
2293 if (inst->cursor_type == 1) {
2294 uheight = inst->fonts[0]->ascent + 1;
2295 if (uheight >= inst->font_height)
2296 uheight = inst->font_height - 1;
2298 startx = x * inst->font_width + inst->window_border;
2299 starty = y * inst->font_height + inst->window_border + uheight;
2302 length = len * widefactor * char_width;
2305 if (attr & TATTR_RIGHTCURS)
2306 xadjust = char_width - 1;
2307 startx = x * inst->font_width + inst->window_border + xadjust;
2308 starty = y * inst->font_height + inst->window_border;
2311 length = inst->font_height;
2314 gdk_gc_set_foreground(gc, &inst->cols[261]);
2316 for (i = 0; i < length; i++) {
2318 gdk_draw_point(inst->pixmap, gc, startx, starty);
2323 } else if (active) {
2324 gdk_draw_line(inst->pixmap, gc, startx, starty,
2325 startx + (length-1) * dx, starty + (length-1) * dy);
2326 } /* else no cursor (e.g., blinked off) */
2329 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2330 x*inst->font_width+inst->window_border,
2331 y*inst->font_height+inst->window_border,
2332 x*inst->font_width+inst->window_border,
2333 y*inst->font_height+inst->window_border,
2334 len*widefactor*inst->font_width, inst->font_height);
2337 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2340 * Truly hideous hack: GTK doesn't allow us to set the mouse
2341 * cursor foreground and background colours unless we've _also_
2342 * created our own cursor from bitmaps. Therefore, I need to
2343 * load the `cursor' font and draw glyphs from it on to
2344 * pixmaps, in order to construct my cursors with the fg and bg
2345 * I want. This is a gross hack, but it's more self-contained
2346 * than linking in Xlib to find the X window handle to
2347 * inst->area and calling XRecolorCursor, and it's more
2348 * futureproof than hard-coding the shapes as bitmap arrays.
2350 static GdkFont *cursor_font = NULL;
2351 GdkPixmap *source, *mask;
2353 GdkColor cfg = { 0, 65535, 65535, 65535 };
2354 GdkColor cbg = { 0, 0, 0, 0 };
2355 GdkColor dfg = { 1, 65535, 65535, 65535 };
2356 GdkColor dbg = { 0, 0, 0, 0 };
2359 gint lb, rb, wid, asc, desc, w, h, x, y;
2361 if (cursor_val == -2) {
2362 gdk_font_unref(cursor_font);
2366 if (cursor_val >= 0 && !cursor_font) {
2367 cursor_font = gdk_font_load("cursor");
2369 gdk_font_ref(cursor_font);
2373 * Get the text extent of the cursor in question. We use the
2374 * mask character for this, because it's typically slightly
2375 * bigger than the main character.
2377 if (cursor_val >= 0) {
2379 text[0] = (char)cursor_val + 1;
2380 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2381 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2387 source = gdk_pixmap_new(NULL, w, h, 1);
2388 mask = gdk_pixmap_new(NULL, w, h, 1);
2391 * Draw the mask character on the mask pixmap.
2393 gc = gdk_gc_new(mask);
2394 gdk_gc_set_foreground(gc, &dbg);
2395 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2396 if (cursor_val >= 0) {
2398 text[0] = (char)cursor_val + 1;
2399 gdk_gc_set_foreground(gc, &dfg);
2400 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2405 * Draw the main character on the source pixmap.
2407 gc = gdk_gc_new(source);
2408 gdk_gc_set_foreground(gc, &dbg);
2409 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2410 if (cursor_val >= 0) {
2412 text[0] = (char)cursor_val;
2413 gdk_gc_set_foreground(gc, &dfg);
2414 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2419 * Create the cursor.
2421 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2426 gdk_pixmap_unref(source);
2427 gdk_pixmap_unref(mask);
2432 void modalfatalbox(char *p, ...)
2435 fprintf(stderr, "FATAL ERROR: ");
2437 vfprintf(stderr, p, ap);
2439 fputc('\n', stderr);
2443 void cmdline_error(char *p, ...)
2446 fprintf(stderr, "%s: ", appname);
2448 vfprintf(stderr, p, ap);
2450 fputc('\n', stderr);
2454 char *get_x_display(void *frontend)
2456 return gdk_get_display();
2459 long get_windowid(void *frontend)
2461 struct gui_data *inst = (struct gui_data *)frontend;
2462 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2465 static void help(FILE *fp) {
2467 "pterm option summary:\n"
2469 " --display DISPLAY Specify X display to use (note '--')\n"
2470 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2471 " -fn FONT Normal text font\n"
2472 " -fb FONT Bold text font\n"
2473 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2474 " -sl LINES Number of lines of scrollback\n"
2475 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2476 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2477 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2478 " -T TITLE Window title\n"
2479 " -ut, +ut Do(default) or do not update utmp\n"
2480 " -ls, +ls Do(default) or do not make shell a login shell\n"
2481 " -sb, +sb Do(default) or do not display a scrollbar\n"
2482 " -log PATH Log all output to a file\n"
2483 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2484 " -xrm RESOURCE-STRING Set an X resource\n"
2485 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2486 ) < 0 || fflush(fp) < 0) {
2487 perror("output error");
2492 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2493 struct gui_data *inst, Conf *conf)
2499 * Macros to make argument handling easier. Note that because
2500 * they need to call `continue', they cannot be contained in
2501 * the usual do {...} while (0) wrapper to make them
2502 * syntactically single statements; hence it is not legal to
2503 * use one of these macros as an unbraced statement between
2506 #define EXPECTS_ARG { \
2507 if (--argc <= 0) { \
2509 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2514 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2516 while (--argc > 0) {
2521 * Shameless cheating. Debian requires all X terminal
2522 * emulators to support `-T title'; but
2523 * cmdline_process_param will eat -T (it means no-pty) and
2524 * complain that pterm doesn't support it. So, in pterm
2525 * only, we convert -T into -title.
2527 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2531 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2532 do_everything ? 1 : -1, conf);
2535 cmdline_error("option \"%s\" requires an argument", p);
2536 } else if (ret == 2) {
2537 --argc, ++argv; /* skip next argument */
2539 } else if (ret == 1) {
2543 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2547 strncpy(fs.name, val, sizeof(fs.name));
2548 fs.name[sizeof(fs.name)-1] = '\0';
2549 conf_set_fontspec(conf, CONF_font, &fs);
2551 } else if (!strcmp(p, "-fb")) {
2555 strncpy(fs.name, val, sizeof(fs.name));
2556 fs.name[sizeof(fs.name)-1] = '\0';
2557 conf_set_fontspec(conf, CONF_boldfont, &fs);
2559 } else if (!strcmp(p, "-fw")) {
2563 strncpy(fs.name, val, sizeof(fs.name));
2564 fs.name[sizeof(fs.name)-1] = '\0';
2565 conf_set_fontspec(conf, CONF_widefont, &fs);
2567 } else if (!strcmp(p, "-fwb")) {
2571 strncpy(fs.name, val, sizeof(fs.name));
2572 fs.name[sizeof(fs.name)-1] = '\0';
2573 conf_set_fontspec(conf, CONF_wideboldfont, &fs);
2575 } else if (!strcmp(p, "-cs")) {
2578 conf_set_str(conf, CONF_line_codepage, val);
2580 } else if (!strcmp(p, "-geometry")) {
2586 flags = XParseGeometry(val, &x, &y, &w, &h);
2587 if (flags & WidthValue)
2588 conf_set_int(conf, CONF_width, w);
2589 if (flags & HeightValue)
2590 conf_set_int(conf, CONF_height, h);
2592 if (flags & (XValue | YValue)) {
2595 inst->gotpos = TRUE;
2596 inst->gravity = ((flags & XNegative ? 1 : 0) |
2597 (flags & YNegative ? 2 : 0));
2600 } else if (!strcmp(p, "-sl")) {
2603 conf_set_int(conf, CONF_savelines, atoi(val));
2605 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2606 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2607 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2612 if (!gdk_color_parse(val, &col)) {
2614 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2618 index = (!strcmp(p, "-fg") ? 0 :
2619 !strcmp(p, "-bg") ? 2 :
2620 !strcmp(p, "-bfg") ? 1 :
2621 !strcmp(p, "-bbg") ? 3 :
2622 !strcmp(p, "-cfg") ? 4 :
2623 !strcmp(p, "-cbg") ? 5 : -1);
2624 assert(index != -1);
2625 conf_set_int_int(conf, CONF_colours, index*3+0, col.red / 256);
2626 conf_set_int_int(conf, CONF_colours, index*3+1,col.green/ 256);
2627 conf_set_int_int(conf, CONF_colours, index*3+2, col.blue/ 256);
2630 } else if (use_pty_argv && !strcmp(p, "-e")) {
2631 /* This option swallows all further arguments. */
2637 pty_argv = snewn(argc+1, char *);
2639 for (i = 0; i < argc; i++)
2640 pty_argv[i] = argv[i];
2641 pty_argv[argc] = NULL;
2642 break; /* finished command-line processing */
2644 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2647 } else if (!strcmp(p, "-title")) {
2650 conf_set_str(conf, CONF_wintitle, val);
2652 } else if (!strcmp(p, "-log")) {
2656 strncpy(fn.path, val, sizeof(fn.path));
2657 fn.path[sizeof(fn.path)-1] = '\0';
2658 conf_set_filename(conf, CONF_logfilename, &fn);
2659 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
2661 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2663 conf_set_int(conf, CONF_stamp_utmp, 0);
2665 } else if (!strcmp(p, "-ut")) {
2667 conf_set_int(conf, CONF_stamp_utmp, 1);
2669 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2671 conf_set_int(conf, CONF_login_shell, 0);
2673 } else if (!strcmp(p, "-ls")) {
2675 conf_set_int(conf, CONF_login_shell, 1);
2677 } else if (!strcmp(p, "-nethack")) {
2679 conf_set_int(conf, CONF_nethack_keypad, 1);
2681 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2683 conf_set_int(conf, CONF_scrollbar, 0);
2685 } else if (!strcmp(p, "-sb")) {
2687 conf_set_int(conf, CONF_scrollbar, 1);
2689 } else if (!strcmp(p, "-name")) {
2693 } else if (!strcmp(p, "-xrm")) {
2695 provide_xrm_string(val);
2697 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2701 } else if (!strcmp(p, "-pgpfp")) {
2705 } else if(p[0] != '-' && (!do_everything ||
2706 process_nonoption_arg(p, conf,
2712 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2719 int uxsel_input_add(int fd, int rwx) {
2721 if (rwx & 1) flags |= GDK_INPUT_READ;
2722 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2723 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2725 return gdk_input_add(fd, flags, fd_input_func, NULL);
2728 void uxsel_input_remove(int id) {
2729 gdk_input_remove(id);
2732 void setup_fonts_ucs(struct gui_data *inst)
2734 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
2735 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
2739 unifont_destroy(inst->fonts[0]);
2741 unifont_destroy(inst->fonts[1]);
2743 unifont_destroy(inst->fonts[2]);
2745 unifont_destroy(inst->fonts[3]);
2747 fs = conf_get_fontspec(inst->conf, CONF_font);
2748 inst->fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
2749 shadowboldoffset, shadowbold);
2750 if (!inst->fonts[0]) {
2751 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2756 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
2757 if (shadowbold || !fs->name[0]) {
2758 inst->fonts[1] = NULL;
2760 inst->fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
2761 shadowboldoffset, shadowbold);
2762 if (!inst->fonts[1]) {
2763 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2769 fs = conf_get_fontspec(inst->conf, CONF_widefont);
2771 inst->fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
2772 shadowboldoffset, shadowbold);
2773 if (!inst->fonts[2]) {
2774 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2779 inst->fonts[2] = NULL;
2782 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
2783 if (shadowbold || !fs->name[0]) {
2784 inst->fonts[3] = NULL;
2786 inst->fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
2787 shadowboldoffset, shadowbold);
2788 if (!inst->fonts[3]) {
2789 fprintf(stderr, "%s: unable to load wide bold font \"%s\"\n", appname,
2795 inst->font_width = inst->fonts[0]->width;
2796 inst->font_height = inst->fonts[0]->height;
2798 inst->direct_to_font = init_ucs(&inst->ucsdata,
2799 conf_get_str(inst->conf, CONF_line_codepage),
2800 conf_get_int(inst->conf, CONF_utf8_override),
2801 inst->fonts[0]->public_charset,
2802 conf_get_int(inst->conf, CONF_vtmode));
2805 void set_geom_hints(struct gui_data *inst)
2808 geom.min_width = inst->font_width + 2*inst->window_border;
2809 geom.min_height = inst->font_height + 2*inst->window_border;
2810 geom.max_width = geom.max_height = -1;
2811 geom.base_width = 2*inst->window_border;
2812 geom.base_height = 2*inst->window_border;
2813 geom.width_inc = inst->font_width;
2814 geom.height_inc = inst->font_height;
2815 geom.min_aspect = geom.max_aspect = 0;
2816 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2817 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2818 GDK_HINT_RESIZE_INC);
2821 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2823 struct gui_data *inst = (struct gui_data *)data;
2824 term_clrsb(inst->term);
2827 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2829 struct gui_data *inst = (struct gui_data *)data;
2830 term_pwron(inst->term, TRUE);
2832 ldisc_send(inst->ldisc, NULL, 0, 0);
2835 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2837 struct gui_data *inst = (struct gui_data *)data;
2838 term_copyall(inst->term);
2841 void special_menuitem(GtkMenuItem *item, gpointer data)
2843 struct gui_data *inst = (struct gui_data *)data;
2844 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2848 inst->back->special(inst->backhandle, code);
2851 void about_menuitem(GtkMenuItem *item, gpointer data)
2853 struct gui_data *inst = (struct gui_data *)data;
2854 about_box(inst->window);
2857 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2859 struct gui_data *inst = (struct gui_data *)data;
2860 showeventlog(inst->eventlogstuff, inst->window);
2863 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2865 /* This maps colour indices in inst->conf to those used in inst->cols. */
2866 static const int ww[] = {
2867 256, 257, 258, 259, 260, 261,
2868 0, 8, 1, 9, 2, 10, 3, 11,
2869 4, 12, 5, 13, 6, 14, 7, 15
2871 struct gui_data *inst = (struct gui_data *)data;
2872 char *title = dupcat(appname, " Reconfiguration", NULL);
2873 Conf *oldconf, *newconf;
2874 int i, j, need_size;
2876 assert(lenof(ww) == NCFGCOLOURS);
2878 if (inst->reconfiguring)
2881 inst->reconfiguring = TRUE;
2883 oldconf = inst->conf;
2884 newconf = conf_copy(inst->conf);
2886 if (do_config_box(title, newconf, 1,
2887 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2888 inst->conf = newconf;
2890 /* Pass new config data to the logging module */
2891 log_reconfig(inst->logctx, inst->conf);
2893 * Flush the line discipline's edit buffer in the case
2894 * where local editing has just been disabled.
2896 ldisc_configure(inst->ldisc, inst->conf);
2898 ldisc_send(inst->ldisc, NULL, 0, 0);
2899 /* Pass new config data to the terminal */
2900 term_reconfig(inst->term, inst->conf);
2901 /* Pass new config data to the back end */
2903 inst->back->reconfig(inst->backhandle, inst->conf);
2905 cache_conf_values(inst);
2908 * Just setting inst->conf is sufficient to cause colour
2909 * setting changes to appear on the next ESC]R palette
2910 * reset. But we should also check whether any colour
2911 * settings have been changed, and revert the ones that have
2912 * to the new default, on the assumption that the user is
2913 * most likely to want an immediate update.
2915 for (i = 0; i < NCFGCOLOURS; i++) {
2916 for (j = 0; j < 3; j++)
2917 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
2918 conf_get_int_int(newconf, CONF_colours, i*3+j))
2921 real_palette_set(inst, ww[i],
2922 conf_get_int_int(newconf,CONF_colours,i*3+0),
2923 conf_get_int_int(newconf,CONF_colours,i*3+1),
2924 conf_get_int_int(newconf,CONF_colours,i*3+2));
2927 * If the default background has changed, we must
2928 * repaint the space in between the window border
2929 * and the text area.
2932 set_window_background(inst);
2933 draw_backing_rect(inst);
2939 * If the scrollbar needs to be shown, hidden, or moved
2940 * from one end to the other of the window, do so now.
2942 if (conf_get_int(oldconf, CONF_scrollbar) !=
2943 conf_get_int(newconf, CONF_scrollbar)) {
2944 if (conf_get_int(newconf, CONF_scrollbar))
2945 gtk_widget_show(inst->sbar);
2947 gtk_widget_hide(inst->sbar);
2949 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
2950 conf_get_int(newconf, CONF_scrollbar_on_left)) {
2951 gtk_box_reorder_child(inst->hbox, inst->sbar,
2952 conf_get_int(newconf, CONF_scrollbar_on_left)
2957 * Change the window title, if required.
2959 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
2960 conf_get_str(newconf, CONF_wintitle)))
2961 set_title(inst, conf_get_str(newconf, CONF_wintitle));
2962 set_window_titles(inst);
2965 * Redo the whole tangled fonts and Unicode mess if
2968 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
2969 conf_get_fontspec(newconf, CONF_font)->name) ||
2970 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
2971 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
2972 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
2973 conf_get_fontspec(newconf, CONF_widefont)->name) ||
2974 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
2975 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
2976 strcmp(conf_get_str(oldconf, CONF_line_codepage),
2977 conf_get_str(newconf, CONF_line_codepage)) ||
2978 conf_get_int(oldconf, CONF_vtmode) !=
2979 conf_get_int(newconf, CONF_vtmode) ||
2980 conf_get_int(oldconf, CONF_shadowbold) !=
2981 conf_get_int(newconf, CONF_shadowbold) ||
2982 conf_get_int(oldconf, CONF_shadowboldoffset) !=
2983 conf_get_int(newconf, CONF_shadowboldoffset)) {
2984 setup_fonts_ucs(inst);
2990 * Resize the window.
2992 if (conf_get_int(oldconf, CONF_width) !=
2993 conf_get_int(newconf, CONF_width) ||
2994 conf_get_int(oldconf, CONF_height) !=
2995 conf_get_int(newconf, CONF_height) ||
2996 conf_get_int(oldconf, CONF_window_border) !=
2997 conf_get_int(newconf, CONF_window_border) ||
2999 set_geom_hints(inst);
3000 request_resize(inst, conf_get_int(newconf, CONF_width),
3001 conf_get_int(newconf, CONF_height));
3004 * The above will have caused a call to term_size() for
3005 * us if it happened. If the user has fiddled with only
3006 * the scrollback size, the above will not have
3007 * happened and we will need an explicit term_size()
3010 if (conf_get_int(oldconf, CONF_savelines) !=
3011 conf_get_int(newconf, CONF_savelines))
3012 term_size(inst->term, inst->term->rows, inst->term->cols,
3013 conf_get_int(newconf, CONF_savelines));
3016 term_invalidate(inst->term);
3019 * We do an explicit full redraw here to ensure the window
3020 * border has been redrawn as well as the text area.
3022 gtk_widget_queue_draw(inst->area);
3029 inst->reconfiguring = FALSE;
3032 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3035 * Re-execing ourself is not an exact science under Unix. I do
3036 * the best I can by using /proc/self/exe if available and by
3037 * assuming argv[0] can be found on $PATH if not.
3039 * Note that we also have to reconstruct the elements of the
3040 * original argv which gtk swallowed, since the user wants the
3041 * new session to appear on the same X display as the old one.
3049 * Collect the arguments with which to re-exec ourself.
3051 va_start(ap, fd_to_close);
3052 n = 2; /* progname and terminating NULL */
3053 n += inst->ngtkargs;
3054 while (va_arg(ap, char *) != NULL)
3058 args = snewn(n, char *);
3059 args[0] = inst->progname;
3061 for (i = 0; i < inst->ngtkargs; i++)
3062 args[i+1] = inst->gtkargvstart[i];
3065 va_start(ap, fd_to_close);
3066 while ((args[i++] = va_arg(ap, char *)) != NULL);
3072 * Do the double fork.
3085 } else if (pid2 > 0) {
3087 * First child has successfully forked second child. My
3088 * Work Here Is Done. Note the use of _exit rather than
3089 * exit: the latter appears to cause destroy messages
3090 * to be sent to the X server. I suspect gtk uses
3097 * If we reach here, we are the second child, so we now
3098 * actually perform the exec.
3100 if (fd_to_close >= 0)
3103 execv("/proc/self/exe", args);
3104 execvp(inst->progname, args);
3110 waitpid(pid, &status, 0);
3115 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3117 struct gui_data *inst = (struct gui_data *)gdata;
3119 * For this feature we must marshal conf and (possibly) pty_argv
3120 * into a byte stream, create a pipe, and send this byte stream
3121 * to the child through the pipe.
3123 int i, ret, sersize, size;
3128 if (pipe(pipefd) < 0) {
3133 size = sersize = conf_serialised_size(inst->conf);
3134 if (use_pty_argv && pty_argv) {
3135 for (i = 0; pty_argv[i]; i++)
3136 size += strlen(pty_argv[i]) + 1;
3139 data = snewn(size, char);
3140 conf_serialise(inst->conf, data);
3141 if (use_pty_argv && pty_argv) {
3143 for (i = 0; pty_argv[i]; i++) {
3144 strcpy(data + p, pty_argv[i]);
3145 p += strlen(pty_argv[i]) + 1;
3150 sprintf(option, "---[%d,%d]", pipefd[0], size);
3151 fcntl(pipefd[0], F_SETFD, 0);
3152 fork_and_exec_self(inst, pipefd[1], option, NULL);
3156 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3159 perror("write to pipe");
3164 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
3166 int fd, i, ret, size, size_used;
3169 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3170 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3174 data = snewn(size, char);
3176 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3179 perror("read from pipe");
3181 } else if (i < size) {
3182 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3187 size_used = conf_deserialise(conf, data, size);
3188 if (use_pty_argv && size > size_used) {
3192 while (i < size && data[i]) i++;
3194 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3201 pty_argv = snewn(n+1, char *);
3207 while (i < size && data[i]) i++;
3210 pty_argv[n++] = dupstr(p);
3217 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3219 struct gui_data *inst = (struct gui_data *)data;
3221 fork_and_exec_self(inst, -1, NULL);
3224 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3226 struct gui_data *inst = (struct gui_data *)data;
3229 logevent(inst, "----- Session restarted -----");
3230 term_pwron(inst->term, FALSE);
3231 start_backend(inst);
3232 inst->exited = FALSE;
3236 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3238 struct gui_data *inst = (struct gui_data *)data;
3239 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3241 fork_and_exec_self(inst, -1, "-load", str, NULL);
3244 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3246 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3251 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3253 struct gui_data *inst = (struct gui_data *)data;
3254 struct sesslist sesslist;
3257 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3258 (GtkCallback)gtk_widget_destroy, NULL);
3260 get_sesslist(&sesslist, TRUE);
3261 /* skip sesslist.sessions[0] == Default Settings */
3262 for (i = 1; i < sesslist.nsessions; i++) {
3263 GtkWidget *menuitem =
3264 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3265 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3266 gtk_widget_show(menuitem);
3267 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3268 dupstr(sesslist.sessions[i]));
3269 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3270 GTK_SIGNAL_FUNC(saved_session_menuitem),
3272 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3273 GTK_SIGNAL_FUNC(saved_session_freedata),
3276 if (sesslist.nsessions <= 1) {
3277 GtkWidget *menuitem =
3278 gtk_menu_item_new_with_label("(No sessions)");
3279 gtk_widget_set_sensitive(menuitem, FALSE);
3280 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3281 gtk_widget_show(menuitem);
3283 get_sesslist(&sesslist, FALSE); /* free up */
3286 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3290 GdkBitmap *iconmask;
3291 #if GTK_CHECK_VERSION(2,0,0)
3299 gtk_widget_realize(window);
3300 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3301 NULL, (gchar **)icon[0]);
3302 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3304 #if GTK_CHECK_VERSION(2,0,0)
3306 for (n = 0; n < n_icon; n++) {
3308 g_list_append(iconlist,
3309 gdk_pixbuf_new_from_xpm_data((const gchar **)
3312 gdk_window_set_icon_list(window->window, iconlist);
3316 void update_specials_menu(void *frontend)
3318 struct gui_data *inst = (struct gui_data *)frontend;
3320 const struct telnet_special *specials;
3323 specials = inst->back->get_specials(inst->backhandle);
3327 /* I believe this disposes of submenus too. */
3328 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3329 (GtkCallback)gtk_widget_destroy, NULL);
3332 GtkWidget *menu = inst->specialsmenu;
3333 /* A lame "stack" for submenus that will do for now. */
3334 GtkWidget *saved_menu = NULL;
3336 for (i = 0; nesting > 0; i++) {
3337 GtkWidget *menuitem = NULL;
3338 switch (specials[i].code) {
3340 assert (nesting < 2);
3341 saved_menu = menu; /* XXX lame stacking */
3342 menu = gtk_menu_new();
3343 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3344 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3345 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3346 gtk_widget_show(menuitem);
3353 menu = saved_menu; /* XXX lame stacking */
3358 menuitem = gtk_menu_item_new();
3361 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3362 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3363 GINT_TO_POINTER(specials[i].code));
3364 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3365 GTK_SIGNAL_FUNC(special_menuitem), inst);
3369 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3370 gtk_widget_show(menuitem);
3373 gtk_widget_show(inst->specialsitem1);
3374 gtk_widget_show(inst->specialsitem2);
3376 gtk_widget_hide(inst->specialsitem1);
3377 gtk_widget_hide(inst->specialsitem2);
3381 static void start_backend(struct gui_data *inst)
3383 extern Backend *select_backend(Conf *conf);
3388 inst->back = select_backend(inst->conf);
3390 error = inst->back->init((void *)inst, &inst->backhandle,
3392 conf_get_str(inst->conf, CONF_host),
3393 conf_get_int(inst->conf, CONF_port),
3395 conf_get_int(inst->conf, CONF_tcp_nodelay),
3396 conf_get_int(inst->conf, CONF_tcp_keepalives));
3399 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3400 conf_get_str(inst->conf, CONF_host), error);
3401 inst->exited = TRUE;
3402 fatal_message_box(inst->window, msg);
3407 s = conf_get_str(inst->conf, CONF_wintitle);
3409 set_title_and_icon(inst, s, s);
3411 char *title = make_default_wintitle(realhost);
3412 set_title_and_icon(inst, title, title);
3417 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3419 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3422 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
3425 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3428 int pt_main(int argc, char **argv)
3430 extern int cfgbox(Conf *conf);
3431 struct gui_data *inst;
3434 * Create an instance structure and initialise to zeroes
3436 inst = snew(struct gui_data);
3437 memset(inst, 0, sizeof(*inst));
3438 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3439 inst->busy_status = BUSY_NOT;
3440 inst->conf = conf_new();
3441 inst->wintitle = inst->icontitle = NULL;
3443 /* defer any child exit handling until we're ready to deal with
3445 block_signal(SIGCHLD, 1);
3447 inst->progname = argv[0];
3449 * Copy the original argv before letting gtk_init fiddle with
3450 * it. It will be required later.
3454 inst->gtkargvstart = snewn(argc-1, char *);
3455 for (i = 1; i < argc; i++)
3456 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3458 gtk_init(&argc, &argv);
3459 inst->ngtkargs = oldargc - argc;
3462 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3463 read_dupsession_data(inst, inst->conf, argv[1]);
3464 /* Splatter this argument so it doesn't clutter a ps listing */
3465 memset(argv[1], 0, strlen(argv[1]));
3467 /* By default, we bring up the config dialog, rather than launching
3468 * a session. This gets set to TRUE if something happens to change
3469 * that (e.g., a hostname is specified on the command-line). */
3470 int allow_launch = FALSE;
3471 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
3472 exit(1); /* pre-defaults pass to get -class */
3473 do_defaults(NULL, inst->conf);
3474 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
3475 exit(1); /* post-defaults, do everything */
3477 cmdline_run_saved(inst->conf);
3480 allow_launch = TRUE;
3482 if ((!allow_launch || !conf_launchable(inst->conf)) &&
3483 !cfgbox(inst->conf))
3484 exit(0); /* config box hit Cancel */
3487 if (!compound_text_atom)
3488 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3489 if (!utf8_string_atom)
3490 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3492 inst->area = gtk_drawing_area_new();
3494 setup_fonts_ucs(inst);
3497 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3499 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
3501 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
3502 winclass, winclass);
3506 * Set up the colour map.
3508 palette_reset(inst);
3510 inst->width = conf_get_int(inst->conf, CONF_width);
3511 inst->height = conf_get_int(inst->conf, CONF_height);
3512 cache_conf_values(inst);
3514 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3515 inst->font_width * inst->width + 2*inst->window_border,
3516 inst->font_height * inst->height + 2*inst->window_border);
3517 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3518 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3519 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3521 * We always create the scrollbar; it remains invisible if
3522 * unwanted, so we can pop it up quickly if it suddenly becomes
3525 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
3526 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3527 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3528 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
3529 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3531 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3533 set_geom_hints(inst);
3535 gtk_widget_show(inst->area);
3536 if (conf_get_int(inst->conf, CONF_scrollbar))
3537 gtk_widget_show(inst->sbar);
3539 gtk_widget_hide(inst->sbar);
3540 gtk_widget_show(GTK_WIDGET(inst->hbox));
3543 int x = inst->xpos, y = inst->ypos;
3545 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3546 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3547 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3548 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3549 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3552 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3553 GTK_SIGNAL_FUNC(destroy), inst);
3554 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3555 GTK_SIGNAL_FUNC(delete_window), inst);
3556 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3557 GTK_SIGNAL_FUNC(key_event), inst);
3558 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3559 GTK_SIGNAL_FUNC(key_event), inst);
3560 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3561 GTK_SIGNAL_FUNC(focus_event), inst);
3562 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3563 GTK_SIGNAL_FUNC(focus_event), inst);
3564 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3565 GTK_SIGNAL_FUNC(configure_area), inst);
3566 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3567 GTK_SIGNAL_FUNC(expose_area), inst);
3568 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3569 GTK_SIGNAL_FUNC(button_event), inst);
3570 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3571 GTK_SIGNAL_FUNC(button_event), inst);
3572 #if GTK_CHECK_VERSION(2,0,0)
3573 gtk_signal_connect(GTK_OBJECT(inst->area), "scroll_event",
3574 GTK_SIGNAL_FUNC(scroll_event), inst);
3576 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3577 GTK_SIGNAL_FUNC(motion_event), inst);
3578 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3579 GTK_SIGNAL_FUNC(selection_received), inst);
3580 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3581 GTK_SIGNAL_FUNC(selection_get), inst);
3582 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3583 GTK_SIGNAL_FUNC(selection_clear), inst);
3584 if (conf_get_int(inst->conf, CONF_scrollbar))
3585 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3586 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3587 gtk_widget_add_events(GTK_WIDGET(inst->area),
3588 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3589 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3590 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3593 extern const char *const *const main_icon[];
3594 extern const int n_main_icon;
3595 set_window_icon(inst->window, main_icon, n_main_icon);
3598 gtk_widget_show(inst->window);
3600 set_window_background(inst);
3603 * Set up the Ctrl+rightclick context menu.
3606 GtkWidget *menuitem;
3608 extern const int use_event_log, new_session, saved_sessions;
3610 inst->menu = gtk_menu_new();
3612 #define MKMENUITEM(title, func) do \
3614 menuitem = gtk_menu_item_new_with_label(title); \
3615 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3616 gtk_widget_show(menuitem); \
3617 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3618 GTK_SIGNAL_FUNC(func), inst); \
3621 #define MKSUBMENU(title) do \
3623 menuitem = gtk_menu_item_new_with_label(title); \
3624 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3625 gtk_widget_show(menuitem); \
3628 #define MKSEP() do \
3630 menuitem = gtk_menu_item_new(); \
3631 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3632 gtk_widget_show(menuitem); \
3636 MKMENUITEM("New Session...", new_session_menuitem);
3637 MKMENUITEM("Restart Session", restart_session_menuitem);
3638 inst->restartitem = menuitem;
3639 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3640 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3641 if (saved_sessions) {
3642 inst->sessionsmenu = gtk_menu_new();
3643 /* sessionsmenu will be updated when it's invoked */
3644 /* XXX is this the right way to do dynamic menus in Gtk? */
3645 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3646 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3647 inst->sessionsmenu);
3650 MKMENUITEM("Change Settings...", change_settings_menuitem);
3653 MKMENUITEM("Event Log", event_log_menuitem);
3654 MKSUBMENU("Special Commands");
3655 inst->specialsmenu = gtk_menu_new();
3656 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3657 inst->specialsitem1 = menuitem;
3659 inst->specialsitem2 = menuitem;
3660 gtk_widget_hide(inst->specialsitem1);
3661 gtk_widget_hide(inst->specialsitem2);
3662 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3663 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3664 MKMENUITEM("Copy All", copy_all_menuitem);
3666 s = dupcat("About ", appname, NULL);
3667 MKMENUITEM(s, about_menuitem);
3674 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3675 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3676 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3677 inst->blankcursor = make_mouse_ptr(inst, -1);
3678 make_mouse_ptr(inst, -2); /* clean up cursor font */
3679 inst->currcursor = inst->textcursor;
3680 show_mouseptr(inst, 1);
3682 inst->eventlogstuff = eventlogstuff_new();
3684 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
3685 inst->logctx = log_init(inst, inst->conf);
3686 term_provide_logctx(inst->term, inst->logctx);
3690 term_size(inst->term, inst->height, inst->width,
3691 conf_get_int(inst->conf, CONF_savelines));
3693 start_backend(inst);
3695 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3697 /* now we're reday to deal with the child exit handler being
3699 block_signal(SIGCHLD, 0);
3702 * Block SIGPIPE: if we attempt Duplicate Session or similar
3703 * and it falls over in some way, we certainly don't want
3704 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3705 * this _after_ (at least pterm) forks off its child process,
3706 * since the child wants SIGPIPE handled in the usual way.
3708 block_signal(SIGPIPE, 1);
3710 inst->exited = FALSE;