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;
40 GtkWidget *window, *area, *sbar;
42 GtkAdjustment *sbar_adjust;
43 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2;
45 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
50 int xpos, ypos, gotpos, gravity;
51 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
52 GdkColor cols[NCOLOURS];
54 wchar_t *pastein_data;
57 char *pasteout_data, *pasteout_data_utf8;
58 int pasteout_data_len, pasteout_data_utf8_len;
59 int font_width, font_height;
63 guint term_paste_idle_id;
66 char wintitle[sizeof(((Config *)0)->wintitle)];
67 char icontitle[sizeof(((Config *)0)->wintitle)];
68 int master_fd, master_func_id;
75 struct unicode_data ucsdata;
82 struct gui_data *inst;
85 static int send_raw_mouse;
87 static char *app_name = "pterm";
89 char *x_get_default(const char *key)
91 return XGetDefault(GDK_DISPLAY(), app_name, key);
94 void connection_fatal(void *frontend, char *p, ...)
96 Terminal *term = (Terminal *)frontend;
97 struct gui_data *inst = (struct gui_data *)term->frontend;
102 msg = dupvprintf(p, ap);
105 fatal_message_box(inst->window, msg);
107 if (inst->cfg.close_on_exit == FORCE_ON)
112 * Default settings that are specific to pterm.
114 FontSpec platform_default_fontspec(const char *name)
117 if (!strcmp(name, "Font"))
118 strcpy(ret.name, "fixed");
124 Filename platform_default_filename(const char *name)
127 if (!strcmp(name, "LogFileName"))
128 strcpy(ret.path, "putty.log");
134 char *platform_default_s(const char *name)
139 int platform_default_i(const char *name, int def)
141 if (!strcmp(name, "CloseOnExit"))
142 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
146 void ldisc_update(void *frontend, int echo, int edit)
149 * This is a stub in pterm. If I ever produce a Unix
150 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
151 * then it will require some termios manoeuvring analogous to
152 * that in the Windows plink.c, but here it's meaningless.
156 int askappend(void *frontend, Filename filename)
159 * Logging in an xterm-alike is liable to be something you only
160 * do at serious diagnostic need. Hence, I'm going to take the
161 * easy option for now and assume we always want to overwrite
162 * log files. I can always make it properly configurable later.
167 void logevent(void *frontend, char *string)
169 Terminal *term = (Terminal *)frontend;
170 struct gui_data *inst = (struct gui_data *)term->frontend;
172 log_eventlog(inst->logctx, string);
174 logevent_dlg(inst->eventlogstuff, string);
177 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
179 struct gui_data *inst = (struct gui_data *)frontend;
182 return inst->font_height;
184 return inst->font_width;
188 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
189 * into a cooked one (SELECT, EXTEND, PASTE).
191 * In Unix, this is not configurable; the X button arrangement is
192 * rock-solid across all applications, everyone has a three-button
193 * mouse or a means of faking it, and there is no need to switch
194 * buttons around at all.
196 static Mouse_Button translate_button(Mouse_Button button)
198 /* struct gui_data *inst = (struct gui_data *)frontend; */
200 if (button == MBT_LEFT)
202 if (button == MBT_MIDDLE)
204 if (button == MBT_RIGHT)
206 return 0; /* shouldn't happen */
210 * Return the top-level GtkWindow associated with a particular
211 * front end instance.
213 void *get_window(void *frontend)
215 Terminal *term = (Terminal *)frontend;
216 struct gui_data *inst = (struct gui_data *)term->frontend;
221 * Minimise or restore the window in response to a server-side
224 void set_iconic(void *frontend, int iconic)
227 * GTK 1.2 doesn't know how to do this.
229 #if GTK_CHECK_VERSION(2,0,0)
230 struct gui_data *inst = (struct gui_data *)frontend;
232 gtk_window_iconify(GTK_WINDOW(inst->window));
234 gtk_window_deiconify(GTK_WINDOW(inst->window));
239 * Move the window in response to a server-side request.
241 void move_window(void *frontend, int x, int y)
243 struct gui_data *inst = (struct gui_data *)frontend;
245 * I assume that when the GTK version of this call is available
246 * we should use it. Not sure how it differs from the GDK one,
249 #if GTK_CHECK_VERSION(2,0,0)
250 gtk_window_move(GTK_WINDOW(inst->window), x, y);
252 gdk_window_move(inst->window->window, x, y);
257 * Move the window to the top or bottom of the z-order in response
258 * to a server-side request.
260 void set_zorder(void *frontend, int top)
262 struct gui_data *inst = (struct gui_data *)frontend;
264 gdk_window_raise(inst->window->window);
266 gdk_window_lower(inst->window->window);
270 * Refresh the window in response to a server-side request.
272 void refresh_window(void *frontend)
274 struct gui_data *inst = (struct gui_data *)frontend;
275 term_invalidate(inst->term);
279 * Maximise or restore the window in response to a server-side
282 void set_zoomed(void *frontend, int zoomed)
285 * GTK 1.2 doesn't know how to do this.
287 #if GTK_CHECK_VERSION(2,0,0)
288 struct gui_data *inst = (struct gui_data *)frontend;
290 gtk_window_maximize(GTK_WINDOW(inst->window));
292 gtk_window_unmaximize(GTK_WINDOW(inst->window));
297 * Report whether the window is iconic, for terminal reports.
299 int is_iconic(void *frontend)
301 struct gui_data *inst = (struct gui_data *)frontend;
302 return !gdk_window_is_viewable(inst->window->window);
306 * Report the window's position, for terminal reports.
308 void get_window_pos(void *frontend, int *x, int *y)
310 struct gui_data *inst = (struct gui_data *)frontend;
312 * I assume that when the GTK version of this call is available
313 * we should use it. Not sure how it differs from the GDK one,
316 #if GTK_CHECK_VERSION(2,0,0)
317 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
319 gdk_window_get_position(inst->window->window, x, y);
324 * Report the window's pixel size, for terminal reports.
326 void get_window_pixels(void *frontend, int *x, int *y)
328 struct gui_data *inst = (struct gui_data *)frontend;
330 * I assume that when the GTK version of this call is available
331 * we should use it. Not sure how it differs from the GDK one,
334 #if GTK_CHECK_VERSION(2,0,0)
335 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
337 gdk_window_get_size(inst->window->window, x, y);
342 * Return the window or icon title.
344 char *get_window_title(void *frontend, int icon)
346 struct gui_data *inst = (struct gui_data *)frontend;
347 return icon ? inst->wintitle : inst->icontitle;
350 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
352 struct gui_data *inst = (struct gui_data *)data;
353 if (inst->cfg.warn_on_close) {
354 if (!reallyclose(inst->term))
360 static void show_mouseptr(struct gui_data *inst, int show)
362 if (!inst->cfg.hide_mouseptr)
365 gdk_window_set_cursor(inst->area->window, inst->currcursor);
367 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
368 inst->mouseptr_visible = show;
371 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
373 struct gui_data *inst = (struct gui_data *)data;
374 int w, h, need_size = 0;
376 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
377 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
379 if (w != inst->width || h != inst->height) {
381 gdk_pixmap_unref(inst->pixmap);
384 inst->cfg.width = inst->width = w;
385 inst->cfg.height = inst->height = h;
391 inst->pixmap = gdk_pixmap_new(widget->window,
392 (inst->cfg.width * inst->font_width +
393 2*inst->cfg.window_border),
394 (inst->cfg.height * inst->font_height +
395 2*inst->cfg.window_border), -1);
397 gc = gdk_gc_new(inst->area->window);
398 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
399 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
400 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
401 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
406 term_size(inst->term, h, w, inst->cfg.savelines);
412 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
414 struct gui_data *inst = (struct gui_data *)data;
417 * Pass the exposed rectangle to terminal.c, which will call us
418 * back to do the actual painting.
421 gdk_draw_pixmap(widget->window,
422 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
424 event->area.x, event->area.y,
425 event->area.x, event->area.y,
426 event->area.width, event->area.height);
431 #define KEY_PRESSED(k) \
432 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
434 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
436 struct gui_data *inst = (struct gui_data *)data;
440 /* By default, nothing is generated. */
444 * If Alt is being released after typing an Alt+numberpad
445 * sequence, we should generate the code that was typed.
447 * Note that we only do this if more than one key was actually
448 * pressed - I don't think Alt+NumPad4 should be ^D or that
449 * Alt+NumPad3 should be ^C, for example. There's no serious
450 * inconvenience in having to type a zero before a single-digit
453 if (event->type == GDK_KEY_RELEASE &&
454 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
455 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
456 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
458 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
460 output[0] = inst->alt_keycode;
465 if (event->type == GDK_KEY_PRESS) {
469 printf("keypress: keyval = %04x, state = %08x; string =",
470 event->keyval, event->state);
471 for (i = 0; event->string[i]; i++)
472 printf(" %02x", (unsigned char) event->string[i]);
478 * NYI: Compose key (!!! requires Unicode faff before even trying)
482 * If Alt has just been pressed, we start potentially
483 * accumulating an Alt+numberpad code. We do this by
484 * setting alt_keycode to -1 (nothing yet but plausible).
486 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
487 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
488 inst->alt_keycode = -1;
489 inst->alt_digits = 0;
490 goto done; /* this generates nothing else */
494 * If we're seeing a numberpad key press with Mod1 down,
495 * consider adding it to alt_keycode if that's sensible.
496 * Anything _else_ with Mod1 down cancels any possibility
497 * of an ALT keycode: we set alt_keycode to -2.
499 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
501 switch (event->keyval) {
502 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
503 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
504 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
505 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
506 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
507 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
508 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
509 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
510 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
511 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
514 inst->alt_keycode = -2; /* it's invalid */
517 printf("Adding digit %d to keycode %d", digit,
520 if (inst->alt_keycode == -1)
521 inst->alt_keycode = digit; /* one-digit code */
523 inst->alt_keycode = inst->alt_keycode * 10 + digit;
526 printf(" gives new code %d\n", inst->alt_keycode);
528 /* Having used this digit, we now do nothing more with it. */
534 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
537 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
538 term_scroll(inst->term, 0, -inst->cfg.height/2);
541 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
542 term_scroll(inst->term, 0, -1);
545 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
546 term_scroll(inst->term, 0, +inst->cfg.height/2);
549 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
550 term_scroll(inst->term, 0, +1);
555 * Neither does Shift-Ins.
557 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
562 /* ALT+things gives leading Escape. */
564 strncpy(output+1, event->string, 31);
566 end = strlen(output);
567 if (event->state & GDK_MOD1_MASK) {
569 if (end == 1) end = 0;
573 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
574 if (!event->string[0] && event->keyval == '`' &&
575 (event->state & GDK_CONTROL_MASK)) {
580 /* Control-Break is the same as Control-C */
581 if (event->keyval == GDK_Break &&
582 (event->state & GDK_CONTROL_MASK)) {
587 /* Control-2, Control-Space and Control-@ are NUL */
588 if (!event->string[0] &&
589 (event->keyval == ' ' || event->keyval == '2' ||
590 event->keyval == '@') &&
591 (event->state & (GDK_SHIFT_MASK |
592 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
597 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
598 if (!event->string[0] && event->keyval == ' ' &&
599 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
600 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
605 /* We don't let GTK tell us what Backspace is! We know better. */
606 if (event->keyval == GDK_BackSpace &&
607 !(event->state & GDK_SHIFT_MASK)) {
608 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
611 /* For Shift Backspace, do opposite of what is configured. */
612 if (event->keyval == GDK_BackSpace &&
613 (event->state & GDK_SHIFT_MASK)) {
614 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
618 /* Shift-Tab is ESC [ Z */
619 if (event->keyval == GDK_ISO_Left_Tab ||
620 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
621 end = 1 + sprintf(output+1, "\033[Z");
625 * NetHack keypad mode.
627 if (inst->cfg.nethack_keypad) {
629 switch (event->keyval) {
630 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
631 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
632 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
633 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
634 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
635 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
636 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
637 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
638 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
642 if (event->state & GDK_SHIFT_MASK)
651 * Application keypad mode.
653 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
655 switch (event->keyval) {
656 case GDK_Num_Lock: xkey = 'P'; break;
657 case GDK_KP_Divide: xkey = 'Q'; break;
658 case GDK_KP_Multiply: xkey = 'R'; break;
659 case GDK_KP_Subtract: xkey = 'S'; break;
661 * Keypad + is tricky. It covers a space that would
662 * be taken up on the VT100 by _two_ keys; so we
663 * let Shift select between the two. Worse still,
664 * in xterm function key mode we change which two...
667 if (inst->cfg.funky_type == 2) {
668 if (event->state & GDK_SHIFT_MASK)
672 } else if (event->state & GDK_SHIFT_MASK)
677 case GDK_KP_Enter: xkey = 'M'; break;
678 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
679 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
680 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
681 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
682 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
683 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
684 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
685 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
686 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
687 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
688 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
691 if (inst->term->vt52_mode) {
692 if (xkey >= 'P' && xkey <= 'S')
693 end = 1 + sprintf(output+1, "\033%c", xkey);
695 end = 1 + sprintf(output+1, "\033?%c", xkey);
697 end = 1 + sprintf(output+1, "\033O%c", xkey);
703 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
704 * for integer decimal nn.)
706 * We also deal with the weird ones here. Linux VCs replace F1
707 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
708 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
713 switch (event->keyval) {
715 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
718 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
721 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
724 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
727 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
730 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
733 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
736 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
739 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
742 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
775 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
776 case GDK_Home: case GDK_KP_Home:
779 case GDK_Insert: case GDK_KP_Insert:
782 case GDK_Delete: case GDK_KP_Delete:
785 case GDK_End: case GDK_KP_End:
788 case GDK_Page_Up: case GDK_KP_Page_Up:
791 case GDK_Page_Down: case GDK_KP_Page_Down:
795 /* Reorder edit keys to physical order */
796 if (inst->cfg.funky_type == 3 && code <= 6)
797 code = "\0\2\1\4\5\3\6"[code];
799 if (inst->term->vt52_mode && code > 0 && code <= 6) {
800 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
804 if (inst->cfg.funky_type == 5 && /* SCO function keys */
805 code >= 11 && code <= 34) {
806 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
808 switch (event->keyval) {
809 case GDK_F1: index = 0; break;
810 case GDK_F2: index = 1; break;
811 case GDK_F3: index = 2; break;
812 case GDK_F4: index = 3; break;
813 case GDK_F5: index = 4; break;
814 case GDK_F6: index = 5; break;
815 case GDK_F7: index = 6; break;
816 case GDK_F8: index = 7; break;
817 case GDK_F9: index = 8; break;
818 case GDK_F10: index = 9; break;
819 case GDK_F11: index = 10; break;
820 case GDK_F12: index = 11; break;
822 if (event->state & GDK_SHIFT_MASK) index += 12;
823 if (event->state & GDK_CONTROL_MASK) index += 24;
824 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
827 if (inst->cfg.funky_type == 5 && /* SCO small keypad */
828 code >= 1 && code <= 6) {
829 char codes[] = "HL.FIG";
834 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
838 if ((inst->term->vt52_mode || inst->cfg.funky_type == 4) &&
839 code >= 11 && code <= 24) {
845 if (inst->term->vt52_mode)
846 end = 1 + sprintf(output+1,
847 "\x1B%c", code + 'P' - 11 - offt);
849 end = 1 + sprintf(output+1,
850 "\x1BO%c", code + 'P' - 11 - offt);
853 if (inst->cfg.funky_type == 1 && code >= 11 && code <= 15) {
854 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
857 if (inst->cfg.funky_type == 2 && code >= 11 && code <= 14) {
858 if (inst->term->vt52_mode)
859 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
861 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
864 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
865 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
869 end = 1 + sprintf(output+1, "\x1B[%d~", code);
875 * Cursor keys. (This includes the numberpad cursor keys,
876 * if we haven't already done them due to app keypad mode.)
878 * Here we also process un-numlocked un-appkeypadded KP5,
879 * which sends ESC [ G.
883 switch (event->keyval) {
884 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
885 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
886 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
887 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
888 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
892 * The arrow keys normally do ESC [ A and so on. In
893 * app cursor keys mode they do ESC O A instead.
894 * Ctrl toggles the two modes.
896 if (inst->term->vt52_mode) {
897 end = 1 + sprintf(output+1, "\033%c", xkey);
898 } else if (!inst->term->app_cursor_keys ^
899 !(event->state & GDK_CONTROL_MASK)) {
900 end = 1 + sprintf(output+1, "\033O%c", xkey);
902 end = 1 + sprintf(output+1, "\033[%c", xkey);
915 printf("generating sequence:");
916 for (i = start; i < end; i++)
917 printf(" %02x", (unsigned char) output[i]);
921 if (!inst->direct_to_font) {
923 * The stuff we've just generated is assumed to be
924 * ISO-8859-1! This sounds insane, but `man
925 * XLookupString' agrees: strings of this type returned
926 * from the X server are hardcoded to 8859-1. Strictly
927 * speaking we should be doing this using some sort of
928 * GtkIMContext, which (if we're lucky) would give us
929 * our data directly in Unicode; but that's not
930 * supported in GTK 1.2 as far as I can tell, and it's
931 * poorly documented even in 2.0, so it'll have to
934 lpage_send(inst->ldisc, CS_ISO8859_1, output+start, end-start, 1);
937 * In direct-to-font mode, we just send the string
938 * exactly as we received it.
940 ldisc_send(inst->ldisc, output+start, end-start, 1);
943 show_mouseptr(inst, 0);
944 term_seen_key_event(inst->term);
945 term_out(inst->term);
951 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
953 struct gui_data *inst = (struct gui_data *)data;
954 int shift, ctrl, alt, x, y, button, act;
956 show_mouseptr(inst, 1);
958 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
959 term_scroll(inst->term, 0, -5);
962 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
963 term_scroll(inst->term, 0, +5);
967 shift = event->state & GDK_SHIFT_MASK;
968 ctrl = event->state & GDK_CONTROL_MASK;
969 alt = event->state & GDK_MOD1_MASK;
971 if (event->button == 3 && ctrl) {
972 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
973 event->button, event->time);
977 if (event->button == 1)
979 else if (event->button == 2)
981 else if (event->button == 3)
984 return FALSE; /* don't even know what button! */
986 switch (event->type) {
987 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
988 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
989 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
990 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
991 default: return FALSE; /* don't know this event type */
994 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
995 act != MA_CLICK && act != MA_RELEASE)
996 return TRUE; /* we ignore these in raw mouse mode */
998 x = (event->x - inst->cfg.window_border) / inst->font_width;
999 y = (event->y - inst->cfg.window_border) / inst->font_height;
1001 term_mouse(inst->term, button, translate_button(button), act,
1002 x, y, shift, ctrl, alt);
1007 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1009 struct gui_data *inst = (struct gui_data *)data;
1010 int shift, ctrl, alt, x, y, button;
1012 show_mouseptr(inst, 1);
1014 shift = event->state & GDK_SHIFT_MASK;
1015 ctrl = event->state & GDK_CONTROL_MASK;
1016 alt = event->state & GDK_MOD1_MASK;
1017 if (event->state & GDK_BUTTON1_MASK)
1019 else if (event->state & GDK_BUTTON2_MASK)
1020 button = MBT_MIDDLE;
1021 else if (event->state & GDK_BUTTON3_MASK)
1024 return FALSE; /* don't even know what button! */
1026 x = (event->x - inst->cfg.window_border) / inst->font_width;
1027 y = (event->y - inst->cfg.window_border) / inst->font_height;
1029 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1030 x, y, shift, ctrl, alt);
1035 void frontend_keypress(void *handle)
1037 struct gui_data *inst = (struct gui_data *)handle;
1040 * If our child process has exited but not closed, terminate on
1047 gint timer_func(gpointer data)
1049 struct gui_data *inst = (struct gui_data *)data;
1052 if (!inst->exited &&
1053 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1054 inst->exited = TRUE;
1055 if (inst->cfg.close_on_exit == FORCE_ON ||
1056 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1057 exit(0); /* just go. */
1060 term_update(inst->term);
1061 term_blink(inst->term, 0);
1065 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1068 * We must process exceptional notifications before ordinary
1069 * readability ones, or we may go straight past the urgent
1072 if (condition & GDK_INPUT_EXCEPTION)
1073 select_result(sourcefd, 4);
1074 if (condition & GDK_INPUT_READ)
1075 select_result(sourcefd, 1);
1076 if (condition & GDK_INPUT_WRITE)
1077 select_result(sourcefd, 2);
1080 void destroy(GtkWidget *widget, gpointer data)
1085 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1087 struct gui_data *inst = (struct gui_data *)data;
1088 inst->term->has_focus = event->in;
1089 term_out(inst->term);
1090 term_update(inst->term);
1091 show_mouseptr(inst, 1);
1096 * set or clear the "raw mouse message" mode
1098 void set_raw_mouse_mode(void *frontend, int activate)
1100 struct gui_data *inst = (struct gui_data *)frontend;
1101 activate = activate && !inst->cfg.no_mouse_rep;
1102 send_raw_mouse = activate;
1104 inst->currcursor = inst->rawcursor;
1106 inst->currcursor = inst->textcursor;
1107 show_mouseptr(inst, inst->mouseptr_visible);
1110 void request_resize(void *frontend, int w, int h)
1112 struct gui_data *inst = (struct gui_data *)frontend;
1113 int large_x, large_y;
1114 int offset_x, offset_y;
1116 GtkRequisition inner, outer;
1119 * This is a heinous hack dreamed up by the gnome-terminal
1120 * people to get around a limitation in gtk. The problem is
1121 * that in order to set the size correctly we really need to be
1122 * calling gtk_window_resize - but that needs to know the size
1123 * of the _whole window_, not the drawing area. So what we do
1124 * is to set an artificially huge size request on the drawing
1125 * area, recompute the resulting size request on the window,
1126 * and look at the difference between the two. That gives us
1127 * the x and y offsets we need to translate drawing area size
1128 * into window size for real, and then we call
1129 * gtk_window_resize.
1133 * We start by retrieving the current size of the whole window.
1134 * Adding a bit to _that_ will give us a value we can use as a
1135 * bogus size request which guarantees to be bigger than the
1136 * current size of the drawing area.
1138 get_window_pixels(inst, &large_x, &large_y);
1142 #if GTK_CHECK_VERSION(2,0,0)
1143 gtk_widget_set_size_request(inst->area, large_x, large_y);
1145 gtk_widget_set_usize(inst->area, large_x, large_y);
1147 gtk_widget_size_request(inst->area, &inner);
1148 gtk_widget_size_request(inst->window, &outer);
1150 offset_x = outer.width - inner.width;
1151 offset_y = outer.height - inner.height;
1153 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1154 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1157 * Now we must set the size request on the drawing area back to
1158 * something sensible before we commit the real resize. Best
1159 * way to do this, I think, is to set it to what the size is
1160 * really going to end up being.
1162 #if GTK_CHECK_VERSION(2,0,0)
1163 gtk_widget_set_size_request(inst->area, area_x, area_y);
1165 gtk_widget_set_usize(inst->area, area_x, area_y);
1166 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1169 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1171 #if GTK_CHECK_VERSION(2,0,0)
1172 gtk_window_resize(GTK_WINDOW(inst->window),
1173 area_x + offset_x, area_y + offset_y);
1175 gdk_window_resize(inst->window->window,
1176 area_x + offset_x, area_y + offset_y);
1180 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1182 gboolean success[1];
1184 inst->cols[n].red = r * 0x0101;
1185 inst->cols[n].green = g * 0x0101;
1186 inst->cols[n].blue = b * 0x0101;
1188 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1189 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1190 FALSE, FALSE, success);
1192 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1196 void set_window_background(struct gui_data *inst)
1198 if (inst->area && inst->area->window)
1199 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1200 if (inst->window && inst->window->window)
1201 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1204 void palette_set(void *frontend, int n, int r, int g, int b)
1206 struct gui_data *inst = (struct gui_data *)frontend;
1207 static const int first[21] = {
1208 0, 2, 4, 6, 8, 10, 12, 14,
1209 1, 3, 5, 7, 9, 11, 13, 15,
1212 real_palette_set(inst, first[n], r, g, b);
1214 real_palette_set(inst, first[n] + 1, r, g, b);
1216 set_window_background(inst);
1219 void palette_reset(void *frontend)
1221 struct gui_data *inst = (struct gui_data *)frontend;
1222 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1223 static const int ww[] = {
1224 6, 7, 8, 9, 10, 11, 12, 13,
1225 14, 15, 16, 17, 18, 19, 20, 21,
1228 gboolean success[NCOLOURS];
1231 assert(lenof(ww) == NCOLOURS);
1233 if (!inst->colmap) {
1234 inst->colmap = gdk_colormap_get_system();
1236 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1239 for (i = 0; i < NCOLOURS; i++) {
1240 inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
1241 inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
1242 inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
1245 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1246 FALSE, FALSE, success);
1247 for (i = 0; i < NCOLOURS; i++) {
1249 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1250 appname, i, inst->cfg.colours[i][0],
1251 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1254 set_window_background(inst);
1257 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1259 struct gui_data *inst = (struct gui_data *)frontend;
1260 if (inst->pasteout_data)
1261 sfree(inst->pasteout_data);
1262 if (inst->pasteout_data_utf8)
1263 sfree(inst->pasteout_data_utf8);
1266 * Set up UTF-8 paste data. This only happens if we aren't in
1267 * direct-to-font mode using the D800 hack.
1269 if (!inst->direct_to_font) {
1270 wchar_t *tmp = data;
1273 inst->pasteout_data_utf8 = snewn(len*6, char);
1274 inst->pasteout_data_utf8_len = len*6;
1275 inst->pasteout_data_utf8_len =
1276 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1277 inst->pasteout_data_utf8_len,
1278 CS_UTF8, NULL, NULL, 0);
1279 if (inst->pasteout_data_utf8_len == 0) {
1280 sfree(inst->pasteout_data_utf8);
1281 inst->pasteout_data_utf8 = NULL;
1283 inst->pasteout_data_utf8 =
1284 sresize(inst->pasteout_data_utf8,
1285 inst->pasteout_data_utf8_len, char);
1288 inst->pasteout_data_utf8 = NULL;
1289 inst->pasteout_data_utf8_len = 0;
1292 inst->pasteout_data = snewn(len*6, char);
1293 inst->pasteout_data_len = len*6;
1294 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1295 data, len, inst->pasteout_data,
1296 inst->pasteout_data_len,
1298 if (inst->pasteout_data_len == 0) {
1299 sfree(inst->pasteout_data);
1300 inst->pasteout_data = NULL;
1302 inst->pasteout_data =
1303 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1306 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1307 GDK_CURRENT_TIME)) {
1308 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1309 GDK_SELECTION_TYPE_STRING, 1);
1310 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1311 compound_text_atom, 1);
1312 if (inst->pasteout_data_utf8)
1313 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1314 utf8_string_atom, 1);
1318 term_deselect(inst->term);
1321 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1322 guint info, guint time_stamp, gpointer data)
1324 struct gui_data *inst = (struct gui_data *)data;
1325 if (seldata->target == utf8_string_atom)
1326 gtk_selection_data_set(seldata, seldata->target, 8,
1327 inst->pasteout_data_utf8,
1328 inst->pasteout_data_utf8_len);
1330 gtk_selection_data_set(seldata, seldata->target, 8,
1331 inst->pasteout_data, inst->pasteout_data_len);
1334 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1337 struct gui_data *inst = (struct gui_data *)data;
1338 term_deselect(inst->term);
1339 if (inst->pasteout_data)
1340 sfree(inst->pasteout_data);
1341 if (inst->pasteout_data_utf8)
1342 sfree(inst->pasteout_data_utf8);
1343 inst->pasteout_data = NULL;
1344 inst->pasteout_data_len = 0;
1345 inst->pasteout_data_utf8 = NULL;
1346 inst->pasteout_data_utf8_len = 0;
1350 void request_paste(void *frontend)
1352 struct gui_data *inst = (struct gui_data *)frontend;
1354 * In Unix, pasting is asynchronous: all we can do at the
1355 * moment is to call gtk_selection_convert(), and when the data
1356 * comes back _then_ we can call term_do_paste().
1359 if (!inst->direct_to_font) {
1361 * First we attempt to retrieve the selection as a UTF-8
1362 * string (which we will convert to the correct code page
1363 * before sending to the session, of course). If that
1364 * fails, selection_received() will be informed and will
1365 * fall back to an ordinary string.
1367 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1368 utf8_string_atom, GDK_CURRENT_TIME);
1371 * If we're in direct-to-font mode, we disable UTF-8
1372 * pasting, and go straight to ordinary string data.
1374 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1375 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1379 gint idle_paste_func(gpointer data); /* forward ref */
1381 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1382 guint time, gpointer data)
1384 struct gui_data *inst = (struct gui_data *)data;
1386 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1388 * Failed to get a UTF-8 selection string. Try an ordinary
1391 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1392 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1397 * Any other failure should just go foom.
1399 if (seldata->length <= 0 ||
1400 (seldata->type != GDK_SELECTION_TYPE_STRING &&
1401 seldata->type != utf8_string_atom))
1402 return; /* Nothing happens. */
1404 if (inst->pastein_data)
1405 sfree(inst->pastein_data);
1407 inst->pastein_data = snewn(seldata->length, wchar_t);
1408 inst->pastein_data_len = seldata->length;
1409 inst->pastein_data_len =
1410 mb_to_wc((seldata->type == utf8_string_atom ?
1411 CS_UTF8 : inst->ucsdata.line_codepage),
1412 0, seldata->data, seldata->length,
1413 inst->pastein_data, inst->pastein_data_len);
1415 term_do_paste(inst->term);
1417 if (term_paste_pending(inst->term))
1418 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1421 gint idle_paste_func(gpointer data)
1423 struct gui_data *inst = (struct gui_data *)data;
1425 if (term_paste_pending(inst->term))
1426 term_paste(inst->term);
1428 gtk_idle_remove(inst->term_paste_idle_id);
1434 void get_clip(void *frontend, wchar_t ** p, int *len)
1436 struct gui_data *inst = (struct gui_data *)frontend;
1439 *p = inst->pastein_data;
1440 *len = inst->pastein_data_len;
1444 void set_title(void *frontend, char *title)
1446 struct gui_data *inst = (struct gui_data *)frontend;
1447 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1448 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1449 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1452 void set_icon(void *frontend, char *title)
1454 struct gui_data *inst = (struct gui_data *)frontend;
1455 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1456 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1457 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1460 void set_sbar(void *frontend, int total, int start, int page)
1462 struct gui_data *inst = (struct gui_data *)frontend;
1463 if (!inst->cfg.scrollbar)
1465 inst->sbar_adjust->lower = 0;
1466 inst->sbar_adjust->upper = total;
1467 inst->sbar_adjust->value = start;
1468 inst->sbar_adjust->page_size = page;
1469 inst->sbar_adjust->step_increment = 1;
1470 inst->sbar_adjust->page_increment = page/2;
1471 inst->ignore_sbar = TRUE;
1472 gtk_adjustment_changed(inst->sbar_adjust);
1473 inst->ignore_sbar = FALSE;
1476 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1478 struct gui_data *inst = (struct gui_data *)data;
1480 if (!inst->cfg.scrollbar)
1482 if (!inst->ignore_sbar)
1483 term_scroll(inst->term, 1, (int)adj->value);
1486 void sys_cursor(void *frontend, int x, int y)
1489 * This is meaningless under X.
1494 * This is still called when mode==BELL_VISUAL, even though the
1495 * visual bell is handled entirely within terminal.c, because we
1496 * may want to perform additional actions on any kind of bell (for
1497 * example, taskbar flashing in Windows).
1499 void beep(void *frontend, int mode)
1501 if (mode != BELL_VISUAL)
1505 int char_width(Context ctx, int uc)
1508 * Under X, any fixed-width font really _is_ fixed-width.
1509 * Double-width characters will be dealt with using a separate
1510 * font. For the moment we can simply return 1.
1515 Context get_ctx(void *frontend)
1517 struct gui_data *inst = (struct gui_data *)frontend;
1518 struct draw_ctx *dctx;
1520 if (!inst->area->window)
1523 dctx = snew(struct draw_ctx);
1525 dctx->gc = gdk_gc_new(inst->area->window);
1529 void free_ctx(Context ctx)
1531 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1532 /* struct gui_data *inst = dctx->inst; */
1533 GdkGC *gc = dctx->gc;
1539 * Draw a line of text in the window, at given character
1540 * coordinates, in given attributes.
1542 * We are allowed to fiddle with the contents of `text'.
1544 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1545 unsigned long attr, int lattr)
1547 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1548 struct gui_data *inst = dctx->inst;
1549 GdkGC *gc = dctx->gc;
1551 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1553 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1554 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
1555 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1556 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
1557 if (attr & ATTR_REVERSE) {
1562 if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
1564 if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
1566 if (attr & TATTR_ACTCURS) {
1571 fontid = shadow = 0;
1573 if (attr & ATTR_WIDE) {
1580 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1581 if (inst->fonts[fontid | 1])
1587 if (lattr != LATTR_NORM) {
1589 if (x >= inst->term->cols)
1591 if (x + len*2*widefactor > inst->term->cols)
1592 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1600 r.x = x*inst->font_width+inst->cfg.window_border;
1601 r.y = y*inst->font_height+inst->cfg.window_border;
1602 r.width = rlen*widefactor*inst->font_width;
1603 r.height = inst->font_height;
1604 gdk_gc_set_clip_rectangle(gc, &r);
1607 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1608 gdk_draw_rectangle(inst->pixmap, gc, 1,
1609 x*inst->font_width+inst->cfg.window_border,
1610 y*inst->font_height+inst->cfg.window_border,
1611 rlen*widefactor*inst->font_width, inst->font_height);
1613 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1620 wcs = snewn(len+1, wchar_t);
1621 for (i = 0; i < len; i++) {
1622 wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
1625 if (inst->fonts[fontid] == NULL) {
1627 * The font for this contingency does not exist.
1628 * Typically this means we've been given ATTR_WIDE
1629 * character and have no wide font. So we display
1630 * nothing at all; such is life.
1632 } else if (inst->fontinfo[fontid].is_wide) {
1634 * At least one version of gdk_draw_text_wc() has a
1635 * weird bug whereby it reads `len' elements of the
1636 * input string, but only draws `len/2'. Hence I'm
1637 * going to make its input array twice as long as it
1638 * theoretically needs to be, and pass in twice the
1639 * actual number of characters. If a fixed gdk actually
1640 * takes the doubled length seriously, then (a) the
1641 * array will stand scrutiny up to the full length, (b)
1642 * the spare elements of the array are full of zeroes
1643 * which will probably be an empty glyph in the font,
1644 * and (c) the clip rectangle should prevent it causing
1647 gwcs = snewn(len*2+1, GdkWChar);
1648 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
1650 * FIXME: when we have a wide-char equivalent of
1651 * from_unicode, use it instead of this.
1653 for (i = 0; i <= len; i++)
1655 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
1656 x*inst->font_width+inst->cfg.window_border,
1657 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1661 gcs = snewn(len+1, gchar);
1662 wc_to_mb(inst->fontinfo[fontid].charset, 0,
1663 wcs, len, gcs, len, ".", NULL, NULL);
1664 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1665 x*inst->font_width+inst->cfg.window_border,
1666 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1674 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1675 x*inst->font_width+inst->cfg.window_border + inst->cfg.shadowboldoffset,
1676 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1680 if (attr & ATTR_UNDER) {
1681 int uheight = inst->fonts[0]->ascent + 1;
1682 if (uheight >= inst->font_height)
1683 uheight = inst->font_height - 1;
1684 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
1685 y*inst->font_height + uheight + inst->cfg.window_border,
1686 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
1687 y*inst->font_height + uheight + inst->cfg.window_border);
1690 if (lattr != LATTR_NORM) {
1692 * I can't find any plausible StretchBlt equivalent in the
1693 * X server, so I'm going to do this the slow and painful
1694 * way. This will involve repeated calls to
1695 * gdk_draw_pixmap() to stretch the text horizontally. It's
1696 * O(N^2) in time and O(N) in network bandwidth, but you
1697 * try thinking of a better way. :-(
1700 for (i = 0; i < len * widefactor * inst->font_width; i++) {
1701 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1702 x*inst->font_width+inst->cfg.window_border + 2*i,
1703 y*inst->font_height+inst->cfg.window_border,
1704 x*inst->font_width+inst->cfg.window_border + 2*i+1,
1705 y*inst->font_height+inst->cfg.window_border,
1706 len * inst->font_width - i, inst->font_height);
1709 if (lattr != LATTR_WIDE) {
1711 /* Now stretch vertically, in the same way. */
1712 if (lattr == LATTR_BOT)
1716 for (i = 0; i < inst->font_height; i+=2) {
1717 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1718 x*inst->font_width+inst->cfg.window_border,
1719 y*inst->font_height+inst->cfg.window_border+dt*i+db,
1720 x*widefactor*inst->font_width+inst->cfg.window_border,
1721 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
1722 len * inst->font_width, inst->font_height-i-1);
1728 void do_text(Context ctx, int x, int y, char *text, int len,
1729 unsigned long attr, int lattr)
1731 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1732 struct gui_data *inst = dctx->inst;
1733 GdkGC *gc = dctx->gc;
1736 do_text_internal(ctx, x, y, text, len, attr, lattr);
1738 if (attr & ATTR_WIDE) {
1744 if (lattr != LATTR_NORM) {
1746 if (x >= inst->term->cols)
1748 if (x + len*2*widefactor > inst->term->cols)
1749 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1753 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1754 x*inst->font_width+inst->cfg.window_border,
1755 y*inst->font_height+inst->cfg.window_border,
1756 x*inst->font_width+inst->cfg.window_border,
1757 y*inst->font_height+inst->cfg.window_border,
1758 len*widefactor*inst->font_width, inst->font_height);
1761 void do_cursor(Context ctx, int x, int y, char *text, int len,
1762 unsigned long attr, int lattr)
1764 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1765 struct gui_data *inst = dctx->inst;
1766 GdkGC *gc = dctx->gc;
1768 int passive, widefactor;
1770 if (attr & TATTR_PASCURS) {
1771 attr &= ~TATTR_PASCURS;
1775 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
1776 attr &= ~TATTR_ACTCURS;
1778 do_text_internal(ctx, x, y, text, len, attr, lattr);
1780 if (attr & ATTR_WIDE) {
1786 if (lattr != LATTR_NORM) {
1788 if (x >= inst->term->cols)
1790 if (x + len*2*widefactor > inst->term->cols)
1791 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1795 if (inst->cfg.cursor_type == 0) {
1797 * An active block cursor will already have been done by
1798 * the above do_text call, so we only need to do anything
1802 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1803 gdk_draw_rectangle(inst->pixmap, gc, 0,
1804 x*inst->font_width+inst->cfg.window_border,
1805 y*inst->font_height+inst->cfg.window_border,
1806 len*inst->font_width-1, inst->font_height-1);
1810 int startx, starty, dx, dy, length, i;
1814 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1815 char_width = 2*inst->font_width;
1817 char_width = inst->font_width;
1819 if (inst->cfg.cursor_type == 1) {
1820 uheight = inst->fonts[0]->ascent + 1;
1821 if (uheight >= inst->font_height)
1822 uheight = inst->font_height - 1;
1824 startx = x * inst->font_width + inst->cfg.window_border;
1825 starty = y * inst->font_height + inst->cfg.window_border + uheight;
1828 length = len * char_width;
1831 if (attr & TATTR_RIGHTCURS)
1832 xadjust = char_width - 1;
1833 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
1834 starty = y * inst->font_height + inst->cfg.window_border;
1837 length = inst->font_height;
1840 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1842 for (i = 0; i < length; i++) {
1844 gdk_draw_point(inst->pixmap, gc, startx, starty);
1850 gdk_draw_line(inst->pixmap, gc, startx, starty,
1851 startx + (length-1) * dx, starty + (length-1) * dy);
1855 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1856 x*inst->font_width+inst->cfg.window_border,
1857 y*inst->font_height+inst->cfg.window_border,
1858 x*inst->font_width+inst->cfg.window_border,
1859 y*inst->font_height+inst->cfg.window_border,
1860 len*widefactor*inst->font_width, inst->font_height);
1863 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1866 * Truly hideous hack: GTK doesn't allow us to set the mouse
1867 * cursor foreground and background colours unless we've _also_
1868 * created our own cursor from bitmaps. Therefore, I need to
1869 * load the `cursor' font and draw glyphs from it on to
1870 * pixmaps, in order to construct my cursors with the fg and bg
1871 * I want. This is a gross hack, but it's more self-contained
1872 * than linking in Xlib to find the X window handle to
1873 * inst->area and calling XRecolorCursor, and it's more
1874 * futureproof than hard-coding the shapes as bitmap arrays.
1876 static GdkFont *cursor_font = NULL;
1877 GdkPixmap *source, *mask;
1879 GdkColor cfg = { 0, 65535, 65535, 65535 };
1880 GdkColor cbg = { 0, 0, 0, 0 };
1881 GdkColor dfg = { 1, 65535, 65535, 65535 };
1882 GdkColor dbg = { 0, 0, 0, 0 };
1885 gint lb, rb, wid, asc, desc, w, h, x, y;
1887 if (cursor_val == -2) {
1888 gdk_font_unref(cursor_font);
1892 if (cursor_val >= 0 && !cursor_font)
1893 cursor_font = gdk_font_load("cursor");
1896 * Get the text extent of the cursor in question. We use the
1897 * mask character for this, because it's typically slightly
1898 * bigger than the main character.
1900 if (cursor_val >= 0) {
1902 text[0] = (char)cursor_val + 1;
1903 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1904 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1910 source = gdk_pixmap_new(NULL, w, h, 1);
1911 mask = gdk_pixmap_new(NULL, w, h, 1);
1914 * Draw the mask character on the mask pixmap.
1916 gc = gdk_gc_new(mask);
1917 gdk_gc_set_foreground(gc, &dbg);
1918 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1919 if (cursor_val >= 0) {
1921 text[0] = (char)cursor_val + 1;
1922 gdk_gc_set_foreground(gc, &dfg);
1923 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1928 * Draw the main character on the source pixmap.
1930 gc = gdk_gc_new(source);
1931 gdk_gc_set_foreground(gc, &dbg);
1932 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1933 if (cursor_val >= 0) {
1935 text[0] = (char)cursor_val;
1936 gdk_gc_set_foreground(gc, &dfg);
1937 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1942 * Create the cursor.
1944 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1949 gdk_pixmap_unref(source);
1950 gdk_pixmap_unref(mask);
1955 void modalfatalbox(char *p, ...)
1958 fprintf(stderr, "FATAL ERROR: ");
1960 vfprintf(stderr, p, ap);
1962 fputc('\n', stderr);
1966 void cmdline_error(char *p, ...)
1969 fprintf(stderr, "%s: ", appname);
1971 vfprintf(stderr, p, ap);
1973 fputc('\n', stderr);
1977 char *get_x_display(void *frontend)
1979 return gdk_get_display();
1982 long get_windowid(void *frontend)
1984 Terminal *term = (Terminal *)frontend;
1985 struct gui_data *inst = (struct gui_data *)(term->frontend);
1986 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
1989 static void help(FILE *fp) {
1991 "pterm option summary:\n"
1993 " --display DISPLAY Specify X display to use (note '--')\n"
1994 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1995 " -fn FONT Normal text font\n"
1996 " -fb FONT Bold text font\n"
1997 " -geometry GEOMETRY Position and size of window (size in characters)\n"
1998 " -sl LINES Number of lines of scrollback\n"
1999 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2000 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2001 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2002 " -T TITLE Window title\n"
2003 " -ut, +ut Do(default) or do not update utmp\n"
2004 " -ls, +ls Do(default) or do not make shell a login shell\n"
2005 " -sb, +sb Do(default) or do not display a scrollbar\n"
2006 " -log PATH Log all output to a file\n"
2007 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2008 " -xrm RESOURCE-STRING Set an X resource\n"
2009 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2010 ) < 0 || fflush(fp) < 0) {
2011 perror("output error");
2016 int do_cmdline(int argc, char **argv, int do_everything,
2017 struct gui_data *inst, Config *cfg)
2020 extern char **pty_argv; /* declared in pty.c */
2021 extern int use_pty_argv;
2024 * Macros to make argument handling easier. Note that because
2025 * they need to call `continue', they cannot be contained in
2026 * the usual do {...} while (0) wrapper to make them
2027 * syntactically single statements; hence it is not legal to
2028 * use one of these macros as an unbraced statement between
2031 #define EXPECTS_ARG { \
2032 if (--argc <= 0) { \
2034 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2039 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2042 while (--argc > 0) {
2046 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2047 do_everything ? 1 : -1, cfg);
2050 cmdline_error("option \"%s\" requires an argument", p);
2051 } else if (ret == 2) {
2052 --argc, ++argv; /* skip next argument */
2054 } else if (ret == 1) {
2058 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2061 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2062 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2064 } else if (!strcmp(p, "-fb")) {
2067 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2068 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2070 } else if (!strcmp(p, "-fw")) {
2073 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2074 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2076 } else if (!strcmp(p, "-fwb")) {
2079 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2080 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2082 } else if (!strcmp(p, "-cs")) {
2085 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2086 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2088 } else if (!strcmp(p, "-geometry")) {
2089 int flags, x, y, w, h;
2093 flags = XParseGeometry(val, &x, &y, &w, &h);
2094 if (flags & WidthValue)
2096 if (flags & HeightValue)
2099 if (flags & (XValue | YValue)) {
2102 inst->gotpos = TRUE;
2103 inst->gravity = ((flags & XNegative ? 1 : 0) |
2104 (flags & YNegative ? 2 : 0));
2107 } else if (!strcmp(p, "-sl")) {
2110 cfg->savelines = atoi(val);
2112 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2113 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2114 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2119 if (!gdk_color_parse(val, &col)) {
2121 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2125 index = (!strcmp(p, "-fg") ? 0 :
2126 !strcmp(p, "-bg") ? 2 :
2127 !strcmp(p, "-bfg") ? 1 :
2128 !strcmp(p, "-bbg") ? 3 :
2129 !strcmp(p, "-cfg") ? 4 :
2130 !strcmp(p, "-cbg") ? 5 : -1);
2131 assert(index != -1);
2132 cfg->colours[index][0] = col.red / 256;
2133 cfg->colours[index][1] = col.green / 256;
2134 cfg->colours[index][2] = col.blue / 256;
2137 } else if (use_pty_argv && !strcmp(p, "-e")) {
2138 /* This option swallows all further arguments. */
2144 pty_argv = snewn(argc+1, char *);
2146 for (i = 0; i < argc; i++)
2147 pty_argv[i] = argv[i];
2148 pty_argv[argc] = NULL;
2149 break; /* finished command-line processing */
2151 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2154 } else if (!strcmp(p, "-title")) {
2157 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2158 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2160 } else if (!strcmp(p, "-log")) {
2163 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2164 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2165 cfg->logtype = LGTYP_DEBUG;
2167 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2169 cfg->stamp_utmp = 0;
2171 } else if (!strcmp(p, "-ut")) {
2173 cfg->stamp_utmp = 1;
2175 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2177 cfg->login_shell = 0;
2179 } else if (!strcmp(p, "-ls")) {
2181 cfg->login_shell = 1;
2183 } else if (!strcmp(p, "-nethack")) {
2185 cfg->nethack_keypad = 1;
2187 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2191 } else if (!strcmp(p, "-sb")) {
2195 } else if (!strcmp(p, "-name")) {
2199 } else if (!strcmp(p, "-xrm")) {
2201 provide_xrm_string(val);
2203 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2207 } else if(p[0] != '-' && (!do_everything ||
2208 process_nonoption_arg(p, cfg))) {
2213 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2220 static void block_signal(int sig, int block_it) {
2224 sigaddset(&ss, sig);
2225 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2226 perror("sigprocmask");
2232 * This function retrieves the character set encoding of a font. It
2233 * returns the character set without the X11 hack (in case the user
2234 * asks to use the font's own encoding).
2236 static int set_font_info(struct gui_data *inst, int fontid)
2238 GdkFont *font = inst->fonts[fontid];
2239 XFontStruct *xfs = GDK_FONT_XFONT(font);
2240 Display *disp = GDK_FONT_XDISPLAY(font);
2241 Atom charset_registry, charset_encoding;
2242 unsigned long registry_ret, encoding_ret;
2243 int retval = CS_NONE;
2245 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2246 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2247 inst->fontinfo[fontid].charset = CS_NONE;
2248 inst->fontinfo[fontid].is_wide = 0;
2249 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2250 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2252 reg = XGetAtomName(disp, (Atom)registry_ret);
2253 enc = XGetAtomName(disp, (Atom)encoding_ret);
2255 char *encoding = dupcat(reg, "-", enc, NULL);
2256 retval = inst->fontinfo[fontid].charset =
2257 charset_from_xenc(encoding);
2258 /* FIXME: when libcharset supports wide encodings fix this. */
2259 if (!strcasecmp(encoding, "iso10646-1")) {
2260 inst->fontinfo[fontid].is_wide = 1;
2265 * Hack for X line-drawing characters: if the primary
2266 * font is encoded as ISO-8859-anything, and has valid
2267 * glyphs in the first 32 char positions, it is assumed
2268 * that those glyphs are the VT100 line-drawing
2271 * Actually, we'll hack even harder by only checking
2272 * position 0x19 (vertical line, VT100 linedrawing
2273 * `x'). Then we can check it easily by seeing if the
2274 * ascent and descent differ.
2276 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2277 int lb, rb, wid, asc, desc;
2282 gdk_string_extents(inst->fonts[fontid], text,
2283 &lb, &rb, &wid, &asc, &desc);
2285 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2295 int uxsel_input_add(int fd, int rwx) {
2297 if (rwx & 1) flags |= GDK_INPUT_READ;
2298 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2299 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2300 return gdk_input_add(fd, flags, fd_input_func, NULL);
2303 void uxsel_input_remove(int id) {
2304 gdk_input_remove(id);
2307 void setup_fonts_ucs(struct gui_data *inst)
2312 gdk_font_unref(inst->fonts[0]);
2314 gdk_font_unref(inst->fonts[1]);
2316 gdk_font_unref(inst->fonts[2]);
2318 gdk_font_unref(inst->fonts[3]);
2320 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2321 if (!inst->fonts[0]) {
2322 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2323 inst->cfg.font.name);
2326 font_charset = set_font_info(inst, 0);
2327 if (inst->cfg.boldfont.name[0]) {
2328 inst->fonts[1] = gdk_font_load(inst->cfg.boldfont.name);
2329 if (!inst->fonts[1]) {
2330 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2331 inst->cfg.boldfont.name);
2334 set_font_info(inst, 1);
2336 inst->fonts[1] = NULL;
2337 if (inst->cfg.widefont.name[0]) {
2338 inst->fonts[2] = gdk_font_load(inst->cfg.widefont.name);
2339 if (!inst->fonts[2]) {
2340 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2341 inst->cfg.widefont.name);
2344 set_font_info(inst, 2);
2346 inst->fonts[2] = NULL;
2347 if (inst->cfg.wideboldfont.name[0]) {
2348 inst->fonts[3] = gdk_font_load(inst->cfg.wideboldfont.name);
2349 if (!inst->fonts[3]) {
2350 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n",
2351 appname, inst->cfg.wideboldfont.name);
2354 set_font_info(inst, 3);
2356 inst->fonts[3] = NULL;
2358 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2359 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2361 inst->direct_to_font = init_ucs(&inst->ucsdata,
2362 inst->cfg.line_codepage, font_charset);
2365 void set_geom_hints(struct gui_data *inst)
2368 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2369 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2370 geom.max_width = geom.max_height = -1;
2371 geom.base_width = 2*inst->cfg.window_border;
2372 geom.base_height = 2*inst->cfg.window_border;
2373 geom.width_inc = inst->font_width;
2374 geom.height_inc = inst->font_height;
2375 geom.min_aspect = geom.max_aspect = 0;
2376 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2377 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2378 GDK_HINT_RESIZE_INC);
2381 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2383 struct gui_data *inst = (struct gui_data *)data;
2384 term_clrsb(inst->term);
2387 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2389 struct gui_data *inst = (struct gui_data *)data;
2390 term_pwron(inst->term);
2391 ldisc_send(inst->ldisc, NULL, 0, 0);
2394 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2396 struct gui_data *inst = (struct gui_data *)data;
2397 term_copyall(inst->term);
2400 void special_menuitem(GtkMenuItem *item, gpointer data)
2402 struct gui_data *inst = (struct gui_data *)data;
2403 int code = (int)gtk_object_get_data(GTK_OBJECT(item), "user-data");
2405 inst->back->special(inst->backhandle, code);
2408 void about_menuitem(GtkMenuItem *item, gpointer data)
2410 struct gui_data *inst = (struct gui_data *)data;
2411 about_box(inst->window);
2414 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2416 struct gui_data *inst = (struct gui_data *)data;
2417 showeventlog(inst->eventlogstuff, inst->window);
2420 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2422 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2423 static const int ww[] = {
2424 6, 7, 8, 9, 10, 11, 12, 13,
2425 14, 15, 16, 17, 18, 19, 20, 21,
2428 struct gui_data *inst = (struct gui_data *)data;
2429 char *title = dupcat(appname, " Reconfiguration", NULL);
2430 Config cfg2, oldcfg;
2433 cfg2 = inst->cfg; /* structure copy */
2435 if (do_config_box(title, &cfg2, 1)) {
2437 oldcfg = inst->cfg; /* structure copy */
2438 inst->cfg = cfg2; /* structure copy */
2440 /* Pass new config data to the logging module */
2441 log_reconfig(inst->logctx, &cfg2);
2443 * Flush the line discipline's edit buffer in the case
2444 * where local editing has just been disabled.
2446 ldisc_send(inst->ldisc, NULL, 0, 0);
2447 /* Pass new config data to the terminal */
2448 term_reconfig(inst->term, &cfg2);
2449 /* Pass new config data to the back end */
2450 inst->back->reconfig(inst->backhandle, &cfg2);
2453 * Just setting inst->cfg is sufficient to cause colour
2454 * setting changes to appear on the next ESC]R palette
2455 * reset. But we should also check whether any colour
2456 * settings have been changed, and revert the ones that
2457 * have to the new default, on the assumption that the user
2458 * is most likely to want an immediate update.
2460 for (i = 0; i < NCOLOURS; i++) {
2461 if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
2462 oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
2463 oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2])
2464 real_palette_set(inst, i, cfg2.colours[ww[i]][0],
2465 cfg2.colours[ww[i]][1],
2466 cfg2.colours[ww[i]][2]);
2470 * If the scrollbar needs to be shown, hidden, or moved
2471 * from one end to the other of the window, do so now.
2473 if (oldcfg.scrollbar != cfg2.scrollbar) {
2475 gtk_widget_show(inst->sbar);
2477 gtk_widget_hide(inst->sbar);
2479 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2480 gtk_box_reorder_child(inst->hbox, inst->sbar,
2481 cfg2.scrollbar_on_left ? 0 : 1);
2485 * Change the window title, if required.
2487 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2488 set_title(inst, cfg2.wintitle);
2491 * Redo the whole tangled fonts and Unicode mess if
2494 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2495 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2496 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2497 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
2498 strcmp(oldcfg.line_codepage, cfg2.line_codepage)) {
2499 setup_fonts_ucs(inst);
2505 * Resize the window.
2507 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2508 oldcfg.window_border != cfg2.window_border || need_size) {
2509 set_geom_hints(inst);
2510 request_resize(inst, cfg2.width, cfg2.height);
2511 //term_size(inst->term, cfg2.height, cfg2.width, cfg2.savelines);
2512 // where TF is our configure event going?!
2515 term_invalidate(inst->term);
2520 void update_specials_menu(void *frontend)
2522 Terminal *term = (Terminal *)frontend;
2523 struct gui_data *inst = (struct gui_data *)term->frontend;
2525 const struct telnet_special *specials;
2527 specials = inst->back->get_specials(inst->backhandle);
2528 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
2529 (GtkCallback)gtk_widget_destroy, NULL);
2532 GtkWidget *menuitem;
2533 for (i = 0; specials[i].name; i++) {
2534 if (*specials[i].name) {
2535 menuitem = gtk_menu_item_new_with_label(specials[i].name);
2536 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2537 (gpointer)specials[i].code);
2538 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2539 GTK_SIGNAL_FUNC(special_menuitem), inst);
2541 menuitem = gtk_menu_item_new();
2542 gtk_container_add(GTK_CONTAINER(inst->specialsmenu), menuitem);
2543 gtk_widget_show(menuitem);
2545 gtk_widget_show(inst->specialsitem1);
2546 gtk_widget_show(inst->specialsitem2);
2548 gtk_widget_hide(inst->specialsitem1);
2549 gtk_widget_hide(inst->specialsitem2);
2553 int pt_main(int argc, char **argv)
2555 extern Backend *select_backend(Config *cfg);
2556 extern int cfgbox(Config *cfg);
2557 struct gui_data *inst;
2559 /* defer any child exit handling until we're ready to deal with
2561 block_signal(SIGCHLD, 1);
2563 gtk_init(&argc, &argv);
2566 * Create an instance structure and initialise to zeroes
2568 inst = snew(struct gui_data);
2569 memset(inst, 0, sizeof(*inst));
2570 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
2572 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
2573 exit(1); /* pre-defaults pass to get -class */
2574 do_defaults(NULL, &inst->cfg);
2575 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
2576 exit(1); /* post-defaults, do everything */
2578 cmdline_run_saved(&inst->cfg);
2580 if (!*inst->cfg.host && !cfgbox(&inst->cfg))
2581 exit(0); /* config box hit Cancel */
2583 if (!compound_text_atom)
2584 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2585 if (!utf8_string_atom)
2586 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
2588 setup_fonts_ucs(inst);
2590 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2593 * Set up the colour map.
2595 palette_reset(inst);
2597 inst->width = inst->cfg.width;
2598 inst->height = inst->cfg.height;
2600 inst->area = gtk_drawing_area_new();
2601 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
2602 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
2603 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
2604 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2605 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2606 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2608 * We always create the scrollbar; it remains invisible if
2609 * unwanted, so we can pop it up quickly if it suddenly becomes
2612 if (inst->cfg.scrollbar_on_left)
2613 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2614 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2615 if (!inst->cfg.scrollbar_on_left)
2616 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2618 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2620 set_geom_hints(inst);
2622 gtk_widget_show(inst->area);
2623 if (inst->cfg.scrollbar)
2624 gtk_widget_show(inst->sbar);
2626 gtk_widget_hide(inst->sbar);
2627 gtk_widget_show(GTK_WIDGET(inst->hbox));
2630 int x = inst->xpos, y = inst->ypos;
2632 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
2633 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
2634 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
2635 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
2636 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
2639 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2640 GTK_SIGNAL_FUNC(destroy), inst);
2641 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2642 GTK_SIGNAL_FUNC(delete_window), inst);
2643 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2644 GTK_SIGNAL_FUNC(key_event), inst);
2645 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2646 GTK_SIGNAL_FUNC(key_event), inst);
2647 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2648 GTK_SIGNAL_FUNC(focus_event), inst);
2649 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2650 GTK_SIGNAL_FUNC(focus_event), inst);
2651 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2652 GTK_SIGNAL_FUNC(configure_area), inst);
2653 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2654 GTK_SIGNAL_FUNC(expose_area), inst);
2655 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2656 GTK_SIGNAL_FUNC(button_event), inst);
2657 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2658 GTK_SIGNAL_FUNC(button_event), inst);
2659 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2660 GTK_SIGNAL_FUNC(motion_event), inst);
2661 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2662 GTK_SIGNAL_FUNC(selection_received), inst);
2663 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2664 GTK_SIGNAL_FUNC(selection_get), inst);
2665 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2666 GTK_SIGNAL_FUNC(selection_clear), inst);
2667 if (inst->cfg.scrollbar)
2668 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2669 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2670 gtk_timeout_add(20, timer_func, inst);
2671 gtk_widget_add_events(GTK_WIDGET(inst->area),
2672 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2673 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2674 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2676 gtk_widget_show(inst->window);
2678 set_window_background(inst);
2681 * Set up the Ctrl+rightclick context menu.
2684 GtkWidget *menuitem;
2686 extern const int use_event_log;
2688 inst->menu = gtk_menu_new();
2690 #define MKMENUITEM(title, func) do { \
2691 menuitem = title ? gtk_menu_item_new_with_label(title) : \
2692 gtk_menu_item_new(); \
2693 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
2694 gtk_widget_show(menuitem); \
2696 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
2697 GTK_SIGNAL_FUNC(func), inst); \
2699 MKMENUITEM("Change Settings", change_settings_menuitem);
2700 MKMENUITEM(NULL, NULL);
2702 MKMENUITEM("Event Log", event_log_menuitem);
2703 MKMENUITEM("Special Commands", NULL);
2704 inst->specialsmenu = gtk_menu_new();
2705 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
2706 inst->specialsitem1 = menuitem;
2707 MKMENUITEM(NULL, NULL);
2708 inst->specialsitem2 = menuitem;
2709 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
2710 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
2711 MKMENUITEM("Copy All", copy_all_menuitem);
2712 MKMENUITEM(NULL, NULL);
2713 s = dupcat("About ", appname, NULL);
2714 MKMENUITEM(s, about_menuitem);
2719 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2720 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2721 inst->blankcursor = make_mouse_ptr(inst, -1);
2722 make_mouse_ptr(inst, -2); /* clean up cursor font */
2723 inst->currcursor = inst->textcursor;
2724 show_mouseptr(inst, 1);
2726 inst->eventlogstuff = eventlogstuff_new();
2728 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
2729 inst->logctx = log_init(inst, &inst->cfg);
2730 term_provide_logctx(inst->term, inst->logctx);
2734 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
2736 inst->back = select_backend(&inst->cfg);
2738 char *realhost, *error;
2740 error = inst->back->init((void *)inst->term, &inst->backhandle,
2741 &inst->cfg, inst->cfg.host, inst->cfg.port,
2742 &realhost, inst->cfg.tcp_nodelay);
2745 char *msg = dupprintf("Unable to open connection to %s:\n%s",
2746 inst->cfg.host, error);
2747 inst->exited = TRUE;
2748 fatal_message_box(inst->window, msg);
2753 if (inst->cfg.wintitle[0])
2754 set_title(inst, inst->cfg.wintitle);
2756 char *title = make_default_wintitle(realhost);
2757 set_title(inst, title);
2761 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2762 update_specials_menu(inst->term);
2764 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2767 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle, inst);
2768 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2770 /* now we're reday to deal with the child exit handler being
2772 block_signal(SIGCHLD, 0);
2774 inst->exited = FALSE;