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"
42 #include <X11/Xutil.h>
43 #include <X11/Xatom.h>
46 #define CAT2(x,y) x ## y
47 #define CAT(x,y) CAT2(x,y)
48 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
50 #if GTK_CHECK_VERSION(2,0,0)
51 ASSERT(sizeof(long) <= sizeof(gsize));
52 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
53 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
55 ASSERT(sizeof(long) <= sizeof(gpointer));
56 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
57 #define GPOINTER_TO_LONG(p) ((long)(p))
60 /* Colours come in two flavours: configurable, and xterm-extended. */
61 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
62 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
64 GdkAtom compound_text_atom, utf8_string_atom;
66 extern char **pty_argv; /* declared in pty.c */
67 extern int use_pty_argv;
70 * Timers are global across all sessions (even if we were handling
71 * multiple sessions, which we aren't), so the current timer ID is
74 static guint timer_id = 0;
77 GtkWidget *window, *area, *sbar;
79 GtkAdjustment *sbar_adjust;
80 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
82 GtkWidget *sessionsmenu;
83 #ifndef NO_BACKING_PIXMAPS
86 #if GTK_CHECK_VERSION(2,0,0)
89 unifont *fonts[4]; /* normal, bold, wide, widebold */
90 #if GTK_CHECK_VERSION(2,0,0)
93 int xpos, ypos, gotpos, gravity;
95 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
96 GdkColor cols[NALLCOLOURS];
97 #if !GTK_CHECK_VERSION(3,0,0)
100 wchar_t *pastein_data;
102 int pastein_data_len;
103 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
104 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
105 int font_width, font_height;
108 int mouseptr_visible;
110 guint toplevel_callback_idle_id;
111 int idle_fn_scheduled, quit_fn_scheduled;
116 int master_fd, master_func_id;
123 struct unicode_data ucsdata;
126 char *progname, **gtkargvstart;
128 guint32 input_event_time; /* Timestamp of the most recent input event. */
130 /* Cached things out of conf that we refer to a lot */
137 static void cache_conf_values(struct gui_data *inst)
139 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
140 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
141 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
145 struct gui_data *inst;
146 unifont_drawctx uctx;
149 static int send_raw_mouse;
151 static const char *app_name = "pterm";
153 static void start_backend(struct gui_data *inst);
154 static void exit_callback(void *vinst);
156 char *x_get_default(const char *key)
158 #ifndef NOT_X_WINDOWS
159 return XGetDefault(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
166 void connection_fatal(void *frontend, const char *p, ...)
168 struct gui_data *inst = (struct gui_data *)frontend;
173 msg = dupvprintf(p, ap);
175 fatal_message_box(inst->window, msg);
178 queue_toplevel_callback(exit_callback, inst);
182 * Default settings that are specific to pterm.
184 FontSpec *platform_default_fontspec(const char *name)
186 if (!strcmp(name, "Font"))
187 return fontspec_new(DEFAULT_GTK_FONT);
189 return fontspec_new("");
192 Filename *platform_default_filename(const char *name)
194 if (!strcmp(name, "LogFileName"))
195 return filename_from_str("putty.log");
197 return filename_from_str("");
200 char *platform_default_s(const char *name)
202 if (!strcmp(name, "SerialLine"))
203 return dupstr("/dev/ttyS0");
207 int platform_default_i(const char *name, int def)
209 if (!strcmp(name, "CloseOnExit"))
210 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
211 if (!strcmp(name, "WinNameAlways"))
212 return 0; /* X natively supports icon titles, so use 'em by default */
216 /* Dummy routine, only required in plink. */
217 void frontend_echoedit_update(void *frontend, int echo, int edit)
221 char *get_ttymode(void *frontend, const char *mode)
223 struct gui_data *inst = (struct gui_data *)frontend;
224 return term_get_ttymode(inst->term, mode);
227 int from_backend(void *frontend, int is_stderr, const char *data, int len)
229 struct gui_data *inst = (struct gui_data *)frontend;
230 return term_data(inst->term, is_stderr, data, len);
233 int from_backend_untrusted(void *frontend, const char *data, int len)
235 struct gui_data *inst = (struct gui_data *)frontend;
236 return term_data_untrusted(inst->term, data, len);
239 int from_backend_eof(void *frontend)
241 return TRUE; /* do respond to incoming EOF with outgoing */
244 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
246 struct gui_data *inst = (struct gui_data *)p->frontend;
248 ret = cmdline_get_passwd_input(p, in, inlen);
250 ret = term_get_userpass_input(inst->term, p, in, inlen);
254 void logevent(void *frontend, const char *string)
256 struct gui_data *inst = (struct gui_data *)frontend;
258 log_eventlog(inst->logctx, string);
260 logevent_dlg(inst->eventlogstuff, string);
263 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
265 struct gui_data *inst = (struct gui_data *)frontend;
268 return inst->font_height;
270 return inst->font_width;
274 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
275 * into a cooked one (SELECT, EXTEND, PASTE).
277 * In Unix, this is not configurable; the X button arrangement is
278 * rock-solid across all applications, everyone has a three-button
279 * mouse or a means of faking it, and there is no need to switch
280 * buttons around at all.
282 static Mouse_Button translate_button(Mouse_Button button)
284 /* struct gui_data *inst = (struct gui_data *)frontend; */
286 if (button == MBT_LEFT)
288 if (button == MBT_MIDDLE)
290 if (button == MBT_RIGHT)
292 return 0; /* shouldn't happen */
296 * Return the top-level GtkWindow associated with a particular
297 * front end instance.
299 void *get_window(void *frontend)
301 struct gui_data *inst = (struct gui_data *)frontend;
306 * Minimise or restore the window in response to a server-side
309 void set_iconic(void *frontend, int iconic)
312 * GTK 1.2 doesn't know how to do this.
314 #if GTK_CHECK_VERSION(2,0,0)
315 struct gui_data *inst = (struct gui_data *)frontend;
317 gtk_window_iconify(GTK_WINDOW(inst->window));
319 gtk_window_deiconify(GTK_WINDOW(inst->window));
324 * Move the window in response to a server-side request.
326 void move_window(void *frontend, int x, int y)
328 struct gui_data *inst = (struct gui_data *)frontend;
330 * I assume that when the GTK version of this call is available
331 * we should use it. Not sure how it differs from the GDK one,
334 #if GTK_CHECK_VERSION(2,0,0)
335 gtk_window_move(GTK_WINDOW(inst->window), x, y);
337 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
342 * Move the window to the top or bottom of the z-order in response
343 * to a server-side request.
345 void set_zorder(void *frontend, int top)
347 struct gui_data *inst = (struct gui_data *)frontend;
349 gdk_window_raise(gtk_widget_get_window(inst->window));
351 gdk_window_lower(gtk_widget_get_window(inst->window));
355 * Refresh the window in response to a server-side request.
357 void refresh_window(void *frontend)
359 struct gui_data *inst = (struct gui_data *)frontend;
360 term_invalidate(inst->term);
364 * Maximise or restore the window in response to a server-side
367 void set_zoomed(void *frontend, int zoomed)
370 * GTK 1.2 doesn't know how to do this.
372 #if GTK_CHECK_VERSION(2,0,0)
373 struct gui_data *inst = (struct gui_data *)frontend;
375 gtk_window_maximize(GTK_WINDOW(inst->window));
377 gtk_window_unmaximize(GTK_WINDOW(inst->window));
382 * Report whether the window is iconic, for terminal reports.
384 int is_iconic(void *frontend)
386 struct gui_data *inst = (struct gui_data *)frontend;
387 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
391 * Report the window's position, for terminal reports.
393 void get_window_pos(void *frontend, int *x, int *y)
395 struct gui_data *inst = (struct gui_data *)frontend;
397 * I assume that when the GTK version of this call is available
398 * we should use it. Not sure how it differs from the GDK one,
401 #if GTK_CHECK_VERSION(2,0,0)
402 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
404 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
409 * Report the window's pixel size, for terminal reports.
411 void get_window_pixels(void *frontend, int *x, int *y)
413 struct gui_data *inst = (struct gui_data *)frontend;
415 * I assume that when the GTK version of this call is available
416 * we should use it. Not sure how it differs from the GDK one,
419 #if GTK_CHECK_VERSION(2,0,0)
420 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
422 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
427 * Return the window or icon title.
429 char *get_window_title(void *frontend, int icon)
431 struct gui_data *inst = (struct gui_data *)frontend;
432 return icon ? inst->icontitle : inst->wintitle;
435 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
437 struct gui_data *inst = (struct gui_data *)data;
438 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
439 if (!reallyclose(inst))
445 static void update_mouseptr(struct gui_data *inst)
447 switch (inst->busy_status) {
449 if (!inst->mouseptr_visible) {
450 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
452 } else if (send_raw_mouse) {
453 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
456 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
460 case BUSY_WAITING: /* XXX can we do better? */
462 /* We always display these cursors. */
463 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
471 static void show_mouseptr(struct gui_data *inst, int show)
473 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
475 inst->mouseptr_visible = show;
476 update_mouseptr(inst);
479 static void draw_backing_rect(struct gui_data *inst);
481 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
483 struct gui_data *inst = (struct gui_data *)data;
484 int w, h, need_size = 0;
487 * See if the terminal size has changed, in which case we must
488 * let the terminal know.
490 w = (event->width - 2*inst->window_border) / inst->font_width;
491 h = (event->height - 2*inst->window_border) / inst->font_height;
492 if (w != inst->width || h != inst->height) {
495 conf_set_int(inst->conf, CONF_width, inst->width);
496 conf_set_int(inst->conf, CONF_height, inst->height);
500 #ifndef NO_BACKING_PIXMAPS
502 gdk_pixmap_unref(inst->pixmap);
506 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
507 (w * inst->font_width + 2*inst->window_border),
508 (h * inst->font_height + 2*inst->window_border), -1);
511 draw_backing_rect(inst);
513 if (need_size && inst->term) {
514 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
518 term_invalidate(inst->term);
520 #if GTK_CHECK_VERSION(2,0,0)
521 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
527 #ifdef DRAW_TEXT_CAIRO
528 static void cairo_setup_dctx(struct draw_ctx *dctx)
530 cairo_get_matrix(dctx->uctx.u.cairo.cr,
531 &dctx->uctx.u.cairo.origmatrix);
532 cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
533 cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
534 cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
535 /* This antialiasing setting appears to be ignored for Pango
536 * font rendering but honoured for stroking and filling paths;
537 * I don't quite understand the logic of that, but I won't
538 * complain since it's exactly what I happen to want */
539 cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
543 #if GTK_CHECK_VERSION(3,0,0)
544 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
546 struct gui_data *inst = (struct gui_data *)data;
549 struct draw_ctx adctx, *dctx = &adctx;
550 GdkRectangle dirtyrect;
553 dctx->uctx.type = DRAWTYPE_CAIRO;
554 dctx->uctx.u.cairo.widget = widget;
555 dctx->uctx.u.cairo.cr = cr;
556 cairo_setup_dctx(dctx);
558 gdk_cairo_get_clip_rectangle(cr, &dirtyrect);
561 * As in window.c, we clear the 'immediately' flag in the
562 * term_paint() call if the terminal has an update pending, in
563 * case we're constrained within this event to only draw on
564 * the exposed rectangle of the window. (Because if the whole
565 * of a character cell needs a redraw due to a terminal
566 * contents change, the last thing we want is to give it a
567 * _partial_ redraw here due to system-imposed clipping, and
568 * then have the next main terminal update believe it's been
571 * I don't actually know if GTK draw events will constrain us
572 * in this way, but it's best to be careful...
574 term_paint(inst->term, dctx,
575 (dirtyrect.x - inst->window_border) / inst->font_width,
576 (dirtyrect.y - inst->window_border) / inst->font_height,
577 (dirtyrect.x + dirtyrect.width -
578 inst->window_border) / inst->font_width,
579 (dirtyrect.y + dirtyrect.height -
580 inst->window_border) / inst->font_height,
581 !inst->term->window_update_pending);
587 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
589 struct gui_data *inst = (struct gui_data *)data;
591 #ifndef NO_BACKING_PIXMAPS
593 * Pass the exposed rectangle to terminal.c, which will call us
594 * back to do the actual painting.
597 gdk_draw_pixmap(gtk_widget_get_window(widget),
598 (gtk_widget_get_style(widget)->fg_gc
599 [gtk_widget_get_state(widget)]),
601 event->area.x, event->area.y,
602 event->area.x, event->area.y,
603 event->area.width, event->area.height);
607 Context ctx = get_ctx(inst);
608 term_paint(inst->term, ctx,
609 (event->area.x - inst->window_border) / inst->font_width,
610 (event->area.y - inst->window_border) / inst->font_height,
611 (event->area.x + event->area.width -
612 inst->window_border) / inst->font_width,
613 (event->area.y + event->area.height -
614 inst->window_border) / inst->font_height,
615 !inst->term->window_update_pending);
624 #define KEY_PRESSED(k) \
625 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
627 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
629 struct gui_data *inst = (struct gui_data *)data;
631 wchar_t ucsoutput[2];
632 int ucsval, start, end, special, output_charset, use_ucsoutput;
633 int nethack_mode, app_keypad_mode;
635 /* Remember the timestamp. */
636 inst->input_event_time = event->time;
638 /* By default, nothing is generated. */
640 special = use_ucsoutput = FALSE;
641 output_charset = CS_ISO8859_1;
643 #ifdef KEY_EVENT_DIAGNOSTICS
645 * Condition this mess in if you want to debug keyboard events as
646 * they come in to this function (e.g. because some particular
647 * port of GDK is producing an unexpected arrangement of
650 #define TRY(val, prefix, string) \
651 if ((val) == prefix ## string) printf("%s", #string); else
652 #define GIVE_UP(val) \
653 printf("%d", (int)(val))
654 #define TRY_MASK(val, prefix, string, suffix) \
655 if ((val) & prefix ## string ## suffix) { \
656 (val) &= ~ prefix ## string ## suffix; \
657 printf("%s", #string); \
658 if (val) printf("|"); \
660 #define GIVE_UP_MASK(val) \
663 if ((val)) printf("%d", (int)(val)); \
666 printf("key_event: type=");
667 TRY(event->type, GDK_KEY_, PRESS)
668 TRY(event->type, GDK_KEY_, RELEASE)
669 GIVE_UP(event->type);
671 TRY(event->keyval, GDK_KEY_, Alt_L)
672 TRY(event->keyval, GDK_KEY_, Alt_R)
673 TRY(event->keyval, GDK_KEY_, BackSpace)
674 TRY(event->keyval, GDK_KEY_, Begin)
675 TRY(event->keyval, GDK_KEY_, Break)
676 TRY(event->keyval, GDK_KEY_, Delete)
677 TRY(event->keyval, GDK_KEY_, Down)
678 TRY(event->keyval, GDK_KEY_, End)
679 TRY(event->keyval, GDK_KEY_, Escape)
680 TRY(event->keyval, GDK_KEY_, F10)
681 TRY(event->keyval, GDK_KEY_, F11)
682 TRY(event->keyval, GDK_KEY_, F12)
683 TRY(event->keyval, GDK_KEY_, F13)
684 TRY(event->keyval, GDK_KEY_, F14)
685 TRY(event->keyval, GDK_KEY_, F15)
686 TRY(event->keyval, GDK_KEY_, F16)
687 TRY(event->keyval, GDK_KEY_, F17)
688 TRY(event->keyval, GDK_KEY_, F18)
689 TRY(event->keyval, GDK_KEY_, F19)
690 TRY(event->keyval, GDK_KEY_, F1)
691 TRY(event->keyval, GDK_KEY_, F20)
692 TRY(event->keyval, GDK_KEY_, F2)
693 TRY(event->keyval, GDK_KEY_, F3)
694 TRY(event->keyval, GDK_KEY_, F4)
695 TRY(event->keyval, GDK_KEY_, F5)
696 TRY(event->keyval, GDK_KEY_, F6)
697 TRY(event->keyval, GDK_KEY_, F7)
698 TRY(event->keyval, GDK_KEY_, F8)
699 TRY(event->keyval, GDK_KEY_, F9)
700 TRY(event->keyval, GDK_KEY_, Home)
701 TRY(event->keyval, GDK_KEY_, Insert)
702 TRY(event->keyval, GDK_KEY_, ISO_Left_Tab)
703 TRY(event->keyval, GDK_KEY_, KP_0)
704 TRY(event->keyval, GDK_KEY_, KP_1)
705 TRY(event->keyval, GDK_KEY_, KP_2)
706 TRY(event->keyval, GDK_KEY_, KP_3)
707 TRY(event->keyval, GDK_KEY_, KP_4)
708 TRY(event->keyval, GDK_KEY_, KP_5)
709 TRY(event->keyval, GDK_KEY_, KP_6)
710 TRY(event->keyval, GDK_KEY_, KP_7)
711 TRY(event->keyval, GDK_KEY_, KP_8)
712 TRY(event->keyval, GDK_KEY_, KP_9)
713 TRY(event->keyval, GDK_KEY_, KP_Add)
714 TRY(event->keyval, GDK_KEY_, KP_Begin)
715 TRY(event->keyval, GDK_KEY_, KP_Decimal)
716 TRY(event->keyval, GDK_KEY_, KP_Delete)
717 TRY(event->keyval, GDK_KEY_, KP_Divide)
718 TRY(event->keyval, GDK_KEY_, KP_Down)
719 TRY(event->keyval, GDK_KEY_, KP_End)
720 TRY(event->keyval, GDK_KEY_, KP_Enter)
721 TRY(event->keyval, GDK_KEY_, KP_Home)
722 TRY(event->keyval, GDK_KEY_, KP_Insert)
723 TRY(event->keyval, GDK_KEY_, KP_Left)
724 TRY(event->keyval, GDK_KEY_, KP_Multiply)
725 TRY(event->keyval, GDK_KEY_, KP_Page_Down)
726 TRY(event->keyval, GDK_KEY_, KP_Page_Up)
727 TRY(event->keyval, GDK_KEY_, KP_Right)
728 TRY(event->keyval, GDK_KEY_, KP_Subtract)
729 TRY(event->keyval, GDK_KEY_, KP_Up)
730 TRY(event->keyval, GDK_KEY_, Left)
731 TRY(event->keyval, GDK_KEY_, Meta_L)
732 TRY(event->keyval, GDK_KEY_, Meta_R)
733 TRY(event->keyval, GDK_KEY_, Num_Lock)
734 TRY(event->keyval, GDK_KEY_, Page_Down)
735 TRY(event->keyval, GDK_KEY_, Page_Up)
736 TRY(event->keyval, GDK_KEY_, Return)
737 TRY(event->keyval, GDK_KEY_, Right)
738 TRY(event->keyval, GDK_KEY_, Tab)
739 TRY(event->keyval, GDK_KEY_, Up)
740 TRY(event->keyval, GDK_KEY_, Shift_L)
741 TRY(event->keyval, GDK_KEY_, Shift_R)
742 TRY(event->keyval, GDK_KEY_, Control_L)
743 TRY(event->keyval, GDK_KEY_, Control_R)
744 TRY(event->keyval, GDK_KEY_, Caps_Lock)
745 TRY(event->keyval, GDK_KEY_, Shift_Lock)
746 TRY(event->keyval, GDK_KEY_, Super_L)
747 TRY(event->keyval, GDK_KEY_, Super_R)
748 TRY(event->keyval, GDK_KEY_, Hyper_L)
749 TRY(event->keyval, GDK_KEY_, Hyper_R)
750 GIVE_UP(event->keyval);
753 int val = event->state;
754 TRY_MASK(val, GDK_, SHIFT, _MASK)
755 TRY_MASK(val, GDK_, LOCK, _MASK)
756 TRY_MASK(val, GDK_, CONTROL, _MASK)
757 TRY_MASK(val, GDK_, MOD1, _MASK)
758 TRY_MASK(val, GDK_, MOD2, _MASK)
759 TRY_MASK(val, GDK_, MOD3, _MASK)
760 TRY_MASK(val, GDK_, MOD4, _MASK)
761 TRY_MASK(val, GDK_, MOD5, _MASK)
762 TRY_MASK(val, GDK_, SUPER, _MASK)
763 TRY_MASK(val, GDK_, HYPER, _MASK)
764 TRY_MASK(val, GDK_, META, _MASK)
767 printf(" hardware_keycode=%d is_modifier=%s\n",
768 (int)event->hardware_keycode, event->is_modifier ? "TRUE" : "FALSE");
769 #endif /* KEY_EVENT_DIAGNOSTICS */
772 * If Alt is being released after typing an Alt+numberpad
773 * sequence, we should generate the code that was typed.
775 * Note that we only do this if more than one key was actually
776 * pressed - I don't think Alt+NumPad4 should be ^D or that
777 * Alt+NumPad3 should be ^C, for example. There's no serious
778 * inconvenience in having to type a zero before a single-digit
781 if (event->type == GDK_KEY_RELEASE) {
782 if ((event->keyval == GDK_KEY_Meta_L ||
783 event->keyval == GDK_KEY_Meta_R ||
784 event->keyval == GDK_KEY_Alt_L ||
785 event->keyval == GDK_KEY_Alt_R) &&
786 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
788 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
791 * FIXME: we might usefully try to do something clever here
792 * about interpreting the generated key code in a way that's
793 * appropriate to the line code page.
795 output[0] = inst->alt_keycode;
799 #if GTK_CHECK_VERSION(2,0,0)
800 if (gtk_im_context_filter_keypress(inst->imc, event))
805 if (event->type == GDK_KEY_PRESS) {
809 printf("keypress: keyval = %04x, state = %08x; string =",
810 event->keyval, event->state);
811 for (i = 0; event->string[i]; i++)
812 printf(" %02x", (unsigned char) event->string[i]);
818 * NYI: Compose key (!!! requires Unicode faff before even trying)
822 * If Alt has just been pressed, we start potentially
823 * accumulating an Alt+numberpad code. We do this by
824 * setting alt_keycode to -1 (nothing yet but plausible).
826 if ((event->keyval == GDK_KEY_Meta_L ||
827 event->keyval == GDK_KEY_Meta_R ||
828 event->keyval == GDK_KEY_Alt_L ||
829 event->keyval == GDK_KEY_Alt_R)) {
830 inst->alt_keycode = -1;
831 inst->alt_digits = 0;
832 goto done; /* this generates nothing else */
836 * If we're seeing a numberpad key press with Mod1 down,
837 * consider adding it to alt_keycode if that's sensible.
838 * Anything _else_ with Mod1 down cancels any possibility
839 * of an ALT keycode: we set alt_keycode to -2.
841 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
843 switch (event->keyval) {
844 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
845 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
846 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
847 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
848 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
849 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
850 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
851 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
852 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
853 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
856 inst->alt_keycode = -2; /* it's invalid */
859 printf("Adding digit %d to keycode %d", digit,
862 if (inst->alt_keycode == -1)
863 inst->alt_keycode = digit; /* one-digit code */
865 inst->alt_keycode = inst->alt_keycode * 10 + digit;
868 printf(" gives new code %d\n", inst->alt_keycode);
870 /* Having used this digit, we now do nothing more with it. */
876 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
879 if (event->keyval == GDK_KEY_Page_Up &&
880 (event->state & GDK_SHIFT_MASK)) {
881 term_scroll(inst->term, 0, -inst->height/2);
884 if (event->keyval == GDK_KEY_Page_Up &&
885 (event->state & GDK_CONTROL_MASK)) {
886 term_scroll(inst->term, 0, -1);
889 if (event->keyval == GDK_KEY_Page_Down &&
890 (event->state & GDK_SHIFT_MASK)) {
891 term_scroll(inst->term, 0, +inst->height/2);
894 if (event->keyval == GDK_KEY_Page_Down &&
895 (event->state & GDK_CONTROL_MASK)) {
896 term_scroll(inst->term, 0, +1);
901 * Neither does Shift-Ins.
903 if (event->keyval == GDK_KEY_Insert &&
904 (event->state & GDK_SHIFT_MASK)) {
910 use_ucsoutput = FALSE;
912 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
913 app_keypad_mode = (inst->term->app_keypad_keys &&
914 !conf_get_int(inst->conf, CONF_no_applic_k));
916 /* ALT+things gives leading Escape. */
918 #if !GTK_CHECK_VERSION(2,0,0)
920 * In vanilla X, and hence also GDK 1.2, the string received
921 * as part of a keyboard event is assumed to be in
922 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
923 * but it's true: see the man page for XLookupString(3) for
926 output_charset = CS_ISO8859_1;
927 strncpy(output+1, event->string, lenof(output)-1);
930 * Most things can now be passed to
931 * gtk_im_context_filter_keypress without breaking anything
932 * below this point. An exception is the numeric keypad if
933 * we're in Nethack or application mode: the IM will eat
934 * numeric keypad presses if Num Lock is on, but we don't want
937 if (app_keypad_mode &&
938 (event->keyval == GDK_KEY_Num_Lock ||
939 event->keyval == GDK_KEY_KP_Divide ||
940 event->keyval == GDK_KEY_KP_Multiply ||
941 event->keyval == GDK_KEY_KP_Subtract ||
942 event->keyval == GDK_KEY_KP_Add ||
943 event->keyval == GDK_KEY_KP_Enter ||
944 event->keyval == GDK_KEY_KP_0 ||
945 event->keyval == GDK_KEY_KP_Insert ||
946 event->keyval == GDK_KEY_KP_1 ||
947 event->keyval == GDK_KEY_KP_End ||
948 event->keyval == GDK_KEY_KP_2 ||
949 event->keyval == GDK_KEY_KP_Down ||
950 event->keyval == GDK_KEY_KP_3 ||
951 event->keyval == GDK_KEY_KP_Page_Down ||
952 event->keyval == GDK_KEY_KP_4 ||
953 event->keyval == GDK_KEY_KP_Left ||
954 event->keyval == GDK_KEY_KP_5 ||
955 event->keyval == GDK_KEY_KP_Begin ||
956 event->keyval == GDK_KEY_KP_6 ||
957 event->keyval == GDK_KEY_KP_Right ||
958 event->keyval == GDK_KEY_KP_7 ||
959 event->keyval == GDK_KEY_KP_Home ||
960 event->keyval == GDK_KEY_KP_8 ||
961 event->keyval == GDK_KEY_KP_Up ||
962 event->keyval == GDK_KEY_KP_9 ||
963 event->keyval == GDK_KEY_KP_Page_Up ||
964 event->keyval == GDK_KEY_KP_Decimal ||
965 event->keyval == GDK_KEY_KP_Delete)) {
966 /* app keypad; do nothing */
967 } else if (nethack_mode &&
968 (event->keyval == GDK_KEY_KP_1 ||
969 event->keyval == GDK_KEY_KP_End ||
970 event->keyval == GDK_KEY_KP_2 ||
971 event->keyval == GDK_KEY_KP_Down ||
972 event->keyval == GDK_KEY_KP_3 ||
973 event->keyval == GDK_KEY_KP_Page_Down ||
974 event->keyval == GDK_KEY_KP_4 ||
975 event->keyval == GDK_KEY_KP_Left ||
976 event->keyval == GDK_KEY_KP_5 ||
977 event->keyval == GDK_KEY_KP_Begin ||
978 event->keyval == GDK_KEY_KP_6 ||
979 event->keyval == GDK_KEY_KP_Right ||
980 event->keyval == GDK_KEY_KP_7 ||
981 event->keyval == GDK_KEY_KP_Home ||
982 event->keyval == GDK_KEY_KP_8 ||
983 event->keyval == GDK_KEY_KP_Up ||
984 event->keyval == GDK_KEY_KP_9 ||
985 event->keyval == GDK_KEY_KP_Page_Up)) {
986 /* nethack mode; do nothing */
988 if (gtk_im_context_filter_keypress(inst->imc, event))
993 * GDK 2.0 arranges to have done some translation for us: in
994 * GDK 2.0, event->string is encoded in the current locale.
996 * So we use the standard C library function mbstowcs() to
997 * convert from the current locale into Unicode; from there
998 * we can convert to whatever PuTTY is currently working in.
999 * (In fact I convert straight back to UTF-8 from
1000 * wide-character Unicode, for the sake of simplicity: that
1001 * way we can still use exactly the same code to manipulate
1002 * the string, such as prefixing ESC.)
1004 output_charset = CS_UTF8;
1006 wchar_t widedata[32];
1011 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
1012 event->string, strlen(event->string),
1013 widedata, lenof(widedata)-1);
1016 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
1017 CS_UTF8, NULL, NULL, 0);
1018 output[1+ulen] = '\0';
1023 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
1024 ucsoutput[0] = '\033';
1025 ucsoutput[1] = ucsval;
1026 use_ucsoutput = TRUE;
1029 output[lenof(output)-1] = '\0';
1030 end = strlen(output);
1032 if (event->state & GDK_MOD1_MASK) {
1034 if (end == 1) end = 0;
1038 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
1039 if (!output[1] && event->keyval == '`' &&
1040 (event->state & GDK_CONTROL_MASK)) {
1042 use_ucsoutput = FALSE;
1046 /* Some GTK backends (e.g. Quartz) do not change event->string
1047 * in response to the Control modifier. So we do it ourselves
1048 * here, if it's not already happened.
1050 * The translations below are in line with X11 policy as far
1052 if ((event->state & GDK_CONTROL_MASK) && end == 2) {
1053 if (output[1] >= '3' && output[1] <= '7') {
1054 /* ^3,...,^7 map to 0x1B,...,0x1F */
1055 output[1] += '\x1B' - '3';
1056 } else if (output[1] == '2') {
1057 /* ^2 is ^@, i.e. \0 */
1059 } else if (output[1] == '8') {
1062 } else if (output[1] == '/') {
1063 /* ^/ is the same as ^_ */
1065 } else if (output[1] >= 0x40 && output[1] < 0x7F) {
1066 /* Everything anywhere near the alphabetics just gets
1070 /* Anything else, e.g. '0', is unchanged. */
1073 /* Control-Break sends a Break special to the backend */
1074 if (event->keyval == GDK_KEY_Break &&
1075 (event->state & GDK_CONTROL_MASK)) {
1077 inst->back->special(inst->backhandle, TS_BRK);
1081 /* We handle Return ourselves, because it needs to be flagged as
1082 * special to ldisc. */
1083 if (event->keyval == GDK_KEY_Return) {
1085 use_ucsoutput = FALSE;
1090 /* Control-2, Control-Space and Control-@ are NUL */
1092 (event->keyval == ' ' || event->keyval == '2' ||
1093 event->keyval == '@') &&
1094 (event->state & (GDK_SHIFT_MASK |
1095 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
1097 use_ucsoutput = FALSE;
1101 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
1102 if (!output[1] && event->keyval == ' ' &&
1103 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
1104 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
1106 output_charset = CS_ISO8859_1;
1107 use_ucsoutput = FALSE;
1111 /* We don't let GTK tell us what Backspace is! We know better. */
1112 if (event->keyval == GDK_KEY_BackSpace &&
1113 !(event->state & GDK_SHIFT_MASK)) {
1114 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1116 use_ucsoutput = FALSE;
1120 /* For Shift Backspace, do opposite of what is configured. */
1121 if (event->keyval == GDK_KEY_BackSpace &&
1122 (event->state & GDK_SHIFT_MASK)) {
1123 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1125 use_ucsoutput = FALSE;
1130 /* Shift-Tab is ESC [ Z */
1131 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
1132 (event->keyval == GDK_KEY_Tab &&
1133 (event->state & GDK_SHIFT_MASK))) {
1134 end = 1 + sprintf(output+1, "\033[Z");
1135 use_ucsoutput = FALSE;
1137 /* And normal Tab is Tab, if the keymap hasn't already told us.
1138 * (Curiously, at least one version of the MacOS 10.5 X server
1139 * doesn't translate Tab for us. */
1140 if (event->keyval == GDK_KEY_Tab && end <= 1) {
1146 * NetHack keypad mode.
1149 const char *keys = NULL;
1150 switch (event->keyval) {
1151 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
1152 keys = "bB\002"; break;
1153 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
1154 keys = "jJ\012"; break;
1155 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
1156 keys = "nN\016"; break;
1157 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
1158 keys = "hH\010"; break;
1159 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
1160 keys = "..."; break;
1161 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
1162 keys = "lL\014"; break;
1163 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
1164 keys = "yY\031"; break;
1165 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
1166 keys = "kK\013"; break;
1167 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
1168 keys = "uU\025"; break;
1172 if (event->state & GDK_CONTROL_MASK)
1173 output[1] = keys[2];
1174 else if (event->state & GDK_SHIFT_MASK)
1175 output[1] = keys[1];
1177 output[1] = keys[0];
1178 use_ucsoutput = FALSE;
1184 * Application keypad mode.
1186 if (app_keypad_mode) {
1188 switch (event->keyval) {
1189 case GDK_KEY_Num_Lock: xkey = 'P'; break;
1190 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
1191 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
1192 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
1194 * Keypad + is tricky. It covers a space that would
1195 * be taken up on the VT100 by _two_ keys; so we
1196 * let Shift select between the two. Worse still,
1197 * in xterm function key mode we change which two...
1199 case GDK_KEY_KP_Add:
1200 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
1201 if (event->state & GDK_SHIFT_MASK)
1205 } else if (event->state & GDK_SHIFT_MASK)
1210 case GDK_KEY_KP_Enter: xkey = 'M'; break;
1211 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
1212 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
1213 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
1214 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
1215 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
1216 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
1217 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
1218 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
1219 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
1220 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
1221 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
1225 if (inst->term->vt52_mode) {
1226 if (xkey >= 'P' && xkey <= 'S')
1227 end = 1 + sprintf(output+1, "\033%c", xkey);
1229 end = 1 + sprintf(output+1, "\033?%c", xkey);
1231 end = 1 + sprintf(output+1, "\033O%c", xkey);
1232 use_ucsoutput = FALSE;
1238 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
1239 * for integer decimal nn.)
1241 * We also deal with the weird ones here. Linux VCs replace F1
1242 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1243 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1248 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1249 switch (event->keyval) {
1251 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1254 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1257 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1260 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1263 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1266 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1269 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1272 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1275 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1278 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1311 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1312 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1315 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1318 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1321 case GDK_KEY_End: case GDK_KEY_KP_End:
1324 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1327 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1331 /* Reorder edit keys to physical order */
1332 if (funky_type == FUNKY_VT400 && code <= 6)
1333 code = "\0\2\1\4\5\3\6"[code];
1335 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1336 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1337 use_ucsoutput = FALSE;
1341 if (funky_type == FUNKY_SCO && /* SCO function keys */
1342 code >= 11 && code <= 34) {
1343 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1345 switch (event->keyval) {
1346 case GDK_KEY_F1: index = 0; break;
1347 case GDK_KEY_F2: index = 1; break;
1348 case GDK_KEY_F3: index = 2; break;
1349 case GDK_KEY_F4: index = 3; break;
1350 case GDK_KEY_F5: index = 4; break;
1351 case GDK_KEY_F6: index = 5; break;
1352 case GDK_KEY_F7: index = 6; break;
1353 case GDK_KEY_F8: index = 7; break;
1354 case GDK_KEY_F9: index = 8; break;
1355 case GDK_KEY_F10: index = 9; break;
1356 case GDK_KEY_F11: index = 10; break;
1357 case GDK_KEY_F12: index = 11; break;
1359 if (event->state & GDK_SHIFT_MASK) index += 12;
1360 if (event->state & GDK_CONTROL_MASK) index += 24;
1361 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1362 use_ucsoutput = FALSE;
1365 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1366 code >= 1 && code <= 6) {
1367 char codes[] = "HL.FIG";
1372 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1374 use_ucsoutput = FALSE;
1377 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1378 code >= 11 && code <= 24) {
1384 if (inst->term->vt52_mode)
1385 end = 1 + sprintf(output+1,
1386 "\x1B%c", code + 'P' - 11 - offt);
1388 end = 1 + sprintf(output+1,
1389 "\x1BO%c", code + 'P' - 11 - offt);
1390 use_ucsoutput = FALSE;
1393 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1394 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1395 use_ucsoutput = FALSE;
1398 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1399 if (inst->term->vt52_mode)
1400 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1402 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1403 use_ucsoutput = FALSE;
1406 if ((code == 1 || code == 4) &&
1407 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1408 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1409 use_ucsoutput = FALSE;
1413 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1414 use_ucsoutput = FALSE;
1420 * Cursor keys. (This includes the numberpad cursor keys,
1421 * if we haven't already done them due to app keypad mode.)
1423 * Here we also process un-numlocked un-appkeypadded KP5,
1424 * which sends ESC [ G.
1428 switch (event->keyval) {
1429 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1430 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1431 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1432 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1433 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1436 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1437 event->state & GDK_CONTROL_MASK);
1438 use_ucsoutput = FALSE;
1447 if (end-start > 0) {
1448 #ifdef KEY_DEBUGGING
1450 printf("generating sequence:");
1451 for (i = start; i < end; i++)
1452 printf(" %02x", (unsigned char) output[i]);
1458 * For special control characters, the character set
1459 * should never matter.
1461 output[end] = '\0'; /* NUL-terminate */
1463 ldisc_send(inst->ldisc, output+start, -2, 1);
1464 } else if (!inst->direct_to_font) {
1465 if (!use_ucsoutput) {
1467 lpage_send(inst->ldisc, output_charset, output+start,
1471 * We generated our own Unicode key data from the
1472 * keysym, so use that instead.
1475 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1479 * In direct-to-font mode, we just send the string
1480 * exactly as we received it.
1483 ldisc_send(inst->ldisc, output+start, end-start, 1);
1486 show_mouseptr(inst, 0);
1487 term_seen_key_event(inst->term);
1493 #if GTK_CHECK_VERSION(2,0,0)
1494 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1496 struct gui_data *inst = (struct gui_data *)data;
1498 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1499 show_mouseptr(inst, 0);
1500 term_seen_key_event(inst->term);
1504 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1505 GdkEventType type, guint ebutton, guint state,
1506 gdouble ex, gdouble ey)
1508 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1510 /* Remember the timestamp. */
1511 inst->input_event_time = timestamp;
1513 show_mouseptr(inst, 1);
1515 shift = state & GDK_SHIFT_MASK;
1516 ctrl = state & GDK_CONTROL_MASK;
1517 alt = state & GDK_MOD1_MASK;
1520 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1521 CONF_mouse_override));
1523 if (!raw_mouse_mode) {
1524 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1525 term_scroll(inst->term, 0, -5);
1528 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1529 term_scroll(inst->term, 0, +5);
1534 if (ebutton == 3 && ctrl) {
1535 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1536 ebutton, timestamp);
1542 else if (ebutton == 2)
1543 button = MBT_MIDDLE;
1544 else if (ebutton == 3)
1546 else if (ebutton == 4)
1547 button = MBT_WHEEL_UP;
1548 else if (ebutton == 5)
1549 button = MBT_WHEEL_DOWN;
1551 return FALSE; /* don't even know what button! */
1554 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1555 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1556 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1557 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1558 default: return FALSE; /* don't know this event type */
1561 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1562 return TRUE; /* we ignore these in raw mouse mode */
1564 x = (ex - inst->window_border) / inst->font_width;
1565 y = (ey - inst->window_border) / inst->font_height;
1567 term_mouse(inst->term, button, translate_button(button), act,
1568 x, y, shift, ctrl, alt);
1573 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1575 struct gui_data *inst = (struct gui_data *)data;
1576 return button_internal(inst, event->time, event->type, event->button,
1577 event->state, event->x, event->y);
1580 #if GTK_CHECK_VERSION(2,0,0)
1582 * In GTK 2, mouse wheel events have become a new type of event.
1583 * This handler translates them back into button-4 and button-5
1584 * presses so that I don't have to change my old code too much :-)
1586 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1588 struct gui_data *inst = (struct gui_data *)data;
1591 if (event->direction == GDK_SCROLL_UP)
1593 else if (event->direction == GDK_SCROLL_DOWN)
1598 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1599 button, event->state, event->x, event->y);
1603 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1605 struct gui_data *inst = (struct gui_data *)data;
1606 int shift, ctrl, alt, x, y, button;
1608 /* Remember the timestamp. */
1609 inst->input_event_time = event->time;
1611 show_mouseptr(inst, 1);
1613 shift = event->state & GDK_SHIFT_MASK;
1614 ctrl = event->state & GDK_CONTROL_MASK;
1615 alt = event->state & GDK_MOD1_MASK;
1616 if (event->state & GDK_BUTTON1_MASK)
1618 else if (event->state & GDK_BUTTON2_MASK)
1619 button = MBT_MIDDLE;
1620 else if (event->state & GDK_BUTTON3_MASK)
1623 return FALSE; /* don't even know what button! */
1625 x = (event->x - inst->window_border) / inst->font_width;
1626 y = (event->y - inst->window_border) / inst->font_height;
1628 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1629 x, y, shift, ctrl, alt);
1634 void frontend_keypress(void *handle)
1636 struct gui_data *inst = (struct gui_data *)handle;
1639 * If our child process has exited but not closed, terminate on
1646 static void exit_callback(void *vinst)
1648 struct gui_data *inst = (struct gui_data *)vinst;
1649 int exitcode, close_on_exit;
1651 if (!inst->exited &&
1652 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1653 inst->exited = TRUE;
1654 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1655 if (close_on_exit == FORCE_ON ||
1656 (close_on_exit == AUTO && exitcode == 0))
1657 gtk_main_quit(); /* just go */
1659 ldisc_free(inst->ldisc);
1662 inst->back->free(inst->backhandle);
1663 inst->backhandle = NULL;
1665 term_provide_resize_fn(inst->term, NULL, NULL);
1666 update_specials_menu(inst);
1667 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1672 * Replacement code for the gtk_quit_add() function, which GTK2 - in
1673 * their unbounded wisdom - deprecated without providing any usable
1674 * replacement, and which we were using to ensure that our idle
1675 * function for toplevel callbacks was only run from the outermost
1678 * We maintain a global variable with a list of 'struct gui_data'
1679 * instances on which we should call inst_post_main() when a
1680 * subsidiary gtk_main() terminates; then we must make sure that all
1681 * our subsidiary calls to gtk_main() are followed by a call to
1684 * This is kind of overkill in the sense that at the time of writing
1685 * we don't actually ever run more than one 'struct gui_data' instance
1686 * in the same process, but we're _so nearly_ prepared to do that that
1687 * I want to remain futureproof against the possibility of doing so in
1690 struct post_main_context {
1691 struct post_main_context *next;
1692 struct gui_data *inst;
1694 struct post_main_context *post_main_list_head = NULL;
1695 static void request_post_main(struct gui_data *inst)
1697 struct post_main_context *node = snew(struct post_main_context);
1698 node->next = post_main_list_head;
1700 post_main_list_head = node;
1702 static void inst_post_main(struct gui_data *inst);
1703 void post_main(void)
1705 while (post_main_list_head) {
1706 struct post_main_context *node = post_main_list_head;
1707 post_main_list_head = node->next;
1708 inst_post_main(node->inst);
1713 void notify_remote_exit(void *frontend)
1715 struct gui_data *inst = (struct gui_data *)frontend;
1717 queue_toplevel_callback(exit_callback, inst);
1720 static void notify_toplevel_callback(void *frontend);
1722 static void inst_post_main(struct gui_data *inst)
1724 if (gtk_main_level() == 1) {
1725 notify_toplevel_callback(inst);
1726 inst->quit_fn_scheduled = FALSE;
1728 /* Apparently we're _still_ more than one level deep in
1729 * gtk_main() instances, so we'll need another callback for
1730 * when we get out of the next one. */
1731 request_post_main(inst);
1735 static gint idle_toplevel_callback_func(gpointer data)
1737 struct gui_data *inst = (struct gui_data *)data;
1739 if (gtk_main_level() > 1) {
1741 * We don't run the callbacks if we're in the middle of a
1742 * subsidiary gtk_main. Instead, ask for a callback when we
1743 * get back out of the subsidiary main loop (if we haven't
1744 * already arranged one), so we can reschedule ourself then.
1746 if (!inst->quit_fn_scheduled) {
1747 request_post_main(inst);
1748 inst->quit_fn_scheduled = TRUE;
1751 * And unschedule this idle function, since we've now done
1752 * everything we can until the innermost gtk_main has quit and
1753 * can reschedule us with a chance of actually taking action.
1755 if (inst->idle_fn_scheduled) { /* double-check, just in case */
1756 g_source_remove(inst->toplevel_callback_idle_id);
1757 inst->idle_fn_scheduled = FALSE;
1760 run_toplevel_callbacks();
1764 * If we've emptied our toplevel callback queue, unschedule
1765 * ourself. Otherwise, leave ourselves pending so we'll be called
1766 * again to deal with more callbacks after another round of the
1769 if (!toplevel_callback_pending() && inst->idle_fn_scheduled) {
1770 g_source_remove(inst->toplevel_callback_idle_id);
1771 inst->idle_fn_scheduled = FALSE;
1777 static void notify_toplevel_callback(void *frontend)
1779 struct gui_data *inst = (struct gui_data *)frontend;
1781 if (!inst->idle_fn_scheduled) {
1782 inst->toplevel_callback_idle_id =
1783 g_idle_add(idle_toplevel_callback_func, inst);
1784 inst->idle_fn_scheduled = TRUE;
1788 static gint timer_trigger(gpointer data)
1790 unsigned long now = GPOINTER_TO_LONG(data);
1791 unsigned long next, then;
1795 * Destroy the timer we got here on.
1798 g_source_remove(timer_id);
1803 * run_timers() may cause a call to timer_change_notify, in which
1804 * case a new timer will already have been set up and left in
1805 * timer_id. If it hasn't, and run_timers reports that some timing
1806 * still needs to be done, we do it ourselves.
1808 if (run_timers(now, &next) && !timer_id) {
1810 now = GETTICKCOUNT();
1811 if (now - then > next - then)
1815 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1819 * Returning FALSE means 'don't call this timer again', which
1820 * _should_ be redundant given that we removed it above, but just
1821 * in case, return FALSE anyway.
1826 void timer_change_notify(unsigned long next)
1831 g_source_remove(timer_id);
1833 ticks = next - GETTICKCOUNT();
1835 ticks = 1; /* just in case */
1837 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1840 #if GTK_CHECK_VERSION(2,0,0)
1841 gboolean fd_input_func(GIOChannel *source, GIOCondition condition,
1844 int sourcefd = g_io_channel_unix_get_fd(source);
1846 * We must process exceptional notifications before ordinary
1847 * readability ones, or we may go straight past the urgent
1850 if (condition & G_IO_PRI)
1851 select_result(sourcefd, 4);
1852 if (condition & G_IO_IN)
1853 select_result(sourcefd, 1);
1854 if (condition & G_IO_OUT)
1855 select_result(sourcefd, 2);
1860 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1862 if (condition & GDK_INPUT_EXCEPTION)
1863 select_result(sourcefd, 4);
1864 if (condition & GDK_INPUT_READ)
1865 select_result(sourcefd, 1);
1866 if (condition & GDK_INPUT_WRITE)
1867 select_result(sourcefd, 2);
1871 void destroy(GtkWidget *widget, gpointer data)
1876 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1878 struct gui_data *inst = (struct gui_data *)data;
1879 term_set_focus(inst->term, event->in);
1880 term_update(inst->term);
1881 show_mouseptr(inst, 1);
1885 void set_busy_status(void *frontend, int status)
1887 struct gui_data *inst = (struct gui_data *)frontend;
1888 inst->busy_status = status;
1889 update_mouseptr(inst);
1893 * set or clear the "raw mouse message" mode
1895 void set_raw_mouse_mode(void *frontend, int activate)
1897 struct gui_data *inst = (struct gui_data *)frontend;
1898 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1899 send_raw_mouse = activate;
1900 update_mouseptr(inst);
1903 void request_resize(void *frontend, int w, int h)
1905 struct gui_data *inst = (struct gui_data *)frontend;
1907 #if !GTK_CHECK_VERSION(3,0,0)
1909 int large_x, large_y;
1910 int offset_x, offset_y;
1912 GtkRequisition inner, outer;
1915 * This is a heinous hack dreamed up by the gnome-terminal
1916 * people to get around a limitation in gtk. The problem is
1917 * that in order to set the size correctly we really need to be
1918 * calling gtk_window_resize - but that needs to know the size
1919 * of the _whole window_, not the drawing area. So what we do
1920 * is to set an artificially huge size request on the drawing
1921 * area, recompute the resulting size request on the window,
1922 * and look at the difference between the two. That gives us
1923 * the x and y offsets we need to translate drawing area size
1924 * into window size for real, and then we call
1925 * gtk_window_resize.
1929 * We start by retrieving the current size of the whole window.
1930 * Adding a bit to _that_ will give us a value we can use as a
1931 * bogus size request which guarantees to be bigger than the
1932 * current size of the drawing area.
1934 get_window_pixels(inst, &large_x, &large_y);
1938 gtk_widget_set_size_request(inst->area, large_x, large_y);
1939 gtk_widget_size_request(inst->area, &inner);
1940 gtk_widget_size_request(inst->window, &outer);
1942 offset_x = outer.width - inner.width;
1943 offset_y = outer.height - inner.height;
1945 area_x = inst->font_width * w + 2*inst->window_border;
1946 area_y = inst->font_height * h + 2*inst->window_border;
1949 * Now we must set the size request on the drawing area back to
1950 * something sensible before we commit the real resize. Best
1951 * way to do this, I think, is to set it to what the size is
1952 * really going to end up being.
1954 gtk_widget_set_size_request(inst->area, area_x, area_y);
1955 #if GTK_CHECK_VERSION(2,0,0)
1956 gtk_window_resize(GTK_WINDOW(inst->window),
1957 area_x + offset_x, area_y + offset_y);
1959 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1961 * I can no longer remember what this call to
1962 * gtk_container_dequeue_resize_handler is for. It was
1963 * introduced in r3092 with no comment, and the commit log
1964 * message was uninformative. I'm _guessing_ its purpose is to
1965 * prevent gratuitous resize processing on the window given
1966 * that we're about to resize it anyway, but I have no idea
1967 * why that's so incredibly vital.
1969 * I've tried removing the call, and nothing seems to go
1970 * wrong. I've backtracked to r3092 and tried removing the
1971 * call there, and still nothing goes wrong. So I'm going to
1972 * adopt the working hypothesis that it's superfluous; I won't
1973 * actually remove it from the GTK 1.2 code, but I won't
1974 * attempt to replicate its functionality in the GTK 2 code
1977 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1978 gdk_window_resize(gtk_widget_get_window(inst->window),
1979 area_x + offset_x, area_y + offset_y);
1982 #else /* GTK_CHECK_VERSION(3,0,0) */
1985 * In GTK3, we can do this by using gtk_window_resize_to_geometry,
1986 * which uses the fact that we've already set up the main window's
1987 * WM hints to reflect the terminal drawing area's resize
1988 * increment (i.e. character cell) and the fixed amount of stuff
1991 gtk_window_resize_to_geometry(GTK_WINDOW(inst->window), w, h);
1997 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1999 inst->cols[n].red = r * 0x0101;
2000 inst->cols[n].green = g * 0x0101;
2001 inst->cols[n].blue = b * 0x0101;
2003 #if !GTK_CHECK_VERSION(3,0,0)
2005 gboolean success[1];
2006 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
2007 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
2008 FALSE, TRUE, success);
2010 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2011 appname, n, r, g, b);
2016 void set_gdk_window_background(GdkWindow *win, const GdkColor *col)
2018 #if GTK_CHECK_VERSION(3,0,0)
2019 /* gdk_window_set_background is deprecated; work around its
2022 rgba.red = col->red / 65535.0;
2023 rgba.green = col->green / 65535.0;
2024 rgba.blue = col->blue / 65535.0;
2026 gdk_window_set_background_rgba(win, &rgba);
2029 /* For GTK1, which doesn't have a 'const' on
2030 * gdk_window_set_background's second parameter type. */
2031 GdkColor col_mutable = *col;
2032 gdk_window_set_background(win, &col_mutable);
2037 void set_window_background(struct gui_data *inst)
2039 if (inst->area && gtk_widget_get_window(inst->area))
2040 set_gdk_window_background(gtk_widget_get_window(inst->area),
2042 if (inst->window && gtk_widget_get_window(inst->window))
2043 set_gdk_window_background(gtk_widget_get_window(inst->window),
2047 void palette_set(void *frontend, int n, int r, int g, int b)
2049 struct gui_data *inst = (struct gui_data *)frontend;
2052 if (n >= NALLCOLOURS)
2054 real_palette_set(inst, n, r, g, b);
2056 /* Default Background changed. Ensure space between text area and
2057 * window border is redrawn */
2058 set_window_background(inst);
2059 draw_backing_rect(inst);
2060 gtk_widget_queue_draw(inst->area);
2064 void palette_reset(void *frontend)
2066 struct gui_data *inst = (struct gui_data *)frontend;
2067 /* This maps colour indices in inst->conf to those used in inst->cols. */
2068 static const int ww[] = {
2069 256, 257, 258, 259, 260, 261,
2070 0, 8, 1, 9, 2, 10, 3, 11,
2071 4, 12, 5, 13, 6, 14, 7, 15
2075 assert(lenof(ww) == NCFGCOLOURS);
2077 #if !GTK_CHECK_VERSION(3,0,0)
2078 if (!inst->colmap) {
2079 inst->colmap = gdk_colormap_get_system();
2081 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
2085 for (i = 0; i < NCFGCOLOURS; i++) {
2086 inst->cols[ww[i]].red =
2087 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
2088 inst->cols[ww[i]].green =
2089 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
2090 inst->cols[ww[i]].blue =
2091 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
2094 for (i = 0; i < NEXTCOLOURS; i++) {
2096 int r = i / 36, g = (i / 6) % 6, b = i % 6;
2097 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
2098 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
2099 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
2101 int shade = i - 216;
2102 shade = shade * 0x0a0a + 0x0808;
2103 inst->cols[i+16].red = inst->cols[i+16].green =
2104 inst->cols[i+16].blue = shade;
2108 #if !GTK_CHECK_VERSION(3,0,0)
2110 gboolean success[NALLCOLOURS];
2111 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
2112 FALSE, TRUE, success);
2113 for (i = 0; i < NALLCOLOURS; i++) {
2115 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2117 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
2118 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
2119 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
2124 /* Since Default Background may have changed, ensure that space
2125 * between text area and window border is refreshed. */
2126 set_window_background(inst);
2127 if (inst->area && gtk_widget_get_window(inst->area)) {
2128 draw_backing_rect(inst);
2129 gtk_widget_queue_draw(inst->area);
2133 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
2134 * do this before we start using cut buffers.
2136 void init_cutbuffers()
2138 #ifndef NOT_X_WINDOWS
2139 unsigned char empty[] = "";
2140 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2141 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2142 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
2143 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2144 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
2145 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2146 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
2147 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2148 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
2149 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2150 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
2151 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2152 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
2153 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2154 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
2155 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2156 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
2160 /* Store the data in a cut-buffer. */
2161 void store_cutbuffer(char * ptr, int len)
2163 #ifndef NOT_X_WINDOWS
2164 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2165 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
2166 XRotateBuffers(disp, 1);
2167 XStoreBytes(disp, ptr, len);
2171 /* Retrieve data from a cut-buffer.
2172 * Returned data needs to be freed with XFree().
2174 char * retrieve_cutbuffer(int * nbytes)
2176 #ifndef NOT_X_WINDOWS
2177 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2179 ptr = XFetchBytes(disp, nbytes);
2180 if (*nbytes <= 0 && ptr != 0) {
2190 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
2192 struct gui_data *inst = (struct gui_data *)frontend;
2193 if (inst->pasteout_data)
2194 sfree(inst->pasteout_data);
2195 if (inst->pasteout_data_ctext)
2196 sfree(inst->pasteout_data_ctext);
2197 if (inst->pasteout_data_utf8)
2198 sfree(inst->pasteout_data_utf8);
2201 * Set up UTF-8 and compound text paste data. This only happens
2202 * if we aren't in direct-to-font mode using the D800 hack.
2204 if (!inst->direct_to_font) {
2205 const wchar_t *tmp = data;
2207 #ifndef NOT_X_WINDOWS
2210 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2213 inst->pasteout_data_utf8 = snewn(len*6, char);
2214 inst->pasteout_data_utf8_len = len*6;
2215 inst->pasteout_data_utf8_len =
2216 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
2217 inst->pasteout_data_utf8_len,
2218 CS_UTF8, NULL, NULL, 0);
2219 if (inst->pasteout_data_utf8_len == 0) {
2220 sfree(inst->pasteout_data_utf8);
2221 inst->pasteout_data_utf8 = NULL;
2223 inst->pasteout_data_utf8 =
2224 sresize(inst->pasteout_data_utf8,
2225 inst->pasteout_data_utf8_len + 1, char);
2226 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
2230 * Now let Xlib convert our UTF-8 data into compound text.
2232 #ifndef NOT_X_WINDOWS
2233 list[0] = inst->pasteout_data_utf8;
2234 if (Xutf8TextListToTextProperty(disp, list, 1,
2235 XCompoundTextStyle, &tp) == 0) {
2236 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
2237 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
2238 inst->pasteout_data_ctext_len = tp.nitems;
2243 inst->pasteout_data_ctext = NULL;
2244 inst->pasteout_data_ctext_len = 0;
2247 inst->pasteout_data_utf8 = NULL;
2248 inst->pasteout_data_utf8_len = 0;
2249 inst->pasteout_data_ctext = NULL;
2250 inst->pasteout_data_ctext_len = 0;
2253 inst->pasteout_data = snewn(len*6, char);
2254 inst->pasteout_data_len = len*6;
2255 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
2256 data, len, inst->pasteout_data,
2257 inst->pasteout_data_len,
2259 if (inst->pasteout_data_len == 0) {
2260 sfree(inst->pasteout_data);
2261 inst->pasteout_data = NULL;
2263 inst->pasteout_data =
2264 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
2267 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
2269 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
2270 inst->input_event_time)) {
2271 #if GTK_CHECK_VERSION(2,0,0)
2272 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
2274 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2275 GDK_SELECTION_TYPE_STRING, 1);
2276 if (inst->pasteout_data_ctext)
2277 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2278 compound_text_atom, 1);
2279 if (inst->pasteout_data_utf8)
2280 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2281 utf8_string_atom, 1);
2285 term_deselect(inst->term);
2288 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2289 guint info, guint time_stamp, gpointer data)
2291 struct gui_data *inst = (struct gui_data *)data;
2292 GdkAtom target = gtk_selection_data_get_target(seldata);
2293 if (target == utf8_string_atom)
2294 gtk_selection_data_set(seldata, target, 8,
2295 (unsigned char *)inst->pasteout_data_utf8,
2296 inst->pasteout_data_utf8_len);
2297 else if (target == compound_text_atom)
2298 gtk_selection_data_set(seldata, target, 8,
2299 (unsigned char *)inst->pasteout_data_ctext,
2300 inst->pasteout_data_ctext_len);
2302 gtk_selection_data_set(seldata, target, 8,
2303 (unsigned char *)inst->pasteout_data,
2304 inst->pasteout_data_len);
2307 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2310 struct gui_data *inst = (struct gui_data *)data;
2312 term_deselect(inst->term);
2313 if (inst->pasteout_data)
2314 sfree(inst->pasteout_data);
2315 if (inst->pasteout_data_ctext)
2316 sfree(inst->pasteout_data_ctext);
2317 if (inst->pasteout_data_utf8)
2318 sfree(inst->pasteout_data_utf8);
2319 inst->pasteout_data = NULL;
2320 inst->pasteout_data_len = 0;
2321 inst->pasteout_data_ctext = NULL;
2322 inst->pasteout_data_ctext_len = 0;
2323 inst->pasteout_data_utf8 = NULL;
2324 inst->pasteout_data_utf8_len = 0;
2328 void request_paste(void *frontend)
2330 struct gui_data *inst = (struct gui_data *)frontend;
2332 * In Unix, pasting is asynchronous: all we can do at the
2333 * moment is to call gtk_selection_convert(), and when the data
2334 * comes back _then_ we can call term_do_paste().
2337 if (!inst->direct_to_font) {
2339 * First we attempt to retrieve the selection as a UTF-8
2340 * string (which we will convert to the correct code page
2341 * before sending to the session, of course). If that
2342 * fails, selection_received() will be informed and will
2343 * fall back to an ordinary string.
2345 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2347 inst->input_event_time);
2350 * If we're in direct-to-font mode, we disable UTF-8
2351 * pasting, and go straight to ordinary string data.
2353 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2354 GDK_SELECTION_TYPE_STRING,
2355 inst->input_event_time);
2359 gint idle_paste_func(gpointer data); /* forward ref */
2361 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2362 guint time, gpointer data)
2364 struct gui_data *inst = (struct gui_data *)data;
2367 #ifndef NOT_X_WINDOWS
2369 int free_list_required = 0;
2370 int free_required = 0;
2373 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2374 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2375 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2376 gint seldata_length = gtk_selection_data_get_length(seldata);
2378 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2380 * Failed to get a UTF-8 selection string. Try compound
2383 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2385 inst->input_event_time);
2389 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2391 * Failed to get UTF-8 or compound text. Try an ordinary
2394 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2395 GDK_SELECTION_TYPE_STRING,
2396 inst->input_event_time);
2401 * If we have data, but it's not of a type we can deal with,
2402 * we have to ignore the data.
2404 if (seldata_length > 0 &&
2405 seldata_type != GDK_SELECTION_TYPE_STRING &&
2406 seldata_type != compound_text_atom &&
2407 seldata_type != utf8_string_atom)
2411 * If we have no data, try looking in a cut buffer.
2413 if (seldata_length <= 0) {
2414 #ifndef NOT_X_WINDOWS
2415 text = retrieve_cutbuffer(&length);
2418 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2419 * source, so use that as a de-facto standard. */
2420 charset = CS_ISO8859_1;
2427 * Convert COMPOUND_TEXT into UTF-8.
2429 if (seldata_type == compound_text_atom) {
2430 #ifndef NOT_X_WINDOWS
2433 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2435 tp.value = (unsigned char *)seldata_data;
2436 tp.encoding = (Atom) seldata_type;
2437 tp.format = gtk_selection_data_get_format(seldata);
2438 tp.nitems = seldata_length;
2439 ret = Xutf8TextPropertyToTextList(disp, &tp, &list, &count);
2440 if (ret == 0 && count == 1) {
2442 length = strlen(list[0]);
2444 free_list_required = 1;
2449 * Compound text failed; fall back to STRING.
2451 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2452 GDK_SELECTION_TYPE_STRING,
2453 inst->input_event_time);
2457 text = (char *)seldata_data;
2458 length = seldata_length;
2459 charset = (seldata_type == utf8_string_atom ?
2460 CS_UTF8 : inst->ucsdata.line_codepage);
2464 if (inst->pastein_data)
2465 sfree(inst->pastein_data);
2467 inst->pastein_data = snewn(length, wchar_t);
2468 inst->pastein_data_len = length;
2469 inst->pastein_data_len =
2470 mb_to_wc(charset, 0, text, length,
2471 inst->pastein_data, inst->pastein_data_len);
2473 term_do_paste(inst->term);
2475 #ifndef NOT_X_WINDOWS
2476 if (free_list_required)
2477 XFreeStringList(list);
2483 void get_clip(void *frontend, wchar_t ** p, int *len)
2485 struct gui_data *inst = (struct gui_data *)frontend;
2488 *p = inst->pastein_data;
2489 *len = inst->pastein_data_len;
2493 static void set_window_titles(struct gui_data *inst)
2496 * We must always call set_icon_name after calling set_title,
2497 * since set_title will write both names. Irritating, but such
2500 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2501 if (!conf_get_int(inst->conf, CONF_win_name_always))
2502 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
2506 void set_title(void *frontend, char *title)
2508 struct gui_data *inst = (struct gui_data *)frontend;
2509 sfree(inst->wintitle);
2510 inst->wintitle = dupstr(title);
2511 set_window_titles(inst);
2514 void set_icon(void *frontend, char *title)
2516 struct gui_data *inst = (struct gui_data *)frontend;
2517 sfree(inst->icontitle);
2518 inst->icontitle = dupstr(title);
2519 set_window_titles(inst);
2522 void set_title_and_icon(void *frontend, char *title, char *icon)
2524 struct gui_data *inst = (struct gui_data *)frontend;
2525 sfree(inst->wintitle);
2526 inst->wintitle = dupstr(title);
2527 sfree(inst->icontitle);
2528 inst->icontitle = dupstr(icon);
2529 set_window_titles(inst);
2532 void set_sbar(void *frontend, int total, int start, int page)
2534 struct gui_data *inst = (struct gui_data *)frontend;
2535 if (!conf_get_int(inst->conf, CONF_scrollbar))
2537 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
2538 gtk_adjustment_set_upper(inst->sbar_adjust, total);
2539 gtk_adjustment_set_value(inst->sbar_adjust, start);
2540 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
2541 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
2542 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
2543 inst->ignore_sbar = TRUE;
2544 gtk_adjustment_changed(inst->sbar_adjust);
2545 inst->ignore_sbar = FALSE;
2548 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2550 struct gui_data *inst = (struct gui_data *)data;
2552 if (!conf_get_int(inst->conf, CONF_scrollbar))
2554 if (!inst->ignore_sbar)
2555 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
2558 void sys_cursor(void *frontend, int x, int y)
2561 * This is meaningless under X.
2566 * This is still called when mode==BELL_VISUAL, even though the
2567 * visual bell is handled entirely within terminal.c, because we
2568 * may want to perform additional actions on any kind of bell (for
2569 * example, taskbar flashing in Windows).
2571 void do_beep(void *frontend, int mode)
2573 if (mode == BELL_DEFAULT)
2577 int char_width(Context ctx, int uc)
2580 * Under X, any fixed-width font really _is_ fixed-width.
2581 * Double-width characters will be dealt with using a separate
2582 * font. For the moment we can simply return 1.
2584 * FIXME: but is that also true of Pango?
2589 Context get_ctx(void *frontend)
2591 struct gui_data *inst = (struct gui_data *)frontend;
2592 struct draw_ctx *dctx;
2595 if (!gtk_widget_get_window(inst->area))
2598 #ifndef NO_BACKING_PIXMAPS
2599 target = inst->pixmap;
2601 target = gtk_widget_get_window(inst->area);
2604 dctx = snew(struct draw_ctx);
2606 dctx->uctx.type = inst->drawtype;
2607 #ifdef DRAW_TEXT_GDK
2608 if (dctx->uctx.type == DRAWTYPE_GDK) {
2609 dctx->uctx.u.gdk.target = target;
2610 dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
2613 #ifdef DRAW_TEXT_CAIRO
2614 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2615 dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
2616 dctx->uctx.u.cairo.cr = gdk_cairo_create(target);
2617 cairo_setup_dctx(dctx);
2623 void free_ctx(Context ctx)
2625 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2626 /* struct gui_data *inst = dctx->inst; */
2627 #ifdef DRAW_TEXT_GDK
2628 if (dctx->uctx.type == DRAWTYPE_GDK) {
2629 gdk_gc_unref(dctx->uctx.u.gdk.gc);
2632 #ifdef DRAW_TEXT_CAIRO
2633 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2634 cairo_destroy(dctx->uctx.u.cairo.cr);
2641 static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
2643 #ifndef NO_BACKING_PIXMAPS
2644 #ifdef DRAW_TEXT_GDK
2645 if (dctx->uctx.type == DRAWTYPE_GDK) {
2646 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2647 dctx->uctx.u.gdk.gc, dctx->inst->pixmap,
2651 #ifdef DRAW_TEXT_CAIRO /* FIXME: and not GTK3 where a cairo_t is all we have */
2652 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2653 GdkGC *gc = gdk_gc_new(gtk_widget_get_window(dctx->inst->area));
2654 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2655 gc, dctx->inst->pixmap, x, y, x, y, w, h);
2662 static void draw_set_colour(struct draw_ctx *dctx, int col)
2664 #ifdef DRAW_TEXT_GDK
2665 if (dctx->uctx.type == DRAWTYPE_GDK) {
2666 gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
2669 #ifdef DRAW_TEXT_CAIRO
2670 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2671 cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
2672 dctx->inst->cols[col].red / 65535.0,
2673 dctx->inst->cols[col].green / 65535.0,
2674 dctx->inst->cols[col].blue / 65535.0);
2679 static void draw_rectangle(struct draw_ctx *dctx, int filled,
2680 int x, int y, int w, int h)
2682 #ifdef DRAW_TEXT_GDK
2683 if (dctx->uctx.type == DRAWTYPE_GDK) {
2684 gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2685 filled, x, y, w, h);
2688 #ifdef DRAW_TEXT_CAIRO
2689 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2690 cairo_new_path(dctx->uctx.u.cairo.cr);
2692 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2693 cairo_fill(dctx->uctx.u.cairo.cr);
2695 cairo_rectangle(dctx->uctx.u.cairo.cr,
2696 x + 0.5, y + 0.5, w, h);
2697 cairo_close_path(dctx->uctx.u.cairo.cr);
2698 cairo_stroke(dctx->uctx.u.cairo.cr);
2704 static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
2706 #ifdef DRAW_TEXT_GDK
2707 if (dctx->uctx.type == DRAWTYPE_GDK) {
2715 gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
2718 #ifdef DRAW_TEXT_CAIRO
2719 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2720 cairo_reset_clip(dctx->uctx.u.cairo.cr);
2721 cairo_new_path(dctx->uctx.u.cairo.cr);
2722 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2723 cairo_clip(dctx->uctx.u.cairo.cr);
2728 static void draw_point(struct draw_ctx *dctx, int x, int y)
2730 #ifdef DRAW_TEXT_GDK
2731 if (dctx->uctx.type == DRAWTYPE_GDK) {
2732 gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
2735 #ifdef DRAW_TEXT_CAIRO
2736 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2737 cairo_new_path(dctx->uctx.u.cairo.cr);
2738 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
2739 cairo_fill(dctx->uctx.u.cairo.cr);
2744 static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
2746 #ifdef DRAW_TEXT_GDK
2747 if (dctx->uctx.type == DRAWTYPE_GDK) {
2748 gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2752 #ifdef DRAW_TEXT_CAIRO
2753 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2754 cairo_new_path(dctx->uctx.u.cairo.cr);
2755 cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
2756 cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
2757 cairo_stroke(dctx->uctx.u.cairo.cr);
2762 static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
2764 int h, int hdouble, int hbothalf)
2766 #ifdef DRAW_TEXT_CAIRO
2767 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2768 cairo_matrix_t matrix;
2792 cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
2797 static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
2799 int h, int hdouble, int hbothalf)
2801 #ifdef DRAW_TEXT_GDK
2802 #ifndef NO_BACKING_PIXMAPS
2803 if (dctx->uctx.type == DRAWTYPE_GDK) {
2805 * I can't find any plausible StretchBlt equivalent in the X
2806 * server, so I'm going to do this the slow and painful way.
2807 * This will involve repeated calls to gdk_draw_pixmap() to
2808 * stretch the text horizontally. It's O(N^2) in time and O(N)
2809 * in network bandwidth, but you try thinking of a better way.
2814 for (i = 0; i < w; i++) {
2815 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
2816 dctx->uctx.u.gdk.gc,
2817 dctx->uctx.u.gdk.target,
2827 /* Now stretch vertically, in the same way. */
2832 for (i = 0; i < h; i += 2) {
2833 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
2834 dctx->uctx.u.gdk.gc,
2835 dctx->uctx.u.gdk.target,
2843 #error No way to implement stretching in GDK without a reliable backing pixmap
2845 #endif /* DRAW_TEXT_GDK */
2846 #ifdef DRAW_TEXT_CAIRO
2847 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2848 cairo_set_matrix(dctx->uctx.u.cairo.cr,
2849 &dctx->uctx.u.cairo.origmatrix);
2854 static void draw_backing_rect(struct gui_data *inst)
2856 struct draw_ctx *dctx = get_ctx(inst);
2857 draw_set_colour(dctx, 258);
2858 draw_rectangle(dctx, 1, 0, 0,
2859 inst->width * inst->font_width + 2*inst->window_border,
2860 inst->height * inst->font_height + 2*inst->window_border);
2865 * Draw a line of text in the window, at given character
2866 * coordinates, in given attributes.
2868 * We are allowed to fiddle with the contents of `text'.
2870 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2871 unsigned long attr, int lattr)
2873 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2874 struct gui_data *inst = dctx->inst;
2875 int ncombining, combining;
2876 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2878 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
2880 if (attr & TATTR_COMBINING) {
2886 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2887 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2888 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2893 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
2894 if (nfg < 16) nfg |= 8;
2895 else if (nfg >= 256) nfg |= 1;
2897 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
2898 if (nbg < 16) nbg |= 8;
2899 else if (nbg >= 256) nbg |= 1;
2901 if ((attr & TATTR_ACTCURS) && !monochrome) {
2906 fontid = shadow = 0;
2908 if (attr & ATTR_WIDE) {
2915 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
2922 if (!inst->fonts[fontid]) {
2925 * Fall back through font ids with subsets of this one's
2926 * set bits, in order.
2928 for (i = fontid; i-- > 0 ;) {
2930 continue; /* some other bit is set */
2931 if (inst->fonts[i]) {
2936 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2939 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2941 if (x >= inst->term->cols)
2943 if (x + len*2*widefactor > inst->term->cols)
2944 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2950 x*inst->font_width+inst->window_border,
2951 y*inst->font_height+inst->window_border,
2952 rlen*widefactor*inst->font_width,
2955 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2956 draw_stretch_before(dctx,
2957 x*inst->font_width+inst->window_border,
2958 y*inst->font_height+inst->window_border,
2959 rlen*widefactor*inst->font_width, TRUE,
2961 ((lattr & LATTR_MODE) != LATTR_WIDE),
2962 ((lattr & LATTR_MODE) == LATTR_BOT));
2965 draw_set_colour(dctx, nbg);
2966 draw_rectangle(dctx, TRUE,
2967 x*inst->font_width+inst->window_border,
2968 y*inst->font_height+inst->window_border,
2969 rlen*widefactor*inst->font_width, inst->font_height);
2971 draw_set_colour(dctx, nfg);
2972 for (combining = 0; combining < ncombining; combining++) {
2973 unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
2974 x*inst->font_width+inst->window_border,
2975 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2976 text + combining, len, widefactor > 1,
2977 bold, inst->font_width);
2980 if (attr & ATTR_UNDER) {
2981 int uheight = inst->fonts[0]->ascent + 1;
2982 if (uheight >= inst->font_height)
2983 uheight = inst->font_height - 1;
2984 draw_line(dctx, x*inst->font_width+inst->window_border,
2985 y*inst->font_height + uheight + inst->window_border,
2986 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2987 y*inst->font_height + uheight + inst->window_border);
2990 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2991 draw_stretch_after(dctx,
2992 x*inst->font_width+inst->window_border,
2993 y*inst->font_height+inst->window_border,
2994 rlen*widefactor*inst->font_width, TRUE,
2996 ((lattr & LATTR_MODE) != LATTR_WIDE),
2997 ((lattr & LATTR_MODE) == LATTR_BOT));
3001 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3002 unsigned long attr, int lattr)
3004 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3005 struct gui_data *inst = dctx->inst;
3008 do_text_internal(ctx, x, y, text, len, attr, lattr);
3010 if (attr & ATTR_WIDE) {
3016 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3018 if (x >= inst->term->cols)
3020 if (x + len*2*widefactor > inst->term->cols)
3021 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3026 x*inst->font_width+inst->window_border,
3027 y*inst->font_height+inst->window_border,
3028 len*widefactor*inst->font_width, inst->font_height);
3031 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
3032 unsigned long attr, int lattr)
3034 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3035 struct gui_data *inst = dctx->inst;
3037 int active, passive, widefactor;
3039 if (attr & TATTR_PASCURS) {
3040 attr &= ~TATTR_PASCURS;
3044 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
3045 attr &= ~TATTR_ACTCURS;
3049 do_text_internal(ctx, x, y, text, len, attr, lattr);
3051 if (attr & TATTR_COMBINING)
3054 if (attr & ATTR_WIDE) {
3060 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3062 if (x >= inst->term->cols)
3064 if (x + len*2*widefactor > inst->term->cols)
3065 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3069 if (inst->cursor_type == 0) {
3071 * An active block cursor will already have been done by
3072 * the above do_text call, so we only need to do anything
3076 draw_set_colour(dctx, 261);
3077 draw_rectangle(dctx, FALSE,
3078 x*inst->font_width+inst->window_border,
3079 y*inst->font_height+inst->window_border,
3080 len*widefactor*inst->font_width-1,
3081 inst->font_height-1);
3085 int startx, starty, dx, dy, length, i;
3089 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
3090 char_width = 2*inst->font_width;
3092 char_width = inst->font_width;
3094 if (inst->cursor_type == 1) {
3095 uheight = inst->fonts[0]->ascent + 1;
3096 if (uheight >= inst->font_height)
3097 uheight = inst->font_height - 1;
3099 startx = x * inst->font_width + inst->window_border;
3100 starty = y * inst->font_height + inst->window_border + uheight;
3103 length = len * widefactor * char_width;
3106 if (attr & TATTR_RIGHTCURS)
3107 xadjust = char_width - 1;
3108 startx = x * inst->font_width + inst->window_border + xadjust;
3109 starty = y * inst->font_height + inst->window_border;
3112 length = inst->font_height;
3115 draw_set_colour(dctx, 261);
3117 for (i = 0; i < length; i++) {
3119 draw_point(dctx, startx, starty);
3124 } else if (active) {
3125 draw_line(dctx, startx, starty,
3126 startx + (length-1) * dx, starty + (length-1) * dy);
3127 } /* else no cursor (e.g., blinked off) */
3131 x*inst->font_width+inst->window_border,
3132 y*inst->font_height+inst->window_border,
3133 len*widefactor*inst->font_width, inst->font_height);
3135 #if GTK_CHECK_VERSION(2,0,0)
3137 GdkRectangle cursorrect;
3138 cursorrect.x = x*inst->font_width+inst->window_border;
3139 cursorrect.y = y*inst->font_height+inst->window_border;
3140 cursorrect.width = len*widefactor*inst->font_width;
3141 cursorrect.height = inst->font_height;
3142 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
3147 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
3149 if (cursor_val == -1) {
3150 #if GTK_CHECK_VERSION(2,16,0)
3151 cursor_val = GDK_BLANK_CURSOR;
3154 * Work around absence of GDK_BLANK_CURSOR by inventing a
3158 GdkColor bg = { 0, 0, 0, 0 };
3159 GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
3160 GdkGC *gc = gdk_gc_new(pm);
3161 gdk_gc_set_foreground(gc, &bg);
3162 gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
3164 ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
3165 gdk_pixmap_unref(pm);
3170 return gdk_cursor_new(cursor_val);
3173 void modalfatalbox(const char *p, ...)
3176 fprintf(stderr, "FATAL ERROR: ");
3178 vfprintf(stderr, p, ap);
3180 fputc('\n', stderr);
3184 void cmdline_error(const char *p, ...)
3187 fprintf(stderr, "%s: ", appname);
3189 vfprintf(stderr, p, ap);
3191 fputc('\n', stderr);
3195 const char *get_x_display(void *frontend)
3197 return gdk_get_display();
3200 #ifndef NOT_X_WINDOWS
3201 long get_windowid(void *frontend)
3203 struct gui_data *inst = (struct gui_data *)frontend;
3204 return (long)GDK_WINDOW_XID(gtk_widget_get_window(inst->area));
3208 static void help(FILE *fp) {
3210 "pterm option summary:\n"
3212 " --display DISPLAY Specify X display to use (note '--')\n"
3213 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
3214 " -fn FONT Normal text font\n"
3215 " -fb FONT Bold text font\n"
3216 " -geometry GEOMETRY Position and size of window (size in characters)\n"
3217 " -sl LINES Number of lines of scrollback\n"
3218 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
3219 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
3220 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
3221 " -T TITLE Window title\n"
3222 " -ut, +ut Do(default) or do not update utmp\n"
3223 " -ls, +ls Do(default) or do not make shell a login shell\n"
3224 " -sb, +sb Do(default) or do not display a scrollbar\n"
3225 " -log PATH Log all output to a file\n"
3226 " -nethack Map numeric keypad to hjklyubn direction keys\n"
3227 " -xrm RESOURCE-STRING Set an X resource\n"
3228 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
3229 ) < 0 || fflush(fp) < 0) {
3230 perror("output error");
3235 static void version(FILE *fp) {
3236 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
3237 perror("output error");
3242 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
3243 struct gui_data *inst, Conf *conf)
3249 * Macros to make argument handling easier. Note that because
3250 * they need to call `continue', they cannot be contained in
3251 * the usual do {...} while (0) wrapper to make them
3252 * syntactically single statements; hence it is not legal to
3253 * use one of these macros as an unbraced statement between
3256 #define EXPECTS_ARG { \
3257 if (--argc <= 0) { \
3259 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
3264 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
3266 while (--argc > 0) {
3267 const char *p = *++argv;
3271 * Shameless cheating. Debian requires all X terminal
3272 * emulators to support `-T title'; but
3273 * cmdline_process_param will eat -T (it means no-pty) and
3274 * complain that pterm doesn't support it. So, in pterm
3275 * only, we convert -T into -title.
3277 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
3281 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
3282 do_everything ? 1 : -1, conf);
3285 cmdline_error("option \"%s\" requires an argument", p);
3286 } else if (ret == 2) {
3287 --argc, ++argv; /* skip next argument */
3289 } else if (ret == 1) {
3293 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
3297 fs = fontspec_new(val);
3298 conf_set_fontspec(conf, CONF_font, fs);
3301 } else if (!strcmp(p, "-fb")) {
3305 fs = fontspec_new(val);
3306 conf_set_fontspec(conf, CONF_boldfont, fs);
3309 } else if (!strcmp(p, "-fw")) {
3313 fs = fontspec_new(val);
3314 conf_set_fontspec(conf, CONF_widefont, fs);
3317 } else if (!strcmp(p, "-fwb")) {
3321 fs = fontspec_new(val);
3322 conf_set_fontspec(conf, CONF_wideboldfont, fs);
3325 } else if (!strcmp(p, "-cs")) {
3328 conf_set_str(conf, CONF_line_codepage, val);
3330 } else if (!strcmp(p, "-geometry")) {
3334 #if GTK_CHECK_VERSION(2,0,0)
3335 inst->geometry = val;
3337 /* On GTK 1, we have to do this using raw Xlib */
3341 flags = XParseGeometry(val, &x, &y, &w, &h);
3342 if (flags & WidthValue)
3343 conf_set_int(conf, CONF_width, w);
3344 if (flags & HeightValue)
3345 conf_set_int(conf, CONF_height, h);
3347 if (flags & (XValue | YValue)) {
3350 inst->gotpos = TRUE;
3351 inst->gravity = ((flags & XNegative ? 1 : 0) |
3352 (flags & YNegative ? 2 : 0));
3357 } else if (!strcmp(p, "-sl")) {
3360 conf_set_int(conf, CONF_savelines, atoi(val));
3362 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
3363 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
3364 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
3369 #if GTK_CHECK_VERSION(3,0,0)
3371 int success = gdk_rgba_parse(&rgba, val);
3374 int success = gdk_color_parse(val, &col);
3379 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
3382 #if GTK_CHECK_VERSION(3,0,0)
3383 int r = rgba.red * 255;
3384 int g = rgba.green * 255;
3385 int b = rgba.blue * 255;
3387 int r = col.red / 256;
3388 int g = col.green / 256;
3389 int b = col.blue / 256;
3393 index = (!strcmp(p, "-fg") ? 0 :
3394 !strcmp(p, "-bg") ? 2 :
3395 !strcmp(p, "-bfg") ? 1 :
3396 !strcmp(p, "-bbg") ? 3 :
3397 !strcmp(p, "-cfg") ? 4 :
3398 !strcmp(p, "-cbg") ? 5 : -1);
3399 assert(index != -1);
3401 conf_set_int_int(conf, CONF_colours, index*3+0, r);
3402 conf_set_int_int(conf, CONF_colours, index*3+1, g);
3403 conf_set_int_int(conf, CONF_colours, index*3+2, b);
3407 } else if (use_pty_argv && !strcmp(p, "-e")) {
3408 /* This option swallows all further arguments. */
3414 pty_argv = snewn(argc+1, char *);
3416 for (i = 0; i < argc; i++)
3417 pty_argv[i] = argv[i];
3418 pty_argv[argc] = NULL;
3419 break; /* finished command-line processing */
3421 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
3424 } else if (!strcmp(p, "-title")) {
3427 conf_set_str(conf, CONF_wintitle, val);
3429 } else if (!strcmp(p, "-log")) {
3433 fn = filename_from_str(val);
3434 conf_set_filename(conf, CONF_logfilename, fn);
3435 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
3438 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
3440 conf_set_int(conf, CONF_stamp_utmp, 0);
3442 } else if (!strcmp(p, "-ut")) {
3444 conf_set_int(conf, CONF_stamp_utmp, 1);
3446 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
3448 conf_set_int(conf, CONF_login_shell, 0);
3450 } else if (!strcmp(p, "-ls")) {
3452 conf_set_int(conf, CONF_login_shell, 1);
3454 } else if (!strcmp(p, "-nethack")) {
3456 conf_set_int(conf, CONF_nethack_keypad, 1);
3458 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
3460 conf_set_int(conf, CONF_scrollbar, 0);
3462 } else if (!strcmp(p, "-sb")) {
3464 conf_set_int(conf, CONF_scrollbar, 1);
3466 } else if (!strcmp(p, "-name")) {
3470 } else if (!strcmp(p, "-xrm")) {
3472 provide_xrm_string(val);
3474 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
3478 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
3482 } else if (!strcmp(p, "-pgpfp")) {
3486 } else if(p[0] != '-' && (!do_everything ||
3487 process_nonoption_arg(p, conf,
3493 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
3501 #if GTK_CHECK_VERSION(2,0,0)
3509 uxsel_id *uxsel_input_add(int fd, int rwx) {
3510 uxsel_id *id = snew(uxsel_id);
3512 #if GTK_CHECK_VERSION(2,0,0)
3514 if (rwx & 1) flags |= G_IO_IN;
3515 if (rwx & 2) flags |= G_IO_OUT;
3516 if (rwx & 4) flags |= G_IO_PRI;
3517 id->chan = g_io_channel_unix_new(fd);
3518 g_io_channel_set_encoding(id->chan, NULL, NULL);
3519 id->watch_id = g_io_add_watch(id->chan, flags, fd_input_func, NULL);
3522 if (rwx & 1) flags |= GDK_INPUT_READ;
3523 if (rwx & 2) flags |= GDK_INPUT_WRITE;
3524 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
3526 id->id = gdk_input_add(fd, flags, fd_input_func, NULL);
3532 void uxsel_input_remove(uxsel_id *id) {
3533 #if GTK_CHECK_VERSION(2,0,0)
3534 g_source_remove(id->watch_id);
3535 g_io_channel_unref(id->chan);
3537 gdk_input_remove(id->id);
3542 char *setup_fonts_ucs(struct gui_data *inst)
3544 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
3545 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
3550 fs = conf_get_fontspec(inst->conf, CONF_font);
3551 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
3552 shadowboldoffset, shadowbold);
3554 return dupprintf("unable to load font \"%s\"", fs->name);
3557 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
3558 if (shadowbold || !fs->name[0]) {
3561 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
3562 shadowboldoffset, shadowbold);
3565 unifont_destroy(fonts[0]);
3566 return dupprintf("unable to load bold font \"%s\"", fs->name);
3570 fs = conf_get_fontspec(inst->conf, CONF_widefont);
3572 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
3573 shadowboldoffset, shadowbold);
3575 for (i = 0; i < 2; i++)
3577 unifont_destroy(fonts[i]);
3578 return dupprintf("unable to load wide font \"%s\"", fs->name);
3584 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
3585 if (shadowbold || !fs->name[0]) {
3588 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
3589 shadowboldoffset, shadowbold);
3591 for (i = 0; i < 3; i++)
3593 unifont_destroy(fonts[i]);
3594 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
3599 * Now we've got past all the possible error conditions, we can
3600 * actually update our state.
3603 for (i = 0; i < 4; i++) {
3605 unifont_destroy(inst->fonts[i]);
3606 inst->fonts[i] = fonts[i];
3609 inst->font_width = inst->fonts[0]->width;
3610 inst->font_height = inst->fonts[0]->height;
3612 inst->direct_to_font = init_ucs(&inst->ucsdata,
3613 conf_get_str(inst->conf, CONF_line_codepage),
3614 conf_get_int(inst->conf, CONF_utf8_override),
3615 inst->fonts[0]->public_charset,
3616 conf_get_int(inst->conf, CONF_vtmode));
3618 inst->drawtype = inst->fonts[0]->preferred_drawtype;
3623 void set_geom_hints(struct gui_data *inst)
3626 geom.min_width = inst->font_width + 2*inst->window_border;
3627 geom.min_height = inst->font_height + 2*inst->window_border;
3628 geom.max_width = geom.max_height = -1;
3629 geom.base_width = 2*inst->window_border;
3630 geom.base_height = 2*inst->window_border;
3631 geom.width_inc = inst->font_width;
3632 geom.height_inc = inst->font_height;
3633 geom.min_aspect = geom.max_aspect = 0;
3634 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
3635 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
3636 GDK_HINT_RESIZE_INC);
3639 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
3641 struct gui_data *inst = (struct gui_data *)data;
3642 term_clrsb(inst->term);
3645 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3647 struct gui_data *inst = (struct gui_data *)data;
3648 term_pwron(inst->term, TRUE);
3650 ldisc_echoedit_update(inst->ldisc);
3653 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3655 struct gui_data *inst = (struct gui_data *)data;
3656 term_copyall(inst->term);
3659 void special_menuitem(GtkMenuItem *item, gpointer data)
3661 struct gui_data *inst = (struct gui_data *)data;
3662 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
3666 inst->back->special(inst->backhandle, code);
3669 void about_menuitem(GtkMenuItem *item, gpointer data)
3671 struct gui_data *inst = (struct gui_data *)data;
3672 about_box(inst->window);
3675 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3677 struct gui_data *inst = (struct gui_data *)data;
3678 showeventlog(inst->eventlogstuff, inst->window);
3681 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3683 /* This maps colour indices in inst->conf to those used in inst->cols. */
3684 static const int ww[] = {
3685 256, 257, 258, 259, 260, 261,
3686 0, 8, 1, 9, 2, 10, 3, 11,
3687 4, 12, 5, 13, 6, 14, 7, 15
3689 struct gui_data *inst = (struct gui_data *)data;
3691 Conf *oldconf, *newconf;
3692 int i, j, need_size;
3694 assert(lenof(ww) == NCFGCOLOURS);
3696 if (inst->reconfiguring)
3699 inst->reconfiguring = TRUE;
3701 title = dupcat(appname, " Reconfiguration", NULL);
3703 oldconf = inst->conf;
3704 newconf = conf_copy(inst->conf);
3706 if (do_config_box(title, newconf, 1,
3707 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3708 inst->conf = newconf;
3710 /* Pass new config data to the logging module */
3711 log_reconfig(inst->logctx, inst->conf);
3713 * Flush the line discipline's edit buffer in the case
3714 * where local editing has just been disabled.
3717 ldisc_configure(inst->ldisc, inst->conf);
3718 ldisc_echoedit_update(inst->ldisc);
3720 /* Pass new config data to the terminal */
3721 term_reconfig(inst->term, inst->conf);
3722 /* Pass new config data to the back end */
3724 inst->back->reconfig(inst->backhandle, inst->conf);
3726 cache_conf_values(inst);
3729 * Just setting inst->conf is sufficient to cause colour
3730 * setting changes to appear on the next ESC]R palette
3731 * reset. But we should also check whether any colour
3732 * settings have been changed, and revert the ones that have
3733 * to the new default, on the assumption that the user is
3734 * most likely to want an immediate update.
3736 for (i = 0; i < NCFGCOLOURS; i++) {
3737 for (j = 0; j < 3; j++)
3738 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3739 conf_get_int_int(newconf, CONF_colours, i*3+j))
3742 real_palette_set(inst, ww[i],
3743 conf_get_int_int(newconf,CONF_colours,i*3+0),
3744 conf_get_int_int(newconf,CONF_colours,i*3+1),
3745 conf_get_int_int(newconf,CONF_colours,i*3+2));
3748 * If the default background has changed, we must
3749 * repaint the space in between the window border
3750 * and the text area.
3753 set_window_background(inst);
3754 draw_backing_rect(inst);
3760 * If the scrollbar needs to be shown, hidden, or moved
3761 * from one end to the other of the window, do so now.
3763 if (conf_get_int(oldconf, CONF_scrollbar) !=
3764 conf_get_int(newconf, CONF_scrollbar)) {
3765 if (conf_get_int(newconf, CONF_scrollbar))
3766 gtk_widget_show(inst->sbar);
3768 gtk_widget_hide(inst->sbar);
3770 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3771 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3772 gtk_box_reorder_child(inst->hbox, inst->sbar,
3773 conf_get_int(newconf, CONF_scrollbar_on_left)
3778 * Change the window title, if required.
3780 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3781 conf_get_str(newconf, CONF_wintitle)))
3782 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3783 set_window_titles(inst);
3786 * Redo the whole tangled fonts and Unicode mess if
3790 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3791 conf_get_fontspec(newconf, CONF_font)->name) ||
3792 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3793 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3794 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3795 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3796 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3797 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3798 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3799 conf_get_str(newconf, CONF_line_codepage)) ||
3800 conf_get_int(oldconf, CONF_utf8_override) !=
3801 conf_get_int(newconf, CONF_utf8_override) ||
3802 conf_get_int(oldconf, CONF_vtmode) !=
3803 conf_get_int(newconf, CONF_vtmode) ||
3804 conf_get_int(oldconf, CONF_shadowbold) !=
3805 conf_get_int(newconf, CONF_shadowbold) ||
3806 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3807 conf_get_int(newconf, CONF_shadowboldoffset)) {
3808 char *errmsg = setup_fonts_ucs(inst);
3811 dupprintf("Could not change fonts in terminal window: %s\n",
3813 messagebox(inst->window, "Font setup error", msgboxtext,
3814 string_width("Could not change fonts in terminal window:"),
3825 * Resize the window.
3827 if (conf_get_int(oldconf, CONF_width) !=
3828 conf_get_int(newconf, CONF_width) ||
3829 conf_get_int(oldconf, CONF_height) !=
3830 conf_get_int(newconf, CONF_height) ||
3831 conf_get_int(oldconf, CONF_window_border) !=
3832 conf_get_int(newconf, CONF_window_border) ||
3834 set_geom_hints(inst);
3835 request_resize(inst, conf_get_int(newconf, CONF_width),
3836 conf_get_int(newconf, CONF_height));
3839 * The above will have caused a call to term_size() for
3840 * us if it happened. If the user has fiddled with only
3841 * the scrollback size, the above will not have
3842 * happened and we will need an explicit term_size()
3845 if (conf_get_int(oldconf, CONF_savelines) !=
3846 conf_get_int(newconf, CONF_savelines))
3847 term_size(inst->term, inst->term->rows, inst->term->cols,
3848 conf_get_int(newconf, CONF_savelines));
3851 term_invalidate(inst->term);
3854 * We do an explicit full redraw here to ensure the window
3855 * border has been redrawn as well as the text area.
3857 gtk_widget_queue_draw(inst->area);
3864 inst->reconfiguring = FALSE;
3867 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3870 * Re-execing ourself is not an exact science under Unix. I do
3871 * the best I can by using /proc/self/exe if available and by
3872 * assuming argv[0] can be found on $PATH if not.
3874 * Note that we also have to reconstruct the elements of the
3875 * original argv which gtk swallowed, since the user wants the
3876 * new session to appear on the same X display as the old one.
3884 * Collect the arguments with which to re-exec ourself.
3886 va_start(ap, fd_to_close);
3887 n = 2; /* progname and terminating NULL */
3888 n += inst->ngtkargs;
3889 while (va_arg(ap, char *) != NULL)
3893 args = snewn(n, char *);
3894 args[0] = inst->progname;
3896 for (i = 0; i < inst->ngtkargs; i++)
3897 args[i+1] = inst->gtkargvstart[i];
3900 va_start(ap, fd_to_close);
3901 while ((args[i++] = va_arg(ap, char *)) != NULL);
3907 * Do the double fork.
3921 } else if (pid2 > 0) {
3923 * First child has successfully forked second child. My
3924 * Work Here Is Done. Note the use of _exit rather than
3925 * exit: the latter appears to cause destroy messages
3926 * to be sent to the X server. I suspect gtk uses
3933 * If we reach here, we are the second child, so we now
3934 * actually perform the exec.
3936 if (fd_to_close >= 0)
3939 execv("/proc/self/exe", args);
3940 execvp(inst->progname, args);
3947 waitpid(pid, &status, 0);
3952 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3954 struct gui_data *inst = (struct gui_data *)gdata;
3956 * For this feature we must marshal conf and (possibly) pty_argv
3957 * into a byte stream, create a pipe, and send this byte stream
3958 * to the child through the pipe.
3960 int i, ret, sersize, size;
3965 if (pipe(pipefd) < 0) {
3970 size = sersize = conf_serialised_size(inst->conf);
3971 if (use_pty_argv && pty_argv) {
3972 for (i = 0; pty_argv[i]; i++)
3973 size += strlen(pty_argv[i]) + 1;
3976 data = snewn(size, char);
3977 conf_serialise(inst->conf, data);
3978 if (use_pty_argv && pty_argv) {
3980 for (i = 0; pty_argv[i]; i++) {
3981 strcpy(data + p, pty_argv[i]);
3982 p += strlen(pty_argv[i]) + 1;
3987 sprintf(option, "---[%d,%d]", pipefd[0], size);
3988 noncloexec(pipefd[0]);
3989 fork_and_exec_self(inst, pipefd[1], option, NULL);
3993 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3996 perror("write to pipe");
4001 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
4003 int fd, i, ret, size, size_used;
4006 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
4007 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
4011 data = snewn(size, char);
4013 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
4016 perror("read from pipe");
4018 } else if (i < size) {
4019 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
4024 size_used = conf_deserialise(conf, data, size);
4025 if (use_pty_argv && size > size_used) {
4029 while (i < size && data[i]) i++;
4031 fprintf(stderr, "%s: malformed Duplicate Session data\n",
4038 pty_argv = snewn(n+1, char *);
4044 while (i < size && data[i]) i++;
4047 pty_argv[n++] = dupstr(p);
4056 void new_session_menuitem(GtkMenuItem *item, gpointer data)
4058 struct gui_data *inst = (struct gui_data *)data;
4060 fork_and_exec_self(inst, -1, NULL);
4063 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
4065 struct gui_data *inst = (struct gui_data *)data;
4068 logevent(inst, "----- Session restarted -----");
4069 term_pwron(inst->term, FALSE);
4070 start_backend(inst);
4071 inst->exited = FALSE;
4075 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
4077 struct gui_data *inst = (struct gui_data *)data;
4078 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4080 fork_and_exec_self(inst, -1, "-load", str, NULL);
4083 void saved_session_freedata(GtkMenuItem *item, gpointer data)
4085 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4090 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
4092 struct gui_data *inst = (struct gui_data *)data;
4093 struct sesslist sesslist;
4096 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
4097 (GtkCallback)gtk_widget_destroy, NULL);
4099 get_sesslist(&sesslist, TRUE);
4100 /* skip sesslist.sessions[0] == Default Settings */
4101 for (i = 1; i < sesslist.nsessions; i++) {
4102 GtkWidget *menuitem =
4103 gtk_menu_item_new_with_label(sesslist.sessions[i]);
4104 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4105 gtk_widget_show(menuitem);
4106 g_object_set_data(G_OBJECT(menuitem), "user-data",
4107 dupstr(sesslist.sessions[i]));
4108 g_signal_connect(G_OBJECT(menuitem), "activate",
4109 G_CALLBACK(saved_session_menuitem),
4111 g_signal_connect(G_OBJECT(menuitem), "destroy",
4112 G_CALLBACK(saved_session_freedata),
4115 if (sesslist.nsessions <= 1) {
4116 GtkWidget *menuitem =
4117 gtk_menu_item_new_with_label("(No sessions)");
4118 gtk_widget_set_sensitive(menuitem, FALSE);
4119 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4120 gtk_widget_show(menuitem);
4122 get_sesslist(&sesslist, FALSE); /* free up */
4125 void set_window_icon(GtkWidget *window, const char *const *const *icon,
4128 #if GTK_CHECK_VERSION(2,0,0)
4133 GdkBitmap *iconmask;
4139 gtk_widget_realize(window);
4140 #if GTK_CHECK_VERSION(2,0,0)
4141 gtk_window_set_icon(GTK_WINDOW(window),
4142 gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
4144 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
4145 &iconmask, NULL, (gchar **)icon[0]);
4146 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
4149 #if GTK_CHECK_VERSION(2,0,0)
4151 for (n = 0; n < n_icon; n++) {
4153 g_list_append(iconlist,
4154 gdk_pixbuf_new_from_xpm_data((const gchar **)
4157 gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
4161 void update_specials_menu(void *frontend)
4163 struct gui_data *inst = (struct gui_data *)frontend;
4165 const struct telnet_special *specials;
4168 specials = inst->back->get_specials(inst->backhandle);
4172 /* I believe this disposes of submenus too. */
4173 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
4174 (GtkCallback)gtk_widget_destroy, NULL);
4177 GtkWidget *menu = inst->specialsmenu;
4178 /* A lame "stack" for submenus that will do for now. */
4179 GtkWidget *saved_menu = NULL;
4181 for (i = 0; nesting > 0; i++) {
4182 GtkWidget *menuitem = NULL;
4183 switch (specials[i].code) {
4185 assert (nesting < 2);
4186 saved_menu = menu; /* XXX lame stacking */
4187 menu = gtk_menu_new();
4188 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4189 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
4190 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
4191 gtk_widget_show(menuitem);
4198 menu = saved_menu; /* XXX lame stacking */
4203 menuitem = gtk_menu_item_new();
4206 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4207 g_object_set_data(G_OBJECT(menuitem), "user-data",
4208 GINT_TO_POINTER(specials[i].code));
4209 g_signal_connect(G_OBJECT(menuitem), "activate",
4210 G_CALLBACK(special_menuitem), inst);
4214 gtk_container_add(GTK_CONTAINER(menu), menuitem);
4215 gtk_widget_show(menuitem);
4218 gtk_widget_show(inst->specialsitem1);
4219 gtk_widget_show(inst->specialsitem2);
4221 gtk_widget_hide(inst->specialsitem1);
4222 gtk_widget_hide(inst->specialsitem2);
4226 static void start_backend(struct gui_data *inst)
4228 extern Backend *select_backend(Conf *conf);
4233 inst->back = select_backend(inst->conf);
4235 error = inst->back->init((void *)inst, &inst->backhandle,
4237 conf_get_str(inst->conf, CONF_host),
4238 conf_get_int(inst->conf, CONF_port),
4240 conf_get_int(inst->conf, CONF_tcp_nodelay),
4241 conf_get_int(inst->conf, CONF_tcp_keepalives));
4244 char *msg = dupprintf("Unable to open connection to %s:\n%s",
4245 conf_get_str(inst->conf, CONF_host), error);
4246 inst->exited = TRUE;
4247 fatal_message_box(inst->window, msg);
4252 s = conf_get_str(inst->conf, CONF_wintitle);
4254 set_title_and_icon(inst, s, s);
4256 char *title = make_default_wintitle(realhost);
4257 set_title_and_icon(inst, title, title);
4262 inst->back->provide_logctx(inst->backhandle, inst->logctx);
4264 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
4267 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
4270 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4273 int pt_main(int argc, char **argv)
4275 extern int cfgbox(Conf *conf);
4276 struct gui_data *inst;
4278 setlocale(LC_CTYPE, "");
4281 * Create an instance structure and initialise to zeroes
4283 inst = snew(struct gui_data);
4284 memset(inst, 0, sizeof(*inst));
4285 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
4286 inst->busy_status = BUSY_NOT;
4287 inst->conf = conf_new();
4288 inst->wintitle = inst->icontitle = NULL;
4289 inst->quit_fn_scheduled = FALSE;
4290 inst->idle_fn_scheduled = FALSE;
4291 inst->drawtype = DRAWTYPE_DEFAULT;
4293 /* defer any child exit handling until we're ready to deal with
4295 block_signal(SIGCHLD, 1);
4297 inst->progname = argv[0];
4299 * Copy the original argv before letting gtk_init fiddle with
4300 * it. It will be required later.
4304 inst->gtkargvstart = snewn(argc-1, char *);
4305 for (i = 1; i < argc; i++)
4306 inst->gtkargvstart[i-1] = dupstr(argv[i]);
4308 gtk_init(&argc, &argv);
4309 inst->ngtkargs = oldargc - argc;
4312 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
4313 read_dupsession_data(inst, inst->conf, argv[1]);
4314 /* Splatter this argument so it doesn't clutter a ps listing */
4315 smemclr(argv[1], strlen(argv[1]));
4317 /* By default, we bring up the config dialog, rather than launching
4318 * a session. This gets set to TRUE if something happens to change
4319 * that (e.g., a hostname is specified on the command-line). */
4320 int allow_launch = FALSE;
4321 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
4322 exit(1); /* pre-defaults pass to get -class */
4323 do_defaults(NULL, inst->conf);
4324 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
4325 exit(1); /* post-defaults, do everything */
4327 cmdline_run_saved(inst->conf);
4330 allow_launch = TRUE;
4332 if ((!allow_launch || !conf_launchable(inst->conf)) &&
4333 !cfgbox(inst->conf))
4334 exit(0); /* config box hit Cancel */
4337 if (!compound_text_atom)
4338 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
4339 if (!utf8_string_atom)
4340 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
4342 inst->area = gtk_drawing_area_new();
4344 #if GTK_CHECK_VERSION(2,0,0)
4345 inst->imc = gtk_im_multicontext_new();
4349 char *errmsg = setup_fonts_ucs(inst);
4351 fprintf(stderr, "%s: %s\n", appname, errmsg);
4357 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4359 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
4361 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
4362 winclass, winclass);
4366 * Set up the colour map.
4368 palette_reset(inst);
4370 inst->width = conf_get_int(inst->conf, CONF_width);
4371 inst->height = conf_get_int(inst->conf, CONF_height);
4372 cache_conf_values(inst);
4375 int w = inst->font_width * inst->width + 2*inst->window_border;
4376 int h = inst->font_height * inst->height + 2*inst->window_border;
4377 #if GTK_CHECK_VERSION(2,0,0)
4378 gtk_widget_set_size_request(inst->area, w, h);
4380 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
4383 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
4384 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
4385 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
4387 * We always create the scrollbar; it remains invisible if
4388 * unwanted, so we can pop it up quickly if it suddenly becomes
4391 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
4392 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4393 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
4394 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
4395 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4397 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
4399 set_geom_hints(inst);
4401 gtk_widget_show(inst->area);
4402 if (conf_get_int(inst->conf, CONF_scrollbar))
4403 gtk_widget_show(inst->sbar);
4405 gtk_widget_hide(inst->sbar);
4406 gtk_widget_show(GTK_WIDGET(inst->hbox));
4408 #if GTK_CHECK_VERSION(2,0,0)
4409 if (inst->geometry) {
4410 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
4414 int x = inst->xpos, y = inst->ypos;
4416 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
4417 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
4418 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
4419 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
4420 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
4424 g_signal_connect(G_OBJECT(inst->window), "destroy",
4425 G_CALLBACK(destroy), inst);
4426 g_signal_connect(G_OBJECT(inst->window), "delete_event",
4427 G_CALLBACK(delete_window), inst);
4428 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
4429 G_CALLBACK(key_event), inst);
4430 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
4431 G_CALLBACK(key_event), inst);
4432 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
4433 G_CALLBACK(focus_event), inst);
4434 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
4435 G_CALLBACK(focus_event), inst);
4436 g_signal_connect(G_OBJECT(inst->area), "configure_event",
4437 G_CALLBACK(configure_area), inst);
4438 #if GTK_CHECK_VERSION(3,0,0)
4439 g_signal_connect(G_OBJECT(inst->area), "draw",
4440 G_CALLBACK(draw_area), inst);
4442 g_signal_connect(G_OBJECT(inst->area), "expose_event",
4443 G_CALLBACK(expose_area), inst);
4445 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
4446 G_CALLBACK(button_event), inst);
4447 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
4448 G_CALLBACK(button_event), inst);
4449 #if GTK_CHECK_VERSION(2,0,0)
4450 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
4451 G_CALLBACK(scroll_event), inst);
4453 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
4454 G_CALLBACK(motion_event), inst);
4455 g_signal_connect(G_OBJECT(inst->area), "selection_received",
4456 G_CALLBACK(selection_received), inst);
4457 g_signal_connect(G_OBJECT(inst->area), "selection_get",
4458 G_CALLBACK(selection_get), inst);
4459 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
4460 G_CALLBACK(selection_clear), inst);
4461 #if GTK_CHECK_VERSION(2,0,0)
4462 g_signal_connect(G_OBJECT(inst->imc), "commit",
4463 G_CALLBACK(input_method_commit_event), inst);
4465 if (conf_get_int(inst->conf, CONF_scrollbar))
4466 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
4467 G_CALLBACK(scrollbar_moved), inst);
4468 gtk_widget_add_events(GTK_WIDGET(inst->area),
4469 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
4470 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
4471 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
4474 extern const char *const *const main_icon[];
4475 extern const int n_main_icon;
4476 set_window_icon(inst->window, main_icon, n_main_icon);
4479 gtk_widget_show(inst->window);
4481 set_window_background(inst);
4484 * Set up the Ctrl+rightclick context menu.
4487 GtkWidget *menuitem;
4489 extern const int use_event_log, new_session, saved_sessions;
4491 inst->menu = gtk_menu_new();
4493 #define MKMENUITEM(title, func) do \
4495 menuitem = gtk_menu_item_new_with_label(title); \
4496 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4497 gtk_widget_show(menuitem); \
4498 g_signal_connect(G_OBJECT(menuitem), "activate", \
4499 G_CALLBACK(func), inst); \
4502 #define MKSUBMENU(title) do \
4504 menuitem = gtk_menu_item_new_with_label(title); \
4505 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4506 gtk_widget_show(menuitem); \
4509 #define MKSEP() do \
4511 menuitem = gtk_menu_item_new(); \
4512 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4513 gtk_widget_show(menuitem); \
4517 MKMENUITEM("New Session...", new_session_menuitem);
4518 MKMENUITEM("Restart Session", restart_session_menuitem);
4519 inst->restartitem = menuitem;
4520 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4521 MKMENUITEM("Duplicate Session", dup_session_menuitem);
4522 if (saved_sessions) {
4523 inst->sessionsmenu = gtk_menu_new();
4524 /* sessionsmenu will be updated when it's invoked */
4525 /* XXX is this the right way to do dynamic menus in Gtk? */
4526 MKMENUITEM("Saved Sessions", update_savedsess_menu);
4527 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
4528 inst->sessionsmenu);
4531 MKMENUITEM("Change Settings...", change_settings_menuitem);
4534 MKMENUITEM("Event Log", event_log_menuitem);
4535 MKSUBMENU("Special Commands");
4536 inst->specialsmenu = gtk_menu_new();
4537 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
4538 inst->specialsitem1 = menuitem;
4540 inst->specialsitem2 = menuitem;
4541 gtk_widget_hide(inst->specialsitem1);
4542 gtk_widget_hide(inst->specialsitem2);
4543 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
4544 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
4545 MKMENUITEM("Copy All", copy_all_menuitem);
4547 s = dupcat("About ", appname, NULL);
4548 MKMENUITEM(s, about_menuitem);
4555 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
4556 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
4557 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
4558 inst->blankcursor = make_mouse_ptr(inst, -1);
4559 inst->currcursor = inst->textcursor;
4560 show_mouseptr(inst, 1);
4562 inst->eventlogstuff = eventlogstuff_new();
4564 request_callback_notifications(notify_toplevel_callback, inst);
4566 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
4567 inst->logctx = log_init(inst, inst->conf);
4568 term_provide_logctx(inst->term, inst->logctx);
4572 term_size(inst->term, inst->height, inst->width,
4573 conf_get_int(inst->conf, CONF_savelines));
4575 start_backend(inst);
4577 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
4579 /* now we're reday to deal with the child exit handler being
4581 block_signal(SIGCHLD, 0);
4584 * Block SIGPIPE: if we attempt Duplicate Session or similar
4585 * and it falls over in some way, we certainly don't want
4586 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
4587 * this _after_ (at least pterm) forks off its child process,
4588 * since the child wants SIGPIPE handled in the usual way.
4590 block_signal(SIGPIPE, 1);
4592 inst->exited = FALSE;