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))
38 GtkWidget *window, *area, *sbar;
40 GtkAdjustment *sbar_adjust;
41 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2;
43 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
48 int xpos, ypos, gotpos, gravity;
49 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
50 GdkColor cols[NCOLOURS];
52 wchar_t *pastein_data;
55 char *pasteout_data, *pasteout_data_utf8;
56 int pasteout_data_len, pasteout_data_utf8_len;
57 int font_width, font_height;
61 guint term_paste_idle_id;
62 GdkAtom compound_text_atom, utf8_string_atom;
65 char wintitle[sizeof(((Config *)0)->wintitle)];
66 char icontitle[sizeof(((Config *)0)->wintitle)];
67 int master_fd, master_func_id;
74 struct unicode_data ucsdata;
81 struct gui_data *inst;
84 static int send_raw_mouse;
86 static char *app_name = "pterm";
88 char *x_get_default(const char *key)
90 return XGetDefault(GDK_DISPLAY(), app_name, key);
93 void connection_fatal(void *frontend, char *p, ...)
95 Terminal *term = (Terminal *)frontend;
96 struct gui_data *inst = (struct gui_data *)term->frontend;
101 msg = dupvprintf(p, ap);
104 fatal_message_box(inst->window, msg);
106 if (inst->cfg.close_on_exit == FORCE_ON)
111 * Default settings that are specific to pterm.
113 FontSpec platform_default_fontspec(const char *name)
116 if (!strcmp(name, "Font"))
117 strcpy(ret.name, "fixed");
123 Filename platform_default_filename(const char *name)
126 if (!strcmp(name, "LogFileName"))
127 strcpy(ret.path, "putty.log");
133 char *platform_default_s(const char *name)
138 int platform_default_i(const char *name, int def)
140 if (!strcmp(name, "CloseOnExit"))
141 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
145 void ldisc_update(void *frontend, int echo, int edit)
148 * This is a stub in pterm. If I ever produce a Unix
149 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
150 * then it will require some termios manoeuvring analogous to
151 * that in the Windows plink.c, but here it's meaningless.
155 int askappend(void *frontend, Filename filename)
158 * Logging in an xterm-alike is liable to be something you only
159 * do at serious diagnostic need. Hence, I'm going to take the
160 * easy option for now and assume we always want to overwrite
161 * log files. I can always make it properly configurable later.
166 void logevent(void *frontend, char *string)
168 Terminal *term = (Terminal *)frontend;
169 struct gui_data *inst = (struct gui_data *)term->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 Terminal *term = (Terminal *)frontend;
215 struct gui_data *inst = (struct gui_data *)term->frontend;
220 * Minimise or restore the window in response to a server-side
223 void set_iconic(void *frontend, int iconic)
226 * GTK 1.2 doesn't know how to do this.
228 #if GTK_CHECK_VERSION(2,0,0)
229 struct gui_data *inst = (struct gui_data *)frontend;
231 gtk_window_iconify(GTK_WINDOW(inst->window));
233 gtk_window_deiconify(GTK_WINDOW(inst->window));
238 * Move the window in response to a server-side request.
240 void move_window(void *frontend, int x, int y)
242 struct gui_data *inst = (struct gui_data *)frontend;
244 * I assume that when the GTK version of this call is available
245 * we should use it. Not sure how it differs from the GDK one,
248 #if GTK_CHECK_VERSION(2,0,0)
249 gtk_window_move(GTK_WINDOW(inst->window), x, y);
251 gdk_window_move(inst->window->window, x, y);
256 * Move the window to the top or bottom of the z-order in response
257 * to a server-side request.
259 void set_zorder(void *frontend, int top)
261 struct gui_data *inst = (struct gui_data *)frontend;
263 gdk_window_raise(inst->window->window);
265 gdk_window_lower(inst->window->window);
269 * Refresh the window in response to a server-side request.
271 void refresh_window(void *frontend)
273 struct gui_data *inst = (struct gui_data *)frontend;
274 term_invalidate(inst->term);
278 * Maximise or restore the window in response to a server-side
281 void set_zoomed(void *frontend, int zoomed)
284 * GTK 1.2 doesn't know how to do this.
286 #if GTK_CHECK_VERSION(2,0,0)
287 struct gui_data *inst = (struct gui_data *)frontend;
289 gtk_window_maximize(GTK_WINDOW(inst->window));
291 gtk_window_unmaximize(GTK_WINDOW(inst->window));
296 * Report whether the window is iconic, for terminal reports.
298 int is_iconic(void *frontend)
300 struct gui_data *inst = (struct gui_data *)frontend;
301 return !gdk_window_is_viewable(inst->window->window);
305 * Report the window's position, for terminal reports.
307 void get_window_pos(void *frontend, int *x, int *y)
309 struct gui_data *inst = (struct gui_data *)frontend;
311 * I assume that when the GTK version of this call is available
312 * we should use it. Not sure how it differs from the GDK one,
315 #if GTK_CHECK_VERSION(2,0,0)
316 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
318 gdk_window_get_position(inst->window->window, x, y);
323 * Report the window's pixel size, for terminal reports.
325 void get_window_pixels(void *frontend, int *x, int *y)
327 struct gui_data *inst = (struct gui_data *)frontend;
329 * I assume that when the GTK version of this call is available
330 * we should use it. Not sure how it differs from the GDK one,
333 #if GTK_CHECK_VERSION(2,0,0)
334 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
336 gdk_window_get_size(inst->window->window, x, y);
341 * Return the window or icon title.
343 char *get_window_title(void *frontend, int icon)
345 struct gui_data *inst = (struct gui_data *)frontend;
346 return icon ? inst->wintitle : inst->icontitle;
349 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
352 * We could implement warn-on-close here if we really wanted
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;
374 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
375 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
377 if (w != inst->width || h != inst->height) {
379 gdk_pixmap_unref(inst->pixmap);
382 inst->cfg.width = inst->width = w;
383 inst->cfg.height = inst->height = h;
389 inst->pixmap = gdk_pixmap_new(widget->window,
390 (inst->cfg.width * inst->font_width +
391 2*inst->cfg.window_border),
392 (inst->cfg.height * inst->font_height +
393 2*inst->cfg.window_border), -1);
395 gc = gdk_gc_new(inst->area->window);
396 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
397 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
398 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
399 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
404 term_size(inst->term, h, w, inst->cfg.savelines);
410 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
412 struct gui_data *inst = (struct gui_data *)data;
415 * Pass the exposed rectangle to terminal.c, which will call us
416 * back to do the actual painting.
419 gdk_draw_pixmap(widget->window,
420 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
422 event->area.x, event->area.y,
423 event->area.x, event->area.y,
424 event->area.width, event->area.height);
429 #define KEY_PRESSED(k) \
430 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
432 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
434 struct gui_data *inst = (struct gui_data *)data;
438 /* By default, nothing is generated. */
442 * If Alt is being released after typing an Alt+numberpad
443 * sequence, we should generate the code that was typed.
445 * Note that we only do this if more than one key was actually
446 * pressed - I don't think Alt+NumPad4 should be ^D or that
447 * Alt+NumPad3 should be ^C, for example. There's no serious
448 * inconvenience in having to type a zero before a single-digit
451 if (event->type == GDK_KEY_RELEASE &&
452 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
453 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
454 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
456 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
458 output[0] = inst->alt_keycode;
463 if (event->type == GDK_KEY_PRESS) {
467 printf("keypress: keyval = %04x, state = %08x; string =",
468 event->keyval, event->state);
469 for (i = 0; event->string[i]; i++)
470 printf(" %02x", (unsigned char) event->string[i]);
476 * NYI: Compose key (!!! requires Unicode faff before even trying)
480 * If Alt has just been pressed, we start potentially
481 * accumulating an Alt+numberpad code. We do this by
482 * setting alt_keycode to -1 (nothing yet but plausible).
484 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
485 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
486 inst->alt_keycode = -1;
487 inst->alt_digits = 0;
488 goto done; /* this generates nothing else */
492 * If we're seeing a numberpad key press with Mod1 down,
493 * consider adding it to alt_keycode if that's sensible.
494 * Anything _else_ with Mod1 down cancels any possibility
495 * of an ALT keycode: we set alt_keycode to -2.
497 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
499 switch (event->keyval) {
500 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
501 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
502 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
503 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
504 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
505 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
506 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
507 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
508 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
509 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
512 inst->alt_keycode = -2; /* it's invalid */
515 printf("Adding digit %d to keycode %d", digit,
518 if (inst->alt_keycode == -1)
519 inst->alt_keycode = digit; /* one-digit code */
521 inst->alt_keycode = inst->alt_keycode * 10 + digit;
524 printf(" gives new code %d\n", inst->alt_keycode);
526 /* Having used this digit, we now do nothing more with it. */
532 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
535 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
536 term_scroll(inst->term, 0, -inst->cfg.height/2);
539 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
540 term_scroll(inst->term, 0, -1);
543 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
544 term_scroll(inst->term, 0, +inst->cfg.height/2);
547 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
548 term_scroll(inst->term, 0, +1);
553 * Neither does Shift-Ins.
555 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
560 /* ALT+things gives leading Escape. */
562 strncpy(output+1, event->string, 31);
564 end = strlen(output);
565 if (event->state & GDK_MOD1_MASK) {
567 if (end == 1) end = 0;
571 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
572 if (!event->string[0] && event->keyval == '`' &&
573 (event->state & GDK_CONTROL_MASK)) {
578 /* Control-Break is the same as Control-C */
579 if (event->keyval == GDK_Break &&
580 (event->state & GDK_CONTROL_MASK)) {
585 /* Control-2, Control-Space and Control-@ are NUL */
586 if (!event->string[0] &&
587 (event->keyval == ' ' || event->keyval == '2' ||
588 event->keyval == '@') &&
589 (event->state & (GDK_SHIFT_MASK |
590 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
595 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
596 if (!event->string[0] && event->keyval == ' ' &&
597 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
598 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
603 /* We don't let GTK tell us what Backspace is! We know better. */
604 if (event->keyval == GDK_BackSpace &&
605 !(event->state & GDK_SHIFT_MASK)) {
606 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
609 /* For Shift Backspace, do opposite of what is configured. */
610 if (event->keyval == GDK_BackSpace &&
611 (event->state & GDK_SHIFT_MASK)) {
612 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
616 /* Shift-Tab is ESC [ Z */
617 if (event->keyval == GDK_ISO_Left_Tab ||
618 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
619 end = 1 + sprintf(output+1, "\033[Z");
623 * NetHack keypad mode.
625 if (inst->cfg.nethack_keypad) {
627 switch (event->keyval) {
628 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
629 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
630 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
631 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
632 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
633 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
634 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
635 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
636 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
640 if (event->state & GDK_SHIFT_MASK)
649 * Application keypad mode.
651 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
653 switch (event->keyval) {
654 case GDK_Num_Lock: xkey = 'P'; break;
655 case GDK_KP_Divide: xkey = 'Q'; break;
656 case GDK_KP_Multiply: xkey = 'R'; break;
657 case GDK_KP_Subtract: xkey = 'S'; break;
659 * Keypad + is tricky. It covers a space that would
660 * be taken up on the VT100 by _two_ keys; so we
661 * let Shift select between the two. Worse still,
662 * in xterm function key mode we change which two...
665 if (inst->cfg.funky_type == 2) {
666 if (event->state & GDK_SHIFT_MASK)
670 } else if (event->state & GDK_SHIFT_MASK)
675 case GDK_KP_Enter: xkey = 'M'; break;
676 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
677 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
678 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
679 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
680 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
681 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
682 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
683 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
684 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
685 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
686 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
689 if (inst->term->vt52_mode) {
690 if (xkey >= 'P' && xkey <= 'S')
691 end = 1 + sprintf(output+1, "\033%c", xkey);
693 end = 1 + sprintf(output+1, "\033?%c", xkey);
695 end = 1 + sprintf(output+1, "\033O%c", xkey);
701 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
702 * for integer decimal nn.)
704 * We also deal with the weird ones here. Linux VCs replace F1
705 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
706 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
711 switch (event->keyval) {
713 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
716 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
719 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
722 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
725 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
728 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
731 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
734 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
737 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
740 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
773 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
774 case GDK_Home: case GDK_KP_Home:
777 case GDK_Insert: case GDK_KP_Insert:
780 case GDK_Delete: case GDK_KP_Delete:
783 case GDK_End: case GDK_KP_End:
786 case GDK_Page_Up: case GDK_KP_Page_Up:
789 case GDK_Page_Down: case GDK_KP_Page_Down:
793 /* Reorder edit keys to physical order */
794 if (inst->cfg.funky_type == 3 && code <= 6)
795 code = "\0\2\1\4\5\3\6"[code];
797 if (inst->term->vt52_mode && code > 0 && code <= 6) {
798 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
802 if (inst->cfg.funky_type == 5 && /* SCO function keys */
803 code >= 11 && code <= 34) {
804 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
806 switch (event->keyval) {
807 case GDK_F1: index = 0; break;
808 case GDK_F2: index = 1; break;
809 case GDK_F3: index = 2; break;
810 case GDK_F4: index = 3; break;
811 case GDK_F5: index = 4; break;
812 case GDK_F6: index = 5; break;
813 case GDK_F7: index = 6; break;
814 case GDK_F8: index = 7; break;
815 case GDK_F9: index = 8; break;
816 case GDK_F10: index = 9; break;
817 case GDK_F11: index = 10; break;
818 case GDK_F12: index = 11; break;
820 if (event->state & GDK_SHIFT_MASK) index += 12;
821 if (event->state & GDK_CONTROL_MASK) index += 24;
822 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
825 if (inst->cfg.funky_type == 5 && /* SCO small keypad */
826 code >= 1 && code <= 6) {
827 char codes[] = "HL.FIG";
832 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
836 if ((inst->term->vt52_mode || inst->cfg.funky_type == 4) &&
837 code >= 11 && code <= 24) {
843 if (inst->term->vt52_mode)
844 end = 1 + sprintf(output+1,
845 "\x1B%c", code + 'P' - 11 - offt);
847 end = 1 + sprintf(output+1,
848 "\x1BO%c", code + 'P' - 11 - offt);
851 if (inst->cfg.funky_type == 1 && code >= 11 && code <= 15) {
852 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
855 if (inst->cfg.funky_type == 2 && code >= 11 && code <= 14) {
856 if (inst->term->vt52_mode)
857 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
859 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
862 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
863 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
867 end = 1 + sprintf(output+1, "\x1B[%d~", code);
873 * Cursor keys. (This includes the numberpad cursor keys,
874 * if we haven't already done them due to app keypad mode.)
876 * Here we also process un-numlocked un-appkeypadded KP5,
877 * which sends ESC [ G.
881 switch (event->keyval) {
882 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
883 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
884 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
885 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
886 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
890 * The arrow keys normally do ESC [ A and so on. In
891 * app cursor keys mode they do ESC O A instead.
892 * Ctrl toggles the two modes.
894 if (inst->term->vt52_mode) {
895 end = 1 + sprintf(output+1, "\033%c", xkey);
896 } else if (!inst->term->app_cursor_keys ^
897 !(event->state & GDK_CONTROL_MASK)) {
898 end = 1 + sprintf(output+1, "\033O%c", xkey);
900 end = 1 + sprintf(output+1, "\033[%c", xkey);
913 printf("generating sequence:");
914 for (i = start; i < end; i++)
915 printf(" %02x", (unsigned char) output[i]);
919 if (!inst->direct_to_font) {
921 * The stuff we've just generated is assumed to be
922 * ISO-8859-1! This sounds insane, but `man
923 * XLookupString' agrees: strings of this type returned
924 * from the X server are hardcoded to 8859-1. Strictly
925 * speaking we should be doing this using some sort of
926 * GtkIMContext, which (if we're lucky) would give us
927 * our data directly in Unicode; but that's not
928 * supported in GTK 1.2 as far as I can tell, and it's
929 * poorly documented even in 2.0, so it'll have to
932 lpage_send(inst->ldisc, CS_ISO8859_1, output+start, end-start, 1);
935 * In direct-to-font mode, we just send the string
936 * exactly as we received it.
938 ldisc_send(inst->ldisc, output+start, end-start, 1);
941 show_mouseptr(inst, 0);
942 term_seen_key_event(inst->term);
943 term_out(inst->term);
949 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
951 struct gui_data *inst = (struct gui_data *)data;
952 int shift, ctrl, alt, x, y, button, act;
954 show_mouseptr(inst, 1);
956 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
957 term_scroll(inst->term, 0, -5);
960 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
961 term_scroll(inst->term, 0, +5);
965 shift = event->state & GDK_SHIFT_MASK;
966 ctrl = event->state & GDK_CONTROL_MASK;
967 alt = event->state & GDK_MOD1_MASK;
969 if (event->button == 3 && ctrl) {
970 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
971 event->button, event->time);
975 if (event->button == 1)
977 else if (event->button == 2)
979 else if (event->button == 3)
982 return FALSE; /* don't even know what button! */
984 switch (event->type) {
985 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
986 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
987 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
988 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
989 default: return FALSE; /* don't know this event type */
992 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
993 act != MA_CLICK && act != MA_RELEASE)
994 return TRUE; /* we ignore these in raw mouse mode */
996 x = (event->x - inst->cfg.window_border) / inst->font_width;
997 y = (event->y - inst->cfg.window_border) / inst->font_height;
999 term_mouse(inst->term, button, translate_button(button), act,
1000 x, y, shift, ctrl, alt);
1005 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1007 struct gui_data *inst = (struct gui_data *)data;
1008 int shift, ctrl, alt, x, y, button;
1010 show_mouseptr(inst, 1);
1012 shift = event->state & GDK_SHIFT_MASK;
1013 ctrl = event->state & GDK_CONTROL_MASK;
1014 alt = event->state & GDK_MOD1_MASK;
1015 if (event->state & GDK_BUTTON1_MASK)
1017 else if (event->state & GDK_BUTTON2_MASK)
1018 button = MBT_MIDDLE;
1019 else if (event->state & GDK_BUTTON3_MASK)
1022 return FALSE; /* don't even know what button! */
1024 x = (event->x - inst->cfg.window_border) / inst->font_width;
1025 y = (event->y - inst->cfg.window_border) / inst->font_height;
1027 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1028 x, y, shift, ctrl, alt);
1033 void frontend_keypress(void *handle)
1035 struct gui_data *inst = (struct gui_data *)handle;
1038 * If our child process has exited but not closed, terminate on
1045 gint timer_func(gpointer data)
1047 struct gui_data *inst = (struct gui_data *)data;
1050 if (!inst->exited &&
1051 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1052 inst->exited = TRUE;
1053 if (inst->cfg.close_on_exit == FORCE_ON ||
1054 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1055 exit(0); /* just go. */
1058 term_update(inst->term);
1059 term_blink(inst->term, 0);
1063 void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1066 * We must process exceptional notifications before ordinary
1067 * readability ones, or we may go straight past the urgent
1070 if (condition & GDK_INPUT_EXCEPTION)
1071 select_result(sourcefd, 4);
1072 if (condition & GDK_INPUT_READ)
1073 select_result(sourcefd, 1);
1074 if (condition & GDK_INPUT_WRITE)
1075 select_result(sourcefd, 2);
1078 void destroy(GtkWidget *widget, gpointer data)
1083 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1085 struct gui_data *inst = (struct gui_data *)data;
1086 inst->term->has_focus = event->in;
1087 term_out(inst->term);
1088 term_update(inst->term);
1089 show_mouseptr(inst, 1);
1094 * set or clear the "raw mouse message" mode
1096 void set_raw_mouse_mode(void *frontend, int activate)
1098 struct gui_data *inst = (struct gui_data *)frontend;
1099 activate = activate && !inst->cfg.no_mouse_rep;
1100 send_raw_mouse = activate;
1102 inst->currcursor = inst->rawcursor;
1104 inst->currcursor = inst->textcursor;
1105 show_mouseptr(inst, inst->mouseptr_visible);
1108 void request_resize(void *frontend, int w, int h)
1110 struct gui_data *inst = (struct gui_data *)frontend;
1111 int large_x, large_y;
1112 int offset_x, offset_y;
1114 GtkRequisition inner, outer;
1117 * This is a heinous hack dreamed up by the gnome-terminal
1118 * people to get around a limitation in gtk. The problem is
1119 * that in order to set the size correctly we really need to be
1120 * calling gtk_window_resize - but that needs to know the size
1121 * of the _whole window_, not the drawing area. So what we do
1122 * is to set an artificially huge size request on the drawing
1123 * area, recompute the resulting size request on the window,
1124 * and look at the difference between the two. That gives us
1125 * the x and y offsets we need to translate drawing area size
1126 * into window size for real, and then we call
1127 * gtk_window_resize.
1131 * We start by retrieving the current size of the whole window.
1132 * Adding a bit to _that_ will give us a value we can use as a
1133 * bogus size request which guarantees to be bigger than the
1134 * current size of the drawing area.
1136 get_window_pixels(inst, &large_x, &large_y);
1140 #if GTK_CHECK_VERSION(2,0,0)
1141 gtk_widget_set_size_request(inst->area, large_x, large_y);
1143 gtk_widget_set_usize(inst->area, large_x, large_y);
1145 gtk_widget_size_request(inst->area, &inner);
1146 gtk_widget_size_request(inst->window, &outer);
1148 offset_x = outer.width - inner.width;
1149 offset_y = outer.height - inner.height;
1151 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1152 area_y = inst->font_height * h + 2*inst->cfg.window_border;
1155 * Now we must set the size request on the drawing area back to
1156 * something sensible before we commit the real resize. Best
1157 * way to do this, I think, is to set it to what the size is
1158 * really going to end up being.
1160 #if GTK_CHECK_VERSION(2,0,0)
1161 gtk_widget_set_size_request(inst->area, area_x, area_y);
1163 gtk_widget_set_usize(inst->area, area_x, area_y);
1164 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
1167 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1169 #if GTK_CHECK_VERSION(2,0,0)
1170 gtk_window_resize(GTK_WINDOW(inst->window),
1171 area_x + offset_x, area_y + offset_y);
1173 gdk_window_resize(inst->window->window,
1174 area_x + offset_x, area_y + offset_y);
1178 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1180 gboolean success[1];
1182 inst->cols[n].red = r * 0x0101;
1183 inst->cols[n].green = g * 0x0101;
1184 inst->cols[n].blue = b * 0x0101;
1186 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
1187 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1188 FALSE, FALSE, success);
1190 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
1194 void set_window_background(struct gui_data *inst)
1196 if (inst->area && inst->area->window)
1197 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1198 if (inst->window && inst->window->window)
1199 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1202 void palette_set(void *frontend, int n, int r, int g, int b)
1204 struct gui_data *inst = (struct gui_data *)frontend;
1205 static const int first[21] = {
1206 0, 2, 4, 6, 8, 10, 12, 14,
1207 1, 3, 5, 7, 9, 11, 13, 15,
1210 real_palette_set(inst, first[n], r, g, b);
1212 real_palette_set(inst, first[n] + 1, r, g, b);
1214 set_window_background(inst);
1217 void palette_reset(void *frontend)
1219 struct gui_data *inst = (struct gui_data *)frontend;
1220 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1221 static const int ww[] = {
1222 6, 7, 8, 9, 10, 11, 12, 13,
1223 14, 15, 16, 17, 18, 19, 20, 21,
1226 gboolean success[NCOLOURS];
1229 assert(lenof(ww) == NCOLOURS);
1231 if (!inst->colmap) {
1232 inst->colmap = gdk_colormap_get_system();
1234 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1237 for (i = 0; i < NCOLOURS; i++) {
1238 inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
1239 inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
1240 inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
1243 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1244 FALSE, FALSE, success);
1245 for (i = 0; i < NCOLOURS; i++) {
1247 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1248 appname, i, inst->cfg.colours[i][0],
1249 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
1252 set_window_background(inst);
1255 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1257 struct gui_data *inst = (struct gui_data *)frontend;
1258 if (inst->pasteout_data)
1259 sfree(inst->pasteout_data);
1260 if (inst->pasteout_data_utf8)
1261 sfree(inst->pasteout_data_utf8);
1264 * Set up UTF-8 paste data. This only happens if we aren't in
1265 * direct-to-font mode using the D800 hack.
1267 if (!inst->direct_to_font) {
1268 wchar_t *tmp = data;
1271 inst->pasteout_data_utf8 = snewn(len*6, char);
1272 inst->pasteout_data_utf8_len = len*6;
1273 inst->pasteout_data_utf8_len =
1274 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1275 inst->pasteout_data_utf8_len,
1276 CS_UTF8, NULL, NULL, 0);
1277 if (inst->pasteout_data_utf8_len == 0) {
1278 sfree(inst->pasteout_data_utf8);
1279 inst->pasteout_data_utf8 = NULL;
1281 inst->pasteout_data_utf8 =
1282 sresize(inst->pasteout_data_utf8,
1283 inst->pasteout_data_utf8_len, char);
1286 inst->pasteout_data_utf8 = NULL;
1287 inst->pasteout_data_utf8_len = 0;
1290 inst->pasteout_data = snewn(len*6, char);
1291 inst->pasteout_data_len = len*6;
1292 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1293 data, len, inst->pasteout_data,
1294 inst->pasteout_data_len,
1296 if (inst->pasteout_data_len == 0) {
1297 sfree(inst->pasteout_data);
1298 inst->pasteout_data = NULL;
1300 inst->pasteout_data =
1301 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1304 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1305 GDK_CURRENT_TIME)) {
1306 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1307 GDK_SELECTION_TYPE_STRING, 1);
1308 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1309 inst->compound_text_atom, 1);
1310 if (inst->pasteout_data_utf8)
1311 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1312 inst->utf8_string_atom, 1);
1316 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1317 guint info, guint time_stamp, gpointer data)
1319 struct gui_data *inst = (struct gui_data *)data;
1320 if (seldata->target == inst->utf8_string_atom)
1321 gtk_selection_data_set(seldata, seldata->target, 8,
1322 inst->pasteout_data_utf8,
1323 inst->pasteout_data_utf8_len);
1325 gtk_selection_data_set(seldata, seldata->target, 8,
1326 inst->pasteout_data, inst->pasteout_data_len);
1329 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1332 struct gui_data *inst = (struct gui_data *)data;
1333 term_deselect(inst->term);
1334 if (inst->pasteout_data)
1335 sfree(inst->pasteout_data);
1336 if (inst->pasteout_data_utf8)
1337 sfree(inst->pasteout_data_utf8);
1338 inst->pasteout_data = NULL;
1339 inst->pasteout_data_len = 0;
1340 inst->pasteout_data_utf8 = NULL;
1341 inst->pasteout_data_utf8_len = 0;
1345 void request_paste(void *frontend)
1347 struct gui_data *inst = (struct gui_data *)frontend;
1349 * In Unix, pasting is asynchronous: all we can do at the
1350 * moment is to call gtk_selection_convert(), and when the data
1351 * comes back _then_ we can call term_do_paste().
1354 if (!inst->direct_to_font) {
1356 * First we attempt to retrieve the selection as a UTF-8
1357 * string (which we will convert to the correct code page
1358 * before sending to the session, of course). If that
1359 * fails, selection_received() will be informed and will
1360 * fall back to an ordinary string.
1362 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1363 inst->utf8_string_atom, GDK_CURRENT_TIME);
1366 * If we're in direct-to-font mode, we disable UTF-8
1367 * pasting, and go straight to ordinary string data.
1369 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1370 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1374 gint idle_paste_func(gpointer data); /* forward ref */
1376 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1377 guint time, gpointer data)
1379 struct gui_data *inst = (struct gui_data *)data;
1381 if (seldata->target == inst->utf8_string_atom && seldata->length <= 0) {
1383 * Failed to get a UTF-8 selection string. Try an ordinary
1386 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1387 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1392 * Any other failure should just go foom.
1394 if (seldata->length <= 0 ||
1395 (seldata->type != GDK_SELECTION_TYPE_STRING &&
1396 seldata->type != inst->utf8_string_atom))
1397 return; /* Nothing happens. */
1399 if (inst->pastein_data)
1400 sfree(inst->pastein_data);
1402 inst->pastein_data = snewn(seldata->length, wchar_t);
1403 inst->pastein_data_len = seldata->length;
1404 inst->pastein_data_len =
1405 mb_to_wc((seldata->type == inst->utf8_string_atom ?
1406 CS_UTF8 : inst->ucsdata.line_codepage),
1407 0, seldata->data, seldata->length,
1408 inst->pastein_data, inst->pastein_data_len);
1410 term_do_paste(inst->term);
1412 if (term_paste_pending(inst->term))
1413 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1416 gint idle_paste_func(gpointer data)
1418 struct gui_data *inst = (struct gui_data *)data;
1420 if (term_paste_pending(inst->term))
1421 term_paste(inst->term);
1423 gtk_idle_remove(inst->term_paste_idle_id);
1429 void get_clip(void *frontend, wchar_t ** p, int *len)
1431 struct gui_data *inst = (struct gui_data *)frontend;
1434 *p = inst->pastein_data;
1435 *len = inst->pastein_data_len;
1439 void set_title(void *frontend, char *title)
1441 struct gui_data *inst = (struct gui_data *)frontend;
1442 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1443 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1444 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1447 void set_icon(void *frontend, char *title)
1449 struct gui_data *inst = (struct gui_data *)frontend;
1450 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1451 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1452 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1455 void set_sbar(void *frontend, int total, int start, int page)
1457 struct gui_data *inst = (struct gui_data *)frontend;
1458 if (!inst->cfg.scrollbar)
1460 inst->sbar_adjust->lower = 0;
1461 inst->sbar_adjust->upper = total;
1462 inst->sbar_adjust->value = start;
1463 inst->sbar_adjust->page_size = page;
1464 inst->sbar_adjust->step_increment = 1;
1465 inst->sbar_adjust->page_increment = page/2;
1466 inst->ignore_sbar = TRUE;
1467 gtk_adjustment_changed(inst->sbar_adjust);
1468 inst->ignore_sbar = FALSE;
1471 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1473 struct gui_data *inst = (struct gui_data *)data;
1475 if (!inst->cfg.scrollbar)
1477 if (!inst->ignore_sbar)
1478 term_scroll(inst->term, 1, (int)adj->value);
1481 void sys_cursor(void *frontend, int x, int y)
1484 * This is meaningless under X.
1489 * This is still called when mode==BELL_VISUAL, even though the
1490 * visual bell is handled entirely within terminal.c, because we
1491 * may want to perform additional actions on any kind of bell (for
1492 * example, taskbar flashing in Windows).
1494 void beep(void *frontend, int mode)
1496 if (mode != BELL_VISUAL)
1500 int char_width(Context ctx, int uc)
1503 * Under X, any fixed-width font really _is_ fixed-width.
1504 * Double-width characters will be dealt with using a separate
1505 * font. For the moment we can simply return 1.
1510 Context get_ctx(void *frontend)
1512 struct gui_data *inst = (struct gui_data *)frontend;
1513 struct draw_ctx *dctx;
1515 if (!inst->area->window)
1518 dctx = snew(struct draw_ctx);
1520 dctx->gc = gdk_gc_new(inst->area->window);
1524 void free_ctx(Context ctx)
1526 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1527 /* struct gui_data *inst = dctx->inst; */
1528 GdkGC *gc = dctx->gc;
1534 * Draw a line of text in the window, at given character
1535 * coordinates, in given attributes.
1537 * We are allowed to fiddle with the contents of `text'.
1539 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1540 unsigned long attr, int lattr)
1542 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1543 struct gui_data *inst = dctx->inst;
1544 GdkGC *gc = dctx->gc;
1546 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1548 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1549 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
1550 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1551 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
1552 if (attr & ATTR_REVERSE) {
1557 if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
1559 if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
1561 if (attr & TATTR_ACTCURS) {
1566 fontid = shadow = 0;
1568 if (attr & ATTR_WIDE) {
1575 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1576 if (inst->fonts[fontid | 1])
1582 if (lattr != LATTR_NORM) {
1584 if (x >= inst->term->cols)
1586 if (x + len*2*widefactor > inst->term->cols)
1587 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1595 r.x = x*inst->font_width+inst->cfg.window_border;
1596 r.y = y*inst->font_height+inst->cfg.window_border;
1597 r.width = rlen*widefactor*inst->font_width;
1598 r.height = inst->font_height;
1599 gdk_gc_set_clip_rectangle(gc, &r);
1602 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1603 gdk_draw_rectangle(inst->pixmap, gc, 1,
1604 x*inst->font_width+inst->cfg.window_border,
1605 y*inst->font_height+inst->cfg.window_border,
1606 rlen*widefactor*inst->font_width, inst->font_height);
1608 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1615 wcs = snewn(len+1, wchar_t);
1616 for (i = 0; i < len; i++) {
1617 wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
1620 if (inst->fonts[fontid] == NULL) {
1622 * The font for this contingency does not exist.
1623 * Typically this means we've been given ATTR_WIDE
1624 * character and have no wide font. So we display
1625 * nothing at all; such is life.
1627 } else if (inst->fontinfo[fontid].is_wide) {
1629 * At least one version of gdk_draw_text_wc() has a
1630 * weird bug whereby it reads `len' elements of the
1631 * input string, but only draws `len/2'. Hence I'm
1632 * going to make its input array twice as long as it
1633 * theoretically needs to be, and pass in twice the
1634 * actual number of characters. If a fixed gdk actually
1635 * takes the doubled length seriously, then (a) the
1636 * array will stand scrutiny up to the full length, (b)
1637 * the spare elements of the array are full of zeroes
1638 * which will probably be an empty glyph in the font,
1639 * and (c) the clip rectangle should prevent it causing
1642 gwcs = snewn(len*2+1, GdkWChar);
1643 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
1645 * FIXME: when we have a wide-char equivalent of
1646 * from_unicode, use it instead of this.
1648 for (i = 0; i <= len; i++)
1650 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
1651 x*inst->font_width+inst->cfg.window_border,
1652 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1656 gcs = snewn(len+1, gchar);
1657 wc_to_mb(inst->fontinfo[fontid].charset, 0,
1658 wcs, len, gcs, len, ".", NULL, NULL);
1659 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1660 x*inst->font_width+inst->cfg.window_border,
1661 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1669 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1670 x*inst->font_width+inst->cfg.window_border + inst->cfg.shadowboldoffset,
1671 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1675 if (attr & ATTR_UNDER) {
1676 int uheight = inst->fonts[0]->ascent + 1;
1677 if (uheight >= inst->font_height)
1678 uheight = inst->font_height - 1;
1679 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
1680 y*inst->font_height + uheight + inst->cfg.window_border,
1681 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
1682 y*inst->font_height + uheight + inst->cfg.window_border);
1685 if (lattr != LATTR_NORM) {
1687 * I can't find any plausible StretchBlt equivalent in the
1688 * X server, so I'm going to do this the slow and painful
1689 * way. This will involve repeated calls to
1690 * gdk_draw_pixmap() to stretch the text horizontally. It's
1691 * O(N^2) in time and O(N) in network bandwidth, but you
1692 * try thinking of a better way. :-(
1695 for (i = 0; i < len * widefactor * inst->font_width; i++) {
1696 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1697 x*inst->font_width+inst->cfg.window_border + 2*i,
1698 y*inst->font_height+inst->cfg.window_border,
1699 x*inst->font_width+inst->cfg.window_border + 2*i+1,
1700 y*inst->font_height+inst->cfg.window_border,
1701 len * inst->font_width - i, inst->font_height);
1704 if (lattr != LATTR_WIDE) {
1706 /* Now stretch vertically, in the same way. */
1707 if (lattr == LATTR_BOT)
1711 for (i = 0; i < inst->font_height; i+=2) {
1712 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1713 x*inst->font_width+inst->cfg.window_border,
1714 y*inst->font_height+inst->cfg.window_border+dt*i+db,
1715 x*widefactor*inst->font_width+inst->cfg.window_border,
1716 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
1717 len * inst->font_width, inst->font_height-i-1);
1723 void do_text(Context ctx, int x, int y, char *text, int len,
1724 unsigned long attr, int lattr)
1726 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1727 struct gui_data *inst = dctx->inst;
1728 GdkGC *gc = dctx->gc;
1731 do_text_internal(ctx, x, y, text, len, attr, lattr);
1733 if (attr & ATTR_WIDE) {
1739 if (lattr != LATTR_NORM) {
1741 if (x >= inst->term->cols)
1743 if (x + len*2*widefactor > inst->term->cols)
1744 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1748 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1749 x*inst->font_width+inst->cfg.window_border,
1750 y*inst->font_height+inst->cfg.window_border,
1751 x*inst->font_width+inst->cfg.window_border,
1752 y*inst->font_height+inst->cfg.window_border,
1753 len*widefactor*inst->font_width, inst->font_height);
1756 void do_cursor(Context ctx, int x, int y, char *text, int len,
1757 unsigned long attr, int lattr)
1759 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1760 struct gui_data *inst = dctx->inst;
1761 GdkGC *gc = dctx->gc;
1763 int passive, widefactor;
1765 if (attr & TATTR_PASCURS) {
1766 attr &= ~TATTR_PASCURS;
1770 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
1771 attr &= ~TATTR_ACTCURS;
1773 do_text_internal(ctx, x, y, text, len, attr, lattr);
1775 if (attr & ATTR_WIDE) {
1781 if (lattr != LATTR_NORM) {
1783 if (x >= inst->term->cols)
1785 if (x + len*2*widefactor > inst->term->cols)
1786 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1790 if (inst->cfg.cursor_type == 0) {
1792 * An active block cursor will already have been done by
1793 * the above do_text call, so we only need to do anything
1797 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1798 gdk_draw_rectangle(inst->pixmap, gc, 0,
1799 x*inst->font_width+inst->cfg.window_border,
1800 y*inst->font_height+inst->cfg.window_border,
1801 len*inst->font_width-1, inst->font_height-1);
1805 int startx, starty, dx, dy, length, i;
1809 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1810 char_width = 2*inst->font_width;
1812 char_width = inst->font_width;
1814 if (inst->cfg.cursor_type == 1) {
1815 uheight = inst->fonts[0]->ascent + 1;
1816 if (uheight >= inst->font_height)
1817 uheight = inst->font_height - 1;
1819 startx = x * inst->font_width + inst->cfg.window_border;
1820 starty = y * inst->font_height + inst->cfg.window_border + uheight;
1823 length = len * char_width;
1826 if (attr & TATTR_RIGHTCURS)
1827 xadjust = char_width - 1;
1828 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
1829 starty = y * inst->font_height + inst->cfg.window_border;
1832 length = inst->font_height;
1835 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1837 for (i = 0; i < length; i++) {
1839 gdk_draw_point(inst->pixmap, gc, startx, starty);
1845 gdk_draw_line(inst->pixmap, gc, startx, starty,
1846 startx + (length-1) * dx, starty + (length-1) * dy);
1850 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1851 x*inst->font_width+inst->cfg.window_border,
1852 y*inst->font_height+inst->cfg.window_border,
1853 x*inst->font_width+inst->cfg.window_border,
1854 y*inst->font_height+inst->cfg.window_border,
1855 len*widefactor*inst->font_width, inst->font_height);
1858 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1861 * Truly hideous hack: GTK doesn't allow us to set the mouse
1862 * cursor foreground and background colours unless we've _also_
1863 * created our own cursor from bitmaps. Therefore, I need to
1864 * load the `cursor' font and draw glyphs from it on to
1865 * pixmaps, in order to construct my cursors with the fg and bg
1866 * I want. This is a gross hack, but it's more self-contained
1867 * than linking in Xlib to find the X window handle to
1868 * inst->area and calling XRecolorCursor, and it's more
1869 * futureproof than hard-coding the shapes as bitmap arrays.
1871 static GdkFont *cursor_font = NULL;
1872 GdkPixmap *source, *mask;
1874 GdkColor cfg = { 0, 65535, 65535, 65535 };
1875 GdkColor cbg = { 0, 0, 0, 0 };
1876 GdkColor dfg = { 1, 65535, 65535, 65535 };
1877 GdkColor dbg = { 0, 0, 0, 0 };
1880 gint lb, rb, wid, asc, desc, w, h, x, y;
1882 if (cursor_val == -2) {
1883 gdk_font_unref(cursor_font);
1887 if (cursor_val >= 0 && !cursor_font)
1888 cursor_font = gdk_font_load("cursor");
1891 * Get the text extent of the cursor in question. We use the
1892 * mask character for this, because it's typically slightly
1893 * bigger than the main character.
1895 if (cursor_val >= 0) {
1897 text[0] = (char)cursor_val + 1;
1898 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1899 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1905 source = gdk_pixmap_new(NULL, w, h, 1);
1906 mask = gdk_pixmap_new(NULL, w, h, 1);
1909 * Draw the mask character on the mask pixmap.
1911 gc = gdk_gc_new(mask);
1912 gdk_gc_set_foreground(gc, &dbg);
1913 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1914 if (cursor_val >= 0) {
1916 text[0] = (char)cursor_val + 1;
1917 gdk_gc_set_foreground(gc, &dfg);
1918 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1923 * Draw the main character on the source pixmap.
1925 gc = gdk_gc_new(source);
1926 gdk_gc_set_foreground(gc, &dbg);
1927 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1928 if (cursor_val >= 0) {
1930 text[0] = (char)cursor_val;
1931 gdk_gc_set_foreground(gc, &dfg);
1932 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1937 * Create the cursor.
1939 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1944 gdk_pixmap_unref(source);
1945 gdk_pixmap_unref(mask);
1950 void modalfatalbox(char *p, ...)
1953 fprintf(stderr, "FATAL ERROR: ");
1955 vfprintf(stderr, p, ap);
1957 fputc('\n', stderr);
1961 void cmdline_error(char *p, ...)
1964 fprintf(stderr, "%s: ", appname);
1966 vfprintf(stderr, p, ap);
1968 fputc('\n', stderr);
1972 char *get_x_display(void *frontend)
1974 return gdk_get_display();
1977 long get_windowid(void *frontend)
1979 Terminal *term = (Terminal *)frontend;
1980 struct gui_data *inst = (struct gui_data *)(term->frontend);
1981 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
1984 static void help(FILE *fp) {
1986 "pterm option summary:\n"
1988 " --display DISPLAY Specify X display to use (note '--')\n"
1989 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1990 " -fn FONT Normal text font\n"
1991 " -fb FONT Bold text font\n"
1992 " -geometry GEOMETRY Position and size of window (size in characters)\n"
1993 " -sl LINES Number of lines of scrollback\n"
1994 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1995 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1996 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1997 " -T TITLE Window title\n"
1998 " -ut, +ut Do(default) or do not update utmp\n"
1999 " -ls, +ls Do(default) or do not make shell a login shell\n"
2000 " -sb, +sb Do(default) or do not display a scrollbar\n"
2001 " -log PATH Log all output to a file\n"
2002 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2003 " -xrm RESOURCE-STRING Set an X resource\n"
2004 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2005 ) < 0 || fflush(fp) < 0) {
2006 perror("output error");
2011 int do_cmdline(int argc, char **argv, int do_everything,
2012 struct gui_data *inst, Config *cfg)
2015 extern char **pty_argv; /* declared in pty.c */
2016 extern int use_pty_argv;
2019 * Macros to make argument handling easier. Note that because
2020 * they need to call `continue', they cannot be contained in
2021 * the usual do {...} while (0) wrapper to make them
2022 * syntactically single statements; hence it is not legal to
2023 * use one of these macros as an unbraced statement between
2026 #define EXPECTS_ARG { \
2027 if (--argc <= 0) { \
2029 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2034 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2037 while (--argc > 0) {
2041 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2042 do_everything ? 1 : -1, cfg);
2045 cmdline_error("option \"%s\" requires an argument", p);
2046 } else if (ret == 2) {
2047 --argc, ++argv; /* skip next argument */
2049 } else if (ret == 1) {
2053 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2056 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2057 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2059 } else if (!strcmp(p, "-fb")) {
2062 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2063 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2065 } else if (!strcmp(p, "-fw")) {
2068 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2069 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2071 } else if (!strcmp(p, "-fwb")) {
2074 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2075 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2077 } else if (!strcmp(p, "-cs")) {
2080 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2081 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2083 } else if (!strcmp(p, "-geometry")) {
2084 int flags, x, y, w, h;
2088 flags = XParseGeometry(val, &x, &y, &w, &h);
2089 if (flags & WidthValue)
2091 if (flags & HeightValue)
2094 if (flags & (XValue | YValue)) {
2097 inst->gotpos = TRUE;
2098 inst->gravity = ((flags & XNegative ? 1 : 0) |
2099 (flags & YNegative ? 2 : 0));
2102 } else if (!strcmp(p, "-sl")) {
2105 cfg->savelines = atoi(val);
2107 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2108 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2109 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2114 if (!gdk_color_parse(val, &col)) {
2116 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2120 index = (!strcmp(p, "-fg") ? 0 :
2121 !strcmp(p, "-bg") ? 2 :
2122 !strcmp(p, "-bfg") ? 1 :
2123 !strcmp(p, "-bbg") ? 3 :
2124 !strcmp(p, "-cfg") ? 4 :
2125 !strcmp(p, "-cbg") ? 5 : -1);
2126 assert(index != -1);
2127 cfg->colours[index][0] = col.red / 256;
2128 cfg->colours[index][1] = col.green / 256;
2129 cfg->colours[index][2] = col.blue / 256;
2132 } else if (use_pty_argv && !strcmp(p, "-e")) {
2133 /* This option swallows all further arguments. */
2139 pty_argv = snewn(argc+1, char *);
2141 for (i = 0; i < argc; i++)
2142 pty_argv[i] = argv[i];
2143 pty_argv[argc] = NULL;
2144 break; /* finished command-line processing */
2146 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2149 } else if (!strcmp(p, "-title")) {
2152 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2153 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2155 } else if (!strcmp(p, "-log")) {
2158 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2159 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2160 cfg->logtype = LGTYP_DEBUG;
2162 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2164 cfg->stamp_utmp = 0;
2166 } else if (!strcmp(p, "-ut")) {
2168 cfg->stamp_utmp = 1;
2170 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2172 cfg->login_shell = 0;
2174 } else if (!strcmp(p, "-ls")) {
2176 cfg->login_shell = 1;
2178 } else if (!strcmp(p, "-nethack")) {
2180 cfg->nethack_keypad = 1;
2182 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2186 } else if (!strcmp(p, "-sb")) {
2190 } else if (!strcmp(p, "-name")) {
2194 } else if (!strcmp(p, "-xrm")) {
2196 provide_xrm_string(val);
2198 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2202 } else if(p[0] != '-' && (!do_everything ||
2203 process_nonoption_arg(p, cfg))) {
2208 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2215 static void block_signal(int sig, int block_it) {
2219 sigaddset(&ss, sig);
2220 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2221 perror("sigprocmask");
2227 * This function retrieves the character set encoding of a font. It
2228 * returns the character set without the X11 hack (in case the user
2229 * asks to use the font's own encoding).
2231 static int set_font_info(struct gui_data *inst, int fontid)
2233 GdkFont *font = inst->fonts[fontid];
2234 XFontStruct *xfs = GDK_FONT_XFONT(font);
2235 Display *disp = GDK_FONT_XDISPLAY(font);
2236 Atom charset_registry, charset_encoding;
2237 unsigned long registry_ret, encoding_ret;
2238 int retval = CS_NONE;
2240 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2241 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2242 inst->fontinfo[fontid].charset = CS_NONE;
2243 inst->fontinfo[fontid].is_wide = 0;
2244 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2245 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2247 reg = XGetAtomName(disp, (Atom)registry_ret);
2248 enc = XGetAtomName(disp, (Atom)encoding_ret);
2250 char *encoding = dupcat(reg, "-", enc, NULL);
2251 retval = inst->fontinfo[fontid].charset =
2252 charset_from_xenc(encoding);
2253 /* FIXME: when libcharset supports wide encodings fix this. */
2254 if (!strcasecmp(encoding, "iso10646-1")) {
2255 inst->fontinfo[fontid].is_wide = 1;
2260 * Hack for X line-drawing characters: if the primary
2261 * font is encoded as ISO-8859-anything, and has valid
2262 * glyphs in the first 32 char positions, it is assumed
2263 * that those glyphs are the VT100 line-drawing
2266 * Actually, we'll hack even harder by only checking
2267 * position 0x19 (vertical line, VT100 linedrawing
2268 * `x'). Then we can check it easily by seeing if the
2269 * ascent and descent differ.
2271 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2272 int lb, rb, wid, asc, desc;
2277 gdk_string_extents(inst->fonts[fontid], text,
2278 &lb, &rb, &wid, &asc, &desc);
2280 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2290 int uxsel_input_add(int fd, int rwx) {
2292 if (rwx & 1) flags |= GDK_INPUT_READ;
2293 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2294 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2295 return gdk_input_add(fd, flags, fd_input_func, NULL);
2298 void uxsel_input_remove(int id) {
2299 gdk_input_remove(id);
2302 void setup_fonts_ucs(struct gui_data *inst)
2307 gdk_font_unref(inst->fonts[0]);
2309 gdk_font_unref(inst->fonts[1]);
2311 gdk_font_unref(inst->fonts[2]);
2313 gdk_font_unref(inst->fonts[3]);
2315 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2316 if (!inst->fonts[0]) {
2317 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2318 inst->cfg.font.name);
2321 font_charset = set_font_info(inst, 0);
2322 if (inst->cfg.boldfont.name[0]) {
2323 inst->fonts[1] = gdk_font_load(inst->cfg.boldfont.name);
2324 if (!inst->fonts[1]) {
2325 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2326 inst->cfg.boldfont.name);
2329 set_font_info(inst, 1);
2331 inst->fonts[1] = NULL;
2332 if (inst->cfg.widefont.name[0]) {
2333 inst->fonts[2] = gdk_font_load(inst->cfg.widefont.name);
2334 if (!inst->fonts[2]) {
2335 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2336 inst->cfg.widefont.name);
2339 set_font_info(inst, 2);
2341 inst->fonts[2] = NULL;
2342 if (inst->cfg.wideboldfont.name[0]) {
2343 inst->fonts[3] = gdk_font_load(inst->cfg.wideboldfont.name);
2344 if (!inst->fonts[3]) {
2345 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n",
2346 appname, inst->cfg.wideboldfont.name);
2349 set_font_info(inst, 3);
2351 inst->fonts[3] = NULL;
2353 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2354 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2356 inst->direct_to_font = init_ucs(&inst->ucsdata,
2357 inst->cfg.line_codepage, font_charset);
2360 void set_geom_hints(struct gui_data *inst)
2363 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2364 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2365 geom.max_width = geom.max_height = -1;
2366 geom.base_width = 2*inst->cfg.window_border;
2367 geom.base_height = 2*inst->cfg.window_border;
2368 geom.width_inc = inst->font_width;
2369 geom.height_inc = inst->font_height;
2370 geom.min_aspect = geom.max_aspect = 0;
2371 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2372 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2373 GDK_HINT_RESIZE_INC);
2376 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2378 struct gui_data *inst = (struct gui_data *)data;
2379 term_clrsb(inst->term);
2382 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2384 struct gui_data *inst = (struct gui_data *)data;
2385 term_pwron(inst->term);
2386 ldisc_send(inst->ldisc, NULL, 0, 0);
2389 void special_menuitem(GtkMenuItem *item, gpointer data)
2391 struct gui_data *inst = (struct gui_data *)data;
2392 int code = (int)gtk_object_get_data(GTK_OBJECT(item), "user-data");
2394 inst->back->special(inst->backhandle, code);
2397 void about_menuitem(GtkMenuItem *item, gpointer data)
2399 struct gui_data *inst = (struct gui_data *)data;
2400 about_box(inst->window);
2403 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2405 struct gui_data *inst = (struct gui_data *)data;
2406 showeventlog(inst->eventlogstuff, inst->window);
2409 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2411 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2412 static const int ww[] = {
2413 6, 7, 8, 9, 10, 11, 12, 13,
2414 14, 15, 16, 17, 18, 19, 20, 21,
2417 struct gui_data *inst = (struct gui_data *)data;
2418 char *title = dupcat(appname, " Reconfiguration", NULL);
2419 Config cfg2, oldcfg;
2422 cfg2 = inst->cfg; /* structure copy */
2424 if (do_config_box(title, &cfg2, 1)) {
2426 oldcfg = inst->cfg; /* structure copy */
2427 inst->cfg = cfg2; /* structure copy */
2429 /* Pass new config data to the logging module */
2430 log_reconfig(inst->logctx, &cfg2);
2432 * Flush the line discipline's edit buffer in the case
2433 * where local editing has just been disabled.
2435 ldisc_send(inst->ldisc, NULL, 0, 0);
2436 /* Pass new config data to the terminal */
2437 term_reconfig(inst->term, &cfg2);
2438 /* Pass new config data to the back end */
2439 inst->back->reconfig(inst->backhandle, &cfg2);
2442 * Just setting inst->cfg is sufficient to cause colour
2443 * setting changes to appear on the next ESC]R palette
2444 * reset. But we should also check whether any colour
2445 * settings have been changed, and revert the ones that
2446 * have to the new default, on the assumption that the user
2447 * is most likely to want an immediate update.
2449 for (i = 0; i < NCOLOURS; i++) {
2450 if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
2451 oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
2452 oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2])
2453 real_palette_set(inst, i, cfg2.colours[ww[i]][0],
2454 cfg2.colours[ww[i]][1],
2455 cfg2.colours[ww[i]][2]);
2459 * If the scrollbar needs to be shown, hidden, or moved
2460 * from one end to the other of the window, do so now.
2462 if (oldcfg.scrollbar != cfg2.scrollbar) {
2464 gtk_widget_show(inst->sbar);
2466 gtk_widget_hide(inst->sbar);
2468 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2469 gtk_box_reorder_child(inst->hbox, inst->sbar,
2470 cfg2.scrollbar_on_left ? 0 : 1);
2474 * Change the window title, if required.
2476 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2477 set_title(inst, cfg2.wintitle);
2480 * Redo the whole tangled fonts and Unicode mess if
2483 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2484 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2485 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2486 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
2487 strcmp(oldcfg.line_codepage, cfg2.line_codepage)) {
2488 setup_fonts_ucs(inst);
2494 * Resize the window.
2496 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2497 oldcfg.window_border != cfg2.window_border || need_size) {
2498 set_geom_hints(inst);
2499 request_resize(inst, cfg2.width, cfg2.height);
2500 //term_size(inst->term, cfg2.height, cfg2.width, cfg2.savelines);
2501 // where TF is our configure event going?!
2504 term_invalidate(inst->term);
2509 void update_specials_menu(void *frontend)
2511 Terminal *term = (Terminal *)frontend;
2512 struct gui_data *inst = (struct gui_data *)term->frontend;
2514 const struct telnet_special *specials;
2516 specials = inst->back->get_specials(inst->backhandle);
2517 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
2518 (GtkCallback)gtk_widget_destroy, NULL);
2521 GtkWidget *menuitem;
2522 for (i = 0; specials[i].name; i++) {
2523 if (*specials[i].name) {
2524 menuitem = gtk_menu_item_new_with_label(specials[i].name);
2525 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2526 (gpointer)specials[i].code);
2527 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2528 GTK_SIGNAL_FUNC(special_menuitem), inst);
2530 menuitem = gtk_menu_item_new();
2531 gtk_container_add(GTK_CONTAINER(inst->specialsmenu), menuitem);
2532 gtk_widget_show(menuitem);
2534 gtk_widget_show(inst->specialsitem1);
2535 gtk_widget_show(inst->specialsitem2);
2537 gtk_widget_hide(inst->specialsitem1);
2538 gtk_widget_hide(inst->specialsitem2);
2542 int pt_main(int argc, char **argv)
2544 extern Backend *select_backend(Config *cfg);
2545 extern int cfgbox(Config *cfg);
2546 struct gui_data *inst;
2548 /* defer any child exit handling until we're ready to deal with
2550 block_signal(SIGCHLD, 1);
2552 gtk_init(&argc, &argv);
2555 * Create an instance structure and initialise to zeroes
2557 inst = snew(struct gui_data);
2558 memset(inst, 0, sizeof(*inst));
2559 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
2561 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
2562 exit(1); /* pre-defaults pass to get -class */
2563 do_defaults(NULL, &inst->cfg);
2564 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
2565 exit(1); /* post-defaults, do everything */
2567 cmdline_run_saved(&inst->cfg);
2569 if (!*inst->cfg.host && !cfgbox(&inst->cfg))
2570 exit(0); /* config box hit Cancel */
2572 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2573 inst->utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
2575 setup_fonts_ucs(inst);
2577 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2580 * Set up the colour map.
2582 palette_reset(inst);
2584 inst->width = inst->cfg.width;
2585 inst->height = inst->cfg.height;
2587 inst->area = gtk_drawing_area_new();
2588 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
2589 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
2590 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
2591 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2592 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2593 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2595 * We always create the scrollbar; it remains invisible if
2596 * unwanted, so we can pop it up quickly if it suddenly becomes
2599 if (inst->cfg.scrollbar_on_left)
2600 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2601 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2602 if (!inst->cfg.scrollbar_on_left)
2603 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2605 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2607 set_geom_hints(inst);
2609 gtk_widget_show(inst->area);
2610 if (inst->cfg.scrollbar)
2611 gtk_widget_show(inst->sbar);
2613 gtk_widget_hide(inst->sbar);
2614 gtk_widget_show(GTK_WIDGET(inst->hbox));
2617 int x = inst->xpos, y = inst->ypos;
2619 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
2620 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
2621 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
2622 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
2623 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
2626 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2627 GTK_SIGNAL_FUNC(destroy), inst);
2628 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2629 GTK_SIGNAL_FUNC(delete_window), inst);
2630 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2631 GTK_SIGNAL_FUNC(key_event), inst);
2632 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2633 GTK_SIGNAL_FUNC(key_event), inst);
2634 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2635 GTK_SIGNAL_FUNC(focus_event), inst);
2636 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2637 GTK_SIGNAL_FUNC(focus_event), inst);
2638 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2639 GTK_SIGNAL_FUNC(configure_area), inst);
2640 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2641 GTK_SIGNAL_FUNC(expose_area), inst);
2642 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2643 GTK_SIGNAL_FUNC(button_event), inst);
2644 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2645 GTK_SIGNAL_FUNC(button_event), inst);
2646 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2647 GTK_SIGNAL_FUNC(motion_event), inst);
2648 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2649 GTK_SIGNAL_FUNC(selection_received), inst);
2650 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2651 GTK_SIGNAL_FUNC(selection_get), inst);
2652 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2653 GTK_SIGNAL_FUNC(selection_clear), inst);
2654 if (inst->cfg.scrollbar)
2655 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2656 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2657 gtk_timeout_add(20, timer_func, inst);
2658 gtk_widget_add_events(GTK_WIDGET(inst->area),
2659 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2660 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2661 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2663 gtk_widget_show(inst->window);
2665 set_window_background(inst);
2668 * Set up the Ctrl+rightclick context menu.
2671 GtkWidget *menuitem;
2673 extern const int use_event_log;
2675 inst->menu = gtk_menu_new();
2677 #define MKMENUITEM(title, func) do { \
2678 menuitem = title ? gtk_menu_item_new_with_label(title) : \
2679 gtk_menu_item_new(); \
2680 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
2681 gtk_widget_show(menuitem); \
2683 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
2684 GTK_SIGNAL_FUNC(func), inst); \
2686 MKMENUITEM("Change Settings", change_settings_menuitem);
2687 MKMENUITEM(NULL, NULL);
2689 MKMENUITEM("Event Log", event_log_menuitem);
2690 MKMENUITEM("Special Commands", NULL);
2691 inst->specialsmenu = gtk_menu_new();
2692 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
2693 inst->specialsitem1 = menuitem;
2694 MKMENUITEM(NULL, NULL);
2695 inst->specialsitem2 = menuitem;
2696 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
2697 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
2698 MKMENUITEM(NULL, NULL);
2699 s = dupcat("About ", appname, NULL);
2700 MKMENUITEM(s, about_menuitem);
2705 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2706 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2707 inst->blankcursor = make_mouse_ptr(inst, -1);
2708 make_mouse_ptr(inst, -2); /* clean up cursor font */
2709 inst->currcursor = inst->textcursor;
2710 show_mouseptr(inst, 1);
2712 inst->eventlogstuff = eventlogstuff_new();
2714 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
2715 inst->logctx = log_init(inst, &inst->cfg);
2716 term_provide_logctx(inst->term, inst->logctx);
2720 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
2722 inst->back = select_backend(&inst->cfg);
2724 char *realhost, *error;
2726 error = inst->back->init((void *)inst->term, &inst->backhandle,
2727 &inst->cfg, inst->cfg.host, inst->cfg.port,
2728 &realhost, inst->cfg.tcp_nodelay);
2731 char *msg = dupprintf("Unable to open connection to %s:\n%s",
2732 inst->cfg.host, error);
2733 inst->exited = TRUE;
2734 fatal_message_box(inst->window, msg);
2739 if (inst->cfg.wintitle[0])
2740 set_title(inst, inst->cfg.wintitle);
2742 char *title = make_default_wintitle(realhost);
2743 set_title(inst, title);
2747 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2748 update_specials_menu(inst->term);
2750 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2753 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle, inst);
2754 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2756 /* now we're reday to deal with the child exit handler being
2758 block_signal(SIGCHLD, 0);
2760 inst->exited = FALSE;