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;
60 GtkAdjustment *sbar_adjust;
61 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
63 GtkWidget *sessionsmenu;
64 #ifndef NO_BACKING_PIXMAPS
66 * Server-side pixmap which we use to cache the terminal window's
67 * contents. When we draw text in the terminal, we draw it to this
68 * pixmap first, and then blit from there to the actual window;
69 * this way, X expose events can be handled with an absolute
70 * minimum of network traffic, by just sending a command to
71 * re-blit an appropriate rectangle from this pixmap.
75 #ifdef DRAW_TEXT_CAIRO
77 * If we're drawing using Cairo, we cache the same image on the
78 * client side in a Cairo surface.
80 * In GTK2+Cairo, this happens _as well_ as having the server-side
81 * pixmap cache above; in GTK3+Cairo, server-side pixmaps are
82 * deprecated, so we _just_ have this client-side cache. In the
83 * latter case that means we have to transmit a big wodge of
84 * bitmap data over the X connection on every expose event; but
85 * GTK3 apparently deliberately provides no way to avoid that
86 * inefficiency, and at least this way we don't _also_ have to
87 * redo any font rendering just because the window was temporarily
90 cairo_surface_t *surface;
92 #if GTK_CHECK_VERSION(2,0,0)
95 unifont *fonts[4]; /* normal, bold, wide, widebold */
96 #if GTK_CHECK_VERSION(2,0,0)
99 int xpos, ypos, gotpos, gravity;
101 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
102 GdkColor cols[NALLCOLOURS];
103 #if !GTK_CHECK_VERSION(3,0,0)
107 wchar_t *pastein_data;
108 int pastein_data_len;
109 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
110 GtkClipboard *clipboard;
111 struct clipboard_data_instance *current_cdi;
113 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
114 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
116 int font_width, font_height;
119 int mouseptr_visible;
125 int master_fd, master_func_id;
132 struct unicode_data ucsdata;
135 guint32 input_event_time; /* Timestamp of the most recent input event. */
137 #if GTK_CHECK_VERSION(3,4,0)
138 gdouble cumulative_scroll;
140 /* Cached things out of conf that we refer to a lot */
148 static void cache_conf_values(struct gui_data *inst)
150 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
151 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
152 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
153 #ifdef OSX_META_KEY_CONFIG
154 inst->meta_mod_mask = 0;
155 if (conf_get_int(inst->conf, CONF_osx_option_meta))
156 inst->meta_mod_mask |= GDK_MOD1_MASK;
157 if (conf_get_int(inst->conf, CONF_osx_command_meta))
158 inst->meta_mod_mask |= GDK_MOD2_MASK;
160 inst->meta_mod_mask = GDK_MOD1_MASK;
165 struct gui_data *inst;
166 unifont_drawctx uctx;
169 static int send_raw_mouse;
171 static void start_backend(struct gui_data *inst);
172 static void exit_callback(void *vinst);
174 void connection_fatal(void *frontend, const char *p, ...)
176 struct gui_data *inst = (struct gui_data *)frontend;
181 msg = dupvprintf(p, ap);
183 fatal_message_box(inst->window, msg);
186 queue_toplevel_callback(exit_callback, inst);
190 * Default settings that are specific to pterm.
192 FontSpec *platform_default_fontspec(const char *name)
194 if (!strcmp(name, "Font"))
195 return fontspec_new(DEFAULT_GTK_FONT);
197 return fontspec_new("");
200 Filename *platform_default_filename(const char *name)
202 if (!strcmp(name, "LogFileName"))
203 return filename_from_str("putty.log");
205 return filename_from_str("");
208 char *platform_default_s(const char *name)
210 if (!strcmp(name, "SerialLine"))
211 return dupstr("/dev/ttyS0");
215 int platform_default_i(const char *name, int def)
217 if (!strcmp(name, "CloseOnExit"))
218 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
219 if (!strcmp(name, "WinNameAlways"))
220 return 0; /* X natively supports icon titles, so use 'em by default */
224 /* Dummy routine, only required in plink. */
225 void frontend_echoedit_update(void *frontend, int echo, int edit)
229 char *get_ttymode(void *frontend, const char *mode)
231 struct gui_data *inst = (struct gui_data *)frontend;
232 return term_get_ttymode(inst->term, mode);
235 int from_backend(void *frontend, int is_stderr, const char *data, int len)
237 struct gui_data *inst = (struct gui_data *)frontend;
238 return term_data(inst->term, is_stderr, data, len);
241 int from_backend_untrusted(void *frontend, const char *data, int len)
243 struct gui_data *inst = (struct gui_data *)frontend;
244 return term_data_untrusted(inst->term, data, len);
247 int from_backend_eof(void *frontend)
249 return TRUE; /* do respond to incoming EOF with outgoing */
252 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
254 struct gui_data *inst = (struct gui_data *)p->frontend;
256 ret = cmdline_get_passwd_input(p, in, inlen);
258 ret = term_get_userpass_input(inst->term, p, in, inlen);
262 void logevent(void *frontend, const char *string)
264 struct gui_data *inst = (struct gui_data *)frontend;
266 log_eventlog(inst->logctx, string);
268 logevent_dlg(inst->eventlogstuff, string);
271 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
273 struct gui_data *inst = (struct gui_data *)frontend;
276 return inst->font_height;
278 return inst->font_width;
282 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
283 * into a cooked one (SELECT, EXTEND, PASTE).
285 * In Unix, this is not configurable; the X button arrangement is
286 * rock-solid across all applications, everyone has a three-button
287 * mouse or a means of faking it, and there is no need to switch
288 * buttons around at all.
290 static Mouse_Button translate_button(Mouse_Button button)
292 /* struct gui_data *inst = (struct gui_data *)frontend; */
294 if (button == MBT_LEFT)
296 if (button == MBT_MIDDLE)
298 if (button == MBT_RIGHT)
300 return 0; /* shouldn't happen */
304 * Return the top-level GtkWindow associated with a particular
305 * front end instance.
307 void *get_window(void *frontend)
309 struct gui_data *inst = (struct gui_data *)frontend;
314 * Minimise or restore the window in response to a server-side
317 void set_iconic(void *frontend, int iconic)
320 * GTK 1.2 doesn't know how to do this.
322 #if GTK_CHECK_VERSION(2,0,0)
323 struct gui_data *inst = (struct gui_data *)frontend;
325 gtk_window_iconify(GTK_WINDOW(inst->window));
327 gtk_window_deiconify(GTK_WINDOW(inst->window));
332 * Move the window in response to a server-side request.
334 void move_window(void *frontend, int x, int y)
336 struct gui_data *inst = (struct gui_data *)frontend;
338 * I assume that when the GTK version of this call is available
339 * we should use it. Not sure how it differs from the GDK one,
342 #if GTK_CHECK_VERSION(2,0,0)
343 gtk_window_move(GTK_WINDOW(inst->window), x, y);
345 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
350 * Move the window to the top or bottom of the z-order in response
351 * to a server-side request.
353 void set_zorder(void *frontend, int top)
355 struct gui_data *inst = (struct gui_data *)frontend;
357 gdk_window_raise(gtk_widget_get_window(inst->window));
359 gdk_window_lower(gtk_widget_get_window(inst->window));
363 * Refresh the window in response to a server-side request.
365 void refresh_window(void *frontend)
367 struct gui_data *inst = (struct gui_data *)frontend;
368 term_invalidate(inst->term);
372 * Maximise or restore the window in response to a server-side
375 void set_zoomed(void *frontend, int zoomed)
378 * GTK 1.2 doesn't know how to do this.
380 #if GTK_CHECK_VERSION(2,0,0)
381 struct gui_data *inst = (struct gui_data *)frontend;
383 gtk_window_maximize(GTK_WINDOW(inst->window));
385 gtk_window_unmaximize(GTK_WINDOW(inst->window));
390 * Report whether the window is iconic, for terminal reports.
392 int is_iconic(void *frontend)
394 struct gui_data *inst = (struct gui_data *)frontend;
395 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
399 * Report the window's position, for terminal reports.
401 void get_window_pos(void *frontend, int *x, int *y)
403 struct gui_data *inst = (struct gui_data *)frontend;
405 * I assume that when the GTK version of this call is available
406 * we should use it. Not sure how it differs from the GDK one,
409 #if GTK_CHECK_VERSION(2,0,0)
410 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
412 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
417 * Report the window's pixel size, for terminal reports.
419 void get_window_pixels(void *frontend, int *x, int *y)
421 struct gui_data *inst = (struct gui_data *)frontend;
423 * I assume that when the GTK version of this call is available
424 * we should use it. Not sure how it differs from the GDK one,
427 #if GTK_CHECK_VERSION(2,0,0)
428 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
430 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
435 * Return the window or icon title.
437 char *get_window_title(void *frontend, int icon)
439 struct gui_data *inst = (struct gui_data *)frontend;
440 return icon ? inst->icontitle : inst->wintitle;
443 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
445 struct gui_data *inst = (struct gui_data *)data;
446 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
447 if (!reallyclose(inst))
453 static void update_mouseptr(struct gui_data *inst)
455 switch (inst->busy_status) {
457 if (!inst->mouseptr_visible) {
458 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
460 } else if (send_raw_mouse) {
461 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
464 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
468 case BUSY_WAITING: /* XXX can we do better? */
470 /* We always display these cursors. */
471 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
479 static void show_mouseptr(struct gui_data *inst, int show)
481 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
483 inst->mouseptr_visible = show;
484 update_mouseptr(inst);
487 static void draw_backing_rect(struct gui_data *inst);
489 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
491 struct gui_data *inst = (struct gui_data *)data;
492 int w, h, need_size = 0;
495 * See if the terminal size has changed, in which case we must
496 * let the terminal know.
498 w = (event->width - 2*inst->window_border) / inst->font_width;
499 h = (event->height - 2*inst->window_border) / inst->font_height;
500 if (w != inst->width || h != inst->height) {
503 conf_set_int(inst->conf, CONF_width, inst->width);
504 conf_set_int(inst->conf, CONF_height, inst->height);
509 int backing_w = w * inst->font_width + 2*inst->window_border;
510 int backing_h = h * inst->font_height + 2*inst->window_border;
512 #ifndef NO_BACKING_PIXMAPS
514 gdk_pixmap_unref(inst->pixmap);
518 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
519 backing_w, backing_h, -1);
522 #ifdef DRAW_TEXT_CAIRO
524 cairo_surface_destroy(inst->surface);
525 inst->surface = NULL;
528 inst->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
529 backing_w, backing_h);
533 draw_backing_rect(inst);
535 if (need_size && inst->term) {
536 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
540 term_invalidate(inst->term);
542 #if GTK_CHECK_VERSION(2,0,0)
543 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
549 #ifdef DRAW_TEXT_CAIRO
550 static void cairo_setup_dctx(struct draw_ctx *dctx)
552 cairo_get_matrix(dctx->uctx.u.cairo.cr,
553 &dctx->uctx.u.cairo.origmatrix);
554 cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
555 cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
556 cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
557 /* This antialiasing setting appears to be ignored for Pango
558 * font rendering but honoured for stroking and filling paths;
559 * I don't quite understand the logic of that, but I won't
560 * complain since it's exactly what I happen to want */
561 cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
565 #if GTK_CHECK_VERSION(3,0,0)
566 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
568 struct gui_data *inst = (struct gui_data *)data;
571 * GTK3 window redraw: we always expect Cairo to be enabled, so
572 * that inst->surface exists, and pixmaps to be disabled, so that
573 * inst->pixmap does not exist. Hence, we just blit from
574 * inst->surface to the window.
577 GdkRectangle dirtyrect;
579 gdk_cairo_get_clip_rectangle(cr, &dirtyrect);
581 cairo_set_source_surface(cr, inst->surface, 0, 0);
582 cairo_rectangle(cr, dirtyrect.x, dirtyrect.y,
583 dirtyrect.width, dirtyrect.height);
590 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
592 struct gui_data *inst = (struct gui_data *)data;
594 #ifndef NO_BACKING_PIXMAPS
596 * Draw to the exposed part of the window from the server-side
600 gdk_draw_pixmap(gtk_widget_get_window(widget),
601 (gtk_widget_get_style(widget)->fg_gc
602 [gtk_widget_get_state(widget)]),
604 event->area.x, event->area.y,
605 event->area.x, event->area.y,
606 event->area.width, event->area.height);
610 * Failing that, draw from the client-side Cairo surface. (We
611 * should never be compiled in a context where we have _neither_
612 * inst->surface nor inst->pixmap.)
615 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
616 cairo_set_source_surface(cr, inst->surface, 0, 0);
617 cairo_rectangle(cr, event->area.x, event->area.y,
618 event->area.width, event->area.height);
628 #define KEY_PRESSED(k) \
629 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
631 #ifdef KEY_EVENT_DIAGNOSTICS
632 char *dup_keyval_name(guint keyval)
634 const char *name = gdk_keyval_name(keyval);
638 return dupprintf("UNKNOWN[%u]", (unsigned)keyval);
642 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
644 struct gui_data *inst = (struct gui_data *)data;
646 wchar_t ucsoutput[2];
647 int ucsval, start, end, special, output_charset, use_ucsoutput;
648 int nethack_mode, app_keypad_mode;
650 /* Remember the timestamp. */
651 inst->input_event_time = event->time;
653 /* By default, nothing is generated. */
655 special = use_ucsoutput = FALSE;
656 output_charset = CS_ISO8859_1;
658 #ifdef KEY_EVENT_DIAGNOSTICS
660 char *type_string, *state_string, *keyval_string, *string_string;
662 type_string = (event->type == GDK_KEY_PRESS ? dupstr("PRESS") :
663 event->type == GDK_KEY_RELEASE ? dupstr("RELEASE") :
664 dupprintf("UNKNOWN[%d]", (int)event->type));
667 static const struct {
671 {GDK_SHIFT_MASK, "SHIFT"},
672 {GDK_LOCK_MASK, "LOCK"},
673 {GDK_CONTROL_MASK, "CONTROL"},
674 {GDK_MOD1_MASK, "MOD1"},
675 {GDK_MOD2_MASK, "MOD2"},
676 {GDK_MOD3_MASK, "MOD3"},
677 {GDK_MOD4_MASK, "MOD4"},
678 {GDK_MOD5_MASK, "MOD5"},
679 {GDK_SUPER_MASK, "SUPER"},
680 {GDK_HYPER_MASK, "HYPER"},
681 {GDK_META_MASK, "META"},
684 int val = event->state;
686 state_string = dupstr("");
688 for (i = 0; i < lenof(mod_bits); i++) {
689 if (val & mod_bits[i].mod_bit) {
690 char *old = state_string;
691 state_string = dupcat(state_string,
692 state_string[0] ? "|" : "",
697 val &= ~mod_bits[i].mod_bit;
701 if (val || !state_string[0]) {
702 char *old = state_string;
703 state_string = dupprintf("%s%s%d", state_string,
704 state_string[0] ? "|" : "", val);
709 keyval_string = dup_keyval_name(event->keyval);
711 string_string = dupstr("");
714 for (i = 0; event->string[i]; i++) {
715 char *old = string_string;
716 string_string = dupprintf("%s%s%02x", string_string,
717 string_string[0] ? " " : "",
718 (unsigned)event->string[i] & 0xFF);
723 debug(("key_event: type=%s keyval=%s state=%s "
724 "hardware_keycode=%d is_modifier=%s string=[%s]\n",
725 type_string, keyval_string, state_string,
726 (int)event->hardware_keycode,
727 event->is_modifier ? "TRUE" : "FALSE",
732 sfree(keyval_string);
733 sfree(string_string);
735 #endif /* KEY_EVENT_DIAGNOSTICS */
738 * If Alt is being released after typing an Alt+numberpad
739 * sequence, we should generate the code that was typed.
741 * Note that we only do this if more than one key was actually
742 * pressed - I don't think Alt+NumPad4 should be ^D or that
743 * Alt+NumPad3 should be ^C, for example. There's no serious
744 * inconvenience in having to type a zero before a single-digit
747 if (event->type == GDK_KEY_RELEASE) {
748 if ((event->keyval == GDK_KEY_Meta_L ||
749 event->keyval == GDK_KEY_Meta_R ||
750 event->keyval == GDK_KEY_Alt_L ||
751 event->keyval == GDK_KEY_Alt_R) &&
752 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
753 #ifdef KEY_EVENT_DIAGNOSTICS
754 debug((" - modifier release terminates Alt+numberpad input, "
755 "keycode = %d\n", inst->alt_keycode));
758 * FIXME: we might usefully try to do something clever here
759 * about interpreting the generated key code in a way that's
760 * appropriate to the line code page.
762 output[0] = inst->alt_keycode;
766 #if GTK_CHECK_VERSION(2,0,0)
767 #ifdef KEY_EVENT_DIAGNOSTICS
768 debug((" - key release, passing to IM\n"));
770 if (gtk_im_context_filter_keypress(inst->imc, event)) {
771 #ifdef KEY_EVENT_DIAGNOSTICS
772 debug((" - key release accepted by IM\n"));
776 #ifdef KEY_EVENT_DIAGNOSTICS
777 debug((" - key release not accepted by IM\n"));
783 if (event->type == GDK_KEY_PRESS) {
785 * If Alt has just been pressed, we start potentially
786 * accumulating an Alt+numberpad code. We do this by
787 * setting alt_keycode to -1 (nothing yet but plausible).
789 if ((event->keyval == GDK_KEY_Meta_L ||
790 event->keyval == GDK_KEY_Meta_R ||
791 event->keyval == GDK_KEY_Alt_L ||
792 event->keyval == GDK_KEY_Alt_R)) {
793 #ifdef KEY_EVENT_DIAGNOSTICS
794 debug((" - modifier press potentially begins Alt+numberpad "
797 inst->alt_keycode = -1;
798 inst->alt_digits = 0;
799 goto done; /* this generates nothing else */
803 * If we're seeing a numberpad key press with Meta down,
804 * consider adding it to alt_keycode if that's sensible.
805 * Anything _else_ with Meta down cancels any possibility
806 * of an ALT keycode: we set alt_keycode to -2.
808 if ((event->state & inst->meta_mod_mask) && inst->alt_keycode != -2) {
810 switch (event->keyval) {
811 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
812 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
813 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
814 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
815 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
816 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
817 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
818 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
819 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
820 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
823 inst->alt_keycode = -2; /* it's invalid */
825 #ifdef KEY_EVENT_DIAGNOSTICS
826 int old_keycode = inst->alt_keycode;
828 if (inst->alt_keycode == -1)
829 inst->alt_keycode = digit; /* one-digit code */
831 inst->alt_keycode = inst->alt_keycode * 10 + digit;
833 #ifdef KEY_EVENT_DIAGNOSTICS
834 debug((" - Alt+numberpad digit %d added to keycode %d"
835 " gives %d\n", digit, old_keycode, inst->alt_keycode));
837 /* Having used this digit, we now do nothing more with it. */
843 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
846 if (event->keyval == GDK_KEY_Page_Up &&
847 (event->state & GDK_SHIFT_MASK)) {
848 #ifdef KEY_EVENT_DIAGNOSTICS
849 debug((" - Shift-PgUp scroll\n"));
851 term_scroll(inst->term, 0, -inst->height/2);
854 if (event->keyval == GDK_KEY_Page_Up &&
855 (event->state & GDK_CONTROL_MASK)) {
856 #ifdef KEY_EVENT_DIAGNOSTICS
857 debug((" - Ctrl-PgUp scroll\n"));
859 term_scroll(inst->term, 0, -1);
862 if (event->keyval == GDK_KEY_Page_Down &&
863 (event->state & GDK_SHIFT_MASK)) {
864 #ifdef KEY_EVENT_DIAGNOSTICS
865 debug((" - Shift-PgDn scroll\n"));
867 term_scroll(inst->term, 0, +inst->height/2);
870 if (event->keyval == GDK_KEY_Page_Down &&
871 (event->state & GDK_CONTROL_MASK)) {
872 #ifdef KEY_EVENT_DIAGNOSTICS
873 debug((" - Ctrl-PgDn scroll\n"));
875 term_scroll(inst->term, 0, +1);
880 * Neither does Shift-Ins.
882 if (event->keyval == GDK_KEY_Insert &&
883 (event->state & GDK_SHIFT_MASK)) {
884 #ifdef KEY_EVENT_DIAGNOSTICS
885 debug((" - Shift-Insert paste\n"));
892 use_ucsoutput = FALSE;
894 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
895 app_keypad_mode = (inst->term->app_keypad_keys &&
896 !conf_get_int(inst->conf, CONF_no_applic_k));
898 /* ALT+things gives leading Escape. */
900 #if !GTK_CHECK_VERSION(2,0,0)
902 * In vanilla X, and hence also GDK 1.2, the string received
903 * as part of a keyboard event is assumed to be in
904 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
905 * but it's true: see the man page for XLookupString(3) for
908 output_charset = CS_ISO8859_1;
909 strncpy(output+1, event->string, lenof(output)-1);
910 #else /* !GTK_CHECK_VERSION(2,0,0) */
912 * Most things can now be passed to
913 * gtk_im_context_filter_keypress without breaking anything
914 * below this point. An exception is the numeric keypad if
915 * we're in Nethack or application mode: the IM will eat
916 * numeric keypad presses if Num Lock is on, but we don't want
919 if (app_keypad_mode &&
920 (event->keyval == GDK_KEY_Num_Lock ||
921 event->keyval == GDK_KEY_KP_Divide ||
922 event->keyval == GDK_KEY_KP_Multiply ||
923 event->keyval == GDK_KEY_KP_Subtract ||
924 event->keyval == GDK_KEY_KP_Add ||
925 event->keyval == GDK_KEY_KP_Enter ||
926 event->keyval == GDK_KEY_KP_0 ||
927 event->keyval == GDK_KEY_KP_Insert ||
928 event->keyval == GDK_KEY_KP_1 ||
929 event->keyval == GDK_KEY_KP_End ||
930 event->keyval == GDK_KEY_KP_2 ||
931 event->keyval == GDK_KEY_KP_Down ||
932 event->keyval == GDK_KEY_KP_3 ||
933 event->keyval == GDK_KEY_KP_Page_Down ||
934 event->keyval == GDK_KEY_KP_4 ||
935 event->keyval == GDK_KEY_KP_Left ||
936 event->keyval == GDK_KEY_KP_5 ||
937 event->keyval == GDK_KEY_KP_Begin ||
938 event->keyval == GDK_KEY_KP_6 ||
939 event->keyval == GDK_KEY_KP_Right ||
940 event->keyval == GDK_KEY_KP_7 ||
941 event->keyval == GDK_KEY_KP_Home ||
942 event->keyval == GDK_KEY_KP_8 ||
943 event->keyval == GDK_KEY_KP_Up ||
944 event->keyval == GDK_KEY_KP_9 ||
945 event->keyval == GDK_KEY_KP_Page_Up ||
946 event->keyval == GDK_KEY_KP_Decimal ||
947 event->keyval == GDK_KEY_KP_Delete)) {
948 /* app keypad; do nothing */
949 } else if (nethack_mode &&
950 (event->keyval == GDK_KEY_KP_1 ||
951 event->keyval == GDK_KEY_KP_End ||
952 event->keyval == GDK_KEY_KP_2 ||
953 event->keyval == GDK_KEY_KP_Down ||
954 event->keyval == GDK_KEY_KP_3 ||
955 event->keyval == GDK_KEY_KP_Page_Down ||
956 event->keyval == GDK_KEY_KP_4 ||
957 event->keyval == GDK_KEY_KP_Left ||
958 event->keyval == GDK_KEY_KP_5 ||
959 event->keyval == GDK_KEY_KP_Begin ||
960 event->keyval == GDK_KEY_KP_6 ||
961 event->keyval == GDK_KEY_KP_Right ||
962 event->keyval == GDK_KEY_KP_7 ||
963 event->keyval == GDK_KEY_KP_Home ||
964 event->keyval == GDK_KEY_KP_8 ||
965 event->keyval == GDK_KEY_KP_Up ||
966 event->keyval == GDK_KEY_KP_9 ||
967 event->keyval == GDK_KEY_KP_Page_Up)) {
968 /* nethack mode; do nothing */
970 int try_filter = TRUE;
972 #ifdef META_MANUAL_MASK
973 if (event->state & META_MANUAL_MASK & inst->meta_mod_mask) {
975 * If this key event had a Meta modifier bit set which
976 * is also in META_MANUAL_MASK, that means passing
977 * such an event to the GtkIMContext will be unhelpful
978 * (it will eat the keystroke and turn it into
979 * something not what we wanted).
981 #ifdef KEY_EVENT_DIAGNOSTICS
982 debug((" - Meta modifier requiring manual intervention, "
983 "suppressing IM filtering\n"));
990 #ifdef KEY_EVENT_DIAGNOSTICS
991 debug((" - general key press, passing to IM\n"));
993 if (gtk_im_context_filter_keypress(inst->imc, event)) {
994 #ifdef KEY_EVENT_DIAGNOSTICS
995 debug((" - key press accepted by IM\n"));
999 #ifdef KEY_EVENT_DIAGNOSTICS
1000 debug((" - key press not accepted by IM\n"));
1007 * GDK 2.0 arranges to have done some translation for us: in
1008 * GDK 2.0, event->string is encoded in the current locale.
1010 * So we use the standard C library function mbstowcs() to
1011 * convert from the current locale into Unicode; from there
1012 * we can convert to whatever PuTTY is currently working in.
1013 * (In fact I convert straight back to UTF-8 from
1014 * wide-character Unicode, for the sake of simplicity: that
1015 * way we can still use exactly the same code to manipulate
1016 * the string, such as prefixing ESC.)
1018 output_charset = CS_UTF8;
1020 wchar_t widedata[32];
1025 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
1026 event->string, strlen(event->string),
1027 widedata, lenof(widedata)-1);
1029 #ifdef KEY_EVENT_DIAGNOSTICS
1031 char *string_string = dupstr("");
1034 for (i = 0; i < wlen; i++) {
1035 char *old = string_string;
1036 string_string = dupprintf("%s%s%04x", string_string,
1037 string_string[0] ? " " : "",
1038 (unsigned)widedata[i]);
1041 debug((" - string translated into Unicode = [%s]\n",
1043 sfree(string_string);
1048 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
1049 CS_UTF8, NULL, NULL, 0);
1051 #ifdef KEY_EVENT_DIAGNOSTICS
1053 char *string_string = dupstr("");
1056 for (i = 0; i < ulen; i++) {
1057 char *old = string_string;
1058 string_string = dupprintf("%s%s%02x", string_string,
1059 string_string[0] ? " " : "",
1060 (unsigned)output[i+1] & 0xFF);
1063 debug((" - string translated into UTF-8 = [%s]\n",
1065 sfree(string_string);
1069 output[1+ulen] = '\0';
1071 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1074 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
1075 ucsoutput[0] = '\033';
1076 ucsoutput[1] = ucsval;
1077 #ifdef KEY_EVENT_DIAGNOSTICS
1078 debug((" - keysym_to_unicode gave %04x\n",
1079 (unsigned)ucsoutput[1]));
1081 use_ucsoutput = TRUE;
1084 output[lenof(output)-1] = '\0';
1085 end = strlen(output);
1087 if (event->state & inst->meta_mod_mask) {
1089 if (end == 1) end = 0;
1091 #ifdef META_MANUAL_MASK
1092 if (event->state & META_MANUAL_MASK) {
1094 * Key events which have a META_MANUAL_MASK meta bit
1095 * set may have a keyval reflecting that, e.g. on OS X
1096 * the Option key acts as an AltGr-like modifier and
1097 * causes different Unicode characters to be output.
1099 * To work around this, we clear the dangerous
1100 * modifier bit and retranslate from the hardware
1101 * keycode as if the key had been pressed without that
1102 * modifier. Then we prefix Esc to *that*.
1105 GdkModifierType consumed;
1106 if (gdk_keymap_translate_keyboard_state
1107 (gdk_keymap_get_for_display(gdk_display_get_default()),
1108 event->hardware_keycode, event->state & ~META_MANUAL_MASK,
1109 0, &new_keyval, NULL, NULL, &consumed)) {
1110 ucsoutput[0] = '\033';
1111 ucsoutput[1] = gdk_keyval_to_unicode(new_keyval);
1112 #ifdef KEY_EVENT_DIAGNOSTICS
1114 char *keyval_name = dup_keyval_name(new_keyval);
1115 debug((" - retranslation for manual Meta: "
1116 "new keyval = %s, Unicode = %04x\n",
1117 keyval_name, (unsigned)ucsoutput[1]));
1121 use_ucsoutput = TRUE;
1129 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
1130 if (!output[1] && event->keyval == '`' &&
1131 (event->state & GDK_CONTROL_MASK)) {
1132 #ifdef KEY_EVENT_DIAGNOSTICS
1133 debug((" - Ctrl-` special case, translating as 1c\n"));
1136 use_ucsoutput = FALSE;
1140 /* Some GTK backends (e.g. Quartz) do not change event->string
1141 * in response to the Control modifier. So we do it ourselves
1142 * here, if it's not already happened.
1144 * The translations below are in line with X11 policy as far
1146 if ((event->state & GDK_CONTROL_MASK) && end == 2) {
1147 #ifdef KEY_EVENT_DIAGNOSTICS
1148 int orig = output[1];
1151 if (output[1] >= '3' && output[1] <= '7') {
1152 /* ^3,...,^7 map to 0x1B,...,0x1F */
1153 output[1] += '\x1B' - '3';
1154 } else if (output[1] == '2' || output[1] == ' ') {
1155 /* ^2 and ^Space are both ^@, i.e. \0 */
1157 } else if (output[1] == '8') {
1160 } else if (output[1] == '/') {
1161 /* ^/ is the same as ^_ */
1163 } else if (output[1] >= 0x40 && output[1] < 0x7F) {
1164 /* Everything anywhere near the alphabetics just gets
1168 /* Anything else, e.g. '0', is unchanged. */
1170 #ifdef KEY_EVENT_DIAGNOSTICS
1171 if (orig == output[1])
1172 debug((" - manual Ctrl key handling did nothing\n"));
1174 debug((" - manual Ctrl key handling: %02x -> %02x\n",
1175 (unsigned)orig, (unsigned)output[1]));
1179 /* Control-Break sends a Break special to the backend */
1180 if (event->keyval == GDK_KEY_Break &&
1181 (event->state & GDK_CONTROL_MASK)) {
1182 #ifdef KEY_EVENT_DIAGNOSTICS
1183 debug((" - Ctrl-Break special case, sending TS_BRK\n"));
1186 inst->back->special(inst->backhandle, TS_BRK);
1190 /* We handle Return ourselves, because it needs to be flagged as
1191 * special to ldisc. */
1192 if (event->keyval == GDK_KEY_Return) {
1193 #ifdef KEY_EVENT_DIAGNOSTICS
1194 debug((" - Return special case, translating as 0d + special\n"));
1197 use_ucsoutput = FALSE;
1202 /* Control-2, Control-Space and Control-@ are NUL */
1204 (event->keyval == ' ' || event->keyval == '2' ||
1205 event->keyval == '@') &&
1206 (event->state & (GDK_SHIFT_MASK |
1207 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
1208 #ifdef KEY_EVENT_DIAGNOSTICS
1209 debug((" - Ctrl-{space,2,@} special case, translating as 00\n"));
1212 use_ucsoutput = FALSE;
1216 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
1217 if (!output[1] && event->keyval == ' ' &&
1218 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
1219 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
1220 #ifdef KEY_EVENT_DIAGNOSTICS
1221 debug((" - Ctrl-Shift-space special case, translating as 00a0\n"));
1224 output_charset = CS_ISO8859_1;
1225 use_ucsoutput = FALSE;
1229 /* We don't let GTK tell us what Backspace is! We know better. */
1230 if (event->keyval == GDK_KEY_BackSpace &&
1231 !(event->state & GDK_SHIFT_MASK)) {
1232 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1234 #ifdef KEY_EVENT_DIAGNOSTICS
1235 debug((" - Backspace, translating as %02x\n",
1236 (unsigned)output[1]));
1238 use_ucsoutput = FALSE;
1242 /* For Shift Backspace, do opposite of what is configured. */
1243 if (event->keyval == GDK_KEY_BackSpace &&
1244 (event->state & GDK_SHIFT_MASK)) {
1245 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1247 #ifdef KEY_EVENT_DIAGNOSTICS
1248 debug((" - Shift-Backspace, translating as %02x\n",
1249 (unsigned)output[1]));
1251 use_ucsoutput = FALSE;
1256 /* Shift-Tab is ESC [ Z */
1257 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
1258 (event->keyval == GDK_KEY_Tab &&
1259 (event->state & GDK_SHIFT_MASK))) {
1260 #ifdef KEY_EVENT_DIAGNOSTICS
1261 debug((" - Shift-Tab, translating as ESC [ Z\n"));
1263 end = 1 + sprintf(output+1, "\033[Z");
1264 use_ucsoutput = FALSE;
1266 /* And normal Tab is Tab, if the keymap hasn't already told us.
1267 * (Curiously, at least one version of the MacOS 10.5 X server
1268 * doesn't translate Tab for us. */
1269 if (event->keyval == GDK_KEY_Tab && end <= 1) {
1270 #ifdef KEY_EVENT_DIAGNOSTICS
1271 debug((" - Tab, translating as 09\n"));
1278 * NetHack keypad mode.
1281 const char *keys = NULL;
1282 switch (event->keyval) {
1283 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
1284 keys = "bB\002"; break;
1285 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
1286 keys = "jJ\012"; break;
1287 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
1288 keys = "nN\016"; break;
1289 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
1290 keys = "hH\010"; break;
1291 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
1292 keys = "..."; break;
1293 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
1294 keys = "lL\014"; break;
1295 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
1296 keys = "yY\031"; break;
1297 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
1298 keys = "kK\013"; break;
1299 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
1300 keys = "uU\025"; break;
1304 if (event->state & GDK_CONTROL_MASK)
1305 output[1] = keys[2];
1306 else if (event->state & GDK_SHIFT_MASK)
1307 output[1] = keys[1];
1309 output[1] = keys[0];
1310 #ifdef KEY_EVENT_DIAGNOSTICS
1311 debug((" - Nethack-mode key"));
1313 use_ucsoutput = FALSE;
1319 * Application keypad mode.
1321 if (app_keypad_mode) {
1323 switch (event->keyval) {
1324 case GDK_KEY_Num_Lock: xkey = 'P'; break;
1325 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
1326 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
1327 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
1329 * Keypad + is tricky. It covers a space that would
1330 * be taken up on the VT100 by _two_ keys; so we
1331 * let Shift select between the two. Worse still,
1332 * in xterm function key mode we change which two...
1334 case GDK_KEY_KP_Add:
1335 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
1336 if (event->state & GDK_SHIFT_MASK)
1340 } else if (event->state & GDK_SHIFT_MASK)
1345 case GDK_KEY_KP_Enter: xkey = 'M'; break;
1346 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
1347 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
1348 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
1349 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
1350 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
1351 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
1352 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
1353 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
1354 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
1355 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
1356 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
1360 if (inst->term->vt52_mode) {
1361 if (xkey >= 'P' && xkey <= 'S')
1362 end = 1 + sprintf(output+1, "\033%c", xkey);
1364 end = 1 + sprintf(output+1, "\033?%c", xkey);
1366 end = 1 + sprintf(output+1, "\033O%c", xkey);
1367 use_ucsoutput = FALSE;
1368 #ifdef KEY_EVENT_DIAGNOSTICS
1369 debug((" - Application keypad mode key"));
1376 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
1377 * for integer decimal nn.)
1379 * We also deal with the weird ones here. Linux VCs replace F1
1380 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1381 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1386 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1387 switch (event->keyval) {
1389 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1392 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1395 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1398 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1401 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1404 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1407 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1410 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1413 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1416 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1449 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1450 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1453 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1456 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1459 case GDK_KEY_End: case GDK_KEY_KP_End:
1462 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1465 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1469 /* Reorder edit keys to physical order */
1470 if (funky_type == FUNKY_VT400 && code <= 6)
1471 code = "\0\2\1\4\5\3\6"[code];
1473 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1474 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1475 #ifdef KEY_EVENT_DIAGNOSTICS
1476 debug((" - VT52 mode small keypad key"));
1478 use_ucsoutput = FALSE;
1482 if (funky_type == FUNKY_SCO && /* SCO function keys */
1483 code >= 11 && code <= 34) {
1484 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1486 switch (event->keyval) {
1487 case GDK_KEY_F1: index = 0; break;
1488 case GDK_KEY_F2: index = 1; break;
1489 case GDK_KEY_F3: index = 2; break;
1490 case GDK_KEY_F4: index = 3; break;
1491 case GDK_KEY_F5: index = 4; break;
1492 case GDK_KEY_F6: index = 5; break;
1493 case GDK_KEY_F7: index = 6; break;
1494 case GDK_KEY_F8: index = 7; break;
1495 case GDK_KEY_F9: index = 8; break;
1496 case GDK_KEY_F10: index = 9; break;
1497 case GDK_KEY_F11: index = 10; break;
1498 case GDK_KEY_F12: index = 11; break;
1500 if (event->state & GDK_SHIFT_MASK) index += 12;
1501 if (event->state & GDK_CONTROL_MASK) index += 24;
1502 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1503 #ifdef KEY_EVENT_DIAGNOSTICS
1504 debug((" - SCO mode function key"));
1506 use_ucsoutput = FALSE;
1509 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1510 code >= 1 && code <= 6) {
1511 char codes[] = "HL.FIG";
1516 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1518 #ifdef KEY_EVENT_DIAGNOSTICS
1519 debug((" - SCO mode small keypad key"));
1521 use_ucsoutput = FALSE;
1524 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1525 code >= 11 && code <= 24) {
1531 if (inst->term->vt52_mode) {
1532 #ifdef KEY_EVENT_DIAGNOSTICS
1533 debug((" - VT52 mode function key"));
1535 end = 1 + sprintf(output+1,
1536 "\x1B%c", code + 'P' - 11 - offt);
1538 #ifdef KEY_EVENT_DIAGNOSTICS
1539 debug((" - VT100+ mode function key"));
1541 end = 1 + sprintf(output+1,
1542 "\x1BO%c", code + 'P' - 11 - offt);
1544 use_ucsoutput = FALSE;
1547 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1548 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1549 #ifdef KEY_EVENT_DIAGNOSTICS
1550 debug((" - Linux mode F1-F5 function key"));
1552 use_ucsoutput = FALSE;
1555 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1556 if (inst->term->vt52_mode) {
1557 #ifdef KEY_EVENT_DIAGNOSTICS
1558 debug((" - VT52 mode (overriding xterm) F1-F4 function"
1561 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1563 #ifdef KEY_EVENT_DIAGNOSTICS
1564 debug((" - xterm mode F1-F4 function key"));
1566 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1568 use_ucsoutput = FALSE;
1571 if ((code == 1 || code == 4) &&
1572 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1573 #ifdef KEY_EVENT_DIAGNOSTICS
1574 debug((" - rxvt style Home/End"));
1576 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1577 use_ucsoutput = FALSE;
1581 #ifdef KEY_EVENT_DIAGNOSTICS
1582 debug((" - ordinary function key encoding"));
1584 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1585 use_ucsoutput = FALSE;
1591 * Cursor keys. (This includes the numberpad cursor keys,
1592 * if we haven't already done them due to app keypad mode.)
1594 * Here we also process un-numlocked un-appkeypadded KP5,
1595 * which sends ESC [ G.
1599 switch (event->keyval) {
1600 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1601 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1602 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1603 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1604 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1607 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1608 event->state & GDK_CONTROL_MASK);
1609 #ifdef KEY_EVENT_DIAGNOSTICS
1610 debug((" - arrow key"));
1612 use_ucsoutput = FALSE;
1621 if (end-start > 0) {
1623 #ifdef KEY_EVENT_DIAGNOSTICS
1624 char *string_string = dupstr("");
1627 for (i = start; i < end; i++) {
1628 char *old = string_string;
1629 string_string = dupprintf("%s%s%02x", string_string,
1630 string_string[0] ? " " : "",
1631 (unsigned)output[i] & 0xFF);
1634 debug((" - final output, special, generic encoding = [%s]\n",
1635 charset_to_localenc(output_charset), string_string));
1636 sfree(string_string);
1639 * For special control characters, the character set
1640 * should never matter.
1642 output[end] = '\0'; /* NUL-terminate */
1644 ldisc_send(inst->ldisc, output+start, -2, 1);
1645 } else if (!inst->direct_to_font) {
1646 if (!use_ucsoutput) {
1647 #ifdef KEY_EVENT_DIAGNOSTICS
1648 char *string_string = dupstr("");
1651 for (i = start; i < end; i++) {
1652 char *old = string_string;
1653 string_string = dupprintf("%s%s%02x", string_string,
1654 string_string[0] ? " " : "",
1655 (unsigned)output[i] & 0xFF);
1658 debug((" - final output in %s = [%s]\n",
1659 charset_to_localenc(output_charset), string_string));
1660 sfree(string_string);
1663 lpage_send(inst->ldisc, output_charset, output+start,
1666 #ifdef KEY_EVENT_DIAGNOSTICS
1667 char *string_string = dupstr("");
1670 for (i = start; i < end; i++) {
1671 char *old = string_string;
1672 string_string = dupprintf("%s%s%04x", string_string,
1673 string_string[0] ? " " : "",
1674 (unsigned)ucsoutput[i]);
1677 debug((" - final output in Unicode = [%s]\n",
1679 sfree(string_string);
1683 * We generated our own Unicode key data from the
1684 * keysym, so use that instead.
1687 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1691 * In direct-to-font mode, we just send the string
1692 * exactly as we received it.
1694 #ifdef KEY_EVENT_DIAGNOSTICS
1695 char *string_string = dupstr("");
1698 for (i = start; i < end; i++) {
1699 char *old = string_string;
1700 string_string = dupprintf("%s%s%02x", string_string,
1701 string_string[0] ? " " : "",
1702 (unsigned)output[i] & 0xFF);
1705 debug((" - final output in direct-to-font encoding = [%s]\n",
1707 sfree(string_string);
1710 ldisc_send(inst->ldisc, output+start, end-start, 1);
1713 show_mouseptr(inst, 0);
1714 term_seen_key_event(inst->term);
1720 #if GTK_CHECK_VERSION(2,0,0)
1721 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1723 struct gui_data *inst = (struct gui_data *)data;
1725 #ifdef KEY_EVENT_DIAGNOSTICS
1726 char *string_string = dupstr("");
1729 for (i = 0; str[i]; i++) {
1730 char *old = string_string;
1731 string_string = dupprintf("%s%s%02x", string_string,
1732 string_string[0] ? " " : "",
1733 (unsigned)str[i] & 0xFF);
1736 debug((" - IM commit event in UTF-8 = [%s]\n", string_string));
1737 sfree(string_string);
1741 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1742 show_mouseptr(inst, 0);
1743 term_seen_key_event(inst->term);
1747 #define SCROLL_INCREMENT_LINES 5
1749 #if GTK_CHECK_VERSION(3,4,0)
1750 gboolean scroll_internal(struct gui_data *inst, gdouble delta, guint state,
1751 gdouble ex, gdouble ey)
1753 int shift, ctrl, alt, x, y, raw_mouse_mode;
1755 show_mouseptr(inst, 1);
1757 shift = state & GDK_SHIFT_MASK;
1758 ctrl = state & GDK_CONTROL_MASK;
1759 alt = state & inst->meta_mod_mask;
1761 x = (ex - inst->window_border) / inst->font_width;
1762 y = (ey - inst->window_border) / inst->font_height;
1765 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1766 CONF_mouse_override));
1768 inst->cumulative_scroll += delta * SCROLL_INCREMENT_LINES;
1770 if (!raw_mouse_mode) {
1771 int scroll_lines = (int)inst->cumulative_scroll; /* rounds toward 0 */
1773 term_scroll(inst->term, 0, scroll_lines);
1774 inst->cumulative_scroll -= scroll_lines;
1778 int scroll_events = (int)(inst->cumulative_scroll /
1779 SCROLL_INCREMENT_LINES);
1780 if (scroll_events) {
1783 inst->cumulative_scroll -= scroll_events * SCROLL_INCREMENT_LINES;
1785 if (scroll_events > 0) {
1786 button = MBT_WHEEL_DOWN;
1788 button = MBT_WHEEL_UP;
1789 scroll_events = -scroll_events;
1792 while (scroll_events-- > 0) {
1793 term_mouse(inst->term, button, translate_button(button),
1794 MA_CLICK, x, y, shift, ctrl, alt);
1802 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1803 GdkEventType type, guint ebutton, guint state,
1804 gdouble ex, gdouble ey)
1806 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1808 /* Remember the timestamp. */
1809 inst->input_event_time = timestamp;
1811 show_mouseptr(inst, 1);
1813 shift = state & GDK_SHIFT_MASK;
1814 ctrl = state & GDK_CONTROL_MASK;
1815 alt = state & inst->meta_mod_mask;
1818 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1819 CONF_mouse_override));
1821 if (!raw_mouse_mode) {
1822 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1823 term_scroll(inst->term, 0, -SCROLL_INCREMENT_LINES);
1826 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1827 term_scroll(inst->term, 0, +SCROLL_INCREMENT_LINES);
1832 if (ebutton == 3 && ctrl) {
1833 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1834 ebutton, timestamp);
1840 else if (ebutton == 2)
1841 button = MBT_MIDDLE;
1842 else if (ebutton == 3)
1844 else if (ebutton == 4)
1845 button = MBT_WHEEL_UP;
1846 else if (ebutton == 5)
1847 button = MBT_WHEEL_DOWN;
1849 return FALSE; /* don't even know what button! */
1852 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1853 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1854 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1855 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1856 default: return FALSE; /* don't know this event type */
1859 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1860 return TRUE; /* we ignore these in raw mouse mode */
1862 x = (ex - inst->window_border) / inst->font_width;
1863 y = (ey - inst->window_border) / inst->font_height;
1865 term_mouse(inst->term, button, translate_button(button), act,
1866 x, y, shift, ctrl, alt);
1871 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1873 struct gui_data *inst = (struct gui_data *)data;
1874 return button_internal(inst, event->time, event->type, event->button,
1875 event->state, event->x, event->y);
1878 #if GTK_CHECK_VERSION(2,0,0)
1880 * In GTK 2, mouse wheel events have become a new type of event.
1881 * This handler translates them back into button-4 and button-5
1882 * presses so that I don't have to change my old code too much :-)
1884 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1886 struct gui_data *inst = (struct gui_data *)data;
1888 #if GTK_CHECK_VERSION(3,4,0)
1890 if (gdk_event_get_scroll_deltas((GdkEvent *)event, &dx, &dy)) {
1891 return scroll_internal(inst, dy, event->state, event->x, event->y);
1896 if (event->direction == GDK_SCROLL_UP)
1898 else if (event->direction == GDK_SCROLL_DOWN)
1903 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1904 button, event->state, event->x, event->y);
1909 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1911 struct gui_data *inst = (struct gui_data *)data;
1912 int shift, ctrl, alt, x, y, button;
1914 /* Remember the timestamp. */
1915 inst->input_event_time = event->time;
1917 show_mouseptr(inst, 1);
1919 shift = event->state & GDK_SHIFT_MASK;
1920 ctrl = event->state & GDK_CONTROL_MASK;
1921 alt = event->state & inst->meta_mod_mask;
1922 if (event->state & GDK_BUTTON1_MASK)
1924 else if (event->state & GDK_BUTTON2_MASK)
1925 button = MBT_MIDDLE;
1926 else if (event->state & GDK_BUTTON3_MASK)
1929 return FALSE; /* don't even know what button! */
1931 x = (event->x - inst->window_border) / inst->font_width;
1932 y = (event->y - inst->window_border) / inst->font_height;
1934 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1935 x, y, shift, ctrl, alt);
1940 void frontend_keypress(void *handle)
1942 struct gui_data *inst = (struct gui_data *)handle;
1945 * If our child process has exited but not closed, terminate on
1952 static void exit_callback(void *vinst)
1954 struct gui_data *inst = (struct gui_data *)vinst;
1955 int exitcode, close_on_exit;
1957 if (!inst->exited &&
1958 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1959 inst->exited = TRUE;
1960 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1961 if (close_on_exit == FORCE_ON ||
1962 (close_on_exit == AUTO && exitcode == 0))
1963 gtk_main_quit(); /* just go */
1965 ldisc_free(inst->ldisc);
1968 inst->back->free(inst->backhandle);
1969 inst->backhandle = NULL;
1971 term_provide_resize_fn(inst->term, NULL, NULL);
1972 update_specials_menu(inst);
1973 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1977 void notify_remote_exit(void *frontend)
1979 struct gui_data *inst = (struct gui_data *)frontend;
1981 queue_toplevel_callback(exit_callback, inst);
1984 void destroy(GtkWidget *widget, gpointer data)
1989 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1991 struct gui_data *inst = (struct gui_data *)data;
1992 term_set_focus(inst->term, event->in);
1993 term_update(inst->term);
1994 show_mouseptr(inst, 1);
1998 void set_busy_status(void *frontend, int status)
2000 struct gui_data *inst = (struct gui_data *)frontend;
2001 inst->busy_status = status;
2002 update_mouseptr(inst);
2006 * set or clear the "raw mouse message" mode
2008 void set_raw_mouse_mode(void *frontend, int activate)
2010 struct gui_data *inst = (struct gui_data *)frontend;
2011 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
2012 send_raw_mouse = activate;
2013 update_mouseptr(inst);
2016 void request_resize(void *frontend, int w, int h)
2018 struct gui_data *inst = (struct gui_data *)frontend;
2020 #if !GTK_CHECK_VERSION(3,0,0)
2022 int large_x, large_y;
2023 int offset_x, offset_y;
2025 GtkRequisition inner, outer;
2028 * This is a heinous hack dreamed up by the gnome-terminal
2029 * people to get around a limitation in gtk. The problem is
2030 * that in order to set the size correctly we really need to be
2031 * calling gtk_window_resize - but that needs to know the size
2032 * of the _whole window_, not the drawing area. So what we do
2033 * is to set an artificially huge size request on the drawing
2034 * area, recompute the resulting size request on the window,
2035 * and look at the difference between the two. That gives us
2036 * the x and y offsets we need to translate drawing area size
2037 * into window size for real, and then we call
2038 * gtk_window_resize.
2042 * We start by retrieving the current size of the whole window.
2043 * Adding a bit to _that_ will give us a value we can use as a
2044 * bogus size request which guarantees to be bigger than the
2045 * current size of the drawing area.
2047 get_window_pixels(inst, &large_x, &large_y);
2051 gtk_widget_set_size_request(inst->area, large_x, large_y);
2052 gtk_widget_size_request(inst->area, &inner);
2053 gtk_widget_size_request(inst->window, &outer);
2055 offset_x = outer.width - inner.width;
2056 offset_y = outer.height - inner.height;
2058 area_x = inst->font_width * w + 2*inst->window_border;
2059 area_y = inst->font_height * h + 2*inst->window_border;
2062 * Now we must set the size request on the drawing area back to
2063 * something sensible before we commit the real resize. Best
2064 * way to do this, I think, is to set it to what the size is
2065 * really going to end up being.
2067 gtk_widget_set_size_request(inst->area, area_x, area_y);
2068 #if GTK_CHECK_VERSION(2,0,0)
2069 gtk_window_resize(GTK_WINDOW(inst->window),
2070 area_x + offset_x, area_y + offset_y);
2072 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
2074 * I can no longer remember what this call to
2075 * gtk_container_dequeue_resize_handler is for. It was
2076 * introduced in r3092 with no comment, and the commit log
2077 * message was uninformative. I'm _guessing_ its purpose is to
2078 * prevent gratuitous resize processing on the window given
2079 * that we're about to resize it anyway, but I have no idea
2080 * why that's so incredibly vital.
2082 * I've tried removing the call, and nothing seems to go
2083 * wrong. I've backtracked to r3092 and tried removing the
2084 * call there, and still nothing goes wrong. So I'm going to
2085 * adopt the working hypothesis that it's superfluous; I won't
2086 * actually remove it from the GTK 1.2 code, but I won't
2087 * attempt to replicate its functionality in the GTK 2 code
2090 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
2091 gdk_window_resize(gtk_widget_get_window(inst->window),
2092 area_x + offset_x, area_y + offset_y);
2095 #else /* GTK_CHECK_VERSION(3,0,0) */
2098 * In GTK3, we can do this by using gtk_window_resize_to_geometry,
2099 * which uses the fact that we've already set up the main window's
2100 * WM hints to reflect the terminal drawing area's resize
2101 * increment (i.e. character cell) and the fixed amount of stuff
2104 gtk_window_resize_to_geometry(GTK_WINDOW(inst->window), w, h);
2110 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
2112 inst->cols[n].red = r * 0x0101;
2113 inst->cols[n].green = g * 0x0101;
2114 inst->cols[n].blue = b * 0x0101;
2116 #if !GTK_CHECK_VERSION(3,0,0)
2118 gboolean success[1];
2119 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
2120 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
2121 FALSE, TRUE, success);
2123 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2124 appname, n, r, g, b);
2129 void set_gdk_window_background(GdkWindow *win, const GdkColor *col)
2131 #if GTK_CHECK_VERSION(3,0,0)
2132 /* gdk_window_set_background is deprecated; work around its
2135 rgba.red = col->red / 65535.0;
2136 rgba.green = col->green / 65535.0;
2137 rgba.blue = col->blue / 65535.0;
2139 gdk_window_set_background_rgba(win, &rgba);
2142 /* For GTK1, which doesn't have a 'const' on
2143 * gdk_window_set_background's second parameter type. */
2144 GdkColor col_mutable = *col;
2145 gdk_window_set_background(win, &col_mutable);
2150 void set_window_background(struct gui_data *inst)
2152 if (inst->area && gtk_widget_get_window(inst->area))
2153 set_gdk_window_background(gtk_widget_get_window(inst->area),
2155 if (inst->window && gtk_widget_get_window(inst->window))
2156 set_gdk_window_background(gtk_widget_get_window(inst->window),
2160 void palette_set(void *frontend, int n, int r, int g, int b)
2162 struct gui_data *inst = (struct gui_data *)frontend;
2165 if (n >= NALLCOLOURS)
2167 real_palette_set(inst, n, r, g, b);
2169 /* Default Background changed. Ensure space between text area and
2170 * window border is redrawn */
2171 set_window_background(inst);
2172 draw_backing_rect(inst);
2173 gtk_widget_queue_draw(inst->area);
2177 void palette_reset(void *frontend)
2179 struct gui_data *inst = (struct gui_data *)frontend;
2180 /* This maps colour indices in inst->conf to those used in inst->cols. */
2181 static const int ww[] = {
2182 256, 257, 258, 259, 260, 261,
2183 0, 8, 1, 9, 2, 10, 3, 11,
2184 4, 12, 5, 13, 6, 14, 7, 15
2188 assert(lenof(ww) == NCFGCOLOURS);
2190 #if !GTK_CHECK_VERSION(3,0,0)
2191 if (!inst->colmap) {
2192 inst->colmap = gdk_colormap_get_system();
2194 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
2198 for (i = 0; i < NCFGCOLOURS; i++) {
2199 inst->cols[ww[i]].red =
2200 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
2201 inst->cols[ww[i]].green =
2202 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
2203 inst->cols[ww[i]].blue =
2204 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
2207 for (i = 0; i < NEXTCOLOURS; i++) {
2209 int r = i / 36, g = (i / 6) % 6, b = i % 6;
2210 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
2211 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
2212 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
2214 int shade = i - 216;
2215 shade = shade * 0x0a0a + 0x0808;
2216 inst->cols[i+16].red = inst->cols[i+16].green =
2217 inst->cols[i+16].blue = shade;
2221 #if !GTK_CHECK_VERSION(3,0,0)
2223 gboolean success[NALLCOLOURS];
2224 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
2225 FALSE, TRUE, success);
2226 for (i = 0; i < NALLCOLOURS; i++) {
2228 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2230 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
2231 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
2232 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
2237 /* Since Default Background may have changed, ensure that space
2238 * between text area and window border is refreshed. */
2239 set_window_background(inst);
2240 if (inst->area && gtk_widget_get_window(inst->area)) {
2241 draw_backing_rect(inst);
2242 gtk_widget_queue_draw(inst->area);
2246 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
2248 /* ----------------------------------------------------------------------
2249 * Clipboard handling, using the high-level GtkClipboard interface in
2250 * as hands-off a way as possible. We write and read the clipboard as
2251 * UTF-8 text, and let GTK deal with converting to any other text
2252 * formats it feels like.
2255 void init_clipboard(struct gui_data *inst)
2257 inst->clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(),
2262 * Because calling gtk_clipboard_set_with_data triggers a call to the
2263 * clipboard_clear function from the last time, we need to arrange a
2264 * way to distinguish a real call to clipboard_clear for the _new_
2265 * instance of the clipboard data from the leftover call for the
2266 * outgoing one. We do this by setting the user data field in our
2267 * gtk_clipboard_set_with_data() call, instead of the obvious pointer
2268 * to 'inst', to one of these.
2270 struct clipboard_data_instance {
2271 struct gui_data *inst;
2272 char *pasteout_data_utf8;
2273 int pasteout_data_utf8_len;
2276 static void clipboard_provide_data(GtkClipboard *clipboard,
2277 GtkSelectionData *selection_data,
2278 guint info, gpointer data)
2280 struct clipboard_data_instance *cdi =
2281 (struct clipboard_data_instance *)data;
2282 struct gui_data *inst = cdi->inst;
2284 if (inst->current_cdi == cdi) {
2285 gtk_selection_data_set_text(selection_data, cdi->pasteout_data_utf8,
2286 cdi->pasteout_data_utf8_len);
2290 static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
2292 struct clipboard_data_instance *cdi =
2293 (struct clipboard_data_instance *)data;
2294 struct gui_data *inst = cdi->inst;
2296 if (inst->current_cdi == cdi) {
2297 term_deselect(inst->term);
2298 inst->current_cdi = NULL;
2300 sfree(cdi->pasteout_data_utf8);
2304 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2307 struct gui_data *inst = (struct gui_data *)frontend;
2308 struct clipboard_data_instance *cdi;
2310 if (inst->direct_to_font) {
2311 /* In this clipboard mode, we just can't paste if we're in
2312 * direct-to-font mode. Fortunately, that shouldn't be
2313 * important, because we'll only use this clipboard handling
2314 * code on systems where that kind of font doesn't exist
2319 cdi = snew(struct clipboard_data_instance);
2321 inst->current_cdi = cdi;
2322 cdi->pasteout_data_utf8 = snewn(len*6, char);
2324 const wchar_t *tmp = data;
2326 cdi->pasteout_data_utf8_len =
2327 charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
2328 len*6, CS_UTF8, NULL, NULL, 0);
2332 * It would be nice to just call gtk_clipboard_set_text() in place
2333 * of all of the faffing below. Unfortunately, that won't give me
2334 * access to the clipboard-clear event, which we use to visually
2335 * deselect text in the terminal.
2338 GtkTargetList *targetlist;
2339 GtkTargetEntry *targettable;
2342 targetlist = gtk_target_list_new(NULL, 0);
2343 gtk_target_list_add_text_targets(targetlist, 0);
2344 targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
2345 gtk_clipboard_set_with_data(inst->clipboard, targettable, n_targets,
2346 clipboard_provide_data, clipboard_clear,
2348 gtk_target_table_free(targettable, n_targets);
2349 gtk_target_list_unref(targetlist);
2353 static void clipboard_text_received(GtkClipboard *clipboard,
2354 const gchar *text, gpointer data)
2356 struct gui_data *inst = (struct gui_data *)data;
2362 length = strlen(text);
2364 if (inst->pastein_data)
2365 sfree(inst->pastein_data);
2367 inst->pastein_data = snewn(length, wchar_t);
2368 inst->pastein_data_len = mb_to_wc(CS_UTF8, 0, text, length,
2369 inst->pastein_data, length);
2371 term_do_paste(inst->term);
2374 void request_paste(void *frontend)
2376 struct gui_data *inst = (struct gui_data *)frontend;
2377 gtk_clipboard_request_text(inst->clipboard, clipboard_text_received, inst);
2380 #else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
2382 /* ----------------------------------------------------------------------
2383 * Clipboard handling for X, using the low-level gtk_selection_*
2384 * interface, handling conversions to fiddly things like compound text
2385 * ourselves, and storing in X cut buffers too.
2387 * This version of the clipboard code has to be kept around for GTK1,
2388 * which doesn't have the higher-level GtkClipboard interface at all.
2389 * And since it works on GTK2 and GTK3 too and has had a good few
2390 * years of shakedown and bug fixing, we might as well keep using it
2391 * where it's applicable.
2393 * It's _possible_ that we might be able to replicate all the
2394 * important wrinkles of this code in GtkClipboard. (In particular,
2395 * cut buffers or local analogue look as if they might be accessible
2396 * via gtk_clipboard_set_can_store(), and delivering text in
2397 * non-Unicode formats only in the direct-to-font case ought to be
2398 * possible if we can figure out the right set of things to put in the
2399 * GtkTargetList.) But that work can wait until there's a need for it!
2402 /* Store the data in a cut-buffer. */
2403 static void store_cutbuffer(char * ptr, int len)
2405 #ifndef NOT_X_WINDOWS
2406 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2407 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
2408 XRotateBuffers(disp, 1);
2409 XStoreBytes(disp, ptr, len);
2413 /* Retrieve data from a cut-buffer.
2414 * Returned data needs to be freed with XFree().
2416 static char *retrieve_cutbuffer(int *nbytes)
2418 #ifndef NOT_X_WINDOWS
2419 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2421 ptr = XFetchBytes(disp, nbytes);
2422 if (*nbytes <= 0 && ptr != 0) {
2432 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2435 struct gui_data *inst = (struct gui_data *)frontend;
2436 if (inst->pasteout_data)
2437 sfree(inst->pasteout_data);
2438 if (inst->pasteout_data_ctext)
2439 sfree(inst->pasteout_data_ctext);
2440 if (inst->pasteout_data_utf8)
2441 sfree(inst->pasteout_data_utf8);
2444 * Set up UTF-8 and compound text paste data. This only happens
2445 * if we aren't in direct-to-font mode using the D800 hack.
2447 if (!inst->direct_to_font) {
2448 const wchar_t *tmp = data;
2450 #ifndef NOT_X_WINDOWS
2453 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2456 inst->pasteout_data_utf8 = snewn(len*6, char);
2457 inst->pasteout_data_utf8_len = len*6;
2458 inst->pasteout_data_utf8_len =
2459 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
2460 inst->pasteout_data_utf8_len,
2461 CS_UTF8, NULL, NULL, 0);
2462 if (inst->pasteout_data_utf8_len == 0) {
2463 sfree(inst->pasteout_data_utf8);
2464 inst->pasteout_data_utf8 = NULL;
2466 inst->pasteout_data_utf8 =
2467 sresize(inst->pasteout_data_utf8,
2468 inst->pasteout_data_utf8_len + 1, char);
2469 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
2473 * Now let Xlib convert our UTF-8 data into compound text.
2475 #ifndef NOT_X_WINDOWS
2476 list[0] = inst->pasteout_data_utf8;
2477 if (Xutf8TextListToTextProperty(disp, list, 1,
2478 XCompoundTextStyle, &tp) == 0) {
2479 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
2480 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
2481 inst->pasteout_data_ctext_len = tp.nitems;
2486 inst->pasteout_data_ctext = NULL;
2487 inst->pasteout_data_ctext_len = 0;
2490 inst->pasteout_data_utf8 = NULL;
2491 inst->pasteout_data_utf8_len = 0;
2492 inst->pasteout_data_ctext = NULL;
2493 inst->pasteout_data_ctext_len = 0;
2496 inst->pasteout_data = snewn(len*6, char);
2497 inst->pasteout_data_len = len*6;
2498 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
2499 data, len, inst->pasteout_data,
2500 inst->pasteout_data_len,
2502 if (inst->pasteout_data_len == 0) {
2503 sfree(inst->pasteout_data);
2504 inst->pasteout_data = NULL;
2506 inst->pasteout_data =
2507 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
2510 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
2512 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
2513 inst->input_event_time)) {
2514 #if GTK_CHECK_VERSION(2,0,0)
2515 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
2517 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2518 GDK_SELECTION_TYPE_STRING, 1);
2519 if (inst->pasteout_data_ctext)
2520 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2521 compound_text_atom, 1);
2522 if (inst->pasteout_data_utf8)
2523 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2524 utf8_string_atom, 1);
2528 term_deselect(inst->term);
2531 static void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2532 guint info, guint time_stamp, gpointer data)
2534 struct gui_data *inst = (struct gui_data *)data;
2535 GdkAtom target = gtk_selection_data_get_target(seldata);
2536 if (target == utf8_string_atom)
2537 gtk_selection_data_set(seldata, target, 8,
2538 (unsigned char *)inst->pasteout_data_utf8,
2539 inst->pasteout_data_utf8_len);
2540 else if (target == compound_text_atom)
2541 gtk_selection_data_set(seldata, target, 8,
2542 (unsigned char *)inst->pasteout_data_ctext,
2543 inst->pasteout_data_ctext_len);
2545 gtk_selection_data_set(seldata, target, 8,
2546 (unsigned char *)inst->pasteout_data,
2547 inst->pasteout_data_len);
2550 static gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2553 struct gui_data *inst = (struct gui_data *)data;
2555 term_deselect(inst->term);
2556 if (inst->pasteout_data)
2557 sfree(inst->pasteout_data);
2558 if (inst->pasteout_data_ctext)
2559 sfree(inst->pasteout_data_ctext);
2560 if (inst->pasteout_data_utf8)
2561 sfree(inst->pasteout_data_utf8);
2562 inst->pasteout_data = NULL;
2563 inst->pasteout_data_len = 0;
2564 inst->pasteout_data_ctext = NULL;
2565 inst->pasteout_data_ctext_len = 0;
2566 inst->pasteout_data_utf8 = NULL;
2567 inst->pasteout_data_utf8_len = 0;
2571 void request_paste(void *frontend)
2573 struct gui_data *inst = (struct gui_data *)frontend;
2575 * In Unix, pasting is asynchronous: all we can do at the
2576 * moment is to call gtk_selection_convert(), and when the data
2577 * comes back _then_ we can call term_do_paste().
2580 if (!inst->direct_to_font) {
2582 * First we attempt to retrieve the selection as a UTF-8
2583 * string (which we will convert to the correct code page
2584 * before sending to the session, of course). If that
2585 * fails, selection_received() will be informed and will
2586 * fall back to an ordinary string.
2588 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2590 inst->input_event_time);
2593 * If we're in direct-to-font mode, we disable UTF-8
2594 * pasting, and go straight to ordinary string data.
2596 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2597 GDK_SELECTION_TYPE_STRING,
2598 inst->input_event_time);
2602 static void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2603 guint time, gpointer data)
2605 struct gui_data *inst = (struct gui_data *)data;
2608 #ifndef NOT_X_WINDOWS
2610 int free_list_required = 0;
2611 int free_required = 0;
2614 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2615 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2616 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2617 gint seldata_length = gtk_selection_data_get_length(seldata);
2619 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2621 * Failed to get a UTF-8 selection string. Try compound
2624 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2626 inst->input_event_time);
2630 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2632 * Failed to get UTF-8 or compound text. Try an ordinary
2635 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2636 GDK_SELECTION_TYPE_STRING,
2637 inst->input_event_time);
2642 * If we have data, but it's not of a type we can deal with,
2643 * we have to ignore the data.
2645 if (seldata_length > 0 &&
2646 seldata_type != GDK_SELECTION_TYPE_STRING &&
2647 seldata_type != compound_text_atom &&
2648 seldata_type != utf8_string_atom)
2652 * If we have no data, try looking in a cut buffer.
2654 if (seldata_length <= 0) {
2655 #ifndef NOT_X_WINDOWS
2656 text = retrieve_cutbuffer(&length);
2659 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2660 * source, so use that as a de-facto standard. */
2661 charset = CS_ISO8859_1;
2668 * Convert COMPOUND_TEXT into UTF-8.
2670 if (seldata_type == compound_text_atom) {
2671 #ifndef NOT_X_WINDOWS
2674 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2676 tp.value = (unsigned char *)seldata_data;
2677 tp.encoding = (Atom) seldata_type;
2678 tp.format = gtk_selection_data_get_format(seldata);
2679 tp.nitems = seldata_length;
2680 ret = Xutf8TextPropertyToTextList(disp, &tp, &list, &count);
2681 if (ret == 0 && count == 1) {
2683 length = strlen(list[0]);
2685 free_list_required = 1;
2690 * Compound text failed; fall back to STRING.
2692 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2693 GDK_SELECTION_TYPE_STRING,
2694 inst->input_event_time);
2698 text = (char *)seldata_data;
2699 length = seldata_length;
2700 charset = (seldata_type == utf8_string_atom ?
2701 CS_UTF8 : inst->ucsdata.line_codepage);
2705 if (inst->pastein_data)
2706 sfree(inst->pastein_data);
2708 inst->pastein_data = snewn(length, wchar_t);
2709 inst->pastein_data_len = length;
2710 inst->pastein_data_len =
2711 mb_to_wc(charset, 0, text, length,
2712 inst->pastein_data, inst->pastein_data_len);
2714 term_do_paste(inst->term);
2716 #ifndef NOT_X_WINDOWS
2717 if (free_list_required)
2718 XFreeStringList(list);
2724 void init_clipboard(struct gui_data *inst)
2726 #ifndef NOT_X_WINDOWS
2728 * Ensure that all the cut buffers exist - according to the ICCCM,
2729 * we must do this before we start using cut buffers.
2731 unsigned char empty[] = "";
2732 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2733 x11_ignore_error(disp, BadMatch);
2734 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2735 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
2736 x11_ignore_error(disp, BadMatch);
2737 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2738 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
2739 x11_ignore_error(disp, BadMatch);
2740 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2741 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
2742 x11_ignore_error(disp, BadMatch);
2743 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2744 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
2745 x11_ignore_error(disp, BadMatch);
2746 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2747 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
2748 x11_ignore_error(disp, BadMatch);
2749 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2750 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
2751 x11_ignore_error(disp, BadMatch);
2752 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2753 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
2754 x11_ignore_error(disp, BadMatch);
2755 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2756 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
2759 g_signal_connect(G_OBJECT(inst->area), "selection_received",
2760 G_CALLBACK(selection_received), inst);
2761 g_signal_connect(G_OBJECT(inst->area), "selection_get",
2762 G_CALLBACK(selection_get), inst);
2763 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
2764 G_CALLBACK(selection_clear), inst);
2768 * End of selection/clipboard handling.
2769 * ----------------------------------------------------------------------
2772 #endif /* JUST_USE_GTK_CLIPBOARD_UTF8 */
2774 void get_clip(void *frontend, wchar_t ** p, int *len)
2776 struct gui_data *inst = (struct gui_data *)frontend;
2779 *p = inst->pastein_data;
2780 *len = inst->pastein_data_len;
2784 static void set_window_titles(struct gui_data *inst)
2787 * We must always call set_icon_name after calling set_title,
2788 * since set_title will write both names. Irritating, but such
2791 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2792 if (!conf_get_int(inst->conf, CONF_win_name_always))
2793 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
2797 void set_title(void *frontend, char *title)
2799 struct gui_data *inst = (struct gui_data *)frontend;
2800 sfree(inst->wintitle);
2801 inst->wintitle = dupstr(title);
2802 set_window_titles(inst);
2805 void set_icon(void *frontend, char *title)
2807 struct gui_data *inst = (struct gui_data *)frontend;
2808 sfree(inst->icontitle);
2809 inst->icontitle = dupstr(title);
2810 set_window_titles(inst);
2813 void set_title_and_icon(void *frontend, char *title, char *icon)
2815 struct gui_data *inst = (struct gui_data *)frontend;
2816 sfree(inst->wintitle);
2817 inst->wintitle = dupstr(title);
2818 sfree(inst->icontitle);
2819 inst->icontitle = dupstr(icon);
2820 set_window_titles(inst);
2823 void set_sbar(void *frontend, int total, int start, int page)
2825 struct gui_data *inst = (struct gui_data *)frontend;
2826 if (!conf_get_int(inst->conf, CONF_scrollbar))
2828 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
2829 gtk_adjustment_set_upper(inst->sbar_adjust, total);
2830 gtk_adjustment_set_value(inst->sbar_adjust, start);
2831 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
2832 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
2833 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
2834 inst->ignore_sbar = TRUE;
2835 gtk_adjustment_changed(inst->sbar_adjust);
2836 inst->ignore_sbar = FALSE;
2839 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2841 struct gui_data *inst = (struct gui_data *)data;
2843 if (!conf_get_int(inst->conf, CONF_scrollbar))
2845 if (!inst->ignore_sbar)
2846 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
2849 void sys_cursor(void *frontend, int x, int y)
2852 * This is meaningless under X.
2857 * This is still called when mode==BELL_VISUAL, even though the
2858 * visual bell is handled entirely within terminal.c, because we
2859 * may want to perform additional actions on any kind of bell (for
2860 * example, taskbar flashing in Windows).
2862 void do_beep(void *frontend, int mode)
2864 if (mode == BELL_DEFAULT)
2868 int char_width(Context ctx, int uc)
2871 * In this front end, double-width characters are handled using a
2872 * separate font, so this can safely just return 1 always.
2877 Context get_ctx(void *frontend)
2879 struct gui_data *inst = (struct gui_data *)frontend;
2880 struct draw_ctx *dctx;
2882 if (!gtk_widget_get_window(inst->area))
2885 dctx = snew(struct draw_ctx);
2887 dctx->uctx.type = inst->drawtype;
2888 #ifdef DRAW_TEXT_GDK
2889 if (dctx->uctx.type == DRAWTYPE_GDK) {
2890 /* If we're doing GDK-based drawing, then we also expect
2891 * inst->pixmap to exist. */
2892 dctx->uctx.u.gdk.target = inst->pixmap;
2893 dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
2896 #ifdef DRAW_TEXT_CAIRO
2897 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2898 dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
2899 /* If we're doing Cairo drawing, we expect inst->surface to
2900 * exist, and we draw to that first, regardless of whether we
2901 * subsequently copy the results to inst->pixmap. */
2902 dctx->uctx.u.cairo.cr = cairo_create(inst->surface);
2903 cairo_setup_dctx(dctx);
2909 void free_ctx(Context ctx)
2911 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2912 /* struct gui_data *inst = dctx->inst; */
2913 #ifdef DRAW_TEXT_GDK
2914 if (dctx->uctx.type == DRAWTYPE_GDK) {
2915 gdk_gc_unref(dctx->uctx.u.gdk.gc);
2918 #ifdef DRAW_TEXT_CAIRO
2919 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2920 cairo_destroy(dctx->uctx.u.cairo.cr);
2927 static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
2929 #if defined DRAW_TEXT_CAIRO && !defined NO_BACKING_PIXMAPS
2930 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2932 * If inst->surface and inst->pixmap both exist, then we've
2933 * just drawn new content to the former which we must copy to
2936 cairo_t *cr = gdk_cairo_create(dctx->inst->pixmap);
2937 cairo_set_source_surface(cr, dctx->inst->surface, 0, 0);
2938 cairo_rectangle(cr, x, y, w, h);
2945 * Now we just queue a window redraw, which will cause
2946 * inst->surface or inst->pixmap (whichever is appropriate for our
2947 * compile mode) to be copied to the real window when we receive
2948 * the resulting "expose" or "draw" event.
2950 * Amazingly, this one API call is actually valid in all versions
2953 gtk_widget_queue_draw_area(dctx->inst->area, x, y, w, h);
2956 static void draw_set_colour(struct draw_ctx *dctx, int col)
2958 #ifdef DRAW_TEXT_GDK
2959 if (dctx->uctx.type == DRAWTYPE_GDK) {
2960 gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
2963 #ifdef DRAW_TEXT_CAIRO
2964 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2965 cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
2966 dctx->inst->cols[col].red / 65535.0,
2967 dctx->inst->cols[col].green / 65535.0,
2968 dctx->inst->cols[col].blue / 65535.0);
2973 static void draw_rectangle(struct draw_ctx *dctx, int filled,
2974 int x, int y, int w, int h)
2976 #ifdef DRAW_TEXT_GDK
2977 if (dctx->uctx.type == DRAWTYPE_GDK) {
2978 gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2979 filled, x, y, w, h);
2982 #ifdef DRAW_TEXT_CAIRO
2983 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2984 cairo_new_path(dctx->uctx.u.cairo.cr);
2986 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2987 cairo_fill(dctx->uctx.u.cairo.cr);
2989 cairo_rectangle(dctx->uctx.u.cairo.cr,
2990 x + 0.5, y + 0.5, w, h);
2991 cairo_close_path(dctx->uctx.u.cairo.cr);
2992 cairo_stroke(dctx->uctx.u.cairo.cr);
2998 static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
3000 #ifdef DRAW_TEXT_GDK
3001 if (dctx->uctx.type == DRAWTYPE_GDK) {
3009 gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
3012 #ifdef DRAW_TEXT_CAIRO
3013 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3014 cairo_reset_clip(dctx->uctx.u.cairo.cr);
3015 cairo_new_path(dctx->uctx.u.cairo.cr);
3016 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
3017 cairo_clip(dctx->uctx.u.cairo.cr);
3022 static void draw_point(struct draw_ctx *dctx, int x, int y)
3024 #ifdef DRAW_TEXT_GDK
3025 if (dctx->uctx.type == DRAWTYPE_GDK) {
3026 gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
3029 #ifdef DRAW_TEXT_CAIRO
3030 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3031 cairo_new_path(dctx->uctx.u.cairo.cr);
3032 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
3033 cairo_fill(dctx->uctx.u.cairo.cr);
3038 static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
3040 #ifdef DRAW_TEXT_GDK
3041 if (dctx->uctx.type == DRAWTYPE_GDK) {
3042 gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
3046 #ifdef DRAW_TEXT_CAIRO
3047 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3048 cairo_new_path(dctx->uctx.u.cairo.cr);
3049 cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
3050 cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
3051 cairo_stroke(dctx->uctx.u.cairo.cr);
3056 static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
3058 int h, int hdouble, int hbothalf)
3060 #ifdef DRAW_TEXT_CAIRO
3061 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3062 cairo_matrix_t matrix;
3086 cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
3091 static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
3093 int h, int hdouble, int hbothalf)
3095 #ifdef DRAW_TEXT_GDK
3096 #ifndef NO_BACKING_PIXMAPS
3097 if (dctx->uctx.type == DRAWTYPE_GDK) {
3099 * I can't find any plausible StretchBlt equivalent in the X
3100 * server, so I'm going to do this the slow and painful way.
3101 * This will involve repeated calls to gdk_draw_pixmap() to
3102 * stretch the text horizontally. It's O(N^2) in time and O(N)
3103 * in network bandwidth, but you try thinking of a better way.
3108 for (i = 0; i < w; i++) {
3109 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3110 dctx->uctx.u.gdk.gc,
3111 dctx->uctx.u.gdk.target,
3121 /* Now stretch vertically, in the same way. */
3126 for (i = 0; i < h; i += 2) {
3127 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3128 dctx->uctx.u.gdk.gc,
3129 dctx->uctx.u.gdk.target,
3137 #error No way to implement stretching in GDK without a reliable backing pixmap
3139 #endif /* DRAW_TEXT_GDK */
3140 #ifdef DRAW_TEXT_CAIRO
3141 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3142 cairo_set_matrix(dctx->uctx.u.cairo.cr,
3143 &dctx->uctx.u.cairo.origmatrix);
3148 static void draw_backing_rect(struct gui_data *inst)
3150 struct draw_ctx *dctx = get_ctx(inst);
3151 int w = inst->width * inst->font_width + 2*inst->window_border;
3152 int h = inst->height * inst->font_height + 2*inst->window_border;
3153 draw_set_colour(dctx, 258);
3154 draw_rectangle(dctx, 1, 0, 0, w, h);
3155 draw_update(dctx, 0, 0, w, h);
3160 * Draw a line of text in the window, at given character
3161 * coordinates, in given attributes.
3163 * We are allowed to fiddle with the contents of `text'.
3165 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
3166 unsigned long attr, int lattr)
3168 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3169 struct gui_data *inst = dctx->inst;
3171 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
3173 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
3175 if (attr & TATTR_COMBINING) {
3181 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
3182 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
3183 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
3188 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
3189 if (nfg < 16) nfg |= 8;
3190 else if (nfg >= 256) nfg |= 1;
3192 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
3193 if (nbg < 16) nbg |= 8;
3194 else if (nbg >= 256) nbg |= 1;
3196 if ((attr & TATTR_ACTCURS) && !monochrome) {
3201 fontid = shadow = 0;
3203 if (attr & ATTR_WIDE) {
3210 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
3217 if (!inst->fonts[fontid]) {
3220 * Fall back through font ids with subsets of this one's
3221 * set bits, in order.
3223 for (i = fontid; i-- > 0 ;) {
3225 continue; /* some other bit is set */
3226 if (inst->fonts[i]) {
3231 assert(inst->fonts[fontid]); /* we should at least have hit zero */
3234 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3236 if (x >= inst->term->cols)
3238 if (x + len*2*widefactor > inst->term->cols)
3239 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3245 x*inst->font_width+inst->window_border,
3246 y*inst->font_height+inst->window_border,
3247 rlen*widefactor*inst->font_width,
3250 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3251 draw_stretch_before(dctx,
3252 x*inst->font_width+inst->window_border,
3253 y*inst->font_height+inst->window_border,
3254 rlen*widefactor*inst->font_width, TRUE,
3256 ((lattr & LATTR_MODE) != LATTR_WIDE),
3257 ((lattr & LATTR_MODE) == LATTR_BOT));
3260 draw_set_colour(dctx, nbg);
3261 draw_rectangle(dctx, TRUE,
3262 x*inst->font_width+inst->window_border,
3263 y*inst->font_height+inst->window_border,
3264 rlen*widefactor*inst->font_width, inst->font_height);
3266 draw_set_colour(dctx, nfg);
3267 if (ncombining > 1) {
3269 unifont_draw_combining(&dctx->uctx, inst->fonts[fontid],
3270 x*inst->font_width+inst->window_border,
3271 (y*inst->font_height+inst->window_border+
3272 inst->fonts[0]->ascent),
3273 text, ncombining, widefactor > 1,
3274 bold, inst->font_width);
3276 unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
3277 x*inst->font_width+inst->window_border,
3278 (y*inst->font_height+inst->window_border+
3279 inst->fonts[0]->ascent),
3280 text, len, widefactor > 1,
3281 bold, inst->font_width);
3284 if (attr & ATTR_UNDER) {
3285 int uheight = inst->fonts[0]->ascent + 1;
3286 if (uheight >= inst->font_height)
3287 uheight = inst->font_height - 1;
3288 draw_line(dctx, x*inst->font_width+inst->window_border,
3289 y*inst->font_height + uheight + inst->window_border,
3290 (x+len)*widefactor*inst->font_width-1+inst->window_border,
3291 y*inst->font_height + uheight + inst->window_border);
3294 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3295 draw_stretch_after(dctx,
3296 x*inst->font_width+inst->window_border,
3297 y*inst->font_height+inst->window_border,
3298 rlen*widefactor*inst->font_width, TRUE,
3300 ((lattr & LATTR_MODE) != LATTR_WIDE),
3301 ((lattr & LATTR_MODE) == LATTR_BOT));
3305 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3306 unsigned long attr, int lattr)
3308 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3309 struct gui_data *inst = dctx->inst;
3312 do_text_internal(ctx, x, y, text, len, attr, lattr);
3314 if (attr & ATTR_WIDE) {
3320 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3322 if (x >= inst->term->cols)
3324 if (x + len*2*widefactor > inst->term->cols)
3325 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3330 x*inst->font_width+inst->window_border,
3331 y*inst->font_height+inst->window_border,
3332 len*widefactor*inst->font_width, inst->font_height);
3335 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
3336 unsigned long attr, int lattr)
3338 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3339 struct gui_data *inst = dctx->inst;
3341 int active, passive, widefactor;
3343 if (attr & TATTR_PASCURS) {
3344 attr &= ~TATTR_PASCURS;
3348 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
3349 attr &= ~TATTR_ACTCURS;
3353 do_text_internal(ctx, x, y, text, len, attr, lattr);
3355 if (attr & TATTR_COMBINING)
3358 if (attr & ATTR_WIDE) {
3364 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3366 if (x >= inst->term->cols)
3368 if (x + len*2*widefactor > inst->term->cols)
3369 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3373 if (inst->cursor_type == 0) {
3375 * An active block cursor will already have been done by
3376 * the above do_text call, so we only need to do anything
3380 draw_set_colour(dctx, 261);
3381 draw_rectangle(dctx, FALSE,
3382 x*inst->font_width+inst->window_border,
3383 y*inst->font_height+inst->window_border,
3384 len*widefactor*inst->font_width-1,
3385 inst->font_height-1);
3389 int startx, starty, dx, dy, length, i;
3393 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
3394 char_width = 2*inst->font_width;
3396 char_width = inst->font_width;
3398 if (inst->cursor_type == 1) {
3399 uheight = inst->fonts[0]->ascent + 1;
3400 if (uheight >= inst->font_height)
3401 uheight = inst->font_height - 1;
3403 startx = x * inst->font_width + inst->window_border;
3404 starty = y * inst->font_height + inst->window_border + uheight;
3407 length = len * widefactor * char_width;
3410 if (attr & TATTR_RIGHTCURS)
3411 xadjust = char_width - 1;
3412 startx = x * inst->font_width + inst->window_border + xadjust;
3413 starty = y * inst->font_height + inst->window_border;
3416 length = inst->font_height;
3419 draw_set_colour(dctx, 261);
3421 for (i = 0; i < length; i++) {
3423 draw_point(dctx, startx, starty);
3428 } else if (active) {
3429 draw_line(dctx, startx, starty,
3430 startx + (length-1) * dx, starty + (length-1) * dy);
3431 } /* else no cursor (e.g., blinked off) */
3435 x*inst->font_width+inst->window_border,
3436 y*inst->font_height+inst->window_border,
3437 len*widefactor*inst->font_width, inst->font_height);
3439 #if GTK_CHECK_VERSION(2,0,0)
3441 GdkRectangle cursorrect;
3442 cursorrect.x = x*inst->font_width+inst->window_border;
3443 cursorrect.y = y*inst->font_height+inst->window_border;
3444 cursorrect.width = len*widefactor*inst->font_width;
3445 cursorrect.height = inst->font_height;
3446 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
3451 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
3453 if (cursor_val == -1) {
3454 #if GTK_CHECK_VERSION(2,16,0)
3455 cursor_val = GDK_BLANK_CURSOR;
3458 * Work around absence of GDK_BLANK_CURSOR by inventing a
3462 GdkColor bg = { 0, 0, 0, 0 };
3463 GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
3464 GdkGC *gc = gdk_gc_new(pm);
3465 gdk_gc_set_foreground(gc, &bg);
3466 gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
3468 ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
3469 gdk_pixmap_unref(pm);
3474 return gdk_cursor_new(cursor_val);
3477 void modalfatalbox(const char *p, ...)
3480 fprintf(stderr, "FATAL ERROR: ");
3482 vfprintf(stderr, p, ap);
3484 fputc('\n', stderr);
3488 void cmdline_error(const char *p, ...)
3491 fprintf(stderr, "%s: ", appname);
3493 vfprintf(stderr, p, ap);
3495 fputc('\n', stderr);
3499 const char *get_x_display(void *frontend)
3501 return gdk_get_display();
3504 #ifndef NOT_X_WINDOWS
3505 long get_windowid(void *frontend)
3507 struct gui_data *inst = (struct gui_data *)frontend;
3508 return (long)GDK_WINDOW_XID(gtk_widget_get_window(inst->area));
3512 int frontend_is_utf8(void *frontend)
3514 struct gui_data *inst = (struct gui_data *)frontend;
3515 return inst->ucsdata.line_codepage == CS_UTF8;
3518 char *setup_fonts_ucs(struct gui_data *inst)
3520 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
3521 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
3526 fs = conf_get_fontspec(inst->conf, CONF_font);
3527 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
3528 shadowboldoffset, shadowbold);
3530 return dupprintf("unable to load font \"%s\"", fs->name);
3533 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
3534 if (shadowbold || !fs->name[0]) {
3537 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
3538 shadowboldoffset, shadowbold);
3541 unifont_destroy(fonts[0]);
3542 return dupprintf("unable to load bold font \"%s\"", fs->name);
3546 fs = conf_get_fontspec(inst->conf, CONF_widefont);
3548 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
3549 shadowboldoffset, shadowbold);
3551 for (i = 0; i < 2; i++)
3553 unifont_destroy(fonts[i]);
3554 return dupprintf("unable to load wide font \"%s\"", fs->name);
3560 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
3561 if (shadowbold || !fs->name[0]) {
3564 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
3565 shadowboldoffset, shadowbold);
3567 for (i = 0; i < 3; i++)
3569 unifont_destroy(fonts[i]);
3570 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
3575 * Now we've got past all the possible error conditions, we can
3576 * actually update our state.
3579 for (i = 0; i < 4; i++) {
3581 unifont_destroy(inst->fonts[i]);
3582 inst->fonts[i] = fonts[i];
3585 inst->font_width = inst->fonts[0]->width;
3586 inst->font_height = inst->fonts[0]->height;
3588 inst->direct_to_font = init_ucs(&inst->ucsdata,
3589 conf_get_str(inst->conf, CONF_line_codepage),
3590 conf_get_int(inst->conf, CONF_utf8_override),
3591 inst->fonts[0]->public_charset,
3592 conf_get_int(inst->conf, CONF_vtmode));
3594 inst->drawtype = inst->fonts[0]->preferred_drawtype;
3599 void set_geom_hints(struct gui_data *inst)
3602 geom.min_width = inst->font_width + 2*inst->window_border;
3603 geom.min_height = inst->font_height + 2*inst->window_border;
3604 geom.max_width = geom.max_height = -1;
3605 geom.base_width = 2*inst->window_border;
3606 geom.base_height = 2*inst->window_border;
3607 geom.width_inc = inst->font_width;
3608 geom.height_inc = inst->font_height;
3609 geom.min_aspect = geom.max_aspect = 0;
3610 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
3611 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
3612 GDK_HINT_RESIZE_INC);
3615 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
3617 struct gui_data *inst = (struct gui_data *)data;
3618 term_clrsb(inst->term);
3621 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3623 struct gui_data *inst = (struct gui_data *)data;
3624 term_pwron(inst->term, TRUE);
3626 ldisc_echoedit_update(inst->ldisc);
3629 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3631 struct gui_data *inst = (struct gui_data *)data;
3632 term_copyall(inst->term);
3635 void special_menuitem(GtkMenuItem *item, gpointer data)
3637 struct gui_data *inst = (struct gui_data *)data;
3638 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
3642 inst->back->special(inst->backhandle, code);
3645 void about_menuitem(GtkMenuItem *item, gpointer data)
3647 struct gui_data *inst = (struct gui_data *)data;
3648 about_box(inst->window);
3651 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3653 struct gui_data *inst = (struct gui_data *)data;
3654 showeventlog(inst->eventlogstuff, inst->window);
3657 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3659 /* This maps colour indices in inst->conf to those used in inst->cols. */
3660 static const int ww[] = {
3661 256, 257, 258, 259, 260, 261,
3662 0, 8, 1, 9, 2, 10, 3, 11,
3663 4, 12, 5, 13, 6, 14, 7, 15
3665 struct gui_data *inst = (struct gui_data *)data;
3667 Conf *oldconf, *newconf;
3668 int i, j, need_size;
3670 assert(lenof(ww) == NCFGCOLOURS);
3672 if (inst->reconfiguring)
3675 inst->reconfiguring = TRUE;
3677 title = dupcat(appname, " Reconfiguration", NULL);
3679 oldconf = inst->conf;
3680 newconf = conf_copy(inst->conf);
3682 if (do_config_box(title, newconf, 1,
3683 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3684 inst->conf = newconf;
3686 /* Pass new config data to the logging module */
3687 log_reconfig(inst->logctx, inst->conf);
3689 * Flush the line discipline's edit buffer in the case
3690 * where local editing has just been disabled.
3693 ldisc_configure(inst->ldisc, inst->conf);
3694 ldisc_echoedit_update(inst->ldisc);
3696 /* Pass new config data to the terminal */
3697 term_reconfig(inst->term, inst->conf);
3698 /* Pass new config data to the back end */
3700 inst->back->reconfig(inst->backhandle, inst->conf);
3702 cache_conf_values(inst);
3705 * Just setting inst->conf is sufficient to cause colour
3706 * setting changes to appear on the next ESC]R palette
3707 * reset. But we should also check whether any colour
3708 * settings have been changed, and revert the ones that have
3709 * to the new default, on the assumption that the user is
3710 * most likely to want an immediate update.
3712 for (i = 0; i < NCFGCOLOURS; i++) {
3713 for (j = 0; j < 3; j++)
3714 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3715 conf_get_int_int(newconf, CONF_colours, i*3+j))
3718 real_palette_set(inst, ww[i],
3719 conf_get_int_int(newconf,CONF_colours,i*3+0),
3720 conf_get_int_int(newconf,CONF_colours,i*3+1),
3721 conf_get_int_int(newconf,CONF_colours,i*3+2));
3724 * If the default background has changed, we must
3725 * repaint the space in between the window border
3726 * and the text area.
3729 set_window_background(inst);
3730 draw_backing_rect(inst);
3736 * If the scrollbar needs to be shown, hidden, or moved
3737 * from one end to the other of the window, do so now.
3739 if (conf_get_int(oldconf, CONF_scrollbar) !=
3740 conf_get_int(newconf, CONF_scrollbar)) {
3741 if (conf_get_int(newconf, CONF_scrollbar))
3742 gtk_widget_show(inst->sbar);
3744 gtk_widget_hide(inst->sbar);
3746 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3747 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3748 gtk_box_reorder_child(inst->hbox, inst->sbar,
3749 conf_get_int(newconf, CONF_scrollbar_on_left)
3754 * Change the window title, if required.
3756 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3757 conf_get_str(newconf, CONF_wintitle)))
3758 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3759 set_window_titles(inst);
3762 * Redo the whole tangled fonts and Unicode mess if
3766 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3767 conf_get_fontspec(newconf, CONF_font)->name) ||
3768 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3769 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3770 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3771 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3772 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3773 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3774 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3775 conf_get_str(newconf, CONF_line_codepage)) ||
3776 conf_get_int(oldconf, CONF_utf8_override) !=
3777 conf_get_int(newconf, CONF_utf8_override) ||
3778 conf_get_int(oldconf, CONF_vtmode) !=
3779 conf_get_int(newconf, CONF_vtmode) ||
3780 conf_get_int(oldconf, CONF_shadowbold) !=
3781 conf_get_int(newconf, CONF_shadowbold) ||
3782 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3783 conf_get_int(newconf, CONF_shadowboldoffset)) {
3784 char *errmsg = setup_fonts_ucs(inst);
3787 dupprintf("Could not change fonts in terminal window: %s\n",
3789 messagebox(inst->window, "Font setup error", msgboxtext,
3790 string_width("Could not change fonts in terminal window:"),
3791 FALSE, "OK", 'o', +1, 1,
3801 * Resize the window.
3803 if (conf_get_int(oldconf, CONF_width) !=
3804 conf_get_int(newconf, CONF_width) ||
3805 conf_get_int(oldconf, CONF_height) !=
3806 conf_get_int(newconf, CONF_height) ||
3807 conf_get_int(oldconf, CONF_window_border) !=
3808 conf_get_int(newconf, CONF_window_border) ||
3810 set_geom_hints(inst);
3811 request_resize(inst, conf_get_int(newconf, CONF_width),
3812 conf_get_int(newconf, CONF_height));
3815 * The above will have caused a call to term_size() for
3816 * us if it happened. If the user has fiddled with only
3817 * the scrollback size, the above will not have
3818 * happened and we will need an explicit term_size()
3821 if (conf_get_int(oldconf, CONF_savelines) !=
3822 conf_get_int(newconf, CONF_savelines))
3823 term_size(inst->term, inst->term->rows, inst->term->cols,
3824 conf_get_int(newconf, CONF_savelines));
3827 term_invalidate(inst->term);
3830 * We do an explicit full redraw here to ensure the window
3831 * border has been redrawn as well as the text area.
3833 gtk_widget_queue_draw(inst->area);
3840 inst->reconfiguring = FALSE;
3843 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3845 struct gui_data *inst = (struct gui_data *)gdata;
3847 launch_duplicate_session(inst->conf);
3850 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3852 launch_new_session();
3855 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3857 struct gui_data *inst = (struct gui_data *)data;
3860 logevent(inst, "----- Session restarted -----");
3861 term_pwron(inst->term, FALSE);
3862 start_backend(inst);
3863 inst->exited = FALSE;
3867 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3869 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3871 launch_saved_session(str);
3874 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3876 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
3881 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3883 struct gui_data *inst = (struct gui_data *)data;
3884 struct sesslist sesslist;
3887 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3888 (GtkCallback)gtk_widget_destroy, NULL);
3890 get_sesslist(&sesslist, TRUE);
3891 /* skip sesslist.sessions[0] == Default Settings */
3892 for (i = 1; i < sesslist.nsessions; i++) {
3893 GtkWidget *menuitem =
3894 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3895 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3896 gtk_widget_show(menuitem);
3897 g_object_set_data(G_OBJECT(menuitem), "user-data",
3898 dupstr(sesslist.sessions[i]));
3899 g_signal_connect(G_OBJECT(menuitem), "activate",
3900 G_CALLBACK(saved_session_menuitem),
3902 g_signal_connect(G_OBJECT(menuitem), "destroy",
3903 G_CALLBACK(saved_session_freedata),
3906 if (sesslist.nsessions <= 1) {
3907 GtkWidget *menuitem =
3908 gtk_menu_item_new_with_label("(No sessions)");
3909 gtk_widget_set_sensitive(menuitem, FALSE);
3910 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3911 gtk_widget_show(menuitem);
3913 get_sesslist(&sesslist, FALSE); /* free up */
3916 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3919 #if GTK_CHECK_VERSION(2,0,0)
3924 GdkBitmap *iconmask;
3930 gtk_widget_realize(window);
3931 #if GTK_CHECK_VERSION(2,0,0)
3932 gtk_window_set_icon(GTK_WINDOW(window),
3933 gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
3935 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
3936 &iconmask, NULL, (gchar **)icon[0]);
3937 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
3940 #if GTK_CHECK_VERSION(2,0,0)
3942 for (n = 0; n < n_icon; n++) {
3944 g_list_append(iconlist,
3945 gdk_pixbuf_new_from_xpm_data((const gchar **)
3948 gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
3952 void update_specials_menu(void *frontend)
3954 struct gui_data *inst = (struct gui_data *)frontend;
3956 const struct telnet_special *specials;
3959 specials = inst->back->get_specials(inst->backhandle);
3963 /* I believe this disposes of submenus too. */
3964 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3965 (GtkCallback)gtk_widget_destroy, NULL);
3968 GtkWidget *menu = inst->specialsmenu;
3969 /* A lame "stack" for submenus that will do for now. */
3970 GtkWidget *saved_menu = NULL;
3972 for (i = 0; nesting > 0; i++) {
3973 GtkWidget *menuitem = NULL;
3974 switch (specials[i].code) {
3976 assert (nesting < 2);
3977 saved_menu = menu; /* XXX lame stacking */
3978 menu = gtk_menu_new();
3979 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3980 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3981 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3982 gtk_widget_show(menuitem);
3989 menu = saved_menu; /* XXX lame stacking */
3994 menuitem = gtk_menu_item_new();
3997 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3998 g_object_set_data(G_OBJECT(menuitem), "user-data",
3999 GINT_TO_POINTER(specials[i].code));
4000 g_signal_connect(G_OBJECT(menuitem), "activate",
4001 G_CALLBACK(special_menuitem), inst);
4005 gtk_container_add(GTK_CONTAINER(menu), menuitem);
4006 gtk_widget_show(menuitem);
4009 gtk_widget_show(inst->specialsitem1);
4010 gtk_widget_show(inst->specialsitem2);
4012 gtk_widget_hide(inst->specialsitem1);
4013 gtk_widget_hide(inst->specialsitem2);
4017 static void start_backend(struct gui_data *inst)
4019 extern Backend *select_backend(Conf *conf);
4024 inst->back = select_backend(inst->conf);
4026 error = inst->back->init((void *)inst, &inst->backhandle,
4028 conf_get_str(inst->conf, CONF_host),
4029 conf_get_int(inst->conf, CONF_port),
4031 conf_get_int(inst->conf, CONF_tcp_nodelay),
4032 conf_get_int(inst->conf, CONF_tcp_keepalives));
4035 char *msg = dupprintf("Unable to open connection to %s:\n%s",
4036 conf_get_str(inst->conf, CONF_host), error);
4037 inst->exited = TRUE;
4038 fatal_message_box(inst->window, msg);
4043 s = conf_get_str(inst->conf, CONF_wintitle);
4045 set_title_and_icon(inst, s, s);
4047 char *title = make_default_wintitle(realhost);
4048 set_title_and_icon(inst, title, title);
4053 inst->back->provide_logctx(inst->backhandle, inst->logctx);
4055 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
4058 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
4061 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4064 struct gui_data *new_session_window(Conf *conf, const char *geometry_string)
4066 struct gui_data *inst;
4069 * Create an instance structure and initialise to zeroes
4071 inst = snew(struct gui_data);
4072 memset(inst, 0, sizeof(*inst));
4073 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
4074 inst->busy_status = BUSY_NOT;
4076 inst->wintitle = inst->icontitle = NULL;
4077 inst->drawtype = DRAWTYPE_DEFAULT;
4078 #if GTK_CHECK_VERSION(3,4,0)
4079 inst->cumulative_scroll = 0.0;
4082 if (geometry_string) {
4083 #if GTK_CHECK_VERSION(2,0,0)
4084 inst->geometry = geometry_string;
4086 /* On GTK 1, we have to do this using raw Xlib */
4089 flags = XParseGeometry(geometry_string, &x, &y, &w, &h);
4090 if (flags & WidthValue)
4091 conf_set_int(conf, CONF_width, w);
4092 if (flags & HeightValue)
4093 conf_set_int(conf, CONF_height, h);
4095 if (flags & (XValue | YValue)) {
4098 inst->gotpos = TRUE;
4099 inst->gravity = ((flags & XNegative ? 1 : 0) |
4100 (flags & YNegative ? 2 : 0));
4105 if (!compound_text_atom)
4106 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
4107 if (!utf8_string_atom)
4108 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
4110 inst->area = gtk_drawing_area_new();
4112 #if GTK_CHECK_VERSION(2,0,0)
4113 inst->imc = gtk_im_multicontext_new();
4117 char *errmsg = setup_fonts_ucs(inst);
4119 fprintf(stderr, "%s: %s\n", appname, errmsg);
4123 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4125 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
4127 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
4128 winclass, winclass);
4132 * Set up the colour map.
4134 palette_reset(inst);
4136 inst->width = conf_get_int(inst->conf, CONF_width);
4137 inst->height = conf_get_int(inst->conf, CONF_height);
4138 cache_conf_values(inst);
4140 init_clipboard(inst);
4142 set_geom_hints(inst);
4144 #if GTK_CHECK_VERSION(3,0,0)
4145 gtk_window_set_default_geometry(GTK_WINDOW(inst->window),
4146 inst->width, inst->height);
4149 int w = inst->font_width * inst->width + 2*inst->window_border;
4150 int h = inst->font_height * inst->height + 2*inst->window_border;
4151 #if GTK_CHECK_VERSION(2,0,0)
4152 gtk_widget_set_size_request(inst->area, w, h);
4154 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
4159 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
4160 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
4161 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
4163 * We always create the scrollbar; it remains invisible if
4164 * unwanted, so we can pop it up quickly if it suddenly becomes
4167 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
4168 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4169 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
4170 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
4171 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4173 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
4175 gtk_widget_show(inst->area);
4176 if (conf_get_int(inst->conf, CONF_scrollbar))
4177 gtk_widget_show(inst->sbar);
4179 gtk_widget_hide(inst->sbar);
4180 gtk_widget_show(GTK_WIDGET(inst->hbox));
4182 #if GTK_CHECK_VERSION(2,0,0)
4183 if (inst->geometry) {
4184 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
4188 int x = inst->xpos, y = inst->ypos;
4190 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
4191 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
4192 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
4193 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
4194 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
4198 g_signal_connect(G_OBJECT(inst->window), "destroy",
4199 G_CALLBACK(destroy), inst);
4200 g_signal_connect(G_OBJECT(inst->window), "delete_event",
4201 G_CALLBACK(delete_window), inst);
4202 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
4203 G_CALLBACK(key_event), inst);
4204 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
4205 G_CALLBACK(key_event), inst);
4206 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
4207 G_CALLBACK(focus_event), inst);
4208 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
4209 G_CALLBACK(focus_event), inst);
4210 g_signal_connect(G_OBJECT(inst->area), "configure_event",
4211 G_CALLBACK(configure_area), inst);
4212 #if GTK_CHECK_VERSION(3,0,0)
4213 g_signal_connect(G_OBJECT(inst->area), "draw",
4214 G_CALLBACK(draw_area), inst);
4216 g_signal_connect(G_OBJECT(inst->area), "expose_event",
4217 G_CALLBACK(expose_area), inst);
4219 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
4220 G_CALLBACK(button_event), inst);
4221 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
4222 G_CALLBACK(button_event), inst);
4223 #if GTK_CHECK_VERSION(2,0,0)
4224 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
4225 G_CALLBACK(scroll_event), inst);
4227 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
4228 G_CALLBACK(motion_event), inst);
4229 #if GTK_CHECK_VERSION(2,0,0)
4230 g_signal_connect(G_OBJECT(inst->imc), "commit",
4231 G_CALLBACK(input_method_commit_event), inst);
4233 if (conf_get_int(inst->conf, CONF_scrollbar))
4234 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
4235 G_CALLBACK(scrollbar_moved), inst);
4236 gtk_widget_add_events(GTK_WIDGET(inst->area),
4237 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
4238 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
4239 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK
4240 #if GTK_CHECK_VERSION(3,4,0)
4241 | GDK_SMOOTH_SCROLL_MASK
4246 extern const char *const *const main_icon[];
4247 extern const int n_main_icon;
4248 set_window_icon(inst->window, main_icon, n_main_icon);
4251 gtk_widget_show(inst->window);
4253 set_window_background(inst);
4256 * Set up the Ctrl+rightclick context menu.
4259 GtkWidget *menuitem;
4261 extern const int use_event_log, new_session, saved_sessions;
4263 inst->menu = gtk_menu_new();
4265 #define MKMENUITEM(title, func) do \
4267 menuitem = gtk_menu_item_new_with_label(title); \
4268 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4269 gtk_widget_show(menuitem); \
4270 g_signal_connect(G_OBJECT(menuitem), "activate", \
4271 G_CALLBACK(func), inst); \
4274 #define MKSUBMENU(title) do \
4276 menuitem = gtk_menu_item_new_with_label(title); \
4277 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4278 gtk_widget_show(menuitem); \
4281 #define MKSEP() do \
4283 menuitem = gtk_menu_item_new(); \
4284 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4285 gtk_widget_show(menuitem); \
4289 MKMENUITEM("New Session...", new_session_menuitem);
4290 MKMENUITEM("Restart Session", restart_session_menuitem);
4291 inst->restartitem = menuitem;
4292 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4293 MKMENUITEM("Duplicate Session", dup_session_menuitem);
4294 if (saved_sessions) {
4295 inst->sessionsmenu = gtk_menu_new();
4296 /* sessionsmenu will be updated when it's invoked */
4297 /* XXX is this the right way to do dynamic menus in Gtk? */
4298 MKMENUITEM("Saved Sessions", update_savedsess_menu);
4299 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
4300 inst->sessionsmenu);
4303 MKMENUITEM("Change Settings...", change_settings_menuitem);
4306 MKMENUITEM("Event Log", event_log_menuitem);
4307 MKSUBMENU("Special Commands");
4308 inst->specialsmenu = gtk_menu_new();
4309 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
4310 inst->specialsitem1 = menuitem;
4312 inst->specialsitem2 = menuitem;
4313 gtk_widget_hide(inst->specialsitem1);
4314 gtk_widget_hide(inst->specialsitem2);
4315 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
4316 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
4317 MKMENUITEM("Copy All", copy_all_menuitem);
4319 s = dupcat("About ", appname, NULL);
4320 MKMENUITEM(s, about_menuitem);
4327 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
4328 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
4329 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
4330 inst->blankcursor = make_mouse_ptr(inst, -1);
4331 inst->currcursor = inst->textcursor;
4332 show_mouseptr(inst, 1);
4334 inst->eventlogstuff = eventlogstuff_new();
4336 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
4337 inst->logctx = log_init(inst, inst->conf);
4338 term_provide_logctx(inst->term, inst->logctx);
4340 term_size(inst->term, inst->height, inst->width,
4341 conf_get_int(inst->conf, CONF_savelines));
4343 start_backend(inst);
4345 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
4347 inst->exited = FALSE;