2 * gtkwin.c: the main code that runs a PuTTY terminal emulator and
3 * backend in a GTK window.
19 #include <sys/types.h>
22 #if !GTK_CHECK_VERSION(3,0,0)
23 #include <gdk/gdkkeysyms.h>
26 #if GTK_CHECK_VERSION(2,0,0)
27 #include <gtk/gtkimmodule.h>
30 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
32 #define MAY_REFER_TO_GTK_IN_HEADERS
36 #include "gtkcompat.h"
43 #include <X11/Xutil.h>
44 #include <X11/Xatom.h>
47 #define CAT2(x,y) x ## y
48 #define CAT(x,y) CAT2(x,y)
49 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
51 #if GTK_CHECK_VERSION(2,0,0)
52 ASSERT(sizeof(long) <= sizeof(gsize));
53 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
54 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
56 ASSERT(sizeof(long) <= sizeof(gpointer));
57 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
58 #define GPOINTER_TO_LONG(p) ((long)(p))
61 /* Colours come in two flavours: configurable, and xterm-extended. */
62 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
63 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
65 GdkAtom compound_text_atom, utf8_string_atom;
67 extern char **pty_argv; /* declared in pty.c */
68 extern int use_pty_argv;
71 * Timers are global across all sessions (even if we were handling
72 * multiple sessions, which we aren't), so the current timer ID is
75 static guint timer_id = 0;
77 struct clipboard_data_instance;
80 GtkWidget *window, *area, *sbar;
82 GtkAdjustment *sbar_adjust;
83 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
85 GtkWidget *sessionsmenu;
86 #ifndef NO_BACKING_PIXMAPS
88 * Server-side pixmap which we use to cache the terminal window's
89 * contents. When we draw text in the terminal, we draw it to this
90 * pixmap first, and then blit from there to the actual window;
91 * this way, X expose events can be handled with an absolute
92 * minimum of network traffic, by just sending a command to
93 * re-blit an appropriate rectangle from this pixmap.
97 #ifdef DRAW_TEXT_CAIRO
99 * If we're drawing using Cairo, we cache the same image on the
100 * client side in a Cairo surface.
102 * In GTK2+Cairo, this happens _as well_ as having the server-side
103 * pixmap cache above; in GTK3+Cairo, server-side pixmaps are
104 * deprecated, so we _just_ have this client-side cache. In the
105 * latter case that means we have to transmit a big wodge of
106 * bitmap data over the X connection on every expose event; but
107 * GTK3 apparently deliberately provides no way to avoid that
108 * inefficiency, and at least this way we don't _also_ have to
109 * redo any font rendering just because the window was temporarily
112 cairo_surface_t *surface;
114 #if GTK_CHECK_VERSION(2,0,0)
117 unifont *fonts[4]; /* normal, bold, wide, widebold */
118 #if GTK_CHECK_VERSION(2,0,0)
119 const char *geometry;
121 int xpos, ypos, gotpos, gravity;
123 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
124 GdkColor cols[NALLCOLOURS];
125 #if !GTK_CHECK_VERSION(3,0,0)
129 wchar_t *pastein_data;
130 int pastein_data_len;
131 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
132 GtkClipboard *clipboard;
133 struct clipboard_data_instance *current_cdi;
135 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
136 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
138 int font_width, font_height;
141 int mouseptr_visible;
143 guint toplevel_callback_idle_id;
144 int idle_fn_scheduled, quit_fn_scheduled;
149 int master_fd, master_func_id;
156 struct unicode_data ucsdata;
159 char *progname, **gtkargvstart;
161 guint32 input_event_time; /* Timestamp of the most recent input event. */
163 #if GTK_CHECK_VERSION(3,4,0)
164 gdouble cumulative_scroll;
166 /* Cached things out of conf that we refer to a lot */
174 static void cache_conf_values(struct gui_data *inst)
176 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
177 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
178 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
179 #ifdef OSX_META_KEY_CONFIG
180 inst->meta_mod_mask = 0;
181 if (conf_get_int(inst->conf, CONF_osx_option_meta))
182 inst->meta_mod_mask |= GDK_MOD1_MASK;
183 if (conf_get_int(inst->conf, CONF_osx_command_meta))
184 inst->meta_mod_mask |= GDK_MOD2_MASK;
186 inst->meta_mod_mask = GDK_MOD1_MASK;
191 struct gui_data *inst;
192 unifont_drawctx uctx;
195 static int send_raw_mouse;
197 static const char *app_name = "pterm";
199 static void start_backend(struct gui_data *inst);
200 static void exit_callback(void *vinst);
202 char *x_get_default(const char *key)
204 #ifndef NOT_X_WINDOWS
205 return XGetDefault(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
212 void connection_fatal(void *frontend, const char *p, ...)
214 struct gui_data *inst = (struct gui_data *)frontend;
219 msg = dupvprintf(p, ap);
221 fatal_message_box(inst->window, msg);
224 queue_toplevel_callback(exit_callback, inst);
228 * Default settings that are specific to pterm.
230 FontSpec *platform_default_fontspec(const char *name)
232 if (!strcmp(name, "Font"))
233 return fontspec_new(DEFAULT_GTK_FONT);
235 return fontspec_new("");
238 Filename *platform_default_filename(const char *name)
240 if (!strcmp(name, "LogFileName"))
241 return filename_from_str("putty.log");
243 return filename_from_str("");
246 char *platform_default_s(const char *name)
248 if (!strcmp(name, "SerialLine"))
249 return dupstr("/dev/ttyS0");
253 int platform_default_i(const char *name, int def)
255 if (!strcmp(name, "CloseOnExit"))
256 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
257 if (!strcmp(name, "WinNameAlways"))
258 return 0; /* X natively supports icon titles, so use 'em by default */
262 /* Dummy routine, only required in plink. */
263 void frontend_echoedit_update(void *frontend, int echo, int edit)
267 char *get_ttymode(void *frontend, const char *mode)
269 struct gui_data *inst = (struct gui_data *)frontend;
270 return term_get_ttymode(inst->term, mode);
273 int from_backend(void *frontend, int is_stderr, const char *data, int len)
275 struct gui_data *inst = (struct gui_data *)frontend;
276 return term_data(inst->term, is_stderr, data, len);
279 int from_backend_untrusted(void *frontend, const char *data, int len)
281 struct gui_data *inst = (struct gui_data *)frontend;
282 return term_data_untrusted(inst->term, data, len);
285 int from_backend_eof(void *frontend)
287 return TRUE; /* do respond to incoming EOF with outgoing */
290 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
292 struct gui_data *inst = (struct gui_data *)p->frontend;
294 ret = cmdline_get_passwd_input(p, in, inlen);
296 ret = term_get_userpass_input(inst->term, p, in, inlen);
300 void logevent(void *frontend, const char *string)
302 struct gui_data *inst = (struct gui_data *)frontend;
304 log_eventlog(inst->logctx, string);
306 logevent_dlg(inst->eventlogstuff, string);
309 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
311 struct gui_data *inst = (struct gui_data *)frontend;
314 return inst->font_height;
316 return inst->font_width;
320 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
321 * into a cooked one (SELECT, EXTEND, PASTE).
323 * In Unix, this is not configurable; the X button arrangement is
324 * rock-solid across all applications, everyone has a three-button
325 * mouse or a means of faking it, and there is no need to switch
326 * buttons around at all.
328 static Mouse_Button translate_button(Mouse_Button button)
330 /* struct gui_data *inst = (struct gui_data *)frontend; */
332 if (button == MBT_LEFT)
334 if (button == MBT_MIDDLE)
336 if (button == MBT_RIGHT)
338 return 0; /* shouldn't happen */
342 * Return the top-level GtkWindow associated with a particular
343 * front end instance.
345 void *get_window(void *frontend)
347 struct gui_data *inst = (struct gui_data *)frontend;
352 * Minimise or restore the window in response to a server-side
355 void set_iconic(void *frontend, int iconic)
358 * GTK 1.2 doesn't know how to do this.
360 #if GTK_CHECK_VERSION(2,0,0)
361 struct gui_data *inst = (struct gui_data *)frontend;
363 gtk_window_iconify(GTK_WINDOW(inst->window));
365 gtk_window_deiconify(GTK_WINDOW(inst->window));
370 * Move the window in response to a server-side request.
372 void move_window(void *frontend, int x, int y)
374 struct gui_data *inst = (struct gui_data *)frontend;
376 * I assume that when the GTK version of this call is available
377 * we should use it. Not sure how it differs from the GDK one,
380 #if GTK_CHECK_VERSION(2,0,0)
381 gtk_window_move(GTK_WINDOW(inst->window), x, y);
383 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
388 * Move the window to the top or bottom of the z-order in response
389 * to a server-side request.
391 void set_zorder(void *frontend, int top)
393 struct gui_data *inst = (struct gui_data *)frontend;
395 gdk_window_raise(gtk_widget_get_window(inst->window));
397 gdk_window_lower(gtk_widget_get_window(inst->window));
401 * Refresh the window in response to a server-side request.
403 void refresh_window(void *frontend)
405 struct gui_data *inst = (struct gui_data *)frontend;
406 term_invalidate(inst->term);
410 * Maximise or restore the window in response to a server-side
413 void set_zoomed(void *frontend, int zoomed)
416 * GTK 1.2 doesn't know how to do this.
418 #if GTK_CHECK_VERSION(2,0,0)
419 struct gui_data *inst = (struct gui_data *)frontend;
421 gtk_window_maximize(GTK_WINDOW(inst->window));
423 gtk_window_unmaximize(GTK_WINDOW(inst->window));
428 * Report whether the window is iconic, for terminal reports.
430 int is_iconic(void *frontend)
432 struct gui_data *inst = (struct gui_data *)frontend;
433 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
437 * Report the window's position, for terminal reports.
439 void get_window_pos(void *frontend, int *x, int *y)
441 struct gui_data *inst = (struct gui_data *)frontend;
443 * I assume that when the GTK version of this call is available
444 * we should use it. Not sure how it differs from the GDK one,
447 #if GTK_CHECK_VERSION(2,0,0)
448 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
450 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
455 * Report the window's pixel size, for terminal reports.
457 void get_window_pixels(void *frontend, int *x, int *y)
459 struct gui_data *inst = (struct gui_data *)frontend;
461 * I assume that when the GTK version of this call is available
462 * we should use it. Not sure how it differs from the GDK one,
465 #if GTK_CHECK_VERSION(2,0,0)
466 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
468 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
473 * Return the window or icon title.
475 char *get_window_title(void *frontend, int icon)
477 struct gui_data *inst = (struct gui_data *)frontend;
478 return icon ? inst->icontitle : inst->wintitle;
481 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
483 struct gui_data *inst = (struct gui_data *)data;
484 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
485 if (!reallyclose(inst))
491 static void update_mouseptr(struct gui_data *inst)
493 switch (inst->busy_status) {
495 if (!inst->mouseptr_visible) {
496 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
498 } else if (send_raw_mouse) {
499 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
502 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
506 case BUSY_WAITING: /* XXX can we do better? */
508 /* We always display these cursors. */
509 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
517 static void show_mouseptr(struct gui_data *inst, int show)
519 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
521 inst->mouseptr_visible = show;
522 update_mouseptr(inst);
525 static void draw_backing_rect(struct gui_data *inst);
527 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
529 struct gui_data *inst = (struct gui_data *)data;
530 int w, h, need_size = 0;
533 * See if the terminal size has changed, in which case we must
534 * let the terminal know.
536 w = (event->width - 2*inst->window_border) / inst->font_width;
537 h = (event->height - 2*inst->window_border) / inst->font_height;
538 if (w != inst->width || h != inst->height) {
541 conf_set_int(inst->conf, CONF_width, inst->width);
542 conf_set_int(inst->conf, CONF_height, inst->height);
547 int backing_w = w * inst->font_width + 2*inst->window_border;
548 int backing_h = h * inst->font_height + 2*inst->window_border;
550 #ifndef NO_BACKING_PIXMAPS
552 gdk_pixmap_unref(inst->pixmap);
556 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
557 backing_w, backing_h, -1);
560 #ifdef DRAW_TEXT_CAIRO
562 cairo_surface_destroy(inst->surface);
563 inst->surface = NULL;
566 inst->surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
567 backing_w, backing_h);
571 draw_backing_rect(inst);
573 if (need_size && inst->term) {
574 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
578 term_invalidate(inst->term);
580 #if GTK_CHECK_VERSION(2,0,0)
581 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
587 #ifdef DRAW_TEXT_CAIRO
588 static void cairo_setup_dctx(struct draw_ctx *dctx)
590 cairo_get_matrix(dctx->uctx.u.cairo.cr,
591 &dctx->uctx.u.cairo.origmatrix);
592 cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
593 cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
594 cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
595 /* This antialiasing setting appears to be ignored for Pango
596 * font rendering but honoured for stroking and filling paths;
597 * I don't quite understand the logic of that, but I won't
598 * complain since it's exactly what I happen to want */
599 cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
603 #if GTK_CHECK_VERSION(3,0,0)
604 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
606 struct gui_data *inst = (struct gui_data *)data;
609 * GTK3 window redraw: we always expect Cairo to be enabled, so
610 * that inst->surface exists, and pixmaps to be disabled, so that
611 * inst->pixmap does not exist. Hence, we just blit from
612 * inst->surface to the window.
615 GdkRectangle dirtyrect;
617 gdk_cairo_get_clip_rectangle(cr, &dirtyrect);
619 cairo_set_source_surface(cr, inst->surface, 0, 0);
620 cairo_rectangle(cr, dirtyrect.x, dirtyrect.y,
621 dirtyrect.width, dirtyrect.height);
628 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
630 struct gui_data *inst = (struct gui_data *)data;
632 #ifndef NO_BACKING_PIXMAPS
634 * Draw to the exposed part of the window from the server-side
638 gdk_draw_pixmap(gtk_widget_get_window(widget),
639 (gtk_widget_get_style(widget)->fg_gc
640 [gtk_widget_get_state(widget)]),
642 event->area.x, event->area.y,
643 event->area.x, event->area.y,
644 event->area.width, event->area.height);
648 * Failing that, draw from the client-side Cairo surface. (We
649 * should never be compiled in a context where we have _neither_
650 * inst->surface nor inst->pixmap.)
653 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
654 cairo_set_source_surface(cr, inst->surface, 0, 0);
655 cairo_rectangle(cr, event->area.x, event->area.y,
656 event->area.width, event->area.height);
666 #define KEY_PRESSED(k) \
667 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
669 #ifdef KEY_EVENT_DIAGNOSTICS
670 char *dup_keyval_name(guint keyval)
672 const char *name = gdk_keyval_name(keyval);
676 return dupprintf("UNKNOWN[%u]", (unsigned)keyval);
680 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
682 struct gui_data *inst = (struct gui_data *)data;
684 wchar_t ucsoutput[2];
685 int ucsval, start, end, special, output_charset, use_ucsoutput;
686 int nethack_mode, app_keypad_mode;
688 /* Remember the timestamp. */
689 inst->input_event_time = event->time;
691 /* By default, nothing is generated. */
693 special = use_ucsoutput = FALSE;
694 output_charset = CS_ISO8859_1;
696 #ifdef KEY_EVENT_DIAGNOSTICS
698 char *type_string, *state_string, *keyval_string, *string_string;
700 type_string = (event->type == GDK_KEY_PRESS ? dupstr("PRESS") :
701 event->type == GDK_KEY_RELEASE ? dupstr("RELEASE") :
702 dupprintf("UNKNOWN[%d]", (int)event->type));
705 static const struct {
709 {GDK_SHIFT_MASK, "SHIFT"},
710 {GDK_LOCK_MASK, "LOCK"},
711 {GDK_CONTROL_MASK, "CONTROL"},
712 {GDK_MOD1_MASK, "MOD1"},
713 {GDK_MOD2_MASK, "MOD2"},
714 {GDK_MOD3_MASK, "MOD3"},
715 {GDK_MOD4_MASK, "MOD4"},
716 {GDK_MOD5_MASK, "MOD5"},
717 {GDK_SUPER_MASK, "SUPER"},
718 {GDK_HYPER_MASK, "HYPER"},
719 {GDK_META_MASK, "META"},
722 int val = event->state;
724 state_string = dupstr("");
726 for (i = 0; i < lenof(mod_bits); i++) {
727 if (val & mod_bits[i].mod_bit) {
728 char *old = state_string;
729 state_string = dupcat(state_string,
730 state_string[0] ? "|" : "",
735 val &= ~mod_bits[i].mod_bit;
739 if (val || !state_string[0]) {
740 char *old = state_string;
741 state_string = dupprintf("%s%s%d", state_string,
742 state_string[0] ? "|" : "", val);
747 keyval_string = dup_keyval_name(event->keyval);
749 string_string = dupstr("");
752 for (i = 0; event->string[i]; i++) {
753 char *old = string_string;
754 string_string = dupprintf("%s%s%02x", string_string,
755 string_string[0] ? " " : "",
756 (unsigned)event->string[i] & 0xFF);
761 debug(("key_event: type=%s keyval=%s state=%s "
762 "hardware_keycode=%d is_modifier=%s string=[%s]\n",
763 type_string, keyval_string, state_string,
764 (int)event->hardware_keycode,
765 event->is_modifier ? "TRUE" : "FALSE",
770 sfree(keyval_string);
771 sfree(string_string);
773 #endif /* KEY_EVENT_DIAGNOSTICS */
776 * If Alt is being released after typing an Alt+numberpad
777 * sequence, we should generate the code that was typed.
779 * Note that we only do this if more than one key was actually
780 * pressed - I don't think Alt+NumPad4 should be ^D or that
781 * Alt+NumPad3 should be ^C, for example. There's no serious
782 * inconvenience in having to type a zero before a single-digit
785 if (event->type == GDK_KEY_RELEASE) {
786 if ((event->keyval == GDK_KEY_Meta_L ||
787 event->keyval == GDK_KEY_Meta_R ||
788 event->keyval == GDK_KEY_Alt_L ||
789 event->keyval == GDK_KEY_Alt_R) &&
790 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
791 #ifdef KEY_EVENT_DIAGNOSTICS
792 debug((" - modifier release terminates Alt+numberpad input, "
793 "keycode = %d\n", inst->alt_keycode));
796 * FIXME: we might usefully try to do something clever here
797 * about interpreting the generated key code in a way that's
798 * appropriate to the line code page.
800 output[0] = inst->alt_keycode;
804 #if GTK_CHECK_VERSION(2,0,0)
805 #ifdef KEY_EVENT_DIAGNOSTICS
806 debug((" - key release, passing to IM\n"));
808 if (gtk_im_context_filter_keypress(inst->imc, event)) {
809 #ifdef KEY_EVENT_DIAGNOSTICS
810 debug((" - key release accepted by IM\n"));
814 #ifdef KEY_EVENT_DIAGNOSTICS
815 debug((" - key release not accepted by IM\n"));
821 if (event->type == GDK_KEY_PRESS) {
823 * NYI: Compose key (!!! requires Unicode faff before even trying)
827 * If Alt has just been pressed, we start potentially
828 * accumulating an Alt+numberpad code. We do this by
829 * setting alt_keycode to -1 (nothing yet but plausible).
831 if ((event->keyval == GDK_KEY_Meta_L ||
832 event->keyval == GDK_KEY_Meta_R ||
833 event->keyval == GDK_KEY_Alt_L ||
834 event->keyval == GDK_KEY_Alt_R)) {
835 #ifdef KEY_EVENT_DIAGNOSTICS
836 debug((" - modifier press potentially begins Alt+numberpad "
839 inst->alt_keycode = -1;
840 inst->alt_digits = 0;
841 goto done; /* this generates nothing else */
845 * If we're seeing a numberpad key press with Meta down,
846 * consider adding it to alt_keycode if that's sensible.
847 * Anything _else_ with Meta down cancels any possibility
848 * of an ALT keycode: we set alt_keycode to -2.
850 if ((event->state & inst->meta_mod_mask) && inst->alt_keycode != -2) {
852 switch (event->keyval) {
853 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
854 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
855 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
856 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
857 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
858 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
859 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
860 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
861 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
862 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
865 inst->alt_keycode = -2; /* it's invalid */
867 #ifdef KEY_EVENT_DIAGNOSTICS
868 int old_keycode = inst->alt_keycode;
870 if (inst->alt_keycode == -1)
871 inst->alt_keycode = digit; /* one-digit code */
873 inst->alt_keycode = inst->alt_keycode * 10 + digit;
875 #ifdef KEY_EVENT_DIAGNOSTICS
876 debug((" - Alt+numberpad digit %d added to keycode %d"
877 " gives %d\n", digit, old_keycode, inst->alt_keycode));
879 /* Having used this digit, we now do nothing more with it. */
885 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
888 if (event->keyval == GDK_KEY_Page_Up &&
889 (event->state & GDK_SHIFT_MASK)) {
890 #ifdef KEY_EVENT_DIAGNOSTICS
891 debug((" - Shift-PgUp scroll\n"));
893 term_scroll(inst->term, 0, -inst->height/2);
896 if (event->keyval == GDK_KEY_Page_Up &&
897 (event->state & GDK_CONTROL_MASK)) {
898 #ifdef KEY_EVENT_DIAGNOSTICS
899 debug((" - Ctrl-PgUp scroll\n"));
901 term_scroll(inst->term, 0, -1);
904 if (event->keyval == GDK_KEY_Page_Down &&
905 (event->state & GDK_SHIFT_MASK)) {
906 #ifdef KEY_EVENT_DIAGNOSTICS
907 debug((" - Shift-PgDn scroll\n"));
909 term_scroll(inst->term, 0, +inst->height/2);
912 if (event->keyval == GDK_KEY_Page_Down &&
913 (event->state & GDK_CONTROL_MASK)) {
914 #ifdef KEY_EVENT_DIAGNOSTICS
915 debug((" - Ctrl-PgDn scroll\n"));
917 term_scroll(inst->term, 0, +1);
922 * Neither does Shift-Ins.
924 if (event->keyval == GDK_KEY_Insert &&
925 (event->state & GDK_SHIFT_MASK)) {
926 #ifdef KEY_EVENT_DIAGNOSTICS
927 debug((" - Shift-Insert paste\n"));
934 use_ucsoutput = FALSE;
936 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
937 app_keypad_mode = (inst->term->app_keypad_keys &&
938 !conf_get_int(inst->conf, CONF_no_applic_k));
940 /* ALT+things gives leading Escape. */
942 #if !GTK_CHECK_VERSION(2,0,0)
944 * In vanilla X, and hence also GDK 1.2, the string received
945 * as part of a keyboard event is assumed to be in
946 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
947 * but it's true: see the man page for XLookupString(3) for
950 output_charset = CS_ISO8859_1;
951 strncpy(output+1, event->string, lenof(output)-1);
952 #else /* !GTK_CHECK_VERSION(2,0,0) */
954 * Most things can now be passed to
955 * gtk_im_context_filter_keypress without breaking anything
956 * below this point. An exception is the numeric keypad if
957 * we're in Nethack or application mode: the IM will eat
958 * numeric keypad presses if Num Lock is on, but we don't want
961 if (app_keypad_mode &&
962 (event->keyval == GDK_KEY_Num_Lock ||
963 event->keyval == GDK_KEY_KP_Divide ||
964 event->keyval == GDK_KEY_KP_Multiply ||
965 event->keyval == GDK_KEY_KP_Subtract ||
966 event->keyval == GDK_KEY_KP_Add ||
967 event->keyval == GDK_KEY_KP_Enter ||
968 event->keyval == GDK_KEY_KP_0 ||
969 event->keyval == GDK_KEY_KP_Insert ||
970 event->keyval == GDK_KEY_KP_1 ||
971 event->keyval == GDK_KEY_KP_End ||
972 event->keyval == GDK_KEY_KP_2 ||
973 event->keyval == GDK_KEY_KP_Down ||
974 event->keyval == GDK_KEY_KP_3 ||
975 event->keyval == GDK_KEY_KP_Page_Down ||
976 event->keyval == GDK_KEY_KP_4 ||
977 event->keyval == GDK_KEY_KP_Left ||
978 event->keyval == GDK_KEY_KP_5 ||
979 event->keyval == GDK_KEY_KP_Begin ||
980 event->keyval == GDK_KEY_KP_6 ||
981 event->keyval == GDK_KEY_KP_Right ||
982 event->keyval == GDK_KEY_KP_7 ||
983 event->keyval == GDK_KEY_KP_Home ||
984 event->keyval == GDK_KEY_KP_8 ||
985 event->keyval == GDK_KEY_KP_Up ||
986 event->keyval == GDK_KEY_KP_9 ||
987 event->keyval == GDK_KEY_KP_Page_Up ||
988 event->keyval == GDK_KEY_KP_Decimal ||
989 event->keyval == GDK_KEY_KP_Delete)) {
990 /* app keypad; do nothing */
991 } else if (nethack_mode &&
992 (event->keyval == GDK_KEY_KP_1 ||
993 event->keyval == GDK_KEY_KP_End ||
994 event->keyval == GDK_KEY_KP_2 ||
995 event->keyval == GDK_KEY_KP_Down ||
996 event->keyval == GDK_KEY_KP_3 ||
997 event->keyval == GDK_KEY_KP_Page_Down ||
998 event->keyval == GDK_KEY_KP_4 ||
999 event->keyval == GDK_KEY_KP_Left ||
1000 event->keyval == GDK_KEY_KP_5 ||
1001 event->keyval == GDK_KEY_KP_Begin ||
1002 event->keyval == GDK_KEY_KP_6 ||
1003 event->keyval == GDK_KEY_KP_Right ||
1004 event->keyval == GDK_KEY_KP_7 ||
1005 event->keyval == GDK_KEY_KP_Home ||
1006 event->keyval == GDK_KEY_KP_8 ||
1007 event->keyval == GDK_KEY_KP_Up ||
1008 event->keyval == GDK_KEY_KP_9 ||
1009 event->keyval == GDK_KEY_KP_Page_Up)) {
1010 /* nethack mode; do nothing */
1012 int try_filter = TRUE;
1014 #ifdef META_MANUAL_MASK
1015 if (event->state & META_MANUAL_MASK & inst->meta_mod_mask) {
1017 * If this key event had a Meta modifier bit set which
1018 * is also in META_MANUAL_MASK, that means passing
1019 * such an event to the GtkIMContext will be unhelpful
1020 * (it will eat the keystroke and turn it into
1021 * something not what we wanted).
1023 #ifdef KEY_EVENT_DIAGNOSTICS
1024 debug((" - Meta modifier requiring manual intervention, "
1025 "suppressing IM filtering\n"));
1032 #ifdef KEY_EVENT_DIAGNOSTICS
1033 debug((" - general key press, passing to IM\n"));
1035 if (gtk_im_context_filter_keypress(inst->imc, event)) {
1036 #ifdef KEY_EVENT_DIAGNOSTICS
1037 debug((" - key press accepted by IM\n"));
1041 #ifdef KEY_EVENT_DIAGNOSTICS
1042 debug((" - key press not accepted by IM\n"));
1049 * GDK 2.0 arranges to have done some translation for us: in
1050 * GDK 2.0, event->string is encoded in the current locale.
1052 * So we use the standard C library function mbstowcs() to
1053 * convert from the current locale into Unicode; from there
1054 * we can convert to whatever PuTTY is currently working in.
1055 * (In fact I convert straight back to UTF-8 from
1056 * wide-character Unicode, for the sake of simplicity: that
1057 * way we can still use exactly the same code to manipulate
1058 * the string, such as prefixing ESC.)
1060 output_charset = CS_UTF8;
1062 wchar_t widedata[32];
1067 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
1068 event->string, strlen(event->string),
1069 widedata, lenof(widedata)-1);
1071 #ifdef KEY_EVENT_DIAGNOSTICS
1073 char *string_string = dupstr("");
1076 for (i = 0; i < wlen; i++) {
1077 char *old = string_string;
1078 string_string = dupprintf("%s%s%04x", string_string,
1079 string_string[0] ? " " : "",
1080 (unsigned)widedata[i]);
1083 debug((" - string translated into Unicode = [%s]\n",
1085 sfree(string_string);
1090 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
1091 CS_UTF8, NULL, NULL, 0);
1093 #ifdef KEY_EVENT_DIAGNOSTICS
1095 char *string_string = dupstr("");
1098 for (i = 0; i < ulen; i++) {
1099 char *old = string_string;
1100 string_string = dupprintf("%s%s%02x", string_string,
1101 string_string[0] ? " " : "",
1102 (unsigned)output[i+1] & 0xFF);
1105 debug((" - string translated into UTF-8 = [%s]\n",
1107 sfree(string_string);
1111 output[1+ulen] = '\0';
1113 #endif /* !GTK_CHECK_VERSION(2,0,0) */
1116 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
1117 ucsoutput[0] = '\033';
1118 ucsoutput[1] = ucsval;
1119 #ifdef KEY_EVENT_DIAGNOSTICS
1120 debug((" - keysym_to_unicode gave %04x\n",
1121 (unsigned)ucsoutput[1]));
1123 use_ucsoutput = TRUE;
1126 output[lenof(output)-1] = '\0';
1127 end = strlen(output);
1129 if (event->state & inst->meta_mod_mask) {
1131 if (end == 1) end = 0;
1133 #ifdef META_MANUAL_MASK
1134 if (event->state & META_MANUAL_MASK) {
1136 * Key events which have a META_MANUAL_MASK meta bit
1137 * set may have a keyval reflecting that, e.g. on OS X
1138 * the Option key acts as an AltGr-like modifier and
1139 * causes different Unicode characters to be output.
1141 * To work around this, we clear the dangerous
1142 * modifier bit and retranslate from the hardware
1143 * keycode as if the key had been pressed without that
1144 * modifier. Then we prefix Esc to *that*.
1147 GdkModifierType consumed;
1148 if (gdk_keymap_translate_keyboard_state
1149 (gdk_keymap_get_for_display(gdk_display_get_default()),
1150 event->hardware_keycode, event->state & ~META_MANUAL_MASK,
1151 0, &new_keyval, NULL, NULL, &consumed)) {
1152 ucsoutput[0] = '\033';
1153 ucsoutput[1] = gdk_keyval_to_unicode(new_keyval);
1154 #ifdef KEY_EVENT_DIAGNOSTICS
1156 char *keyval_name = dup_keyval_name(new_keyval);
1157 debug((" - retranslation for manual Meta: "
1158 "new keyval = %s, Unicode = %04x\n",
1159 keyval_name, (unsigned)ucsoutput[1]));
1163 use_ucsoutput = TRUE;
1171 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
1172 if (!output[1] && event->keyval == '`' &&
1173 (event->state & GDK_CONTROL_MASK)) {
1174 #ifdef KEY_EVENT_DIAGNOSTICS
1175 debug((" - Ctrl-` special case, translating as 1c\n"));
1178 use_ucsoutput = FALSE;
1182 /* Some GTK backends (e.g. Quartz) do not change event->string
1183 * in response to the Control modifier. So we do it ourselves
1184 * here, if it's not already happened.
1186 * The translations below are in line with X11 policy as far
1188 if ((event->state & GDK_CONTROL_MASK) && end == 2) {
1189 #ifdef KEY_EVENT_DIAGNOSTICS
1190 int orig = output[1];
1193 if (output[1] >= '3' && output[1] <= '7') {
1194 /* ^3,...,^7 map to 0x1B,...,0x1F */
1195 output[1] += '\x1B' - '3';
1196 } else if (output[1] == '2' || output[1] == ' ') {
1197 /* ^2 and ^Space are both ^@, i.e. \0 */
1199 } else if (output[1] == '8') {
1202 } else if (output[1] == '/') {
1203 /* ^/ is the same as ^_ */
1205 } else if (output[1] >= 0x40 && output[1] < 0x7F) {
1206 /* Everything anywhere near the alphabetics just gets
1210 /* Anything else, e.g. '0', is unchanged. */
1212 #ifdef KEY_EVENT_DIAGNOSTICS
1213 if (orig == output[1])
1214 debug((" - manual Ctrl key handling did nothing\n"));
1216 debug((" - manual Ctrl key handling: %02x -> %02x\n",
1217 (unsigned)orig, (unsigned)output[1]));
1221 /* Control-Break sends a Break special to the backend */
1222 if (event->keyval == GDK_KEY_Break &&
1223 (event->state & GDK_CONTROL_MASK)) {
1224 #ifdef KEY_EVENT_DIAGNOSTICS
1225 debug((" - Ctrl-Break special case, sending TS_BRK\n"));
1228 inst->back->special(inst->backhandle, TS_BRK);
1232 /* We handle Return ourselves, because it needs to be flagged as
1233 * special to ldisc. */
1234 if (event->keyval == GDK_KEY_Return) {
1235 #ifdef KEY_EVENT_DIAGNOSTICS
1236 debug((" - Return special case, translating as 0d + special\n"));
1239 use_ucsoutput = FALSE;
1244 /* Control-2, Control-Space and Control-@ are NUL */
1246 (event->keyval == ' ' || event->keyval == '2' ||
1247 event->keyval == '@') &&
1248 (event->state & (GDK_SHIFT_MASK |
1249 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
1250 #ifdef KEY_EVENT_DIAGNOSTICS
1251 debug((" - Ctrl-{space,2,@} special case, translating as 00\n"));
1254 use_ucsoutput = FALSE;
1258 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
1259 if (!output[1] && event->keyval == ' ' &&
1260 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
1261 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
1262 #ifdef KEY_EVENT_DIAGNOSTICS
1263 debug((" - Ctrl-Shift-space special case, translating as 00a0\n"));
1266 output_charset = CS_ISO8859_1;
1267 use_ucsoutput = FALSE;
1271 /* We don't let GTK tell us what Backspace is! We know better. */
1272 if (event->keyval == GDK_KEY_BackSpace &&
1273 !(event->state & GDK_SHIFT_MASK)) {
1274 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1276 #ifdef KEY_EVENT_DIAGNOSTICS
1277 debug((" - Backspace, translating as %02x\n",
1278 (unsigned)output[1]));
1280 use_ucsoutput = FALSE;
1284 /* For Shift Backspace, do opposite of what is configured. */
1285 if (event->keyval == GDK_KEY_BackSpace &&
1286 (event->state & GDK_SHIFT_MASK)) {
1287 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1289 #ifdef KEY_EVENT_DIAGNOSTICS
1290 debug((" - Shift-Backspace, translating as %02x\n",
1291 (unsigned)output[1]));
1293 use_ucsoutput = FALSE;
1298 /* Shift-Tab is ESC [ Z */
1299 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
1300 (event->keyval == GDK_KEY_Tab &&
1301 (event->state & GDK_SHIFT_MASK))) {
1302 #ifdef KEY_EVENT_DIAGNOSTICS
1303 debug((" - Shift-Tab, translating as ESC [ Z\n"));
1305 end = 1 + sprintf(output+1, "\033[Z");
1306 use_ucsoutput = FALSE;
1308 /* And normal Tab is Tab, if the keymap hasn't already told us.
1309 * (Curiously, at least one version of the MacOS 10.5 X server
1310 * doesn't translate Tab for us. */
1311 if (event->keyval == GDK_KEY_Tab && end <= 1) {
1312 #ifdef KEY_EVENT_DIAGNOSTICS
1313 debug((" - Tab, translating as 09\n"));
1320 * NetHack keypad mode.
1323 const char *keys = NULL;
1324 switch (event->keyval) {
1325 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
1326 keys = "bB\002"; break;
1327 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
1328 keys = "jJ\012"; break;
1329 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
1330 keys = "nN\016"; break;
1331 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
1332 keys = "hH\010"; break;
1333 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
1334 keys = "..."; break;
1335 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
1336 keys = "lL\014"; break;
1337 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
1338 keys = "yY\031"; break;
1339 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
1340 keys = "kK\013"; break;
1341 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
1342 keys = "uU\025"; break;
1346 if (event->state & GDK_CONTROL_MASK)
1347 output[1] = keys[2];
1348 else if (event->state & GDK_SHIFT_MASK)
1349 output[1] = keys[1];
1351 output[1] = keys[0];
1352 #ifdef KEY_EVENT_DIAGNOSTICS
1353 debug((" - Nethack-mode key"));
1355 use_ucsoutput = FALSE;
1361 * Application keypad mode.
1363 if (app_keypad_mode) {
1365 switch (event->keyval) {
1366 case GDK_KEY_Num_Lock: xkey = 'P'; break;
1367 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
1368 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
1369 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
1371 * Keypad + is tricky. It covers a space that would
1372 * be taken up on the VT100 by _two_ keys; so we
1373 * let Shift select between the two. Worse still,
1374 * in xterm function key mode we change which two...
1376 case GDK_KEY_KP_Add:
1377 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
1378 if (event->state & GDK_SHIFT_MASK)
1382 } else if (event->state & GDK_SHIFT_MASK)
1387 case GDK_KEY_KP_Enter: xkey = 'M'; break;
1388 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
1389 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
1390 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
1391 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
1392 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
1393 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
1394 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
1395 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
1396 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
1397 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
1398 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
1402 if (inst->term->vt52_mode) {
1403 if (xkey >= 'P' && xkey <= 'S')
1404 end = 1 + sprintf(output+1, "\033%c", xkey);
1406 end = 1 + sprintf(output+1, "\033?%c", xkey);
1408 end = 1 + sprintf(output+1, "\033O%c", xkey);
1409 use_ucsoutput = FALSE;
1410 #ifdef KEY_EVENT_DIAGNOSTICS
1411 debug((" - Application keypad mode key"));
1418 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
1419 * for integer decimal nn.)
1421 * We also deal with the weird ones here. Linux VCs replace F1
1422 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1423 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1428 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1429 switch (event->keyval) {
1431 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1434 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1437 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1440 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1443 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1446 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1449 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1452 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1455 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1458 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1491 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1492 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1495 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1498 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1501 case GDK_KEY_End: case GDK_KEY_KP_End:
1504 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1507 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1511 /* Reorder edit keys to physical order */
1512 if (funky_type == FUNKY_VT400 && code <= 6)
1513 code = "\0\2\1\4\5\3\6"[code];
1515 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1516 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1517 #ifdef KEY_EVENT_DIAGNOSTICS
1518 debug((" - VT52 mode small keypad key"));
1520 use_ucsoutput = FALSE;
1524 if (funky_type == FUNKY_SCO && /* SCO function keys */
1525 code >= 11 && code <= 34) {
1526 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1528 switch (event->keyval) {
1529 case GDK_KEY_F1: index = 0; break;
1530 case GDK_KEY_F2: index = 1; break;
1531 case GDK_KEY_F3: index = 2; break;
1532 case GDK_KEY_F4: index = 3; break;
1533 case GDK_KEY_F5: index = 4; break;
1534 case GDK_KEY_F6: index = 5; break;
1535 case GDK_KEY_F7: index = 6; break;
1536 case GDK_KEY_F8: index = 7; break;
1537 case GDK_KEY_F9: index = 8; break;
1538 case GDK_KEY_F10: index = 9; break;
1539 case GDK_KEY_F11: index = 10; break;
1540 case GDK_KEY_F12: index = 11; break;
1542 if (event->state & GDK_SHIFT_MASK) index += 12;
1543 if (event->state & GDK_CONTROL_MASK) index += 24;
1544 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1545 #ifdef KEY_EVENT_DIAGNOSTICS
1546 debug((" - SCO mode function key"));
1548 use_ucsoutput = FALSE;
1551 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1552 code >= 1 && code <= 6) {
1553 char codes[] = "HL.FIG";
1558 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1560 #ifdef KEY_EVENT_DIAGNOSTICS
1561 debug((" - SCO mode small keypad key"));
1563 use_ucsoutput = FALSE;
1566 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1567 code >= 11 && code <= 24) {
1573 if (inst->term->vt52_mode) {
1574 #ifdef KEY_EVENT_DIAGNOSTICS
1575 debug((" - VT52 mode function key"));
1577 end = 1 + sprintf(output+1,
1578 "\x1B%c", code + 'P' - 11 - offt);
1580 #ifdef KEY_EVENT_DIAGNOSTICS
1581 debug((" - VT100+ mode function key"));
1583 end = 1 + sprintf(output+1,
1584 "\x1BO%c", code + 'P' - 11 - offt);
1586 use_ucsoutput = FALSE;
1589 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1590 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1591 #ifdef KEY_EVENT_DIAGNOSTICS
1592 debug((" - Linux mode F1-F5 function key"));
1594 use_ucsoutput = FALSE;
1597 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1598 if (inst->term->vt52_mode) {
1599 #ifdef KEY_EVENT_DIAGNOSTICS
1600 debug((" - VT52 mode (overriding xterm) F1-F4 function"
1603 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1605 #ifdef KEY_EVENT_DIAGNOSTICS
1606 debug((" - xterm mode F1-F4 function key"));
1608 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1610 use_ucsoutput = FALSE;
1613 if ((code == 1 || code == 4) &&
1614 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1615 #ifdef KEY_EVENT_DIAGNOSTICS
1616 debug((" - rxvt style Home/End"));
1618 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1619 use_ucsoutput = FALSE;
1623 #ifdef KEY_EVENT_DIAGNOSTICS
1624 debug((" - ordinary function key encoding"));
1626 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1627 use_ucsoutput = FALSE;
1633 * Cursor keys. (This includes the numberpad cursor keys,
1634 * if we haven't already done them due to app keypad mode.)
1636 * Here we also process un-numlocked un-appkeypadded KP5,
1637 * which sends ESC [ G.
1641 switch (event->keyval) {
1642 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1643 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1644 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1645 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1646 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1649 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1650 event->state & GDK_CONTROL_MASK);
1651 #ifdef KEY_EVENT_DIAGNOSTICS
1652 debug((" - arrow key"));
1654 use_ucsoutput = FALSE;
1663 if (end-start > 0) {
1665 #ifdef KEY_EVENT_DIAGNOSTICS
1666 char *string_string = dupstr("");
1669 for (i = start; i < end; i++) {
1670 char *old = string_string;
1671 string_string = dupprintf("%s%s%02x", string_string,
1672 string_string[0] ? " " : "",
1673 (unsigned)output[i] & 0xFF);
1676 debug((" - final output, special, generic encoding = [%s]\n",
1677 charset_to_localenc(output_charset), string_string));
1678 sfree(string_string);
1681 * For special control characters, the character set
1682 * should never matter.
1684 output[end] = '\0'; /* NUL-terminate */
1686 ldisc_send(inst->ldisc, output+start, -2, 1);
1687 } else if (!inst->direct_to_font) {
1688 if (!use_ucsoutput) {
1689 #ifdef KEY_EVENT_DIAGNOSTICS
1690 char *string_string = dupstr("");
1693 for (i = start; i < end; i++) {
1694 char *old = string_string;
1695 string_string = dupprintf("%s%s%02x", string_string,
1696 string_string[0] ? " " : "",
1697 (unsigned)output[i] & 0xFF);
1700 debug((" - final output in %s = [%s]\n",
1701 charset_to_localenc(output_charset), string_string));
1702 sfree(string_string);
1705 lpage_send(inst->ldisc, output_charset, output+start,
1708 #ifdef KEY_EVENT_DIAGNOSTICS
1709 char *string_string = dupstr("");
1712 for (i = start; i < end; i++) {
1713 char *old = string_string;
1714 string_string = dupprintf("%s%s%04x", string_string,
1715 string_string[0] ? " " : "",
1716 (unsigned)ucsoutput[i]);
1719 debug((" - final output in Unicode = [%s]\n",
1721 sfree(string_string);
1725 * We generated our own Unicode key data from the
1726 * keysym, so use that instead.
1729 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1733 * In direct-to-font mode, we just send the string
1734 * exactly as we received it.
1736 #ifdef KEY_EVENT_DIAGNOSTICS
1737 char *string_string = dupstr("");
1740 for (i = start; i < end; i++) {
1741 char *old = string_string;
1742 string_string = dupprintf("%s%s%02x", string_string,
1743 string_string[0] ? " " : "",
1744 (unsigned)output[i] & 0xFF);
1747 debug((" - final output in direct-to-font encoding = [%s]\n",
1749 sfree(string_string);
1752 ldisc_send(inst->ldisc, output+start, end-start, 1);
1755 show_mouseptr(inst, 0);
1756 term_seen_key_event(inst->term);
1762 #if GTK_CHECK_VERSION(2,0,0)
1763 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1765 struct gui_data *inst = (struct gui_data *)data;
1767 #ifdef KEY_EVENT_DIAGNOSTICS
1768 char *string_string = dupstr("");
1771 for (i = 0; str[i]; i++) {
1772 char *old = string_string;
1773 string_string = dupprintf("%s%s%02x", string_string,
1774 string_string[0] ? " " : "",
1775 (unsigned)str[i] & 0xFF);
1778 debug((" - IM commit event in UTF-8 = [%s]\n", string_string));
1779 sfree(string_string);
1783 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1784 show_mouseptr(inst, 0);
1785 term_seen_key_event(inst->term);
1789 #define SCROLL_INCREMENT_LINES 5
1791 #if GTK_CHECK_VERSION(3,4,0)
1792 gboolean scroll_internal(struct gui_data *inst, gdouble delta, guint state,
1793 gdouble ex, gdouble ey)
1795 int shift, ctrl, alt, x, y, raw_mouse_mode;
1797 show_mouseptr(inst, 1);
1799 shift = state & GDK_SHIFT_MASK;
1800 ctrl = state & GDK_CONTROL_MASK;
1801 alt = state & inst->meta_mod_mask;
1803 x = (ex - inst->window_border) / inst->font_width;
1804 y = (ey - inst->window_border) / inst->font_height;
1807 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1808 CONF_mouse_override));
1810 inst->cumulative_scroll += delta * SCROLL_INCREMENT_LINES;
1812 if (!raw_mouse_mode) {
1813 int scroll_lines = (int)inst->cumulative_scroll; /* rounds toward 0 */
1815 term_scroll(inst->term, 0, scroll_lines);
1816 inst->cumulative_scroll -= scroll_lines;
1820 int scroll_events = (int)(inst->cumulative_scroll /
1821 SCROLL_INCREMENT_LINES);
1822 if (scroll_events) {
1825 inst->cumulative_scroll -= scroll_events * SCROLL_INCREMENT_LINES;
1827 if (scroll_events > 0) {
1828 button = MBT_WHEEL_DOWN;
1830 button = MBT_WHEEL_UP;
1831 scroll_events = -scroll_events;
1834 while (scroll_events-- > 0) {
1835 term_mouse(inst->term, button, translate_button(button),
1836 MA_CLICK, x, y, shift, ctrl, alt);
1844 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1845 GdkEventType type, guint ebutton, guint state,
1846 gdouble ex, gdouble ey)
1848 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1850 /* Remember the timestamp. */
1851 inst->input_event_time = timestamp;
1853 show_mouseptr(inst, 1);
1855 shift = state & GDK_SHIFT_MASK;
1856 ctrl = state & GDK_CONTROL_MASK;
1857 alt = state & inst->meta_mod_mask;
1860 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1861 CONF_mouse_override));
1863 if (!raw_mouse_mode) {
1864 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1865 term_scroll(inst->term, 0, -SCROLL_INCREMENT_LINES);
1868 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1869 term_scroll(inst->term, 0, +SCROLL_INCREMENT_LINES);
1874 if (ebutton == 3 && ctrl) {
1875 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1876 ebutton, timestamp);
1882 else if (ebutton == 2)
1883 button = MBT_MIDDLE;
1884 else if (ebutton == 3)
1886 else if (ebutton == 4)
1887 button = MBT_WHEEL_UP;
1888 else if (ebutton == 5)
1889 button = MBT_WHEEL_DOWN;
1891 return FALSE; /* don't even know what button! */
1894 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1895 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1896 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1897 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1898 default: return FALSE; /* don't know this event type */
1901 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1902 return TRUE; /* we ignore these in raw mouse mode */
1904 x = (ex - inst->window_border) / inst->font_width;
1905 y = (ey - inst->window_border) / inst->font_height;
1907 term_mouse(inst->term, button, translate_button(button), act,
1908 x, y, shift, ctrl, alt);
1913 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1915 struct gui_data *inst = (struct gui_data *)data;
1916 return button_internal(inst, event->time, event->type, event->button,
1917 event->state, event->x, event->y);
1920 #if GTK_CHECK_VERSION(2,0,0)
1922 * In GTK 2, mouse wheel events have become a new type of event.
1923 * This handler translates them back into button-4 and button-5
1924 * presses so that I don't have to change my old code too much :-)
1926 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1928 struct gui_data *inst = (struct gui_data *)data;
1930 #if GTK_CHECK_VERSION(3,4,0)
1932 if (gdk_event_get_scroll_deltas((GdkEvent *)event, &dx, &dy)) {
1933 return scroll_internal(inst, dy, event->state, event->x, event->y);
1938 if (event->direction == GDK_SCROLL_UP)
1940 else if (event->direction == GDK_SCROLL_DOWN)
1945 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1946 button, event->state, event->x, event->y);
1951 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1953 struct gui_data *inst = (struct gui_data *)data;
1954 int shift, ctrl, alt, x, y, button;
1956 /* Remember the timestamp. */
1957 inst->input_event_time = event->time;
1959 show_mouseptr(inst, 1);
1961 shift = event->state & GDK_SHIFT_MASK;
1962 ctrl = event->state & GDK_CONTROL_MASK;
1963 alt = event->state & inst->meta_mod_mask;
1964 if (event->state & GDK_BUTTON1_MASK)
1966 else if (event->state & GDK_BUTTON2_MASK)
1967 button = MBT_MIDDLE;
1968 else if (event->state & GDK_BUTTON3_MASK)
1971 return FALSE; /* don't even know what button! */
1973 x = (event->x - inst->window_border) / inst->font_width;
1974 y = (event->y - inst->window_border) / inst->font_height;
1976 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1977 x, y, shift, ctrl, alt);
1982 void frontend_keypress(void *handle)
1984 struct gui_data *inst = (struct gui_data *)handle;
1987 * If our child process has exited but not closed, terminate on
1994 static void exit_callback(void *vinst)
1996 struct gui_data *inst = (struct gui_data *)vinst;
1997 int exitcode, close_on_exit;
1999 if (!inst->exited &&
2000 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
2001 inst->exited = TRUE;
2002 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
2003 if (close_on_exit == FORCE_ON ||
2004 (close_on_exit == AUTO && exitcode == 0))
2005 gtk_main_quit(); /* just go */
2007 ldisc_free(inst->ldisc);
2010 inst->back->free(inst->backhandle);
2011 inst->backhandle = NULL;
2013 term_provide_resize_fn(inst->term, NULL, NULL);
2014 update_specials_menu(inst);
2015 gtk_widget_set_sensitive(inst->restartitem, TRUE);
2020 * Replacement code for the gtk_quit_add() function, which GTK2 - in
2021 * their unbounded wisdom - deprecated without providing any usable
2022 * replacement, and which we were using to ensure that our idle
2023 * function for toplevel callbacks was only run from the outermost
2026 * We maintain a global variable with a list of 'struct gui_data'
2027 * instances on which we should call inst_post_main() when a
2028 * subsidiary gtk_main() terminates; then we must make sure that all
2029 * our subsidiary calls to gtk_main() are followed by a call to
2032 * This is kind of overkill in the sense that at the time of writing
2033 * we don't actually ever run more than one 'struct gui_data' instance
2034 * in the same process, but we're _so nearly_ prepared to do that that
2035 * I want to remain futureproof against the possibility of doing so in
2038 struct post_main_context {
2039 struct post_main_context *next;
2040 struct gui_data *inst;
2042 struct post_main_context *post_main_list_head = NULL;
2043 static void request_post_main(struct gui_data *inst)
2045 struct post_main_context *node = snew(struct post_main_context);
2046 node->next = post_main_list_head;
2048 post_main_list_head = node;
2050 static void inst_post_main(struct gui_data *inst);
2051 void post_main(void)
2053 while (post_main_list_head) {
2054 struct post_main_context *node = post_main_list_head;
2055 post_main_list_head = node->next;
2056 inst_post_main(node->inst);
2061 void notify_remote_exit(void *frontend)
2063 struct gui_data *inst = (struct gui_data *)frontend;
2065 queue_toplevel_callback(exit_callback, inst);
2068 static void notify_toplevel_callback(void *frontend);
2070 static void inst_post_main(struct gui_data *inst)
2072 if (gtk_main_level() == 1) {
2073 notify_toplevel_callback(inst);
2074 inst->quit_fn_scheduled = FALSE;
2076 /* Apparently we're _still_ more than one level deep in
2077 * gtk_main() instances, so we'll need another callback for
2078 * when we get out of the next one. */
2079 request_post_main(inst);
2083 static gint idle_toplevel_callback_func(gpointer data)
2085 struct gui_data *inst = (struct gui_data *)data;
2087 if (gtk_main_level() > 1) {
2089 * We don't run the callbacks if we're in the middle of a
2090 * subsidiary gtk_main. Instead, ask for a callback when we
2091 * get back out of the subsidiary main loop (if we haven't
2092 * already arranged one), so we can reschedule ourself then.
2094 if (!inst->quit_fn_scheduled) {
2095 request_post_main(inst);
2096 inst->quit_fn_scheduled = TRUE;
2099 * And unschedule this idle function, since we've now done
2100 * everything we can until the innermost gtk_main has quit and
2101 * can reschedule us with a chance of actually taking action.
2103 if (inst->idle_fn_scheduled) { /* double-check, just in case */
2104 g_source_remove(inst->toplevel_callback_idle_id);
2105 inst->idle_fn_scheduled = FALSE;
2108 run_toplevel_callbacks();
2112 * If we've emptied our toplevel callback queue, unschedule
2113 * ourself. Otherwise, leave ourselves pending so we'll be called
2114 * again to deal with more callbacks after another round of the
2117 if (!toplevel_callback_pending() && inst->idle_fn_scheduled) {
2118 g_source_remove(inst->toplevel_callback_idle_id);
2119 inst->idle_fn_scheduled = FALSE;
2125 static void notify_toplevel_callback(void *frontend)
2127 struct gui_data *inst = (struct gui_data *)frontend;
2129 if (!inst->idle_fn_scheduled) {
2130 inst->toplevel_callback_idle_id =
2131 g_idle_add(idle_toplevel_callback_func, inst);
2132 inst->idle_fn_scheduled = TRUE;
2136 static gint timer_trigger(gpointer data)
2138 unsigned long now = GPOINTER_TO_LONG(data);
2139 unsigned long next, then;
2143 * Destroy the timer we got here on.
2146 g_source_remove(timer_id);
2151 * run_timers() may cause a call to timer_change_notify, in which
2152 * case a new timer will already have been set up and left in
2153 * timer_id. If it hasn't, and run_timers reports that some timing
2154 * still needs to be done, we do it ourselves.
2156 if (run_timers(now, &next) && !timer_id) {
2158 now = GETTICKCOUNT();
2159 if (now - then > next - then)
2163 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
2167 * Returning FALSE means 'don't call this timer again', which
2168 * _should_ be redundant given that we removed it above, but just
2169 * in case, return FALSE anyway.
2174 void timer_change_notify(unsigned long next)
2179 g_source_remove(timer_id);
2181 ticks = next - GETTICKCOUNT();
2183 ticks = 1; /* just in case */
2185 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
2188 #if GTK_CHECK_VERSION(2,0,0)
2189 gboolean fd_input_func(GIOChannel *source, GIOCondition condition,
2192 int sourcefd = g_io_channel_unix_get_fd(source);
2194 * We must process exceptional notifications before ordinary
2195 * readability ones, or we may go straight past the urgent
2198 if (condition & G_IO_PRI)
2199 select_result(sourcefd, 4);
2200 if (condition & G_IO_IN)
2201 select_result(sourcefd, 1);
2202 if (condition & G_IO_OUT)
2203 select_result(sourcefd, 2);
2208 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
2210 if (condition & GDK_INPUT_EXCEPTION)
2211 select_result(sourcefd, 4);
2212 if (condition & GDK_INPUT_READ)
2213 select_result(sourcefd, 1);
2214 if (condition & GDK_INPUT_WRITE)
2215 select_result(sourcefd, 2);
2219 void destroy(GtkWidget *widget, gpointer data)
2224 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
2226 struct gui_data *inst = (struct gui_data *)data;
2227 term_set_focus(inst->term, event->in);
2228 term_update(inst->term);
2229 show_mouseptr(inst, 1);
2233 void set_busy_status(void *frontend, int status)
2235 struct gui_data *inst = (struct gui_data *)frontend;
2236 inst->busy_status = status;
2237 update_mouseptr(inst);
2241 * set or clear the "raw mouse message" mode
2243 void set_raw_mouse_mode(void *frontend, int activate)
2245 struct gui_data *inst = (struct gui_data *)frontend;
2246 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
2247 send_raw_mouse = activate;
2248 update_mouseptr(inst);
2251 void request_resize(void *frontend, int w, int h)
2253 struct gui_data *inst = (struct gui_data *)frontend;
2255 #if !GTK_CHECK_VERSION(3,0,0)
2257 int large_x, large_y;
2258 int offset_x, offset_y;
2260 GtkRequisition inner, outer;
2263 * This is a heinous hack dreamed up by the gnome-terminal
2264 * people to get around a limitation in gtk. The problem is
2265 * that in order to set the size correctly we really need to be
2266 * calling gtk_window_resize - but that needs to know the size
2267 * of the _whole window_, not the drawing area. So what we do
2268 * is to set an artificially huge size request on the drawing
2269 * area, recompute the resulting size request on the window,
2270 * and look at the difference between the two. That gives us
2271 * the x and y offsets we need to translate drawing area size
2272 * into window size for real, and then we call
2273 * gtk_window_resize.
2277 * We start by retrieving the current size of the whole window.
2278 * Adding a bit to _that_ will give us a value we can use as a
2279 * bogus size request which guarantees to be bigger than the
2280 * current size of the drawing area.
2282 get_window_pixels(inst, &large_x, &large_y);
2286 gtk_widget_set_size_request(inst->area, large_x, large_y);
2287 gtk_widget_size_request(inst->area, &inner);
2288 gtk_widget_size_request(inst->window, &outer);
2290 offset_x = outer.width - inner.width;
2291 offset_y = outer.height - inner.height;
2293 area_x = inst->font_width * w + 2*inst->window_border;
2294 area_y = inst->font_height * h + 2*inst->window_border;
2297 * Now we must set the size request on the drawing area back to
2298 * something sensible before we commit the real resize. Best
2299 * way to do this, I think, is to set it to what the size is
2300 * really going to end up being.
2302 gtk_widget_set_size_request(inst->area, area_x, area_y);
2303 #if GTK_CHECK_VERSION(2,0,0)
2304 gtk_window_resize(GTK_WINDOW(inst->window),
2305 area_x + offset_x, area_y + offset_y);
2307 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
2309 * I can no longer remember what this call to
2310 * gtk_container_dequeue_resize_handler is for. It was
2311 * introduced in r3092 with no comment, and the commit log
2312 * message was uninformative. I'm _guessing_ its purpose is to
2313 * prevent gratuitous resize processing on the window given
2314 * that we're about to resize it anyway, but I have no idea
2315 * why that's so incredibly vital.
2317 * I've tried removing the call, and nothing seems to go
2318 * wrong. I've backtracked to r3092 and tried removing the
2319 * call there, and still nothing goes wrong. So I'm going to
2320 * adopt the working hypothesis that it's superfluous; I won't
2321 * actually remove it from the GTK 1.2 code, but I won't
2322 * attempt to replicate its functionality in the GTK 2 code
2325 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
2326 gdk_window_resize(gtk_widget_get_window(inst->window),
2327 area_x + offset_x, area_y + offset_y);
2330 #else /* GTK_CHECK_VERSION(3,0,0) */
2333 * In GTK3, we can do this by using gtk_window_resize_to_geometry,
2334 * which uses the fact that we've already set up the main window's
2335 * WM hints to reflect the terminal drawing area's resize
2336 * increment (i.e. character cell) and the fixed amount of stuff
2339 gtk_window_resize_to_geometry(GTK_WINDOW(inst->window), w, h);
2345 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
2347 inst->cols[n].red = r * 0x0101;
2348 inst->cols[n].green = g * 0x0101;
2349 inst->cols[n].blue = b * 0x0101;
2351 #if !GTK_CHECK_VERSION(3,0,0)
2353 gboolean success[1];
2354 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
2355 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
2356 FALSE, TRUE, success);
2358 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2359 appname, n, r, g, b);
2364 void set_gdk_window_background(GdkWindow *win, const GdkColor *col)
2366 #if GTK_CHECK_VERSION(3,0,0)
2367 /* gdk_window_set_background is deprecated; work around its
2370 rgba.red = col->red / 65535.0;
2371 rgba.green = col->green / 65535.0;
2372 rgba.blue = col->blue / 65535.0;
2374 gdk_window_set_background_rgba(win, &rgba);
2377 /* For GTK1, which doesn't have a 'const' on
2378 * gdk_window_set_background's second parameter type. */
2379 GdkColor col_mutable = *col;
2380 gdk_window_set_background(win, &col_mutable);
2385 void set_window_background(struct gui_data *inst)
2387 if (inst->area && gtk_widget_get_window(inst->area))
2388 set_gdk_window_background(gtk_widget_get_window(inst->area),
2390 if (inst->window && gtk_widget_get_window(inst->window))
2391 set_gdk_window_background(gtk_widget_get_window(inst->window),
2395 void palette_set(void *frontend, int n, int r, int g, int b)
2397 struct gui_data *inst = (struct gui_data *)frontend;
2400 if (n >= NALLCOLOURS)
2402 real_palette_set(inst, n, r, g, b);
2404 /* Default Background changed. Ensure space between text area and
2405 * window border is redrawn */
2406 set_window_background(inst);
2407 draw_backing_rect(inst);
2408 gtk_widget_queue_draw(inst->area);
2412 void palette_reset(void *frontend)
2414 struct gui_data *inst = (struct gui_data *)frontend;
2415 /* This maps colour indices in inst->conf to those used in inst->cols. */
2416 static const int ww[] = {
2417 256, 257, 258, 259, 260, 261,
2418 0, 8, 1, 9, 2, 10, 3, 11,
2419 4, 12, 5, 13, 6, 14, 7, 15
2423 assert(lenof(ww) == NCFGCOLOURS);
2425 #if !GTK_CHECK_VERSION(3,0,0)
2426 if (!inst->colmap) {
2427 inst->colmap = gdk_colormap_get_system();
2429 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
2433 for (i = 0; i < NCFGCOLOURS; i++) {
2434 inst->cols[ww[i]].red =
2435 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
2436 inst->cols[ww[i]].green =
2437 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
2438 inst->cols[ww[i]].blue =
2439 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
2442 for (i = 0; i < NEXTCOLOURS; i++) {
2444 int r = i / 36, g = (i / 6) % 6, b = i % 6;
2445 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
2446 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
2447 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
2449 int shade = i - 216;
2450 shade = shade * 0x0a0a + 0x0808;
2451 inst->cols[i+16].red = inst->cols[i+16].green =
2452 inst->cols[i+16].blue = shade;
2456 #if !GTK_CHECK_VERSION(3,0,0)
2458 gboolean success[NALLCOLOURS];
2459 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
2460 FALSE, TRUE, success);
2461 for (i = 0; i < NALLCOLOURS; i++) {
2463 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2465 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
2466 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
2467 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
2472 /* Since Default Background may have changed, ensure that space
2473 * between text area and window border is refreshed. */
2474 set_window_background(inst);
2475 if (inst->area && gtk_widget_get_window(inst->area)) {
2476 draw_backing_rect(inst);
2477 gtk_widget_queue_draw(inst->area);
2481 #ifdef JUST_USE_GTK_CLIPBOARD_UTF8
2483 /* ----------------------------------------------------------------------
2484 * Clipboard handling, using the high-level GtkClipboard interface in
2485 * as hands-off a way as possible. We write and read the clipboard as
2486 * UTF-8 text, and let GTK deal with converting to any other text
2487 * formats it feels like.
2490 void init_clipboard(struct gui_data *inst)
2492 inst->clipboard = gtk_clipboard_get_for_display(gdk_display_get_default(),
2497 * Because calling gtk_clipboard_set_with_data triggers a call to the
2498 * clipboard_clear function from the last time, we need to arrange a
2499 * way to distinguish a real call to clipboard_clear for the _new_
2500 * instance of the clipboard data from the leftover call for the
2501 * outgoing one. We do this by setting the user data field in our
2502 * gtk_clipboard_set_with_data() call, instead of the obvious pointer
2503 * to 'inst', to one of these.
2505 struct clipboard_data_instance {
2506 struct gui_data *inst;
2507 char *pasteout_data_utf8;
2508 int pasteout_data_utf8_len;
2511 static void clipboard_provide_data(GtkClipboard *clipboard,
2512 GtkSelectionData *selection_data,
2513 guint info, gpointer data)
2515 struct clipboard_data_instance *cdi =
2516 (struct clipboard_data_instance *)data;
2517 struct gui_data *inst = cdi->inst;
2519 if (inst->current_cdi == cdi) {
2520 gtk_selection_data_set_text(selection_data, cdi->pasteout_data_utf8,
2521 cdi->pasteout_data_utf8_len);
2525 static void clipboard_clear(GtkClipboard *clipboard, gpointer data)
2527 struct clipboard_data_instance *cdi =
2528 (struct clipboard_data_instance *)data;
2529 struct gui_data *inst = cdi->inst;
2531 if (inst->current_cdi == cdi) {
2532 term_deselect(inst->term);
2533 inst->current_cdi = NULL;
2535 sfree(cdi->pasteout_data_utf8);
2539 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2542 struct gui_data *inst = (struct gui_data *)frontend;
2543 struct clipboard_data_instance *cdi;
2545 if (inst->direct_to_font) {
2546 /* In this clipboard mode, we just can't paste if we're in
2547 * direct-to-font mode. Fortunately, that shouldn't be
2548 * important, because we'll only use this clipboard handling
2549 * code on systems where that kind of font doesn't exist
2554 cdi = snew(struct clipboard_data_instance);
2556 inst->current_cdi = cdi;
2557 cdi->pasteout_data_utf8 = snewn(len*6, char);
2559 const wchar_t *tmp = data;
2561 cdi->pasteout_data_utf8_len =
2562 charset_from_unicode(&tmp, &tmplen, cdi->pasteout_data_utf8,
2563 len*6, CS_UTF8, NULL, NULL, 0);
2567 * It would be nice to just call gtk_clipboard_set_text() in place
2568 * of all of the faffing below. Unfortunately, that won't give me
2569 * access to the clipboard-clear event, which we use to visually
2570 * deselect text in the terminal.
2573 GtkTargetList *targetlist;
2574 GtkTargetEntry *targettable;
2577 targetlist = gtk_target_list_new(NULL, 0);
2578 gtk_target_list_add_text_targets(targetlist, 0);
2579 targettable = gtk_target_table_new_from_list(targetlist, &n_targets);
2580 gtk_clipboard_set_with_data(inst->clipboard, targettable, n_targets,
2581 clipboard_provide_data, clipboard_clear,
2583 gtk_target_table_free(targettable, n_targets);
2584 gtk_target_list_unref(targetlist);
2588 static void clipboard_text_received(GtkClipboard *clipboard,
2589 const gchar *text, gpointer data)
2591 struct gui_data *inst = (struct gui_data *)data;
2597 length = strlen(text);
2599 if (inst->pastein_data)
2600 sfree(inst->pastein_data);
2602 inst->pastein_data = snewn(length, wchar_t);
2603 inst->pastein_data_len = mb_to_wc(CS_UTF8, 0, text, length,
2604 inst->pastein_data, length);
2606 term_do_paste(inst->term);
2609 void request_paste(void *frontend)
2611 struct gui_data *inst = (struct gui_data *)frontend;
2612 gtk_clipboard_request_text(inst->clipboard, clipboard_text_received, inst);
2615 #else /* JUST_USE_GTK_CLIPBOARD_UTF8 */
2617 /* ----------------------------------------------------------------------
2618 * Clipboard handling for X, using the low-level gtk_selection_*
2619 * interface, handling conversions to fiddly things like compound text
2620 * ourselves, and storing in X cut buffers too.
2622 * This version of the clipboard code has to be kept around for GTK1,
2623 * which doesn't have the higher-level GtkClipboard interface at all.
2624 * And since it works on GTK2 and GTK3 too and has had a good few
2625 * years of shakedown and bug fixing, we might as well keep using it
2626 * where it's applicable.
2628 * It's _possible_ that we might be able to replicate all the
2629 * important wrinkles of this code in GtkClipboard. (In particular,
2630 * cut buffers or local analogue look as if they might be accessible
2631 * via gtk_clipboard_set_can_store(), and delivering text in
2632 * non-Unicode formats only in the direct-to-font case ought to be
2633 * possible if we can figure out the right set of things to put in the
2634 * GtkTargetList.) But that work can wait until there's a need for it!
2637 /* Store the data in a cut-buffer. */
2638 static void store_cutbuffer(char * ptr, int len)
2640 #ifndef NOT_X_WINDOWS
2641 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2642 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
2643 XRotateBuffers(disp, 1);
2644 XStoreBytes(disp, ptr, len);
2648 /* Retrieve data from a cut-buffer.
2649 * Returned data needs to be freed with XFree().
2651 static char *retrieve_cutbuffer(int *nbytes)
2653 #ifndef NOT_X_WINDOWS
2654 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2656 ptr = XFetchBytes(disp, nbytes);
2657 if (*nbytes <= 0 && ptr != 0) {
2667 void write_clip(void *frontend, wchar_t *data, int *attr, int len,
2670 struct gui_data *inst = (struct gui_data *)frontend;
2671 if (inst->pasteout_data)
2672 sfree(inst->pasteout_data);
2673 if (inst->pasteout_data_ctext)
2674 sfree(inst->pasteout_data_ctext);
2675 if (inst->pasteout_data_utf8)
2676 sfree(inst->pasteout_data_utf8);
2679 * Set up UTF-8 and compound text paste data. This only happens
2680 * if we aren't in direct-to-font mode using the D800 hack.
2682 if (!inst->direct_to_font) {
2683 const wchar_t *tmp = data;
2685 #ifndef NOT_X_WINDOWS
2688 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2691 inst->pasteout_data_utf8 = snewn(len*6, char);
2692 inst->pasteout_data_utf8_len = len*6;
2693 inst->pasteout_data_utf8_len =
2694 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
2695 inst->pasteout_data_utf8_len,
2696 CS_UTF8, NULL, NULL, 0);
2697 if (inst->pasteout_data_utf8_len == 0) {
2698 sfree(inst->pasteout_data_utf8);
2699 inst->pasteout_data_utf8 = NULL;
2701 inst->pasteout_data_utf8 =
2702 sresize(inst->pasteout_data_utf8,
2703 inst->pasteout_data_utf8_len + 1, char);
2704 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
2708 * Now let Xlib convert our UTF-8 data into compound text.
2710 #ifndef NOT_X_WINDOWS
2711 list[0] = inst->pasteout_data_utf8;
2712 if (Xutf8TextListToTextProperty(disp, list, 1,
2713 XCompoundTextStyle, &tp) == 0) {
2714 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
2715 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
2716 inst->pasteout_data_ctext_len = tp.nitems;
2721 inst->pasteout_data_ctext = NULL;
2722 inst->pasteout_data_ctext_len = 0;
2725 inst->pasteout_data_utf8 = NULL;
2726 inst->pasteout_data_utf8_len = 0;
2727 inst->pasteout_data_ctext = NULL;
2728 inst->pasteout_data_ctext_len = 0;
2731 inst->pasteout_data = snewn(len*6, char);
2732 inst->pasteout_data_len = len*6;
2733 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
2734 data, len, inst->pasteout_data,
2735 inst->pasteout_data_len,
2737 if (inst->pasteout_data_len == 0) {
2738 sfree(inst->pasteout_data);
2739 inst->pasteout_data = NULL;
2741 inst->pasteout_data =
2742 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
2745 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
2747 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
2748 inst->input_event_time)) {
2749 #if GTK_CHECK_VERSION(2,0,0)
2750 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
2752 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2753 GDK_SELECTION_TYPE_STRING, 1);
2754 if (inst->pasteout_data_ctext)
2755 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2756 compound_text_atom, 1);
2757 if (inst->pasteout_data_utf8)
2758 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2759 utf8_string_atom, 1);
2763 term_deselect(inst->term);
2766 static void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2767 guint info, guint time_stamp, gpointer data)
2769 struct gui_data *inst = (struct gui_data *)data;
2770 GdkAtom target = gtk_selection_data_get_target(seldata);
2771 if (target == utf8_string_atom)
2772 gtk_selection_data_set(seldata, target, 8,
2773 (unsigned char *)inst->pasteout_data_utf8,
2774 inst->pasteout_data_utf8_len);
2775 else if (target == compound_text_atom)
2776 gtk_selection_data_set(seldata, target, 8,
2777 (unsigned char *)inst->pasteout_data_ctext,
2778 inst->pasteout_data_ctext_len);
2780 gtk_selection_data_set(seldata, target, 8,
2781 (unsigned char *)inst->pasteout_data,
2782 inst->pasteout_data_len);
2785 static gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2788 struct gui_data *inst = (struct gui_data *)data;
2790 term_deselect(inst->term);
2791 if (inst->pasteout_data)
2792 sfree(inst->pasteout_data);
2793 if (inst->pasteout_data_ctext)
2794 sfree(inst->pasteout_data_ctext);
2795 if (inst->pasteout_data_utf8)
2796 sfree(inst->pasteout_data_utf8);
2797 inst->pasteout_data = NULL;
2798 inst->pasteout_data_len = 0;
2799 inst->pasteout_data_ctext = NULL;
2800 inst->pasteout_data_ctext_len = 0;
2801 inst->pasteout_data_utf8 = NULL;
2802 inst->pasteout_data_utf8_len = 0;
2806 void request_paste(void *frontend)
2808 struct gui_data *inst = (struct gui_data *)frontend;
2810 * In Unix, pasting is asynchronous: all we can do at the
2811 * moment is to call gtk_selection_convert(), and when the data
2812 * comes back _then_ we can call term_do_paste().
2815 if (!inst->direct_to_font) {
2817 * First we attempt to retrieve the selection as a UTF-8
2818 * string (which we will convert to the correct code page
2819 * before sending to the session, of course). If that
2820 * fails, selection_received() will be informed and will
2821 * fall back to an ordinary string.
2823 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2825 inst->input_event_time);
2828 * If we're in direct-to-font mode, we disable UTF-8
2829 * pasting, and go straight to ordinary string data.
2831 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2832 GDK_SELECTION_TYPE_STRING,
2833 inst->input_event_time);
2837 static void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2838 guint time, gpointer data)
2840 struct gui_data *inst = (struct gui_data *)data;
2843 #ifndef NOT_X_WINDOWS
2845 int free_list_required = 0;
2846 int free_required = 0;
2849 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2850 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2851 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2852 gint seldata_length = gtk_selection_data_get_length(seldata);
2854 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2856 * Failed to get a UTF-8 selection string. Try compound
2859 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2861 inst->input_event_time);
2865 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2867 * Failed to get UTF-8 or compound text. Try an ordinary
2870 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2871 GDK_SELECTION_TYPE_STRING,
2872 inst->input_event_time);
2877 * If we have data, but it's not of a type we can deal with,
2878 * we have to ignore the data.
2880 if (seldata_length > 0 &&
2881 seldata_type != GDK_SELECTION_TYPE_STRING &&
2882 seldata_type != compound_text_atom &&
2883 seldata_type != utf8_string_atom)
2887 * If we have no data, try looking in a cut buffer.
2889 if (seldata_length <= 0) {
2890 #ifndef NOT_X_WINDOWS
2891 text = retrieve_cutbuffer(&length);
2894 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2895 * source, so use that as a de-facto standard. */
2896 charset = CS_ISO8859_1;
2903 * Convert COMPOUND_TEXT into UTF-8.
2905 if (seldata_type == compound_text_atom) {
2906 #ifndef NOT_X_WINDOWS
2909 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2911 tp.value = (unsigned char *)seldata_data;
2912 tp.encoding = (Atom) seldata_type;
2913 tp.format = gtk_selection_data_get_format(seldata);
2914 tp.nitems = seldata_length;
2915 ret = Xutf8TextPropertyToTextList(disp, &tp, &list, &count);
2916 if (ret == 0 && count == 1) {
2918 length = strlen(list[0]);
2920 free_list_required = 1;
2925 * Compound text failed; fall back to STRING.
2927 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2928 GDK_SELECTION_TYPE_STRING,
2929 inst->input_event_time);
2933 text = (char *)seldata_data;
2934 length = seldata_length;
2935 charset = (seldata_type == utf8_string_atom ?
2936 CS_UTF8 : inst->ucsdata.line_codepage);
2940 if (inst->pastein_data)
2941 sfree(inst->pastein_data);
2943 inst->pastein_data = snewn(length, wchar_t);
2944 inst->pastein_data_len = length;
2945 inst->pastein_data_len =
2946 mb_to_wc(charset, 0, text, length,
2947 inst->pastein_data, inst->pastein_data_len);
2949 term_do_paste(inst->term);
2951 #ifndef NOT_X_WINDOWS
2952 if (free_list_required)
2953 XFreeStringList(list);
2959 void init_clipboard(struct gui_data *inst)
2961 #ifndef NOT_X_WINDOWS
2963 * Ensure that all the cut buffers exist - according to the ICCCM,
2964 * we must do this before we start using cut buffers.
2966 unsigned char empty[] = "";
2967 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2968 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2969 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
2970 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2971 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
2972 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2973 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
2974 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2975 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
2976 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2977 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
2978 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2979 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
2980 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2981 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
2982 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2983 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
2986 g_signal_connect(G_OBJECT(inst->area), "selection_received",
2987 G_CALLBACK(selection_received), inst);
2988 g_signal_connect(G_OBJECT(inst->area), "selection_get",
2989 G_CALLBACK(selection_get), inst);
2990 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
2991 G_CALLBACK(selection_clear), inst);
2995 * End of selection/clipboard handling.
2996 * ----------------------------------------------------------------------
2999 #endif /* JUST_USE_GTK_CLIPBOARD_UTF8 */
3001 void get_clip(void *frontend, wchar_t ** p, int *len)
3003 struct gui_data *inst = (struct gui_data *)frontend;
3006 *p = inst->pastein_data;
3007 *len = inst->pastein_data_len;
3011 static void set_window_titles(struct gui_data *inst)
3014 * We must always call set_icon_name after calling set_title,
3015 * since set_title will write both names. Irritating, but such
3018 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
3019 if (!conf_get_int(inst->conf, CONF_win_name_always))
3020 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
3024 void set_title(void *frontend, char *title)
3026 struct gui_data *inst = (struct gui_data *)frontend;
3027 sfree(inst->wintitle);
3028 inst->wintitle = dupstr(title);
3029 set_window_titles(inst);
3032 void set_icon(void *frontend, char *title)
3034 struct gui_data *inst = (struct gui_data *)frontend;
3035 sfree(inst->icontitle);
3036 inst->icontitle = dupstr(title);
3037 set_window_titles(inst);
3040 void set_title_and_icon(void *frontend, char *title, char *icon)
3042 struct gui_data *inst = (struct gui_data *)frontend;
3043 sfree(inst->wintitle);
3044 inst->wintitle = dupstr(title);
3045 sfree(inst->icontitle);
3046 inst->icontitle = dupstr(icon);
3047 set_window_titles(inst);
3050 void set_sbar(void *frontend, int total, int start, int page)
3052 struct gui_data *inst = (struct gui_data *)frontend;
3053 if (!conf_get_int(inst->conf, CONF_scrollbar))
3055 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
3056 gtk_adjustment_set_upper(inst->sbar_adjust, total);
3057 gtk_adjustment_set_value(inst->sbar_adjust, start);
3058 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
3059 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
3060 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
3061 inst->ignore_sbar = TRUE;
3062 gtk_adjustment_changed(inst->sbar_adjust);
3063 inst->ignore_sbar = FALSE;
3066 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
3068 struct gui_data *inst = (struct gui_data *)data;
3070 if (!conf_get_int(inst->conf, CONF_scrollbar))
3072 if (!inst->ignore_sbar)
3073 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
3076 void sys_cursor(void *frontend, int x, int y)
3079 * This is meaningless under X.
3084 * This is still called when mode==BELL_VISUAL, even though the
3085 * visual bell is handled entirely within terminal.c, because we
3086 * may want to perform additional actions on any kind of bell (for
3087 * example, taskbar flashing in Windows).
3089 void do_beep(void *frontend, int mode)
3091 if (mode == BELL_DEFAULT)
3095 int char_width(Context ctx, int uc)
3098 * Under X, any fixed-width font really _is_ fixed-width.
3099 * Double-width characters will be dealt with using a separate
3100 * font. For the moment we can simply return 1.
3102 * FIXME: but is that also true of Pango?
3107 Context get_ctx(void *frontend)
3109 struct gui_data *inst = (struct gui_data *)frontend;
3110 struct draw_ctx *dctx;
3112 if (!gtk_widget_get_window(inst->area))
3115 dctx = snew(struct draw_ctx);
3117 dctx->uctx.type = inst->drawtype;
3118 #ifdef DRAW_TEXT_GDK
3119 if (dctx->uctx.type == DRAWTYPE_GDK) {
3120 /* If we're doing GDK-based drawing, then we also expect
3121 * inst->pixmap to exist. */
3122 dctx->uctx.u.gdk.target = inst->pixmap;
3123 dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
3126 #ifdef DRAW_TEXT_CAIRO
3127 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3128 dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
3129 /* If we're doing Cairo drawing, we expect inst->surface to
3130 * exist, and we draw to that first, regardless of whether we
3131 * subsequently copy the results to inst->pixmap. */
3132 dctx->uctx.u.cairo.cr = cairo_create(inst->surface);
3133 cairo_setup_dctx(dctx);
3139 void free_ctx(Context ctx)
3141 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3142 /* struct gui_data *inst = dctx->inst; */
3143 #ifdef DRAW_TEXT_GDK
3144 if (dctx->uctx.type == DRAWTYPE_GDK) {
3145 gdk_gc_unref(dctx->uctx.u.gdk.gc);
3148 #ifdef DRAW_TEXT_CAIRO
3149 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3150 cairo_destroy(dctx->uctx.u.cairo.cr);
3157 static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
3159 #if defined DRAW_TEXT_CAIRO && !defined NO_BACKING_PIXMAPS
3160 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3162 * If inst->surface and inst->pixmap both exist, then we've
3163 * just drawn new content to the former which we must copy to
3166 cairo_t *cr = gdk_cairo_create(dctx->inst->pixmap);
3167 cairo_set_source_surface(cr, dctx->inst->surface, 0, 0);
3168 cairo_rectangle(cr, x, y, w, h);
3175 * Now we just queue a window redraw, which will cause
3176 * inst->surface or inst->pixmap (whichever is appropriate for our
3177 * compile mode) to be copied to the real window when we receive
3178 * the resulting "expose" or "draw" event.
3180 * Amazingly, this one API call is actually valid in all versions
3183 gtk_widget_queue_draw_area(dctx->inst->area, x, y, w, h);
3186 static void draw_set_colour(struct draw_ctx *dctx, int col)
3188 #ifdef DRAW_TEXT_GDK
3189 if (dctx->uctx.type == DRAWTYPE_GDK) {
3190 gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
3193 #ifdef DRAW_TEXT_CAIRO
3194 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3195 cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
3196 dctx->inst->cols[col].red / 65535.0,
3197 dctx->inst->cols[col].green / 65535.0,
3198 dctx->inst->cols[col].blue / 65535.0);
3203 static void draw_rectangle(struct draw_ctx *dctx, int filled,
3204 int x, int y, int w, int h)
3206 #ifdef DRAW_TEXT_GDK
3207 if (dctx->uctx.type == DRAWTYPE_GDK) {
3208 gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
3209 filled, x, y, w, h);
3212 #ifdef DRAW_TEXT_CAIRO
3213 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3214 cairo_new_path(dctx->uctx.u.cairo.cr);
3216 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
3217 cairo_fill(dctx->uctx.u.cairo.cr);
3219 cairo_rectangle(dctx->uctx.u.cairo.cr,
3220 x + 0.5, y + 0.5, w, h);
3221 cairo_close_path(dctx->uctx.u.cairo.cr);
3222 cairo_stroke(dctx->uctx.u.cairo.cr);
3228 static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
3230 #ifdef DRAW_TEXT_GDK
3231 if (dctx->uctx.type == DRAWTYPE_GDK) {
3239 gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
3242 #ifdef DRAW_TEXT_CAIRO
3243 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3244 cairo_reset_clip(dctx->uctx.u.cairo.cr);
3245 cairo_new_path(dctx->uctx.u.cairo.cr);
3246 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
3247 cairo_clip(dctx->uctx.u.cairo.cr);
3252 static void draw_point(struct draw_ctx *dctx, int x, int y)
3254 #ifdef DRAW_TEXT_GDK
3255 if (dctx->uctx.type == DRAWTYPE_GDK) {
3256 gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
3259 #ifdef DRAW_TEXT_CAIRO
3260 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3261 cairo_new_path(dctx->uctx.u.cairo.cr);
3262 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
3263 cairo_fill(dctx->uctx.u.cairo.cr);
3268 static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
3270 #ifdef DRAW_TEXT_GDK
3271 if (dctx->uctx.type == DRAWTYPE_GDK) {
3272 gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
3276 #ifdef DRAW_TEXT_CAIRO
3277 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3278 cairo_new_path(dctx->uctx.u.cairo.cr);
3279 cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
3280 cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
3281 cairo_stroke(dctx->uctx.u.cairo.cr);
3286 static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
3288 int h, int hdouble, int hbothalf)
3290 #ifdef DRAW_TEXT_CAIRO
3291 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3292 cairo_matrix_t matrix;
3316 cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
3321 static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
3323 int h, int hdouble, int hbothalf)
3325 #ifdef DRAW_TEXT_GDK
3326 #ifndef NO_BACKING_PIXMAPS
3327 if (dctx->uctx.type == DRAWTYPE_GDK) {
3329 * I can't find any plausible StretchBlt equivalent in the X
3330 * server, so I'm going to do this the slow and painful way.
3331 * This will involve repeated calls to gdk_draw_pixmap() to
3332 * stretch the text horizontally. It's O(N^2) in time and O(N)
3333 * in network bandwidth, but you try thinking of a better way.
3338 for (i = 0; i < w; i++) {
3339 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3340 dctx->uctx.u.gdk.gc,
3341 dctx->uctx.u.gdk.target,
3351 /* Now stretch vertically, in the same way. */
3356 for (i = 0; i < h; i += 2) {
3357 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
3358 dctx->uctx.u.gdk.gc,
3359 dctx->uctx.u.gdk.target,
3367 #error No way to implement stretching in GDK without a reliable backing pixmap
3369 #endif /* DRAW_TEXT_GDK */
3370 #ifdef DRAW_TEXT_CAIRO
3371 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
3372 cairo_set_matrix(dctx->uctx.u.cairo.cr,
3373 &dctx->uctx.u.cairo.origmatrix);
3378 static void draw_backing_rect(struct gui_data *inst)
3380 struct draw_ctx *dctx = get_ctx(inst);
3381 int w = inst->width * inst->font_width + 2*inst->window_border;
3382 int h = inst->height * inst->font_height + 2*inst->window_border;
3383 draw_set_colour(dctx, 258);
3384 draw_rectangle(dctx, 1, 0, 0, w, h);
3385 draw_update(dctx, 0, 0, w, h);
3390 * Draw a line of text in the window, at given character
3391 * coordinates, in given attributes.
3393 * We are allowed to fiddle with the contents of `text'.
3395 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
3396 unsigned long attr, int lattr)
3398 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3399 struct gui_data *inst = dctx->inst;
3401 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
3403 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
3405 if (attr & TATTR_COMBINING) {
3411 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
3412 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
3413 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
3418 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
3419 if (nfg < 16) nfg |= 8;
3420 else if (nfg >= 256) nfg |= 1;
3422 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
3423 if (nbg < 16) nbg |= 8;
3424 else if (nbg >= 256) nbg |= 1;
3426 if ((attr & TATTR_ACTCURS) && !monochrome) {
3431 fontid = shadow = 0;
3433 if (attr & ATTR_WIDE) {
3440 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
3447 if (!inst->fonts[fontid]) {
3450 * Fall back through font ids with subsets of this one's
3451 * set bits, in order.
3453 for (i = fontid; i-- > 0 ;) {
3455 continue; /* some other bit is set */
3456 if (inst->fonts[i]) {
3461 assert(inst->fonts[fontid]); /* we should at least have hit zero */
3464 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3466 if (x >= inst->term->cols)
3468 if (x + len*2*widefactor > inst->term->cols)
3469 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3475 x*inst->font_width+inst->window_border,
3476 y*inst->font_height+inst->window_border,
3477 rlen*widefactor*inst->font_width,
3480 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3481 draw_stretch_before(dctx,
3482 x*inst->font_width+inst->window_border,
3483 y*inst->font_height+inst->window_border,
3484 rlen*widefactor*inst->font_width, TRUE,
3486 ((lattr & LATTR_MODE) != LATTR_WIDE),
3487 ((lattr & LATTR_MODE) == LATTR_BOT));
3490 draw_set_colour(dctx, nbg);
3491 draw_rectangle(dctx, TRUE,
3492 x*inst->font_width+inst->window_border,
3493 y*inst->font_height+inst->window_border,
3494 rlen*widefactor*inst->font_width, inst->font_height);
3496 draw_set_colour(dctx, nfg);
3497 if (ncombining > 1) {
3499 unifont_draw_combining(&dctx->uctx, inst->fonts[fontid],
3500 x*inst->font_width+inst->window_border,
3501 (y*inst->font_height+inst->window_border+
3502 inst->fonts[0]->ascent),
3503 text, ncombining, widefactor > 1,
3504 bold, inst->font_width);
3506 unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
3507 x*inst->font_width+inst->window_border,
3508 (y*inst->font_height+inst->window_border+
3509 inst->fonts[0]->ascent),
3510 text, len, widefactor > 1,
3511 bold, inst->font_width);
3514 if (attr & ATTR_UNDER) {
3515 int uheight = inst->fonts[0]->ascent + 1;
3516 if (uheight >= inst->font_height)
3517 uheight = inst->font_height - 1;
3518 draw_line(dctx, x*inst->font_width+inst->window_border,
3519 y*inst->font_height + uheight + inst->window_border,
3520 (x+len)*widefactor*inst->font_width-1+inst->window_border,
3521 y*inst->font_height + uheight + inst->window_border);
3524 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3525 draw_stretch_after(dctx,
3526 x*inst->font_width+inst->window_border,
3527 y*inst->font_height+inst->window_border,
3528 rlen*widefactor*inst->font_width, TRUE,
3530 ((lattr & LATTR_MODE) != LATTR_WIDE),
3531 ((lattr & LATTR_MODE) == LATTR_BOT));
3535 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3536 unsigned long attr, int lattr)
3538 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3539 struct gui_data *inst = dctx->inst;
3542 do_text_internal(ctx, x, y, text, len, attr, lattr);
3544 if (attr & ATTR_WIDE) {
3550 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3552 if (x >= inst->term->cols)
3554 if (x + len*2*widefactor > inst->term->cols)
3555 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3560 x*inst->font_width+inst->window_border,
3561 y*inst->font_height+inst->window_border,
3562 len*widefactor*inst->font_width, inst->font_height);
3565 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
3566 unsigned long attr, int lattr)
3568 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3569 struct gui_data *inst = dctx->inst;
3571 int active, passive, widefactor;
3573 if (attr & TATTR_PASCURS) {
3574 attr &= ~TATTR_PASCURS;
3578 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
3579 attr &= ~TATTR_ACTCURS;
3583 do_text_internal(ctx, x, y, text, len, attr, lattr);
3585 if (attr & TATTR_COMBINING)
3588 if (attr & ATTR_WIDE) {
3594 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3596 if (x >= inst->term->cols)
3598 if (x + len*2*widefactor > inst->term->cols)
3599 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3603 if (inst->cursor_type == 0) {
3605 * An active block cursor will already have been done by
3606 * the above do_text call, so we only need to do anything
3610 draw_set_colour(dctx, 261);
3611 draw_rectangle(dctx, FALSE,
3612 x*inst->font_width+inst->window_border,
3613 y*inst->font_height+inst->window_border,
3614 len*widefactor*inst->font_width-1,
3615 inst->font_height-1);
3619 int startx, starty, dx, dy, length, i;
3623 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
3624 char_width = 2*inst->font_width;
3626 char_width = inst->font_width;
3628 if (inst->cursor_type == 1) {
3629 uheight = inst->fonts[0]->ascent + 1;
3630 if (uheight >= inst->font_height)
3631 uheight = inst->font_height - 1;
3633 startx = x * inst->font_width + inst->window_border;
3634 starty = y * inst->font_height + inst->window_border + uheight;
3637 length = len * widefactor * char_width;
3640 if (attr & TATTR_RIGHTCURS)
3641 xadjust = char_width - 1;
3642 startx = x * inst->font_width + inst->window_border + xadjust;
3643 starty = y * inst->font_height + inst->window_border;
3646 length = inst->font_height;
3649 draw_set_colour(dctx, 261);
3651 for (i = 0; i < length; i++) {
3653 draw_point(dctx, startx, starty);
3658 } else if (active) {
3659 draw_line(dctx, startx, starty,
3660 startx + (length-1) * dx, starty + (length-1) * dy);
3661 } /* else no cursor (e.g., blinked off) */
3665 x*inst->font_width+inst->window_border,
3666 y*inst->font_height+inst->window_border,
3667 len*widefactor*inst->font_width, inst->font_height);
3669 #if GTK_CHECK_VERSION(2,0,0)
3671 GdkRectangle cursorrect;
3672 cursorrect.x = x*inst->font_width+inst->window_border;
3673 cursorrect.y = y*inst->font_height+inst->window_border;
3674 cursorrect.width = len*widefactor*inst->font_width;
3675 cursorrect.height = inst->font_height;
3676 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
3681 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
3683 if (cursor_val == -1) {
3684 #if GTK_CHECK_VERSION(2,16,0)
3685 cursor_val = GDK_BLANK_CURSOR;
3688 * Work around absence of GDK_BLANK_CURSOR by inventing a
3692 GdkColor bg = { 0, 0, 0, 0 };
3693 GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
3694 GdkGC *gc = gdk_gc_new(pm);
3695 gdk_gc_set_foreground(gc, &bg);
3696 gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
3698 ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
3699 gdk_pixmap_unref(pm);
3704 return gdk_cursor_new(cursor_val);
3707 void modalfatalbox(const char *p, ...)
3710 fprintf(stderr, "FATAL ERROR: ");
3712 vfprintf(stderr, p, ap);
3714 fputc('\n', stderr);
3718 void cmdline_error(const char *p, ...)
3721 fprintf(stderr, "%s: ", appname);
3723 vfprintf(stderr, p, ap);
3725 fputc('\n', stderr);
3729 const char *get_x_display(void *frontend)
3731 return gdk_get_display();
3734 #ifndef NOT_X_WINDOWS
3735 long get_windowid(void *frontend)
3737 struct gui_data *inst = (struct gui_data *)frontend;
3738 return (long)GDK_WINDOW_XID(gtk_widget_get_window(inst->area));
3742 static void help(FILE *fp) {
3744 "pterm option summary:\n"
3746 " --display DISPLAY Specify X display to use (note '--')\n"
3747 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
3748 " -fn FONT Normal text font\n"
3749 " -fb FONT Bold text font\n"
3750 " -geometry GEOMETRY Position and size of window (size in characters)\n"
3751 " -sl LINES Number of lines of scrollback\n"
3752 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
3753 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
3754 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
3755 " -T TITLE Window title\n"
3756 " -ut, +ut Do(default) or do not update utmp\n"
3757 " -ls, +ls Do(default) or do not make shell a login shell\n"
3758 " -sb, +sb Do(default) or do not display a scrollbar\n"
3759 " -log PATH Log all output to a file\n"
3760 " -nethack Map numeric keypad to hjklyubn direction keys\n"
3761 " -xrm RESOURCE-STRING Set an X resource\n"
3762 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
3763 ) < 0 || fflush(fp) < 0) {
3764 perror("output error");
3769 static void version(FILE *fp) {
3770 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
3771 perror("output error");
3776 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
3777 struct gui_data *inst, Conf *conf)
3783 * Macros to make argument handling easier. Note that because
3784 * they need to call `continue', they cannot be contained in
3785 * the usual do {...} while (0) wrapper to make them
3786 * syntactically single statements; hence it is not legal to
3787 * use one of these macros as an unbraced statement between
3790 #define EXPECTS_ARG { \
3791 if (--argc <= 0) { \
3793 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
3798 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
3800 while (--argc > 0) {
3801 const char *p = *++argv;
3805 * Shameless cheating. Debian requires all X terminal
3806 * emulators to support `-T title'; but
3807 * cmdline_process_param will eat -T (it means no-pty) and
3808 * complain that pterm doesn't support it. So, in pterm
3809 * only, we convert -T into -title.
3811 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
3815 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
3816 do_everything ? 1 : -1, conf);
3819 cmdline_error("option \"%s\" requires an argument", p);
3820 } else if (ret == 2) {
3821 --argc, ++argv; /* skip next argument */
3823 } else if (ret == 1) {
3827 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
3831 fs = fontspec_new(val);
3832 conf_set_fontspec(conf, CONF_font, fs);
3835 } else if (!strcmp(p, "-fb")) {
3839 fs = fontspec_new(val);
3840 conf_set_fontspec(conf, CONF_boldfont, fs);
3843 } else if (!strcmp(p, "-fw")) {
3847 fs = fontspec_new(val);
3848 conf_set_fontspec(conf, CONF_widefont, fs);
3851 } else if (!strcmp(p, "-fwb")) {
3855 fs = fontspec_new(val);
3856 conf_set_fontspec(conf, CONF_wideboldfont, fs);
3859 } else if (!strcmp(p, "-cs")) {
3862 conf_set_str(conf, CONF_line_codepage, val);
3864 } else if (!strcmp(p, "-geometry")) {
3868 #if GTK_CHECK_VERSION(2,0,0)
3869 inst->geometry = val;
3871 /* On GTK 1, we have to do this using raw Xlib */
3875 flags = XParseGeometry(val, &x, &y, &w, &h);
3876 if (flags & WidthValue)
3877 conf_set_int(conf, CONF_width, w);
3878 if (flags & HeightValue)
3879 conf_set_int(conf, CONF_height, h);
3881 if (flags & (XValue | YValue)) {
3884 inst->gotpos = TRUE;
3885 inst->gravity = ((flags & XNegative ? 1 : 0) |
3886 (flags & YNegative ? 2 : 0));
3891 } else if (!strcmp(p, "-sl")) {
3894 conf_set_int(conf, CONF_savelines, atoi(val));
3896 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
3897 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
3898 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
3903 #if GTK_CHECK_VERSION(3,0,0)
3905 int success = gdk_rgba_parse(&rgba, val);
3908 int success = gdk_color_parse(val, &col);
3913 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
3916 #if GTK_CHECK_VERSION(3,0,0)
3917 int r = rgba.red * 255;
3918 int g = rgba.green * 255;
3919 int b = rgba.blue * 255;
3921 int r = col.red / 256;
3922 int g = col.green / 256;
3923 int b = col.blue / 256;
3927 index = (!strcmp(p, "-fg") ? 0 :
3928 !strcmp(p, "-bg") ? 2 :
3929 !strcmp(p, "-bfg") ? 1 :
3930 !strcmp(p, "-bbg") ? 3 :
3931 !strcmp(p, "-cfg") ? 4 :
3932 !strcmp(p, "-cbg") ? 5 : -1);
3933 assert(index != -1);
3935 conf_set_int_int(conf, CONF_colours, index*3+0, r);
3936 conf_set_int_int(conf, CONF_colours, index*3+1, g);
3937 conf_set_int_int(conf, CONF_colours, index*3+2, b);
3941 } else if (use_pty_argv && !strcmp(p, "-e")) {
3942 /* This option swallows all further arguments. */
3948 pty_argv = snewn(argc+1, char *);
3950 for (i = 0; i < argc; i++)
3951 pty_argv[i] = argv[i];
3952 pty_argv[argc] = NULL;
3953 break; /* finished command-line processing */
3955 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
3958 } else if (!strcmp(p, "-title")) {
3961 conf_set_str(conf, CONF_wintitle, val);
3963 } else if (!strcmp(p, "-log")) {
3967 fn = filename_from_str(val);
3968 conf_set_filename(conf, CONF_logfilename, fn);
3969 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
3972 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
3974 conf_set_int(conf, CONF_stamp_utmp, 0);
3976 } else if (!strcmp(p, "-ut")) {
3978 conf_set_int(conf, CONF_stamp_utmp, 1);
3980 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
3982 conf_set_int(conf, CONF_login_shell, 0);
3984 } else if (!strcmp(p, "-ls")) {
3986 conf_set_int(conf, CONF_login_shell, 1);
3988 } else if (!strcmp(p, "-nethack")) {
3990 conf_set_int(conf, CONF_nethack_keypad, 1);
3992 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
3994 conf_set_int(conf, CONF_scrollbar, 0);
3996 } else if (!strcmp(p, "-sb")) {
3998 conf_set_int(conf, CONF_scrollbar, 1);
4000 } else if (!strcmp(p, "-name")) {
4004 } else if (!strcmp(p, "-xrm")) {
4006 provide_xrm_string(val);
4008 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
4012 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
4016 } else if (!strcmp(p, "-pgpfp")) {
4020 } else if(p[0] != '-' && (!do_everything ||
4021 process_nonoption_arg(p, conf,
4027 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
4035 #if GTK_CHECK_VERSION(2,0,0)
4043 uxsel_id *uxsel_input_add(int fd, int rwx) {
4044 uxsel_id *id = snew(uxsel_id);
4046 #if GTK_CHECK_VERSION(2,0,0)
4048 if (rwx & 1) flags |= G_IO_IN;
4049 if (rwx & 2) flags |= G_IO_OUT;
4050 if (rwx & 4) flags |= G_IO_PRI;
4051 id->chan = g_io_channel_unix_new(fd);
4052 g_io_channel_set_encoding(id->chan, NULL, NULL);
4053 id->watch_id = g_io_add_watch(id->chan, flags, fd_input_func, NULL);
4056 if (rwx & 1) flags |= GDK_INPUT_READ;
4057 if (rwx & 2) flags |= GDK_INPUT_WRITE;
4058 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
4060 id->id = gdk_input_add(fd, flags, fd_input_func, NULL);
4066 void uxsel_input_remove(uxsel_id *id) {
4067 #if GTK_CHECK_VERSION(2,0,0)
4068 g_source_remove(id->watch_id);
4069 g_io_channel_unref(id->chan);
4071 gdk_input_remove(id->id);
4076 int frontend_is_utf8(void *frontend)
4078 struct gui_data *inst = (struct gui_data *)frontend;
4079 return inst->ucsdata.line_codepage == CS_UTF8;
4082 char *setup_fonts_ucs(struct gui_data *inst)
4084 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
4085 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
4090 fs = conf_get_fontspec(inst->conf, CONF_font);
4091 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
4092 shadowboldoffset, shadowbold);
4094 return dupprintf("unable to load font \"%s\"", fs->name);
4097 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
4098 if (shadowbold || !fs->name[0]) {
4101 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
4102 shadowboldoffset, shadowbold);
4105 unifont_destroy(fonts[0]);
4106 return dupprintf("unable to load bold font \"%s\"", fs->name);
4110 fs = conf_get_fontspec(inst->conf, CONF_widefont);
4112 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
4113 shadowboldoffset, shadowbold);
4115 for (i = 0; i < 2; i++)
4117 unifont_destroy(fonts[i]);
4118 return dupprintf("unable to load wide font \"%s\"", fs->name);
4124 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
4125 if (shadowbold || !fs->name[0]) {
4128 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
4129 shadowboldoffset, shadowbold);
4131 for (i = 0; i < 3; i++)
4133 unifont_destroy(fonts[i]);
4134 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
4139 * Now we've got past all the possible error conditions, we can
4140 * actually update our state.
4143 for (i = 0; i < 4; i++) {
4145 unifont_destroy(inst->fonts[i]);
4146 inst->fonts[i] = fonts[i];
4149 inst->font_width = inst->fonts[0]->width;
4150 inst->font_height = inst->fonts[0]->height;
4152 inst->direct_to_font = init_ucs(&inst->ucsdata,
4153 conf_get_str(inst->conf, CONF_line_codepage),
4154 conf_get_int(inst->conf, CONF_utf8_override),
4155 inst->fonts[0]->public_charset,
4156 conf_get_int(inst->conf, CONF_vtmode));
4158 inst->drawtype = inst->fonts[0]->preferred_drawtype;
4163 void set_geom_hints(struct gui_data *inst)
4166 geom.min_width = inst->font_width + 2*inst->window_border;
4167 geom.min_height = inst->font_height + 2*inst->window_border;
4168 geom.max_width = geom.max_height = -1;
4169 geom.base_width = 2*inst->window_border;
4170 geom.base_height = 2*inst->window_border;
4171 geom.width_inc = inst->font_width;
4172 geom.height_inc = inst->font_height;
4173 geom.min_aspect = geom.max_aspect = 0;
4174 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
4175 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
4176 GDK_HINT_RESIZE_INC);
4179 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
4181 struct gui_data *inst = (struct gui_data *)data;
4182 term_clrsb(inst->term);
4185 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
4187 struct gui_data *inst = (struct gui_data *)data;
4188 term_pwron(inst->term, TRUE);
4190 ldisc_echoedit_update(inst->ldisc);
4193 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
4195 struct gui_data *inst = (struct gui_data *)data;
4196 term_copyall(inst->term);
4199 void special_menuitem(GtkMenuItem *item, gpointer data)
4201 struct gui_data *inst = (struct gui_data *)data;
4202 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
4206 inst->back->special(inst->backhandle, code);
4209 void about_menuitem(GtkMenuItem *item, gpointer data)
4211 struct gui_data *inst = (struct gui_data *)data;
4212 about_box(inst->window);
4215 void event_log_menuitem(GtkMenuItem *item, gpointer data)
4217 struct gui_data *inst = (struct gui_data *)data;
4218 showeventlog(inst->eventlogstuff, inst->window);
4221 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
4223 /* This maps colour indices in inst->conf to those used in inst->cols. */
4224 static const int ww[] = {
4225 256, 257, 258, 259, 260, 261,
4226 0, 8, 1, 9, 2, 10, 3, 11,
4227 4, 12, 5, 13, 6, 14, 7, 15
4229 struct gui_data *inst = (struct gui_data *)data;
4231 Conf *oldconf, *newconf;
4232 int i, j, need_size;
4234 assert(lenof(ww) == NCFGCOLOURS);
4236 if (inst->reconfiguring)
4239 inst->reconfiguring = TRUE;
4241 title = dupcat(appname, " Reconfiguration", NULL);
4243 oldconf = inst->conf;
4244 newconf = conf_copy(inst->conf);
4246 if (do_config_box(title, newconf, 1,
4247 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
4248 inst->conf = newconf;
4250 /* Pass new config data to the logging module */
4251 log_reconfig(inst->logctx, inst->conf);
4253 * Flush the line discipline's edit buffer in the case
4254 * where local editing has just been disabled.
4257 ldisc_configure(inst->ldisc, inst->conf);
4258 ldisc_echoedit_update(inst->ldisc);
4260 /* Pass new config data to the terminal */
4261 term_reconfig(inst->term, inst->conf);
4262 /* Pass new config data to the back end */
4264 inst->back->reconfig(inst->backhandle, inst->conf);
4266 cache_conf_values(inst);
4269 * Just setting inst->conf is sufficient to cause colour
4270 * setting changes to appear on the next ESC]R palette
4271 * reset. But we should also check whether any colour
4272 * settings have been changed, and revert the ones that have
4273 * to the new default, on the assumption that the user is
4274 * most likely to want an immediate update.
4276 for (i = 0; i < NCFGCOLOURS; i++) {
4277 for (j = 0; j < 3; j++)
4278 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
4279 conf_get_int_int(newconf, CONF_colours, i*3+j))
4282 real_palette_set(inst, ww[i],
4283 conf_get_int_int(newconf,CONF_colours,i*3+0),
4284 conf_get_int_int(newconf,CONF_colours,i*3+1),
4285 conf_get_int_int(newconf,CONF_colours,i*3+2));
4288 * If the default background has changed, we must
4289 * repaint the space in between the window border
4290 * and the text area.
4293 set_window_background(inst);
4294 draw_backing_rect(inst);
4300 * If the scrollbar needs to be shown, hidden, or moved
4301 * from one end to the other of the window, do so now.
4303 if (conf_get_int(oldconf, CONF_scrollbar) !=
4304 conf_get_int(newconf, CONF_scrollbar)) {
4305 if (conf_get_int(newconf, CONF_scrollbar))
4306 gtk_widget_show(inst->sbar);
4308 gtk_widget_hide(inst->sbar);
4310 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
4311 conf_get_int(newconf, CONF_scrollbar_on_left)) {
4312 gtk_box_reorder_child(inst->hbox, inst->sbar,
4313 conf_get_int(newconf, CONF_scrollbar_on_left)
4318 * Change the window title, if required.
4320 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
4321 conf_get_str(newconf, CONF_wintitle)))
4322 set_title(inst, conf_get_str(newconf, CONF_wintitle));
4323 set_window_titles(inst);
4326 * Redo the whole tangled fonts and Unicode mess if
4330 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
4331 conf_get_fontspec(newconf, CONF_font)->name) ||
4332 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
4333 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
4334 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
4335 conf_get_fontspec(newconf, CONF_widefont)->name) ||
4336 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
4337 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
4338 strcmp(conf_get_str(oldconf, CONF_line_codepage),
4339 conf_get_str(newconf, CONF_line_codepage)) ||
4340 conf_get_int(oldconf, CONF_utf8_override) !=
4341 conf_get_int(newconf, CONF_utf8_override) ||
4342 conf_get_int(oldconf, CONF_vtmode) !=
4343 conf_get_int(newconf, CONF_vtmode) ||
4344 conf_get_int(oldconf, CONF_shadowbold) !=
4345 conf_get_int(newconf, CONF_shadowbold) ||
4346 conf_get_int(oldconf, CONF_shadowboldoffset) !=
4347 conf_get_int(newconf, CONF_shadowboldoffset)) {
4348 char *errmsg = setup_fonts_ucs(inst);
4351 dupprintf("Could not change fonts in terminal window: %s\n",
4353 messagebox(inst->window, "Font setup error", msgboxtext,
4354 string_width("Could not change fonts in terminal window:"),
4365 * Resize the window.
4367 if (conf_get_int(oldconf, CONF_width) !=
4368 conf_get_int(newconf, CONF_width) ||
4369 conf_get_int(oldconf, CONF_height) !=
4370 conf_get_int(newconf, CONF_height) ||
4371 conf_get_int(oldconf, CONF_window_border) !=
4372 conf_get_int(newconf, CONF_window_border) ||
4374 set_geom_hints(inst);
4375 request_resize(inst, conf_get_int(newconf, CONF_width),
4376 conf_get_int(newconf, CONF_height));
4379 * The above will have caused a call to term_size() for
4380 * us if it happened. If the user has fiddled with only
4381 * the scrollback size, the above will not have
4382 * happened and we will need an explicit term_size()
4385 if (conf_get_int(oldconf, CONF_savelines) !=
4386 conf_get_int(newconf, CONF_savelines))
4387 term_size(inst->term, inst->term->rows, inst->term->cols,
4388 conf_get_int(newconf, CONF_savelines));
4391 term_invalidate(inst->term);
4394 * We do an explicit full redraw here to ensure the window
4395 * border has been redrawn as well as the text area.
4397 gtk_widget_queue_draw(inst->area);
4404 inst->reconfiguring = FALSE;
4407 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
4410 * Re-execing ourself is not an exact science under Unix. I do
4411 * the best I can by using /proc/self/exe if available and by
4412 * assuming argv[0] can be found on $PATH if not.
4414 * Note that we also have to reconstruct the elements of the
4415 * original argv which gtk swallowed, since the user wants the
4416 * new session to appear on the same X display as the old one.
4424 * Collect the arguments with which to re-exec ourself.
4426 va_start(ap, fd_to_close);
4427 n = 2; /* progname and terminating NULL */
4428 n += inst->ngtkargs;
4429 while (va_arg(ap, char *) != NULL)
4433 args = snewn(n, char *);
4434 args[0] = inst->progname;
4436 for (i = 0; i < inst->ngtkargs; i++)
4437 args[i+1] = inst->gtkargvstart[i];
4440 va_start(ap, fd_to_close);
4441 while ((args[i++] = va_arg(ap, char *)) != NULL);
4447 * Do the double fork.
4461 } else if (pid2 > 0) {
4463 * First child has successfully forked second child. My
4464 * Work Here Is Done. Note the use of _exit rather than
4465 * exit: the latter appears to cause destroy messages
4466 * to be sent to the X server. I suspect gtk uses
4473 * If we reach here, we are the second child, so we now
4474 * actually perform the exec.
4476 if (fd_to_close >= 0)
4479 execv("/proc/self/exe", args);
4480 execvp(inst->progname, args);
4487 waitpid(pid, &status, 0);
4492 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
4494 struct gui_data *inst = (struct gui_data *)gdata;
4496 * For this feature we must marshal conf and (possibly) pty_argv
4497 * into a byte stream, create a pipe, and send this byte stream
4498 * to the child through the pipe.
4500 int i, ret, sersize, size;
4505 if (pipe(pipefd) < 0) {
4510 size = sersize = conf_serialised_size(inst->conf);
4511 if (use_pty_argv && pty_argv) {
4512 for (i = 0; pty_argv[i]; i++)
4513 size += strlen(pty_argv[i]) + 1;
4516 data = snewn(size, char);
4517 conf_serialise(inst->conf, data);
4518 if (use_pty_argv && pty_argv) {
4520 for (i = 0; pty_argv[i]; i++) {
4521 strcpy(data + p, pty_argv[i]);
4522 p += strlen(pty_argv[i]) + 1;
4527 sprintf(option, "---[%d,%d]", pipefd[0], size);
4528 noncloexec(pipefd[0]);
4529 fork_and_exec_self(inst, pipefd[1], option, NULL);
4533 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
4536 perror("write to pipe");
4541 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
4543 int fd, i, ret, size, size_used;
4546 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
4547 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
4551 data = snewn(size, char);
4553 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
4556 perror("read from pipe");
4558 } else if (i < size) {
4559 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
4564 size_used = conf_deserialise(conf, data, size);
4565 if (use_pty_argv && size > size_used) {
4569 while (i < size && data[i]) i++;
4571 fprintf(stderr, "%s: malformed Duplicate Session data\n",
4578 pty_argv = snewn(n+1, char *);
4584 while (i < size && data[i]) i++;
4587 pty_argv[n++] = dupstr(p);
4596 void new_session_menuitem(GtkMenuItem *item, gpointer data)
4598 struct gui_data *inst = (struct gui_data *)data;
4600 fork_and_exec_self(inst, -1, NULL);
4603 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
4605 struct gui_data *inst = (struct gui_data *)data;
4608 logevent(inst, "----- Session restarted -----");
4609 term_pwron(inst->term, FALSE);
4610 start_backend(inst);
4611 inst->exited = FALSE;
4615 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
4617 struct gui_data *inst = (struct gui_data *)data;
4618 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4620 fork_and_exec_self(inst, -1, "-load", str, NULL);
4623 void saved_session_freedata(GtkMenuItem *item, gpointer data)
4625 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4630 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
4632 struct gui_data *inst = (struct gui_data *)data;
4633 struct sesslist sesslist;
4636 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
4637 (GtkCallback)gtk_widget_destroy, NULL);
4639 get_sesslist(&sesslist, TRUE);
4640 /* skip sesslist.sessions[0] == Default Settings */
4641 for (i = 1; i < sesslist.nsessions; i++) {
4642 GtkWidget *menuitem =
4643 gtk_menu_item_new_with_label(sesslist.sessions[i]);
4644 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4645 gtk_widget_show(menuitem);
4646 g_object_set_data(G_OBJECT(menuitem), "user-data",
4647 dupstr(sesslist.sessions[i]));
4648 g_signal_connect(G_OBJECT(menuitem), "activate",
4649 G_CALLBACK(saved_session_menuitem),
4651 g_signal_connect(G_OBJECT(menuitem), "destroy",
4652 G_CALLBACK(saved_session_freedata),
4655 if (sesslist.nsessions <= 1) {
4656 GtkWidget *menuitem =
4657 gtk_menu_item_new_with_label("(No sessions)");
4658 gtk_widget_set_sensitive(menuitem, FALSE);
4659 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4660 gtk_widget_show(menuitem);
4662 get_sesslist(&sesslist, FALSE); /* free up */
4665 void set_window_icon(GtkWidget *window, const char *const *const *icon,
4668 #if GTK_CHECK_VERSION(2,0,0)
4673 GdkBitmap *iconmask;
4679 gtk_widget_realize(window);
4680 #if GTK_CHECK_VERSION(2,0,0)
4681 gtk_window_set_icon(GTK_WINDOW(window),
4682 gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
4684 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
4685 &iconmask, NULL, (gchar **)icon[0]);
4686 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
4689 #if GTK_CHECK_VERSION(2,0,0)
4691 for (n = 0; n < n_icon; n++) {
4693 g_list_append(iconlist,
4694 gdk_pixbuf_new_from_xpm_data((const gchar **)
4697 gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
4701 void update_specials_menu(void *frontend)
4703 struct gui_data *inst = (struct gui_data *)frontend;
4705 const struct telnet_special *specials;
4708 specials = inst->back->get_specials(inst->backhandle);
4712 /* I believe this disposes of submenus too. */
4713 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
4714 (GtkCallback)gtk_widget_destroy, NULL);
4717 GtkWidget *menu = inst->specialsmenu;
4718 /* A lame "stack" for submenus that will do for now. */
4719 GtkWidget *saved_menu = NULL;
4721 for (i = 0; nesting > 0; i++) {
4722 GtkWidget *menuitem = NULL;
4723 switch (specials[i].code) {
4725 assert (nesting < 2);
4726 saved_menu = menu; /* XXX lame stacking */
4727 menu = gtk_menu_new();
4728 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4729 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
4730 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
4731 gtk_widget_show(menuitem);
4738 menu = saved_menu; /* XXX lame stacking */
4743 menuitem = gtk_menu_item_new();
4746 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4747 g_object_set_data(G_OBJECT(menuitem), "user-data",
4748 GINT_TO_POINTER(specials[i].code));
4749 g_signal_connect(G_OBJECT(menuitem), "activate",
4750 G_CALLBACK(special_menuitem), inst);
4754 gtk_container_add(GTK_CONTAINER(menu), menuitem);
4755 gtk_widget_show(menuitem);
4758 gtk_widget_show(inst->specialsitem1);
4759 gtk_widget_show(inst->specialsitem2);
4761 gtk_widget_hide(inst->specialsitem1);
4762 gtk_widget_hide(inst->specialsitem2);
4766 static void start_backend(struct gui_data *inst)
4768 extern Backend *select_backend(Conf *conf);
4773 inst->back = select_backend(inst->conf);
4775 error = inst->back->init((void *)inst, &inst->backhandle,
4777 conf_get_str(inst->conf, CONF_host),
4778 conf_get_int(inst->conf, CONF_port),
4780 conf_get_int(inst->conf, CONF_tcp_nodelay),
4781 conf_get_int(inst->conf, CONF_tcp_keepalives));
4784 char *msg = dupprintf("Unable to open connection to %s:\n%s",
4785 conf_get_str(inst->conf, CONF_host), error);
4786 inst->exited = TRUE;
4787 fatal_message_box(inst->window, msg);
4792 s = conf_get_str(inst->conf, CONF_wintitle);
4794 set_title_and_icon(inst, s, s);
4796 char *title = make_default_wintitle(realhost);
4797 set_title_and_icon(inst, title, title);
4802 inst->back->provide_logctx(inst->backhandle, inst->logctx);
4804 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
4807 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
4810 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4813 int pt_main(int argc, char **argv)
4815 extern int cfgbox(Conf *conf);
4816 struct gui_data *inst;
4818 setlocale(LC_CTYPE, "");
4821 * Create an instance structure and initialise to zeroes
4823 inst = snew(struct gui_data);
4824 memset(inst, 0, sizeof(*inst));
4825 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
4826 inst->busy_status = BUSY_NOT;
4827 inst->conf = conf_new();
4828 inst->wintitle = inst->icontitle = NULL;
4829 inst->quit_fn_scheduled = FALSE;
4830 inst->idle_fn_scheduled = FALSE;
4831 inst->drawtype = DRAWTYPE_DEFAULT;
4832 #if GTK_CHECK_VERSION(3,4,0)
4833 inst->cumulative_scroll = 0.0;
4836 /* defer any child exit handling until we're ready to deal with
4838 block_signal(SIGCHLD, 1);
4840 inst->progname = argv[0];
4842 * Copy the original argv before letting gtk_init fiddle with
4843 * it. It will be required later.
4847 inst->gtkargvstart = snewn(argc-1, char *);
4848 for (i = 1; i < argc; i++)
4849 inst->gtkargvstart[i-1] = dupstr(argv[i]);
4851 gtk_init(&argc, &argv);
4852 inst->ngtkargs = oldargc - argc;
4855 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
4856 read_dupsession_data(inst, inst->conf, argv[1]);
4857 /* Splatter this argument so it doesn't clutter a ps listing */
4858 smemclr(argv[1], strlen(argv[1]));
4860 /* By default, we bring up the config dialog, rather than launching
4861 * a session. This gets set to TRUE if something happens to change
4862 * that (e.g., a hostname is specified on the command-line). */
4863 int allow_launch = FALSE;
4864 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
4865 exit(1); /* pre-defaults pass to get -class */
4866 do_defaults(NULL, inst->conf);
4867 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
4868 exit(1); /* post-defaults, do everything */
4870 cmdline_run_saved(inst->conf);
4873 allow_launch = TRUE;
4875 if ((!allow_launch || !conf_launchable(inst->conf)) &&
4876 !cfgbox(inst->conf))
4877 exit(0); /* config box hit Cancel */
4880 if (!compound_text_atom)
4881 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
4882 if (!utf8_string_atom)
4883 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
4885 inst->area = gtk_drawing_area_new();
4887 #if GTK_CHECK_VERSION(2,0,0)
4888 inst->imc = gtk_im_multicontext_new();
4892 char *errmsg = setup_fonts_ucs(inst);
4894 fprintf(stderr, "%s: %s\n", appname, errmsg);
4898 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4900 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
4902 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
4903 winclass, winclass);
4907 * Set up the colour map.
4909 palette_reset(inst);
4911 inst->width = conf_get_int(inst->conf, CONF_width);
4912 inst->height = conf_get_int(inst->conf, CONF_height);
4913 cache_conf_values(inst);
4915 init_clipboard(inst);
4917 set_geom_hints(inst);
4919 #if GTK_CHECK_VERSION(3,0,0)
4920 gtk_window_set_default_geometry(GTK_WINDOW(inst->window),
4921 inst->width, inst->height);
4924 int w = inst->font_width * inst->width + 2*inst->window_border;
4925 int h = inst->font_height * inst->height + 2*inst->window_border;
4926 #if GTK_CHECK_VERSION(2,0,0)
4927 gtk_widget_set_size_request(inst->area, w, h);
4929 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
4934 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
4935 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
4936 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
4938 * We always create the scrollbar; it remains invisible if
4939 * unwanted, so we can pop it up quickly if it suddenly becomes
4942 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
4943 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4944 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
4945 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
4946 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4948 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
4950 gtk_widget_show(inst->area);
4951 if (conf_get_int(inst->conf, CONF_scrollbar))
4952 gtk_widget_show(inst->sbar);
4954 gtk_widget_hide(inst->sbar);
4955 gtk_widget_show(GTK_WIDGET(inst->hbox));
4957 #if GTK_CHECK_VERSION(2,0,0)
4958 if (inst->geometry) {
4959 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
4963 int x = inst->xpos, y = inst->ypos;
4965 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
4966 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
4967 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
4968 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
4969 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
4973 g_signal_connect(G_OBJECT(inst->window), "destroy",
4974 G_CALLBACK(destroy), inst);
4975 g_signal_connect(G_OBJECT(inst->window), "delete_event",
4976 G_CALLBACK(delete_window), inst);
4977 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
4978 G_CALLBACK(key_event), inst);
4979 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
4980 G_CALLBACK(key_event), inst);
4981 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
4982 G_CALLBACK(focus_event), inst);
4983 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
4984 G_CALLBACK(focus_event), inst);
4985 g_signal_connect(G_OBJECT(inst->area), "configure_event",
4986 G_CALLBACK(configure_area), inst);
4987 #if GTK_CHECK_VERSION(3,0,0)
4988 g_signal_connect(G_OBJECT(inst->area), "draw",
4989 G_CALLBACK(draw_area), inst);
4991 g_signal_connect(G_OBJECT(inst->area), "expose_event",
4992 G_CALLBACK(expose_area), inst);
4994 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
4995 G_CALLBACK(button_event), inst);
4996 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
4997 G_CALLBACK(button_event), inst);
4998 #if GTK_CHECK_VERSION(2,0,0)
4999 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
5000 G_CALLBACK(scroll_event), inst);
5002 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
5003 G_CALLBACK(motion_event), inst);
5004 #if GTK_CHECK_VERSION(2,0,0)
5005 g_signal_connect(G_OBJECT(inst->imc), "commit",
5006 G_CALLBACK(input_method_commit_event), inst);
5008 if (conf_get_int(inst->conf, CONF_scrollbar))
5009 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
5010 G_CALLBACK(scrollbar_moved), inst);
5011 gtk_widget_add_events(GTK_WIDGET(inst->area),
5012 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
5013 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
5014 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK
5015 #if GTK_CHECK_VERSION(3,4,0)
5016 | GDK_SMOOTH_SCROLL_MASK
5021 extern const char *const *const main_icon[];
5022 extern const int n_main_icon;
5023 set_window_icon(inst->window, main_icon, n_main_icon);
5026 gtk_widget_show(inst->window);
5028 set_window_background(inst);
5031 * Set up the Ctrl+rightclick context menu.
5034 GtkWidget *menuitem;
5036 extern const int use_event_log, new_session, saved_sessions;
5038 inst->menu = gtk_menu_new();
5040 #define MKMENUITEM(title, func) do \
5042 menuitem = gtk_menu_item_new_with_label(title); \
5043 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
5044 gtk_widget_show(menuitem); \
5045 g_signal_connect(G_OBJECT(menuitem), "activate", \
5046 G_CALLBACK(func), inst); \
5049 #define MKSUBMENU(title) do \
5051 menuitem = gtk_menu_item_new_with_label(title); \
5052 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
5053 gtk_widget_show(menuitem); \
5056 #define MKSEP() do \
5058 menuitem = gtk_menu_item_new(); \
5059 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
5060 gtk_widget_show(menuitem); \
5064 MKMENUITEM("New Session...", new_session_menuitem);
5065 MKMENUITEM("Restart Session", restart_session_menuitem);
5066 inst->restartitem = menuitem;
5067 gtk_widget_set_sensitive(inst->restartitem, FALSE);
5068 MKMENUITEM("Duplicate Session", dup_session_menuitem);
5069 if (saved_sessions) {
5070 inst->sessionsmenu = gtk_menu_new();
5071 /* sessionsmenu will be updated when it's invoked */
5072 /* XXX is this the right way to do dynamic menus in Gtk? */
5073 MKMENUITEM("Saved Sessions", update_savedsess_menu);
5074 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
5075 inst->sessionsmenu);
5078 MKMENUITEM("Change Settings...", change_settings_menuitem);
5081 MKMENUITEM("Event Log", event_log_menuitem);
5082 MKSUBMENU("Special Commands");
5083 inst->specialsmenu = gtk_menu_new();
5084 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
5085 inst->specialsitem1 = menuitem;
5087 inst->specialsitem2 = menuitem;
5088 gtk_widget_hide(inst->specialsitem1);
5089 gtk_widget_hide(inst->specialsitem2);
5090 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
5091 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
5092 MKMENUITEM("Copy All", copy_all_menuitem);
5094 s = dupcat("About ", appname, NULL);
5095 MKMENUITEM(s, about_menuitem);
5102 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
5103 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
5104 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
5105 inst->blankcursor = make_mouse_ptr(inst, -1);
5106 inst->currcursor = inst->textcursor;
5107 show_mouseptr(inst, 1);
5109 inst->eventlogstuff = eventlogstuff_new();
5111 request_callback_notifications(notify_toplevel_callback, inst);
5113 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
5114 inst->logctx = log_init(inst, inst->conf);
5115 term_provide_logctx(inst->term, inst->logctx);
5119 term_size(inst->term, inst->height, inst->width,
5120 conf_get_int(inst->conf, CONF_savelines));
5122 start_backend(inst);
5124 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
5126 /* now we're reday to deal with the child exit handler being
5128 block_signal(SIGCHLD, 0);
5131 * Block SIGPIPE: if we attempt Duplicate Session or similar
5132 * and it falls over in some way, we certainly don't want
5133 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
5134 * this _after_ (at least pterm) forks off its child process,
5135 * since the child wants SIGPIPE handled in the usual way.
5137 block_signal(SIGPIPE, 1);
5139 inst->exited = FALSE;