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 */
34 #define MAY_REFER_TO_GTK_IN_HEADERS
40 #define CAT2(x,y) x ## y
41 #define CAT(x,y) CAT2(x,y)
42 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
44 #if GTK_CHECK_VERSION(2,0,0)
45 ASSERT(sizeof(long) <= sizeof(gsize));
46 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
47 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
49 ASSERT(sizeof(long) <= sizeof(gpointer));
50 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
51 #define GPOINTER_TO_LONG(p) ((long)(p))
54 /* Colours come in two flavours: configurable, and xterm-extended. */
55 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
56 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
58 GdkAtom compound_text_atom, utf8_string_atom;
60 extern char **pty_argv; /* declared in pty.c */
61 extern int use_pty_argv;
64 * Timers are global across all sessions (even if we were handling
65 * multiple sessions, which we aren't), so the current timer ID is
68 static guint timer_id = 0;
71 GtkWidget *window, *area, *sbar;
73 GtkAdjustment *sbar_adjust;
74 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
76 GtkWidget *sessionsmenu;
78 #if GTK_CHECK_VERSION(2,0,0)
81 unifont *fonts[4]; /* normal, bold, wide, widebold */
82 int xpos, ypos, gotpos, gravity;
83 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
84 GdkColor cols[NALLCOLOURS];
86 wchar_t *pastein_data;
89 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
90 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
91 int font_width, font_height;
96 guint toplevel_callback_idle_id;
101 int master_fd, master_func_id;
108 struct unicode_data ucsdata;
111 char *progname, **gtkargvstart;
113 guint32 input_event_time; /* Timestamp of the most recent input event. */
115 /* Cached things out of conf that we refer to a lot */
121 static void cache_conf_values(struct gui_data *inst)
123 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
124 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
125 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
130 struct gui_data *inst;
133 static int send_raw_mouse;
135 static char *app_name = "pterm";
137 static void start_backend(struct gui_data *inst);
138 static void exit_callback(void *vinst);
140 char *x_get_default(const char *key)
142 return XGetDefault(GDK_DISPLAY(), app_name, key);
145 void connection_fatal(void *frontend, char *p, ...)
147 struct gui_data *inst = (struct gui_data *)frontend;
152 msg = dupvprintf(p, ap);
154 fatal_message_box(inst->window, msg);
157 queue_toplevel_callback(exit_callback, inst);
161 * Default settings that are specific to pterm.
163 FontSpec *platform_default_fontspec(const char *name)
165 if (!strcmp(name, "Font"))
166 return fontspec_new("server:fixed");
168 return fontspec_new("");
171 Filename *platform_default_filename(const char *name)
173 if (!strcmp(name, "LogFileName"))
174 return filename_from_str("putty.log");
176 return filename_from_str("");
179 char *platform_default_s(const char *name)
181 if (!strcmp(name, "SerialLine"))
182 return dupstr("/dev/ttyS0");
186 int platform_default_i(const char *name, int def)
188 if (!strcmp(name, "CloseOnExit"))
189 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
190 if (!strcmp(name, "WinNameAlways"))
191 return 0; /* X natively supports icon titles, so use 'em by default */
195 /* Dummy routine, only required in plink. */
196 void ldisc_update(void *frontend, int echo, int edit)
200 char *get_ttymode(void *frontend, const char *mode)
202 struct gui_data *inst = (struct gui_data *)frontend;
203 return term_get_ttymode(inst->term, mode);
206 int from_backend(void *frontend, int is_stderr, const char *data, int len)
208 struct gui_data *inst = (struct gui_data *)frontend;
209 return term_data(inst->term, is_stderr, data, len);
212 int from_backend_untrusted(void *frontend, const char *data, int len)
214 struct gui_data *inst = (struct gui_data *)frontend;
215 return term_data_untrusted(inst->term, data, len);
218 int from_backend_eof(void *frontend)
220 return TRUE; /* do respond to incoming EOF with outgoing */
223 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
225 struct gui_data *inst = (struct gui_data *)p->frontend;
227 ret = cmdline_get_passwd_input(p, in, inlen);
229 ret = term_get_userpass_input(inst->term, p, in, inlen);
233 void logevent(void *frontend, const char *string)
235 struct gui_data *inst = (struct gui_data *)frontend;
237 log_eventlog(inst->logctx, string);
239 logevent_dlg(inst->eventlogstuff, string);
242 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
244 struct gui_data *inst = (struct gui_data *)frontend;
247 return inst->font_height;
249 return inst->font_width;
253 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
254 * into a cooked one (SELECT, EXTEND, PASTE).
256 * In Unix, this is not configurable; the X button arrangement is
257 * rock-solid across all applications, everyone has a three-button
258 * mouse or a means of faking it, and there is no need to switch
259 * buttons around at all.
261 static Mouse_Button translate_button(Mouse_Button button)
263 /* struct gui_data *inst = (struct gui_data *)frontend; */
265 if (button == MBT_LEFT)
267 if (button == MBT_MIDDLE)
269 if (button == MBT_RIGHT)
271 return 0; /* shouldn't happen */
275 * Return the top-level GtkWindow associated with a particular
276 * front end instance.
278 void *get_window(void *frontend)
280 struct gui_data *inst = (struct gui_data *)frontend;
285 * Minimise or restore the window in response to a server-side
288 void set_iconic(void *frontend, int iconic)
291 * GTK 1.2 doesn't know how to do this.
293 #if GTK_CHECK_VERSION(2,0,0)
294 struct gui_data *inst = (struct gui_data *)frontend;
296 gtk_window_iconify(GTK_WINDOW(inst->window));
298 gtk_window_deiconify(GTK_WINDOW(inst->window));
303 * Move the window in response to a server-side request.
305 void move_window(void *frontend, int x, int y)
307 struct gui_data *inst = (struct gui_data *)frontend;
309 * I assume that when the GTK version of this call is available
310 * we should use it. Not sure how it differs from the GDK one,
313 #if GTK_CHECK_VERSION(2,0,0)
314 gtk_window_move(GTK_WINDOW(inst->window), x, y);
316 gdk_window_move(inst->window->window, x, y);
321 * Move the window to the top or bottom of the z-order in response
322 * to a server-side request.
324 void set_zorder(void *frontend, int top)
326 struct gui_data *inst = (struct gui_data *)frontend;
328 gdk_window_raise(inst->window->window);
330 gdk_window_lower(inst->window->window);
334 * Refresh the window in response to a server-side request.
336 void refresh_window(void *frontend)
338 struct gui_data *inst = (struct gui_data *)frontend;
339 term_invalidate(inst->term);
343 * Maximise or restore the window in response to a server-side
346 void set_zoomed(void *frontend, int zoomed)
349 * GTK 1.2 doesn't know how to do this.
351 #if GTK_CHECK_VERSION(2,0,0)
352 struct gui_data *inst = (struct gui_data *)frontend;
354 gtk_window_maximize(GTK_WINDOW(inst->window));
356 gtk_window_unmaximize(GTK_WINDOW(inst->window));
361 * Report whether the window is iconic, for terminal reports.
363 int is_iconic(void *frontend)
365 struct gui_data *inst = (struct gui_data *)frontend;
366 return !gdk_window_is_viewable(inst->window->window);
370 * Report the window's position, for terminal reports.
372 void get_window_pos(void *frontend, int *x, int *y)
374 struct gui_data *inst = (struct gui_data *)frontend;
376 * I assume that when the GTK version of this call is available
377 * we should use it. Not sure how it differs from the GDK one,
380 #if GTK_CHECK_VERSION(2,0,0)
381 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
383 gdk_window_get_position(inst->window->window, x, y);
388 * Report the window's pixel size, for terminal reports.
390 void get_window_pixels(void *frontend, int *x, int *y)
392 struct gui_data *inst = (struct gui_data *)frontend;
394 * I assume that when the GTK version of this call is available
395 * we should use it. Not sure how it differs from the GDK one,
398 #if GTK_CHECK_VERSION(2,0,0)
399 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
401 gdk_window_get_size(inst->window->window, x, y);
406 * Return the window or icon title.
408 char *get_window_title(void *frontend, int icon)
410 struct gui_data *inst = (struct gui_data *)frontend;
411 return icon ? inst->icontitle : inst->wintitle;
414 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
416 struct gui_data *inst = (struct gui_data *)data;
417 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
418 if (!reallyclose(inst))
424 static void update_mouseptr(struct gui_data *inst)
426 switch (inst->busy_status) {
428 if (!inst->mouseptr_visible) {
429 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
430 } else if (send_raw_mouse) {
431 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
433 gdk_window_set_cursor(inst->area->window, inst->textcursor);
436 case BUSY_WAITING: /* XXX can we do better? */
438 /* We always display these cursors. */
439 gdk_window_set_cursor(inst->area->window, inst->waitcursor);
446 static void show_mouseptr(struct gui_data *inst, int show)
448 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
450 inst->mouseptr_visible = show;
451 update_mouseptr(inst);
454 void draw_backing_rect(struct gui_data *inst)
456 GdkGC *gc = gdk_gc_new(inst->area->window);
457 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
458 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
459 inst->width * inst->font_width + 2*inst->window_border,
460 inst->height * inst->font_height + 2*inst->window_border);
464 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
466 struct gui_data *inst = (struct gui_data *)data;
467 int w, h, need_size = 0;
470 * See if the terminal size has changed, in which case we must
471 * let the terminal know.
473 w = (event->width - 2*inst->window_border) / inst->font_width;
474 h = (event->height - 2*inst->window_border) / inst->font_height;
475 if (w != inst->width || h != inst->height) {
478 conf_set_int(inst->conf, CONF_width, inst->width);
479 conf_set_int(inst->conf, CONF_height, inst->height);
484 gdk_pixmap_unref(inst->pixmap);
488 inst->pixmap = gdk_pixmap_new(widget->window,
489 (w * inst->font_width + 2*inst->window_border),
490 (h * inst->font_height + 2*inst->window_border), -1);
492 draw_backing_rect(inst);
494 if (need_size && inst->term) {
495 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
499 term_invalidate(inst->term);
501 #if GTK_CHECK_VERSION(2,0,0)
502 gtk_im_context_set_client_window(inst->imc, widget->window);
508 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
510 struct gui_data *inst = (struct gui_data *)data;
513 * Pass the exposed rectangle to terminal.c, which will call us
514 * back to do the actual painting.
517 gdk_draw_pixmap(widget->window,
518 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
520 event->area.x, event->area.y,
521 event->area.x, event->area.y,
522 event->area.width, event->area.height);
527 #define KEY_PRESSED(k) \
528 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
530 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
532 struct gui_data *inst = (struct gui_data *)data;
534 wchar_t ucsoutput[2];
535 int ucsval, start, end, special, output_charset, use_ucsoutput;
536 int nethack_mode, app_keypad_mode;
538 /* Remember the timestamp. */
539 inst->input_event_time = event->time;
541 /* By default, nothing is generated. */
543 special = use_ucsoutput = FALSE;
544 output_charset = CS_ISO8859_1;
547 * If Alt is being released after typing an Alt+numberpad
548 * sequence, we should generate the code that was typed.
550 * Note that we only do this if more than one key was actually
551 * pressed - I don't think Alt+NumPad4 should be ^D or that
552 * Alt+NumPad3 should be ^C, for example. There's no serious
553 * inconvenience in having to type a zero before a single-digit
556 if (event->type == GDK_KEY_RELEASE) {
557 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
558 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
559 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
561 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
564 * FIXME: we might usefully try to do something clever here
565 * about interpreting the generated key code in a way that's
566 * appropriate to the line code page.
568 output[0] = inst->alt_keycode;
572 #if GTK_CHECK_VERSION(2,0,0)
573 if (gtk_im_context_filter_keypress(inst->imc, event))
578 if (event->type == GDK_KEY_PRESS) {
582 printf("keypress: keyval = %04x, state = %08x; string =",
583 event->keyval, event->state);
584 for (i = 0; event->string[i]; i++)
585 printf(" %02x", (unsigned char) event->string[i]);
591 * NYI: Compose key (!!! requires Unicode faff before even trying)
595 * If Alt has just been pressed, we start potentially
596 * accumulating an Alt+numberpad code. We do this by
597 * setting alt_keycode to -1 (nothing yet but plausible).
599 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
600 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
601 inst->alt_keycode = -1;
602 inst->alt_digits = 0;
603 goto done; /* this generates nothing else */
607 * If we're seeing a numberpad key press with Mod1 down,
608 * consider adding it to alt_keycode if that's sensible.
609 * Anything _else_ with Mod1 down cancels any possibility
610 * of an ALT keycode: we set alt_keycode to -2.
612 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
614 switch (event->keyval) {
615 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
616 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
617 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
618 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
619 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
620 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
621 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
622 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
623 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
624 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
627 inst->alt_keycode = -2; /* it's invalid */
630 printf("Adding digit %d to keycode %d", digit,
633 if (inst->alt_keycode == -1)
634 inst->alt_keycode = digit; /* one-digit code */
636 inst->alt_keycode = inst->alt_keycode * 10 + digit;
639 printf(" gives new code %d\n", inst->alt_keycode);
641 /* Having used this digit, we now do nothing more with it. */
647 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
650 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
651 term_scroll(inst->term, 0, -inst->height/2);
654 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
655 term_scroll(inst->term, 0, -1);
658 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
659 term_scroll(inst->term, 0, +inst->height/2);
662 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
663 term_scroll(inst->term, 0, +1);
668 * Neither does Shift-Ins.
670 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
676 use_ucsoutput = FALSE;
678 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
679 app_keypad_mode = (inst->term->app_keypad_keys &&
680 !conf_get_int(inst->conf, CONF_no_applic_k));
682 /* ALT+things gives leading Escape. */
684 #if !GTK_CHECK_VERSION(2,0,0)
686 * In vanilla X, and hence also GDK 1.2, the string received
687 * as part of a keyboard event is assumed to be in
688 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
689 * but it's true: see the man page for XLookupString(3) for
692 output_charset = CS_ISO8859_1;
693 strncpy(output+1, event->string, lenof(output)-1);
696 * Most things can now be passed to
697 * gtk_im_context_filter_keypress without breaking anything
698 * below this point. An exception is the numeric keypad if
699 * we're in Nethack or application mode: the IM will eat
700 * numeric keypad presses if Num Lock is on, but we don't want
703 if (app_keypad_mode &&
704 (event->keyval == GDK_Num_Lock ||
705 event->keyval == GDK_KP_Divide ||
706 event->keyval == GDK_KP_Multiply ||
707 event->keyval == GDK_KP_Subtract ||
708 event->keyval == GDK_KP_Add ||
709 event->keyval == GDK_KP_Enter ||
710 event->keyval == GDK_KP_0 ||
711 event->keyval == GDK_KP_Insert ||
712 event->keyval == GDK_KP_1 ||
713 event->keyval == GDK_KP_End ||
714 event->keyval == GDK_KP_2 ||
715 event->keyval == GDK_KP_Down ||
716 event->keyval == GDK_KP_3 ||
717 event->keyval == GDK_KP_Page_Down ||
718 event->keyval == GDK_KP_4 ||
719 event->keyval == GDK_KP_Left ||
720 event->keyval == GDK_KP_5 ||
721 event->keyval == GDK_KP_Begin ||
722 event->keyval == GDK_KP_6 ||
723 event->keyval == GDK_KP_Right ||
724 event->keyval == GDK_KP_7 ||
725 event->keyval == GDK_KP_Home ||
726 event->keyval == GDK_KP_8 ||
727 event->keyval == GDK_KP_Up ||
728 event->keyval == GDK_KP_9 ||
729 event->keyval == GDK_KP_Page_Up ||
730 event->keyval == GDK_KP_Decimal ||
731 event->keyval == GDK_KP_Delete)) {
732 /* app keypad; do nothing */
733 } else if (nethack_mode &&
734 (event->keyval == GDK_KP_1 ||
735 event->keyval == GDK_KP_End ||
736 event->keyval == GDK_KP_2 ||
737 event->keyval == GDK_KP_Down ||
738 event->keyval == GDK_KP_3 ||
739 event->keyval == GDK_KP_Page_Down ||
740 event->keyval == GDK_KP_4 ||
741 event->keyval == GDK_KP_Left ||
742 event->keyval == GDK_KP_5 ||
743 event->keyval == GDK_KP_Begin ||
744 event->keyval == GDK_KP_6 ||
745 event->keyval == GDK_KP_Right ||
746 event->keyval == GDK_KP_7 ||
747 event->keyval == GDK_KP_Home ||
748 event->keyval == GDK_KP_8 ||
749 event->keyval == GDK_KP_Up ||
750 event->keyval == GDK_KP_9 ||
751 event->keyval == GDK_KP_Page_Up)) {
752 /* nethack mode; do nothing */
754 if (gtk_im_context_filter_keypress(inst->imc, event))
759 * GDK 2.0 arranges to have done some translation for us: in
760 * GDK 2.0, event->string is encoded in the current locale.
762 * So we use the standard C library function mbstowcs() to
763 * convert from the current locale into Unicode; from there
764 * we can convert to whatever PuTTY is currently working in.
765 * (In fact I convert straight back to UTF-8 from
766 * wide-character Unicode, for the sake of simplicity: that
767 * way we can still use exactly the same code to manipulate
768 * the string, such as prefixing ESC.)
770 output_charset = CS_UTF8;
772 wchar_t widedata[32];
777 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
778 event->string, strlen(event->string),
779 widedata, lenof(widedata)-1);
782 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
783 CS_UTF8, NULL, NULL, 0);
784 output[1+ulen] = '\0';
789 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
790 ucsoutput[0] = '\033';
791 ucsoutput[1] = ucsval;
792 use_ucsoutput = TRUE;
795 output[lenof(output)-1] = '\0';
796 end = strlen(output);
798 if (event->state & GDK_MOD1_MASK) {
800 if (end == 1) end = 0;
804 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
805 if (!output[1] && event->keyval == '`' &&
806 (event->state & GDK_CONTROL_MASK)) {
808 use_ucsoutput = FALSE;
812 /* Control-Break sends a Break special to the backend */
813 if (event->keyval == GDK_Break &&
814 (event->state & GDK_CONTROL_MASK)) {
816 inst->back->special(inst->backhandle, TS_BRK);
820 /* We handle Return ourselves, because it needs to be flagged as
821 * special to ldisc. */
822 if (event->keyval == GDK_Return) {
824 use_ucsoutput = FALSE;
829 /* Control-2, Control-Space and Control-@ are NUL */
831 (event->keyval == ' ' || event->keyval == '2' ||
832 event->keyval == '@') &&
833 (event->state & (GDK_SHIFT_MASK |
834 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
836 use_ucsoutput = FALSE;
840 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
841 if (!output[1] && event->keyval == ' ' &&
842 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
843 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
845 output_charset = CS_ISO8859_1;
846 use_ucsoutput = FALSE;
850 /* We don't let GTK tell us what Backspace is! We know better. */
851 if (event->keyval == GDK_BackSpace &&
852 !(event->state & GDK_SHIFT_MASK)) {
853 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
855 use_ucsoutput = FALSE;
859 /* For Shift Backspace, do opposite of what is configured. */
860 if (event->keyval == GDK_BackSpace &&
861 (event->state & GDK_SHIFT_MASK)) {
862 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
864 use_ucsoutput = FALSE;
869 /* Shift-Tab is ESC [ Z */
870 if (event->keyval == GDK_ISO_Left_Tab ||
871 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
872 end = 1 + sprintf(output+1, "\033[Z");
873 use_ucsoutput = FALSE;
875 /* And normal Tab is Tab, if the keymap hasn't already told us.
876 * (Curiously, at least one version of the MacOS 10.5 X server
877 * doesn't translate Tab for us. */
878 if (event->keyval == GDK_Tab && end <= 1) {
884 * NetHack keypad mode.
888 switch (event->keyval) {
889 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
890 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
891 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
892 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
893 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
894 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
895 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
896 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
897 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
901 if (event->state & GDK_CONTROL_MASK)
903 else if (event->state & GDK_SHIFT_MASK)
907 use_ucsoutput = FALSE;
913 * Application keypad mode.
915 if (app_keypad_mode) {
917 switch (event->keyval) {
918 case GDK_Num_Lock: xkey = 'P'; break;
919 case GDK_KP_Divide: xkey = 'Q'; break;
920 case GDK_KP_Multiply: xkey = 'R'; break;
921 case GDK_KP_Subtract: xkey = 'S'; break;
923 * Keypad + is tricky. It covers a space that would
924 * be taken up on the VT100 by _two_ keys; so we
925 * let Shift select between the two. Worse still,
926 * in xterm function key mode we change which two...
929 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
930 if (event->state & GDK_SHIFT_MASK)
934 } else if (event->state & GDK_SHIFT_MASK)
939 case GDK_KP_Enter: xkey = 'M'; break;
940 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
941 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
942 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
943 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
944 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
945 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
946 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
947 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
948 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
949 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
950 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
953 if (inst->term->vt52_mode) {
954 if (xkey >= 'P' && xkey <= 'S')
955 end = 1 + sprintf(output+1, "\033%c", xkey);
957 end = 1 + sprintf(output+1, "\033?%c", xkey);
959 end = 1 + sprintf(output+1, "\033O%c", xkey);
960 use_ucsoutput = FALSE;
966 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
967 * for integer decimal nn.)
969 * We also deal with the weird ones here. Linux VCs replace F1
970 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
971 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
976 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
977 switch (event->keyval) {
979 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
982 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
985 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
988 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
991 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
994 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
997 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1000 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1003 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1006 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1039 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1040 case GDK_Home: case GDK_KP_Home:
1043 case GDK_Insert: case GDK_KP_Insert:
1046 case GDK_Delete: case GDK_KP_Delete:
1049 case GDK_End: case GDK_KP_End:
1052 case GDK_Page_Up: case GDK_KP_Page_Up:
1055 case GDK_Page_Down: case GDK_KP_Page_Down:
1059 /* Reorder edit keys to physical order */
1060 if (funky_type == FUNKY_VT400 && code <= 6)
1061 code = "\0\2\1\4\5\3\6"[code];
1063 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1064 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1065 use_ucsoutput = FALSE;
1069 if (funky_type == FUNKY_SCO && /* SCO function keys */
1070 code >= 11 && code <= 34) {
1071 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1073 switch (event->keyval) {
1074 case GDK_F1: index = 0; break;
1075 case GDK_F2: index = 1; break;
1076 case GDK_F3: index = 2; break;
1077 case GDK_F4: index = 3; break;
1078 case GDK_F5: index = 4; break;
1079 case GDK_F6: index = 5; break;
1080 case GDK_F7: index = 6; break;
1081 case GDK_F8: index = 7; break;
1082 case GDK_F9: index = 8; break;
1083 case GDK_F10: index = 9; break;
1084 case GDK_F11: index = 10; break;
1085 case GDK_F12: index = 11; break;
1087 if (event->state & GDK_SHIFT_MASK) index += 12;
1088 if (event->state & GDK_CONTROL_MASK) index += 24;
1089 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1090 use_ucsoutput = FALSE;
1093 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1094 code >= 1 && code <= 6) {
1095 char codes[] = "HL.FIG";
1100 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1102 use_ucsoutput = FALSE;
1105 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1106 code >= 11 && code <= 24) {
1112 if (inst->term->vt52_mode)
1113 end = 1 + sprintf(output+1,
1114 "\x1B%c", code + 'P' - 11 - offt);
1116 end = 1 + sprintf(output+1,
1117 "\x1BO%c", code + 'P' - 11 - offt);
1118 use_ucsoutput = FALSE;
1121 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1122 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1123 use_ucsoutput = FALSE;
1126 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1127 if (inst->term->vt52_mode)
1128 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1130 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1131 use_ucsoutput = FALSE;
1134 if ((code == 1 || code == 4) &&
1135 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1136 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1137 use_ucsoutput = FALSE;
1141 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1142 use_ucsoutput = FALSE;
1148 * Cursor keys. (This includes the numberpad cursor keys,
1149 * if we haven't already done them due to app keypad mode.)
1151 * Here we also process un-numlocked un-appkeypadded KP5,
1152 * which sends ESC [ G.
1156 switch (event->keyval) {
1157 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1158 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1159 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1160 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1161 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1164 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1165 event->state & GDK_CONTROL_MASK);
1166 use_ucsoutput = FALSE;
1175 if (end-start > 0) {
1176 #ifdef KEY_DEBUGGING
1178 printf("generating sequence:");
1179 for (i = start; i < end; i++)
1180 printf(" %02x", (unsigned char) output[i]);
1186 * For special control characters, the character set
1187 * should never matter.
1189 output[end] = '\0'; /* NUL-terminate */
1191 ldisc_send(inst->ldisc, output+start, -2, 1);
1192 } else if (!inst->direct_to_font) {
1193 if (!use_ucsoutput) {
1195 lpage_send(inst->ldisc, output_charset, output+start,
1199 * We generated our own Unicode key data from the
1200 * keysym, so use that instead.
1203 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1207 * In direct-to-font mode, we just send the string
1208 * exactly as we received it.
1211 ldisc_send(inst->ldisc, output+start, end-start, 1);
1214 show_mouseptr(inst, 0);
1215 term_seen_key_event(inst->term);
1221 #if GTK_CHECK_VERSION(2,0,0)
1222 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1224 struct gui_data *inst = (struct gui_data *)data;
1226 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1227 show_mouseptr(inst, 0);
1228 term_seen_key_event(inst->term);
1232 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1233 GdkEventType type, guint ebutton, guint state,
1234 gdouble ex, gdouble ey)
1236 int shift, ctrl, alt, x, y, button, act;
1238 /* Remember the timestamp. */
1239 inst->input_event_time = timestamp;
1241 show_mouseptr(inst, 1);
1243 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1244 term_scroll(inst->term, 0, -5);
1247 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1248 term_scroll(inst->term, 0, +5);
1252 shift = state & GDK_SHIFT_MASK;
1253 ctrl = state & GDK_CONTROL_MASK;
1254 alt = state & GDK_MOD1_MASK;
1256 if (ebutton == 3 && ctrl) {
1257 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1258 ebutton, timestamp);
1264 else if (ebutton == 2)
1265 button = MBT_MIDDLE;
1266 else if (ebutton == 3)
1269 return FALSE; /* don't even know what button! */
1272 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1273 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1274 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1275 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1276 default: return FALSE; /* don't know this event type */
1279 if (send_raw_mouse && !(shift && conf_get_int(inst->conf,
1280 CONF_mouse_override)) &&
1281 act != MA_CLICK && act != MA_RELEASE)
1282 return TRUE; /* we ignore these in raw mouse mode */
1284 x = (ex - inst->window_border) / inst->font_width;
1285 y = (ey - inst->window_border) / inst->font_height;
1287 term_mouse(inst->term, button, translate_button(button), act,
1288 x, y, shift, ctrl, alt);
1293 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1295 struct gui_data *inst = (struct gui_data *)data;
1296 return button_internal(inst, event->time, event->type, event->button,
1297 event->state, event->x, event->y);
1300 #if GTK_CHECK_VERSION(2,0,0)
1302 * In GTK 2, mouse wheel events have become a new type of event.
1303 * This handler translates them back into button-4 and button-5
1304 * presses so that I don't have to change my old code too much :-)
1306 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1308 struct gui_data *inst = (struct gui_data *)data;
1311 if (event->direction == GDK_SCROLL_UP)
1313 else if (event->direction == GDK_SCROLL_DOWN)
1318 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1319 button, event->state, event->x, event->y);
1323 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1325 struct gui_data *inst = (struct gui_data *)data;
1326 int shift, ctrl, alt, x, y, button;
1328 /* Remember the timestamp. */
1329 inst->input_event_time = event->time;
1331 show_mouseptr(inst, 1);
1333 shift = event->state & GDK_SHIFT_MASK;
1334 ctrl = event->state & GDK_CONTROL_MASK;
1335 alt = event->state & GDK_MOD1_MASK;
1336 if (event->state & GDK_BUTTON1_MASK)
1338 else if (event->state & GDK_BUTTON2_MASK)
1339 button = MBT_MIDDLE;
1340 else if (event->state & GDK_BUTTON3_MASK)
1343 return FALSE; /* don't even know what button! */
1345 x = (event->x - inst->window_border) / inst->font_width;
1346 y = (event->y - inst->window_border) / inst->font_height;
1348 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1349 x, y, shift, ctrl, alt);
1354 void frontend_keypress(void *handle)
1356 struct gui_data *inst = (struct gui_data *)handle;
1359 * If our child process has exited but not closed, terminate on
1366 static void exit_callback(void *vinst)
1368 struct gui_data *inst = (struct gui_data *)vinst;
1369 int exitcode, close_on_exit;
1371 if (!inst->exited &&
1372 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1373 inst->exited = TRUE;
1374 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1375 if (close_on_exit == FORCE_ON ||
1376 (close_on_exit == AUTO && exitcode == 0))
1377 gtk_main_quit(); /* just go */
1379 ldisc_free(inst->ldisc);
1382 inst->back->free(inst->backhandle);
1383 inst->backhandle = NULL;
1385 term_provide_resize_fn(inst->term, NULL, NULL);
1386 update_specials_menu(inst);
1387 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1391 void notify_remote_exit(void *frontend)
1393 struct gui_data *inst = (struct gui_data *)frontend;
1395 queue_toplevel_callback(exit_callback, inst);
1398 static gint idle_toplevel_callback_func(gpointer data)
1400 struct gui_data *inst = (struct gui_data *)data;
1402 run_toplevel_callbacks();
1404 gtk_idle_remove(inst->toplevel_callback_idle_id);
1409 void notify_toplevel_callback(void *frontend)
1411 struct gui_data *inst = (struct gui_data *)frontend;
1413 inst->toplevel_callback_idle_id =
1414 gtk_idle_add(idle_toplevel_callback_func, inst);
1417 static gint timer_trigger(gpointer data)
1419 unsigned long now = GPOINTER_TO_LONG(data);
1420 unsigned long next, then;
1423 if (run_timers(now, &next)) {
1425 now = GETTICKCOUNT();
1426 if (now - then > next - then)
1430 timer_id = gtk_timeout_add(ticks, timer_trigger,
1431 LONG_TO_GPOINTER(next));
1435 * Never let a timer resume. If we need another one, we've
1436 * asked for it explicitly above.
1441 void timer_change_notify(unsigned long next)
1446 gtk_timeout_remove(timer_id);
1448 ticks = next - GETTICKCOUNT();
1450 ticks = 1; /* just in case */
1452 timer_id = gtk_timeout_add(ticks, timer_trigger,
1453 LONG_TO_GPOINTER(next));
1456 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1459 * We must process exceptional notifications before ordinary
1460 * readability ones, or we may go straight past the urgent
1463 if (condition & GDK_INPUT_EXCEPTION)
1464 select_result(sourcefd, 4);
1465 if (condition & GDK_INPUT_READ)
1466 select_result(sourcefd, 1);
1467 if (condition & GDK_INPUT_WRITE)
1468 select_result(sourcefd, 2);
1471 void destroy(GtkWidget *widget, gpointer data)
1476 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1478 struct gui_data *inst = (struct gui_data *)data;
1479 term_set_focus(inst->term, event->in);
1480 term_update(inst->term);
1481 show_mouseptr(inst, 1);
1485 void set_busy_status(void *frontend, int status)
1487 struct gui_data *inst = (struct gui_data *)frontend;
1488 inst->busy_status = status;
1489 update_mouseptr(inst);
1493 * set or clear the "raw mouse message" mode
1495 void set_raw_mouse_mode(void *frontend, int activate)
1497 struct gui_data *inst = (struct gui_data *)frontend;
1498 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1499 send_raw_mouse = activate;
1500 update_mouseptr(inst);
1503 void request_resize(void *frontend, int w, int h)
1505 struct gui_data *inst = (struct gui_data *)frontend;
1506 int large_x, large_y;
1507 int offset_x, offset_y;
1509 GtkRequisition inner, outer;
1512 * This is a heinous hack dreamed up by the gnome-terminal
1513 * people to get around a limitation in gtk. The problem is
1514 * that in order to set the size correctly we really need to be
1515 * calling gtk_window_resize - but that needs to know the size
1516 * of the _whole window_, not the drawing area. So what we do
1517 * is to set an artificially huge size request on the drawing
1518 * area, recompute the resulting size request on the window,
1519 * and look at the difference between the two. That gives us
1520 * the x and y offsets we need to translate drawing area size
1521 * into window size for real, and then we call
1522 * gtk_window_resize.
1526 * We start by retrieving the current size of the whole window.
1527 * Adding a bit to _that_ will give us a value we can use as a
1528 * bogus size request which guarantees to be bigger than the
1529 * current size of the drawing area.
1531 get_window_pixels(inst, &large_x, &large_y);
1535 #if GTK_CHECK_VERSION(2,0,0)
1536 gtk_widget_set_size_request(inst->area, large_x, large_y);
1538 gtk_widget_set_usize(inst->area, large_x, large_y);
1540 gtk_widget_size_request(inst->area, &inner);
1541 gtk_widget_size_request(inst->window, &outer);
1543 offset_x = outer.width - inner.width;
1544 offset_y = outer.height - inner.height;
1546 area_x = inst->font_width * w + 2*inst->window_border;
1547 area_y = inst->font_height * h + 2*inst->window_border;
1550 * Now we must set the size request on the drawing area back to
1551 * something sensible before we commit the real resize. Best
1552 * way to do this, I think, is to set it to what the size is
1553 * really going to end up being.
1555 #if GTK_CHECK_VERSION(2,0,0)
1556 gtk_widget_set_size_request(inst->area, area_x, area_y);
1557 gtk_window_resize(GTK_WINDOW(inst->window),
1558 area_x + offset_x, area_y + offset_y);
1560 gtk_widget_set_usize(inst->area, area_x, area_y);
1561 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1563 * I can no longer remember what this call to
1564 * gtk_container_dequeue_resize_handler is for. It was
1565 * introduced in r3092 with no comment, and the commit log
1566 * message was uninformative. I'm _guessing_ its purpose is to
1567 * prevent gratuitous resize processing on the window given
1568 * that we're about to resize it anyway, but I have no idea
1569 * why that's so incredibly vital.
1571 * I've tried removing the call, and nothing seems to go
1572 * wrong. I've backtracked to r3092 and tried removing the
1573 * call there, and still nothing goes wrong. So I'm going to
1574 * adopt the working hypothesis that it's superfluous; I won't
1575 * actually remove it from the GTK 1.2 code, but I won't
1576 * attempt to replicate its functionality in the GTK 2 code
1579 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1580 gdk_window_resize(inst->window->window,
1581 area_x + offset_x, area_y + offset_y);
1585 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1587 gboolean success[1];
1589 inst->cols[n].red = r * 0x0101;
1590 inst->cols[n].green = g * 0x0101;
1591 inst->cols[n].blue = b * 0x0101;
1593 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1594 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1595 FALSE, TRUE, success);
1597 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1601 void set_window_background(struct gui_data *inst)
1603 if (inst->area && inst->area->window)
1604 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1605 if (inst->window && inst->window->window)
1606 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1609 void palette_set(void *frontend, int n, int r, int g, int b)
1611 struct gui_data *inst = (struct gui_data *)frontend;
1614 if (n >= NALLCOLOURS)
1616 real_palette_set(inst, n, r, g, b);
1618 /* Default Background changed. Ensure space between text area and
1619 * window border is redrawn */
1620 set_window_background(inst);
1621 draw_backing_rect(inst);
1622 gtk_widget_queue_draw(inst->area);
1626 void palette_reset(void *frontend)
1628 struct gui_data *inst = (struct gui_data *)frontend;
1629 /* This maps colour indices in inst->conf to those used in inst->cols. */
1630 static const int ww[] = {
1631 256, 257, 258, 259, 260, 261,
1632 0, 8, 1, 9, 2, 10, 3, 11,
1633 4, 12, 5, 13, 6, 14, 7, 15
1635 gboolean success[NALLCOLOURS];
1638 assert(lenof(ww) == NCFGCOLOURS);
1640 if (!inst->colmap) {
1641 inst->colmap = gdk_colormap_get_system();
1643 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1646 for (i = 0; i < NCFGCOLOURS; i++) {
1647 inst->cols[ww[i]].red =
1648 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
1649 inst->cols[ww[i]].green =
1650 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
1651 inst->cols[ww[i]].blue =
1652 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
1655 for (i = 0; i < NEXTCOLOURS; i++) {
1657 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1658 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1659 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1660 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1662 int shade = i - 216;
1663 shade = shade * 0x0a0a + 0x0808;
1664 inst->cols[i+16].red = inst->cols[i+16].green =
1665 inst->cols[i+16].blue = shade;
1669 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1670 FALSE, TRUE, success);
1671 for (i = 0; i < NALLCOLOURS; i++) {
1673 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1675 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
1676 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
1677 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
1680 /* Since Default Background may have changed, ensure that space
1681 * between text area and window border is refreshed. */
1682 set_window_background(inst);
1683 if (inst->area && inst->area->window) {
1684 draw_backing_rect(inst);
1685 gtk_widget_queue_draw(inst->area);
1689 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1690 * do this before we start using cut buffers.
1692 void init_cutbuffers()
1694 unsigned char empty[] = "";
1695 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1696 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1697 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1698 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1699 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1700 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1701 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1702 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1703 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1704 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1705 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1706 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1707 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1708 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1709 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1710 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1713 /* Store the data in a cut-buffer. */
1714 void store_cutbuffer(char * ptr, int len)
1716 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1717 XRotateBuffers(GDK_DISPLAY(), 1);
1718 XStoreBytes(GDK_DISPLAY(), ptr, len);
1721 /* Retrieve data from a cut-buffer.
1722 * Returned data needs to be freed with XFree().
1724 char * retrieve_cutbuffer(int * nbytes)
1727 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1728 if (*nbytes <= 0 && ptr != 0) {
1735 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1737 struct gui_data *inst = (struct gui_data *)frontend;
1738 if (inst->pasteout_data)
1739 sfree(inst->pasteout_data);
1740 if (inst->pasteout_data_ctext)
1741 sfree(inst->pasteout_data_ctext);
1742 if (inst->pasteout_data_utf8)
1743 sfree(inst->pasteout_data_utf8);
1746 * Set up UTF-8 and compound text paste data. This only happens
1747 * if we aren't in direct-to-font mode using the D800 hack.
1749 if (!inst->direct_to_font) {
1750 const wchar_t *tmp = data;
1755 inst->pasteout_data_utf8 = snewn(len*6, char);
1756 inst->pasteout_data_utf8_len = len*6;
1757 inst->pasteout_data_utf8_len =
1758 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1759 inst->pasteout_data_utf8_len,
1760 CS_UTF8, NULL, NULL, 0);
1761 if (inst->pasteout_data_utf8_len == 0) {
1762 sfree(inst->pasteout_data_utf8);
1763 inst->pasteout_data_utf8 = NULL;
1765 inst->pasteout_data_utf8 =
1766 sresize(inst->pasteout_data_utf8,
1767 inst->pasteout_data_utf8_len + 1, char);
1768 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1772 * Now let Xlib convert our UTF-8 data into compound text.
1774 list[0] = inst->pasteout_data_utf8;
1775 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1776 XCompoundTextStyle, &tp) == 0) {
1777 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1778 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1779 inst->pasteout_data_ctext_len = tp.nitems;
1782 inst->pasteout_data_ctext = NULL;
1783 inst->pasteout_data_ctext_len = 0;
1786 inst->pasteout_data_utf8 = NULL;
1787 inst->pasteout_data_utf8_len = 0;
1788 inst->pasteout_data_ctext = NULL;
1789 inst->pasteout_data_ctext_len = 0;
1792 inst->pasteout_data = snewn(len*6, char);
1793 inst->pasteout_data_len = len*6;
1794 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1795 data, len, inst->pasteout_data,
1796 inst->pasteout_data_len,
1798 if (inst->pasteout_data_len == 0) {
1799 sfree(inst->pasteout_data);
1800 inst->pasteout_data = NULL;
1802 inst->pasteout_data =
1803 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1806 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1808 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1809 inst->input_event_time)) {
1810 #if GTK_CHECK_VERSION(2,0,0)
1811 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
1813 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1814 GDK_SELECTION_TYPE_STRING, 1);
1815 if (inst->pasteout_data_ctext)
1816 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1817 compound_text_atom, 1);
1818 if (inst->pasteout_data_utf8)
1819 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1820 utf8_string_atom, 1);
1824 term_deselect(inst->term);
1827 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1828 guint info, guint time_stamp, gpointer data)
1830 struct gui_data *inst = (struct gui_data *)data;
1831 if (seldata->target == utf8_string_atom)
1832 gtk_selection_data_set(seldata, seldata->target, 8,
1833 (unsigned char *)inst->pasteout_data_utf8,
1834 inst->pasteout_data_utf8_len);
1835 else if (seldata->target == compound_text_atom)
1836 gtk_selection_data_set(seldata, seldata->target, 8,
1837 (unsigned char *)inst->pasteout_data_ctext,
1838 inst->pasteout_data_ctext_len);
1840 gtk_selection_data_set(seldata, seldata->target, 8,
1841 (unsigned char *)inst->pasteout_data,
1842 inst->pasteout_data_len);
1845 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1848 struct gui_data *inst = (struct gui_data *)data;
1850 term_deselect(inst->term);
1851 if (inst->pasteout_data)
1852 sfree(inst->pasteout_data);
1853 if (inst->pasteout_data_ctext)
1854 sfree(inst->pasteout_data_ctext);
1855 if (inst->pasteout_data_utf8)
1856 sfree(inst->pasteout_data_utf8);
1857 inst->pasteout_data = NULL;
1858 inst->pasteout_data_len = 0;
1859 inst->pasteout_data_ctext = NULL;
1860 inst->pasteout_data_ctext_len = 0;
1861 inst->pasteout_data_utf8 = NULL;
1862 inst->pasteout_data_utf8_len = 0;
1866 void request_paste(void *frontend)
1868 struct gui_data *inst = (struct gui_data *)frontend;
1870 * In Unix, pasting is asynchronous: all we can do at the
1871 * moment is to call gtk_selection_convert(), and when the data
1872 * comes back _then_ we can call term_do_paste().
1875 if (!inst->direct_to_font) {
1877 * First we attempt to retrieve the selection as a UTF-8
1878 * string (which we will convert to the correct code page
1879 * before sending to the session, of course). If that
1880 * fails, selection_received() will be informed and will
1881 * fall back to an ordinary string.
1883 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1885 inst->input_event_time);
1888 * If we're in direct-to-font mode, we disable UTF-8
1889 * pasting, and go straight to ordinary string data.
1891 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1892 GDK_SELECTION_TYPE_STRING,
1893 inst->input_event_time);
1897 gint idle_paste_func(gpointer data); /* forward ref */
1899 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1900 guint time, gpointer data)
1902 struct gui_data *inst = (struct gui_data *)data;
1906 int length, count, ret;
1907 int free_list_required = 0;
1908 int free_required = 0;
1911 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1913 * Failed to get a UTF-8 selection string. Try compound
1916 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1918 inst->input_event_time);
1922 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1924 * Failed to get UTF-8 or compound text. Try an ordinary
1927 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1928 GDK_SELECTION_TYPE_STRING,
1929 inst->input_event_time);
1934 * If we have data, but it's not of a type we can deal with,
1935 * we have to ignore the data.
1937 if (seldata->length > 0 &&
1938 seldata->type != GDK_SELECTION_TYPE_STRING &&
1939 seldata->type != compound_text_atom &&
1940 seldata->type != utf8_string_atom)
1944 * If we have no data, try looking in a cut buffer.
1946 if (seldata->length <= 0) {
1947 text = retrieve_cutbuffer(&length);
1950 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1951 * source, so use that as a de-facto standard. */
1952 charset = CS_ISO8859_1;
1956 * Convert COMPOUND_TEXT into UTF-8.
1958 if (seldata->type == compound_text_atom) {
1959 tp.value = seldata->data;
1960 tp.encoding = (Atom) seldata->type;
1961 tp.format = seldata->format;
1962 tp.nitems = seldata->length;
1963 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1965 if (ret != 0 || count != 1) {
1967 * Compound text failed; fall back to STRING.
1969 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1970 GDK_SELECTION_TYPE_STRING,
1971 inst->input_event_time);
1975 length = strlen(list[0]);
1977 free_list_required = 1;
1979 text = (char *)seldata->data;
1980 length = seldata->length;
1981 charset = (seldata->type == utf8_string_atom ?
1982 CS_UTF8 : inst->ucsdata.line_codepage);
1986 if (inst->pastein_data)
1987 sfree(inst->pastein_data);
1989 inst->pastein_data = snewn(length, wchar_t);
1990 inst->pastein_data_len = length;
1991 inst->pastein_data_len =
1992 mb_to_wc(charset, 0, text, length,
1993 inst->pastein_data, inst->pastein_data_len);
1995 term_do_paste(inst->term);
1997 if (free_list_required)
1998 XFreeStringList(list);
2003 void get_clip(void *frontend, wchar_t ** p, int *len)
2005 struct gui_data *inst = (struct gui_data *)frontend;
2008 *p = inst->pastein_data;
2009 *len = inst->pastein_data_len;
2013 static void set_window_titles(struct gui_data *inst)
2016 * We must always call set_icon_name after calling set_title,
2017 * since set_title will write both names. Irritating, but such
2020 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2021 if (!conf_get_int(inst->conf, CONF_win_name_always))
2022 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
2025 void set_title(void *frontend, char *title)
2027 struct gui_data *inst = (struct gui_data *)frontend;
2028 sfree(inst->wintitle);
2029 inst->wintitle = dupstr(title);
2030 set_window_titles(inst);
2033 void set_icon(void *frontend, char *title)
2035 struct gui_data *inst = (struct gui_data *)frontend;
2036 sfree(inst->icontitle);
2037 inst->icontitle = dupstr(title);
2038 set_window_titles(inst);
2041 void set_title_and_icon(void *frontend, char *title, char *icon)
2043 struct gui_data *inst = (struct gui_data *)frontend;
2044 sfree(inst->wintitle);
2045 inst->wintitle = dupstr(title);
2046 sfree(inst->icontitle);
2047 inst->icontitle = dupstr(icon);
2048 set_window_titles(inst);
2051 void set_sbar(void *frontend, int total, int start, int page)
2053 struct gui_data *inst = (struct gui_data *)frontend;
2054 if (!conf_get_int(inst->conf, CONF_scrollbar))
2056 inst->sbar_adjust->lower = 0;
2057 inst->sbar_adjust->upper = total;
2058 inst->sbar_adjust->value = start;
2059 inst->sbar_adjust->page_size = page;
2060 inst->sbar_adjust->step_increment = 1;
2061 inst->sbar_adjust->page_increment = page/2;
2062 inst->ignore_sbar = TRUE;
2063 gtk_adjustment_changed(inst->sbar_adjust);
2064 inst->ignore_sbar = FALSE;
2067 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2069 struct gui_data *inst = (struct gui_data *)data;
2071 if (!conf_get_int(inst->conf, CONF_scrollbar))
2073 if (!inst->ignore_sbar)
2074 term_scroll(inst->term, 1, (int)adj->value);
2077 void sys_cursor(void *frontend, int x, int y)
2080 * This is meaningless under X.
2085 * This is still called when mode==BELL_VISUAL, even though the
2086 * visual bell is handled entirely within terminal.c, because we
2087 * may want to perform additional actions on any kind of bell (for
2088 * example, taskbar flashing in Windows).
2090 void do_beep(void *frontend, int mode)
2092 if (mode == BELL_DEFAULT)
2096 int char_width(Context ctx, int uc)
2099 * Under X, any fixed-width font really _is_ fixed-width.
2100 * Double-width characters will be dealt with using a separate
2101 * font. For the moment we can simply return 1.
2103 * FIXME: but is that also true of Pango?
2108 Context get_ctx(void *frontend)
2110 struct gui_data *inst = (struct gui_data *)frontend;
2111 struct draw_ctx *dctx;
2113 if (!inst->area->window)
2116 dctx = snew(struct draw_ctx);
2118 dctx->gc = gdk_gc_new(inst->area->window);
2122 void free_ctx(Context ctx)
2124 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2125 /* struct gui_data *inst = dctx->inst; */
2126 GdkGC *gc = dctx->gc;
2132 * Draw a line of text in the window, at given character
2133 * coordinates, in given attributes.
2135 * We are allowed to fiddle with the contents of `text'.
2137 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2138 unsigned long attr, int lattr)
2140 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2141 struct gui_data *inst = dctx->inst;
2142 GdkGC *gc = dctx->gc;
2143 int ncombining, combining;
2144 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2145 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
2147 if (attr & TATTR_COMBINING) {
2153 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2154 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2155 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2160 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
2161 if (nfg < 16) nfg |= 8;
2162 else if (nfg >= 256) nfg |= 1;
2164 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
2165 if (nbg < 16) nbg |= 8;
2166 else if (nbg >= 256) nbg |= 1;
2168 if ((attr & TATTR_ACTCURS) && !monochrome) {
2173 fontid = shadow = 0;
2175 if (attr & ATTR_WIDE) {
2182 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
2189 if (!inst->fonts[fontid]) {
2192 * Fall back through font ids with subsets of this one's
2193 * set bits, in order.
2195 for (i = fontid; i-- > 0 ;) {
2197 continue; /* some other bit is set */
2198 if (inst->fonts[i]) {
2203 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2206 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2208 if (x >= inst->term->cols)
2210 if (x + len*2*widefactor > inst->term->cols)
2211 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2219 r.x = x*inst->font_width+inst->window_border;
2220 r.y = y*inst->font_height+inst->window_border;
2221 r.width = rlen*widefactor*inst->font_width;
2222 r.height = inst->font_height;
2223 gdk_gc_set_clip_rectangle(gc, &r);
2226 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
2227 gdk_draw_rectangle(inst->pixmap, gc, 1,
2228 x*inst->font_width+inst->window_border,
2229 y*inst->font_height+inst->window_border,
2230 rlen*widefactor*inst->font_width, inst->font_height);
2232 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2233 for (combining = 0; combining < ncombining; combining++) {
2234 unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid],
2235 x*inst->font_width+inst->window_border,
2236 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2237 text + combining, len, widefactor > 1,
2238 bold, inst->font_width);
2241 if (attr & ATTR_UNDER) {
2242 int uheight = inst->fonts[0]->ascent + 1;
2243 if (uheight >= inst->font_height)
2244 uheight = inst->font_height - 1;
2245 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->window_border,
2246 y*inst->font_height + uheight + inst->window_border,
2247 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2248 y*inst->font_height + uheight + inst->window_border);
2251 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2253 * I can't find any plausible StretchBlt equivalent in the
2254 * X server, so I'm going to do this the slow and painful
2255 * way. This will involve repeated calls to
2256 * gdk_draw_pixmap() to stretch the text horizontally. It's
2257 * O(N^2) in time and O(N) in network bandwidth, but you
2258 * try thinking of a better way. :-(
2261 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2262 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2263 x*inst->font_width+inst->window_border + 2*i,
2264 y*inst->font_height+inst->window_border,
2265 x*inst->font_width+inst->window_border + 2*i+1,
2266 y*inst->font_height+inst->window_border,
2267 len * widefactor * inst->font_width - i, inst->font_height);
2270 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2272 /* Now stretch vertically, in the same way. */
2273 if ((lattr & LATTR_MODE) == LATTR_BOT)
2277 for (i = 0; i < inst->font_height; i+=2) {
2278 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2279 x*inst->font_width+inst->window_border,
2280 y*inst->font_height+inst->window_border+dt*i+db,
2281 x*inst->font_width+inst->window_border,
2282 y*inst->font_height+inst->window_border+dt*(i+1),
2283 len * widefactor * inst->font_width, inst->font_height-i-1);
2289 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2290 unsigned long attr, int lattr)
2292 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2293 struct gui_data *inst = dctx->inst;
2294 GdkGC *gc = dctx->gc;
2297 do_text_internal(ctx, x, y, text, len, attr, lattr);
2299 if (attr & ATTR_WIDE) {
2305 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2307 if (x >= inst->term->cols)
2309 if (x + len*2*widefactor > inst->term->cols)
2310 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2314 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2315 x*inst->font_width+inst->window_border,
2316 y*inst->font_height+inst->window_border,
2317 x*inst->font_width+inst->window_border,
2318 y*inst->font_height+inst->window_border,
2319 len*widefactor*inst->font_width, inst->font_height);
2322 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2323 unsigned long attr, int lattr)
2325 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2326 struct gui_data *inst = dctx->inst;
2327 GdkGC *gc = dctx->gc;
2329 int active, passive, widefactor;
2331 if (attr & TATTR_PASCURS) {
2332 attr &= ~TATTR_PASCURS;
2336 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
2337 attr &= ~TATTR_ACTCURS;
2341 do_text_internal(ctx, x, y, text, len, attr, lattr);
2343 if (attr & TATTR_COMBINING)
2346 if (attr & ATTR_WIDE) {
2352 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2354 if (x >= inst->term->cols)
2356 if (x + len*2*widefactor > inst->term->cols)
2357 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2361 if (inst->cursor_type == 0) {
2363 * An active block cursor will already have been done by
2364 * the above do_text call, so we only need to do anything
2368 gdk_gc_set_foreground(gc, &inst->cols[261]);
2369 gdk_draw_rectangle(inst->pixmap, gc, 0,
2370 x*inst->font_width+inst->window_border,
2371 y*inst->font_height+inst->window_border,
2372 len*widefactor*inst->font_width-1, inst->font_height-1);
2376 int startx, starty, dx, dy, length, i;
2380 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2381 char_width = 2*inst->font_width;
2383 char_width = inst->font_width;
2385 if (inst->cursor_type == 1) {
2386 uheight = inst->fonts[0]->ascent + 1;
2387 if (uheight >= inst->font_height)
2388 uheight = inst->font_height - 1;
2390 startx = x * inst->font_width + inst->window_border;
2391 starty = y * inst->font_height + inst->window_border + uheight;
2394 length = len * widefactor * char_width;
2397 if (attr & TATTR_RIGHTCURS)
2398 xadjust = char_width - 1;
2399 startx = x * inst->font_width + inst->window_border + xadjust;
2400 starty = y * inst->font_height + inst->window_border;
2403 length = inst->font_height;
2406 gdk_gc_set_foreground(gc, &inst->cols[261]);
2408 for (i = 0; i < length; i++) {
2410 gdk_draw_point(inst->pixmap, gc, startx, starty);
2415 } else if (active) {
2416 gdk_draw_line(inst->pixmap, gc, startx, starty,
2417 startx + (length-1) * dx, starty + (length-1) * dy);
2418 } /* else no cursor (e.g., blinked off) */
2421 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2422 x*inst->font_width+inst->window_border,
2423 y*inst->font_height+inst->window_border,
2424 x*inst->font_width+inst->window_border,
2425 y*inst->font_height+inst->window_border,
2426 len*widefactor*inst->font_width, inst->font_height);
2428 #if GTK_CHECK_VERSION(2,0,0)
2430 GdkRectangle cursorrect;
2431 cursorrect.x = x*inst->font_width+inst->window_border;
2432 cursorrect.y = y*inst->font_height+inst->window_border;
2433 cursorrect.width = len*widefactor*inst->font_width;
2434 cursorrect.height = inst->font_height;
2435 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
2440 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2443 * Truly hideous hack: GTK doesn't allow us to set the mouse
2444 * cursor foreground and background colours unless we've _also_
2445 * created our own cursor from bitmaps. Therefore, I need to
2446 * load the `cursor' font and draw glyphs from it on to
2447 * pixmaps, in order to construct my cursors with the fg and bg
2448 * I want. This is a gross hack, but it's more self-contained
2449 * than linking in Xlib to find the X window handle to
2450 * inst->area and calling XRecolorCursor, and it's more
2451 * futureproof than hard-coding the shapes as bitmap arrays.
2453 static GdkFont *cursor_font = NULL;
2454 GdkPixmap *source, *mask;
2456 GdkColor cfg = { 0, 65535, 65535, 65535 };
2457 GdkColor cbg = { 0, 0, 0, 0 };
2458 GdkColor dfg = { 1, 65535, 65535, 65535 };
2459 GdkColor dbg = { 0, 0, 0, 0 };
2462 gint lb, rb, wid, asc, desc, w, h, x, y;
2464 if (cursor_val == -2) {
2465 gdk_font_unref(cursor_font);
2469 if (cursor_val >= 0 && !cursor_font) {
2470 cursor_font = gdk_font_load("cursor");
2472 gdk_font_ref(cursor_font);
2476 * Get the text extent of the cursor in question. We use the
2477 * mask character for this, because it's typically slightly
2478 * bigger than the main character.
2480 if (cursor_val >= 0) {
2482 text[0] = (char)cursor_val + 1;
2483 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2484 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2490 source = gdk_pixmap_new(NULL, w, h, 1);
2491 mask = gdk_pixmap_new(NULL, w, h, 1);
2494 * Draw the mask character on the mask pixmap.
2496 gc = gdk_gc_new(mask);
2497 gdk_gc_set_foreground(gc, &dbg);
2498 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2499 if (cursor_val >= 0) {
2501 text[0] = (char)cursor_val + 1;
2502 gdk_gc_set_foreground(gc, &dfg);
2503 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2508 * Draw the main character on the source pixmap.
2510 gc = gdk_gc_new(source);
2511 gdk_gc_set_foreground(gc, &dbg);
2512 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2513 if (cursor_val >= 0) {
2515 text[0] = (char)cursor_val;
2516 gdk_gc_set_foreground(gc, &dfg);
2517 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2522 * Create the cursor.
2524 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2529 gdk_pixmap_unref(source);
2530 gdk_pixmap_unref(mask);
2535 void modalfatalbox(char *p, ...)
2538 fprintf(stderr, "FATAL ERROR: ");
2540 vfprintf(stderr, p, ap);
2542 fputc('\n', stderr);
2546 void cmdline_error(char *p, ...)
2549 fprintf(stderr, "%s: ", appname);
2551 vfprintf(stderr, p, ap);
2553 fputc('\n', stderr);
2557 char *get_x_display(void *frontend)
2559 return gdk_get_display();
2562 long get_windowid(void *frontend)
2564 struct gui_data *inst = (struct gui_data *)frontend;
2565 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2568 static void help(FILE *fp) {
2570 "pterm option summary:\n"
2572 " --display DISPLAY Specify X display to use (note '--')\n"
2573 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2574 " -fn FONT Normal text font\n"
2575 " -fb FONT Bold text font\n"
2576 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2577 " -sl LINES Number of lines of scrollback\n"
2578 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2579 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2580 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2581 " -T TITLE Window title\n"
2582 " -ut, +ut Do(default) or do not update utmp\n"
2583 " -ls, +ls Do(default) or do not make shell a login shell\n"
2584 " -sb, +sb Do(default) or do not display a scrollbar\n"
2585 " -log PATH Log all output to a file\n"
2586 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2587 " -xrm RESOURCE-STRING Set an X resource\n"
2588 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2589 ) < 0 || fflush(fp) < 0) {
2590 perror("output error");
2595 static void version(FILE *fp) {
2596 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
2597 perror("output error");
2602 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2603 struct gui_data *inst, Conf *conf)
2609 * Macros to make argument handling easier. Note that because
2610 * they need to call `continue', they cannot be contained in
2611 * the usual do {...} while (0) wrapper to make them
2612 * syntactically single statements; hence it is not legal to
2613 * use one of these macros as an unbraced statement between
2616 #define EXPECTS_ARG { \
2617 if (--argc <= 0) { \
2619 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2624 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2626 while (--argc > 0) {
2631 * Shameless cheating. Debian requires all X terminal
2632 * emulators to support `-T title'; but
2633 * cmdline_process_param will eat -T (it means no-pty) and
2634 * complain that pterm doesn't support it. So, in pterm
2635 * only, we convert -T into -title.
2637 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2641 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2642 do_everything ? 1 : -1, conf);
2645 cmdline_error("option \"%s\" requires an argument", p);
2646 } else if (ret == 2) {
2647 --argc, ++argv; /* skip next argument */
2649 } else if (ret == 1) {
2653 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2657 fs = fontspec_new(val);
2658 conf_set_fontspec(conf, CONF_font, fs);
2661 } else if (!strcmp(p, "-fb")) {
2665 fs = fontspec_new(val);
2666 conf_set_fontspec(conf, CONF_boldfont, fs);
2669 } else if (!strcmp(p, "-fw")) {
2673 fs = fontspec_new(val);
2674 conf_set_fontspec(conf, CONF_widefont, fs);
2677 } else if (!strcmp(p, "-fwb")) {
2681 fs = fontspec_new(val);
2682 conf_set_fontspec(conf, CONF_wideboldfont, fs);
2685 } else if (!strcmp(p, "-cs")) {
2688 conf_set_str(conf, CONF_line_codepage, val);
2690 } else if (!strcmp(p, "-geometry")) {
2696 flags = XParseGeometry(val, &x, &y, &w, &h);
2697 if (flags & WidthValue)
2698 conf_set_int(conf, CONF_width, w);
2699 if (flags & HeightValue)
2700 conf_set_int(conf, CONF_height, h);
2702 if (flags & (XValue | YValue)) {
2705 inst->gotpos = TRUE;
2706 inst->gravity = ((flags & XNegative ? 1 : 0) |
2707 (flags & YNegative ? 2 : 0));
2710 } else if (!strcmp(p, "-sl")) {
2713 conf_set_int(conf, CONF_savelines, atoi(val));
2715 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2716 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2717 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2722 if (!gdk_color_parse(val, &col)) {
2724 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2728 index = (!strcmp(p, "-fg") ? 0 :
2729 !strcmp(p, "-bg") ? 2 :
2730 !strcmp(p, "-bfg") ? 1 :
2731 !strcmp(p, "-bbg") ? 3 :
2732 !strcmp(p, "-cfg") ? 4 :
2733 !strcmp(p, "-cbg") ? 5 : -1);
2734 assert(index != -1);
2735 conf_set_int_int(conf, CONF_colours, index*3+0, col.red / 256);
2736 conf_set_int_int(conf, CONF_colours, index*3+1,col.green/ 256);
2737 conf_set_int_int(conf, CONF_colours, index*3+2, col.blue/ 256);
2740 } else if (use_pty_argv && !strcmp(p, "-e")) {
2741 /* This option swallows all further arguments. */
2747 pty_argv = snewn(argc+1, char *);
2749 for (i = 0; i < argc; i++)
2750 pty_argv[i] = argv[i];
2751 pty_argv[argc] = NULL;
2752 break; /* finished command-line processing */
2754 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2757 } else if (!strcmp(p, "-title")) {
2760 conf_set_str(conf, CONF_wintitle, val);
2762 } else if (!strcmp(p, "-log")) {
2766 fn = filename_from_str(val);
2767 conf_set_filename(conf, CONF_logfilename, fn);
2768 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
2771 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2773 conf_set_int(conf, CONF_stamp_utmp, 0);
2775 } else if (!strcmp(p, "-ut")) {
2777 conf_set_int(conf, CONF_stamp_utmp, 1);
2779 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2781 conf_set_int(conf, CONF_login_shell, 0);
2783 } else if (!strcmp(p, "-ls")) {
2785 conf_set_int(conf, CONF_login_shell, 1);
2787 } else if (!strcmp(p, "-nethack")) {
2789 conf_set_int(conf, CONF_nethack_keypad, 1);
2791 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2793 conf_set_int(conf, CONF_scrollbar, 0);
2795 } else if (!strcmp(p, "-sb")) {
2797 conf_set_int(conf, CONF_scrollbar, 1);
2799 } else if (!strcmp(p, "-name")) {
2803 } else if (!strcmp(p, "-xrm")) {
2805 provide_xrm_string(val);
2807 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2811 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
2815 } else if (!strcmp(p, "-pgpfp")) {
2819 } else if(p[0] != '-' && (!do_everything ||
2820 process_nonoption_arg(p, conf,
2826 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2833 int uxsel_input_add(int fd, int rwx) {
2835 if (rwx & 1) flags |= GDK_INPUT_READ;
2836 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2837 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2839 return gdk_input_add(fd, flags, fd_input_func, NULL);
2842 void uxsel_input_remove(int id) {
2843 gdk_input_remove(id);
2846 int frontend_net_pending_error_idle_id;
2847 int frontend_got_net_pending_errors = FALSE;
2848 gboolean frontend_net_pending_errors(gpointer data)
2850 net_pending_errors();
2851 gtk_idle_remove(frontend_net_pending_error_idle_id);
2852 frontend_got_net_pending_errors = FALSE;
2855 void frontend_net_error_pending(void)
2857 if (!frontend_got_net_pending_errors) {
2858 frontend_got_net_pending_errors = TRUE;
2859 frontend_net_pending_error_idle_id =
2860 gtk_idle_add(frontend_net_pending_errors, NULL);
2864 char *setup_fonts_ucs(struct gui_data *inst)
2866 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
2867 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
2872 fs = conf_get_fontspec(inst->conf, CONF_font);
2873 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
2874 shadowboldoffset, shadowbold);
2876 return dupprintf("unable to load font \"%s\"", fs->name);
2879 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
2880 if (shadowbold || !fs->name[0]) {
2883 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
2884 shadowboldoffset, shadowbold);
2887 unifont_destroy(fonts[0]);
2888 return dupprintf("unable to load bold font \"%s\"", fs->name);
2892 fs = conf_get_fontspec(inst->conf, CONF_widefont);
2894 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
2895 shadowboldoffset, shadowbold);
2897 for (i = 0; i < 2; i++)
2899 unifont_destroy(fonts[i]);
2900 return dupprintf("%s: unable to load wide font \"%s\"", fs->name);
2906 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
2907 if (shadowbold || !fs->name[0]) {
2910 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
2911 shadowboldoffset, shadowbold);
2913 for (i = 0; i < 3; i++)
2915 unifont_destroy(fonts[i]);
2916 return dupprintf("%s: unable to load wide bold font \"%s\"",
2922 * Now we've got past all the possible error conditions, we can
2923 * actually update our state.
2926 for (i = 0; i < 4; i++) {
2928 unifont_destroy(inst->fonts[i]);
2929 inst->fonts[i] = fonts[i];
2932 inst->font_width = inst->fonts[0]->width;
2933 inst->font_height = inst->fonts[0]->height;
2935 inst->direct_to_font = init_ucs(&inst->ucsdata,
2936 conf_get_str(inst->conf, CONF_line_codepage),
2937 conf_get_int(inst->conf, CONF_utf8_override),
2938 inst->fonts[0]->public_charset,
2939 conf_get_int(inst->conf, CONF_vtmode));
2944 void set_geom_hints(struct gui_data *inst)
2947 geom.min_width = inst->font_width + 2*inst->window_border;
2948 geom.min_height = inst->font_height + 2*inst->window_border;
2949 geom.max_width = geom.max_height = -1;
2950 geom.base_width = 2*inst->window_border;
2951 geom.base_height = 2*inst->window_border;
2952 geom.width_inc = inst->font_width;
2953 geom.height_inc = inst->font_height;
2954 geom.min_aspect = geom.max_aspect = 0;
2955 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2956 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2957 GDK_HINT_RESIZE_INC);
2960 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2962 struct gui_data *inst = (struct gui_data *)data;
2963 term_clrsb(inst->term);
2966 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2968 struct gui_data *inst = (struct gui_data *)data;
2969 term_pwron(inst->term, TRUE);
2971 ldisc_send(inst->ldisc, NULL, 0, 0);
2974 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2976 struct gui_data *inst = (struct gui_data *)data;
2977 term_copyall(inst->term);
2980 void special_menuitem(GtkMenuItem *item, gpointer data)
2982 struct gui_data *inst = (struct gui_data *)data;
2983 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2987 inst->back->special(inst->backhandle, code);
2990 void about_menuitem(GtkMenuItem *item, gpointer data)
2992 struct gui_data *inst = (struct gui_data *)data;
2993 about_box(inst->window);
2996 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2998 struct gui_data *inst = (struct gui_data *)data;
2999 showeventlog(inst->eventlogstuff, inst->window);
3002 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3004 /* This maps colour indices in inst->conf to those used in inst->cols. */
3005 static const int ww[] = {
3006 256, 257, 258, 259, 260, 261,
3007 0, 8, 1, 9, 2, 10, 3, 11,
3008 4, 12, 5, 13, 6, 14, 7, 15
3010 struct gui_data *inst = (struct gui_data *)data;
3012 Conf *oldconf, *newconf;
3013 int i, j, need_size;
3015 assert(lenof(ww) == NCFGCOLOURS);
3017 if (inst->reconfiguring)
3020 inst->reconfiguring = TRUE;
3022 title = dupcat(appname, " Reconfiguration", NULL);
3024 oldconf = inst->conf;
3025 newconf = conf_copy(inst->conf);
3027 if (do_config_box(title, newconf, 1,
3028 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3029 inst->conf = newconf;
3031 /* Pass new config data to the logging module */
3032 log_reconfig(inst->logctx, inst->conf);
3034 * Flush the line discipline's edit buffer in the case
3035 * where local editing has just been disabled.
3038 ldisc_configure(inst->ldisc, inst->conf);
3039 ldisc_send(inst->ldisc, NULL, 0, 0);
3041 /* Pass new config data to the terminal */
3042 term_reconfig(inst->term, inst->conf);
3043 /* Pass new config data to the back end */
3045 inst->back->reconfig(inst->backhandle, inst->conf);
3047 cache_conf_values(inst);
3050 * Just setting inst->conf is sufficient to cause colour
3051 * setting changes to appear on the next ESC]R palette
3052 * reset. But we should also check whether any colour
3053 * settings have been changed, and revert the ones that have
3054 * to the new default, on the assumption that the user is
3055 * most likely to want an immediate update.
3057 for (i = 0; i < NCFGCOLOURS; i++) {
3058 for (j = 0; j < 3; j++)
3059 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3060 conf_get_int_int(newconf, CONF_colours, i*3+j))
3063 real_palette_set(inst, ww[i],
3064 conf_get_int_int(newconf,CONF_colours,i*3+0),
3065 conf_get_int_int(newconf,CONF_colours,i*3+1),
3066 conf_get_int_int(newconf,CONF_colours,i*3+2));
3069 * If the default background has changed, we must
3070 * repaint the space in between the window border
3071 * and the text area.
3074 set_window_background(inst);
3075 draw_backing_rect(inst);
3081 * If the scrollbar needs to be shown, hidden, or moved
3082 * from one end to the other of the window, do so now.
3084 if (conf_get_int(oldconf, CONF_scrollbar) !=
3085 conf_get_int(newconf, CONF_scrollbar)) {
3086 if (conf_get_int(newconf, CONF_scrollbar))
3087 gtk_widget_show(inst->sbar);
3089 gtk_widget_hide(inst->sbar);
3091 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3092 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3093 gtk_box_reorder_child(inst->hbox, inst->sbar,
3094 conf_get_int(newconf, CONF_scrollbar_on_left)
3099 * Change the window title, if required.
3101 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3102 conf_get_str(newconf, CONF_wintitle)))
3103 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3104 set_window_titles(inst);
3107 * Redo the whole tangled fonts and Unicode mess if
3111 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3112 conf_get_fontspec(newconf, CONF_font)->name) ||
3113 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3114 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3115 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3116 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3117 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3118 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3119 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3120 conf_get_str(newconf, CONF_line_codepage)) ||
3121 conf_get_int(oldconf, CONF_utf8_override) !=
3122 conf_get_int(newconf, CONF_utf8_override) ||
3123 conf_get_int(oldconf, CONF_vtmode) !=
3124 conf_get_int(newconf, CONF_vtmode) ||
3125 conf_get_int(oldconf, CONF_shadowbold) !=
3126 conf_get_int(newconf, CONF_shadowbold) ||
3127 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3128 conf_get_int(newconf, CONF_shadowboldoffset)) {
3129 char *errmsg = setup_fonts_ucs(inst);
3132 dupprintf("Could not change fonts in terminal window: %s\n",
3134 messagebox(inst->window, "Font setup error", msgboxtext,
3135 string_width("Could not change fonts in terminal window:"),
3146 * Resize the window.
3148 if (conf_get_int(oldconf, CONF_width) !=
3149 conf_get_int(newconf, CONF_width) ||
3150 conf_get_int(oldconf, CONF_height) !=
3151 conf_get_int(newconf, CONF_height) ||
3152 conf_get_int(oldconf, CONF_window_border) !=
3153 conf_get_int(newconf, CONF_window_border) ||
3155 set_geom_hints(inst);
3156 request_resize(inst, conf_get_int(newconf, CONF_width),
3157 conf_get_int(newconf, CONF_height));
3160 * The above will have caused a call to term_size() for
3161 * us if it happened. If the user has fiddled with only
3162 * the scrollback size, the above will not have
3163 * happened and we will need an explicit term_size()
3166 if (conf_get_int(oldconf, CONF_savelines) !=
3167 conf_get_int(newconf, CONF_savelines))
3168 term_size(inst->term, inst->term->rows, inst->term->cols,
3169 conf_get_int(newconf, CONF_savelines));
3172 term_invalidate(inst->term);
3175 * We do an explicit full redraw here to ensure the window
3176 * border has been redrawn as well as the text area.
3178 gtk_widget_queue_draw(inst->area);
3185 inst->reconfiguring = FALSE;
3188 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3191 * Re-execing ourself is not an exact science under Unix. I do
3192 * the best I can by using /proc/self/exe if available and by
3193 * assuming argv[0] can be found on $PATH if not.
3195 * Note that we also have to reconstruct the elements of the
3196 * original argv which gtk swallowed, since the user wants the
3197 * new session to appear on the same X display as the old one.
3205 * Collect the arguments with which to re-exec ourself.
3207 va_start(ap, fd_to_close);
3208 n = 2; /* progname and terminating NULL */
3209 n += inst->ngtkargs;
3210 while (va_arg(ap, char *) != NULL)
3214 args = snewn(n, char *);
3215 args[0] = inst->progname;
3217 for (i = 0; i < inst->ngtkargs; i++)
3218 args[i+1] = inst->gtkargvstart[i];
3221 va_start(ap, fd_to_close);
3222 while ((args[i++] = va_arg(ap, char *)) != NULL);
3228 * Do the double fork.
3242 } else if (pid2 > 0) {
3244 * First child has successfully forked second child. My
3245 * Work Here Is Done. Note the use of _exit rather than
3246 * exit: the latter appears to cause destroy messages
3247 * to be sent to the X server. I suspect gtk uses
3254 * If we reach here, we are the second child, so we now
3255 * actually perform the exec.
3257 if (fd_to_close >= 0)
3260 execv("/proc/self/exe", args);
3261 execvp(inst->progname, args);
3268 waitpid(pid, &status, 0);
3273 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3275 struct gui_data *inst = (struct gui_data *)gdata;
3277 * For this feature we must marshal conf and (possibly) pty_argv
3278 * into a byte stream, create a pipe, and send this byte stream
3279 * to the child through the pipe.
3281 int i, ret, sersize, size;
3286 if (pipe(pipefd) < 0) {
3291 size = sersize = conf_serialised_size(inst->conf);
3292 if (use_pty_argv && pty_argv) {
3293 for (i = 0; pty_argv[i]; i++)
3294 size += strlen(pty_argv[i]) + 1;
3297 data = snewn(size, char);
3298 conf_serialise(inst->conf, data);
3299 if (use_pty_argv && pty_argv) {
3301 for (i = 0; pty_argv[i]; i++) {
3302 strcpy(data + p, pty_argv[i]);
3303 p += strlen(pty_argv[i]) + 1;
3308 sprintf(option, "---[%d,%d]", pipefd[0], size);
3309 noncloexec(pipefd[0]);
3310 fork_and_exec_self(inst, pipefd[1], option, NULL);
3314 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3317 perror("write to pipe");
3322 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
3324 int fd, i, ret, size, size_used;
3327 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3328 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3332 data = snewn(size, char);
3334 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3337 perror("read from pipe");
3339 } else if (i < size) {
3340 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3345 size_used = conf_deserialise(conf, data, size);
3346 if (use_pty_argv && size > size_used) {
3350 while (i < size && data[i]) i++;
3352 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3359 pty_argv = snewn(n+1, char *);
3365 while (i < size && data[i]) i++;
3368 pty_argv[n++] = dupstr(p);
3377 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3379 struct gui_data *inst = (struct gui_data *)data;
3381 fork_and_exec_self(inst, -1, NULL);
3384 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3386 struct gui_data *inst = (struct gui_data *)data;
3389 logevent(inst, "----- Session restarted -----");
3390 term_pwron(inst->term, FALSE);
3391 start_backend(inst);
3392 inst->exited = FALSE;
3396 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3398 struct gui_data *inst = (struct gui_data *)data;
3399 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3401 fork_and_exec_self(inst, -1, "-load", str, NULL);
3404 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3406 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3411 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3413 struct gui_data *inst = (struct gui_data *)data;
3414 struct sesslist sesslist;
3417 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3418 (GtkCallback)gtk_widget_destroy, NULL);
3420 get_sesslist(&sesslist, TRUE);
3421 /* skip sesslist.sessions[0] == Default Settings */
3422 for (i = 1; i < sesslist.nsessions; i++) {
3423 GtkWidget *menuitem =
3424 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3425 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3426 gtk_widget_show(menuitem);
3427 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3428 dupstr(sesslist.sessions[i]));
3429 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3430 GTK_SIGNAL_FUNC(saved_session_menuitem),
3432 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3433 GTK_SIGNAL_FUNC(saved_session_freedata),
3436 if (sesslist.nsessions <= 1) {
3437 GtkWidget *menuitem =
3438 gtk_menu_item_new_with_label("(No sessions)");
3439 gtk_widget_set_sensitive(menuitem, FALSE);
3440 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3441 gtk_widget_show(menuitem);
3443 get_sesslist(&sesslist, FALSE); /* free up */
3446 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3450 GdkBitmap *iconmask;
3451 #if GTK_CHECK_VERSION(2,0,0)
3459 gtk_widget_realize(window);
3460 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3461 NULL, (gchar **)icon[0]);
3462 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3464 #if GTK_CHECK_VERSION(2,0,0)
3466 for (n = 0; n < n_icon; n++) {
3468 g_list_append(iconlist,
3469 gdk_pixbuf_new_from_xpm_data((const gchar **)
3472 gdk_window_set_icon_list(window->window, iconlist);
3476 void update_specials_menu(void *frontend)
3478 struct gui_data *inst = (struct gui_data *)frontend;
3480 const struct telnet_special *specials;
3483 specials = inst->back->get_specials(inst->backhandle);
3487 /* I believe this disposes of submenus too. */
3488 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3489 (GtkCallback)gtk_widget_destroy, NULL);
3492 GtkWidget *menu = inst->specialsmenu;
3493 /* A lame "stack" for submenus that will do for now. */
3494 GtkWidget *saved_menu = NULL;
3496 for (i = 0; nesting > 0; i++) {
3497 GtkWidget *menuitem = NULL;
3498 switch (specials[i].code) {
3500 assert (nesting < 2);
3501 saved_menu = menu; /* XXX lame stacking */
3502 menu = gtk_menu_new();
3503 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3504 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3505 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3506 gtk_widget_show(menuitem);
3513 menu = saved_menu; /* XXX lame stacking */
3518 menuitem = gtk_menu_item_new();
3521 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3522 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3523 GINT_TO_POINTER(specials[i].code));
3524 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3525 GTK_SIGNAL_FUNC(special_menuitem), inst);
3529 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3530 gtk_widget_show(menuitem);
3533 gtk_widget_show(inst->specialsitem1);
3534 gtk_widget_show(inst->specialsitem2);
3536 gtk_widget_hide(inst->specialsitem1);
3537 gtk_widget_hide(inst->specialsitem2);
3541 static void start_backend(struct gui_data *inst)
3543 extern Backend *select_backend(Conf *conf);
3548 inst->back = select_backend(inst->conf);
3550 error = inst->back->init((void *)inst, &inst->backhandle,
3552 conf_get_str(inst->conf, CONF_host),
3553 conf_get_int(inst->conf, CONF_port),
3555 conf_get_int(inst->conf, CONF_tcp_nodelay),
3556 conf_get_int(inst->conf, CONF_tcp_keepalives));
3559 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3560 conf_get_str(inst->conf, CONF_host), error);
3561 inst->exited = TRUE;
3562 fatal_message_box(inst->window, msg);
3567 s = conf_get_str(inst->conf, CONF_wintitle);
3569 set_title_and_icon(inst, s, s);
3571 char *title = make_default_wintitle(realhost);
3572 set_title_and_icon(inst, title, title);
3577 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3579 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3582 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
3585 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3588 int pt_main(int argc, char **argv)
3590 extern int cfgbox(Conf *conf);
3591 struct gui_data *inst;
3593 setlocale(LC_CTYPE, "");
3596 * Create an instance structure and initialise to zeroes
3598 inst = snew(struct gui_data);
3599 memset(inst, 0, sizeof(*inst));
3600 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3601 inst->busy_status = BUSY_NOT;
3602 inst->conf = conf_new();
3603 inst->wintitle = inst->icontitle = NULL;
3605 /* defer any child exit handling until we're ready to deal with
3607 block_signal(SIGCHLD, 1);
3609 inst->progname = argv[0];
3611 * Copy the original argv before letting gtk_init fiddle with
3612 * it. It will be required later.
3616 inst->gtkargvstart = snewn(argc-1, char *);
3617 for (i = 1; i < argc; i++)
3618 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3620 gtk_init(&argc, &argv);
3621 inst->ngtkargs = oldargc - argc;
3624 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3625 read_dupsession_data(inst, inst->conf, argv[1]);
3626 /* Splatter this argument so it doesn't clutter a ps listing */
3627 smemclr(argv[1], strlen(argv[1]));
3629 /* By default, we bring up the config dialog, rather than launching
3630 * a session. This gets set to TRUE if something happens to change
3631 * that (e.g., a hostname is specified on the command-line). */
3632 int allow_launch = FALSE;
3633 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
3634 exit(1); /* pre-defaults pass to get -class */
3635 do_defaults(NULL, inst->conf);
3636 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
3637 exit(1); /* post-defaults, do everything */
3639 cmdline_run_saved(inst->conf);
3642 allow_launch = TRUE;
3644 if ((!allow_launch || !conf_launchable(inst->conf)) &&
3645 !cfgbox(inst->conf))
3646 exit(0); /* config box hit Cancel */
3649 if (!compound_text_atom)
3650 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3651 if (!utf8_string_atom)
3652 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3654 inst->area = gtk_drawing_area_new();
3656 #if GTK_CHECK_VERSION(2,0,0)
3657 inst->imc = gtk_im_multicontext_new();
3661 char *errmsg = setup_fonts_ucs(inst);
3663 fprintf(stderr, "%s: %s\n", appname, errmsg);
3669 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3671 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
3673 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
3674 winclass, winclass);
3678 * Set up the colour map.
3680 palette_reset(inst);
3682 inst->width = conf_get_int(inst->conf, CONF_width);
3683 inst->height = conf_get_int(inst->conf, CONF_height);
3684 cache_conf_values(inst);
3686 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3687 inst->font_width * inst->width + 2*inst->window_border,
3688 inst->font_height * inst->height + 2*inst->window_border);
3689 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3690 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3691 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3693 * We always create the scrollbar; it remains invisible if
3694 * unwanted, so we can pop it up quickly if it suddenly becomes
3697 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
3698 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3699 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3700 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
3701 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3703 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3705 set_geom_hints(inst);
3707 gtk_widget_show(inst->area);
3708 if (conf_get_int(inst->conf, CONF_scrollbar))
3709 gtk_widget_show(inst->sbar);
3711 gtk_widget_hide(inst->sbar);
3712 gtk_widget_show(GTK_WIDGET(inst->hbox));
3715 int x = inst->xpos, y = inst->ypos;
3717 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3718 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3719 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3720 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3721 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3724 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3725 GTK_SIGNAL_FUNC(destroy), inst);
3726 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3727 GTK_SIGNAL_FUNC(delete_window), inst);
3728 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3729 GTK_SIGNAL_FUNC(key_event), inst);
3730 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3731 GTK_SIGNAL_FUNC(key_event), inst);
3732 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3733 GTK_SIGNAL_FUNC(focus_event), inst);
3734 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3735 GTK_SIGNAL_FUNC(focus_event), inst);
3736 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3737 GTK_SIGNAL_FUNC(configure_area), inst);
3738 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3739 GTK_SIGNAL_FUNC(expose_area), inst);
3740 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3741 GTK_SIGNAL_FUNC(button_event), inst);
3742 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3743 GTK_SIGNAL_FUNC(button_event), inst);
3744 #if GTK_CHECK_VERSION(2,0,0)
3745 gtk_signal_connect(GTK_OBJECT(inst->area), "scroll_event",
3746 GTK_SIGNAL_FUNC(scroll_event), inst);
3748 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3749 GTK_SIGNAL_FUNC(motion_event), inst);
3750 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3751 GTK_SIGNAL_FUNC(selection_received), inst);
3752 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3753 GTK_SIGNAL_FUNC(selection_get), inst);
3754 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3755 GTK_SIGNAL_FUNC(selection_clear), inst);
3756 #if GTK_CHECK_VERSION(2,0,0)
3757 g_signal_connect(G_OBJECT(inst->imc), "commit",
3758 G_CALLBACK(input_method_commit_event), inst);
3760 if (conf_get_int(inst->conf, CONF_scrollbar))
3761 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3762 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3763 gtk_widget_add_events(GTK_WIDGET(inst->area),
3764 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3765 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3766 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3769 extern const char *const *const main_icon[];
3770 extern const int n_main_icon;
3771 set_window_icon(inst->window, main_icon, n_main_icon);
3774 gtk_widget_show(inst->window);
3776 set_window_background(inst);
3779 * Set up the Ctrl+rightclick context menu.
3782 GtkWidget *menuitem;
3784 extern const int use_event_log, new_session, saved_sessions;
3786 inst->menu = gtk_menu_new();
3788 #define MKMENUITEM(title, func) do \
3790 menuitem = gtk_menu_item_new_with_label(title); \
3791 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3792 gtk_widget_show(menuitem); \
3793 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3794 GTK_SIGNAL_FUNC(func), inst); \
3797 #define MKSUBMENU(title) do \
3799 menuitem = gtk_menu_item_new_with_label(title); \
3800 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3801 gtk_widget_show(menuitem); \
3804 #define MKSEP() do \
3806 menuitem = gtk_menu_item_new(); \
3807 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3808 gtk_widget_show(menuitem); \
3812 MKMENUITEM("New Session...", new_session_menuitem);
3813 MKMENUITEM("Restart Session", restart_session_menuitem);
3814 inst->restartitem = menuitem;
3815 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3816 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3817 if (saved_sessions) {
3818 inst->sessionsmenu = gtk_menu_new();
3819 /* sessionsmenu will be updated when it's invoked */
3820 /* XXX is this the right way to do dynamic menus in Gtk? */
3821 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3822 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3823 inst->sessionsmenu);
3826 MKMENUITEM("Change Settings...", change_settings_menuitem);
3829 MKMENUITEM("Event Log", event_log_menuitem);
3830 MKSUBMENU("Special Commands");
3831 inst->specialsmenu = gtk_menu_new();
3832 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3833 inst->specialsitem1 = menuitem;
3835 inst->specialsitem2 = menuitem;
3836 gtk_widget_hide(inst->specialsitem1);
3837 gtk_widget_hide(inst->specialsitem2);
3838 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3839 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3840 MKMENUITEM("Copy All", copy_all_menuitem);
3842 s = dupcat("About ", appname, NULL);
3843 MKMENUITEM(s, about_menuitem);
3850 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3851 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3852 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3853 inst->blankcursor = make_mouse_ptr(inst, -1);
3854 make_mouse_ptr(inst, -2); /* clean up cursor font */
3855 inst->currcursor = inst->textcursor;
3856 show_mouseptr(inst, 1);
3858 inst->eventlogstuff = eventlogstuff_new();
3860 request_callback_notifications(notify_toplevel_callback, inst);
3862 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
3863 inst->logctx = log_init(inst, inst->conf);
3864 term_provide_logctx(inst->term, inst->logctx);
3868 term_size(inst->term, inst->height, inst->width,
3869 conf_get_int(inst->conf, CONF_savelines));
3871 start_backend(inst);
3873 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3875 /* now we're reday to deal with the child exit handler being
3877 block_signal(SIGCHLD, 0);
3880 * Block SIGPIPE: if we attempt Duplicate Session or similar
3881 * and it falls over in some way, we certainly don't want
3882 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3883 * this _after_ (at least pterm) forks off its child process,
3884 * since the child wants SIGPIPE handled in the usual way.
3886 block_signal(SIGPIPE, 1);
3888 inst->exited = FALSE;