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 */
131 static void cache_conf_values(struct gui_data *inst)
133 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
134 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
135 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
140 struct gui_data *inst;
143 static int send_raw_mouse;
145 static const char *app_name = "pterm";
147 static void start_backend(struct gui_data *inst);
148 static void exit_callback(void *vinst);
150 char *x_get_default(const char *key)
152 #ifndef NOT_X_WINDOWS
153 return XGetDefault(GDK_DISPLAY(), app_name, key);
159 void connection_fatal(void *frontend, const char *p, ...)
161 struct gui_data *inst = (struct gui_data *)frontend;
166 msg = dupvprintf(p, ap);
168 fatal_message_box(inst->window, msg);
171 queue_toplevel_callback(exit_callback, inst);
175 * Default settings that are specific to pterm.
177 FontSpec *platform_default_fontspec(const char *name)
179 if (!strcmp(name, "Font"))
180 return fontspec_new("server:fixed");
182 return fontspec_new("");
185 Filename *platform_default_filename(const char *name)
187 if (!strcmp(name, "LogFileName"))
188 return filename_from_str("putty.log");
190 return filename_from_str("");
193 char *platform_default_s(const char *name)
195 if (!strcmp(name, "SerialLine"))
196 return dupstr("/dev/ttyS0");
200 int platform_default_i(const char *name, int def)
202 if (!strcmp(name, "CloseOnExit"))
203 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
204 if (!strcmp(name, "WinNameAlways"))
205 return 0; /* X natively supports icon titles, so use 'em by default */
209 /* Dummy routine, only required in plink. */
210 void frontend_echoedit_update(void *frontend, int echo, int edit)
214 char *get_ttymode(void *frontend, const char *mode)
216 struct gui_data *inst = (struct gui_data *)frontend;
217 return term_get_ttymode(inst->term, mode);
220 int from_backend(void *frontend, int is_stderr, const char *data, int len)
222 struct gui_data *inst = (struct gui_data *)frontend;
223 return term_data(inst->term, is_stderr, data, len);
226 int from_backend_untrusted(void *frontend, const char *data, int len)
228 struct gui_data *inst = (struct gui_data *)frontend;
229 return term_data_untrusted(inst->term, data, len);
232 int from_backend_eof(void *frontend)
234 return TRUE; /* do respond to incoming EOF with outgoing */
237 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
239 struct gui_data *inst = (struct gui_data *)p->frontend;
241 ret = cmdline_get_passwd_input(p, in, inlen);
243 ret = term_get_userpass_input(inst->term, p, in, inlen);
247 void logevent(void *frontend, const char *string)
249 struct gui_data *inst = (struct gui_data *)frontend;
251 log_eventlog(inst->logctx, string);
253 logevent_dlg(inst->eventlogstuff, string);
256 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
258 struct gui_data *inst = (struct gui_data *)frontend;
261 return inst->font_height;
263 return inst->font_width;
267 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
268 * into a cooked one (SELECT, EXTEND, PASTE).
270 * In Unix, this is not configurable; the X button arrangement is
271 * rock-solid across all applications, everyone has a three-button
272 * mouse or a means of faking it, and there is no need to switch
273 * buttons around at all.
275 static Mouse_Button translate_button(Mouse_Button button)
277 /* struct gui_data *inst = (struct gui_data *)frontend; */
279 if (button == MBT_LEFT)
281 if (button == MBT_MIDDLE)
283 if (button == MBT_RIGHT)
285 return 0; /* shouldn't happen */
289 * Return the top-level GtkWindow associated with a particular
290 * front end instance.
292 void *get_window(void *frontend)
294 struct gui_data *inst = (struct gui_data *)frontend;
299 * Minimise or restore the window in response to a server-side
302 void set_iconic(void *frontend, int iconic)
305 * GTK 1.2 doesn't know how to do this.
307 #if GTK_CHECK_VERSION(2,0,0)
308 struct gui_data *inst = (struct gui_data *)frontend;
310 gtk_window_iconify(GTK_WINDOW(inst->window));
312 gtk_window_deiconify(GTK_WINDOW(inst->window));
317 * Move the window in response to a server-side request.
319 void move_window(void *frontend, int x, int y)
321 struct gui_data *inst = (struct gui_data *)frontend;
323 * I assume that when the GTK version of this call is available
324 * we should use it. Not sure how it differs from the GDK one,
327 #if GTK_CHECK_VERSION(2,0,0)
328 gtk_window_move(GTK_WINDOW(inst->window), x, y);
330 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
335 * Move the window to the top or bottom of the z-order in response
336 * to a server-side request.
338 void set_zorder(void *frontend, int top)
340 struct gui_data *inst = (struct gui_data *)frontend;
342 gdk_window_raise(gtk_widget_get_window(inst->window));
344 gdk_window_lower(gtk_widget_get_window(inst->window));
348 * Refresh the window in response to a server-side request.
350 void refresh_window(void *frontend)
352 struct gui_data *inst = (struct gui_data *)frontend;
353 term_invalidate(inst->term);
357 * Maximise or restore the window in response to a server-side
360 void set_zoomed(void *frontend, int zoomed)
363 * GTK 1.2 doesn't know how to do this.
365 #if GTK_CHECK_VERSION(2,0,0)
366 struct gui_data *inst = (struct gui_data *)frontend;
368 gtk_window_maximize(GTK_WINDOW(inst->window));
370 gtk_window_unmaximize(GTK_WINDOW(inst->window));
375 * Report whether the window is iconic, for terminal reports.
377 int is_iconic(void *frontend)
379 struct gui_data *inst = (struct gui_data *)frontend;
380 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
384 * Report the window's position, for terminal reports.
386 void get_window_pos(void *frontend, int *x, int *y)
388 struct gui_data *inst = (struct gui_data *)frontend;
390 * I assume that when the GTK version of this call is available
391 * we should use it. Not sure how it differs from the GDK one,
394 #if GTK_CHECK_VERSION(2,0,0)
395 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
397 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
402 * Report the window's pixel size, for terminal reports.
404 void get_window_pixels(void *frontend, int *x, int *y)
406 struct gui_data *inst = (struct gui_data *)frontend;
408 * I assume that when the GTK version of this call is available
409 * we should use it. Not sure how it differs from the GDK one,
412 #if GTK_CHECK_VERSION(2,0,0)
413 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
415 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
420 * Return the window or icon title.
422 char *get_window_title(void *frontend, int icon)
424 struct gui_data *inst = (struct gui_data *)frontend;
425 return icon ? inst->icontitle : inst->wintitle;
428 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
430 struct gui_data *inst = (struct gui_data *)data;
431 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
432 if (!reallyclose(inst))
438 static void update_mouseptr(struct gui_data *inst)
440 switch (inst->busy_status) {
442 if (!inst->mouseptr_visible) {
443 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
445 } else if (send_raw_mouse) {
446 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
449 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
453 case BUSY_WAITING: /* XXX can we do better? */
455 /* We always display these cursors. */
456 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
464 static void show_mouseptr(struct gui_data *inst, int show)
466 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
468 inst->mouseptr_visible = show;
469 update_mouseptr(inst);
472 void draw_backing_rect(struct gui_data *inst)
474 GdkGC *gc = gdk_gc_new(gtk_widget_get_window(inst->area));
475 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
476 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
477 inst->width * inst->font_width + 2*inst->window_border,
478 inst->height * inst->font_height + 2*inst->window_border);
482 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
484 struct gui_data *inst = (struct gui_data *)data;
485 int w, h, need_size = 0;
488 * See if the terminal size has changed, in which case we must
489 * let the terminal know.
491 w = (event->width - 2*inst->window_border) / inst->font_width;
492 h = (event->height - 2*inst->window_border) / inst->font_height;
493 if (w != inst->width || h != inst->height) {
496 conf_set_int(inst->conf, CONF_width, inst->width);
497 conf_set_int(inst->conf, CONF_height, inst->height);
502 gdk_pixmap_unref(inst->pixmap);
506 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
507 (w * inst->font_width + 2*inst->window_border),
508 (h * inst->font_height + 2*inst->window_border), -1);
510 draw_backing_rect(inst);
512 if (need_size && inst->term) {
513 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
517 term_invalidate(inst->term);
519 #if GTK_CHECK_VERSION(2,0,0)
520 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
526 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
528 struct gui_data *inst = (struct gui_data *)data;
531 * Pass the exposed rectangle to terminal.c, which will call us
532 * back to do the actual painting.
535 gdk_draw_pixmap(gtk_widget_get_window(widget),
536 (gtk_widget_get_style(widget)->fg_gc
537 [gtk_widget_get_state(widget)]),
539 event->area.x, event->area.y,
540 event->area.x, event->area.y,
541 event->area.width, event->area.height);
546 #define KEY_PRESSED(k) \
547 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
549 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
551 struct gui_data *inst = (struct gui_data *)data;
553 wchar_t ucsoutput[2];
554 int ucsval, start, end, special, output_charset, use_ucsoutput;
555 int nethack_mode, app_keypad_mode;
557 /* Remember the timestamp. */
558 inst->input_event_time = event->time;
560 /* By default, nothing is generated. */
562 special = use_ucsoutput = FALSE;
563 output_charset = CS_ISO8859_1;
566 * If Alt is being released after typing an Alt+numberpad
567 * sequence, we should generate the code that was typed.
569 * Note that we only do this if more than one key was actually
570 * pressed - I don't think Alt+NumPad4 should be ^D or that
571 * Alt+NumPad3 should be ^C, for example. There's no serious
572 * inconvenience in having to type a zero before a single-digit
575 if (event->type == GDK_KEY_RELEASE) {
576 if ((event->keyval == GDK_KEY_Meta_L ||
577 event->keyval == GDK_KEY_Meta_R ||
578 event->keyval == GDK_KEY_Alt_L ||
579 event->keyval == GDK_KEY_Alt_R) &&
580 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
582 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
585 * FIXME: we might usefully try to do something clever here
586 * about interpreting the generated key code in a way that's
587 * appropriate to the line code page.
589 output[0] = inst->alt_keycode;
593 #if GTK_CHECK_VERSION(2,0,0)
594 if (gtk_im_context_filter_keypress(inst->imc, event))
599 if (event->type == GDK_KEY_PRESS) {
603 printf("keypress: keyval = %04x, state = %08x; string =",
604 event->keyval, event->state);
605 for (i = 0; event->string[i]; i++)
606 printf(" %02x", (unsigned char) event->string[i]);
612 * NYI: Compose key (!!! requires Unicode faff before even trying)
616 * If Alt has just been pressed, we start potentially
617 * accumulating an Alt+numberpad code. We do this by
618 * setting alt_keycode to -1 (nothing yet but plausible).
620 if ((event->keyval == GDK_KEY_Meta_L ||
621 event->keyval == GDK_KEY_Meta_R ||
622 event->keyval == GDK_KEY_Alt_L ||
623 event->keyval == GDK_KEY_Alt_R)) {
624 inst->alt_keycode = -1;
625 inst->alt_digits = 0;
626 goto done; /* this generates nothing else */
630 * If we're seeing a numberpad key press with Mod1 down,
631 * consider adding it to alt_keycode if that's sensible.
632 * Anything _else_ with Mod1 down cancels any possibility
633 * of an ALT keycode: we set alt_keycode to -2.
635 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
637 switch (event->keyval) {
638 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
639 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
640 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
641 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
642 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
643 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
644 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
645 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
646 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
647 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
650 inst->alt_keycode = -2; /* it's invalid */
653 printf("Adding digit %d to keycode %d", digit,
656 if (inst->alt_keycode == -1)
657 inst->alt_keycode = digit; /* one-digit code */
659 inst->alt_keycode = inst->alt_keycode * 10 + digit;
662 printf(" gives new code %d\n", inst->alt_keycode);
664 /* Having used this digit, we now do nothing more with it. */
670 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
673 if (event->keyval == GDK_KEY_Page_Up &&
674 (event->state & GDK_SHIFT_MASK)) {
675 term_scroll(inst->term, 0, -inst->height/2);
678 if (event->keyval == GDK_KEY_Page_Up &&
679 (event->state & GDK_CONTROL_MASK)) {
680 term_scroll(inst->term, 0, -1);
683 if (event->keyval == GDK_KEY_Page_Down &&
684 (event->state & GDK_SHIFT_MASK)) {
685 term_scroll(inst->term, 0, +inst->height/2);
688 if (event->keyval == GDK_KEY_Page_Down &&
689 (event->state & GDK_CONTROL_MASK)) {
690 term_scroll(inst->term, 0, +1);
695 * Neither does Shift-Ins.
697 if (event->keyval == GDK_KEY_Insert &&
698 (event->state & GDK_SHIFT_MASK)) {
704 use_ucsoutput = FALSE;
706 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
707 app_keypad_mode = (inst->term->app_keypad_keys &&
708 !conf_get_int(inst->conf, CONF_no_applic_k));
710 /* ALT+things gives leading Escape. */
712 #if !GTK_CHECK_VERSION(2,0,0)
714 * In vanilla X, and hence also GDK 1.2, the string received
715 * as part of a keyboard event is assumed to be in
716 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
717 * but it's true: see the man page for XLookupString(3) for
720 output_charset = CS_ISO8859_1;
721 strncpy(output+1, event->string, lenof(output)-1);
724 * Most things can now be passed to
725 * gtk_im_context_filter_keypress without breaking anything
726 * below this point. An exception is the numeric keypad if
727 * we're in Nethack or application mode: the IM will eat
728 * numeric keypad presses if Num Lock is on, but we don't want
731 if (app_keypad_mode &&
732 (event->keyval == GDK_KEY_Num_Lock ||
733 event->keyval == GDK_KEY_KP_Divide ||
734 event->keyval == GDK_KEY_KP_Multiply ||
735 event->keyval == GDK_KEY_KP_Subtract ||
736 event->keyval == GDK_KEY_KP_Add ||
737 event->keyval == GDK_KEY_KP_Enter ||
738 event->keyval == GDK_KEY_KP_0 ||
739 event->keyval == GDK_KEY_KP_Insert ||
740 event->keyval == GDK_KEY_KP_1 ||
741 event->keyval == GDK_KEY_KP_End ||
742 event->keyval == GDK_KEY_KP_2 ||
743 event->keyval == GDK_KEY_KP_Down ||
744 event->keyval == GDK_KEY_KP_3 ||
745 event->keyval == GDK_KEY_KP_Page_Down ||
746 event->keyval == GDK_KEY_KP_4 ||
747 event->keyval == GDK_KEY_KP_Left ||
748 event->keyval == GDK_KEY_KP_5 ||
749 event->keyval == GDK_KEY_KP_Begin ||
750 event->keyval == GDK_KEY_KP_6 ||
751 event->keyval == GDK_KEY_KP_Right ||
752 event->keyval == GDK_KEY_KP_7 ||
753 event->keyval == GDK_KEY_KP_Home ||
754 event->keyval == GDK_KEY_KP_8 ||
755 event->keyval == GDK_KEY_KP_Up ||
756 event->keyval == GDK_KEY_KP_9 ||
757 event->keyval == GDK_KEY_KP_Page_Up ||
758 event->keyval == GDK_KEY_KP_Decimal ||
759 event->keyval == GDK_KEY_KP_Delete)) {
760 /* app keypad; do nothing */
761 } else if (nethack_mode &&
762 (event->keyval == GDK_KEY_KP_1 ||
763 event->keyval == GDK_KEY_KP_End ||
764 event->keyval == GDK_KEY_KP_2 ||
765 event->keyval == GDK_KEY_KP_Down ||
766 event->keyval == GDK_KEY_KP_3 ||
767 event->keyval == GDK_KEY_KP_Page_Down ||
768 event->keyval == GDK_KEY_KP_4 ||
769 event->keyval == GDK_KEY_KP_Left ||
770 event->keyval == GDK_KEY_KP_5 ||
771 event->keyval == GDK_KEY_KP_Begin ||
772 event->keyval == GDK_KEY_KP_6 ||
773 event->keyval == GDK_KEY_KP_Right ||
774 event->keyval == GDK_KEY_KP_7 ||
775 event->keyval == GDK_KEY_KP_Home ||
776 event->keyval == GDK_KEY_KP_8 ||
777 event->keyval == GDK_KEY_KP_Up ||
778 event->keyval == GDK_KEY_KP_9 ||
779 event->keyval == GDK_KEY_KP_Page_Up)) {
780 /* nethack mode; do nothing */
782 if (gtk_im_context_filter_keypress(inst->imc, event))
787 * GDK 2.0 arranges to have done some translation for us: in
788 * GDK 2.0, event->string is encoded in the current locale.
790 * So we use the standard C library function mbstowcs() to
791 * convert from the current locale into Unicode; from there
792 * we can convert to whatever PuTTY is currently working in.
793 * (In fact I convert straight back to UTF-8 from
794 * wide-character Unicode, for the sake of simplicity: that
795 * way we can still use exactly the same code to manipulate
796 * the string, such as prefixing ESC.)
798 output_charset = CS_UTF8;
800 wchar_t widedata[32];
805 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
806 event->string, strlen(event->string),
807 widedata, lenof(widedata)-1);
810 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
811 CS_UTF8, NULL, NULL, 0);
812 output[1+ulen] = '\0';
817 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
818 ucsoutput[0] = '\033';
819 ucsoutput[1] = ucsval;
820 use_ucsoutput = TRUE;
823 output[lenof(output)-1] = '\0';
824 end = strlen(output);
826 if (event->state & GDK_MOD1_MASK) {
828 if (end == 1) end = 0;
832 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
833 if (!output[1] && event->keyval == '`' &&
834 (event->state & GDK_CONTROL_MASK)) {
836 use_ucsoutput = FALSE;
840 /* Control-Break sends a Break special to the backend */
841 if (event->keyval == GDK_KEY_Break &&
842 (event->state & GDK_CONTROL_MASK)) {
844 inst->back->special(inst->backhandle, TS_BRK);
848 /* We handle Return ourselves, because it needs to be flagged as
849 * special to ldisc. */
850 if (event->keyval == GDK_KEY_Return) {
852 use_ucsoutput = FALSE;
857 /* Control-2, Control-Space and Control-@ are NUL */
859 (event->keyval == ' ' || event->keyval == '2' ||
860 event->keyval == '@') &&
861 (event->state & (GDK_SHIFT_MASK |
862 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
864 use_ucsoutput = FALSE;
868 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
869 if (!output[1] && event->keyval == ' ' &&
870 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
871 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
873 output_charset = CS_ISO8859_1;
874 use_ucsoutput = FALSE;
878 /* We don't let GTK tell us what Backspace is! We know better. */
879 if (event->keyval == GDK_KEY_BackSpace &&
880 !(event->state & GDK_SHIFT_MASK)) {
881 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
883 use_ucsoutput = FALSE;
887 /* For Shift Backspace, do opposite of what is configured. */
888 if (event->keyval == GDK_KEY_BackSpace &&
889 (event->state & GDK_SHIFT_MASK)) {
890 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
892 use_ucsoutput = FALSE;
897 /* Shift-Tab is ESC [ Z */
898 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
899 (event->keyval == GDK_KEY_Tab &&
900 (event->state & GDK_SHIFT_MASK))) {
901 end = 1 + sprintf(output+1, "\033[Z");
902 use_ucsoutput = FALSE;
904 /* And normal Tab is Tab, if the keymap hasn't already told us.
905 * (Curiously, at least one version of the MacOS 10.5 X server
906 * doesn't translate Tab for us. */
907 if (event->keyval == GDK_KEY_Tab && end <= 1) {
913 * NetHack keypad mode.
916 const char *keys = NULL;
917 switch (event->keyval) {
918 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
919 keys = "bB\002"; break;
920 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
921 keys = "jJ\012"; break;
922 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
923 keys = "nN\016"; break;
924 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
925 keys = "hH\010"; break;
926 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
928 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
929 keys = "lL\014"; break;
930 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
931 keys = "yY\031"; break;
932 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
933 keys = "kK\013"; break;
934 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
935 keys = "uU\025"; break;
939 if (event->state & GDK_CONTROL_MASK)
941 else if (event->state & GDK_SHIFT_MASK)
945 use_ucsoutput = FALSE;
951 * Application keypad mode.
953 if (app_keypad_mode) {
955 switch (event->keyval) {
956 case GDK_KEY_Num_Lock: xkey = 'P'; break;
957 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
958 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
959 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
961 * Keypad + is tricky. It covers a space that would
962 * be taken up on the VT100 by _two_ keys; so we
963 * let Shift select between the two. Worse still,
964 * in xterm function key mode we change which two...
967 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
968 if (event->state & GDK_SHIFT_MASK)
972 } else if (event->state & GDK_SHIFT_MASK)
977 case GDK_KEY_KP_Enter: xkey = 'M'; break;
978 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
979 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
980 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
981 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
982 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
983 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
984 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
985 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
986 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
987 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
988 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
992 if (inst->term->vt52_mode) {
993 if (xkey >= 'P' && xkey <= 'S')
994 end = 1 + sprintf(output+1, "\033%c", xkey);
996 end = 1 + sprintf(output+1, "\033?%c", xkey);
998 end = 1 + sprintf(output+1, "\033O%c", xkey);
999 use_ucsoutput = FALSE;
1005 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
1006 * for integer decimal nn.)
1008 * We also deal with the weird ones here. Linux VCs replace F1
1009 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1010 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1015 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1016 switch (event->keyval) {
1018 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1021 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1024 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1027 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1030 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1033 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1036 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1039 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1042 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1045 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1078 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1079 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1082 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1085 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1088 case GDK_KEY_End: case GDK_KEY_KP_End:
1091 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1094 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1098 /* Reorder edit keys to physical order */
1099 if (funky_type == FUNKY_VT400 && code <= 6)
1100 code = "\0\2\1\4\5\3\6"[code];
1102 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1103 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1104 use_ucsoutput = FALSE;
1108 if (funky_type == FUNKY_SCO && /* SCO function keys */
1109 code >= 11 && code <= 34) {
1110 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1112 switch (event->keyval) {
1113 case GDK_KEY_F1: index = 0; break;
1114 case GDK_KEY_F2: index = 1; break;
1115 case GDK_KEY_F3: index = 2; break;
1116 case GDK_KEY_F4: index = 3; break;
1117 case GDK_KEY_F5: index = 4; break;
1118 case GDK_KEY_F6: index = 5; break;
1119 case GDK_KEY_F7: index = 6; break;
1120 case GDK_KEY_F8: index = 7; break;
1121 case GDK_KEY_F9: index = 8; break;
1122 case GDK_KEY_F10: index = 9; break;
1123 case GDK_KEY_F11: index = 10; break;
1124 case GDK_KEY_F12: index = 11; break;
1126 if (event->state & GDK_SHIFT_MASK) index += 12;
1127 if (event->state & GDK_CONTROL_MASK) index += 24;
1128 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1129 use_ucsoutput = FALSE;
1132 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1133 code >= 1 && code <= 6) {
1134 char codes[] = "HL.FIG";
1139 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1141 use_ucsoutput = FALSE;
1144 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1145 code >= 11 && code <= 24) {
1151 if (inst->term->vt52_mode)
1152 end = 1 + sprintf(output+1,
1153 "\x1B%c", code + 'P' - 11 - offt);
1155 end = 1 + sprintf(output+1,
1156 "\x1BO%c", code + 'P' - 11 - offt);
1157 use_ucsoutput = FALSE;
1160 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1161 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1162 use_ucsoutput = FALSE;
1165 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1166 if (inst->term->vt52_mode)
1167 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1169 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1170 use_ucsoutput = FALSE;
1173 if ((code == 1 || code == 4) &&
1174 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1175 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1176 use_ucsoutput = FALSE;
1180 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1181 use_ucsoutput = FALSE;
1187 * Cursor keys. (This includes the numberpad cursor keys,
1188 * if we haven't already done them due to app keypad mode.)
1190 * Here we also process un-numlocked un-appkeypadded KP5,
1191 * which sends ESC [ G.
1195 switch (event->keyval) {
1196 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1197 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1198 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1199 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1200 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1203 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1204 event->state & GDK_CONTROL_MASK);
1205 use_ucsoutput = FALSE;
1214 if (end-start > 0) {
1215 #ifdef KEY_DEBUGGING
1217 printf("generating sequence:");
1218 for (i = start; i < end; i++)
1219 printf(" %02x", (unsigned char) output[i]);
1225 * For special control characters, the character set
1226 * should never matter.
1228 output[end] = '\0'; /* NUL-terminate */
1230 ldisc_send(inst->ldisc, output+start, -2, 1);
1231 } else if (!inst->direct_to_font) {
1232 if (!use_ucsoutput) {
1234 lpage_send(inst->ldisc, output_charset, output+start,
1238 * We generated our own Unicode key data from the
1239 * keysym, so use that instead.
1242 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1246 * In direct-to-font mode, we just send the string
1247 * exactly as we received it.
1250 ldisc_send(inst->ldisc, output+start, end-start, 1);
1253 show_mouseptr(inst, 0);
1254 term_seen_key_event(inst->term);
1260 #if GTK_CHECK_VERSION(2,0,0)
1261 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1263 struct gui_data *inst = (struct gui_data *)data;
1265 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1266 show_mouseptr(inst, 0);
1267 term_seen_key_event(inst->term);
1271 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1272 GdkEventType type, guint ebutton, guint state,
1273 gdouble ex, gdouble ey)
1275 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1277 /* Remember the timestamp. */
1278 inst->input_event_time = timestamp;
1280 show_mouseptr(inst, 1);
1282 shift = state & GDK_SHIFT_MASK;
1283 ctrl = state & GDK_CONTROL_MASK;
1284 alt = state & GDK_MOD1_MASK;
1287 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1288 CONF_mouse_override));
1290 if (!raw_mouse_mode) {
1291 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1292 term_scroll(inst->term, 0, -5);
1295 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1296 term_scroll(inst->term, 0, +5);
1301 if (ebutton == 3 && ctrl) {
1302 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1303 ebutton, timestamp);
1309 else if (ebutton == 2)
1310 button = MBT_MIDDLE;
1311 else if (ebutton == 3)
1313 else if (ebutton == 4)
1314 button = MBT_WHEEL_UP;
1315 else if (ebutton == 5)
1316 button = MBT_WHEEL_DOWN;
1318 return FALSE; /* don't even know what button! */
1321 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1322 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1323 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1324 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1325 default: return FALSE; /* don't know this event type */
1328 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1329 return TRUE; /* we ignore these in raw mouse mode */
1331 x = (ex - inst->window_border) / inst->font_width;
1332 y = (ey - inst->window_border) / inst->font_height;
1334 term_mouse(inst->term, button, translate_button(button), act,
1335 x, y, shift, ctrl, alt);
1340 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1342 struct gui_data *inst = (struct gui_data *)data;
1343 return button_internal(inst, event->time, event->type, event->button,
1344 event->state, event->x, event->y);
1347 #if GTK_CHECK_VERSION(2,0,0)
1349 * In GTK 2, mouse wheel events have become a new type of event.
1350 * This handler translates them back into button-4 and button-5
1351 * presses so that I don't have to change my old code too much :-)
1353 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1355 struct gui_data *inst = (struct gui_data *)data;
1358 if (event->direction == GDK_SCROLL_UP)
1360 else if (event->direction == GDK_SCROLL_DOWN)
1365 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1366 button, event->state, event->x, event->y);
1370 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1372 struct gui_data *inst = (struct gui_data *)data;
1373 int shift, ctrl, alt, x, y, button;
1375 /* Remember the timestamp. */
1376 inst->input_event_time = event->time;
1378 show_mouseptr(inst, 1);
1380 shift = event->state & GDK_SHIFT_MASK;
1381 ctrl = event->state & GDK_CONTROL_MASK;
1382 alt = event->state & GDK_MOD1_MASK;
1383 if (event->state & GDK_BUTTON1_MASK)
1385 else if (event->state & GDK_BUTTON2_MASK)
1386 button = MBT_MIDDLE;
1387 else if (event->state & GDK_BUTTON3_MASK)
1390 return FALSE; /* don't even know what button! */
1392 x = (event->x - inst->window_border) / inst->font_width;
1393 y = (event->y - inst->window_border) / inst->font_height;
1395 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1396 x, y, shift, ctrl, alt);
1401 void frontend_keypress(void *handle)
1403 struct gui_data *inst = (struct gui_data *)handle;
1406 * If our child process has exited but not closed, terminate on
1413 static void exit_callback(void *vinst)
1415 struct gui_data *inst = (struct gui_data *)vinst;
1416 int exitcode, close_on_exit;
1418 if (!inst->exited &&
1419 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1420 inst->exited = TRUE;
1421 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1422 if (close_on_exit == FORCE_ON ||
1423 (close_on_exit == AUTO && exitcode == 0))
1424 gtk_main_quit(); /* just go */
1426 ldisc_free(inst->ldisc);
1429 inst->back->free(inst->backhandle);
1430 inst->backhandle = NULL;
1432 term_provide_resize_fn(inst->term, NULL, NULL);
1433 update_specials_menu(inst);
1434 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1439 * Replacement code for the gtk_quit_add() function, which GTK2 - in
1440 * their unbounded wisdom - deprecated without providing any usable
1441 * replacement, and which we were using to ensure that our idle
1442 * function for toplevel callbacks was only run from the outermost
1445 * We maintain a global variable with a list of 'struct gui_data'
1446 * instances on which we should call inst_post_main() when a
1447 * subsidiary gtk_main() terminates; then we must make sure that all
1448 * our subsidiary calls to gtk_main() are followed by a call to
1451 * This is kind of overkill in the sense that at the time of writing
1452 * we don't actually ever run more than one 'struct gui_data' instance
1453 * in the same process, but we're _so nearly_ prepared to do that that
1454 * I want to remain futureproof against the possibility of doing so in
1457 struct post_main_context {
1458 struct post_main_context *next;
1459 struct gui_data *inst;
1461 struct post_main_context *post_main_list_head = NULL;
1462 static void request_post_main(struct gui_data *inst)
1464 struct post_main_context *node = snew(struct post_main_context);
1465 node->next = post_main_list_head;
1467 post_main_list_head = node;
1469 static void inst_post_main(struct gui_data *inst);
1470 void post_main(void)
1472 while (post_main_list_head) {
1473 struct post_main_context *node = post_main_list_head;
1474 post_main_list_head = node->next;
1475 inst_post_main(node->inst);
1480 void notify_remote_exit(void *frontend)
1482 struct gui_data *inst = (struct gui_data *)frontend;
1484 queue_toplevel_callback(exit_callback, inst);
1487 static void notify_toplevel_callback(void *frontend);
1489 static void inst_post_main(struct gui_data *inst)
1491 if (gtk_main_level() == 1) {
1492 notify_toplevel_callback(inst);
1493 inst->quit_fn_scheduled = FALSE;
1495 /* Apparently we're _still_ more than one level deep in
1496 * gtk_main() instances, so we'll need another callback for
1497 * when we get out of the next one. */
1498 request_post_main(inst);
1502 static gint idle_toplevel_callback_func(gpointer data)
1504 struct gui_data *inst = (struct gui_data *)data;
1506 if (gtk_main_level() > 1) {
1508 * We don't run the callbacks if we're in the middle of a
1509 * subsidiary gtk_main. Instead, ask for a callback when we
1510 * get back out of the subsidiary main loop (if we haven't
1511 * already arranged one), so we can reschedule ourself then.
1513 if (!inst->quit_fn_scheduled) {
1514 request_post_main(inst);
1515 inst->quit_fn_scheduled = TRUE;
1518 * And unschedule this idle function, since we've now done
1519 * everything we can until the innermost gtk_main has quit and
1520 * can reschedule us with a chance of actually taking action.
1522 if (inst->idle_fn_scheduled) { /* double-check, just in case */
1523 g_source_remove(inst->toplevel_callback_idle_id);
1524 inst->idle_fn_scheduled = FALSE;
1527 run_toplevel_callbacks();
1531 * If we've emptied our toplevel callback queue, unschedule
1532 * ourself. Otherwise, leave ourselves pending so we'll be called
1533 * again to deal with more callbacks after another round of the
1536 if (!toplevel_callback_pending() && inst->idle_fn_scheduled) {
1537 g_source_remove(inst->toplevel_callback_idle_id);
1538 inst->idle_fn_scheduled = FALSE;
1544 static void notify_toplevel_callback(void *frontend)
1546 struct gui_data *inst = (struct gui_data *)frontend;
1548 if (!inst->idle_fn_scheduled) {
1549 inst->toplevel_callback_idle_id =
1550 g_idle_add(idle_toplevel_callback_func, inst);
1551 inst->idle_fn_scheduled = TRUE;
1555 static gint timer_trigger(gpointer data)
1557 unsigned long now = GPOINTER_TO_LONG(data);
1558 unsigned long next, then;
1562 * Destroy the timer we got here on.
1565 g_source_remove(timer_id);
1570 * run_timers() may cause a call to timer_change_notify, in which
1571 * case a new timer will already have been set up and left in
1572 * timer_id. If it hasn't, and run_timers reports that some timing
1573 * still needs to be done, we do it ourselves.
1575 if (run_timers(now, &next) && !timer_id) {
1577 now = GETTICKCOUNT();
1578 if (now - then > next - then)
1582 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1586 * Returning FALSE means 'don't call this timer again', which
1587 * _should_ be redundant given that we removed it above, but just
1588 * in case, return FALSE anyway.
1593 void timer_change_notify(unsigned long next)
1598 g_source_remove(timer_id);
1600 ticks = next - GETTICKCOUNT();
1602 ticks = 1; /* just in case */
1604 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1607 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1610 * We must process exceptional notifications before ordinary
1611 * readability ones, or we may go straight past the urgent
1614 if (condition & GDK_INPUT_EXCEPTION)
1615 select_result(sourcefd, 4);
1616 if (condition & GDK_INPUT_READ)
1617 select_result(sourcefd, 1);
1618 if (condition & GDK_INPUT_WRITE)
1619 select_result(sourcefd, 2);
1622 void destroy(GtkWidget *widget, gpointer data)
1627 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1629 struct gui_data *inst = (struct gui_data *)data;
1630 term_set_focus(inst->term, event->in);
1631 term_update(inst->term);
1632 show_mouseptr(inst, 1);
1636 void set_busy_status(void *frontend, int status)
1638 struct gui_data *inst = (struct gui_data *)frontend;
1639 inst->busy_status = status;
1640 update_mouseptr(inst);
1644 * set or clear the "raw mouse message" mode
1646 void set_raw_mouse_mode(void *frontend, int activate)
1648 struct gui_data *inst = (struct gui_data *)frontend;
1649 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1650 send_raw_mouse = activate;
1651 update_mouseptr(inst);
1654 void request_resize(void *frontend, int w, int h)
1656 struct gui_data *inst = (struct gui_data *)frontend;
1657 int large_x, large_y;
1658 int offset_x, offset_y;
1660 GtkRequisition inner, outer;
1663 * This is a heinous hack dreamed up by the gnome-terminal
1664 * people to get around a limitation in gtk. The problem is
1665 * that in order to set the size correctly we really need to be
1666 * calling gtk_window_resize - but that needs to know the size
1667 * of the _whole window_, not the drawing area. So what we do
1668 * is to set an artificially huge size request on the drawing
1669 * area, recompute the resulting size request on the window,
1670 * and look at the difference between the two. That gives us
1671 * the x and y offsets we need to translate drawing area size
1672 * into window size for real, and then we call
1673 * gtk_window_resize.
1677 * We start by retrieving the current size of the whole window.
1678 * Adding a bit to _that_ will give us a value we can use as a
1679 * bogus size request which guarantees to be bigger than the
1680 * current size of the drawing area.
1682 get_window_pixels(inst, &large_x, &large_y);
1686 gtk_widget_set_size_request(inst->area, large_x, large_y);
1687 gtk_widget_size_request(inst->area, &inner);
1688 gtk_widget_size_request(inst->window, &outer);
1690 offset_x = outer.width - inner.width;
1691 offset_y = outer.height - inner.height;
1693 area_x = inst->font_width * w + 2*inst->window_border;
1694 area_y = inst->font_height * h + 2*inst->window_border;
1697 * Now we must set the size request on the drawing area back to
1698 * something sensible before we commit the real resize. Best
1699 * way to do this, I think, is to set it to what the size is
1700 * really going to end up being.
1702 gtk_widget_set_size_request(inst->area, area_x, area_y);
1703 #if GTK_CHECK_VERSION(2,0,0)
1704 gtk_window_resize(GTK_WINDOW(inst->window),
1705 area_x + offset_x, area_y + offset_y);
1707 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1709 * I can no longer remember what this call to
1710 * gtk_container_dequeue_resize_handler is for. It was
1711 * introduced in r3092 with no comment, and the commit log
1712 * message was uninformative. I'm _guessing_ its purpose is to
1713 * prevent gratuitous resize processing on the window given
1714 * that we're about to resize it anyway, but I have no idea
1715 * why that's so incredibly vital.
1717 * I've tried removing the call, and nothing seems to go
1718 * wrong. I've backtracked to r3092 and tried removing the
1719 * call there, and still nothing goes wrong. So I'm going to
1720 * adopt the working hypothesis that it's superfluous; I won't
1721 * actually remove it from the GTK 1.2 code, but I won't
1722 * attempt to replicate its functionality in the GTK 2 code
1725 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1726 gdk_window_resize(gtk_widget_get_window(inst->window),
1727 area_x + offset_x, area_y + offset_y);
1731 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1733 gboolean success[1];
1735 inst->cols[n].red = r * 0x0101;
1736 inst->cols[n].green = g * 0x0101;
1737 inst->cols[n].blue = b * 0x0101;
1739 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1740 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1741 FALSE, TRUE, success);
1743 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1747 void set_window_background(struct gui_data *inst)
1749 if (inst->area && gtk_widget_get_window(inst->area))
1750 gdk_window_set_background(gtk_widget_get_window(inst->area),
1752 if (inst->window && gtk_widget_get_window(inst->window))
1753 gdk_window_set_background(gtk_widget_get_window(inst->window),
1757 void palette_set(void *frontend, int n, int r, int g, int b)
1759 struct gui_data *inst = (struct gui_data *)frontend;
1762 if (n >= NALLCOLOURS)
1764 real_palette_set(inst, n, r, g, b);
1766 /* Default Background changed. Ensure space between text area and
1767 * window border is redrawn */
1768 set_window_background(inst);
1769 draw_backing_rect(inst);
1770 gtk_widget_queue_draw(inst->area);
1774 void palette_reset(void *frontend)
1776 struct gui_data *inst = (struct gui_data *)frontend;
1777 /* This maps colour indices in inst->conf to those used in inst->cols. */
1778 static const int ww[] = {
1779 256, 257, 258, 259, 260, 261,
1780 0, 8, 1, 9, 2, 10, 3, 11,
1781 4, 12, 5, 13, 6, 14, 7, 15
1783 gboolean success[NALLCOLOURS];
1786 assert(lenof(ww) == NCFGCOLOURS);
1788 if (!inst->colmap) {
1789 inst->colmap = gdk_colormap_get_system();
1791 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1794 for (i = 0; i < NCFGCOLOURS; i++) {
1795 inst->cols[ww[i]].red =
1796 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
1797 inst->cols[ww[i]].green =
1798 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
1799 inst->cols[ww[i]].blue =
1800 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
1803 for (i = 0; i < NEXTCOLOURS; i++) {
1805 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1806 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1807 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1808 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1810 int shade = i - 216;
1811 shade = shade * 0x0a0a + 0x0808;
1812 inst->cols[i+16].red = inst->cols[i+16].green =
1813 inst->cols[i+16].blue = shade;
1817 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1818 FALSE, TRUE, success);
1819 for (i = 0; i < NALLCOLOURS; i++) {
1821 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1823 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
1824 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
1825 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
1828 /* Since Default Background may have changed, ensure that space
1829 * between text area and window border is refreshed. */
1830 set_window_background(inst);
1831 if (inst->area && gtk_widget_get_window(inst->area)) {
1832 draw_backing_rect(inst);
1833 gtk_widget_queue_draw(inst->area);
1837 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1838 * do this before we start using cut buffers.
1840 void init_cutbuffers()
1842 #ifndef NOT_X_WINDOWS
1843 unsigned char empty[] = "";
1844 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1845 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1846 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1847 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1848 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1849 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1850 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1851 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1852 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1853 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1854 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1855 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1856 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1857 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1858 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1859 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1863 /* Store the data in a cut-buffer. */
1864 void store_cutbuffer(char * ptr, int len)
1866 #ifndef NOT_X_WINDOWS
1867 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1868 XRotateBuffers(GDK_DISPLAY(), 1);
1869 XStoreBytes(GDK_DISPLAY(), ptr, len);
1873 /* Retrieve data from a cut-buffer.
1874 * Returned data needs to be freed with XFree().
1876 char * retrieve_cutbuffer(int * nbytes)
1878 #ifndef NOT_X_WINDOWS
1880 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1881 if (*nbytes <= 0 && ptr != 0) {
1891 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1893 struct gui_data *inst = (struct gui_data *)frontend;
1894 if (inst->pasteout_data)
1895 sfree(inst->pasteout_data);
1896 if (inst->pasteout_data_ctext)
1897 sfree(inst->pasteout_data_ctext);
1898 if (inst->pasteout_data_utf8)
1899 sfree(inst->pasteout_data_utf8);
1902 * Set up UTF-8 and compound text paste data. This only happens
1903 * if we aren't in direct-to-font mode using the D800 hack.
1905 if (!inst->direct_to_font) {
1906 const wchar_t *tmp = data;
1908 #ifndef NOT_X_WINDOWS
1913 inst->pasteout_data_utf8 = snewn(len*6, char);
1914 inst->pasteout_data_utf8_len = len*6;
1915 inst->pasteout_data_utf8_len =
1916 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1917 inst->pasteout_data_utf8_len,
1918 CS_UTF8, NULL, NULL, 0);
1919 if (inst->pasteout_data_utf8_len == 0) {
1920 sfree(inst->pasteout_data_utf8);
1921 inst->pasteout_data_utf8 = NULL;
1923 inst->pasteout_data_utf8 =
1924 sresize(inst->pasteout_data_utf8,
1925 inst->pasteout_data_utf8_len + 1, char);
1926 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1930 * Now let Xlib convert our UTF-8 data into compound text.
1932 #ifndef NOT_X_WINDOWS
1933 list[0] = inst->pasteout_data_utf8;
1934 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1935 XCompoundTextStyle, &tp) == 0) {
1936 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1937 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1938 inst->pasteout_data_ctext_len = tp.nitems;
1943 inst->pasteout_data_ctext = NULL;
1944 inst->pasteout_data_ctext_len = 0;
1947 inst->pasteout_data_utf8 = NULL;
1948 inst->pasteout_data_utf8_len = 0;
1949 inst->pasteout_data_ctext = NULL;
1950 inst->pasteout_data_ctext_len = 0;
1953 inst->pasteout_data = snewn(len*6, char);
1954 inst->pasteout_data_len = len*6;
1955 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1956 data, len, inst->pasteout_data,
1957 inst->pasteout_data_len,
1959 if (inst->pasteout_data_len == 0) {
1960 sfree(inst->pasteout_data);
1961 inst->pasteout_data = NULL;
1963 inst->pasteout_data =
1964 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1967 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1969 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1970 inst->input_event_time)) {
1971 #if GTK_CHECK_VERSION(2,0,0)
1972 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
1974 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1975 GDK_SELECTION_TYPE_STRING, 1);
1976 if (inst->pasteout_data_ctext)
1977 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1978 compound_text_atom, 1);
1979 if (inst->pasteout_data_utf8)
1980 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1981 utf8_string_atom, 1);
1985 term_deselect(inst->term);
1988 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1989 guint info, guint time_stamp, gpointer data)
1991 struct gui_data *inst = (struct gui_data *)data;
1992 GdkAtom target = gtk_selection_data_get_target(seldata);
1993 if (target == utf8_string_atom)
1994 gtk_selection_data_set(seldata, target, 8,
1995 (unsigned char *)inst->pasteout_data_utf8,
1996 inst->pasteout_data_utf8_len);
1997 else if (target == compound_text_atom)
1998 gtk_selection_data_set(seldata, target, 8,
1999 (unsigned char *)inst->pasteout_data_ctext,
2000 inst->pasteout_data_ctext_len);
2002 gtk_selection_data_set(seldata, target, 8,
2003 (unsigned char *)inst->pasteout_data,
2004 inst->pasteout_data_len);
2007 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2010 struct gui_data *inst = (struct gui_data *)data;
2012 term_deselect(inst->term);
2013 if (inst->pasteout_data)
2014 sfree(inst->pasteout_data);
2015 if (inst->pasteout_data_ctext)
2016 sfree(inst->pasteout_data_ctext);
2017 if (inst->pasteout_data_utf8)
2018 sfree(inst->pasteout_data_utf8);
2019 inst->pasteout_data = NULL;
2020 inst->pasteout_data_len = 0;
2021 inst->pasteout_data_ctext = NULL;
2022 inst->pasteout_data_ctext_len = 0;
2023 inst->pasteout_data_utf8 = NULL;
2024 inst->pasteout_data_utf8_len = 0;
2028 void request_paste(void *frontend)
2030 struct gui_data *inst = (struct gui_data *)frontend;
2032 * In Unix, pasting is asynchronous: all we can do at the
2033 * moment is to call gtk_selection_convert(), and when the data
2034 * comes back _then_ we can call term_do_paste().
2037 if (!inst->direct_to_font) {
2039 * First we attempt to retrieve the selection as a UTF-8
2040 * string (which we will convert to the correct code page
2041 * before sending to the session, of course). If that
2042 * fails, selection_received() will be informed and will
2043 * fall back to an ordinary string.
2045 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2047 inst->input_event_time);
2050 * If we're in direct-to-font mode, we disable UTF-8
2051 * pasting, and go straight to ordinary string data.
2053 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2054 GDK_SELECTION_TYPE_STRING,
2055 inst->input_event_time);
2059 gint idle_paste_func(gpointer data); /* forward ref */
2061 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2062 guint time, gpointer data)
2064 struct gui_data *inst = (struct gui_data *)data;
2067 #ifndef NOT_X_WINDOWS
2069 int free_list_required = 0;
2070 int free_required = 0;
2073 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2074 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2075 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2076 gint seldata_length = gtk_selection_data_get_length(seldata);
2078 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2080 * Failed to get a UTF-8 selection string. Try compound
2083 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2085 inst->input_event_time);
2089 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2091 * Failed to get UTF-8 or compound text. Try an ordinary
2094 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2095 GDK_SELECTION_TYPE_STRING,
2096 inst->input_event_time);
2101 * If we have data, but it's not of a type we can deal with,
2102 * we have to ignore the data.
2104 if (seldata_length > 0 &&
2105 seldata_type != GDK_SELECTION_TYPE_STRING &&
2106 seldata_type != compound_text_atom &&
2107 seldata_type != utf8_string_atom)
2111 * If we have no data, try looking in a cut buffer.
2113 if (seldata_length <= 0) {
2114 #ifndef NOT_X_WINDOWS
2115 text = retrieve_cutbuffer(&length);
2118 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2119 * source, so use that as a de-facto standard. */
2120 charset = CS_ISO8859_1;
2127 * Convert COMPOUND_TEXT into UTF-8.
2129 if (seldata_type == compound_text_atom) {
2130 #ifndef NOT_X_WINDOWS
2134 tp.value = (unsigned char *)seldata_data;
2135 tp.encoding = (Atom) seldata_type;
2136 tp.format = gtk_selection_data_get_format(seldata);
2137 tp.nitems = seldata_length;
2138 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
2140 if (ret == 0 && count == 1) {
2142 length = strlen(list[0]);
2144 free_list_required = 1;
2149 * Compound text failed; fall back to STRING.
2151 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2152 GDK_SELECTION_TYPE_STRING,
2153 inst->input_event_time);
2157 text = (char *)seldata_data;
2158 length = seldata_length;
2159 charset = (seldata_type == utf8_string_atom ?
2160 CS_UTF8 : inst->ucsdata.line_codepage);
2164 if (inst->pastein_data)
2165 sfree(inst->pastein_data);
2167 inst->pastein_data = snewn(length, wchar_t);
2168 inst->pastein_data_len = length;
2169 inst->pastein_data_len =
2170 mb_to_wc(charset, 0, text, length,
2171 inst->pastein_data, inst->pastein_data_len);
2173 term_do_paste(inst->term);
2175 #ifndef NOT_X_WINDOWS
2176 if (free_list_required)
2177 XFreeStringList(list);
2183 void get_clip(void *frontend, wchar_t ** p, int *len)
2185 struct gui_data *inst = (struct gui_data *)frontend;
2188 *p = inst->pastein_data;
2189 *len = inst->pastein_data_len;
2193 static void set_window_titles(struct gui_data *inst)
2196 * We must always call set_icon_name after calling set_title,
2197 * since set_title will write both names. Irritating, but such
2200 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2201 if (!conf_get_int(inst->conf, CONF_win_name_always))
2202 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
2206 void set_title(void *frontend, char *title)
2208 struct gui_data *inst = (struct gui_data *)frontend;
2209 sfree(inst->wintitle);
2210 inst->wintitle = dupstr(title);
2211 set_window_titles(inst);
2214 void set_icon(void *frontend, char *title)
2216 struct gui_data *inst = (struct gui_data *)frontend;
2217 sfree(inst->icontitle);
2218 inst->icontitle = dupstr(title);
2219 set_window_titles(inst);
2222 void set_title_and_icon(void *frontend, char *title, char *icon)
2224 struct gui_data *inst = (struct gui_data *)frontend;
2225 sfree(inst->wintitle);
2226 inst->wintitle = dupstr(title);
2227 sfree(inst->icontitle);
2228 inst->icontitle = dupstr(icon);
2229 set_window_titles(inst);
2232 void set_sbar(void *frontend, int total, int start, int page)
2234 struct gui_data *inst = (struct gui_data *)frontend;
2235 if (!conf_get_int(inst->conf, CONF_scrollbar))
2237 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
2238 gtk_adjustment_set_upper(inst->sbar_adjust, total);
2239 gtk_adjustment_set_value(inst->sbar_adjust, start);
2240 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
2241 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
2242 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
2243 inst->ignore_sbar = TRUE;
2244 gtk_adjustment_changed(inst->sbar_adjust);
2245 inst->ignore_sbar = FALSE;
2248 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2250 struct gui_data *inst = (struct gui_data *)data;
2252 if (!conf_get_int(inst->conf, CONF_scrollbar))
2254 if (!inst->ignore_sbar)
2255 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
2258 void sys_cursor(void *frontend, int x, int y)
2261 * This is meaningless under X.
2266 * This is still called when mode==BELL_VISUAL, even though the
2267 * visual bell is handled entirely within terminal.c, because we
2268 * may want to perform additional actions on any kind of bell (for
2269 * example, taskbar flashing in Windows).
2271 void do_beep(void *frontend, int mode)
2273 if (mode == BELL_DEFAULT)
2277 int char_width(Context ctx, int uc)
2280 * Under X, any fixed-width font really _is_ fixed-width.
2281 * Double-width characters will be dealt with using a separate
2282 * font. For the moment we can simply return 1.
2284 * FIXME: but is that also true of Pango?
2289 Context get_ctx(void *frontend)
2291 struct gui_data *inst = (struct gui_data *)frontend;
2292 struct draw_ctx *dctx;
2294 if (!gtk_widget_get_window(inst->area))
2297 dctx = snew(struct draw_ctx);
2299 dctx->gc = gdk_gc_new(gtk_widget_get_window(inst->area));
2303 void free_ctx(Context ctx)
2305 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2306 /* struct gui_data *inst = dctx->inst; */
2307 GdkGC *gc = dctx->gc;
2313 * Draw a line of text in the window, at given character
2314 * coordinates, in given attributes.
2316 * We are allowed to fiddle with the contents of `text'.
2318 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2319 unsigned long attr, int lattr)
2321 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2322 struct gui_data *inst = dctx->inst;
2323 GdkGC *gc = dctx->gc;
2324 int ncombining, combining;
2325 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2327 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
2329 if (attr & TATTR_COMBINING) {
2335 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2336 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2337 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2342 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
2343 if (nfg < 16) nfg |= 8;
2344 else if (nfg >= 256) nfg |= 1;
2346 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
2347 if (nbg < 16) nbg |= 8;
2348 else if (nbg >= 256) nbg |= 1;
2350 if ((attr & TATTR_ACTCURS) && !monochrome) {
2355 fontid = shadow = 0;
2357 if (attr & ATTR_WIDE) {
2364 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
2371 if (!inst->fonts[fontid]) {
2374 * Fall back through font ids with subsets of this one's
2375 * set bits, in order.
2377 for (i = fontid; i-- > 0 ;) {
2379 continue; /* some other bit is set */
2380 if (inst->fonts[i]) {
2385 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2388 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2390 if (x >= inst->term->cols)
2392 if (x + len*2*widefactor > inst->term->cols)
2393 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2401 r.x = x*inst->font_width+inst->window_border;
2402 r.y = y*inst->font_height+inst->window_border;
2403 r.width = rlen*widefactor*inst->font_width;
2404 r.height = inst->font_height;
2405 gdk_gc_set_clip_rectangle(gc, &r);
2408 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
2409 gdk_draw_rectangle(inst->pixmap, gc, 1,
2410 x*inst->font_width+inst->window_border,
2411 y*inst->font_height+inst->window_border,
2412 rlen*widefactor*inst->font_width, inst->font_height);
2414 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2415 for (combining = 0; combining < ncombining; combining++) {
2416 unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid],
2417 x*inst->font_width+inst->window_border,
2418 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2419 text + combining, len, widefactor > 1,
2420 bold, inst->font_width);
2423 if (attr & ATTR_UNDER) {
2424 int uheight = inst->fonts[0]->ascent + 1;
2425 if (uheight >= inst->font_height)
2426 uheight = inst->font_height - 1;
2427 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->window_border,
2428 y*inst->font_height + uheight + inst->window_border,
2429 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2430 y*inst->font_height + uheight + inst->window_border);
2433 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2435 * I can't find any plausible StretchBlt equivalent in the
2436 * X server, so I'm going to do this the slow and painful
2437 * way. This will involve repeated calls to
2438 * gdk_draw_pixmap() to stretch the text horizontally. It's
2439 * O(N^2) in time and O(N) in network bandwidth, but you
2440 * try thinking of a better way. :-(
2443 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2444 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2445 x*inst->font_width+inst->window_border + 2*i,
2446 y*inst->font_height+inst->window_border,
2447 x*inst->font_width+inst->window_border + 2*i+1,
2448 y*inst->font_height+inst->window_border,
2449 len * widefactor * inst->font_width - i, inst->font_height);
2452 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2454 /* Now stretch vertically, in the same way. */
2455 if ((lattr & LATTR_MODE) == LATTR_BOT)
2459 for (i = 0; i < inst->font_height; i+=2) {
2460 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2461 x*inst->font_width+inst->window_border,
2462 y*inst->font_height+inst->window_border+dt*i+db,
2463 x*inst->font_width+inst->window_border,
2464 y*inst->font_height+inst->window_border+dt*(i+1),
2465 len * widefactor * inst->font_width, inst->font_height-i-1);
2471 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2472 unsigned long attr, int lattr)
2474 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2475 struct gui_data *inst = dctx->inst;
2476 GdkGC *gc = dctx->gc;
2479 do_text_internal(ctx, x, y, text, len, attr, lattr);
2481 if (attr & ATTR_WIDE) {
2487 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2489 if (x >= inst->term->cols)
2491 if (x + len*2*widefactor > inst->term->cols)
2492 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2496 gdk_draw_pixmap(gtk_widget_get_window(inst->area), gc, inst->pixmap,
2497 x*inst->font_width+inst->window_border,
2498 y*inst->font_height+inst->window_border,
2499 x*inst->font_width+inst->window_border,
2500 y*inst->font_height+inst->window_border,
2501 len*widefactor*inst->font_width, inst->font_height);
2504 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2505 unsigned long attr, int lattr)
2507 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2508 struct gui_data *inst = dctx->inst;
2509 GdkGC *gc = dctx->gc;
2511 int active, passive, widefactor;
2513 if (attr & TATTR_PASCURS) {
2514 attr &= ~TATTR_PASCURS;
2518 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
2519 attr &= ~TATTR_ACTCURS;
2523 do_text_internal(ctx, x, y, text, len, attr, lattr);
2525 if (attr & TATTR_COMBINING)
2528 if (attr & ATTR_WIDE) {
2534 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2536 if (x >= inst->term->cols)
2538 if (x + len*2*widefactor > inst->term->cols)
2539 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2543 if (inst->cursor_type == 0) {
2545 * An active block cursor will already have been done by
2546 * the above do_text call, so we only need to do anything
2550 gdk_gc_set_foreground(gc, &inst->cols[261]);
2551 gdk_draw_rectangle(inst->pixmap, gc, 0,
2552 x*inst->font_width+inst->window_border,
2553 y*inst->font_height+inst->window_border,
2554 len*widefactor*inst->font_width-1, inst->font_height-1);
2558 int startx, starty, dx, dy, length, i;
2562 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2563 char_width = 2*inst->font_width;
2565 char_width = inst->font_width;
2567 if (inst->cursor_type == 1) {
2568 uheight = inst->fonts[0]->ascent + 1;
2569 if (uheight >= inst->font_height)
2570 uheight = inst->font_height - 1;
2572 startx = x * inst->font_width + inst->window_border;
2573 starty = y * inst->font_height + inst->window_border + uheight;
2576 length = len * widefactor * char_width;
2579 if (attr & TATTR_RIGHTCURS)
2580 xadjust = char_width - 1;
2581 startx = x * inst->font_width + inst->window_border + xadjust;
2582 starty = y * inst->font_height + inst->window_border;
2585 length = inst->font_height;
2588 gdk_gc_set_foreground(gc, &inst->cols[261]);
2590 for (i = 0; i < length; i++) {
2592 gdk_draw_point(inst->pixmap, gc, startx, starty);
2597 } else if (active) {
2598 gdk_draw_line(inst->pixmap, gc, startx, starty,
2599 startx + (length-1) * dx, starty + (length-1) * dy);
2600 } /* else no cursor (e.g., blinked off) */
2603 gdk_draw_pixmap(gtk_widget_get_window(inst->area), gc, inst->pixmap,
2604 x*inst->font_width+inst->window_border,
2605 y*inst->font_height+inst->window_border,
2606 x*inst->font_width+inst->window_border,
2607 y*inst->font_height+inst->window_border,
2608 len*widefactor*inst->font_width, inst->font_height);
2610 #if GTK_CHECK_VERSION(2,0,0)
2612 GdkRectangle cursorrect;
2613 cursorrect.x = x*inst->font_width+inst->window_border;
2614 cursorrect.y = y*inst->font_height+inst->window_border;
2615 cursorrect.width = len*widefactor*inst->font_width;
2616 cursorrect.height = inst->font_height;
2617 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
2622 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2625 * Truly hideous hack: GTK doesn't allow us to set the mouse
2626 * cursor foreground and background colours unless we've _also_
2627 * created our own cursor from bitmaps. Therefore, I need to
2628 * load the `cursor' font and draw glyphs from it on to
2629 * pixmaps, in order to construct my cursors with the fg and bg
2630 * I want. This is a gross hack, but it's more self-contained
2631 * than linking in Xlib to find the X window handle to
2632 * inst->area and calling XRecolorCursor, and it's more
2633 * futureproof than hard-coding the shapes as bitmap arrays.
2635 static GdkFont *cursor_font = NULL;
2636 GdkPixmap *source, *mask;
2638 GdkColor cfg = { 0, 65535, 65535, 65535 };
2639 GdkColor cbg = { 0, 0, 0, 0 };
2640 GdkColor dfg = { 1, 65535, 65535, 65535 };
2641 GdkColor dbg = { 0, 0, 0, 0 };
2644 gint lb, rb, wid, asc, desc, w, h, x, y;
2646 if (cursor_val == -2) {
2647 gdk_font_unref(cursor_font);
2651 if (cursor_val >= 0 && !cursor_font) {
2652 cursor_font = gdk_font_load("cursor");
2654 gdk_font_ref(cursor_font);
2658 * Get the text extent of the cursor in question. We use the
2659 * mask character for this, because it's typically slightly
2660 * bigger than the main character.
2662 if (cursor_val >= 0) {
2664 text[0] = (char)cursor_val + 1;
2665 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2666 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2672 source = gdk_pixmap_new(NULL, w, h, 1);
2673 mask = gdk_pixmap_new(NULL, w, h, 1);
2676 * Draw the mask character on the mask pixmap.
2678 gc = gdk_gc_new(mask);
2679 gdk_gc_set_foreground(gc, &dbg);
2680 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2681 if (cursor_val >= 0) {
2683 text[0] = (char)cursor_val + 1;
2684 gdk_gc_set_foreground(gc, &dfg);
2685 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2690 * Draw the main character on the source pixmap.
2692 gc = gdk_gc_new(source);
2693 gdk_gc_set_foreground(gc, &dbg);
2694 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2695 if (cursor_val >= 0) {
2697 text[0] = (char)cursor_val;
2698 gdk_gc_set_foreground(gc, &dfg);
2699 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2704 * Create the cursor.
2706 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2711 gdk_pixmap_unref(source);
2712 gdk_pixmap_unref(mask);
2717 void modalfatalbox(const char *p, ...)
2720 fprintf(stderr, "FATAL ERROR: ");
2722 vfprintf(stderr, p, ap);
2724 fputc('\n', stderr);
2728 void cmdline_error(const char *p, ...)
2731 fprintf(stderr, "%s: ", appname);
2733 vfprintf(stderr, p, ap);
2735 fputc('\n', stderr);
2739 char *get_x_display(void *frontend)
2741 return gdk_get_display();
2744 #ifndef NOT_X_WINDOWS
2745 long get_windowid(void *frontend)
2747 struct gui_data *inst = (struct gui_data *)frontend;
2748 return (long)GDK_WINDOW_XWINDOW(gtk_widget_get_window(inst->area));
2752 static void help(FILE *fp) {
2754 "pterm option summary:\n"
2756 " --display DISPLAY Specify X display to use (note '--')\n"
2757 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2758 " -fn FONT Normal text font\n"
2759 " -fb FONT Bold text font\n"
2760 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2761 " -sl LINES Number of lines of scrollback\n"
2762 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2763 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2764 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2765 " -T TITLE Window title\n"
2766 " -ut, +ut Do(default) or do not update utmp\n"
2767 " -ls, +ls Do(default) or do not make shell a login shell\n"
2768 " -sb, +sb Do(default) or do not display a scrollbar\n"
2769 " -log PATH Log all output to a file\n"
2770 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2771 " -xrm RESOURCE-STRING Set an X resource\n"
2772 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2773 ) < 0 || fflush(fp) < 0) {
2774 perror("output error");
2779 static void version(FILE *fp) {
2780 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
2781 perror("output error");
2786 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2787 struct gui_data *inst, Conf *conf)
2793 * Macros to make argument handling easier. Note that because
2794 * they need to call `continue', they cannot be contained in
2795 * the usual do {...} while (0) wrapper to make them
2796 * syntactically single statements; hence it is not legal to
2797 * use one of these macros as an unbraced statement between
2800 #define EXPECTS_ARG { \
2801 if (--argc <= 0) { \
2803 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2808 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2810 while (--argc > 0) {
2811 const char *p = *++argv;
2815 * Shameless cheating. Debian requires all X terminal
2816 * emulators to support `-T title'; but
2817 * cmdline_process_param will eat -T (it means no-pty) and
2818 * complain that pterm doesn't support it. So, in pterm
2819 * only, we convert -T into -title.
2821 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2825 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2826 do_everything ? 1 : -1, conf);
2829 cmdline_error("option \"%s\" requires an argument", p);
2830 } else if (ret == 2) {
2831 --argc, ++argv; /* skip next argument */
2833 } else if (ret == 1) {
2837 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2841 fs = fontspec_new(val);
2842 conf_set_fontspec(conf, CONF_font, fs);
2845 } else if (!strcmp(p, "-fb")) {
2849 fs = fontspec_new(val);
2850 conf_set_fontspec(conf, CONF_boldfont, fs);
2853 } else if (!strcmp(p, "-fw")) {
2857 fs = fontspec_new(val);
2858 conf_set_fontspec(conf, CONF_widefont, fs);
2861 } else if (!strcmp(p, "-fwb")) {
2865 fs = fontspec_new(val);
2866 conf_set_fontspec(conf, CONF_wideboldfont, fs);
2869 } else if (!strcmp(p, "-cs")) {
2872 conf_set_str(conf, CONF_line_codepage, val);
2874 } else if (!strcmp(p, "-geometry")) {
2878 #if GTK_CHECK_VERSION(2,0,0)
2879 inst->geometry = val;
2881 /* On GTK 1, we have to do this using raw Xlib */
2885 flags = XParseGeometry(val, &x, &y, &w, &h);
2886 if (flags & WidthValue)
2887 conf_set_int(conf, CONF_width, w);
2888 if (flags & HeightValue)
2889 conf_set_int(conf, CONF_height, h);
2891 if (flags & (XValue | YValue)) {
2894 inst->gotpos = TRUE;
2895 inst->gravity = ((flags & XNegative ? 1 : 0) |
2896 (flags & YNegative ? 2 : 0));
2901 } else if (!strcmp(p, "-sl")) {
2904 conf_set_int(conf, CONF_savelines, atoi(val));
2906 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2907 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2908 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2913 if (!gdk_color_parse(val, &col)) {
2915 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2919 index = (!strcmp(p, "-fg") ? 0 :
2920 !strcmp(p, "-bg") ? 2 :
2921 !strcmp(p, "-bfg") ? 1 :
2922 !strcmp(p, "-bbg") ? 3 :
2923 !strcmp(p, "-cfg") ? 4 :
2924 !strcmp(p, "-cbg") ? 5 : -1);
2925 assert(index != -1);
2926 conf_set_int_int(conf, CONF_colours, index*3+0, col.red / 256);
2927 conf_set_int_int(conf, CONF_colours, index*3+1,col.green/ 256);
2928 conf_set_int_int(conf, CONF_colours, index*3+2, col.blue/ 256);
2931 } else if (use_pty_argv && !strcmp(p, "-e")) {
2932 /* This option swallows all further arguments. */
2938 pty_argv = snewn(argc+1, char *);
2940 for (i = 0; i < argc; i++)
2941 pty_argv[i] = argv[i];
2942 pty_argv[argc] = NULL;
2943 break; /* finished command-line processing */
2945 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2948 } else if (!strcmp(p, "-title")) {
2951 conf_set_str(conf, CONF_wintitle, val);
2953 } else if (!strcmp(p, "-log")) {
2957 fn = filename_from_str(val);
2958 conf_set_filename(conf, CONF_logfilename, fn);
2959 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
2962 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2964 conf_set_int(conf, CONF_stamp_utmp, 0);
2966 } else if (!strcmp(p, "-ut")) {
2968 conf_set_int(conf, CONF_stamp_utmp, 1);
2970 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2972 conf_set_int(conf, CONF_login_shell, 0);
2974 } else if (!strcmp(p, "-ls")) {
2976 conf_set_int(conf, CONF_login_shell, 1);
2978 } else if (!strcmp(p, "-nethack")) {
2980 conf_set_int(conf, CONF_nethack_keypad, 1);
2982 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2984 conf_set_int(conf, CONF_scrollbar, 0);
2986 } else if (!strcmp(p, "-sb")) {
2988 conf_set_int(conf, CONF_scrollbar, 1);
2990 } else if (!strcmp(p, "-name")) {
2994 } else if (!strcmp(p, "-xrm")) {
2996 provide_xrm_string(val);
2998 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
3002 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
3006 } else if (!strcmp(p, "-pgpfp")) {
3010 } else if(p[0] != '-' && (!do_everything ||
3011 process_nonoption_arg(p, conf,
3017 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
3024 int uxsel_input_add(int fd, int rwx) {
3026 if (rwx & 1) flags |= GDK_INPUT_READ;
3027 if (rwx & 2) flags |= GDK_INPUT_WRITE;
3028 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
3030 return gdk_input_add(fd, flags, fd_input_func, NULL);
3033 void uxsel_input_remove(int id) {
3034 gdk_input_remove(id);
3037 char *setup_fonts_ucs(struct gui_data *inst)
3039 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
3040 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
3045 fs = conf_get_fontspec(inst->conf, CONF_font);
3046 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
3047 shadowboldoffset, shadowbold);
3049 return dupprintf("unable to load font \"%s\"", fs->name);
3052 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
3053 if (shadowbold || !fs->name[0]) {
3056 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
3057 shadowboldoffset, shadowbold);
3060 unifont_destroy(fonts[0]);
3061 return dupprintf("unable to load bold font \"%s\"", fs->name);
3065 fs = conf_get_fontspec(inst->conf, CONF_widefont);
3067 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
3068 shadowboldoffset, shadowbold);
3070 for (i = 0; i < 2; i++)
3072 unifont_destroy(fonts[i]);
3073 return dupprintf("unable to load wide font \"%s\"", fs->name);
3079 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
3080 if (shadowbold || !fs->name[0]) {
3083 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
3084 shadowboldoffset, shadowbold);
3086 for (i = 0; i < 3; i++)
3088 unifont_destroy(fonts[i]);
3089 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
3094 * Now we've got past all the possible error conditions, we can
3095 * actually update our state.
3098 for (i = 0; i < 4; i++) {
3100 unifont_destroy(inst->fonts[i]);
3101 inst->fonts[i] = fonts[i];
3104 inst->font_width = inst->fonts[0]->width;
3105 inst->font_height = inst->fonts[0]->height;
3107 inst->direct_to_font = init_ucs(&inst->ucsdata,
3108 conf_get_str(inst->conf, CONF_line_codepage),
3109 conf_get_int(inst->conf, CONF_utf8_override),
3110 inst->fonts[0]->public_charset,
3111 conf_get_int(inst->conf, CONF_vtmode));
3116 void set_geom_hints(struct gui_data *inst)
3119 geom.min_width = inst->font_width + 2*inst->window_border;
3120 geom.min_height = inst->font_height + 2*inst->window_border;
3121 geom.max_width = geom.max_height = -1;
3122 geom.base_width = 2*inst->window_border;
3123 geom.base_height = 2*inst->window_border;
3124 geom.width_inc = inst->font_width;
3125 geom.height_inc = inst->font_height;
3126 geom.min_aspect = geom.max_aspect = 0;
3127 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
3128 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
3129 GDK_HINT_RESIZE_INC);
3132 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
3134 struct gui_data *inst = (struct gui_data *)data;
3135 term_clrsb(inst->term);
3138 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3140 struct gui_data *inst = (struct gui_data *)data;
3141 term_pwron(inst->term, TRUE);
3143 ldisc_echoedit_update(inst->ldisc);
3146 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3148 struct gui_data *inst = (struct gui_data *)data;
3149 term_copyall(inst->term);
3152 void special_menuitem(GtkMenuItem *item, gpointer data)
3154 struct gui_data *inst = (struct gui_data *)data;
3155 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
3159 inst->back->special(inst->backhandle, code);
3162 void about_menuitem(GtkMenuItem *item, gpointer data)
3164 struct gui_data *inst = (struct gui_data *)data;
3165 about_box(inst->window);
3168 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3170 struct gui_data *inst = (struct gui_data *)data;
3171 showeventlog(inst->eventlogstuff, inst->window);
3174 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3176 /* This maps colour indices in inst->conf to those used in inst->cols. */
3177 static const int ww[] = {
3178 256, 257, 258, 259, 260, 261,
3179 0, 8, 1, 9, 2, 10, 3, 11,
3180 4, 12, 5, 13, 6, 14, 7, 15
3182 struct gui_data *inst = (struct gui_data *)data;
3184 Conf *oldconf, *newconf;
3185 int i, j, need_size;
3187 assert(lenof(ww) == NCFGCOLOURS);
3189 if (inst->reconfiguring)
3192 inst->reconfiguring = TRUE;
3194 title = dupcat(appname, " Reconfiguration", NULL);
3196 oldconf = inst->conf;
3197 newconf = conf_copy(inst->conf);
3199 if (do_config_box(title, newconf, 1,
3200 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3201 inst->conf = newconf;
3203 /* Pass new config data to the logging module */
3204 log_reconfig(inst->logctx, inst->conf);
3206 * Flush the line discipline's edit buffer in the case
3207 * where local editing has just been disabled.
3210 ldisc_configure(inst->ldisc, inst->conf);
3211 ldisc_echoedit_update(inst->ldisc);
3213 /* Pass new config data to the terminal */
3214 term_reconfig(inst->term, inst->conf);
3215 /* Pass new config data to the back end */
3217 inst->back->reconfig(inst->backhandle, inst->conf);
3219 cache_conf_values(inst);
3222 * Just setting inst->conf is sufficient to cause colour
3223 * setting changes to appear on the next ESC]R palette
3224 * reset. But we should also check whether any colour
3225 * settings have been changed, and revert the ones that have
3226 * to the new default, on the assumption that the user is
3227 * most likely to want an immediate update.
3229 for (i = 0; i < NCFGCOLOURS; i++) {
3230 for (j = 0; j < 3; j++)
3231 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3232 conf_get_int_int(newconf, CONF_colours, i*3+j))
3235 real_palette_set(inst, ww[i],
3236 conf_get_int_int(newconf,CONF_colours,i*3+0),
3237 conf_get_int_int(newconf,CONF_colours,i*3+1),
3238 conf_get_int_int(newconf,CONF_colours,i*3+2));
3241 * If the default background has changed, we must
3242 * repaint the space in between the window border
3243 * and the text area.
3246 set_window_background(inst);
3247 draw_backing_rect(inst);
3253 * If the scrollbar needs to be shown, hidden, or moved
3254 * from one end to the other of the window, do so now.
3256 if (conf_get_int(oldconf, CONF_scrollbar) !=
3257 conf_get_int(newconf, CONF_scrollbar)) {
3258 if (conf_get_int(newconf, CONF_scrollbar))
3259 gtk_widget_show(inst->sbar);
3261 gtk_widget_hide(inst->sbar);
3263 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3264 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3265 gtk_box_reorder_child(inst->hbox, inst->sbar,
3266 conf_get_int(newconf, CONF_scrollbar_on_left)
3271 * Change the window title, if required.
3273 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3274 conf_get_str(newconf, CONF_wintitle)))
3275 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3276 set_window_titles(inst);
3279 * Redo the whole tangled fonts and Unicode mess if
3283 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3284 conf_get_fontspec(newconf, CONF_font)->name) ||
3285 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3286 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3287 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3288 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3289 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3290 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3291 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3292 conf_get_str(newconf, CONF_line_codepage)) ||
3293 conf_get_int(oldconf, CONF_utf8_override) !=
3294 conf_get_int(newconf, CONF_utf8_override) ||
3295 conf_get_int(oldconf, CONF_vtmode) !=
3296 conf_get_int(newconf, CONF_vtmode) ||
3297 conf_get_int(oldconf, CONF_shadowbold) !=
3298 conf_get_int(newconf, CONF_shadowbold) ||
3299 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3300 conf_get_int(newconf, CONF_shadowboldoffset)) {
3301 char *errmsg = setup_fonts_ucs(inst);
3304 dupprintf("Could not change fonts in terminal window: %s\n",
3306 messagebox(inst->window, "Font setup error", msgboxtext,
3307 string_width("Could not change fonts in terminal window:"),
3318 * Resize the window.
3320 if (conf_get_int(oldconf, CONF_width) !=
3321 conf_get_int(newconf, CONF_width) ||
3322 conf_get_int(oldconf, CONF_height) !=
3323 conf_get_int(newconf, CONF_height) ||
3324 conf_get_int(oldconf, CONF_window_border) !=
3325 conf_get_int(newconf, CONF_window_border) ||
3327 set_geom_hints(inst);
3328 request_resize(inst, conf_get_int(newconf, CONF_width),
3329 conf_get_int(newconf, CONF_height));
3332 * The above will have caused a call to term_size() for
3333 * us if it happened. If the user has fiddled with only
3334 * the scrollback size, the above will not have
3335 * happened and we will need an explicit term_size()
3338 if (conf_get_int(oldconf, CONF_savelines) !=
3339 conf_get_int(newconf, CONF_savelines))
3340 term_size(inst->term, inst->term->rows, inst->term->cols,
3341 conf_get_int(newconf, CONF_savelines));
3344 term_invalidate(inst->term);
3347 * We do an explicit full redraw here to ensure the window
3348 * border has been redrawn as well as the text area.
3350 gtk_widget_queue_draw(inst->area);
3357 inst->reconfiguring = FALSE;
3360 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3363 * Re-execing ourself is not an exact science under Unix. I do
3364 * the best I can by using /proc/self/exe if available and by
3365 * assuming argv[0] can be found on $PATH if not.
3367 * Note that we also have to reconstruct the elements of the
3368 * original argv which gtk swallowed, since the user wants the
3369 * new session to appear on the same X display as the old one.
3377 * Collect the arguments with which to re-exec ourself.
3379 va_start(ap, fd_to_close);
3380 n = 2; /* progname and terminating NULL */
3381 n += inst->ngtkargs;
3382 while (va_arg(ap, char *) != NULL)
3386 args = snewn(n, char *);
3387 args[0] = inst->progname;
3389 for (i = 0; i < inst->ngtkargs; i++)
3390 args[i+1] = inst->gtkargvstart[i];
3393 va_start(ap, fd_to_close);
3394 while ((args[i++] = va_arg(ap, char *)) != NULL);
3400 * Do the double fork.
3414 } else if (pid2 > 0) {
3416 * First child has successfully forked second child. My
3417 * Work Here Is Done. Note the use of _exit rather than
3418 * exit: the latter appears to cause destroy messages
3419 * to be sent to the X server. I suspect gtk uses
3426 * If we reach here, we are the second child, so we now
3427 * actually perform the exec.
3429 if (fd_to_close >= 0)
3432 execv("/proc/self/exe", args);
3433 execvp(inst->progname, args);
3440 waitpid(pid, &status, 0);
3445 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3447 struct gui_data *inst = (struct gui_data *)gdata;
3449 * For this feature we must marshal conf and (possibly) pty_argv
3450 * into a byte stream, create a pipe, and send this byte stream
3451 * to the child through the pipe.
3453 int i, ret, sersize, size;
3458 if (pipe(pipefd) < 0) {
3463 size = sersize = conf_serialised_size(inst->conf);
3464 if (use_pty_argv && pty_argv) {
3465 for (i = 0; pty_argv[i]; i++)
3466 size += strlen(pty_argv[i]) + 1;
3469 data = snewn(size, char);
3470 conf_serialise(inst->conf, data);
3471 if (use_pty_argv && pty_argv) {
3473 for (i = 0; pty_argv[i]; i++) {
3474 strcpy(data + p, pty_argv[i]);
3475 p += strlen(pty_argv[i]) + 1;
3480 sprintf(option, "---[%d,%d]", pipefd[0], size);
3481 noncloexec(pipefd[0]);
3482 fork_and_exec_self(inst, pipefd[1], option, NULL);
3486 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3489 perror("write to pipe");
3494 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
3496 int fd, i, ret, size, size_used;
3499 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3500 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3504 data = snewn(size, char);
3506 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3509 perror("read from pipe");
3511 } else if (i < size) {
3512 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3517 size_used = conf_deserialise(conf, data, size);
3518 if (use_pty_argv && size > size_used) {
3522 while (i < size && data[i]) i++;
3524 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3531 pty_argv = snewn(n+1, char *);
3537 while (i < size && data[i]) i++;
3540 pty_argv[n++] = dupstr(p);
3549 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3551 struct gui_data *inst = (struct gui_data *)data;
3553 fork_and_exec_self(inst, -1, NULL);
3556 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3558 struct gui_data *inst = (struct gui_data *)data;
3561 logevent(inst, "----- Session restarted -----");
3562 term_pwron(inst->term, FALSE);
3563 start_backend(inst);
3564 inst->exited = FALSE;
3568 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3570 struct gui_data *inst = (struct gui_data *)data;
3571 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3573 fork_and_exec_self(inst, -1, "-load", str, NULL);
3576 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3578 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3583 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3585 struct gui_data *inst = (struct gui_data *)data;
3586 struct sesslist sesslist;
3589 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3590 (GtkCallback)gtk_widget_destroy, NULL);
3592 get_sesslist(&sesslist, TRUE);
3593 /* skip sesslist.sessions[0] == Default Settings */
3594 for (i = 1; i < sesslist.nsessions; i++) {
3595 GtkWidget *menuitem =
3596 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3597 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3598 gtk_widget_show(menuitem);
3599 g_object_set_data(G_OBJECT(menuitem), "user-data",
3600 dupstr(sesslist.sessions[i]));
3601 g_signal_connect(G_OBJECT(menuitem), "activate",
3602 G_CALLBACK(saved_session_menuitem),
3604 g_signal_connect(G_OBJECT(menuitem), "destroy",
3605 G_CALLBACK(saved_session_freedata),
3608 if (sesslist.nsessions <= 1) {
3609 GtkWidget *menuitem =
3610 gtk_menu_item_new_with_label("(No sessions)");
3611 gtk_widget_set_sensitive(menuitem, FALSE);
3612 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3613 gtk_widget_show(menuitem);
3615 get_sesslist(&sesslist, FALSE); /* free up */
3618 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3622 GdkBitmap *iconmask;
3623 #if GTK_CHECK_VERSION(2,0,0)
3631 gtk_widget_realize(window);
3632 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
3633 &iconmask, NULL, (gchar **)icon[0]);
3634 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
3636 #if GTK_CHECK_VERSION(2,0,0)
3638 for (n = 0; n < n_icon; n++) {
3640 g_list_append(iconlist,
3641 gdk_pixbuf_new_from_xpm_data((const gchar **)
3644 gdk_window_set_icon_list(gtk_widget_get_window(window), iconlist);
3648 void update_specials_menu(void *frontend)
3650 struct gui_data *inst = (struct gui_data *)frontend;
3652 const struct telnet_special *specials;
3655 specials = inst->back->get_specials(inst->backhandle);
3659 /* I believe this disposes of submenus too. */
3660 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3661 (GtkCallback)gtk_widget_destroy, NULL);
3664 GtkWidget *menu = inst->specialsmenu;
3665 /* A lame "stack" for submenus that will do for now. */
3666 GtkWidget *saved_menu = NULL;
3668 for (i = 0; nesting > 0; i++) {
3669 GtkWidget *menuitem = NULL;
3670 switch (specials[i].code) {
3672 assert (nesting < 2);
3673 saved_menu = menu; /* XXX lame stacking */
3674 menu = gtk_menu_new();
3675 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3676 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3677 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3678 gtk_widget_show(menuitem);
3685 menu = saved_menu; /* XXX lame stacking */
3690 menuitem = gtk_menu_item_new();
3693 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3694 g_object_set_data(G_OBJECT(menuitem), "user-data",
3695 GINT_TO_POINTER(specials[i].code));
3696 g_signal_connect(G_OBJECT(menuitem), "activate",
3697 G_CALLBACK(special_menuitem), inst);
3701 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3702 gtk_widget_show(menuitem);
3705 gtk_widget_show(inst->specialsitem1);
3706 gtk_widget_show(inst->specialsitem2);
3708 gtk_widget_hide(inst->specialsitem1);
3709 gtk_widget_hide(inst->specialsitem2);
3713 static void start_backend(struct gui_data *inst)
3715 extern Backend *select_backend(Conf *conf);
3720 inst->back = select_backend(inst->conf);
3722 error = inst->back->init((void *)inst, &inst->backhandle,
3724 conf_get_str(inst->conf, CONF_host),
3725 conf_get_int(inst->conf, CONF_port),
3727 conf_get_int(inst->conf, CONF_tcp_nodelay),
3728 conf_get_int(inst->conf, CONF_tcp_keepalives));
3731 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3732 conf_get_str(inst->conf, CONF_host), error);
3733 inst->exited = TRUE;
3734 fatal_message_box(inst->window, msg);
3739 s = conf_get_str(inst->conf, CONF_wintitle);
3741 set_title_and_icon(inst, s, s);
3743 char *title = make_default_wintitle(realhost);
3744 set_title_and_icon(inst, title, title);
3749 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3751 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3754 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
3757 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3760 int pt_main(int argc, char **argv)
3762 extern int cfgbox(Conf *conf);
3763 struct gui_data *inst;
3765 setlocale(LC_CTYPE, "");
3768 * Create an instance structure and initialise to zeroes
3770 inst = snew(struct gui_data);
3771 memset(inst, 0, sizeof(*inst));
3772 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3773 inst->busy_status = BUSY_NOT;
3774 inst->conf = conf_new();
3775 inst->wintitle = inst->icontitle = NULL;
3776 inst->quit_fn_scheduled = FALSE;
3777 inst->idle_fn_scheduled = FALSE;
3779 /* defer any child exit handling until we're ready to deal with
3781 block_signal(SIGCHLD, 1);
3783 inst->progname = argv[0];
3785 * Copy the original argv before letting gtk_init fiddle with
3786 * it. It will be required later.
3790 inst->gtkargvstart = snewn(argc-1, char *);
3791 for (i = 1; i < argc; i++)
3792 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3794 gtk_init(&argc, &argv);
3795 inst->ngtkargs = oldargc - argc;
3798 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3799 read_dupsession_data(inst, inst->conf, argv[1]);
3800 /* Splatter this argument so it doesn't clutter a ps listing */
3801 smemclr(argv[1], strlen(argv[1]));
3803 /* By default, we bring up the config dialog, rather than launching
3804 * a session. This gets set to TRUE if something happens to change
3805 * that (e.g., a hostname is specified on the command-line). */
3806 int allow_launch = FALSE;
3807 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
3808 exit(1); /* pre-defaults pass to get -class */
3809 do_defaults(NULL, inst->conf);
3810 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
3811 exit(1); /* post-defaults, do everything */
3813 cmdline_run_saved(inst->conf);
3816 allow_launch = TRUE;
3818 if ((!allow_launch || !conf_launchable(inst->conf)) &&
3819 !cfgbox(inst->conf))
3820 exit(0); /* config box hit Cancel */
3823 if (!compound_text_atom)
3824 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3825 if (!utf8_string_atom)
3826 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3828 inst->area = gtk_drawing_area_new();
3830 #if GTK_CHECK_VERSION(2,0,0)
3831 inst->imc = gtk_im_multicontext_new();
3835 char *errmsg = setup_fonts_ucs(inst);
3837 fprintf(stderr, "%s: %s\n", appname, errmsg);
3843 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3845 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
3847 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
3848 winclass, winclass);
3852 * Set up the colour map.
3854 palette_reset(inst);
3856 inst->width = conf_get_int(inst->conf, CONF_width);
3857 inst->height = conf_get_int(inst->conf, CONF_height);
3858 cache_conf_values(inst);
3861 int w = inst->font_width * inst->width + 2*inst->window_border;
3862 int h = inst->font_height * inst->height + 2*inst->window_border;
3863 #if GTK_CHECK_VERSION(2,0,0)
3864 gtk_widget_set_size_request(inst->area, w, h);
3866 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
3869 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3870 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3871 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3873 * We always create the scrollbar; it remains invisible if
3874 * unwanted, so we can pop it up quickly if it suddenly becomes
3877 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
3878 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3879 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3880 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
3881 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3883 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3885 set_geom_hints(inst);
3887 gtk_widget_show(inst->area);
3888 if (conf_get_int(inst->conf, CONF_scrollbar))
3889 gtk_widget_show(inst->sbar);
3891 gtk_widget_hide(inst->sbar);
3892 gtk_widget_show(GTK_WIDGET(inst->hbox));
3894 #if GTK_CHECK_VERSION(2,0,0)
3895 if (inst->geometry) {
3896 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
3900 int x = inst->xpos, y = inst->ypos;
3902 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3903 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3904 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3905 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3906 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3910 g_signal_connect(G_OBJECT(inst->window), "destroy",
3911 G_CALLBACK(destroy), inst);
3912 g_signal_connect(G_OBJECT(inst->window), "delete_event",
3913 G_CALLBACK(delete_window), inst);
3914 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
3915 G_CALLBACK(key_event), inst);
3916 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
3917 G_CALLBACK(key_event), inst);
3918 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
3919 G_CALLBACK(focus_event), inst);
3920 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
3921 G_CALLBACK(focus_event), inst);
3922 g_signal_connect(G_OBJECT(inst->area), "configure_event",
3923 G_CALLBACK(configure_area), inst);
3924 g_signal_connect(G_OBJECT(inst->area), "expose_event",
3925 G_CALLBACK(expose_area), inst);
3926 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
3927 G_CALLBACK(button_event), inst);
3928 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
3929 G_CALLBACK(button_event), inst);
3930 #if GTK_CHECK_VERSION(2,0,0)
3931 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
3932 G_CALLBACK(scroll_event), inst);
3934 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
3935 G_CALLBACK(motion_event), inst);
3936 g_signal_connect(G_OBJECT(inst->area), "selection_received",
3937 G_CALLBACK(selection_received), inst);
3938 g_signal_connect(G_OBJECT(inst->area), "selection_get",
3939 G_CALLBACK(selection_get), inst);
3940 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
3941 G_CALLBACK(selection_clear), inst);
3942 #if GTK_CHECK_VERSION(2,0,0)
3943 g_signal_connect(G_OBJECT(inst->imc), "commit",
3944 G_CALLBACK(input_method_commit_event), inst);
3946 if (conf_get_int(inst->conf, CONF_scrollbar))
3947 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
3948 G_CALLBACK(scrollbar_moved), inst);
3949 gtk_widget_add_events(GTK_WIDGET(inst->area),
3950 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3951 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3952 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3955 extern const char *const *const main_icon[];
3956 extern const int n_main_icon;
3957 set_window_icon(inst->window, main_icon, n_main_icon);
3960 gtk_widget_show(inst->window);
3962 set_window_background(inst);
3965 * Set up the Ctrl+rightclick context menu.
3968 GtkWidget *menuitem;
3970 extern const int use_event_log, new_session, saved_sessions;
3972 inst->menu = gtk_menu_new();
3974 #define MKMENUITEM(title, func) do \
3976 menuitem = gtk_menu_item_new_with_label(title); \
3977 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3978 gtk_widget_show(menuitem); \
3979 g_signal_connect(G_OBJECT(menuitem), "activate", \
3980 G_CALLBACK(func), inst); \
3983 #define MKSUBMENU(title) do \
3985 menuitem = gtk_menu_item_new_with_label(title); \
3986 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3987 gtk_widget_show(menuitem); \
3990 #define MKSEP() do \
3992 menuitem = gtk_menu_item_new(); \
3993 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3994 gtk_widget_show(menuitem); \
3998 MKMENUITEM("New Session...", new_session_menuitem);
3999 MKMENUITEM("Restart Session", restart_session_menuitem);
4000 inst->restartitem = menuitem;
4001 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4002 MKMENUITEM("Duplicate Session", dup_session_menuitem);
4003 if (saved_sessions) {
4004 inst->sessionsmenu = gtk_menu_new();
4005 /* sessionsmenu will be updated when it's invoked */
4006 /* XXX is this the right way to do dynamic menus in Gtk? */
4007 MKMENUITEM("Saved Sessions", update_savedsess_menu);
4008 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
4009 inst->sessionsmenu);
4012 MKMENUITEM("Change Settings...", change_settings_menuitem);
4015 MKMENUITEM("Event Log", event_log_menuitem);
4016 MKSUBMENU("Special Commands");
4017 inst->specialsmenu = gtk_menu_new();
4018 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
4019 inst->specialsitem1 = menuitem;
4021 inst->specialsitem2 = menuitem;
4022 gtk_widget_hide(inst->specialsitem1);
4023 gtk_widget_hide(inst->specialsitem2);
4024 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
4025 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
4026 MKMENUITEM("Copy All", copy_all_menuitem);
4028 s = dupcat("About ", appname, NULL);
4029 MKMENUITEM(s, about_menuitem);
4036 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
4037 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
4038 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
4039 inst->blankcursor = make_mouse_ptr(inst, -1);
4040 make_mouse_ptr(inst, -2); /* clean up cursor font */
4041 inst->currcursor = inst->textcursor;
4042 show_mouseptr(inst, 1);
4044 inst->eventlogstuff = eventlogstuff_new();
4046 request_callback_notifications(notify_toplevel_callback, inst);
4048 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
4049 inst->logctx = log_init(inst, inst->conf);
4050 term_provide_logctx(inst->term, inst->logctx);
4054 term_size(inst->term, inst->height, inst->width,
4055 conf_get_int(inst->conf, CONF_savelines));
4057 start_backend(inst);
4059 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
4061 /* now we're reday to deal with the child exit handler being
4063 block_signal(SIGCHLD, 0);
4066 * Block SIGPIPE: if we attempt Duplicate Session or similar
4067 * and it falls over in some way, we certainly don't want
4068 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
4069 * this _after_ (at least pterm) forks off its child process,
4070 * since the child wants SIGPIPE handled in the usual way.
4072 block_signal(SIGPIPE, 1);
4074 inst->exited = FALSE;