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;
42 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
47 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
48 GdkColor cols[NCOLOURS];
50 wchar_t *pastein_data;
53 char *pasteout_data, *pasteout_data_utf8;
54 int pasteout_data_len, pasteout_data_utf8_len;
55 int font_width, font_height;
58 guint term_paste_idle_id;
59 GdkAtom compound_text_atom, utf8_string_atom;
62 char wintitle[sizeof(((Config *)0)->wintitle)];
63 char icontitle[sizeof(((Config *)0)->wintitle)];
64 int master_fd, master_func_id, exited;
74 struct gui_data *inst;
77 static int send_raw_mouse;
79 static char *app_name = "pterm";
81 char *x_get_default(char *key)
83 return XGetDefault(GDK_DISPLAY(), app_name, key);
87 * Default settings that are specific to pterm.
89 char *platform_default_s(char *name)
91 if (!strcmp(name, "Font"))
92 return "fixed"; /* COE_NORMAL works badly in an xterm */
96 int platform_default_i(char *name, int def)
98 if (!strcmp(name, "CloseOnExit"))
99 return COE_ALWAYS; /* COE_NORMAL works badly in an xterm */
103 void ldisc_update(void *frontend, int echo, int edit)
106 * This is a stub in pterm. If I ever produce a Unix
107 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
108 * then it will require some termios manoeuvring analogous to
109 * that in the Windows plink.c, but here it's meaningless.
113 int askappend(void *frontend, char *filename)
116 * Logging in an xterm-alike is liable to be something you only
117 * do at serious diagnostic need. Hence, I'm going to take the
118 * easy option for now and assume we always want to overwrite
119 * log files. I can always make it properly configurable later.
124 void logevent(void *frontend, char *string)
127 * This is not a very helpful function: events are logged
128 * pretty much exclusively by the back end, and our pty back
129 * end is self-contained. So we need do nothing.
133 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
135 struct gui_data *inst = (struct gui_data *)frontend;
138 return inst->font_height;
140 return inst->font_width;
144 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
145 * into a cooked one (SELECT, EXTEND, PASTE).
147 * In Unix, this is not configurable; the X button arrangement is
148 * rock-solid across all applications, everyone has a three-button
149 * mouse or a means of faking it, and there is no need to switch
150 * buttons around at all.
152 Mouse_Button translate_button(void *frontend, Mouse_Button button)
154 /* struct gui_data *inst = (struct gui_data *)frontend; */
156 if (button == MBT_LEFT)
158 if (button == MBT_MIDDLE)
160 if (button == MBT_RIGHT)
162 return 0; /* shouldn't happen */
166 * Minimise or restore the window in response to a server-side
169 void set_iconic(void *frontend, int iconic)
172 * GTK 1.2 doesn't know how to do this.
174 #if GTK_CHECK_VERSION(2,0,0)
175 struct gui_data *inst = (struct gui_data *)frontend;
177 gtk_window_iconify(GTK_WINDOW(inst->window));
179 gtk_window_deiconify(GTK_WINDOW(inst->window));
184 * Move the window in response to a server-side request.
186 void move_window(void *frontend, int x, int y)
188 struct gui_data *inst = (struct gui_data *)frontend;
190 * I assume that when the GTK version of this call is available
191 * we should use it. Not sure how it differs from the GDK one,
194 #if GTK_CHECK_VERSION(2,0,0)
195 gtk_window_move(GTK_WINDOW(inst->window), x, y);
197 gdk_window_move(inst->window->window, x, y);
202 * Move the window to the top or bottom of the z-order in response
203 * to a server-side request.
205 void set_zorder(void *frontend, int top)
207 struct gui_data *inst = (struct gui_data *)frontend;
209 gdk_window_raise(inst->window->window);
211 gdk_window_lower(inst->window->window);
215 * Refresh the window in response to a server-side request.
217 void refresh_window(void *frontend)
219 struct gui_data *inst = (struct gui_data *)frontend;
220 term_invalidate(inst->term);
224 * Maximise or restore the window in response to a server-side
227 void set_zoomed(void *frontend, int zoomed)
230 * GTK 1.2 doesn't know how to do this.
232 #if GTK_CHECK_VERSION(2,0,0)
233 struct gui_data *inst = (struct gui_data *)frontend;
235 gtk_window_maximize(GTK_WINDOW(inst->window));
237 gtk_window_unmaximize(GTK_WINDOW(inst->window));
242 * Report whether the window is iconic, for terminal reports.
244 int is_iconic(void *frontend)
246 struct gui_data *inst = (struct gui_data *)frontend;
247 return !gdk_window_is_viewable(inst->window->window);
251 * Report the window's position, for terminal reports.
253 void get_window_pos(void *frontend, int *x, int *y)
255 struct gui_data *inst = (struct gui_data *)frontend;
257 * I assume that when the GTK version of this call is available
258 * we should use it. Not sure how it differs from the GDK one,
261 #if GTK_CHECK_VERSION(2,0,0)
262 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
264 gdk_window_get_position(inst->window->window, x, y);
269 * Report the window's pixel size, for terminal reports.
271 void get_window_pixels(void *frontend, int *x, int *y)
273 struct gui_data *inst = (struct gui_data *)frontend;
275 * I assume that when the GTK version of this call is available
276 * we should use it. Not sure how it differs from the GDK one,
279 #if GTK_CHECK_VERSION(2,0,0)
280 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
282 gdk_window_get_size(inst->window->window, x, y);
287 * Return the window or icon title.
289 char *get_window_title(void *frontend, int icon)
291 struct gui_data *inst = (struct gui_data *)frontend;
292 return icon ? inst->wintitle : inst->icontitle;
295 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
298 * We could implement warn-on-close here if we really wanted
304 static void show_mouseptr(struct gui_data *inst, int show)
306 if (!cfg.hide_mouseptr)
309 gdk_window_set_cursor(inst->area->window, inst->currcursor);
311 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
312 inst->mouseptr_visible = show;
315 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
317 struct gui_data *inst = (struct gui_data *)data;
318 int w, h, need_size = 0;
320 w = (event->width - 2*cfg.window_border) / inst->font_width;
321 h = (event->height - 2*cfg.window_border) / inst->font_height;
323 if (w != cfg.width || h != cfg.height) {
325 gdk_pixmap_unref(inst->pixmap);
335 inst->pixmap = gdk_pixmap_new(widget->window,
336 (cfg.width * inst->font_width +
337 2*cfg.window_border),
338 (cfg.height * inst->font_height +
339 2*cfg.window_border), -1);
341 gc = gdk_gc_new(inst->area->window);
342 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
343 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
344 cfg.width * inst->font_width + 2*cfg.window_border,
345 cfg.height * inst->font_height + 2*cfg.window_border);
350 term_size(inst->term, h, w, cfg.savelines);
356 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
358 struct gui_data *inst = (struct gui_data *)data;
361 * Pass the exposed rectangle to terminal.c, which will call us
362 * back to do the actual painting.
365 gdk_draw_pixmap(widget->window,
366 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
368 event->area.x, event->area.y,
369 event->area.x, event->area.y,
370 event->area.width, event->area.height);
375 #define KEY_PRESSED(k) \
376 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
378 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
380 struct gui_data *inst = (struct gui_data *)data;
384 /* By default, nothing is generated. */
388 * If Alt is being released after typing an Alt+numberpad
389 * sequence, we should generate the code that was typed.
391 * Note that we only do this if more than one key was actually
392 * pressed - I don't think Alt+NumPad4 should be ^D or that
393 * Alt+NumPad3 should be ^C, for example. There's no serious
394 * inconvenience in having to type a zero before a single-digit
397 if (event->type == GDK_KEY_RELEASE &&
398 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
399 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
400 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
402 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
404 output[0] = inst->alt_keycode;
409 if (event->type == GDK_KEY_PRESS) {
413 printf("keypress: keyval = %04x, state = %08x; string =",
414 event->keyval, event->state);
415 for (i = 0; event->string[i]; i++)
416 printf(" %02x", (unsigned char) event->string[i]);
422 * NYI: Compose key (!!! requires Unicode faff before even trying)
426 * If Alt has just been pressed, we start potentially
427 * accumulating an Alt+numberpad code. We do this by
428 * setting alt_keycode to -1 (nothing yet but plausible).
430 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
431 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
432 inst->alt_keycode = -1;
433 inst->alt_digits = 0;
434 goto done; /* this generates nothing else */
438 * If we're seeing a numberpad key press with Mod1 down,
439 * consider adding it to alt_keycode if that's sensible.
440 * Anything _else_ with Mod1 down cancels any possibility
441 * of an ALT keycode: we set alt_keycode to -2.
443 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
445 switch (event->keyval) {
446 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
447 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
448 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
449 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
450 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
451 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
452 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
453 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
454 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
455 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
458 inst->alt_keycode = -2; /* it's invalid */
461 printf("Adding digit %d to keycode %d", digit,
464 if (inst->alt_keycode == -1)
465 inst->alt_keycode = digit; /* one-digit code */
467 inst->alt_keycode = inst->alt_keycode * 10 + digit;
470 printf(" gives new code %d\n", inst->alt_keycode);
472 /* Having used this digit, we now do nothing more with it. */
478 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
481 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
482 term_scroll(inst->term, 0, -cfg.height/2);
485 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
486 term_scroll(inst->term, 0, +cfg.height/2);
491 * Neither does Shift-Ins.
493 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
498 /* ALT+things gives leading Escape. */
500 strncpy(output+1, event->string, 31);
502 end = strlen(output);
503 if (event->state & GDK_MOD1_MASK) {
505 if (end == 1) end = 0;
509 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
510 if (!event->string[0] && event->keyval == '`' &&
511 (event->state & GDK_CONTROL_MASK)) {
516 /* Control-Break is the same as Control-C */
517 if (event->keyval == GDK_Break &&
518 (event->state & GDK_CONTROL_MASK)) {
523 /* Control-2, Control-Space and Control-@ are NUL */
524 if (!event->string[0] &&
525 (event->keyval == ' ' || event->keyval == '2' ||
526 event->keyval == '@') &&
527 (event->state & (GDK_SHIFT_MASK |
528 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
533 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
534 if (!event->string[0] && event->keyval == ' ' &&
535 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
536 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
541 /* We don't let GTK tell us what Backspace is! We know better. */
542 if (event->keyval == GDK_BackSpace &&
543 !(event->state & GDK_SHIFT_MASK)) {
544 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
547 /* For Shift Backspace, do opposite of what is configured. */
548 if (event->keyval == GDK_BackSpace &&
549 (event->state & GDK_SHIFT_MASK)) {
550 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
554 /* Shift-Tab is ESC [ Z */
555 if (event->keyval == GDK_ISO_Left_Tab ||
556 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
557 end = 1 + sprintf(output+1, "\033[Z");
561 * NetHack keypad mode.
563 if (cfg.nethack_keypad) {
565 switch (event->keyval) {
566 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
567 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
568 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
569 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
570 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
571 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
572 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
573 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
574 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
578 if (event->state & GDK_SHIFT_MASK)
587 * Application keypad mode.
589 if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
591 switch (event->keyval) {
592 case GDK_Num_Lock: xkey = 'P'; break;
593 case GDK_KP_Divide: xkey = 'Q'; break;
594 case GDK_KP_Multiply: xkey = 'R'; break;
595 case GDK_KP_Subtract: xkey = 'S'; break;
597 * Keypad + is tricky. It covers a space that would
598 * be taken up on the VT100 by _two_ keys; so we
599 * let Shift select between the two. Worse still,
600 * in xterm function key mode we change which two...
603 if (cfg.funky_type == 2) {
604 if (event->state & GDK_SHIFT_MASK)
608 } else if (event->state & GDK_SHIFT_MASK)
613 case GDK_KP_Enter: xkey = 'M'; break;
614 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
615 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
616 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
617 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
618 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
619 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
620 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
621 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
622 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
623 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
624 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
627 if (inst->term->vt52_mode) {
628 if (xkey >= 'P' && xkey <= 'S')
629 end = 1 + sprintf(output+1, "\033%c", xkey);
631 end = 1 + sprintf(output+1, "\033?%c", xkey);
633 end = 1 + sprintf(output+1, "\033O%c", xkey);
639 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
640 * for integer decimal nn.)
642 * We also deal with the weird ones here. Linux VCs replace F1
643 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
644 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
649 switch (event->keyval) {
651 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
654 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
657 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
660 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
663 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
666 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
669 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
672 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
675 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
678 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
711 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
712 case GDK_Home: case GDK_KP_Home:
715 case GDK_Insert: case GDK_KP_Insert:
718 case GDK_Delete: case GDK_KP_Delete:
721 case GDK_End: case GDK_KP_End:
724 case GDK_Page_Up: case GDK_KP_Page_Up:
727 case GDK_Page_Down: case GDK_KP_Page_Down:
731 /* Reorder edit keys to physical order */
732 if (cfg.funky_type == 3 && code <= 6)
733 code = "\0\2\1\4\5\3\6"[code];
735 if (inst->term->vt52_mode && code > 0 && code <= 6) {
736 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
740 if (cfg.funky_type == 5 && /* SCO function keys */
741 code >= 11 && code <= 34) {
742 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
744 switch (event->keyval) {
745 case GDK_F1: index = 0; break;
746 case GDK_F2: index = 1; break;
747 case GDK_F3: index = 2; break;
748 case GDK_F4: index = 3; break;
749 case GDK_F5: index = 4; break;
750 case GDK_F6: index = 5; break;
751 case GDK_F7: index = 6; break;
752 case GDK_F8: index = 7; break;
753 case GDK_F9: index = 8; break;
754 case GDK_F10: index = 9; break;
755 case GDK_F11: index = 10; break;
756 case GDK_F12: index = 11; break;
758 if (event->state & GDK_SHIFT_MASK) index += 12;
759 if (event->state & GDK_CONTROL_MASK) index += 24;
760 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
763 if (cfg.funky_type == 5 && /* SCO small keypad */
764 code >= 1 && code <= 6) {
765 char codes[] = "HL.FIG";
770 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
774 if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
775 code >= 11 && code <= 24) {
781 if (inst->term->vt52_mode)
782 end = 1 + sprintf(output+1,
783 "\x1B%c", code + 'P' - 11 - offt);
785 end = 1 + sprintf(output+1,
786 "\x1BO%c", code + 'P' - 11 - offt);
789 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
790 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
793 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
794 if (inst->term->vt52_mode)
795 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
797 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
800 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
801 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
805 end = 1 + sprintf(output+1, "\x1B[%d~", code);
811 * Cursor keys. (This includes the numberpad cursor keys,
812 * if we haven't already done them due to app keypad mode.)
814 * Here we also process un-numlocked un-appkeypadded KP5,
815 * which sends ESC [ G.
819 switch (event->keyval) {
820 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
821 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
822 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
823 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
824 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
828 * The arrow keys normally do ESC [ A and so on. In
829 * app cursor keys mode they do ESC O A instead.
830 * Ctrl toggles the two modes.
832 if (inst->term->vt52_mode) {
833 end = 1 + sprintf(output+1, "\033%c", xkey);
834 } else if (!inst->term->app_cursor_keys ^
835 !(event->state & GDK_CONTROL_MASK)) {
836 end = 1 + sprintf(output+1, "\033O%c", xkey);
838 end = 1 + sprintf(output+1, "\033[%c", xkey);
851 printf("generating sequence:");
852 for (i = start; i < end; i++)
853 printf(" %02x", (unsigned char) output[i]);
857 if (!inst->direct_to_font) {
859 * The stuff we've just generated is assumed to be
860 * ISO-8859-1! This sounds insane, but `man
861 * XLookupString' agrees: strings of this type returned
862 * from the X server are hardcoded to 8859-1. Strictly
863 * speaking we should be doing this using some sort of
864 * GtkIMContext, which (if we're lucky) would give us
865 * our data directly in Unicode; but that's not
866 * supported in GTK 1.2 as far as I can tell, and it's
867 * poorly documented even in 2.0, so it'll have to
870 lpage_send(inst->ldisc, CS_ISO8859_1, output+start, end-start, 1);
873 * In direct-to-font mode, we just send the string
874 * exactly as we received it.
876 ldisc_send(inst->ldisc, output+start, end-start, 1);
879 show_mouseptr(inst, 0);
880 term_seen_key_event(inst->term);
881 term_out(inst->term);
887 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
889 struct gui_data *inst = (struct gui_data *)data;
890 int shift, ctrl, alt, x, y, button, act;
892 show_mouseptr(inst, 1);
894 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
895 term_scroll(inst->term, 0, -5);
898 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
899 term_scroll(inst->term, 0, +5);
903 shift = event->state & GDK_SHIFT_MASK;
904 ctrl = event->state & GDK_CONTROL_MASK;
905 alt = event->state & GDK_MOD1_MASK;
906 if (event->button == 1)
908 else if (event->button == 2)
910 else if (event->button == 3)
913 return FALSE; /* don't even know what button! */
915 switch (event->type) {
916 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
917 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
918 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
919 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
920 default: return FALSE; /* don't know this event type */
923 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
924 act != MA_CLICK && act != MA_RELEASE)
925 return TRUE; /* we ignore these in raw mouse mode */
927 x = (event->x - cfg.window_border) / inst->font_width;
928 y = (event->y - cfg.window_border) / inst->font_height;
930 term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
935 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
937 struct gui_data *inst = (struct gui_data *)data;
938 int shift, ctrl, alt, x, y, button;
940 show_mouseptr(inst, 1);
942 shift = event->state & GDK_SHIFT_MASK;
943 ctrl = event->state & GDK_CONTROL_MASK;
944 alt = event->state & GDK_MOD1_MASK;
945 if (event->state & GDK_BUTTON1_MASK)
947 else if (event->state & GDK_BUTTON2_MASK)
949 else if (event->state & GDK_BUTTON3_MASK)
952 return FALSE; /* don't even know what button! */
954 x = (event->x - cfg.window_border) / inst->font_width;
955 y = (event->y - cfg.window_border) / inst->font_height;
957 term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
962 void done_with_pty(struct gui_data *inst)
964 extern void pty_close(void);
966 if (inst->master_fd >= 0) {
968 inst->master_fd = -1;
969 gtk_input_remove(inst->master_func_id);
972 if (!inst->exited && inst->back->exitcode(inst->backhandle) >= 0) {
973 int exitcode = inst->back->exitcode(inst->backhandle);
976 clean = WIFEXITED(exitcode) && (WEXITSTATUS(exitcode) == 0);
979 * Terminate now, if the Close On Exit setting is
982 if (cfg.close_on_exit == COE_ALWAYS ||
983 (cfg.close_on_exit == COE_NORMAL && clean))
987 * Otherwise, output an indication that the session has
992 if (WIFEXITED(exitcode))
993 sprintf(message, "\r\n[pterm: process terminated with exit"
994 " code %d]\r\n", WEXITSTATUS(exitcode));
995 else if (WIFSIGNALED(exitcode))
996 #ifdef HAVE_NO_STRSIGNAL
997 sprintf(message, "\r\n[pterm: process terminated on signal"
998 " %d]\r\n", WTERMSIG(exitcode));
1000 sprintf(message, "\r\n[pterm: process terminated on signal"
1001 " %d (%.400s)]\r\n", WTERMSIG(exitcode),
1002 strsignal(WTERMSIG(exitcode)));
1004 from_backend((void *)inst->term, 0, message, strlen(message));
1010 void frontend_keypress(void *handle)
1012 struct gui_data *inst = (struct gui_data *)handle;
1015 * If our child process has exited but not closed, terminate on
1022 gint timer_func(gpointer data)
1024 struct gui_data *inst = (struct gui_data *)data;
1026 if (inst->back->exitcode(inst->backhandle) >= 0) {
1028 * The primary child process died. We could keep the
1029 * terminal open for remaining subprocesses to output to,
1030 * but conventional wisdom seems to feel that that's the
1031 * Wrong Thing for an xterm-alike, so we bail out now
1032 * (though we don't necessarily _close_ the window,
1033 * depending on the state of Close On Exit). This would be
1034 * easy enough to change or make configurable if necessary.
1036 done_with_pty(inst);
1039 term_update(inst->term);
1040 term_blink(inst->term, 0);
1044 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1046 struct gui_data *inst = (struct gui_data *)data;
1050 ret = read(sourcefd, buf, sizeof(buf));
1053 * Clean termination condition is that either ret == 0, or ret
1054 * < 0 and errno == EIO. Not sure why the latter, but it seems
1057 if (ret == 0 || (ret < 0 && errno == EIO)) {
1058 done_with_pty(inst);
1059 } else if (ret < 0) {
1060 perror("read pty master");
1063 from_backend(inst->term, 0, buf, ret);
1064 term_blink(inst->term, 1);
1065 term_out(inst->term);
1068 void destroy(GtkWidget *widget, gpointer data)
1073 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1075 struct gui_data *inst = (struct gui_data *)data;
1076 inst->term->has_focus = event->in;
1077 term_out(inst->term);
1078 term_update(inst->term);
1079 show_mouseptr(inst, 1);
1084 * set or clear the "raw mouse message" mode
1086 void set_raw_mouse_mode(void *frontend, int activate)
1088 struct gui_data *inst = (struct gui_data *)frontend;
1089 activate = activate && !cfg.no_mouse_rep;
1090 send_raw_mouse = activate;
1092 inst->currcursor = inst->rawcursor;
1094 inst->currcursor = inst->textcursor;
1095 show_mouseptr(inst, inst->mouseptr_visible);
1098 void request_resize(void *frontend, int w, int h)
1100 struct gui_data *inst = (struct gui_data *)frontend;
1101 int large_x, large_y;
1102 int offset_x, offset_y;
1104 GtkRequisition inner, outer;
1107 * This is a heinous hack dreamed up by the gnome-terminal
1108 * people to get around a limitation in gtk. The problem is
1109 * that in order to set the size correctly we really need to be
1110 * calling gtk_window_resize - but that needs to know the size
1111 * of the _whole window_, not the drawing area. So what we do
1112 * is to set an artificially huge size request on the drawing
1113 * area, recompute the resulting size request on the window,
1114 * and look at the difference between the two. That gives us
1115 * the x and y offsets we need to translate drawing area size
1116 * into window size for real, and then we call
1117 * gtk_window_resize.
1121 * We start by retrieving the current size of the whole window.
1122 * Adding a bit to _that_ will give us a value we can use as a
1123 * bogus size request which guarantees to be bigger than the
1124 * current size of the drawing area.
1126 get_window_pixels(inst, &large_x, &large_y);
1130 #if GTK_CHECK_VERSION(2,0,0)
1131 gtk_widget_set_size_request(inst->area, large_x, large_y);
1133 gtk_widget_set_usize(inst->area, large_x, large_y);
1135 gtk_widget_size_request(inst->area, &inner);
1136 gtk_widget_size_request(inst->window, &outer);
1138 offset_x = outer.width - inner.width;
1139 offset_y = outer.height - inner.height;
1141 area_x = inst->font_width * w + 2*cfg.window_border;
1142 area_y = inst->font_height * h + 2*cfg.window_border;
1145 * Now we must set the size request on the drawing area back to
1146 * something sensible before we commit the real resize. Best
1147 * way to do this, I think, is to set it to what the size is
1148 * really going to end up being.
1150 #if GTK_CHECK_VERSION(2,0,0)
1151 gtk_widget_set_size_request(inst->area, area_x, area_y);
1153 gtk_widget_set_usize(inst->area, area_x, area_y);
1156 #if GTK_CHECK_VERSION(2,0,0)
1157 gtk_window_resize(GTK_WINDOW(inst->window),
1158 area_x + offset_x, area_y + offset_y);
1160 gdk_window_resize(inst->window->window,
1161 area_x + offset_x, area_y + offset_y);
1165 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1167 gboolean success[1];
1169 inst->cols[n].red = r * 0x0101;
1170 inst->cols[n].green = g * 0x0101;
1171 inst->cols[n].blue = b * 0x0101;
1173 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1174 FALSE, FALSE, success);
1176 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1180 void set_window_background(struct gui_data *inst)
1182 if (inst->area && inst->area->window)
1183 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1184 if (inst->window && inst->window->window)
1185 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1188 void palette_set(void *frontend, int n, int r, int g, int b)
1190 struct gui_data *inst = (struct gui_data *)frontend;
1191 static const int first[21] = {
1192 0, 2, 4, 6, 8, 10, 12, 14,
1193 1, 3, 5, 7, 9, 11, 13, 15,
1196 real_palette_set(inst, first[n], r, g, b);
1198 real_palette_set(inst, first[n] + 1, r, g, b);
1200 set_window_background(inst);
1203 void palette_reset(void *frontend)
1205 struct gui_data *inst = (struct gui_data *)frontend;
1206 /* This maps colour indices in cfg to those used in inst->cols. */
1207 static const int ww[] = {
1208 6, 7, 8, 9, 10, 11, 12, 13,
1209 14, 15, 16, 17, 18, 19, 20, 21,
1212 gboolean success[NCOLOURS];
1215 assert(lenof(ww) == NCOLOURS);
1217 if (!inst->colmap) {
1218 inst->colmap = gdk_colormap_get_system();
1220 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1223 for (i = 0; i < NCOLOURS; i++) {
1224 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1225 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1226 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1229 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1230 FALSE, FALSE, success);
1231 for (i = 0; i < NCOLOURS; i++) {
1233 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1234 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1237 set_window_background(inst);
1240 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1242 struct gui_data *inst = (struct gui_data *)frontend;
1243 if (inst->pasteout_data)
1244 sfree(inst->pasteout_data);
1245 if (inst->pasteout_data_utf8)
1246 sfree(inst->pasteout_data_utf8);
1249 * Set up UTF-8 paste data. This only happens if we aren't in
1250 * direct-to-font mode using the D800 hack.
1252 if (!inst->direct_to_font) {
1253 wchar_t *tmp = data;
1256 inst->pasteout_data_utf8 = smalloc(len*6);
1257 inst->pasteout_data_utf8_len = len*6;
1258 inst->pasteout_data_utf8_len =
1259 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1260 inst->pasteout_data_utf8_len,
1261 CS_UTF8, NULL, NULL, 0);
1262 if (inst->pasteout_data_utf8_len == 0) {
1263 sfree(inst->pasteout_data_utf8);
1264 inst->pasteout_data_utf8 = NULL;
1266 inst->pasteout_data_utf8 =
1267 srealloc(inst->pasteout_data_utf8,
1268 inst->pasteout_data_utf8_len);
1271 inst->pasteout_data_utf8 = NULL;
1272 inst->pasteout_data_utf8_len = 0;
1275 inst->pasteout_data = smalloc(len*6);
1276 inst->pasteout_data_len = len*6;
1277 inst->pasteout_data_len = wc_to_mb(line_codepage, 0, data, len,
1278 inst->pasteout_data,
1279 inst->pasteout_data_len, NULL, NULL);
1280 if (inst->pasteout_data_len == 0) {
1281 sfree(inst->pasteout_data);
1282 inst->pasteout_data = NULL;
1284 inst->pasteout_data =
1285 srealloc(inst->pasteout_data, inst->pasteout_data_len);
1288 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1289 GDK_CURRENT_TIME)) {
1290 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1291 GDK_SELECTION_TYPE_STRING, 1);
1292 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1293 inst->compound_text_atom, 1);
1294 if (inst->pasteout_data_utf8)
1295 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1296 inst->utf8_string_atom, 1);
1300 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1301 guint info, guint time_stamp, gpointer data)
1303 struct gui_data *inst = (struct gui_data *)data;
1304 if (seldata->target == inst->utf8_string_atom)
1305 gtk_selection_data_set(seldata, seldata->target, 8,
1306 inst->pasteout_data_utf8,
1307 inst->pasteout_data_utf8_len);
1309 gtk_selection_data_set(seldata, seldata->target, 8,
1310 inst->pasteout_data, inst->pasteout_data_len);
1313 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1316 struct gui_data *inst = (struct gui_data *)data;
1317 term_deselect(inst->term);
1318 if (inst->pasteout_data)
1319 sfree(inst->pasteout_data);
1320 if (inst->pasteout_data_utf8)
1321 sfree(inst->pasteout_data_utf8);
1322 inst->pasteout_data = NULL;
1323 inst->pasteout_data_len = 0;
1324 inst->pasteout_data_utf8 = NULL;
1325 inst->pasteout_data_utf8_len = 0;
1329 void request_paste(void *frontend)
1331 struct gui_data *inst = (struct gui_data *)frontend;
1333 * In Unix, pasting is asynchronous: all we can do at the
1334 * moment is to call gtk_selection_convert(), and when the data
1335 * comes back _then_ we can call term_do_paste().
1338 if (!inst->direct_to_font) {
1340 * First we attempt to retrieve the selection as a UTF-8
1341 * string (which we will convert to the correct code page
1342 * before sending to the session, of course). If that
1343 * fails, selection_received() will be informed and will
1344 * fall back to an ordinary string.
1346 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1347 inst->utf8_string_atom, GDK_CURRENT_TIME);
1350 * If we're in direct-to-font mode, we disable UTF-8
1351 * pasting, and go straight to ordinary string data.
1353 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1354 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1358 gint idle_paste_func(gpointer data); /* forward ref */
1360 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1361 guint time, gpointer data)
1363 struct gui_data *inst = (struct gui_data *)data;
1365 if (seldata->target == inst->utf8_string_atom && seldata->length <= 0) {
1367 * Failed to get a UTF-8 selection string. Try an ordinary
1370 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1371 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1376 * Any other failure should just go foom.
1378 if (seldata->length <= 0 ||
1379 (seldata->type != GDK_SELECTION_TYPE_STRING &&
1380 seldata->type != inst->utf8_string_atom))
1381 return; /* Nothing happens. */
1383 if (inst->pastein_data)
1384 sfree(inst->pastein_data);
1386 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1387 inst->pastein_data_len = seldata->length;
1388 inst->pastein_data_len =
1389 mb_to_wc((seldata->type == inst->utf8_string_atom ?
1390 CS_UTF8 : line_codepage),
1391 0, seldata->data, seldata->length,
1392 inst->pastein_data, inst->pastein_data_len);
1394 term_do_paste(inst->term);
1396 if (term_paste_pending(inst->term))
1397 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1400 gint idle_paste_func(gpointer data)
1402 struct gui_data *inst = (struct gui_data *)data;
1404 if (term_paste_pending(inst->term))
1405 term_paste(inst->term);
1407 gtk_idle_remove(inst->term_paste_idle_id);
1413 void get_clip(void *frontend, wchar_t ** p, int *len)
1415 struct gui_data *inst = (struct gui_data *)frontend;
1418 *p = inst->pastein_data;
1419 *len = inst->pastein_data_len;
1423 void set_title(void *frontend, char *title)
1425 struct gui_data *inst = (struct gui_data *)frontend;
1426 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1427 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1428 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1431 void set_icon(void *frontend, char *title)
1433 struct gui_data *inst = (struct gui_data *)frontend;
1434 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1435 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1436 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1439 void set_sbar(void *frontend, int total, int start, int page)
1441 struct gui_data *inst = (struct gui_data *)frontend;
1444 inst->sbar_adjust->lower = 0;
1445 inst->sbar_adjust->upper = total;
1446 inst->sbar_adjust->value = start;
1447 inst->sbar_adjust->page_size = page;
1448 inst->sbar_adjust->step_increment = 1;
1449 inst->sbar_adjust->page_increment = page/2;
1450 inst->ignore_sbar = TRUE;
1451 gtk_adjustment_changed(inst->sbar_adjust);
1452 inst->ignore_sbar = FALSE;
1455 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1457 struct gui_data *inst = (struct gui_data *)data;
1461 if (!inst->ignore_sbar)
1462 term_scroll(inst->term, 1, (int)adj->value);
1465 void sys_cursor(void *frontend, int x, int y)
1468 * This is meaningless under X.
1473 * This is still called when mode==BELL_VISUAL, even though the
1474 * visual bell is handled entirely within terminal.c, because we
1475 * may want to perform additional actions on any kind of bell (for
1476 * example, taskbar flashing in Windows).
1478 void beep(void *frontend, int mode)
1480 if (mode != BELL_VISUAL)
1484 int char_width(Context ctx, int uc)
1487 * Under X, any fixed-width font really _is_ fixed-width.
1488 * Double-width characters will be dealt with using a separate
1489 * font. For the moment we can simply return 1.
1494 Context get_ctx(void *frontend)
1496 struct gui_data *inst = (struct gui_data *)frontend;
1497 struct draw_ctx *dctx;
1499 if (!inst->area->window)
1502 dctx = smalloc(sizeof(*dctx));
1504 dctx->gc = gdk_gc_new(inst->area->window);
1508 void free_ctx(Context ctx)
1510 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1511 /* struct gui_data *inst = dctx->inst; */
1512 GdkGC *gc = dctx->gc;
1518 * Draw a line of text in the window, at given character
1519 * coordinates, in given attributes.
1521 * We are allowed to fiddle with the contents of `text'.
1523 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1524 unsigned long attr, int lattr)
1526 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1527 struct gui_data *inst = dctx->inst;
1528 GdkGC *gc = dctx->gc;
1530 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1532 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1533 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1534 if (attr & ATTR_REVERSE) {
1539 if (cfg.bold_colour && (attr & ATTR_BOLD))
1541 if (cfg.bold_colour && (attr & ATTR_BLINK))
1543 if (attr & TATTR_ACTCURS) {
1548 fontid = shadow = 0;
1550 if (attr & ATTR_WIDE) {
1557 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1558 if (inst->fonts[fontid | 1])
1564 if (lattr != LATTR_NORM) {
1566 if (x >= inst->term->cols)
1568 if (x + len*2*widefactor > inst->term->cols)
1569 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1577 r.x = x*inst->font_width+cfg.window_border;
1578 r.y = y*inst->font_height+cfg.window_border;
1579 r.width = rlen*widefactor*inst->font_width;
1580 r.height = inst->font_height;
1581 gdk_gc_set_clip_rectangle(gc, &r);
1584 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1585 gdk_draw_rectangle(inst->pixmap, gc, 1,
1586 x*inst->font_width+cfg.window_border,
1587 y*inst->font_height+cfg.window_border,
1588 rlen*widefactor*inst->font_width, inst->font_height);
1590 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1597 wcs = smalloc(sizeof(wchar_t) * (len+1));
1598 for (i = 0; i < len; i++) {
1599 wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
1602 if (inst->fonts[fontid] == NULL) {
1604 * The font for this contingency does not exist.
1605 * Typically this means we've been given ATTR_WIDE
1606 * character and have no wide font. So we display
1607 * nothing at all; such is life.
1609 } else if (inst->fontinfo[fontid].is_wide) {
1611 * At least one version of gdk_draw_text_wc() has a
1612 * weird bug whereby it reads `len' elements of the
1613 * input string, but only draws `len/2'. Hence I'm
1614 * going to make its input array twice as long as it
1615 * theoretically needs to be, and pass in twice the
1616 * actual number of characters. If a fixed gdk actually
1617 * takes the doubled length seriously, then (a) the
1618 * array will stand scrutiny up to the full length, (b)
1619 * the spare elements of the array are full of zeroes
1620 * which will probably be an empty glyph in the font,
1621 * and (c) the clip rectangle should prevent it causing
1624 gwcs = smalloc(sizeof(GdkWChar) * (len*2+1));
1625 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
1627 * FIXME: when we have a wide-char equivalent of
1628 * from_unicode, use it instead of this.
1630 for (i = 0; i <= len; i++)
1632 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
1633 x*inst->font_width+cfg.window_border,
1634 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1638 gcs = smalloc(sizeof(GdkWChar) * (len+1));
1639 wc_to_mb(inst->fontinfo[fontid].charset, 0,
1640 wcs, len, gcs, len, ".", NULL);
1641 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1642 x*inst->font_width+cfg.window_border,
1643 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1651 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1652 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1653 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1657 if (attr & ATTR_UNDER) {
1658 int uheight = inst->fonts[0]->ascent + 1;
1659 if (uheight >= inst->font_height)
1660 uheight = inst->font_height - 1;
1661 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1662 y*inst->font_height + uheight + cfg.window_border,
1663 (x+len)*widefactor*inst->font_width-1+cfg.window_border,
1664 y*inst->font_height + uheight + cfg.window_border);
1667 if (lattr != LATTR_NORM) {
1669 * I can't find any plausible StretchBlt equivalent in the
1670 * X server, so I'm going to do this the slow and painful
1671 * way. This will involve repeated calls to
1672 * gdk_draw_pixmap() to stretch the text horizontally. It's
1673 * O(N^2) in time and O(N) in network bandwidth, but you
1674 * try thinking of a better way. :-(
1677 for (i = 0; i < len * widefactor * inst->font_width; i++) {
1678 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1679 x*inst->font_width+cfg.window_border + 2*i,
1680 y*inst->font_height+cfg.window_border,
1681 x*inst->font_width+cfg.window_border + 2*i+1,
1682 y*inst->font_height+cfg.window_border,
1683 len * inst->font_width - i, inst->font_height);
1686 if (lattr != LATTR_WIDE) {
1688 /* Now stretch vertically, in the same way. */
1689 if (lattr == LATTR_BOT)
1693 for (i = 0; i < inst->font_height; i+=2) {
1694 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1695 x*inst->font_width+cfg.window_border,
1696 y*inst->font_height+cfg.window_border+dt*i+db,
1697 x*widefactor*inst->font_width+cfg.window_border,
1698 y*inst->font_height+cfg.window_border+dt*(i+1),
1699 len * inst->font_width, inst->font_height-i-1);
1705 void do_text(Context ctx, int x, int y, char *text, int len,
1706 unsigned long attr, int lattr)
1708 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1709 struct gui_data *inst = dctx->inst;
1710 GdkGC *gc = dctx->gc;
1713 do_text_internal(ctx, x, y, text, len, attr, lattr);
1715 if (attr & ATTR_WIDE) {
1721 if (lattr != LATTR_NORM) {
1723 if (x >= inst->term->cols)
1725 if (x + len*2*widefactor > inst->term->cols)
1726 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1730 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1731 x*inst->font_width+cfg.window_border,
1732 y*inst->font_height+cfg.window_border,
1733 x*inst->font_width+cfg.window_border,
1734 y*inst->font_height+cfg.window_border,
1735 len*widefactor*inst->font_width, inst->font_height);
1738 void do_cursor(Context ctx, int x, int y, char *text, int len,
1739 unsigned long attr, int lattr)
1741 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1742 struct gui_data *inst = dctx->inst;
1743 GdkGC *gc = dctx->gc;
1745 int passive, widefactor;
1747 if (attr & TATTR_PASCURS) {
1748 attr &= ~TATTR_PASCURS;
1752 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1753 attr &= ~TATTR_ACTCURS;
1755 do_text_internal(ctx, x, y, text, len, attr, lattr);
1757 if (attr & ATTR_WIDE) {
1763 if (lattr != LATTR_NORM) {
1765 if (x >= inst->term->cols)
1767 if (x + len*2*widefactor > inst->term->cols)
1768 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1772 if (cfg.cursor_type == 0) {
1774 * An active block cursor will already have been done by
1775 * the above do_text call, so we only need to do anything
1779 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1780 gdk_draw_rectangle(inst->pixmap, gc, 0,
1781 x*inst->font_width+cfg.window_border,
1782 y*inst->font_height+cfg.window_border,
1783 len*inst->font_width-1, inst->font_height-1);
1787 int startx, starty, dx, dy, length, i;
1791 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1792 char_width = 2*inst->font_width;
1794 char_width = inst->font_width;
1796 if (cfg.cursor_type == 1) {
1797 uheight = inst->fonts[0]->ascent + 1;
1798 if (uheight >= inst->font_height)
1799 uheight = inst->font_height - 1;
1801 startx = x * inst->font_width + cfg.window_border;
1802 starty = y * inst->font_height + cfg.window_border + uheight;
1805 length = len * char_width;
1808 if (attr & TATTR_RIGHTCURS)
1809 xadjust = char_width - 1;
1810 startx = x * inst->font_width + cfg.window_border + xadjust;
1811 starty = y * inst->font_height + cfg.window_border;
1814 length = inst->font_height;
1817 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1819 for (i = 0; i < length; i++) {
1821 gdk_draw_point(inst->pixmap, gc, startx, starty);
1827 gdk_draw_line(inst->pixmap, gc, startx, starty,
1828 startx + (length-1) * dx, starty + (length-1) * dy);
1832 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1833 x*inst->font_width+cfg.window_border,
1834 y*inst->font_height+cfg.window_border,
1835 x*inst->font_width+cfg.window_border,
1836 y*inst->font_height+cfg.window_border,
1837 len*widefactor*inst->font_width, inst->font_height);
1840 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1843 * Truly hideous hack: GTK doesn't allow us to set the mouse
1844 * cursor foreground and background colours unless we've _also_
1845 * created our own cursor from bitmaps. Therefore, I need to
1846 * load the `cursor' font and draw glyphs from it on to
1847 * pixmaps, in order to construct my cursors with the fg and bg
1848 * I want. This is a gross hack, but it's more self-contained
1849 * than linking in Xlib to find the X window handle to
1850 * inst->area and calling XRecolorCursor, and it's more
1851 * futureproof than hard-coding the shapes as bitmap arrays.
1853 static GdkFont *cursor_font = NULL;
1854 GdkPixmap *source, *mask;
1856 GdkColor cfg = { 0, 65535, 65535, 65535 };
1857 GdkColor cbg = { 0, 0, 0, 0 };
1858 GdkColor dfg = { 1, 65535, 65535, 65535 };
1859 GdkColor dbg = { 0, 0, 0, 0 };
1862 gint lb, rb, wid, asc, desc, w, h, x, y;
1864 if (cursor_val == -2) {
1865 gdk_font_unref(cursor_font);
1869 if (cursor_val >= 0 && !cursor_font)
1870 cursor_font = gdk_font_load("cursor");
1873 * Get the text extent of the cursor in question. We use the
1874 * mask character for this, because it's typically slightly
1875 * bigger than the main character.
1877 if (cursor_val >= 0) {
1879 text[0] = (char)cursor_val + 1;
1880 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1881 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1887 source = gdk_pixmap_new(NULL, w, h, 1);
1888 mask = gdk_pixmap_new(NULL, w, h, 1);
1891 * Draw the mask character on the mask pixmap.
1893 gc = gdk_gc_new(mask);
1894 gdk_gc_set_foreground(gc, &dbg);
1895 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1896 if (cursor_val >= 0) {
1898 text[0] = (char)cursor_val + 1;
1899 gdk_gc_set_foreground(gc, &dfg);
1900 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1905 * Draw the main character on the source pixmap.
1907 gc = gdk_gc_new(source);
1908 gdk_gc_set_foreground(gc, &dbg);
1909 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1910 if (cursor_val >= 0) {
1912 text[0] = (char)cursor_val;
1913 gdk_gc_set_foreground(gc, &dfg);
1914 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1919 * Create the cursor.
1921 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1926 gdk_pixmap_unref(source);
1927 gdk_pixmap_unref(mask);
1932 void modalfatalbox(char *p, ...)
1935 fprintf(stderr, "FATAL ERROR: ");
1937 vfprintf(stderr, p, ap);
1939 fputc('\n', stderr);
1943 char *get_x_display(void *frontend)
1945 return gdk_get_display();
1948 static void help(FILE *fp) {
1950 "pterm option summary:\n"
1952 " --display DISPLAY Specify X display to use (note '--')\n"
1953 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1954 " -fn FONT Normal text font\n"
1955 " -fb FONT Bold text font\n"
1956 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1957 " -sl LINES Number of lines of scrollback\n"
1958 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1959 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1960 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1961 " -T TITLE Window title\n"
1962 " -ut, +ut Do(default) or do not update utmp\n"
1963 " -ls, +ls Do(default) or do not make shell a login shell\n"
1964 " -sb, +sb Do(default) or do not display a scrollbar\n"
1965 " -log PATH Log all output to a file\n"
1966 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1967 " -xrm RESOURCE-STRING Set an X resource\n"
1968 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1969 ) < 0 || fflush(fp) < 0) {
1970 perror("output error");
1975 int do_cmdline(int argc, char **argv, int do_everything)
1978 extern char **pty_argv; /* declared in pty.c */
1981 * Macros to make argument handling easier. Note that because
1982 * they need to call `continue', they cannot be contained in
1983 * the usual do {...} while (0) wrapper to make them
1984 * syntactically single statements; hence it is not legal to
1985 * use one of these macros as an unbraced statement between
1988 #define EXPECTS_ARG { \
1989 if (--argc <= 0) { \
1991 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1996 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2005 while (--argc > 0) {
2007 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2010 strncpy(cfg.font, val, sizeof(cfg.font));
2011 cfg.font[sizeof(cfg.font)-1] = '\0';
2013 } else if (!strcmp(p, "-fb")) {
2016 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
2017 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
2019 } else if (!strcmp(p, "-fw")) {
2022 strncpy(cfg.widefont, val, sizeof(cfg.widefont));
2023 cfg.widefont[sizeof(cfg.widefont)-1] = '\0';
2025 } else if (!strcmp(p, "-fwb")) {
2028 strncpy(cfg.wideboldfont, val, sizeof(cfg.wideboldfont));
2029 cfg.wideboldfont[sizeof(cfg.wideboldfont)-1] = '\0';
2031 } else if (!strcmp(p, "-cs")) {
2034 strncpy(cfg.line_codepage, val, sizeof(cfg.line_codepage));
2035 cfg.line_codepage[sizeof(cfg.line_codepage)-1] = '\0';
2037 } else if (!strcmp(p, "-geometry")) {
2038 int flags, x, y, w, h;
2042 flags = XParseGeometry(val, &x, &y, &w, &h);
2043 if (flags & WidthValue)
2045 if (flags & HeightValue)
2049 * Apparently setting the initial window position is
2050 * difficult in GTK 1.2. Not entirely sure why this
2051 * should be. 2.0 has gtk_window_parse_geometry(),
2052 * which would help... For the moment, though, I can't
2053 * be bothered with this.
2056 } else if (!strcmp(p, "-sl")) {
2059 cfg.savelines = atoi(val);
2061 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2062 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2063 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2068 if (!gdk_color_parse(val, &col)) {
2070 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
2073 index = (!strcmp(p, "-fg") ? 0 :
2074 !strcmp(p, "-bg") ? 2 :
2075 !strcmp(p, "-bfg") ? 1 :
2076 !strcmp(p, "-bbg") ? 3 :
2077 !strcmp(p, "-cfg") ? 4 :
2078 !strcmp(p, "-cbg") ? 5 : -1);
2079 assert(index != -1);
2080 cfg.colours[index][0] = col.red / 256;
2081 cfg.colours[index][1] = col.green / 256;
2082 cfg.colours[index][2] = col.blue / 256;
2085 } else if (!strcmp(p, "-e")) {
2086 /* This option swallows all further arguments. */
2092 pty_argv = smalloc((argc+1) * sizeof(char *));
2094 for (i = 0; i < argc; i++)
2095 pty_argv[i] = argv[i];
2096 pty_argv[argc] = NULL;
2097 break; /* finished command-line processing */
2099 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
2101 } else if (!strcmp(p, "-T")) {
2104 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
2105 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
2107 } else if (!strcmp(p, "-log")) {
2110 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
2111 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
2112 cfg.logtype = LGTYP_DEBUG;
2114 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2118 } else if (!strcmp(p, "-ut")) {
2122 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2124 cfg.login_shell = 0;
2126 } else if (!strcmp(p, "-ls")) {
2128 cfg.login_shell = 1;
2130 } else if (!strcmp(p, "-nethack")) {
2132 cfg.nethack_keypad = 1;
2134 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2138 } else if (!strcmp(p, "-sb")) {
2142 } else if (!strcmp(p, "-name")) {
2146 } else if (!strcmp(p, "-xrm")) {
2148 provide_xrm_string(val);
2150 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2156 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
2163 static void block_signal(int sig, int block_it) {
2167 sigaddset(&ss, sig);
2168 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2169 perror("sigprocmask");
2175 * This function retrieves the character set encoding of a font. It
2176 * returns the character set without the X11 hack (in case the user
2177 * asks to use the font's own encoding).
2179 static int set_font_info(struct gui_data *inst, int fontid)
2181 GdkFont *font = inst->fonts[fontid];
2182 XFontStruct *xfs = GDK_FONT_XFONT(font);
2183 Display *disp = GDK_FONT_XDISPLAY(font);
2184 Atom charset_registry, charset_encoding;
2185 unsigned long registry_ret, encoding_ret;
2186 int retval = CS_NONE;
2188 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2189 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2190 inst->fontinfo[fontid].charset = CS_NONE;
2191 inst->fontinfo[fontid].is_wide = 0;
2192 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2193 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2195 reg = XGetAtomName(disp, (Atom)registry_ret);
2196 enc = XGetAtomName(disp, (Atom)encoding_ret);
2198 char *encoding = dupcat(reg, "-", enc, NULL);
2199 retval = inst->fontinfo[fontid].charset =
2200 charset_from_xenc(encoding);
2201 /* FIXME: when libcharset supports wide encodings fix this. */
2202 if (!strcasecmp(encoding, "iso10646-1")) {
2203 inst->fontinfo[fontid].is_wide = 1;
2208 * Hack for X line-drawing characters: if the primary
2209 * font is encoded as ISO-8859-anything, and has valid
2210 * glyphs in the first 32 char positions, it is assumed
2211 * that those glyphs are the VT100 line-drawing
2214 * Actually, we'll hack even harder by only checking
2215 * position 0x19 (vertical line, VT100 linedrawing
2216 * `x'). Then we can check it easily by seeing if the
2217 * ascent and descent differ.
2219 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2220 int lb, rb, wid, asc, desc;
2225 gdk_string_extents(inst->fonts[fontid], text,
2226 &lb, &rb, &wid, &asc, &desc);
2228 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2238 int main(int argc, char **argv)
2240 extern int pty_master_fd; /* declared in pty.c */
2241 extern void pty_pre_init(void); /* declared in pty.c */
2242 struct gui_data *inst;
2245 /* defer any child exit handling until we're ready to deal with
2247 block_signal(SIGCHLD, 1);
2251 gtk_init(&argc, &argv);
2253 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
2255 do_defaults(NULL, &cfg);
2256 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
2260 * Create an instance structure and initialise to zeroes
2262 inst = smalloc(sizeof(*inst));
2263 memset(inst, 0, sizeof(*inst));
2264 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
2266 inst->fonts[0] = gdk_font_load(cfg.font);
2267 if (!inst->fonts[0]) {
2268 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
2271 font_charset = set_font_info(inst, 0);
2272 if (cfg.boldfont[0]) {
2273 inst->fonts[1] = gdk_font_load(cfg.boldfont);
2274 if (!inst->fonts[1]) {
2275 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
2279 set_font_info(inst, 1);
2281 inst->fonts[1] = NULL;
2282 if (cfg.widefont[0]) {
2283 inst->fonts[2] = gdk_font_load(cfg.widefont);
2284 if (!inst->fonts[2]) {
2285 fprintf(stderr, "pterm: unable to load wide font \"%s\"\n",
2289 set_font_info(inst, 2);
2291 inst->fonts[2] = NULL;
2292 if (cfg.wideboldfont[0]) {
2293 inst->fonts[3] = gdk_font_load(cfg.wideboldfont);
2294 if (!inst->fonts[3]) {
2295 fprintf(stderr, "pterm: unable to load wide/bold font \"%s\"\n",
2299 set_font_info(inst, 3);
2301 inst->fonts[3] = NULL;
2303 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2304 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2306 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2307 inst->utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
2309 inst->direct_to_font = init_ucs(cfg.line_codepage, font_charset);
2311 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2313 if (cfg.wintitle[0])
2314 set_title(inst, cfg.wintitle);
2316 set_title(inst, "pterm");
2319 * Set up the colour map.
2321 palette_reset(inst);
2323 inst->area = gtk_drawing_area_new();
2324 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
2325 inst->font_width * cfg.width + 2*cfg.window_border,
2326 inst->font_height * cfg.height + 2*cfg.window_border);
2327 if (cfg.scrollbar) {
2328 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2329 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2331 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2332 if (cfg.scrollbar) {
2333 if (cfg.scrollbar_on_left)
2334 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2336 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2338 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2340 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2344 geom.min_width = inst->font_width + 2*cfg.window_border;
2345 geom.min_height = inst->font_height + 2*cfg.window_border;
2346 geom.max_width = geom.max_height = -1;
2347 geom.base_width = 2*cfg.window_border;
2348 geom.base_height = 2*cfg.window_border;
2349 geom.width_inc = inst->font_width;
2350 geom.height_inc = inst->font_height;
2351 geom.min_aspect = geom.max_aspect = 0;
2352 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2353 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2354 GDK_HINT_RESIZE_INC);
2357 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2358 GTK_SIGNAL_FUNC(destroy), inst);
2359 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2360 GTK_SIGNAL_FUNC(delete_window), inst);
2361 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2362 GTK_SIGNAL_FUNC(key_event), inst);
2363 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2364 GTK_SIGNAL_FUNC(key_event), inst);
2365 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2366 GTK_SIGNAL_FUNC(focus_event), inst);
2367 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2368 GTK_SIGNAL_FUNC(focus_event), inst);
2369 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2370 GTK_SIGNAL_FUNC(configure_area), inst);
2371 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2372 GTK_SIGNAL_FUNC(expose_area), inst);
2373 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2374 GTK_SIGNAL_FUNC(button_event), inst);
2375 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2376 GTK_SIGNAL_FUNC(button_event), inst);
2377 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2378 GTK_SIGNAL_FUNC(motion_event), inst);
2379 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2380 GTK_SIGNAL_FUNC(selection_received), inst);
2381 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2382 GTK_SIGNAL_FUNC(selection_get), inst);
2383 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2384 GTK_SIGNAL_FUNC(selection_clear), inst);
2386 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2387 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2388 gtk_timeout_add(20, timer_func, inst);
2389 gtk_widget_add_events(GTK_WIDGET(inst->area),
2390 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2391 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2392 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2394 gtk_widget_show(inst->area);
2396 gtk_widget_show(inst->sbar);
2397 gtk_widget_show(GTK_WIDGET(inst->hbox));
2398 gtk_widget_show(inst->window);
2400 set_window_background(inst);
2402 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2403 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2404 inst->blankcursor = make_mouse_ptr(inst, -1);
2405 make_mouse_ptr(inst, -2); /* clean up cursor font */
2406 inst->currcursor = inst->textcursor;
2407 show_mouseptr(inst, 1);
2409 inst->term = term_init(&cfg, inst);
2410 inst->logctx = log_init(inst);
2411 term_provide_logctx(inst->term, inst->logctx);
2413 inst->back = &pty_backend;
2414 inst->back->init((void *)inst->term, &inst->backhandle, &cfg,
2416 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2418 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2420 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
2423 ldisc_create(&cfg, inst->term, inst->back, inst->backhandle, inst);
2424 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2426 inst->master_fd = pty_master_fd;
2427 inst->exited = FALSE;
2428 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
2429 pty_input_func, inst);
2431 /* now we're reday to deal with the child exit handler being
2433 block_signal(SIGCHLD, 0);