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>
26 #if GTK_CHECK_VERSION(2,0,0)
27 #include <gtk/gtkimmodule.h>
30 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
32 #define MAY_REFER_TO_GTK_IN_HEADERS
36 #include "gtkcompat.h"
43 #include <X11/Xutil.h>
44 #include <X11/Xatom.h>
47 #define CAT2(x,y) x ## y
48 #define CAT(x,y) CAT2(x,y)
49 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
51 #if GTK_CHECK_VERSION(2,0,0)
52 ASSERT(sizeof(long) <= sizeof(gsize));
53 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
54 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
56 ASSERT(sizeof(long) <= sizeof(gpointer));
57 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
58 #define GPOINTER_TO_LONG(p) ((long)(p))
61 /* Colours come in two flavours: configurable, and xterm-extended. */
62 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
63 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
65 GdkAtom compound_text_atom, utf8_string_atom;
67 extern char **pty_argv; /* declared in pty.c */
68 extern int use_pty_argv;
71 * Timers are global across all sessions (even if we were handling
72 * multiple sessions, which we aren't), so the current timer ID is
75 static guint timer_id = 0;
77 struct clipboard_data_instance;
80 GtkWidget *window, *area, *sbar;
82 GtkAdjustment *sbar_adjust;
83 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
85 GtkWidget *sessionsmenu;
86 #ifndef NO_BACKING_PIXMAPS
89 #if GTK_CHECK_VERSION(2,0,0)
92 unifont *fonts[4]; /* normal, bold, wide, widebold */
93 #if GTK_CHECK_VERSION(2,0,0)
96 int xpos, ypos, gotpos, gravity;
98 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
99 GdkColor cols[NALLCOLOURS];
100 #if !GTK_CHECK_VERSION(3,0,0)
104 wchar_t *pastein_data;
105 int pastein_data_len;
106 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
107 GtkClipboard *clipboard;
108 struct clipboard_data_instance *current_cdi;
110 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
111 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
113 int font_width, font_height;
116 int mouseptr_visible;
118 guint toplevel_callback_idle_id;
119 int idle_fn_scheduled, quit_fn_scheduled;
124 int master_fd, master_func_id;
131 struct unicode_data ucsdata;
134 char *progname, **gtkargvstart;
136 guint32 input_event_time; /* Timestamp of the most recent input event. */
138 /* Cached things out of conf that we refer to a lot */
146 static void cache_conf_values(struct gui_data *inst)
148 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
149 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
150 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
151 #ifdef OSX_META_KEY_CONFIG
152 inst->meta_mod_mask = 0;
153 if (conf_get_int(inst->conf, CONF_osx_option_meta))
154 inst->meta_mod_mask |= GDK_MOD1_MASK;
155 if (conf_get_int(inst->conf, CONF_osx_command_meta))
156 inst->meta_mod_mask |= GDK_MOD2_MASK;
158 inst->meta_mod_mask = GDK_MOD1_MASK;
163 struct gui_data *inst;
164 unifont_drawctx uctx;
167 static int send_raw_mouse;
169 static const char *app_name = "pterm";
171 static void start_backend(struct gui_data *inst);
172 static void exit_callback(void *vinst);
174 char *x_get_default(const char *key)
176 #ifndef NOT_X_WINDOWS
177 return XGetDefault(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
184 void connection_fatal(void *frontend, const char *p, ...)
186 struct gui_data *inst = (struct gui_data *)frontend;
191 msg = dupvprintf(p, ap);
193 fatal_message_box(inst->window, msg);
196 queue_toplevel_callback(exit_callback, inst);
200 * Default settings that are specific to pterm.
202 FontSpec *platform_default_fontspec(const char *name)
204 if (!strcmp(name, "Font"))
205 return fontspec_new(DEFAULT_GTK_FONT);
207 return fontspec_new("");
210 Filename *platform_default_filename(const char *name)
212 if (!strcmp(name, "LogFileName"))
213 return filename_from_str("putty.log");
215 return filename_from_str("");
218 char *platform_default_s(const char *name)
220 if (!strcmp(name, "SerialLine"))
221 return dupstr("/dev/ttyS0");
225 int platform_default_i(const char *name, int def)
227 if (!strcmp(name, "CloseOnExit"))
228 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
229 if (!strcmp(name, "WinNameAlways"))
230 return 0; /* X natively supports icon titles, so use 'em by default */
234 /* Dummy routine, only required in plink. */
235 void frontend_echoedit_update(void *frontend, int echo, int edit)
239 char *get_ttymode(void *frontend, const char *mode)
241 struct gui_data *inst = (struct gui_data *)frontend;
242 return term_get_ttymode(inst->term, mode);
245 int from_backend(void *frontend, int is_stderr, const char *data, int len)
247 struct gui_data *inst = (struct gui_data *)frontend;
248 return term_data(inst->term, is_stderr, data, len);
251 int from_backend_untrusted(void *frontend, const char *data, int len)
253 struct gui_data *inst = (struct gui_data *)frontend;
254 return term_data_untrusted(inst->term, data, len);
257 int from_backend_eof(void *frontend)
259 return TRUE; /* do respond to incoming EOF with outgoing */
262 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
264 struct gui_data *inst = (struct gui_data *)p->frontend;
266 ret = cmdline_get_passwd_input(p, in, inlen);
268 ret = term_get_userpass_input(inst->term, p, in, inlen);
272 void logevent(void *frontend, const char *string)
274 struct gui_data *inst = (struct gui_data *)frontend;
276 log_eventlog(inst->logctx, string);
278 logevent_dlg(inst->eventlogstuff, string);
281 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
283 struct gui_data *inst = (struct gui_data *)frontend;
286 return inst->font_height;
288 return inst->font_width;
292 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
293 * into a cooked one (SELECT, EXTEND, PASTE).
295 * In Unix, this is not configurable; the X button arrangement is
296 * rock-solid across all applications, everyone has a three-button
297 * mouse or a means of faking it, and there is no need to switch
298 * buttons around at all.
300 static Mouse_Button translate_button(Mouse_Button button)
302 /* struct gui_data *inst = (struct gui_data *)frontend; */
304 if (button == MBT_LEFT)
306 if (button == MBT_MIDDLE)
308 if (button == MBT_RIGHT)
310 return 0; /* shouldn't happen */
314 * Return the top-level GtkWindow associated with a particular
315 * front end instance.
317 void *get_window(void *frontend)
319 struct gui_data *inst = (struct gui_data *)frontend;
324 * Minimise or restore the window in response to a server-side
327 void set_iconic(void *frontend, int iconic)
330 * GTK 1.2 doesn't know how to do this.
332 #if GTK_CHECK_VERSION(2,0,0)
333 struct gui_data *inst = (struct gui_data *)frontend;
335 gtk_window_iconify(GTK_WINDOW(inst->window));
337 gtk_window_deiconify(GTK_WINDOW(inst->window));
342 * Move the window in response to a server-side request.
344 void move_window(void *frontend, int x, int y)
346 struct gui_data *inst = (struct gui_data *)frontend;
348 * I assume that when the GTK version of this call is available
349 * we should use it. Not sure how it differs from the GDK one,
352 #if GTK_CHECK_VERSION(2,0,0)
353 gtk_window_move(GTK_WINDOW(inst->window), x, y);
355 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
360 * Move the window to the top or bottom of the z-order in response
361 * to a server-side request.
363 void set_zorder(void *frontend, int top)
365 struct gui_data *inst = (struct gui_data *)frontend;
367 gdk_window_raise(gtk_widget_get_window(inst->window));
369 gdk_window_lower(gtk_widget_get_window(inst->window));
373 * Refresh the window in response to a server-side request.
375 void refresh_window(void *frontend)
377 struct gui_data *inst = (struct gui_data *)frontend;
378 term_invalidate(inst->term);
382 * Maximise or restore the window in response to a server-side
385 void set_zoomed(void *frontend, int zoomed)
388 * GTK 1.2 doesn't know how to do this.
390 #if GTK_CHECK_VERSION(2,0,0)
391 struct gui_data *inst = (struct gui_data *)frontend;
393 gtk_window_maximize(GTK_WINDOW(inst->window));
395 gtk_window_unmaximize(GTK_WINDOW(inst->window));
400 * Report whether the window is iconic, for terminal reports.
402 int is_iconic(void *frontend)
404 struct gui_data *inst = (struct gui_data *)frontend;
405 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
409 * Report the window's position, for terminal reports.
411 void get_window_pos(void *frontend, int *x, int *y)
413 struct gui_data *inst = (struct gui_data *)frontend;
415 * I assume that when the GTK version of this call is available
416 * we should use it. Not sure how it differs from the GDK one,
419 #if GTK_CHECK_VERSION(2,0,0)
420 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
422 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
427 * Report the window's pixel size, for terminal reports.
429 void get_window_pixels(void *frontend, int *x, int *y)
431 struct gui_data *inst = (struct gui_data *)frontend;
433 * I assume that when the GTK version of this call is available
434 * we should use it. Not sure how it differs from the GDK one,
437 #if GTK_CHECK_VERSION(2,0,0)
438 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
440 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
445 * Return the window or icon title.
447 char *get_window_title(void *frontend, int icon)
449 struct gui_data *inst = (struct gui_data *)frontend;
450 return icon ? inst->icontitle : inst->wintitle;
453 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
455 struct gui_data *inst = (struct gui_data *)data;
456 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
457 if (!reallyclose(inst))
463 static void update_mouseptr(struct gui_data *inst)
465 switch (inst->busy_status) {
467 if (!inst->mouseptr_visible) {
468 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
470 } else if (send_raw_mouse) {
471 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
474 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
478 case BUSY_WAITING: /* XXX can we do better? */
480 /* We always display these cursors. */
481 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
489 static void show_mouseptr(struct gui_data *inst, int show)
491 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
493 inst->mouseptr_visible = show;
494 update_mouseptr(inst);
497 static void draw_backing_rect(struct gui_data *inst);
499 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
501 struct gui_data *inst = (struct gui_data *)data;
502 int w, h, need_size = 0;
505 * See if the terminal size has changed, in which case we must
506 * let the terminal know.
508 w = (event->width - 2*inst->window_border) / inst->font_width;
509 h = (event->height - 2*inst->window_border) / inst->font_height;
510 if (w != inst->width || h != inst->height) {
513 conf_set_int(inst->conf, CONF_width, inst->width);
514 conf_set_int(inst->conf, CONF_height, inst->height);
518 #ifndef NO_BACKING_PIXMAPS
520 gdk_pixmap_unref(inst->pixmap);
524 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
525 (w * inst->font_width + 2*inst->window_border),
526 (h * inst->font_height + 2*inst->window_border), -1);
529 draw_backing_rect(inst);
531 if (need_size && inst->term) {
532 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
536 term_invalidate(inst->term);
538 #if GTK_CHECK_VERSION(2,0,0)
539 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
545 #ifdef DRAW_TEXT_CAIRO
546 static void cairo_setup_dctx(struct draw_ctx *dctx)
548 cairo_get_matrix(dctx->uctx.u.cairo.cr,
549 &dctx->uctx.u.cairo.origmatrix);
550 cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
551 cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
552 cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
553 /* This antialiasing setting appears to be ignored for Pango
554 * font rendering but honoured for stroking and filling paths;
555 * I don't quite understand the logic of that, but I won't
556 * complain since it's exactly what I happen to want */
557 cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
561 #if GTK_CHECK_VERSION(3,0,0)
562 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
564 struct gui_data *inst = (struct gui_data *)data;
567 struct draw_ctx adctx, *dctx = &adctx;
568 GdkRectangle dirtyrect;
571 dctx->uctx.type = DRAWTYPE_CAIRO;
572 dctx->uctx.u.cairo.widget = widget;
573 dctx->uctx.u.cairo.cr = cr;
574 cairo_setup_dctx(dctx);
576 gdk_cairo_get_clip_rectangle(cr, &dirtyrect);
579 * As in window.c, we clear the 'immediately' flag in the
580 * term_paint() call if the terminal has an update pending, in
581 * case we're constrained within this event to only draw on
582 * the exposed rectangle of the window. (Because if the whole
583 * of a character cell needs a redraw due to a terminal
584 * contents change, the last thing we want is to give it a
585 * _partial_ redraw here due to system-imposed clipping, and
586 * then have the next main terminal update believe it's been
589 * I don't actually know if GTK draw events will constrain us
590 * in this way, but it's best to be careful...
592 term_paint(inst->term, dctx,
593 (dirtyrect.x - inst->window_border) / inst->font_width,
594 (dirtyrect.y - inst->window_border) / inst->font_height,
595 (dirtyrect.x + dirtyrect.width -
596 inst->window_border) / inst->font_width,
597 (dirtyrect.y + dirtyrect.height -
598 inst->window_border) / inst->font_height,
599 !inst->term->window_update_pending);
605 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
607 struct gui_data *inst = (struct gui_data *)data;
609 #ifndef NO_BACKING_PIXMAPS
611 * Pass the exposed rectangle to terminal.c, which will call us
612 * back to do the actual painting.
615 gdk_draw_pixmap(gtk_widget_get_window(widget),
616 (gtk_widget_get_style(widget)->fg_gc
617 [gtk_widget_get_state(widget)]),
619 event->area.x, event->area.y,
620 event->area.x, event->area.y,
621 event->area.width, event->area.height);
625 Context ctx = get_ctx(inst);
626 term_paint(inst->term, ctx,
627 (event->area.x - inst->window_border) / inst->font_width,
628 (event->area.y - inst->window_border) / inst->font_height,
629 (event->area.x + event->area.width -
630 inst->window_border) / inst->font_width,
631 (event->area.y + event->area.height -
632 inst->window_border) / inst->font_height,
633 !inst->term->window_update_pending);
642 #define KEY_PRESSED(k) \
643 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
645 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
647 struct gui_data *inst = (struct gui_data *)data;
649 wchar_t ucsoutput[2];
650 int ucsval, start, end, special, output_charset, use_ucsoutput;
651 int nethack_mode, app_keypad_mode;
653 /* Remember the timestamp. */
654 inst->input_event_time = event->time;
656 /* By default, nothing is generated. */
658 special = use_ucsoutput = FALSE;
659 output_charset = CS_ISO8859_1;
661 #ifdef KEY_EVENT_DIAGNOSTICS
663 * Condition this mess in if you want to debug keyboard events as
664 * they come in to this function (e.g. because some particular
665 * port of GDK is producing an unexpected arrangement of
668 #define TRY(val, prefix, string) \
669 if ((val) == prefix ## string) printf("%s", #string); else
670 #define GIVE_UP(val) \
671 printf("%d", (int)(val))
672 #define TRY_MASK(val, prefix, string, suffix) \
673 if ((val) & prefix ## string ## suffix) { \
674 (val) &= ~ prefix ## string ## suffix; \
675 printf("%s", #string); \
676 if (val) printf("|"); \
678 #define GIVE_UP_MASK(val) \
681 if ((val)) printf("%d", (int)(val)); \
684 printf("key_event: type=");
685 TRY(event->type, GDK_KEY_, PRESS)
686 TRY(event->type, GDK_KEY_, RELEASE)
687 GIVE_UP(event->type);
689 TRY(event->keyval, GDK_KEY_, Alt_L)
690 TRY(event->keyval, GDK_KEY_, Alt_R)
691 TRY(event->keyval, GDK_KEY_, BackSpace)
692 TRY(event->keyval, GDK_KEY_, Begin)
693 TRY(event->keyval, GDK_KEY_, Break)
694 TRY(event->keyval, GDK_KEY_, Delete)
695 TRY(event->keyval, GDK_KEY_, Down)
696 TRY(event->keyval, GDK_KEY_, End)
697 TRY(event->keyval, GDK_KEY_, Escape)
698 TRY(event->keyval, GDK_KEY_, F10)
699 TRY(event->keyval, GDK_KEY_, F11)
700 TRY(event->keyval, GDK_KEY_, F12)
701 TRY(event->keyval, GDK_KEY_, F13)
702 TRY(event->keyval, GDK_KEY_, F14)
703 TRY(event->keyval, GDK_KEY_, F15)
704 TRY(event->keyval, GDK_KEY_, F16)
705 TRY(event->keyval, GDK_KEY_, F17)
706 TRY(event->keyval, GDK_KEY_, F18)
707 TRY(event->keyval, GDK_KEY_, F19)
708 TRY(event->keyval, GDK_KEY_, F1)
709 TRY(event->keyval, GDK_KEY_, F20)
710 TRY(event->keyval, GDK_KEY_, F2)
711 TRY(event->keyval, GDK_KEY_, F3)
712 TRY(event->keyval, GDK_KEY_, F4)
713 TRY(event->keyval, GDK_KEY_, F5)
714 TRY(event->keyval, GDK_KEY_, F6)
715 TRY(event->keyval, GDK_KEY_, F7)
716 TRY(event->keyval, GDK_KEY_, F8)
717 TRY(event->keyval, GDK_KEY_, F9)
718 TRY(event->keyval, GDK_KEY_, Home)
719 TRY(event->keyval, GDK_KEY_, Insert)
720 TRY(event->keyval, GDK_KEY_, ISO_Left_Tab)
721 TRY(event->keyval, GDK_KEY_, KP_0)
722 TRY(event->keyval, GDK_KEY_, KP_1)
723 TRY(event->keyval, GDK_KEY_, KP_2)
724 TRY(event->keyval, GDK_KEY_, KP_3)
725 TRY(event->keyval, GDK_KEY_, KP_4)
726 TRY(event->keyval, GDK_KEY_, KP_5)
727 TRY(event->keyval, GDK_KEY_, KP_6)
728 TRY(event->keyval, GDK_KEY_, KP_7)
729 TRY(event->keyval, GDK_KEY_, KP_8)
730 TRY(event->keyval, GDK_KEY_, KP_9)
731 TRY(event->keyval, GDK_KEY_, KP_Add)
732 TRY(event->keyval, GDK_KEY_, KP_Begin)
733 TRY(event->keyval, GDK_KEY_, KP_Decimal)
734 TRY(event->keyval, GDK_KEY_, KP_Delete)
735 TRY(event->keyval, GDK_KEY_, KP_Divide)
736 TRY(event->keyval, GDK_KEY_, KP_Down)
737 TRY(event->keyval, GDK_KEY_, KP_End)
738 TRY(event->keyval, GDK_KEY_, KP_Enter)
739 TRY(event->keyval, GDK_KEY_, KP_Home)
740 TRY(event->keyval, GDK_KEY_, KP_Insert)
741 TRY(event->keyval, GDK_KEY_, KP_Left)
742 TRY(event->keyval, GDK_KEY_, KP_Multiply)
743 TRY(event->keyval, GDK_KEY_, KP_Page_Down)
744 TRY(event->keyval, GDK_KEY_, KP_Page_Up)
745 TRY(event->keyval, GDK_KEY_, KP_Right)
746 TRY(event->keyval, GDK_KEY_, KP_Subtract)
747 TRY(event->keyval, GDK_KEY_, KP_Up)
748 TRY(event->keyval, GDK_KEY_, Left)
749 TRY(event->keyval, GDK_KEY_, Meta_L)
750 TRY(event->keyval, GDK_KEY_, Meta_R)
751 TRY(event->keyval, GDK_KEY_, Num_Lock)
752 TRY(event->keyval, GDK_KEY_, Page_Down)
753 TRY(event->keyval, GDK_KEY_, Page_Up)
754 TRY(event->keyval, GDK_KEY_, Return)
755 TRY(event->keyval, GDK_KEY_, Right)
756 TRY(event->keyval, GDK_KEY_, Tab)
757 TRY(event->keyval, GDK_KEY_, Up)
758 TRY(event->keyval, GDK_KEY_, Shift_L)
759 TRY(event->keyval, GDK_KEY_, Shift_R)
760 TRY(event->keyval, GDK_KEY_, Control_L)
761 TRY(event->keyval, GDK_KEY_, Control_R)
762 TRY(event->keyval, GDK_KEY_, Caps_Lock)
763 TRY(event->keyval, GDK_KEY_, Shift_Lock)
764 TRY(event->keyval, GDK_KEY_, Super_L)
765 TRY(event->keyval, GDK_KEY_, Super_R)
766 TRY(event->keyval, GDK_KEY_, Hyper_L)
767 TRY(event->keyval, GDK_KEY_, Hyper_R)
768 GIVE_UP(event->keyval);
771 int val = event->state;
772 TRY_MASK(val, GDK_, SHIFT, _MASK)
773 TRY_MASK(val, GDK_, LOCK, _MASK)
774 TRY_MASK(val, GDK_, CONTROL, _MASK)
775 TRY_MASK(val, GDK_, MOD1, _MASK)
776 TRY_MASK(val, GDK_, MOD2, _MASK)
777 TRY_MASK(val, GDK_, MOD3, _MASK)
778 TRY_MASK(val, GDK_, MOD4, _MASK)
779 TRY_MASK(val, GDK_, MOD5, _MASK)
780 TRY_MASK(val, GDK_, SUPER, _MASK)
781 TRY_MASK(val, GDK_, HYPER, _MASK)
782 TRY_MASK(val, GDK_, META, _MASK)
785 printf(" hardware_keycode=%d is_modifier=%s\n",
786 (int)event->hardware_keycode, event->is_modifier ? "TRUE" : "FALSE");
787 #endif /* KEY_EVENT_DIAGNOSTICS */
790 * If Alt is being released after typing an Alt+numberpad
791 * sequence, we should generate the code that was typed.
793 * Note that we only do this if more than one key was actually
794 * pressed - I don't think Alt+NumPad4 should be ^D or that
795 * Alt+NumPad3 should be ^C, for example. There's no serious
796 * inconvenience in having to type a zero before a single-digit
799 if (event->type == GDK_KEY_RELEASE) {
800 if ((event->keyval == GDK_KEY_Meta_L ||
801 event->keyval == GDK_KEY_Meta_R ||
802 event->keyval == GDK_KEY_Alt_L ||
803 event->keyval == GDK_KEY_Alt_R) &&
804 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
806 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
809 * FIXME: we might usefully try to do something clever here
810 * about interpreting the generated key code in a way that's
811 * appropriate to the line code page.
813 output[0] = inst->alt_keycode;
817 #if GTK_CHECK_VERSION(2,0,0)
818 if (gtk_im_context_filter_keypress(inst->imc, event))
823 if (event->type == GDK_KEY_PRESS) {
827 printf("keypress: keyval = %04x, state = %08x; string =",
828 event->keyval, event->state);
829 for (i = 0; event->string[i]; i++)
830 printf(" %02x", (unsigned char) event->string[i]);
836 * NYI: Compose key (!!! requires Unicode faff before even trying)
840 * If Alt has just been pressed, we start potentially
841 * accumulating an Alt+numberpad code. We do this by
842 * setting alt_keycode to -1 (nothing yet but plausible).
844 if ((event->keyval == GDK_KEY_Meta_L ||
845 event->keyval == GDK_KEY_Meta_R ||
846 event->keyval == GDK_KEY_Alt_L ||
847 event->keyval == GDK_KEY_Alt_R)) {
848 inst->alt_keycode = -1;
849 inst->alt_digits = 0;
850 goto done; /* this generates nothing else */
854 * If we're seeing a numberpad key press with Meta down,
855 * consider adding it to alt_keycode if that's sensible.
856 * Anything _else_ with Meta down cancels any possibility
857 * of an ALT keycode: we set alt_keycode to -2.
859 if ((event->state & inst->meta_mod_mask) && inst->alt_keycode != -2) {
861 switch (event->keyval) {
862 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
863 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
864 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
865 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
866 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
867 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
868 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
869 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
870 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
871 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
874 inst->alt_keycode = -2; /* it's invalid */
877 printf("Adding digit %d to keycode %d", digit,
880 if (inst->alt_keycode == -1)
881 inst->alt_keycode = digit; /* one-digit code */
883 inst->alt_keycode = inst->alt_keycode * 10 + digit;
886 printf(" gives new code %d\n", inst->alt_keycode);
888 /* Having used this digit, we now do nothing more with it. */
894 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
897 if (event->keyval == GDK_KEY_Page_Up &&
898 (event->state & GDK_SHIFT_MASK)) {
899 term_scroll(inst->term, 0, -inst->height/2);
902 if (event->keyval == GDK_KEY_Page_Up &&
903 (event->state & GDK_CONTROL_MASK)) {
904 term_scroll(inst->term, 0, -1);
907 if (event->keyval == GDK_KEY_Page_Down &&
908 (event->state & GDK_SHIFT_MASK)) {
909 term_scroll(inst->term, 0, +inst->height/2);
912 if (event->keyval == GDK_KEY_Page_Down &&
913 (event->state & GDK_CONTROL_MASK)) {
914 term_scroll(inst->term, 0, +1);
919 * Neither does Shift-Ins.
921 if (event->keyval == GDK_KEY_Insert &&
922 (event->state & GDK_SHIFT_MASK)) {
928 use_ucsoutput = FALSE;
930 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
931 app_keypad_mode = (inst->term->app_keypad_keys &&
932 !conf_get_int(inst->conf, CONF_no_applic_k));
934 /* ALT+things gives leading Escape. */
936 #if !GTK_CHECK_VERSION(2,0,0)
938 * In vanilla X, and hence also GDK 1.2, the string received
939 * as part of a keyboard event is assumed to be in
940 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
941 * but it's true: see the man page for XLookupString(3) for
944 output_charset = CS_ISO8859_1;
945 strncpy(output+1, event->string, lenof(output)-1);
948 * Most things can now be passed to
949 * gtk_im_context_filter_keypress without breaking anything
950 * below this point. An exception is the numeric keypad if
951 * we're in Nethack or application mode: the IM will eat
952 * numeric keypad presses if Num Lock is on, but we don't want
955 if (app_keypad_mode &&
956 (event->keyval == GDK_KEY_Num_Lock ||
957 event->keyval == GDK_KEY_KP_Divide ||
958 event->keyval == GDK_KEY_KP_Multiply ||
959 event->keyval == GDK_KEY_KP_Subtract ||
960 event->keyval == GDK_KEY_KP_Add ||
961 event->keyval == GDK_KEY_KP_Enter ||
962 event->keyval == GDK_KEY_KP_0 ||
963 event->keyval == GDK_KEY_KP_Insert ||
964 event->keyval == GDK_KEY_KP_1 ||
965 event->keyval == GDK_KEY_KP_End ||
966 event->keyval == GDK_KEY_KP_2 ||
967 event->keyval == GDK_KEY_KP_Down ||
968 event->keyval == GDK_KEY_KP_3 ||
969 event->keyval == GDK_KEY_KP_Page_Down ||
970 event->keyval == GDK_KEY_KP_4 ||
971 event->keyval == GDK_KEY_KP_Left ||
972 event->keyval == GDK_KEY_KP_5 ||
973 event->keyval == GDK_KEY_KP_Begin ||
974 event->keyval == GDK_KEY_KP_6 ||
975 event->keyval == GDK_KEY_KP_Right ||
976 event->keyval == GDK_KEY_KP_7 ||
977 event->keyval == GDK_KEY_KP_Home ||
978 event->keyval == GDK_KEY_KP_8 ||
979 event->keyval == GDK_KEY_KP_Up ||
980 event->keyval == GDK_KEY_KP_9 ||
981 event->keyval == GDK_KEY_KP_Page_Up ||
982 event->keyval == GDK_KEY_KP_Decimal ||
983 event->keyval == GDK_KEY_KP_Delete)) {
984 /* app keypad; do nothing */
985 } else if (nethack_mode &&
986 (event->keyval == GDK_KEY_KP_1 ||
987 event->keyval == GDK_KEY_KP_End ||
988 event->keyval == GDK_KEY_KP_2 ||
989 event->keyval == GDK_KEY_KP_Down ||
990 event->keyval == GDK_KEY_KP_3 ||
991 event->keyval == GDK_KEY_KP_Page_Down ||
992 event->keyval == GDK_KEY_KP_4 ||
993 event->keyval == GDK_KEY_KP_Left ||
994 event->keyval == GDK_KEY_KP_5 ||
995 event->keyval == GDK_KEY_KP_Begin ||
996 event->keyval == GDK_KEY_KP_6 ||
997 event->keyval == GDK_KEY_KP_Right ||
998 event->keyval == GDK_KEY_KP_7 ||
999 event->keyval == GDK_KEY_KP_Home ||
1000 event->keyval == GDK_KEY_KP_8 ||
1001 event->keyval == GDK_KEY_KP_Up ||
1002 event->keyval == GDK_KEY_KP_9 ||
1003 event->keyval == GDK_KEY_KP_Page_Up)) {
1004 /* nethack mode; do nothing */
1006 #ifdef META_MANUAL_MASK
1007 if (event->state & META_MANUAL_MASK & inst->meta_mod_mask) {
1009 * If this key event had a Meta modifier bit set which
1010 * is also in META_MANUAL_MASK, that means passing
1011 * such an event to the GtkIMContext will be unhelpful
1012 * (it will eat the keystroke and turn it into
1013 * something not what we wanted).
1017 if (gtk_im_context_filter_keypress(inst->imc, event))
1022 * GDK 2.0 arranges to have done some translation for us: in
1023 * GDK 2.0, event->string is encoded in the current locale.
1025 * So we use the standard C library function mbstowcs() to
1026 * convert from the current locale into Unicode; from there
1027 * we can convert to whatever PuTTY is currently working in.
1028 * (In fact I convert straight back to UTF-8 from
1029 * wide-character Unicode, for the sake of simplicity: that
1030 * way we can still use exactly the same code to manipulate
1031 * the string, such as prefixing ESC.)
1033 output_charset = CS_UTF8;
1035 wchar_t widedata[32];
1040 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
1041 event->string, strlen(event->string),
1042 widedata, lenof(widedata)-1);
1045 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
1046 CS_UTF8, NULL, NULL, 0);
1047 output[1+ulen] = '\0';
1052 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
1053 ucsoutput[0] = '\033';
1054 ucsoutput[1] = ucsval;
1055 use_ucsoutput = TRUE;
1058 output[lenof(output)-1] = '\0';
1059 end = strlen(output);
1061 if (event->state & inst->meta_mod_mask) {
1063 if (end == 1) end = 0;
1065 #ifdef META_MANUAL_MASK
1066 if (event->state & META_MANUAL_MASK) {
1068 * Key events which have a META_MANUAL_MASK meta bit
1069 * set may have a keyval reflecting that, e.g. on OS X
1070 * the Option key acts as an AltGr-like modifier and
1071 * causes different Unicode characters to be output.
1073 * To work around this, we clear the dangerous
1074 * modifier bit and retranslate from the hardware
1075 * keycode as if the key had been pressed without that
1076 * modifier. Then we prefix Esc to *that*.
1079 GdkModifierType consumed;
1080 if (gdk_keymap_translate_keyboard_state
1081 (gdk_keymap_get_for_display(gdk_display_get_default()),
1082 event->hardware_keycode, event->state & ~META_MANUAL_MASK,
1083 0, &new_keyval, NULL, NULL, &consumed)) {
1084 ucsoutput[0] = '\033';
1085 ucsoutput[1] = gdk_keyval_to_unicode(new_keyval);
1086 use_ucsoutput = TRUE;
1094 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
1095 if (!output[1] && event->keyval == '`' &&
1096 (event->state & GDK_CONTROL_MASK)) {
1098 use_ucsoutput = FALSE;
1102 /* Some GTK backends (e.g. Quartz) do not change event->string
1103 * in response to the Control modifier. So we do it ourselves
1104 * here, if it's not already happened.
1106 * The translations below are in line with X11 policy as far
1108 if ((event->state & GDK_CONTROL_MASK) && end == 2) {
1109 if (output[1] >= '3' && output[1] <= '7') {
1110 /* ^3,...,^7 map to 0x1B,...,0x1F */
1111 output[1] += '\x1B' - '3';
1112 } else if (output[1] == '2' || output[1] == ' ') {
1113 /* ^2 and ^Space are both ^@, i.e. \0 */
1115 } else if (output[1] == '8') {
1118 } else if (output[1] == '/') {
1119 /* ^/ is the same as ^_ */
1121 } else if (output[1] >= 0x40 && output[1] < 0x7F) {
1122 /* Everything anywhere near the alphabetics just gets
1126 /* Anything else, e.g. '0', is unchanged. */
1129 /* Control-Break sends a Break special to the backend */
1130 if (event->keyval == GDK_KEY_Break &&
1131 (event->state & GDK_CONTROL_MASK)) {
1133 inst->back->special(inst->backhandle, TS_BRK);
1137 /* We handle Return ourselves, because it needs to be flagged as
1138 * special to ldisc. */
1139 if (event->keyval == GDK_KEY_Return) {
1141 use_ucsoutput = FALSE;
1146 /* Control-2, Control-Space and Control-@ are NUL */
1148 (event->keyval == ' ' || event->keyval == '2' ||
1149 event->keyval == '@') &&
1150 (event->state & (GDK_SHIFT_MASK |
1151 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
1153 use_ucsoutput = FALSE;
1157 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
1158 if (!output[1] && event->keyval == ' ' &&
1159 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
1160 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
1162 output_charset = CS_ISO8859_1;
1163 use_ucsoutput = FALSE;
1167 /* We don't let GTK tell us what Backspace is! We know better. */
1168 if (event->keyval == GDK_KEY_BackSpace &&
1169 !(event->state & GDK_SHIFT_MASK)) {
1170 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1172 use_ucsoutput = FALSE;
1176 /* For Shift Backspace, do opposite of what is configured. */
1177 if (event->keyval == GDK_KEY_BackSpace &&
1178 (event->state & GDK_SHIFT_MASK)) {
1179 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1181 use_ucsoutput = FALSE;
1186 /* Shift-Tab is ESC [ Z */
1187 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
1188 (event->keyval == GDK_KEY_Tab &&
1189 (event->state & GDK_SHIFT_MASK))) {
1190 end = 1 + sprintf(output+1, "\033[Z");
1191 use_ucsoutput = FALSE;
1193 /* And normal Tab is Tab, if the keymap hasn't already told us.
1194 * (Curiously, at least one version of the MacOS 10.5 X server
1195 * doesn't translate Tab for us. */
1196 if (event->keyval == GDK_KEY_Tab && end <= 1) {
1202 * NetHack keypad mode.
1205 const char *keys = NULL;
1206 switch (event->keyval) {
1207 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
1208 keys = "bB\002"; break;
1209 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
1210 keys = "jJ\012"; break;
1211 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
1212 keys = "nN\016"; break;
1213 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
1214 keys = "hH\010"; break;
1215 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
1216 keys = "..."; break;
1217 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
1218 keys = "lL\014"; break;
1219 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
1220 keys = "yY\031"; break;
1221 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
1222 keys = "kK\013"; break;
1223 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
1224 keys = "uU\025"; break;
1228 if (event->state & GDK_CONTROL_MASK)
1229 output[1] = keys[2];
1230 else if (event->state & GDK_SHIFT_MASK)
1231 output[1] = keys[1];
1233 output[1] = keys[0];
1234 use_ucsoutput = FALSE;
1240 * Application keypad mode.
1242 if (app_keypad_mode) {
1244 switch (event->keyval) {
1245 case GDK_KEY_Num_Lock: xkey = 'P'; break;
1246 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
1247 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
1248 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
1250 * Keypad + is tricky. It covers a space that would
1251 * be taken up on the VT100 by _two_ keys; so we
1252 * let Shift select between the two. Worse still,
1253 * in xterm function key mode we change which two...
1255 case GDK_KEY_KP_Add:
1256 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
1257 if (event->state & GDK_SHIFT_MASK)
1261 } else if (event->state & GDK_SHIFT_MASK)
1266 case GDK_KEY_KP_Enter: xkey = 'M'; break;
1267 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
1268 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
1269 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
1270 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
1271 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
1272 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
1273 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
1274 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
1275 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
1276 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
1277 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
1281 if (inst->term->vt52_mode) {
1282 if (xkey >= 'P' && xkey <= 'S')
1283 end = 1 + sprintf(output+1, "\033%c", xkey);
1285 end = 1 + sprintf(output+1, "\033?%c", xkey);
1287 end = 1 + sprintf(output+1, "\033O%c", xkey);
1288 use_ucsoutput = FALSE;
1294 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
1295 * for integer decimal nn.)
1297 * We also deal with the weird ones here. Linux VCs replace F1
1298 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1299 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1304 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1305 switch (event->keyval) {
1307 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1310 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1313 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1316 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1319 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1322 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1325 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1328 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1331 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1334 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1367 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1368 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1371 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1374 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1377 case GDK_KEY_End: case GDK_KEY_KP_End:
1380 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1383 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1387 /* Reorder edit keys to physical order */
1388 if (funky_type == FUNKY_VT400 && code <= 6)
1389 code = "\0\2\1\4\5\3\6"[code];
1391 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1392 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1393 use_ucsoutput = FALSE;
1397 if (funky_type == FUNKY_SCO && /* SCO function keys */
1398 code >= 11 && code <= 34) {
1399 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1401 switch (event->keyval) {
1402 case GDK_KEY_F1: index = 0; break;
1403 case GDK_KEY_F2: index = 1; break;
1404 case GDK_KEY_F3: index = 2; break;
1405 case GDK_KEY_F4: index = 3; break;
1406 case GDK_KEY_F5: index = 4; break;
1407 case GDK_KEY_F6: index = 5; break;
1408 case GDK_KEY_F7: index = 6; break;
1409 case GDK_KEY_F8: index = 7; break;
1410 case GDK_KEY_F9: index = 8; break;
1411 case GDK_KEY_F10: index = 9; break;
1412 case GDK_KEY_F11: index = 10; break;
1413 case GDK_KEY_F12: index = 11; break;
1415 if (event->state & GDK_SHIFT_MASK) index += 12;
1416 if (event->state & GDK_CONTROL_MASK) index += 24;
1417 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1418 use_ucsoutput = FALSE;
1421 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1422 code >= 1 && code <= 6) {
1423 char codes[] = "HL.FIG";
1428 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1430 use_ucsoutput = FALSE;
1433 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1434 code >= 11 && code <= 24) {
1440 if (inst->term->vt52_mode)
1441 end = 1 + sprintf(output+1,
1442 "\x1B%c", code + 'P' - 11 - offt);
1444 end = 1 + sprintf(output+1,
1445 "\x1BO%c", code + 'P' - 11 - offt);
1446 use_ucsoutput = FALSE;
1449 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1450 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1451 use_ucsoutput = FALSE;
1454 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1455 if (inst->term->vt52_mode)
1456 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1458 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1459 use_ucsoutput = FALSE;
1462 if ((code == 1 || code == 4) &&
1463 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1464 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1465 use_ucsoutput = FALSE;
1469 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1470 use_ucsoutput = FALSE;
1476 * Cursor keys. (This includes the numberpad cursor keys,
1477 * if we haven't already done them due to app keypad mode.)
1479 * Here we also process un-numlocked un-appkeypadded KP5,
1480 * which sends ESC [ G.
1484 switch (event->keyval) {
1485 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1486 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1487 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1488 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1489 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1492 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1493 event->state & GDK_CONTROL_MASK);
1494 use_ucsoutput = FALSE;
1503 if (end-start > 0) {
1504 #ifdef KEY_DEBUGGING
1506 printf("generating sequence:");
1507 for (i = start; i < end; i++)
1508 printf(" %02x", (unsigned char) output[i]);
1514 * For special control characters, the character set
1515 * should never matter.
1517 output[end] = '\0'; /* NUL-terminate */
1519 ldisc_send(inst->ldisc, output+start, -2, 1);
1520 } else if (!inst->direct_to_font) {
1521 if (!use_ucsoutput) {
1523 lpage_send(inst->ldisc, output_charset, output+start,
1527 * We generated our own Unicode key data from the
1528 * keysym, so use that instead.
1531 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1535 * In direct-to-font mode, we just send the string
1536 * exactly as we received it.
1539 ldisc_send(inst->ldisc, output+start, end-start, 1);
1542 show_mouseptr(inst, 0);
1543 term_seen_key_event(inst->term);
1549 #if GTK_CHECK_VERSION(2,0,0)
1550 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1552 struct gui_data *inst = (struct gui_data *)data;
1554 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1555 show_mouseptr(inst, 0);
1556 term_seen_key_event(inst->term);
1560 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1561 GdkEventType type, guint ebutton, guint state,
1562 gdouble ex, gdouble ey)
1564 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1566 /* Remember the timestamp. */
1567 inst->input_event_time = timestamp;
1569 show_mouseptr(inst, 1);
1571 shift = state & GDK_SHIFT_MASK;
1572 ctrl = state & GDK_CONTROL_MASK;
1573 alt = state & inst->meta_mod_mask;
1576 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1577 CONF_mouse_override));
1579 if (!raw_mouse_mode) {
1580 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1581 term_scroll(inst->term, 0, -5);
1584 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1585 term_scroll(inst->term, 0, +5);
1590 if (ebutton == 3 && ctrl) {
1591 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1592 ebutton, timestamp);
1598 else if (ebutton == 2)
1599 button = MBT_MIDDLE;
1600 else if (ebutton == 3)
1602 else if (ebutton == 4)
1603 button = MBT_WHEEL_UP;
1604 else if (ebutton == 5)
1605 button = MBT_WHEEL_DOWN;
1607 return FALSE; /* don't even know what button! */
1610 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1611 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1612 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1613 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1614 default: return FALSE; /* don't know this event type */
1617 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1618 return TRUE; /* we ignore these in raw mouse mode */
1620 x = (ex - inst->window_border) / inst->font_width;
1621 y = (ey - inst->window_border) / inst->font_height;
1623 term_mouse(inst->term, button, translate_button(button), act,
1624 x, y, shift, ctrl, alt);
1629 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1631 struct gui_data *inst = (struct gui_data *)data;
1632 return button_internal(inst, event->time, event->type, event->button,
1633 event->state, event->x, event->y);
1636 #if GTK_CHECK_VERSION(2,0,0)
1638 * In GTK 2, mouse wheel events have become a new type of event.
1639 * This handler translates them back into button-4 and button-5
1640 * presses so that I don't have to change my old code too much :-)
1642 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1644 struct gui_data *inst = (struct gui_data *)data;
1647 if (event->direction == GDK_SCROLL_UP)
1649 else if (event->direction == GDK_SCROLL_DOWN)
1654 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1655 button, event->state, event->x, event->y);
1659 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1661 struct gui_data *inst = (struct gui_data *)data;
1662 int shift, ctrl, alt, x, y, button;
1664 /* Remember the timestamp. */
1665 inst->input_event_time = event->time;
1667 show_mouseptr(inst, 1);
1669 shift = event->state & GDK_SHIFT_MASK;
1670 ctrl = event->state & GDK_CONTROL_MASK;
1671 alt = event->state & inst->meta_mod_mask;
1672 if (event->state & GDK_BUTTON1_MASK)
1674 else if (event->state & GDK_BUTTON2_MASK)
1675 button = MBT_MIDDLE;
1676 else if (event->state & GDK_BUTTON3_MASK)
1679 return FALSE; /* don't even know what button! */
1681 x = (event->x - inst->window_border) / inst->font_width;
1682 y = (event->y - inst->window_border) / inst->font_height;
1684 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1685 x, y, shift, ctrl, alt);
1690 void frontend_keypress(void *handle)
1692 struct gui_data *inst = (struct gui_data *)handle;
1695 * If our child process has exited but not closed, terminate on
1702 static void exit_callback(void *vinst)
1704 struct gui_data *inst = (struct gui_data *)vinst;
1705 int exitcode, close_on_exit;
1707 if (!inst->exited &&
1708 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1709 inst->exited = TRUE;
1710 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1711 if (close_on_exit == FORCE_ON ||
1712 (close_on_exit == AUTO && exitcode == 0))
1713 gtk_main_quit(); /* just go */
1715 ldisc_free(inst->ldisc);
1718 inst->back->free(inst->backhandle);
1719 inst->backhandle = NULL;
1721 term_provide_resize_fn(inst->term, NULL, NULL);
1722 update_specials_menu(inst);
1723 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1728 * Replacement code for the gtk_quit_add() function, which GTK2 - in
1729 * their unbounded wisdom - deprecated without providing any usable
1730 * replacement, and which we were using to ensure that our idle
1731 * function for toplevel callbacks was only run from the outermost
1734 * We maintain a global variable with a list of 'struct gui_data'
1735 * instances on which we should call inst_post_main() when a
1736 * subsidiary gtk_main() terminates; then we must make sure that all
1737 * our subsidiary calls to gtk_main() are followed by a call to
1740 * This is kind of overkill in the sense that at the time of writing
1741 * we don't actually ever run more than one 'struct gui_data' instance
1742 * in the same process, but we're _so nearly_ prepared to do that that
1743 * I want to remain futureproof against the possibility of doing so in
1746 struct post_main_context {
1747 struct post_main_context *next;
1748 struct gui_data *inst;
1750 struct post_main_context *post_main_list_head = NULL;
1751 static void request_post_main(struct gui_data *inst)
1753 struct post_main_context *node = snew(struct post_main_context);
1754 node->next = post_main_list_head;
1756 post_main_list_head = node;
1758 static void inst_post_main(struct gui_data *inst);
1759 void post_main(void)
1761 while (post_main_list_head) {
1762 struct post_main_context *node = post_main_list_head;
1763 post_main_list_head = node->next;
1764 inst_post_main(node->inst);
1769 void notify_remote_exit(void *frontend)
1771 struct gui_data *inst = (struct gui_data *)frontend;
1773 queue_toplevel_callback(exit_callback, inst);
1776 static void notify_toplevel_callback(void *frontend);
1778 static void inst_post_main(struct gui_data *inst)
1780 if (gtk_main_level() == 1) {
1781 notify_toplevel_callback(inst);
1782 inst->quit_fn_scheduled = FALSE;
1784 /* Apparently we're _still_ more than one level deep in
1785 * gtk_main() instances, so we'll need another callback for
1786 * when we get out of the next one. */
1787 request_post_main(inst);
1791 static gint idle_toplevel_callback_func(gpointer data)
1793 struct gui_data *inst = (struct gui_data *)data;
1795 if (gtk_main_level() > 1) {
1797 * We don't run the callbacks if we're in the middle of a
1798 * subsidiary gtk_main. Instead, ask for a callback when we
1799 * get back out of the subsidiary main loop (if we haven't
1800 * already arranged one), so we can reschedule ourself then.
1802 if (!inst->quit_fn_scheduled) {
1803 request_post_main(inst);
1804 inst->quit_fn_scheduled = TRUE;
1807 * And unschedule this idle function, since we've now done
1808 * everything we can until the innermost gtk_main has quit and
1809 * can reschedule us with a chance of actually taking action.
1811 if (inst->idle_fn_scheduled) { /* double-check, just in case */
1812 g_source_remove(inst->toplevel_callback_idle_id);
1813 inst->idle_fn_scheduled = FALSE;
1816 run_toplevel_callbacks();
1820 * If we've emptied our toplevel callback queue, unschedule
1821 * ourself. Otherwise, leave ourselves pending so we'll be called
1822 * again to deal with more callbacks after another round of the
1825 if (!toplevel_callback_pending() && inst->idle_fn_scheduled) {
1826 g_source_remove(inst->toplevel_callback_idle_id);
1827 inst->idle_fn_scheduled = FALSE;
1833 static void notify_toplevel_callback(void *frontend)
1835 struct gui_data *inst = (struct gui_data *)frontend;
1837 if (!inst->idle_fn_scheduled) {
1838 inst->toplevel_callback_idle_id =
1839 g_idle_add(idle_toplevel_callback_func, inst);
1840 inst->idle_fn_scheduled = TRUE;
1844 static gint timer_trigger(gpointer data)
1846 unsigned long now = GPOINTER_TO_LONG(data);
1847 unsigned long next, then;
1851 * Destroy the timer we got here on.
1854 g_source_remove(timer_id);
1859 * run_timers() may cause a call to timer_change_notify, in which
1860 * case a new timer will already have been set up and left in
1861 * timer_id. If it hasn't, and run_timers reports that some timing
1862 * still needs to be done, we do it ourselves.
1864 if (run_timers(now, &next) && !timer_id) {
1866 now = GETTICKCOUNT();
1867 if (now - then > next - then)
1871 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1875 * Returning FALSE means 'don't call this timer again', which
1876 * _should_ be redundant given that we removed it above, but just
1877 * in case, return FALSE anyway.
1882 void timer_change_notify(unsigned long next)
1887 g_source_remove(timer_id);
1889 ticks = next - GETTICKCOUNT();
1891 ticks = 1; /* just in case */
1893 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1896 #if GTK_CHECK_VERSION(2,0,0)
1897 gboolean fd_input_func(GIOChannel *source, GIOCondition condition,
1900 int sourcefd = g_io_channel_unix_get_fd(source);
1902 * We must process exceptional notifications before ordinary
1903 * readability ones, or we may go straight past the urgent
1906 if (condition & G_IO_PRI)
1907 select_result(sourcefd, 4);
1908 if (condition & G_IO_IN)
1909 select_result(sourcefd, 1);
1910 if (condition & G_IO_OUT)
1911 select_result(sourcefd, 2);
1916 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1918 if (condition & GDK_INPUT_EXCEPTION)
1919 select_result(sourcefd, 4);
1920 if (condition & GDK_INPUT_READ)
1921 select_result(sourcefd, 1);
1922 if (condition & GDK_INPUT_WRITE)
1923 select_result(sourcefd, 2);
1927 void destroy(GtkWidget *widget, gpointer data)
1932 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1934 struct gui_data *inst = (struct gui_data *)data;
1935 term_set_focus(inst->term, event->in);
1936 term_update(inst->term);
1937 show_mouseptr(inst, 1);
1941 void set_busy_status(void *frontend, int status)
1943 struct gui_data *inst = (struct gui_data *)frontend;
1944 inst->busy_status = status;
1945 update_mouseptr(inst);
1949 * set or clear the "raw mouse message" mode
1951 void set_raw_mouse_mode(void *frontend, int activate)
1953 struct gui_data *inst = (struct gui_data *)frontend;
1954 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1955 send_raw_mouse = activate;
1956 update_mouseptr(inst);
1959 void request_resize(void *frontend, int w, int h)
1961 struct gui_data *inst = (struct gui_data *)frontend;
1963 #if !GTK_CHECK_VERSION(3,0,0)
1965 int large_x, large_y;
1966 int offset_x, offset_y;
1968 GtkRequisition inner, outer;
1971 * This is a heinous hack dreamed up by the gnome-terminal
1972 * people to get around a limitation in gtk. The problem is
1973 * that in order to set the size correctly we really need to be
1974 * calling gtk_window_resize - but that needs to know the size
1975 * of the _whole window_, not the drawing area. So what we do
1976 * is to set an artificially huge size request on the drawing
1977 * area, recompute the resulting size request on the window,
1978 * and look at the difference between the two. That gives us
1979 * the x and y offsets we need to translate drawing area size
1980 * into window size for real, and then we call
1981 * gtk_window_resize.
1985 * We start by retrieving the current size of the whole window.
1986 * Adding a bit to _that_ will give us a value we can use as a
1987 * bogus size request which guarantees to be bigger than the
1988 * current size of the drawing area.
1990 get_window_pixels(inst, &large_x, &large_y);
1994 gtk_widget_set_size_request(inst->area, large_x, large_y);
1995 gtk_widget_size_request(inst->area, &inner);
1996 gtk_widget_size_request(inst->window, &outer);
1998 offset_x = outer.width - inner.width;
1999 offset_y = outer.height - inner.height;
2001 area_x = inst->font_width * w + 2*inst->window_border;
2002 area_y = inst->font_height * h + 2*inst->window_border;
2005 * Now we must set the size request on the drawing area back to
2006 * something sensible before we commit the real resize. Best
2007 * way to do this, I think, is to set it to what the size is
2008 * really going to end up being.
2010 gtk_widget_set_size_request(inst->area, area_x, area_y);
2011 #if GTK_CHECK_VERSION(2,0,0)
2012 gtk_window_resize(GTK_WINDOW(inst->window),
2013 area_x + offset_x, area_y + offset_y);
2015 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
2017 * I can no longer remember what this call to
2018 * gtk_container_dequeue_resize_handler is for. It was
2019 * introduced in r3092 with no comment, and the commit log
2020 * message was uninformative. I'm _guessing_ its purpose is to
2021 * prevent gratuitous resize processing on the window given
2022 * that we're about to resize it anyway, but I have no idea
2023 * why that's so incredibly vital.
2025 * I've tried removing the call, and nothing seems to go
2026 * wrong. I've backtracked to r3092 and tried removing the
2027 * call there, and still nothing goes wrong. So I'm going to
2028 * adopt the working hypothesis that it's superfluous; I won't
2029 * actually remove it from the GTK 1.2 code, but I won't
2030 * attempt to replicate its functionality in the GTK 2 code
2033 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
2034 gdk_window_resize(gtk_widget_get_window(inst->window),
2035 area_x + offset_x, area_y + offset_y);
2038 #else /* GTK_CHECK_VERSION(3,0,0) */
2041 * In GTK3, we can do this by using gtk_window_resize_to_geometry,
2042 * which uses the fact that we've already set up the main window's
2043 * WM hints to reflect the terminal drawing area's resize
2044 * increment (i.e. character cell) and the fixed amount of stuff
2047 gtk_window_resize_to_geometry(GTK_WINDOW(inst->window), w, h);
2053 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
2055 inst->cols[n].red = r * 0x0101;
2056 inst->cols[n].green = g * 0x0101;
2057 inst->cols[n].blue = b * 0x0101;
2059 #if !GTK_CHECK_VERSION(3,0,0)
2061 gboolean success[1];
2062 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
2063 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
2064 FALSE, TRUE, success);
2066 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2067 appname, n, r, g, b);
2072 void set_gdk_window_background(GdkWindow *win, const GdkColor *col)
2074 #if GTK_CHECK_VERSION(3,0,0)
2075 /* gdk_window_set_background is deprecated; work around its
2078 rgba.red = col->red / 65535.0;
2079 rgba.green = col->green / 65535.0;
2080 rgba.blue = col->blue / 65535.0;
2082 gdk_window_set_background_rgba(win, &rgba);
2085 /* For GTK1, which doesn't have a 'const' on
2086 * gdk_window_set_background's second parameter type. */
2087 GdkColor col_mutable = *col;
2088 gdk_window_set_background(win, &col_mutable);
2093 void set_window_background(struct gui_data *inst)
2095 if (inst->area && gtk_widget_get_window(inst->area))
2096 set_gdk_window_background(gtk_widget_get_window(inst->area),
2098 if (inst->window && gtk_widget_get_window(inst->window))
2099 set_gdk_window_background(gtk_widget_get_window(inst->window),
2103 void palette_set(void *frontend, int n, int r, int g, int b)
2105 struct gui_data *inst = (struct gui_data *)frontend;
2108 if (n >= NALLCOLOURS)
2110 real_palette_set(inst, n, r, g, b);
2112 /* Default Background changed. Ensure space between text area and
2113 * window border is redrawn */
2114 set_window_background(inst);
2115 draw_backing_rect(inst);
2116 gtk_widget_queue_draw(inst->area);
2120 void palette_reset(void *frontend)
2122 struct gui_data *inst = (struct gui_data *)frontend;
2123 /* This maps colour indices in inst->conf to those used in inst->cols. */
2124 static const int ww[] = {
2125 256, 257, 258, 259, 260, 261,
2126 0, 8, 1, 9, 2, 10, 3, 11,
2127 4, 12, 5, 13, 6, 14, 7, 15
2131 assert(lenof(ww) == NCFGCOLOURS);
2133 #if !GTK_CHECK_VERSION(3,0,0)
2134 if (!inst->colmap) {
2135 inst->colmap = gdk_colormap_get_system();
2137 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
2141 for (i = 0; i < NCFGCOLOURS; i++) {
2142 inst->cols[ww[i]].red =
2143 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
2144 inst->cols[ww[i]].green =
2145 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
2146 inst->cols[ww[i]].blue =
2147 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
2150 for (i = 0; i < NEXTCOLOURS; i++) {
2152 int r = i / 36, g = (i / 6) % 6, b = i % 6;
2153 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
2154 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
2155 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
2157 int shade = i - 216;
2158 shade = shade * 0x0a0a + 0x0808;
2159 inst->cols[i+16].red = inst->cols[i+16].green =
2160 inst->cols[i+16].blue = shade;
2164 #if !GTK_CHECK_VERSION(3,0,0)
2166 gboolean success[NALLCOLOURS];
2167 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
2168 FALSE, TRUE, success);
2169 for (i = 0; i < NALLCOLOURS; i++) {
2171 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2173 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
2174 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
2175 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
2180 /* Since Default Background may have changed, ensure that space
2181 * between text area and window border is refreshed. */
2182 set_window_background(inst);
2183 if (inst->area && gtk_widget_get_window(inst->area)) {
2184 draw_backing_rect(inst);
2185 gtk_widget_queue_draw(inst->area);
2189 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
2191 /* ----------------------------------------------------------------------
2192 * Clipboard handling, using the high-level GtkClipboard interface in
2193 * as hands-off a way as possible. We write and read the clipboard as
2194 * UTF-8 text, and let GTK deal with converting to any other text
2195 * formats it feels like.
2198 void init_clipboard(struct gui_data *inst)
2200 inst->clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(),
2205 * Because calling gtk_clipboard_set_with_data triggers a call to the
2206 * clipboard_clear function from the last time, we need to arrange a
2207 * way to distinguish a real call to clipboard_clear for the _new_
2208 * instance of the clipboard data from the leftover call for the
2209 * outgoing one. We do this by setting the user data field in our
2210 * gtk_clipboard_set_with_data() call, instead of the obvious pointer
2211 * to 'inst', to one of these.
2213 struct clipboard_data_instance {
2214 struct gui_data *inst;
2215 char *pasteout_data_utf8;
2216 int pasteout_data_utf8_len;
2219 static void clipboard_provide_data(GtkClipboard *clipboard,
2220 GtkSelectionData *selection_data,
2221 guint info, gpointer data)
2223 struct clipboard_data_instance *cdi =
2224 (struct clipboard_data_instance *)data;
2225 struct gui_data *inst = cdi->inst;
2227 if (inst->current_cdi == cdi) {
2228 gtk_selection_data_set_text(selection_data, cdi->pasteout_data_utf8,
2229 cdi->pasteout_data_utf8_len);
2233 static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
2235 struct clipboard_data_instance *cdi =
2236 (struct clipboard_data_instance *)data;
2237 struct gui_data *inst = cdi->inst;
2239 if (inst->current_cdi == cdi) {
2240 term_deselect(inst->term);
2241 inst->current_cdi = NULL;
2243 sfree(cdi->pasteout_data_utf8);
2247 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2250 struct gui_data *inst = (struct gui_data *)frontend;
2251 struct clipboard_data_instance *cdi;
2253 if (inst->direct_to_font) {
2254 /* In this clipboard mode, we just can't paste if we're in
2255 * direct-to-font mode. Fortunately, that shouldn't be
2256 * important, because we'll only use this clipboard handling
2257 * code on systems where that kind of font doesn't exist
2262 cdi = snew(struct clipboard_data_instance);
2264 inst->current_cdi = cdi;
2265 cdi->pasteout_data_utf8 = snewn(len*6, char);
2267 const wchar_t *tmp = data;
2269 cdi->pasteout_data_utf8_len =
2270 charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
2271 len*6, CS_UTF8, NULL, NULL, 0);
2275 * It would be nice to just call gtk_clipboard_set_text() in place
2276 * of all of the faffing below. Unfortunately, that won't give me
2277 * access to the clipboard-clear event, which we use to visually
2278 * deselect text in the terminal.
2281 GtkTargetList *targetlist;
2282 GtkTargetEntry *targettable;
2285 targetlist = gtk_target_list_new(NULL, 0);
2286 gtk_target_list_add_text_targets(targetlist, 0);
2287 targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
2288 gtk_clipboard_set_with_data(inst->clipboard, targettable, n_targets,
2289 clipboard_provide_data, clipboard_clear,
2291 gtk_target_table_free(targettable, n_targets);
2292 gtk_target_list_unref(targetlist);
2296 static void clipboard_text_received(GtkClipboard *clipboard,
2297 const gchar *text, gpointer data)
2299 struct gui_data *inst = (struct gui_data *)data;
2305 length = strlen(text);
2307 if (inst->pastein_data)
2308 sfree(inst->pastein_data);
2310 inst->pastein_data = snewn(length, wchar_t);
2311 inst->pastein_data_len = mb_to_wc(CS_UTF8, 0, text, length,
2312 inst->pastein_data, length);
2314 term_do_paste(inst->term);
2317 void request_paste(void *frontend)
2319 struct gui_data *inst = (struct gui_data *)frontend;
2320 gtk_clipboard_request_text(inst->clipboard, clipboard_text_received, inst);
2323 #else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
2325 /* ----------------------------------------------------------------------
2326 * Clipboard handling for X, using the low-level gtk_selection_*
2327 * interface, handling conversions to fiddly things like compound text
2328 * ourselves, and storing in X cut buffers too.
2330 * This version of the clipboard code has to be kept around for GTK1,
2331 * which doesn't have the higher-level GtkClipboard interface at all.
2332 * And since it works on GTK2 and GTK3 too and has had a good few
2333 * years of shakedown and bug fixing, we might as well keep using it
2334 * where it's applicable.
2336 * It's _possible_ that we might be able to replicate all the
2337 * important wrinkles of this code in GtkClipboard. (In particular,
2338 * cut buffers or local analogue look as if they might be accessible
2339 * via gtk_clipboard_set_can_store(), and delivering text in
2340 * non-Unicode formats only in the direct-to-font case ought to be
2341 * possible if we can figure out the right set of things to put in the
2342 * GtkTargetList.) But that work can wait until there's a need for it!
2345 /* Store the data in a cut-buffer. */
2346 static void store_cutbuffer(char * ptr, int len)
2348 #ifndef NOT_X_WINDOWS
2349 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2350 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
2351 XRotateBuffers(disp, 1);
2352 XStoreBytes(disp, ptr, len);
2356 /* Retrieve data from a cut-buffer.
2357 * Returned data needs to be freed with XFree().
2359 static char *retrieve_cutbuffer(int *nbytes)
2361 #ifndef NOT_X_WINDOWS
2362 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2364 ptr = XFetchBytes(disp, nbytes);
2365 if (*nbytes <= 0 && ptr != 0) {
2375 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2378 struct gui_data *inst = (struct gui_data *)frontend;
2379 if (inst->pasteout_data)
2380 sfree(inst->pasteout_data);
2381 if (inst->pasteout_data_ctext)
2382 sfree(inst->pasteout_data_ctext);
2383 if (inst->pasteout_data_utf8)
2384 sfree(inst->pasteout_data_utf8);
2387 * Set up UTF-8 and compound text paste data. This only happens
2388 * if we aren't in direct-to-font mode using the D800 hack.
2390 if (!inst->direct_to_font) {
2391 const wchar_t *tmp = data;
2393 #ifndef NOT_X_WINDOWS
2396 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2399 inst->pasteout_data_utf8 = snewn(len*6, char);
2400 inst->pasteout_data_utf8_len = len*6;
2401 inst->pasteout_data_utf8_len =
2402 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
2403 inst->pasteout_data_utf8_len,
2404 CS_UTF8, NULL, NULL, 0);
2405 if (inst->pasteout_data_utf8_len == 0) {
2406 sfree(inst->pasteout_data_utf8);
2407 inst->pasteout_data_utf8 = NULL;
2409 inst->pasteout_data_utf8 =
2410 sresize(inst->pasteout_data_utf8,
2411 inst->pasteout_data_utf8_len + 1, char);
2412 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
2416 * Now let Xlib convert our UTF-8 data into compound text.
2418 #ifndef NOT_X_WINDOWS
2419 list[0] = inst->pasteout_data_utf8;
2420 if (Xutf8TextListToTextProperty(disp, list, 1,
2421 XCompoundTextStyle, &tp) == 0) {
2422 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
2423 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
2424 inst->pasteout_data_ctext_len = tp.nitems;
2429 inst->pasteout_data_ctext = NULL;
2430 inst->pasteout_data_ctext_len = 0;
2433 inst->pasteout_data_utf8 = NULL;
2434 inst->pasteout_data_utf8_len = 0;
2435 inst->pasteout_data_ctext = NULL;
2436 inst->pasteout_data_ctext_len = 0;
2439 inst->pasteout_data = snewn(len*6, char);
2440 inst->pasteout_data_len = len*6;
2441 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
2442 data, len, inst->pasteout_data,
2443 inst->pasteout_data_len,
2445 if (inst->pasteout_data_len == 0) {
2446 sfree(inst->pasteout_data);
2447 inst->pasteout_data = NULL;
2449 inst->pasteout_data =
2450 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
2453 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
2455 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
2456 inst->input_event_time)) {
2457 #if GTK_CHECK_VERSION(2,0,0)
2458 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
2460 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2461 GDK_SELECTION_TYPE_STRING, 1);
2462 if (inst->pasteout_data_ctext)
2463 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2464 compound_text_atom, 1);
2465 if (inst->pasteout_data_utf8)
2466 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2467 utf8_string_atom, 1);
2471 term_deselect(inst->term);
2474 static void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2475 guint info, guint time_stamp, gpointer data)
2477 struct gui_data *inst = (struct gui_data *)data;
2478 GdkAtom target = gtk_selection_data_get_target(seldata);
2479 if (target == utf8_string_atom)
2480 gtk_selection_data_set(seldata, target, 8,
2481 (unsigned char *)inst->pasteout_data_utf8,
2482 inst->pasteout_data_utf8_len);
2483 else if (target == compound_text_atom)
2484 gtk_selection_data_set(seldata, target, 8,
2485 (unsigned char *)inst->pasteout_data_ctext,
2486 inst->pasteout_data_ctext_len);
2488 gtk_selection_data_set(seldata, target, 8,
2489 (unsigned char *)inst->pasteout_data,
2490 inst->pasteout_data_len);
2493 static gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2496 struct gui_data *inst = (struct gui_data *)data;
2498 term_deselect(inst->term);
2499 if (inst->pasteout_data)
2500 sfree(inst->pasteout_data);
2501 if (inst->pasteout_data_ctext)
2502 sfree(inst->pasteout_data_ctext);
2503 if (inst->pasteout_data_utf8)
2504 sfree(inst->pasteout_data_utf8);
2505 inst->pasteout_data = NULL;
2506 inst->pasteout_data_len = 0;
2507 inst->pasteout_data_ctext = NULL;
2508 inst->pasteout_data_ctext_len = 0;
2509 inst->pasteout_data_utf8 = NULL;
2510 inst->pasteout_data_utf8_len = 0;
2514 void request_paste(void *frontend)
2516 struct gui_data *inst = (struct gui_data *)frontend;
2518 * In Unix, pasting is asynchronous: all we can do at the
2519 * moment is to call gtk_selection_convert(), and when the data
2520 * comes back _then_ we can call term_do_paste().
2523 if (!inst->direct_to_font) {
2525 * First we attempt to retrieve the selection as a UTF-8
2526 * string (which we will convert to the correct code page
2527 * before sending to the session, of course). If that
2528 * fails, selection_received() will be informed and will
2529 * fall back to an ordinary string.
2531 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2533 inst->input_event_time);
2536 * If we're in direct-to-font mode, we disable UTF-8
2537 * pasting, and go straight to ordinary string data.
2539 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2540 GDK_SELECTION_TYPE_STRING,
2541 inst->input_event_time);
2545 static void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2546 guint time, gpointer data)
2548 struct gui_data *inst = (struct gui_data *)data;
2551 #ifndef NOT_X_WINDOWS
2553 int free_list_required = 0;
2554 int free_required = 0;
2557 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2558 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2559 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2560 gint seldata_length = gtk_selection_data_get_length(seldata);
2562 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2564 * Failed to get a UTF-8 selection string. Try compound
2567 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2569 inst->input_event_time);
2573 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2575 * Failed to get UTF-8 or compound text. Try an ordinary
2578 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2579 GDK_SELECTION_TYPE_STRING,
2580 inst->input_event_time);
2585 * If we have data, but it's not of a type we can deal with,
2586 * we have to ignore the data.
2588 if (seldata_length > 0 &&
2589 seldata_type != GDK_SELECTION_TYPE_STRING &&
2590 seldata_type != compound_text_atom &&
2591 seldata_type != utf8_string_atom)
2595 * If we have no data, try looking in a cut buffer.
2597 if (seldata_length <= 0) {
2598 #ifndef NOT_X_WINDOWS
2599 text = retrieve_cutbuffer(&length);
2602 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2603 * source, so use that as a de-facto standard. */
2604 charset = CS_ISO8859_1;
2611 * Convert COMPOUND_TEXT into UTF-8.
2613 if (seldata_type == compound_text_atom) {
2614 #ifndef NOT_X_WINDOWS
2617 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2619 tp.value = (unsigned char *)seldata_data;
2620 tp.encoding = (Atom) seldata_type;
2621 tp.format = gtk_selection_data_get_format(seldata);
2622 tp.nitems = seldata_length;
2623 ret = Xutf8TextPropertyToTextList(disp, &tp, &list, &count);
2624 if (ret == 0 && count == 1) {
2626 length = strlen(list[0]);
2628 free_list_required = 1;
2633 * Compound text failed; fall back to STRING.
2635 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2636 GDK_SELECTION_TYPE_STRING,
2637 inst->input_event_time);
2641 text = (char *)seldata_data;
2642 length = seldata_length;
2643 charset = (seldata_type == utf8_string_atom ?
2644 CS_UTF8 : inst->ucsdata.line_codepage);
2648 if (inst->pastein_data)
2649 sfree(inst->pastein_data);
2651 inst->pastein_data = snewn(length, wchar_t);
2652 inst->pastein_data_len = length;
2653 inst->pastein_data_len =
2654 mb_to_wc(charset, 0, text, length,
2655 inst->pastein_data, inst->pastein_data_len);
2657 term_do_paste(inst->term);
2659 #ifndef NOT_X_WINDOWS
2660 if (free_list_required)
2661 XFreeStringList(list);
2667 void init_clipboard(struct gui_data *inst)
2669 #ifndef NOT_X_WINDOWS
2671 * Ensure that all the cut buffers exist - according to the ICCCM,
2672 * we must do this before we start using cut buffers.
2674 unsigned char empty[] = "";
2675 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2676 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2677 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
2678 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2679 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
2680 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2681 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
2682 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2683 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
2684 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2685 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
2686 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2687 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
2688 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2689 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
2690 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2691 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
2694 g_signal_connect(G_OBJECT(inst->area), "selection_received",
2695 G_CALLBACK(selection_received), inst);
2696 g_signal_connect(G_OBJECT(inst->area), "selection_get",
2697 G_CALLBACK(selection_get), inst);
2698 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
2699 G_CALLBACK(selection_clear), inst);
2703 * End of selection/clipboard handling.
2704 * ----------------------------------------------------------------------
2707 #endif /* JUST_USE_GTK_CLIPBOARD_UTF8 */
2709 void get_clip(void *frontend, wchar_t ** p, int *len)
2711 struct gui_data *inst = (struct gui_data *)frontend;
2714 *p = inst->pastein_data;
2715 *len = inst->pastein_data_len;
2719 static void set_window_titles(struct gui_data *inst)
2722 * We must always call set_icon_name after calling set_title,
2723 * since set_title will write both names. Irritating, but such
2726 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2727 if (!conf_get_int(inst->conf, CONF_win_name_always))
2728 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
2732 void set_title(void *frontend, char *title)
2734 struct gui_data *inst = (struct gui_data *)frontend;
2735 sfree(inst->wintitle);
2736 inst->wintitle = dupstr(title);
2737 set_window_titles(inst);
2740 void set_icon(void *frontend, char *title)
2742 struct gui_data *inst = (struct gui_data *)frontend;
2743 sfree(inst->icontitle);
2744 inst->icontitle = dupstr(title);
2745 set_window_titles(inst);
2748 void set_title_and_icon(void *frontend, char *title, char *icon)
2750 struct gui_data *inst = (struct gui_data *)frontend;
2751 sfree(inst->wintitle);
2752 inst->wintitle = dupstr(title);
2753 sfree(inst->icontitle);
2754 inst->icontitle = dupstr(icon);
2755 set_window_titles(inst);
2758 void set_sbar(void *frontend, int total, int start, int page)
2760 struct gui_data *inst = (struct gui_data *)frontend;
2761 if (!conf_get_int(inst->conf, CONF_scrollbar))
2763 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
2764 gtk_adjustment_set_upper(inst->sbar_adjust, total);
2765 gtk_adjustment_set_value(inst->sbar_adjust, start);
2766 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
2767 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
2768 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
2769 inst->ignore_sbar = TRUE;
2770 gtk_adjustment_changed(inst->sbar_adjust);
2771 inst->ignore_sbar = FALSE;
2774 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2776 struct gui_data *inst = (struct gui_data *)data;
2778 if (!conf_get_int(inst->conf, CONF_scrollbar))
2780 if (!inst->ignore_sbar)
2781 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
2784 void sys_cursor(void *frontend, int x, int y)
2787 * This is meaningless under X.
2792 * This is still called when mode==BELL_VISUAL, even though the
2793 * visual bell is handled entirely within terminal.c, because we
2794 * may want to perform additional actions on any kind of bell (for
2795 * example, taskbar flashing in Windows).
2797 void do_beep(void *frontend, int mode)
2799 if (mode == BELL_DEFAULT)
2803 int char_width(Context ctx, int uc)
2806 * Under X, any fixed-width font really _is_ fixed-width.
2807 * Double-width characters will be dealt with using a separate
2808 * font. For the moment we can simply return 1.
2810 * FIXME: but is that also true of Pango?
2815 Context get_ctx(void *frontend)
2817 struct gui_data *inst = (struct gui_data *)frontend;
2818 struct draw_ctx *dctx;
2821 if (!gtk_widget_get_window(inst->area))
2824 #ifndef NO_BACKING_PIXMAPS
2825 target = inst->pixmap;
2827 target = gtk_widget_get_window(inst->area);
2830 dctx = snew(struct draw_ctx);
2832 dctx->uctx.type = inst->drawtype;
2833 #ifdef DRAW_TEXT_GDK
2834 if (dctx->uctx.type == DRAWTYPE_GDK) {
2835 dctx->uctx.u.gdk.target = target;
2836 dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
2839 #ifdef DRAW_TEXT_CAIRO
2840 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2841 dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
2842 dctx->uctx.u.cairo.cr = gdk_cairo_create(target);
2843 cairo_setup_dctx(dctx);
2849 void free_ctx(Context ctx)
2851 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2852 /* struct gui_data *inst = dctx->inst; */
2853 #ifdef DRAW_TEXT_GDK
2854 if (dctx->uctx.type == DRAWTYPE_GDK) {
2855 gdk_gc_unref(dctx->uctx.u.gdk.gc);
2858 #ifdef DRAW_TEXT_CAIRO
2859 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2860 cairo_destroy(dctx->uctx.u.cairo.cr);
2867 static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
2869 #ifndef NO_BACKING_PIXMAPS
2870 #ifdef DRAW_TEXT_GDK
2871 if (dctx->uctx.type == DRAWTYPE_GDK) {
2872 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2873 dctx->uctx.u.gdk.gc, dctx->inst->pixmap,
2877 #ifdef DRAW_TEXT_CAIRO /* FIXME: and not GTK3 where a cairo_t is all we have */
2878 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2879 GdkGC *gc = gdk_gc_new(gtk_widget_get_window(dctx->inst->area));
2880 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2881 gc, dctx->inst->pixmap, x, y, x, y, w, h);
2888 static void draw_set_colour(struct draw_ctx *dctx, int col)
2890 #ifdef DRAW_TEXT_GDK
2891 if (dctx->uctx.type == DRAWTYPE_GDK) {
2892 gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
2895 #ifdef DRAW_TEXT_CAIRO
2896 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2897 cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
2898 dctx->inst->cols[col].red / 65535.0,
2899 dctx->inst->cols[col].green / 65535.0,
2900 dctx->inst->cols[col].blue / 65535.0);
2905 static void draw_rectangle(struct draw_ctx *dctx, int filled,
2906 int x, int y, int w, int h)
2908 #ifdef DRAW_TEXT_GDK
2909 if (dctx->uctx.type == DRAWTYPE_GDK) {
2910 gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2911 filled, x, y, w, h);
2914 #ifdef DRAW_TEXT_CAIRO
2915 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2916 cairo_new_path(dctx->uctx.u.cairo.cr);
2918 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2919 cairo_fill(dctx->uctx.u.cairo.cr);
2921 cairo_rectangle(dctx->uctx.u.cairo.cr,
2922 x + 0.5, y + 0.5, w, h);
2923 cairo_close_path(dctx->uctx.u.cairo.cr);
2924 cairo_stroke(dctx->uctx.u.cairo.cr);
2930 static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
2932 #ifdef DRAW_TEXT_GDK
2933 if (dctx->uctx.type == DRAWTYPE_GDK) {
2941 gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
2944 #ifdef DRAW_TEXT_CAIRO
2945 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2946 cairo_reset_clip(dctx->uctx.u.cairo.cr);
2947 cairo_new_path(dctx->uctx.u.cairo.cr);
2948 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2949 cairo_clip(dctx->uctx.u.cairo.cr);
2954 static void draw_point(struct draw_ctx *dctx, int x, int y)
2956 #ifdef DRAW_TEXT_GDK
2957 if (dctx->uctx.type == DRAWTYPE_GDK) {
2958 gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
2961 #ifdef DRAW_TEXT_CAIRO
2962 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2963 cairo_new_path(dctx->uctx.u.cairo.cr);
2964 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
2965 cairo_fill(dctx->uctx.u.cairo.cr);
2970 static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
2972 #ifdef DRAW_TEXT_GDK
2973 if (dctx->uctx.type == DRAWTYPE_GDK) {
2974 gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2978 #ifdef DRAW_TEXT_CAIRO
2979 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2980 cairo_new_path(dctx->uctx.u.cairo.cr);
2981 cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
2982 cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
2983 cairo_stroke(dctx->uctx.u.cairo.cr);
2988 static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
2990 int h, int hdouble, int hbothalf)
2992 #ifdef DRAW_TEXT_CAIRO
2993 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2994 cairo_matrix_t matrix;
3018 cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
3023 static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
3025 int h, int hdouble, int hbothalf)
3027 #ifdef DRAW_TEXT_GDK
3028 #ifndef NO_BACKING_PIXMAPS
3029 if (dctx->uctx.type == DRAWTYPE_GDK) {
3031 * I can't find any plausible StretchBlt equivalent in the X
3032 * server, so I'm going to do this the slow and painful way.
3033 * This will involve repeated calls to gdk_draw_pixmap() to
3034 * stretch the text horizontally. It's O(N^2) in time and O(N)
3035 * in network bandwidth, but you try thinking of a better way.
3040 for (i = 0; i < w; i++) {
3041 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3042 dctx->uctx.u.gdk.gc,
3043 dctx->uctx.u.gdk.target,
3053 /* Now stretch vertically, in the same way. */
3058 for (i = 0; i < h; i += 2) {
3059 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3060 dctx->uctx.u.gdk.gc,
3061 dctx->uctx.u.gdk.target,
3069 #error No way to implement stretching in GDK without a reliable backing pixmap
3071 #endif /* DRAW_TEXT_GDK */
3072 #ifdef DRAW_TEXT_CAIRO
3073 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3074 cairo_set_matrix(dctx->uctx.u.cairo.cr,
3075 &dctx->uctx.u.cairo.origmatrix);
3080 static void draw_backing_rect(struct gui_data *inst)
3082 struct draw_ctx *dctx = get_ctx(inst);
3083 draw_set_colour(dctx, 258);
3084 draw_rectangle(dctx, 1, 0, 0,
3085 inst->width * inst->font_width + 2*inst->window_border,
3086 inst->height * inst->font_height + 2*inst->window_border);
3091 * Draw a line of text in the window, at given character
3092 * coordinates, in given attributes.
3094 * We are allowed to fiddle with the contents of `text'.
3096 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
3097 unsigned long attr, int lattr)
3099 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3100 struct gui_data *inst = dctx->inst;
3101 int ncombining, combining;
3102 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
3104 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
3106 if (attr & TATTR_COMBINING) {
3112 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
3113 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
3114 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
3119 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
3120 if (nfg < 16) nfg |= 8;
3121 else if (nfg >= 256) nfg |= 1;
3123 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
3124 if (nbg < 16) nbg |= 8;
3125 else if (nbg >= 256) nbg |= 1;
3127 if ((attr & TATTR_ACTCURS) && !monochrome) {
3132 fontid = shadow = 0;
3134 if (attr & ATTR_WIDE) {
3141 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
3148 if (!inst->fonts[fontid]) {
3151 * Fall back through font ids with subsets of this one's
3152 * set bits, in order.
3154 for (i = fontid; i-- > 0 ;) {
3156 continue; /* some other bit is set */
3157 if (inst->fonts[i]) {
3162 assert(inst->fonts[fontid]); /* we should at least have hit zero */
3165 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3167 if (x >= inst->term->cols)
3169 if (x + len*2*widefactor > inst->term->cols)
3170 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3176 x*inst->font_width+inst->window_border,
3177 y*inst->font_height+inst->window_border,
3178 rlen*widefactor*inst->font_width,
3181 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3182 draw_stretch_before(dctx,
3183 x*inst->font_width+inst->window_border,
3184 y*inst->font_height+inst->window_border,
3185 rlen*widefactor*inst->font_width, TRUE,
3187 ((lattr & LATTR_MODE) != LATTR_WIDE),
3188 ((lattr & LATTR_MODE) == LATTR_BOT));
3191 draw_set_colour(dctx, nbg);
3192 draw_rectangle(dctx, TRUE,
3193 x*inst->font_width+inst->window_border,
3194 y*inst->font_height+inst->window_border,
3195 rlen*widefactor*inst->font_width, inst->font_height);
3197 draw_set_colour(dctx, nfg);
3198 for (combining = 0; combining < ncombining; combining++) {
3199 unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
3200 x*inst->font_width+inst->window_border,
3201 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
3202 text + combining, len, widefactor > 1,
3203 bold, inst->font_width);
3206 if (attr & ATTR_UNDER) {
3207 int uheight = inst->fonts[0]->ascent + 1;
3208 if (uheight >= inst->font_height)
3209 uheight = inst->font_height - 1;
3210 draw_line(dctx, x*inst->font_width+inst->window_border,
3211 y*inst->font_height + uheight + inst->window_border,
3212 (x+len)*widefactor*inst->font_width-1+inst->window_border,
3213 y*inst->font_height + uheight + inst->window_border);
3216 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3217 draw_stretch_after(dctx,
3218 x*inst->font_width+inst->window_border,
3219 y*inst->font_height+inst->window_border,
3220 rlen*widefactor*inst->font_width, TRUE,
3222 ((lattr & LATTR_MODE) != LATTR_WIDE),
3223 ((lattr & LATTR_MODE) == LATTR_BOT));
3227 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3228 unsigned long attr, int lattr)
3230 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3231 struct gui_data *inst = dctx->inst;
3234 do_text_internal(ctx, x, y, text, len, attr, lattr);
3236 if (attr & ATTR_WIDE) {
3242 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3244 if (x >= inst->term->cols)
3246 if (x + len*2*widefactor > inst->term->cols)
3247 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3252 x*inst->font_width+inst->window_border,
3253 y*inst->font_height+inst->window_border,
3254 len*widefactor*inst->font_width, inst->font_height);
3257 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
3258 unsigned long attr, int lattr)
3260 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3261 struct gui_data *inst = dctx->inst;
3263 int active, passive, widefactor;
3265 if (attr & TATTR_PASCURS) {
3266 attr &= ~TATTR_PASCURS;
3270 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
3271 attr &= ~TATTR_ACTCURS;
3275 do_text_internal(ctx, x, y, text, len, attr, lattr);
3277 if (attr & TATTR_COMBINING)
3280 if (attr & ATTR_WIDE) {
3286 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3288 if (x >= inst->term->cols)
3290 if (x + len*2*widefactor > inst->term->cols)
3291 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3295 if (inst->cursor_type == 0) {
3297 * An active block cursor will already have been done by
3298 * the above do_text call, so we only need to do anything
3302 draw_set_colour(dctx, 261);
3303 draw_rectangle(dctx, FALSE,
3304 x*inst->font_width+inst->window_border,
3305 y*inst->font_height+inst->window_border,
3306 len*widefactor*inst->font_width-1,
3307 inst->font_height-1);
3311 int startx, starty, dx, dy, length, i;
3315 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
3316 char_width = 2*inst->font_width;
3318 char_width = inst->font_width;
3320 if (inst->cursor_type == 1) {
3321 uheight = inst->fonts[0]->ascent + 1;
3322 if (uheight >= inst->font_height)
3323 uheight = inst->font_height - 1;
3325 startx = x * inst->font_width + inst->window_border;
3326 starty = y * inst->font_height + inst->window_border + uheight;
3329 length = len * widefactor * char_width;
3332 if (attr & TATTR_RIGHTCURS)
3333 xadjust = char_width - 1;
3334 startx = x * inst->font_width + inst->window_border + xadjust;
3335 starty = y * inst->font_height + inst->window_border;
3338 length = inst->font_height;
3341 draw_set_colour(dctx, 261);
3343 for (i = 0; i < length; i++) {
3345 draw_point(dctx, startx, starty);
3350 } else if (active) {
3351 draw_line(dctx, startx, starty,
3352 startx + (length-1) * dx, starty + (length-1) * dy);
3353 } /* else no cursor (e.g., blinked off) */
3357 x*inst->font_width+inst->window_border,
3358 y*inst->font_height+inst->window_border,
3359 len*widefactor*inst->font_width, inst->font_height);
3361 #if GTK_CHECK_VERSION(2,0,0)
3363 GdkRectangle cursorrect;
3364 cursorrect.x = x*inst->font_width+inst->window_border;
3365 cursorrect.y = y*inst->font_height+inst->window_border;
3366 cursorrect.width = len*widefactor*inst->font_width;
3367 cursorrect.height = inst->font_height;
3368 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
3373 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
3375 if (cursor_val == -1) {
3376 #if GTK_CHECK_VERSION(2,16,0)
3377 cursor_val = GDK_BLANK_CURSOR;
3380 * Work around absence of GDK_BLANK_CURSOR by inventing a
3384 GdkColor bg = { 0, 0, 0, 0 };
3385 GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
3386 GdkGC *gc = gdk_gc_new(pm);
3387 gdk_gc_set_foreground(gc, &bg);
3388 gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
3390 ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
3391 gdk_pixmap_unref(pm);
3396 return gdk_cursor_new(cursor_val);
3399 void modalfatalbox(const char *p, ...)
3402 fprintf(stderr, "FATAL ERROR: ");
3404 vfprintf(stderr, p, ap);
3406 fputc('\n', stderr);
3410 void cmdline_error(const char *p, ...)
3413 fprintf(stderr, "%s: ", appname);
3415 vfprintf(stderr, p, ap);
3417 fputc('\n', stderr);
3421 const char *get_x_display(void *frontend)
3423 return gdk_get_display();
3426 #ifndef NOT_X_WINDOWS
3427 long get_windowid(void *frontend)
3429 struct gui_data *inst = (struct gui_data *)frontend;
3430 return (long)GDK_WINDOW_XID(gtk_widget_get_window(inst->area));
3434 static void help(FILE *fp) {
3436 "pterm option summary:\n"
3438 " --display DISPLAY Specify X display to use (note '--')\n"
3439 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
3440 " -fn FONT Normal text font\n"
3441 " -fb FONT Bold text font\n"
3442 " -geometry GEOMETRY Position and size of window (size in characters)\n"
3443 " -sl LINES Number of lines of scrollback\n"
3444 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
3445 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
3446 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
3447 " -T TITLE Window title\n"
3448 " -ut, +ut Do(default) or do not update utmp\n"
3449 " -ls, +ls Do(default) or do not make shell a login shell\n"
3450 " -sb, +sb Do(default) or do not display a scrollbar\n"
3451 " -log PATH Log all output to a file\n"
3452 " -nethack Map numeric keypad to hjklyubn direction keys\n"
3453 " -xrm RESOURCE-STRING Set an X resource\n"
3454 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
3455 ) < 0 || fflush(fp) < 0) {
3456 perror("output error");
3461 static void version(FILE *fp) {
3462 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
3463 perror("output error");
3468 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
3469 struct gui_data *inst, Conf *conf)
3475 * Macros to make argument handling easier. Note that because
3476 * they need to call `continue', they cannot be contained in
3477 * the usual do {...} while (0) wrapper to make them
3478 * syntactically single statements; hence it is not legal to
3479 * use one of these macros as an unbraced statement between
3482 #define EXPECTS_ARG { \
3483 if (--argc <= 0) { \
3485 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
3490 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
3492 while (--argc > 0) {
3493 const char *p = *++argv;
3497 * Shameless cheating. Debian requires all X terminal
3498 * emulators to support `-T title'; but
3499 * cmdline_process_param will eat -T (it means no-pty) and
3500 * complain that pterm doesn't support it. So, in pterm
3501 * only, we convert -T into -title.
3503 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
3507 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
3508 do_everything ? 1 : -1, conf);
3511 cmdline_error("option \"%s\" requires an argument", p);
3512 } else if (ret == 2) {
3513 --argc, ++argv; /* skip next argument */
3515 } else if (ret == 1) {
3519 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
3523 fs = fontspec_new(val);
3524 conf_set_fontspec(conf, CONF_font, fs);
3527 } else if (!strcmp(p, "-fb")) {
3531 fs = fontspec_new(val);
3532 conf_set_fontspec(conf, CONF_boldfont, fs);
3535 } else if (!strcmp(p, "-fw")) {
3539 fs = fontspec_new(val);
3540 conf_set_fontspec(conf, CONF_widefont, fs);
3543 } else if (!strcmp(p, "-fwb")) {
3547 fs = fontspec_new(val);
3548 conf_set_fontspec(conf, CONF_wideboldfont, fs);
3551 } else if (!strcmp(p, "-cs")) {
3554 conf_set_str(conf, CONF_line_codepage, val);
3556 } else if (!strcmp(p, "-geometry")) {
3560 #if GTK_CHECK_VERSION(2,0,0)
3561 inst->geometry = val;
3563 /* On GTK 1, we have to do this using raw Xlib */
3567 flags = XParseGeometry(val, &x, &y, &w, &h);
3568 if (flags & WidthValue)
3569 conf_set_int(conf, CONF_width, w);
3570 if (flags & HeightValue)
3571 conf_set_int(conf, CONF_height, h);
3573 if (flags & (XValue | YValue)) {
3576 inst->gotpos = TRUE;
3577 inst->gravity = ((flags & XNegative ? 1 : 0) |
3578 (flags & YNegative ? 2 : 0));
3583 } else if (!strcmp(p, "-sl")) {
3586 conf_set_int(conf, CONF_savelines, atoi(val));
3588 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
3589 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
3590 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
3595 #if GTK_CHECK_VERSION(3,0,0)
3597 int success = gdk_rgba_parse(&rgba, val);
3600 int success = gdk_color_parse(val, &col);
3605 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
3608 #if GTK_CHECK_VERSION(3,0,0)
3609 int r = rgba.red * 255;
3610 int g = rgba.green * 255;
3611 int b = rgba.blue * 255;
3613 int r = col.red / 256;
3614 int g = col.green / 256;
3615 int b = col.blue / 256;
3619 index = (!strcmp(p, "-fg") ? 0 :
3620 !strcmp(p, "-bg") ? 2 :
3621 !strcmp(p, "-bfg") ? 1 :
3622 !strcmp(p, "-bbg") ? 3 :
3623 !strcmp(p, "-cfg") ? 4 :
3624 !strcmp(p, "-cbg") ? 5 : -1);
3625 assert(index != -1);
3627 conf_set_int_int(conf, CONF_colours, index*3+0, r);
3628 conf_set_int_int(conf, CONF_colours, index*3+1, g);
3629 conf_set_int_int(conf, CONF_colours, index*3+2, b);
3633 } else if (use_pty_argv && !strcmp(p, "-e")) {
3634 /* This option swallows all further arguments. */
3640 pty_argv = snewn(argc+1, char *);
3642 for (i = 0; i < argc; i++)
3643 pty_argv[i] = argv[i];
3644 pty_argv[argc] = NULL;
3645 break; /* finished command-line processing */
3647 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
3650 } else if (!strcmp(p, "-title")) {
3653 conf_set_str(conf, CONF_wintitle, val);
3655 } else if (!strcmp(p, "-log")) {
3659 fn = filename_from_str(val);
3660 conf_set_filename(conf, CONF_logfilename, fn);
3661 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
3664 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
3666 conf_set_int(conf, CONF_stamp_utmp, 0);
3668 } else if (!strcmp(p, "-ut")) {
3670 conf_set_int(conf, CONF_stamp_utmp, 1);
3672 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
3674 conf_set_int(conf, CONF_login_shell, 0);
3676 } else if (!strcmp(p, "-ls")) {
3678 conf_set_int(conf, CONF_login_shell, 1);
3680 } else if (!strcmp(p, "-nethack")) {
3682 conf_set_int(conf, CONF_nethack_keypad, 1);
3684 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
3686 conf_set_int(conf, CONF_scrollbar, 0);
3688 } else if (!strcmp(p, "-sb")) {
3690 conf_set_int(conf, CONF_scrollbar, 1);
3692 } else if (!strcmp(p, "-name")) {
3696 } else if (!strcmp(p, "-xrm")) {
3698 provide_xrm_string(val);
3700 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
3704 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
3708 } else if (!strcmp(p, "-pgpfp")) {
3712 } else if(p[0] != '-' && (!do_everything ||
3713 process_nonoption_arg(p, conf,
3719 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
3727 #if GTK_CHECK_VERSION(2,0,0)
3735 uxsel_id *uxsel_input_add(int fd, int rwx) {
3736 uxsel_id *id = snew(uxsel_id);
3738 #if GTK_CHECK_VERSION(2,0,0)
3740 if (rwx & 1) flags |= G_IO_IN;
3741 if (rwx & 2) flags |= G_IO_OUT;
3742 if (rwx & 4) flags |= G_IO_PRI;
3743 id->chan = g_io_channel_unix_new(fd);
3744 g_io_channel_set_encoding(id->chan, NULL, NULL);
3745 id->watch_id = g_io_add_watch(id->chan, flags, fd_input_func, NULL);
3748 if (rwx & 1) flags |= GDK_INPUT_READ;
3749 if (rwx & 2) flags |= GDK_INPUT_WRITE;
3750 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
3752 id->id = gdk_input_add(fd, flags, fd_input_func, NULL);
3758 void uxsel_input_remove(uxsel_id *id) {
3759 #if GTK_CHECK_VERSION(2,0,0)
3760 g_source_remove(id->watch_id);
3761 g_io_channel_unref(id->chan);
3763 gdk_input_remove(id->id);
3768 int frontend_is_utf8(void *frontend)
3770 struct gui_data *inst = (struct gui_data *)frontend;
3771 return inst->ucsdata.line_codepage == CS_UTF8;
3774 char *setup_fonts_ucs(struct gui_data *inst)
3776 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
3777 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
3782 fs = conf_get_fontspec(inst->conf, CONF_font);
3783 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
3784 shadowboldoffset, shadowbold);
3786 return dupprintf("unable to load font \"%s\"", fs->name);
3789 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
3790 if (shadowbold || !fs->name[0]) {
3793 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
3794 shadowboldoffset, shadowbold);
3797 unifont_destroy(fonts[0]);
3798 return dupprintf("unable to load bold font \"%s\"", fs->name);
3802 fs = conf_get_fontspec(inst->conf, CONF_widefont);
3804 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
3805 shadowboldoffset, shadowbold);
3807 for (i = 0; i < 2; i++)
3809 unifont_destroy(fonts[i]);
3810 return dupprintf("unable to load wide font \"%s\"", fs->name);
3816 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
3817 if (shadowbold || !fs->name[0]) {
3820 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
3821 shadowboldoffset, shadowbold);
3823 for (i = 0; i < 3; i++)
3825 unifont_destroy(fonts[i]);
3826 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
3831 * Now we've got past all the possible error conditions, we can
3832 * actually update our state.
3835 for (i = 0; i < 4; i++) {
3837 unifont_destroy(inst->fonts[i]);
3838 inst->fonts[i] = fonts[i];
3841 inst->font_width = inst->fonts[0]->width;
3842 inst->font_height = inst->fonts[0]->height;
3844 inst->direct_to_font = init_ucs(&inst->ucsdata,
3845 conf_get_str(inst->conf, CONF_line_codepage),
3846 conf_get_int(inst->conf, CONF_utf8_override),
3847 inst->fonts[0]->public_charset,
3848 conf_get_int(inst->conf, CONF_vtmode));
3850 inst->drawtype = inst->fonts[0]->preferred_drawtype;
3855 void set_geom_hints(struct gui_data *inst)
3858 geom.min_width = inst->font_width + 2*inst->window_border;
3859 geom.min_height = inst->font_height + 2*inst->window_border;
3860 geom.max_width = geom.max_height = -1;
3861 geom.base_width = 2*inst->window_border;
3862 geom.base_height = 2*inst->window_border;
3863 geom.width_inc = inst->font_width;
3864 geom.height_inc = inst->font_height;
3865 geom.min_aspect = geom.max_aspect = 0;
3866 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
3867 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
3868 GDK_HINT_RESIZE_INC);
3871 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
3873 struct gui_data *inst = (struct gui_data *)data;
3874 term_clrsb(inst->term);
3877 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3879 struct gui_data *inst = (struct gui_data *)data;
3880 term_pwron(inst->term, TRUE);
3882 ldisc_echoedit_update(inst->ldisc);
3885 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3887 struct gui_data *inst = (struct gui_data *)data;
3888 term_copyall(inst->term);
3891 void special_menuitem(GtkMenuItem *item, gpointer data)
3893 struct gui_data *inst = (struct gui_data *)data;
3894 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
3898 inst->back->special(inst->backhandle, code);
3901 void about_menuitem(GtkMenuItem *item, gpointer data)
3903 struct gui_data *inst = (struct gui_data *)data;
3904 about_box(inst->window);
3907 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3909 struct gui_data *inst = (struct gui_data *)data;
3910 showeventlog(inst->eventlogstuff, inst->window);
3913 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3915 /* This maps colour indices in inst->conf to those used in inst->cols. */
3916 static const int ww[] = {
3917 256, 257, 258, 259, 260, 261,
3918 0, 8, 1, 9, 2, 10, 3, 11,
3919 4, 12, 5, 13, 6, 14, 7, 15
3921 struct gui_data *inst = (struct gui_data *)data;
3923 Conf *oldconf, *newconf;
3924 int i, j, need_size;
3926 assert(lenof(ww) == NCFGCOLOURS);
3928 if (inst->reconfiguring)
3931 inst->reconfiguring = TRUE;
3933 title = dupcat(appname, " Reconfiguration", NULL);
3935 oldconf = inst->conf;
3936 newconf = conf_copy(inst->conf);
3938 if (do_config_box(title, newconf, 1,
3939 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3940 inst->conf = newconf;
3942 /* Pass new config data to the logging module */
3943 log_reconfig(inst->logctx, inst->conf);
3945 * Flush the line discipline's edit buffer in the case
3946 * where local editing has just been disabled.
3949 ldisc_configure(inst->ldisc, inst->conf);
3950 ldisc_echoedit_update(inst->ldisc);
3952 /* Pass new config data to the terminal */
3953 term_reconfig(inst->term, inst->conf);
3954 /* Pass new config data to the back end */
3956 inst->back->reconfig(inst->backhandle, inst->conf);
3958 cache_conf_values(inst);
3961 * Just setting inst->conf is sufficient to cause colour
3962 * setting changes to appear on the next ESC]R palette
3963 * reset. But we should also check whether any colour
3964 * settings have been changed, and revert the ones that have
3965 * to the new default, on the assumption that the user is
3966 * most likely to want an immediate update.
3968 for (i = 0; i < NCFGCOLOURS; i++) {
3969 for (j = 0; j < 3; j++)
3970 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3971 conf_get_int_int(newconf, CONF_colours, i*3+j))
3974 real_palette_set(inst, ww[i],
3975 conf_get_int_int(newconf,CONF_colours,i*3+0),
3976 conf_get_int_int(newconf,CONF_colours,i*3+1),
3977 conf_get_int_int(newconf,CONF_colours,i*3+2));
3980 * If the default background has changed, we must
3981 * repaint the space in between the window border
3982 * and the text area.
3985 set_window_background(inst);
3986 draw_backing_rect(inst);
3992 * If the scrollbar needs to be shown, hidden, or moved
3993 * from one end to the other of the window, do so now.
3995 if (conf_get_int(oldconf, CONF_scrollbar) !=
3996 conf_get_int(newconf, CONF_scrollbar)) {
3997 if (conf_get_int(newconf, CONF_scrollbar))
3998 gtk_widget_show(inst->sbar);
4000 gtk_widget_hide(inst->sbar);
4002 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
4003 conf_get_int(newconf, CONF_scrollbar_on_left)) {
4004 gtk_box_reorder_child(inst->hbox, inst->sbar,
4005 conf_get_int(newconf, CONF_scrollbar_on_left)
4010 * Change the window title, if required.
4012 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
4013 conf_get_str(newconf, CONF_wintitle)))
4014 set_title(inst, conf_get_str(newconf, CONF_wintitle));
4015 set_window_titles(inst);
4018 * Redo the whole tangled fonts and Unicode mess if
4022 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
4023 conf_get_fontspec(newconf, CONF_font)->name) ||
4024 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
4025 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
4026 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
4027 conf_get_fontspec(newconf, CONF_widefont)->name) ||
4028 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
4029 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
4030 strcmp(conf_get_str(oldconf, CONF_line_codepage),
4031 conf_get_str(newconf, CONF_line_codepage)) ||
4032 conf_get_int(oldconf, CONF_utf8_override) !=
4033 conf_get_int(newconf, CONF_utf8_override) ||
4034 conf_get_int(oldconf, CONF_vtmode) !=
4035 conf_get_int(newconf, CONF_vtmode) ||
4036 conf_get_int(oldconf, CONF_shadowbold) !=
4037 conf_get_int(newconf, CONF_shadowbold) ||
4038 conf_get_int(oldconf, CONF_shadowboldoffset) !=
4039 conf_get_int(newconf, CONF_shadowboldoffset)) {
4040 char *errmsg = setup_fonts_ucs(inst);
4043 dupprintf("Could not change fonts in terminal window: %s\n",
4045 messagebox(inst->window, "Font setup error", msgboxtext,
4046 string_width("Could not change fonts in terminal window:"),
4057 * Resize the window.
4059 if (conf_get_int(oldconf, CONF_width) !=
4060 conf_get_int(newconf, CONF_width) ||
4061 conf_get_int(oldconf, CONF_height) !=
4062 conf_get_int(newconf, CONF_height) ||
4063 conf_get_int(oldconf, CONF_window_border) !=
4064 conf_get_int(newconf, CONF_window_border) ||
4066 set_geom_hints(inst);
4067 request_resize(inst, conf_get_int(newconf, CONF_width),
4068 conf_get_int(newconf, CONF_height));
4071 * The above will have caused a call to term_size() for
4072 * us if it happened. If the user has fiddled with only
4073 * the scrollback size, the above will not have
4074 * happened and we will need an explicit term_size()
4077 if (conf_get_int(oldconf, CONF_savelines) !=
4078 conf_get_int(newconf, CONF_savelines))
4079 term_size(inst->term, inst->term->rows, inst->term->cols,
4080 conf_get_int(newconf, CONF_savelines));
4083 term_invalidate(inst->term);
4086 * We do an explicit full redraw here to ensure the window
4087 * border has been redrawn as well as the text area.
4089 gtk_widget_queue_draw(inst->area);
4096 inst->reconfiguring = FALSE;
4099 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
4102 * Re-execing ourself is not an exact science under Unix. I do
4103 * the best I can by using /proc/self/exe if available and by
4104 * assuming argv[0] can be found on $PATH if not.
4106 * Note that we also have to reconstruct the elements of the
4107 * original argv which gtk swallowed, since the user wants the
4108 * new session to appear on the same X display as the old one.
4116 * Collect the arguments with which to re-exec ourself.
4118 va_start(ap, fd_to_close);
4119 n = 2; /* progname and terminating NULL */
4120 n += inst->ngtkargs;
4121 while (va_arg(ap, char *) != NULL)
4125 args = snewn(n, char *);
4126 args[0] = inst->progname;
4128 for (i = 0; i < inst->ngtkargs; i++)
4129 args[i+1] = inst->gtkargvstart[i];
4132 va_start(ap, fd_to_close);
4133 while ((args[i++] = va_arg(ap, char *)) != NULL);
4139 * Do the double fork.
4153 } else if (pid2 > 0) {
4155 * First child has successfully forked second child. My
4156 * Work Here Is Done. Note the use of _exit rather than
4157 * exit: the latter appears to cause destroy messages
4158 * to be sent to the X server. I suspect gtk uses
4165 * If we reach here, we are the second child, so we now
4166 * actually perform the exec.
4168 if (fd_to_close >= 0)
4171 execv("/proc/self/exe", args);
4172 execvp(inst->progname, args);
4179 waitpid(pid, &status, 0);
4184 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
4186 struct gui_data *inst = (struct gui_data *)gdata;
4188 * For this feature we must marshal conf and (possibly) pty_argv
4189 * into a byte stream, create a pipe, and send this byte stream
4190 * to the child through the pipe.
4192 int i, ret, sersize, size;
4197 if (pipe(pipefd) < 0) {
4202 size = sersize = conf_serialised_size(inst->conf);
4203 if (use_pty_argv && pty_argv) {
4204 for (i = 0; pty_argv[i]; i++)
4205 size += strlen(pty_argv[i]) + 1;
4208 data = snewn(size, char);
4209 conf_serialise(inst->conf, data);
4210 if (use_pty_argv && pty_argv) {
4212 for (i = 0; pty_argv[i]; i++) {
4213 strcpy(data + p, pty_argv[i]);
4214 p += strlen(pty_argv[i]) + 1;
4219 sprintf(option, "---[%d,%d]", pipefd[0], size);
4220 noncloexec(pipefd[0]);
4221 fork_and_exec_self(inst, pipefd[1], option, NULL);
4225 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
4228 perror("write to pipe");
4233 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
4235 int fd, i, ret, size, size_used;
4238 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
4239 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
4243 data = snewn(size, char);
4245 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
4248 perror("read from pipe");
4250 } else if (i < size) {
4251 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
4256 size_used = conf_deserialise(conf, data, size);
4257 if (use_pty_argv && size > size_used) {
4261 while (i < size && data[i]) i++;
4263 fprintf(stderr, "%s: malformed Duplicate Session data\n",
4270 pty_argv = snewn(n+1, char *);
4276 while (i < size && data[i]) i++;
4279 pty_argv[n++] = dupstr(p);
4288 void new_session_menuitem(GtkMenuItem *item, gpointer data)
4290 struct gui_data *inst = (struct gui_data *)data;
4292 fork_and_exec_self(inst, -1, NULL);
4295 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
4297 struct gui_data *inst = (struct gui_data *)data;
4300 logevent(inst, "----- Session restarted -----");
4301 term_pwron(inst->term, FALSE);
4302 start_backend(inst);
4303 inst->exited = FALSE;
4307 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
4309 struct gui_data *inst = (struct gui_data *)data;
4310 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4312 fork_and_exec_self(inst, -1, "-load", str, NULL);
4315 void saved_session_freedata(GtkMenuItem *item, gpointer data)
4317 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4322 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
4324 struct gui_data *inst = (struct gui_data *)data;
4325 struct sesslist sesslist;
4328 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
4329 (GtkCallback)gtk_widget_destroy, NULL);
4331 get_sesslist(&sesslist, TRUE);
4332 /* skip sesslist.sessions[0] == Default Settings */
4333 for (i = 1; i < sesslist.nsessions; i++) {
4334 GtkWidget *menuitem =
4335 gtk_menu_item_new_with_label(sesslist.sessions[i]);
4336 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4337 gtk_widget_show(menuitem);
4338 g_object_set_data(G_OBJECT(menuitem), "user-data",
4339 dupstr(sesslist.sessions[i]));
4340 g_signal_connect(G_OBJECT(menuitem), "activate",
4341 G_CALLBACK(saved_session_menuitem),
4343 g_signal_connect(G_OBJECT(menuitem), "destroy",
4344 G_CALLBACK(saved_session_freedata),
4347 if (sesslist.nsessions <= 1) {
4348 GtkWidget *menuitem =
4349 gtk_menu_item_new_with_label("(No sessions)");
4350 gtk_widget_set_sensitive(menuitem, FALSE);
4351 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4352 gtk_widget_show(menuitem);
4354 get_sesslist(&sesslist, FALSE); /* free up */
4357 void set_window_icon(GtkWidget *window, const char *const *const *icon,
4360 #if GTK_CHECK_VERSION(2,0,0)
4365 GdkBitmap *iconmask;
4371 gtk_widget_realize(window);
4372 #if GTK_CHECK_VERSION(2,0,0)
4373 gtk_window_set_icon(GTK_WINDOW(window),
4374 gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
4376 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
4377 &iconmask, NULL, (gchar **)icon[0]);
4378 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
4381 #if GTK_CHECK_VERSION(2,0,0)
4383 for (n = 0; n < n_icon; n++) {
4385 g_list_append(iconlist,
4386 gdk_pixbuf_new_from_xpm_data((const gchar **)
4389 gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
4393 void update_specials_menu(void *frontend)
4395 struct gui_data *inst = (struct gui_data *)frontend;
4397 const struct telnet_special *specials;
4400 specials = inst->back->get_specials(inst->backhandle);
4404 /* I believe this disposes of submenus too. */
4405 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
4406 (GtkCallback)gtk_widget_destroy, NULL);
4409 GtkWidget *menu = inst->specialsmenu;
4410 /* A lame "stack" for submenus that will do for now. */
4411 GtkWidget *saved_menu = NULL;
4413 for (i = 0; nesting > 0; i++) {
4414 GtkWidget *menuitem = NULL;
4415 switch (specials[i].code) {
4417 assert (nesting < 2);
4418 saved_menu = menu; /* XXX lame stacking */
4419 menu = gtk_menu_new();
4420 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4421 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
4422 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
4423 gtk_widget_show(menuitem);
4430 menu = saved_menu; /* XXX lame stacking */
4435 menuitem = gtk_menu_item_new();
4438 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4439 g_object_set_data(G_OBJECT(menuitem), "user-data",
4440 GINT_TO_POINTER(specials[i].code));
4441 g_signal_connect(G_OBJECT(menuitem), "activate",
4442 G_CALLBACK(special_menuitem), inst);
4446 gtk_container_add(GTK_CONTAINER(menu), menuitem);
4447 gtk_widget_show(menuitem);
4450 gtk_widget_show(inst->specialsitem1);
4451 gtk_widget_show(inst->specialsitem2);
4453 gtk_widget_hide(inst->specialsitem1);
4454 gtk_widget_hide(inst->specialsitem2);
4458 static void start_backend(struct gui_data *inst)
4460 extern Backend *select_backend(Conf *conf);
4465 inst->back = select_backend(inst->conf);
4467 error = inst->back->init((void *)inst, &inst->backhandle,
4469 conf_get_str(inst->conf, CONF_host),
4470 conf_get_int(inst->conf, CONF_port),
4472 conf_get_int(inst->conf, CONF_tcp_nodelay),
4473 conf_get_int(inst->conf, CONF_tcp_keepalives));
4476 char *msg = dupprintf("Unable to open connection to %s:\n%s",
4477 conf_get_str(inst->conf, CONF_host), error);
4478 inst->exited = TRUE;
4479 fatal_message_box(inst->window, msg);
4484 s = conf_get_str(inst->conf, CONF_wintitle);
4486 set_title_and_icon(inst, s, s);
4488 char *title = make_default_wintitle(realhost);
4489 set_title_and_icon(inst, title, title);
4494 inst->back->provide_logctx(inst->backhandle, inst->logctx);
4496 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
4499 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
4502 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4505 int pt_main(int argc, char **argv)
4507 extern int cfgbox(Conf *conf);
4508 struct gui_data *inst;
4510 setlocale(LC_CTYPE, "");
4513 * Create an instance structure and initialise to zeroes
4515 inst = snew(struct gui_data);
4516 memset(inst, 0, sizeof(*inst));
4517 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
4518 inst->busy_status = BUSY_NOT;
4519 inst->conf = conf_new();
4520 inst->wintitle = inst->icontitle = NULL;
4521 inst->quit_fn_scheduled = FALSE;
4522 inst->idle_fn_scheduled = FALSE;
4523 inst->drawtype = DRAWTYPE_DEFAULT;
4525 /* defer any child exit handling until we're ready to deal with
4527 block_signal(SIGCHLD, 1);
4529 inst->progname = argv[0];
4531 * Copy the original argv before letting gtk_init fiddle with
4532 * it. It will be required later.
4536 inst->gtkargvstart = snewn(argc-1, char *);
4537 for (i = 1; i < argc; i++)
4538 inst->gtkargvstart[i-1] = dupstr(argv[i]);
4540 gtk_init(&argc, &argv);
4541 inst->ngtkargs = oldargc - argc;
4544 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
4545 read_dupsession_data(inst, inst->conf, argv[1]);
4546 /* Splatter this argument so it doesn't clutter a ps listing */
4547 smemclr(argv[1], strlen(argv[1]));
4549 /* By default, we bring up the config dialog, rather than launching
4550 * a session. This gets set to TRUE if something happens to change
4551 * that (e.g., a hostname is specified on the command-line). */
4552 int allow_launch = FALSE;
4553 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
4554 exit(1); /* pre-defaults pass to get -class */
4555 do_defaults(NULL, inst->conf);
4556 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
4557 exit(1); /* post-defaults, do everything */
4559 cmdline_run_saved(inst->conf);
4562 allow_launch = TRUE;
4564 if ((!allow_launch || !conf_launchable(inst->conf)) &&
4565 !cfgbox(inst->conf))
4566 exit(0); /* config box hit Cancel */
4569 if (!compound_text_atom)
4570 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
4571 if (!utf8_string_atom)
4572 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
4574 inst->area = gtk_drawing_area_new();
4576 #if GTK_CHECK_VERSION(2,0,0)
4577 inst->imc = gtk_im_multicontext_new();
4581 char *errmsg = setup_fonts_ucs(inst);
4583 fprintf(stderr, "%s: %s\n", appname, errmsg);
4587 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4589 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
4591 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
4592 winclass, winclass);
4596 * Set up the colour map.
4598 palette_reset(inst);
4600 inst->width = conf_get_int(inst->conf, CONF_width);
4601 inst->height = conf_get_int(inst->conf, CONF_height);
4602 cache_conf_values(inst);
4604 init_clipboard(inst);
4607 int w = inst->font_width * inst->width + 2*inst->window_border;
4608 int h = inst->font_height * inst->height + 2*inst->window_border;
4609 #if GTK_CHECK_VERSION(2,0,0)
4610 gtk_widget_set_size_request(inst->area, w, h);
4612 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
4615 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
4616 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
4617 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
4619 * We always create the scrollbar; it remains invisible if
4620 * unwanted, so we can pop it up quickly if it suddenly becomes
4623 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
4624 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4625 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
4626 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
4627 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4629 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
4631 set_geom_hints(inst);
4633 gtk_widget_show(inst->area);
4634 if (conf_get_int(inst->conf, CONF_scrollbar))
4635 gtk_widget_show(inst->sbar);
4637 gtk_widget_hide(inst->sbar);
4638 gtk_widget_show(GTK_WIDGET(inst->hbox));
4640 #if GTK_CHECK_VERSION(2,0,0)
4641 if (inst->geometry) {
4642 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
4646 int x = inst->xpos, y = inst->ypos;
4648 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
4649 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
4650 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
4651 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
4652 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
4656 g_signal_connect(G_OBJECT(inst->window), "destroy",
4657 G_CALLBACK(destroy), inst);
4658 g_signal_connect(G_OBJECT(inst->window), "delete_event",
4659 G_CALLBACK(delete_window), inst);
4660 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
4661 G_CALLBACK(key_event), inst);
4662 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
4663 G_CALLBACK(key_event), inst);
4664 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
4665 G_CALLBACK(focus_event), inst);
4666 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
4667 G_CALLBACK(focus_event), inst);
4668 g_signal_connect(G_OBJECT(inst->area), "configure_event",
4669 G_CALLBACK(configure_area), inst);
4670 #if GTK_CHECK_VERSION(3,0,0)
4671 g_signal_connect(G_OBJECT(inst->area), "draw",
4672 G_CALLBACK(draw_area), inst);
4674 g_signal_connect(G_OBJECT(inst->area), "expose_event",
4675 G_CALLBACK(expose_area), inst);
4677 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
4678 G_CALLBACK(button_event), inst);
4679 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
4680 G_CALLBACK(button_event), inst);
4681 #if GTK_CHECK_VERSION(2,0,0)
4682 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
4683 G_CALLBACK(scroll_event), inst);
4685 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
4686 G_CALLBACK(motion_event), inst);
4687 #if GTK_CHECK_VERSION(2,0,0)
4688 g_signal_connect(G_OBJECT(inst->imc), "commit",
4689 G_CALLBACK(input_method_commit_event), inst);
4691 if (conf_get_int(inst->conf, CONF_scrollbar))
4692 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
4693 G_CALLBACK(scrollbar_moved), inst);
4694 gtk_widget_add_events(GTK_WIDGET(inst->area),
4695 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
4696 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
4697 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
4700 extern const char *const *const main_icon[];
4701 extern const int n_main_icon;
4702 set_window_icon(inst->window, main_icon, n_main_icon);
4705 gtk_widget_show(inst->window);
4707 set_window_background(inst);
4710 * Set up the Ctrl+rightclick context menu.
4713 GtkWidget *menuitem;
4715 extern const int use_event_log, new_session, saved_sessions;
4717 inst->menu = gtk_menu_new();
4719 #define MKMENUITEM(title, func) do \
4721 menuitem = gtk_menu_item_new_with_label(title); \
4722 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4723 gtk_widget_show(menuitem); \
4724 g_signal_connect(G_OBJECT(menuitem), "activate", \
4725 G_CALLBACK(func), inst); \
4728 #define MKSUBMENU(title) do \
4730 menuitem = gtk_menu_item_new_with_label(title); \
4731 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4732 gtk_widget_show(menuitem); \
4735 #define MKSEP() do \
4737 menuitem = gtk_menu_item_new(); \
4738 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4739 gtk_widget_show(menuitem); \
4743 MKMENUITEM("New Session...", new_session_menuitem);
4744 MKMENUITEM("Restart Session", restart_session_menuitem);
4745 inst->restartitem = menuitem;
4746 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4747 MKMENUITEM("Duplicate Session", dup_session_menuitem);
4748 if (saved_sessions) {
4749 inst->sessionsmenu = gtk_menu_new();
4750 /* sessionsmenu will be updated when it's invoked */
4751 /* XXX is this the right way to do dynamic menus in Gtk? */
4752 MKMENUITEM("Saved Sessions", update_savedsess_menu);
4753 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
4754 inst->sessionsmenu);
4757 MKMENUITEM("Change Settings...", change_settings_menuitem);
4760 MKMENUITEM("Event Log", event_log_menuitem);
4761 MKSUBMENU("Special Commands");
4762 inst->specialsmenu = gtk_menu_new();
4763 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
4764 inst->specialsitem1 = menuitem;
4766 inst->specialsitem2 = menuitem;
4767 gtk_widget_hide(inst->specialsitem1);
4768 gtk_widget_hide(inst->specialsitem2);
4769 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
4770 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
4771 MKMENUITEM("Copy All", copy_all_menuitem);
4773 s = dupcat("About ", appname, NULL);
4774 MKMENUITEM(s, about_menuitem);
4781 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
4782 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
4783 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
4784 inst->blankcursor = make_mouse_ptr(inst, -1);
4785 inst->currcursor = inst->textcursor;
4786 show_mouseptr(inst, 1);
4788 inst->eventlogstuff = eventlogstuff_new();
4790 request_callback_notifications(notify_toplevel_callback, inst);
4792 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
4793 inst->logctx = log_init(inst, inst->conf);
4794 term_provide_logctx(inst->term, inst->logctx);
4798 term_size(inst->term, inst->height, inst->width,
4799 conf_get_int(inst->conf, CONF_savelines));
4801 start_backend(inst);
4803 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
4805 /* now we're reday to deal with the child exit handler being
4807 block_signal(SIGCHLD, 0);
4810 * Block SIGPIPE: if we attempt Duplicate Session or similar
4811 * and it falls over in some way, we certainly don't want
4812 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
4813 * this _after_ (at least pterm) forks off its child process,
4814 * since the child wants SIGPIPE handled in the usual way.
4816 block_signal(SIGPIPE, 1);
4818 inst->exited = FALSE;