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>
49 /* Colours come in two flavours: configurable, and xterm-extended. */
50 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
51 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
53 GdkAtom compound_text_atom, utf8_string_atom;
55 struct clipboard_data_instance;
58 GtkWidget *window, *area, *sbar;
59 gboolean sbar_visible;
61 GtkAdjustment *sbar_adjust;
62 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
64 GtkWidget *sessionsmenu;
65 #ifndef NO_BACKING_PIXMAPS
67 * Server-side pixmap which we use to cache the terminal window's
68 * contents. When we draw text in the terminal, we draw it to this
69 * pixmap first, and then blit from there to the actual window;
70 * this way, X expose events can be handled with an absolute
71 * minimum of network traffic, by just sending a command to
72 * re-blit an appropriate rectangle from this pixmap.
76 #ifdef DRAW_TEXT_CAIRO
78 * If we're drawing using Cairo, we cache the same image on the
79 * client side in a Cairo surface.
81 * In GTK2+Cairo, this happens _as well_ as having the server-side
82 * pixmap cache above; in GTK3+Cairo, server-side pixmaps are
83 * deprecated, so we _just_ have this client-side cache. In the
84 * latter case that means we have to transmit a big wodge of
85 * bitmap data over the X connection on every expose event; but
86 * GTK3 apparently deliberately provides no way to avoid that
87 * inefficiency, and at least this way we don't _also_ have to
88 * redo any font rendering just because the window was temporarily
91 cairo_surface_t *surface;
93 #if GTK_CHECK_VERSION(2,0,0)
96 unifont *fonts[4]; /* normal, bold, wide, widebold */
97 #if GTK_CHECK_VERSION(2,0,0)
100 int xpos, ypos, gotpos, gravity;
102 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
103 GdkColor cols[NALLCOLOURS];
104 #if !GTK_CHECK_VERSION(3,0,0)
108 wchar_t *pastein_data;
109 int pastein_data_len;
110 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
111 GtkClipboard *clipboard;
112 struct clipboard_data_instance *current_cdi;
114 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
115 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
117 int font_width, font_height;
120 int mouseptr_visible;
126 int master_fd, master_func_id;
133 struct unicode_data ucsdata;
136 guint32 input_event_time; /* Timestamp of the most recent input event. */
138 #if GTK_CHECK_VERSION(3,4,0)
139 gdouble cumulative_scroll;
141 /* Cached things out of conf that we refer to a lot */
149 static void cache_conf_values(struct gui_data *inst)
151 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
152 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
153 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
154 #ifdef OSX_META_KEY_CONFIG
155 inst->meta_mod_mask = 0;
156 if (conf_get_int(inst->conf, CONF_osx_option_meta))
157 inst->meta_mod_mask |= GDK_MOD1_MASK;
158 if (conf_get_int(inst->conf, CONF_osx_command_meta))
159 inst->meta_mod_mask |= GDK_MOD2_MASK;
161 inst->meta_mod_mask = GDK_MOD1_MASK;
166 struct gui_data *inst;
167 unifont_drawctx uctx;
170 static int send_raw_mouse;
172 static void start_backend(struct gui_data *inst);
173 static void exit_callback(void *vinst);
175 void connection_fatal(void *frontend, const char *p, ...)
177 struct gui_data *inst = (struct gui_data *)frontend;
182 msg = dupvprintf(p, ap);
184 fatal_message_box(inst->window, msg);
187 queue_toplevel_callback(exit_callback, inst);
191 * Default settings that are specific to pterm.
193 FontSpec *platform_default_fontspec(const char *name)
195 if (!strcmp(name, "Font"))
196 return fontspec_new(DEFAULT_GTK_FONT);
198 return fontspec_new("");
201 Filename *platform_default_filename(const char *name)
203 if (!strcmp(name, "LogFileName"))
204 return filename_from_str("putty.log");
206 return filename_from_str("");
209 char *platform_default_s(const char *name)
211 if (!strcmp(name, "SerialLine"))
212 return dupstr("/dev/ttyS0");
216 int platform_default_i(const char *name, int def)
218 if (!strcmp(name, "CloseOnExit"))
219 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
220 if (!strcmp(name, "WinNameAlways"))
221 return 0; /* X natively supports icon titles, so use 'em by default */
225 /* Dummy routine, only required in plink. */
226 void frontend_echoedit_update(void *frontend, int echo, int edit)
230 char *get_ttymode(void *frontend, const char *mode)
232 struct gui_data *inst = (struct gui_data *)frontend;
233 return term_get_ttymode(inst->term, mode);
236 int from_backend(void *frontend, int is_stderr, const char *data, int len)
238 struct gui_data *inst = (struct gui_data *)frontend;
239 return term_data(inst->term, is_stderr, data, len);
242 int from_backend_untrusted(void *frontend, const char *data, int len)
244 struct gui_data *inst = (struct gui_data *)frontend;
245 return term_data_untrusted(inst->term, data, len);
248 int from_backend_eof(void *frontend)
250 return TRUE; /* do respond to incoming EOF with outgoing */
253 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
255 struct gui_data *inst = (struct gui_data *)p->frontend;
257 ret = cmdline_get_passwd_input(p, in, inlen);
259 ret = term_get_userpass_input(inst->term, p, in, inlen);
263 void logevent(void *frontend, const char *string)
265 struct gui_data *inst = (struct gui_data *)frontend;
267 log_eventlog(inst->logctx, string);
269 logevent_dlg(inst->eventlogstuff, string);
272 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
274 struct gui_data *inst = (struct gui_data *)frontend;
277 return inst->font_height;
279 return inst->font_width;
283 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
284 * into a cooked one (SELECT, EXTEND, PASTE).
286 * In Unix, this is not configurable; the X button arrangement is
287 * rock-solid across all applications, everyone has a three-button
288 * mouse or a means of faking it, and there is no need to switch
289 * buttons around at all.
291 static Mouse_Button translate_button(Mouse_Button button)
293 /* struct gui_data *inst = (struct gui_data *)frontend; */
295 if (button == MBT_LEFT)
297 if (button == MBT_MIDDLE)
299 if (button == MBT_RIGHT)
301 return 0; /* shouldn't happen */
305 * Return the top-level GtkWindow associated with a particular
306 * front end instance.
308 void *get_window(void *frontend)
310 struct gui_data *inst = (struct gui_data *)frontend;
315 * Minimise or restore the window in response to a server-side
318 void set_iconic(void *frontend, int iconic)
321 * GTK 1.2 doesn't know how to do this.
323 #if GTK_CHECK_VERSION(2,0,0)
324 struct gui_data *inst = (struct gui_data *)frontend;
326 gtk_window_iconify(GTK_WINDOW(inst->window));
328 gtk_window_deiconify(GTK_WINDOW(inst->window));
333 * Move the window in response to a server-side request.
335 void move_window(void *frontend, int x, int y)
337 struct gui_data *inst = (struct gui_data *)frontend;
339 * I assume that when the GTK version of this call is available
340 * we should use it. Not sure how it differs from the GDK one,
343 #if GTK_CHECK_VERSION(2,0,0)
344 gtk_window_move(GTK_WINDOW(inst->window), x, y);
346 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
351 * Move the window to the top or bottom of the z-order in response
352 * to a server-side request.
354 void set_zorder(void *frontend, int top)
356 struct gui_data *inst = (struct gui_data *)frontend;
358 gdk_window_raise(gtk_widget_get_window(inst->window));
360 gdk_window_lower(gtk_widget_get_window(inst->window));
364 * Refresh the window in response to a server-side request.
366 void refresh_window(void *frontend)
368 struct gui_data *inst = (struct gui_data *)frontend;
369 term_invalidate(inst->term);
373 * Maximise or restore the window in response to a server-side
376 void set_zoomed(void *frontend, int zoomed)
379 * GTK 1.2 doesn't know how to do this.
381 #if GTK_CHECK_VERSION(2,0,0)
382 struct gui_data *inst = (struct gui_data *)frontend;
384 gtk_window_maximize(GTK_WINDOW(inst->window));
386 gtk_window_unmaximize(GTK_WINDOW(inst->window));
391 * Report whether the window is iconic, for terminal reports.
393 int is_iconic(void *frontend)
395 struct gui_data *inst = (struct gui_data *)frontend;
396 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
400 * Report the window's position, for terminal reports.
402 void get_window_pos(void *frontend, int *x, int *y)
404 struct gui_data *inst = (struct gui_data *)frontend;
406 * I assume that when the GTK version of this call is available
407 * we should use it. Not sure how it differs from the GDK one,
410 #if GTK_CHECK_VERSION(2,0,0)
411 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
413 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
418 * Report the window's pixel size, for terminal reports.
420 void get_window_pixels(void *frontend, int *x, int *y)
422 struct gui_data *inst = (struct gui_data *)frontend;
424 * I assume that when the GTK version of this call is available
425 * we should use it. Not sure how it differs from the GDK one,
428 #if GTK_CHECK_VERSION(2,0,0)
429 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
431 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
436 * Return the window or icon title.
438 char *get_window_title(void *frontend, int icon)
440 struct gui_data *inst = (struct gui_data *)frontend;
441 return icon ? inst->icontitle : inst->wintitle;
444 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
446 struct gui_data *inst = (struct gui_data *)data;
447 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
448 if (!reallyclose(inst))
454 static void update_mouseptr(struct gui_data *inst)
456 switch (inst->busy_status) {
458 if (!inst->mouseptr_visible) {
459 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
461 } else if (send_raw_mouse) {
462 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
465 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
469 case BUSY_WAITING: /* XXX can we do better? */
471 /* We always display these cursors. */
472 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
480 static void show_mouseptr(struct gui_data *inst, int show)
482 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
484 inst->mouseptr_visible = show;
485 update_mouseptr(inst);
488 static void draw_backing_rect(struct gui_data *inst);
490 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
492 struct gui_data *inst = (struct gui_data *)data;
493 int w, h, need_size = 0;
496 * See if the terminal size has changed, in which case we must
497 * let the terminal know.
499 w = (event->width - 2*inst->window_border) / inst->font_width;
500 h = (event->height - 2*inst->window_border) / inst->font_height;
501 if (w != inst->width || h != inst->height) {
504 conf_set_int(inst->conf, CONF_width, inst->width);
505 conf_set_int(inst->conf, CONF_height, inst->height);
510 int backing_w = w * inst->font_width + 2*inst->window_border;
511 int backing_h = h * inst->font_height + 2*inst->window_border;
513 #ifndef NO_BACKING_PIXMAPS
515 gdk_pixmap_unref(inst->pixmap);
519 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
520 backing_w, backing_h, -1);
523 #ifdef DRAW_TEXT_CAIRO
525 cairo_surface_destroy(inst->surface);
526 inst->surface = NULL;
529 inst->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
530 backing_w, backing_h);
534 draw_backing_rect(inst);
536 if (need_size && inst->term) {
537 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
541 term_invalidate(inst->term);
543 #if GTK_CHECK_VERSION(2,0,0)
544 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
550 #ifdef DRAW_TEXT_CAIRO
551 static void cairo_setup_dctx(struct draw_ctx *dctx)
553 cairo_get_matrix(dctx->uctx.u.cairo.cr,
554 &dctx->uctx.u.cairo.origmatrix);
555 cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
556 cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
557 cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
558 /* This antialiasing setting appears to be ignored for Pango
559 * font rendering but honoured for stroking and filling paths;
560 * I don't quite understand the logic of that, but I won't
561 * complain since it's exactly what I happen to want */
562 cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
566 #if GTK_CHECK_VERSION(3,0,0)
567 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
569 struct gui_data *inst = (struct gui_data *)data;
572 * GTK3 window redraw: we always expect Cairo to be enabled, so
573 * that inst->surface exists, and pixmaps to be disabled, so that
574 * inst->pixmap does not exist. Hence, we just blit from
575 * inst->surface to the window.
578 GdkRectangle dirtyrect;
580 gdk_cairo_get_clip_rectangle(cr, &dirtyrect);
582 cairo_set_source_surface(cr, inst->surface, 0, 0);
583 cairo_rectangle(cr, dirtyrect.x, dirtyrect.y,
584 dirtyrect.width, dirtyrect.height);
591 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
593 struct gui_data *inst = (struct gui_data *)data;
595 #ifndef NO_BACKING_PIXMAPS
597 * Draw to the exposed part of the window from the server-side
601 gdk_draw_pixmap(gtk_widget_get_window(widget),
602 (gtk_widget_get_style(widget)->fg_gc
603 [gtk_widget_get_state(widget)]),
605 event->area.x, event->area.y,
606 event->area.x, event->area.y,
607 event->area.width, event->area.height);
611 * Failing that, draw from the client-side Cairo surface. (We
612 * should never be compiled in a context where we have _neither_
613 * inst->surface nor inst->pixmap.)
616 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
617 cairo_set_source_surface(cr, inst->surface, 0, 0);
618 cairo_rectangle(cr, event->area.x, event->area.y,
619 event->area.width, event->area.height);
629 #define KEY_PRESSED(k) \
630 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
632 #ifdef KEY_EVENT_DIAGNOSTICS
633 char *dup_keyval_name(guint keyval)
635 const char *name = gdk_keyval_name(keyval);
639 return dupprintf("UNKNOWN[%u]", (unsigned)keyval);
643 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
645 struct gui_data *inst = (struct gui_data *)data;
647 wchar_t ucsoutput[2];
648 int ucsval, start, end, special, output_charset, use_ucsoutput;
649 int nethack_mode, app_keypad_mode;
651 /* Remember the timestamp. */
652 inst->input_event_time = event->time;
654 /* By default, nothing is generated. */
656 special = use_ucsoutput = FALSE;
657 output_charset = CS_ISO8859_1;
659 #ifdef KEY_EVENT_DIAGNOSTICS
661 char *type_string, *state_string, *keyval_string, *string_string;
663 type_string = (event->type == GDK_KEY_PRESS ? dupstr("PRESS") :
664 event->type == GDK_KEY_RELEASE ? dupstr("RELEASE") :
665 dupprintf("UNKNOWN[%d]", (int)event->type));
668 static const struct {
672 {GDK_SHIFT_MASK, "SHIFT"},
673 {GDK_LOCK_MASK, "LOCK"},
674 {GDK_CONTROL_MASK, "CONTROL"},
675 {GDK_MOD1_MASK, "MOD1"},
676 {GDK_MOD2_MASK, "MOD2"},
677 {GDK_MOD3_MASK, "MOD3"},
678 {GDK_MOD4_MASK, "MOD4"},
679 {GDK_MOD5_MASK, "MOD5"},
680 {GDK_SUPER_MASK, "SUPER"},
681 {GDK_HYPER_MASK, "HYPER"},
682 {GDK_META_MASK, "META"},
685 int val = event->state;
687 state_string = dupstr("");
689 for (i = 0; i < lenof(mod_bits); i++) {
690 if (val & mod_bits[i].mod_bit) {
691 char *old = state_string;
692 state_string = dupcat(state_string,
693 state_string[0] ? "|" : "",
698 val &= ~mod_bits[i].mod_bit;
702 if (val || !state_string[0]) {
703 char *old = state_string;
704 state_string = dupprintf("%s%s%d", state_string,
705 state_string[0] ? "|" : "", val);
710 keyval_string = dup_keyval_name(event->keyval);
712 string_string = dupstr("");
715 for (i = 0; event->string[i]; i++) {
716 char *old = string_string;
717 string_string = dupprintf("%s%s%02x", string_string,
718 string_string[0] ? " " : "",
719 (unsigned)event->string[i] & 0xFF);
724 debug(("key_event: type=%s keyval=%s state=%s "
725 "hardware_keycode=%d is_modifier=%s string=[%s]\n",
726 type_string, keyval_string, state_string,
727 (int)event->hardware_keycode,
728 event->is_modifier ? "TRUE" : "FALSE",
733 sfree(keyval_string);
734 sfree(string_string);
736 #endif /* KEY_EVENT_DIAGNOSTICS */
739 * If Alt is being released after typing an Alt+numberpad
740 * sequence, we should generate the code that was typed.
742 * Note that we only do this if more than one key was actually
743 * pressed - I don't think Alt+NumPad4 should be ^D or that
744 * Alt+NumPad3 should be ^C, for example. There's no serious
745 * inconvenience in having to type a zero before a single-digit
748 if (event->type == GDK_KEY_RELEASE) {
749 if ((event->keyval == GDK_KEY_Meta_L ||
750 event->keyval == GDK_KEY_Meta_R ||
751 event->keyval == GDK_KEY_Alt_L ||
752 event->keyval == GDK_KEY_Alt_R) &&
753 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
754 #ifdef KEY_EVENT_DIAGNOSTICS
755 debug((" - modifier release terminates Alt+numberpad input, "
756 "keycode = %d\n", inst->alt_keycode));
759 * FIXME: we might usefully try to do something clever here
760 * about interpreting the generated key code in a way that's
761 * appropriate to the line code page.
763 output[0] = inst->alt_keycode;
767 #if GTK_CHECK_VERSION(2,0,0)
768 #ifdef KEY_EVENT_DIAGNOSTICS
769 debug((" - key release, passing to IM\n"));
771 if (gtk_im_context_filter_keypress(inst->imc, event)) {
772 #ifdef KEY_EVENT_DIAGNOSTICS
773 debug((" - key release accepted by IM\n"));
777 #ifdef KEY_EVENT_DIAGNOSTICS
778 debug((" - key release not accepted by IM\n"));
784 if (event->type == GDK_KEY_PRESS) {
786 * If Alt has just been pressed, we start potentially
787 * accumulating an Alt+numberpad code. We do this by
788 * setting alt_keycode to -1 (nothing yet but plausible).
790 if ((event->keyval == GDK_KEY_Meta_L ||
791 event->keyval == GDK_KEY_Meta_R ||
792 event->keyval == GDK_KEY_Alt_L ||
793 event->keyval == GDK_KEY_Alt_R)) {
794 #ifdef KEY_EVENT_DIAGNOSTICS
795 debug((" - modifier press potentially begins Alt+numberpad "
798 inst->alt_keycode = -1;
799 inst->alt_digits = 0;
800 goto done; /* this generates nothing else */
804 * If we're seeing a numberpad key press with Meta down,
805 * consider adding it to alt_keycode if that's sensible.
806 * Anything _else_ with Meta down cancels any possibility
807 * of an ALT keycode: we set alt_keycode to -2.
809 if ((event->state & inst->meta_mod_mask) && inst->alt_keycode != -2) {
811 switch (event->keyval) {
812 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
813 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
814 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
815 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
816 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
817 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
818 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
819 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
820 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
821 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
824 inst->alt_keycode = -2; /* it's invalid */
826 #ifdef KEY_EVENT_DIAGNOSTICS
827 int old_keycode = inst->alt_keycode;
829 if (inst->alt_keycode == -1)
830 inst->alt_keycode = digit; /* one-digit code */
832 inst->alt_keycode = inst->alt_keycode * 10 + digit;
834 #ifdef KEY_EVENT_DIAGNOSTICS
835 debug((" - Alt+numberpad digit %d added to keycode %d"
836 " gives %d\n", digit, old_keycode, inst->alt_keycode));
838 /* Having used this digit, we now do nothing more with it. */
844 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
847 if (event->keyval == GDK_KEY_Page_Up &&
848 (event->state & GDK_SHIFT_MASK)) {
849 #ifdef KEY_EVENT_DIAGNOSTICS
850 debug((" - Shift-PgUp scroll\n"));
852 term_scroll(inst->term, 0, -inst->height/2);
855 if (event->keyval == GDK_KEY_Page_Up &&
856 (event->state & GDK_CONTROL_MASK)) {
857 #ifdef KEY_EVENT_DIAGNOSTICS
858 debug((" - Ctrl-PgUp scroll\n"));
860 term_scroll(inst->term, 0, -1);
863 if (event->keyval == GDK_KEY_Page_Down &&
864 (event->state & GDK_SHIFT_MASK)) {
865 #ifdef KEY_EVENT_DIAGNOSTICS
866 debug((" - Shift-PgDn scroll\n"));
868 term_scroll(inst->term, 0, +inst->height/2);
871 if (event->keyval == GDK_KEY_Page_Down &&
872 (event->state & GDK_CONTROL_MASK)) {
873 #ifdef KEY_EVENT_DIAGNOSTICS
874 debug((" - Ctrl-PgDn scroll\n"));
876 term_scroll(inst->term, 0, +1);
881 * Neither does Shift-Ins.
883 if (event->keyval == GDK_KEY_Insert &&
884 (event->state & GDK_SHIFT_MASK)) {
885 #ifdef KEY_EVENT_DIAGNOSTICS
886 debug((" - Shift-Insert paste\n"));
893 use_ucsoutput = FALSE;
895 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
896 app_keypad_mode = (inst->term->app_keypad_keys &&
897 !conf_get_int(inst->conf, CONF_no_applic_k));
899 /* ALT+things gives leading Escape. */
901 #if !GTK_CHECK_VERSION(2,0,0)
903 * In vanilla X, and hence also GDK 1.2, the string received
904 * as part of a keyboard event is assumed to be in
905 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
906 * but it's true: see the man page for XLookupString(3) for
909 output_charset = CS_ISO8859_1;
910 strncpy(output+1, event->string, lenof(output)-1);
911 #else /* !GTK_CHECK_VERSION(2,0,0) */
913 * Most things can now be passed to
914 * gtk_im_context_filter_keypress without breaking anything
915 * below this point. An exception is the numeric keypad if
916 * we're in Nethack or application mode: the IM will eat
917 * numeric keypad presses if Num Lock is on, but we don't want
920 if (app_keypad_mode &&
921 (event->keyval == GDK_KEY_Num_Lock ||
922 event->keyval == GDK_KEY_KP_Divide ||
923 event->keyval == GDK_KEY_KP_Multiply ||
924 event->keyval == GDK_KEY_KP_Subtract ||
925 event->keyval == GDK_KEY_KP_Add ||
926 event->keyval == GDK_KEY_KP_Enter ||
927 event->keyval == GDK_KEY_KP_0 ||
928 event->keyval == GDK_KEY_KP_Insert ||
929 event->keyval == GDK_KEY_KP_1 ||
930 event->keyval == GDK_KEY_KP_End ||
931 event->keyval == GDK_KEY_KP_2 ||
932 event->keyval == GDK_KEY_KP_Down ||
933 event->keyval == GDK_KEY_KP_3 ||
934 event->keyval == GDK_KEY_KP_Page_Down ||
935 event->keyval == GDK_KEY_KP_4 ||
936 event->keyval == GDK_KEY_KP_Left ||
937 event->keyval == GDK_KEY_KP_5 ||
938 event->keyval == GDK_KEY_KP_Begin ||
939 event->keyval == GDK_KEY_KP_6 ||
940 event->keyval == GDK_KEY_KP_Right ||
941 event->keyval == GDK_KEY_KP_7 ||
942 event->keyval == GDK_KEY_KP_Home ||
943 event->keyval == GDK_KEY_KP_8 ||
944 event->keyval == GDK_KEY_KP_Up ||
945 event->keyval == GDK_KEY_KP_9 ||
946 event->keyval == GDK_KEY_KP_Page_Up ||
947 event->keyval == GDK_KEY_KP_Decimal ||
948 event->keyval == GDK_KEY_KP_Delete)) {
949 /* app keypad; do nothing */
950 } else if (nethack_mode &&
951 (event->keyval == GDK_KEY_KP_1 ||
952 event->keyval == GDK_KEY_KP_End ||
953 event->keyval == GDK_KEY_KP_2 ||
954 event->keyval == GDK_KEY_KP_Down ||
955 event->keyval == GDK_KEY_KP_3 ||
956 event->keyval == GDK_KEY_KP_Page_Down ||
957 event->keyval == GDK_KEY_KP_4 ||
958 event->keyval == GDK_KEY_KP_Left ||
959 event->keyval == GDK_KEY_KP_5 ||
960 event->keyval == GDK_KEY_KP_Begin ||
961 event->keyval == GDK_KEY_KP_6 ||
962 event->keyval == GDK_KEY_KP_Right ||
963 event->keyval == GDK_KEY_KP_7 ||
964 event->keyval == GDK_KEY_KP_Home ||
965 event->keyval == GDK_KEY_KP_8 ||
966 event->keyval == GDK_KEY_KP_Up ||
967 event->keyval == GDK_KEY_KP_9 ||
968 event->keyval == GDK_KEY_KP_Page_Up)) {
969 /* nethack mode; do nothing */
971 int try_filter = TRUE;
973 #ifdef META_MANUAL_MASK
974 if (event->state & META_MANUAL_MASK & inst->meta_mod_mask) {
976 * If this key event had a Meta modifier bit set which
977 * is also in META_MANUAL_MASK, that means passing
978 * such an event to the GtkIMContext will be unhelpful
979 * (it will eat the keystroke and turn it into
980 * something not what we wanted).
982 #ifdef KEY_EVENT_DIAGNOSTICS
983 debug((" - Meta modifier requiring manual intervention, "
984 "suppressing IM filtering\n"));
991 #ifdef KEY_EVENT_DIAGNOSTICS
992 debug((" - general key press, passing to IM\n"));
994 if (gtk_im_context_filter_keypress(inst->imc, event)) {
995 #ifdef KEY_EVENT_DIAGNOSTICS
996 debug((" - key press accepted by IM\n"));
1000 #ifdef KEY_EVENT_DIAGNOSTICS
1001 debug((" - key press not accepted by IM\n"));
1008 * GDK 2.0 arranges to have done some translation for us: in
1009 * GDK 2.0, event->string is encoded in the current locale.
1011 * So we use the standard C library function mbstowcs() to
1012 * convert from the current locale into Unicode; from there
1013 * we can convert to whatever PuTTY is currently working in.
1014 * (In fact I convert straight back to UTF-8 from
1015 * wide-character Unicode, for the sake of simplicity: that
1016 * way we can still use exactly the same code to manipulate
1017 * the string, such as prefixing ESC.)
1019 output_charset = CS_UTF8;
1021 wchar_t widedata[32];
1026 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
1027 event->string, strlen(event->string),
1028 widedata, lenof(widedata)-1);
1030 #ifdef KEY_EVENT_DIAGNOSTICS
1032 char *string_string = dupstr("");
1035 for (i = 0; i < wlen; i++) {
1036 char *old = string_string;
1037 string_string = dupprintf("%s%s%04x", string_string,
1038 string_string[0] ? " " : "",
1039 (unsigned)widedata[i]);
1042 debug((" - string translated into Unicode = [%s]\n",
1044 sfree(string_string);
1049 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
1050 CS_UTF8, NULL, NULL, 0);
1052 #ifdef KEY_EVENT_DIAGNOSTICS
1054 char *string_string = dupstr("");
1057 for (i = 0; i < ulen; i++) {
1058 char *old = string_string;
1059 string_string = dupprintf("%s%s%02x", string_string,
1060 string_string[0] ? " " : "",
1061 (unsigned)output[i+1] & 0xFF);
1064 debug((" - string translated into UTF-8 = [%s]\n",
1066 sfree(string_string);
1070 output[1+ulen] = '\0';
1072 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1075 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
1076 ucsoutput[0] = '\033';
1077 ucsoutput[1] = ucsval;
1078 #ifdef KEY_EVENT_DIAGNOSTICS
1079 debug((" - keysym_to_unicode gave %04x\n",
1080 (unsigned)ucsoutput[1]));
1082 use_ucsoutput = TRUE;
1085 output[lenof(output)-1] = '\0';
1086 end = strlen(output);
1088 if (event->state & inst->meta_mod_mask) {
1090 if (end == 1) end = 0;
1092 #ifdef META_MANUAL_MASK
1093 if (event->state & META_MANUAL_MASK) {
1095 * Key events which have a META_MANUAL_MASK meta bit
1096 * set may have a keyval reflecting that, e.g. on OS X
1097 * the Option key acts as an AltGr-like modifier and
1098 * causes different Unicode characters to be output.
1100 * To work around this, we clear the dangerous
1101 * modifier bit and retranslate from the hardware
1102 * keycode as if the key had been pressed without that
1103 * modifier. Then we prefix Esc to *that*.
1106 GdkModifierType consumed;
1107 if (gdk_keymap_translate_keyboard_state
1108 (gdk_keymap_get_for_display(gdk_display_get_default()),
1109 event->hardware_keycode, event->state & ~META_MANUAL_MASK,
1110 0, &new_keyval, NULL, NULL, &consumed)) {
1111 ucsoutput[0] = '\033';
1112 ucsoutput[1] = gdk_keyval_to_unicode(new_keyval);
1113 #ifdef KEY_EVENT_DIAGNOSTICS
1115 char *keyval_name = dup_keyval_name(new_keyval);
1116 debug((" - retranslation for manual Meta: "
1117 "new keyval = %s, Unicode = %04x\n",
1118 keyval_name, (unsigned)ucsoutput[1]));
1122 use_ucsoutput = TRUE;
1130 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
1131 if (!output[1] && event->keyval == '`' &&
1132 (event->state & GDK_CONTROL_MASK)) {
1133 #ifdef KEY_EVENT_DIAGNOSTICS
1134 debug((" - Ctrl-` special case, translating as 1c\n"));
1137 use_ucsoutput = FALSE;
1141 /* Some GTK backends (e.g. Quartz) do not change event->string
1142 * in response to the Control modifier. So we do it ourselves
1143 * here, if it's not already happened.
1145 * The translations below are in line with X11 policy as far
1147 if ((event->state & GDK_CONTROL_MASK) && end == 2) {
1148 #ifdef KEY_EVENT_DIAGNOSTICS
1149 int orig = output[1];
1152 if (output[1] >= '3' && output[1] <= '7') {
1153 /* ^3,...,^7 map to 0x1B,...,0x1F */
1154 output[1] += '\x1B' - '3';
1155 } else if (output[1] == '2' || output[1] == ' ') {
1156 /* ^2 and ^Space are both ^@, i.e. \0 */
1158 } else if (output[1] == '8') {
1161 } else if (output[1] == '/') {
1162 /* ^/ is the same as ^_ */
1164 } else if (output[1] >= 0x40 && output[1] < 0x7F) {
1165 /* Everything anywhere near the alphabetics just gets
1169 /* Anything else, e.g. '0', is unchanged. */
1171 #ifdef KEY_EVENT_DIAGNOSTICS
1172 if (orig == output[1])
1173 debug((" - manual Ctrl key handling did nothing\n"));
1175 debug((" - manual Ctrl key handling: %02x -> %02x\n",
1176 (unsigned)orig, (unsigned)output[1]));
1180 /* Control-Break sends a Break special to the backend */
1181 if (event->keyval == GDK_KEY_Break &&
1182 (event->state & GDK_CONTROL_MASK)) {
1183 #ifdef KEY_EVENT_DIAGNOSTICS
1184 debug((" - Ctrl-Break special case, sending TS_BRK\n"));
1187 inst->back->special(inst->backhandle, TS_BRK);
1191 /* We handle Return ourselves, because it needs to be flagged as
1192 * special to ldisc. */
1193 if (event->keyval == GDK_KEY_Return) {
1194 #ifdef KEY_EVENT_DIAGNOSTICS
1195 debug((" - Return special case, translating as 0d + special\n"));
1198 use_ucsoutput = FALSE;
1203 /* Control-2, Control-Space and Control-@ are NUL */
1205 (event->keyval == ' ' || event->keyval == '2' ||
1206 event->keyval == '@') &&
1207 (event->state & (GDK_SHIFT_MASK |
1208 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
1209 #ifdef KEY_EVENT_DIAGNOSTICS
1210 debug((" - Ctrl-{space,2,@} special case, translating as 00\n"));
1213 use_ucsoutput = FALSE;
1217 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
1218 if (!output[1] && event->keyval == ' ' &&
1219 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
1220 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
1221 #ifdef KEY_EVENT_DIAGNOSTICS
1222 debug((" - Ctrl-Shift-space special case, translating as 00a0\n"));
1225 output_charset = CS_ISO8859_1;
1226 use_ucsoutput = FALSE;
1230 /* We don't let GTK tell us what Backspace is! We know better. */
1231 if (event->keyval == GDK_KEY_BackSpace &&
1232 !(event->state & GDK_SHIFT_MASK)) {
1233 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1235 #ifdef KEY_EVENT_DIAGNOSTICS
1236 debug((" - Backspace, translating as %02x\n",
1237 (unsigned)output[1]));
1239 use_ucsoutput = FALSE;
1243 /* For Shift Backspace, do opposite of what is configured. */
1244 if (event->keyval == GDK_KEY_BackSpace &&
1245 (event->state & GDK_SHIFT_MASK)) {
1246 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1248 #ifdef KEY_EVENT_DIAGNOSTICS
1249 debug((" - Shift-Backspace, translating as %02x\n",
1250 (unsigned)output[1]));
1252 use_ucsoutput = FALSE;
1257 /* Shift-Tab is ESC [ Z */
1258 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
1259 (event->keyval == GDK_KEY_Tab &&
1260 (event->state & GDK_SHIFT_MASK))) {
1261 #ifdef KEY_EVENT_DIAGNOSTICS
1262 debug((" - Shift-Tab, translating as ESC [ Z\n"));
1264 end = 1 + sprintf(output+1, "\033[Z");
1265 use_ucsoutput = FALSE;
1267 /* And normal Tab is Tab, if the keymap hasn't already told us.
1268 * (Curiously, at least one version of the MacOS 10.5 X server
1269 * doesn't translate Tab for us. */
1270 if (event->keyval == GDK_KEY_Tab && end <= 1) {
1271 #ifdef KEY_EVENT_DIAGNOSTICS
1272 debug((" - Tab, translating as 09\n"));
1279 * NetHack keypad mode.
1282 const char *keys = NULL;
1283 switch (event->keyval) {
1284 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
1285 keys = "bB\002"; break;
1286 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
1287 keys = "jJ\012"; break;
1288 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
1289 keys = "nN\016"; break;
1290 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
1291 keys = "hH\010"; break;
1292 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
1293 keys = "..."; break;
1294 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
1295 keys = "lL\014"; break;
1296 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
1297 keys = "yY\031"; break;
1298 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
1299 keys = "kK\013"; break;
1300 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
1301 keys = "uU\025"; break;
1305 if (event->state & GDK_CONTROL_MASK)
1306 output[1] = keys[2];
1307 else if (event->state & GDK_SHIFT_MASK)
1308 output[1] = keys[1];
1310 output[1] = keys[0];
1311 #ifdef KEY_EVENT_DIAGNOSTICS
1312 debug((" - Nethack-mode key"));
1314 use_ucsoutput = FALSE;
1320 * Application keypad mode.
1322 if (app_keypad_mode) {
1324 switch (event->keyval) {
1325 case GDK_KEY_Num_Lock: xkey = 'P'; break;
1326 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
1327 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
1328 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
1330 * Keypad + is tricky. It covers a space that would
1331 * be taken up on the VT100 by _two_ keys; so we
1332 * let Shift select between the two. Worse still,
1333 * in xterm function key mode we change which two...
1335 case GDK_KEY_KP_Add:
1336 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
1337 if (event->state & GDK_SHIFT_MASK)
1341 } else if (event->state & GDK_SHIFT_MASK)
1346 case GDK_KEY_KP_Enter: xkey = 'M'; break;
1347 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
1348 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
1349 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
1350 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
1351 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
1352 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
1353 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
1354 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
1355 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
1356 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
1357 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
1361 if (inst->term->vt52_mode) {
1362 if (xkey >= 'P' && xkey <= 'S')
1363 end = 1 + sprintf(output+1, "\033%c", xkey);
1365 end = 1 + sprintf(output+1, "\033?%c", xkey);
1367 end = 1 + sprintf(output+1, "\033O%c", xkey);
1368 use_ucsoutput = FALSE;
1369 #ifdef KEY_EVENT_DIAGNOSTICS
1370 debug((" - Application keypad mode key"));
1377 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
1378 * for integer decimal nn.)
1380 * We also deal with the weird ones here. Linux VCs replace F1
1381 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1382 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1387 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1388 switch (event->keyval) {
1390 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1393 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1396 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1399 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1402 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1405 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1408 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1411 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1414 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1417 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1450 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1451 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1454 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1457 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1460 case GDK_KEY_End: case GDK_KEY_KP_End:
1463 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1466 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1470 /* Reorder edit keys to physical order */
1471 if (funky_type == FUNKY_VT400 && code <= 6)
1472 code = "\0\2\1\4\5\3\6"[code];
1474 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1475 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1476 #ifdef KEY_EVENT_DIAGNOSTICS
1477 debug((" - VT52 mode small keypad key"));
1479 use_ucsoutput = FALSE;
1483 if (funky_type == FUNKY_SCO && /* SCO function keys */
1484 code >= 11 && code <= 34) {
1485 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1487 switch (event->keyval) {
1488 case GDK_KEY_F1: index = 0; break;
1489 case GDK_KEY_F2: index = 1; break;
1490 case GDK_KEY_F3: index = 2; break;
1491 case GDK_KEY_F4: index = 3; break;
1492 case GDK_KEY_F5: index = 4; break;
1493 case GDK_KEY_F6: index = 5; break;
1494 case GDK_KEY_F7: index = 6; break;
1495 case GDK_KEY_F8: index = 7; break;
1496 case GDK_KEY_F9: index = 8; break;
1497 case GDK_KEY_F10: index = 9; break;
1498 case GDK_KEY_F11: index = 10; break;
1499 case GDK_KEY_F12: index = 11; break;
1501 if (event->state & GDK_SHIFT_MASK) index += 12;
1502 if (event->state & GDK_CONTROL_MASK) index += 24;
1503 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1504 #ifdef KEY_EVENT_DIAGNOSTICS
1505 debug((" - SCO mode function key"));
1507 use_ucsoutput = FALSE;
1510 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1511 code >= 1 && code <= 6) {
1512 char codes[] = "HL.FIG";
1517 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1519 #ifdef KEY_EVENT_DIAGNOSTICS
1520 debug((" - SCO mode small keypad key"));
1522 use_ucsoutput = FALSE;
1525 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1526 code >= 11 && code <= 24) {
1532 if (inst->term->vt52_mode) {
1533 #ifdef KEY_EVENT_DIAGNOSTICS
1534 debug((" - VT52 mode function key"));
1536 end = 1 + sprintf(output+1,
1537 "\x1B%c", code + 'P' - 11 - offt);
1539 #ifdef KEY_EVENT_DIAGNOSTICS
1540 debug((" - VT100+ mode function key"));
1542 end = 1 + sprintf(output+1,
1543 "\x1BO%c", code + 'P' - 11 - offt);
1545 use_ucsoutput = FALSE;
1548 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1549 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1550 #ifdef KEY_EVENT_DIAGNOSTICS
1551 debug((" - Linux mode F1-F5 function key"));
1553 use_ucsoutput = FALSE;
1556 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1557 if (inst->term->vt52_mode) {
1558 #ifdef KEY_EVENT_DIAGNOSTICS
1559 debug((" - VT52 mode (overriding xterm) F1-F4 function"
1562 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1564 #ifdef KEY_EVENT_DIAGNOSTICS
1565 debug((" - xterm mode F1-F4 function key"));
1567 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1569 use_ucsoutput = FALSE;
1572 if ((code == 1 || code == 4) &&
1573 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1574 #ifdef KEY_EVENT_DIAGNOSTICS
1575 debug((" - rxvt style Home/End"));
1577 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1578 use_ucsoutput = FALSE;
1582 #ifdef KEY_EVENT_DIAGNOSTICS
1583 debug((" - ordinary function key encoding"));
1585 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1586 use_ucsoutput = FALSE;
1592 * Cursor keys. (This includes the numberpad cursor keys,
1593 * if we haven't already done them due to app keypad mode.)
1595 * Here we also process un-numlocked un-appkeypadded KP5,
1596 * which sends ESC [ G.
1600 switch (event->keyval) {
1601 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1602 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1603 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1604 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1605 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1608 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1609 event->state & GDK_CONTROL_MASK);
1610 #ifdef KEY_EVENT_DIAGNOSTICS
1611 debug((" - arrow key"));
1613 use_ucsoutput = FALSE;
1622 if (end-start > 0) {
1624 #ifdef KEY_EVENT_DIAGNOSTICS
1625 char *string_string = dupstr("");
1628 for (i = start; i < end; i++) {
1629 char *old = string_string;
1630 string_string = dupprintf("%s%s%02x", string_string,
1631 string_string[0] ? " " : "",
1632 (unsigned)output[i] & 0xFF);
1635 debug((" - final output, special, generic encoding = [%s]\n",
1636 charset_to_localenc(output_charset), string_string));
1637 sfree(string_string);
1640 * For special control characters, the character set
1641 * should never matter.
1643 output[end] = '\0'; /* NUL-terminate */
1645 ldisc_send(inst->ldisc, output+start, -2, 1);
1646 } else if (!inst->direct_to_font) {
1647 if (!use_ucsoutput) {
1648 #ifdef KEY_EVENT_DIAGNOSTICS
1649 char *string_string = dupstr("");
1652 for (i = start; i < end; i++) {
1653 char *old = string_string;
1654 string_string = dupprintf("%s%s%02x", string_string,
1655 string_string[0] ? " " : "",
1656 (unsigned)output[i] & 0xFF);
1659 debug((" - final output in %s = [%s]\n",
1660 charset_to_localenc(output_charset), string_string));
1661 sfree(string_string);
1664 lpage_send(inst->ldisc, output_charset, output+start,
1667 #ifdef KEY_EVENT_DIAGNOSTICS
1668 char *string_string = dupstr("");
1671 for (i = start; i < end; i++) {
1672 char *old = string_string;
1673 string_string = dupprintf("%s%s%04x", string_string,
1674 string_string[0] ? " " : "",
1675 (unsigned)ucsoutput[i]);
1678 debug((" - final output in Unicode = [%s]\n",
1680 sfree(string_string);
1684 * We generated our own Unicode key data from the
1685 * keysym, so use that instead.
1688 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1692 * In direct-to-font mode, we just send the string
1693 * exactly as we received it.
1695 #ifdef KEY_EVENT_DIAGNOSTICS
1696 char *string_string = dupstr("");
1699 for (i = start; i < end; i++) {
1700 char *old = string_string;
1701 string_string = dupprintf("%s%s%02x", string_string,
1702 string_string[0] ? " " : "",
1703 (unsigned)output[i] & 0xFF);
1706 debug((" - final output in direct-to-font encoding = [%s]\n",
1708 sfree(string_string);
1711 ldisc_send(inst->ldisc, output+start, end-start, 1);
1714 show_mouseptr(inst, 0);
1715 term_seen_key_event(inst->term);
1721 #if GTK_CHECK_VERSION(2,0,0)
1722 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1724 struct gui_data *inst = (struct gui_data *)data;
1726 #ifdef KEY_EVENT_DIAGNOSTICS
1727 char *string_string = dupstr("");
1730 for (i = 0; str[i]; i++) {
1731 char *old = string_string;
1732 string_string = dupprintf("%s%s%02x", string_string,
1733 string_string[0] ? " " : "",
1734 (unsigned)str[i] & 0xFF);
1737 debug((" - IM commit event in UTF-8 = [%s]\n", string_string));
1738 sfree(string_string);
1742 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1743 show_mouseptr(inst, 0);
1744 term_seen_key_event(inst->term);
1748 #define SCROLL_INCREMENT_LINES 5
1750 #if GTK_CHECK_VERSION(3,4,0)
1751 gboolean scroll_internal(struct gui_data *inst, gdouble delta, guint state,
1752 gdouble ex, gdouble ey)
1754 int shift, ctrl, alt, x, y, raw_mouse_mode;
1756 show_mouseptr(inst, 1);
1758 shift = state & GDK_SHIFT_MASK;
1759 ctrl = state & GDK_CONTROL_MASK;
1760 alt = state & inst->meta_mod_mask;
1762 x = (ex - inst->window_border) / inst->font_width;
1763 y = (ey - inst->window_border) / inst->font_height;
1766 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1767 CONF_mouse_override));
1769 inst->cumulative_scroll += delta * SCROLL_INCREMENT_LINES;
1771 if (!raw_mouse_mode) {
1772 int scroll_lines = (int)inst->cumulative_scroll; /* rounds toward 0 */
1774 term_scroll(inst->term, 0, scroll_lines);
1775 inst->cumulative_scroll -= scroll_lines;
1779 int scroll_events = (int)(inst->cumulative_scroll /
1780 SCROLL_INCREMENT_LINES);
1781 if (scroll_events) {
1784 inst->cumulative_scroll -= scroll_events * SCROLL_INCREMENT_LINES;
1786 if (scroll_events > 0) {
1787 button = MBT_WHEEL_DOWN;
1789 button = MBT_WHEEL_UP;
1790 scroll_events = -scroll_events;
1793 while (scroll_events-- > 0) {
1794 term_mouse(inst->term, button, translate_button(button),
1795 MA_CLICK, x, y, shift, ctrl, alt);
1803 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1804 GdkEventType type, guint ebutton, guint state,
1805 gdouble ex, gdouble ey)
1807 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1809 /* Remember the timestamp. */
1810 inst->input_event_time = timestamp;
1812 show_mouseptr(inst, 1);
1814 shift = state & GDK_SHIFT_MASK;
1815 ctrl = state & GDK_CONTROL_MASK;
1816 alt = state & inst->meta_mod_mask;
1819 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1820 CONF_mouse_override));
1822 if (!raw_mouse_mode) {
1823 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1824 term_scroll(inst->term, 0, -SCROLL_INCREMENT_LINES);
1827 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1828 term_scroll(inst->term, 0, +SCROLL_INCREMENT_LINES);
1833 if (ebutton == 3 && ctrl) {
1834 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1835 ebutton, timestamp);
1841 else if (ebutton == 2)
1842 button = MBT_MIDDLE;
1843 else if (ebutton == 3)
1845 else if (ebutton == 4)
1846 button = MBT_WHEEL_UP;
1847 else if (ebutton == 5)
1848 button = MBT_WHEEL_DOWN;
1850 return FALSE; /* don't even know what button! */
1853 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1854 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1855 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1856 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1857 default: return FALSE; /* don't know this event type */
1860 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1861 return TRUE; /* we ignore these in raw mouse mode */
1863 x = (ex - inst->window_border) / inst->font_width;
1864 y = (ey - inst->window_border) / inst->font_height;
1866 term_mouse(inst->term, button, translate_button(button), act,
1867 x, y, shift, ctrl, alt);
1872 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1874 struct gui_data *inst = (struct gui_data *)data;
1875 return button_internal(inst, event->time, event->type, event->button,
1876 event->state, event->x, event->y);
1879 #if GTK_CHECK_VERSION(2,0,0)
1881 * In GTK 2, mouse wheel events have become a new type of event.
1882 * This handler translates them back into button-4 and button-5
1883 * presses so that I don't have to change my old code too much :-)
1885 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1887 struct gui_data *inst = (struct gui_data *)data;
1889 #if GTK_CHECK_VERSION(3,4,0)
1891 if (gdk_event_get_scroll_deltas((GdkEvent *)event, &dx, &dy)) {
1892 return scroll_internal(inst, dy, event->state, event->x, event->y);
1897 if (event->direction == GDK_SCROLL_UP)
1899 else if (event->direction == GDK_SCROLL_DOWN)
1904 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1905 button, event->state, event->x, event->y);
1910 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1912 struct gui_data *inst = (struct gui_data *)data;
1913 int shift, ctrl, alt, x, y, button;
1915 /* Remember the timestamp. */
1916 inst->input_event_time = event->time;
1918 show_mouseptr(inst, 1);
1920 shift = event->state & GDK_SHIFT_MASK;
1921 ctrl = event->state & GDK_CONTROL_MASK;
1922 alt = event->state & inst->meta_mod_mask;
1923 if (event->state & GDK_BUTTON1_MASK)
1925 else if (event->state & GDK_BUTTON2_MASK)
1926 button = MBT_MIDDLE;
1927 else if (event->state & GDK_BUTTON3_MASK)
1930 return FALSE; /* don't even know what button! */
1932 x = (event->x - inst->window_border) / inst->font_width;
1933 y = (event->y - inst->window_border) / inst->font_height;
1935 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1936 x, y, shift, ctrl, alt);
1941 void frontend_keypress(void *handle)
1943 struct gui_data *inst = (struct gui_data *)handle;
1946 * If our child process has exited but not closed, terminate on
1953 static void exit_callback(void *vinst)
1955 struct gui_data *inst = (struct gui_data *)vinst;
1956 int exitcode, close_on_exit;
1958 if (!inst->exited &&
1959 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1960 inst->exited = TRUE;
1961 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1962 if (close_on_exit == FORCE_ON ||
1963 (close_on_exit == AUTO && exitcode == 0))
1964 gtk_main_quit(); /* just go */
1966 ldisc_free(inst->ldisc);
1969 inst->back->free(inst->backhandle);
1970 inst->backhandle = NULL;
1972 term_provide_resize_fn(inst->term, NULL, NULL);
1973 update_specials_menu(inst);
1974 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1978 void notify_remote_exit(void *frontend)
1980 struct gui_data *inst = (struct gui_data *)frontend;
1982 queue_toplevel_callback(exit_callback, inst);
1985 void destroy(GtkWidget *widget, gpointer data)
1990 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1992 struct gui_data *inst = (struct gui_data *)data;
1993 term_set_focus(inst->term, event->in);
1994 term_update(inst->term);
1995 show_mouseptr(inst, 1);
1999 void set_busy_status(void *frontend, int status)
2001 struct gui_data *inst = (struct gui_data *)frontend;
2002 inst->busy_status = status;
2003 update_mouseptr(inst);
2007 * set or clear the "raw mouse message" mode
2009 void set_raw_mouse_mode(void *frontend, int activate)
2011 struct gui_data *inst = (struct gui_data *)frontend;
2012 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
2013 send_raw_mouse = activate;
2014 update_mouseptr(inst);
2017 static void compute_geom_hints(struct gui_data *inst, GdkGeometry *geom);
2019 void request_resize(void *frontend, int w, int h)
2021 struct gui_data *inst = (struct gui_data *)frontend;
2023 #if !GTK_CHECK_VERSION(3,0,0)
2025 int large_x, large_y;
2026 int offset_x, offset_y;
2028 GtkRequisition inner, outer;
2031 * This is a heinous hack dreamed up by the gnome-terminal
2032 * people to get around a limitation in gtk. The problem is
2033 * that in order to set the size correctly we really need to be
2034 * calling gtk_window_resize - but that needs to know the size
2035 * of the _whole window_, not the drawing area. So what we do
2036 * is to set an artificially huge size request on the drawing
2037 * area, recompute the resulting size request on the window,
2038 * and look at the difference between the two. That gives us
2039 * the x and y offsets we need to translate drawing area size
2040 * into window size for real, and then we call
2041 * gtk_window_resize.
2045 * We start by retrieving the current size of the whole window.
2046 * Adding a bit to _that_ will give us a value we can use as a
2047 * bogus size request which guarantees to be bigger than the
2048 * current size of the drawing area.
2050 get_window_pixels(inst, &large_x, &large_y);
2054 gtk_widget_set_size_request(inst->area, large_x, large_y);
2055 gtk_widget_size_request(inst->area, &inner);
2056 gtk_widget_size_request(inst->window, &outer);
2058 offset_x = outer.width - inner.width;
2059 offset_y = outer.height - inner.height;
2061 area_x = inst->font_width * w + 2*inst->window_border;
2062 area_y = inst->font_height * h + 2*inst->window_border;
2065 * Now we must set the size request on the drawing area back to
2066 * something sensible before we commit the real resize. Best
2067 * way to do this, I think, is to set it to what the size is
2068 * really going to end up being.
2070 gtk_widget_set_size_request(inst->area, area_x, area_y);
2071 #if GTK_CHECK_VERSION(2,0,0)
2072 gtk_window_resize(GTK_WINDOW(inst->window),
2073 area_x + offset_x, area_y + offset_y);
2075 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
2077 * I can no longer remember what this call to
2078 * gtk_container_dequeue_resize_handler is for. It was
2079 * introduced in r3092 with no comment, and the commit log
2080 * message was uninformative. I'm _guessing_ its purpose is to
2081 * prevent gratuitous resize processing on the window given
2082 * that we're about to resize it anyway, but I have no idea
2083 * why that's so incredibly vital.
2085 * I've tried removing the call, and nothing seems to go
2086 * wrong. I've backtracked to r3092 and tried removing the
2087 * call there, and still nothing goes wrong. So I'm going to
2088 * adopt the working hypothesis that it's superfluous; I won't
2089 * actually remove it from the GTK 1.2 code, but I won't
2090 * attempt to replicate its functionality in the GTK 2 code
2093 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
2094 gdk_window_resize(gtk_widget_get_window(inst->window),
2095 area_x + offset_x, area_y + offset_y);
2098 #else /* GTK_CHECK_VERSION(3,0,0) */
2101 * In GTK3, we can do this by using gtk_window_resize_to_geometry,
2102 * which uses the fact that we've already set up the main window's
2103 * WM hints to reflect the terminal drawing area's resize
2104 * increment (i.e. character cell) and the fixed amount of stuff
2107 gtk_window_resize_to_geometry(GTK_WINDOW(inst->window), w, h);
2113 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
2115 inst->cols[n].red = r * 0x0101;
2116 inst->cols[n].green = g * 0x0101;
2117 inst->cols[n].blue = b * 0x0101;
2119 #if !GTK_CHECK_VERSION(3,0,0)
2121 gboolean success[1];
2122 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
2123 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
2124 FALSE, TRUE, success);
2126 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2127 appname, n, r, g, b);
2132 void set_gdk_window_background(GdkWindow *win, const GdkColor *col)
2134 #if GTK_CHECK_VERSION(3,0,0)
2135 /* gdk_window_set_background is deprecated; work around its
2138 rgba.red = col->red / 65535.0;
2139 rgba.green = col->green / 65535.0;
2140 rgba.blue = col->blue / 65535.0;
2142 gdk_window_set_background_rgba(win, &rgba);
2145 /* For GTK1, which doesn't have a 'const' on
2146 * gdk_window_set_background's second parameter type. */
2147 GdkColor col_mutable = *col;
2148 gdk_window_set_background(win, &col_mutable);
2153 void set_window_background(struct gui_data *inst)
2155 if (inst->area && gtk_widget_get_window(inst->area))
2156 set_gdk_window_background(gtk_widget_get_window(inst->area),
2158 if (inst->window && gtk_widget_get_window(inst->window))
2159 set_gdk_window_background(gtk_widget_get_window(inst->window),
2163 void palette_set(void *frontend, int n, int r, int g, int b)
2165 struct gui_data *inst = (struct gui_data *)frontend;
2168 if (n >= NALLCOLOURS)
2170 real_palette_set(inst, n, r, g, b);
2172 /* Default Background changed. Ensure space between text area and
2173 * window border is redrawn */
2174 set_window_background(inst);
2175 draw_backing_rect(inst);
2176 gtk_widget_queue_draw(inst->area);
2180 void palette_reset(void *frontend)
2182 struct gui_data *inst = (struct gui_data *)frontend;
2183 /* This maps colour indices in inst->conf to those used in inst->cols. */
2184 static const int ww[] = {
2185 256, 257, 258, 259, 260, 261,
2186 0, 8, 1, 9, 2, 10, 3, 11,
2187 4, 12, 5, 13, 6, 14, 7, 15
2191 assert(lenof(ww) == NCFGCOLOURS);
2193 #if !GTK_CHECK_VERSION(3,0,0)
2194 if (!inst->colmap) {
2195 inst->colmap = gdk_colormap_get_system();
2197 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
2201 for (i = 0; i < NCFGCOLOURS; i++) {
2202 inst->cols[ww[i]].red =
2203 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
2204 inst->cols[ww[i]].green =
2205 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
2206 inst->cols[ww[i]].blue =
2207 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
2210 for (i = 0; i < NEXTCOLOURS; i++) {
2212 int r = i / 36, g = (i / 6) % 6, b = i % 6;
2213 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
2214 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
2215 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
2217 int shade = i - 216;
2218 shade = shade * 0x0a0a + 0x0808;
2219 inst->cols[i+16].red = inst->cols[i+16].green =
2220 inst->cols[i+16].blue = shade;
2224 #if !GTK_CHECK_VERSION(3,0,0)
2226 gboolean success[NALLCOLOURS];
2227 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
2228 FALSE, TRUE, success);
2229 for (i = 0; i < NALLCOLOURS; i++) {
2231 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2233 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
2234 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
2235 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
2240 /* Since Default Background may have changed, ensure that space
2241 * between text area and window border is refreshed. */
2242 set_window_background(inst);
2243 if (inst->area && gtk_widget_get_window(inst->area)) {
2244 draw_backing_rect(inst);
2245 gtk_widget_queue_draw(inst->area);
2249 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
2251 /* ----------------------------------------------------------------------
2252 * Clipboard handling, using the high-level GtkClipboard interface in
2253 * as hands-off a way as possible. We write and read the clipboard as
2254 * UTF-8 text, and let GTK deal with converting to any other text
2255 * formats it feels like.
2258 void init_clipboard(struct gui_data *inst)
2260 inst->clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(),
2265 * Because calling gtk_clipboard_set_with_data triggers a call to the
2266 * clipboard_clear function from the last time, we need to arrange a
2267 * way to distinguish a real call to clipboard_clear for the _new_
2268 * instance of the clipboard data from the leftover call for the
2269 * outgoing one. We do this by setting the user data field in our
2270 * gtk_clipboard_set_with_data() call, instead of the obvious pointer
2271 * to 'inst', to one of these.
2273 struct clipboard_data_instance {
2274 struct gui_data *inst;
2275 char *pasteout_data_utf8;
2276 int pasteout_data_utf8_len;
2279 static void clipboard_provide_data(GtkClipboard *clipboard,
2280 GtkSelectionData *selection_data,
2281 guint info, gpointer data)
2283 struct clipboard_data_instance *cdi =
2284 (struct clipboard_data_instance *)data;
2285 struct gui_data *inst = cdi->inst;
2287 if (inst->current_cdi == cdi) {
2288 gtk_selection_data_set_text(selection_data, cdi->pasteout_data_utf8,
2289 cdi->pasteout_data_utf8_len);
2293 static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
2295 struct clipboard_data_instance *cdi =
2296 (struct clipboard_data_instance *)data;
2297 struct gui_data *inst = cdi->inst;
2299 if (inst->current_cdi == cdi) {
2300 term_deselect(inst->term);
2301 inst->current_cdi = NULL;
2303 sfree(cdi->pasteout_data_utf8);
2307 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2310 struct gui_data *inst = (struct gui_data *)frontend;
2311 struct clipboard_data_instance *cdi;
2313 if (inst->direct_to_font) {
2314 /* In this clipboard mode, we just can't paste if we're in
2315 * direct-to-font mode. Fortunately, that shouldn't be
2316 * important, because we'll only use this clipboard handling
2317 * code on systems where that kind of font doesn't exist
2322 cdi = snew(struct clipboard_data_instance);
2324 inst->current_cdi = cdi;
2325 cdi->pasteout_data_utf8 = snewn(len*6, char);
2327 const wchar_t *tmp = data;
2329 cdi->pasteout_data_utf8_len =
2330 charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
2331 len*6, CS_UTF8, NULL, NULL, 0);
2335 * It would be nice to just call gtk_clipboard_set_text() in place
2336 * of all of the faffing below. Unfortunately, that won't give me
2337 * access to the clipboard-clear event, which we use to visually
2338 * deselect text in the terminal.
2341 GtkTargetList *targetlist;
2342 GtkTargetEntry *targettable;
2345 targetlist = gtk_target_list_new(NULL, 0);
2346 gtk_target_list_add_text_targets(targetlist, 0);
2347 targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
2348 gtk_clipboard_set_with_data(inst->clipboard, targettable, n_targets,
2349 clipboard_provide_data, clipboard_clear,
2351 gtk_target_table_free(targettable, n_targets);
2352 gtk_target_list_unref(targetlist);
2356 static void clipboard_text_received(GtkClipboard *clipboard,
2357 const gchar *text, gpointer data)
2359 struct gui_data *inst = (struct gui_data *)data;
2365 length = strlen(text);
2367 if (inst->pastein_data)
2368 sfree(inst->pastein_data);
2370 inst->pastein_data = snewn(length, wchar_t);
2371 inst->pastein_data_len = mb_to_wc(CS_UTF8, 0, text, length,
2372 inst->pastein_data, length);
2374 term_do_paste(inst->term);
2377 void request_paste(void *frontend)
2379 struct gui_data *inst = (struct gui_data *)frontend;
2380 gtk_clipboard_request_text(inst->clipboard, clipboard_text_received, inst);
2383 #else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
2385 /* ----------------------------------------------------------------------
2386 * Clipboard handling for X, using the low-level gtk_selection_*
2387 * interface, handling conversions to fiddly things like compound text
2388 * ourselves, and storing in X cut buffers too.
2390 * This version of the clipboard code has to be kept around for GTK1,
2391 * which doesn't have the higher-level GtkClipboard interface at all.
2392 * And since it works on GTK2 and GTK3 too and has had a good few
2393 * years of shakedown and bug fixing, we might as well keep using it
2394 * where it's applicable.
2396 * It's _possible_ that we might be able to replicate all the
2397 * important wrinkles of this code in GtkClipboard. (In particular,
2398 * cut buffers or local analogue look as if they might be accessible
2399 * via gtk_clipboard_set_can_store(), and delivering text in
2400 * non-Unicode formats only in the direct-to-font case ought to be
2401 * possible if we can figure out the right set of things to put in the
2402 * GtkTargetList.) But that work can wait until there's a need for it!
2405 /* Store the data in a cut-buffer. */
2406 static void store_cutbuffer(char * ptr, int len)
2408 #ifndef NOT_X_WINDOWS
2409 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2410 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
2411 XRotateBuffers(disp, 1);
2412 XStoreBytes(disp, ptr, len);
2416 /* Retrieve data from a cut-buffer.
2417 * Returned data needs to be freed with XFree().
2419 static char *retrieve_cutbuffer(int *nbytes)
2421 #ifndef NOT_X_WINDOWS
2422 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2424 ptr = XFetchBytes(disp, nbytes);
2425 if (*nbytes <= 0 && ptr != 0) {
2435 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2438 struct gui_data *inst = (struct gui_data *)frontend;
2439 if (inst->pasteout_data)
2440 sfree(inst->pasteout_data);
2441 if (inst->pasteout_data_ctext)
2442 sfree(inst->pasteout_data_ctext);
2443 if (inst->pasteout_data_utf8)
2444 sfree(inst->pasteout_data_utf8);
2447 * Set up UTF-8 and compound text paste data. This only happens
2448 * if we aren't in direct-to-font mode using the D800 hack.
2450 if (!inst->direct_to_font) {
2451 const wchar_t *tmp = data;
2453 #ifndef NOT_X_WINDOWS
2456 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2459 inst->pasteout_data_utf8 = snewn(len*6, char);
2460 inst->pasteout_data_utf8_len = len*6;
2461 inst->pasteout_data_utf8_len =
2462 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
2463 inst->pasteout_data_utf8_len,
2464 CS_UTF8, NULL, NULL, 0);
2465 if (inst->pasteout_data_utf8_len == 0) {
2466 sfree(inst->pasteout_data_utf8);
2467 inst->pasteout_data_utf8 = NULL;
2469 inst->pasteout_data_utf8 =
2470 sresize(inst->pasteout_data_utf8,
2471 inst->pasteout_data_utf8_len + 1, char);
2472 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
2476 * Now let Xlib convert our UTF-8 data into compound text.
2478 #ifndef NOT_X_WINDOWS
2479 list[0] = inst->pasteout_data_utf8;
2480 if (Xutf8TextListToTextProperty(disp, list, 1,
2481 XCompoundTextStyle, &tp) == 0) {
2482 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
2483 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
2484 inst->pasteout_data_ctext_len = tp.nitems;
2489 inst->pasteout_data_ctext = NULL;
2490 inst->pasteout_data_ctext_len = 0;
2493 inst->pasteout_data_utf8 = NULL;
2494 inst->pasteout_data_utf8_len = 0;
2495 inst->pasteout_data_ctext = NULL;
2496 inst->pasteout_data_ctext_len = 0;
2499 inst->pasteout_data = snewn(len*6, char);
2500 inst->pasteout_data_len = len*6;
2501 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
2502 data, len, inst->pasteout_data,
2503 inst->pasteout_data_len,
2505 if (inst->pasteout_data_len == 0) {
2506 sfree(inst->pasteout_data);
2507 inst->pasteout_data = NULL;
2509 inst->pasteout_data =
2510 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
2513 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
2515 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
2516 inst->input_event_time)) {
2517 #if GTK_CHECK_VERSION(2,0,0)
2518 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
2520 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2521 GDK_SELECTION_TYPE_STRING, 1);
2522 if (inst->pasteout_data_ctext)
2523 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2524 compound_text_atom, 1);
2525 if (inst->pasteout_data_utf8)
2526 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2527 utf8_string_atom, 1);
2531 term_deselect(inst->term);
2534 static void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2535 guint info, guint time_stamp, gpointer data)
2537 struct gui_data *inst = (struct gui_data *)data;
2538 GdkAtom target = gtk_selection_data_get_target(seldata);
2539 if (target == utf8_string_atom)
2540 gtk_selection_data_set(seldata, target, 8,
2541 (unsigned char *)inst->pasteout_data_utf8,
2542 inst->pasteout_data_utf8_len);
2543 else if (target == compound_text_atom)
2544 gtk_selection_data_set(seldata, target, 8,
2545 (unsigned char *)inst->pasteout_data_ctext,
2546 inst->pasteout_data_ctext_len);
2548 gtk_selection_data_set(seldata, target, 8,
2549 (unsigned char *)inst->pasteout_data,
2550 inst->pasteout_data_len);
2553 static gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2556 struct gui_data *inst = (struct gui_data *)data;
2558 term_deselect(inst->term);
2559 if (inst->pasteout_data)
2560 sfree(inst->pasteout_data);
2561 if (inst->pasteout_data_ctext)
2562 sfree(inst->pasteout_data_ctext);
2563 if (inst->pasteout_data_utf8)
2564 sfree(inst->pasteout_data_utf8);
2565 inst->pasteout_data = NULL;
2566 inst->pasteout_data_len = 0;
2567 inst->pasteout_data_ctext = NULL;
2568 inst->pasteout_data_ctext_len = 0;
2569 inst->pasteout_data_utf8 = NULL;
2570 inst->pasteout_data_utf8_len = 0;
2574 void request_paste(void *frontend)
2576 struct gui_data *inst = (struct gui_data *)frontend;
2578 * In Unix, pasting is asynchronous: all we can do at the
2579 * moment is to call gtk_selection_convert(), and when the data
2580 * comes back _then_ we can call term_do_paste().
2583 if (!inst->direct_to_font) {
2585 * First we attempt to retrieve the selection as a UTF-8
2586 * string (which we will convert to the correct code page
2587 * before sending to the session, of course). If that
2588 * fails, selection_received() will be informed and will
2589 * fall back to an ordinary string.
2591 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2593 inst->input_event_time);
2596 * If we're in direct-to-font mode, we disable UTF-8
2597 * pasting, and go straight to ordinary string data.
2599 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2600 GDK_SELECTION_TYPE_STRING,
2601 inst->input_event_time);
2605 static void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2606 guint time, gpointer data)
2608 struct gui_data *inst = (struct gui_data *)data;
2611 #ifndef NOT_X_WINDOWS
2613 int free_list_required = 0;
2614 int free_required = 0;
2617 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2618 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2619 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2620 gint seldata_length = gtk_selection_data_get_length(seldata);
2622 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2624 * Failed to get a UTF-8 selection string. Try compound
2627 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2629 inst->input_event_time);
2633 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2635 * Failed to get UTF-8 or compound text. Try an ordinary
2638 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2639 GDK_SELECTION_TYPE_STRING,
2640 inst->input_event_time);
2645 * If we have data, but it's not of a type we can deal with,
2646 * we have to ignore the data.
2648 if (seldata_length > 0 &&
2649 seldata_type != GDK_SELECTION_TYPE_STRING &&
2650 seldata_type != compound_text_atom &&
2651 seldata_type != utf8_string_atom)
2655 * If we have no data, try looking in a cut buffer.
2657 if (seldata_length <= 0) {
2658 #ifndef NOT_X_WINDOWS
2659 text = retrieve_cutbuffer(&length);
2662 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2663 * source, so use that as a de-facto standard. */
2664 charset = CS_ISO8859_1;
2671 * Convert COMPOUND_TEXT into UTF-8.
2673 if (seldata_type == compound_text_atom) {
2674 #ifndef NOT_X_WINDOWS
2677 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2679 tp.value = (unsigned char *)seldata_data;
2680 tp.encoding = (Atom) seldata_type;
2681 tp.format = gtk_selection_data_get_format(seldata);
2682 tp.nitems = seldata_length;
2683 ret = Xutf8TextPropertyToTextList(disp, &tp, &list, &count);
2684 if (ret == 0 && count == 1) {
2686 length = strlen(list[0]);
2688 free_list_required = 1;
2693 * Compound text failed; fall back to STRING.
2695 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2696 GDK_SELECTION_TYPE_STRING,
2697 inst->input_event_time);
2701 text = (char *)seldata_data;
2702 length = seldata_length;
2703 charset = (seldata_type == utf8_string_atom ?
2704 CS_UTF8 : inst->ucsdata.line_codepage);
2708 if (inst->pastein_data)
2709 sfree(inst->pastein_data);
2711 inst->pastein_data = snewn(length, wchar_t);
2712 inst->pastein_data_len = length;
2713 inst->pastein_data_len =
2714 mb_to_wc(charset, 0, text, length,
2715 inst->pastein_data, inst->pastein_data_len);
2717 term_do_paste(inst->term);
2719 #ifndef NOT_X_WINDOWS
2720 if (free_list_required)
2721 XFreeStringList(list);
2727 void init_clipboard(struct gui_data *inst)
2729 #ifndef NOT_X_WINDOWS
2731 * Ensure that all the cut buffers exist - according to the ICCCM,
2732 * we must do this before we start using cut buffers.
2734 unsigned char empty[] = "";
2735 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2736 x11_ignore_error(disp, BadMatch);
2737 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2738 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
2739 x11_ignore_error(disp, BadMatch);
2740 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2741 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
2742 x11_ignore_error(disp, BadMatch);
2743 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2744 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
2745 x11_ignore_error(disp, BadMatch);
2746 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2747 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
2748 x11_ignore_error(disp, BadMatch);
2749 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2750 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
2751 x11_ignore_error(disp, BadMatch);
2752 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2753 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
2754 x11_ignore_error(disp, BadMatch);
2755 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2756 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
2757 x11_ignore_error(disp, BadMatch);
2758 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2759 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
2762 g_signal_connect(G_OBJECT(inst->area), "selection_received",
2763 G_CALLBACK(selection_received), inst);
2764 g_signal_connect(G_OBJECT(inst->area), "selection_get",
2765 G_CALLBACK(selection_get), inst);
2766 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
2767 G_CALLBACK(selection_clear), inst);
2771 * End of selection/clipboard handling.
2772 * ----------------------------------------------------------------------
2775 #endif /* JUST_USE_GTK_CLIPBOARD_UTF8 */
2777 void get_clip(void *frontend, wchar_t ** p, int *len)
2779 struct gui_data *inst = (struct gui_data *)frontend;
2782 *p = inst->pastein_data;
2783 *len = inst->pastein_data_len;
2787 static void set_window_titles(struct gui_data *inst)
2790 * We must always call set_icon_name after calling set_title,
2791 * since set_title will write both names. Irritating, but such
2794 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2795 if (!conf_get_int(inst->conf, CONF_win_name_always))
2796 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
2800 void set_title(void *frontend, char *title)
2802 struct gui_data *inst = (struct gui_data *)frontend;
2803 sfree(inst->wintitle);
2804 inst->wintitle = dupstr(title);
2805 set_window_titles(inst);
2808 void set_icon(void *frontend, char *title)
2810 struct gui_data *inst = (struct gui_data *)frontend;
2811 sfree(inst->icontitle);
2812 inst->icontitle = dupstr(title);
2813 set_window_titles(inst);
2816 void set_title_and_icon(void *frontend, char *title, char *icon)
2818 struct gui_data *inst = (struct gui_data *)frontend;
2819 sfree(inst->wintitle);
2820 inst->wintitle = dupstr(title);
2821 sfree(inst->icontitle);
2822 inst->icontitle = dupstr(icon);
2823 set_window_titles(inst);
2826 void set_sbar(void *frontend, int total, int start, int page)
2828 struct gui_data *inst = (struct gui_data *)frontend;
2829 if (!conf_get_int(inst->conf, CONF_scrollbar))
2831 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
2832 gtk_adjustment_set_upper(inst->sbar_adjust, total);
2833 gtk_adjustment_set_value(inst->sbar_adjust, start);
2834 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
2835 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
2836 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
2837 inst->ignore_sbar = TRUE;
2838 #if !GTK_CHECK_VERSION(3,18,0)
2839 gtk_adjustment_changed(inst->sbar_adjust);
2841 inst->ignore_sbar = FALSE;
2844 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2846 struct gui_data *inst = (struct gui_data *)data;
2848 if (!conf_get_int(inst->conf, CONF_scrollbar))
2850 if (!inst->ignore_sbar)
2851 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
2854 static void show_scrollbar(struct gui_data *inst, gboolean visible)
2856 inst->sbar_visible = visible;
2858 gtk_widget_show(inst->sbar);
2860 gtk_widget_hide(inst->sbar);
2863 void sys_cursor(void *frontend, int x, int y)
2866 * This is meaningless under X.
2871 * This is still called when mode==BELL_VISUAL, even though the
2872 * visual bell is handled entirely within terminal.c, because we
2873 * may want to perform additional actions on any kind of bell (for
2874 * example, taskbar flashing in Windows).
2876 void do_beep(void *frontend, int mode)
2878 if (mode == BELL_DEFAULT)
2882 int char_width(Context ctx, int uc)
2885 * In this front end, double-width characters are handled using a
2886 * separate font, so this can safely just return 1 always.
2891 Context get_ctx(void *frontend)
2893 struct gui_data *inst = (struct gui_data *)frontend;
2894 struct draw_ctx *dctx;
2896 if (!gtk_widget_get_window(inst->area))
2899 dctx = snew(struct draw_ctx);
2901 dctx->uctx.type = inst->drawtype;
2902 #ifdef DRAW_TEXT_GDK
2903 if (dctx->uctx.type == DRAWTYPE_GDK) {
2904 /* If we're doing GDK-based drawing, then we also expect
2905 * inst->pixmap to exist. */
2906 dctx->uctx.u.gdk.target = inst->pixmap;
2907 dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
2910 #ifdef DRAW_TEXT_CAIRO
2911 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2912 dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
2913 /* If we're doing Cairo drawing, we expect inst->surface to
2914 * exist, and we draw to that first, regardless of whether we
2915 * subsequently copy the results to inst->pixmap. */
2916 dctx->uctx.u.cairo.cr = cairo_create(inst->surface);
2917 cairo_setup_dctx(dctx);
2923 void free_ctx(Context ctx)
2925 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2926 /* struct gui_data *inst = dctx->inst; */
2927 #ifdef DRAW_TEXT_GDK
2928 if (dctx->uctx.type == DRAWTYPE_GDK) {
2929 gdk_gc_unref(dctx->uctx.u.gdk.gc);
2932 #ifdef DRAW_TEXT_CAIRO
2933 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2934 cairo_destroy(dctx->uctx.u.cairo.cr);
2941 static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
2943 #if defined DRAW_TEXT_CAIRO && !defined NO_BACKING_PIXMAPS
2944 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2946 * If inst->surface and inst->pixmap both exist, then we've
2947 * just drawn new content to the former which we must copy to
2950 cairo_t *cr = gdk_cairo_create(dctx->inst->pixmap);
2951 cairo_set_source_surface(cr, dctx->inst->surface, 0, 0);
2952 cairo_rectangle(cr, x, y, w, h);
2959 * Now we just queue a window redraw, which will cause
2960 * inst->surface or inst->pixmap (whichever is appropriate for our
2961 * compile mode) to be copied to the real window when we receive
2962 * the resulting "expose" or "draw" event.
2964 * Amazingly, this one API call is actually valid in all versions
2967 gtk_widget_queue_draw_area(dctx->inst->area, x, y, w, h);
2970 static void draw_set_colour(struct draw_ctx *dctx, int col)
2972 #ifdef DRAW_TEXT_GDK
2973 if (dctx->uctx.type == DRAWTYPE_GDK) {
2974 gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
2977 #ifdef DRAW_TEXT_CAIRO
2978 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2979 cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
2980 dctx->inst->cols[col].red / 65535.0,
2981 dctx->inst->cols[col].green / 65535.0,
2982 dctx->inst->cols[col].blue / 65535.0);
2987 static void draw_rectangle(struct draw_ctx *dctx, int filled,
2988 int x, int y, int w, int h)
2990 #ifdef DRAW_TEXT_GDK
2991 if (dctx->uctx.type == DRAWTYPE_GDK) {
2992 gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2993 filled, x, y, w, h);
2996 #ifdef DRAW_TEXT_CAIRO
2997 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2998 cairo_new_path(dctx->uctx.u.cairo.cr);
3000 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
3001 cairo_fill(dctx->uctx.u.cairo.cr);
3003 cairo_rectangle(dctx->uctx.u.cairo.cr,
3004 x + 0.5, y + 0.5, w, h);
3005 cairo_close_path(dctx->uctx.u.cairo.cr);
3006 cairo_stroke(dctx->uctx.u.cairo.cr);
3012 static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
3014 #ifdef DRAW_TEXT_GDK
3015 if (dctx->uctx.type == DRAWTYPE_GDK) {
3023 gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
3026 #ifdef DRAW_TEXT_CAIRO
3027 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3028 cairo_reset_clip(dctx->uctx.u.cairo.cr);
3029 cairo_new_path(dctx->uctx.u.cairo.cr);
3030 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
3031 cairo_clip(dctx->uctx.u.cairo.cr);
3036 static void draw_point(struct draw_ctx *dctx, int x, int y)
3038 #ifdef DRAW_TEXT_GDK
3039 if (dctx->uctx.type == DRAWTYPE_GDK) {
3040 gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
3043 #ifdef DRAW_TEXT_CAIRO
3044 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3045 cairo_new_path(dctx->uctx.u.cairo.cr);
3046 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
3047 cairo_fill(dctx->uctx.u.cairo.cr);
3052 static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
3054 #ifdef DRAW_TEXT_GDK
3055 if (dctx->uctx.type == DRAWTYPE_GDK) {
3056 gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
3060 #ifdef DRAW_TEXT_CAIRO
3061 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3062 cairo_new_path(dctx->uctx.u.cairo.cr);
3063 cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
3064 cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
3065 cairo_stroke(dctx->uctx.u.cairo.cr);
3070 static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
3072 int h, int hdouble, int hbothalf)
3074 #ifdef DRAW_TEXT_CAIRO
3075 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3076 cairo_matrix_t matrix;
3100 cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
3105 static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
3107 int h, int hdouble, int hbothalf)
3109 #ifdef DRAW_TEXT_GDK
3110 #ifndef NO_BACKING_PIXMAPS
3111 if (dctx->uctx.type == DRAWTYPE_GDK) {
3113 * I can't find any plausible StretchBlt equivalent in the X
3114 * server, so I'm going to do this the slow and painful way.
3115 * This will involve repeated calls to gdk_draw_pixmap() to
3116 * stretch the text horizontally. It's O(N^2) in time and O(N)
3117 * in network bandwidth, but you try thinking of a better way.
3122 for (i = 0; i < w; i++) {
3123 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3124 dctx->uctx.u.gdk.gc,
3125 dctx->uctx.u.gdk.target,
3135 /* Now stretch vertically, in the same way. */
3140 for (i = 0; i < h; i += 2) {
3141 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3142 dctx->uctx.u.gdk.gc,
3143 dctx->uctx.u.gdk.target,
3151 #error No way to implement stretching in GDK without a reliable backing pixmap
3153 #endif /* DRAW_TEXT_GDK */
3154 #ifdef DRAW_TEXT_CAIRO
3155 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3156 cairo_set_matrix(dctx->uctx.u.cairo.cr,
3157 &dctx->uctx.u.cairo.origmatrix);
3162 static void draw_backing_rect(struct gui_data *inst)
3164 struct draw_ctx *dctx = get_ctx(inst);
3165 int w = inst->width * inst->font_width + 2*inst->window_border;
3166 int h = inst->height * inst->font_height + 2*inst->window_border;
3167 draw_set_colour(dctx, 258);
3168 draw_rectangle(dctx, 1, 0, 0, w, h);
3169 draw_update(dctx, 0, 0, w, h);
3174 * Draw a line of text in the window, at given character
3175 * coordinates, in given attributes.
3177 * We are allowed to fiddle with the contents of `text'.
3179 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
3180 unsigned long attr, int lattr)
3182 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3183 struct gui_data *inst = dctx->inst;
3185 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
3187 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
3189 if (attr & TATTR_COMBINING) {
3195 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
3196 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
3197 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
3202 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
3203 if (nfg < 16) nfg |= 8;
3204 else if (nfg >= 256) nfg |= 1;
3206 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
3207 if (nbg < 16) nbg |= 8;
3208 else if (nbg >= 256) nbg |= 1;
3210 if ((attr & TATTR_ACTCURS) && !monochrome) {
3215 fontid = shadow = 0;
3217 if (attr & ATTR_WIDE) {
3224 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
3231 if (!inst->fonts[fontid]) {
3234 * Fall back through font ids with subsets of this one's
3235 * set bits, in order.
3237 for (i = fontid; i-- > 0 ;) {
3239 continue; /* some other bit is set */
3240 if (inst->fonts[i]) {
3245 assert(inst->fonts[fontid]); /* we should at least have hit zero */
3248 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3250 if (x >= inst->term->cols)
3252 if (x + len*2*widefactor > inst->term->cols)
3253 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3259 x*inst->font_width+inst->window_border,
3260 y*inst->font_height+inst->window_border,
3261 rlen*widefactor*inst->font_width,
3264 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3265 draw_stretch_before(dctx,
3266 x*inst->font_width+inst->window_border,
3267 y*inst->font_height+inst->window_border,
3268 rlen*widefactor*inst->font_width, TRUE,
3270 ((lattr & LATTR_MODE) != LATTR_WIDE),
3271 ((lattr & LATTR_MODE) == LATTR_BOT));
3274 draw_set_colour(dctx, nbg);
3275 draw_rectangle(dctx, TRUE,
3276 x*inst->font_width+inst->window_border,
3277 y*inst->font_height+inst->window_border,
3278 rlen*widefactor*inst->font_width, inst->font_height);
3280 draw_set_colour(dctx, nfg);
3281 if (ncombining > 1) {
3283 unifont_draw_combining(&dctx->uctx, inst->fonts[fontid],
3284 x*inst->font_width+inst->window_border,
3285 (y*inst->font_height+inst->window_border+
3286 inst->fonts[0]->ascent),
3287 text, ncombining, widefactor > 1,
3288 bold, inst->font_width);
3290 unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
3291 x*inst->font_width+inst->window_border,
3292 (y*inst->font_height+inst->window_border+
3293 inst->fonts[0]->ascent),
3294 text, len, widefactor > 1,
3295 bold, inst->font_width);
3298 if (attr & ATTR_UNDER) {
3299 int uheight = inst->fonts[0]->ascent + 1;
3300 if (uheight >= inst->font_height)
3301 uheight = inst->font_height - 1;
3302 draw_line(dctx, x*inst->font_width+inst->window_border,
3303 y*inst->font_height + uheight + inst->window_border,
3304 (x+len)*widefactor*inst->font_width-1+inst->window_border,
3305 y*inst->font_height + uheight + inst->window_border);
3308 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3309 draw_stretch_after(dctx,
3310 x*inst->font_width+inst->window_border,
3311 y*inst->font_height+inst->window_border,
3312 rlen*widefactor*inst->font_width, TRUE,
3314 ((lattr & LATTR_MODE) != LATTR_WIDE),
3315 ((lattr & LATTR_MODE) == LATTR_BOT));
3319 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3320 unsigned long attr, int lattr)
3322 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3323 struct gui_data *inst = dctx->inst;
3326 do_text_internal(ctx, x, y, text, len, attr, lattr);
3328 if (attr & ATTR_WIDE) {
3334 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3336 if (x >= inst->term->cols)
3338 if (x + len*2*widefactor > inst->term->cols)
3339 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3344 x*inst->font_width+inst->window_border,
3345 y*inst->font_height+inst->window_border,
3346 len*widefactor*inst->font_width, inst->font_height);
3349 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
3350 unsigned long attr, int lattr)
3352 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3353 struct gui_data *inst = dctx->inst;
3355 int active, passive, widefactor;
3357 if (attr & TATTR_PASCURS) {
3358 attr &= ~TATTR_PASCURS;
3362 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
3363 attr &= ~TATTR_ACTCURS;
3367 do_text_internal(ctx, x, y, text, len, attr, lattr);
3369 if (attr & TATTR_COMBINING)
3372 if (attr & ATTR_WIDE) {
3378 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3380 if (x >= inst->term->cols)
3382 if (x + len*2*widefactor > inst->term->cols)
3383 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3387 if (inst->cursor_type == 0) {
3389 * An active block cursor will already have been done by
3390 * the above do_text call, so we only need to do anything
3394 draw_set_colour(dctx, 261);
3395 draw_rectangle(dctx, FALSE,
3396 x*inst->font_width+inst->window_border,
3397 y*inst->font_height+inst->window_border,
3398 len*widefactor*inst->font_width-1,
3399 inst->font_height-1);
3403 int startx, starty, dx, dy, length, i;
3407 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
3408 char_width = 2*inst->font_width;
3410 char_width = inst->font_width;
3412 if (inst->cursor_type == 1) {
3413 uheight = inst->fonts[0]->ascent + 1;
3414 if (uheight >= inst->font_height)
3415 uheight = inst->font_height - 1;
3417 startx = x * inst->font_width + inst->window_border;
3418 starty = y * inst->font_height + inst->window_border + uheight;
3421 length = len * widefactor * char_width;
3424 if (attr & TATTR_RIGHTCURS)
3425 xadjust = char_width - 1;
3426 startx = x * inst->font_width + inst->window_border + xadjust;
3427 starty = y * inst->font_height + inst->window_border;
3430 length = inst->font_height;
3433 draw_set_colour(dctx, 261);
3435 for (i = 0; i < length; i++) {
3437 draw_point(dctx, startx, starty);
3442 } else if (active) {
3443 draw_line(dctx, startx, starty,
3444 startx + (length-1) * dx, starty + (length-1) * dy);
3445 } /* else no cursor (e.g., blinked off) */
3449 x*inst->font_width+inst->window_border,
3450 y*inst->font_height+inst->window_border,
3451 len*widefactor*inst->font_width, inst->font_height);
3453 #if GTK_CHECK_VERSION(2,0,0)
3455 GdkRectangle cursorrect;
3456 cursorrect.x = x*inst->font_width+inst->window_border;
3457 cursorrect.y = y*inst->font_height+inst->window_border;
3458 cursorrect.width = len*widefactor*inst->font_width;
3459 cursorrect.height = inst->font_height;
3460 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
3465 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
3467 if (cursor_val == -1) {
3468 #if GTK_CHECK_VERSION(2,16,0)
3469 cursor_val = GDK_BLANK_CURSOR;
3472 * Work around absence of GDK_BLANK_CURSOR by inventing a
3476 GdkColor bg = { 0, 0, 0, 0 };
3477 GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
3478 GdkGC *gc = gdk_gc_new(pm);
3479 gdk_gc_set_foreground(gc, &bg);
3480 gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
3482 ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
3483 gdk_pixmap_unref(pm);
3488 return gdk_cursor_new(cursor_val);
3491 void modalfatalbox(const char *p, ...)
3494 fprintf(stderr, "FATAL ERROR: ");
3496 vfprintf(stderr, p, ap);
3498 fputc('\n', stderr);
3502 void cmdline_error(const char *p, ...)
3505 fprintf(stderr, "%s: ", appname);
3507 vfprintf(stderr, p, ap);
3509 fputc('\n', stderr);
3513 const char *get_x_display(void *frontend)
3515 return gdk_get_display();
3518 #ifndef NOT_X_WINDOWS
3519 long get_windowid(void *frontend)
3521 struct gui_data *inst = (struct gui_data *)frontend;
3522 return (long)GDK_WINDOW_XID(gtk_widget_get_window(inst->area));
3526 int frontend_is_utf8(void *frontend)
3528 struct gui_data *inst = (struct gui_data *)frontend;
3529 return inst->ucsdata.line_codepage == CS_UTF8;
3532 char *setup_fonts_ucs(struct gui_data *inst)
3534 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
3535 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
3540 fs = conf_get_fontspec(inst->conf, CONF_font);
3541 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
3542 shadowboldoffset, shadowbold);
3544 return dupprintf("unable to load font \"%s\"", fs->name);
3547 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
3548 if (shadowbold || !fs->name[0]) {
3551 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
3552 shadowboldoffset, shadowbold);
3555 unifont_destroy(fonts[0]);
3556 return dupprintf("unable to load bold font \"%s\"", fs->name);
3560 fs = conf_get_fontspec(inst->conf, CONF_widefont);
3562 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
3563 shadowboldoffset, shadowbold);
3565 for (i = 0; i < 2; i++)
3567 unifont_destroy(fonts[i]);
3568 return dupprintf("unable to load wide font \"%s\"", fs->name);
3574 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
3575 if (shadowbold || !fs->name[0]) {
3578 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
3579 shadowboldoffset, shadowbold);
3581 for (i = 0; i < 3; i++)
3583 unifont_destroy(fonts[i]);
3584 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
3589 * Now we've got past all the possible error conditions, we can
3590 * actually update our state.
3593 for (i = 0; i < 4; i++) {
3595 unifont_destroy(inst->fonts[i]);
3596 inst->fonts[i] = fonts[i];
3599 inst->font_width = inst->fonts[0]->width;
3600 inst->font_height = inst->fonts[0]->height;
3602 inst->direct_to_font = init_ucs(&inst->ucsdata,
3603 conf_get_str(inst->conf, CONF_line_codepage),
3604 conf_get_int(inst->conf, CONF_utf8_override),
3605 inst->fonts[0]->public_charset,
3606 conf_get_int(inst->conf, CONF_vtmode));
3608 inst->drawtype = inst->fonts[0]->preferred_drawtype;
3613 static void compute_geom_hints(struct gui_data *inst, GdkGeometry *geom)
3616 * Unused fields in geom.
3618 geom->max_width = geom->max_height = -1;
3619 geom->min_aspect = geom->max_aspect = 0;
3622 * Set up the geometry fields we care about, with reference to
3623 * just the drawing area. We'll correct for the scrollbar in a
3626 geom->min_width = inst->font_width + 2*inst->window_border;
3627 geom->min_height = inst->font_height + 2*inst->window_border;
3628 geom->base_width = 2*inst->window_border;
3629 geom->base_height = 2*inst->window_border;
3630 geom->width_inc = inst->font_width;
3631 geom->height_inc = inst->font_height;
3634 * If we've got a scrollbar visible, then we must include its
3635 * width as part of the base and min width, and also ensure that
3636 * our window's minimum height is at least the height required by
3639 * In the latter case, we must also take care to arrange that
3640 * (geom->min_height - geom->base_height) is an integer multiple of
3641 * geom->height_inc, because if it's not, then some window managers
3642 * (we know of xfwm4) get confused, with the effect that they
3643 * resize our window to a height based on min_height instead of
3644 * base_height, which we then round down and the window ends up
3647 if (inst->sbar_visible) {
3651 #if GTK_CHECK_VERSION(3,0,0)
3652 gtk_widget_get_preferred_size(inst->sbar, &req, NULL);
3654 gtk_widget_size_request(inst->sbar, &req);
3657 /* Compute rounded-up scrollbar height. */
3658 min_sb_height = req.height;
3659 min_sb_height += geom->height_inc - 1;
3660 min_sb_height -= ((min_sb_height - geom->base_height%geom->height_inc)
3661 % geom->height_inc);
3663 geom->min_width += req.width;
3664 geom->base_width += req.width;
3665 if (geom->min_height < min_sb_height)
3666 geom->min_height = min_sb_height;
3670 void set_geom_hints(struct gui_data *inst)
3673 compute_geom_hints(inst, &geom);
3674 gtk_window_set_geometry_hints
3675 (GTK_WINDOW(inst->window), NULL, &geom,
3676 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | GDK_HINT_RESIZE_INC);
3679 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
3681 struct gui_data *inst = (struct gui_data *)data;
3682 term_clrsb(inst->term);
3685 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3687 struct gui_data *inst = (struct gui_data *)data;
3688 term_pwron(inst->term, TRUE);
3690 ldisc_echoedit_update(inst->ldisc);
3693 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3695 struct gui_data *inst = (struct gui_data *)data;
3696 term_copyall(inst->term);
3699 void special_menuitem(GtkMenuItem *item, gpointer data)
3701 struct gui_data *inst = (struct gui_data *)data;
3702 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
3706 inst->back->special(inst->backhandle, code);
3709 void about_menuitem(GtkMenuItem *item, gpointer data)
3711 struct gui_data *inst = (struct gui_data *)data;
3712 about_box(inst->window);
3715 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3717 struct gui_data *inst = (struct gui_data *)data;
3718 showeventlog(inst->eventlogstuff, inst->window);
3721 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3723 /* This maps colour indices in inst->conf to those used in inst->cols. */
3724 static const int ww[] = {
3725 256, 257, 258, 259, 260, 261,
3726 0, 8, 1, 9, 2, 10, 3, 11,
3727 4, 12, 5, 13, 6, 14, 7, 15
3729 struct gui_data *inst = (struct gui_data *)data;
3731 Conf *oldconf, *newconf;
3732 int i, j, need_size;
3734 assert(lenof(ww) == NCFGCOLOURS);
3736 if (inst->reconfiguring)
3739 inst->reconfiguring = TRUE;
3741 title = dupcat(appname, " Reconfiguration", NULL);
3743 oldconf = inst->conf;
3744 newconf = conf_copy(inst->conf);
3746 if (do_config_box(title, newconf, 1,
3747 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3748 inst->conf = newconf;
3750 /* Pass new config data to the logging module */
3751 log_reconfig(inst->logctx, inst->conf);
3753 * Flush the line discipline's edit buffer in the case
3754 * where local editing has just been disabled.
3757 ldisc_configure(inst->ldisc, inst->conf);
3758 ldisc_echoedit_update(inst->ldisc);
3760 /* Pass new config data to the terminal */
3761 term_reconfig(inst->term, inst->conf);
3762 /* Pass new config data to the back end */
3764 inst->back->reconfig(inst->backhandle, inst->conf);
3766 cache_conf_values(inst);
3769 * Just setting inst->conf is sufficient to cause colour
3770 * setting changes to appear on the next ESC]R palette
3771 * reset. But we should also check whether any colour
3772 * settings have been changed, and revert the ones that have
3773 * to the new default, on the assumption that the user is
3774 * most likely to want an immediate update.
3776 for (i = 0; i < NCFGCOLOURS; i++) {
3777 for (j = 0; j < 3; j++)
3778 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3779 conf_get_int_int(newconf, CONF_colours, i*3+j))
3782 real_palette_set(inst, ww[i],
3783 conf_get_int_int(newconf,CONF_colours,i*3+0),
3784 conf_get_int_int(newconf,CONF_colours,i*3+1),
3785 conf_get_int_int(newconf,CONF_colours,i*3+2));
3788 * If the default background has changed, we must
3789 * repaint the space in between the window border
3790 * and the text area.
3793 set_window_background(inst);
3794 draw_backing_rect(inst);
3802 * If the scrollbar needs to be shown, hidden, or moved
3803 * from one end to the other of the window, do so now.
3805 if (conf_get_int(oldconf, CONF_scrollbar) !=
3806 conf_get_int(newconf, CONF_scrollbar)) {
3807 show_scrollbar(inst, conf_get_int(newconf, CONF_scrollbar));
3810 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3811 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3812 gtk_box_reorder_child(inst->hbox, inst->sbar,
3813 conf_get_int(newconf, CONF_scrollbar_on_left)
3818 * Change the window title, if required.
3820 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3821 conf_get_str(newconf, CONF_wintitle)))
3822 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3823 set_window_titles(inst);
3826 * Redo the whole tangled fonts and Unicode mess if
3829 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3830 conf_get_fontspec(newconf, CONF_font)->name) ||
3831 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3832 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3833 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3834 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3835 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3836 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3837 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3838 conf_get_str(newconf, CONF_line_codepage)) ||
3839 conf_get_int(oldconf, CONF_utf8_override) !=
3840 conf_get_int(newconf, CONF_utf8_override) ||
3841 conf_get_int(oldconf, CONF_vtmode) !=
3842 conf_get_int(newconf, CONF_vtmode) ||
3843 conf_get_int(oldconf, CONF_shadowbold) !=
3844 conf_get_int(newconf, CONF_shadowbold) ||
3845 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3846 conf_get_int(newconf, CONF_shadowboldoffset)) {
3847 char *errmsg = setup_fonts_ucs(inst);
3850 dupprintf("Could not change fonts in terminal window: %s\n",
3852 messagebox(inst->window, "Font setup error", msgboxtext,
3853 string_width("Could not change fonts in terminal window:"),
3854 FALSE, "OK", 'o', +1, 1,
3864 * Resize the window.
3866 if (conf_get_int(oldconf, CONF_width) !=
3867 conf_get_int(newconf, CONF_width) ||
3868 conf_get_int(oldconf, CONF_height) !=
3869 conf_get_int(newconf, CONF_height) ||
3870 conf_get_int(oldconf, CONF_window_border) !=
3871 conf_get_int(newconf, CONF_window_border) ||
3873 set_geom_hints(inst);
3874 request_resize(inst, conf_get_int(newconf, CONF_width),
3875 conf_get_int(newconf, CONF_height));
3878 * The above will have caused a call to term_size() for
3879 * us if it happened. If the user has fiddled with only
3880 * the scrollback size, the above will not have
3881 * happened and we will need an explicit term_size()
3884 if (conf_get_int(oldconf, CONF_savelines) !=
3885 conf_get_int(newconf, CONF_savelines))
3886 term_size(inst->term, inst->term->rows, inst->term->cols,
3887 conf_get_int(newconf, CONF_savelines));
3890 term_invalidate(inst->term);
3893 * We do an explicit full redraw here to ensure the window
3894 * border has been redrawn as well as the text area.
3896 gtk_widget_queue_draw(inst->area);
3903 inst->reconfiguring = FALSE;
3906 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3908 struct gui_data *inst = (struct gui_data *)gdata;
3910 launch_duplicate_session(inst->conf);
3913 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3915 launch_new_session();
3918 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3920 struct gui_data *inst = (struct gui_data *)data;
3923 logevent(inst, "----- Session restarted -----");
3924 term_pwron(inst->term, FALSE);
3925 start_backend(inst);
3926 inst->exited = FALSE;
3930 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3932 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3934 launch_saved_session(str);
3937 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3939 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3944 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3946 struct gui_data *inst = (struct gui_data *)data;
3947 struct sesslist sesslist;
3950 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3951 (GtkCallback)gtk_widget_destroy, NULL);
3953 get_sesslist(&sesslist, TRUE);
3954 /* skip sesslist.sessions[0] == Default Settings */
3955 for (i = 1; i < sesslist.nsessions; i++) {
3956 GtkWidget *menuitem =
3957 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3958 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3959 gtk_widget_show(menuitem);
3960 g_object_set_data(G_OBJECT(menuitem), "user-data",
3961 dupstr(sesslist.sessions[i]));
3962 g_signal_connect(G_OBJECT(menuitem), "activate",
3963 G_CALLBACK(saved_session_menuitem),
3965 g_signal_connect(G_OBJECT(menuitem), "destroy",
3966 G_CALLBACK(saved_session_freedata),
3969 if (sesslist.nsessions <= 1) {
3970 GtkWidget *menuitem =
3971 gtk_menu_item_new_with_label("(No sessions)");
3972 gtk_widget_set_sensitive(menuitem, FALSE);
3973 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3974 gtk_widget_show(menuitem);
3976 get_sesslist(&sesslist, FALSE); /* free up */
3979 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3982 #if GTK_CHECK_VERSION(2,0,0)
3987 GdkBitmap *iconmask;
3993 gtk_widget_realize(window);
3994 #if GTK_CHECK_VERSION(2,0,0)
3995 gtk_window_set_icon(GTK_WINDOW(window),
3996 gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
3998 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
3999 &iconmask, NULL, (gchar **)icon[0]);
4000 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
4003 #if GTK_CHECK_VERSION(2,0,0)
4005 for (n = 0; n < n_icon; n++) {
4007 g_list_append(iconlist,
4008 gdk_pixbuf_new_from_xpm_data((const gchar **)
4011 gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
4015 void update_specials_menu(void *frontend)
4017 struct gui_data *inst = (struct gui_data *)frontend;
4019 const struct telnet_special *specials;
4022 specials = inst->back->get_specials(inst->backhandle);
4026 /* I believe this disposes of submenus too. */
4027 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
4028 (GtkCallback)gtk_widget_destroy, NULL);
4031 GtkWidget *menu = inst->specialsmenu;
4032 /* A lame "stack" for submenus that will do for now. */
4033 GtkWidget *saved_menu = NULL;
4035 for (i = 0; nesting > 0; i++) {
4036 GtkWidget *menuitem = NULL;
4037 switch (specials[i].code) {
4039 assert (nesting < 2);
4040 saved_menu = menu; /* XXX lame stacking */
4041 menu = gtk_menu_new();
4042 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4043 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
4044 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
4045 gtk_widget_show(menuitem);
4052 menu = saved_menu; /* XXX lame stacking */
4057 menuitem = gtk_menu_item_new();
4060 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4061 g_object_set_data(G_OBJECT(menuitem), "user-data",
4062 GINT_TO_POINTER(specials[i].code));
4063 g_signal_connect(G_OBJECT(menuitem), "activate",
4064 G_CALLBACK(special_menuitem), inst);
4068 gtk_container_add(GTK_CONTAINER(menu), menuitem);
4069 gtk_widget_show(menuitem);
4072 gtk_widget_show(inst->specialsitem1);
4073 gtk_widget_show(inst->specialsitem2);
4075 gtk_widget_hide(inst->specialsitem1);
4076 gtk_widget_hide(inst->specialsitem2);
4080 static void start_backend(struct gui_data *inst)
4082 extern Backend *select_backend(Conf *conf);
4087 inst->back = select_backend(inst->conf);
4089 error = inst->back->init((void *)inst, &inst->backhandle,
4091 conf_get_str(inst->conf, CONF_host),
4092 conf_get_int(inst->conf, CONF_port),
4094 conf_get_int(inst->conf, CONF_tcp_nodelay),
4095 conf_get_int(inst->conf, CONF_tcp_keepalives));
4098 char *msg = dupprintf("Unable to open connection to %s:\n%s",
4099 conf_get_str(inst->conf, CONF_host), error);
4100 inst->exited = TRUE;
4101 fatal_message_box(inst->window, msg);
4106 s = conf_get_str(inst->conf, CONF_wintitle);
4108 set_title_and_icon(inst, s, s);
4110 char *title = make_default_wintitle(realhost);
4111 set_title_and_icon(inst, title, title);
4116 inst->back->provide_logctx(inst->backhandle, inst->logctx);
4118 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
4121 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
4124 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4127 struct gui_data *new_session_window(Conf *conf, const char *geometry_string)
4129 struct gui_data *inst;
4132 * Create an instance structure and initialise to zeroes
4134 inst = snew(struct gui_data);
4135 memset(inst, 0, sizeof(*inst));
4136 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
4137 inst->busy_status = BUSY_NOT;
4139 inst->wintitle = inst->icontitle = NULL;
4140 inst->drawtype = DRAWTYPE_DEFAULT;
4141 #if GTK_CHECK_VERSION(3,4,0)
4142 inst->cumulative_scroll = 0.0;
4145 if (geometry_string) {
4146 #if GTK_CHECK_VERSION(2,0,0)
4147 inst->geometry = geometry_string;
4149 /* On GTK 1, we have to do this using raw Xlib */
4152 flags = XParseGeometry(geometry_string, &x, &y, &w, &h);
4153 if (flags & WidthValue)
4154 conf_set_int(conf, CONF_width, w);
4155 if (flags & HeightValue)
4156 conf_set_int(conf, CONF_height, h);
4158 if (flags & (XValue | YValue)) {
4161 inst->gotpos = TRUE;
4162 inst->gravity = ((flags & XNegative ? 1 : 0) |
4163 (flags & YNegative ? 2 : 0));
4168 if (!compound_text_atom)
4169 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
4170 if (!utf8_string_atom)
4171 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
4173 inst->area = gtk_drawing_area_new();
4175 #if GTK_CHECK_VERSION(2,0,0)
4176 inst->imc = gtk_im_multicontext_new();
4180 char *errmsg = setup_fonts_ucs(inst);
4182 fprintf(stderr, "%s: %s\n", appname, errmsg);
4186 inst->window = make_gtk_toplevel_window(inst);
4188 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
4190 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
4191 winclass, winclass);
4195 * Set up the colour map.
4197 palette_reset(inst);
4199 inst->width = conf_get_int(inst->conf, CONF_width);
4200 inst->height = conf_get_int(inst->conf, CONF_height);
4201 cache_conf_values(inst);
4203 init_clipboard(inst);
4205 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
4206 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
4207 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
4209 * We always create the scrollbar; it remains invisible if
4210 * unwanted, so we can pop it up quickly if it suddenly becomes
4213 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
4214 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4215 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
4216 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
4217 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4219 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
4221 gtk_widget_show(inst->area);
4222 show_scrollbar(inst, conf_get_int(inst->conf, CONF_scrollbar));
4223 gtk_widget_show(GTK_WIDGET(inst->hbox));
4225 set_geom_hints(inst);
4227 #if GTK_CHECK_VERSION(3,0,0)
4228 gtk_window_set_default_geometry(GTK_WINDOW(inst->window),
4229 inst->width, inst->height);
4232 int w = inst->font_width * inst->width + 2*inst->window_border;
4233 int h = inst->font_height * inst->height + 2*inst->window_border;
4234 #if GTK_CHECK_VERSION(2,0,0)
4235 gtk_widget_set_size_request(inst->area, w, h);
4237 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
4242 #if GTK_CHECK_VERSION(2,0,0)
4243 if (inst->geometry) {
4244 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
4248 int x = inst->xpos, y = inst->ypos;
4250 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
4251 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
4252 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
4253 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
4254 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
4258 g_signal_connect(G_OBJECT(inst->window), "destroy",
4259 G_CALLBACK(destroy), inst);
4260 g_signal_connect(G_OBJECT(inst->window), "delete_event",
4261 G_CALLBACK(delete_window), inst);
4262 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
4263 G_CALLBACK(key_event), inst);
4264 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
4265 G_CALLBACK(key_event), inst);
4266 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
4267 G_CALLBACK(focus_event), inst);
4268 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
4269 G_CALLBACK(focus_event), inst);
4270 g_signal_connect(G_OBJECT(inst->area), "configure_event",
4271 G_CALLBACK(configure_area), inst);
4272 #if GTK_CHECK_VERSION(3,0,0)
4273 g_signal_connect(G_OBJECT(inst->area), "draw",
4274 G_CALLBACK(draw_area), inst);
4276 g_signal_connect(G_OBJECT(inst->area), "expose_event",
4277 G_CALLBACK(expose_area), inst);
4279 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
4280 G_CALLBACK(button_event), inst);
4281 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
4282 G_CALLBACK(button_event), inst);
4283 #if GTK_CHECK_VERSION(2,0,0)
4284 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
4285 G_CALLBACK(scroll_event), inst);
4287 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
4288 G_CALLBACK(motion_event), inst);
4289 #if GTK_CHECK_VERSION(2,0,0)
4290 g_signal_connect(G_OBJECT(inst->imc), "commit",
4291 G_CALLBACK(input_method_commit_event), inst);
4293 if (conf_get_int(inst->conf, CONF_scrollbar))
4294 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
4295 G_CALLBACK(scrollbar_moved), inst);
4296 gtk_widget_add_events(GTK_WIDGET(inst->area),
4297 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
4298 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
4299 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK
4300 #if GTK_CHECK_VERSION(3,4,0)
4301 | GDK_SMOOTH_SCROLL_MASK
4306 extern const char *const *const main_icon[];
4307 extern const int n_main_icon;
4308 set_window_icon(inst->window, main_icon, n_main_icon);
4311 gtk_widget_show(inst->window);
4313 set_window_background(inst);
4316 * Set up the Ctrl+rightclick context menu.
4319 GtkWidget *menuitem;
4321 extern const int use_event_log, new_session, saved_sessions;
4323 inst->menu = gtk_menu_new();
4325 #define MKMENUITEM(title, func) do \
4327 menuitem = gtk_menu_item_new_with_label(title); \
4328 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4329 gtk_widget_show(menuitem); \
4330 g_signal_connect(G_OBJECT(menuitem), "activate", \
4331 G_CALLBACK(func), inst); \
4334 #define MKSUBMENU(title) do \
4336 menuitem = gtk_menu_item_new_with_label(title); \
4337 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4338 gtk_widget_show(menuitem); \
4341 #define MKSEP() do \
4343 menuitem = gtk_menu_item_new(); \
4344 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4345 gtk_widget_show(menuitem); \
4349 MKMENUITEM("New Session...", new_session_menuitem);
4350 MKMENUITEM("Restart Session", restart_session_menuitem);
4351 inst->restartitem = menuitem;
4352 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4353 MKMENUITEM("Duplicate Session", dup_session_menuitem);
4354 if (saved_sessions) {
4355 inst->sessionsmenu = gtk_menu_new();
4356 /* sessionsmenu will be updated when it's invoked */
4357 /* XXX is this the right way to do dynamic menus in Gtk? */
4358 MKMENUITEM("Saved Sessions", update_savedsess_menu);
4359 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
4360 inst->sessionsmenu);
4363 MKMENUITEM("Change Settings...", change_settings_menuitem);
4366 MKMENUITEM("Event Log", event_log_menuitem);
4367 MKSUBMENU("Special Commands");
4368 inst->specialsmenu = gtk_menu_new();
4369 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
4370 inst->specialsitem1 = menuitem;
4372 inst->specialsitem2 = menuitem;
4373 gtk_widget_hide(inst->specialsitem1);
4374 gtk_widget_hide(inst->specialsitem2);
4375 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
4376 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
4377 MKMENUITEM("Copy All", copy_all_menuitem);
4379 s = dupcat("About ", appname, NULL);
4380 MKMENUITEM(s, about_menuitem);
4387 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
4388 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
4389 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
4390 inst->blankcursor = make_mouse_ptr(inst, -1);
4391 inst->currcursor = inst->textcursor;
4392 show_mouseptr(inst, 1);
4394 inst->eventlogstuff = eventlogstuff_new();
4396 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
4397 inst->logctx = log_init(inst, inst->conf);
4398 term_provide_logctx(inst->term, inst->logctx);
4400 term_size(inst->term, inst->height, inst->width,
4401 conf_get_int(inst->conf, CONF_savelines));
4403 start_backend(inst);
4405 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
4407 inst->exited = FALSE;