2 * gtkwin.c: the main code that runs a PuTTY terminal emulator and
3 * backend in a GTK window.
18 #include <sys/types.h>
21 #include <gdk/gdkkeysyms.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
33 #define CAT2(x,y) x ## y
34 #define CAT(x,y) CAT2(x,y)
35 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
37 /* Colours come in two flavours: configurable, and xterm-extended. */
38 #define NCFGCOLOURS (lenof(((Config *)0)->colours))
39 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
40 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
42 GdkAtom compound_text_atom, utf8_string_atom;
44 extern char **pty_argv; /* declared in pty.c */
45 extern int use_pty_argv;
48 * Timers are global across all sessions (even if we were handling
49 * multiple sessions, which we aren't), so the current timer ID is
52 static guint timer_id = 0;
55 GtkWidget *window, *area, *sbar;
57 GtkAdjustment *sbar_adjust;
58 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
60 GtkWidget *sessionsmenu;
62 unifont *fonts[4]; /* normal, bold, wide, widebold */
63 int xpos, ypos, gotpos, gravity;
64 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
65 GdkColor cols[NALLCOLOURS];
67 wchar_t *pastein_data;
70 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
71 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
72 int font_width, font_height;
77 guint term_paste_idle_id;
80 char wintitle[sizeof(((Config *)0)->wintitle)];
81 char icontitle[sizeof(((Config *)0)->wintitle)];
82 int master_fd, master_func_id;
89 struct unicode_data ucsdata;
92 char *progname, **gtkargvstart;
94 guint32 input_event_time; /* Timestamp of the most recent input event. */
100 struct gui_data *inst;
103 static int send_raw_mouse;
105 static char *app_name = "pterm";
107 static void start_backend(struct gui_data *inst);
109 char *x_get_default(const char *key)
111 return XGetDefault(GDK_DISPLAY(), app_name, key);
114 void connection_fatal(void *frontend, char *p, ...)
116 struct gui_data *inst = (struct gui_data *)frontend;
121 msg = dupvprintf(p, ap);
124 fatal_message_box(inst->window, msg);
126 if (inst->cfg.close_on_exit == FORCE_ON)
131 * Default settings that are specific to pterm.
133 FontSpec platform_default_fontspec(const char *name)
136 if (!strcmp(name, "Font"))
137 strcpy(ret.name, "fixed");
143 Filename platform_default_filename(const char *name)
146 if (!strcmp(name, "LogFileName"))
147 strcpy(ret.path, "putty.log");
153 char *platform_default_s(const char *name)
155 if (!strcmp(name, "SerialLine"))
156 return dupstr("/dev/ttyS0");
160 int platform_default_i(const char *name, int def)
162 if (!strcmp(name, "CloseOnExit"))
163 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
164 if (!strcmp(name, "WinNameAlways"))
165 return 0; /* X natively supports icon titles, so use 'em by default */
169 void ldisc_update(void *frontend, int echo, int edit)
172 * This is a stub in pterm. If I ever produce a Unix
173 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
174 * then it will require some termios manoeuvring analogous to
175 * that in the Windows plink.c, but here it's meaningless.
179 char *get_ttymode(void *frontend, const char *mode)
181 struct gui_data *inst = (struct gui_data *)frontend;
182 return term_get_ttymode(inst->term, mode);
185 int from_backend(void *frontend, int is_stderr, const char *data, int len)
187 struct gui_data *inst = (struct gui_data *)frontend;
188 return term_data(inst->term, is_stderr, data, len);
191 int from_backend_untrusted(void *frontend, const char *data, int len)
193 struct gui_data *inst = (struct gui_data *)frontend;
194 return term_data_untrusted(inst->term, data, len);
197 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
199 struct gui_data *inst = (struct gui_data *)p->frontend;
201 ret = cmdline_get_passwd_input(p, in, inlen);
203 ret = term_get_userpass_input(inst->term, p, in, inlen);
207 void logevent(void *frontend, const char *string)
209 struct gui_data *inst = (struct gui_data *)frontend;
211 log_eventlog(inst->logctx, string);
213 logevent_dlg(inst->eventlogstuff, string);
216 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
218 struct gui_data *inst = (struct gui_data *)frontend;
221 return inst->font_height;
223 return inst->font_width;
227 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
228 * into a cooked one (SELECT, EXTEND, PASTE).
230 * In Unix, this is not configurable; the X button arrangement is
231 * rock-solid across all applications, everyone has a three-button
232 * mouse or a means of faking it, and there is no need to switch
233 * buttons around at all.
235 static Mouse_Button translate_button(Mouse_Button button)
237 /* struct gui_data *inst = (struct gui_data *)frontend; */
239 if (button == MBT_LEFT)
241 if (button == MBT_MIDDLE)
243 if (button == MBT_RIGHT)
245 return 0; /* shouldn't happen */
249 * Return the top-level GtkWindow associated with a particular
250 * front end instance.
252 void *get_window(void *frontend)
254 struct gui_data *inst = (struct gui_data *)frontend;
259 * Minimise or restore the window in response to a server-side
262 void set_iconic(void *frontend, int iconic)
265 * GTK 1.2 doesn't know how to do this.
267 #if GTK_CHECK_VERSION(2,0,0)
268 struct gui_data *inst = (struct gui_data *)frontend;
270 gtk_window_iconify(GTK_WINDOW(inst->window));
272 gtk_window_deiconify(GTK_WINDOW(inst->window));
277 * Move the window in response to a server-side request.
279 void move_window(void *frontend, int x, int y)
281 struct gui_data *inst = (struct gui_data *)frontend;
283 * I assume that when the GTK version of this call is available
284 * we should use it. Not sure how it differs from the GDK one,
287 #if GTK_CHECK_VERSION(2,0,0)
288 gtk_window_move(GTK_WINDOW(inst->window), x, y);
290 gdk_window_move(inst->window->window, x, y);
295 * Move the window to the top or bottom of the z-order in response
296 * to a server-side request.
298 void set_zorder(void *frontend, int top)
300 struct gui_data *inst = (struct gui_data *)frontend;
302 gdk_window_raise(inst->window->window);
304 gdk_window_lower(inst->window->window);
308 * Refresh the window in response to a server-side request.
310 void refresh_window(void *frontend)
312 struct gui_data *inst = (struct gui_data *)frontend;
313 term_invalidate(inst->term);
317 * Maximise or restore the window in response to a server-side
320 void set_zoomed(void *frontend, int zoomed)
323 * GTK 1.2 doesn't know how to do this.
325 #if GTK_CHECK_VERSION(2,0,0)
326 struct gui_data *inst = (struct gui_data *)frontend;
328 gtk_window_maximize(GTK_WINDOW(inst->window));
330 gtk_window_unmaximize(GTK_WINDOW(inst->window));
335 * Report whether the window is iconic, for terminal reports.
337 int is_iconic(void *frontend)
339 struct gui_data *inst = (struct gui_data *)frontend;
340 return !gdk_window_is_viewable(inst->window->window);
344 * Report the window's position, for terminal reports.
346 void get_window_pos(void *frontend, int *x, int *y)
348 struct gui_data *inst = (struct gui_data *)frontend;
350 * I assume that when the GTK version of this call is available
351 * we should use it. Not sure how it differs from the GDK one,
354 #if GTK_CHECK_VERSION(2,0,0)
355 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
357 gdk_window_get_position(inst->window->window, x, y);
362 * Report the window's pixel size, for terminal reports.
364 void get_window_pixels(void *frontend, int *x, int *y)
366 struct gui_data *inst = (struct gui_data *)frontend;
368 * I assume that when the GTK version of this call is available
369 * we should use it. Not sure how it differs from the GDK one,
372 #if GTK_CHECK_VERSION(2,0,0)
373 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
375 gdk_window_get_size(inst->window->window, x, y);
380 * Return the window or icon title.
382 char *get_window_title(void *frontend, int icon)
384 struct gui_data *inst = (struct gui_data *)frontend;
385 return icon ? inst->icontitle : inst->wintitle;
388 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
390 struct gui_data *inst = (struct gui_data *)data;
391 if (!inst->exited && inst->cfg.warn_on_close) {
392 if (!reallyclose(inst))
398 static void update_mouseptr(struct gui_data *inst)
400 switch (inst->busy_status) {
402 if (!inst->mouseptr_visible) {
403 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
404 } else if (send_raw_mouse) {
405 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
407 gdk_window_set_cursor(inst->area->window, inst->textcursor);
410 case BUSY_WAITING: /* XXX can we do better? */
412 /* We always display these cursors. */
413 gdk_window_set_cursor(inst->area->window, inst->waitcursor);
420 static void show_mouseptr(struct gui_data *inst, int show)
422 if (!inst->cfg.hide_mouseptr)
424 inst->mouseptr_visible = show;
425 update_mouseptr(inst);
428 void draw_backing_rect(struct gui_data *inst)
430 GdkGC *gc = gdk_gc_new(inst->area->window);
431 gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */
432 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
433 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
434 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
438 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
440 struct gui_data *inst = (struct gui_data *)data;
441 int w, h, need_size = 0;
444 * See if the terminal size has changed, in which case we must
445 * let the terminal know.
447 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
448 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
449 if (w != inst->width || h != inst->height) {
450 inst->cfg.width = inst->width = w;
451 inst->cfg.height = inst->height = h;
456 gdk_pixmap_unref(inst->pixmap);
460 inst->pixmap = gdk_pixmap_new(widget->window,
461 (inst->cfg.width * inst->font_width +
462 2*inst->cfg.window_border),
463 (inst->cfg.height * inst->font_height +
464 2*inst->cfg.window_border), -1);
466 draw_backing_rect(inst);
468 if (need_size && inst->term) {
469 term_size(inst->term, h, w, inst->cfg.savelines);
473 term_invalidate(inst->term);
478 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
480 struct gui_data *inst = (struct gui_data *)data;
483 * Pass the exposed rectangle to terminal.c, which will call us
484 * back to do the actual painting.
487 gdk_draw_pixmap(widget->window,
488 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
490 event->area.x, event->area.y,
491 event->area.x, event->area.y,
492 event->area.width, event->area.height);
497 #define KEY_PRESSED(k) \
498 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
500 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
502 struct gui_data *inst = (struct gui_data *)data;
504 wchar_t ucsoutput[2];
505 int ucsval, start, end, special, use_ucsoutput;
507 /* Remember the timestamp. */
508 inst->input_event_time = event->time;
510 /* By default, nothing is generated. */
512 special = use_ucsoutput = FALSE;
515 * If Alt is being released after typing an Alt+numberpad
516 * sequence, we should generate the code that was typed.
518 * Note that we only do this if more than one key was actually
519 * pressed - I don't think Alt+NumPad4 should be ^D or that
520 * Alt+NumPad3 should be ^C, for example. There's no serious
521 * inconvenience in having to type a zero before a single-digit
524 if (event->type == GDK_KEY_RELEASE &&
525 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
526 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
527 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
529 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
531 output[0] = inst->alt_keycode;
536 if (event->type == GDK_KEY_PRESS) {
540 printf("keypress: keyval = %04x, state = %08x; string =",
541 event->keyval, event->state);
542 for (i = 0; event->string[i]; i++)
543 printf(" %02x", (unsigned char) event->string[i]);
549 * NYI: Compose key (!!! requires Unicode faff before even trying)
553 * If Alt has just been pressed, we start potentially
554 * accumulating an Alt+numberpad code. We do this by
555 * setting alt_keycode to -1 (nothing yet but plausible).
557 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
558 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
559 inst->alt_keycode = -1;
560 inst->alt_digits = 0;
561 goto done; /* this generates nothing else */
565 * If we're seeing a numberpad key press with Mod1 down,
566 * consider adding it to alt_keycode if that's sensible.
567 * Anything _else_ with Mod1 down cancels any possibility
568 * of an ALT keycode: we set alt_keycode to -2.
570 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
572 switch (event->keyval) {
573 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
574 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
575 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
576 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
577 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
578 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
579 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
580 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
581 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
582 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
585 inst->alt_keycode = -2; /* it's invalid */
588 printf("Adding digit %d to keycode %d", digit,
591 if (inst->alt_keycode == -1)
592 inst->alt_keycode = digit; /* one-digit code */
594 inst->alt_keycode = inst->alt_keycode * 10 + digit;
597 printf(" gives new code %d\n", inst->alt_keycode);
599 /* Having used this digit, we now do nothing more with it. */
605 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
608 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
609 term_scroll(inst->term, 0, -inst->cfg.height/2);
612 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
613 term_scroll(inst->term, 0, -1);
616 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
617 term_scroll(inst->term, 0, +inst->cfg.height/2);
620 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
621 term_scroll(inst->term, 0, +1);
626 * Neither does Shift-Ins.
628 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
634 use_ucsoutput = FALSE;
636 /* ALT+things gives leading Escape. */
638 strncpy(output+1, event->string, 31);
639 if (!*event->string &&
640 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
641 ucsoutput[0] = '\033';
642 ucsoutput[1] = ucsval;
643 use_ucsoutput = TRUE;
647 end = strlen(output);
649 if (event->state & GDK_MOD1_MASK) {
651 if (end == 1) end = 0;
655 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
656 if (!event->string[0] && event->keyval == '`' &&
657 (event->state & GDK_CONTROL_MASK)) {
659 use_ucsoutput = FALSE;
663 /* Control-Break sends a Break special to the backend */
664 if (event->keyval == GDK_Break &&
665 (event->state & GDK_CONTROL_MASK)) {
667 inst->back->special(inst->backhandle, TS_BRK);
671 /* We handle Return ourselves, because it needs to be flagged as
672 * special to ldisc. */
673 if (event->keyval == GDK_Return) {
675 use_ucsoutput = FALSE;
680 /* Control-2, Control-Space and Control-@ are NUL */
681 if (!event->string[0] &&
682 (event->keyval == ' ' || event->keyval == '2' ||
683 event->keyval == '@') &&
684 (event->state & (GDK_SHIFT_MASK |
685 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
687 use_ucsoutput = FALSE;
691 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
692 if (!event->string[0] && event->keyval == ' ' &&
693 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
694 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
696 use_ucsoutput = FALSE;
700 /* We don't let GTK tell us what Backspace is! We know better. */
701 if (event->keyval == GDK_BackSpace &&
702 !(event->state & GDK_SHIFT_MASK)) {
703 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
704 use_ucsoutput = FALSE;
708 /* For Shift Backspace, do opposite of what is configured. */
709 if (event->keyval == GDK_BackSpace &&
710 (event->state & GDK_SHIFT_MASK)) {
711 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
712 use_ucsoutput = FALSE;
717 /* Shift-Tab is ESC [ Z */
718 if (event->keyval == GDK_ISO_Left_Tab ||
719 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
720 end = 1 + sprintf(output+1, "\033[Z");
721 use_ucsoutput = FALSE;
723 /* And normal Tab is Tab, if the keymap hasn't already told us.
724 * (Curiously, at least one version of the MacOS 10.5 X server
725 * doesn't translate Tab for us. */
726 if (event->keyval == GDK_Tab && end <= 1) {
732 * NetHack keypad mode.
734 if (inst->cfg.nethack_keypad) {
736 switch (event->keyval) {
737 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
738 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
739 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
740 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
741 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
742 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
743 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
744 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
745 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
749 if (event->state & GDK_CONTROL_MASK)
751 else if (event->state & GDK_SHIFT_MASK)
755 use_ucsoutput = FALSE;
761 * Application keypad mode.
763 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
765 switch (event->keyval) {
766 case GDK_Num_Lock: xkey = 'P'; break;
767 case GDK_KP_Divide: xkey = 'Q'; break;
768 case GDK_KP_Multiply: xkey = 'R'; break;
769 case GDK_KP_Subtract: xkey = 'S'; break;
771 * Keypad + is tricky. It covers a space that would
772 * be taken up on the VT100 by _two_ keys; so we
773 * let Shift select between the two. Worse still,
774 * in xterm function key mode we change which two...
777 if (inst->cfg.funky_type == FUNKY_XTERM) {
778 if (event->state & GDK_SHIFT_MASK)
782 } else if (event->state & GDK_SHIFT_MASK)
787 case GDK_KP_Enter: xkey = 'M'; break;
788 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
789 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
790 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
791 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
792 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
793 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
794 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
795 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
796 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
797 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
798 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
801 if (inst->term->vt52_mode) {
802 if (xkey >= 'P' && xkey <= 'S')
803 end = 1 + sprintf(output+1, "\033%c", xkey);
805 end = 1 + sprintf(output+1, "\033?%c", xkey);
807 end = 1 + sprintf(output+1, "\033O%c", xkey);
808 use_ucsoutput = FALSE;
814 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
815 * for integer decimal nn.)
817 * We also deal with the weird ones here. Linux VCs replace F1
818 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
819 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
824 switch (event->keyval) {
826 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
829 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
832 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
835 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
838 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
841 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
844 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
847 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
850 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
853 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
886 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
887 case GDK_Home: case GDK_KP_Home:
890 case GDK_Insert: case GDK_KP_Insert:
893 case GDK_Delete: case GDK_KP_Delete:
896 case GDK_End: case GDK_KP_End:
899 case GDK_Page_Up: case GDK_KP_Page_Up:
902 case GDK_Page_Down: case GDK_KP_Page_Down:
906 /* Reorder edit keys to physical order */
907 if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
908 code = "\0\2\1\4\5\3\6"[code];
910 if (inst->term->vt52_mode && code > 0 && code <= 6) {
911 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
912 use_ucsoutput = FALSE;
916 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
917 code >= 11 && code <= 34) {
918 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
920 switch (event->keyval) {
921 case GDK_F1: index = 0; break;
922 case GDK_F2: index = 1; break;
923 case GDK_F3: index = 2; break;
924 case GDK_F4: index = 3; break;
925 case GDK_F5: index = 4; break;
926 case GDK_F6: index = 5; break;
927 case GDK_F7: index = 6; break;
928 case GDK_F8: index = 7; break;
929 case GDK_F9: index = 8; break;
930 case GDK_F10: index = 9; break;
931 case GDK_F11: index = 10; break;
932 case GDK_F12: index = 11; break;
934 if (event->state & GDK_SHIFT_MASK) index += 12;
935 if (event->state & GDK_CONTROL_MASK) index += 24;
936 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
937 use_ucsoutput = FALSE;
940 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
941 code >= 1 && code <= 6) {
942 char codes[] = "HL.FIG";
947 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
949 use_ucsoutput = FALSE;
952 if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
953 code >= 11 && code <= 24) {
959 if (inst->term->vt52_mode)
960 end = 1 + sprintf(output+1,
961 "\x1B%c", code + 'P' - 11 - offt);
963 end = 1 + sprintf(output+1,
964 "\x1BO%c", code + 'P' - 11 - offt);
965 use_ucsoutput = FALSE;
968 if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
969 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
970 use_ucsoutput = FALSE;
973 if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
974 if (inst->term->vt52_mode)
975 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
977 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
978 use_ucsoutput = FALSE;
981 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
982 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
983 use_ucsoutput = FALSE;
987 end = 1 + sprintf(output+1, "\x1B[%d~", code);
988 use_ucsoutput = FALSE;
994 * Cursor keys. (This includes the numberpad cursor keys,
995 * if we haven't already done them due to app keypad mode.)
997 * Here we also process un-numlocked un-appkeypadded KP5,
998 * which sends ESC [ G.
1002 switch (event->keyval) {
1003 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1004 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1005 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1006 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1007 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1011 * The arrow keys normally do ESC [ A and so on. In
1012 * app cursor keys mode they do ESC O A instead.
1013 * Ctrl toggles the two modes.
1015 if (inst->term->vt52_mode) {
1016 end = 1 + sprintf(output+1, "\033%c", xkey);
1017 } else if (!inst->term->app_cursor_keys ^
1018 !(event->state & GDK_CONTROL_MASK)) {
1019 end = 1 + sprintf(output+1, "\033O%c", xkey);
1021 end = 1 + sprintf(output+1, "\033[%c", xkey);
1023 use_ucsoutput = FALSE;
1032 if (end-start > 0) {
1033 #ifdef KEY_DEBUGGING
1035 printf("generating sequence:");
1036 for (i = start; i < end; i++)
1037 printf(" %02x", (unsigned char) output[i]);
1043 * For special control characters, the character set
1044 * should never matter.
1046 output[end] = '\0'; /* NUL-terminate */
1048 ldisc_send(inst->ldisc, output+start, -2, 1);
1049 } else if (!inst->direct_to_font) {
1050 if (!use_ucsoutput) {
1052 * The stuff we've just generated is assumed to be
1053 * ISO-8859-1! This sounds insane, but `man
1054 * XLookupString' agrees: strings of this type
1055 * returned from the X server are hardcoded to
1056 * 8859-1. Strictly speaking we should be doing
1057 * this using some sort of GtkIMContext, which (if
1058 * we're lucky) would give us our data directly in
1059 * Unicode; but that's not supported in GTK 1.2 as
1060 * far as I can tell, and it's poorly documented
1061 * even in 2.0, so it'll have to wait.
1064 lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
1068 * We generated our own Unicode key data from the
1069 * keysym, so use that instead.
1072 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1076 * In direct-to-font mode, we just send the string
1077 * exactly as we received it.
1080 ldisc_send(inst->ldisc, output+start, end-start, 1);
1083 show_mouseptr(inst, 0);
1084 term_seen_key_event(inst->term);
1090 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1092 struct gui_data *inst = (struct gui_data *)data;
1093 int shift, ctrl, alt, x, y, button, act;
1095 /* Remember the timestamp. */
1096 inst->input_event_time = event->time;
1098 show_mouseptr(inst, 1);
1100 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
1101 term_scroll(inst->term, 0, -5);
1104 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
1105 term_scroll(inst->term, 0, +5);
1109 shift = event->state & GDK_SHIFT_MASK;
1110 ctrl = event->state & GDK_CONTROL_MASK;
1111 alt = event->state & GDK_MOD1_MASK;
1113 if (event->button == 3 && ctrl) {
1114 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1115 event->button, event->time);
1119 if (event->button == 1)
1121 else if (event->button == 2)
1122 button = MBT_MIDDLE;
1123 else if (event->button == 3)
1126 return FALSE; /* don't even know what button! */
1128 switch (event->type) {
1129 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1130 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1131 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1132 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1133 default: return FALSE; /* don't know this event type */
1136 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
1137 act != MA_CLICK && act != MA_RELEASE)
1138 return TRUE; /* we ignore these in raw mouse mode */
1140 x = (event->x - inst->cfg.window_border) / inst->font_width;
1141 y = (event->y - inst->cfg.window_border) / inst->font_height;
1143 term_mouse(inst->term, button, translate_button(button), act,
1144 x, y, shift, ctrl, alt);
1149 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1151 struct gui_data *inst = (struct gui_data *)data;
1152 int shift, ctrl, alt, x, y, button;
1154 /* Remember the timestamp. */
1155 inst->input_event_time = event->time;
1157 show_mouseptr(inst, 1);
1159 shift = event->state & GDK_SHIFT_MASK;
1160 ctrl = event->state & GDK_CONTROL_MASK;
1161 alt = event->state & GDK_MOD1_MASK;
1162 if (event->state & GDK_BUTTON1_MASK)
1164 else if (event->state & GDK_BUTTON2_MASK)
1165 button = MBT_MIDDLE;
1166 else if (event->state & GDK_BUTTON3_MASK)
1169 return FALSE; /* don't even know what button! */
1171 x = (event->x - inst->cfg.window_border) / inst->font_width;
1172 y = (event->y - inst->cfg.window_border) / inst->font_height;
1174 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1175 x, y, shift, ctrl, alt);
1180 void frontend_keypress(void *handle)
1182 struct gui_data *inst = (struct gui_data *)handle;
1185 * If our child process has exited but not closed, terminate on
1192 void notify_remote_exit(void *frontend)
1194 struct gui_data *inst = (struct gui_data *)frontend;
1197 if (!inst->exited &&
1198 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1199 inst->exited = TRUE;
1200 if (inst->cfg.close_on_exit == FORCE_ON ||
1201 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1202 gtk_main_quit(); /* just go */
1204 ldisc_free(inst->ldisc);
1208 inst->back->free(inst->backhandle);
1209 inst->backhandle = NULL;
1211 term_provide_resize_fn(inst->term, NULL, NULL);
1212 update_specials_menu(inst);
1214 gtk_widget_show(inst->restartitem);
1218 static gint timer_trigger(gpointer data)
1220 long now = GPOINTER_TO_INT(data);
1224 if (run_timers(now, &next)) {
1225 ticks = next - GETTICKCOUNT();
1226 timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger,
1227 GINT_TO_POINTER(next));
1231 * Never let a timer resume. If we need another one, we've
1232 * asked for it explicitly above.
1237 void timer_change_notify(long next)
1242 gtk_timeout_remove(timer_id);
1244 ticks = next - GETTICKCOUNT();
1246 ticks = 1; /* just in case */
1248 timer_id = gtk_timeout_add(ticks, timer_trigger,
1249 GINT_TO_POINTER(next));
1252 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1255 * We must process exceptional notifications before ordinary
1256 * readability ones, or we may go straight past the urgent
1259 if (condition & GDK_INPUT_EXCEPTION)
1260 select_result(sourcefd, 4);
1261 if (condition & GDK_INPUT_READ)
1262 select_result(sourcefd, 1);
1263 if (condition & GDK_INPUT_WRITE)
1264 select_result(sourcefd, 2);
1267 void destroy(GtkWidget *widget, gpointer data)
1272 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1274 struct gui_data *inst = (struct gui_data *)data;
1275 term_set_focus(inst->term, event->in);
1276 term_update(inst->term);
1277 show_mouseptr(inst, 1);
1281 void set_busy_status(void *frontend, int status)
1283 struct gui_data *inst = (struct gui_data *)frontend;
1284 inst->busy_status = status;
1285 update_mouseptr(inst);
1289 * set or clear the "raw mouse message" mode
1291 void set_raw_mouse_mode(void *frontend, int activate)
1293 struct gui_data *inst = (struct gui_data *)frontend;
1294 activate = activate && !inst->cfg.no_mouse_rep;
1295 send_raw_mouse = activate;
1296 update_mouseptr(inst);
1299 void request_resize(void *frontend, int w, int h)
1301 struct gui_data *inst = (struct gui_data *)frontend;
1302 int large_x, large_y;
1303 int offset_x, offset_y;
1305 GtkRequisition inner, outer;
1308 * This is a heinous hack dreamed up by the gnome-terminal
1309 * people to get around a limitation in gtk. The problem is
1310 * that in order to set the size correctly we really need to be
1311 * calling gtk_window_resize - but that needs to know the size
1312 * of the _whole window_, not the drawing area. So what we do
1313 * is to set an artificially huge size request on the drawing
1314 * area, recompute the resulting size request on the window,
1315 * and look at the difference between the two. That gives us
1316 * the x and y offsets we need to translate drawing area size
1317 * into window size for real, and then we call
1318 * gtk_window_resize.
1322 * We start by retrieving the current size of the whole window.
1323 * Adding a bit to _that_ will give us a value we can use as a
1324 * bogus size request which guarantees to be bigger than the
1325 * current size of the drawing area.
1327 get_window_pixels(inst, &large_x, &large_y);
1331 #if GTK_CHECK_VERSION(2,0,0)
1332 gtk_widget_set_size_request(inst->area, large_x, large_y);
1334 gtk_widget_set_usize(inst->area, large_x, large_y);
1336 gtk_widget_size_request(inst->area, &inner);
1337 gtk_widget_size_request(inst->window, &outer);
1339 offset_x = outer.width - inner.width;
1340 offset_y = outer.height - inner.height;
1342 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1343 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1346 * Now we must set the size request on the drawing area back to
1347 * something sensible before we commit the real resize. Best
1348 * way to do this, I think, is to set it to what the size is
1349 * really going to end up being.
1351 #if GTK_CHECK_VERSION(2,0,0)
1352 gtk_widget_set_size_request(inst->area, area_x, area_y);
1353 _gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1354 gtk_window_resize(GTK_WINDOW(inst->window),
1355 area_x + offset_x, area_y + offset_y);
1357 gtk_widget_set_usize(inst->area, area_x, area_y);
1358 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1359 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1360 gdk_window_resize(inst->window->window,
1361 area_x + offset_x, area_y + offset_y);
1365 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1367 gboolean success[1];
1369 inst->cols[n].red = r * 0x0101;
1370 inst->cols[n].green = g * 0x0101;
1371 inst->cols[n].blue = b * 0x0101;
1373 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1374 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1375 FALSE, TRUE, success);
1377 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1381 void set_window_background(struct gui_data *inst)
1383 if (inst->area && inst->area->window)
1384 gdk_window_set_background(inst->area->window, &inst->cols[258]);
1385 if (inst->window && inst->window->window)
1386 gdk_window_set_background(inst->window->window, &inst->cols[258]);
1389 void palette_set(void *frontend, int n, int r, int g, int b)
1391 struct gui_data *inst = (struct gui_data *)frontend;
1394 if (n > NALLCOLOURS)
1396 real_palette_set(inst, n, r, g, b);
1398 /* Default Background changed. Ensure space between text area and
1399 * window border is redrawn */
1400 set_window_background(inst);
1401 draw_backing_rect(inst);
1402 gtk_widget_queue_draw(inst->area);
1406 void palette_reset(void *frontend)
1408 struct gui_data *inst = (struct gui_data *)frontend;
1409 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1410 static const int ww[] = {
1411 256, 257, 258, 259, 260, 261,
1412 0, 8, 1, 9, 2, 10, 3, 11,
1413 4, 12, 5, 13, 6, 14, 7, 15
1415 gboolean success[NALLCOLOURS];
1418 assert(lenof(ww) == NCFGCOLOURS);
1420 if (!inst->colmap) {
1421 inst->colmap = gdk_colormap_get_system();
1423 gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS);
1426 for (i = 0; i < NCFGCOLOURS; i++) {
1427 inst->cols[ww[i]].red = inst->cfg.colours[i][0] * 0x0101;
1428 inst->cols[ww[i]].green = inst->cfg.colours[i][1] * 0x0101;
1429 inst->cols[ww[i]].blue = inst->cfg.colours[i][2] * 0x0101;
1432 for (i = 0; i < NEXTCOLOURS; i++) {
1434 int r = i / 36, g = (i / 6) % 6, b = i % 6;
1435 inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0;
1436 inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0;
1437 inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0;
1439 int shade = i - 216;
1440 shade = shade * 0x0a0a + 0x0808;
1441 inst->cols[i+16].red = inst->cols[i+16].green =
1442 inst->cols[i+16].blue = shade;
1446 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS,
1447 FALSE, TRUE, success);
1448 for (i = 0; i < NALLCOLOURS; i++) {
1450 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1451 appname, i, inst->cfg.colours[i][0],
1452 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1455 /* Since Default Background may have changed, ensure that space
1456 * between text area and window border is refreshed. */
1457 set_window_background(inst);
1458 if (inst->area && inst->area->window) {
1459 draw_backing_rect(inst);
1460 gtk_widget_queue_draw(inst->area);
1464 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1465 * do this before we start using cut buffers.
1467 void init_cutbuffers()
1469 unsigned char empty[] = "";
1470 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1471 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1472 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1473 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1474 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1475 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1476 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1477 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1478 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1479 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1480 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1481 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1482 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1483 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1484 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1485 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1488 /* Store the data in a cut-buffer. */
1489 void store_cutbuffer(char * ptr, int len)
1491 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1492 XRotateBuffers(GDK_DISPLAY(), 1);
1493 XStoreBytes(GDK_DISPLAY(), ptr, len);
1496 /* Retrieve data from a cut-buffer.
1497 * Returned data needs to be freed with XFree().
1499 char * retrieve_cutbuffer(int * nbytes)
1502 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1503 if (*nbytes <= 0 && ptr != 0) {
1510 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1512 struct gui_data *inst = (struct gui_data *)frontend;
1513 if (inst->pasteout_data)
1514 sfree(inst->pasteout_data);
1515 if (inst->pasteout_data_ctext)
1516 sfree(inst->pasteout_data_ctext);
1517 if (inst->pasteout_data_utf8)
1518 sfree(inst->pasteout_data_utf8);
1521 * Set up UTF-8 and compound text paste data. This only happens
1522 * if we aren't in direct-to-font mode using the D800 hack.
1524 if (!inst->direct_to_font) {
1525 wchar_t *tmp = data;
1530 inst->pasteout_data_utf8 = snewn(len*6, char);
1531 inst->pasteout_data_utf8_len = len*6;
1532 inst->pasteout_data_utf8_len =
1533 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1534 inst->pasteout_data_utf8_len,
1535 CS_UTF8, NULL, NULL, 0);
1536 if (inst->pasteout_data_utf8_len == 0) {
1537 sfree(inst->pasteout_data_utf8);
1538 inst->pasteout_data_utf8 = NULL;
1540 inst->pasteout_data_utf8 =
1541 sresize(inst->pasteout_data_utf8,
1542 inst->pasteout_data_utf8_len + 1, char);
1543 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1547 * Now let Xlib convert our UTF-8 data into compound text.
1549 list[0] = inst->pasteout_data_utf8;
1550 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1551 XCompoundTextStyle, &tp) == 0) {
1552 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1553 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1554 inst->pasteout_data_ctext_len = tp.nitems;
1557 inst->pasteout_data_ctext = NULL;
1558 inst->pasteout_data_ctext_len = 0;
1561 inst->pasteout_data_utf8 = NULL;
1562 inst->pasteout_data_utf8_len = 0;
1563 inst->pasteout_data_ctext = NULL;
1564 inst->pasteout_data_ctext_len = 0;
1567 inst->pasteout_data = snewn(len*6, char);
1568 inst->pasteout_data_len = len*6;
1569 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1570 data, len, inst->pasteout_data,
1571 inst->pasteout_data_len,
1573 if (inst->pasteout_data_len == 0) {
1574 sfree(inst->pasteout_data);
1575 inst->pasteout_data = NULL;
1577 inst->pasteout_data =
1578 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1581 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1583 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1584 inst->input_event_time)) {
1585 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1586 GDK_SELECTION_TYPE_STRING, 1);
1587 if (inst->pasteout_data_ctext)
1588 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1589 compound_text_atom, 1);
1590 if (inst->pasteout_data_utf8)
1591 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1592 utf8_string_atom, 1);
1596 term_deselect(inst->term);
1599 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1600 guint info, guint time_stamp, gpointer data)
1602 struct gui_data *inst = (struct gui_data *)data;
1603 if (seldata->target == utf8_string_atom)
1604 gtk_selection_data_set(seldata, seldata->target, 8,
1605 (unsigned char *)inst->pasteout_data_utf8,
1606 inst->pasteout_data_utf8_len);
1607 else if (seldata->target == compound_text_atom)
1608 gtk_selection_data_set(seldata, seldata->target, 8,
1609 (unsigned char *)inst->pasteout_data_ctext,
1610 inst->pasteout_data_ctext_len);
1612 gtk_selection_data_set(seldata, seldata->target, 8,
1613 (unsigned char *)inst->pasteout_data,
1614 inst->pasteout_data_len);
1617 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1620 struct gui_data *inst = (struct gui_data *)data;
1622 term_deselect(inst->term);
1623 if (inst->pasteout_data)
1624 sfree(inst->pasteout_data);
1625 if (inst->pasteout_data_ctext)
1626 sfree(inst->pasteout_data_ctext);
1627 if (inst->pasteout_data_utf8)
1628 sfree(inst->pasteout_data_utf8);
1629 inst->pasteout_data = NULL;
1630 inst->pasteout_data_len = 0;
1631 inst->pasteout_data_ctext = NULL;
1632 inst->pasteout_data_ctext_len = 0;
1633 inst->pasteout_data_utf8 = NULL;
1634 inst->pasteout_data_utf8_len = 0;
1638 void request_paste(void *frontend)
1640 struct gui_data *inst = (struct gui_data *)frontend;
1642 * In Unix, pasting is asynchronous: all we can do at the
1643 * moment is to call gtk_selection_convert(), and when the data
1644 * comes back _then_ we can call term_do_paste().
1647 if (!inst->direct_to_font) {
1649 * First we attempt to retrieve the selection as a UTF-8
1650 * string (which we will convert to the correct code page
1651 * before sending to the session, of course). If that
1652 * fails, selection_received() will be informed and will
1653 * fall back to an ordinary string.
1655 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1657 inst->input_event_time);
1660 * If we're in direct-to-font mode, we disable UTF-8
1661 * pasting, and go straight to ordinary string data.
1663 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1664 GDK_SELECTION_TYPE_STRING,
1665 inst->input_event_time);
1669 gint idle_paste_func(gpointer data); /* forward ref */
1671 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1672 guint time, gpointer data)
1674 struct gui_data *inst = (struct gui_data *)data;
1678 int length, count, ret;
1679 int free_list_required = 0;
1680 int free_required = 0;
1683 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1685 * Failed to get a UTF-8 selection string. Try compound
1688 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1690 inst->input_event_time);
1694 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1696 * Failed to get UTF-8 or compound text. Try an ordinary
1699 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1700 GDK_SELECTION_TYPE_STRING,
1701 inst->input_event_time);
1706 * If we have data, but it's not of a type we can deal with,
1707 * we have to ignore the data.
1709 if (seldata->length > 0 &&
1710 seldata->type != GDK_SELECTION_TYPE_STRING &&
1711 seldata->type != compound_text_atom &&
1712 seldata->type != utf8_string_atom)
1716 * If we have no data, try looking in a cut buffer.
1718 if (seldata->length <= 0) {
1719 text = retrieve_cutbuffer(&length);
1722 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1723 * source, so use that as a de-facto standard. */
1724 charset = CS_ISO8859_1;
1728 * Convert COMPOUND_TEXT into UTF-8.
1730 if (seldata->type == compound_text_atom) {
1731 tp.value = seldata->data;
1732 tp.encoding = (Atom) seldata->type;
1733 tp.format = seldata->format;
1734 tp.nitems = seldata->length;
1735 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1737 if (ret != 0 || count != 1) {
1739 * Compound text failed; fall back to STRING.
1741 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1742 GDK_SELECTION_TYPE_STRING,
1743 inst->input_event_time);
1747 length = strlen(list[0]);
1749 free_list_required = 1;
1751 text = (char *)seldata->data;
1752 length = seldata->length;
1753 charset = (seldata->type == utf8_string_atom ?
1754 CS_UTF8 : inst->ucsdata.line_codepage);
1758 if (inst->pastein_data)
1759 sfree(inst->pastein_data);
1761 inst->pastein_data = snewn(length, wchar_t);
1762 inst->pastein_data_len = length;
1763 inst->pastein_data_len =
1764 mb_to_wc(charset, 0, text, length,
1765 inst->pastein_data, inst->pastein_data_len);
1767 term_do_paste(inst->term);
1769 if (term_paste_pending(inst->term))
1770 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1772 if (free_list_required)
1773 XFreeStringList(list);
1778 gint idle_paste_func(gpointer data)
1780 struct gui_data *inst = (struct gui_data *)data;
1782 if (term_paste_pending(inst->term))
1783 term_paste(inst->term);
1785 gtk_idle_remove(inst->term_paste_idle_id);
1791 void get_clip(void *frontend, wchar_t ** p, int *len)
1793 struct gui_data *inst = (struct gui_data *)frontend;
1796 *p = inst->pastein_data;
1797 *len = inst->pastein_data_len;
1801 static void set_window_titles(struct gui_data *inst)
1804 * We must always call set_icon_name after calling set_title,
1805 * since set_title will write both names. Irritating, but such
1808 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1809 if (!inst->cfg.win_name_always)
1810 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1813 void set_title(void *frontend, char *title)
1815 struct gui_data *inst = (struct gui_data *)frontend;
1816 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1817 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1818 set_window_titles(inst);
1821 void set_icon(void *frontend, char *title)
1823 struct gui_data *inst = (struct gui_data *)frontend;
1824 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1825 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1826 set_window_titles(inst);
1829 void set_sbar(void *frontend, int total, int start, int page)
1831 struct gui_data *inst = (struct gui_data *)frontend;
1832 if (!inst->cfg.scrollbar)
1834 inst->sbar_adjust->lower = 0;
1835 inst->sbar_adjust->upper = total;
1836 inst->sbar_adjust->value = start;
1837 inst->sbar_adjust->page_size = page;
1838 inst->sbar_adjust->step_increment = 1;
1839 inst->sbar_adjust->page_increment = page/2;
1840 inst->ignore_sbar = TRUE;
1841 gtk_adjustment_changed(inst->sbar_adjust);
1842 inst->ignore_sbar = FALSE;
1845 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1847 struct gui_data *inst = (struct gui_data *)data;
1849 if (!inst->cfg.scrollbar)
1851 if (!inst->ignore_sbar)
1852 term_scroll(inst->term, 1, (int)adj->value);
1855 void sys_cursor(void *frontend, int x, int y)
1858 * This is meaningless under X.
1863 * This is still called when mode==BELL_VISUAL, even though the
1864 * visual bell is handled entirely within terminal.c, because we
1865 * may want to perform additional actions on any kind of bell (for
1866 * example, taskbar flashing in Windows).
1868 void do_beep(void *frontend, int mode)
1870 if (mode == BELL_DEFAULT)
1874 int char_width(Context ctx, int uc)
1877 * Under X, any fixed-width font really _is_ fixed-width.
1878 * Double-width characters will be dealt with using a separate
1879 * font. For the moment we can simply return 1.
1881 * FIXME: but is that also true of Pango?
1886 Context get_ctx(void *frontend)
1888 struct gui_data *inst = (struct gui_data *)frontend;
1889 struct draw_ctx *dctx;
1891 if (!inst->area->window)
1894 dctx = snew(struct draw_ctx);
1896 dctx->gc = gdk_gc_new(inst->area->window);
1900 void free_ctx(Context ctx)
1902 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1903 /* struct gui_data *inst = dctx->inst; */
1904 GdkGC *gc = dctx->gc;
1910 * Draw a line of text in the window, at given character
1911 * coordinates, in given attributes.
1913 * We are allowed to fiddle with the contents of `text'.
1915 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1916 unsigned long attr, int lattr)
1918 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1919 struct gui_data *inst = dctx->inst;
1920 GdkGC *gc = dctx->gc;
1921 int ncombining, combining;
1922 int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold;
1923 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
1925 if (attr & TATTR_COMBINING) {
1931 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
1932 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
1933 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
1938 if (inst->cfg.bold_colour && (attr & ATTR_BOLD)) {
1939 if (nfg < 16) nfg |= 8;
1940 else if (nfg >= 256) nfg |= 1;
1942 if (inst->cfg.bold_colour && (attr & ATTR_BLINK)) {
1943 if (nbg < 16) nbg |= 8;
1944 else if (nbg >= 256) nbg |= 1;
1946 if ((attr & TATTR_ACTCURS) && !monochrome) {
1951 fontid = shadow = 0;
1953 if (attr & ATTR_WIDE) {
1960 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1967 if (!inst->fonts[fontid]) {
1970 * Fall back through font ids with subsets of this one's
1971 * set bits, in order.
1973 for (i = fontid; i-- > 0 ;) {
1975 continue; /* some other bit is set */
1976 if (inst->fonts[i]) {
1981 assert(inst->fonts[fontid]); /* we should at least have hit zero */
1984 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1986 if (x >= inst->term->cols)
1988 if (x + len*2*widefactor > inst->term->cols)
1989 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1997 r.x = x*inst->font_width+inst->cfg.window_border;
1998 r.y = y*inst->font_height+inst->cfg.window_border;
1999 r.width = rlen*widefactor*inst->font_width;
2000 r.height = inst->font_height;
2001 gdk_gc_set_clip_rectangle(gc, &r);
2004 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
2005 gdk_draw_rectangle(inst->pixmap, gc, 1,
2006 x*inst->font_width+inst->cfg.window_border,
2007 y*inst->font_height+inst->cfg.window_border,
2008 rlen*widefactor*inst->font_width, inst->font_height);
2010 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2015 * FIXME: this length is hardwired on the assumption that
2016 * conversions from wide to multibyte characters will
2017 * never generate more than 10 bytes for a single wide
2020 gcs = snewn(len*10+1, gchar);
2022 for (combining = 0; combining < ncombining; combining++) {
2023 int mblen = wc_to_mb(inst->fonts[fontid]->real_charset, 0,
2024 text + combining, len, gcs, len*10+1, ".",
2026 unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid],
2027 x*inst->font_width+inst->cfg.window_border,
2028 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2029 gcs, mblen, widefactor > 1, bold, inst->font_width);
2035 if (attr & ATTR_UNDER) {
2036 int uheight = inst->fonts[0]->ascent + 1;
2037 if (uheight >= inst->font_height)
2038 uheight = inst->font_height - 1;
2039 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
2040 y*inst->font_height + uheight + inst->cfg.window_border,
2041 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
2042 y*inst->font_height + uheight + inst->cfg.window_border);
2045 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2047 * I can't find any plausible StretchBlt equivalent in the
2048 * X server, so I'm going to do this the slow and painful
2049 * way. This will involve repeated calls to
2050 * gdk_draw_pixmap() to stretch the text horizontally. It's
2051 * O(N^2) in time and O(N) in network bandwidth, but you
2052 * try thinking of a better way. :-(
2055 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2056 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2057 x*inst->font_width+inst->cfg.window_border + 2*i,
2058 y*inst->font_height+inst->cfg.window_border,
2059 x*inst->font_width+inst->cfg.window_border + 2*i+1,
2060 y*inst->font_height+inst->cfg.window_border,
2061 len * widefactor * inst->font_width - i, inst->font_height);
2064 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2066 /* Now stretch vertically, in the same way. */
2067 if ((lattr & LATTR_MODE) == LATTR_BOT)
2071 for (i = 0; i < inst->font_height; i+=2) {
2072 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2073 x*inst->font_width+inst->cfg.window_border,
2074 y*inst->font_height+inst->cfg.window_border+dt*i+db,
2075 x*inst->font_width+inst->cfg.window_border,
2076 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
2077 len * widefactor * inst->font_width, inst->font_height-i-1);
2083 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2084 unsigned long attr, int lattr)
2086 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2087 struct gui_data *inst = dctx->inst;
2088 GdkGC *gc = dctx->gc;
2091 do_text_internal(ctx, x, y, text, len, attr, lattr);
2093 if (attr & ATTR_WIDE) {
2099 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2101 if (x >= inst->term->cols)
2103 if (x + len*2*widefactor > inst->term->cols)
2104 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2108 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2109 x*inst->font_width+inst->cfg.window_border,
2110 y*inst->font_height+inst->cfg.window_border,
2111 x*inst->font_width+inst->cfg.window_border,
2112 y*inst->font_height+inst->cfg.window_border,
2113 len*widefactor*inst->font_width, inst->font_height);
2116 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2117 unsigned long attr, int lattr)
2119 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2120 struct gui_data *inst = dctx->inst;
2121 GdkGC *gc = dctx->gc;
2123 int active, passive, widefactor;
2125 if (attr & TATTR_PASCURS) {
2126 attr &= ~TATTR_PASCURS;
2130 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
2131 attr &= ~TATTR_ACTCURS;
2135 do_text_internal(ctx, x, y, text, len, attr, lattr);
2137 if (attr & TATTR_COMBINING)
2140 if (attr & ATTR_WIDE) {
2146 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2148 if (x >= inst->term->cols)
2150 if (x + len*2*widefactor > inst->term->cols)
2151 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2155 if (inst->cfg.cursor_type == 0) {
2157 * An active block cursor will already have been done by
2158 * the above do_text call, so we only need to do anything
2162 gdk_gc_set_foreground(gc, &inst->cols[261]);
2163 gdk_draw_rectangle(inst->pixmap, gc, 0,
2164 x*inst->font_width+inst->cfg.window_border,
2165 y*inst->font_height+inst->cfg.window_border,
2166 len*widefactor*inst->font_width-1, inst->font_height-1);
2170 int startx, starty, dx, dy, length, i;
2174 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2175 char_width = 2*inst->font_width;
2177 char_width = inst->font_width;
2179 if (inst->cfg.cursor_type == 1) {
2180 uheight = inst->fonts[0]->ascent + 1;
2181 if (uheight >= inst->font_height)
2182 uheight = inst->font_height - 1;
2184 startx = x * inst->font_width + inst->cfg.window_border;
2185 starty = y * inst->font_height + inst->cfg.window_border + uheight;
2188 length = len * widefactor * char_width;
2191 if (attr & TATTR_RIGHTCURS)
2192 xadjust = char_width - 1;
2193 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2194 starty = y * inst->font_height + inst->cfg.window_border;
2197 length = inst->font_height;
2200 gdk_gc_set_foreground(gc, &inst->cols[261]);
2202 for (i = 0; i < length; i++) {
2204 gdk_draw_point(inst->pixmap, gc, startx, starty);
2209 } else if (active) {
2210 gdk_draw_line(inst->pixmap, gc, startx, starty,
2211 startx + (length-1) * dx, starty + (length-1) * dy);
2212 } /* else no cursor (e.g., blinked off) */
2215 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2216 x*inst->font_width+inst->cfg.window_border,
2217 y*inst->font_height+inst->cfg.window_border,
2218 x*inst->font_width+inst->cfg.window_border,
2219 y*inst->font_height+inst->cfg.window_border,
2220 len*widefactor*inst->font_width, inst->font_height);
2223 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2226 * Truly hideous hack: GTK doesn't allow us to set the mouse
2227 * cursor foreground and background colours unless we've _also_
2228 * created our own cursor from bitmaps. Therefore, I need to
2229 * load the `cursor' font and draw glyphs from it on to
2230 * pixmaps, in order to construct my cursors with the fg and bg
2231 * I want. This is a gross hack, but it's more self-contained
2232 * than linking in Xlib to find the X window handle to
2233 * inst->area and calling XRecolorCursor, and it's more
2234 * futureproof than hard-coding the shapes as bitmap arrays.
2236 static GdkFont *cursor_font = NULL;
2237 GdkPixmap *source, *mask;
2239 GdkColor cfg = { 0, 65535, 65535, 65535 };
2240 GdkColor cbg = { 0, 0, 0, 0 };
2241 GdkColor dfg = { 1, 65535, 65535, 65535 };
2242 GdkColor dbg = { 0, 0, 0, 0 };
2245 gint lb, rb, wid, asc, desc, w, h, x, y;
2247 if (cursor_val == -2) {
2248 gdk_font_unref(cursor_font);
2252 if (cursor_val >= 0 && !cursor_font) {
2253 cursor_font = gdk_font_load("cursor");
2255 gdk_font_ref(cursor_font);
2259 * Get the text extent of the cursor in question. We use the
2260 * mask character for this, because it's typically slightly
2261 * bigger than the main character.
2263 if (cursor_val >= 0) {
2265 text[0] = (char)cursor_val + 1;
2266 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2267 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2273 source = gdk_pixmap_new(NULL, w, h, 1);
2274 mask = gdk_pixmap_new(NULL, w, h, 1);
2277 * Draw the mask character on the mask pixmap.
2279 gc = gdk_gc_new(mask);
2280 gdk_gc_set_foreground(gc, &dbg);
2281 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2282 if (cursor_val >= 0) {
2284 text[0] = (char)cursor_val + 1;
2285 gdk_gc_set_foreground(gc, &dfg);
2286 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2291 * Draw the main character on the source pixmap.
2293 gc = gdk_gc_new(source);
2294 gdk_gc_set_foreground(gc, &dbg);
2295 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2296 if (cursor_val >= 0) {
2298 text[0] = (char)cursor_val;
2299 gdk_gc_set_foreground(gc, &dfg);
2300 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2305 * Create the cursor.
2307 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2312 gdk_pixmap_unref(source);
2313 gdk_pixmap_unref(mask);
2318 void modalfatalbox(char *p, ...)
2321 fprintf(stderr, "FATAL ERROR: ");
2323 vfprintf(stderr, p, ap);
2325 fputc('\n', stderr);
2329 void cmdline_error(char *p, ...)
2332 fprintf(stderr, "%s: ", appname);
2334 vfprintf(stderr, p, ap);
2336 fputc('\n', stderr);
2340 char *get_x_display(void *frontend)
2342 return gdk_get_display();
2345 long get_windowid(void *frontend)
2347 struct gui_data *inst = (struct gui_data *)frontend;
2348 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2351 static void help(FILE *fp) {
2353 "pterm option summary:\n"
2355 " --display DISPLAY Specify X display to use (note '--')\n"
2356 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2357 " -fn FONT Normal text font\n"
2358 " -fb FONT Bold text font\n"
2359 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2360 " -sl LINES Number of lines of scrollback\n"
2361 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2362 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2363 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2364 " -T TITLE Window title\n"
2365 " -ut, +ut Do(default) or do not update utmp\n"
2366 " -ls, +ls Do(default) or do not make shell a login shell\n"
2367 " -sb, +sb Do(default) or do not display a scrollbar\n"
2368 " -log PATH Log all output to a file\n"
2369 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2370 " -xrm RESOURCE-STRING Set an X resource\n"
2371 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2372 ) < 0 || fflush(fp) < 0) {
2373 perror("output error");
2378 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2379 struct gui_data *inst, Config *cfg)
2385 * Macros to make argument handling easier. Note that because
2386 * they need to call `continue', they cannot be contained in
2387 * the usual do {...} while (0) wrapper to make them
2388 * syntactically single statements; hence it is not legal to
2389 * use one of these macros as an unbraced statement between
2392 #define EXPECTS_ARG { \
2393 if (--argc <= 0) { \
2395 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2400 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2402 while (--argc > 0) {
2407 * Shameless cheating. Debian requires all X terminal
2408 * emulators to support `-T title'; but
2409 * cmdline_process_param will eat -T (it means no-pty) and
2410 * complain that pterm doesn't support it. So, in pterm
2411 * only, we convert -T into -title.
2413 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2417 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2418 do_everything ? 1 : -1, cfg);
2421 cmdline_error("option \"%s\" requires an argument", p);
2422 } else if (ret == 2) {
2423 --argc, ++argv; /* skip next argument */
2425 } else if (ret == 1) {
2429 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2432 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2433 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2435 } else if (!strcmp(p, "-fb")) {
2438 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2439 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2441 } else if (!strcmp(p, "-fw")) {
2444 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2445 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2447 } else if (!strcmp(p, "-fwb")) {
2450 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2451 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2453 } else if (!strcmp(p, "-cs")) {
2456 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2457 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2459 } else if (!strcmp(p, "-geometry")) {
2465 flags = XParseGeometry(val, &x, &y, &w, &h);
2466 if (flags & WidthValue)
2467 cfg->width = (int)w;
2468 if (flags & HeightValue)
2469 cfg->height = (int)h;
2471 if (flags & (XValue | YValue)) {
2474 inst->gotpos = TRUE;
2475 inst->gravity = ((flags & XNegative ? 1 : 0) |
2476 (flags & YNegative ? 2 : 0));
2479 } else if (!strcmp(p, "-sl")) {
2482 cfg->savelines = atoi(val);
2484 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2485 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2486 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2491 if (!gdk_color_parse(val, &col)) {
2493 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2497 index = (!strcmp(p, "-fg") ? 0 :
2498 !strcmp(p, "-bg") ? 2 :
2499 !strcmp(p, "-bfg") ? 1 :
2500 !strcmp(p, "-bbg") ? 3 :
2501 !strcmp(p, "-cfg") ? 4 :
2502 !strcmp(p, "-cbg") ? 5 : -1);
2503 assert(index != -1);
2504 cfg->colours[index][0] = col.red / 256;
2505 cfg->colours[index][1] = col.green / 256;
2506 cfg->colours[index][2] = col.blue / 256;
2509 } else if (use_pty_argv && !strcmp(p, "-e")) {
2510 /* This option swallows all further arguments. */
2516 pty_argv = snewn(argc+1, char *);
2518 for (i = 0; i < argc; i++)
2519 pty_argv[i] = argv[i];
2520 pty_argv[argc] = NULL;
2521 break; /* finished command-line processing */
2523 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2526 } else if (!strcmp(p, "-title")) {
2529 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2530 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2532 } else if (!strcmp(p, "-log")) {
2535 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2536 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2537 cfg->logtype = LGTYP_DEBUG;
2539 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2541 cfg->stamp_utmp = 0;
2543 } else if (!strcmp(p, "-ut")) {
2545 cfg->stamp_utmp = 1;
2547 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2549 cfg->login_shell = 0;
2551 } else if (!strcmp(p, "-ls")) {
2553 cfg->login_shell = 1;
2555 } else if (!strcmp(p, "-nethack")) {
2557 cfg->nethack_keypad = 1;
2559 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2563 } else if (!strcmp(p, "-sb")) {
2567 } else if (!strcmp(p, "-name")) {
2571 } else if (!strcmp(p, "-xrm")) {
2573 provide_xrm_string(val);
2575 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2579 } else if (!strcmp(p, "-pgpfp")) {
2583 } else if(p[0] != '-' && (!do_everything ||
2584 process_nonoption_arg(p, cfg,
2590 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2597 int uxsel_input_add(int fd, int rwx) {
2599 if (rwx & 1) flags |= GDK_INPUT_READ;
2600 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2601 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2603 return gdk_input_add(fd, flags, fd_input_func, NULL);
2606 void uxsel_input_remove(int id) {
2607 gdk_input_remove(id);
2610 void setup_fonts_ucs(struct gui_data *inst)
2613 unifont_destroy(inst->fonts[0]);
2615 unifont_destroy(inst->fonts[1]);
2617 unifont_destroy(inst->fonts[2]);
2619 unifont_destroy(inst->fonts[3]);
2621 inst->fonts[0] = unifont_create(inst->area, inst->cfg.font.name,
2623 inst->cfg.shadowboldoffset,
2624 inst->cfg.shadowbold);
2625 if (!inst->fonts[0]) {
2626 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2627 inst->cfg.font.name);
2631 if (inst->cfg.shadowbold || !inst->cfg.boldfont.name[0]) {
2632 inst->fonts[1] = NULL;
2634 inst->fonts[1] = unifont_create(inst->area, inst->cfg.boldfont.name,
2636 inst->cfg.shadowboldoffset,
2637 inst->cfg.shadowbold);
2638 if (!inst->fonts[1]) {
2639 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2640 inst->cfg.boldfont.name);
2645 if (inst->cfg.widefont.name[0]) {
2646 inst->fonts[2] = unifont_create(inst->area, inst->cfg.widefont.name,
2648 inst->cfg.shadowboldoffset,
2649 inst->cfg.shadowbold);
2650 if (!inst->fonts[2]) {
2651 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2652 inst->cfg.widefont.name);
2656 inst->fonts[2] = NULL;
2659 if (inst->cfg.shadowbold || !inst->cfg.wideboldfont.name[0]) {
2660 inst->fonts[3] = NULL;
2662 inst->fonts[3] = unifont_create(inst->area,
2663 inst->cfg.wideboldfont.name, TRUE,
2664 TRUE, inst->cfg.shadowboldoffset,
2665 inst->cfg.shadowbold);
2666 if (!inst->fonts[3]) {
2667 fprintf(stderr, "%s: unable to load wide bold font \"%s\"\n", appname,
2668 inst->cfg.boldfont.name);
2673 inst->font_width = inst->fonts[0]->width;
2674 inst->font_height = inst->fonts[0]->height;
2676 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2677 inst->cfg.utf8_override,
2678 inst->fonts[0]->public_charset,
2682 void set_geom_hints(struct gui_data *inst)
2685 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2686 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2687 geom.max_width = geom.max_height = -1;
2688 geom.base_width = 2*inst->cfg.window_border;
2689 geom.base_height = 2*inst->cfg.window_border;
2690 geom.width_inc = inst->font_width;
2691 geom.height_inc = inst->font_height;
2692 geom.min_aspect = geom.max_aspect = 0;
2693 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2694 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2695 GDK_HINT_RESIZE_INC);
2698 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2700 struct gui_data *inst = (struct gui_data *)data;
2701 term_clrsb(inst->term);
2704 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2706 struct gui_data *inst = (struct gui_data *)data;
2707 term_pwron(inst->term, TRUE);
2709 ldisc_send(inst->ldisc, NULL, 0, 0);
2712 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2714 struct gui_data *inst = (struct gui_data *)data;
2715 term_copyall(inst->term);
2718 void special_menuitem(GtkMenuItem *item, gpointer data)
2720 struct gui_data *inst = (struct gui_data *)data;
2721 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2725 inst->back->special(inst->backhandle, code);
2728 void about_menuitem(GtkMenuItem *item, gpointer data)
2730 struct gui_data *inst = (struct gui_data *)data;
2731 about_box(inst->window);
2734 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2736 struct gui_data *inst = (struct gui_data *)data;
2737 showeventlog(inst->eventlogstuff, inst->window);
2740 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2742 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2743 static const int ww[] = {
2744 256, 257, 258, 259, 260, 261,
2745 0, 8, 1, 9, 2, 10, 3, 11,
2746 4, 12, 5, 13, 6, 14, 7, 15
2748 struct gui_data *inst = (struct gui_data *)data;
2749 char *title = dupcat(appname, " Reconfiguration", NULL);
2750 Config cfg2, oldcfg;
2753 assert(lenof(ww) == NCFGCOLOURS);
2755 if (inst->reconfiguring)
2758 inst->reconfiguring = TRUE;
2760 cfg2 = inst->cfg; /* structure copy */
2762 if (do_config_box(title, &cfg2, 1,
2763 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2765 oldcfg = inst->cfg; /* structure copy */
2766 inst->cfg = cfg2; /* structure copy */
2768 /* Pass new config data to the logging module */
2769 log_reconfig(inst->logctx, &cfg2);
2771 * Flush the line discipline's edit buffer in the case
2772 * where local editing has just been disabled.
2775 ldisc_send(inst->ldisc, NULL, 0, 0);
2776 /* Pass new config data to the terminal */
2777 term_reconfig(inst->term, &cfg2);
2778 /* Pass new config data to the back end */
2780 inst->back->reconfig(inst->backhandle, &cfg2);
2783 * Just setting inst->cfg is sufficient to cause colour
2784 * setting changes to appear on the next ESC]R palette
2785 * reset. But we should also check whether any colour
2786 * settings have been changed, and revert the ones that
2787 * have to the new default, on the assumption that the user
2788 * is most likely to want an immediate update.
2790 for (i = 0; i < NCFGCOLOURS; i++) {
2791 if (oldcfg.colours[i][0] != cfg2.colours[i][0] ||
2792 oldcfg.colours[i][1] != cfg2.colours[i][1] ||
2793 oldcfg.colours[i][2] != cfg2.colours[i][2]) {
2794 real_palette_set(inst, ww[i], cfg2.colours[i][0],
2796 cfg2.colours[i][2]);
2799 * If the default background has changed, we must
2800 * repaint the space in between the window border
2801 * and the text area.
2804 set_window_background(inst);
2805 draw_backing_rect(inst);
2811 * If the scrollbar needs to be shown, hidden, or moved
2812 * from one end to the other of the window, do so now.
2814 if (oldcfg.scrollbar != cfg2.scrollbar) {
2816 gtk_widget_show(inst->sbar);
2818 gtk_widget_hide(inst->sbar);
2820 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2821 gtk_box_reorder_child(inst->hbox, inst->sbar,
2822 cfg2.scrollbar_on_left ? 0 : 1);
2826 * Change the window title, if required.
2828 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2829 set_title(inst, cfg2.wintitle);
2830 set_window_titles(inst);
2833 * Redo the whole tangled fonts and Unicode mess if
2836 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2837 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2838 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2839 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
2840 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
2841 oldcfg.vtmode != cfg2.vtmode ||
2842 oldcfg.shadowbold != cfg2.shadowbold) {
2843 setup_fonts_ucs(inst);
2849 * Resize the window.
2851 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2852 oldcfg.window_border != cfg2.window_border || need_size) {
2853 set_geom_hints(inst);
2854 request_resize(inst, cfg2.width, cfg2.height);
2857 * The above will have caused a call to term_size() for
2858 * us if it happened. If the user has fiddled with only
2859 * the scrollback size, the above will not have
2860 * happened and we will need an explicit term_size()
2863 if (oldcfg.savelines != cfg2.savelines)
2864 term_size(inst->term, inst->term->rows, inst->term->cols,
2868 term_invalidate(inst->term);
2871 * We do an explicit full redraw here to ensure the window
2872 * border has been redrawn as well as the text area.
2874 gtk_widget_queue_draw(inst->area);
2877 inst->reconfiguring = FALSE;
2880 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
2883 * Re-execing ourself is not an exact science under Unix. I do
2884 * the best I can by using /proc/self/exe if available and by
2885 * assuming argv[0] can be found on $PATH if not.
2887 * Note that we also have to reconstruct the elements of the
2888 * original argv which gtk swallowed, since the user wants the
2889 * new session to appear on the same X display as the old one.
2897 * Collect the arguments with which to re-exec ourself.
2899 va_start(ap, fd_to_close);
2900 n = 2; /* progname and terminating NULL */
2901 n += inst->ngtkargs;
2902 while (va_arg(ap, char *) != NULL)
2906 args = snewn(n, char *);
2907 args[0] = inst->progname;
2909 for (i = 0; i < inst->ngtkargs; i++)
2910 args[i+1] = inst->gtkargvstart[i];
2913 va_start(ap, fd_to_close);
2914 while ((args[i++] = va_arg(ap, char *)) != NULL);
2920 * Do the double fork.
2933 } else if (pid2 > 0) {
2935 * First child has successfully forked second child. My
2936 * Work Here Is Done. Note the use of _exit rather than
2937 * exit: the latter appears to cause destroy messages
2938 * to be sent to the X server. I suspect gtk uses
2945 * If we reach here, we are the second child, so we now
2946 * actually perform the exec.
2948 if (fd_to_close >= 0)
2951 execv("/proc/self/exe", args);
2952 execvp(inst->progname, args);
2958 waitpid(pid, &status, 0);
2963 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
2965 struct gui_data *inst = (struct gui_data *)gdata;
2967 * For this feature we must marshal cfg and (possibly) pty_argv
2968 * into a byte stream, create a pipe, and send this byte stream
2969 * to the child through the pipe.
2976 if (pipe(pipefd) < 0) {
2981 size = sizeof(inst->cfg);
2982 if (use_pty_argv && pty_argv) {
2983 for (i = 0; pty_argv[i]; i++)
2984 size += strlen(pty_argv[i]) + 1;
2987 data = snewn(size, char);
2988 memcpy(data, &inst->cfg, sizeof(inst->cfg));
2989 if (use_pty_argv && pty_argv) {
2990 int p = sizeof(inst->cfg);
2991 for (i = 0; pty_argv[i]; i++) {
2992 strcpy(data + p, pty_argv[i]);
2993 p += strlen(pty_argv[i]) + 1;
2998 sprintf(option, "---[%d,%d]", pipefd[0], size);
2999 fcntl(pipefd[0], F_SETFD, 0);
3000 fork_and_exec_self(inst, pipefd[1], option, NULL);
3004 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3007 perror("write to pipe");
3012 int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3014 int fd, i, ret, size;
3017 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3018 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3022 data = snewn(size, char);
3024 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3027 perror("read from pipe");
3029 } else if (i < size) {
3030 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3035 memcpy(cfg, data, sizeof(Config));
3036 if (use_pty_argv && size > sizeof(Config)) {
3040 while (i < size && data[i]) i++;
3042 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3049 pty_argv = snewn(n+1, char *);
3055 while (i < size && data[i]) i++;
3058 pty_argv[n++] = dupstr(p);
3065 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3067 struct gui_data *inst = (struct gui_data *)data;
3069 fork_and_exec_self(inst, -1, NULL);
3072 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3074 struct gui_data *inst = (struct gui_data *)data;
3077 logevent(inst, "----- Session restarted -----");
3078 term_pwron(inst->term, FALSE);
3079 start_backend(inst);
3080 inst->exited = FALSE;
3084 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3086 struct gui_data *inst = (struct gui_data *)data;
3087 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3089 fork_and_exec_self(inst, -1, "-load", str, NULL);
3092 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3094 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3099 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3101 struct gui_data *inst = (struct gui_data *)data;
3102 struct sesslist sesslist;
3105 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3106 (GtkCallback)gtk_widget_destroy, NULL);
3108 get_sesslist(&sesslist, TRUE);
3109 for (i = 1; i < sesslist.nsessions; i++) {
3110 GtkWidget *menuitem =
3111 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3112 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3113 gtk_widget_show(menuitem);
3114 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3115 dupstr(sesslist.sessions[i]));
3116 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3117 GTK_SIGNAL_FUNC(saved_session_menuitem),
3119 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3120 GTK_SIGNAL_FUNC(saved_session_freedata),
3123 get_sesslist(&sesslist, FALSE); /* free up */
3126 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3130 GdkBitmap *iconmask;
3131 #if GTK_CHECK_VERSION(2,0,0)
3139 gtk_widget_realize(window);
3140 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3141 NULL, (gchar **)icon[0]);
3142 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3144 #if GTK_CHECK_VERSION(2,0,0)
3146 for (n = 0; n < n_icon; n++) {
3148 g_list_append(iconlist,
3149 gdk_pixbuf_new_from_xpm_data((const gchar **)
3152 gdk_window_set_icon_list(window->window, iconlist);
3156 void update_specials_menu(void *frontend)
3158 struct gui_data *inst = (struct gui_data *)frontend;
3160 const struct telnet_special *specials;
3163 specials = inst->back->get_specials(inst->backhandle);
3167 /* I believe this disposes of submenus too. */
3168 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3169 (GtkCallback)gtk_widget_destroy, NULL);
3172 GtkWidget *menu = inst->specialsmenu;
3173 /* A lame "stack" for submenus that will do for now. */
3174 GtkWidget *saved_menu = NULL;
3176 for (i = 0; nesting > 0; i++) {
3177 GtkWidget *menuitem = NULL;
3178 switch (specials[i].code) {
3180 assert (nesting < 2);
3181 saved_menu = menu; /* XXX lame stacking */
3182 menu = gtk_menu_new();
3183 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3184 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3185 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3186 gtk_widget_show(menuitem);
3193 menu = saved_menu; /* XXX lame stacking */
3198 menuitem = gtk_menu_item_new();
3201 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3202 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3203 GINT_TO_POINTER(specials[i].code));
3204 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3205 GTK_SIGNAL_FUNC(special_menuitem), inst);
3209 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3210 gtk_widget_show(menuitem);
3213 gtk_widget_show(inst->specialsitem1);
3214 gtk_widget_show(inst->specialsitem2);
3216 gtk_widget_hide(inst->specialsitem1);
3217 gtk_widget_hide(inst->specialsitem2);
3221 static void start_backend(struct gui_data *inst)
3223 extern Backend *select_backend(Config *cfg);
3227 inst->back = select_backend(&inst->cfg);
3229 error = inst->back->init((void *)inst, &inst->backhandle,
3230 &inst->cfg, inst->cfg.host, inst->cfg.port,
3231 &realhost, inst->cfg.tcp_nodelay,
3232 inst->cfg.tcp_keepalives);
3235 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3236 inst->cfg.host, error);
3237 inst->exited = TRUE;
3238 fatal_message_box(inst->window, msg);
3243 if (inst->cfg.wintitle[0]) {
3244 set_title(inst, inst->cfg.wintitle);
3245 set_icon(inst, inst->cfg.wintitle);
3247 char *title = make_default_wintitle(realhost);
3248 set_title(inst, title);
3249 set_icon(inst, title);
3254 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3256 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3259 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3262 gtk_widget_hide(inst->restartitem);
3265 int pt_main(int argc, char **argv)
3267 extern int cfgbox(Config *cfg);
3268 struct gui_data *inst;
3271 * Create an instance structure and initialise to zeroes
3273 inst = snew(struct gui_data);
3274 memset(inst, 0, sizeof(*inst));
3275 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3276 inst->busy_status = BUSY_NOT;
3278 /* defer any child exit handling until we're ready to deal with
3280 block_signal(SIGCHLD, 1);
3282 inst->progname = argv[0];
3284 * Copy the original argv before letting gtk_init fiddle with
3285 * it. It will be required later.
3289 inst->gtkargvstart = snewn(argc-1, char *);
3290 for (i = 1; i < argc; i++)
3291 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3293 gtk_init(&argc, &argv);
3294 inst->ngtkargs = oldargc - argc;
3297 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3298 read_dupsession_data(inst, &inst->cfg, argv[1]);
3299 /* Splatter this argument so it doesn't clutter a ps listing */
3300 memset(argv[1], 0, strlen(argv[1]));
3302 /* By default, we bring up the config dialog, rather than launching
3303 * a session. This gets set to TRUE if something happens to change
3304 * that (e.g., a hostname is specified on the command-line). */
3305 int allow_launch = FALSE;
3306 if (do_cmdline(argc, argv, 0, &allow_launch, inst, &inst->cfg))
3307 exit(1); /* pre-defaults pass to get -class */
3308 do_defaults(NULL, &inst->cfg);
3309 if (do_cmdline(argc, argv, 1, &allow_launch, inst, &inst->cfg))
3310 exit(1); /* post-defaults, do everything */
3312 cmdline_run_saved(&inst->cfg);
3315 allow_launch = TRUE;
3317 if ((!allow_launch || !cfg_launchable(&inst->cfg)) &&
3318 !cfgbox(&inst->cfg))
3319 exit(0); /* config box hit Cancel */
3322 if (!compound_text_atom)
3323 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3324 if (!utf8_string_atom)
3325 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3327 inst->area = gtk_drawing_area_new();
3329 setup_fonts_ucs(inst);
3332 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3335 * Set up the colour map.
3337 palette_reset(inst);
3339 inst->width = inst->cfg.width;
3340 inst->height = inst->cfg.height;
3342 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3343 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3344 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
3345 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3346 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3347 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3349 * We always create the scrollbar; it remains invisible if
3350 * unwanted, so we can pop it up quickly if it suddenly becomes
3353 if (inst->cfg.scrollbar_on_left)
3354 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3355 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3356 if (!inst->cfg.scrollbar_on_left)
3357 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3359 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3361 set_geom_hints(inst);
3363 gtk_widget_show(inst->area);
3364 if (inst->cfg.scrollbar)
3365 gtk_widget_show(inst->sbar);
3367 gtk_widget_hide(inst->sbar);
3368 gtk_widget_show(GTK_WIDGET(inst->hbox));
3371 int x = inst->xpos, y = inst->ypos;
3373 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3374 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3375 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3376 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3377 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3380 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3381 GTK_SIGNAL_FUNC(destroy), inst);
3382 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3383 GTK_SIGNAL_FUNC(delete_window), inst);
3384 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3385 GTK_SIGNAL_FUNC(key_event), inst);
3386 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3387 GTK_SIGNAL_FUNC(key_event), inst);
3388 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3389 GTK_SIGNAL_FUNC(focus_event), inst);
3390 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3391 GTK_SIGNAL_FUNC(focus_event), inst);
3392 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3393 GTK_SIGNAL_FUNC(configure_area), inst);
3394 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3395 GTK_SIGNAL_FUNC(expose_area), inst);
3396 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3397 GTK_SIGNAL_FUNC(button_event), inst);
3398 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3399 GTK_SIGNAL_FUNC(button_event), inst);
3400 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3401 GTK_SIGNAL_FUNC(motion_event), inst);
3402 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3403 GTK_SIGNAL_FUNC(selection_received), inst);
3404 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3405 GTK_SIGNAL_FUNC(selection_get), inst);
3406 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3407 GTK_SIGNAL_FUNC(selection_clear), inst);
3408 if (inst->cfg.scrollbar)
3409 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3410 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3411 gtk_widget_add_events(GTK_WIDGET(inst->area),
3412 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3413 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3414 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3417 extern const char *const *const main_icon[];
3418 extern const int n_main_icon;
3419 set_window_icon(inst->window, main_icon, n_main_icon);
3422 gtk_widget_show(inst->window);
3424 set_window_background(inst);
3427 * Set up the Ctrl+rightclick context menu.
3430 GtkWidget *menuitem;
3432 extern const int use_event_log, new_session, saved_sessions;
3434 inst->menu = gtk_menu_new();
3436 #define MKMENUITEM(title, func) do { \
3437 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3438 gtk_menu_item_new(); \
3439 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3440 gtk_widget_show(menuitem); \
3442 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3443 GTK_SIGNAL_FUNC(func), inst); \
3446 MKMENUITEM("New Session", new_session_menuitem);
3447 MKMENUITEM("Restart Session", restart_session_menuitem);
3448 inst->restartitem = menuitem;
3449 gtk_widget_hide(inst->restartitem);
3450 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3451 if (saved_sessions) {
3452 inst->sessionsmenu = gtk_menu_new();
3453 /* sessionsmenu will be updated when it's invoked */
3454 /* XXX is this the right way to do dynamic menus in Gtk? */
3455 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3456 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3457 inst->sessionsmenu);
3459 MKMENUITEM(NULL, NULL);
3460 MKMENUITEM("Change Settings", change_settings_menuitem);
3461 MKMENUITEM(NULL, NULL);
3463 MKMENUITEM("Event Log", event_log_menuitem);
3464 MKMENUITEM("Special Commands", NULL);
3465 inst->specialsmenu = gtk_menu_new();
3466 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3467 inst->specialsitem1 = menuitem;
3468 MKMENUITEM(NULL, NULL);
3469 inst->specialsitem2 = menuitem;
3470 gtk_widget_hide(inst->specialsitem1);
3471 gtk_widget_hide(inst->specialsitem2);
3472 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3473 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3474 MKMENUITEM("Copy All", copy_all_menuitem);
3475 MKMENUITEM(NULL, NULL);
3476 s = dupcat("About ", appname, NULL);
3477 MKMENUITEM(s, about_menuitem);
3482 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3483 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3484 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3485 inst->blankcursor = make_mouse_ptr(inst, -1);
3486 make_mouse_ptr(inst, -2); /* clean up cursor font */
3487 inst->currcursor = inst->textcursor;
3488 show_mouseptr(inst, 1);
3490 inst->eventlogstuff = eventlogstuff_new();
3492 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3493 inst->logctx = log_init(inst, &inst->cfg);
3494 term_provide_logctx(inst->term, inst->logctx);
3498 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3500 start_backend(inst);
3502 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3504 /* now we're reday to deal with the child exit handler being
3506 block_signal(SIGCHLD, 0);
3509 * Block SIGPIPE: if we attempt Duplicate Session or similar
3510 * and it falls over in some way, we certainly don't want
3511 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3512 * this _after_ (at least pterm) forks off its child process,
3513 * since the child wants SIGPIPE handled in the usual way.
3515 block_signal(SIGPIPE, 1);
3517 inst->exited = FALSE;