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 #include <gdk/gdkkeysyms.h>
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
28 #if GTK_CHECK_VERSION(2,0,0)
29 #include <gtk/gtkimmodule.h>
32 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
34 #define MAY_REFER_TO_GTK_IN_HEADERS
40 #define CAT2(x,y) x ## y
41 #define CAT(x,y) CAT2(x,y)
42 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
44 #if GTK_CHECK_VERSION(2,0,0)
45 ASSERT(sizeof(long) <= sizeof(gsize));
46 #define LONG_TO_GPOINTER(l) GSIZE_TO_POINTER(l)
47 #define GPOINTER_TO_LONG(p) GPOINTER_TO_SIZE(p)
49 ASSERT(sizeof(long) <= sizeof(gpointer));
50 #define LONG_TO_GPOINTER(l) ((gpointer)(long)(l))
51 #define GPOINTER_TO_LONG(p) ((long)(p))
54 /* Colours come in two flavours: configurable, and xterm-extended. */
55 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
56 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
58 GdkAtom compound_text_atom, utf8_string_atom;
60 extern char **pty_argv; /* declared in pty.c */
61 extern int use_pty_argv;
64 * Timers are global across all sessions (even if we were handling
65 * multiple sessions, which we aren't), so the current timer ID is
68 static guint timer_id = 0;
71 GtkWidget *window, *area, *sbar;
73 GtkAdjustment *sbar_adjust;
74 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
76 GtkWidget *sessionsmenu;
78 #if GTK_CHECK_VERSION(2,0,0)
81 unifont *fonts[4]; /* normal, bold, wide, widebold */
82 int xpos, ypos, gotpos, gravity;
83 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
84 GdkColor cols[NALLCOLOURS];
86 wchar_t *pastein_data;
89 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
90 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
91 int font_width, font_height;
96 guint toplevel_callback_idle_id;
97 int idle_fn_scheduled, quit_fn_scheduled;
102 int master_fd, master_func_id;
109 struct unicode_data ucsdata;
112 char *progname, **gtkargvstart;
114 guint32 input_event_time; /* Timestamp of the most recent input event. */
116 /* Cached things out of conf that we refer to a lot */
122 static void cache_conf_values(struct gui_data *inst)
124 inst->bold_style = conf_get_int(inst->conf, CONF_bold_style);
125 inst->window_border = conf_get_int(inst->conf, CONF_window_border);
126 inst->cursor_type = conf_get_int(inst->conf, CONF_cursor_type);
131 struct gui_data *inst;
134 static int send_raw_mouse;
136 static char *app_name = "pterm";
138 static void start_backend(struct gui_data *inst);
139 static void exit_callback(void *vinst);
141 char *x_get_default(const char *key)
143 return XGetDefault(GDK_DISPLAY(), app_name, key);
146 void connection_fatal(void *frontend, char *p, ...)
148 struct gui_data *inst = (struct gui_data *)frontend;
153 msg = dupvprintf(p, ap);
155 fatal_message_box(inst->window, msg);
158 queue_toplevel_callback(exit_callback, inst);
162 * Default settings that are specific to pterm.
164 FontSpec *platform_default_fontspec(const char *name)
166 if (!strcmp(name, "Font"))
167 return fontspec_new("server:fixed");
169 return fontspec_new("");
172 Filename *platform_default_filename(const char *name)
174 if (!strcmp(name, "LogFileName"))
175 return filename_from_str("putty.log");
177 return filename_from_str("");
180 char *platform_default_s(const char *name)
182 if (!strcmp(name, "SerialLine"))
183 return dupstr("/dev/ttyS0");
187 int platform_default_i(const char *name, int def)
189 if (!strcmp(name, "CloseOnExit"))
190 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
191 if (!strcmp(name, "WinNameAlways"))
192 return 0; /* X natively supports icon titles, so use 'em by default */
196 /* Dummy routine, only required in plink. */
197 void ldisc_update(void *frontend, int echo, int edit)
201 char *get_ttymode(void *frontend, const char *mode)
203 struct gui_data *inst = (struct gui_data *)frontend;
204 return term_get_ttymode(inst->term, mode);
207 int from_backend(void *frontend, int is_stderr, const char *data, int len)
209 struct gui_data *inst = (struct gui_data *)frontend;
210 return term_data(inst->term, is_stderr, data, len);
213 int from_backend_untrusted(void *frontend, const char *data, int len)
215 struct gui_data *inst = (struct gui_data *)frontend;
216 return term_data_untrusted(inst->term, data, len);
219 int from_backend_eof(void *frontend)
221 return TRUE; /* do respond to incoming EOF with outgoing */
224 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
226 struct gui_data *inst = (struct gui_data *)p->frontend;
228 ret = cmdline_get_passwd_input(p, in, inlen);
230 ret = term_get_userpass_input(inst->term, p, in, inlen);
234 void logevent(void *frontend, const char *string)
236 struct gui_data *inst = (struct gui_data *)frontend;
238 log_eventlog(inst->logctx, string);
240 logevent_dlg(inst->eventlogstuff, string);
243 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
245 struct gui_data *inst = (struct gui_data *)frontend;
248 return inst->font_height;
250 return inst->font_width;
254 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
255 * into a cooked one (SELECT, EXTEND, PASTE).
257 * In Unix, this is not configurable; the X button arrangement is
258 * rock-solid across all applications, everyone has a three-button
259 * mouse or a means of faking it, and there is no need to switch
260 * buttons around at all.
262 static Mouse_Button translate_button(Mouse_Button button)
264 /* struct gui_data *inst = (struct gui_data *)frontend; */
266 if (button == MBT_LEFT)
268 if (button == MBT_MIDDLE)
270 if (button == MBT_RIGHT)
272 return 0; /* shouldn't happen */
276 * Return the top-level GtkWindow associated with a particular
277 * front end instance.
279 void *get_window(void *frontend)
281 struct gui_data *inst = (struct gui_data *)frontend;
286 * Minimise or restore the window in response to a server-side
289 void set_iconic(void *frontend, int iconic)
292 * GTK 1.2 doesn't know how to do this.
294 #if GTK_CHECK_VERSION(2,0,0)
295 struct gui_data *inst = (struct gui_data *)frontend;
297 gtk_window_iconify(GTK_WINDOW(inst->window));
299 gtk_window_deiconify(GTK_WINDOW(inst->window));
304 * Move the window in response to a server-side request.
306 void move_window(void *frontend, int x, int y)
308 struct gui_data *inst = (struct gui_data *)frontend;
310 * I assume that when the GTK version of this call is available
311 * we should use it. Not sure how it differs from the GDK one,
314 #if GTK_CHECK_VERSION(2,0,0)
315 gtk_window_move(GTK_WINDOW(inst->window), x, y);
317 gdk_window_move(inst->window->window, x, y);
322 * Move the window to the top or bottom of the z-order in response
323 * to a server-side request.
325 void set_zorder(void *frontend, int top)
327 struct gui_data *inst = (struct gui_data *)frontend;
329 gdk_window_raise(inst->window->window);
331 gdk_window_lower(inst->window->window);
335 * Refresh the window in response to a server-side request.
337 void refresh_window(void *frontend)
339 struct gui_data *inst = (struct gui_data *)frontend;
340 term_invalidate(inst->term);
344 * Maximise or restore the window in response to a server-side
347 void set_zoomed(void *frontend, int zoomed)
350 * GTK 1.2 doesn't know how to do this.
352 #if GTK_CHECK_VERSION(2,0,0)
353 struct gui_data *inst = (struct gui_data *)frontend;
355 gtk_window_maximize(GTK_WINDOW(inst->window));
357 gtk_window_unmaximize(GTK_WINDOW(inst->window));
362 * Report whether the window is iconic, for terminal reports.
364 int is_iconic(void *frontend)
366 struct gui_data *inst = (struct gui_data *)frontend;
367 return !gdk_window_is_viewable(inst->window->window);
371 * Report the window's position, for terminal reports.
373 void get_window_pos(void *frontend, int *x, int *y)
375 struct gui_data *inst = (struct gui_data *)frontend;
377 * I assume that when the GTK version of this call is available
378 * we should use it. Not sure how it differs from the GDK one,
381 #if GTK_CHECK_VERSION(2,0,0)
382 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
384 gdk_window_get_position(inst->window->window, x, y);
389 * Report the window's pixel size, for terminal reports.
391 void get_window_pixels(void *frontend, int *x, int *y)
393 struct gui_data *inst = (struct gui_data *)frontend;
395 * I assume that when the GTK version of this call is available
396 * we should use it. Not sure how it differs from the GDK one,
399 #if GTK_CHECK_VERSION(2,0,0)
400 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
402 gdk_window_get_size(inst->window->window, x, y);
407 * Return the window or icon title.
409 char *get_window_title(void *frontend, int icon)
411 struct gui_data *inst = (struct gui_data *)frontend;
412 return icon ? inst->icontitle : inst->wintitle;
415 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
417 struct gui_data *inst = (struct gui_data *)data;
418 if (!inst->exited && conf_get_int(inst->conf, CONF_warn_on_close)) {
419 if (!reallyclose(inst))
425 static void update_mouseptr(struct gui_data *inst)
427 switch (inst->busy_status) {
429 if (!inst->mouseptr_visible) {
430 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
431 } else if (send_raw_mouse) {
432 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
434 gdk_window_set_cursor(inst->area->window, inst->textcursor);
437 case BUSY_WAITING: /* XXX can we do better? */
439 /* We always display these cursors. */
440 gdk_window_set_cursor(inst->area->window, inst->waitcursor);
447 static void show_mouseptr(struct gui_data *inst, int show)
449 if (!conf_get_int(inst->conf, CONF_hide_mouseptr))
451 inst->mouseptr_visible = show;
452 update_mouseptr(inst);
455 void draw_backing_rect(struct gui_data *inst)
457 GdkGC *gc = gdk_gc_new(inst->area->window);
458 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
459 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
460 inst->width * inst->font_width + 2*inst->window_border,
461 inst->height * inst->font_height + 2*inst->window_border);
465 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
467 struct gui_data *inst = (struct gui_data *)data;
468 int w, h, need_size = 0;
471 * See if the terminal size has changed, in which case we must
472 * let the terminal know.
474 w = (event->width - 2*inst->window_border) / inst->font_width;
475 h = (event->height - 2*inst->window_border) / inst->font_height;
476 if (w != inst->width || h != inst->height) {
479 conf_set_int(inst->conf, CONF_width, inst->width);
480 conf_set_int(inst->conf, CONF_height, inst->height);
485 gdk_pixmap_unref(inst->pixmap);
489 inst->pixmap = gdk_pixmap_new(widget->window,
490 (w * inst->font_width + 2*inst->window_border),
491 (h * inst->font_height + 2*inst->window_border), -1);
493 draw_backing_rect(inst);
495 if (need_size && inst->term) {
496 term_size(inst->term, h, w, conf_get_int(inst->conf, CONF_savelines));
500 term_invalidate(inst->term);
502 #if GTK_CHECK_VERSION(2,0,0)
503 gtk_im_context_set_client_window(inst->imc, widget->window);
509 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
511 struct gui_data *inst = (struct gui_data *)data;
514 * Pass the exposed rectangle to terminal.c, which will call us
515 * back to do the actual painting.
518 gdk_draw_pixmap(widget->window,
519 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
521 event->area.x, event->area.y,
522 event->area.x, event->area.y,
523 event->area.width, event->area.height);
528 #define KEY_PRESSED(k) \
529 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
531 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
533 struct gui_data *inst = (struct gui_data *)data;
535 wchar_t ucsoutput[2];
536 int ucsval, start, end, special, output_charset, use_ucsoutput;
537 int nethack_mode, app_keypad_mode;
539 /* Remember the timestamp. */
540 inst->input_event_time = event->time;
542 /* By default, nothing is generated. */
544 special = use_ucsoutput = FALSE;
545 output_charset = CS_ISO8859_1;
548 * If Alt is being released after typing an Alt+numberpad
549 * sequence, we should generate the code that was typed.
551 * Note that we only do this if more than one key was actually
552 * pressed - I don't think Alt+NumPad4 should be ^D or that
553 * Alt+NumPad3 should be ^C, for example. There's no serious
554 * inconvenience in having to type a zero before a single-digit
557 if (event->type == GDK_KEY_RELEASE) {
558 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
559 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
560 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
562 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
565 * FIXME: we might usefully try to do something clever here
566 * about interpreting the generated key code in a way that's
567 * appropriate to the line code page.
569 output[0] = inst->alt_keycode;
573 #if GTK_CHECK_VERSION(2,0,0)
574 if (gtk_im_context_filter_keypress(inst->imc, event))
579 if (event->type == GDK_KEY_PRESS) {
583 printf("keypress: keyval = %04x, state = %08x; string =",
584 event->keyval, event->state);
585 for (i = 0; event->string[i]; i++)
586 printf(" %02x", (unsigned char) event->string[i]);
592 * NYI: Compose key (!!! requires Unicode faff before even trying)
596 * If Alt has just been pressed, we start potentially
597 * accumulating an Alt+numberpad code. We do this by
598 * setting alt_keycode to -1 (nothing yet but plausible).
600 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
601 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
602 inst->alt_keycode = -1;
603 inst->alt_digits = 0;
604 goto done; /* this generates nothing else */
608 * If we're seeing a numberpad key press with Mod1 down,
609 * consider adding it to alt_keycode if that's sensible.
610 * Anything _else_ with Mod1 down cancels any possibility
611 * of an ALT keycode: we set alt_keycode to -2.
613 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
615 switch (event->keyval) {
616 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
617 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
618 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
619 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
620 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
621 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
622 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
623 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
624 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
625 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
628 inst->alt_keycode = -2; /* it's invalid */
631 printf("Adding digit %d to keycode %d", digit,
634 if (inst->alt_keycode == -1)
635 inst->alt_keycode = digit; /* one-digit code */
637 inst->alt_keycode = inst->alt_keycode * 10 + digit;
640 printf(" gives new code %d\n", inst->alt_keycode);
642 /* Having used this digit, we now do nothing more with it. */
648 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
651 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
652 term_scroll(inst->term, 0, -inst->height/2);
655 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
656 term_scroll(inst->term, 0, -1);
659 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
660 term_scroll(inst->term, 0, +inst->height/2);
663 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
664 term_scroll(inst->term, 0, +1);
669 * Neither does Shift-Ins.
671 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
677 use_ucsoutput = FALSE;
679 nethack_mode = conf_get_int(inst->conf, CONF_nethack_keypad);
680 app_keypad_mode = (inst->term->app_keypad_keys &&
681 !conf_get_int(inst->conf, CONF_no_applic_k));
683 /* ALT+things gives leading Escape. */
685 #if !GTK_CHECK_VERSION(2,0,0)
687 * In vanilla X, and hence also GDK 1.2, the string received
688 * as part of a keyboard event is assumed to be in
689 * ISO-8859-1. (Seems woefully shortsighted in i18n terms,
690 * but it's true: see the man page for XLookupString(3) for
693 output_charset = CS_ISO8859_1;
694 strncpy(output+1, event->string, lenof(output)-1);
697 * Most things can now be passed to
698 * gtk_im_context_filter_keypress without breaking anything
699 * below this point. An exception is the numeric keypad if
700 * we're in Nethack or application mode: the IM will eat
701 * numeric keypad presses if Num Lock is on, but we don't want
704 if (app_keypad_mode &&
705 (event->keyval == GDK_Num_Lock ||
706 event->keyval == GDK_KP_Divide ||
707 event->keyval == GDK_KP_Multiply ||
708 event->keyval == GDK_KP_Subtract ||
709 event->keyval == GDK_KP_Add ||
710 event->keyval == GDK_KP_Enter ||
711 event->keyval == GDK_KP_0 ||
712 event->keyval == GDK_KP_Insert ||
713 event->keyval == GDK_KP_1 ||
714 event->keyval == GDK_KP_End ||
715 event->keyval == GDK_KP_2 ||
716 event->keyval == GDK_KP_Down ||
717 event->keyval == GDK_KP_3 ||
718 event->keyval == GDK_KP_Page_Down ||
719 event->keyval == GDK_KP_4 ||
720 event->keyval == GDK_KP_Left ||
721 event->keyval == GDK_KP_5 ||
722 event->keyval == GDK_KP_Begin ||
723 event->keyval == GDK_KP_6 ||
724 event->keyval == GDK_KP_Right ||
725 event->keyval == GDK_KP_7 ||
726 event->keyval == GDK_KP_Home ||
727 event->keyval == GDK_KP_8 ||
728 event->keyval == GDK_KP_Up ||
729 event->keyval == GDK_KP_9 ||
730 event->keyval == GDK_KP_Page_Up ||
731 event->keyval == GDK_KP_Decimal ||
732 event->keyval == GDK_KP_Delete)) {
733 /* app keypad; do nothing */
734 } else if (nethack_mode &&
735 (event->keyval == GDK_KP_1 ||
736 event->keyval == GDK_KP_End ||
737 event->keyval == GDK_KP_2 ||
738 event->keyval == GDK_KP_Down ||
739 event->keyval == GDK_KP_3 ||
740 event->keyval == GDK_KP_Page_Down ||
741 event->keyval == GDK_KP_4 ||
742 event->keyval == GDK_KP_Left ||
743 event->keyval == GDK_KP_5 ||
744 event->keyval == GDK_KP_Begin ||
745 event->keyval == GDK_KP_6 ||
746 event->keyval == GDK_KP_Right ||
747 event->keyval == GDK_KP_7 ||
748 event->keyval == GDK_KP_Home ||
749 event->keyval == GDK_KP_8 ||
750 event->keyval == GDK_KP_Up ||
751 event->keyval == GDK_KP_9 ||
752 event->keyval == GDK_KP_Page_Up)) {
753 /* nethack mode; do nothing */
755 if (gtk_im_context_filter_keypress(inst->imc, event))
760 * GDK 2.0 arranges to have done some translation for us: in
761 * GDK 2.0, event->string is encoded in the current locale.
763 * So we use the standard C library function mbstowcs() to
764 * convert from the current locale into Unicode; from there
765 * we can convert to whatever PuTTY is currently working in.
766 * (In fact I convert straight back to UTF-8 from
767 * wide-character Unicode, for the sake of simplicity: that
768 * way we can still use exactly the same code to manipulate
769 * the string, such as prefixing ESC.)
771 output_charset = CS_UTF8;
773 wchar_t widedata[32];
778 wlen = mb_to_wc(DEFAULT_CODEPAGE, 0,
779 event->string, strlen(event->string),
780 widedata, lenof(widedata)-1);
783 ulen = charset_from_unicode(&wp, &wlen, output+1, lenof(output)-2,
784 CS_UTF8, NULL, NULL, 0);
785 output[1+ulen] = '\0';
790 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
791 ucsoutput[0] = '\033';
792 ucsoutput[1] = ucsval;
793 use_ucsoutput = TRUE;
796 output[lenof(output)-1] = '\0';
797 end = strlen(output);
799 if (event->state & GDK_MOD1_MASK) {
801 if (end == 1) end = 0;
805 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
806 if (!output[1] && event->keyval == '`' &&
807 (event->state & GDK_CONTROL_MASK)) {
809 use_ucsoutput = FALSE;
813 /* Control-Break sends a Break special to the backend */
814 if (event->keyval == GDK_Break &&
815 (event->state & GDK_CONTROL_MASK)) {
817 inst->back->special(inst->backhandle, TS_BRK);
821 /* We handle Return ourselves, because it needs to be flagged as
822 * special to ldisc. */
823 if (event->keyval == GDK_Return) {
825 use_ucsoutput = FALSE;
830 /* Control-2, Control-Space and Control-@ are NUL */
832 (event->keyval == ' ' || event->keyval == '2' ||
833 event->keyval == '@') &&
834 (event->state & (GDK_SHIFT_MASK |
835 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
837 use_ucsoutput = FALSE;
841 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
842 if (!output[1] && event->keyval == ' ' &&
843 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
844 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
846 output_charset = CS_ISO8859_1;
847 use_ucsoutput = FALSE;
851 /* We don't let GTK tell us what Backspace is! We know better. */
852 if (event->keyval == GDK_BackSpace &&
853 !(event->state & GDK_SHIFT_MASK)) {
854 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
856 use_ucsoutput = FALSE;
860 /* For Shift Backspace, do opposite of what is configured. */
861 if (event->keyval == GDK_BackSpace &&
862 (event->state & GDK_SHIFT_MASK)) {
863 output[1] = conf_get_int(inst->conf, CONF_bksp_is_delete) ?
865 use_ucsoutput = FALSE;
870 /* Shift-Tab is ESC [ Z */
871 if (event->keyval == GDK_ISO_Left_Tab ||
872 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
873 end = 1 + sprintf(output+1, "\033[Z");
874 use_ucsoutput = FALSE;
876 /* And normal Tab is Tab, if the keymap hasn't already told us.
877 * (Curiously, at least one version of the MacOS 10.5 X server
878 * doesn't translate Tab for us. */
879 if (event->keyval == GDK_Tab && end <= 1) {
885 * NetHack keypad mode.
889 switch (event->keyval) {
890 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
891 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
892 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
893 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
894 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
895 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
896 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
897 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
898 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
902 if (event->state & GDK_CONTROL_MASK)
904 else if (event->state & GDK_SHIFT_MASK)
908 use_ucsoutput = FALSE;
914 * Application keypad mode.
916 if (app_keypad_mode) {
918 switch (event->keyval) {
919 case GDK_Num_Lock: xkey = 'P'; break;
920 case GDK_KP_Divide: xkey = 'Q'; break;
921 case GDK_KP_Multiply: xkey = 'R'; break;
922 case GDK_KP_Subtract: xkey = 'S'; break;
924 * Keypad + is tricky. It covers a space that would
925 * be taken up on the VT100 by _two_ keys; so we
926 * let Shift select between the two. Worse still,
927 * in xterm function key mode we change which two...
930 if (conf_get_int(inst->conf, CONF_funky_type) == FUNKY_XTERM) {
931 if (event->state & GDK_SHIFT_MASK)
935 } else if (event->state & GDK_SHIFT_MASK)
940 case GDK_KP_Enter: xkey = 'M'; break;
941 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
942 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
943 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
944 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
945 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
946 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
947 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
948 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
949 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
950 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
951 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
954 if (inst->term->vt52_mode) {
955 if (xkey >= 'P' && xkey <= 'S')
956 end = 1 + sprintf(output+1, "\033%c", xkey);
958 end = 1 + sprintf(output+1, "\033?%c", xkey);
960 end = 1 + sprintf(output+1, "\033O%c", xkey);
961 use_ucsoutput = FALSE;
967 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
968 * for integer decimal nn.)
970 * We also deal with the weird ones here. Linux VCs replace F1
971 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
972 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
977 int funky_type = conf_get_int(inst->conf, CONF_funky_type);
978 switch (event->keyval) {
980 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
983 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
986 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
989 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
992 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
995 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
998 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
1001 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
1004 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
1007 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
1040 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
1041 case GDK_Home: case GDK_KP_Home:
1044 case GDK_Insert: case GDK_KP_Insert:
1047 case GDK_Delete: case GDK_KP_Delete:
1050 case GDK_End: case GDK_KP_End:
1053 case GDK_Page_Up: case GDK_KP_Page_Up:
1056 case GDK_Page_Down: case GDK_KP_Page_Down:
1060 /* Reorder edit keys to physical order */
1061 if (funky_type == FUNKY_VT400 && code <= 6)
1062 code = "\0\2\1\4\5\3\6"[code];
1064 if (inst->term->vt52_mode && code > 0 && code <= 6) {
1065 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
1066 use_ucsoutput = FALSE;
1070 if (funky_type == FUNKY_SCO && /* SCO function keys */
1071 code >= 11 && code <= 34) {
1072 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
1074 switch (event->keyval) {
1075 case GDK_F1: index = 0; break;
1076 case GDK_F2: index = 1; break;
1077 case GDK_F3: index = 2; break;
1078 case GDK_F4: index = 3; break;
1079 case GDK_F5: index = 4; break;
1080 case GDK_F6: index = 5; break;
1081 case GDK_F7: index = 6; break;
1082 case GDK_F8: index = 7; break;
1083 case GDK_F9: index = 8; break;
1084 case GDK_F10: index = 9; break;
1085 case GDK_F11: index = 10; break;
1086 case GDK_F12: index = 11; break;
1088 if (event->state & GDK_SHIFT_MASK) index += 12;
1089 if (event->state & GDK_CONTROL_MASK) index += 24;
1090 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
1091 use_ucsoutput = FALSE;
1094 if (funky_type == FUNKY_SCO && /* SCO small keypad */
1095 code >= 1 && code <= 6) {
1096 char codes[] = "HL.FIG";
1101 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
1103 use_ucsoutput = FALSE;
1106 if ((inst->term->vt52_mode || funky_type == FUNKY_VT100P) &&
1107 code >= 11 && code <= 24) {
1113 if (inst->term->vt52_mode)
1114 end = 1 + sprintf(output+1,
1115 "\x1B%c", code + 'P' - 11 - offt);
1117 end = 1 + sprintf(output+1,
1118 "\x1BO%c", code + 'P' - 11 - offt);
1119 use_ucsoutput = FALSE;
1122 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
1123 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
1124 use_ucsoutput = FALSE;
1127 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
1128 if (inst->term->vt52_mode)
1129 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
1131 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
1132 use_ucsoutput = FALSE;
1135 if ((code == 1 || code == 4) &&
1136 conf_get_int(inst->conf, CONF_rxvt_homeend)) {
1137 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
1138 use_ucsoutput = FALSE;
1142 end = 1 + sprintf(output+1, "\x1B[%d~", code);
1143 use_ucsoutput = FALSE;
1149 * Cursor keys. (This includes the numberpad cursor keys,
1150 * if we haven't already done them due to app keypad mode.)
1152 * Here we also process un-numlocked un-appkeypadded KP5,
1153 * which sends ESC [ G.
1157 switch (event->keyval) {
1158 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1159 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1160 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1161 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1162 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1165 end = 1 + format_arrow_key(output+1, inst->term, xkey,
1166 event->state & GDK_CONTROL_MASK);
1167 use_ucsoutput = FALSE;
1176 if (end-start > 0) {
1177 #ifdef KEY_DEBUGGING
1179 printf("generating sequence:");
1180 for (i = start; i < end; i++)
1181 printf(" %02x", (unsigned char) output[i]);
1187 * For special control characters, the character set
1188 * should never matter.
1190 output[end] = '\0'; /* NUL-terminate */
1192 ldisc_send(inst->ldisc, output+start, -2, 1);
1193 } else if (!inst->direct_to_font) {
1194 if (!use_ucsoutput) {
1196 lpage_send(inst->ldisc, output_charset, output+start,
1200 * We generated our own Unicode key data from the
1201 * keysym, so use that instead.
1204 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1208 * In direct-to-font mode, we just send the string
1209 * exactly as we received it.
1212 ldisc_send(inst->ldisc, output+start, end-start, 1);
1215 show_mouseptr(inst, 0);
1216 term_seen_key_event(inst->term);
1222 #if GTK_CHECK_VERSION(2,0,0)
1223 void input_method_commit_event(GtkIMContext *imc, gchar *str, gpointer data)
1225 struct gui_data *inst = (struct gui_data *)data;
1227 lpage_send(inst->ldisc, CS_UTF8, str, strlen(str), 1);
1228 show_mouseptr(inst, 0);
1229 term_seen_key_event(inst->term);
1233 gboolean button_internal(struct gui_data *inst, guint32 timestamp,
1234 GdkEventType type, guint ebutton, guint state,
1235 gdouble ex, gdouble ey)
1237 int shift, ctrl, alt, x, y, button, act, raw_mouse_mode;
1239 /* Remember the timestamp. */
1240 inst->input_event_time = timestamp;
1242 show_mouseptr(inst, 1);
1244 shift = state & GDK_SHIFT_MASK;
1245 ctrl = state & GDK_CONTROL_MASK;
1246 alt = state & GDK_MOD1_MASK;
1249 send_raw_mouse && !(shift && conf_get_int(inst->conf,
1250 CONF_mouse_override));
1252 if (!raw_mouse_mode) {
1253 if (ebutton == 4 && type == GDK_BUTTON_PRESS) {
1254 term_scroll(inst->term, 0, -5);
1257 if (ebutton == 5 && type == GDK_BUTTON_PRESS) {
1258 term_scroll(inst->term, 0, +5);
1263 if (ebutton == 3 && ctrl) {
1264 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1265 ebutton, timestamp);
1271 else if (ebutton == 2)
1272 button = MBT_MIDDLE;
1273 else if (ebutton == 3)
1275 else if (ebutton == 4)
1276 button = MBT_WHEEL_UP;
1277 else if (ebutton == 5)
1278 button = MBT_WHEEL_DOWN;
1280 return FALSE; /* don't even know what button! */
1283 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1284 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1285 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1286 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1287 default: return FALSE; /* don't know this event type */
1290 if (raw_mouse_mode && act != MA_CLICK && act != MA_RELEASE)
1291 return TRUE; /* we ignore these in raw mouse mode */
1293 x = (ex - inst->window_border) / inst->font_width;
1294 y = (ey - inst->window_border) / inst->font_height;
1296 term_mouse(inst->term, button, translate_button(button), act,
1297 x, y, shift, ctrl, alt);
1302 gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1304 struct gui_data *inst = (struct gui_data *)data;
1305 return button_internal(inst, event->time, event->type, event->button,
1306 event->state, event->x, event->y);
1309 #if GTK_CHECK_VERSION(2,0,0)
1311 * In GTK 2, mouse wheel events have become a new type of event.
1312 * This handler translates them back into button-4 and button-5
1313 * presses so that I don't have to change my old code too much :-)
1315 gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data)
1317 struct gui_data *inst = (struct gui_data *)data;
1320 if (event->direction == GDK_SCROLL_UP)
1322 else if (event->direction == GDK_SCROLL_DOWN)
1327 return button_internal(inst, event->time, GDK_BUTTON_PRESS,
1328 button, event->state, event->x, event->y);
1332 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1334 struct gui_data *inst = (struct gui_data *)data;
1335 int shift, ctrl, alt, x, y, button;
1337 /* Remember the timestamp. */
1338 inst->input_event_time = event->time;
1340 show_mouseptr(inst, 1);
1342 shift = event->state & GDK_SHIFT_MASK;
1343 ctrl = event->state & GDK_CONTROL_MASK;
1344 alt = event->state & GDK_MOD1_MASK;
1345 if (event->state & GDK_BUTTON1_MASK)
1347 else if (event->state & GDK_BUTTON2_MASK)
1348 button = MBT_MIDDLE;
1349 else if (event->state & GDK_BUTTON3_MASK)
1352 return FALSE; /* don't even know what button! */
1354 x = (event->x - inst->window_border) / inst->font_width;
1355 y = (event->y - inst->window_border) / inst->font_height;
1357 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1358 x, y, shift, ctrl, alt);
1363 void frontend_keypress(void *handle)
1365 struct gui_data *inst = (struct gui_data *)handle;
1368 * If our child process has exited but not closed, terminate on
1375 static void exit_callback(void *vinst)
1377 struct gui_data *inst = (struct gui_data *)vinst;
1378 int exitcode, close_on_exit;
1380 if (!inst->exited &&
1381 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1382 inst->exited = TRUE;
1383 close_on_exit = conf_get_int(inst->conf, CONF_close_on_exit);
1384 if (close_on_exit == FORCE_ON ||
1385 (close_on_exit == AUTO && exitcode == 0))
1386 gtk_main_quit(); /* just go */
1388 ldisc_free(inst->ldisc);
1391 inst->back->free(inst->backhandle);
1392 inst->backhandle = NULL;
1394 term_provide_resize_fn(inst->term, NULL, NULL);
1395 update_specials_menu(inst);
1396 gtk_widget_set_sensitive(inst->restartitem, TRUE);
1400 void notify_remote_exit(void *frontend)
1402 struct gui_data *inst = (struct gui_data *)frontend;
1404 queue_toplevel_callback(exit_callback, inst);
1407 static void notify_toplevel_callback(void *frontend);
1409 static gint quit_toplevel_callback_func(gpointer data)
1411 struct gui_data *inst = (struct gui_data *)data;
1413 notify_toplevel_callback(inst);
1415 inst->quit_fn_scheduled = FALSE;
1420 static gint idle_toplevel_callback_func(gpointer data)
1422 struct gui_data *inst = (struct gui_data *)data;
1424 if (gtk_main_level() > 1) {
1426 * We don't run the callbacks if we're in the middle of a
1427 * subsidiary gtk_main. Instead, ask for a callback when we
1428 * get back out of the subsidiary main loop (if we haven't
1429 * already arranged one), so we can reschedule ourself then.
1431 if (!inst->quit_fn_scheduled) {
1432 gtk_quit_add(2, quit_toplevel_callback_func, inst);
1433 inst->quit_fn_scheduled = TRUE;
1436 * And unschedule this idle function, since we've now done
1437 * everything we can until the innermost gtk_main has quit and
1438 * can reschedule us with a chance of actually taking action.
1440 if (inst->idle_fn_scheduled) { /* double-check, just in case */
1441 gtk_idle_remove(inst->toplevel_callback_idle_id);
1442 inst->idle_fn_scheduled = FALSE;
1445 run_toplevel_callbacks();
1449 * If we've emptied our toplevel callback queue, unschedule
1450 * ourself. Otherwise, leave ourselves pending so we'll be called
1451 * again to deal with more callbacks after another round of the
1454 if (!toplevel_callback_pending() && inst->idle_fn_scheduled) {
1455 gtk_idle_remove(inst->toplevel_callback_idle_id);
1456 inst->idle_fn_scheduled = FALSE;
1462 static void notify_toplevel_callback(void *frontend)
1464 struct gui_data *inst = (struct gui_data *)frontend;
1466 if (!inst->idle_fn_scheduled) {
1467 inst->toplevel_callback_idle_id =
1468 gtk_idle_add(idle_toplevel_callback_func, inst);
1469 inst->idle_fn_scheduled = TRUE;
1473 static gint timer_trigger(gpointer data)
1475 unsigned long now = GPOINTER_TO_LONG(data);
1476 unsigned long next, then;
1479 if (run_timers(now, &next)) {
1481 now = GETTICKCOUNT();
1482 if (now - then > next - then)
1486 timer_id = gtk_timeout_add(ticks, timer_trigger,
1487 LONG_TO_GPOINTER(next));
1491 * Never let a timer resume. If we need another one, we've
1492 * asked for it explicitly above.
1497 void timer_change_notify(unsigned long next)
1502 gtk_timeout_remove(timer_id);
1504 ticks = next - GETTICKCOUNT();
1506 ticks = 1; /* just in case */
1508 timer_id = gtk_timeout_add(ticks, timer_trigger,
1509 LONG_TO_GPOINTER(next));
1512 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1515 * We must process exceptional notifications before ordinary
1516 * readability ones, or we may go straight past the urgent
1519 if (condition & GDK_INPUT_EXCEPTION)
1520 select_result(sourcefd, 4);
1521 if (condition & GDK_INPUT_READ)
1522 select_result(sourcefd, 1);
1523 if (condition & GDK_INPUT_WRITE)
1524 select_result(sourcefd, 2);
1527 void destroy(GtkWidget *widget, gpointer data)
1532 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1534 struct gui_data *inst = (struct gui_data *)data;
1535 term_set_focus(inst->term, event->in);
1536 term_update(inst->term);
1537 show_mouseptr(inst, 1);
1541 void set_busy_status(void *frontend, int status)
1543 struct gui_data *inst = (struct gui_data *)frontend;
1544 inst->busy_status = status;
1545 update_mouseptr(inst);
1549 * set or clear the "raw mouse message" mode
1551 void set_raw_mouse_mode(void *frontend, int activate)
1553 struct gui_data *inst = (struct gui_data *)frontend;
1554 activate = activate && !conf_get_int(inst->conf, CONF_no_mouse_rep);
1555 send_raw_mouse = activate;
1556 update_mouseptr(inst);
1559 void request_resize(void *frontend, int w, int h)
1561 struct gui_data *inst = (struct gui_data *)frontend;
1562 int large_x, large_y;
1563 int offset_x, offset_y;
1565 GtkRequisition inner, outer;
1568 * This is a heinous hack dreamed up by the gnome-terminal
1569 * people to get around a limitation in gtk. The problem is
1570 * that in order to set the size correctly we really need to be
1571 * calling gtk_window_resize - but that needs to know the size
1572 * of the _whole window_, not the drawing area. So what we do
1573 * is to set an artificially huge size request on the drawing
1574 * area, recompute the resulting size request on the window,
1575 * and look at the difference between the two. That gives us
1576 * the x and y offsets we need to translate drawing area size
1577 * into window size for real, and then we call
1578 * gtk_window_resize.
1582 * We start by retrieving the current size of the whole window.
1583 * Adding a bit to _that_ will give us a value we can use as a
1584 * bogus size request which guarantees to be bigger than the
1585 * current size of the drawing area.
1587 get_window_pixels(inst, &large_x, &large_y);
1591 #if GTK_CHECK_VERSION(2,0,0)
1592 gtk_widget_set_size_request(inst->area, large_x, large_y);
1594 gtk_widget_set_usize(inst->area, large_x, large_y);
1596 gtk_widget_size_request(inst->area, &inner);
1597 gtk_widget_size_request(inst->window, &outer);
1599 offset_x = outer.width - inner.width;
1600 offset_y = outer.height - inner.height;
1602 area_x = inst->font_width * w + 2*inst->window_border;
1603 area_y = inst->font_height * h + 2*inst->window_border;
1606 * Now we must set the size request on the drawing area back to
1607 * something sensible before we commit the real resize. Best
1608 * way to do this, I think, is to set it to what the size is
1609 * really going to end up being.
1611 #if GTK_CHECK_VERSION(2,0,0)
1612 gtk_widget_set_size_request(inst->area, area_x, area_y);
1613 gtk_window_resize(GTK_WINDOW(inst->window),
1614 area_x + offset_x, area_y + offset_y);
1616 gtk_widget_set_usize(inst->area, area_x, area_y);
1617 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1619 * I can no longer remember what this call to
1620 * gtk_container_dequeue_resize_handler is for. It was
1621 * introduced in r3092 with no comment, and the commit log
1622 * message was uninformative. I'm _guessing_ its purpose is to
1623 * prevent gratuitous resize processing on the window given
1624 * that we're about to resize it anyway, but I have no idea
1625 * why that's so incredibly vital.
1627 * I've tried removing the call, and nothing seems to go
1628 * wrong. I've backtracked to r3092 and tried removing the
1629 * call there, and still nothing goes wrong. So I'm going to
1630 * adopt the working hypothesis that it's superfluous; I won't
1631 * actually remove it from the GTK 1.2 code, but I won't
1632 * attempt to replicate its functionality in the GTK 2 code
1635 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1636 gdk_window_resize(inst->window->window,
1637 area_x + offset_x, area_y + offset_y);
1641 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1643 gboolean success[1];
1645 inst->cols[n].red = r * 0x0101;
1646 inst->cols[n].green = g * 0x0101;
1647 inst->cols[n].blue = b * 0x0101;
1649 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1650 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1651 FALSE, TRUE, success);
1653 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1657 void set_window_background(struct gui_data *inst)
1659 if (inst->area && inst->area->window)
1660 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1661 if (inst->window && inst->window->window)
1662 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1665 void palette_set(void *frontend, int n, int r, int g, int b)
1667 struct gui_data *inst = (struct gui_data *)frontend;
1670 if (n >= NALLCOLOURS)
1672 real_palette_set(inst, n, r, g, b);
1674 /* Default Background changed. Ensure space between text area and
1675 * window border is redrawn */
1676 set_window_background(inst);
1677 draw_backing_rect(inst);
1678 gtk_widget_queue_draw(inst->area);
1682 void palette_reset(void *frontend)
1684 struct gui_data *inst = (struct gui_data *)frontend;
1685 /* This maps colour indices in inst->conf to those used in inst->cols. */
1686 static const int ww[] = {
1687 256, 257, 258, 259, 260, 261,
1688 0, 8, 1, 9, 2, 10, 3, 11,
1689 4, 12, 5, 13, 6, 14, 7, 15
1691 gboolean success[NALLCOLOURS];
1694 assert(lenof(ww) == NCFGCOLOURS);
1696 if (!inst->colmap) {
1697 inst->colmap = gdk_colormap_get_system();
1699 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1702 for (i = 0; i < NCFGCOLOURS; i++) {
1703 inst->cols[ww[i]].red =
1704 conf_get_int_int(inst->conf, CONF_colours, i*3+0) * 0x0101;
1705 inst->cols[ww[i]].green =
1706 conf_get_int_int(inst->conf, CONF_colours, i*3+1) * 0x0101;
1707 inst->cols[ww[i]].blue =
1708 conf_get_int_int(inst->conf, CONF_colours, i*3+2) * 0x0101;
1711 for (i = 0; i < NEXTCOLOURS; i++) {
1713 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1714 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1715 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1716 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1718 int shade = i - 216;
1719 shade = shade * 0x0a0a + 0x0808;
1720 inst->cols[i+16].red = inst->cols[i+16].green =
1721 inst->cols[i+16].blue = shade;
1725 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1726 FALSE, TRUE, success);
1727 for (i = 0; i < NALLCOLOURS; i++) {
1729 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1731 conf_get_int_int(inst->conf, CONF_colours, i*3+0),
1732 conf_get_int_int(inst->conf, CONF_colours, i*3+1),
1733 conf_get_int_int(inst->conf, CONF_colours, i*3+2));
1736 /* Since Default Background may have changed, ensure that space
1737 * between text area and window border is refreshed. */
1738 set_window_background(inst);
1739 if (inst->area && inst->area->window) {
1740 draw_backing_rect(inst);
1741 gtk_widget_queue_draw(inst->area);
1745 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1746 * do this before we start using cut buffers.
1748 void init_cutbuffers()
1750 unsigned char empty[] = "";
1751 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1752 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1753 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1754 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1755 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1756 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1757 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1758 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1759 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1760 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1761 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1762 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1763 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1764 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1765 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1766 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1769 /* Store the data in a cut-buffer. */
1770 void store_cutbuffer(char * ptr, int len)
1772 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1773 XRotateBuffers(GDK_DISPLAY(), 1);
1774 XStoreBytes(GDK_DISPLAY(), ptr, len);
1777 /* Retrieve data from a cut-buffer.
1778 * Returned data needs to be freed with XFree().
1780 char * retrieve_cutbuffer(int * nbytes)
1783 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1784 if (*nbytes <= 0 && ptr != 0) {
1791 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1793 struct gui_data *inst = (struct gui_data *)frontend;
1794 if (inst->pasteout_data)
1795 sfree(inst->pasteout_data);
1796 if (inst->pasteout_data_ctext)
1797 sfree(inst->pasteout_data_ctext);
1798 if (inst->pasteout_data_utf8)
1799 sfree(inst->pasteout_data_utf8);
1802 * Set up UTF-8 and compound text paste data. This only happens
1803 * if we aren't in direct-to-font mode using the D800 hack.
1805 if (!inst->direct_to_font) {
1806 const wchar_t *tmp = data;
1811 inst->pasteout_data_utf8 = snewn(len*6, char);
1812 inst->pasteout_data_utf8_len = len*6;
1813 inst->pasteout_data_utf8_len =
1814 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1815 inst->pasteout_data_utf8_len,
1816 CS_UTF8, NULL, NULL, 0);
1817 if (inst->pasteout_data_utf8_len == 0) {
1818 sfree(inst->pasteout_data_utf8);
1819 inst->pasteout_data_utf8 = NULL;
1821 inst->pasteout_data_utf8 =
1822 sresize(inst->pasteout_data_utf8,
1823 inst->pasteout_data_utf8_len + 1, char);
1824 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1828 * Now let Xlib convert our UTF-8 data into compound text.
1830 list[0] = inst->pasteout_data_utf8;
1831 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1832 XCompoundTextStyle, &tp) == 0) {
1833 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1834 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1835 inst->pasteout_data_ctext_len = tp.nitems;
1838 inst->pasteout_data_ctext = NULL;
1839 inst->pasteout_data_ctext_len = 0;
1842 inst->pasteout_data_utf8 = NULL;
1843 inst->pasteout_data_utf8_len = 0;
1844 inst->pasteout_data_ctext = NULL;
1845 inst->pasteout_data_ctext_len = 0;
1848 inst->pasteout_data = snewn(len*6, char);
1849 inst->pasteout_data_len = len*6;
1850 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1851 data, len, inst->pasteout_data,
1852 inst->pasteout_data_len,
1854 if (inst->pasteout_data_len == 0) {
1855 sfree(inst->pasteout_data);
1856 inst->pasteout_data = NULL;
1858 inst->pasteout_data =
1859 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1862 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1864 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1865 inst->input_event_time)) {
1866 #if GTK_CHECK_VERSION(2,0,0)
1867 gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY);
1869 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1870 GDK_SELECTION_TYPE_STRING, 1);
1871 if (inst->pasteout_data_ctext)
1872 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1873 compound_text_atom, 1);
1874 if (inst->pasteout_data_utf8)
1875 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1876 utf8_string_atom, 1);
1880 term_deselect(inst->term);
1883 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1884 guint info, guint time_stamp, gpointer data)
1886 struct gui_data *inst = (struct gui_data *)data;
1887 if (seldata->target == utf8_string_atom)
1888 gtk_selection_data_set(seldata, seldata->target, 8,
1889 (unsigned char *)inst->pasteout_data_utf8,
1890 inst->pasteout_data_utf8_len);
1891 else if (seldata->target == compound_text_atom)
1892 gtk_selection_data_set(seldata, seldata->target, 8,
1893 (unsigned char *)inst->pasteout_data_ctext,
1894 inst->pasteout_data_ctext_len);
1896 gtk_selection_data_set(seldata, seldata->target, 8,
1897 (unsigned char *)inst->pasteout_data,
1898 inst->pasteout_data_len);
1901 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1904 struct gui_data *inst = (struct gui_data *)data;
1906 term_deselect(inst->term);
1907 if (inst->pasteout_data)
1908 sfree(inst->pasteout_data);
1909 if (inst->pasteout_data_ctext)
1910 sfree(inst->pasteout_data_ctext);
1911 if (inst->pasteout_data_utf8)
1912 sfree(inst->pasteout_data_utf8);
1913 inst->pasteout_data = NULL;
1914 inst->pasteout_data_len = 0;
1915 inst->pasteout_data_ctext = NULL;
1916 inst->pasteout_data_ctext_len = 0;
1917 inst->pasteout_data_utf8 = NULL;
1918 inst->pasteout_data_utf8_len = 0;
1922 void request_paste(void *frontend)
1924 struct gui_data *inst = (struct gui_data *)frontend;
1926 * In Unix, pasting is asynchronous: all we can do at the
1927 * moment is to call gtk_selection_convert(), and when the data
1928 * comes back _then_ we can call term_do_paste().
1931 if (!inst->direct_to_font) {
1933 * First we attempt to retrieve the selection as a UTF-8
1934 * string (which we will convert to the correct code page
1935 * before sending to the session, of course). If that
1936 * fails, selection_received() will be informed and will
1937 * fall back to an ordinary string.
1939 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1941 inst->input_event_time);
1944 * If we're in direct-to-font mode, we disable UTF-8
1945 * pasting, and go straight to ordinary string data.
1947 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1948 GDK_SELECTION_TYPE_STRING,
1949 inst->input_event_time);
1953 gint idle_paste_func(gpointer data); /* forward ref */
1955 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1956 guint time, gpointer data)
1958 struct gui_data *inst = (struct gui_data *)data;
1962 int length, count, ret;
1963 int free_list_required = 0;
1964 int free_required = 0;
1967 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1969 * Failed to get a UTF-8 selection string. Try compound
1972 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1974 inst->input_event_time);
1978 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1980 * Failed to get UTF-8 or compound text. Try an ordinary
1983 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1984 GDK_SELECTION_TYPE_STRING,
1985 inst->input_event_time);
1990 * If we have data, but it's not of a type we can deal with,
1991 * we have to ignore the data.
1993 if (seldata->length > 0 &&
1994 seldata->type != GDK_SELECTION_TYPE_STRING &&
1995 seldata->type != compound_text_atom &&
1996 seldata->type != utf8_string_atom)
2000 * If we have no data, try looking in a cut buffer.
2002 if (seldata->length <= 0) {
2003 text = retrieve_cutbuffer(&length);
2006 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
2007 * source, so use that as a de-facto standard. */
2008 charset = CS_ISO8859_1;
2012 * Convert COMPOUND_TEXT into UTF-8.
2014 if (seldata->type == compound_text_atom) {
2015 tp.value = seldata->data;
2016 tp.encoding = (Atom) seldata->type;
2017 tp.format = seldata->format;
2018 tp.nitems = seldata->length;
2019 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
2021 if (ret != 0 || count != 1) {
2023 * Compound text failed; fall back to STRING.
2025 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
2026 GDK_SELECTION_TYPE_STRING,
2027 inst->input_event_time);
2031 length = strlen(list[0]);
2033 free_list_required = 1;
2035 text = (char *)seldata->data;
2036 length = seldata->length;
2037 charset = (seldata->type == utf8_string_atom ?
2038 CS_UTF8 : inst->ucsdata.line_codepage);
2042 if (inst->pastein_data)
2043 sfree(inst->pastein_data);
2045 inst->pastein_data = snewn(length, wchar_t);
2046 inst->pastein_data_len = length;
2047 inst->pastein_data_len =
2048 mb_to_wc(charset, 0, text, length,
2049 inst->pastein_data, inst->pastein_data_len);
2051 term_do_paste(inst->term);
2053 if (free_list_required)
2054 XFreeStringList(list);
2059 void get_clip(void *frontend, wchar_t ** p, int *len)
2061 struct gui_data *inst = (struct gui_data *)frontend;
2064 *p = inst->pastein_data;
2065 *len = inst->pastein_data_len;
2069 static void set_window_titles(struct gui_data *inst)
2072 * We must always call set_icon_name after calling set_title,
2073 * since set_title will write both names. Irritating, but such
2076 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
2077 if (!conf_get_int(inst->conf, CONF_win_name_always))
2078 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
2081 void set_title(void *frontend, char *title)
2083 struct gui_data *inst = (struct gui_data *)frontend;
2084 sfree(inst->wintitle);
2085 inst->wintitle = dupstr(title);
2086 set_window_titles(inst);
2089 void set_icon(void *frontend, char *title)
2091 struct gui_data *inst = (struct gui_data *)frontend;
2092 sfree(inst->icontitle);
2093 inst->icontitle = dupstr(title);
2094 set_window_titles(inst);
2097 void set_title_and_icon(void *frontend, char *title, char *icon)
2099 struct gui_data *inst = (struct gui_data *)frontend;
2100 sfree(inst->wintitle);
2101 inst->wintitle = dupstr(title);
2102 sfree(inst->icontitle);
2103 inst->icontitle = dupstr(icon);
2104 set_window_titles(inst);
2107 void set_sbar(void *frontend, int total, int start, int page)
2109 struct gui_data *inst = (struct gui_data *)frontend;
2110 if (!conf_get_int(inst->conf, CONF_scrollbar))
2112 inst->sbar_adjust->lower = 0;
2113 inst->sbar_adjust->upper = total;
2114 inst->sbar_adjust->value = start;
2115 inst->sbar_adjust->page_size = page;
2116 inst->sbar_adjust->step_increment = 1;
2117 inst->sbar_adjust->page_increment = page/2;
2118 inst->ignore_sbar = TRUE;
2119 gtk_adjustment_changed(inst->sbar_adjust);
2120 inst->ignore_sbar = FALSE;
2123 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
2125 struct gui_data *inst = (struct gui_data *)data;
2127 if (!conf_get_int(inst->conf, CONF_scrollbar))
2129 if (!inst->ignore_sbar)
2130 term_scroll(inst->term, 1, (int)adj->value);
2133 void sys_cursor(void *frontend, int x, int y)
2136 * This is meaningless under X.
2141 * This is still called when mode==BELL_VISUAL, even though the
2142 * visual bell is handled entirely within terminal.c, because we
2143 * may want to perform additional actions on any kind of bell (for
2144 * example, taskbar flashing in Windows).
2146 void do_beep(void *frontend, int mode)
2148 if (mode == BELL_DEFAULT)
2152 int char_width(Context ctx, int uc)
2155 * Under X, any fixed-width font really _is_ fixed-width.
2156 * Double-width characters will be dealt with using a separate
2157 * font. For the moment we can simply return 1.
2159 * FIXME: but is that also true of Pango?
2164 Context get_ctx(void *frontend)
2166 struct gui_data *inst = (struct gui_data *)frontend;
2167 struct draw_ctx *dctx;
2169 if (!inst->area->window)
2172 dctx = snew(struct draw_ctx);
2174 dctx->gc = gdk_gc_new(inst->area->window);
2178 void free_ctx(Context ctx)
2180 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2181 /* struct gui_data *inst = dctx->inst; */
2182 GdkGC *gc = dctx->gc;
2188 * Draw a line of text in the window, at given character
2189 * coordinates, in given attributes.
2191 * We are allowed to fiddle with the contents of `text'.
2193 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2194 unsigned long attr, int lattr)
2196 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2197 struct gui_data *inst = dctx->inst;
2198 GdkGC *gc = dctx->gc;
2199 int ncombining, combining;
2200 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
2201 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
2203 if (attr & TATTR_COMBINING) {
2209 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
2210 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
2211 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
2216 if ((inst->bold_style & 2) && (attr & ATTR_BOLD)) {
2217 if (nfg < 16) nfg |= 8;
2218 else if (nfg >= 256) nfg |= 1;
2220 if ((inst->bold_style & 2) && (attr & ATTR_BLINK)) {
2221 if (nbg < 16) nbg |= 8;
2222 else if (nbg >= 256) nbg |= 1;
2224 if ((attr & TATTR_ACTCURS) && !monochrome) {
2229 fontid = shadow = 0;
2231 if (attr & ATTR_WIDE) {
2238 if ((attr & ATTR_BOLD) && (inst->bold_style & 1)) {
2245 if (!inst->fonts[fontid]) {
2248 * Fall back through font ids with subsets of this one's
2249 * set bits, in order.
2251 for (i = fontid; i-- > 0 ;) {
2253 continue; /* some other bit is set */
2254 if (inst->fonts[i]) {
2259 assert(inst->fonts[fontid]); /* we should at least have hit zero */
2262 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2264 if (x >= inst->term->cols)
2266 if (x + len*2*widefactor > inst->term->cols)
2267 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2275 r.x = x*inst->font_width+inst->window_border;
2276 r.y = y*inst->font_height+inst->window_border;
2277 r.width = rlen*widefactor*inst->font_width;
2278 r.height = inst->font_height;
2279 gdk_gc_set_clip_rectangle(gc, &r);
2282 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
2283 gdk_draw_rectangle(inst->pixmap, gc, 1,
2284 x*inst->font_width+inst->window_border,
2285 y*inst->font_height+inst->window_border,
2286 rlen*widefactor*inst->font_width, inst->font_height);
2288 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2289 for (combining = 0; combining < ncombining; combining++) {
2290 unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid],
2291 x*inst->font_width+inst->window_border,
2292 y*inst->font_height+inst->window_border+inst->fonts[0]->ascent,
2293 text + combining, len, widefactor > 1,
2294 bold, inst->font_width);
2297 if (attr & ATTR_UNDER) {
2298 int uheight = inst->fonts[0]->ascent + 1;
2299 if (uheight >= inst->font_height)
2300 uheight = inst->font_height - 1;
2301 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->window_border,
2302 y*inst->font_height + uheight + inst->window_border,
2303 (x+len)*widefactor*inst->font_width-1+inst->window_border,
2304 y*inst->font_height + uheight + inst->window_border);
2307 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2309 * I can't find any plausible StretchBlt equivalent in the
2310 * X server, so I'm going to do this the slow and painful
2311 * way. This will involve repeated calls to
2312 * gdk_draw_pixmap() to stretch the text horizontally. It's
2313 * O(N^2) in time and O(N) in network bandwidth, but you
2314 * try thinking of a better way. :-(
2317 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2318 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2319 x*inst->font_width+inst->window_border + 2*i,
2320 y*inst->font_height+inst->window_border,
2321 x*inst->font_width+inst->window_border + 2*i+1,
2322 y*inst->font_height+inst->window_border,
2323 len * widefactor * inst->font_width - i, inst->font_height);
2326 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2328 /* Now stretch vertically, in the same way. */
2329 if ((lattr & LATTR_MODE) == LATTR_BOT)
2333 for (i = 0; i < inst->font_height; i+=2) {
2334 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2335 x*inst->font_width+inst->window_border,
2336 y*inst->font_height+inst->window_border+dt*i+db,
2337 x*inst->font_width+inst->window_border,
2338 y*inst->font_height+inst->window_border+dt*(i+1),
2339 len * widefactor * inst->font_width, inst->font_height-i-1);
2345 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2346 unsigned long attr, int lattr)
2348 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2349 struct gui_data *inst = dctx->inst;
2350 GdkGC *gc = dctx->gc;
2353 do_text_internal(ctx, x, y, text, len, attr, lattr);
2355 if (attr & ATTR_WIDE) {
2361 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2363 if (x >= inst->term->cols)
2365 if (x + len*2*widefactor > inst->term->cols)
2366 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2370 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2371 x*inst->font_width+inst->window_border,
2372 y*inst->font_height+inst->window_border,
2373 x*inst->font_width+inst->window_border,
2374 y*inst->font_height+inst->window_border,
2375 len*widefactor*inst->font_width, inst->font_height);
2378 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2379 unsigned long attr, int lattr)
2381 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2382 struct gui_data *inst = dctx->inst;
2383 GdkGC *gc = dctx->gc;
2385 int active, passive, widefactor;
2387 if (attr & TATTR_PASCURS) {
2388 attr &= ~TATTR_PASCURS;
2392 if ((attr & TATTR_ACTCURS) && inst->cursor_type != 0) {
2393 attr &= ~TATTR_ACTCURS;
2397 do_text_internal(ctx, x, y, text, len, attr, lattr);
2399 if (attr & TATTR_COMBINING)
2402 if (attr & ATTR_WIDE) {
2408 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2410 if (x >= inst->term->cols)
2412 if (x + len*2*widefactor > inst->term->cols)
2413 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2417 if (inst->cursor_type == 0) {
2419 * An active block cursor will already have been done by
2420 * the above do_text call, so we only need to do anything
2424 gdk_gc_set_foreground(gc, &inst->cols[261]);
2425 gdk_draw_rectangle(inst->pixmap, gc, 0,
2426 x*inst->font_width+inst->window_border,
2427 y*inst->font_height+inst->window_border,
2428 len*widefactor*inst->font_width-1, inst->font_height-1);
2432 int startx, starty, dx, dy, length, i;
2436 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2437 char_width = 2*inst->font_width;
2439 char_width = inst->font_width;
2441 if (inst->cursor_type == 1) {
2442 uheight = inst->fonts[0]->ascent + 1;
2443 if (uheight >= inst->font_height)
2444 uheight = inst->font_height - 1;
2446 startx = x * inst->font_width + inst->window_border;
2447 starty = y * inst->font_height + inst->window_border + uheight;
2450 length = len * widefactor * char_width;
2453 if (attr & TATTR_RIGHTCURS)
2454 xadjust = char_width - 1;
2455 startx = x * inst->font_width + inst->window_border + xadjust;
2456 starty = y * inst->font_height + inst->window_border;
2459 length = inst->font_height;
2462 gdk_gc_set_foreground(gc, &inst->cols[261]);
2464 for (i = 0; i < length; i++) {
2466 gdk_draw_point(inst->pixmap, gc, startx, starty);
2471 } else if (active) {
2472 gdk_draw_line(inst->pixmap, gc, startx, starty,
2473 startx + (length-1) * dx, starty + (length-1) * dy);
2474 } /* else no cursor (e.g., blinked off) */
2477 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2478 x*inst->font_width+inst->window_border,
2479 y*inst->font_height+inst->window_border,
2480 x*inst->font_width+inst->window_border,
2481 y*inst->font_height+inst->window_border,
2482 len*widefactor*inst->font_width, inst->font_height);
2484 #if GTK_CHECK_VERSION(2,0,0)
2486 GdkRectangle cursorrect;
2487 cursorrect.x = x*inst->font_width+inst->window_border;
2488 cursorrect.y = y*inst->font_height+inst->window_border;
2489 cursorrect.width = len*widefactor*inst->font_width;
2490 cursorrect.height = inst->font_height;
2491 gtk_im_context_set_cursor_location(inst->imc, &cursorrect);
2496 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2499 * Truly hideous hack: GTK doesn't allow us to set the mouse
2500 * cursor foreground and background colours unless we've _also_
2501 * created our own cursor from bitmaps. Therefore, I need to
2502 * load the `cursor' font and draw glyphs from it on to
2503 * pixmaps, in order to construct my cursors with the fg and bg
2504 * I want. This is a gross hack, but it's more self-contained
2505 * than linking in Xlib to find the X window handle to
2506 * inst->area and calling XRecolorCursor, and it's more
2507 * futureproof than hard-coding the shapes as bitmap arrays.
2509 static GdkFont *cursor_font = NULL;
2510 GdkPixmap *source, *mask;
2512 GdkColor cfg = { 0, 65535, 65535, 65535 };
2513 GdkColor cbg = { 0, 0, 0, 0 };
2514 GdkColor dfg = { 1, 65535, 65535, 65535 };
2515 GdkColor dbg = { 0, 0, 0, 0 };
2518 gint lb, rb, wid, asc, desc, w, h, x, y;
2520 if (cursor_val == -2) {
2521 gdk_font_unref(cursor_font);
2525 if (cursor_val >= 0 && !cursor_font) {
2526 cursor_font = gdk_font_load("cursor");
2528 gdk_font_ref(cursor_font);
2532 * Get the text extent of the cursor in question. We use the
2533 * mask character for this, because it's typically slightly
2534 * bigger than the main character.
2536 if (cursor_val >= 0) {
2538 text[0] = (char)cursor_val + 1;
2539 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2540 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2546 source = gdk_pixmap_new(NULL, w, h, 1);
2547 mask = gdk_pixmap_new(NULL, w, h, 1);
2550 * Draw the mask character on the mask pixmap.
2552 gc = gdk_gc_new(mask);
2553 gdk_gc_set_foreground(gc, &dbg);
2554 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2555 if (cursor_val >= 0) {
2557 text[0] = (char)cursor_val + 1;
2558 gdk_gc_set_foreground(gc, &dfg);
2559 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2564 * Draw the main character on the source pixmap.
2566 gc = gdk_gc_new(source);
2567 gdk_gc_set_foreground(gc, &dbg);
2568 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2569 if (cursor_val >= 0) {
2571 text[0] = (char)cursor_val;
2572 gdk_gc_set_foreground(gc, &dfg);
2573 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2578 * Create the cursor.
2580 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2585 gdk_pixmap_unref(source);
2586 gdk_pixmap_unref(mask);
2591 void modalfatalbox(char *p, ...)
2594 fprintf(stderr, "FATAL ERROR: ");
2596 vfprintf(stderr, p, ap);
2598 fputc('\n', stderr);
2602 void cmdline_error(char *p, ...)
2605 fprintf(stderr, "%s: ", appname);
2607 vfprintf(stderr, p, ap);
2609 fputc('\n', stderr);
2613 char *get_x_display(void *frontend)
2615 return gdk_get_display();
2618 long get_windowid(void *frontend)
2620 struct gui_data *inst = (struct gui_data *)frontend;
2621 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2624 static void help(FILE *fp) {
2626 "pterm option summary:\n"
2628 " --display DISPLAY Specify X display to use (note '--')\n"
2629 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2630 " -fn FONT Normal text font\n"
2631 " -fb FONT Bold text font\n"
2632 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2633 " -sl LINES Number of lines of scrollback\n"
2634 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2635 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2636 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2637 " -T TITLE Window title\n"
2638 " -ut, +ut Do(default) or do not update utmp\n"
2639 " -ls, +ls Do(default) or do not make shell a login shell\n"
2640 " -sb, +sb Do(default) or do not display a scrollbar\n"
2641 " -log PATH Log all output to a file\n"
2642 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2643 " -xrm RESOURCE-STRING Set an X resource\n"
2644 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2645 ) < 0 || fflush(fp) < 0) {
2646 perror("output error");
2651 static void version(FILE *fp) {
2652 if(fprintf(fp, "%s: %s\n", appname, ver) < 0 || fflush(fp) < 0) {
2653 perror("output error");
2658 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2659 struct gui_data *inst, Conf *conf)
2665 * Macros to make argument handling easier. Note that because
2666 * they need to call `continue', they cannot be contained in
2667 * the usual do {...} while (0) wrapper to make them
2668 * syntactically single statements; hence it is not legal to
2669 * use one of these macros as an unbraced statement between
2672 #define EXPECTS_ARG { \
2673 if (--argc <= 0) { \
2675 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2680 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2682 while (--argc > 0) {
2687 * Shameless cheating. Debian requires all X terminal
2688 * emulators to support `-T title'; but
2689 * cmdline_process_param will eat -T (it means no-pty) and
2690 * complain that pterm doesn't support it. So, in pterm
2691 * only, we convert -T into -title.
2693 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2697 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2698 do_everything ? 1 : -1, conf);
2701 cmdline_error("option \"%s\" requires an argument", p);
2702 } else if (ret == 2) {
2703 --argc, ++argv; /* skip next argument */
2705 } else if (ret == 1) {
2709 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2713 fs = fontspec_new(val);
2714 conf_set_fontspec(conf, CONF_font, fs);
2717 } else if (!strcmp(p, "-fb")) {
2721 fs = fontspec_new(val);
2722 conf_set_fontspec(conf, CONF_boldfont, fs);
2725 } else if (!strcmp(p, "-fw")) {
2729 fs = fontspec_new(val);
2730 conf_set_fontspec(conf, CONF_widefont, fs);
2733 } else if (!strcmp(p, "-fwb")) {
2737 fs = fontspec_new(val);
2738 conf_set_fontspec(conf, CONF_wideboldfont, fs);
2741 } else if (!strcmp(p, "-cs")) {
2744 conf_set_str(conf, CONF_line_codepage, val);
2746 } else if (!strcmp(p, "-geometry")) {
2752 flags = XParseGeometry(val, &x, &y, &w, &h);
2753 if (flags & WidthValue)
2754 conf_set_int(conf, CONF_width, w);
2755 if (flags & HeightValue)
2756 conf_set_int(conf, CONF_height, h);
2758 if (flags & (XValue | YValue)) {
2761 inst->gotpos = TRUE;
2762 inst->gravity = ((flags & XNegative ? 1 : 0) |
2763 (flags & YNegative ? 2 : 0));
2766 } else if (!strcmp(p, "-sl")) {
2769 conf_set_int(conf, CONF_savelines, atoi(val));
2771 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2772 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2773 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2778 if (!gdk_color_parse(val, &col)) {
2780 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2784 index = (!strcmp(p, "-fg") ? 0 :
2785 !strcmp(p, "-bg") ? 2 :
2786 !strcmp(p, "-bfg") ? 1 :
2787 !strcmp(p, "-bbg") ? 3 :
2788 !strcmp(p, "-cfg") ? 4 :
2789 !strcmp(p, "-cbg") ? 5 : -1);
2790 assert(index != -1);
2791 conf_set_int_int(conf, CONF_colours, index*3+0, col.red / 256);
2792 conf_set_int_int(conf, CONF_colours, index*3+1,col.green/ 256);
2793 conf_set_int_int(conf, CONF_colours, index*3+2, col.blue/ 256);
2796 } else if (use_pty_argv && !strcmp(p, "-e")) {
2797 /* This option swallows all further arguments. */
2803 pty_argv = snewn(argc+1, char *);
2805 for (i = 0; i < argc; i++)
2806 pty_argv[i] = argv[i];
2807 pty_argv[argc] = NULL;
2808 break; /* finished command-line processing */
2810 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2813 } else if (!strcmp(p, "-title")) {
2816 conf_set_str(conf, CONF_wintitle, val);
2818 } else if (!strcmp(p, "-log")) {
2822 fn = filename_from_str(val);
2823 conf_set_filename(conf, CONF_logfilename, fn);
2824 conf_set_int(conf, CONF_logtype, LGTYP_DEBUG);
2827 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2829 conf_set_int(conf, CONF_stamp_utmp, 0);
2831 } else if (!strcmp(p, "-ut")) {
2833 conf_set_int(conf, CONF_stamp_utmp, 1);
2835 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2837 conf_set_int(conf, CONF_login_shell, 0);
2839 } else if (!strcmp(p, "-ls")) {
2841 conf_set_int(conf, CONF_login_shell, 1);
2843 } else if (!strcmp(p, "-nethack")) {
2845 conf_set_int(conf, CONF_nethack_keypad, 1);
2847 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2849 conf_set_int(conf, CONF_scrollbar, 0);
2851 } else if (!strcmp(p, "-sb")) {
2853 conf_set_int(conf, CONF_scrollbar, 1);
2855 } else if (!strcmp(p, "-name")) {
2859 } else if (!strcmp(p, "-xrm")) {
2861 provide_xrm_string(val);
2863 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2867 } else if(!strcmp(p, "-version") || !strcmp(p, "--version")) {
2871 } else if (!strcmp(p, "-pgpfp")) {
2875 } else if(p[0] != '-' && (!do_everything ||
2876 process_nonoption_arg(p, conf,
2882 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2889 int uxsel_input_add(int fd, int rwx) {
2891 if (rwx & 1) flags |= GDK_INPUT_READ;
2892 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2893 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2895 return gdk_input_add(fd, flags, fd_input_func, NULL);
2898 void uxsel_input_remove(int id) {
2899 gdk_input_remove(id);
2902 char *setup_fonts_ucs(struct gui_data *inst)
2904 int shadowbold = conf_get_int(inst->conf, CONF_shadowbold);
2905 int shadowboldoffset = conf_get_int(inst->conf, CONF_shadowboldoffset);
2910 fs = conf_get_fontspec(inst->conf, CONF_font);
2911 fonts[0] = multifont_create(inst->area, fs->name, FALSE, FALSE,
2912 shadowboldoffset, shadowbold);
2914 return dupprintf("unable to load font \"%s\"", fs->name);
2917 fs = conf_get_fontspec(inst->conf, CONF_boldfont);
2918 if (shadowbold || !fs->name[0]) {
2921 fonts[1] = multifont_create(inst->area, fs->name, FALSE, TRUE,
2922 shadowboldoffset, shadowbold);
2925 unifont_destroy(fonts[0]);
2926 return dupprintf("unable to load bold font \"%s\"", fs->name);
2930 fs = conf_get_fontspec(inst->conf, CONF_widefont);
2932 fonts[2] = multifont_create(inst->area, fs->name, TRUE, FALSE,
2933 shadowboldoffset, shadowbold);
2935 for (i = 0; i < 2; i++)
2937 unifont_destroy(fonts[i]);
2938 return dupprintf("unable to load wide font \"%s\"", fs->name);
2944 fs = conf_get_fontspec(inst->conf, CONF_wideboldfont);
2945 if (shadowbold || !fs->name[0]) {
2948 fonts[3] = multifont_create(inst->area, fs->name, TRUE, TRUE,
2949 shadowboldoffset, shadowbold);
2951 for (i = 0; i < 3; i++)
2953 unifont_destroy(fonts[i]);
2954 return dupprintf("unable to load wide bold font \"%s\"", fs->name);
2959 * Now we've got past all the possible error conditions, we can
2960 * actually update our state.
2963 for (i = 0; i < 4; i++) {
2965 unifont_destroy(inst->fonts[i]);
2966 inst->fonts[i] = fonts[i];
2969 inst->font_width = inst->fonts[0]->width;
2970 inst->font_height = inst->fonts[0]->height;
2972 inst->direct_to_font = init_ucs(&inst->ucsdata,
2973 conf_get_str(inst->conf, CONF_line_codepage),
2974 conf_get_int(inst->conf, CONF_utf8_override),
2975 inst->fonts[0]->public_charset,
2976 conf_get_int(inst->conf, CONF_vtmode));
2981 void set_geom_hints(struct gui_data *inst)
2984 geom.min_width = inst->font_width + 2*inst->window_border;
2985 geom.min_height = inst->font_height + 2*inst->window_border;
2986 geom.max_width = geom.max_height = -1;
2987 geom.base_width = 2*inst->window_border;
2988 geom.base_height = 2*inst->window_border;
2989 geom.width_inc = inst->font_width;
2990 geom.height_inc = inst->font_height;
2991 geom.min_aspect = geom.max_aspect = 0;
2992 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2993 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2994 GDK_HINT_RESIZE_INC);
2997 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2999 struct gui_data *inst = (struct gui_data *)data;
3000 term_clrsb(inst->term);
3003 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
3005 struct gui_data *inst = (struct gui_data *)data;
3006 term_pwron(inst->term, TRUE);
3008 ldisc_send(inst->ldisc, NULL, 0, 0);
3011 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
3013 struct gui_data *inst = (struct gui_data *)data;
3014 term_copyall(inst->term);
3017 void special_menuitem(GtkMenuItem *item, gpointer data)
3019 struct gui_data *inst = (struct gui_data *)data;
3020 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
3024 inst->back->special(inst->backhandle, code);
3027 void about_menuitem(GtkMenuItem *item, gpointer data)
3029 struct gui_data *inst = (struct gui_data *)data;
3030 about_box(inst->window);
3033 void event_log_menuitem(GtkMenuItem *item, gpointer data)
3035 struct gui_data *inst = (struct gui_data *)data;
3036 showeventlog(inst->eventlogstuff, inst->window);
3039 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
3041 /* This maps colour indices in inst->conf to those used in inst->cols. */
3042 static const int ww[] = {
3043 256, 257, 258, 259, 260, 261,
3044 0, 8, 1, 9, 2, 10, 3, 11,
3045 4, 12, 5, 13, 6, 14, 7, 15
3047 struct gui_data *inst = (struct gui_data *)data;
3049 Conf *oldconf, *newconf;
3050 int i, j, need_size;
3052 assert(lenof(ww) == NCFGCOLOURS);
3054 if (inst->reconfiguring)
3057 inst->reconfiguring = TRUE;
3059 title = dupcat(appname, " Reconfiguration", NULL);
3061 oldconf = inst->conf;
3062 newconf = conf_copy(inst->conf);
3064 if (do_config_box(title, newconf, 1,
3065 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
3066 inst->conf = newconf;
3068 /* Pass new config data to the logging module */
3069 log_reconfig(inst->logctx, inst->conf);
3071 * Flush the line discipline's edit buffer in the case
3072 * where local editing has just been disabled.
3075 ldisc_configure(inst->ldisc, inst->conf);
3076 ldisc_send(inst->ldisc, NULL, 0, 0);
3078 /* Pass new config data to the terminal */
3079 term_reconfig(inst->term, inst->conf);
3080 /* Pass new config data to the back end */
3082 inst->back->reconfig(inst->backhandle, inst->conf);
3084 cache_conf_values(inst);
3087 * Just setting inst->conf is sufficient to cause colour
3088 * setting changes to appear on the next ESC]R palette
3089 * reset. But we should also check whether any colour
3090 * settings have been changed, and revert the ones that have
3091 * to the new default, on the assumption that the user is
3092 * most likely to want an immediate update.
3094 for (i = 0; i < NCFGCOLOURS; i++) {
3095 for (j = 0; j < 3; j++)
3096 if (conf_get_int_int(oldconf, CONF_colours, i*3+j) !=
3097 conf_get_int_int(newconf, CONF_colours, i*3+j))
3100 real_palette_set(inst, ww[i],
3101 conf_get_int_int(newconf,CONF_colours,i*3+0),
3102 conf_get_int_int(newconf,CONF_colours,i*3+1),
3103 conf_get_int_int(newconf,CONF_colours,i*3+2));
3106 * If the default background has changed, we must
3107 * repaint the space in between the window border
3108 * and the text area.
3111 set_window_background(inst);
3112 draw_backing_rect(inst);
3118 * If the scrollbar needs to be shown, hidden, or moved
3119 * from one end to the other of the window, do so now.
3121 if (conf_get_int(oldconf, CONF_scrollbar) !=
3122 conf_get_int(newconf, CONF_scrollbar)) {
3123 if (conf_get_int(newconf, CONF_scrollbar))
3124 gtk_widget_show(inst->sbar);
3126 gtk_widget_hide(inst->sbar);
3128 if (conf_get_int(oldconf, CONF_scrollbar_on_left) !=
3129 conf_get_int(newconf, CONF_scrollbar_on_left)) {
3130 gtk_box_reorder_child(inst->hbox, inst->sbar,
3131 conf_get_int(newconf, CONF_scrollbar_on_left)
3136 * Change the window title, if required.
3138 if (strcmp(conf_get_str(oldconf, CONF_wintitle),
3139 conf_get_str(newconf, CONF_wintitle)))
3140 set_title(inst, conf_get_str(newconf, CONF_wintitle));
3141 set_window_titles(inst);
3144 * Redo the whole tangled fonts and Unicode mess if
3148 if (strcmp(conf_get_fontspec(oldconf, CONF_font)->name,
3149 conf_get_fontspec(newconf, CONF_font)->name) ||
3150 strcmp(conf_get_fontspec(oldconf, CONF_boldfont)->name,
3151 conf_get_fontspec(newconf, CONF_boldfont)->name) ||
3152 strcmp(conf_get_fontspec(oldconf, CONF_widefont)->name,
3153 conf_get_fontspec(newconf, CONF_widefont)->name) ||
3154 strcmp(conf_get_fontspec(oldconf, CONF_wideboldfont)->name,
3155 conf_get_fontspec(newconf, CONF_wideboldfont)->name) ||
3156 strcmp(conf_get_str(oldconf, CONF_line_codepage),
3157 conf_get_str(newconf, CONF_line_codepage)) ||
3158 conf_get_int(oldconf, CONF_utf8_override) !=
3159 conf_get_int(newconf, CONF_utf8_override) ||
3160 conf_get_int(oldconf, CONF_vtmode) !=
3161 conf_get_int(newconf, CONF_vtmode) ||
3162 conf_get_int(oldconf, CONF_shadowbold) !=
3163 conf_get_int(newconf, CONF_shadowbold) ||
3164 conf_get_int(oldconf, CONF_shadowboldoffset) !=
3165 conf_get_int(newconf, CONF_shadowboldoffset)) {
3166 char *errmsg = setup_fonts_ucs(inst);
3169 dupprintf("Could not change fonts in terminal window: %s\n",
3171 messagebox(inst->window, "Font setup error", msgboxtext,
3172 string_width("Could not change fonts in terminal window:"),
3183 * Resize the window.
3185 if (conf_get_int(oldconf, CONF_width) !=
3186 conf_get_int(newconf, CONF_width) ||
3187 conf_get_int(oldconf, CONF_height) !=
3188 conf_get_int(newconf, CONF_height) ||
3189 conf_get_int(oldconf, CONF_window_border) !=
3190 conf_get_int(newconf, CONF_window_border) ||
3192 set_geom_hints(inst);
3193 request_resize(inst, conf_get_int(newconf, CONF_width),
3194 conf_get_int(newconf, CONF_height));
3197 * The above will have caused a call to term_size() for
3198 * us if it happened. If the user has fiddled with only
3199 * the scrollback size, the above will not have
3200 * happened and we will need an explicit term_size()
3203 if (conf_get_int(oldconf, CONF_savelines) !=
3204 conf_get_int(newconf, CONF_savelines))
3205 term_size(inst->term, inst->term->rows, inst->term->cols,
3206 conf_get_int(newconf, CONF_savelines));
3209 term_invalidate(inst->term);
3212 * We do an explicit full redraw here to ensure the window
3213 * border has been redrawn as well as the text area.
3215 gtk_widget_queue_draw(inst->area);
3222 inst->reconfiguring = FALSE;
3225 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3228 * Re-execing ourself is not an exact science under Unix. I do
3229 * the best I can by using /proc/self/exe if available and by
3230 * assuming argv[0] can be found on $PATH if not.
3232 * Note that we also have to reconstruct the elements of the
3233 * original argv which gtk swallowed, since the user wants the
3234 * new session to appear on the same X display as the old one.
3242 * Collect the arguments with which to re-exec ourself.
3244 va_start(ap, fd_to_close);
3245 n = 2; /* progname and terminating NULL */
3246 n += inst->ngtkargs;
3247 while (va_arg(ap, char *) != NULL)
3251 args = snewn(n, char *);
3252 args[0] = inst->progname;
3254 for (i = 0; i < inst->ngtkargs; i++)
3255 args[i+1] = inst->gtkargvstart[i];
3258 va_start(ap, fd_to_close);
3259 while ((args[i++] = va_arg(ap, char *)) != NULL);
3265 * Do the double fork.
3279 } else if (pid2 > 0) {
3281 * First child has successfully forked second child. My
3282 * Work Here Is Done. Note the use of _exit rather than
3283 * exit: the latter appears to cause destroy messages
3284 * to be sent to the X server. I suspect gtk uses
3291 * If we reach here, we are the second child, so we now
3292 * actually perform the exec.
3294 if (fd_to_close >= 0)
3297 execv("/proc/self/exe", args);
3298 execvp(inst->progname, args);
3305 waitpid(pid, &status, 0);
3310 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3312 struct gui_data *inst = (struct gui_data *)gdata;
3314 * For this feature we must marshal conf and (possibly) pty_argv
3315 * into a byte stream, create a pipe, and send this byte stream
3316 * to the child through the pipe.
3318 int i, ret, sersize, size;
3323 if (pipe(pipefd) < 0) {
3328 size = sersize = conf_serialised_size(inst->conf);
3329 if (use_pty_argv && pty_argv) {
3330 for (i = 0; pty_argv[i]; i++)
3331 size += strlen(pty_argv[i]) + 1;
3334 data = snewn(size, char);
3335 conf_serialise(inst->conf, data);
3336 if (use_pty_argv && pty_argv) {
3338 for (i = 0; pty_argv[i]; i++) {
3339 strcpy(data + p, pty_argv[i]);
3340 p += strlen(pty_argv[i]) + 1;
3345 sprintf(option, "---[%d,%d]", pipefd[0], size);
3346 noncloexec(pipefd[0]);
3347 fork_and_exec_self(inst, pipefd[1], option, NULL);
3351 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3354 perror("write to pipe");
3359 int read_dupsession_data(struct gui_data *inst, Conf *conf, char *arg)
3361 int fd, i, ret, size, size_used;
3364 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3365 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3369 data = snewn(size, char);
3371 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3374 perror("read from pipe");
3376 } else if (i < size) {
3377 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3382 size_used = conf_deserialise(conf, data, size);
3383 if (use_pty_argv && size > size_used) {
3387 while (i < size && data[i]) i++;
3389 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3396 pty_argv = snewn(n+1, char *);
3402 while (i < size && data[i]) i++;
3405 pty_argv[n++] = dupstr(p);
3414 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3416 struct gui_data *inst = (struct gui_data *)data;
3418 fork_and_exec_self(inst, -1, NULL);
3421 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3423 struct gui_data *inst = (struct gui_data *)data;
3426 logevent(inst, "----- Session restarted -----");
3427 term_pwron(inst->term, FALSE);
3428 start_backend(inst);
3429 inst->exited = FALSE;
3433 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3435 struct gui_data *inst = (struct gui_data *)data;
3436 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3438 fork_and_exec_self(inst, -1, "-load", str, NULL);
3441 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3443 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3448 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3450 struct gui_data *inst = (struct gui_data *)data;
3451 struct sesslist sesslist;
3454 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3455 (GtkCallback)gtk_widget_destroy, NULL);
3457 get_sesslist(&sesslist, TRUE);
3458 /* skip sesslist.sessions[0] == Default Settings */
3459 for (i = 1; i < sesslist.nsessions; i++) {
3460 GtkWidget *menuitem =
3461 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3462 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3463 gtk_widget_show(menuitem);
3464 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3465 dupstr(sesslist.sessions[i]));
3466 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3467 GTK_SIGNAL_FUNC(saved_session_menuitem),
3469 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3470 GTK_SIGNAL_FUNC(saved_session_freedata),
3473 if (sesslist.nsessions <= 1) {
3474 GtkWidget *menuitem =
3475 gtk_menu_item_new_with_label("(No sessions)");
3476 gtk_widget_set_sensitive(menuitem, FALSE);
3477 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3478 gtk_widget_show(menuitem);
3480 get_sesslist(&sesslist, FALSE); /* free up */
3483 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3487 GdkBitmap *iconmask;
3488 #if GTK_CHECK_VERSION(2,0,0)
3496 gtk_widget_realize(window);
3497 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3498 NULL, (gchar **)icon[0]);
3499 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3501 #if GTK_CHECK_VERSION(2,0,0)
3503 for (n = 0; n < n_icon; n++) {
3505 g_list_append(iconlist,
3506 gdk_pixbuf_new_from_xpm_data((const gchar **)
3509 gdk_window_set_icon_list(window->window, iconlist);
3513 void update_specials_menu(void *frontend)
3515 struct gui_data *inst = (struct gui_data *)frontend;
3517 const struct telnet_special *specials;
3520 specials = inst->back->get_specials(inst->backhandle);
3524 /* I believe this disposes of submenus too. */
3525 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3526 (GtkCallback)gtk_widget_destroy, NULL);
3529 GtkWidget *menu = inst->specialsmenu;
3530 /* A lame "stack" for submenus that will do for now. */
3531 GtkWidget *saved_menu = NULL;
3533 for (i = 0; nesting > 0; i++) {
3534 GtkWidget *menuitem = NULL;
3535 switch (specials[i].code) {
3537 assert (nesting < 2);
3538 saved_menu = menu; /* XXX lame stacking */
3539 menu = gtk_menu_new();
3540 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3541 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3542 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3543 gtk_widget_show(menuitem);
3550 menu = saved_menu; /* XXX lame stacking */
3555 menuitem = gtk_menu_item_new();
3558 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3559 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3560 GINT_TO_POINTER(specials[i].code));
3561 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3562 GTK_SIGNAL_FUNC(special_menuitem), inst);
3566 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3567 gtk_widget_show(menuitem);
3570 gtk_widget_show(inst->specialsitem1);
3571 gtk_widget_show(inst->specialsitem2);
3573 gtk_widget_hide(inst->specialsitem1);
3574 gtk_widget_hide(inst->specialsitem2);
3578 static void start_backend(struct gui_data *inst)
3580 extern Backend *select_backend(Conf *conf);
3585 inst->back = select_backend(inst->conf);
3587 error = inst->back->init((void *)inst, &inst->backhandle,
3589 conf_get_str(inst->conf, CONF_host),
3590 conf_get_int(inst->conf, CONF_port),
3592 conf_get_int(inst->conf, CONF_tcp_nodelay),
3593 conf_get_int(inst->conf, CONF_tcp_keepalives));
3596 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3597 conf_get_str(inst->conf, CONF_host), error);
3598 inst->exited = TRUE;
3599 fatal_message_box(inst->window, msg);
3604 s = conf_get_str(inst->conf, CONF_wintitle);
3606 set_title_and_icon(inst, s, s);
3608 char *title = make_default_wintitle(realhost);
3609 set_title_and_icon(inst, title, title);
3614 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3616 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3619 ldisc_create(inst->conf, inst->term, inst->back, inst->backhandle,
3622 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3625 int pt_main(int argc, char **argv)
3627 extern int cfgbox(Conf *conf);
3628 struct gui_data *inst;
3630 setlocale(LC_CTYPE, "");
3633 * Create an instance structure and initialise to zeroes
3635 inst = snew(struct gui_data);
3636 memset(inst, 0, sizeof(*inst));
3637 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3638 inst->busy_status = BUSY_NOT;
3639 inst->conf = conf_new();
3640 inst->wintitle = inst->icontitle = NULL;
3641 inst->quit_fn_scheduled = FALSE;
3642 inst->idle_fn_scheduled = FALSE;
3644 /* defer any child exit handling until we're ready to deal with
3646 block_signal(SIGCHLD, 1);
3648 inst->progname = argv[0];
3650 * Copy the original argv before letting gtk_init fiddle with
3651 * it. It will be required later.
3655 inst->gtkargvstart = snewn(argc-1, char *);
3656 for (i = 1; i < argc; i++)
3657 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3659 gtk_init(&argc, &argv);
3660 inst->ngtkargs = oldargc - argc;
3663 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3664 read_dupsession_data(inst, inst->conf, argv[1]);
3665 /* Splatter this argument so it doesn't clutter a ps listing */
3666 smemclr(argv[1], strlen(argv[1]));
3668 /* By default, we bring up the config dialog, rather than launching
3669 * a session. This gets set to TRUE if something happens to change
3670 * that (e.g., a hostname is specified on the command-line). */
3671 int allow_launch = FALSE;
3672 if (do_cmdline(argc, argv, 0, &allow_launch, inst, inst->conf))
3673 exit(1); /* pre-defaults pass to get -class */
3674 do_defaults(NULL, inst->conf);
3675 if (do_cmdline(argc, argv, 1, &allow_launch, inst, inst->conf))
3676 exit(1); /* post-defaults, do everything */
3678 cmdline_run_saved(inst->conf);
3681 allow_launch = TRUE;
3683 if ((!allow_launch || !conf_launchable(inst->conf)) &&
3684 !cfgbox(inst->conf))
3685 exit(0); /* config box hit Cancel */
3688 if (!compound_text_atom)
3689 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3690 if (!utf8_string_atom)
3691 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3693 inst->area = gtk_drawing_area_new();
3695 #if GTK_CHECK_VERSION(2,0,0)
3696 inst->imc = gtk_im_multicontext_new();
3700 char *errmsg = setup_fonts_ucs(inst);
3702 fprintf(stderr, "%s: %s\n", appname, errmsg);
3708 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3710 const char *winclass = conf_get_str(inst->conf, CONF_winclass);
3712 gtk_window_set_wmclass(GTK_WINDOW(inst->window),
3713 winclass, winclass);
3717 * Set up the colour map.
3719 palette_reset(inst);
3721 inst->width = conf_get_int(inst->conf, CONF_width);
3722 inst->height = conf_get_int(inst->conf, CONF_height);
3723 cache_conf_values(inst);
3725 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3726 inst->font_width * inst->width + 2*inst->window_border,
3727 inst->font_height * inst->height + 2*inst->window_border);
3728 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3729 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3730 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3732 * We always create the scrollbar; it remains invisible if
3733 * unwanted, so we can pop it up quickly if it suddenly becomes
3736 if (conf_get_int(inst->conf, CONF_scrollbar_on_left))
3737 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3738 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3739 if (!conf_get_int(inst->conf, CONF_scrollbar_on_left))
3740 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3742 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3744 set_geom_hints(inst);
3746 gtk_widget_show(inst->area);
3747 if (conf_get_int(inst->conf, CONF_scrollbar))
3748 gtk_widget_show(inst->sbar);
3750 gtk_widget_hide(inst->sbar);
3751 gtk_widget_show(GTK_WIDGET(inst->hbox));
3754 int x = inst->xpos, y = inst->ypos;
3756 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3757 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3758 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3759 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3760 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3763 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3764 GTK_SIGNAL_FUNC(destroy), inst);
3765 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3766 GTK_SIGNAL_FUNC(delete_window), inst);
3767 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3768 GTK_SIGNAL_FUNC(key_event), inst);
3769 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3770 GTK_SIGNAL_FUNC(key_event), inst);
3771 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3772 GTK_SIGNAL_FUNC(focus_event), inst);
3773 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3774 GTK_SIGNAL_FUNC(focus_event), inst);
3775 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3776 GTK_SIGNAL_FUNC(configure_area), inst);
3777 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3778 GTK_SIGNAL_FUNC(expose_area), inst);
3779 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3780 GTK_SIGNAL_FUNC(button_event), inst);
3781 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3782 GTK_SIGNAL_FUNC(button_event), inst);
3783 #if GTK_CHECK_VERSION(2,0,0)
3784 gtk_signal_connect(GTK_OBJECT(inst->area), "scroll_event",
3785 GTK_SIGNAL_FUNC(scroll_event), inst);
3787 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3788 GTK_SIGNAL_FUNC(motion_event), inst);
3789 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3790 GTK_SIGNAL_FUNC(selection_received), inst);
3791 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3792 GTK_SIGNAL_FUNC(selection_get), inst);
3793 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3794 GTK_SIGNAL_FUNC(selection_clear), inst);
3795 #if GTK_CHECK_VERSION(2,0,0)
3796 g_signal_connect(G_OBJECT(inst->imc), "commit",
3797 G_CALLBACK(input_method_commit_event), inst);
3799 if (conf_get_int(inst->conf, CONF_scrollbar))
3800 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3801 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3802 gtk_widget_add_events(GTK_WIDGET(inst->area),
3803 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3804 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3805 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3808 extern const char *const *const main_icon[];
3809 extern const int n_main_icon;
3810 set_window_icon(inst->window, main_icon, n_main_icon);
3813 gtk_widget_show(inst->window);
3815 set_window_background(inst);
3818 * Set up the Ctrl+rightclick context menu.
3821 GtkWidget *menuitem;
3823 extern const int use_event_log, new_session, saved_sessions;
3825 inst->menu = gtk_menu_new();
3827 #define MKMENUITEM(title, func) do \
3829 menuitem = gtk_menu_item_new_with_label(title); \
3830 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3831 gtk_widget_show(menuitem); \
3832 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3833 GTK_SIGNAL_FUNC(func), inst); \
3836 #define MKSUBMENU(title) do \
3838 menuitem = gtk_menu_item_new_with_label(title); \
3839 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3840 gtk_widget_show(menuitem); \
3843 #define MKSEP() do \
3845 menuitem = gtk_menu_item_new(); \
3846 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3847 gtk_widget_show(menuitem); \
3851 MKMENUITEM("New Session...", new_session_menuitem);
3852 MKMENUITEM("Restart Session", restart_session_menuitem);
3853 inst->restartitem = menuitem;
3854 gtk_widget_set_sensitive(inst->restartitem, FALSE);
3855 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3856 if (saved_sessions) {
3857 inst->sessionsmenu = gtk_menu_new();
3858 /* sessionsmenu will be updated when it's invoked */
3859 /* XXX is this the right way to do dynamic menus in Gtk? */
3860 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3861 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3862 inst->sessionsmenu);
3865 MKMENUITEM("Change Settings...", change_settings_menuitem);
3868 MKMENUITEM("Event Log", event_log_menuitem);
3869 MKSUBMENU("Special Commands");
3870 inst->specialsmenu = gtk_menu_new();
3871 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3872 inst->specialsitem1 = menuitem;
3874 inst->specialsitem2 = menuitem;
3875 gtk_widget_hide(inst->specialsitem1);
3876 gtk_widget_hide(inst->specialsitem2);
3877 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3878 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3879 MKMENUITEM("Copy All", copy_all_menuitem);
3881 s = dupcat("About ", appname, NULL);
3882 MKMENUITEM(s, about_menuitem);
3889 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3890 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3891 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3892 inst->blankcursor = make_mouse_ptr(inst, -1);
3893 make_mouse_ptr(inst, -2); /* clean up cursor font */
3894 inst->currcursor = inst->textcursor;
3895 show_mouseptr(inst, 1);
3897 inst->eventlogstuff = eventlogstuff_new();
3899 request_callback_notifications(notify_toplevel_callback, inst);
3901 inst->term = term_init(inst->conf, &inst->ucsdata, inst);
3902 inst->logctx = log_init(inst, inst->conf);
3903 term_provide_logctx(inst->term, inst->logctx);
3907 term_size(inst->term, inst->height, inst->width,
3908 conf_get_int(inst->conf, CONF_savelines));
3910 start_backend(inst);
3912 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3914 /* now we're reday to deal with the child exit handler being
3916 block_signal(SIGCHLD, 0);
3919 * Block SIGPIPE: if we attempt Duplicate Session or similar
3920 * and it falls over in some way, we certainly don't want
3921 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3922 * this _after_ (at least pterm) forks off its child process,
3923 * since the child wants SIGPIPE handled in the usual way.
3925 block_signal(SIGPIPE, 1);
3927 inst->exited = FALSE;