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 #if !GTK_CHECK_VERSION(3,0,0)
23 #include <gdk/gdkkeysyms.h>
28 #include <X11/Xutil.h>
29 #include <X11/Xatom.h>
32 #if GTK_CHECK_VERSION(2,0,0)
33 #include <gtk/gtkimmodule.h>
36 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
38 #define MAY_REFER_TO_GTK_IN_HEADERS
42 #include "gtkcompat.h"
45 #define CAT2(x,y) x ## y
46 #define CAT(x,y) CAT2(x,y)
47 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
49 #if GTK_CHECK_VERSION(2,0,0)
50 ASSERT(sizeof(long) <= sizeof(gsize));
51 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
52 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
54 ASSERT(sizeof(long) <= sizeof(gpointer));
55 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
56 #define GPOINTER_TO_LONG(p) ((long)(p))
59 /* Colours come in two flavours: configurable, and xterm-extended. */
60 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
61 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
63 GdkAtom compound_text_atom, utf8_string_atom;
65 extern char **pty_argv; /* declared in pty.c */
66 extern int use_pty_argv;
69 * Timers are global across all sessions (even if we were handling
70 * multiple sessions, which we aren't), so the current timer ID is
73 static guint timer_id = 0;
76 GtkWidget *window, *area, *sbar;
78 GtkAdjustment *sbar_adjust;
79 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
81 GtkWidget *sessionsmenu;
83 #if GTK_CHECK_VERSION(2,0,0)
86 unifont *fonts[4]; /* normal, bold, wide, widebold */
87 #if GTK_CHECK_VERSION(2,0,0)
90 int xpos, ypos, gotpos, gravity;
92 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
93 GdkColor cols[NALLCOLOURS];
95 wchar_t *pastein_data;
98 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
99 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
100 int font_width, font_height;
103 int mouseptr_visible;
105 guint toplevel_callback_idle_id;
106 int idle_fn_scheduled, quit_fn_scheduled;
111 int master_fd, master_func_id;
118 struct unicode_data ucsdata;
121 char *progname, **gtkargvstart;
123 guint32 input_event_time; /* Timestamp of the most recent input event. */
125 /* Cached things out of conf that we refer to a lot */
132 static void cache_conf_values(struct gui_data *inst)
134 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
135 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
136 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
140 struct gui_data *inst;
141 unifont_drawctx uctx;
144 static int send_raw_mouse;
146 static const char *app_name = "pterm";
148 static void start_backend(struct gui_data *inst);
149 static void exit_callback(void *vinst);
151 char *x_get_default(const char *key)
153 #ifndef NOT_X_WINDOWS
154 return XGetDefault(GDK_DISPLAY(), app_name, key);
160 void connection_fatal(void *frontend, const char *p, ...)
162 struct gui_data *inst = (struct gui_data *)frontend;
167 msg = dupvprintf(p, ap);
169 fatal_message_box(inst->window, msg);
172 queue_toplevel_callback(exit_callback, inst);
176 * Default settings that are specific to pterm.
178 FontSpec *platform_default_fontspec(const char *name)
180 if (!strcmp(name, "Font"))
181 return fontspec_new("server:fixed");
183 return fontspec_new("");
186 Filename *platform_default_filename(const char *name)
188 if (!strcmp(name, "LogFileName"))
189 return filename_from_str("putty.log");
191 return filename_from_str("");
194 char *platform_default_s(const char *name)
196 if (!strcmp(name, "SerialLine"))
197 return dupstr("/dev/ttyS0");
201 int platform_default_i(const char *name, int def)
203 if (!strcmp(name, "CloseOnExit"))
204 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
205 if (!strcmp(name, "WinNameAlways"))
206 return 0; /* X natively supports icon titles, so use 'em by default */
210 /* Dummy routine, only required in plink. */
211 void frontend_echoedit_update(void *frontend, int echo, int edit)
215 char *get_ttymode(void *frontend, const char *mode)
217 struct gui_data *inst = (struct gui_data *)frontend;
218 return term_get_ttymode(inst->term, mode);
221 int from_backend(void *frontend, int is_stderr, const char *data, int len)
223 struct gui_data *inst = (struct gui_data *)frontend;
224 return term_data(inst->term, is_stderr, data, len);
227 int from_backend_untrusted(void *frontend, const char *data, int len)
229 struct gui_data *inst = (struct gui_data *)frontend;
230 return term_data_untrusted(inst->term, data, len);
233 int from_backend_eof(void *frontend)
235 return TRUE; /* do respond to incoming EOF with outgoing */
238 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
240 struct gui_data *inst = (struct gui_data *)p->frontend;
242 ret = cmdline_get_passwd_input(p, in, inlen);
244 ret = term_get_userpass_input(inst->term, p, in, inlen);
248 void logevent(void *frontend, const char *string)
250 struct gui_data *inst = (struct gui_data *)frontend;
252 log_eventlog(inst->logctx, string);
254 logevent_dlg(inst->eventlogstuff, string);
257 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
259 struct gui_data *inst = (struct gui_data *)frontend;
262 return inst->font_height;
264 return inst->font_width;
268 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
269 * into a cooked one (SELECT, EXTEND, PASTE).
271 * In Unix, this is not configurable; the X button arrangement is
272 * rock-solid across all applications, everyone has a three-button
273 * mouse or a means of faking it, and there is no need to switch
274 * buttons around at all.
276 static Mouse_Button translate_button(Mouse_Button button)
278 /* struct gui_data *inst = (struct gui_data *)frontend; */
280 if (button == MBT_LEFT)
282 if (button == MBT_MIDDLE)
284 if (button == MBT_RIGHT)
286 return 0; /* shouldn't happen */
290 * Return the top-level GtkWindow associated with a particular
291 * front end instance.
293 void *get_window(void *frontend)
295 struct gui_data *inst = (struct gui_data *)frontend;
300 * Minimise or restore the window in response to a server-side
303 void set_iconic(void *frontend, int iconic)
306 * GTK 1.2 doesn't know how to do this.
308 #if GTK_CHECK_VERSION(2,0,0)
309 struct gui_data *inst = (struct gui_data *)frontend;
311 gtk_window_iconify(GTK_WINDOW(inst->window));
313 gtk_window_deiconify(GTK_WINDOW(inst->window));
318 * Move the window in response to a server-side request.
320 void move_window(void *frontend, int x, int y)
322 struct gui_data *inst = (struct gui_data *)frontend;
324 * I assume that when the GTK version of this call is available
325 * we should use it. Not sure how it differs from the GDK one,
328 #if GTK_CHECK_VERSION(2,0,0)
329 gtk_window_move(GTK_WINDOW(inst->window), x, y);
331 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
336 * Move the window to the top or bottom of the z-order in response
337 * to a server-side request.
339 void set_zorder(void *frontend, int top)
341 struct gui_data *inst = (struct gui_data *)frontend;
343 gdk_window_raise(gtk_widget_get_window(inst->window));
345 gdk_window_lower(gtk_widget_get_window(inst->window));
349 * Refresh the window in response to a server-side request.
351 void refresh_window(void *frontend)
353 struct gui_data *inst = (struct gui_data *)frontend;
354 term_invalidate(inst->term);
358 * Maximise or restore the window in response to a server-side
361 void set_zoomed(void *frontend, int zoomed)
364 * GTK 1.2 doesn't know how to do this.
366 #if GTK_CHECK_VERSION(2,0,0)
367 struct gui_data *inst = (struct gui_data *)frontend;
369 gtk_window_maximize(GTK_WINDOW(inst->window));
371 gtk_window_unmaximize(GTK_WINDOW(inst->window));
376 * Report whether the window is iconic, for terminal reports.
378 int is_iconic(void *frontend)
380 struct gui_data *inst = (struct gui_data *)frontend;
381 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
385 * Report the window's position, for terminal reports.
387 void get_window_pos(void *frontend, int *x, int *y)
389 struct gui_data *inst = (struct gui_data *)frontend;
391 * I assume that when the GTK version of this call is available
392 * we should use it. Not sure how it differs from the GDK one,
395 #if GTK_CHECK_VERSION(2,0,0)
396 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
398 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
403 * Report the window's pixel size, for terminal reports.
405 void get_window_pixels(void *frontend, int *x, int *y)
407 struct gui_data *inst = (struct gui_data *)frontend;
409 * I assume that when the GTK version of this call is available
410 * we should use it. Not sure how it differs from the GDK one,
413 #if GTK_CHECK_VERSION(2,0,0)
414 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
416 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
421 * Return the window or icon title.
423 char *get_window_title(void *frontend, int icon)
425 struct gui_data *inst = (struct gui_data *)frontend;
426 return icon ? inst->icontitle : inst->wintitle;
429 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
431 struct gui_data *inst = (struct gui_data *)data;
432 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
433 if (!reallyclose(inst))
439 static void update_mouseptr(struct gui_data *inst)
441 switch (inst->busy_status) {
443 if (!inst->mouseptr_visible) {
444 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
446 } else if (send_raw_mouse) {
447 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
450 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
454 case BUSY_WAITING: /* XXX can we do better? */
456 /* We always display these cursors. */
457 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
465 static void show_mouseptr(struct gui_data *inst, int show)
467 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
469 inst->mouseptr_visible = show;
470 update_mouseptr(inst);
473 static void draw_backing_rect(struct gui_data *inst);
475 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
477 struct gui_data *inst = (struct gui_data *)data;
478 int w, h, need_size = 0;
481 * See if the terminal size has changed, in which case we must
482 * let the terminal know.
484 w = (event->width - 2*inst->window_border) / inst->font_width;
485 h = (event->height - 2*inst->window_border) / inst->font_height;
486 if (w != inst->width || h != inst->height) {
489 conf_set_int(inst->conf, CONF_width, inst->width);
490 conf_set_int(inst->conf, CONF_height, inst->height);
495 gdk_pixmap_unref(inst->pixmap);
499 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
500 (w * inst->font_width + 2*inst->window_border),
501 (h * inst->font_height + 2*inst->window_border), -1);
503 draw_backing_rect(inst);
505 if (need_size && inst->term) {
506 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
510 term_invalidate(inst->term);
512 #if GTK_CHECK_VERSION(2,0,0)
513 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
519 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
521 struct gui_data *inst = (struct gui_data *)data;
524 * Pass the exposed rectangle to terminal.c, which will call us
525 * back to do the actual painting.
528 gdk_draw_pixmap(gtk_widget_get_window(widget),
529 (gtk_widget_get_style(widget)->fg_gc
530 [gtk_widget_get_state(widget)]),
532 event->area.x, event->area.y,
533 event->area.x, event->area.y,
534 event->area.width, event->area.height);
539 #define KEY_PRESSED(k) \
540 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
542 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
544 struct gui_data *inst = (struct gui_data *)data;
546 wchar_t ucsoutput[2];
547 int ucsval, start, end, special, output_charset, use_ucsoutput;
548 int nethack_mode, app_keypad_mode;
550 /* Remember the timestamp. */
551 inst->input_event_time = event->time;
553 /* By default, nothing is generated. */
555 special = use_ucsoutput = FALSE;
556 output_charset = CS_ISO8859_1;
559 * If Alt is being released after typing an Alt+numberpad
560 * sequence, we should generate the code that was typed.
562 * Note that we only do this if more than one key was actually
563 * pressed - I don't think Alt+NumPad4 should be ^D or that
564 * Alt+NumPad3 should be ^C, for example. There's no serious
565 * inconvenience in having to type a zero before a single-digit
568 if (event->type == GDK_KEY_RELEASE) {
569 if ((event->keyval == GDK_KEY_Meta_L ||
570 event->keyval == GDK_KEY_Meta_R ||
571 event->keyval == GDK_KEY_Alt_L ||
572 event->keyval == GDK_KEY_Alt_R) &&
573 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
575 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
578 * FIXME: we might usefully try to do something clever here
579 * about interpreting the generated key code in a way that's
580 * appropriate to the line code page.
582 output[0] = inst->alt_keycode;
586 #if GTK_CHECK_VERSION(2,0,0)
587 if (gtk_im_context_filter_keypress(inst->imc, event))
592 if (event->type == GDK_KEY_PRESS) {
596 printf("keypress: keyval = %04x, state = %08x; string =",
597 event->keyval, event->state);
598 for (i = 0; event->string[i]; i++)
599 printf(" %02x", (unsigned char) event->string[i]);
605 * NYI: Compose key (!!! requires Unicode faff before even trying)
609 * If Alt has just been pressed, we start potentially
610 * accumulating an Alt+numberpad code. We do this by
611 * setting alt_keycode to -1 (nothing yet but plausible).
613 if ((event->keyval == GDK_KEY_Meta_L ||
614 event->keyval == GDK_KEY_Meta_R ||
615 event->keyval == GDK_KEY_Alt_L ||
616 event->keyval == GDK_KEY_Alt_R)) {
617 inst->alt_keycode = -1;
618 inst->alt_digits = 0;
619 goto done; /* this generates nothing else */
623 * If we're seeing a numberpad key press with Mod1 down,
624 * consider adding it to alt_keycode if that's sensible.
625 * Anything _else_ with Mod1 down cancels any possibility
626 * of an ALT keycode: we set alt_keycode to -2.
628 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
630 switch (event->keyval) {
631 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
632 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
633 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
634 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
635 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
636 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
637 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
638 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
639 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
640 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
643 inst->alt_keycode = -2; /* it's invalid */
646 printf("Adding digit %d to keycode %d", digit,
649 if (inst->alt_keycode == -1)
650 inst->alt_keycode = digit; /* one-digit code */
652 inst->alt_keycode = inst->alt_keycode * 10 + digit;
655 printf(" gives new code %d\n", inst->alt_keycode);
657 /* Having used this digit, we now do nothing more with it. */
663 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
666 if (event->keyval == GDK_KEY_Page_Up &&
667 (event->state & GDK_SHIFT_MASK)) {
668 term_scroll(inst->term, 0, -inst->height/2);
671 if (event->keyval == GDK_KEY_Page_Up &&
672 (event->state & GDK_CONTROL_MASK)) {
673 term_scroll(inst->term, 0, -1);
676 if (event->keyval == GDK_KEY_Page_Down &&
677 (event->state & GDK_SHIFT_MASK)) {
678 term_scroll(inst->term, 0, +inst->height/2);
681 if (event->keyval == GDK_KEY_Page_Down &&
682 (event->state & GDK_CONTROL_MASK)) {
683 term_scroll(inst->term, 0, +1);
688 * Neither does Shift-Ins.
690 if (event->keyval == GDK_KEY_Insert &&
691 (event->state & GDK_SHIFT_MASK)) {
697 use_ucsoutput = FALSE;
699 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
700 app_keypad_mode = (inst->term->app_keypad_keys &&
701 !conf_get_int(inst->conf, CONF_no_applic_k));
703 /* ALT+things gives leading Escape. */
705 #if !GTK_CHECK_VERSION(2,0,0)
707 * In vanilla X, and hence also GDK 1.2, the string received
708 * as part of a keyboard event is assumed to be in
709 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
710 * but it's true: see the man page for XLookupString(3) for
713 output_charset = CS_ISO8859_1;
714 strncpy(output+1, event->string, lenof(output)-1);
717 * Most things can now be passed to
718 * gtk_im_context_filter_keypress without breaking anything
719 * below this point. An exception is the numeric keypad if
720 * we're in Nethack or application mode: the IM will eat
721 * numeric keypad presses if Num Lock is on, but we don't want
724 if (app_keypad_mode &&
725 (event->keyval == GDK_KEY_Num_Lock ||
726 event->keyval == GDK_KEY_KP_Divide ||
727 event->keyval == GDK_KEY_KP_Multiply ||
728 event->keyval == GDK_KEY_KP_Subtract ||
729 event->keyval == GDK_KEY_KP_Add ||
730 event->keyval == GDK_KEY_KP_Enter ||
731 event->keyval == GDK_KEY_KP_0 ||
732 event->keyval == GDK_KEY_KP_Insert ||
733 event->keyval == GDK_KEY_KP_1 ||
734 event->keyval == GDK_KEY_KP_End ||
735 event->keyval == GDK_KEY_KP_2 ||
736 event->keyval == GDK_KEY_KP_Down ||
737 event->keyval == GDK_KEY_KP_3 ||
738 event->keyval == GDK_KEY_KP_Page_Down ||
739 event->keyval == GDK_KEY_KP_4 ||
740 event->keyval == GDK_KEY_KP_Left ||
741 event->keyval == GDK_KEY_KP_5 ||
742 event->keyval == GDK_KEY_KP_Begin ||
743 event->keyval == GDK_KEY_KP_6 ||
744 event->keyval == GDK_KEY_KP_Right ||
745 event->keyval == GDK_KEY_KP_7 ||
746 event->keyval == GDK_KEY_KP_Home ||
747 event->keyval == GDK_KEY_KP_8 ||
748 event->keyval == GDK_KEY_KP_Up ||
749 event->keyval == GDK_KEY_KP_9 ||
750 event->keyval == GDK_KEY_KP_Page_Up ||
751 event->keyval == GDK_KEY_KP_Decimal ||
752 event->keyval == GDK_KEY_KP_Delete)) {
753 /* app keypad; do nothing */
754 } else if (nethack_mode &&
755 (event->keyval == GDK_KEY_KP_1 ||
756 event->keyval == GDK_KEY_KP_End ||
757 event->keyval == GDK_KEY_KP_2 ||
758 event->keyval == GDK_KEY_KP_Down ||
759 event->keyval == GDK_KEY_KP_3 ||
760 event->keyval == GDK_KEY_KP_Page_Down ||
761 event->keyval == GDK_KEY_KP_4 ||
762 event->keyval == GDK_KEY_KP_Left ||
763 event->keyval == GDK_KEY_KP_5 ||
764 event->keyval == GDK_KEY_KP_Begin ||
765 event->keyval == GDK_KEY_KP_6 ||
766 event->keyval == GDK_KEY_KP_Right ||
767 event->keyval == GDK_KEY_KP_7 ||
768 event->keyval == GDK_KEY_KP_Home ||
769 event->keyval == GDK_KEY_KP_8 ||
770 event->keyval == GDK_KEY_KP_Up ||
771 event->keyval == GDK_KEY_KP_9 ||
772 event->keyval == GDK_KEY_KP_Page_Up)) {
773 /* nethack mode; do nothing */
775 if (gtk_im_context_filter_keypress(inst->imc, event))
780 * GDK 2.0 arranges to have done some translation for us: in
781 * GDK 2.0, event->string is encoded in the current locale.
783 * So we use the standard C library function mbstowcs() to
784 * convert from the current locale into Unicode; from there
785 * we can convert to whatever PuTTY is currently working in.
786 * (In fact I convert straight back to UTF-8 from
787 * wide-character Unicode, for the sake of simplicity: that
788 * way we can still use exactly the same code to manipulate
789 * the string, such as prefixing ESC.)
791 output_charset = CS_UTF8;
793 wchar_t widedata[32];
798 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
799 event->string, strlen(event->string),
800 widedata, lenof(widedata)-1);
803 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
804 CS_UTF8, NULL, NULL, 0);
805 output[1+ulen] = '\0';
810 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
811 ucsoutput[0] = '\033';
812 ucsoutput[1] = ucsval;
813 use_ucsoutput = TRUE;
816 output[lenof(output)-1] = '\0';
817 end = strlen(output);
819 if (event->state & GDK_MOD1_MASK) {
821 if (end == 1) end = 0;
825 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
826 if (!output[1] && event->keyval == '`' &&
827 (event->state & GDK_CONTROL_MASK)) {
829 use_ucsoutput = FALSE;
833 /* Control-Break sends a Break special to the backend */
834 if (event->keyval == GDK_KEY_Break &&
835 (event->state & GDK_CONTROL_MASK)) {
837 inst->back->special(inst->backhandle, TS_BRK);
841 /* We handle Return ourselves, because it needs to be flagged as
842 * special to ldisc. */
843 if (event->keyval == GDK_KEY_Return) {
845 use_ucsoutput = FALSE;
850 /* Control-2, Control-Space and Control-@ are NUL */
852 (event->keyval == ' ' || event->keyval == '2' ||
853 event->keyval == '@') &&
854 (event->state & (GDK_SHIFT_MASK |
855 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
857 use_ucsoutput = FALSE;
861 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
862 if (!output[1] && event->keyval == ' ' &&
863 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
864 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
866 output_charset = CS_ISO8859_1;
867 use_ucsoutput = FALSE;
871 /* We don't let GTK tell us what Backspace is! We know better. */
872 if (event->keyval == GDK_KEY_BackSpace &&
873 !(event->state & GDK_SHIFT_MASK)) {
874 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
876 use_ucsoutput = FALSE;
880 /* For Shift Backspace, do opposite of what is configured. */
881 if (event->keyval == GDK_KEY_BackSpace &&
882 (event->state & GDK_SHIFT_MASK)) {
883 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
885 use_ucsoutput = FALSE;
890 /* Shift-Tab is ESC [ Z */
891 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
892 (event->keyval == GDK_KEY_Tab &&
893 (event->state & GDK_SHIFT_MASK))) {
894 end = 1 + sprintf(output+1, "\033[Z");
895 use_ucsoutput = FALSE;
897 /* And normal Tab is Tab, if the keymap hasn't already told us.
898 * (Curiously, at least one version of the MacOS 10.5 X server
899 * doesn't translate Tab for us. */
900 if (event->keyval == GDK_KEY_Tab && end <= 1) {
906 * NetHack keypad mode.
909 const char *keys = NULL;
910 switch (event->keyval) {
911 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
912 keys = "bB\002"; break;
913 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
914 keys = "jJ\012"; break;
915 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
916 keys = "nN\016"; break;
917 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
918 keys = "hH\010"; break;
919 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
921 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
922 keys = "lL\014"; break;
923 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
924 keys = "yY\031"; break;
925 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
926 keys = "kK\013"; break;
927 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
928 keys = "uU\025"; break;
932 if (event->state & GDK_CONTROL_MASK)
934 else if (event->state & GDK_SHIFT_MASK)
938 use_ucsoutput = FALSE;
944 * Application keypad mode.
946 if (app_keypad_mode) {
948 switch (event->keyval) {
949 case GDK_KEY_Num_Lock: xkey = 'P'; break;
950 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
951 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
952 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
954 * Keypad + is tricky. It covers a space that would
955 * be taken up on the VT100 by _two_ keys; so we
956 * let Shift select between the two. Worse still,
957 * in xterm function key mode we change which two...
960 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
961 if (event->state & GDK_SHIFT_MASK)
965 } else if (event->state & GDK_SHIFT_MASK)
970 case GDK_KEY_KP_Enter: xkey = 'M'; break;
971 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
972 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
973 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
974 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
975 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
976 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
977 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
978 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
979 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
980 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
981 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
985 if (inst->term->vt52_mode) {
986 if (xkey >= 'P' && xkey <= 'S')
987 end = 1 + sprintf(output+1, "\033%c", xkey);
989 end = 1 + sprintf(output+1, "\033?%c", xkey);
991 end = 1 + sprintf(output+1, "\033O%c", xkey);
992 use_ucsoutput = FALSE;
998 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
999 * for integer decimal nn.)
1001 * We also deal with the weird ones here. Linux VCs replace F1
1002 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1003 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1008 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1009 switch (event->keyval) {
1011 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1014 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1017 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1020 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1023 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1026 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1029 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1032 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1035 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1038 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1071 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1072 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1075 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1078 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1081 case GDK_KEY_End: case GDK_KEY_KP_End:
1084 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1087 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1091 /* Reorder edit keys to physical order */
1092 if (funky_type == FUNKY_VT400 && code <= 6)
1093 code = "\0\2\1\4\5\3\6"[code];
1095 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1096 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1097 use_ucsoutput = FALSE;
1101 if (funky_type == FUNKY_SCO && /* SCO function keys */
1102 code >= 11 && code <= 34) {
1103 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1105 switch (event->keyval) {
1106 case GDK_KEY_F1: index = 0; break;
1107 case GDK_KEY_F2: index = 1; break;
1108 case GDK_KEY_F3: index = 2; break;
1109 case GDK_KEY_F4: index = 3; break;
1110 case GDK_KEY_F5: index = 4; break;
1111 case GDK_KEY_F6: index = 5; break;
1112 case GDK_KEY_F7: index = 6; break;
1113 case GDK_KEY_F8: index = 7; break;
1114 case GDK_KEY_F9: index = 8; break;
1115 case GDK_KEY_F10: index = 9; break;
1116 case GDK_KEY_F11: index = 10; break;
1117 case GDK_KEY_F12: index = 11; break;
1119 if (event->state & GDK_SHIFT_MASK) index += 12;
1120 if (event->state & GDK_CONTROL_MASK) index += 24;
1121 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1122 use_ucsoutput = FALSE;
1125 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1126 code >= 1 && code <= 6) {
1127 char codes[] = "HL.FIG";
1132 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1134 use_ucsoutput = FALSE;
1137 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1138 code >= 11 && code <= 24) {
1144 if (inst->term->vt52_mode)
1145 end = 1 + sprintf(output+1,
1146 "\x1B%c", code + 'P' - 11 - offt);
1148 end = 1 + sprintf(output+1,
1149 "\x1BO%c", code + 'P' - 11 - offt);
1150 use_ucsoutput = FALSE;
1153 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1154 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1155 use_ucsoutput = FALSE;
1158 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1159 if (inst->term->vt52_mode)
1160 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1162 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1163 use_ucsoutput = FALSE;
1166 if ((code == 1 || code == 4) &&
1167 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1168 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1169 use_ucsoutput = FALSE;
1173 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1174 use_ucsoutput = FALSE;
1180 * Cursor keys. (This includes the numberpad cursor keys,
1181 * if we haven't already done them due to app keypad mode.)
1183 * Here we also process un-numlocked un-appkeypadded KP5,
1184 * which sends ESC [ G.
1188 switch (event->keyval) {
1189 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1190 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1191 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1192 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1193 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1196 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1197 event->state & GDK_CONTROL_MASK);
1198 use_ucsoutput = FALSE;
1207 if (end-start > 0) {
1208 #ifdef KEY_DEBUGGING
1210 printf("generating sequence:");
1211 for (i = start; i < end; i++)
1212 printf(" %02x", (unsigned char) output[i]);
1218 * For special control characters, the character set
1219 * should never matter.
1221 output[end] = '\0'; /* NUL-terminate */
1223 ldisc_send(inst->ldisc, output+start, -2, 1);
1224 } else if (!inst->direct_to_font) {
1225 if (!use_ucsoutput) {
1227 lpage_send(inst->ldisc, output_charset, output+start,
1231 * We generated our own Unicode key data from the
1232 * keysym, so use that instead.
1235 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1239 * In direct-to-font mode, we just send the string
1240 * exactly as we received it.
1243 ldisc_send(inst->ldisc, output+start, end-start, 1);
1246 show_mouseptr(inst, 0);
1247 term_seen_key_event(inst->term);
1253 #if GTK_CHECK_VERSION(2,0,0)
1254 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1256 struct gui_data *inst = (struct gui_data *)data;
1258 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1259 show_mouseptr(inst, 0);
1260 term_seen_key_event(inst->term);
1264 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1265 GdkEventType type, guint ebutton, guint state,
1266 gdouble ex, gdouble ey)
1268 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1270 /* Remember the timestamp. */
1271 inst->input_event_time = timestamp;
1273 show_mouseptr(inst, 1);
1275 shift = state & GDK_SHIFT_MASK;
1276 ctrl = state & GDK_CONTROL_MASK;
1277 alt = state & GDK_MOD1_MASK;
1280 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1281 CONF_mouse_override));
1283 if (!raw_mouse_mode) {
1284 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1285 term_scroll(inst->term, 0, -5);
1288 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1289 term_scroll(inst->term, 0, +5);
1294 if (ebutton == 3 && ctrl) {
1295 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1296 ebutton, timestamp);
1302 else if (ebutton == 2)
1303 button = MBT_MIDDLE;
1304 else if (ebutton == 3)
1306 else if (ebutton == 4)
1307 button = MBT_WHEEL_UP;
1308 else if (ebutton == 5)
1309 button = MBT_WHEEL_DOWN;
1311 return FALSE; /* don't even know what button! */
1314 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1315 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1316 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1317 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1318 default: return FALSE; /* don't know this event type */
1321 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1322 return TRUE; /* we ignore these in raw mouse mode */
1324 x = (ex - inst->window_border) / inst->font_width;
1325 y = (ey - inst->window_border) / inst->font_height;
1327 term_mouse(inst->term, button, translate_button(button), act,
1328 x, y, shift, ctrl, alt);
1333 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1335 struct gui_data *inst = (struct gui_data *)data;
1336 return button_internal(inst, event->time, event->type, event->button,
1337 event->state, event->x, event->y);
1340 #if GTK_CHECK_VERSION(2,0,0)
1342 * In GTK 2, mouse wheel events have become a new type of event.
1343 * This handler translates them back into button-4 and button-5
1344 * presses so that I don't have to change my old code too much :-)
1346 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1348 struct gui_data *inst = (struct gui_data *)data;
1351 if (event->direction == GDK_SCROLL_UP)
1353 else if (event->direction == GDK_SCROLL_DOWN)
1358 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1359 button, event->state, event->x, event->y);
1363 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1365 struct gui_data *inst = (struct gui_data *)data;
1366 int shift, ctrl, alt, x, y, button;
1368 /* Remember the timestamp. */
1369 inst->input_event_time = event->time;
1371 show_mouseptr(inst, 1);
1373 shift = event->state & GDK_SHIFT_MASK;
1374 ctrl = event->state & GDK_CONTROL_MASK;
1375 alt = event->state & GDK_MOD1_MASK;
1376 if (event->state & GDK_BUTTON1_MASK)
1378 else if (event->state & GDK_BUTTON2_MASK)
1379 button = MBT_MIDDLE;
1380 else if (event->state & GDK_BUTTON3_MASK)
1383 return FALSE; /* don't even know what button! */
1385 x = (event->x - inst->window_border) / inst->font_width;
1386 y = (event->y - inst->window_border) / inst->font_height;
1388 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1389 x, y, shift, ctrl, alt);
1394 void frontend_keypress(void *handle)
1396 struct gui_data *inst = (struct gui_data *)handle;
1399 * If our child process has exited but not closed, terminate on
1406 static void exit_callback(void *vinst)
1408 struct gui_data *inst = (struct gui_data *)vinst;
1409 int exitcode, close_on_exit;
1411 if (!inst->exited &&
1412 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1413 inst->exited = TRUE;
1414 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1415 if (close_on_exit == FORCE_ON ||
1416 (close_on_exit == AUTO && exitcode == 0))
1417 gtk_main_quit(); /* just go */
1419 ldisc_free(inst->ldisc);
1422 inst->back->free(inst->backhandle);
1423 inst->backhandle = NULL;
1425 term_provide_resize_fn(inst->term, NULL, NULL);
1426 update_specials_menu(inst);
1427 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1432 * Replacement code for the gtk_quit_add() function, which GTK2 - in
1433 * their unbounded wisdom - deprecated without providing any usable
1434 * replacement, and which we were using to ensure that our idle
1435 * function for toplevel callbacks was only run from the outermost
1438 * We maintain a global variable with a list of 'struct gui_data'
1439 * instances on which we should call inst_post_main() when a
1440 * subsidiary gtk_main() terminates; then we must make sure that all
1441 * our subsidiary calls to gtk_main() are followed by a call to
1444 * This is kind of overkill in the sense that at the time of writing
1445 * we don't actually ever run more than one 'struct gui_data' instance
1446 * in the same process, but we're _so nearly_ prepared to do that that
1447 * I want to remain futureproof against the possibility of doing so in
1450 struct post_main_context {
1451 struct post_main_context *next;
1452 struct gui_data *inst;
1454 struct post_main_context *post_main_list_head = NULL;
1455 static void request_post_main(struct gui_data *inst)
1457 struct post_main_context *node = snew(struct post_main_context);
1458 node->next = post_main_list_head;
1460 post_main_list_head = node;
1462 static void inst_post_main(struct gui_data *inst);
1463 void post_main(void)
1465 while (post_main_list_head) {
1466 struct post_main_context *node = post_main_list_head;
1467 post_main_list_head = node->next;
1468 inst_post_main(node->inst);
1473 void notify_remote_exit(void *frontend)
1475 struct gui_data *inst = (struct gui_data *)frontend;
1477 queue_toplevel_callback(exit_callback, inst);
1480 static void notify_toplevel_callback(void *frontend);
1482 static void inst_post_main(struct gui_data *inst)
1484 if (gtk_main_level() == 1) {
1485 notify_toplevel_callback(inst);
1486 inst->quit_fn_scheduled = FALSE;
1488 /* Apparently we're _still_ more than one level deep in
1489 * gtk_main() instances, so we'll need another callback for
1490 * when we get out of the next one. */
1491 request_post_main(inst);
1495 static gint idle_toplevel_callback_func(gpointer data)
1497 struct gui_data *inst = (struct gui_data *)data;
1499 if (gtk_main_level() > 1) {
1501 * We don't run the callbacks if we're in the middle of a
1502 * subsidiary gtk_main. Instead, ask for a callback when we
1503 * get back out of the subsidiary main loop (if we haven't
1504 * already arranged one), so we can reschedule ourself then.
1506 if (!inst->quit_fn_scheduled) {
1507 request_post_main(inst);
1508 inst->quit_fn_scheduled = TRUE;
1511 * And unschedule this idle function, since we've now done
1512 * everything we can until the innermost gtk_main has quit and
1513 * can reschedule us with a chance of actually taking action.
1515 if (inst->idle_fn_scheduled) { /* double-check, just in case */
1516 g_source_remove(inst->toplevel_callback_idle_id);
1517 inst->idle_fn_scheduled = FALSE;
1520 run_toplevel_callbacks();
1524 * If we've emptied our toplevel callback queue, unschedule
1525 * ourself. Otherwise, leave ourselves pending so we'll be called
1526 * again to deal with more callbacks after another round of the
1529 if (!toplevel_callback_pending() && inst->idle_fn_scheduled) {
1530 g_source_remove(inst->toplevel_callback_idle_id);
1531 inst->idle_fn_scheduled = FALSE;
1537 static void notify_toplevel_callback(void *frontend)
1539 struct gui_data *inst = (struct gui_data *)frontend;
1541 if (!inst->idle_fn_scheduled) {
1542 inst->toplevel_callback_idle_id =
1543 g_idle_add(idle_toplevel_callback_func, inst);
1544 inst->idle_fn_scheduled = TRUE;
1548 static gint timer_trigger(gpointer data)
1550 unsigned long now = GPOINTER_TO_LONG(data);
1551 unsigned long next, then;
1555 * Destroy the timer we got here on.
1558 g_source_remove(timer_id);
1563 * run_timers() may cause a call to timer_change_notify, in which
1564 * case a new timer will already have been set up and left in
1565 * timer_id. If it hasn't, and run_timers reports that some timing
1566 * still needs to be done, we do it ourselves.
1568 if (run_timers(now, &next) && !timer_id) {
1570 now = GETTICKCOUNT();
1571 if (now - then > next - then)
1575 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1579 * Returning FALSE means 'don't call this timer again', which
1580 * _should_ be redundant given that we removed it above, but just
1581 * in case, return FALSE anyway.
1586 void timer_change_notify(unsigned long next)
1591 g_source_remove(timer_id);
1593 ticks = next - GETTICKCOUNT();
1595 ticks = 1; /* just in case */
1597 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1600 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1603 * We must process exceptional notifications before ordinary
1604 * readability ones, or we may go straight past the urgent
1607 if (condition & GDK_INPUT_EXCEPTION)
1608 select_result(sourcefd, 4);
1609 if (condition & GDK_INPUT_READ)
1610 select_result(sourcefd, 1);
1611 if (condition & GDK_INPUT_WRITE)
1612 select_result(sourcefd, 2);
1615 void destroy(GtkWidget *widget, gpointer data)
1620 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1622 struct gui_data *inst = (struct gui_data *)data;
1623 term_set_focus(inst->term, event->in);
1624 term_update(inst->term);
1625 show_mouseptr(inst, 1);
1629 void set_busy_status(void *frontend, int status)
1631 struct gui_data *inst = (struct gui_data *)frontend;
1632 inst->busy_status = status;
1633 update_mouseptr(inst);
1637 * set or clear the "raw mouse message" mode
1639 void set_raw_mouse_mode(void *frontend, int activate)
1641 struct gui_data *inst = (struct gui_data *)frontend;
1642 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1643 send_raw_mouse = activate;
1644 update_mouseptr(inst);
1647 void request_resize(void *frontend, int w, int h)
1649 struct gui_data *inst = (struct gui_data *)frontend;
1650 int large_x, large_y;
1651 int offset_x, offset_y;
1653 GtkRequisition inner, outer;
1656 * This is a heinous hack dreamed up by the gnome-terminal
1657 * people to get around a limitation in gtk. The problem is
1658 * that in order to set the size correctly we really need to be
1659 * calling gtk_window_resize - but that needs to know the size
1660 * of the _whole window_, not the drawing area. So what we do
1661 * is to set an artificially huge size request on the drawing
1662 * area, recompute the resulting size request on the window,
1663 * and look at the difference between the two. That gives us
1664 * the x and y offsets we need to translate drawing area size
1665 * into window size for real, and then we call
1666 * gtk_window_resize.
1670 * We start by retrieving the current size of the whole window.
1671 * Adding a bit to _that_ will give us a value we can use as a
1672 * bogus size request which guarantees to be bigger than the
1673 * current size of the drawing area.
1675 get_window_pixels(inst, &large_x, &large_y);
1679 gtk_widget_set_size_request(inst->area, large_x, large_y);
1680 gtk_widget_size_request(inst->area, &inner);
1681 gtk_widget_size_request(inst->window, &outer);
1683 offset_x = outer.width - inner.width;
1684 offset_y = outer.height - inner.height;
1686 area_x = inst->font_width * w + 2*inst->window_border;
1687 area_y = inst->font_height * h + 2*inst->window_border;
1690 * Now we must set the size request on the drawing area back to
1691 * something sensible before we commit the real resize. Best
1692 * way to do this, I think, is to set it to what the size is
1693 * really going to end up being.
1695 gtk_widget_set_size_request(inst->area, area_x, area_y);
1696 #if GTK_CHECK_VERSION(2,0,0)
1697 gtk_window_resize(GTK_WINDOW(inst->window),
1698 area_x + offset_x, area_y + offset_y);
1700 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1702 * I can no longer remember what this call to
1703 * gtk_container_dequeue_resize_handler is for. It was
1704 * introduced in r3092 with no comment, and the commit log
1705 * message was uninformative. I'm _guessing_ its purpose is to
1706 * prevent gratuitous resize processing on the window given
1707 * that we're about to resize it anyway, but I have no idea
1708 * why that's so incredibly vital.
1710 * I've tried removing the call, and nothing seems to go
1711 * wrong. I've backtracked to r3092 and tried removing the
1712 * call there, and still nothing goes wrong. So I'm going to
1713 * adopt the working hypothesis that it's superfluous; I won't
1714 * actually remove it from the GTK 1.2 code, but I won't
1715 * attempt to replicate its functionality in the GTK 2 code
1718 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1719 gdk_window_resize(gtk_widget_get_window(inst->window),
1720 area_x + offset_x, area_y + offset_y);
1724 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1726 gboolean success[1];
1728 inst->cols[n].red = r * 0x0101;
1729 inst->cols[n].green = g * 0x0101;
1730 inst->cols[n].blue = b * 0x0101;
1732 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1733 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1734 FALSE, TRUE, success);
1736 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1740 void set_window_background(struct gui_data *inst)
1742 if (inst->area && gtk_widget_get_window(inst->area))
1743 gdk_window_set_background(gtk_widget_get_window(inst->area),
1745 if (inst->window && gtk_widget_get_window(inst->window))
1746 gdk_window_set_background(gtk_widget_get_window(inst->window),
1750 void palette_set(void *frontend, int n, int r, int g, int b)
1752 struct gui_data *inst = (struct gui_data *)frontend;
1755 if (n >= NALLCOLOURS)
1757 real_palette_set(inst, n, r, g, b);
1759 /* Default Background changed. Ensure space between text area and
1760 * window border is redrawn */
1761 set_window_background(inst);
1762 draw_backing_rect(inst);
1763 gtk_widget_queue_draw(inst->area);
1767 void palette_reset(void *frontend)
1769 struct gui_data *inst = (struct gui_data *)frontend;
1770 /* This maps colour indices in inst->conf to those used in inst->cols. */
1771 static const int ww[] = {
1772 256, 257, 258, 259, 260, 261,
1773 0, 8, 1, 9, 2, 10, 3, 11,
1774 4, 12, 5, 13, 6, 14, 7, 15
1776 gboolean success[NALLCOLOURS];
1779 assert(lenof(ww) == NCFGCOLOURS);
1781 if (!inst->colmap) {
1782 inst->colmap = gdk_colormap_get_system();
1784 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1787 for (i = 0; i < NCFGCOLOURS; i++) {
1788 inst->cols[ww[i]].red =
1789 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
1790 inst->cols[ww[i]].green =
1791 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
1792 inst->cols[ww[i]].blue =
1793 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
1796 for (i = 0; i < NEXTCOLOURS; i++) {
1798 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1799 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1800 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1801 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1803 int shade = i - 216;
1804 shade = shade * 0x0a0a + 0x0808;
1805 inst->cols[i+16].red = inst->cols[i+16].green =
1806 inst->cols[i+16].blue = shade;
1810 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1811 FALSE, TRUE, success);
1812 for (i = 0; i < NALLCOLOURS; i++) {
1814 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1816 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
1817 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
1818 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
1821 /* Since Default Background may have changed, ensure that space
1822 * between text area and window border is refreshed. */
1823 set_window_background(inst);
1824 if (inst->area && gtk_widget_get_window(inst->area)) {
1825 draw_backing_rect(inst);
1826 gtk_widget_queue_draw(inst->area);
1830 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1831 * do this before we start using cut buffers.
1833 void init_cutbuffers()
1835 #ifndef NOT_X_WINDOWS
1836 unsigned char empty[] = "";
1837 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1838 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1839 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1840 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1841 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1842 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1843 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1844 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1845 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1846 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1847 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1848 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1849 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1850 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1851 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1852 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1856 /* Store the data in a cut-buffer. */
1857 void store_cutbuffer(char * ptr, int len)
1859 #ifndef NOT_X_WINDOWS
1860 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1861 XRotateBuffers(GDK_DISPLAY(), 1);
1862 XStoreBytes(GDK_DISPLAY(), ptr, len);
1866 /* Retrieve data from a cut-buffer.
1867 * Returned data needs to be freed with XFree().
1869 char * retrieve_cutbuffer(int * nbytes)
1871 #ifndef NOT_X_WINDOWS
1873 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1874 if (*nbytes <= 0 && ptr != 0) {
1884 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1886 struct gui_data *inst = (struct gui_data *)frontend;
1887 if (inst->pasteout_data)
1888 sfree(inst->pasteout_data);
1889 if (inst->pasteout_data_ctext)
1890 sfree(inst->pasteout_data_ctext);
1891 if (inst->pasteout_data_utf8)
1892 sfree(inst->pasteout_data_utf8);
1895 * Set up UTF-8 and compound text paste data. This only happens
1896 * if we aren't in direct-to-font mode using the D800 hack.
1898 if (!inst->direct_to_font) {
1899 const wchar_t *tmp = data;
1901 #ifndef NOT_X_WINDOWS
1906 inst->pasteout_data_utf8 = snewn(len*6, char);
1907 inst->pasteout_data_utf8_len = len*6;
1908 inst->pasteout_data_utf8_len =
1909 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1910 inst->pasteout_data_utf8_len,
1911 CS_UTF8, NULL, NULL, 0);
1912 if (inst->pasteout_data_utf8_len == 0) {
1913 sfree(inst->pasteout_data_utf8);
1914 inst->pasteout_data_utf8 = NULL;
1916 inst->pasteout_data_utf8 =
1917 sresize(inst->pasteout_data_utf8,
1918 inst->pasteout_data_utf8_len + 1, char);
1919 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1923 * Now let Xlib convert our UTF-8 data into compound text.
1925 #ifndef NOT_X_WINDOWS
1926 list[0] = inst->pasteout_data_utf8;
1927 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1928 XCompoundTextStyle, &tp) == 0) {
1929 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1930 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1931 inst->pasteout_data_ctext_len = tp.nitems;
1936 inst->pasteout_data_ctext = NULL;
1937 inst->pasteout_data_ctext_len = 0;
1940 inst->pasteout_data_utf8 = NULL;
1941 inst->pasteout_data_utf8_len = 0;
1942 inst->pasteout_data_ctext = NULL;
1943 inst->pasteout_data_ctext_len = 0;
1946 inst->pasteout_data = snewn(len*6, char);
1947 inst->pasteout_data_len = len*6;
1948 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1949 data, len, inst->pasteout_data,
1950 inst->pasteout_data_len,
1952 if (inst->pasteout_data_len == 0) {
1953 sfree(inst->pasteout_data);
1954 inst->pasteout_data = NULL;
1956 inst->pasteout_data =
1957 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1960 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1962 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1963 inst->input_event_time)) {
1964 #if GTK_CHECK_VERSION(2,0,0)
1965 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
1967 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1968 GDK_SELECTION_TYPE_STRING, 1);
1969 if (inst->pasteout_data_ctext)
1970 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1971 compound_text_atom, 1);
1972 if (inst->pasteout_data_utf8)
1973 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1974 utf8_string_atom, 1);
1978 term_deselect(inst->term);
1981 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1982 guint info, guint time_stamp, gpointer data)
1984 struct gui_data *inst = (struct gui_data *)data;
1985 GdkAtom target = gtk_selection_data_get_target(seldata);
1986 if (target == utf8_string_atom)
1987 gtk_selection_data_set(seldata, target, 8,
1988 (unsigned char *)inst->pasteout_data_utf8,
1989 inst->pasteout_data_utf8_len);
1990 else if (target == compound_text_atom)
1991 gtk_selection_data_set(seldata, target, 8,
1992 (unsigned char *)inst->pasteout_data_ctext,
1993 inst->pasteout_data_ctext_len);
1995 gtk_selection_data_set(seldata, target, 8,
1996 (unsigned char *)inst->pasteout_data,
1997 inst->pasteout_data_len);
2000 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2003 struct gui_data *inst = (struct gui_data *)data;
2005 term_deselect(inst->term);
2006 if (inst->pasteout_data)
2007 sfree(inst->pasteout_data);
2008 if (inst->pasteout_data_ctext)
2009 sfree(inst->pasteout_data_ctext);
2010 if (inst->pasteout_data_utf8)
2011 sfree(inst->pasteout_data_utf8);
2012 inst->pasteout_data = NULL;
2013 inst->pasteout_data_len = 0;
2014 inst->pasteout_data_ctext = NULL;
2015 inst->pasteout_data_ctext_len = 0;
2016 inst->pasteout_data_utf8 = NULL;
2017 inst->pasteout_data_utf8_len = 0;
2021 void request_paste(void *frontend)
2023 struct gui_data *inst = (struct gui_data *)frontend;
2025 * In Unix, pasting is asynchronous: all we can do at the
2026 * moment is to call gtk_selection_convert(), and when the data
2027 * comes back _then_ we can call term_do_paste().
2030 if (!inst->direct_to_font) {
2032 * First we attempt to retrieve the selection as a UTF-8
2033 * string (which we will convert to the correct code page
2034 * before sending to the session, of course). If that
2035 * fails, selection_received() will be informed and will
2036 * fall back to an ordinary string.
2038 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2040 inst->input_event_time);
2043 * If we're in direct-to-font mode, we disable UTF-8
2044 * pasting, and go straight to ordinary string data.
2046 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2047 GDK_SELECTION_TYPE_STRING,
2048 inst->input_event_time);
2052 gint idle_paste_func(gpointer data); /* forward ref */
2054 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2055 guint time, gpointer data)
2057 struct gui_data *inst = (struct gui_data *)data;
2060 #ifndef NOT_X_WINDOWS
2062 int free_list_required = 0;
2063 int free_required = 0;
2066 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2067 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2068 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2069 gint seldata_length = gtk_selection_data_get_length(seldata);
2071 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2073 * Failed to get a UTF-8 selection string. Try compound
2076 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2078 inst->input_event_time);
2082 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2084 * Failed to get UTF-8 or compound text. Try an ordinary
2087 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2088 GDK_SELECTION_TYPE_STRING,
2089 inst->input_event_time);
2094 * If we have data, but it's not of a type we can deal with,
2095 * we have to ignore the data.
2097 if (seldata_length > 0 &&
2098 seldata_type != GDK_SELECTION_TYPE_STRING &&
2099 seldata_type != compound_text_atom &&
2100 seldata_type != utf8_string_atom)
2104 * If we have no data, try looking in a cut buffer.
2106 if (seldata_length <= 0) {
2107 #ifndef NOT_X_WINDOWS
2108 text = retrieve_cutbuffer(&length);
2111 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2112 * source, so use that as a de-facto standard. */
2113 charset = CS_ISO8859_1;
2120 * Convert COMPOUND_TEXT into UTF-8.
2122 if (seldata_type == compound_text_atom) {
2123 #ifndef NOT_X_WINDOWS
2127 tp.value = (unsigned char *)seldata_data;
2128 tp.encoding = (Atom) seldata_type;
2129 tp.format = gtk_selection_data_get_format(seldata);
2130 tp.nitems = seldata_length;
2131 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
2133 if (ret == 0 && count == 1) {
2135 length = strlen(list[0]);
2137 free_list_required = 1;
2142 * Compound text failed; fall back to STRING.
2144 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2145 GDK_SELECTION_TYPE_STRING,
2146 inst->input_event_time);
2150 text = (char *)seldata_data;
2151 length = seldata_length;
2152 charset = (seldata_type == utf8_string_atom ?
2153 CS_UTF8 : inst->ucsdata.line_codepage);
2157 if (inst->pastein_data)
2158 sfree(inst->pastein_data);
2160 inst->pastein_data = snewn(length, wchar_t);
2161 inst->pastein_data_len = length;
2162 inst->pastein_data_len =
2163 mb_to_wc(charset, 0, text, length,
2164 inst->pastein_data, inst->pastein_data_len);
2166 term_do_paste(inst->term);
2168 #ifndef NOT_X_WINDOWS
2169 if (free_list_required)
2170 XFreeStringList(list);
2176 void get_clip(void *frontend, wchar_t ** p, int *len)
2178 struct gui_data *inst = (struct gui_data *)frontend;
2181 *p = inst->pastein_data;
2182 *len = inst->pastein_data_len;
2186 static void set_window_titles(struct gui_data *inst)
2189 * We must always call set_icon_name after calling set_title,
2190 * since set_title will write both names. Irritating, but such
2193 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2194 if (!conf_get_int(inst->conf, CONF_win_name_always))
2195 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
2199 void set_title(void *frontend, char *title)
2201 struct gui_data *inst = (struct gui_data *)frontend;
2202 sfree(inst->wintitle);
2203 inst->wintitle = dupstr(title);
2204 set_window_titles(inst);
2207 void set_icon(void *frontend, char *title)
2209 struct gui_data *inst = (struct gui_data *)frontend;
2210 sfree(inst->icontitle);
2211 inst->icontitle = dupstr(title);
2212 set_window_titles(inst);
2215 void set_title_and_icon(void *frontend, char *title, char *icon)
2217 struct gui_data *inst = (struct gui_data *)frontend;
2218 sfree(inst->wintitle);
2219 inst->wintitle = dupstr(title);
2220 sfree(inst->icontitle);
2221 inst->icontitle = dupstr(icon);
2222 set_window_titles(inst);
2225 void set_sbar(void *frontend, int total, int start, int page)
2227 struct gui_data *inst = (struct gui_data *)frontend;
2228 if (!conf_get_int(inst->conf, CONF_scrollbar))
2230 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
2231 gtk_adjustment_set_upper(inst->sbar_adjust, total);
2232 gtk_adjustment_set_value(inst->sbar_adjust, start);
2233 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
2234 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
2235 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
2236 inst->ignore_sbar = TRUE;
2237 gtk_adjustment_changed(inst->sbar_adjust);
2238 inst->ignore_sbar = FALSE;
2241 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2243 struct gui_data *inst = (struct gui_data *)data;
2245 if (!conf_get_int(inst->conf, CONF_scrollbar))
2247 if (!inst->ignore_sbar)
2248 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
2251 void sys_cursor(void *frontend, int x, int y)
2254 * This is meaningless under X.
2259 * This is still called when mode==BELL_VISUAL, even though the
2260 * visual bell is handled entirely within terminal.c, because we
2261 * may want to perform additional actions on any kind of bell (for
2262 * example, taskbar flashing in Windows).
2264 void do_beep(void *frontend, int mode)
2266 if (mode == BELL_DEFAULT)
2270 int char_width(Context ctx, int uc)
2273 * Under X, any fixed-width font really _is_ fixed-width.
2274 * Double-width characters will be dealt with using a separate
2275 * font. For the moment we can simply return 1.
2277 * FIXME: but is that also true of Pango?
2282 Context get_ctx(void *frontend)
2284 struct gui_data *inst = (struct gui_data *)frontend;
2285 struct draw_ctx *dctx;
2287 if (!gtk_widget_get_window(inst->area))
2290 dctx = snew(struct draw_ctx);
2292 dctx->uctx.type = inst->drawtype;
2293 #ifdef DRAW_TEXT_GDK
2294 if (dctx->uctx.type == DRAWTYPE_GDK) {
2295 dctx->uctx.u.gdk.target = inst->pixmap;
2296 dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
2299 #ifdef DRAW_TEXT_CAIRO
2300 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2301 dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
2302 dctx->uctx.u.cairo.cr = gdk_cairo_create(inst->pixmap);
2303 cairo_get_matrix(dctx->uctx.u.cairo.cr,
2304 &dctx->uctx.u.cairo.origmatrix);
2305 cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
2306 cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
2307 cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
2308 /* This antialiasing setting appears to be ignored for Pango
2309 * font rendering but honoured for stroking and filling paths;
2310 * I don't quite understand the logic of that, but I won't
2311 * complain since it's exactly what I happen to want */
2312 cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
2318 void free_ctx(Context ctx)
2320 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2321 /* struct gui_data *inst = dctx->inst; */
2322 #ifdef DRAW_TEXT_GDK
2323 if (dctx->uctx.type == DRAWTYPE_GDK) {
2324 gdk_gc_unref(dctx->uctx.u.gdk.gc);
2327 #ifdef DRAW_TEXT_CAIRO
2328 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2329 cairo_destroy(dctx->uctx.u.cairo.cr);
2336 static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
2338 #ifdef DRAW_TEXT_GDK
2339 if (dctx->uctx.type == DRAWTYPE_GDK) {
2340 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2341 dctx->uctx.u.gdk.gc, dctx->inst->pixmap,
2345 #ifdef DRAW_TEXT_CAIRO /* FIXME: and not GTK3 where a cairo_t is all we have */
2346 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2347 GdkGC *gc = gdk_gc_new(gtk_widget_get_window(dctx->inst->area));
2348 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2349 gc, dctx->inst->pixmap, x, y, x, y, w, h);
2355 static void draw_set_colour(struct draw_ctx *dctx, int col)
2357 #ifdef DRAW_TEXT_GDK
2358 if (dctx->uctx.type == DRAWTYPE_GDK) {
2359 gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
2362 #ifdef DRAW_TEXT_CAIRO
2363 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2364 cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
2365 dctx->inst->cols[col].red / 65535.0,
2366 dctx->inst->cols[col].green / 65535.0,
2367 dctx->inst->cols[col].blue / 65535.0);
2372 static void draw_rectangle(struct draw_ctx *dctx, int filled,
2373 int x, int y, int w, int h)
2375 #ifdef DRAW_TEXT_GDK
2376 if (dctx->uctx.type == DRAWTYPE_GDK) {
2377 gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2378 filled, x, y, w, h);
2381 #ifdef DRAW_TEXT_CAIRO
2382 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2383 cairo_new_path(dctx->uctx.u.cairo.cr);
2385 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2386 cairo_fill(dctx->uctx.u.cairo.cr);
2388 cairo_rectangle(dctx->uctx.u.cairo.cr,
2389 x + 0.5, y + 0.5, w, h);
2390 cairo_close_path(dctx->uctx.u.cairo.cr);
2391 cairo_stroke(dctx->uctx.u.cairo.cr);
2397 static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
2399 #ifdef DRAW_TEXT_GDK
2400 if (dctx->uctx.type == DRAWTYPE_GDK) {
2408 gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
2411 #ifdef DRAW_TEXT_CAIRO
2412 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2413 cairo_reset_clip(dctx->uctx.u.cairo.cr);
2414 cairo_new_path(dctx->uctx.u.cairo.cr);
2415 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2416 cairo_clip(dctx->uctx.u.cairo.cr);
2421 static void draw_point(struct draw_ctx *dctx, int x, int y)
2423 #ifdef DRAW_TEXT_GDK
2424 if (dctx->uctx.type == DRAWTYPE_GDK) {
2425 gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
2428 #ifdef DRAW_TEXT_CAIRO
2429 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2430 cairo_new_path(dctx->uctx.u.cairo.cr);
2431 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
2432 cairo_fill(dctx->uctx.u.cairo.cr);
2437 static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
2439 #ifdef DRAW_TEXT_GDK
2440 if (dctx->uctx.type == DRAWTYPE_GDK) {
2441 gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2445 #ifdef DRAW_TEXT_CAIRO
2446 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2447 cairo_new_path(dctx->uctx.u.cairo.cr);
2448 cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
2449 cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
2450 cairo_stroke(dctx->uctx.u.cairo.cr);
2455 static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
2457 int h, int hdouble, int hbothalf)
2459 #ifdef DRAW_TEXT_CAIRO
2460 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2461 cairo_matrix_t matrix;
2485 cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
2490 static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
2492 int h, int hdouble, int hbothalf)
2494 #ifdef DRAW_TEXT_GDK
2495 if (dctx->uctx.type == DRAWTYPE_GDK) {
2497 * I can't find any plausible StretchBlt equivalent in the X
2498 * server, so I'm going to do this the slow and painful way.
2499 * This will involve repeated calls to gdk_draw_pixmap() to
2500 * stretch the text horizontally. It's O(N^2) in time and O(N)
2501 * in network bandwidth, but you try thinking of a better way.
2506 for (i = 0; i < w; i++) {
2507 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
2508 dctx->uctx.u.gdk.gc,
2509 dctx->uctx.u.gdk.target,
2519 /* Now stretch vertically, in the same way. */
2524 for (i = 0; i < h; i += 2) {
2525 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
2526 dctx->uctx.u.gdk.gc,
2527 dctx->uctx.u.gdk.target,
2535 #ifdef DRAW_TEXT_CAIRO
2536 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2537 cairo_set_matrix(dctx->uctx.u.cairo.cr,
2538 &dctx->uctx.u.cairo.origmatrix);
2543 static void draw_backing_rect(struct gui_data *inst)
2545 struct draw_ctx *dctx = get_ctx(inst);
2546 draw_set_colour(dctx, 258);
2547 draw_rectangle(dctx, 1, 0, 0,
2548 inst->width * inst->font_width + 2*inst->window_border,
2549 inst->height * inst->font_height + 2*inst->window_border);
2554 * Draw a line of text in the window, at given character
2555 * coordinates, in given attributes.
2557 * We are allowed to fiddle with the contents of `text'.
2559 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2560 unsigned long attr, int lattr)
2562 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2563 struct gui_data *inst = dctx->inst;
2564 int ncombining, combining;
2565 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2567 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
2569 if (attr & TATTR_COMBINING) {
2575 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2576 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2577 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2582 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
2583 if (nfg < 16) nfg |= 8;
2584 else if (nfg >= 256) nfg |= 1;
2586 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
2587 if (nbg < 16) nbg |= 8;
2588 else if (nbg >= 256) nbg |= 1;
2590 if ((attr & TATTR_ACTCURS) && !monochrome) {
2595 fontid = shadow = 0;
2597 if (attr & ATTR_WIDE) {
2604 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
2611 if (!inst->fonts[fontid]) {
2614 * Fall back through font ids with subsets of this one's
2615 * set bits, in order.
2617 for (i = fontid; i-- > 0 ;) {
2619 continue; /* some other bit is set */
2620 if (inst->fonts[i]) {
2625 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2628 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2630 if (x >= inst->term->cols)
2632 if (x + len*2*widefactor > inst->term->cols)
2633 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2639 x*inst->font_width+inst->window_border,
2640 y*inst->font_height+inst->window_border,
2641 rlen*widefactor*inst->font_width,
2644 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2645 draw_stretch_before(dctx,
2646 x*inst->font_width+inst->window_border,
2647 y*inst->font_height+inst->window_border,
2648 rlen*widefactor*inst->font_width, TRUE,
2650 ((lattr & LATTR_MODE) != LATTR_WIDE),
2651 ((lattr & LATTR_MODE) == LATTR_BOT));
2654 draw_set_colour(dctx, nbg);
2655 draw_rectangle(dctx, TRUE,
2656 x*inst->font_width+inst->window_border,
2657 y*inst->font_height+inst->window_border,
2658 rlen*widefactor*inst->font_width, inst->font_height);
2660 draw_set_colour(dctx, nfg);
2661 for (combining = 0; combining < ncombining; combining++) {
2662 unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
2663 x*inst->font_width+inst->window_border,
2664 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2665 text + combining, len, widefactor > 1,
2666 bold, inst->font_width);
2669 if (attr & ATTR_UNDER) {
2670 int uheight = inst->fonts[0]->ascent + 1;
2671 if (uheight >= inst->font_height)
2672 uheight = inst->font_height - 1;
2673 draw_line(dctx, x*inst->font_width+inst->window_border,
2674 y*inst->font_height + uheight + inst->window_border,
2675 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2676 y*inst->font_height + uheight + inst->window_border);
2679 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2680 draw_stretch_after(dctx,
2681 x*inst->font_width+inst->window_border,
2682 y*inst->font_height+inst->window_border,
2683 rlen*widefactor*inst->font_width, TRUE,
2685 ((lattr & LATTR_MODE) != LATTR_WIDE),
2686 ((lattr & LATTR_MODE) == LATTR_BOT));
2690 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2691 unsigned long attr, int lattr)
2693 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2694 struct gui_data *inst = dctx->inst;
2697 do_text_internal(ctx, x, y, text, len, attr, lattr);
2699 if (attr & ATTR_WIDE) {
2705 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2707 if (x >= inst->term->cols)
2709 if (x + len*2*widefactor > inst->term->cols)
2710 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2715 x*inst->font_width+inst->window_border,
2716 y*inst->font_height+inst->window_border,
2717 len*widefactor*inst->font_width, inst->font_height);
2720 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2721 unsigned long attr, int lattr)
2723 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2724 struct gui_data *inst = dctx->inst;
2726 int active, passive, widefactor;
2728 if (attr & TATTR_PASCURS) {
2729 attr &= ~TATTR_PASCURS;
2733 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
2734 attr &= ~TATTR_ACTCURS;
2738 do_text_internal(ctx, x, y, text, len, attr, lattr);
2740 if (attr & TATTR_COMBINING)
2743 if (attr & ATTR_WIDE) {
2749 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2751 if (x >= inst->term->cols)
2753 if (x + len*2*widefactor > inst->term->cols)
2754 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2758 if (inst->cursor_type == 0) {
2760 * An active block cursor will already have been done by
2761 * the above do_text call, so we only need to do anything
2765 draw_set_colour(dctx, 261);
2766 draw_rectangle(dctx, FALSE,
2767 x*inst->font_width+inst->window_border,
2768 y*inst->font_height+inst->window_border,
2769 len*widefactor*inst->font_width-1,
2770 inst->font_height-1);
2774 int startx, starty, dx, dy, length, i;
2778 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2779 char_width = 2*inst->font_width;
2781 char_width = inst->font_width;
2783 if (inst->cursor_type == 1) {
2784 uheight = inst->fonts[0]->ascent + 1;
2785 if (uheight >= inst->font_height)
2786 uheight = inst->font_height - 1;
2788 startx = x * inst->font_width + inst->window_border;
2789 starty = y * inst->font_height + inst->window_border + uheight;
2792 length = len * widefactor * char_width;
2795 if (attr & TATTR_RIGHTCURS)
2796 xadjust = char_width - 1;
2797 startx = x * inst->font_width + inst->window_border + xadjust;
2798 starty = y * inst->font_height + inst->window_border;
2801 length = inst->font_height;
2804 draw_set_colour(dctx, 261);
2806 for (i = 0; i < length; i++) {
2808 draw_point(dctx, startx, starty);
2813 } else if (active) {
2814 draw_line(dctx, startx, starty,
2815 startx + (length-1) * dx, starty + (length-1) * dy);
2816 } /* else no cursor (e.g., blinked off) */
2820 x*inst->font_width+inst->window_border,
2821 y*inst->font_height+inst->window_border,
2822 len*widefactor*inst->font_width, inst->font_height);
2824 #if GTK_CHECK_VERSION(2,0,0)
2826 GdkRectangle cursorrect;
2827 cursorrect.x = x*inst->font_width+inst->window_border;
2828 cursorrect.y = y*inst->font_height+inst->window_border;
2829 cursorrect.width = len*widefactor*inst->font_width;
2830 cursorrect.height = inst->font_height;
2831 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
2836 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2838 if (cursor_val == -1) {
2839 #if GTK_CHECK_VERSION(2,16,0)
2840 cursor_val = GDK_BLANK_CURSOR;
2843 * Work around absence of GDK_BLANK_CURSOR by inventing a
2847 GdkColor bg = { 0, 0, 0, 0 };
2848 GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
2849 GdkGC *gc = gdk_gc_new(pm);
2850 gdk_gc_set_foreground(gc, &bg);
2851 gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
2853 ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
2854 gdk_pixmap_unref(pm);
2859 return gdk_cursor_new(cursor_val);
2862 void modalfatalbox(const char *p, ...)
2865 fprintf(stderr, "FATAL ERROR: ");
2867 vfprintf(stderr, p, ap);
2869 fputc('\n', stderr);
2873 void cmdline_error(const char *p, ...)
2876 fprintf(stderr, "%s: ", appname);
2878 vfprintf(stderr, p, ap);
2880 fputc('\n', stderr);
2884 char *get_x_display(void *frontend)
2886 return gdk_get_display();
2889 #ifndef NOT_X_WINDOWS
2890 long get_windowid(void *frontend)
2892 struct gui_data *inst = (struct gui_data *)frontend;
2893 return (long)GDK_WINDOW_XWINDOW(gtk_widget_get_window(inst->area));
2897 static void help(FILE *fp) {
2899 "pterm option summary:\n"
2901 " --display DISPLAY Specify X display to use (note '--')\n"
2902 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2903 " -fn FONT Normal text font\n"
2904 " -fb FONT Bold text font\n"
2905 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2906 " -sl LINES Number of lines of scrollback\n"
2907 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2908 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2909 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2910 " -T TITLE Window title\n"
2911 " -ut, +ut Do(default) or do not update utmp\n"
2912 " -ls, +ls Do(default) or do not make shell a login shell\n"
2913 " -sb, +sb Do(default) or do not display a scrollbar\n"
2914 " -log PATH Log all output to a file\n"
2915 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2916 " -xrm RESOURCE-STRING Set an X resource\n"
2917 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2918 ) < 0 || fflush(fp) < 0) {
2919 perror("output error");
2924 static void version(FILE *fp) {
2925 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
2926 perror("output error");
2931 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2932 struct gui_data *inst, Conf *conf)
2938 * Macros to make argument handling easier. Note that because
2939 * they need to call `continue', they cannot be contained in
2940 * the usual do {...} while (0) wrapper to make them
2941 * syntactically single statements; hence it is not legal to
2942 * use one of these macros as an unbraced statement between
2945 #define EXPECTS_ARG { \
2946 if (--argc <= 0) { \
2948 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2953 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2955 while (--argc > 0) {
2956 const char *p = *++argv;
2960 * Shameless cheating. Debian requires all X terminal
2961 * emulators to support `-T title'; but
2962 * cmdline_process_param will eat -T (it means no-pty) and
2963 * complain that pterm doesn't support it. So, in pterm
2964 * only, we convert -T into -title.
2966 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2970 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2971 do_everything ? 1 : -1, conf);
2974 cmdline_error("option \"%s\" requires an argument", p);
2975 } else if (ret == 2) {
2976 --argc, ++argv; /* skip next argument */
2978 } else if (ret == 1) {
2982 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2986 fs = fontspec_new(val);
2987 conf_set_fontspec(conf, CONF_font, fs);
2990 } else if (!strcmp(p, "-fb")) {
2994 fs = fontspec_new(val);
2995 conf_set_fontspec(conf, CONF_boldfont, fs);
2998 } else if (!strcmp(p, "-fw")) {
3002 fs = fontspec_new(val);
3003 conf_set_fontspec(conf, CONF_widefont, fs);
3006 } else if (!strcmp(p, "-fwb")) {
3010 fs = fontspec_new(val);
3011 conf_set_fontspec(conf, CONF_wideboldfont, fs);
3014 } else if (!strcmp(p, "-cs")) {
3017 conf_set_str(conf, CONF_line_codepage, val);
3019 } else if (!strcmp(p, "-geometry")) {
3023 #if GTK_CHECK_VERSION(2,0,0)
3024 inst->geometry = val;
3026 /* On GTK 1, we have to do this using raw Xlib */
3030 flags = XParseGeometry(val, &x, &y, &w, &h);
3031 if (flags & WidthValue)
3032 conf_set_int(conf, CONF_width, w);
3033 if (flags & HeightValue)
3034 conf_set_int(conf, CONF_height, h);
3036 if (flags & (XValue | YValue)) {
3039 inst->gotpos = TRUE;
3040 inst->gravity = ((flags & XNegative ? 1 : 0) |
3041 (flags & YNegative ? 2 : 0));
3046 } else if (!strcmp(p, "-sl")) {
3049 conf_set_int(conf, CONF_savelines, atoi(val));
3051 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
3052 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
3053 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
3058 if (!gdk_color_parse(val, &col)) {
3060 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
3064 index = (!strcmp(p, "-fg") ? 0 :
3065 !strcmp(p, "-bg") ? 2 :
3066 !strcmp(p, "-bfg") ? 1 :
3067 !strcmp(p, "-bbg") ? 3 :
3068 !strcmp(p, "-cfg") ? 4 :
3069 !strcmp(p, "-cbg") ? 5 : -1);
3070 assert(index != -1);
3071 conf_set_int_int(conf, CONF_colours, index*3+0, col.red / 256);
3072 conf_set_int_int(conf, CONF_colours, index*3+1,col.green/ 256);
3073 conf_set_int_int(conf, CONF_colours, index*3+2, col.blue/ 256);
3076 } else if (use_pty_argv && !strcmp(p, "-e")) {
3077 /* This option swallows all further arguments. */
3083 pty_argv = snewn(argc+1, char *);
3085 for (i = 0; i < argc; i++)
3086 pty_argv[i] = argv[i];
3087 pty_argv[argc] = NULL;
3088 break; /* finished command-line processing */
3090 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
3093 } else if (!strcmp(p, "-title")) {
3096 conf_set_str(conf, CONF_wintitle, val);
3098 } else if (!strcmp(p, "-log")) {
3102 fn = filename_from_str(val);
3103 conf_set_filename(conf, CONF_logfilename, fn);
3104 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
3107 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
3109 conf_set_int(conf, CONF_stamp_utmp, 0);
3111 } else if (!strcmp(p, "-ut")) {
3113 conf_set_int(conf, CONF_stamp_utmp, 1);
3115 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
3117 conf_set_int(conf, CONF_login_shell, 0);
3119 } else if (!strcmp(p, "-ls")) {
3121 conf_set_int(conf, CONF_login_shell, 1);
3123 } else if (!strcmp(p, "-nethack")) {
3125 conf_set_int(conf, CONF_nethack_keypad, 1);
3127 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
3129 conf_set_int(conf, CONF_scrollbar, 0);
3131 } else if (!strcmp(p, "-sb")) {
3133 conf_set_int(conf, CONF_scrollbar, 1);
3135 } else if (!strcmp(p, "-name")) {
3139 } else if (!strcmp(p, "-xrm")) {
3141 provide_xrm_string(val);
3143 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
3147 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
3151 } else if (!strcmp(p, "-pgpfp")) {
3155 } else if(p[0] != '-' && (!do_everything ||
3156 process_nonoption_arg(p, conf,
3162 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
3169 int uxsel_input_add(int fd, int rwx) {
3171 if (rwx & 1) flags |= GDK_INPUT_READ;
3172 if (rwx & 2) flags |= GDK_INPUT_WRITE;
3173 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
3175 return gdk_input_add(fd, flags, fd_input_func, NULL);
3178 void uxsel_input_remove(int id) {
3179 gdk_input_remove(id);
3182 char *setup_fonts_ucs(struct gui_data *inst)
3184 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
3185 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
3190 fs = conf_get_fontspec(inst->conf, CONF_font);
3191 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
3192 shadowboldoffset, shadowbold);
3194 return dupprintf("unable to load font \"%s\"", fs->name);
3197 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
3198 if (shadowbold || !fs->name[0]) {
3201 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
3202 shadowboldoffset, shadowbold);
3205 unifont_destroy(fonts[0]);
3206 return dupprintf("unable to load bold font \"%s\"", fs->name);
3210 fs = conf_get_fontspec(inst->conf, CONF_widefont);
3212 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
3213 shadowboldoffset, shadowbold);
3215 for (i = 0; i < 2; i++)
3217 unifont_destroy(fonts[i]);
3218 return dupprintf("unable to load wide font \"%s\"", fs->name);
3224 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
3225 if (shadowbold || !fs->name[0]) {
3228 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
3229 shadowboldoffset, shadowbold);
3231 for (i = 0; i < 3; i++)
3233 unifont_destroy(fonts[i]);
3234 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
3239 * Now we've got past all the possible error conditions, we can
3240 * actually update our state.
3243 for (i = 0; i < 4; i++) {
3245 unifont_destroy(inst->fonts[i]);
3246 inst->fonts[i] = fonts[i];
3249 inst->font_width = inst->fonts[0]->width;
3250 inst->font_height = inst->fonts[0]->height;
3252 inst->direct_to_font = init_ucs(&inst->ucsdata,
3253 conf_get_str(inst->conf, CONF_line_codepage),
3254 conf_get_int(inst->conf, CONF_utf8_override),
3255 inst->fonts[0]->public_charset,
3256 conf_get_int(inst->conf, CONF_vtmode));
3258 inst->drawtype = inst->fonts[0]->preferred_drawtype;
3263 void set_geom_hints(struct gui_data *inst)
3266 geom.min_width = inst->font_width + 2*inst->window_border;
3267 geom.min_height = inst->font_height + 2*inst->window_border;
3268 geom.max_width = geom.max_height = -1;
3269 geom.base_width = 2*inst->window_border;
3270 geom.base_height = 2*inst->window_border;
3271 geom.width_inc = inst->font_width;
3272 geom.height_inc = inst->font_height;
3273 geom.min_aspect = geom.max_aspect = 0;
3274 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
3275 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
3276 GDK_HINT_RESIZE_INC);
3279 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
3281 struct gui_data *inst = (struct gui_data *)data;
3282 term_clrsb(inst->term);
3285 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3287 struct gui_data *inst = (struct gui_data *)data;
3288 term_pwron(inst->term, TRUE);
3290 ldisc_echoedit_update(inst->ldisc);
3293 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3295 struct gui_data *inst = (struct gui_data *)data;
3296 term_copyall(inst->term);
3299 void special_menuitem(GtkMenuItem *item, gpointer data)
3301 struct gui_data *inst = (struct gui_data *)data;
3302 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
3306 inst->back->special(inst->backhandle, code);
3309 void about_menuitem(GtkMenuItem *item, gpointer data)
3311 struct gui_data *inst = (struct gui_data *)data;
3312 about_box(inst->window);
3315 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3317 struct gui_data *inst = (struct gui_data *)data;
3318 showeventlog(inst->eventlogstuff, inst->window);
3321 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3323 /* This maps colour indices in inst->conf to those used in inst->cols. */
3324 static const int ww[] = {
3325 256, 257, 258, 259, 260, 261,
3326 0, 8, 1, 9, 2, 10, 3, 11,
3327 4, 12, 5, 13, 6, 14, 7, 15
3329 struct gui_data *inst = (struct gui_data *)data;
3331 Conf *oldconf, *newconf;
3332 int i, j, need_size;
3334 assert(lenof(ww) == NCFGCOLOURS);
3336 if (inst->reconfiguring)
3339 inst->reconfiguring = TRUE;
3341 title = dupcat(appname, " Reconfiguration", NULL);
3343 oldconf = inst->conf;
3344 newconf = conf_copy(inst->conf);
3346 if (do_config_box(title, newconf, 1,
3347 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3348 inst->conf = newconf;
3350 /* Pass new config data to the logging module */
3351 log_reconfig(inst->logctx, inst->conf);
3353 * Flush the line discipline's edit buffer in the case
3354 * where local editing has just been disabled.
3357 ldisc_configure(inst->ldisc, inst->conf);
3358 ldisc_echoedit_update(inst->ldisc);
3360 /* Pass new config data to the terminal */
3361 term_reconfig(inst->term, inst->conf);
3362 /* Pass new config data to the back end */
3364 inst->back->reconfig(inst->backhandle, inst->conf);
3366 cache_conf_values(inst);
3369 * Just setting inst->conf is sufficient to cause colour
3370 * setting changes to appear on the next ESC]R palette
3371 * reset. But we should also check whether any colour
3372 * settings have been changed, and revert the ones that have
3373 * to the new default, on the assumption that the user is
3374 * most likely to want an immediate update.
3376 for (i = 0; i < NCFGCOLOURS; i++) {
3377 for (j = 0; j < 3; j++)
3378 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3379 conf_get_int_int(newconf, CONF_colours, i*3+j))
3382 real_palette_set(inst, ww[i],
3383 conf_get_int_int(newconf,CONF_colours,i*3+0),
3384 conf_get_int_int(newconf,CONF_colours,i*3+1),
3385 conf_get_int_int(newconf,CONF_colours,i*3+2));
3388 * If the default background has changed, we must
3389 * repaint the space in between the window border
3390 * and the text area.
3393 set_window_background(inst);
3394 draw_backing_rect(inst);
3400 * If the scrollbar needs to be shown, hidden, or moved
3401 * from one end to the other of the window, do so now.
3403 if (conf_get_int(oldconf, CONF_scrollbar) !=
3404 conf_get_int(newconf, CONF_scrollbar)) {
3405 if (conf_get_int(newconf, CONF_scrollbar))
3406 gtk_widget_show(inst->sbar);
3408 gtk_widget_hide(inst->sbar);
3410 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3411 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3412 gtk_box_reorder_child(inst->hbox, inst->sbar,
3413 conf_get_int(newconf, CONF_scrollbar_on_left)
3418 * Change the window title, if required.
3420 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3421 conf_get_str(newconf, CONF_wintitle)))
3422 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3423 set_window_titles(inst);
3426 * Redo the whole tangled fonts and Unicode mess if
3430 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3431 conf_get_fontspec(newconf, CONF_font)->name) ||
3432 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3433 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3434 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3435 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3436 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3437 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3438 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3439 conf_get_str(newconf, CONF_line_codepage)) ||
3440 conf_get_int(oldconf, CONF_utf8_override) !=
3441 conf_get_int(newconf, CONF_utf8_override) ||
3442 conf_get_int(oldconf, CONF_vtmode) !=
3443 conf_get_int(newconf, CONF_vtmode) ||
3444 conf_get_int(oldconf, CONF_shadowbold) !=
3445 conf_get_int(newconf, CONF_shadowbold) ||
3446 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3447 conf_get_int(newconf, CONF_shadowboldoffset)) {
3448 char *errmsg = setup_fonts_ucs(inst);
3451 dupprintf("Could not change fonts in terminal window: %s\n",
3453 messagebox(inst->window, "Font setup error", msgboxtext,
3454 string_width("Could not change fonts in terminal window:"),
3465 * Resize the window.
3467 if (conf_get_int(oldconf, CONF_width) !=
3468 conf_get_int(newconf, CONF_width) ||
3469 conf_get_int(oldconf, CONF_height) !=
3470 conf_get_int(newconf, CONF_height) ||
3471 conf_get_int(oldconf, CONF_window_border) !=
3472 conf_get_int(newconf, CONF_window_border) ||
3474 set_geom_hints(inst);
3475 request_resize(inst, conf_get_int(newconf, CONF_width),
3476 conf_get_int(newconf, CONF_height));
3479 * The above will have caused a call to term_size() for
3480 * us if it happened. If the user has fiddled with only
3481 * the scrollback size, the above will not have
3482 * happened and we will need an explicit term_size()
3485 if (conf_get_int(oldconf, CONF_savelines) !=
3486 conf_get_int(newconf, CONF_savelines))
3487 term_size(inst->term, inst->term->rows, inst->term->cols,
3488 conf_get_int(newconf, CONF_savelines));
3491 term_invalidate(inst->term);
3494 * We do an explicit full redraw here to ensure the window
3495 * border has been redrawn as well as the text area.
3497 gtk_widget_queue_draw(inst->area);
3504 inst->reconfiguring = FALSE;
3507 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3510 * Re-execing ourself is not an exact science under Unix. I do
3511 * the best I can by using /proc/self/exe if available and by
3512 * assuming argv[0] can be found on $PATH if not.
3514 * Note that we also have to reconstruct the elements of the
3515 * original argv which gtk swallowed, since the user wants the
3516 * new session to appear on the same X display as the old one.
3524 * Collect the arguments with which to re-exec ourself.
3526 va_start(ap, fd_to_close);
3527 n = 2; /* progname and terminating NULL */
3528 n += inst->ngtkargs;
3529 while (va_arg(ap, char *) != NULL)
3533 args = snewn(n, char *);
3534 args[0] = inst->progname;
3536 for (i = 0; i < inst->ngtkargs; i++)
3537 args[i+1] = inst->gtkargvstart[i];
3540 va_start(ap, fd_to_close);
3541 while ((args[i++] = va_arg(ap, char *)) != NULL);
3547 * Do the double fork.
3561 } else if (pid2 > 0) {
3563 * First child has successfully forked second child. My
3564 * Work Here Is Done. Note the use of _exit rather than
3565 * exit: the latter appears to cause destroy messages
3566 * to be sent to the X server. I suspect gtk uses
3573 * If we reach here, we are the second child, so we now
3574 * actually perform the exec.
3576 if (fd_to_close >= 0)
3579 execv("/proc/self/exe", args);
3580 execvp(inst->progname, args);
3587 waitpid(pid, &status, 0);
3592 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3594 struct gui_data *inst = (struct gui_data *)gdata;
3596 * For this feature we must marshal conf and (possibly) pty_argv
3597 * into a byte stream, create a pipe, and send this byte stream
3598 * to the child through the pipe.
3600 int i, ret, sersize, size;
3605 if (pipe(pipefd) < 0) {
3610 size = sersize = conf_serialised_size(inst->conf);
3611 if (use_pty_argv && pty_argv) {
3612 for (i = 0; pty_argv[i]; i++)
3613 size += strlen(pty_argv[i]) + 1;
3616 data = snewn(size, char);
3617 conf_serialise(inst->conf, data);
3618 if (use_pty_argv && pty_argv) {
3620 for (i = 0; pty_argv[i]; i++) {
3621 strcpy(data + p, pty_argv[i]);
3622 p += strlen(pty_argv[i]) + 1;
3627 sprintf(option, "---[%d,%d]", pipefd[0], size);
3628 noncloexec(pipefd[0]);
3629 fork_and_exec_self(inst, pipefd[1], option, NULL);
3633 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3636 perror("write to pipe");
3641 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
3643 int fd, i, ret, size, size_used;
3646 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3647 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3651 data = snewn(size, char);
3653 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3656 perror("read from pipe");
3658 } else if (i < size) {
3659 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3664 size_used = conf_deserialise(conf, data, size);
3665 if (use_pty_argv && size > size_used) {
3669 while (i < size && data[i]) i++;
3671 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3678 pty_argv = snewn(n+1, char *);
3684 while (i < size && data[i]) i++;
3687 pty_argv[n++] = dupstr(p);
3696 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3698 struct gui_data *inst = (struct gui_data *)data;
3700 fork_and_exec_self(inst, -1, NULL);
3703 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3705 struct gui_data *inst = (struct gui_data *)data;
3708 logevent(inst, "----- Session restarted -----");
3709 term_pwron(inst->term, FALSE);
3710 start_backend(inst);
3711 inst->exited = FALSE;
3715 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3717 struct gui_data *inst = (struct gui_data *)data;
3718 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3720 fork_and_exec_self(inst, -1, "-load", str, NULL);
3723 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3725 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3730 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3732 struct gui_data *inst = (struct gui_data *)data;
3733 struct sesslist sesslist;
3736 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3737 (GtkCallback)gtk_widget_destroy, NULL);
3739 get_sesslist(&sesslist, TRUE);
3740 /* skip sesslist.sessions[0] == Default Settings */
3741 for (i = 1; i < sesslist.nsessions; i++) {
3742 GtkWidget *menuitem =
3743 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3744 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3745 gtk_widget_show(menuitem);
3746 g_object_set_data(G_OBJECT(menuitem), "user-data",
3747 dupstr(sesslist.sessions[i]));
3748 g_signal_connect(G_OBJECT(menuitem), "activate",
3749 G_CALLBACK(saved_session_menuitem),
3751 g_signal_connect(G_OBJECT(menuitem), "destroy",
3752 G_CALLBACK(saved_session_freedata),
3755 if (sesslist.nsessions <= 1) {
3756 GtkWidget *menuitem =
3757 gtk_menu_item_new_with_label("(No sessions)");
3758 gtk_widget_set_sensitive(menuitem, FALSE);
3759 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3760 gtk_widget_show(menuitem);
3762 get_sesslist(&sesslist, FALSE); /* free up */
3765 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3769 GdkBitmap *iconmask;
3770 #if GTK_CHECK_VERSION(2,0,0)
3778 gtk_widget_realize(window);
3779 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
3780 &iconmask, NULL, (gchar **)icon[0]);
3781 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
3783 #if GTK_CHECK_VERSION(2,0,0)
3785 for (n = 0; n < n_icon; n++) {
3787 g_list_append(iconlist,
3788 gdk_pixbuf_new_from_xpm_data((const gchar **)
3791 gdk_window_set_icon_list(gtk_widget_get_window(window), iconlist);
3795 void update_specials_menu(void *frontend)
3797 struct gui_data *inst = (struct gui_data *)frontend;
3799 const struct telnet_special *specials;
3802 specials = inst->back->get_specials(inst->backhandle);
3806 /* I believe this disposes of submenus too. */
3807 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3808 (GtkCallback)gtk_widget_destroy, NULL);
3811 GtkWidget *menu = inst->specialsmenu;
3812 /* A lame "stack" for submenus that will do for now. */
3813 GtkWidget *saved_menu = NULL;
3815 for (i = 0; nesting > 0; i++) {
3816 GtkWidget *menuitem = NULL;
3817 switch (specials[i].code) {
3819 assert (nesting < 2);
3820 saved_menu = menu; /* XXX lame stacking */
3821 menu = gtk_menu_new();
3822 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3823 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3824 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3825 gtk_widget_show(menuitem);
3832 menu = saved_menu; /* XXX lame stacking */
3837 menuitem = gtk_menu_item_new();
3840 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3841 g_object_set_data(G_OBJECT(menuitem), "user-data",
3842 GINT_TO_POINTER(specials[i].code));
3843 g_signal_connect(G_OBJECT(menuitem), "activate",
3844 G_CALLBACK(special_menuitem), inst);
3848 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3849 gtk_widget_show(menuitem);
3852 gtk_widget_show(inst->specialsitem1);
3853 gtk_widget_show(inst->specialsitem2);
3855 gtk_widget_hide(inst->specialsitem1);
3856 gtk_widget_hide(inst->specialsitem2);
3860 static void start_backend(struct gui_data *inst)
3862 extern Backend *select_backend(Conf *conf);
3867 inst->back = select_backend(inst->conf);
3869 error = inst->back->init((void *)inst, &inst->backhandle,
3871 conf_get_str(inst->conf, CONF_host),
3872 conf_get_int(inst->conf, CONF_port),
3874 conf_get_int(inst->conf, CONF_tcp_nodelay),
3875 conf_get_int(inst->conf, CONF_tcp_keepalives));
3878 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3879 conf_get_str(inst->conf, CONF_host), error);
3880 inst->exited = TRUE;
3881 fatal_message_box(inst->window, msg);
3886 s = conf_get_str(inst->conf, CONF_wintitle);
3888 set_title_and_icon(inst, s, s);
3890 char *title = make_default_wintitle(realhost);
3891 set_title_and_icon(inst, title, title);
3896 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3898 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3901 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
3904 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3907 int pt_main(int argc, char **argv)
3909 extern int cfgbox(Conf *conf);
3910 struct gui_data *inst;
3912 setlocale(LC_CTYPE, "");
3915 * Create an instance structure and initialise to zeroes
3917 inst = snew(struct gui_data);
3918 memset(inst, 0, sizeof(*inst));
3919 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3920 inst->busy_status = BUSY_NOT;
3921 inst->conf = conf_new();
3922 inst->wintitle = inst->icontitle = NULL;
3923 inst->quit_fn_scheduled = FALSE;
3924 inst->idle_fn_scheduled = FALSE;
3925 inst->drawtype = DRAWTYPE_DEFAULT;
3927 /* defer any child exit handling until we're ready to deal with
3929 block_signal(SIGCHLD, 1);
3931 inst->progname = argv[0];
3933 * Copy the original argv before letting gtk_init fiddle with
3934 * it. It will be required later.
3938 inst->gtkargvstart = snewn(argc-1, char *);
3939 for (i = 1; i < argc; i++)
3940 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3942 gtk_init(&argc, &argv);
3943 inst->ngtkargs = oldargc - argc;
3946 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3947 read_dupsession_data(inst, inst->conf, argv[1]);
3948 /* Splatter this argument so it doesn't clutter a ps listing */
3949 smemclr(argv[1], strlen(argv[1]));
3951 /* By default, we bring up the config dialog, rather than launching
3952 * a session. This gets set to TRUE if something happens to change
3953 * that (e.g., a hostname is specified on the command-line). */
3954 int allow_launch = FALSE;
3955 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
3956 exit(1); /* pre-defaults pass to get -class */
3957 do_defaults(NULL, inst->conf);
3958 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
3959 exit(1); /* post-defaults, do everything */
3961 cmdline_run_saved(inst->conf);
3964 allow_launch = TRUE;
3966 if ((!allow_launch || !conf_launchable(inst->conf)) &&
3967 !cfgbox(inst->conf))
3968 exit(0); /* config box hit Cancel */
3971 if (!compound_text_atom)
3972 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3973 if (!utf8_string_atom)
3974 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3976 inst->area = gtk_drawing_area_new();
3978 #if GTK_CHECK_VERSION(2,0,0)
3979 inst->imc = gtk_im_multicontext_new();
3983 char *errmsg = setup_fonts_ucs(inst);
3985 fprintf(stderr, "%s: %s\n", appname, errmsg);
3991 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3993 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
3995 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
3996 winclass, winclass);
4000 * Set up the colour map.
4002 palette_reset(inst);
4004 inst->width = conf_get_int(inst->conf, CONF_width);
4005 inst->height = conf_get_int(inst->conf, CONF_height);
4006 cache_conf_values(inst);
4009 int w = inst->font_width * inst->width + 2*inst->window_border;
4010 int h = inst->font_height * inst->height + 2*inst->window_border;
4011 #if GTK_CHECK_VERSION(2,0,0)
4012 gtk_widget_set_size_request(inst->area, w, h);
4014 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
4017 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
4018 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
4019 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
4021 * We always create the scrollbar; it remains invisible if
4022 * unwanted, so we can pop it up quickly if it suddenly becomes
4025 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
4026 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4027 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
4028 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
4029 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4031 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
4033 set_geom_hints(inst);
4035 gtk_widget_show(inst->area);
4036 if (conf_get_int(inst->conf, CONF_scrollbar))
4037 gtk_widget_show(inst->sbar);
4039 gtk_widget_hide(inst->sbar);
4040 gtk_widget_show(GTK_WIDGET(inst->hbox));
4042 #if GTK_CHECK_VERSION(2,0,0)
4043 if (inst->geometry) {
4044 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
4048 int x = inst->xpos, y = inst->ypos;
4050 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
4051 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
4052 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
4053 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
4054 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
4058 g_signal_connect(G_OBJECT(inst->window), "destroy",
4059 G_CALLBACK(destroy), inst);
4060 g_signal_connect(G_OBJECT(inst->window), "delete_event",
4061 G_CALLBACK(delete_window), inst);
4062 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
4063 G_CALLBACK(key_event), inst);
4064 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
4065 G_CALLBACK(key_event), inst);
4066 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
4067 G_CALLBACK(focus_event), inst);
4068 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
4069 G_CALLBACK(focus_event), inst);
4070 g_signal_connect(G_OBJECT(inst->area), "configure_event",
4071 G_CALLBACK(configure_area), inst);
4072 g_signal_connect(G_OBJECT(inst->area), "expose_event",
4073 G_CALLBACK(expose_area), inst);
4074 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
4075 G_CALLBACK(button_event), inst);
4076 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
4077 G_CALLBACK(button_event), inst);
4078 #if GTK_CHECK_VERSION(2,0,0)
4079 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
4080 G_CALLBACK(scroll_event), inst);
4082 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
4083 G_CALLBACK(motion_event), inst);
4084 g_signal_connect(G_OBJECT(inst->area), "selection_received",
4085 G_CALLBACK(selection_received), inst);
4086 g_signal_connect(G_OBJECT(inst->area), "selection_get",
4087 G_CALLBACK(selection_get), inst);
4088 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
4089 G_CALLBACK(selection_clear), inst);
4090 #if GTK_CHECK_VERSION(2,0,0)
4091 g_signal_connect(G_OBJECT(inst->imc), "commit",
4092 G_CALLBACK(input_method_commit_event), inst);
4094 if (conf_get_int(inst->conf, CONF_scrollbar))
4095 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
4096 G_CALLBACK(scrollbar_moved), inst);
4097 gtk_widget_add_events(GTK_WIDGET(inst->area),
4098 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
4099 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
4100 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
4103 extern const char *const *const main_icon[];
4104 extern const int n_main_icon;
4105 set_window_icon(inst->window, main_icon, n_main_icon);
4108 gtk_widget_show(inst->window);
4110 set_window_background(inst);
4113 * Set up the Ctrl+rightclick context menu.
4116 GtkWidget *menuitem;
4118 extern const int use_event_log, new_session, saved_sessions;
4120 inst->menu = gtk_menu_new();
4122 #define MKMENUITEM(title, func) do \
4124 menuitem = gtk_menu_item_new_with_label(title); \
4125 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4126 gtk_widget_show(menuitem); \
4127 g_signal_connect(G_OBJECT(menuitem), "activate", \
4128 G_CALLBACK(func), inst); \
4131 #define MKSUBMENU(title) do \
4133 menuitem = gtk_menu_item_new_with_label(title); \
4134 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4135 gtk_widget_show(menuitem); \
4138 #define MKSEP() do \
4140 menuitem = gtk_menu_item_new(); \
4141 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4142 gtk_widget_show(menuitem); \
4146 MKMENUITEM("New Session...", new_session_menuitem);
4147 MKMENUITEM("Restart Session", restart_session_menuitem);
4148 inst->restartitem = menuitem;
4149 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4150 MKMENUITEM("Duplicate Session", dup_session_menuitem);
4151 if (saved_sessions) {
4152 inst->sessionsmenu = gtk_menu_new();
4153 /* sessionsmenu will be updated when it's invoked */
4154 /* XXX is this the right way to do dynamic menus in Gtk? */
4155 MKMENUITEM("Saved Sessions", update_savedsess_menu);
4156 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
4157 inst->sessionsmenu);
4160 MKMENUITEM("Change Settings...", change_settings_menuitem);
4163 MKMENUITEM("Event Log", event_log_menuitem);
4164 MKSUBMENU("Special Commands");
4165 inst->specialsmenu = gtk_menu_new();
4166 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
4167 inst->specialsitem1 = menuitem;
4169 inst->specialsitem2 = menuitem;
4170 gtk_widget_hide(inst->specialsitem1);
4171 gtk_widget_hide(inst->specialsitem2);
4172 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
4173 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
4174 MKMENUITEM("Copy All", copy_all_menuitem);
4176 s = dupcat("About ", appname, NULL);
4177 MKMENUITEM(s, about_menuitem);
4184 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
4185 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
4186 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
4187 inst->blankcursor = make_mouse_ptr(inst, -1);
4188 inst->currcursor = inst->textcursor;
4189 show_mouseptr(inst, 1);
4191 inst->eventlogstuff = eventlogstuff_new();
4193 request_callback_notifications(notify_toplevel_callback, inst);
4195 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
4196 inst->logctx = log_init(inst, inst->conf);
4197 term_provide_logctx(inst->term, inst->logctx);
4201 term_size(inst->term, inst->height, inst->width,
4202 conf_get_int(inst->conf, CONF_savelines));
4204 start_backend(inst);
4206 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
4208 /* now we're reday to deal with the child exit handler being
4210 block_signal(SIGCHLD, 0);
4213 * Block SIGPIPE: if we attempt Duplicate Session or similar
4214 * and it falls over in some way, we certainly don't want
4215 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
4216 * this _after_ (at least pterm) forks off its child process,
4217 * since the child wants SIGPIPE handled in the usual way.
4219 block_signal(SIGPIPE, 1);
4221 inst->exited = FALSE;