2 * pterm - a fusion of the PuTTY terminal emulator with a Unix pty
3 * back end, all running as a GTK application. Wish me luck.
18 #include <sys/types.h>
21 #include <gdk/gdkkeysyms.h>
24 #include <X11/Xutil.h>
26 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
31 #define CAT2(x,y) x ## y
32 #define CAT(x,y) CAT2(x,y)
33 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
35 #define NCOLOURS (lenof(((Config *)0)->colours))
37 GdkAtom compound_text_atom, utf8_string_atom;
39 extern char **pty_argv; /* declared in pty.c */
40 extern int use_pty_argv;
43 GtkWidget *window, *area, *sbar;
45 GtkAdjustment *sbar_adjust;
46 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2;
47 GtkWidget *sessionsmenu;
49 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
54 int xpos, ypos, gotpos, gravity;
55 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
56 GdkColor cols[NCOLOURS];
58 wchar_t *pastein_data;
61 char *pasteout_data, *pasteout_data_utf8;
62 int pasteout_data_len, pasteout_data_utf8_len;
63 int font_width, font_height;
67 guint term_paste_idle_id;
70 char wintitle[sizeof(((Config *)0)->wintitle)];
71 char icontitle[sizeof(((Config *)0)->wintitle)];
72 int master_fd, master_func_id;
79 struct unicode_data ucsdata;
82 char *progname, **gtkargvstart;
88 struct gui_data *inst;
91 static int send_raw_mouse;
93 static char *app_name = "pterm";
95 char *x_get_default(const char *key)
97 return XGetDefault(GDK_DISPLAY(), app_name, key);
100 void connection_fatal(void *frontend, char *p, ...)
102 struct gui_data *inst = (struct gui_data *)frontend;
107 msg = dupvprintf(p, ap);
110 fatal_message_box(inst->window, msg);
112 if (inst->cfg.close_on_exit == FORCE_ON)
117 * Default settings that are specific to pterm.
119 FontSpec platform_default_fontspec(const char *name)
122 if (!strcmp(name, "Font"))
123 strcpy(ret.name, "fixed");
129 Filename platform_default_filename(const char *name)
132 if (!strcmp(name, "LogFileName"))
133 strcpy(ret.path, "putty.log");
139 char *platform_default_s(const char *name)
144 int platform_default_i(const char *name, int def)
146 if (!strcmp(name, "CloseOnExit"))
147 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
151 void ldisc_update(void *frontend, int echo, int edit)
154 * This is a stub in pterm. If I ever produce a Unix
155 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
156 * then it will require some termios manoeuvring analogous to
157 * that in the Windows plink.c, but here it's meaningless.
161 int from_backend(void *frontend, int is_stderr, const char *data, int len)
163 struct gui_data *inst = (struct gui_data *)frontend;
164 return term_data(inst->term, is_stderr, data, len);
167 void logevent(void *frontend, char *string)
169 struct gui_data *inst = (struct gui_data *)frontend;
171 log_eventlog(inst->logctx, string);
173 logevent_dlg(inst->eventlogstuff, string);
176 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
178 struct gui_data *inst = (struct gui_data *)frontend;
181 return inst->font_height;
183 return inst->font_width;
187 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
188 * into a cooked one (SELECT, EXTEND, PASTE).
190 * In Unix, this is not configurable; the X button arrangement is
191 * rock-solid across all applications, everyone has a three-button
192 * mouse or a means of faking it, and there is no need to switch
193 * buttons around at all.
195 static Mouse_Button translate_button(Mouse_Button button)
197 /* struct gui_data *inst = (struct gui_data *)frontend; */
199 if (button == MBT_LEFT)
201 if (button == MBT_MIDDLE)
203 if (button == MBT_RIGHT)
205 return 0; /* shouldn't happen */
209 * Return the top-level GtkWindow associated with a particular
210 * front end instance.
212 void *get_window(void *frontend)
214 struct gui_data *inst = (struct gui_data *)frontend;
219 * Minimise or restore the window in response to a server-side
222 void set_iconic(void *frontend, int iconic)
225 * GTK 1.2 doesn't know how to do this.
227 #if GTK_CHECK_VERSION(2,0,0)
228 struct gui_data *inst = (struct gui_data *)frontend;
230 gtk_window_iconify(GTK_WINDOW(inst->window));
232 gtk_window_deiconify(GTK_WINDOW(inst->window));
237 * Move the window in response to a server-side request.
239 void move_window(void *frontend, int x, int y)
241 struct gui_data *inst = (struct gui_data *)frontend;
243 * I assume that when the GTK version of this call is available
244 * we should use it. Not sure how it differs from the GDK one,
247 #if GTK_CHECK_VERSION(2,0,0)
248 gtk_window_move(GTK_WINDOW(inst->window), x, y);
250 gdk_window_move(inst->window->window, x, y);
255 * Move the window to the top or bottom of the z-order in response
256 * to a server-side request.
258 void set_zorder(void *frontend, int top)
260 struct gui_data *inst = (struct gui_data *)frontend;
262 gdk_window_raise(inst->window->window);
264 gdk_window_lower(inst->window->window);
268 * Refresh the window in response to a server-side request.
270 void refresh_window(void *frontend)
272 struct gui_data *inst = (struct gui_data *)frontend;
273 term_invalidate(inst->term);
277 * Maximise or restore the window in response to a server-side
280 void set_zoomed(void *frontend, int zoomed)
283 * GTK 1.2 doesn't know how to do this.
285 #if GTK_CHECK_VERSION(2,0,0)
286 struct gui_data *inst = (struct gui_data *)frontend;
288 gtk_window_maximize(GTK_WINDOW(inst->window));
290 gtk_window_unmaximize(GTK_WINDOW(inst->window));
295 * Report whether the window is iconic, for terminal reports.
297 int is_iconic(void *frontend)
299 struct gui_data *inst = (struct gui_data *)frontend;
300 return !gdk_window_is_viewable(inst->window->window);
304 * Report the window's position, for terminal reports.
306 void get_window_pos(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_get_position(GTK_WINDOW(inst->window), x, y);
317 gdk_window_get_position(inst->window->window, x, y);
322 * Report the window's pixel size, for terminal reports.
324 void get_window_pixels(void *frontend, int *x, int *y)
326 struct gui_data *inst = (struct gui_data *)frontend;
328 * I assume that when the GTK version of this call is available
329 * we should use it. Not sure how it differs from the GDK one,
332 #if GTK_CHECK_VERSION(2,0,0)
333 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
335 gdk_window_get_size(inst->window->window, x, y);
340 * Return the window or icon title.
342 char *get_window_title(void *frontend, int icon)
344 struct gui_data *inst = (struct gui_data *)frontend;
345 return icon ? inst->wintitle : inst->icontitle;
348 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
350 struct gui_data *inst = (struct gui_data *)data;
351 if (!inst->exited && inst->cfg.warn_on_close) {
352 if (!reallyclose(inst))
358 static void show_mouseptr(struct gui_data *inst, int show)
360 if (!inst->cfg.hide_mouseptr)
363 gdk_window_set_cursor(inst->area->window, inst->currcursor);
365 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
366 inst->mouseptr_visible = show;
369 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
371 struct gui_data *inst = (struct gui_data *)data;
372 int w, h, need_size = 0;
376 * See if the terminal size has changed, in which case we must
377 * let the terminal know.
379 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
380 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
381 if (w != inst->width || h != inst->height) {
382 inst->cfg.width = inst->width = w;
383 inst->cfg.height = inst->height = h;
388 gdk_pixmap_unref(inst->pixmap);
392 inst->pixmap = gdk_pixmap_new(widget->window,
393 (inst->cfg.width * inst->font_width +
394 2*inst->cfg.window_border),
395 (inst->cfg.height * inst->font_height +
396 2*inst->cfg.window_border), -1);
398 gc = gdk_gc_new(inst->area->window);
399 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
400 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
401 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
402 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
405 if (need_size && inst->term) {
406 term_size(inst->term, h, w, inst->cfg.savelines);
410 term_invalidate(inst->term);
415 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
417 struct gui_data *inst = (struct gui_data *)data;
420 * Pass the exposed rectangle to terminal.c, which will call us
421 * back to do the actual painting.
424 gdk_draw_pixmap(widget->window,
425 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
427 event->area.x, event->area.y,
428 event->area.x, event->area.y,
429 event->area.width, event->area.height);
434 #define KEY_PRESSED(k) \
435 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
437 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
439 struct gui_data *inst = (struct gui_data *)data;
441 int start, end, special;
443 /* By default, nothing is generated. */
448 * If Alt is being released after typing an Alt+numberpad
449 * sequence, we should generate the code that was typed.
451 * Note that we only do this if more than one key was actually
452 * pressed - I don't think Alt+NumPad4 should be ^D or that
453 * Alt+NumPad3 should be ^C, for example. There's no serious
454 * inconvenience in having to type a zero before a single-digit
457 if (event->type == GDK_KEY_RELEASE &&
458 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
459 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
460 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
462 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
464 output[0] = inst->alt_keycode;
469 if (event->type == GDK_KEY_PRESS) {
473 printf("keypress: keyval = %04x, state = %08x; string =",
474 event->keyval, event->state);
475 for (i = 0; event->string[i]; i++)
476 printf(" %02x", (unsigned char) event->string[i]);
482 * NYI: Compose key (!!! requires Unicode faff before even trying)
486 * If Alt has just been pressed, we start potentially
487 * accumulating an Alt+numberpad code. We do this by
488 * setting alt_keycode to -1 (nothing yet but plausible).
490 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
491 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
492 inst->alt_keycode = -1;
493 inst->alt_digits = 0;
494 goto done; /* this generates nothing else */
498 * If we're seeing a numberpad key press with Mod1 down,
499 * consider adding it to alt_keycode if that's sensible.
500 * Anything _else_ with Mod1 down cancels any possibility
501 * of an ALT keycode: we set alt_keycode to -2.
503 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
505 switch (event->keyval) {
506 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
507 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
508 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
509 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
510 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
511 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
512 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
513 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
514 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
515 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
518 inst->alt_keycode = -2; /* it's invalid */
521 printf("Adding digit %d to keycode %d", digit,
524 if (inst->alt_keycode == -1)
525 inst->alt_keycode = digit; /* one-digit code */
527 inst->alt_keycode = inst->alt_keycode * 10 + digit;
530 printf(" gives new code %d\n", inst->alt_keycode);
532 /* Having used this digit, we now do nothing more with it. */
538 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
541 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
542 term_scroll(inst->term, 0, -inst->cfg.height/2);
545 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
546 term_scroll(inst->term, 0, -1);
549 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
550 term_scroll(inst->term, 0, +inst->cfg.height/2);
553 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
554 term_scroll(inst->term, 0, +1);
559 * Neither does Shift-Ins.
561 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
568 /* ALT+things gives leading Escape. */
570 strncpy(output+1, event->string, 31);
572 end = strlen(output);
573 if (event->state & GDK_MOD1_MASK) {
575 if (end == 1) end = 0;
579 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
580 if (!event->string[0] && event->keyval == '`' &&
581 (event->state & GDK_CONTROL_MASK)) {
586 /* Control-Break is the same as Control-C */
587 if (event->keyval == GDK_Break &&
588 (event->state & GDK_CONTROL_MASK)) {
594 /* We handle Return ourselves, because it needs to be flagged as
595 * special to ldisc. */
596 if (event->keyval == GDK_Return) {
602 /* Control-2, Control-Space and Control-@ are NUL */
603 if (!event->string[0] &&
604 (event->keyval == ' ' || event->keyval == '2' ||
605 event->keyval == '@') &&
606 (event->state & (GDK_SHIFT_MASK |
607 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
612 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
613 if (!event->string[0] && event->keyval == ' ' &&
614 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
615 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
620 /* We don't let GTK tell us what Backspace is! We know better. */
621 if (event->keyval == GDK_BackSpace &&
622 !(event->state & GDK_SHIFT_MASK)) {
623 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
627 /* For Shift Backspace, do opposite of what is configured. */
628 if (event->keyval == GDK_BackSpace &&
629 (event->state & GDK_SHIFT_MASK)) {
630 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
635 /* Shift-Tab is ESC [ Z */
636 if (event->keyval == GDK_ISO_Left_Tab ||
637 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
638 end = 1 + sprintf(output+1, "\033[Z");
642 * NetHack keypad mode.
644 if (inst->cfg.nethack_keypad) {
646 switch (event->keyval) {
647 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
648 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
649 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
650 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
651 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
652 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
653 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
654 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
655 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
659 if (event->state & GDK_SHIFT_MASK)
668 * Application keypad mode.
670 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
672 switch (event->keyval) {
673 case GDK_Num_Lock: xkey = 'P'; break;
674 case GDK_KP_Divide: xkey = 'Q'; break;
675 case GDK_KP_Multiply: xkey = 'R'; break;
676 case GDK_KP_Subtract: xkey = 'S'; break;
678 * Keypad + is tricky. It covers a space that would
679 * be taken up on the VT100 by _two_ keys; so we
680 * let Shift select between the two. Worse still,
681 * in xterm function key mode we change which two...
684 if (inst->cfg.funky_type == 2) {
685 if (event->state & GDK_SHIFT_MASK)
689 } else if (event->state & GDK_SHIFT_MASK)
694 case GDK_KP_Enter: xkey = 'M'; break;
695 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
696 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
697 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
698 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
699 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
700 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
701 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
702 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
703 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
704 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
705 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
708 if (inst->term->vt52_mode) {
709 if (xkey >= 'P' && xkey <= 'S')
710 end = 1 + sprintf(output+1, "\033%c", xkey);
712 end = 1 + sprintf(output+1, "\033?%c", xkey);
714 end = 1 + sprintf(output+1, "\033O%c", xkey);
720 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
721 * for integer decimal nn.)
723 * We also deal with the weird ones here. Linux VCs replace F1
724 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
725 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
730 switch (event->keyval) {
732 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
735 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
738 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
741 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
744 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
747 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
750 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
753 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
756 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
759 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
792 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
793 case GDK_Home: case GDK_KP_Home:
796 case GDK_Insert: case GDK_KP_Insert:
799 case GDK_Delete: case GDK_KP_Delete:
802 case GDK_End: case GDK_KP_End:
805 case GDK_Page_Up: case GDK_KP_Page_Up:
808 case GDK_Page_Down: case GDK_KP_Page_Down:
812 /* Reorder edit keys to physical order */
813 if (inst->cfg.funky_type == 3 && code <= 6)
814 code = "\0\2\1\4\5\3\6"[code];
816 if (inst->term->vt52_mode && code > 0 && code <= 6) {
817 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
821 if (inst->cfg.funky_type == 5 && /* SCO function keys */
822 code >= 11 && code <= 34) {
823 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
825 switch (event->keyval) {
826 case GDK_F1: index = 0; break;
827 case GDK_F2: index = 1; break;
828 case GDK_F3: index = 2; break;
829 case GDK_F4: index = 3; break;
830 case GDK_F5: index = 4; break;
831 case GDK_F6: index = 5; break;
832 case GDK_F7: index = 6; break;
833 case GDK_F8: index = 7; break;
834 case GDK_F9: index = 8; break;
835 case GDK_F10: index = 9; break;
836 case GDK_F11: index = 10; break;
837 case GDK_F12: index = 11; break;
839 if (event->state & GDK_SHIFT_MASK) index += 12;
840 if (event->state & GDK_CONTROL_MASK) index += 24;
841 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
844 if (inst->cfg.funky_type == 5 && /* SCO small keypad */
845 code >= 1 && code <= 6) {
846 char codes[] = "HL.FIG";
851 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
855 if ((inst->term->vt52_mode || inst->cfg.funky_type == 4) &&
856 code >= 11 && code <= 24) {
862 if (inst->term->vt52_mode)
863 end = 1 + sprintf(output+1,
864 "\x1B%c", code + 'P' - 11 - offt);
866 end = 1 + sprintf(output+1,
867 "\x1BO%c", code + 'P' - 11 - offt);
870 if (inst->cfg.funky_type == 1 && code >= 11 && code <= 15) {
871 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
874 if (inst->cfg.funky_type == 2 && code >= 11 && code <= 14) {
875 if (inst->term->vt52_mode)
876 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
878 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
881 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
882 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
886 end = 1 + sprintf(output+1, "\x1B[%d~", code);
892 * Cursor keys. (This includes the numberpad cursor keys,
893 * if we haven't already done them due to app keypad mode.)
895 * Here we also process un-numlocked un-appkeypadded KP5,
896 * which sends ESC [ G.
900 switch (event->keyval) {
901 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
902 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
903 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
904 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
905 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
909 * The arrow keys normally do ESC [ A and so on. In
910 * app cursor keys mode they do ESC O A instead.
911 * Ctrl toggles the two modes.
913 if (inst->term->vt52_mode) {
914 end = 1 + sprintf(output+1, "\033%c", xkey);
915 } else if (!inst->term->app_cursor_keys ^
916 !(event->state & GDK_CONTROL_MASK)) {
917 end = 1 + sprintf(output+1, "\033O%c", xkey);
919 end = 1 + sprintf(output+1, "\033[%c", xkey);
932 printf("generating sequence:");
933 for (i = start; i < end; i++)
934 printf(" %02x", (unsigned char) output[i]);
940 * For special control characters, the character set
941 * should never matter.
943 output[end] = '\0'; /* NUL-terminate */
944 ldisc_send(inst->ldisc, output+start, -2, 1);
945 } else if (!inst->direct_to_font) {
947 * The stuff we've just generated is assumed to be
948 * ISO-8859-1! This sounds insane, but `man
949 * XLookupString' agrees: strings of this type returned
950 * from the X server are hardcoded to 8859-1. Strictly
951 * speaking we should be doing this using some sort of
952 * GtkIMContext, which (if we're lucky) would give us
953 * our data directly in Unicode; but that's not
954 * supported in GTK 1.2 as far as I can tell, and it's
955 * poorly documented even in 2.0, so it'll have to
958 lpage_send(inst->ldisc, CS_ISO8859_1, output+start, end-start, 1);
961 * In direct-to-font mode, we just send the string
962 * exactly as we received it.
964 ldisc_send(inst->ldisc, output+start, end-start, 1);
967 show_mouseptr(inst, 0);
968 term_seen_key_event(inst->term);
969 term_out(inst->term);
975 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
977 struct gui_data *inst = (struct gui_data *)data;
978 int shift, ctrl, alt, x, y, button, act;
980 show_mouseptr(inst, 1);
982 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
983 term_scroll(inst->term, 0, -5);
986 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
987 term_scroll(inst->term, 0, +5);
991 shift = event->state & GDK_SHIFT_MASK;
992 ctrl = event->state & GDK_CONTROL_MASK;
993 alt = event->state & GDK_MOD1_MASK;
995 if (event->button == 3 && ctrl) {
996 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
997 event->button, event->time);
1001 if (event->button == 1)
1003 else if (event->button == 2)
1004 button = MBT_MIDDLE;
1005 else if (event->button == 3)
1008 return FALSE; /* don't even know what button! */
1010 switch (event->type) {
1011 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1012 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1013 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1014 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1015 default: return FALSE; /* don't know this event type */
1018 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
1019 act != MA_CLICK && act != MA_RELEASE)
1020 return TRUE; /* we ignore these in raw mouse mode */
1022 x = (event->x - inst->cfg.window_border) / inst->font_width;
1023 y = (event->y - inst->cfg.window_border) / inst->font_height;
1025 term_mouse(inst->term, button, translate_button(button), act,
1026 x, y, shift, ctrl, alt);
1031 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1033 struct gui_data *inst = (struct gui_data *)data;
1034 int shift, ctrl, alt, x, y, button;
1036 show_mouseptr(inst, 1);
1038 shift = event->state & GDK_SHIFT_MASK;
1039 ctrl = event->state & GDK_CONTROL_MASK;
1040 alt = event->state & GDK_MOD1_MASK;
1041 if (event->state & GDK_BUTTON1_MASK)
1043 else if (event->state & GDK_BUTTON2_MASK)
1044 button = MBT_MIDDLE;
1045 else if (event->state & GDK_BUTTON3_MASK)
1048 return FALSE; /* don't even know what button! */
1050 x = (event->x - inst->cfg.window_border) / inst->font_width;
1051 y = (event->y - inst->cfg.window_border) / inst->font_height;
1053 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1054 x, y, shift, ctrl, alt);
1059 void frontend_keypress(void *handle)
1061 struct gui_data *inst = (struct gui_data *)handle;
1064 * If our child process has exited but not closed, terminate on
1071 gint timer_func(gpointer data)
1073 struct gui_data *inst = (struct gui_data *)data;
1076 if (!inst->exited &&
1077 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1078 inst->exited = TRUE;
1079 if (inst->cfg.close_on_exit == FORCE_ON ||
1080 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1081 exit(0); /* just go. */
1084 term_update(inst->term);
1085 term_blink(inst->term, 0);
1089 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1092 * We must process exceptional notifications before ordinary
1093 * readability ones, or we may go straight past the urgent
1096 if (condition & GDK_INPUT_EXCEPTION)
1097 select_result(sourcefd, 4);
1098 if (condition & GDK_INPUT_READ)
1099 select_result(sourcefd, 1);
1100 if (condition & GDK_INPUT_WRITE)
1101 select_result(sourcefd, 2);
1104 void destroy(GtkWidget *widget, gpointer data)
1109 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1111 struct gui_data *inst = (struct gui_data *)data;
1112 inst->term->has_focus = event->in;
1113 term_out(inst->term);
1114 term_update(inst->term);
1115 show_mouseptr(inst, 1);
1120 * set or clear the "raw mouse message" mode
1122 void set_raw_mouse_mode(void *frontend, int activate)
1124 struct gui_data *inst = (struct gui_data *)frontend;
1125 activate = activate && !inst->cfg.no_mouse_rep;
1126 send_raw_mouse = activate;
1128 inst->currcursor = inst->rawcursor;
1130 inst->currcursor = inst->textcursor;
1131 show_mouseptr(inst, inst->mouseptr_visible);
1134 void request_resize(void *frontend, int w, int h)
1136 struct gui_data *inst = (struct gui_data *)frontend;
1137 int large_x, large_y;
1138 int offset_x, offset_y;
1140 GtkRequisition inner, outer;
1143 * This is a heinous hack dreamed up by the gnome-terminal
1144 * people to get around a limitation in gtk. The problem is
1145 * that in order to set the size correctly we really need to be
1146 * calling gtk_window_resize - but that needs to know the size
1147 * of the _whole window_, not the drawing area. So what we do
1148 * is to set an artificially huge size request on the drawing
1149 * area, recompute the resulting size request on the window,
1150 * and look at the difference between the two. That gives us
1151 * the x and y offsets we need to translate drawing area size
1152 * into window size for real, and then we call
1153 * gtk_window_resize.
1157 * We start by retrieving the current size of the whole window.
1158 * Adding a bit to _that_ will give us a value we can use as a
1159 * bogus size request which guarantees to be bigger than the
1160 * current size of the drawing area.
1162 get_window_pixels(inst, &large_x, &large_y);
1166 #if GTK_CHECK_VERSION(2,0,0)
1167 gtk_widget_set_size_request(inst->area, large_x, large_y);
1169 gtk_widget_set_usize(inst->area, large_x, large_y);
1171 gtk_widget_size_request(inst->area, &inner);
1172 gtk_widget_size_request(inst->window, &outer);
1174 offset_x = outer.width - inner.width;
1175 offset_y = outer.height - inner.height;
1177 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1178 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1181 * Now we must set the size request on the drawing area back to
1182 * something sensible before we commit the real resize. Best
1183 * way to do this, I think, is to set it to what the size is
1184 * really going to end up being.
1186 #if GTK_CHECK_VERSION(2,0,0)
1187 gtk_widget_set_size_request(inst->area, area_x, area_y);
1189 gtk_widget_set_usize(inst->area, area_x, area_y);
1190 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1193 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1195 #if GTK_CHECK_VERSION(2,0,0)
1196 gtk_window_resize(GTK_WINDOW(inst->window),
1197 area_x + offset_x, area_y + offset_y);
1199 gdk_window_resize(inst->window->window,
1200 area_x + offset_x, area_y + offset_y);
1204 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1206 gboolean success[1];
1208 inst->cols[n].red = r * 0x0101;
1209 inst->cols[n].green = g * 0x0101;
1210 inst->cols[n].blue = b * 0x0101;
1212 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1213 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1214 FALSE, FALSE, success);
1216 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1220 void set_window_background(struct gui_data *inst)
1222 if (inst->area && inst->area->window)
1223 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1224 if (inst->window && inst->window->window)
1225 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1228 void palette_set(void *frontend, int n, int r, int g, int b)
1230 struct gui_data *inst = (struct gui_data *)frontend;
1231 static const int first[21] = {
1232 0, 2, 4, 6, 8, 10, 12, 14,
1233 1, 3, 5, 7, 9, 11, 13, 15,
1236 real_palette_set(inst, first[n], r, g, b);
1238 real_palette_set(inst, first[n] + 1, r, g, b);
1240 set_window_background(inst);
1243 void palette_reset(void *frontend)
1245 struct gui_data *inst = (struct gui_data *)frontend;
1246 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1247 static const int ww[] = {
1248 6, 7, 8, 9, 10, 11, 12, 13,
1249 14, 15, 16, 17, 18, 19, 20, 21,
1252 gboolean success[NCOLOURS];
1255 assert(lenof(ww) == NCOLOURS);
1257 if (!inst->colmap) {
1258 inst->colmap = gdk_colormap_get_system();
1260 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1263 for (i = 0; i < NCOLOURS; i++) {
1264 inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
1265 inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
1266 inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
1269 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1270 FALSE, FALSE, success);
1271 for (i = 0; i < NCOLOURS; i++) {
1273 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1274 appname, i, inst->cfg.colours[i][0],
1275 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1278 set_window_background(inst);
1281 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1283 struct gui_data *inst = (struct gui_data *)frontend;
1284 if (inst->pasteout_data)
1285 sfree(inst->pasteout_data);
1286 if (inst->pasteout_data_utf8)
1287 sfree(inst->pasteout_data_utf8);
1290 * Set up UTF-8 paste data. This only happens if we aren't in
1291 * direct-to-font mode using the D800 hack.
1293 if (!inst->direct_to_font) {
1294 wchar_t *tmp = data;
1297 inst->pasteout_data_utf8 = snewn(len*6, char);
1298 inst->pasteout_data_utf8_len = len*6;
1299 inst->pasteout_data_utf8_len =
1300 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1301 inst->pasteout_data_utf8_len,
1302 CS_UTF8, NULL, NULL, 0);
1303 if (inst->pasteout_data_utf8_len == 0) {
1304 sfree(inst->pasteout_data_utf8);
1305 inst->pasteout_data_utf8 = NULL;
1307 inst->pasteout_data_utf8 =
1308 sresize(inst->pasteout_data_utf8,
1309 inst->pasteout_data_utf8_len, char);
1312 inst->pasteout_data_utf8 = NULL;
1313 inst->pasteout_data_utf8_len = 0;
1316 inst->pasteout_data = snewn(len*6, char);
1317 inst->pasteout_data_len = len*6;
1318 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1319 data, len, inst->pasteout_data,
1320 inst->pasteout_data_len,
1322 if (inst->pasteout_data_len == 0) {
1323 sfree(inst->pasteout_data);
1324 inst->pasteout_data = NULL;
1326 inst->pasteout_data =
1327 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1330 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1331 GDK_CURRENT_TIME)) {
1332 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1333 GDK_SELECTION_TYPE_STRING, 1);
1334 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1335 compound_text_atom, 1);
1336 if (inst->pasteout_data_utf8)
1337 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1338 utf8_string_atom, 1);
1342 term_deselect(inst->term);
1345 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1346 guint info, guint time_stamp, gpointer data)
1348 struct gui_data *inst = (struct gui_data *)data;
1349 if (seldata->target == utf8_string_atom)
1350 gtk_selection_data_set(seldata, seldata->target, 8,
1351 inst->pasteout_data_utf8,
1352 inst->pasteout_data_utf8_len);
1354 gtk_selection_data_set(seldata, seldata->target, 8,
1355 inst->pasteout_data, inst->pasteout_data_len);
1358 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1361 struct gui_data *inst = (struct gui_data *)data;
1362 term_deselect(inst->term);
1363 if (inst->pasteout_data)
1364 sfree(inst->pasteout_data);
1365 if (inst->pasteout_data_utf8)
1366 sfree(inst->pasteout_data_utf8);
1367 inst->pasteout_data = NULL;
1368 inst->pasteout_data_len = 0;
1369 inst->pasteout_data_utf8 = NULL;
1370 inst->pasteout_data_utf8_len = 0;
1374 void request_paste(void *frontend)
1376 struct gui_data *inst = (struct gui_data *)frontend;
1378 * In Unix, pasting is asynchronous: all we can do at the
1379 * moment is to call gtk_selection_convert(), and when the data
1380 * comes back _then_ we can call term_do_paste().
1383 if (!inst->direct_to_font) {
1385 * First we attempt to retrieve the selection as a UTF-8
1386 * string (which we will convert to the correct code page
1387 * before sending to the session, of course). If that
1388 * fails, selection_received() will be informed and will
1389 * fall back to an ordinary string.
1391 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1392 utf8_string_atom, GDK_CURRENT_TIME);
1395 * If we're in direct-to-font mode, we disable UTF-8
1396 * pasting, and go straight to ordinary string data.
1398 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1399 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1403 gint idle_paste_func(gpointer data); /* forward ref */
1405 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1406 guint time, gpointer data)
1408 struct gui_data *inst = (struct gui_data *)data;
1410 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1412 * Failed to get a UTF-8 selection string. Try an ordinary
1415 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1416 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1421 * Any other failure should just go foom.
1423 if (seldata->length <= 0 ||
1424 (seldata->type != GDK_SELECTION_TYPE_STRING &&
1425 seldata->type != utf8_string_atom))
1426 return; /* Nothing happens. */
1428 if (inst->pastein_data)
1429 sfree(inst->pastein_data);
1431 inst->pastein_data = snewn(seldata->length, wchar_t);
1432 inst->pastein_data_len = seldata->length;
1433 inst->pastein_data_len =
1434 mb_to_wc((seldata->type == utf8_string_atom ?
1435 CS_UTF8 : inst->ucsdata.line_codepage),
1436 0, seldata->data, seldata->length,
1437 inst->pastein_data, inst->pastein_data_len);
1439 term_do_paste(inst->term);
1441 if (term_paste_pending(inst->term))
1442 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1445 gint idle_paste_func(gpointer data)
1447 struct gui_data *inst = (struct gui_data *)data;
1449 if (term_paste_pending(inst->term))
1450 term_paste(inst->term);
1452 gtk_idle_remove(inst->term_paste_idle_id);
1458 void get_clip(void *frontend, wchar_t ** p, int *len)
1460 struct gui_data *inst = (struct gui_data *)frontend;
1463 *p = inst->pastein_data;
1464 *len = inst->pastein_data_len;
1468 void set_title(void *frontend, char *title)
1470 struct gui_data *inst = (struct gui_data *)frontend;
1471 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1472 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1473 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1476 void set_icon(void *frontend, char *title)
1478 struct gui_data *inst = (struct gui_data *)frontend;
1479 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1480 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1481 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1484 void set_sbar(void *frontend, int total, int start, int page)
1486 struct gui_data *inst = (struct gui_data *)frontend;
1487 if (!inst->cfg.scrollbar)
1489 inst->sbar_adjust->lower = 0;
1490 inst->sbar_adjust->upper = total;
1491 inst->sbar_adjust->value = start;
1492 inst->sbar_adjust->page_size = page;
1493 inst->sbar_adjust->step_increment = 1;
1494 inst->sbar_adjust->page_increment = page/2;
1495 inst->ignore_sbar = TRUE;
1496 gtk_adjustment_changed(inst->sbar_adjust);
1497 inst->ignore_sbar = FALSE;
1500 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1502 struct gui_data *inst = (struct gui_data *)data;
1504 if (!inst->cfg.scrollbar)
1506 if (!inst->ignore_sbar)
1507 term_scroll(inst->term, 1, (int)adj->value);
1510 void sys_cursor(void *frontend, int x, int y)
1513 * This is meaningless under X.
1518 * This is still called when mode==BELL_VISUAL, even though the
1519 * visual bell is handled entirely within terminal.c, because we
1520 * may want to perform additional actions on any kind of bell (for
1521 * example, taskbar flashing in Windows).
1523 void beep(void *frontend, int mode)
1525 if (mode != BELL_VISUAL)
1529 int char_width(Context ctx, int uc)
1532 * Under X, any fixed-width font really _is_ fixed-width.
1533 * Double-width characters will be dealt with using a separate
1534 * font. For the moment we can simply return 1.
1539 Context get_ctx(void *frontend)
1541 struct gui_data *inst = (struct gui_data *)frontend;
1542 struct draw_ctx *dctx;
1544 if (!inst->area->window)
1547 dctx = snew(struct draw_ctx);
1549 dctx->gc = gdk_gc_new(inst->area->window);
1553 void free_ctx(Context ctx)
1555 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1556 /* struct gui_data *inst = dctx->inst; */
1557 GdkGC *gc = dctx->gc;
1563 * Draw a line of text in the window, at given character
1564 * coordinates, in given attributes.
1566 * We are allowed to fiddle with the contents of `text'.
1568 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1569 unsigned long attr, int lattr)
1571 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1572 struct gui_data *inst = dctx->inst;
1573 GdkGC *gc = dctx->gc;
1575 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1577 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1578 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
1579 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1580 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
1581 if (attr & ATTR_REVERSE) {
1586 if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
1588 if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
1590 if (attr & TATTR_ACTCURS) {
1595 fontid = shadow = 0;
1597 if (attr & ATTR_WIDE) {
1604 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1605 if (inst->fonts[fontid | 1])
1611 if (lattr != LATTR_NORM) {
1613 if (x >= inst->term->cols)
1615 if (x + len*2*widefactor > inst->term->cols)
1616 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1624 r.x = x*inst->font_width+inst->cfg.window_border;
1625 r.y = y*inst->font_height+inst->cfg.window_border;
1626 r.width = rlen*widefactor*inst->font_width;
1627 r.height = inst->font_height;
1628 gdk_gc_set_clip_rectangle(gc, &r);
1631 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1632 gdk_draw_rectangle(inst->pixmap, gc, 1,
1633 x*inst->font_width+inst->cfg.window_border,
1634 y*inst->font_height+inst->cfg.window_border,
1635 rlen*widefactor*inst->font_width, inst->font_height);
1637 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1644 wcs = snewn(len+1, wchar_t);
1645 for (i = 0; i < len; i++) {
1646 wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
1649 if (inst->fonts[fontid] == NULL) {
1651 * The font for this contingency does not exist.
1652 * Typically this means we've been given ATTR_WIDE
1653 * character and have no wide font. So we display
1654 * nothing at all; such is life.
1656 } else if (inst->fontinfo[fontid].is_wide) {
1658 * At least one version of gdk_draw_text_wc() has a
1659 * weird bug whereby it reads `len' elements of the
1660 * input string, but only draws `len/2'. Hence I'm
1661 * going to make its input array twice as long as it
1662 * theoretically needs to be, and pass in twice the
1663 * actual number of characters. If a fixed gdk actually
1664 * takes the doubled length seriously, then (a) the
1665 * array will stand scrutiny up to the full length, (b)
1666 * the spare elements of the array are full of zeroes
1667 * which will probably be an empty glyph in the font,
1668 * and (c) the clip rectangle should prevent it causing
1671 gwcs = snewn(len*2+1, GdkWChar);
1672 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
1674 * FIXME: when we have a wide-char equivalent of
1675 * from_unicode, use it instead of this.
1677 for (i = 0; i <= len; i++)
1679 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
1680 x*inst->font_width+inst->cfg.window_border,
1681 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1685 gcs = snewn(len+1, gchar);
1686 wc_to_mb(inst->fontinfo[fontid].charset, 0,
1687 wcs, len, gcs, len, ".", NULL, NULL);
1688 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1689 x*inst->font_width+inst->cfg.window_border,
1690 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1698 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1699 x*inst->font_width+inst->cfg.window_border + inst->cfg.shadowboldoffset,
1700 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1704 if (attr & ATTR_UNDER) {
1705 int uheight = inst->fonts[0]->ascent + 1;
1706 if (uheight >= inst->font_height)
1707 uheight = inst->font_height - 1;
1708 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
1709 y*inst->font_height + uheight + inst->cfg.window_border,
1710 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
1711 y*inst->font_height + uheight + inst->cfg.window_border);
1714 if (lattr != LATTR_NORM) {
1716 * I can't find any plausible StretchBlt equivalent in the
1717 * X server, so I'm going to do this the slow and painful
1718 * way. This will involve repeated calls to
1719 * gdk_draw_pixmap() to stretch the text horizontally. It's
1720 * O(N^2) in time and O(N) in network bandwidth, but you
1721 * try thinking of a better way. :-(
1724 for (i = 0; i < len * widefactor * inst->font_width; i++) {
1725 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1726 x*inst->font_width+inst->cfg.window_border + 2*i,
1727 y*inst->font_height+inst->cfg.window_border,
1728 x*inst->font_width+inst->cfg.window_border + 2*i+1,
1729 y*inst->font_height+inst->cfg.window_border,
1730 len * inst->font_width - i, inst->font_height);
1733 if (lattr != LATTR_WIDE) {
1735 /* Now stretch vertically, in the same way. */
1736 if (lattr == LATTR_BOT)
1740 for (i = 0; i < inst->font_height; i+=2) {
1741 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1742 x*inst->font_width+inst->cfg.window_border,
1743 y*inst->font_height+inst->cfg.window_border+dt*i+db,
1744 x*widefactor*inst->font_width+inst->cfg.window_border,
1745 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
1746 len * inst->font_width, inst->font_height-i-1);
1752 void do_text(Context ctx, int x, int y, char *text, int len,
1753 unsigned long attr, int lattr)
1755 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1756 struct gui_data *inst = dctx->inst;
1757 GdkGC *gc = dctx->gc;
1760 do_text_internal(ctx, x, y, text, len, attr, lattr);
1762 if (attr & ATTR_WIDE) {
1768 if (lattr != LATTR_NORM) {
1770 if (x >= inst->term->cols)
1772 if (x + len*2*widefactor > inst->term->cols)
1773 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1777 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1778 x*inst->font_width+inst->cfg.window_border,
1779 y*inst->font_height+inst->cfg.window_border,
1780 x*inst->font_width+inst->cfg.window_border,
1781 y*inst->font_height+inst->cfg.window_border,
1782 len*widefactor*inst->font_width, inst->font_height);
1785 void do_cursor(Context ctx, int x, int y, char *text, int len,
1786 unsigned long attr, int lattr)
1788 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1789 struct gui_data *inst = dctx->inst;
1790 GdkGC *gc = dctx->gc;
1792 int passive, widefactor;
1794 if (attr & TATTR_PASCURS) {
1795 attr &= ~TATTR_PASCURS;
1799 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
1800 attr &= ~TATTR_ACTCURS;
1802 do_text_internal(ctx, x, y, text, len, attr, lattr);
1804 if (attr & ATTR_WIDE) {
1810 if (lattr != LATTR_NORM) {
1812 if (x >= inst->term->cols)
1814 if (x + len*2*widefactor > inst->term->cols)
1815 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1819 if (inst->cfg.cursor_type == 0) {
1821 * An active block cursor will already have been done by
1822 * the above do_text call, so we only need to do anything
1826 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1827 gdk_draw_rectangle(inst->pixmap, gc, 0,
1828 x*inst->font_width+inst->cfg.window_border,
1829 y*inst->font_height+inst->cfg.window_border,
1830 len*inst->font_width-1, inst->font_height-1);
1834 int startx, starty, dx, dy, length, i;
1838 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1839 char_width = 2*inst->font_width;
1841 char_width = inst->font_width;
1843 if (inst->cfg.cursor_type == 1) {
1844 uheight = inst->fonts[0]->ascent + 1;
1845 if (uheight >= inst->font_height)
1846 uheight = inst->font_height - 1;
1848 startx = x * inst->font_width + inst->cfg.window_border;
1849 starty = y * inst->font_height + inst->cfg.window_border + uheight;
1852 length = len * char_width;
1855 if (attr & TATTR_RIGHTCURS)
1856 xadjust = char_width - 1;
1857 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
1858 starty = y * inst->font_height + inst->cfg.window_border;
1861 length = inst->font_height;
1864 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1866 for (i = 0; i < length; i++) {
1868 gdk_draw_point(inst->pixmap, gc, startx, starty);
1874 gdk_draw_line(inst->pixmap, gc, startx, starty,
1875 startx + (length-1) * dx, starty + (length-1) * dy);
1879 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1880 x*inst->font_width+inst->cfg.window_border,
1881 y*inst->font_height+inst->cfg.window_border,
1882 x*inst->font_width+inst->cfg.window_border,
1883 y*inst->font_height+inst->cfg.window_border,
1884 len*widefactor*inst->font_width, inst->font_height);
1887 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1890 * Truly hideous hack: GTK doesn't allow us to set the mouse
1891 * cursor foreground and background colours unless we've _also_
1892 * created our own cursor from bitmaps. Therefore, I need to
1893 * load the `cursor' font and draw glyphs from it on to
1894 * pixmaps, in order to construct my cursors with the fg and bg
1895 * I want. This is a gross hack, but it's more self-contained
1896 * than linking in Xlib to find the X window handle to
1897 * inst->area and calling XRecolorCursor, and it's more
1898 * futureproof than hard-coding the shapes as bitmap arrays.
1900 static GdkFont *cursor_font = NULL;
1901 GdkPixmap *source, *mask;
1903 GdkColor cfg = { 0, 65535, 65535, 65535 };
1904 GdkColor cbg = { 0, 0, 0, 0 };
1905 GdkColor dfg = { 1, 65535, 65535, 65535 };
1906 GdkColor dbg = { 0, 0, 0, 0 };
1909 gint lb, rb, wid, asc, desc, w, h, x, y;
1911 if (cursor_val == -2) {
1912 gdk_font_unref(cursor_font);
1916 if (cursor_val >= 0 && !cursor_font)
1917 cursor_font = gdk_font_load("cursor");
1920 * Get the text extent of the cursor in question. We use the
1921 * mask character for this, because it's typically slightly
1922 * bigger than the main character.
1924 if (cursor_val >= 0) {
1926 text[0] = (char)cursor_val + 1;
1927 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1928 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1934 source = gdk_pixmap_new(NULL, w, h, 1);
1935 mask = gdk_pixmap_new(NULL, w, h, 1);
1938 * Draw the mask character on the mask pixmap.
1940 gc = gdk_gc_new(mask);
1941 gdk_gc_set_foreground(gc, &dbg);
1942 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1943 if (cursor_val >= 0) {
1945 text[0] = (char)cursor_val + 1;
1946 gdk_gc_set_foreground(gc, &dfg);
1947 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1952 * Draw the main character on the source pixmap.
1954 gc = gdk_gc_new(source);
1955 gdk_gc_set_foreground(gc, &dbg);
1956 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1957 if (cursor_val >= 0) {
1959 text[0] = (char)cursor_val;
1960 gdk_gc_set_foreground(gc, &dfg);
1961 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1966 * Create the cursor.
1968 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1973 gdk_pixmap_unref(source);
1974 gdk_pixmap_unref(mask);
1979 void modalfatalbox(char *p, ...)
1982 fprintf(stderr, "FATAL ERROR: ");
1984 vfprintf(stderr, p, ap);
1986 fputc('\n', stderr);
1990 void cmdline_error(char *p, ...)
1993 fprintf(stderr, "%s: ", appname);
1995 vfprintf(stderr, p, ap);
1997 fputc('\n', stderr);
2001 char *get_x_display(void *frontend)
2003 return gdk_get_display();
2006 long get_windowid(void *frontend)
2008 struct gui_data *inst = (struct gui_data *)frontend;
2009 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2012 static void help(FILE *fp) {
2014 "pterm option summary:\n"
2016 " --display DISPLAY Specify X display to use (note '--')\n"
2017 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2018 " -fn FONT Normal text font\n"
2019 " -fb FONT Bold text font\n"
2020 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2021 " -sl LINES Number of lines of scrollback\n"
2022 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2023 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2024 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2025 " -T TITLE Window title\n"
2026 " -ut, +ut Do(default) or do not update utmp\n"
2027 " -ls, +ls Do(default) or do not make shell a login shell\n"
2028 " -sb, +sb Do(default) or do not display a scrollbar\n"
2029 " -log PATH Log all output to a file\n"
2030 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2031 " -xrm RESOURCE-STRING Set an X resource\n"
2032 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2033 ) < 0 || fflush(fp) < 0) {
2034 perror("output error");
2039 int do_cmdline(int argc, char **argv, int do_everything,
2040 struct gui_data *inst, Config *cfg)
2046 * Macros to make argument handling easier. Note that because
2047 * they need to call `continue', they cannot be contained in
2048 * the usual do {...} while (0) wrapper to make them
2049 * syntactically single statements; hence it is not legal to
2050 * use one of these macros as an unbraced statement between
2053 #define EXPECTS_ARG { \
2054 if (--argc <= 0) { \
2056 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2061 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2063 while (--argc > 0) {
2067 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2068 do_everything ? 1 : -1, cfg);
2071 cmdline_error("option \"%s\" requires an argument", p);
2072 } else if (ret == 2) {
2073 --argc, ++argv; /* skip next argument */
2075 } else if (ret == 1) {
2079 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2082 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2083 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2085 } else if (!strcmp(p, "-fb")) {
2088 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2089 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2091 } else if (!strcmp(p, "-fw")) {
2094 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2095 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2097 } else if (!strcmp(p, "-fwb")) {
2100 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2101 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2103 } else if (!strcmp(p, "-cs")) {
2106 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2107 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2109 } else if (!strcmp(p, "-geometry")) {
2110 int flags, x, y, w, h;
2114 flags = XParseGeometry(val, &x, &y, &w, &h);
2115 if (flags & WidthValue)
2117 if (flags & HeightValue)
2120 if (flags & (XValue | YValue)) {
2123 inst->gotpos = TRUE;
2124 inst->gravity = ((flags & XNegative ? 1 : 0) |
2125 (flags & YNegative ? 2 : 0));
2128 } else if (!strcmp(p, "-sl")) {
2131 cfg->savelines = atoi(val);
2133 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2134 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2135 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2140 if (!gdk_color_parse(val, &col)) {
2142 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2146 index = (!strcmp(p, "-fg") ? 0 :
2147 !strcmp(p, "-bg") ? 2 :
2148 !strcmp(p, "-bfg") ? 1 :
2149 !strcmp(p, "-bbg") ? 3 :
2150 !strcmp(p, "-cfg") ? 4 :
2151 !strcmp(p, "-cbg") ? 5 : -1);
2152 assert(index != -1);
2153 cfg->colours[index][0] = col.red / 256;
2154 cfg->colours[index][1] = col.green / 256;
2155 cfg->colours[index][2] = col.blue / 256;
2158 } else if (use_pty_argv && !strcmp(p, "-e")) {
2159 /* This option swallows all further arguments. */
2165 pty_argv = snewn(argc+1, char *);
2167 for (i = 0; i < argc; i++)
2168 pty_argv[i] = argv[i];
2169 pty_argv[argc] = NULL;
2170 break; /* finished command-line processing */
2172 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2175 } else if (!strcmp(p, "-title")) {
2178 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2179 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2181 } else if (!strcmp(p, "-log")) {
2184 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2185 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2186 cfg->logtype = LGTYP_DEBUG;
2188 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2190 cfg->stamp_utmp = 0;
2192 } else if (!strcmp(p, "-ut")) {
2194 cfg->stamp_utmp = 1;
2196 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2198 cfg->login_shell = 0;
2200 } else if (!strcmp(p, "-ls")) {
2202 cfg->login_shell = 1;
2204 } else if (!strcmp(p, "-nethack")) {
2206 cfg->nethack_keypad = 1;
2208 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2212 } else if (!strcmp(p, "-sb")) {
2216 } else if (!strcmp(p, "-name")) {
2220 } else if (!strcmp(p, "-xrm")) {
2222 provide_xrm_string(val);
2224 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2228 } else if(p[0] != '-' && (!do_everything ||
2229 process_nonoption_arg(p, cfg))) {
2234 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2241 static void block_signal(int sig, int block_it) {
2245 sigaddset(&ss, sig);
2246 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2247 perror("sigprocmask");
2253 * This function retrieves the character set encoding of a font. It
2254 * returns the character set without the X11 hack (in case the user
2255 * asks to use the font's own encoding).
2257 static int set_font_info(struct gui_data *inst, int fontid)
2259 GdkFont *font = inst->fonts[fontid];
2260 XFontStruct *xfs = GDK_FONT_XFONT(font);
2261 Display *disp = GDK_FONT_XDISPLAY(font);
2262 Atom charset_registry, charset_encoding;
2263 unsigned long registry_ret, encoding_ret;
2264 int retval = CS_NONE;
2266 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2267 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2268 inst->fontinfo[fontid].charset = CS_NONE;
2269 inst->fontinfo[fontid].is_wide = 0;
2270 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2271 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2273 reg = XGetAtomName(disp, (Atom)registry_ret);
2274 enc = XGetAtomName(disp, (Atom)encoding_ret);
2276 char *encoding = dupcat(reg, "-", enc, NULL);
2277 retval = inst->fontinfo[fontid].charset =
2278 charset_from_xenc(encoding);
2279 /* FIXME: when libcharset supports wide encodings fix this. */
2280 if (!strcasecmp(encoding, "iso10646-1")) {
2281 inst->fontinfo[fontid].is_wide = 1;
2286 * Hack for X line-drawing characters: if the primary
2287 * font is encoded as ISO-8859-anything, and has valid
2288 * glyphs in the first 32 char positions, it is assumed
2289 * that those glyphs are the VT100 line-drawing
2292 * Actually, we'll hack even harder by only checking
2293 * position 0x19 (vertical line, VT100 linedrawing
2294 * `x'). Then we can check it easily by seeing if the
2295 * ascent and descent differ.
2297 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2298 int lb, rb, wid, asc, desc;
2303 gdk_string_extents(inst->fonts[fontid], text,
2304 &lb, &rb, &wid, &asc, &desc);
2306 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2316 int uxsel_input_add(int fd, int rwx) {
2318 if (rwx & 1) flags |= GDK_INPUT_READ;
2319 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2320 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2321 return gdk_input_add(fd, flags, fd_input_func, NULL);
2324 void uxsel_input_remove(int id) {
2325 gdk_input_remove(id);
2328 void setup_fonts_ucs(struct gui_data *inst)
2333 gdk_font_unref(inst->fonts[0]);
2335 gdk_font_unref(inst->fonts[1]);
2337 gdk_font_unref(inst->fonts[2]);
2339 gdk_font_unref(inst->fonts[3]);
2341 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2342 if (!inst->fonts[0]) {
2343 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2344 inst->cfg.font.name);
2347 font_charset = set_font_info(inst, 0);
2348 if (inst->cfg.boldfont.name[0]) {
2349 inst->fonts[1] = gdk_font_load(inst->cfg.boldfont.name);
2350 if (!inst->fonts[1]) {
2351 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2352 inst->cfg.boldfont.name);
2355 set_font_info(inst, 1);
2357 inst->fonts[1] = NULL;
2358 if (inst->cfg.widefont.name[0]) {
2359 inst->fonts[2] = gdk_font_load(inst->cfg.widefont.name);
2360 if (!inst->fonts[2]) {
2361 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2362 inst->cfg.widefont.name);
2365 set_font_info(inst, 2);
2367 inst->fonts[2] = NULL;
2368 if (inst->cfg.wideboldfont.name[0]) {
2369 inst->fonts[3] = gdk_font_load(inst->cfg.wideboldfont.name);
2370 if (!inst->fonts[3]) {
2371 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n",
2372 appname, inst->cfg.wideboldfont.name);
2375 set_font_info(inst, 3);
2377 inst->fonts[3] = NULL;
2379 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2380 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2382 inst->direct_to_font = init_ucs(&inst->ucsdata,
2383 inst->cfg.line_codepage, font_charset);
2386 void set_geom_hints(struct gui_data *inst)
2389 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2390 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2391 geom.max_width = geom.max_height = -1;
2392 geom.base_width = 2*inst->cfg.window_border;
2393 geom.base_height = 2*inst->cfg.window_border;
2394 geom.width_inc = inst->font_width;
2395 geom.height_inc = inst->font_height;
2396 geom.min_aspect = geom.max_aspect = 0;
2397 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2398 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2399 GDK_HINT_RESIZE_INC);
2402 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2404 struct gui_data *inst = (struct gui_data *)data;
2405 term_clrsb(inst->term);
2408 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2410 struct gui_data *inst = (struct gui_data *)data;
2411 term_pwron(inst->term);
2412 ldisc_send(inst->ldisc, NULL, 0, 0);
2415 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2417 struct gui_data *inst = (struct gui_data *)data;
2418 term_copyall(inst->term);
2421 void special_menuitem(GtkMenuItem *item, gpointer data)
2423 struct gui_data *inst = (struct gui_data *)data;
2424 int code = (int)gtk_object_get_data(GTK_OBJECT(item), "user-data");
2426 inst->back->special(inst->backhandle, code);
2429 void about_menuitem(GtkMenuItem *item, gpointer data)
2431 struct gui_data *inst = (struct gui_data *)data;
2432 about_box(inst->window);
2435 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2437 struct gui_data *inst = (struct gui_data *)data;
2438 showeventlog(inst->eventlogstuff, inst->window);
2441 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2443 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2444 static const int ww[] = {
2445 6, 7, 8, 9, 10, 11, 12, 13,
2446 14, 15, 16, 17, 18, 19, 20, 21,
2449 struct gui_data *inst = (struct gui_data *)data;
2450 char *title = dupcat(appname, " Reconfiguration", NULL);
2451 Config cfg2, oldcfg;
2454 cfg2 = inst->cfg; /* structure copy */
2456 if (do_config_box(title, &cfg2, 1)) {
2458 oldcfg = inst->cfg; /* structure copy */
2459 inst->cfg = cfg2; /* structure copy */
2461 /* Pass new config data to the logging module */
2462 log_reconfig(inst->logctx, &cfg2);
2464 * Flush the line discipline's edit buffer in the case
2465 * where local editing has just been disabled.
2467 ldisc_send(inst->ldisc, NULL, 0, 0);
2468 /* Pass new config data to the terminal */
2469 term_reconfig(inst->term, &cfg2);
2470 /* Pass new config data to the back end */
2471 inst->back->reconfig(inst->backhandle, &cfg2);
2474 * Just setting inst->cfg is sufficient to cause colour
2475 * setting changes to appear on the next ESC]R palette
2476 * reset. But we should also check whether any colour
2477 * settings have been changed, and revert the ones that
2478 * have to the new default, on the assumption that the user
2479 * is most likely to want an immediate update.
2481 for (i = 0; i < NCOLOURS; i++) {
2482 if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
2483 oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
2484 oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2])
2485 real_palette_set(inst, i, cfg2.colours[ww[i]][0],
2486 cfg2.colours[ww[i]][1],
2487 cfg2.colours[ww[i]][2]);
2491 * If the scrollbar needs to be shown, hidden, or moved
2492 * from one end to the other of the window, do so now.
2494 if (oldcfg.scrollbar != cfg2.scrollbar) {
2496 gtk_widget_show(inst->sbar);
2498 gtk_widget_hide(inst->sbar);
2500 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2501 gtk_box_reorder_child(inst->hbox, inst->sbar,
2502 cfg2.scrollbar_on_left ? 0 : 1);
2506 * Change the window title, if required.
2508 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2509 set_title(inst, cfg2.wintitle);
2512 * Redo the whole tangled fonts and Unicode mess if
2515 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2516 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2517 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2518 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
2519 strcmp(oldcfg.line_codepage, cfg2.line_codepage)) {
2520 setup_fonts_ucs(inst);
2526 * Resize the window.
2528 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2529 oldcfg.window_border != cfg2.window_border || need_size) {
2530 set_geom_hints(inst);
2531 request_resize(inst, cfg2.width, cfg2.height);
2534 * The above will have caused a call to term_size() for
2535 * us if it happened. If the user has fiddled with only
2536 * the scrollback size, the above will not have
2537 * happened and we will need an explicit term_size()
2540 if (oldcfg.savelines != cfg2.savelines)
2541 term_size(inst->term, inst->term->rows, inst->term->cols,
2545 term_invalidate(inst->term);
2550 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
2553 * Re-execing ourself is not an exact science under Unix. I do
2554 * the best I can by using /proc/self/exe if available and by
2555 * assuming argv[0] can be found on $PATH if not.
2557 * Note that we also have to reconstruct the elements of the
2558 * original argv which gtk swallowed, since the user wants the
2559 * new session to appear on the same X display as the old one.
2567 * Collect the arguments with which to re-exec ourself.
2569 va_start(ap, fd_to_close);
2570 n = 2; /* progname and terminating NULL */
2571 n += inst->ngtkargs;
2572 while (va_arg(ap, char *) != NULL)
2576 args = snewn(n, char *);
2577 args[0] = inst->progname;
2579 for (i = 0; i < inst->ngtkargs; i++)
2580 args[i+1] = inst->gtkargvstart[i];
2583 va_start(ap, fd_to_close);
2584 while ((args[i++] = va_arg(ap, char *)) != NULL);
2590 * Do the double fork.
2603 } else if (pid2 > 0) {
2605 * First child has successfully forked second child. My
2606 * Work Here Is Done. Note the use of _exit rather than
2607 * exit: the latter appears to cause destroy messages
2608 * to be sent to the X server. I suspect gtk uses
2615 * If we reach here, we are the second child, so we now
2616 * actually perform the exec.
2618 if (fd_to_close >= 0)
2621 execv("/proc/self/exe", args);
2622 execvp(inst->progname, args);
2628 waitpid(pid, &status, 0);
2633 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
2635 struct gui_data *inst = (struct gui_data *)gdata;
2637 * For this feature we must marshal cfg and (possibly) pty_argv
2638 * into a byte stream, create a pipe, and send this byte stream
2639 * to the child through the pipe.
2646 if (pipe(pipefd) < 0) {
2651 size = sizeof(inst->cfg);
2652 if (use_pty_argv && pty_argv) {
2653 for (i = 0; pty_argv[i]; i++)
2654 size += strlen(pty_argv[i]) + 1;
2657 data = snewn(size, char);
2658 memcpy(data, &inst->cfg, sizeof(inst->cfg));
2659 if (use_pty_argv && pty_argv) {
2660 int p = sizeof(inst->cfg);
2661 for (i = 0; pty_argv[i]; i++) {
2662 strcpy(data + p, pty_argv[i]);
2663 p += strlen(pty_argv[i]) + 1;
2668 sprintf(option, "---[%d,%d]", pipefd[0], size);
2669 fcntl(pipefd[0], F_SETFD, 0);
2670 fork_and_exec_self(inst, pipefd[1], option, NULL);
2674 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
2677 perror("write to pipe");
2682 int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
2684 int fd, i, ret, size;
2687 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
2688 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
2692 data = snewn(size, char);
2694 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
2697 perror("read from pipe");
2699 } else if (i < size) {
2700 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
2705 memcpy(cfg, data, sizeof(Config));
2706 if (use_pty_argv && size > sizeof(Config)) {
2710 while (i < size && data[i]) i++;
2712 fprintf(stderr, "%s: malformed Duplicate Session data\n",
2719 pty_argv = snewn(n+1, char *);
2725 while (i < size && data[i]) i++;
2728 pty_argv[n++] = dupstr(p);
2735 void new_session_menuitem(GtkMenuItem *item, gpointer data)
2737 struct gui_data *inst = (struct gui_data *)data;
2739 fork_and_exec_self(inst, -1, NULL);
2742 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
2744 struct gui_data *inst = (struct gui_data *)data;
2745 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
2747 fork_and_exec_self(inst, -1, "-load", str, NULL);
2750 void saved_session_freedata(GtkMenuItem *item, gpointer data)
2752 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
2757 void update_specials_menu(void *frontend)
2759 struct gui_data *inst = (struct gui_data *)frontend;
2761 const struct telnet_special *specials;
2763 specials = inst->back->get_specials(inst->backhandle);
2764 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
2765 (GtkCallback)gtk_widget_destroy, NULL);
2768 GtkWidget *menuitem;
2769 for (i = 0; specials[i].name; i++) {
2770 if (*specials[i].name) {
2771 menuitem = gtk_menu_item_new_with_label(specials[i].name);
2772 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2773 (gpointer)specials[i].code);
2774 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2775 GTK_SIGNAL_FUNC(special_menuitem), inst);
2777 menuitem = gtk_menu_item_new();
2778 gtk_container_add(GTK_CONTAINER(inst->specialsmenu), menuitem);
2779 gtk_widget_show(menuitem);
2781 gtk_widget_show(inst->specialsitem1);
2782 gtk_widget_show(inst->specialsitem2);
2784 gtk_widget_hide(inst->specialsitem1);
2785 gtk_widget_hide(inst->specialsitem2);
2789 int pt_main(int argc, char **argv)
2791 extern Backend *select_backend(Config *cfg);
2792 extern int cfgbox(Config *cfg);
2793 struct gui_data *inst;
2796 * Create an instance structure and initialise to zeroes
2798 inst = snew(struct gui_data);
2799 memset(inst, 0, sizeof(*inst));
2800 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
2802 /* defer any child exit handling until we're ready to deal with
2804 block_signal(SIGCHLD, 1);
2806 inst->progname = argv[0];
2808 * Copy the original argv before letting gtk_init fiddle with
2809 * it. It will be required later.
2813 inst->gtkargvstart = snewn(argc-1, char *);
2814 for (i = 1; i < argc; i++)
2815 inst->gtkargvstart[i-1] = dupstr(argv[i]);
2817 gtk_init(&argc, &argv);
2818 inst->ngtkargs = oldargc - argc;
2821 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
2822 read_dupsession_data(inst, &inst->cfg, argv[1]);
2823 /* Splatter this argument so it doesn't clutter a ps listing */
2824 memset(argv[1], 0, strlen(argv[1]));
2826 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
2827 exit(1); /* pre-defaults pass to get -class */
2828 do_defaults(NULL, &inst->cfg);
2829 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
2830 exit(1); /* post-defaults, do everything */
2832 cmdline_run_saved(&inst->cfg);
2834 if (!*inst->cfg.host && !cfgbox(&inst->cfg))
2835 exit(0); /* config box hit Cancel */
2838 if (!compound_text_atom)
2839 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2840 if (!utf8_string_atom)
2841 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
2843 setup_fonts_ucs(inst);
2845 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2848 * Set up the colour map.
2850 palette_reset(inst);
2852 inst->width = inst->cfg.width;
2853 inst->height = inst->cfg.height;
2855 inst->area = gtk_drawing_area_new();
2856 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
2857 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
2858 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
2859 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2860 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2861 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2863 * We always create the scrollbar; it remains invisible if
2864 * unwanted, so we can pop it up quickly if it suddenly becomes
2867 if (inst->cfg.scrollbar_on_left)
2868 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2869 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2870 if (!inst->cfg.scrollbar_on_left)
2871 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2873 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2875 set_geom_hints(inst);
2877 gtk_widget_show(inst->area);
2878 if (inst->cfg.scrollbar)
2879 gtk_widget_show(inst->sbar);
2881 gtk_widget_hide(inst->sbar);
2882 gtk_widget_show(GTK_WIDGET(inst->hbox));
2885 int x = inst->xpos, y = inst->ypos;
2887 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
2888 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
2889 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
2890 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
2891 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
2894 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2895 GTK_SIGNAL_FUNC(destroy), inst);
2896 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2897 GTK_SIGNAL_FUNC(delete_window), inst);
2898 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2899 GTK_SIGNAL_FUNC(key_event), inst);
2900 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2901 GTK_SIGNAL_FUNC(key_event), inst);
2902 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2903 GTK_SIGNAL_FUNC(focus_event), inst);
2904 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2905 GTK_SIGNAL_FUNC(focus_event), inst);
2906 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2907 GTK_SIGNAL_FUNC(configure_area), inst);
2908 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2909 GTK_SIGNAL_FUNC(expose_area), inst);
2910 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2911 GTK_SIGNAL_FUNC(button_event), inst);
2912 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2913 GTK_SIGNAL_FUNC(button_event), inst);
2914 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2915 GTK_SIGNAL_FUNC(motion_event), inst);
2916 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2917 GTK_SIGNAL_FUNC(selection_received), inst);
2918 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2919 GTK_SIGNAL_FUNC(selection_get), inst);
2920 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2921 GTK_SIGNAL_FUNC(selection_clear), inst);
2922 if (inst->cfg.scrollbar)
2923 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2924 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2925 gtk_timeout_add(20, timer_func, inst);
2926 gtk_widget_add_events(GTK_WIDGET(inst->area),
2927 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2928 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2929 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2931 gtk_widget_show(inst->window);
2933 set_window_background(inst);
2936 * Set up the Ctrl+rightclick context menu.
2939 GtkWidget *menuitem;
2941 extern const int use_event_log, new_session, saved_sessions;
2943 inst->menu = gtk_menu_new();
2945 #define MKMENUITEM(title, func) do { \
2946 menuitem = title ? gtk_menu_item_new_with_label(title) : \
2947 gtk_menu_item_new(); \
2948 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
2949 gtk_widget_show(menuitem); \
2951 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
2952 GTK_SIGNAL_FUNC(func), inst); \
2955 MKMENUITEM("New Session", new_session_menuitem);
2956 MKMENUITEM("Duplicate Session", dup_session_menuitem);
2957 if (saved_sessions) {
2958 struct sesslist sesslist;
2961 inst->sessionsmenu = gtk_menu_new();
2963 get_sesslist(&sesslist, TRUE);
2964 for (i = 1; i < sesslist.nsessions; i++) {
2965 menuitem = gtk_menu_item_new_with_label(sesslist.sessions[i]);
2966 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
2967 gtk_widget_show(menuitem);
2968 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2969 dupstr(sesslist.sessions[i]));
2970 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2971 GTK_SIGNAL_FUNC(saved_session_menuitem),
2973 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
2974 GTK_SIGNAL_FUNC(saved_session_freedata),
2977 get_sesslist(&sesslist, FALSE);
2979 MKMENUITEM("Saved Sessions", NULL);
2980 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
2981 inst->sessionsmenu);
2983 MKMENUITEM(NULL, NULL);
2984 MKMENUITEM("Change Settings", change_settings_menuitem);
2985 MKMENUITEM(NULL, NULL);
2987 MKMENUITEM("Event Log", event_log_menuitem);
2988 MKMENUITEM("Special Commands", NULL);
2989 inst->specialsmenu = gtk_menu_new();
2990 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
2991 inst->specialsitem1 = menuitem;
2992 MKMENUITEM(NULL, NULL);
2993 inst->specialsitem2 = menuitem;
2994 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
2995 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
2996 MKMENUITEM("Copy All", copy_all_menuitem);
2997 MKMENUITEM(NULL, NULL);
2998 s = dupcat("About ", appname, NULL);
2999 MKMENUITEM(s, about_menuitem);
3004 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3005 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3006 inst->blankcursor = make_mouse_ptr(inst, -1);
3007 make_mouse_ptr(inst, -2); /* clean up cursor font */
3008 inst->currcursor = inst->textcursor;
3009 show_mouseptr(inst, 1);
3011 inst->eventlogstuff = eventlogstuff_new();
3013 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3014 inst->logctx = log_init(inst, &inst->cfg);
3015 term_provide_logctx(inst->term, inst->logctx);
3019 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3021 inst->back = select_backend(&inst->cfg);
3023 char *realhost, *error;
3025 error = inst->back->init((void *)inst, &inst->backhandle,
3026 &inst->cfg, inst->cfg.host, inst->cfg.port,
3027 &realhost, inst->cfg.tcp_nodelay);
3030 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3031 inst->cfg.host, error);
3032 inst->exited = TRUE;
3033 fatal_message_box(inst->window, msg);
3038 if (inst->cfg.wintitle[0])
3039 set_title(inst, inst->cfg.wintitle);
3041 char *title = make_default_wintitle(realhost);
3042 set_title(inst, title);
3046 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3047 update_specials_menu(inst);
3049 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3052 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle, inst);
3053 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3055 /* now we're reday to deal with the child exit handler being
3057 block_signal(SIGCHLD, 0);
3060 * Block SIGPIPE: if we attempt Duplicate Session or similar
3061 * and it falls over in some way, we certainly don't want
3062 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3063 * this _after_ (at least pterm) forks off its child process,
3064 * since the child wants SIGPIPE handled in the usual way.
3066 block_signal(SIGPIPE, 1);
3068 inst->exited = FALSE;