2 * gtkwin.c: the main code that runs a PuTTY terminal emulator and
3 * backend in a GTK window.
19 #include <sys/types.h>
22 #if !GTK_CHECK_VERSION(3,0,0)
23 #include <gdk/gdkkeysyms.h>
26 #if GTK_CHECK_VERSION(2,0,0)
27 #include <gtk/gtkimmodule.h>
30 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
32 #define MAY_REFER_TO_GTK_IN_HEADERS
36 #include "gtkcompat.h"
43 #include <X11/Xutil.h>
44 #include <X11/Xatom.h>
47 #define CAT2(x,y) x ## y
48 #define CAT(x,y) CAT2(x,y)
49 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
51 #if GTK_CHECK_VERSION(2,0,0)
52 ASSERT(sizeof(long) <= sizeof(gsize));
53 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
54 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
56 ASSERT(sizeof(long) <= sizeof(gpointer));
57 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
58 #define GPOINTER_TO_LONG(p) ((long)(p))
61 /* Colours come in two flavours: configurable, and xterm-extended. */
62 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
63 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
65 GdkAtom compound_text_atom, utf8_string_atom;
67 extern char **pty_argv; /* declared in pty.c */
68 extern int use_pty_argv;
71 * Timers are global across all sessions (even if we were handling
72 * multiple sessions, which we aren't), so the current timer ID is
75 static guint timer_id = 0;
78 GtkWidget *window, *area, *sbar;
80 GtkAdjustment *sbar_adjust;
81 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
83 GtkWidget *sessionsmenu;
84 #ifndef NO_BACKING_PIXMAPS
87 #if GTK_CHECK_VERSION(2,0,0)
90 unifont *fonts[4]; /* normal, bold, wide, widebold */
91 #if GTK_CHECK_VERSION(2,0,0)
94 int xpos, ypos, gotpos, gravity;
96 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
97 GdkColor cols[NALLCOLOURS];
98 #if !GTK_CHECK_VERSION(3,0,0)
101 wchar_t *pastein_data;
103 int pastein_data_len;
104 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
105 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
106 int font_width, font_height;
109 int mouseptr_visible;
111 guint toplevel_callback_idle_id;
112 int idle_fn_scheduled, quit_fn_scheduled;
117 int master_fd, master_func_id;
124 struct unicode_data ucsdata;
127 char *progname, **gtkargvstart;
129 guint32 input_event_time; /* Timestamp of the most recent input event. */
131 /* Cached things out of conf that we refer to a lot */
138 static void cache_conf_values(struct gui_data *inst)
140 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
141 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
142 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
146 struct gui_data *inst;
147 unifont_drawctx uctx;
150 static int send_raw_mouse;
152 static const char *app_name = "pterm";
154 static void start_backend(struct gui_data *inst);
155 static void exit_callback(void *vinst);
157 char *x_get_default(const char *key)
159 #ifndef NOT_X_WINDOWS
160 return XGetDefault(GDK_DISPLAY_XDISPLAY(gdk_display_get_default()),
167 void connection_fatal(void *frontend, const char *p, ...)
169 struct gui_data *inst = (struct gui_data *)frontend;
174 msg = dupvprintf(p, ap);
176 fatal_message_box(inst->window, msg);
179 queue_toplevel_callback(exit_callback, inst);
183 * Default settings that are specific to pterm.
185 FontSpec *platform_default_fontspec(const char *name)
187 if (!strcmp(name, "Font"))
188 return fontspec_new(DEFAULT_GTK_FONT);
190 return fontspec_new("");
193 Filename *platform_default_filename(const char *name)
195 if (!strcmp(name, "LogFileName"))
196 return filename_from_str("putty.log");
198 return filename_from_str("");
201 char *platform_default_s(const char *name)
203 if (!strcmp(name, "SerialLine"))
204 return dupstr("/dev/ttyS0");
208 int platform_default_i(const char *name, int def)
210 if (!strcmp(name, "CloseOnExit"))
211 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
212 if (!strcmp(name, "WinNameAlways"))
213 return 0; /* X natively supports icon titles, so use 'em by default */
217 /* Dummy routine, only required in plink. */
218 void frontend_echoedit_update(void *frontend, int echo, int edit)
222 char *get_ttymode(void *frontend, const char *mode)
224 struct gui_data *inst = (struct gui_data *)frontend;
225 return term_get_ttymode(inst->term, mode);
228 int from_backend(void *frontend, int is_stderr, const char *data, int len)
230 struct gui_data *inst = (struct gui_data *)frontend;
231 return term_data(inst->term, is_stderr, data, len);
234 int from_backend_untrusted(void *frontend, const char *data, int len)
236 struct gui_data *inst = (struct gui_data *)frontend;
237 return term_data_untrusted(inst->term, data, len);
240 int from_backend_eof(void *frontend)
242 return TRUE; /* do respond to incoming EOF with outgoing */
245 int get_userpass_input(prompts_t *p, const unsigned char *in, int inlen)
247 struct gui_data *inst = (struct gui_data *)p->frontend;
249 ret = cmdline_get_passwd_input(p, in, inlen);
251 ret = term_get_userpass_input(inst->term, p, in, inlen);
255 void logevent(void *frontend, const char *string)
257 struct gui_data *inst = (struct gui_data *)frontend;
259 log_eventlog(inst->logctx, string);
261 logevent_dlg(inst->eventlogstuff, string);
264 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
266 struct gui_data *inst = (struct gui_data *)frontend;
269 return inst->font_height;
271 return inst->font_width;
275 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
276 * into a cooked one (SELECT, EXTEND, PASTE).
278 * In Unix, this is not configurable; the X button arrangement is
279 * rock-solid across all applications, everyone has a three-button
280 * mouse or a means of faking it, and there is no need to switch
281 * buttons around at all.
283 static Mouse_Button translate_button(Mouse_Button button)
285 /* struct gui_data *inst = (struct gui_data *)frontend; */
287 if (button == MBT_LEFT)
289 if (button == MBT_MIDDLE)
291 if (button == MBT_RIGHT)
293 return 0; /* shouldn't happen */
297 * Return the top-level GtkWindow associated with a particular
298 * front end instance.
300 void *get_window(void *frontend)
302 struct gui_data *inst = (struct gui_data *)frontend;
307 * Minimise or restore the window in response to a server-side
310 void set_iconic(void *frontend, int iconic)
313 * GTK 1.2 doesn't know how to do this.
315 #if GTK_CHECK_VERSION(2,0,0)
316 struct gui_data *inst = (struct gui_data *)frontend;
318 gtk_window_iconify(GTK_WINDOW(inst->window));
320 gtk_window_deiconify(GTK_WINDOW(inst->window));
325 * Move the window in response to a server-side request.
327 void move_window(void *frontend, int x, int y)
329 struct gui_data *inst = (struct gui_data *)frontend;
331 * I assume that when the GTK version of this call is available
332 * we should use it. Not sure how it differs from the GDK one,
335 #if GTK_CHECK_VERSION(2,0,0)
336 gtk_window_move(GTK_WINDOW(inst->window), x, y);
338 gdk_window_move(gtk_widget_get_window(inst->window), x, y);
343 * Move the window to the top or bottom of the z-order in response
344 * to a server-side request.
346 void set_zorder(void *frontend, int top)
348 struct gui_data *inst = (struct gui_data *)frontend;
350 gdk_window_raise(gtk_widget_get_window(inst->window));
352 gdk_window_lower(gtk_widget_get_window(inst->window));
356 * Refresh the window in response to a server-side request.
358 void refresh_window(void *frontend)
360 struct gui_data *inst = (struct gui_data *)frontend;
361 term_invalidate(inst->term);
365 * Maximise or restore the window in response to a server-side
368 void set_zoomed(void *frontend, int zoomed)
371 * GTK 1.2 doesn't know how to do this.
373 #if GTK_CHECK_VERSION(2,0,0)
374 struct gui_data *inst = (struct gui_data *)frontend;
376 gtk_window_maximize(GTK_WINDOW(inst->window));
378 gtk_window_unmaximize(GTK_WINDOW(inst->window));
383 * Report whether the window is iconic, for terminal reports.
385 int is_iconic(void *frontend)
387 struct gui_data *inst = (struct gui_data *)frontend;
388 return !gdk_window_is_viewable(gtk_widget_get_window(inst->window));
392 * Report the window's position, for terminal reports.
394 void get_window_pos(void *frontend, int *x, int *y)
396 struct gui_data *inst = (struct gui_data *)frontend;
398 * I assume that when the GTK version of this call is available
399 * we should use it. Not sure how it differs from the GDK one,
402 #if GTK_CHECK_VERSION(2,0,0)
403 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
405 gdk_window_get_position(gtk_widget_get_window(inst->window), x, y);
410 * Report the window's pixel size, for terminal reports.
412 void get_window_pixels(void *frontend, int *x, int *y)
414 struct gui_data *inst = (struct gui_data *)frontend;
416 * I assume that when the GTK version of this call is available
417 * we should use it. Not sure how it differs from the GDK one,
420 #if GTK_CHECK_VERSION(2,0,0)
421 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
423 gdk_window_get_size(gtk_widget_get_window(inst->window), x, y);
428 * Return the window or icon title.
430 char *get_window_title(void *frontend, int icon)
432 struct gui_data *inst = (struct gui_data *)frontend;
433 return icon ? inst->icontitle : inst->wintitle;
436 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
438 struct gui_data *inst = (struct gui_data *)data;
439 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
440 if (!reallyclose(inst))
446 static void update_mouseptr(struct gui_data *inst)
448 switch (inst->busy_status) {
450 if (!inst->mouseptr_visible) {
451 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
453 } else if (send_raw_mouse) {
454 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
457 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
461 case BUSY_WAITING: /* XXX can we do better? */
463 /* We always display these cursors. */
464 gdk_window_set_cursor(gtk_widget_get_window(inst->area),
472 static void show_mouseptr(struct gui_data *inst, int show)
474 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
476 inst->mouseptr_visible = show;
477 update_mouseptr(inst);
480 static void draw_backing_rect(struct gui_data *inst);
482 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
484 struct gui_data *inst = (struct gui_data *)data;
485 int w, h, need_size = 0;
488 * See if the terminal size has changed, in which case we must
489 * let the terminal know.
491 w = (event->width - 2*inst->window_border) / inst->font_width;
492 h = (event->height - 2*inst->window_border) / inst->font_height;
493 if (w != inst->width || h != inst->height) {
496 conf_set_int(inst->conf, CONF_width, inst->width);
497 conf_set_int(inst->conf, CONF_height, inst->height);
501 #ifndef NO_BACKING_PIXMAPS
503 gdk_pixmap_unref(inst->pixmap);
507 inst->pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
508 (w * inst->font_width + 2*inst->window_border),
509 (h * inst->font_height + 2*inst->window_border), -1);
512 draw_backing_rect(inst);
514 if (need_size && inst->term) {
515 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
519 term_invalidate(inst->term);
521 #if GTK_CHECK_VERSION(2,0,0)
522 gtk_im_context_set_client_window(inst->imc, gtk_widget_get_window(widget));
528 #ifdef DRAW_TEXT_CAIRO
529 static void cairo_setup_dctx(struct draw_ctx *dctx)
531 cairo_get_matrix(dctx->uctx.u.cairo.cr,
532 &dctx->uctx.u.cairo.origmatrix);
533 cairo_set_line_width(dctx->uctx.u.cairo.cr, 1.0);
534 cairo_set_line_cap(dctx->uctx.u.cairo.cr, CAIRO_LINE_CAP_SQUARE);
535 cairo_set_line_join(dctx->uctx.u.cairo.cr, CAIRO_LINE_JOIN_MITER);
536 /* This antialiasing setting appears to be ignored for Pango
537 * font rendering but honoured for stroking and filling paths;
538 * I don't quite understand the logic of that, but I won't
539 * complain since it's exactly what I happen to want */
540 cairo_set_antialias(dctx->uctx.u.cairo.cr, CAIRO_ANTIALIAS_NONE);
544 #if GTK_CHECK_VERSION(3,0,0)
545 static gint draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
547 struct gui_data *inst = (struct gui_data *)data;
550 struct draw_ctx adctx, *dctx = &adctx;
551 GdkRectangle dirtyrect;
554 dctx->uctx.type = DRAWTYPE_CAIRO;
555 dctx->uctx.u.cairo.widget = widget;
556 dctx->uctx.u.cairo.cr = cr;
557 cairo_setup_dctx(dctx);
559 gdk_cairo_get_clip_rectangle(cr, &dirtyrect);
562 * As in window.c, we clear the 'immediately' flag in the
563 * term_paint() call if the terminal has an update pending, in
564 * case we're constrained within this event to only draw on
565 * the exposed rectangle of the window. (Because if the whole
566 * of a character cell needs a redraw due to a terminal
567 * contents change, the last thing we want is to give it a
568 * _partial_ redraw here due to system-imposed clipping, and
569 * then have the next main terminal update believe it's been
572 * I don't actually know if GTK draw events will constrain us
573 * in this way, but it's best to be careful...
575 term_paint(inst->term, dctx,
576 (dirtyrect.x - inst->window_border) / inst->font_width,
577 (dirtyrect.y - inst->window_border) / inst->font_height,
578 (dirtyrect.x + dirtyrect.width -
579 inst->window_border) / inst->font_width,
580 (dirtyrect.y + dirtyrect.height -
581 inst->window_border) / inst->font_height,
582 !inst->term->window_update_pending);
588 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
590 struct gui_data *inst = (struct gui_data *)data;
592 #ifndef NO_BACKING_PIXMAPS
594 * Pass the exposed rectangle to terminal.c, which will call us
595 * back to do the actual painting.
598 gdk_draw_pixmap(gtk_widget_get_window(widget),
599 (gtk_widget_get_style(widget)->fg_gc
600 [gtk_widget_get_state(widget)]),
602 event->area.x, event->area.y,
603 event->area.x, event->area.y,
604 event->area.width, event->area.height);
608 Context ctx = get_ctx(inst);
609 term_paint(inst->term, ctx,
610 (event->area.x - inst->window_border) / inst->font_width,
611 (event->area.y - inst->window_border) / inst->font_height,
612 (event->area.x + event->area.width -
613 inst->window_border) / inst->font_width,
614 (event->area.y + event->area.height -
615 inst->window_border) / inst->font_height,
616 !inst->term->window_update_pending);
625 #define KEY_PRESSED(k) \
626 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
628 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
630 struct gui_data *inst = (struct gui_data *)data;
632 wchar_t ucsoutput[2];
633 int ucsval, start, end, special, output_charset, use_ucsoutput;
634 int nethack_mode, app_keypad_mode;
636 /* Remember the timestamp. */
637 inst->input_event_time = event->time;
639 /* By default, nothing is generated. */
641 special = use_ucsoutput = FALSE;
642 output_charset = CS_ISO8859_1;
644 #ifdef KEY_EVENT_DIAGNOSTICS
646 * Condition this mess in if you want to debug keyboard events as
647 * they come in to this function (e.g. because some particular
648 * port of GDK is producing an unexpected arrangement of
651 #define TRY(val, prefix, string) \
652 if ((val) == prefix ## string) printf("%s", #string); else
653 #define GIVE_UP(val) \
654 printf("%d", (int)(val))
655 #define TRY_MASK(val, prefix, string, suffix) \
656 if ((val) & prefix ## string ## suffix) { \
657 (val) &= ~ prefix ## string ## suffix; \
658 printf("%s", #string); \
659 if (val) printf("|"); \
661 #define GIVE_UP_MASK(val) \
664 if ((val)) printf("%d", (int)(val)); \
667 printf("key_event: type=");
668 TRY(event->type, GDK_KEY_, PRESS)
669 TRY(event->type, GDK_KEY_, RELEASE)
670 GIVE_UP(event->type);
672 TRY(event->keyval, GDK_KEY_, Alt_L)
673 TRY(event->keyval, GDK_KEY_, Alt_R)
674 TRY(event->keyval, GDK_KEY_, BackSpace)
675 TRY(event->keyval, GDK_KEY_, Begin)
676 TRY(event->keyval, GDK_KEY_, Break)
677 TRY(event->keyval, GDK_KEY_, Delete)
678 TRY(event->keyval, GDK_KEY_, Down)
679 TRY(event->keyval, GDK_KEY_, End)
680 TRY(event->keyval, GDK_KEY_, Escape)
681 TRY(event->keyval, GDK_KEY_, F10)
682 TRY(event->keyval, GDK_KEY_, F11)
683 TRY(event->keyval, GDK_KEY_, F12)
684 TRY(event->keyval, GDK_KEY_, F13)
685 TRY(event->keyval, GDK_KEY_, F14)
686 TRY(event->keyval, GDK_KEY_, F15)
687 TRY(event->keyval, GDK_KEY_, F16)
688 TRY(event->keyval, GDK_KEY_, F17)
689 TRY(event->keyval, GDK_KEY_, F18)
690 TRY(event->keyval, GDK_KEY_, F19)
691 TRY(event->keyval, GDK_KEY_, F1)
692 TRY(event->keyval, GDK_KEY_, F20)
693 TRY(event->keyval, GDK_KEY_, F2)
694 TRY(event->keyval, GDK_KEY_, F3)
695 TRY(event->keyval, GDK_KEY_, F4)
696 TRY(event->keyval, GDK_KEY_, F5)
697 TRY(event->keyval, GDK_KEY_, F6)
698 TRY(event->keyval, GDK_KEY_, F7)
699 TRY(event->keyval, GDK_KEY_, F8)
700 TRY(event->keyval, GDK_KEY_, F9)
701 TRY(event->keyval, GDK_KEY_, Home)
702 TRY(event->keyval, GDK_KEY_, Insert)
703 TRY(event->keyval, GDK_KEY_, ISO_Left_Tab)
704 TRY(event->keyval, GDK_KEY_, KP_0)
705 TRY(event->keyval, GDK_KEY_, KP_1)
706 TRY(event->keyval, GDK_KEY_, KP_2)
707 TRY(event->keyval, GDK_KEY_, KP_3)
708 TRY(event->keyval, GDK_KEY_, KP_4)
709 TRY(event->keyval, GDK_KEY_, KP_5)
710 TRY(event->keyval, GDK_KEY_, KP_6)
711 TRY(event->keyval, GDK_KEY_, KP_7)
712 TRY(event->keyval, GDK_KEY_, KP_8)
713 TRY(event->keyval, GDK_KEY_, KP_9)
714 TRY(event->keyval, GDK_KEY_, KP_Add)
715 TRY(event->keyval, GDK_KEY_, KP_Begin)
716 TRY(event->keyval, GDK_KEY_, KP_Decimal)
717 TRY(event->keyval, GDK_KEY_, KP_Delete)
718 TRY(event->keyval, GDK_KEY_, KP_Divide)
719 TRY(event->keyval, GDK_KEY_, KP_Down)
720 TRY(event->keyval, GDK_KEY_, KP_End)
721 TRY(event->keyval, GDK_KEY_, KP_Enter)
722 TRY(event->keyval, GDK_KEY_, KP_Home)
723 TRY(event->keyval, GDK_KEY_, KP_Insert)
724 TRY(event->keyval, GDK_KEY_, KP_Left)
725 TRY(event->keyval, GDK_KEY_, KP_Multiply)
726 TRY(event->keyval, GDK_KEY_, KP_Page_Down)
727 TRY(event->keyval, GDK_KEY_, KP_Page_Up)
728 TRY(event->keyval, GDK_KEY_, KP_Right)
729 TRY(event->keyval, GDK_KEY_, KP_Subtract)
730 TRY(event->keyval, GDK_KEY_, KP_Up)
731 TRY(event->keyval, GDK_KEY_, Left)
732 TRY(event->keyval, GDK_KEY_, Meta_L)
733 TRY(event->keyval, GDK_KEY_, Meta_R)
734 TRY(event->keyval, GDK_KEY_, Num_Lock)
735 TRY(event->keyval, GDK_KEY_, Page_Down)
736 TRY(event->keyval, GDK_KEY_, Page_Up)
737 TRY(event->keyval, GDK_KEY_, Return)
738 TRY(event->keyval, GDK_KEY_, Right)
739 TRY(event->keyval, GDK_KEY_, Tab)
740 TRY(event->keyval, GDK_KEY_, Up)
741 TRY(event->keyval, GDK_KEY_, Shift_L)
742 TRY(event->keyval, GDK_KEY_, Shift_R)
743 TRY(event->keyval, GDK_KEY_, Control_L)
744 TRY(event->keyval, GDK_KEY_, Control_R)
745 TRY(event->keyval, GDK_KEY_, Caps_Lock)
746 TRY(event->keyval, GDK_KEY_, Shift_Lock)
747 TRY(event->keyval, GDK_KEY_, Super_L)
748 TRY(event->keyval, GDK_KEY_, Super_R)
749 TRY(event->keyval, GDK_KEY_, Hyper_L)
750 TRY(event->keyval, GDK_KEY_, Hyper_R)
751 GIVE_UP(event->keyval);
754 int val = event->state;
755 TRY_MASK(val, GDK_, SHIFT, _MASK)
756 TRY_MASK(val, GDK_, LOCK, _MASK)
757 TRY_MASK(val, GDK_, CONTROL, _MASK)
758 TRY_MASK(val, GDK_, MOD1, _MASK)
759 TRY_MASK(val, GDK_, MOD2, _MASK)
760 TRY_MASK(val, GDK_, MOD3, _MASK)
761 TRY_MASK(val, GDK_, MOD4, _MASK)
762 TRY_MASK(val, GDK_, MOD5, _MASK)
763 TRY_MASK(val, GDK_, SUPER, _MASK)
764 TRY_MASK(val, GDK_, HYPER, _MASK)
765 TRY_MASK(val, GDK_, META, _MASK)
768 printf(" hardware_keycode=%d is_modifier=%s\n",
769 (int)event->hardware_keycode, event->is_modifier ? "TRUE" : "FALSE");
770 #endif /* KEY_EVENT_DIAGNOSTICS */
773 * If Alt is being released after typing an Alt+numberpad
774 * sequence, we should generate the code that was typed.
776 * Note that we only do this if more than one key was actually
777 * pressed - I don't think Alt+NumPad4 should be ^D or that
778 * Alt+NumPad3 should be ^C, for example. There's no serious
779 * inconvenience in having to type a zero before a single-digit
782 if (event->type == GDK_KEY_RELEASE) {
783 if ((event->keyval == GDK_KEY_Meta_L ||
784 event->keyval == GDK_KEY_Meta_R ||
785 event->keyval == GDK_KEY_Alt_L ||
786 event->keyval == GDK_KEY_Alt_R) &&
787 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
789 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
792 * FIXME: we might usefully try to do something clever here
793 * about interpreting the generated key code in a way that's
794 * appropriate to the line code page.
796 output[0] = inst->alt_keycode;
800 #if GTK_CHECK_VERSION(2,0,0)
801 if (gtk_im_context_filter_keypress(inst->imc, event))
806 if (event->type == GDK_KEY_PRESS) {
810 printf("keypress: keyval = %04x, state = %08x; string =",
811 event->keyval, event->state);
812 for (i = 0; event->string[i]; i++)
813 printf(" %02x", (unsigned char) event->string[i]);
819 * NYI: Compose key (!!! requires Unicode faff before even trying)
823 * If Alt has just been pressed, we start potentially
824 * accumulating an Alt+numberpad code. We do this by
825 * setting alt_keycode to -1 (nothing yet but plausible).
827 if ((event->keyval == GDK_KEY_Meta_L ||
828 event->keyval == GDK_KEY_Meta_R ||
829 event->keyval == GDK_KEY_Alt_L ||
830 event->keyval == GDK_KEY_Alt_R)) {
831 inst->alt_keycode = -1;
832 inst->alt_digits = 0;
833 goto done; /* this generates nothing else */
837 * If we're seeing a numberpad key press with Mod1 down,
838 * consider adding it to alt_keycode if that's sensible.
839 * Anything _else_ with Mod1 down cancels any possibility
840 * of an ALT keycode: we set alt_keycode to -2.
842 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
844 switch (event->keyval) {
845 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: digit = 0; break;
846 case GDK_KEY_KP_1: case GDK_KEY_KP_End: digit = 1; break;
847 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: digit = 2; break;
848 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: digit = 3; break;
849 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: digit = 4; break;
850 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: digit = 5; break;
851 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: digit = 6; break;
852 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: digit = 7; break;
853 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: digit = 8; break;
854 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: digit = 9; break;
857 inst->alt_keycode = -2; /* it's invalid */
860 printf("Adding digit %d to keycode %d", digit,
863 if (inst->alt_keycode == -1)
864 inst->alt_keycode = digit; /* one-digit code */
866 inst->alt_keycode = inst->alt_keycode * 10 + digit;
869 printf(" gives new code %d\n", inst->alt_keycode);
871 /* Having used this digit, we now do nothing more with it. */
877 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
880 if (event->keyval == GDK_KEY_Page_Up &&
881 (event->state & GDK_SHIFT_MASK)) {
882 term_scroll(inst->term, 0, -inst->height/2);
885 if (event->keyval == GDK_KEY_Page_Up &&
886 (event->state & GDK_CONTROL_MASK)) {
887 term_scroll(inst->term, 0, -1);
890 if (event->keyval == GDK_KEY_Page_Down &&
891 (event->state & GDK_SHIFT_MASK)) {
892 term_scroll(inst->term, 0, +inst->height/2);
895 if (event->keyval == GDK_KEY_Page_Down &&
896 (event->state & GDK_CONTROL_MASK)) {
897 term_scroll(inst->term, 0, +1);
902 * Neither does Shift-Ins.
904 if (event->keyval == GDK_KEY_Insert &&
905 (event->state & GDK_SHIFT_MASK)) {
911 use_ucsoutput = FALSE;
913 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
914 app_keypad_mode = (inst->term->app_keypad_keys &&
915 !conf_get_int(inst->conf, CONF_no_applic_k));
917 /* ALT+things gives leading Escape. */
919 #if !GTK_CHECK_VERSION(2,0,0)
921 * In vanilla X, and hence also GDK 1.2, the string received
922 * as part of a keyboard event is assumed to be in
923 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
924 * but it's true: see the man page for XLookupString(3) for
927 output_charset = CS_ISO8859_1;
928 strncpy(output+1, event->string, lenof(output)-1);
931 * Most things can now be passed to
932 * gtk_im_context_filter_keypress without breaking anything
933 * below this point. An exception is the numeric keypad if
934 * we're in Nethack or application mode: the IM will eat
935 * numeric keypad presses if Num Lock is on, but we don't want
938 if (app_keypad_mode &&
939 (event->keyval == GDK_KEY_Num_Lock ||
940 event->keyval == GDK_KEY_KP_Divide ||
941 event->keyval == GDK_KEY_KP_Multiply ||
942 event->keyval == GDK_KEY_KP_Subtract ||
943 event->keyval == GDK_KEY_KP_Add ||
944 event->keyval == GDK_KEY_KP_Enter ||
945 event->keyval == GDK_KEY_KP_0 ||
946 event->keyval == GDK_KEY_KP_Insert ||
947 event->keyval == GDK_KEY_KP_1 ||
948 event->keyval == GDK_KEY_KP_End ||
949 event->keyval == GDK_KEY_KP_2 ||
950 event->keyval == GDK_KEY_KP_Down ||
951 event->keyval == GDK_KEY_KP_3 ||
952 event->keyval == GDK_KEY_KP_Page_Down ||
953 event->keyval == GDK_KEY_KP_4 ||
954 event->keyval == GDK_KEY_KP_Left ||
955 event->keyval == GDK_KEY_KP_5 ||
956 event->keyval == GDK_KEY_KP_Begin ||
957 event->keyval == GDK_KEY_KP_6 ||
958 event->keyval == GDK_KEY_KP_Right ||
959 event->keyval == GDK_KEY_KP_7 ||
960 event->keyval == GDK_KEY_KP_Home ||
961 event->keyval == GDK_KEY_KP_8 ||
962 event->keyval == GDK_KEY_KP_Up ||
963 event->keyval == GDK_KEY_KP_9 ||
964 event->keyval == GDK_KEY_KP_Page_Up ||
965 event->keyval == GDK_KEY_KP_Decimal ||
966 event->keyval == GDK_KEY_KP_Delete)) {
967 /* app keypad; do nothing */
968 } else if (nethack_mode &&
969 (event->keyval == GDK_KEY_KP_1 ||
970 event->keyval == GDK_KEY_KP_End ||
971 event->keyval == GDK_KEY_KP_2 ||
972 event->keyval == GDK_KEY_KP_Down ||
973 event->keyval == GDK_KEY_KP_3 ||
974 event->keyval == GDK_KEY_KP_Page_Down ||
975 event->keyval == GDK_KEY_KP_4 ||
976 event->keyval == GDK_KEY_KP_Left ||
977 event->keyval == GDK_KEY_KP_5 ||
978 event->keyval == GDK_KEY_KP_Begin ||
979 event->keyval == GDK_KEY_KP_6 ||
980 event->keyval == GDK_KEY_KP_Right ||
981 event->keyval == GDK_KEY_KP_7 ||
982 event->keyval == GDK_KEY_KP_Home ||
983 event->keyval == GDK_KEY_KP_8 ||
984 event->keyval == GDK_KEY_KP_Up ||
985 event->keyval == GDK_KEY_KP_9 ||
986 event->keyval == GDK_KEY_KP_Page_Up)) {
987 /* nethack mode; do nothing */
989 if (gtk_im_context_filter_keypress(inst->imc, event))
994 * GDK 2.0 arranges to have done some translation for us: in
995 * GDK 2.0, event->string is encoded in the current locale.
997 * So we use the standard C library function mbstowcs() to
998 * convert from the current locale into Unicode; from there
999 * we can convert to whatever PuTTY is currently working in.
1000 * (In fact I convert straight back to UTF-8 from
1001 * wide-character Unicode, for the sake of simplicity: that
1002 * way we can still use exactly the same code to manipulate
1003 * the string, such as prefixing ESC.)
1005 output_charset = CS_UTF8;
1007 wchar_t widedata[32];
1012 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
1013 event->string, strlen(event->string),
1014 widedata, lenof(widedata)-1);
1017 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
1018 CS_UTF8, NULL, NULL, 0);
1019 output[1+ulen] = '\0';
1024 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
1025 ucsoutput[0] = '\033';
1026 ucsoutput[1] = ucsval;
1027 use_ucsoutput = TRUE;
1030 output[lenof(output)-1] = '\0';
1031 end = strlen(output);
1033 if (event->state & GDK_MOD1_MASK) {
1035 if (end == 1) end = 0;
1039 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
1040 if (!output[1] && event->keyval == '`' &&
1041 (event->state & GDK_CONTROL_MASK)) {
1043 use_ucsoutput = FALSE;
1047 /* Some GTK backends (e.g. Quartz) do not change event->string
1048 * in response to the Control modifier. So we do it ourselves
1049 * here, if it's not already happened.
1051 * The translations below are in line with X11 policy as far
1053 if ((event->state & GDK_CONTROL_MASK) && end == 2) {
1054 if (output[1] >= '3' && output[1] <= '7') {
1055 /* ^3,...,^7 map to 0x1B,...,0x1F */
1056 output[1] += '\x1B' - '3';
1057 } else if (output[1] == '2') {
1058 /* ^2 is ^@, i.e. \0 */
1060 } else if (output[1] == '8') {
1063 } else if (output[1] == '/') {
1064 /* ^/ is the same as ^_ */
1066 } else if (output[1] >= 0x40 && output[1] < 0x7F) {
1067 /* Everything anywhere near the alphabetics just gets
1071 /* Anything else, e.g. '0', is unchanged. */
1074 /* Control-Break sends a Break special to the backend */
1075 if (event->keyval == GDK_KEY_Break &&
1076 (event->state & GDK_CONTROL_MASK)) {
1078 inst->back->special(inst->backhandle, TS_BRK);
1082 /* We handle Return ourselves, because it needs to be flagged as
1083 * special to ldisc. */
1084 if (event->keyval == GDK_KEY_Return) {
1086 use_ucsoutput = FALSE;
1091 /* Control-2, Control-Space and Control-@ are NUL */
1093 (event->keyval == ' ' || event->keyval == '2' ||
1094 event->keyval == '@') &&
1095 (event->state & (GDK_SHIFT_MASK |
1096 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
1098 use_ucsoutput = FALSE;
1102 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
1103 if (!output[1] && event->keyval == ' ' &&
1104 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
1105 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
1107 output_charset = CS_ISO8859_1;
1108 use_ucsoutput = FALSE;
1112 /* We don't let GTK tell us what Backspace is! We know better. */
1113 if (event->keyval == GDK_KEY_BackSpace &&
1114 !(event->state & GDK_SHIFT_MASK)) {
1115 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1117 use_ucsoutput = FALSE;
1121 /* For Shift Backspace, do opposite of what is configured. */
1122 if (event->keyval == GDK_KEY_BackSpace &&
1123 (event->state & GDK_SHIFT_MASK)) {
1124 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
1126 use_ucsoutput = FALSE;
1131 /* Shift-Tab is ESC [ Z */
1132 if (event->keyval == GDK_KEY_ISO_Left_Tab ||
1133 (event->keyval == GDK_KEY_Tab &&
1134 (event->state & GDK_SHIFT_MASK))) {
1135 end = 1 + sprintf(output+1, "\033[Z");
1136 use_ucsoutput = FALSE;
1138 /* And normal Tab is Tab, if the keymap hasn't already told us.
1139 * (Curiously, at least one version of the MacOS 10.5 X server
1140 * doesn't translate Tab for us. */
1141 if (event->keyval == GDK_KEY_Tab && end <= 1) {
1147 * NetHack keypad mode.
1150 const char *keys = NULL;
1151 switch (event->keyval) {
1152 case GDK_KEY_KP_1: case GDK_KEY_KP_End:
1153 keys = "bB\002"; break;
1154 case GDK_KEY_KP_2: case GDK_KEY_KP_Down:
1155 keys = "jJ\012"; break;
1156 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down:
1157 keys = "nN\016"; break;
1158 case GDK_KEY_KP_4: case GDK_KEY_KP_Left:
1159 keys = "hH\010"; break;
1160 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin:
1161 keys = "..."; break;
1162 case GDK_KEY_KP_6: case GDK_KEY_KP_Right:
1163 keys = "lL\014"; break;
1164 case GDK_KEY_KP_7: case GDK_KEY_KP_Home:
1165 keys = "yY\031"; break;
1166 case GDK_KEY_KP_8: case GDK_KEY_KP_Up:
1167 keys = "kK\013"; break;
1168 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up:
1169 keys = "uU\025"; break;
1173 if (event->state & GDK_CONTROL_MASK)
1174 output[1] = keys[2];
1175 else if (event->state & GDK_SHIFT_MASK)
1176 output[1] = keys[1];
1178 output[1] = keys[0];
1179 use_ucsoutput = FALSE;
1185 * Application keypad mode.
1187 if (app_keypad_mode) {
1189 switch (event->keyval) {
1190 case GDK_KEY_Num_Lock: xkey = 'P'; break;
1191 case GDK_KEY_KP_Divide: xkey = 'Q'; break;
1192 case GDK_KEY_KP_Multiply: xkey = 'R'; break;
1193 case GDK_KEY_KP_Subtract: xkey = 'S'; break;
1195 * Keypad + is tricky. It covers a space that would
1196 * be taken up on the VT100 by _two_ keys; so we
1197 * let Shift select between the two. Worse still,
1198 * in xterm function key mode we change which two...
1200 case GDK_KEY_KP_Add:
1201 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
1202 if (event->state & GDK_SHIFT_MASK)
1206 } else if (event->state & GDK_SHIFT_MASK)
1211 case GDK_KEY_KP_Enter: xkey = 'M'; break;
1212 case GDK_KEY_KP_0: case GDK_KEY_KP_Insert: xkey = 'p'; break;
1213 case GDK_KEY_KP_1: case GDK_KEY_KP_End: xkey = 'q'; break;
1214 case GDK_KEY_KP_2: case GDK_KEY_KP_Down: xkey = 'r'; break;
1215 case GDK_KEY_KP_3: case GDK_KEY_KP_Page_Down: xkey = 's'; break;
1216 case GDK_KEY_KP_4: case GDK_KEY_KP_Left: xkey = 't'; break;
1217 case GDK_KEY_KP_5: case GDK_KEY_KP_Begin: xkey = 'u'; break;
1218 case GDK_KEY_KP_6: case GDK_KEY_KP_Right: xkey = 'v'; break;
1219 case GDK_KEY_KP_7: case GDK_KEY_KP_Home: xkey = 'w'; break;
1220 case GDK_KEY_KP_8: case GDK_KEY_KP_Up: xkey = 'x'; break;
1221 case GDK_KEY_KP_9: case GDK_KEY_KP_Page_Up: xkey = 'y'; break;
1222 case GDK_KEY_KP_Decimal: case GDK_KEY_KP_Delete:
1226 if (inst->term->vt52_mode) {
1227 if (xkey >= 'P' && xkey <= 'S')
1228 end = 1 + sprintf(output+1, "\033%c", xkey);
1230 end = 1 + sprintf(output+1, "\033?%c", xkey);
1232 end = 1 + sprintf(output+1, "\033O%c", xkey);
1233 use_ucsoutput = FALSE;
1239 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
1240 * for integer decimal nn.)
1242 * We also deal with the weird ones here. Linux VCs replace F1
1243 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
1244 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
1249 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
1250 switch (event->keyval) {
1252 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
1255 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
1258 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
1261 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
1264 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
1267 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
1270 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1273 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1276 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1279 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1312 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1313 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1316 case GDK_KEY_Insert: case GDK_KEY_KP_Insert:
1319 case GDK_KEY_Delete: case GDK_KEY_KP_Delete:
1322 case GDK_KEY_End: case GDK_KEY_KP_End:
1325 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1328 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1332 /* Reorder edit keys to physical order */
1333 if (funky_type == FUNKY_VT400 && code <= 6)
1334 code = "\0\2\1\4\5\3\6"[code];
1336 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1337 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1338 use_ucsoutput = FALSE;
1342 if (funky_type == FUNKY_SCO && /* SCO function keys */
1343 code >= 11 && code <= 34) {
1344 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1346 switch (event->keyval) {
1347 case GDK_KEY_F1: index = 0; break;
1348 case GDK_KEY_F2: index = 1; break;
1349 case GDK_KEY_F3: index = 2; break;
1350 case GDK_KEY_F4: index = 3; break;
1351 case GDK_KEY_F5: index = 4; break;
1352 case GDK_KEY_F6: index = 5; break;
1353 case GDK_KEY_F7: index = 6; break;
1354 case GDK_KEY_F8: index = 7; break;
1355 case GDK_KEY_F9: index = 8; break;
1356 case GDK_KEY_F10: index = 9; break;
1357 case GDK_KEY_F11: index = 10; break;
1358 case GDK_KEY_F12: index = 11; break;
1360 if (event->state & GDK_SHIFT_MASK) index += 12;
1361 if (event->state & GDK_CONTROL_MASK) index += 24;
1362 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1363 use_ucsoutput = FALSE;
1366 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1367 code >= 1 && code <= 6) {
1368 char codes[] = "HL.FIG";
1373 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1375 use_ucsoutput = FALSE;
1378 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1379 code >= 11 && code <= 24) {
1385 if (inst->term->vt52_mode)
1386 end = 1 + sprintf(output+1,
1387 "\x1B%c", code + 'P' - 11 - offt);
1389 end = 1 + sprintf(output+1,
1390 "\x1BO%c", code + 'P' - 11 - offt);
1391 use_ucsoutput = FALSE;
1394 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1395 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1396 use_ucsoutput = FALSE;
1399 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1400 if (inst->term->vt52_mode)
1401 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1403 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1404 use_ucsoutput = FALSE;
1407 if ((code == 1 || code == 4) &&
1408 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1409 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1410 use_ucsoutput = FALSE;
1414 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1415 use_ucsoutput = FALSE;
1421 * Cursor keys. (This includes the numberpad cursor keys,
1422 * if we haven't already done them due to app keypad mode.)
1424 * Here we also process un-numlocked un-appkeypadded KP5,
1425 * which sends ESC [ G.
1429 switch (event->keyval) {
1430 case GDK_KEY_Up: case GDK_KEY_KP_Up: xkey = 'A'; break;
1431 case GDK_KEY_Down: case GDK_KEY_KP_Down: xkey = 'B'; break;
1432 case GDK_KEY_Right: case GDK_KEY_KP_Right: xkey = 'C'; break;
1433 case GDK_KEY_Left: case GDK_KEY_KP_Left: xkey = 'D'; break;
1434 case GDK_KEY_Begin: case GDK_KEY_KP_Begin: xkey = 'G'; break;
1437 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1438 event->state & GDK_CONTROL_MASK);
1439 use_ucsoutput = FALSE;
1448 if (end-start > 0) {
1449 #ifdef KEY_DEBUGGING
1451 printf("generating sequence:");
1452 for (i = start; i < end; i++)
1453 printf(" %02x", (unsigned char) output[i]);
1459 * For special control characters, the character set
1460 * should never matter.
1462 output[end] = '\0'; /* NUL-terminate */
1464 ldisc_send(inst->ldisc, output+start, -2, 1);
1465 } else if (!inst->direct_to_font) {
1466 if (!use_ucsoutput) {
1468 lpage_send(inst->ldisc, output_charset, output+start,
1472 * We generated our own Unicode key data from the
1473 * keysym, so use that instead.
1476 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1480 * In direct-to-font mode, we just send the string
1481 * exactly as we received it.
1484 ldisc_send(inst->ldisc, output+start, end-start, 1);
1487 show_mouseptr(inst, 0);
1488 term_seen_key_event(inst->term);
1494 #if GTK_CHECK_VERSION(2,0,0)
1495 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1497 struct gui_data *inst = (struct gui_data *)data;
1499 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1500 show_mouseptr(inst, 0);
1501 term_seen_key_event(inst->term);
1505 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1506 GdkEventType type, guint ebutton, guint state,
1507 gdouble ex, gdouble ey)
1509 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1511 /* Remember the timestamp. */
1512 inst->input_event_time = timestamp;
1514 show_mouseptr(inst, 1);
1516 shift = state & GDK_SHIFT_MASK;
1517 ctrl = state & GDK_CONTROL_MASK;
1518 alt = state & GDK_MOD1_MASK;
1521 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1522 CONF_mouse_override));
1524 if (!raw_mouse_mode) {
1525 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1526 term_scroll(inst->term, 0, -5);
1529 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1530 term_scroll(inst->term, 0, +5);
1535 if (ebutton == 3 && ctrl) {
1536 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1537 ebutton, timestamp);
1543 else if (ebutton == 2)
1544 button = MBT_MIDDLE;
1545 else if (ebutton == 3)
1547 else if (ebutton == 4)
1548 button = MBT_WHEEL_UP;
1549 else if (ebutton == 5)
1550 button = MBT_WHEEL_DOWN;
1552 return FALSE; /* don't even know what button! */
1555 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1556 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1557 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1558 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1559 default: return FALSE; /* don't know this event type */
1562 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1563 return TRUE; /* we ignore these in raw mouse mode */
1565 x = (ex - inst->window_border) / inst->font_width;
1566 y = (ey - inst->window_border) / inst->font_height;
1568 term_mouse(inst->term, button, translate_button(button), act,
1569 x, y, shift, ctrl, alt);
1574 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1576 struct gui_data *inst = (struct gui_data *)data;
1577 return button_internal(inst, event->time, event->type, event->button,
1578 event->state, event->x, event->y);
1581 #if GTK_CHECK_VERSION(2,0,0)
1583 * In GTK 2, mouse wheel events have become a new type of event.
1584 * This handler translates them back into button-4 and button-5
1585 * presses so that I don't have to change my old code too much :-)
1587 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1589 struct gui_data *inst = (struct gui_data *)data;
1592 if (event->direction == GDK_SCROLL_UP)
1594 else if (event->direction == GDK_SCROLL_DOWN)
1599 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1600 button, event->state, event->x, event->y);
1604 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1606 struct gui_data *inst = (struct gui_data *)data;
1607 int shift, ctrl, alt, x, y, button;
1609 /* Remember the timestamp. */
1610 inst->input_event_time = event->time;
1612 show_mouseptr(inst, 1);
1614 shift = event->state & GDK_SHIFT_MASK;
1615 ctrl = event->state & GDK_CONTROL_MASK;
1616 alt = event->state & GDK_MOD1_MASK;
1617 if (event->state & GDK_BUTTON1_MASK)
1619 else if (event->state & GDK_BUTTON2_MASK)
1620 button = MBT_MIDDLE;
1621 else if (event->state & GDK_BUTTON3_MASK)
1624 return FALSE; /* don't even know what button! */
1626 x = (event->x - inst->window_border) / inst->font_width;
1627 y = (event->y - inst->window_border) / inst->font_height;
1629 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1630 x, y, shift, ctrl, alt);
1635 void frontend_keypress(void *handle)
1637 struct gui_data *inst = (struct gui_data *)handle;
1640 * If our child process has exited but not closed, terminate on
1647 static void exit_callback(void *vinst)
1649 struct gui_data *inst = (struct gui_data *)vinst;
1650 int exitcode, close_on_exit;
1652 if (!inst->exited &&
1653 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1654 inst->exited = TRUE;
1655 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1656 if (close_on_exit == FORCE_ON ||
1657 (close_on_exit == AUTO && exitcode == 0))
1658 gtk_main_quit(); /* just go */
1660 ldisc_free(inst->ldisc);
1663 inst->back->free(inst->backhandle);
1664 inst->backhandle = NULL;
1666 term_provide_resize_fn(inst->term, NULL, NULL);
1667 update_specials_menu(inst);
1668 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1673 * Replacement code for the gtk_quit_add() function, which GTK2 - in
1674 * their unbounded wisdom - deprecated without providing any usable
1675 * replacement, and which we were using to ensure that our idle
1676 * function for toplevel callbacks was only run from the outermost
1679 * We maintain a global variable with a list of 'struct gui_data'
1680 * instances on which we should call inst_post_main() when a
1681 * subsidiary gtk_main() terminates; then we must make sure that all
1682 * our subsidiary calls to gtk_main() are followed by a call to
1685 * This is kind of overkill in the sense that at the time of writing
1686 * we don't actually ever run more than one 'struct gui_data' instance
1687 * in the same process, but we're _so nearly_ prepared to do that that
1688 * I want to remain futureproof against the possibility of doing so in
1691 struct post_main_context {
1692 struct post_main_context *next;
1693 struct gui_data *inst;
1695 struct post_main_context *post_main_list_head = NULL;
1696 static void request_post_main(struct gui_data *inst)
1698 struct post_main_context *node = snew(struct post_main_context);
1699 node->next = post_main_list_head;
1701 post_main_list_head = node;
1703 static void inst_post_main(struct gui_data *inst);
1704 void post_main(void)
1706 while (post_main_list_head) {
1707 struct post_main_context *node = post_main_list_head;
1708 post_main_list_head = node->next;
1709 inst_post_main(node->inst);
1714 void notify_remote_exit(void *frontend)
1716 struct gui_data *inst = (struct gui_data *)frontend;
1718 queue_toplevel_callback(exit_callback, inst);
1721 static void notify_toplevel_callback(void *frontend);
1723 static void inst_post_main(struct gui_data *inst)
1725 if (gtk_main_level() == 1) {
1726 notify_toplevel_callback(inst);
1727 inst->quit_fn_scheduled = FALSE;
1729 /* Apparently we're _still_ more than one level deep in
1730 * gtk_main() instances, so we'll need another callback for
1731 * when we get out of the next one. */
1732 request_post_main(inst);
1736 static gint idle_toplevel_callback_func(gpointer data)
1738 struct gui_data *inst = (struct gui_data *)data;
1740 if (gtk_main_level() > 1) {
1742 * We don't run the callbacks if we're in the middle of a
1743 * subsidiary gtk_main. Instead, ask for a callback when we
1744 * get back out of the subsidiary main loop (if we haven't
1745 * already arranged one), so we can reschedule ourself then.
1747 if (!inst->quit_fn_scheduled) {
1748 request_post_main(inst);
1749 inst->quit_fn_scheduled = TRUE;
1752 * And unschedule this idle function, since we've now done
1753 * everything we can until the innermost gtk_main has quit and
1754 * can reschedule us with a chance of actually taking action.
1756 if (inst->idle_fn_scheduled) { /* double-check, just in case */
1757 g_source_remove(inst->toplevel_callback_idle_id);
1758 inst->idle_fn_scheduled = FALSE;
1761 run_toplevel_callbacks();
1765 * If we've emptied our toplevel callback queue, unschedule
1766 * ourself. Otherwise, leave ourselves pending so we'll be called
1767 * again to deal with more callbacks after another round of the
1770 if (!toplevel_callback_pending() && inst->idle_fn_scheduled) {
1771 g_source_remove(inst->toplevel_callback_idle_id);
1772 inst->idle_fn_scheduled = FALSE;
1778 static void notify_toplevel_callback(void *frontend)
1780 struct gui_data *inst = (struct gui_data *)frontend;
1782 if (!inst->idle_fn_scheduled) {
1783 inst->toplevel_callback_idle_id =
1784 g_idle_add(idle_toplevel_callback_func, inst);
1785 inst->idle_fn_scheduled = TRUE;
1789 static gint timer_trigger(gpointer data)
1791 unsigned long now = GPOINTER_TO_LONG(data);
1792 unsigned long next, then;
1796 * Destroy the timer we got here on.
1799 g_source_remove(timer_id);
1804 * run_timers() may cause a call to timer_change_notify, in which
1805 * case a new timer will already have been set up and left in
1806 * timer_id. If it hasn't, and run_timers reports that some timing
1807 * still needs to be done, we do it ourselves.
1809 if (run_timers(now, &next) && !timer_id) {
1811 now = GETTICKCOUNT();
1812 if (now - then > next - then)
1816 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1820 * Returning FALSE means 'don't call this timer again', which
1821 * _should_ be redundant given that we removed it above, but just
1822 * in case, return FALSE anyway.
1827 void timer_change_notify(unsigned long next)
1832 g_source_remove(timer_id);
1834 ticks = next - GETTICKCOUNT();
1836 ticks = 1; /* just in case */
1838 timer_id = g_timeout_add(ticks, timer_trigger, LONG_TO_GPOINTER(next));
1841 #if GTK_CHECK_VERSION(2,0,0)
1842 gboolean fd_input_func(GIOChannel *source, GIOCondition condition,
1845 int sourcefd = g_io_channel_unix_get_fd(source);
1847 * We must process exceptional notifications before ordinary
1848 * readability ones, or we may go straight past the urgent
1851 if (condition & G_IO_PRI)
1852 select_result(sourcefd, 4);
1853 if (condition & G_IO_IN)
1854 select_result(sourcefd, 1);
1855 if (condition & G_IO_OUT)
1856 select_result(sourcefd, 2);
1861 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1863 if (condition & GDK_INPUT_EXCEPTION)
1864 select_result(sourcefd, 4);
1865 if (condition & GDK_INPUT_READ)
1866 select_result(sourcefd, 1);
1867 if (condition & GDK_INPUT_WRITE)
1868 select_result(sourcefd, 2);
1872 void destroy(GtkWidget *widget, gpointer data)
1877 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1879 struct gui_data *inst = (struct gui_data *)data;
1880 term_set_focus(inst->term, event->in);
1881 term_update(inst->term);
1882 show_mouseptr(inst, 1);
1886 void set_busy_status(void *frontend, int status)
1888 struct gui_data *inst = (struct gui_data *)frontend;
1889 inst->busy_status = status;
1890 update_mouseptr(inst);
1894 * set or clear the "raw mouse message" mode
1896 void set_raw_mouse_mode(void *frontend, int activate)
1898 struct gui_data *inst = (struct gui_data *)frontend;
1899 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1900 send_raw_mouse = activate;
1901 update_mouseptr(inst);
1904 void request_resize(void *frontend, int w, int h)
1906 struct gui_data *inst = (struct gui_data *)frontend;
1908 #if !GTK_CHECK_VERSION(3,0,0)
1910 int large_x, large_y;
1911 int offset_x, offset_y;
1913 GtkRequisition inner, outer;
1916 * This is a heinous hack dreamed up by the gnome-terminal
1917 * people to get around a limitation in gtk. The problem is
1918 * that in order to set the size correctly we really need to be
1919 * calling gtk_window_resize - but that needs to know the size
1920 * of the _whole window_, not the drawing area. So what we do
1921 * is to set an artificially huge size request on the drawing
1922 * area, recompute the resulting size request on the window,
1923 * and look at the difference between the two. That gives us
1924 * the x and y offsets we need to translate drawing area size
1925 * into window size for real, and then we call
1926 * gtk_window_resize.
1930 * We start by retrieving the current size of the whole window.
1931 * Adding a bit to _that_ will give us a value we can use as a
1932 * bogus size request which guarantees to be bigger than the
1933 * current size of the drawing area.
1935 get_window_pixels(inst, &large_x, &large_y);
1939 gtk_widget_set_size_request(inst->area, large_x, large_y);
1940 gtk_widget_size_request(inst->area, &inner);
1941 gtk_widget_size_request(inst->window, &outer);
1943 offset_x = outer.width - inner.width;
1944 offset_y = outer.height - inner.height;
1946 area_x = inst->font_width * w + 2*inst->window_border;
1947 area_y = inst->font_height * h + 2*inst->window_border;
1950 * Now we must set the size request on the drawing area back to
1951 * something sensible before we commit the real resize. Best
1952 * way to do this, I think, is to set it to what the size is
1953 * really going to end up being.
1955 gtk_widget_set_size_request(inst->area, area_x, area_y);
1956 #if GTK_CHECK_VERSION(2,0,0)
1957 gtk_window_resize(GTK_WINDOW(inst->window),
1958 area_x + offset_x, area_y + offset_y);
1960 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1962 * I can no longer remember what this call to
1963 * gtk_container_dequeue_resize_handler is for. It was
1964 * introduced in r3092 with no comment, and the commit log
1965 * message was uninformative. I'm _guessing_ its purpose is to
1966 * prevent gratuitous resize processing on the window given
1967 * that we're about to resize it anyway, but I have no idea
1968 * why that's so incredibly vital.
1970 * I've tried removing the call, and nothing seems to go
1971 * wrong. I've backtracked to r3092 and tried removing the
1972 * call there, and still nothing goes wrong. So I'm going to
1973 * adopt the working hypothesis that it's superfluous; I won't
1974 * actually remove it from the GTK 1.2 code, but I won't
1975 * attempt to replicate its functionality in the GTK 2 code
1978 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1979 gdk_window_resize(gtk_widget_get_window(inst->window),
1980 area_x + offset_x, area_y + offset_y);
1983 #else /* GTK_CHECK_VERSION(3,0,0) */
1986 * In GTK3, we can do this by using gtk_window_resize_to_geometry,
1987 * which uses the fact that we've already set up the main window's
1988 * WM hints to reflect the terminal drawing area's resize
1989 * increment (i.e. character cell) and the fixed amount of stuff
1992 gtk_window_resize_to_geometry(GTK_WINDOW(inst->window), w, h);
1998 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
2000 inst->cols[n].red = r * 0x0101;
2001 inst->cols[n].green = g * 0x0101;
2002 inst->cols[n].blue = b * 0x0101;
2004 #if !GTK_CHECK_VERSION(3,0,0)
2006 gboolean success[1];
2007 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
2008 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
2009 FALSE, TRUE, success);
2011 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2012 appname, n, r, g, b);
2017 void set_gdk_window_background(GdkWindow *win, const GdkColor *col)
2019 #if GTK_CHECK_VERSION(3,0,0)
2020 /* gdk_window_set_background is deprecated; work around its
2023 rgba.red = col->red / 65535.0;
2024 rgba.green = col->green / 65535.0;
2025 rgba.blue = col->blue / 65535.0;
2027 gdk_window_set_background_rgba(win, &rgba);
2030 /* For GTK1, which doesn't have a 'const' on
2031 * gdk_window_set_background's second parameter type. */
2032 GdkColor col_mutable = *col;
2033 gdk_window_set_background(win, &col_mutable);
2038 void set_window_background(struct gui_data *inst)
2040 if (inst->area && gtk_widget_get_window(inst->area))
2041 set_gdk_window_background(gtk_widget_get_window(inst->area),
2043 if (inst->window && gtk_widget_get_window(inst->window))
2044 set_gdk_window_background(gtk_widget_get_window(inst->window),
2048 void palette_set(void *frontend, int n, int r, int g, int b)
2050 struct gui_data *inst = (struct gui_data *)frontend;
2053 if (n >= NALLCOLOURS)
2055 real_palette_set(inst, n, r, g, b);
2057 /* Default Background changed. Ensure space between text area and
2058 * window border is redrawn */
2059 set_window_background(inst);
2060 draw_backing_rect(inst);
2061 gtk_widget_queue_draw(inst->area);
2065 void palette_reset(void *frontend)
2067 struct gui_data *inst = (struct gui_data *)frontend;
2068 /* This maps colour indices in inst->conf to those used in inst->cols. */
2069 static const int ww[] = {
2070 256, 257, 258, 259, 260, 261,
2071 0, 8, 1, 9, 2, 10, 3, 11,
2072 4, 12, 5, 13, 6, 14, 7, 15
2076 assert(lenof(ww) == NCFGCOLOURS);
2078 #if !GTK_CHECK_VERSION(3,0,0)
2079 if (!inst->colmap) {
2080 inst->colmap = gdk_colormap_get_system();
2082 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
2086 for (i = 0; i < NCFGCOLOURS; i++) {
2087 inst->cols[ww[i]].red =
2088 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
2089 inst->cols[ww[i]].green =
2090 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
2091 inst->cols[ww[i]].blue =
2092 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
2095 for (i = 0; i < NEXTCOLOURS; i++) {
2097 int r = i / 36, g = (i / 6) % 6, b = i % 6;
2098 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
2099 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
2100 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
2102 int shade = i - 216;
2103 shade = shade * 0x0a0a + 0x0808;
2104 inst->cols[i+16].red = inst->cols[i+16].green =
2105 inst->cols[i+16].blue = shade;
2109 #if !GTK_CHECK_VERSION(3,0,0)
2111 gboolean success[NALLCOLOURS];
2112 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
2113 FALSE, TRUE, success);
2114 for (i = 0; i < NALLCOLOURS; i++) {
2116 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
2118 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
2119 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
2120 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
2125 /* Since Default Background may have changed, ensure that space
2126 * between text area and window border is refreshed. */
2127 set_window_background(inst);
2128 if (inst->area && gtk_widget_get_window(inst->area)) {
2129 draw_backing_rect(inst);
2130 gtk_widget_queue_draw(inst->area);
2134 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
2135 * do this before we start using cut buffers.
2137 void init_cutbuffers()
2139 #ifndef NOT_X_WINDOWS
2140 unsigned char empty[] = "";
2141 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2142 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2143 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
2144 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2145 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
2146 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2147 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
2148 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2149 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
2150 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2151 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
2152 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2153 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
2154 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2155 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
2156 XChangeProperty(disp, GDK_ROOT_WINDOW(),
2157 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
2161 /* Store the data in a cut-buffer. */
2162 void store_cutbuffer(char * ptr, int len)
2164 #ifndef NOT_X_WINDOWS
2165 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2166 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
2167 XRotateBuffers(disp, 1);
2168 XStoreBytes(disp, ptr, len);
2172 /* Retrieve data from a cut-buffer.
2173 * Returned data needs to be freed with XFree().
2175 char * retrieve_cutbuffer(int * nbytes)
2177 #ifndef NOT_X_WINDOWS
2178 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2180 ptr = XFetchBytes(disp, nbytes);
2181 if (*nbytes <= 0 && ptr != 0) {
2191 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
2193 struct gui_data *inst = (struct gui_data *)frontend;
2194 if (inst->pasteout_data)
2195 sfree(inst->pasteout_data);
2196 if (inst->pasteout_data_ctext)
2197 sfree(inst->pasteout_data_ctext);
2198 if (inst->pasteout_data_utf8)
2199 sfree(inst->pasteout_data_utf8);
2202 * Set up UTF-8 and compound text paste data. This only happens
2203 * if we aren't in direct-to-font mode using the D800 hack.
2205 if (!inst->direct_to_font) {
2206 const wchar_t *tmp = data;
2208 #ifndef NOT_X_WINDOWS
2211 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2214 inst->pasteout_data_utf8 = snewn(len*6, char);
2215 inst->pasteout_data_utf8_len = len*6;
2216 inst->pasteout_data_utf8_len =
2217 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
2218 inst->pasteout_data_utf8_len,
2219 CS_UTF8, NULL, NULL, 0);
2220 if (inst->pasteout_data_utf8_len == 0) {
2221 sfree(inst->pasteout_data_utf8);
2222 inst->pasteout_data_utf8 = NULL;
2224 inst->pasteout_data_utf8 =
2225 sresize(inst->pasteout_data_utf8,
2226 inst->pasteout_data_utf8_len + 1, char);
2227 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
2231 * Now let Xlib convert our UTF-8 data into compound text.
2233 #ifndef NOT_X_WINDOWS
2234 list[0] = inst->pasteout_data_utf8;
2235 if (Xutf8TextListToTextProperty(disp, list, 1,
2236 XCompoundTextStyle, &tp) == 0) {
2237 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
2238 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
2239 inst->pasteout_data_ctext_len = tp.nitems;
2244 inst->pasteout_data_ctext = NULL;
2245 inst->pasteout_data_ctext_len = 0;
2248 inst->pasteout_data_utf8 = NULL;
2249 inst->pasteout_data_utf8_len = 0;
2250 inst->pasteout_data_ctext = NULL;
2251 inst->pasteout_data_ctext_len = 0;
2254 inst->pasteout_data = snewn(len*6, char);
2255 inst->pasteout_data_len = len*6;
2256 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
2257 data, len, inst->pasteout_data,
2258 inst->pasteout_data_len,
2260 if (inst->pasteout_data_len == 0) {
2261 sfree(inst->pasteout_data);
2262 inst->pasteout_data = NULL;
2264 inst->pasteout_data =
2265 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
2268 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
2270 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
2271 inst->input_event_time)) {
2272 #if GTK_CHECK_VERSION(2,0,0)
2273 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
2275 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2276 GDK_SELECTION_TYPE_STRING, 1);
2277 if (inst->pasteout_data_ctext)
2278 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2279 compound_text_atom, 1);
2280 if (inst->pasteout_data_utf8)
2281 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
2282 utf8_string_atom, 1);
2286 term_deselect(inst->term);
2289 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
2290 guint info, guint time_stamp, gpointer data)
2292 struct gui_data *inst = (struct gui_data *)data;
2293 GdkAtom target = gtk_selection_data_get_target(seldata);
2294 if (target == utf8_string_atom)
2295 gtk_selection_data_set(seldata, target, 8,
2296 (unsigned char *)inst->pasteout_data_utf8,
2297 inst->pasteout_data_utf8_len);
2298 else if (target == compound_text_atom)
2299 gtk_selection_data_set(seldata, target, 8,
2300 (unsigned char *)inst->pasteout_data_ctext,
2301 inst->pasteout_data_ctext_len);
2303 gtk_selection_data_set(seldata, target, 8,
2304 (unsigned char *)inst->pasteout_data,
2305 inst->pasteout_data_len);
2308 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
2311 struct gui_data *inst = (struct gui_data *)data;
2313 term_deselect(inst->term);
2314 if (inst->pasteout_data)
2315 sfree(inst->pasteout_data);
2316 if (inst->pasteout_data_ctext)
2317 sfree(inst->pasteout_data_ctext);
2318 if (inst->pasteout_data_utf8)
2319 sfree(inst->pasteout_data_utf8);
2320 inst->pasteout_data = NULL;
2321 inst->pasteout_data_len = 0;
2322 inst->pasteout_data_ctext = NULL;
2323 inst->pasteout_data_ctext_len = 0;
2324 inst->pasteout_data_utf8 = NULL;
2325 inst->pasteout_data_utf8_len = 0;
2329 void request_paste(void *frontend)
2331 struct gui_data *inst = (struct gui_data *)frontend;
2333 * In Unix, pasting is asynchronous: all we can do at the
2334 * moment is to call gtk_selection_convert(), and when the data
2335 * comes back _then_ we can call term_do_paste().
2338 if (!inst->direct_to_font) {
2340 * First we attempt to retrieve the selection as a UTF-8
2341 * string (which we will convert to the correct code page
2342 * before sending to the session, of course). If that
2343 * fails, selection_received() will be informed and will
2344 * fall back to an ordinary string.
2346 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2348 inst->input_event_time);
2351 * If we're in direct-to-font mode, we disable UTF-8
2352 * pasting, and go straight to ordinary string data.
2354 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2355 GDK_SELECTION_TYPE_STRING,
2356 inst->input_event_time);
2360 gint idle_paste_func(gpointer data); /* forward ref */
2362 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
2363 guint time, gpointer data)
2365 struct gui_data *inst = (struct gui_data *)data;
2368 #ifndef NOT_X_WINDOWS
2370 int free_list_required = 0;
2371 int free_required = 0;
2374 GdkAtom seldata_target = gtk_selection_data_get_target(seldata);
2375 GdkAtom seldata_type = gtk_selection_data_get_data_type(seldata);
2376 const guchar *seldata_data = gtk_selection_data_get_data(seldata);
2377 gint seldata_length = gtk_selection_data_get_length(seldata);
2379 if (seldata_target == utf8_string_atom && seldata_length <= 0) {
2381 * Failed to get a UTF-8 selection string. Try compound
2384 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2386 inst->input_event_time);
2390 if (seldata_target == compound_text_atom && seldata_length <= 0) {
2392 * Failed to get UTF-8 or compound text. Try an ordinary
2395 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2396 GDK_SELECTION_TYPE_STRING,
2397 inst->input_event_time);
2402 * If we have data, but it's not of a type we can deal with,
2403 * we have to ignore the data.
2405 if (seldata_length > 0 &&
2406 seldata_type != GDK_SELECTION_TYPE_STRING &&
2407 seldata_type != compound_text_atom &&
2408 seldata_type != utf8_string_atom)
2412 * If we have no data, try looking in a cut buffer.
2414 if (seldata_length <= 0) {
2415 #ifndef NOT_X_WINDOWS
2416 text = retrieve_cutbuffer(&length);
2419 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2420 * source, so use that as a de-facto standard. */
2421 charset = CS_ISO8859_1;
2428 * Convert COMPOUND_TEXT into UTF-8.
2430 if (seldata_type == compound_text_atom) {
2431 #ifndef NOT_X_WINDOWS
2434 Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
2436 tp.value = (unsigned char *)seldata_data;
2437 tp.encoding = (Atom) seldata_type;
2438 tp.format = gtk_selection_data_get_format(seldata);
2439 tp.nitems = seldata_length;
2440 ret = Xutf8TextPropertyToTextList(disp, &tp, &list, &count);
2441 if (ret == 0 && count == 1) {
2443 length = strlen(list[0]);
2445 free_list_required = 1;
2450 * Compound text failed; fall back to STRING.
2452 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2453 GDK_SELECTION_TYPE_STRING,
2454 inst->input_event_time);
2458 text = (char *)seldata_data;
2459 length = seldata_length;
2460 charset = (seldata_type == utf8_string_atom ?
2461 CS_UTF8 : inst->ucsdata.line_codepage);
2465 if (inst->pastein_data)
2466 sfree(inst->pastein_data);
2468 inst->pastein_data = snewn(length, wchar_t);
2469 inst->pastein_data_len = length;
2470 inst->pastein_data_len =
2471 mb_to_wc(charset, 0, text, length,
2472 inst->pastein_data, inst->pastein_data_len);
2474 term_do_paste(inst->term);
2476 #ifndef NOT_X_WINDOWS
2477 if (free_list_required)
2478 XFreeStringList(list);
2484 void get_clip(void *frontend, wchar_t ** p, int *len)
2486 struct gui_data *inst = (struct gui_data *)frontend;
2489 *p = inst->pastein_data;
2490 *len = inst->pastein_data_len;
2494 static void set_window_titles(struct gui_data *inst)
2497 * We must always call set_icon_name after calling set_title,
2498 * since set_title will write both names. Irritating, but such
2501 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2502 if (!conf_get_int(inst->conf, CONF_win_name_always))
2503 gdk_window_set_icon_name(gtk_widget_get_window(inst->window),
2507 void set_title(void *frontend, char *title)
2509 struct gui_data *inst = (struct gui_data *)frontend;
2510 sfree(inst->wintitle);
2511 inst->wintitle = dupstr(title);
2512 set_window_titles(inst);
2515 void set_icon(void *frontend, char *title)
2517 struct gui_data *inst = (struct gui_data *)frontend;
2518 sfree(inst->icontitle);
2519 inst->icontitle = dupstr(title);
2520 set_window_titles(inst);
2523 void set_title_and_icon(void *frontend, char *title, char *icon)
2525 struct gui_data *inst = (struct gui_data *)frontend;
2526 sfree(inst->wintitle);
2527 inst->wintitle = dupstr(title);
2528 sfree(inst->icontitle);
2529 inst->icontitle = dupstr(icon);
2530 set_window_titles(inst);
2533 void set_sbar(void *frontend, int total, int start, int page)
2535 struct gui_data *inst = (struct gui_data *)frontend;
2536 if (!conf_get_int(inst->conf, CONF_scrollbar))
2538 gtk_adjustment_set_lower(inst->sbar_adjust, 0);
2539 gtk_adjustment_set_upper(inst->sbar_adjust, total);
2540 gtk_adjustment_set_value(inst->sbar_adjust, start);
2541 gtk_adjustment_set_page_size(inst->sbar_adjust, page);
2542 gtk_adjustment_set_step_increment(inst->sbar_adjust, 1);
2543 gtk_adjustment_set_page_increment(inst->sbar_adjust, page/2);
2544 inst->ignore_sbar = TRUE;
2545 gtk_adjustment_changed(inst->sbar_adjust);
2546 inst->ignore_sbar = FALSE;
2549 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2551 struct gui_data *inst = (struct gui_data *)data;
2553 if (!conf_get_int(inst->conf, CONF_scrollbar))
2555 if (!inst->ignore_sbar)
2556 term_scroll(inst->term, 1, (int)gtk_adjustment_get_value(adj));
2559 void sys_cursor(void *frontend, int x, int y)
2562 * This is meaningless under X.
2567 * This is still called when mode==BELL_VISUAL, even though the
2568 * visual bell is handled entirely within terminal.c, because we
2569 * may want to perform additional actions on any kind of bell (for
2570 * example, taskbar flashing in Windows).
2572 void do_beep(void *frontend, int mode)
2574 if (mode == BELL_DEFAULT)
2578 int char_width(Context ctx, int uc)
2581 * Under X, any fixed-width font really _is_ fixed-width.
2582 * Double-width characters will be dealt with using a separate
2583 * font. For the moment we can simply return 1.
2585 * FIXME: but is that also true of Pango?
2590 Context get_ctx(void *frontend)
2592 struct gui_data *inst = (struct gui_data *)frontend;
2593 struct draw_ctx *dctx;
2596 if (!gtk_widget_get_window(inst->area))
2599 #ifndef NO_BACKING_PIXMAPS
2600 target = inst->pixmap;
2602 target = gtk_widget_get_window(inst->area);
2605 dctx = snew(struct draw_ctx);
2607 dctx->uctx.type = inst->drawtype;
2608 #ifdef DRAW_TEXT_GDK
2609 if (dctx->uctx.type == DRAWTYPE_GDK) {
2610 dctx->uctx.u.gdk.target = target;
2611 dctx->uctx.u.gdk.gc = gdk_gc_new(gtk_widget_get_window(inst->area));
2614 #ifdef DRAW_TEXT_CAIRO
2615 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2616 dctx->uctx.u.cairo.widget = GTK_WIDGET(inst->area);
2617 dctx->uctx.u.cairo.cr = gdk_cairo_create(target);
2618 cairo_setup_dctx(dctx);
2624 void free_ctx(Context ctx)
2626 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2627 /* struct gui_data *inst = dctx->inst; */
2628 #ifdef DRAW_TEXT_GDK
2629 if (dctx->uctx.type == DRAWTYPE_GDK) {
2630 gdk_gc_unref(dctx->uctx.u.gdk.gc);
2633 #ifdef DRAW_TEXT_CAIRO
2634 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2635 cairo_destroy(dctx->uctx.u.cairo.cr);
2642 static void draw_update(struct draw_ctx *dctx, int x, int y, int w, int h)
2644 #ifndef NO_BACKING_PIXMAPS
2645 #ifdef DRAW_TEXT_GDK
2646 if (dctx->uctx.type == DRAWTYPE_GDK) {
2647 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2648 dctx->uctx.u.gdk.gc, dctx->inst->pixmap,
2652 #ifdef DRAW_TEXT_CAIRO /* FIXME: and not GTK3 where a cairo_t is all we have */
2653 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2654 GdkGC *gc = gdk_gc_new(gtk_widget_get_window(dctx->inst->area));
2655 gdk_draw_pixmap(gtk_widget_get_window(dctx->inst->area),
2656 gc, dctx->inst->pixmap, x, y, x, y, w, h);
2663 static void draw_set_colour(struct draw_ctx *dctx, int col)
2665 #ifdef DRAW_TEXT_GDK
2666 if (dctx->uctx.type == DRAWTYPE_GDK) {
2667 gdk_gc_set_foreground(dctx->uctx.u.gdk.gc, &dctx->inst->cols[col]);
2670 #ifdef DRAW_TEXT_CAIRO
2671 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2672 cairo_set_source_rgb(dctx->uctx.u.cairo.cr,
2673 dctx->inst->cols[col].red / 65535.0,
2674 dctx->inst->cols[col].green / 65535.0,
2675 dctx->inst->cols[col].blue / 65535.0);
2680 static void draw_rectangle(struct draw_ctx *dctx, int filled,
2681 int x, int y, int w, int h)
2683 #ifdef DRAW_TEXT_GDK
2684 if (dctx->uctx.type == DRAWTYPE_GDK) {
2685 gdk_draw_rectangle(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2686 filled, x, y, w, h);
2689 #ifdef DRAW_TEXT_CAIRO
2690 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2691 cairo_new_path(dctx->uctx.u.cairo.cr);
2693 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2694 cairo_fill(dctx->uctx.u.cairo.cr);
2696 cairo_rectangle(dctx->uctx.u.cairo.cr,
2697 x + 0.5, y + 0.5, w, h);
2698 cairo_close_path(dctx->uctx.u.cairo.cr);
2699 cairo_stroke(dctx->uctx.u.cairo.cr);
2705 static void draw_clip(struct draw_ctx *dctx, int x, int y, int w, int h)
2707 #ifdef DRAW_TEXT_GDK
2708 if (dctx->uctx.type == DRAWTYPE_GDK) {
2716 gdk_gc_set_clip_rectangle(dctx->uctx.u.gdk.gc, &r);
2719 #ifdef DRAW_TEXT_CAIRO
2720 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2721 cairo_reset_clip(dctx->uctx.u.cairo.cr);
2722 cairo_new_path(dctx->uctx.u.cairo.cr);
2723 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, w, h);
2724 cairo_clip(dctx->uctx.u.cairo.cr);
2729 static void draw_point(struct draw_ctx *dctx, int x, int y)
2731 #ifdef DRAW_TEXT_GDK
2732 if (dctx->uctx.type == DRAWTYPE_GDK) {
2733 gdk_draw_point(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc, x, y);
2736 #ifdef DRAW_TEXT_CAIRO
2737 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2738 cairo_new_path(dctx->uctx.u.cairo.cr);
2739 cairo_rectangle(dctx->uctx.u.cairo.cr, x, y, 1, 1);
2740 cairo_fill(dctx->uctx.u.cairo.cr);
2745 static void draw_line(struct draw_ctx *dctx, int x0, int y0, int x1, int y1)
2747 #ifdef DRAW_TEXT_GDK
2748 if (dctx->uctx.type == DRAWTYPE_GDK) {
2749 gdk_draw_line(dctx->uctx.u.gdk.target, dctx->uctx.u.gdk.gc,
2753 #ifdef DRAW_TEXT_CAIRO
2754 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2755 cairo_new_path(dctx->uctx.u.cairo.cr);
2756 cairo_move_to(dctx->uctx.u.cairo.cr, x0 + 0.5, y0 + 0.5);
2757 cairo_line_to(dctx->uctx.u.cairo.cr, x1 + 0.5, y1 + 0.5);
2758 cairo_stroke(dctx->uctx.u.cairo.cr);
2763 static void draw_stretch_before(struct draw_ctx *dctx, int x, int y,
2765 int h, int hdouble, int hbothalf)
2767 #ifdef DRAW_TEXT_CAIRO
2768 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2769 cairo_matrix_t matrix;
2793 cairo_transform(dctx->uctx.u.cairo.cr, &matrix);
2798 static void draw_stretch_after(struct draw_ctx *dctx, int x, int y,
2800 int h, int hdouble, int hbothalf)
2802 #ifdef DRAW_TEXT_GDK
2803 #ifndef NO_BACKING_PIXMAPS
2804 if (dctx->uctx.type == DRAWTYPE_GDK) {
2806 * I can't find any plausible StretchBlt equivalent in the X
2807 * server, so I'm going to do this the slow and painful way.
2808 * This will involve repeated calls to gdk_draw_pixmap() to
2809 * stretch the text horizontally. It's O(N^2) in time and O(N)
2810 * in network bandwidth, but you try thinking of a better way.
2815 for (i = 0; i < w; i++) {
2816 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
2817 dctx->uctx.u.gdk.gc,
2818 dctx->uctx.u.gdk.target,
2828 /* Now stretch vertically, in the same way. */
2833 for (i = 0; i < h; i += 2) {
2834 gdk_draw_pixmap(dctx->uctx.u.gdk.target,
2835 dctx->uctx.u.gdk.gc,
2836 dctx->uctx.u.gdk.target,
2844 #error No way to implement stretching in GDK without a reliable backing pixmap
2846 #endif /* DRAW_TEXT_GDK */
2847 #ifdef DRAW_TEXT_CAIRO
2848 if (dctx->uctx.type == DRAWTYPE_CAIRO) {
2849 cairo_set_matrix(dctx->uctx.u.cairo.cr,
2850 &dctx->uctx.u.cairo.origmatrix);
2855 static void draw_backing_rect(struct gui_data *inst)
2857 struct draw_ctx *dctx = get_ctx(inst);
2858 draw_set_colour(dctx, 258);
2859 draw_rectangle(dctx, 1, 0, 0,
2860 inst->width * inst->font_width + 2*inst->window_border,
2861 inst->height * inst->font_height + 2*inst->window_border);
2866 * Draw a line of text in the window, at given character
2867 * coordinates, in given attributes.
2869 * We are allowed to fiddle with the contents of `text'.
2871 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2872 unsigned long attr, int lattr)
2874 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2875 struct gui_data *inst = dctx->inst;
2876 int ncombining, combining;
2877 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2879 gdk_visual_get_depth(gtk_widget_get_visual(inst->area)) == 1;
2881 if (attr & TATTR_COMBINING) {
2887 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2888 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2889 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2894 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
2895 if (nfg < 16) nfg |= 8;
2896 else if (nfg >= 256) nfg |= 1;
2898 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
2899 if (nbg < 16) nbg |= 8;
2900 else if (nbg >= 256) nbg |= 1;
2902 if ((attr & TATTR_ACTCURS) && !monochrome) {
2907 fontid = shadow = 0;
2909 if (attr & ATTR_WIDE) {
2916 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
2923 if (!inst->fonts[fontid]) {
2926 * Fall back through font ids with subsets of this one's
2927 * set bits, in order.
2929 for (i = fontid; i-- > 0 ;) {
2931 continue; /* some other bit is set */
2932 if (inst->fonts[i]) {
2937 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2940 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2942 if (x >= inst->term->cols)
2944 if (x + len*2*widefactor > inst->term->cols)
2945 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2951 x*inst->font_width+inst->window_border,
2952 y*inst->font_height+inst->window_border,
2953 rlen*widefactor*inst->font_width,
2956 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2957 draw_stretch_before(dctx,
2958 x*inst->font_width+inst->window_border,
2959 y*inst->font_height+inst->window_border,
2960 rlen*widefactor*inst->font_width, TRUE,
2962 ((lattr & LATTR_MODE) != LATTR_WIDE),
2963 ((lattr & LATTR_MODE) == LATTR_BOT));
2966 draw_set_colour(dctx, nbg);
2967 draw_rectangle(dctx, TRUE,
2968 x*inst->font_width+inst->window_border,
2969 y*inst->font_height+inst->window_border,
2970 rlen*widefactor*inst->font_width, inst->font_height);
2972 draw_set_colour(dctx, nfg);
2973 for (combining = 0; combining < ncombining; combining++) {
2974 unifont_draw_text(&dctx->uctx, inst->fonts[fontid],
2975 x*inst->font_width+inst->window_border,
2976 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2977 text + combining, len, widefactor > 1,
2978 bold, inst->font_width);
2981 if (attr & ATTR_UNDER) {
2982 int uheight = inst->fonts[0]->ascent + 1;
2983 if (uheight >= inst->font_height)
2984 uheight = inst->font_height - 1;
2985 draw_line(dctx, x*inst->font_width+inst->window_border,
2986 y*inst->font_height + uheight + inst->window_border,
2987 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2988 y*inst->font_height + uheight + inst->window_border);
2991 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2992 draw_stretch_after(dctx,
2993 x*inst->font_width+inst->window_border,
2994 y*inst->font_height+inst->window_border,
2995 rlen*widefactor*inst->font_width, TRUE,
2997 ((lattr & LATTR_MODE) != LATTR_WIDE),
2998 ((lattr & LATTR_MODE) == LATTR_BOT));
3002 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3003 unsigned long attr, int lattr)
3005 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3006 struct gui_data *inst = dctx->inst;
3009 do_text_internal(ctx, x, y, text, len, attr, lattr);
3011 if (attr & ATTR_WIDE) {
3017 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3019 if (x >= inst->term->cols)
3021 if (x + len*2*widefactor > inst->term->cols)
3022 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3027 x*inst->font_width+inst->window_border,
3028 y*inst->font_height+inst->window_border,
3029 len*widefactor*inst->font_width, inst->font_height);
3032 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
3033 unsigned long attr, int lattr)
3035 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
3036 struct gui_data *inst = dctx->inst;
3038 int active, passive, widefactor;
3040 if (attr & TATTR_PASCURS) {
3041 attr &= ~TATTR_PASCURS;
3045 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
3046 attr &= ~TATTR_ACTCURS;
3050 do_text_internal(ctx, x, y, text, len, attr, lattr);
3052 if (attr & TATTR_COMBINING)
3055 if (attr & ATTR_WIDE) {
3061 if ((lattr & LATTR_MODE) != LATTR_NORM) {
3063 if (x >= inst->term->cols)
3065 if (x + len*2*widefactor > inst->term->cols)
3066 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
3070 if (inst->cursor_type == 0) {
3072 * An active block cursor will already have been done by
3073 * the above do_text call, so we only need to do anything
3077 draw_set_colour(dctx, 261);
3078 draw_rectangle(dctx, FALSE,
3079 x*inst->font_width+inst->window_border,
3080 y*inst->font_height+inst->window_border,
3081 len*widefactor*inst->font_width-1,
3082 inst->font_height-1);
3086 int startx, starty, dx, dy, length, i;
3090 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
3091 char_width = 2*inst->font_width;
3093 char_width = inst->font_width;
3095 if (inst->cursor_type == 1) {
3096 uheight = inst->fonts[0]->ascent + 1;
3097 if (uheight >= inst->font_height)
3098 uheight = inst->font_height - 1;
3100 startx = x * inst->font_width + inst->window_border;
3101 starty = y * inst->font_height + inst->window_border + uheight;
3104 length = len * widefactor * char_width;
3107 if (attr & TATTR_RIGHTCURS)
3108 xadjust = char_width - 1;
3109 startx = x * inst->font_width + inst->window_border + xadjust;
3110 starty = y * inst->font_height + inst->window_border;
3113 length = inst->font_height;
3116 draw_set_colour(dctx, 261);
3118 for (i = 0; i < length; i++) {
3120 draw_point(dctx, startx, starty);
3125 } else if (active) {
3126 draw_line(dctx, startx, starty,
3127 startx + (length-1) * dx, starty + (length-1) * dy);
3128 } /* else no cursor (e.g., blinked off) */
3132 x*inst->font_width+inst->window_border,
3133 y*inst->font_height+inst->window_border,
3134 len*widefactor*inst->font_width, inst->font_height);
3136 #if GTK_CHECK_VERSION(2,0,0)
3138 GdkRectangle cursorrect;
3139 cursorrect.x = x*inst->font_width+inst->window_border;
3140 cursorrect.y = y*inst->font_height+inst->window_border;
3141 cursorrect.width = len*widefactor*inst->font_width;
3142 cursorrect.height = inst->font_height;
3143 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
3148 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
3150 if (cursor_val == -1) {
3151 #if GTK_CHECK_VERSION(2,16,0)
3152 cursor_val = GDK_BLANK_CURSOR;
3155 * Work around absence of GDK_BLANK_CURSOR by inventing a
3159 GdkColor bg = { 0, 0, 0, 0 };
3160 GdkPixmap *pm = gdk_pixmap_new(NULL, 1, 1, 1);
3161 GdkGC *gc = gdk_gc_new(pm);
3162 gdk_gc_set_foreground(gc, &bg);
3163 gdk_draw_rectangle(pm, gc, 1, 0, 0, 1, 1);
3165 ret = gdk_cursor_new_from_pixmap(pm, pm, &bg, &bg, 1, 1);
3166 gdk_pixmap_unref(pm);
3171 return gdk_cursor_new(cursor_val);
3174 void modalfatalbox(const char *p, ...)
3177 fprintf(stderr, "FATAL ERROR: ");
3179 vfprintf(stderr, p, ap);
3181 fputc('\n', stderr);
3185 void cmdline_error(const char *p, ...)
3188 fprintf(stderr, "%s: ", appname);
3190 vfprintf(stderr, p, ap);
3192 fputc('\n', stderr);
3196 const char *get_x_display(void *frontend)
3198 return gdk_get_display();
3201 #ifndef NOT_X_WINDOWS
3202 long get_windowid(void *frontend)
3204 struct gui_data *inst = (struct gui_data *)frontend;
3205 return (long)GDK_WINDOW_XID(gtk_widget_get_window(inst->area));
3209 static void help(FILE *fp) {
3211 "pterm option summary:\n"
3213 " --display DISPLAY Specify X display to use (note '--')\n"
3214 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
3215 " -fn FONT Normal text font\n"
3216 " -fb FONT Bold text font\n"
3217 " -geometry GEOMETRY Position and size of window (size in characters)\n"
3218 " -sl LINES Number of lines of scrollback\n"
3219 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
3220 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
3221 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
3222 " -T TITLE Window title\n"
3223 " -ut, +ut Do(default) or do not update utmp\n"
3224 " -ls, +ls Do(default) or do not make shell a login shell\n"
3225 " -sb, +sb Do(default) or do not display a scrollbar\n"
3226 " -log PATH Log all output to a file\n"
3227 " -nethack Map numeric keypad to hjklyubn direction keys\n"
3228 " -xrm RESOURCE-STRING Set an X resource\n"
3229 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
3230 ) < 0 || fflush(fp) < 0) {
3231 perror("output error");
3236 static void version(FILE *fp) {
3237 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
3238 perror("output error");
3243 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
3244 struct gui_data *inst, Conf *conf)
3250 * Macros to make argument handling easier. Note that because
3251 * they need to call `continue', they cannot be contained in
3252 * the usual do {...} while (0) wrapper to make them
3253 * syntactically single statements; hence it is not legal to
3254 * use one of these macros as an unbraced statement between
3257 #define EXPECTS_ARG { \
3258 if (--argc <= 0) { \
3260 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
3265 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
3267 while (--argc > 0) {
3268 const char *p = *++argv;
3272 * Shameless cheating. Debian requires all X terminal
3273 * emulators to support `-T title'; but
3274 * cmdline_process_param will eat -T (it means no-pty) and
3275 * complain that pterm doesn't support it. So, in pterm
3276 * only, we convert -T into -title.
3278 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
3282 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
3283 do_everything ? 1 : -1, conf);
3286 cmdline_error("option \"%s\" requires an argument", p);
3287 } else if (ret == 2) {
3288 --argc, ++argv; /* skip next argument */
3290 } else if (ret == 1) {
3294 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
3298 fs = fontspec_new(val);
3299 conf_set_fontspec(conf, CONF_font, fs);
3302 } else if (!strcmp(p, "-fb")) {
3306 fs = fontspec_new(val);
3307 conf_set_fontspec(conf, CONF_boldfont, fs);
3310 } else if (!strcmp(p, "-fw")) {
3314 fs = fontspec_new(val);
3315 conf_set_fontspec(conf, CONF_widefont, fs);
3318 } else if (!strcmp(p, "-fwb")) {
3322 fs = fontspec_new(val);
3323 conf_set_fontspec(conf, CONF_wideboldfont, fs);
3326 } else if (!strcmp(p, "-cs")) {
3329 conf_set_str(conf, CONF_line_codepage, val);
3331 } else if (!strcmp(p, "-geometry")) {
3335 #if GTK_CHECK_VERSION(2,0,0)
3336 inst->geometry = val;
3338 /* On GTK 1, we have to do this using raw Xlib */
3342 flags = XParseGeometry(val, &x, &y, &w, &h);
3343 if (flags & WidthValue)
3344 conf_set_int(conf, CONF_width, w);
3345 if (flags & HeightValue)
3346 conf_set_int(conf, CONF_height, h);
3348 if (flags & (XValue | YValue)) {
3351 inst->gotpos = TRUE;
3352 inst->gravity = ((flags & XNegative ? 1 : 0) |
3353 (flags & YNegative ? 2 : 0));
3358 } else if (!strcmp(p, "-sl")) {
3361 conf_set_int(conf, CONF_savelines, atoi(val));
3363 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
3364 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
3365 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
3370 #if GTK_CHECK_VERSION(3,0,0)
3372 int success = gdk_rgba_parse(&rgba, val);
3375 int success = gdk_color_parse(val, &col);
3380 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
3383 #if GTK_CHECK_VERSION(3,0,0)
3384 int r = rgba.red * 255;
3385 int g = rgba.green * 255;
3386 int b = rgba.blue * 255;
3388 int r = col.red / 256;
3389 int g = col.green / 256;
3390 int b = col.blue / 256;
3394 index = (!strcmp(p, "-fg") ? 0 :
3395 !strcmp(p, "-bg") ? 2 :
3396 !strcmp(p, "-bfg") ? 1 :
3397 !strcmp(p, "-bbg") ? 3 :
3398 !strcmp(p, "-cfg") ? 4 :
3399 !strcmp(p, "-cbg") ? 5 : -1);
3400 assert(index != -1);
3402 conf_set_int_int(conf, CONF_colours, index*3+0, r);
3403 conf_set_int_int(conf, CONF_colours, index*3+1, g);
3404 conf_set_int_int(conf, CONF_colours, index*3+2, b);
3408 } else if (use_pty_argv && !strcmp(p, "-e")) {
3409 /* This option swallows all further arguments. */
3415 pty_argv = snewn(argc+1, char *);
3417 for (i = 0; i < argc; i++)
3418 pty_argv[i] = argv[i];
3419 pty_argv[argc] = NULL;
3420 break; /* finished command-line processing */
3422 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
3425 } else if (!strcmp(p, "-title")) {
3428 conf_set_str(conf, CONF_wintitle, val);
3430 } else if (!strcmp(p, "-log")) {
3434 fn = filename_from_str(val);
3435 conf_set_filename(conf, CONF_logfilename, fn);
3436 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
3439 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
3441 conf_set_int(conf, CONF_stamp_utmp, 0);
3443 } else if (!strcmp(p, "-ut")) {
3445 conf_set_int(conf, CONF_stamp_utmp, 1);
3447 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
3449 conf_set_int(conf, CONF_login_shell, 0);
3451 } else if (!strcmp(p, "-ls")) {
3453 conf_set_int(conf, CONF_login_shell, 1);
3455 } else if (!strcmp(p, "-nethack")) {
3457 conf_set_int(conf, CONF_nethack_keypad, 1);
3459 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
3461 conf_set_int(conf, CONF_scrollbar, 0);
3463 } else if (!strcmp(p, "-sb")) {
3465 conf_set_int(conf, CONF_scrollbar, 1);
3467 } else if (!strcmp(p, "-name")) {
3471 } else if (!strcmp(p, "-xrm")) {
3473 provide_xrm_string(val);
3475 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
3479 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
3483 } else if (!strcmp(p, "-pgpfp")) {
3487 } else if(p[0] != '-' && (!do_everything ||
3488 process_nonoption_arg(p, conf,
3494 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
3502 #if GTK_CHECK_VERSION(2,0,0)
3510 uxsel_id *uxsel_input_add(int fd, int rwx) {
3511 uxsel_id *id = snew(uxsel_id);
3513 #if GTK_CHECK_VERSION(2,0,0)
3515 if (rwx & 1) flags |= G_IO_IN;
3516 if (rwx & 2) flags |= G_IO_OUT;
3517 if (rwx & 4) flags |= G_IO_PRI;
3518 id->chan = g_io_channel_unix_new(fd);
3519 g_io_channel_set_encoding(id->chan, NULL, NULL);
3520 id->watch_id = g_io_add_watch(id->chan, flags, fd_input_func, NULL);
3523 if (rwx & 1) flags |= GDK_INPUT_READ;
3524 if (rwx & 2) flags |= GDK_INPUT_WRITE;
3525 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
3527 id->id = gdk_input_add(fd, flags, fd_input_func, NULL);
3533 void uxsel_input_remove(uxsel_id *id) {
3534 #if GTK_CHECK_VERSION(2,0,0)
3535 g_source_remove(id->watch_id);
3536 g_io_channel_unref(id->chan);
3538 gdk_input_remove(id->id);
3543 char *setup_fonts_ucs(struct gui_data *inst)
3545 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
3546 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
3551 fs = conf_get_fontspec(inst->conf, CONF_font);
3552 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
3553 shadowboldoffset, shadowbold);
3555 return dupprintf("unable to load font \"%s\"", fs->name);
3558 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
3559 if (shadowbold || !fs->name[0]) {
3562 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
3563 shadowboldoffset, shadowbold);
3566 unifont_destroy(fonts[0]);
3567 return dupprintf("unable to load bold font \"%s\"", fs->name);
3571 fs = conf_get_fontspec(inst->conf, CONF_widefont);
3573 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
3574 shadowboldoffset, shadowbold);
3576 for (i = 0; i < 2; i++)
3578 unifont_destroy(fonts[i]);
3579 return dupprintf("unable to load wide font \"%s\"", fs->name);
3585 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
3586 if (shadowbold || !fs->name[0]) {
3589 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
3590 shadowboldoffset, shadowbold);
3592 for (i = 0; i < 3; i++)
3594 unifont_destroy(fonts[i]);
3595 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
3600 * Now we've got past all the possible error conditions, we can
3601 * actually update our state.
3604 for (i = 0; i < 4; i++) {
3606 unifont_destroy(inst->fonts[i]);
3607 inst->fonts[i] = fonts[i];
3610 inst->font_width = inst->fonts[0]->width;
3611 inst->font_height = inst->fonts[0]->height;
3613 inst->direct_to_font = init_ucs(&inst->ucsdata,
3614 conf_get_str(inst->conf, CONF_line_codepage),
3615 conf_get_int(inst->conf, CONF_utf8_override),
3616 inst->fonts[0]->public_charset,
3617 conf_get_int(inst->conf, CONF_vtmode));
3619 inst->drawtype = inst->fonts[0]->preferred_drawtype;
3624 void set_geom_hints(struct gui_data *inst)
3627 geom.min_width = inst->font_width + 2*inst->window_border;
3628 geom.min_height = inst->font_height + 2*inst->window_border;
3629 geom.max_width = geom.max_height = -1;
3630 geom.base_width = 2*inst->window_border;
3631 geom.base_height = 2*inst->window_border;
3632 geom.width_inc = inst->font_width;
3633 geom.height_inc = inst->font_height;
3634 geom.min_aspect = geom.max_aspect = 0;
3635 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
3636 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
3637 GDK_HINT_RESIZE_INC);
3640 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
3642 struct gui_data *inst = (struct gui_data *)data;
3643 term_clrsb(inst->term);
3646 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3648 struct gui_data *inst = (struct gui_data *)data;
3649 term_pwron(inst->term, TRUE);
3651 ldisc_echoedit_update(inst->ldisc);
3654 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3656 struct gui_data *inst = (struct gui_data *)data;
3657 term_copyall(inst->term);
3660 void special_menuitem(GtkMenuItem *item, gpointer data)
3662 struct gui_data *inst = (struct gui_data *)data;
3663 int code = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
3667 inst->back->special(inst->backhandle, code);
3670 void about_menuitem(GtkMenuItem *item, gpointer data)
3672 struct gui_data *inst = (struct gui_data *)data;
3673 about_box(inst->window);
3676 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3678 struct gui_data *inst = (struct gui_data *)data;
3679 showeventlog(inst->eventlogstuff, inst->window);
3682 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3684 /* This maps colour indices in inst->conf to those used in inst->cols. */
3685 static const int ww[] = {
3686 256, 257, 258, 259, 260, 261,
3687 0, 8, 1, 9, 2, 10, 3, 11,
3688 4, 12, 5, 13, 6, 14, 7, 15
3690 struct gui_data *inst = (struct gui_data *)data;
3692 Conf *oldconf, *newconf;
3693 int i, j, need_size;
3695 assert(lenof(ww) == NCFGCOLOURS);
3697 if (inst->reconfiguring)
3700 inst->reconfiguring = TRUE;
3702 title = dupcat(appname, " Reconfiguration", NULL);
3704 oldconf = inst->conf;
3705 newconf = conf_copy(inst->conf);
3707 if (do_config_box(title, newconf, 1,
3708 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3709 inst->conf = newconf;
3711 /* Pass new config data to the logging module */
3712 log_reconfig(inst->logctx, inst->conf);
3714 * Flush the line discipline's edit buffer in the case
3715 * where local editing has just been disabled.
3718 ldisc_configure(inst->ldisc, inst->conf);
3719 ldisc_echoedit_update(inst->ldisc);
3721 /* Pass new config data to the terminal */
3722 term_reconfig(inst->term, inst->conf);
3723 /* Pass new config data to the back end */
3725 inst->back->reconfig(inst->backhandle, inst->conf);
3727 cache_conf_values(inst);
3730 * Just setting inst->conf is sufficient to cause colour
3731 * setting changes to appear on the next ESC]R palette
3732 * reset. But we should also check whether any colour
3733 * settings have been changed, and revert the ones that have
3734 * to the new default, on the assumption that the user is
3735 * most likely to want an immediate update.
3737 for (i = 0; i < NCFGCOLOURS; i++) {
3738 for (j = 0; j < 3; j++)
3739 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3740 conf_get_int_int(newconf, CONF_colours, i*3+j))
3743 real_palette_set(inst, ww[i],
3744 conf_get_int_int(newconf,CONF_colours,i*3+0),
3745 conf_get_int_int(newconf,CONF_colours,i*3+1),
3746 conf_get_int_int(newconf,CONF_colours,i*3+2));
3749 * If the default background has changed, we must
3750 * repaint the space in between the window border
3751 * and the text area.
3754 set_window_background(inst);
3755 draw_backing_rect(inst);
3761 * If the scrollbar needs to be shown, hidden, or moved
3762 * from one end to the other of the window, do so now.
3764 if (conf_get_int(oldconf, CONF_scrollbar) !=
3765 conf_get_int(newconf, CONF_scrollbar)) {
3766 if (conf_get_int(newconf, CONF_scrollbar))
3767 gtk_widget_show(inst->sbar);
3769 gtk_widget_hide(inst->sbar);
3771 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3772 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3773 gtk_box_reorder_child(inst->hbox, inst->sbar,
3774 conf_get_int(newconf, CONF_scrollbar_on_left)
3779 * Change the window title, if required.
3781 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3782 conf_get_str(newconf, CONF_wintitle)))
3783 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3784 set_window_titles(inst);
3787 * Redo the whole tangled fonts and Unicode mess if
3791 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3792 conf_get_fontspec(newconf, CONF_font)->name) ||
3793 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3794 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3795 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3796 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3797 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3798 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3799 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3800 conf_get_str(newconf, CONF_line_codepage)) ||
3801 conf_get_int(oldconf, CONF_utf8_override) !=
3802 conf_get_int(newconf, CONF_utf8_override) ||
3803 conf_get_int(oldconf, CONF_vtmode) !=
3804 conf_get_int(newconf, CONF_vtmode) ||
3805 conf_get_int(oldconf, CONF_shadowbold) !=
3806 conf_get_int(newconf, CONF_shadowbold) ||
3807 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3808 conf_get_int(newconf, CONF_shadowboldoffset)) {
3809 char *errmsg = setup_fonts_ucs(inst);
3812 dupprintf("Could not change fonts in terminal window: %s\n",
3814 messagebox(inst->window, "Font setup error", msgboxtext,
3815 string_width("Could not change fonts in terminal window:"),
3826 * Resize the window.
3828 if (conf_get_int(oldconf, CONF_width) !=
3829 conf_get_int(newconf, CONF_width) ||
3830 conf_get_int(oldconf, CONF_height) !=
3831 conf_get_int(newconf, CONF_height) ||
3832 conf_get_int(oldconf, CONF_window_border) !=
3833 conf_get_int(newconf, CONF_window_border) ||
3835 set_geom_hints(inst);
3836 request_resize(inst, conf_get_int(newconf, CONF_width),
3837 conf_get_int(newconf, CONF_height));
3840 * The above will have caused a call to term_size() for
3841 * us if it happened. If the user has fiddled with only
3842 * the scrollback size, the above will not have
3843 * happened and we will need an explicit term_size()
3846 if (conf_get_int(oldconf, CONF_savelines) !=
3847 conf_get_int(newconf, CONF_savelines))
3848 term_size(inst->term, inst->term->rows, inst->term->cols,
3849 conf_get_int(newconf, CONF_savelines));
3852 term_invalidate(inst->term);
3855 * We do an explicit full redraw here to ensure the window
3856 * border has been redrawn as well as the text area.
3858 gtk_widget_queue_draw(inst->area);
3865 inst->reconfiguring = FALSE;
3868 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3871 * Re-execing ourself is not an exact science under Unix. I do
3872 * the best I can by using /proc/self/exe if available and by
3873 * assuming argv[0] can be found on $PATH if not.
3875 * Note that we also have to reconstruct the elements of the
3876 * original argv which gtk swallowed, since the user wants the
3877 * new session to appear on the same X display as the old one.
3885 * Collect the arguments with which to re-exec ourself.
3887 va_start(ap, fd_to_close);
3888 n = 2; /* progname and terminating NULL */
3889 n += inst->ngtkargs;
3890 while (va_arg(ap, char *) != NULL)
3894 args = snewn(n, char *);
3895 args[0] = inst->progname;
3897 for (i = 0; i < inst->ngtkargs; i++)
3898 args[i+1] = inst->gtkargvstart[i];
3901 va_start(ap, fd_to_close);
3902 while ((args[i++] = va_arg(ap, char *)) != NULL);
3908 * Do the double fork.
3922 } else if (pid2 > 0) {
3924 * First child has successfully forked second child. My
3925 * Work Here Is Done. Note the use of _exit rather than
3926 * exit: the latter appears to cause destroy messages
3927 * to be sent to the X server. I suspect gtk uses
3934 * If we reach here, we are the second child, so we now
3935 * actually perform the exec.
3937 if (fd_to_close >= 0)
3940 execv("/proc/self/exe", args);
3941 execvp(inst->progname, args);
3948 waitpid(pid, &status, 0);
3953 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3955 struct gui_data *inst = (struct gui_data *)gdata;
3957 * For this feature we must marshal conf and (possibly) pty_argv
3958 * into a byte stream, create a pipe, and send this byte stream
3959 * to the child through the pipe.
3961 int i, ret, sersize, size;
3966 if (pipe(pipefd) < 0) {
3971 size = sersize = conf_serialised_size(inst->conf);
3972 if (use_pty_argv && pty_argv) {
3973 for (i = 0; pty_argv[i]; i++)
3974 size += strlen(pty_argv[i]) + 1;
3977 data = snewn(size, char);
3978 conf_serialise(inst->conf, data);
3979 if (use_pty_argv && pty_argv) {
3981 for (i = 0; pty_argv[i]; i++) {
3982 strcpy(data + p, pty_argv[i]);
3983 p += strlen(pty_argv[i]) + 1;
3988 sprintf(option, "---[%d,%d]", pipefd[0], size);
3989 noncloexec(pipefd[0]);
3990 fork_and_exec_self(inst, pipefd[1], option, NULL);
3994 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3997 perror("write to pipe");
4002 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
4004 int fd, i, ret, size, size_used;
4007 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
4008 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
4012 data = snewn(size, char);
4014 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
4017 perror("read from pipe");
4019 } else if (i < size) {
4020 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
4025 size_used = conf_deserialise(conf, data, size);
4026 if (use_pty_argv && size > size_used) {
4030 while (i < size && data[i]) i++;
4032 fprintf(stderr, "%s: malformed Duplicate Session data\n",
4039 pty_argv = snewn(n+1, char *);
4045 while (i < size && data[i]) i++;
4048 pty_argv[n++] = dupstr(p);
4057 void new_session_menuitem(GtkMenuItem *item, gpointer data)
4059 struct gui_data *inst = (struct gui_data *)data;
4061 fork_and_exec_self(inst, -1, NULL);
4064 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
4066 struct gui_data *inst = (struct gui_data *)data;
4069 logevent(inst, "----- Session restarted -----");
4070 term_pwron(inst->term, FALSE);
4071 start_backend(inst);
4072 inst->exited = FALSE;
4076 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
4078 struct gui_data *inst = (struct gui_data *)data;
4079 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4081 fork_and_exec_self(inst, -1, "-load", str, NULL);
4084 void saved_session_freedata(GtkMenuItem *item, gpointer data)
4086 char *str = (char *)g_object_get_data(G_OBJECT(item), "user-data");
4091 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
4093 struct gui_data *inst = (struct gui_data *)data;
4094 struct sesslist sesslist;
4097 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
4098 (GtkCallback)gtk_widget_destroy, NULL);
4100 get_sesslist(&sesslist, TRUE);
4101 /* skip sesslist.sessions[0] == Default Settings */
4102 for (i = 1; i < sesslist.nsessions; i++) {
4103 GtkWidget *menuitem =
4104 gtk_menu_item_new_with_label(sesslist.sessions[i]);
4105 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4106 gtk_widget_show(menuitem);
4107 g_object_set_data(G_OBJECT(menuitem), "user-data",
4108 dupstr(sesslist.sessions[i]));
4109 g_signal_connect(G_OBJECT(menuitem), "activate",
4110 G_CALLBACK(saved_session_menuitem),
4112 g_signal_connect(G_OBJECT(menuitem), "destroy",
4113 G_CALLBACK(saved_session_freedata),
4116 if (sesslist.nsessions <= 1) {
4117 GtkWidget *menuitem =
4118 gtk_menu_item_new_with_label("(No sessions)");
4119 gtk_widget_set_sensitive(menuitem, FALSE);
4120 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
4121 gtk_widget_show(menuitem);
4123 get_sesslist(&sesslist, FALSE); /* free up */
4126 void set_window_icon(GtkWidget *window, const char *const *const *icon,
4129 #if GTK_CHECK_VERSION(2,0,0)
4134 GdkBitmap *iconmask;
4140 gtk_widget_realize(window);
4141 #if GTK_CHECK_VERSION(2,0,0)
4142 gtk_window_set_icon(GTK_WINDOW(window),
4143 gdk_pixbuf_new_from_xpm_data((const gchar **)icon[0]));
4145 iconpm = gdk_pixmap_create_from_xpm_d(gtk_widget_get_window(window),
4146 &iconmask, NULL, (gchar **)icon[0]);
4147 gdk_window_set_icon(gtk_widget_get_window(window), NULL, iconpm, iconmask);
4150 #if GTK_CHECK_VERSION(2,0,0)
4152 for (n = 0; n < n_icon; n++) {
4154 g_list_append(iconlist,
4155 gdk_pixbuf_new_from_xpm_data((const gchar **)
4158 gtk_window_set_icon_list(GTK_WINDOW(window), iconlist);
4162 void update_specials_menu(void *frontend)
4164 struct gui_data *inst = (struct gui_data *)frontend;
4166 const struct telnet_special *specials;
4169 specials = inst->back->get_specials(inst->backhandle);
4173 /* I believe this disposes of submenus too. */
4174 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
4175 (GtkCallback)gtk_widget_destroy, NULL);
4178 GtkWidget *menu = inst->specialsmenu;
4179 /* A lame "stack" for submenus that will do for now. */
4180 GtkWidget *saved_menu = NULL;
4182 for (i = 0; nesting > 0; i++) {
4183 GtkWidget *menuitem = NULL;
4184 switch (specials[i].code) {
4186 assert (nesting < 2);
4187 saved_menu = menu; /* XXX lame stacking */
4188 menu = gtk_menu_new();
4189 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4190 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
4191 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
4192 gtk_widget_show(menuitem);
4199 menu = saved_menu; /* XXX lame stacking */
4204 menuitem = gtk_menu_item_new();
4207 menuitem = gtk_menu_item_new_with_label(specials[i].name);
4208 g_object_set_data(G_OBJECT(menuitem), "user-data",
4209 GINT_TO_POINTER(specials[i].code));
4210 g_signal_connect(G_OBJECT(menuitem), "activate",
4211 G_CALLBACK(special_menuitem), inst);
4215 gtk_container_add(GTK_CONTAINER(menu), menuitem);
4216 gtk_widget_show(menuitem);
4219 gtk_widget_show(inst->specialsitem1);
4220 gtk_widget_show(inst->specialsitem2);
4222 gtk_widget_hide(inst->specialsitem1);
4223 gtk_widget_hide(inst->specialsitem2);
4227 static void start_backend(struct gui_data *inst)
4229 extern Backend *select_backend(Conf *conf);
4234 inst->back = select_backend(inst->conf);
4236 error = inst->back->init((void *)inst, &inst->backhandle,
4238 conf_get_str(inst->conf, CONF_host),
4239 conf_get_int(inst->conf, CONF_port),
4241 conf_get_int(inst->conf, CONF_tcp_nodelay),
4242 conf_get_int(inst->conf, CONF_tcp_keepalives));
4245 char *msg = dupprintf("Unable to open connection to %s:\n%s",
4246 conf_get_str(inst->conf, CONF_host), error);
4247 inst->exited = TRUE;
4248 fatal_message_box(inst->window, msg);
4253 s = conf_get_str(inst->conf, CONF_wintitle);
4255 set_title_and_icon(inst, s, s);
4257 char *title = make_default_wintitle(realhost);
4258 set_title_and_icon(inst, title, title);
4263 inst->back->provide_logctx(inst->backhandle, inst->logctx);
4265 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
4268 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
4271 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4274 int pt_main(int argc, char **argv)
4276 extern int cfgbox(Conf *conf);
4277 struct gui_data *inst;
4279 setlocale(LC_CTYPE, "");
4282 * Create an instance structure and initialise to zeroes
4284 inst = snew(struct gui_data);
4285 memset(inst, 0, sizeof(*inst));
4286 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
4287 inst->busy_status = BUSY_NOT;
4288 inst->conf = conf_new();
4289 inst->wintitle = inst->icontitle = NULL;
4290 inst->quit_fn_scheduled = FALSE;
4291 inst->idle_fn_scheduled = FALSE;
4292 inst->drawtype = DRAWTYPE_DEFAULT;
4294 /* defer any child exit handling until we're ready to deal with
4296 block_signal(SIGCHLD, 1);
4298 inst->progname = argv[0];
4300 * Copy the original argv before letting gtk_init fiddle with
4301 * it. It will be required later.
4305 inst->gtkargvstart = snewn(argc-1, char *);
4306 for (i = 1; i < argc; i++)
4307 inst->gtkargvstart[i-1] = dupstr(argv[i]);
4309 gtk_init(&argc, &argv);
4310 inst->ngtkargs = oldargc - argc;
4313 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
4314 read_dupsession_data(inst, inst->conf, argv[1]);
4315 /* Splatter this argument so it doesn't clutter a ps listing */
4316 smemclr(argv[1], strlen(argv[1]));
4318 /* By default, we bring up the config dialog, rather than launching
4319 * a session. This gets set to TRUE if something happens to change
4320 * that (e.g., a hostname is specified on the command-line). */
4321 int allow_launch = FALSE;
4322 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
4323 exit(1); /* pre-defaults pass to get -class */
4324 do_defaults(NULL, inst->conf);
4325 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
4326 exit(1); /* post-defaults, do everything */
4328 cmdline_run_saved(inst->conf);
4331 allow_launch = TRUE;
4333 if ((!allow_launch || !conf_launchable(inst->conf)) &&
4334 !cfgbox(inst->conf))
4335 exit(0); /* config box hit Cancel */
4338 if (!compound_text_atom)
4339 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
4340 if (!utf8_string_atom)
4341 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
4343 inst->area = gtk_drawing_area_new();
4345 #if GTK_CHECK_VERSION(2,0,0)
4346 inst->imc = gtk_im_multicontext_new();
4350 char *errmsg = setup_fonts_ucs(inst);
4352 fprintf(stderr, "%s: %s\n", appname, errmsg);
4358 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4360 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
4362 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
4363 winclass, winclass);
4367 * Set up the colour map.
4369 palette_reset(inst);
4371 inst->width = conf_get_int(inst->conf, CONF_width);
4372 inst->height = conf_get_int(inst->conf, CONF_height);
4373 cache_conf_values(inst);
4376 int w = inst->font_width * inst->width + 2*inst->window_border;
4377 int h = inst->font_height * inst->height + 2*inst->window_border;
4378 #if GTK_CHECK_VERSION(2,0,0)
4379 gtk_widget_set_size_request(inst->area, w, h);
4381 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), w, h);
4384 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
4385 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
4386 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
4388 * We always create the scrollbar; it remains invisible if
4389 * unwanted, so we can pop it up quickly if it suddenly becomes
4392 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
4393 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4394 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
4395 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
4396 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
4398 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
4400 set_geom_hints(inst);
4402 gtk_widget_show(inst->area);
4403 if (conf_get_int(inst->conf, CONF_scrollbar))
4404 gtk_widget_show(inst->sbar);
4406 gtk_widget_hide(inst->sbar);
4407 gtk_widget_show(GTK_WIDGET(inst->hbox));
4409 #if GTK_CHECK_VERSION(2,0,0)
4410 if (inst->geometry) {
4411 gtk_window_parse_geometry(GTK_WINDOW(inst->window), inst->geometry);
4415 int x = inst->xpos, y = inst->ypos;
4417 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
4418 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
4419 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
4420 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
4421 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
4425 g_signal_connect(G_OBJECT(inst->window), "destroy",
4426 G_CALLBACK(destroy), inst);
4427 g_signal_connect(G_OBJECT(inst->window), "delete_event",
4428 G_CALLBACK(delete_window), inst);
4429 g_signal_connect(G_OBJECT(inst->window), "key_press_event",
4430 G_CALLBACK(key_event), inst);
4431 g_signal_connect(G_OBJECT(inst->window), "key_release_event",
4432 G_CALLBACK(key_event), inst);
4433 g_signal_connect(G_OBJECT(inst->window), "focus_in_event",
4434 G_CALLBACK(focus_event), inst);
4435 g_signal_connect(G_OBJECT(inst->window), "focus_out_event",
4436 G_CALLBACK(focus_event), inst);
4437 g_signal_connect(G_OBJECT(inst->area), "configure_event",
4438 G_CALLBACK(configure_area), inst);
4439 #if GTK_CHECK_VERSION(3,0,0)
4440 g_signal_connect(G_OBJECT(inst->area), "draw",
4441 G_CALLBACK(draw_area), inst);
4443 g_signal_connect(G_OBJECT(inst->area), "expose_event",
4444 G_CALLBACK(expose_area), inst);
4446 g_signal_connect(G_OBJECT(inst->area), "button_press_event",
4447 G_CALLBACK(button_event), inst);
4448 g_signal_connect(G_OBJECT(inst->area), "button_release_event",
4449 G_CALLBACK(button_event), inst);
4450 #if GTK_CHECK_VERSION(2,0,0)
4451 g_signal_connect(G_OBJECT(inst->area), "scroll_event",
4452 G_CALLBACK(scroll_event), inst);
4454 g_signal_connect(G_OBJECT(inst->area), "motion_notify_event",
4455 G_CALLBACK(motion_event), inst);
4456 g_signal_connect(G_OBJECT(inst->area), "selection_received",
4457 G_CALLBACK(selection_received), inst);
4458 g_signal_connect(G_OBJECT(inst->area), "selection_get",
4459 G_CALLBACK(selection_get), inst);
4460 g_signal_connect(G_OBJECT(inst->area), "selection_clear_event",
4461 G_CALLBACK(selection_clear), inst);
4462 #if GTK_CHECK_VERSION(2,0,0)
4463 g_signal_connect(G_OBJECT(inst->imc), "commit",
4464 G_CALLBACK(input_method_commit_event), inst);
4466 if (conf_get_int(inst->conf, CONF_scrollbar))
4467 g_signal_connect(G_OBJECT(inst->sbar_adjust), "value_changed",
4468 G_CALLBACK(scrollbar_moved), inst);
4469 gtk_widget_add_events(GTK_WIDGET(inst->area),
4470 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
4471 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
4472 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
4475 extern const char *const *const main_icon[];
4476 extern const int n_main_icon;
4477 set_window_icon(inst->window, main_icon, n_main_icon);
4480 gtk_widget_show(inst->window);
4482 set_window_background(inst);
4485 * Set up the Ctrl+rightclick context menu.
4488 GtkWidget *menuitem;
4490 extern const int use_event_log, new_session, saved_sessions;
4492 inst->menu = gtk_menu_new();
4494 #define MKMENUITEM(title, func) do \
4496 menuitem = gtk_menu_item_new_with_label(title); \
4497 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4498 gtk_widget_show(menuitem); \
4499 g_signal_connect(G_OBJECT(menuitem), "activate", \
4500 G_CALLBACK(func), inst); \
4503 #define MKSUBMENU(title) do \
4505 menuitem = gtk_menu_item_new_with_label(title); \
4506 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4507 gtk_widget_show(menuitem); \
4510 #define MKSEP() do \
4512 menuitem = gtk_menu_item_new(); \
4513 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
4514 gtk_widget_show(menuitem); \
4518 MKMENUITEM("New Session...", new_session_menuitem);
4519 MKMENUITEM("Restart Session", restart_session_menuitem);
4520 inst->restartitem = menuitem;
4521 gtk_widget_set_sensitive(inst->restartitem, FALSE);
4522 MKMENUITEM("Duplicate Session", dup_session_menuitem);
4523 if (saved_sessions) {
4524 inst->sessionsmenu = gtk_menu_new();
4525 /* sessionsmenu will be updated when it's invoked */
4526 /* XXX is this the right way to do dynamic menus in Gtk? */
4527 MKMENUITEM("Saved Sessions", update_savedsess_menu);
4528 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
4529 inst->sessionsmenu);
4532 MKMENUITEM("Change Settings...", change_settings_menuitem);
4535 MKMENUITEM("Event Log", event_log_menuitem);
4536 MKSUBMENU("Special Commands");
4537 inst->specialsmenu = gtk_menu_new();
4538 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
4539 inst->specialsitem1 = menuitem;
4541 inst->specialsitem2 = menuitem;
4542 gtk_widget_hide(inst->specialsitem1);
4543 gtk_widget_hide(inst->specialsitem2);
4544 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
4545 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
4546 MKMENUITEM("Copy All", copy_all_menuitem);
4548 s = dupcat("About ", appname, NULL);
4549 MKMENUITEM(s, about_menuitem);
4556 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
4557 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
4558 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
4559 inst->blankcursor = make_mouse_ptr(inst, -1);
4560 inst->currcursor = inst->textcursor;
4561 show_mouseptr(inst, 1);
4563 inst->eventlogstuff = eventlogstuff_new();
4565 request_callback_notifications(notify_toplevel_callback, inst);
4567 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
4568 inst->logctx = log_init(inst, inst->conf);
4569 term_provide_logctx(inst->term, inst->logctx);
4573 term_size(inst->term, inst->height, inst->width,
4574 conf_get_int(inst->conf, CONF_savelines));
4576 start_backend(inst);
4578 ldisc_echoedit_update(inst->ldisc); /* cause ldisc to notice changes */
4580 /* now we're reday to deal with the child exit handler being
4582 block_signal(SIGCHLD, 0);
4585 * Block SIGPIPE: if we attempt Duplicate Session or similar
4586 * and it falls over in some way, we certainly don't want
4587 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
4588 * this _after_ (at least pterm) forks off its child process,
4589 * since the child wants SIGPIPE handled in the usual way.
4591 block_signal(SIGPIPE, 1);
4593 inst->exited = FALSE;