2 * gtkwin.c: the main code that runs a PuTTY terminal emulator and
3 * backend in a GTK window.
19 #include <sys/types.h>
22 #include <gdk/gdkkeysyms.h>
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
28 #if GTK_CHECK_VERSION(2,0,0)
29 #include <gtk/gtkimmodule.h>
32 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
38 #define CAT2(x,y) x ## y
39 #define CAT(x,y) CAT2(x,y)
40 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
42 #if GTK_CHECK_VERSION(2,0,0)
43 ASSERT(sizeof(long) <= sizeof(gsize));
44 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
45 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
47 ASSERT(sizeof(long) <= sizeof(gpointer));
48 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
49 #define GPOINTER_TO_LONG(p) ((long)(p))
52 /* Colours come in two flavours: configurable, and xterm-extended. */
53 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
54 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
56 GdkAtom compound_text_atom, utf8_string_atom;
58 extern char **pty_argv; /* declared in pty.c */
59 extern int use_pty_argv;
62 * Timers are global across all sessions (even if we were handling
63 * multiple sessions, which we aren't), so the current timer ID is
66 static guint timer_id = 0;
69 GtkWidget *window, *area, *sbar;
71 GtkAdjustment *sbar_adjust;
72 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
74 GtkWidget *sessionsmenu;
76 #if GTK_CHECK_VERSION(2,0,0)
79 unifont *fonts[4]; /* normal, bold, wide, widebold */
80 int xpos, ypos, gotpos, gravity;
81 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
82 GdkColor cols[NALLCOLOURS];
84 wchar_t *pastein_data;
87 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
88 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
89 int font_width, font_height;
94 guint term_paste_idle_id;
95 guint term_exit_idle_id;
100 int master_fd, master_func_id;
107 struct unicode_data ucsdata;
110 char *progname, **gtkargvstart;
112 guint32 input_event_time; /* Timestamp of the most recent input event. */
114 /* Cached things out of conf that we refer to a lot */
120 static void cache_conf_values(struct gui_data *inst)
122 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
123 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
124 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
129 struct gui_data *inst;
132 static int send_raw_mouse;
134 static char *app_name = "pterm";
136 static void start_backend(struct gui_data *inst);
138 char *x_get_default(const char *key)
140 return XGetDefault(GDK_DISPLAY(), app_name, key);
143 void connection_fatal(void *frontend, char *p, ...)
145 struct gui_data *inst = (struct gui_data *)frontend;
150 msg = dupvprintf(p, ap);
153 fatal_message_box(inst->window, msg);
155 if (conf_get_int(inst->conf, CONF_close_on_exit) == FORCE_ON)
160 * Default settings that are specific to pterm.
162 FontSpec *platform_default_fontspec(const char *name)
164 if (!strcmp(name, "Font"))
165 return fontspec_new("server:fixed");
167 return fontspec_new("");
170 Filename *platform_default_filename(const char *name)
172 if (!strcmp(name, "LogFileName"))
173 return filename_from_str("putty.log");
175 return filename_from_str("");
178 char *platform_default_s(const char *name)
180 if (!strcmp(name, "SerialLine"))
181 return dupstr("/dev/ttyS0");
185 int platform_default_i(const char *name, int def)
187 if (!strcmp(name, "CloseOnExit"))
188 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
189 if (!strcmp(name, "WinNameAlways"))
190 return 0; /* X natively supports icon titles, so use 'em by default */
194 /* Dummy routine, only required in plink. */
195 void ldisc_update(void *frontend, int echo, int edit)
199 char *get_ttymode(void *frontend, const char *mode)
201 struct gui_data *inst = (struct gui_data *)frontend;
202 return term_get_ttymode(inst->term, mode);
205 int from_backend(void *frontend, int is_stderr, const char *data, int len)
207 struct gui_data *inst = (struct gui_data *)frontend;
208 return term_data(inst->term, is_stderr, data, len);
211 int from_backend_untrusted(void *frontend, const char *data, int len)
213 struct gui_data *inst = (struct gui_data *)frontend;
214 return term_data_untrusted(inst->term, data, len);
217 int from_backend_eof(void *frontend)
219 return TRUE; /* do respond to incoming EOF with outgoing */
222 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
224 struct gui_data *inst = (struct gui_data *)p->frontend;
226 ret = cmdline_get_passwd_input(p, in, inlen);
228 ret = term_get_userpass_input(inst->term, p, in, inlen);
232 void logevent(void *frontend, const char *string)
234 struct gui_data *inst = (struct gui_data *)frontend;
236 log_eventlog(inst->logctx, string);
238 logevent_dlg(inst->eventlogstuff, string);
241 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
243 struct gui_data *inst = (struct gui_data *)frontend;
246 return inst->font_height;
248 return inst->font_width;
252 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
253 * into a cooked one (SELECT, EXTEND, PASTE).
255 * In Unix, this is not configurable; the X button arrangement is
256 * rock-solid across all applications, everyone has a three-button
257 * mouse or a means of faking it, and there is no need to switch
258 * buttons around at all.
260 static Mouse_Button translate_button(Mouse_Button button)
262 /* struct gui_data *inst = (struct gui_data *)frontend; */
264 if (button == MBT_LEFT)
266 if (button == MBT_MIDDLE)
268 if (button == MBT_RIGHT)
270 return 0; /* shouldn't happen */
274 * Return the top-level GtkWindow associated with a particular
275 * front end instance.
277 void *get_window(void *frontend)
279 struct gui_data *inst = (struct gui_data *)frontend;
284 * Minimise or restore the window in response to a server-side
287 void set_iconic(void *frontend, int iconic)
290 * GTK 1.2 doesn't know how to do this.
292 #if GTK_CHECK_VERSION(2,0,0)
293 struct gui_data *inst = (struct gui_data *)frontend;
295 gtk_window_iconify(GTK_WINDOW(inst->window));
297 gtk_window_deiconify(GTK_WINDOW(inst->window));
302 * Move the window in response to a server-side request.
304 void move_window(void *frontend, int x, int y)
306 struct gui_data *inst = (struct gui_data *)frontend;
308 * I assume that when the GTK version of this call is available
309 * we should use it. Not sure how it differs from the GDK one,
312 #if GTK_CHECK_VERSION(2,0,0)
313 gtk_window_move(GTK_WINDOW(inst->window), x, y);
315 gdk_window_move(inst->window->window, x, y);
320 * Move the window to the top or bottom of the z-order in response
321 * to a server-side request.
323 void set_zorder(void *frontend, int top)
325 struct gui_data *inst = (struct gui_data *)frontend;
327 gdk_window_raise(inst->window->window);
329 gdk_window_lower(inst->window->window);
333 * Refresh the window in response to a server-side request.
335 void refresh_window(void *frontend)
337 struct gui_data *inst = (struct gui_data *)frontend;
338 term_invalidate(inst->term);
342 * Maximise or restore the window in response to a server-side
345 void set_zoomed(void *frontend, int zoomed)
348 * GTK 1.2 doesn't know how to do this.
350 #if GTK_CHECK_VERSION(2,0,0)
351 struct gui_data *inst = (struct gui_data *)frontend;
353 gtk_window_maximize(GTK_WINDOW(inst->window));
355 gtk_window_unmaximize(GTK_WINDOW(inst->window));
360 * Report whether the window is iconic, for terminal reports.
362 int is_iconic(void *frontend)
364 struct gui_data *inst = (struct gui_data *)frontend;
365 return !gdk_window_is_viewable(inst->window->window);
369 * Report the window's position, for terminal reports.
371 void get_window_pos(void *frontend, int *x, int *y)
373 struct gui_data *inst = (struct gui_data *)frontend;
375 * I assume that when the GTK version of this call is available
376 * we should use it. Not sure how it differs from the GDK one,
379 #if GTK_CHECK_VERSION(2,0,0)
380 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
382 gdk_window_get_position(inst->window->window, x, y);
387 * Report the window's pixel size, for terminal reports.
389 void get_window_pixels(void *frontend, int *x, int *y)
391 struct gui_data *inst = (struct gui_data *)frontend;
393 * I assume that when the GTK version of this call is available
394 * we should use it. Not sure how it differs from the GDK one,
397 #if GTK_CHECK_VERSION(2,0,0)
398 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
400 gdk_window_get_size(inst->window->window, x, y);
405 * Return the window or icon title.
407 char *get_window_title(void *frontend, int icon)
409 struct gui_data *inst = (struct gui_data *)frontend;
410 return icon ? inst->icontitle : inst->wintitle;
413 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
415 struct gui_data *inst = (struct gui_data *)data;
416 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
417 if (!reallyclose(inst))
423 static void update_mouseptr(struct gui_data *inst)
425 switch (inst->busy_status) {
427 if (!inst->mouseptr_visible) {
428 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
429 } else if (send_raw_mouse) {
430 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
432 gdk_window_set_cursor(inst->area->window, inst->textcursor);
435 case BUSY_WAITING: /* XXX can we do better? */
437 /* We always display these cursors. */
438 gdk_window_set_cursor(inst->area->window, inst->waitcursor);
445 static void show_mouseptr(struct gui_data *inst, int show)
447 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
449 inst->mouseptr_visible = show;
450 update_mouseptr(inst);
453 void draw_backing_rect(struct gui_data *inst)
455 GdkGC *gc = gdk_gc_new(inst->area->window);
456 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
457 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
458 inst->width * inst->font_width + 2*inst->window_border,
459 inst->height * inst->font_height + 2*inst->window_border);
463 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
465 struct gui_data *inst = (struct gui_data *)data;
466 int w, h, need_size = 0;
469 * See if the terminal size has changed, in which case we must
470 * let the terminal know.
472 w = (event->width - 2*inst->window_border) / inst->font_width;
473 h = (event->height - 2*inst->window_border) / inst->font_height;
474 if (w != inst->width || h != inst->height) {
477 conf_set_int(inst->conf, CONF_width, inst->width);
478 conf_set_int(inst->conf, CONF_height, inst->height);
483 gdk_pixmap_unref(inst->pixmap);
487 inst->pixmap = gdk_pixmap_new(widget->window,
488 (w * inst->font_width + 2*inst->window_border),
489 (h * inst->font_height + 2*inst->window_border), -1);
491 draw_backing_rect(inst);
493 if (need_size && inst->term) {
494 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
498 term_invalidate(inst->term);
500 #if GTK_CHECK_VERSION(2,0,0)
501 gtk_im_context_set_client_window(inst->imc, widget->window);
507 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
509 struct gui_data *inst = (struct gui_data *)data;
512 * Pass the exposed rectangle to terminal.c, which will call us
513 * back to do the actual painting.
516 gdk_draw_pixmap(widget->window,
517 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
519 event->area.x, event->area.y,
520 event->area.x, event->area.y,
521 event->area.width, event->area.height);
526 #define KEY_PRESSED(k) \
527 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
529 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
531 struct gui_data *inst = (struct gui_data *)data;
533 wchar_t ucsoutput[2];
534 int ucsval, start, end, special, output_charset, use_ucsoutput;
536 /* Remember the timestamp. */
537 inst->input_event_time = event->time;
539 /* By default, nothing is generated. */
541 special = use_ucsoutput = FALSE;
542 output_charset = CS_ISO8859_1;
545 * If Alt is being released after typing an Alt+numberpad
546 * sequence, we should generate the code that was typed.
548 * Note that we only do this if more than one key was actually
549 * pressed - I don't think Alt+NumPad4 should be ^D or that
550 * Alt+NumPad3 should be ^C, for example. There's no serious
551 * inconvenience in having to type a zero before a single-digit
554 if (event->type == GDK_KEY_RELEASE) {
555 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
556 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
557 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
559 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
562 * FIXME: we might usefully try to do something clever here
563 * about interpreting the generated key code in a way that's
564 * appropriate to the line code page.
566 output[0] = inst->alt_keycode;
570 #if GTK_CHECK_VERSION(2,0,0)
571 if (gtk_im_context_filter_keypress(inst->imc, event))
576 if (event->type == GDK_KEY_PRESS) {
580 printf("keypress: keyval = %04x, state = %08x; string =",
581 event->keyval, event->state);
582 for (i = 0; event->string[i]; i++)
583 printf(" %02x", (unsigned char) event->string[i]);
589 * NYI: Compose key (!!! requires Unicode faff before even trying)
593 * If Alt has just been pressed, we start potentially
594 * accumulating an Alt+numberpad code. We do this by
595 * setting alt_keycode to -1 (nothing yet but plausible).
597 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
598 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
599 inst->alt_keycode = -1;
600 inst->alt_digits = 0;
601 goto done; /* this generates nothing else */
605 * If we're seeing a numberpad key press with Mod1 down,
606 * consider adding it to alt_keycode if that's sensible.
607 * Anything _else_ with Mod1 down cancels any possibility
608 * of an ALT keycode: we set alt_keycode to -2.
610 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
612 switch (event->keyval) {
613 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
614 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
615 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
616 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
617 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
618 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
619 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
620 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
621 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
622 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
625 inst->alt_keycode = -2; /* it's invalid */
628 printf("Adding digit %d to keycode %d", digit,
631 if (inst->alt_keycode == -1)
632 inst->alt_keycode = digit; /* one-digit code */
634 inst->alt_keycode = inst->alt_keycode * 10 + digit;
637 printf(" gives new code %d\n", inst->alt_keycode);
639 /* Having used this digit, we now do nothing more with it. */
645 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
648 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
649 term_scroll(inst->term, 0, -inst->height/2);
652 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
653 term_scroll(inst->term, 0, -1);
656 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
657 term_scroll(inst->term, 0, +inst->height/2);
660 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
661 term_scroll(inst->term, 0, +1);
666 * Neither does Shift-Ins.
668 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
674 use_ucsoutput = FALSE;
676 /* ALT+things gives leading Escape. */
678 #if !GTK_CHECK_VERSION(2,0,0)
680 * In vanilla X, and hence also GDK 1.2, the string received
681 * as part of a keyboard event is assumed to be in
682 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
683 * but it's true: see the man page for XLookupString(3) for
686 output_charset = CS_ISO8859_1;
687 strncpy(output+1, event->string, lenof(output)-1);
689 if (gtk_im_context_filter_keypress(inst->imc, event))
693 * GDK 2.0 arranges to have done some translation for us: in
694 * GDK 2.0, event->string is encoded in the current locale.
696 * So we use the standard C library function mbstowcs() to
697 * convert from the current locale into Unicode; from there
698 * we can convert to whatever PuTTY is currently working in.
699 * (In fact I convert straight back to UTF-8 from
700 * wide-character Unicode, for the sake of simplicity: that
701 * way we can still use exactly the same code to manipulate
702 * the string, such as prefixing ESC.)
704 output_charset = CS_UTF8;
706 wchar_t widedata[32];
711 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
712 event->string, strlen(event->string),
713 widedata, lenof(widedata)-1);
716 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
717 CS_UTF8, NULL, NULL, 0);
718 output[1+ulen] = '\0';
723 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
724 ucsoutput[0] = '\033';
725 ucsoutput[1] = ucsval;
726 use_ucsoutput = TRUE;
729 output[lenof(output)-1] = '\0';
730 end = strlen(output);
732 if (event->state & GDK_MOD1_MASK) {
734 if (end == 1) end = 0;
738 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
739 if (!output[1] && event->keyval == '`' &&
740 (event->state & GDK_CONTROL_MASK)) {
742 use_ucsoutput = FALSE;
746 /* Control-Break sends a Break special to the backend */
747 if (event->keyval == GDK_Break &&
748 (event->state & GDK_CONTROL_MASK)) {
750 inst->back->special(inst->backhandle, TS_BRK);
754 /* We handle Return ourselves, because it needs to be flagged as
755 * special to ldisc. */
756 if (event->keyval == GDK_Return) {
758 use_ucsoutput = FALSE;
763 /* Control-2, Control-Space and Control-@ are NUL */
765 (event->keyval == ' ' || event->keyval == '2' ||
766 event->keyval == '@') &&
767 (event->state & (GDK_SHIFT_MASK |
768 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
770 use_ucsoutput = FALSE;
774 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
775 if (!output[1] && event->keyval == ' ' &&
776 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
777 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
779 output_charset = CS_ISO8859_1;
780 use_ucsoutput = FALSE;
784 /* We don't let GTK tell us what Backspace is! We know better. */
785 if (event->keyval == GDK_BackSpace &&
786 !(event->state & GDK_SHIFT_MASK)) {
787 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
789 use_ucsoutput = FALSE;
793 /* For Shift Backspace, do opposite of what is configured. */
794 if (event->keyval == GDK_BackSpace &&
795 (event->state & GDK_SHIFT_MASK)) {
796 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
798 use_ucsoutput = FALSE;
803 /* Shift-Tab is ESC [ Z */
804 if (event->keyval == GDK_ISO_Left_Tab ||
805 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
806 end = 1 + sprintf(output+1, "\033[Z");
807 use_ucsoutput = FALSE;
809 /* And normal Tab is Tab, if the keymap hasn't already told us.
810 * (Curiously, at least one version of the MacOS 10.5 X server
811 * doesn't translate Tab for us. */
812 if (event->keyval == GDK_Tab && end <= 1) {
818 * NetHack keypad mode.
820 if (conf_get_int(inst->conf, CONF_nethack_keypad)) {
822 switch (event->keyval) {
823 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
824 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
825 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
826 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
827 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
828 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
829 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
830 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
831 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
835 if (event->state & GDK_CONTROL_MASK)
837 else if (event->state & GDK_SHIFT_MASK)
841 use_ucsoutput = FALSE;
847 * Application keypad mode.
849 if (inst->term->app_keypad_keys &&
850 !conf_get_int(inst->conf, CONF_no_applic_k)) {
852 switch (event->keyval) {
853 case GDK_Num_Lock: xkey = 'P'; break;
854 case GDK_KP_Divide: xkey = 'Q'; break;
855 case GDK_KP_Multiply: xkey = 'R'; break;
856 case GDK_KP_Subtract: xkey = 'S'; break;
858 * Keypad + is tricky. It covers a space that would
859 * be taken up on the VT100 by _two_ keys; so we
860 * let Shift select between the two. Worse still,
861 * in xterm function key mode we change which two...
864 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
865 if (event->state & GDK_SHIFT_MASK)
869 } else if (event->state & GDK_SHIFT_MASK)
874 case GDK_KP_Enter: xkey = 'M'; break;
875 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
876 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
877 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
878 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
879 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
880 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
881 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
882 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
883 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
884 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
885 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
888 if (inst->term->vt52_mode) {
889 if (xkey >= 'P' && xkey <= 'S')
890 end = 1 + sprintf(output+1, "\033%c", xkey);
892 end = 1 + sprintf(output+1, "\033?%c", xkey);
894 end = 1 + sprintf(output+1, "\033O%c", xkey);
895 use_ucsoutput = FALSE;
901 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
902 * for integer decimal nn.)
904 * We also deal with the weird ones here. Linux VCs replace F1
905 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
906 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
911 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
912 switch (event->keyval) {
914 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
917 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
920 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
923 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
926 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
929 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
932 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
935 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
938 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
941 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
974 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
975 case GDK_Home: case GDK_KP_Home:
978 case GDK_Insert: case GDK_KP_Insert:
981 case GDK_Delete: case GDK_KP_Delete:
984 case GDK_End: case GDK_KP_End:
987 case GDK_Page_Up: case GDK_KP_Page_Up:
990 case GDK_Page_Down: case GDK_KP_Page_Down:
994 /* Reorder edit keys to physical order */
995 if (funky_type == FUNKY_VT400 && code <= 6)
996 code = "\0\2\1\4\5\3\6"[code];
998 if (inst->term->vt52_mode && code > 0 && code <= 6) {
999 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1000 use_ucsoutput = FALSE;
1004 if (funky_type == FUNKY_SCO && /* SCO function keys */
1005 code >= 11 && code <= 34) {
1006 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1008 switch (event->keyval) {
1009 case GDK_F1: index = 0; break;
1010 case GDK_F2: index = 1; break;
1011 case GDK_F3: index = 2; break;
1012 case GDK_F4: index = 3; break;
1013 case GDK_F5: index = 4; break;
1014 case GDK_F6: index = 5; break;
1015 case GDK_F7: index = 6; break;
1016 case GDK_F8: index = 7; break;
1017 case GDK_F9: index = 8; break;
1018 case GDK_F10: index = 9; break;
1019 case GDK_F11: index = 10; break;
1020 case GDK_F12: index = 11; break;
1022 if (event->state & GDK_SHIFT_MASK) index += 12;
1023 if (event->state & GDK_CONTROL_MASK) index += 24;
1024 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1025 use_ucsoutput = FALSE;
1028 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1029 code >= 1 && code <= 6) {
1030 char codes[] = "HL.FIG";
1035 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1037 use_ucsoutput = FALSE;
1040 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1041 code >= 11 && code <= 24) {
1047 if (inst->term->vt52_mode)
1048 end = 1 + sprintf(output+1,
1049 "\x1B%c", code + 'P' - 11 - offt);
1051 end = 1 + sprintf(output+1,
1052 "\x1BO%c", code + 'P' - 11 - offt);
1053 use_ucsoutput = FALSE;
1056 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1057 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1058 use_ucsoutput = FALSE;
1061 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1062 if (inst->term->vt52_mode)
1063 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1065 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1066 use_ucsoutput = FALSE;
1069 if ((code == 1 || code == 4) &&
1070 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1071 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1072 use_ucsoutput = FALSE;
1076 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1077 use_ucsoutput = FALSE;
1083 * Cursor keys. (This includes the numberpad cursor keys,
1084 * if we haven't already done them due to app keypad mode.)
1086 * Here we also process un-numlocked un-appkeypadded KP5,
1087 * which sends ESC [ G.
1091 switch (event->keyval) {
1092 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1093 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1094 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1095 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1096 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1099 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1100 event->state & GDK_CONTROL_MASK);
1101 use_ucsoutput = FALSE;
1110 if (end-start > 0) {
1111 #ifdef KEY_DEBUGGING
1113 printf("generating sequence:");
1114 for (i = start; i < end; i++)
1115 printf(" %02x", (unsigned char) output[i]);
1121 * For special control characters, the character set
1122 * should never matter.
1124 output[end] = '\0'; /* NUL-terminate */
1126 ldisc_send(inst->ldisc, output+start, -2, 1);
1127 } else if (!inst->direct_to_font) {
1128 if (!use_ucsoutput) {
1130 lpage_send(inst->ldisc, output_charset, output+start,
1134 * We generated our own Unicode key data from the
1135 * keysym, so use that instead.
1138 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1142 * In direct-to-font mode, we just send the string
1143 * exactly as we received it.
1146 ldisc_send(inst->ldisc, output+start, end-start, 1);
1149 show_mouseptr(inst, 0);
1150 term_seen_key_event(inst->term);
1156 #if GTK_CHECK_VERSION(2,0,0)
1157 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1159 struct gui_data *inst = (struct gui_data *)data;
1160 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1161 show_mouseptr(inst, 0);
1162 term_seen_key_event(inst->term);
1166 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1167 GdkEventType type, guint ebutton, guint state,
1168 gdouble ex, gdouble ey)
1170 int shift, ctrl, alt, x, y, button, act;
1172 /* Remember the timestamp. */
1173 inst->input_event_time = timestamp;
1175 show_mouseptr(inst, 1);
1177 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1178 term_scroll(inst->term, 0, -5);
1181 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1182 term_scroll(inst->term, 0, +5);
1186 shift = state & GDK_SHIFT_MASK;
1187 ctrl = state & GDK_CONTROL_MASK;
1188 alt = state & GDK_MOD1_MASK;
1190 if (ebutton == 3 && ctrl) {
1191 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1192 ebutton, timestamp);
1198 else if (ebutton == 2)
1199 button = MBT_MIDDLE;
1200 else if (ebutton == 3)
1203 return FALSE; /* don't even know what button! */
1206 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1207 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1208 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1209 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1210 default: return FALSE; /* don't know this event type */
1213 if (send_raw_mouse && !(shift && conf_get_int(inst->conf,
1214 CONF_mouse_override)) &&
1215 act != MA_CLICK && act != MA_RELEASE)
1216 return TRUE; /* we ignore these in raw mouse mode */
1218 x = (ex - inst->window_border) / inst->font_width;
1219 y = (ey - inst->window_border) / inst->font_height;
1221 term_mouse(inst->term, button, translate_button(button), act,
1222 x, y, shift, ctrl, alt);
1227 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1229 struct gui_data *inst = (struct gui_data *)data;
1230 return button_internal(inst, event->time, event->type, event->button,
1231 event->state, event->x, event->y);
1234 #if GTK_CHECK_VERSION(2,0,0)
1236 * In GTK 2, mouse wheel events have become a new type of event.
1237 * This handler translates them back into button-4 and button-5
1238 * presses so that I don't have to change my old code too much :-)
1240 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1242 struct gui_data *inst = (struct gui_data *)data;
1245 if (event->direction == GDK_SCROLL_UP)
1247 else if (event->direction == GDK_SCROLL_DOWN)
1252 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1253 button, event->state, event->x, event->y);
1257 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1259 struct gui_data *inst = (struct gui_data *)data;
1260 int shift, ctrl, alt, x, y, button;
1262 /* Remember the timestamp. */
1263 inst->input_event_time = event->time;
1265 show_mouseptr(inst, 1);
1267 shift = event->state & GDK_SHIFT_MASK;
1268 ctrl = event->state & GDK_CONTROL_MASK;
1269 alt = event->state & GDK_MOD1_MASK;
1270 if (event->state & GDK_BUTTON1_MASK)
1272 else if (event->state & GDK_BUTTON2_MASK)
1273 button = MBT_MIDDLE;
1274 else if (event->state & GDK_BUTTON3_MASK)
1277 return FALSE; /* don't even know what button! */
1279 x = (event->x - inst->window_border) / inst->font_width;
1280 y = (event->y - inst->window_border) / inst->font_height;
1282 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1283 x, y, shift, ctrl, alt);
1288 void frontend_keypress(void *handle)
1290 struct gui_data *inst = (struct gui_data *)handle;
1293 * If our child process has exited but not closed, terminate on
1300 static gint idle_exit_func(gpointer data)
1302 struct gui_data *inst = (struct gui_data *)data;
1303 int exitcode, close_on_exit;
1305 if (!inst->exited &&
1306 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1307 inst->exited = TRUE;
1308 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1309 if (close_on_exit == FORCE_ON ||
1310 (close_on_exit == AUTO && exitcode == 0))
1311 gtk_main_quit(); /* just go */
1313 ldisc_free(inst->ldisc);
1317 inst->back->free(inst->backhandle);
1318 inst->backhandle = NULL;
1320 term_provide_resize_fn(inst->term, NULL, NULL);
1321 update_specials_menu(inst);
1323 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1326 gtk_idle_remove(inst->term_exit_idle_id);
1330 void notify_remote_exit(void *frontend)
1332 struct gui_data *inst = (struct gui_data *)frontend;
1334 inst->term_exit_idle_id = gtk_idle_add(idle_exit_func, inst);
1337 static gint timer_trigger(gpointer data)
1339 long now = GPOINTER_TO_LONG(data);
1343 if (run_timers(now, &next)) {
1344 ticks = next - GETTICKCOUNT();
1345 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1346 LONG_TO_GPOINTER(next));
1350 * Never let a timer resume. If we need another one, we've
1351 * asked for it explicitly above.
1356 void timer_change_notify(long next)
1361 gtk_timeout_remove(timer_id);
1363 ticks = next - GETTICKCOUNT();
1365 ticks = 1; /* just in case */
1367 timer_id = gtk_timeout_add(ticks, timer_trigger,
1368 LONG_TO_GPOINTER(next));
1371 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1374 * We must process exceptional notifications before ordinary
1375 * readability ones, or we may go straight past the urgent
1378 if (condition & GDK_INPUT_EXCEPTION)
1379 select_result(sourcefd, 4);
1380 if (condition & GDK_INPUT_READ)
1381 select_result(sourcefd, 1);
1382 if (condition & GDK_INPUT_WRITE)
1383 select_result(sourcefd, 2);
1386 void destroy(GtkWidget *widget, gpointer data)
1391 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1393 struct gui_data *inst = (struct gui_data *)data;
1394 term_set_focus(inst->term, event->in);
1395 term_update(inst->term);
1396 show_mouseptr(inst, 1);
1400 void set_busy_status(void *frontend, int status)
1402 struct gui_data *inst = (struct gui_data *)frontend;
1403 inst->busy_status = status;
1404 update_mouseptr(inst);
1408 * set or clear the "raw mouse message" mode
1410 void set_raw_mouse_mode(void *frontend, int activate)
1412 struct gui_data *inst = (struct gui_data *)frontend;
1413 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1414 send_raw_mouse = activate;
1415 update_mouseptr(inst);
1418 void request_resize(void *frontend, int w, int h)
1420 struct gui_data *inst = (struct gui_data *)frontend;
1421 int large_x, large_y;
1422 int offset_x, offset_y;
1424 GtkRequisition inner, outer;
1427 * This is a heinous hack dreamed up by the gnome-terminal
1428 * people to get around a limitation in gtk. The problem is
1429 * that in order to set the size correctly we really need to be
1430 * calling gtk_window_resize - but that needs to know the size
1431 * of the _whole window_, not the drawing area. So what we do
1432 * is to set an artificially huge size request on the drawing
1433 * area, recompute the resulting size request on the window,
1434 * and look at the difference between the two. That gives us
1435 * the x and y offsets we need to translate drawing area size
1436 * into window size for real, and then we call
1437 * gtk_window_resize.
1441 * We start by retrieving the current size of the whole window.
1442 * Adding a bit to _that_ will give us a value we can use as a
1443 * bogus size request which guarantees to be bigger than the
1444 * current size of the drawing area.
1446 get_window_pixels(inst, &large_x, &large_y);
1450 #if GTK_CHECK_VERSION(2,0,0)
1451 gtk_widget_set_size_request(inst->area, large_x, large_y);
1453 gtk_widget_set_usize(inst->area, large_x, large_y);
1455 gtk_widget_size_request(inst->area, &inner);
1456 gtk_widget_size_request(inst->window, &outer);
1458 offset_x = outer.width - inner.width;
1459 offset_y = outer.height - inner.height;
1461 area_x = inst->font_width * w + 2*inst->window_border;
1462 area_y = inst->font_height * h + 2*inst->window_border;
1465 * Now we must set the size request on the drawing area back to
1466 * something sensible before we commit the real resize. Best
1467 * way to do this, I think, is to set it to what the size is
1468 * really going to end up being.
1470 #if GTK_CHECK_VERSION(2,0,0)
1471 gtk_widget_set_size_request(inst->area, area_x, area_y);
1472 gtk_window_resize(GTK_WINDOW(inst->window),
1473 area_x + offset_x, area_y + offset_y);
1475 gtk_widget_set_usize(inst->area, area_x, area_y);
1476 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1478 * I can no longer remember what this call to
1479 * gtk_container_dequeue_resize_handler is for. It was
1480 * introduced in r3092 with no comment, and the commit log
1481 * message was uninformative. I'm _guessing_ its purpose is to
1482 * prevent gratuitous resize processing on the window given
1483 * that we're about to resize it anyway, but I have no idea
1484 * why that's so incredibly vital.
1486 * I've tried removing the call, and nothing seems to go
1487 * wrong. I've backtracked to r3092 and tried removing the
1488 * call there, and still nothing goes wrong. So I'm going to
1489 * adopt the working hypothesis that it's superfluous; I won't
1490 * actually remove it from the GTK 1.2 code, but I won't
1491 * attempt to replicate its functionality in the GTK 2 code
1494 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1495 gdk_window_resize(inst->window->window,
1496 area_x + offset_x, area_y + offset_y);
1500 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1502 gboolean success[1];
1504 inst->cols[n].red = r * 0x0101;
1505 inst->cols[n].green = g * 0x0101;
1506 inst->cols[n].blue = b * 0x0101;
1508 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1509 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1510 FALSE, TRUE, success);
1512 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1516 void set_window_background(struct gui_data *inst)
1518 if (inst->area && inst->area->window)
1519 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1520 if (inst->window && inst->window->window)
1521 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1524 void palette_set(void *frontend, int n, int r, int g, int b)
1526 struct gui_data *inst = (struct gui_data *)frontend;
1529 if (n > NALLCOLOURS)
1531 real_palette_set(inst, n, r, g, b);
1533 /* Default Background changed. Ensure space between text area and
1534 * window border is redrawn */
1535 set_window_background(inst);
1536 draw_backing_rect(inst);
1537 gtk_widget_queue_draw(inst->area);
1541 void palette_reset(void *frontend)
1543 struct gui_data *inst = (struct gui_data *)frontend;
1544 /* This maps colour indices in inst->conf to those used in inst->cols. */
1545 static const int ww[] = {
1546 256, 257, 258, 259, 260, 261,
1547 0, 8, 1, 9, 2, 10, 3, 11,
1548 4, 12, 5, 13, 6, 14, 7, 15
1550 gboolean success[NALLCOLOURS];
1553 assert(lenof(ww) == NCFGCOLOURS);
1555 if (!inst->colmap) {
1556 inst->colmap = gdk_colormap_get_system();
1558 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1561 for (i = 0; i < NCFGCOLOURS; i++) {
1562 inst->cols[ww[i]].red =
1563 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
1564 inst->cols[ww[i]].green =
1565 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
1566 inst->cols[ww[i]].blue =
1567 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
1570 for (i = 0; i < NEXTCOLOURS; i++) {
1572 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1573 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1574 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1575 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1577 int shade = i - 216;
1578 shade = shade * 0x0a0a + 0x0808;
1579 inst->cols[i+16].red = inst->cols[i+16].green =
1580 inst->cols[i+16].blue = shade;
1584 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1585 FALSE, TRUE, success);
1586 for (i = 0; i < NALLCOLOURS; i++) {
1588 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1590 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
1591 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
1592 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
1595 /* Since Default Background may have changed, ensure that space
1596 * between text area and window border is refreshed. */
1597 set_window_background(inst);
1598 if (inst->area && inst->area->window) {
1599 draw_backing_rect(inst);
1600 gtk_widget_queue_draw(inst->area);
1604 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1605 * do this before we start using cut buffers.
1607 void init_cutbuffers()
1609 unsigned char empty[] = "";
1610 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1611 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1612 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1613 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1614 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1615 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1616 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1617 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1618 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1619 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1620 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1621 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1622 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1623 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1624 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1625 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1628 /* Store the data in a cut-buffer. */
1629 void store_cutbuffer(char * ptr, int len)
1631 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1632 XRotateBuffers(GDK_DISPLAY(), 1);
1633 XStoreBytes(GDK_DISPLAY(), ptr, len);
1636 /* Retrieve data from a cut-buffer.
1637 * Returned data needs to be freed with XFree().
1639 char * retrieve_cutbuffer(int * nbytes)
1642 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1643 if (*nbytes <= 0 && ptr != 0) {
1650 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1652 struct gui_data *inst = (struct gui_data *)frontend;
1653 if (inst->pasteout_data)
1654 sfree(inst->pasteout_data);
1655 if (inst->pasteout_data_ctext)
1656 sfree(inst->pasteout_data_ctext);
1657 if (inst->pasteout_data_utf8)
1658 sfree(inst->pasteout_data_utf8);
1661 * Set up UTF-8 and compound text paste data. This only happens
1662 * if we aren't in direct-to-font mode using the D800 hack.
1664 if (!inst->direct_to_font) {
1665 const wchar_t *tmp = data;
1670 inst->pasteout_data_utf8 = snewn(len*6, char);
1671 inst->pasteout_data_utf8_len = len*6;
1672 inst->pasteout_data_utf8_len =
1673 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1674 inst->pasteout_data_utf8_len,
1675 CS_UTF8, NULL, NULL, 0);
1676 if (inst->pasteout_data_utf8_len == 0) {
1677 sfree(inst->pasteout_data_utf8);
1678 inst->pasteout_data_utf8 = NULL;
1680 inst->pasteout_data_utf8 =
1681 sresize(inst->pasteout_data_utf8,
1682 inst->pasteout_data_utf8_len + 1, char);
1683 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1687 * Now let Xlib convert our UTF-8 data into compound text.
1689 list[0] = inst->pasteout_data_utf8;
1690 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1691 XCompoundTextStyle, &tp) == 0) {
1692 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1693 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1694 inst->pasteout_data_ctext_len = tp.nitems;
1697 inst->pasteout_data_ctext = NULL;
1698 inst->pasteout_data_ctext_len = 0;
1701 inst->pasteout_data_utf8 = NULL;
1702 inst->pasteout_data_utf8_len = 0;
1703 inst->pasteout_data_ctext = NULL;
1704 inst->pasteout_data_ctext_len = 0;
1707 inst->pasteout_data = snewn(len*6, char);
1708 inst->pasteout_data_len = len*6;
1709 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1710 data, len, inst->pasteout_data,
1711 inst->pasteout_data_len,
1713 if (inst->pasteout_data_len == 0) {
1714 sfree(inst->pasteout_data);
1715 inst->pasteout_data = NULL;
1717 inst->pasteout_data =
1718 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1721 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1723 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1724 inst->input_event_time)) {
1725 #if GTK_CHECK_VERSION(2,0,0)
1726 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
1728 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1729 GDK_SELECTION_TYPE_STRING, 1);
1730 if (inst->pasteout_data_ctext)
1731 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1732 compound_text_atom, 1);
1733 if (inst->pasteout_data_utf8)
1734 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1735 utf8_string_atom, 1);
1739 term_deselect(inst->term);
1742 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1743 guint info, guint time_stamp, gpointer data)
1745 struct gui_data *inst = (struct gui_data *)data;
1746 if (seldata->target == utf8_string_atom)
1747 gtk_selection_data_set(seldata, seldata->target, 8,
1748 (unsigned char *)inst->pasteout_data_utf8,
1749 inst->pasteout_data_utf8_len);
1750 else if (seldata->target == compound_text_atom)
1751 gtk_selection_data_set(seldata, seldata->target, 8,
1752 (unsigned char *)inst->pasteout_data_ctext,
1753 inst->pasteout_data_ctext_len);
1755 gtk_selection_data_set(seldata, seldata->target, 8,
1756 (unsigned char *)inst->pasteout_data,
1757 inst->pasteout_data_len);
1760 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1763 struct gui_data *inst = (struct gui_data *)data;
1765 term_deselect(inst->term);
1766 if (inst->pasteout_data)
1767 sfree(inst->pasteout_data);
1768 if (inst->pasteout_data_ctext)
1769 sfree(inst->pasteout_data_ctext);
1770 if (inst->pasteout_data_utf8)
1771 sfree(inst->pasteout_data_utf8);
1772 inst->pasteout_data = NULL;
1773 inst->pasteout_data_len = 0;
1774 inst->pasteout_data_ctext = NULL;
1775 inst->pasteout_data_ctext_len = 0;
1776 inst->pasteout_data_utf8 = NULL;
1777 inst->pasteout_data_utf8_len = 0;
1781 void request_paste(void *frontend)
1783 struct gui_data *inst = (struct gui_data *)frontend;
1785 * In Unix, pasting is asynchronous: all we can do at the
1786 * moment is to call gtk_selection_convert(), and when the data
1787 * comes back _then_ we can call term_do_paste().
1790 if (!inst->direct_to_font) {
1792 * First we attempt to retrieve the selection as a UTF-8
1793 * string (which we will convert to the correct code page
1794 * before sending to the session, of course). If that
1795 * fails, selection_received() will be informed and will
1796 * fall back to an ordinary string.
1798 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1800 inst->input_event_time);
1803 * If we're in direct-to-font mode, we disable UTF-8
1804 * pasting, and go straight to ordinary string data.
1806 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1807 GDK_SELECTION_TYPE_STRING,
1808 inst->input_event_time);
1812 gint idle_paste_func(gpointer data); /* forward ref */
1814 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1815 guint time, gpointer data)
1817 struct gui_data *inst = (struct gui_data *)data;
1821 int length, count, ret;
1822 int free_list_required = 0;
1823 int free_required = 0;
1826 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1828 * Failed to get a UTF-8 selection string. Try compound
1831 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1833 inst->input_event_time);
1837 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1839 * Failed to get UTF-8 or compound text. Try an ordinary
1842 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1843 GDK_SELECTION_TYPE_STRING,
1844 inst->input_event_time);
1849 * If we have data, but it's not of a type we can deal with,
1850 * we have to ignore the data.
1852 if (seldata->length > 0 &&
1853 seldata->type != GDK_SELECTION_TYPE_STRING &&
1854 seldata->type != compound_text_atom &&
1855 seldata->type != utf8_string_atom)
1859 * If we have no data, try looking in a cut buffer.
1861 if (seldata->length <= 0) {
1862 text = retrieve_cutbuffer(&length);
1865 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1866 * source, so use that as a de-facto standard. */
1867 charset = CS_ISO8859_1;
1871 * Convert COMPOUND_TEXT into UTF-8.
1873 if (seldata->type == compound_text_atom) {
1874 tp.value = seldata->data;
1875 tp.encoding = (Atom) seldata->type;
1876 tp.format = seldata->format;
1877 tp.nitems = seldata->length;
1878 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1880 if (ret != 0 || count != 1) {
1882 * Compound text failed; fall back to STRING.
1884 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1885 GDK_SELECTION_TYPE_STRING,
1886 inst->input_event_time);
1890 length = strlen(list[0]);
1892 free_list_required = 1;
1894 text = (char *)seldata->data;
1895 length = seldata->length;
1896 charset = (seldata->type == utf8_string_atom ?
1897 CS_UTF8 : inst->ucsdata.line_codepage);
1901 if (inst->pastein_data)
1902 sfree(inst->pastein_data);
1904 inst->pastein_data = snewn(length, wchar_t);
1905 inst->pastein_data_len = length;
1906 inst->pastein_data_len =
1907 mb_to_wc(charset, 0, text, length,
1908 inst->pastein_data, inst->pastein_data_len);
1910 term_do_paste(inst->term);
1912 if (term_paste_pending(inst->term))
1913 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1915 if (free_list_required)
1916 XFreeStringList(list);
1921 gint idle_paste_func(gpointer data)
1923 struct gui_data *inst = (struct gui_data *)data;
1925 if (term_paste_pending(inst->term))
1926 term_paste(inst->term);
1928 gtk_idle_remove(inst->term_paste_idle_id);
1934 void get_clip(void *frontend, wchar_t ** p, int *len)
1936 struct gui_data *inst = (struct gui_data *)frontend;
1939 *p = inst->pastein_data;
1940 *len = inst->pastein_data_len;
1944 static void set_window_titles(struct gui_data *inst)
1947 * We must always call set_icon_name after calling set_title,
1948 * since set_title will write both names. Irritating, but such
1951 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1952 if (!conf_get_int(inst->conf, CONF_win_name_always))
1953 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1956 void set_title(void *frontend, char *title)
1958 struct gui_data *inst = (struct gui_data *)frontend;
1959 sfree(inst->wintitle);
1960 inst->wintitle = dupstr(title);
1961 set_window_titles(inst);
1964 void set_icon(void *frontend, char *title)
1966 struct gui_data *inst = (struct gui_data *)frontend;
1967 sfree(inst->icontitle);
1968 inst->icontitle = dupstr(title);
1969 set_window_titles(inst);
1972 void set_title_and_icon(void *frontend, char *title, char *icon)
1974 struct gui_data *inst = (struct gui_data *)frontend;
1975 sfree(inst->wintitle);
1976 inst->wintitle = dupstr(title);
1977 sfree(inst->icontitle);
1978 inst->icontitle = dupstr(icon);
1979 set_window_titles(inst);
1982 void set_sbar(void *frontend, int total, int start, int page)
1984 struct gui_data *inst = (struct gui_data *)frontend;
1985 if (!conf_get_int(inst->conf, CONF_scrollbar))
1987 inst->sbar_adjust->lower = 0;
1988 inst->sbar_adjust->upper = total;
1989 inst->sbar_adjust->value = start;
1990 inst->sbar_adjust->page_size = page;
1991 inst->sbar_adjust->step_increment = 1;
1992 inst->sbar_adjust->page_increment = page/2;
1993 inst->ignore_sbar = TRUE;
1994 gtk_adjustment_changed(inst->sbar_adjust);
1995 inst->ignore_sbar = FALSE;
1998 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2000 struct gui_data *inst = (struct gui_data *)data;
2002 if (!conf_get_int(inst->conf, CONF_scrollbar))
2004 if (!inst->ignore_sbar)
2005 term_scroll(inst->term, 1, (int)adj->value);
2008 void sys_cursor(void *frontend, int x, int y)
2011 * This is meaningless under X.
2016 * This is still called when mode==BELL_VISUAL, even though the
2017 * visual bell is handled entirely within terminal.c, because we
2018 * may want to perform additional actions on any kind of bell (for
2019 * example, taskbar flashing in Windows).
2021 void do_beep(void *frontend, int mode)
2023 if (mode == BELL_DEFAULT)
2027 int char_width(Context ctx, int uc)
2030 * Under X, any fixed-width font really _is_ fixed-width.
2031 * Double-width characters will be dealt with using a separate
2032 * font. For the moment we can simply return 1.
2034 * FIXME: but is that also true of Pango?
2039 Context get_ctx(void *frontend)
2041 struct gui_data *inst = (struct gui_data *)frontend;
2042 struct draw_ctx *dctx;
2044 if (!inst->area->window)
2047 dctx = snew(struct draw_ctx);
2049 dctx->gc = gdk_gc_new(inst->area->window);
2053 void free_ctx(Context ctx)
2055 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2056 /* struct gui_data *inst = dctx->inst; */
2057 GdkGC *gc = dctx->gc;
2063 * Draw a line of text in the window, at given character
2064 * coordinates, in given attributes.
2066 * We are allowed to fiddle with the contents of `text'.
2068 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2069 unsigned long attr, int lattr)
2071 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2072 struct gui_data *inst = dctx->inst;
2073 GdkGC *gc = dctx->gc;
2074 int ncombining, combining;
2075 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2076 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
2078 if (attr & TATTR_COMBINING) {
2084 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2085 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2086 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2091 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
2092 if (nfg < 16) nfg |= 8;
2093 else if (nfg >= 256) nfg |= 1;
2095 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
2096 if (nbg < 16) nbg |= 8;
2097 else if (nbg >= 256) nbg |= 1;
2099 if ((attr & TATTR_ACTCURS) && !monochrome) {
2104 fontid = shadow = 0;
2106 if (attr & ATTR_WIDE) {
2113 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
2120 if (!inst->fonts[fontid]) {
2123 * Fall back through font ids with subsets of this one's
2124 * set bits, in order.
2126 for (i = fontid; i-- > 0 ;) {
2128 continue; /* some other bit is set */
2129 if (inst->fonts[i]) {
2134 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2137 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2139 if (x >= inst->term->cols)
2141 if (x + len*2*widefactor > inst->term->cols)
2142 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2150 r.x = x*inst->font_width+inst->window_border;
2151 r.y = y*inst->font_height+inst->window_border;
2152 r.width = rlen*widefactor*inst->font_width;
2153 r.height = inst->font_height;
2154 gdk_gc_set_clip_rectangle(gc, &r);
2157 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
2158 gdk_draw_rectangle(inst->pixmap, gc, 1,
2159 x*inst->font_width+inst->window_border,
2160 y*inst->font_height+inst->window_border,
2161 rlen*widefactor*inst->font_width, inst->font_height);
2163 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2164 for (combining = 0; combining < ncombining; combining++) {
2165 unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid],
2166 x*inst->font_width+inst->window_border,
2167 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2168 text + combining, len, widefactor > 1,
2169 bold, inst->font_width);
2172 if (attr & ATTR_UNDER) {
2173 int uheight = inst->fonts[0]->ascent + 1;
2174 if (uheight >= inst->font_height)
2175 uheight = inst->font_height - 1;
2176 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->window_border,
2177 y*inst->font_height + uheight + inst->window_border,
2178 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2179 y*inst->font_height + uheight + inst->window_border);
2182 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2184 * I can't find any plausible StretchBlt equivalent in the
2185 * X server, so I'm going to do this the slow and painful
2186 * way. This will involve repeated calls to
2187 * gdk_draw_pixmap() to stretch the text horizontally. It's
2188 * O(N^2) in time and O(N) in network bandwidth, but you
2189 * try thinking of a better way. :-(
2192 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2193 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2194 x*inst->font_width+inst->window_border + 2*i,
2195 y*inst->font_height+inst->window_border,
2196 x*inst->font_width+inst->window_border + 2*i+1,
2197 y*inst->font_height+inst->window_border,
2198 len * widefactor * inst->font_width - i, inst->font_height);
2201 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2203 /* Now stretch vertically, in the same way. */
2204 if ((lattr & LATTR_MODE) == LATTR_BOT)
2208 for (i = 0; i < inst->font_height; i+=2) {
2209 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2210 x*inst->font_width+inst->window_border,
2211 y*inst->font_height+inst->window_border+dt*i+db,
2212 x*inst->font_width+inst->window_border,
2213 y*inst->font_height+inst->window_border+dt*(i+1),
2214 len * widefactor * inst->font_width, inst->font_height-i-1);
2220 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2221 unsigned long attr, int lattr)
2223 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2224 struct gui_data *inst = dctx->inst;
2225 GdkGC *gc = dctx->gc;
2228 do_text_internal(ctx, x, y, text, len, attr, lattr);
2230 if (attr & ATTR_WIDE) {
2236 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2238 if (x >= inst->term->cols)
2240 if (x + len*2*widefactor > inst->term->cols)
2241 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2245 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2246 x*inst->font_width+inst->window_border,
2247 y*inst->font_height+inst->window_border,
2248 x*inst->font_width+inst->window_border,
2249 y*inst->font_height+inst->window_border,
2250 len*widefactor*inst->font_width, inst->font_height);
2253 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2254 unsigned long attr, int lattr)
2256 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2257 struct gui_data *inst = dctx->inst;
2258 GdkGC *gc = dctx->gc;
2260 int active, passive, widefactor;
2262 if (attr & TATTR_PASCURS) {
2263 attr &= ~TATTR_PASCURS;
2267 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
2268 attr &= ~TATTR_ACTCURS;
2272 do_text_internal(ctx, x, y, text, len, attr, lattr);
2274 if (attr & TATTR_COMBINING)
2277 if (attr & ATTR_WIDE) {
2283 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2285 if (x >= inst->term->cols)
2287 if (x + len*2*widefactor > inst->term->cols)
2288 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2292 if (inst->cursor_type == 0) {
2294 * An active block cursor will already have been done by
2295 * the above do_text call, so we only need to do anything
2299 gdk_gc_set_foreground(gc, &inst->cols[261]);
2300 gdk_draw_rectangle(inst->pixmap, gc, 0,
2301 x*inst->font_width+inst->window_border,
2302 y*inst->font_height+inst->window_border,
2303 len*widefactor*inst->font_width-1, inst->font_height-1);
2307 int startx, starty, dx, dy, length, i;
2311 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2312 char_width = 2*inst->font_width;
2314 char_width = inst->font_width;
2316 if (inst->cursor_type == 1) {
2317 uheight = inst->fonts[0]->ascent + 1;
2318 if (uheight >= inst->font_height)
2319 uheight = inst->font_height - 1;
2321 startx = x * inst->font_width + inst->window_border;
2322 starty = y * inst->font_height + inst->window_border + uheight;
2325 length = len * widefactor * char_width;
2328 if (attr & TATTR_RIGHTCURS)
2329 xadjust = char_width - 1;
2330 startx = x * inst->font_width + inst->window_border + xadjust;
2331 starty = y * inst->font_height + inst->window_border;
2334 length = inst->font_height;
2337 gdk_gc_set_foreground(gc, &inst->cols[261]);
2339 for (i = 0; i < length; i++) {
2341 gdk_draw_point(inst->pixmap, gc, startx, starty);
2346 } else if (active) {
2347 gdk_draw_line(inst->pixmap, gc, startx, starty,
2348 startx + (length-1) * dx, starty + (length-1) * dy);
2349 } /* else no cursor (e.g., blinked off) */
2352 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2353 x*inst->font_width+inst->window_border,
2354 y*inst->font_height+inst->window_border,
2355 x*inst->font_width+inst->window_border,
2356 y*inst->font_height+inst->window_border,
2357 len*widefactor*inst->font_width, inst->font_height);
2359 #if GTK_CHECK_VERSION(2,0,0)
2361 GdkRectangle cursorrect;
2362 cursorrect.x = x*inst->font_width+inst->window_border;
2363 cursorrect.y = y*inst->font_height+inst->window_border;
2364 cursorrect.width = len*widefactor*inst->font_width;
2365 cursorrect.height = inst->font_height;
2366 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
2371 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2374 * Truly hideous hack: GTK doesn't allow us to set the mouse
2375 * cursor foreground and background colours unless we've _also_
2376 * created our own cursor from bitmaps. Therefore, I need to
2377 * load the `cursor' font and draw glyphs from it on to
2378 * pixmaps, in order to construct my cursors with the fg and bg
2379 * I want. This is a gross hack, but it's more self-contained
2380 * than linking in Xlib to find the X window handle to
2381 * inst->area and calling XRecolorCursor, and it's more
2382 * futureproof than hard-coding the shapes as bitmap arrays.
2384 static GdkFont *cursor_font = NULL;
2385 GdkPixmap *source, *mask;
2387 GdkColor cfg = { 0, 65535, 65535, 65535 };
2388 GdkColor cbg = { 0, 0, 0, 0 };
2389 GdkColor dfg = { 1, 65535, 65535, 65535 };
2390 GdkColor dbg = { 0, 0, 0, 0 };
2393 gint lb, rb, wid, asc, desc, w, h, x, y;
2395 if (cursor_val == -2) {
2396 gdk_font_unref(cursor_font);
2400 if (cursor_val >= 0 && !cursor_font) {
2401 cursor_font = gdk_font_load("cursor");
2403 gdk_font_ref(cursor_font);
2407 * Get the text extent of the cursor in question. We use the
2408 * mask character for this, because it's typically slightly
2409 * bigger than the main character.
2411 if (cursor_val >= 0) {
2413 text[0] = (char)cursor_val + 1;
2414 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2415 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2421 source = gdk_pixmap_new(NULL, w, h, 1);
2422 mask = gdk_pixmap_new(NULL, w, h, 1);
2425 * Draw the mask character on the mask pixmap.
2427 gc = gdk_gc_new(mask);
2428 gdk_gc_set_foreground(gc, &dbg);
2429 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2430 if (cursor_val >= 0) {
2432 text[0] = (char)cursor_val + 1;
2433 gdk_gc_set_foreground(gc, &dfg);
2434 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2439 * Draw the main character on the source pixmap.
2441 gc = gdk_gc_new(source);
2442 gdk_gc_set_foreground(gc, &dbg);
2443 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2444 if (cursor_val >= 0) {
2446 text[0] = (char)cursor_val;
2447 gdk_gc_set_foreground(gc, &dfg);
2448 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2453 * Create the cursor.
2455 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2460 gdk_pixmap_unref(source);
2461 gdk_pixmap_unref(mask);
2466 void modalfatalbox(char *p, ...)
2469 fprintf(stderr, "FATAL ERROR: ");
2471 vfprintf(stderr, p, ap);
2473 fputc('\n', stderr);
2477 void cmdline_error(char *p, ...)
2480 fprintf(stderr, "%s: ", appname);
2482 vfprintf(stderr, p, ap);
2484 fputc('\n', stderr);
2488 char *get_x_display(void *frontend)
2490 return gdk_get_display();
2493 long get_windowid(void *frontend)
2495 struct gui_data *inst = (struct gui_data *)frontend;
2496 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2499 static void help(FILE *fp) {
2501 "pterm option summary:\n"
2503 " --display DISPLAY Specify X display to use (note '--')\n"
2504 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2505 " -fn FONT Normal text font\n"
2506 " -fb FONT Bold text font\n"
2507 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2508 " -sl LINES Number of lines of scrollback\n"
2509 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2510 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2511 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2512 " -T TITLE Window title\n"
2513 " -ut, +ut Do(default) or do not update utmp\n"
2514 " -ls, +ls Do(default) or do not make shell a login shell\n"
2515 " -sb, +sb Do(default) or do not display a scrollbar\n"
2516 " -log PATH Log all output to a file\n"
2517 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2518 " -xrm RESOURCE-STRING Set an X resource\n"
2519 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2520 ) < 0 || fflush(fp) < 0) {
2521 perror("output error");
2526 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2527 struct gui_data *inst, Conf *conf)
2533 * Macros to make argument handling easier. Note that because
2534 * they need to call `continue', they cannot be contained in
2535 * the usual do {...} while (0) wrapper to make them
2536 * syntactically single statements; hence it is not legal to
2537 * use one of these macros as an unbraced statement between
2540 #define EXPECTS_ARG { \
2541 if (--argc <= 0) { \
2543 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2548 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2550 while (--argc > 0) {
2555 * Shameless cheating. Debian requires all X terminal
2556 * emulators to support `-T title'; but
2557 * cmdline_process_param will eat -T (it means no-pty) and
2558 * complain that pterm doesn't support it. So, in pterm
2559 * only, we convert -T into -title.
2561 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2565 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2566 do_everything ? 1 : -1, conf);
2569 cmdline_error("option \"%s\" requires an argument", p);
2570 } else if (ret == 2) {
2571 --argc, ++argv; /* skip next argument */
2573 } else if (ret == 1) {
2577 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2581 fs = fontspec_new(val);
2582 conf_set_fontspec(conf, CONF_font, fs);
2585 } else if (!strcmp(p, "-fb")) {
2589 fs = fontspec_new(val);
2590 conf_set_fontspec(conf, CONF_boldfont, fs);
2593 } else if (!strcmp(p, "-fw")) {
2597 fs = fontspec_new(val);
2598 conf_set_fontspec(conf, CONF_widefont, fs);
2601 } else if (!strcmp(p, "-fwb")) {
2605 fs = fontspec_new(val);
2606 conf_set_fontspec(conf, CONF_wideboldfont, fs);
2609 } else if (!strcmp(p, "-cs")) {
2612 conf_set_str(conf, CONF_line_codepage, val);
2614 } else if (!strcmp(p, "-geometry")) {
2620 flags = XParseGeometry(val, &x, &y, &w, &h);
2621 if (flags & WidthValue)
2622 conf_set_int(conf, CONF_width, w);
2623 if (flags & HeightValue)
2624 conf_set_int(conf, CONF_height, h);
2626 if (flags & (XValue | YValue)) {
2629 inst->gotpos = TRUE;
2630 inst->gravity = ((flags & XNegative ? 1 : 0) |
2631 (flags & YNegative ? 2 : 0));
2634 } else if (!strcmp(p, "-sl")) {
2637 conf_set_int(conf, CONF_savelines, atoi(val));
2639 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2640 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2641 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2646 if (!gdk_color_parse(val, &col)) {
2648 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2652 index = (!strcmp(p, "-fg") ? 0 :
2653 !strcmp(p, "-bg") ? 2 :
2654 !strcmp(p, "-bfg") ? 1 :
2655 !strcmp(p, "-bbg") ? 3 :
2656 !strcmp(p, "-cfg") ? 4 :
2657 !strcmp(p, "-cbg") ? 5 : -1);
2658 assert(index != -1);
2659 conf_set_int_int(conf, CONF_colours, index*3+0, col.red / 256);
2660 conf_set_int_int(conf, CONF_colours, index*3+1,col.green/ 256);
2661 conf_set_int_int(conf, CONF_colours, index*3+2, col.blue/ 256);
2664 } else if (use_pty_argv && !strcmp(p, "-e")) {
2665 /* This option swallows all further arguments. */
2671 pty_argv = snewn(argc+1, char *);
2673 for (i = 0; i < argc; i++)
2674 pty_argv[i] = argv[i];
2675 pty_argv[argc] = NULL;
2676 break; /* finished command-line processing */
2678 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2681 } else if (!strcmp(p, "-title")) {
2684 conf_set_str(conf, CONF_wintitle, val);
2686 } else if (!strcmp(p, "-log")) {
2690 fn = filename_from_str(val);
2691 conf_set_filename(conf, CONF_logfilename, fn);
2692 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
2695 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2697 conf_set_int(conf, CONF_stamp_utmp, 0);
2699 } else if (!strcmp(p, "-ut")) {
2701 conf_set_int(conf, CONF_stamp_utmp, 1);
2703 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2705 conf_set_int(conf, CONF_login_shell, 0);
2707 } else if (!strcmp(p, "-ls")) {
2709 conf_set_int(conf, CONF_login_shell, 1);
2711 } else if (!strcmp(p, "-nethack")) {
2713 conf_set_int(conf, CONF_nethack_keypad, 1);
2715 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2717 conf_set_int(conf, CONF_scrollbar, 0);
2719 } else if (!strcmp(p, "-sb")) {
2721 conf_set_int(conf, CONF_scrollbar, 1);
2723 } else if (!strcmp(p, "-name")) {
2727 } else if (!strcmp(p, "-xrm")) {
2729 provide_xrm_string(val);
2731 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2735 } else if (!strcmp(p, "-pgpfp")) {
2739 } else if(p[0] != '-' && (!do_everything ||
2740 process_nonoption_arg(p, conf,
2746 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2753 int uxsel_input_add(int fd, int rwx) {
2755 if (rwx & 1) flags |= GDK_INPUT_READ;
2756 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2757 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2759 return gdk_input_add(fd, flags, fd_input_func, NULL);
2762 void uxsel_input_remove(int id) {
2763 gdk_input_remove(id);
2766 int frontend_net_pending_error_idle_id;
2767 int frontend_got_net_pending_errors = FALSE;
2768 gboolean frontend_net_pending_errors(gpointer data)
2770 net_pending_errors();
2771 gtk_idle_remove(frontend_net_pending_error_idle_id);
2772 frontend_got_net_pending_errors = FALSE;
2775 void frontend_net_error_pending(void)
2777 if (!frontend_got_net_pending_errors) {
2778 frontend_got_net_pending_errors = TRUE;
2779 frontend_net_pending_error_idle_id =
2780 gtk_idle_add(frontend_net_pending_errors, NULL);
2784 void setup_fonts_ucs(struct gui_data *inst)
2786 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
2787 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
2791 unifont_destroy(inst->fonts[0]);
2793 unifont_destroy(inst->fonts[1]);
2795 unifont_destroy(inst->fonts[2]);
2797 unifont_destroy(inst->fonts[3]);
2799 fs = conf_get_fontspec(inst->conf, CONF_font);
2800 inst->fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
2801 shadowboldoffset, shadowbold);
2802 if (!inst->fonts[0]) {
2803 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2808 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
2809 if (shadowbold || !fs->name[0]) {
2810 inst->fonts[1] = NULL;
2812 inst->fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
2813 shadowboldoffset, shadowbold);
2814 if (!inst->fonts[1]) {
2815 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2821 fs = conf_get_fontspec(inst->conf, CONF_widefont);
2823 inst->fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
2824 shadowboldoffset, shadowbold);
2825 if (!inst->fonts[2]) {
2826 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2831 inst->fonts[2] = NULL;
2834 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
2835 if (shadowbold || !fs->name[0]) {
2836 inst->fonts[3] = NULL;
2838 inst->fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
2839 shadowboldoffset, shadowbold);
2840 if (!inst->fonts[3]) {
2841 fprintf(stderr, "%s: unable to load wide bold font \"%s\"\n", appname,
2847 inst->font_width = inst->fonts[0]->width;
2848 inst->font_height = inst->fonts[0]->height;
2850 inst->direct_to_font = init_ucs(&inst->ucsdata,
2851 conf_get_str(inst->conf, CONF_line_codepage),
2852 conf_get_int(inst->conf, CONF_utf8_override),
2853 inst->fonts[0]->public_charset,
2854 conf_get_int(inst->conf, CONF_vtmode));
2857 void set_geom_hints(struct gui_data *inst)
2860 geom.min_width = inst->font_width + 2*inst->window_border;
2861 geom.min_height = inst->font_height + 2*inst->window_border;
2862 geom.max_width = geom.max_height = -1;
2863 geom.base_width = 2*inst->window_border;
2864 geom.base_height = 2*inst->window_border;
2865 geom.width_inc = inst->font_width;
2866 geom.height_inc = inst->font_height;
2867 geom.min_aspect = geom.max_aspect = 0;
2868 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2869 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2870 GDK_HINT_RESIZE_INC);
2873 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2875 struct gui_data *inst = (struct gui_data *)data;
2876 term_clrsb(inst->term);
2879 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2881 struct gui_data *inst = (struct gui_data *)data;
2882 term_pwron(inst->term, TRUE);
2884 ldisc_send(inst->ldisc, NULL, 0, 0);
2887 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2889 struct gui_data *inst = (struct gui_data *)data;
2890 term_copyall(inst->term);
2893 void special_menuitem(GtkMenuItem *item, gpointer data)
2895 struct gui_data *inst = (struct gui_data *)data;
2896 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2900 inst->back->special(inst->backhandle, code);
2903 void about_menuitem(GtkMenuItem *item, gpointer data)
2905 struct gui_data *inst = (struct gui_data *)data;
2906 about_box(inst->window);
2909 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2911 struct gui_data *inst = (struct gui_data *)data;
2912 showeventlog(inst->eventlogstuff, inst->window);
2915 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2917 /* This maps colour indices in inst->conf to those used in inst->cols. */
2918 static const int ww[] = {
2919 256, 257, 258, 259, 260, 261,
2920 0, 8, 1, 9, 2, 10, 3, 11,
2921 4, 12, 5, 13, 6, 14, 7, 15
2923 struct gui_data *inst = (struct gui_data *)data;
2924 char *title = dupcat(appname, " Reconfiguration", NULL);
2925 Conf *oldconf, *newconf;
2926 int i, j, need_size;
2928 assert(lenof(ww) == NCFGCOLOURS);
2930 if (inst->reconfiguring)
2933 inst->reconfiguring = TRUE;
2935 oldconf = inst->conf;
2936 newconf = conf_copy(inst->conf);
2938 if (do_config_box(title, newconf, 1,
2939 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2940 inst->conf = newconf;
2942 /* Pass new config data to the logging module */
2943 log_reconfig(inst->logctx, inst->conf);
2945 * Flush the line discipline's edit buffer in the case
2946 * where local editing has just been disabled.
2948 ldisc_configure(inst->ldisc, inst->conf);
2950 ldisc_send(inst->ldisc, NULL, 0, 0);
2951 /* Pass new config data to the terminal */
2952 term_reconfig(inst->term, inst->conf);
2953 /* Pass new config data to the back end */
2955 inst->back->reconfig(inst->backhandle, inst->conf);
2957 cache_conf_values(inst);
2960 * Just setting inst->conf is sufficient to cause colour
2961 * setting changes to appear on the next ESC]R palette
2962 * reset. But we should also check whether any colour
2963 * settings have been changed, and revert the ones that have
2964 * to the new default, on the assumption that the user is
2965 * most likely to want an immediate update.
2967 for (i = 0; i < NCFGCOLOURS; i++) {
2968 for (j = 0; j < 3; j++)
2969 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
2970 conf_get_int_int(newconf, CONF_colours, i*3+j))
2973 real_palette_set(inst, ww[i],
2974 conf_get_int_int(newconf,CONF_colours,i*3+0),
2975 conf_get_int_int(newconf,CONF_colours,i*3+1),
2976 conf_get_int_int(newconf,CONF_colours,i*3+2));
2979 * If the default background has changed, we must
2980 * repaint the space in between the window border
2981 * and the text area.
2984 set_window_background(inst);
2985 draw_backing_rect(inst);
2991 * If the scrollbar needs to be shown, hidden, or moved
2992 * from one end to the other of the window, do so now.
2994 if (conf_get_int(oldconf, CONF_scrollbar) !=
2995 conf_get_int(newconf, CONF_scrollbar)) {
2996 if (conf_get_int(newconf, CONF_scrollbar))
2997 gtk_widget_show(inst->sbar);
2999 gtk_widget_hide(inst->sbar);
3001 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3002 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3003 gtk_box_reorder_child(inst->hbox, inst->sbar,
3004 conf_get_int(newconf, CONF_scrollbar_on_left)
3009 * Change the window title, if required.
3011 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3012 conf_get_str(newconf, CONF_wintitle)))
3013 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3014 set_window_titles(inst);
3017 * Redo the whole tangled fonts and Unicode mess if
3020 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3021 conf_get_fontspec(newconf, CONF_font)->name) ||
3022 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3023 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3024 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3025 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3026 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3027 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3028 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3029 conf_get_str(newconf, CONF_line_codepage)) ||
3030 conf_get_int(oldconf, CONF_utf8_override) !=
3031 conf_get_int(newconf, CONF_utf8_override) ||
3032 conf_get_int(oldconf, CONF_vtmode) !=
3033 conf_get_int(newconf, CONF_vtmode) ||
3034 conf_get_int(oldconf, CONF_shadowbold) !=
3035 conf_get_int(newconf, CONF_shadowbold) ||
3036 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3037 conf_get_int(newconf, CONF_shadowboldoffset)) {
3038 setup_fonts_ucs(inst);
3044 * Resize the window.
3046 if (conf_get_int(oldconf, CONF_width) !=
3047 conf_get_int(newconf, CONF_width) ||
3048 conf_get_int(oldconf, CONF_height) !=
3049 conf_get_int(newconf, CONF_height) ||
3050 conf_get_int(oldconf, CONF_window_border) !=
3051 conf_get_int(newconf, CONF_window_border) ||
3053 set_geom_hints(inst);
3054 request_resize(inst, conf_get_int(newconf, CONF_width),
3055 conf_get_int(newconf, CONF_height));
3058 * The above will have caused a call to term_size() for
3059 * us if it happened. If the user has fiddled with only
3060 * the scrollback size, the above will not have
3061 * happened and we will need an explicit term_size()
3064 if (conf_get_int(oldconf, CONF_savelines) !=
3065 conf_get_int(newconf, CONF_savelines))
3066 term_size(inst->term, inst->term->rows, inst->term->cols,
3067 conf_get_int(newconf, CONF_savelines));
3070 term_invalidate(inst->term);
3073 * We do an explicit full redraw here to ensure the window
3074 * border has been redrawn as well as the text area.
3076 gtk_widget_queue_draw(inst->area);
3083 inst->reconfiguring = FALSE;
3086 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3089 * Re-execing ourself is not an exact science under Unix. I do
3090 * the best I can by using /proc/self/exe if available and by
3091 * assuming argv[0] can be found on $PATH if not.
3093 * Note that we also have to reconstruct the elements of the
3094 * original argv which gtk swallowed, since the user wants the
3095 * new session to appear on the same X display as the old one.
3103 * Collect the arguments with which to re-exec ourself.
3105 va_start(ap, fd_to_close);
3106 n = 2; /* progname and terminating NULL */
3107 n += inst->ngtkargs;
3108 while (va_arg(ap, char *) != NULL)
3112 args = snewn(n, char *);
3113 args[0] = inst->progname;
3115 for (i = 0; i < inst->ngtkargs; i++)
3116 args[i+1] = inst->gtkargvstart[i];
3119 va_start(ap, fd_to_close);
3120 while ((args[i++] = va_arg(ap, char *)) != NULL);
3126 * Do the double fork.
3139 } else if (pid2 > 0) {
3141 * First child has successfully forked second child. My
3142 * Work Here Is Done. Note the use of _exit rather than
3143 * exit: the latter appears to cause destroy messages
3144 * to be sent to the X server. I suspect gtk uses
3151 * If we reach here, we are the second child, so we now
3152 * actually perform the exec.
3154 if (fd_to_close >= 0)
3157 execv("/proc/self/exe", args);
3158 execvp(inst->progname, args);
3164 waitpid(pid, &status, 0);
3169 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3171 struct gui_data *inst = (struct gui_data *)gdata;
3173 * For this feature we must marshal conf and (possibly) pty_argv
3174 * into a byte stream, create a pipe, and send this byte stream
3175 * to the child through the pipe.
3177 int i, ret, sersize, size;
3182 if (pipe(pipefd) < 0) {
3187 size = sersize = conf_serialised_size(inst->conf);
3188 if (use_pty_argv && pty_argv) {
3189 for (i = 0; pty_argv[i]; i++)
3190 size += strlen(pty_argv[i]) + 1;
3193 data = snewn(size, char);
3194 conf_serialise(inst->conf, data);
3195 if (use_pty_argv && pty_argv) {
3197 for (i = 0; pty_argv[i]; i++) {
3198 strcpy(data + p, pty_argv[i]);
3199 p += strlen(pty_argv[i]) + 1;
3204 sprintf(option, "---[%d,%d]", pipefd[0], size);
3205 fcntl(pipefd[0], F_SETFD, 0);
3206 fork_and_exec_self(inst, pipefd[1], option, NULL);
3210 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3213 perror("write to pipe");
3218 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
3220 int fd, i, ret, size, size_used;
3223 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3224 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3228 data = snewn(size, char);
3230 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3233 perror("read from pipe");
3235 } else if (i < size) {
3236 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3241 size_used = conf_deserialise(conf, data, size);
3242 if (use_pty_argv && size > size_used) {
3246 while (i < size && data[i]) i++;
3248 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3255 pty_argv = snewn(n+1, char *);
3261 while (i < size && data[i]) i++;
3264 pty_argv[n++] = dupstr(p);
3271 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3273 struct gui_data *inst = (struct gui_data *)data;
3275 fork_and_exec_self(inst, -1, NULL);
3278 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3280 struct gui_data *inst = (struct gui_data *)data;
3283 logevent(inst, "----- Session restarted -----");
3284 term_pwron(inst->term, FALSE);
3285 start_backend(inst);
3286 inst->exited = FALSE;
3290 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3292 struct gui_data *inst = (struct gui_data *)data;
3293 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3295 fork_and_exec_self(inst, -1, "-load", str, NULL);
3298 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3300 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3305 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3307 struct gui_data *inst = (struct gui_data *)data;
3308 struct sesslist sesslist;
3311 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3312 (GtkCallback)gtk_widget_destroy, NULL);
3314 get_sesslist(&sesslist, TRUE);
3315 /* skip sesslist.sessions[0] == Default Settings */
3316 for (i = 1; i < sesslist.nsessions; i++) {
3317 GtkWidget *menuitem =
3318 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3319 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3320 gtk_widget_show(menuitem);
3321 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3322 dupstr(sesslist.sessions[i]));
3323 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3324 GTK_SIGNAL_FUNC(saved_session_menuitem),
3326 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3327 GTK_SIGNAL_FUNC(saved_session_freedata),
3330 if (sesslist.nsessions <= 1) {
3331 GtkWidget *menuitem =
3332 gtk_menu_item_new_with_label("(No sessions)");
3333 gtk_widget_set_sensitive(menuitem, FALSE);
3334 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3335 gtk_widget_show(menuitem);
3337 get_sesslist(&sesslist, FALSE); /* free up */
3340 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3344 GdkBitmap *iconmask;
3345 #if GTK_CHECK_VERSION(2,0,0)
3353 gtk_widget_realize(window);
3354 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3355 NULL, (gchar **)icon[0]);
3356 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3358 #if GTK_CHECK_VERSION(2,0,0)
3360 for (n = 0; n < n_icon; n++) {
3362 g_list_append(iconlist,
3363 gdk_pixbuf_new_from_xpm_data((const gchar **)
3366 gdk_window_set_icon_list(window->window, iconlist);
3370 void update_specials_menu(void *frontend)
3372 struct gui_data *inst = (struct gui_data *)frontend;
3374 const struct telnet_special *specials;
3377 specials = inst->back->get_specials(inst->backhandle);
3381 /* I believe this disposes of submenus too. */
3382 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3383 (GtkCallback)gtk_widget_destroy, NULL);
3386 GtkWidget *menu = inst->specialsmenu;
3387 /* A lame "stack" for submenus that will do for now. */
3388 GtkWidget *saved_menu = NULL;
3390 for (i = 0; nesting > 0; i++) {
3391 GtkWidget *menuitem = NULL;
3392 switch (specials[i].code) {
3394 assert (nesting < 2);
3395 saved_menu = menu; /* XXX lame stacking */
3396 menu = gtk_menu_new();
3397 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3398 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3399 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3400 gtk_widget_show(menuitem);
3407 menu = saved_menu; /* XXX lame stacking */
3412 menuitem = gtk_menu_item_new();
3415 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3416 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3417 GINT_TO_POINTER(specials[i].code));
3418 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3419 GTK_SIGNAL_FUNC(special_menuitem), inst);
3423 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3424 gtk_widget_show(menuitem);
3427 gtk_widget_show(inst->specialsitem1);
3428 gtk_widget_show(inst->specialsitem2);
3430 gtk_widget_hide(inst->specialsitem1);
3431 gtk_widget_hide(inst->specialsitem2);
3435 static void start_backend(struct gui_data *inst)
3437 extern Backend *select_backend(Conf *conf);
3442 inst->back = select_backend(inst->conf);
3444 error = inst->back->init((void *)inst, &inst->backhandle,
3446 conf_get_str(inst->conf, CONF_host),
3447 conf_get_int(inst->conf, CONF_port),
3449 conf_get_int(inst->conf, CONF_tcp_nodelay),
3450 conf_get_int(inst->conf, CONF_tcp_keepalives));
3453 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3454 conf_get_str(inst->conf, CONF_host), error);
3455 inst->exited = TRUE;
3456 fatal_message_box(inst->window, msg);
3461 s = conf_get_str(inst->conf, CONF_wintitle);
3463 set_title_and_icon(inst, s, s);
3465 char *title = make_default_wintitle(realhost);
3466 set_title_and_icon(inst, title, title);
3471 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3473 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3476 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
3479 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3482 int pt_main(int argc, char **argv)
3484 extern int cfgbox(Conf *conf);
3485 struct gui_data *inst;
3487 setlocale(LC_CTYPE, "");
3490 * Create an instance structure and initialise to zeroes
3492 inst = snew(struct gui_data);
3493 memset(inst, 0, sizeof(*inst));
3494 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3495 inst->busy_status = BUSY_NOT;
3496 inst->conf = conf_new();
3497 inst->wintitle = inst->icontitle = NULL;
3499 /* defer any child exit handling until we're ready to deal with
3501 block_signal(SIGCHLD, 1);
3503 inst->progname = argv[0];
3505 * Copy the original argv before letting gtk_init fiddle with
3506 * it. It will be required later.
3510 inst->gtkargvstart = snewn(argc-1, char *);
3511 for (i = 1; i < argc; i++)
3512 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3514 gtk_init(&argc, &argv);
3515 inst->ngtkargs = oldargc - argc;
3518 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3519 read_dupsession_data(inst, inst->conf, argv[1]);
3520 /* Splatter this argument so it doesn't clutter a ps listing */
3521 memset(argv[1], 0, strlen(argv[1]));
3523 /* By default, we bring up the config dialog, rather than launching
3524 * a session. This gets set to TRUE if something happens to change
3525 * that (e.g., a hostname is specified on the command-line). */
3526 int allow_launch = FALSE;
3527 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
3528 exit(1); /* pre-defaults pass to get -class */
3529 do_defaults(NULL, inst->conf);
3530 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
3531 exit(1); /* post-defaults, do everything */
3533 cmdline_run_saved(inst->conf);
3536 allow_launch = TRUE;
3538 if ((!allow_launch || !conf_launchable(inst->conf)) &&
3539 !cfgbox(inst->conf))
3540 exit(0); /* config box hit Cancel */
3543 if (!compound_text_atom)
3544 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3545 if (!utf8_string_atom)
3546 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3548 inst->area = gtk_drawing_area_new();
3550 #if GTK_CHECK_VERSION(2,0,0)
3551 inst->imc = gtk_im_multicontext_new();
3554 setup_fonts_ucs(inst);
3557 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3559 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
3561 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
3562 winclass, winclass);
3566 * Set up the colour map.
3568 palette_reset(inst);
3570 inst->width = conf_get_int(inst->conf, CONF_width);
3571 inst->height = conf_get_int(inst->conf, CONF_height);
3572 cache_conf_values(inst);
3574 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3575 inst->font_width * inst->width + 2*inst->window_border,
3576 inst->font_height * inst->height + 2*inst->window_border);
3577 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3578 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3579 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3581 * We always create the scrollbar; it remains invisible if
3582 * unwanted, so we can pop it up quickly if it suddenly becomes
3585 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
3586 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3587 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3588 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
3589 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3591 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3593 set_geom_hints(inst);
3595 gtk_widget_show(inst->area);
3596 if (conf_get_int(inst->conf, CONF_scrollbar))
3597 gtk_widget_show(inst->sbar);
3599 gtk_widget_hide(inst->sbar);
3600 gtk_widget_show(GTK_WIDGET(inst->hbox));
3603 int x = inst->xpos, y = inst->ypos;
3605 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3606 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3607 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3608 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3609 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3612 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3613 GTK_SIGNAL_FUNC(destroy), inst);
3614 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3615 GTK_SIGNAL_FUNC(delete_window), inst);
3616 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3617 GTK_SIGNAL_FUNC(key_event), inst);
3618 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3619 GTK_SIGNAL_FUNC(key_event), inst);
3620 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3621 GTK_SIGNAL_FUNC(focus_event), inst);
3622 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3623 GTK_SIGNAL_FUNC(focus_event), inst);
3624 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3625 GTK_SIGNAL_FUNC(configure_area), inst);
3626 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3627 GTK_SIGNAL_FUNC(expose_area), inst);
3628 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3629 GTK_SIGNAL_FUNC(button_event), inst);
3630 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3631 GTK_SIGNAL_FUNC(button_event), inst);
3632 #if GTK_CHECK_VERSION(2,0,0)
3633 gtk_signal_connect(GTK_OBJECT(inst->area), "scroll_event",
3634 GTK_SIGNAL_FUNC(scroll_event), inst);
3636 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3637 GTK_SIGNAL_FUNC(motion_event), inst);
3638 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3639 GTK_SIGNAL_FUNC(selection_received), inst);
3640 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3641 GTK_SIGNAL_FUNC(selection_get), inst);
3642 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3643 GTK_SIGNAL_FUNC(selection_clear), inst);
3644 #if GTK_CHECK_VERSION(2,0,0)
3645 g_signal_connect(G_OBJECT(inst->imc), "commit",
3646 G_CALLBACK(input_method_commit_event), inst);
3648 if (conf_get_int(inst->conf, CONF_scrollbar))
3649 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3650 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3651 gtk_widget_add_events(GTK_WIDGET(inst->area),
3652 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3653 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3654 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3657 extern const char *const *const main_icon[];
3658 extern const int n_main_icon;
3659 set_window_icon(inst->window, main_icon, n_main_icon);
3662 gtk_widget_show(inst->window);
3664 set_window_background(inst);
3667 * Set up the Ctrl+rightclick context menu.
3670 GtkWidget *menuitem;
3672 extern const int use_event_log, new_session, saved_sessions;
3674 inst->menu = gtk_menu_new();
3676 #define MKMENUITEM(title, func) do \
3678 menuitem = gtk_menu_item_new_with_label(title); \
3679 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3680 gtk_widget_show(menuitem); \
3681 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3682 GTK_SIGNAL_FUNC(func), inst); \
3685 #define MKSUBMENU(title) do \
3687 menuitem = gtk_menu_item_new_with_label(title); \
3688 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3689 gtk_widget_show(menuitem); \
3692 #define MKSEP() do \
3694 menuitem = gtk_menu_item_new(); \
3695 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3696 gtk_widget_show(menuitem); \
3700 MKMENUITEM("New Session...", new_session_menuitem);
3701 MKMENUITEM("Restart Session", restart_session_menuitem);
3702 inst->restartitem = menuitem;
3703 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3704 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3705 if (saved_sessions) {
3706 inst->sessionsmenu = gtk_menu_new();
3707 /* sessionsmenu will be updated when it's invoked */
3708 /* XXX is this the right way to do dynamic menus in Gtk? */
3709 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3710 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3711 inst->sessionsmenu);
3714 MKMENUITEM("Change Settings...", change_settings_menuitem);
3717 MKMENUITEM("Event Log", event_log_menuitem);
3718 MKSUBMENU("Special Commands");
3719 inst->specialsmenu = gtk_menu_new();
3720 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3721 inst->specialsitem1 = menuitem;
3723 inst->specialsitem2 = menuitem;
3724 gtk_widget_hide(inst->specialsitem1);
3725 gtk_widget_hide(inst->specialsitem2);
3726 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3727 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3728 MKMENUITEM("Copy All", copy_all_menuitem);
3730 s = dupcat("About ", appname, NULL);
3731 MKMENUITEM(s, about_menuitem);
3738 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3739 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3740 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3741 inst->blankcursor = make_mouse_ptr(inst, -1);
3742 make_mouse_ptr(inst, -2); /* clean up cursor font */
3743 inst->currcursor = inst->textcursor;
3744 show_mouseptr(inst, 1);
3746 inst->eventlogstuff = eventlogstuff_new();
3748 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
3749 inst->logctx = log_init(inst, inst->conf);
3750 term_provide_logctx(inst->term, inst->logctx);
3754 term_size(inst->term, inst->height, inst->width,
3755 conf_get_int(inst->conf, CONF_savelines));
3757 start_backend(inst);
3759 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3761 /* now we're reday to deal with the child exit handler being
3763 block_signal(SIGCHLD, 0);
3766 * Block SIGPIPE: if we attempt Duplicate Session or similar
3767 * and it falls over in some way, we certainly don't want
3768 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3769 * this _after_ (at least pterm) forks off its child process,
3770 * since the child wants SIGPIPE handled in the usual way.
3772 block_signal(SIGPIPE, 1);
3774 inst->exited = FALSE;