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);
86 void ldisc_update(void *frontend, int echo, int edit)
89 * This is a stub in pterm. If I ever produce a Unix
90 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
91 * then it will require some termios manoeuvring analogous to
92 * that in the Windows plink.c, but here it's meaningless.
96 int askappend(void *frontend, char *filename)
99 * Logging in an xterm-alike is liable to be something you only
100 * do at serious diagnostic need. Hence, I'm going to take the
101 * easy option for now and assume we always want to overwrite
102 * log files. I can always make it properly configurable later.
107 void logevent(void *frontend, char *string)
110 * This is not a very helpful function: events are logged
111 * pretty much exclusively by the back end, and our pty back
112 * end is self-contained. So we need do nothing.
116 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
118 struct gui_data *inst = (struct gui_data *)frontend;
121 return inst->font_height;
123 return inst->font_width;
127 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
128 * into a cooked one (SELECT, EXTEND, PASTE).
130 * In Unix, this is not configurable; the X button arrangement is
131 * rock-solid across all applications, everyone has a three-button
132 * mouse or a means of faking it, and there is no need to switch
133 * buttons around at all.
135 Mouse_Button translate_button(void *frontend, Mouse_Button button)
137 /* struct gui_data *inst = (struct gui_data *)frontend; */
139 if (button == MBT_LEFT)
141 if (button == MBT_MIDDLE)
143 if (button == MBT_RIGHT)
145 return 0; /* shouldn't happen */
149 * Minimise or restore the window in response to a server-side
152 void set_iconic(void *frontend, int iconic)
155 * GTK 1.2 doesn't know how to do this.
157 #if GTK_CHECK_VERSION(2,0,0)
158 struct gui_data *inst = (struct gui_data *)frontend;
160 gtk_window_iconify(GTK_WINDOW(inst->window));
162 gtk_window_deiconify(GTK_WINDOW(inst->window));
167 * Move the window in response to a server-side request.
169 void move_window(void *frontend, int x, int y)
171 struct gui_data *inst = (struct gui_data *)frontend;
173 * I assume that when the GTK version of this call is available
174 * we should use it. Not sure how it differs from the GDK one,
177 #if GTK_CHECK_VERSION(2,0,0)
178 gtk_window_move(GTK_WINDOW(inst->window), x, y);
180 gdk_window_move(inst->window->window, x, y);
185 * Move the window to the top or bottom of the z-order in response
186 * to a server-side request.
188 void set_zorder(void *frontend, int top)
190 struct gui_data *inst = (struct gui_data *)frontend;
192 gdk_window_raise(inst->window->window);
194 gdk_window_lower(inst->window->window);
198 * Refresh the window in response to a server-side request.
200 void refresh_window(void *frontend)
202 struct gui_data *inst = (struct gui_data *)frontend;
203 term_invalidate(inst->term);
207 * Maximise or restore the window in response to a server-side
210 void set_zoomed(void *frontend, int zoomed)
213 * GTK 1.2 doesn't know how to do this.
215 #if GTK_CHECK_VERSION(2,0,0)
216 struct gui_data *inst = (struct gui_data *)frontend;
218 gtk_window_maximize(GTK_WINDOW(inst->window));
220 gtk_window_unmaximize(GTK_WINDOW(inst->window));
225 * Report whether the window is iconic, for terminal reports.
227 int is_iconic(void *frontend)
229 struct gui_data *inst = (struct gui_data *)frontend;
230 return !gdk_window_is_viewable(inst->window->window);
234 * Report the window's position, for terminal reports.
236 void get_window_pos(void *frontend, int *x, int *y)
238 struct gui_data *inst = (struct gui_data *)frontend;
240 * I assume that when the GTK version of this call is available
241 * we should use it. Not sure how it differs from the GDK one,
244 #if GTK_CHECK_VERSION(2,0,0)
245 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
247 gdk_window_get_position(inst->window->window, x, y);
252 * Report the window's pixel size, for terminal reports.
254 void get_window_pixels(void *frontend, int *x, int *y)
256 struct gui_data *inst = (struct gui_data *)frontend;
258 * I assume that when the GTK version of this call is available
259 * we should use it. Not sure how it differs from the GDK one,
262 #if GTK_CHECK_VERSION(2,0,0)
263 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
265 gdk_window_get_size(inst->window->window, x, y);
270 * Return the window or icon title.
272 char *get_window_title(void *frontend, int icon)
274 struct gui_data *inst = (struct gui_data *)frontend;
275 return icon ? inst->wintitle : inst->icontitle;
278 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
281 * We could implement warn-on-close here if we really wanted
287 static void show_mouseptr(struct gui_data *inst, int show)
289 if (!cfg.hide_mouseptr)
292 gdk_window_set_cursor(inst->area->window, inst->currcursor);
294 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
295 inst->mouseptr_visible = show;
298 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
300 struct gui_data *inst = (struct gui_data *)data;
301 int w, h, need_size = 0;
303 w = (event->width - 2*cfg.window_border) / inst->font_width;
304 h = (event->height - 2*cfg.window_border) / inst->font_height;
306 if (w != cfg.width || h != cfg.height) {
308 gdk_pixmap_unref(inst->pixmap);
318 inst->pixmap = gdk_pixmap_new(widget->window,
319 (cfg.width * inst->font_width +
320 2*cfg.window_border),
321 (cfg.height * inst->font_height +
322 2*cfg.window_border), -1);
324 gc = gdk_gc_new(inst->area->window);
325 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
326 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
327 cfg.width * inst->font_width + 2*cfg.window_border,
328 cfg.height * inst->font_height + 2*cfg.window_border);
333 term_size(inst->term, h, w, cfg.savelines);
339 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
341 struct gui_data *inst = (struct gui_data *)data;
344 * Pass the exposed rectangle to terminal.c, which will call us
345 * back to do the actual painting.
348 gdk_draw_pixmap(widget->window,
349 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
351 event->area.x, event->area.y,
352 event->area.x, event->area.y,
353 event->area.width, event->area.height);
358 #define KEY_PRESSED(k) \
359 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
361 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
363 struct gui_data *inst = (struct gui_data *)data;
367 /* By default, nothing is generated. */
371 * If Alt is being released after typing an Alt+numberpad
372 * sequence, we should generate the code that was typed.
374 * Note that we only do this if more than one key was actually
375 * pressed - I don't think Alt+NumPad4 should be ^D or that
376 * Alt+NumPad3 should be ^C, for example. There's no serious
377 * inconvenience in having to type a zero before a single-digit
380 if (event->type == GDK_KEY_RELEASE &&
381 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
382 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
383 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
385 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
387 output[0] = inst->alt_keycode;
392 if (event->type == GDK_KEY_PRESS) {
396 printf("keypress: keyval = %04x, state = %08x; string =",
397 event->keyval, event->state);
398 for (i = 0; event->string[i]; i++)
399 printf(" %02x", (unsigned char) event->string[i]);
405 * NYI: Compose key (!!! requires Unicode faff before even trying)
409 * If Alt has just been pressed, we start potentially
410 * accumulating an Alt+numberpad code. We do this by
411 * setting alt_keycode to -1 (nothing yet but plausible).
413 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
414 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
415 inst->alt_keycode = -1;
416 inst->alt_digits = 0;
417 goto done; /* this generates nothing else */
421 * If we're seeing a numberpad key press with Mod1 down,
422 * consider adding it to alt_keycode if that's sensible.
423 * Anything _else_ with Mod1 down cancels any possibility
424 * of an ALT keycode: we set alt_keycode to -2.
426 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
428 switch (event->keyval) {
429 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
430 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
431 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
432 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
433 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
434 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
435 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
436 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
437 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
438 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
441 inst->alt_keycode = -2; /* it's invalid */
444 printf("Adding digit %d to keycode %d", digit,
447 if (inst->alt_keycode == -1)
448 inst->alt_keycode = digit; /* one-digit code */
450 inst->alt_keycode = inst->alt_keycode * 10 + digit;
453 printf(" gives new code %d\n", inst->alt_keycode);
455 /* Having used this digit, we now do nothing more with it. */
461 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
464 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
465 term_scroll(inst->term, 0, -cfg.height/2);
468 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
469 term_scroll(inst->term, 0, +cfg.height/2);
474 * Neither does Shift-Ins.
476 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
481 /* ALT+things gives leading Escape. */
483 strncpy(output+1, event->string, 31);
485 end = strlen(output);
486 if (event->state & GDK_MOD1_MASK) {
488 if (end == 1) end = 0;
492 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
493 if (!event->string[0] && event->keyval == '`' &&
494 (event->state & GDK_CONTROL_MASK)) {
499 /* Control-Break is the same as Control-C */
500 if (event->keyval == GDK_Break &&
501 (event->state & GDK_CONTROL_MASK)) {
506 /* Control-2, Control-Space and Control-@ are NUL */
507 if (!event->string[0] &&
508 (event->keyval == ' ' || event->keyval == '2' ||
509 event->keyval == '@') &&
510 (event->state & (GDK_SHIFT_MASK |
511 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
516 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
517 if (!event->string[0] && event->keyval == ' ' &&
518 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
519 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
524 /* We don't let GTK tell us what Backspace is! We know better. */
525 if (event->keyval == GDK_BackSpace &&
526 !(event->state & GDK_SHIFT_MASK)) {
527 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
530 /* For Shift Backspace, do opposite of what is configured. */
531 if (event->keyval == GDK_BackSpace &&
532 (event->state & GDK_SHIFT_MASK)) {
533 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
537 /* Shift-Tab is ESC [ Z */
538 if (event->keyval == GDK_ISO_Left_Tab ||
539 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
540 end = 1 + sprintf(output+1, "\033[Z");
544 * NetHack keypad mode.
546 if (cfg.nethack_keypad) {
548 switch (event->keyval) {
549 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
550 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
551 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
552 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
553 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
554 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
555 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
556 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
557 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
561 if (event->state & GDK_SHIFT_MASK)
570 * Application keypad mode.
572 if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
574 switch (event->keyval) {
575 case GDK_Num_Lock: xkey = 'P'; break;
576 case GDK_KP_Divide: xkey = 'Q'; break;
577 case GDK_KP_Multiply: xkey = 'R'; break;
578 case GDK_KP_Subtract: xkey = 'S'; break;
580 * Keypad + is tricky. It covers a space that would
581 * be taken up on the VT100 by _two_ keys; so we
582 * let Shift select between the two. Worse still,
583 * in xterm function key mode we change which two...
586 if (cfg.funky_type == 2) {
587 if (event->state & GDK_SHIFT_MASK)
591 } else if (event->state & GDK_SHIFT_MASK)
596 case GDK_KP_Enter: xkey = 'M'; break;
597 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
598 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
599 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
600 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
601 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
602 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
603 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
604 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
605 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
606 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
607 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
610 if (inst->term->vt52_mode) {
611 if (xkey >= 'P' && xkey <= 'S')
612 end = 1 + sprintf(output+1, "\033%c", xkey);
614 end = 1 + sprintf(output+1, "\033?%c", xkey);
616 end = 1 + sprintf(output+1, "\033O%c", xkey);
622 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
623 * for integer decimal nn.)
625 * We also deal with the weird ones here. Linux VCs replace F1
626 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
627 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
632 switch (event->keyval) {
634 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
637 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
640 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
643 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
646 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
649 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
652 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
655 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
658 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
661 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
694 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
695 case GDK_Home: case GDK_KP_Home:
698 case GDK_Insert: case GDK_KP_Insert:
701 case GDK_Delete: case GDK_KP_Delete:
704 case GDK_End: case GDK_KP_End:
707 case GDK_Page_Up: case GDK_KP_Page_Up:
710 case GDK_Page_Down: case GDK_KP_Page_Down:
714 /* Reorder edit keys to physical order */
715 if (cfg.funky_type == 3 && code <= 6)
716 code = "\0\2\1\4\5\3\6"[code];
718 if (inst->term->vt52_mode && code > 0 && code <= 6) {
719 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
723 if (cfg.funky_type == 5 && /* SCO function keys */
724 code >= 11 && code <= 34) {
725 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
727 switch (event->keyval) {
728 case GDK_F1: index = 0; break;
729 case GDK_F2: index = 1; break;
730 case GDK_F3: index = 2; break;
731 case GDK_F4: index = 3; break;
732 case GDK_F5: index = 4; break;
733 case GDK_F6: index = 5; break;
734 case GDK_F7: index = 6; break;
735 case GDK_F8: index = 7; break;
736 case GDK_F9: index = 8; break;
737 case GDK_F10: index = 9; break;
738 case GDK_F11: index = 10; break;
739 case GDK_F12: index = 11; break;
741 if (event->state & GDK_SHIFT_MASK) index += 12;
742 if (event->state & GDK_CONTROL_MASK) index += 24;
743 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
746 if (cfg.funky_type == 5 && /* SCO small keypad */
747 code >= 1 && code <= 6) {
748 char codes[] = "HL.FIG";
753 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
757 if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
758 code >= 11 && code <= 24) {
764 if (inst->term->vt52_mode)
765 end = 1 + sprintf(output+1,
766 "\x1B%c", code + 'P' - 11 - offt);
768 end = 1 + sprintf(output+1,
769 "\x1BO%c", code + 'P' - 11 - offt);
772 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
773 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
776 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
777 if (inst->term->vt52_mode)
778 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
780 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
783 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
784 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
788 end = 1 + sprintf(output+1, "\x1B[%d~", code);
794 * Cursor keys. (This includes the numberpad cursor keys,
795 * if we haven't already done them due to app keypad mode.)
797 * Here we also process un-numlocked un-appkeypadded KP5,
798 * which sends ESC [ G.
802 switch (event->keyval) {
803 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
804 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
805 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
806 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
807 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
811 * The arrow keys normally do ESC [ A and so on. In
812 * app cursor keys mode they do ESC O A instead.
813 * Ctrl toggles the two modes.
815 if (inst->term->vt52_mode) {
816 end = 1 + sprintf(output+1, "\033%c", xkey);
817 } else if (!inst->term->app_cursor_keys ^
818 !(event->state & GDK_CONTROL_MASK)) {
819 end = 1 + sprintf(output+1, "\033O%c", xkey);
821 end = 1 + sprintf(output+1, "\033[%c", xkey);
834 printf("generating sequence:");
835 for (i = start; i < end; i++)
836 printf(" %02x", (unsigned char) output[i]);
840 if (!inst->direct_to_font) {
842 * The stuff we've just generated is assumed to be
843 * ISO-8859-1! This sounds insane, but `man
844 * XLookupString' agrees: strings of this type returned
845 * from the X server are hardcoded to 8859-1. Strictly
846 * speaking we should be doing this using some sort of
847 * GtkIMContext, which (if we're lucky) would give us
848 * our data directly in Unicode; but that's not
849 * supported in GTK 1.2 as far as I can tell, and it's
850 * poorly documented even in 2.0, so it'll have to
853 lpage_send(inst->ldisc, CS_ISO8859_1, output+start, end-start, 1);
856 * In direct-to-font mode, we just send the string
857 * exactly as we received it.
859 ldisc_send(inst->ldisc, output+start, end-start, 1);
862 show_mouseptr(inst, 0);
863 term_seen_key_event(inst->term);
864 term_out(inst->term);
870 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
872 struct gui_data *inst = (struct gui_data *)data;
873 int shift, ctrl, alt, x, y, button, act;
875 show_mouseptr(inst, 1);
877 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
878 term_scroll(inst->term, 0, -5);
881 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
882 term_scroll(inst->term, 0, +5);
886 shift = event->state & GDK_SHIFT_MASK;
887 ctrl = event->state & GDK_CONTROL_MASK;
888 alt = event->state & GDK_MOD1_MASK;
889 if (event->button == 1)
891 else if (event->button == 2)
893 else if (event->button == 3)
896 return FALSE; /* don't even know what button! */
898 switch (event->type) {
899 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
900 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
901 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
902 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
903 default: return FALSE; /* don't know this event type */
906 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
907 act != MA_CLICK && act != MA_RELEASE)
908 return TRUE; /* we ignore these in raw mouse mode */
910 x = (event->x - cfg.window_border) / inst->font_width;
911 y = (event->y - cfg.window_border) / inst->font_height;
913 term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
918 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
920 struct gui_data *inst = (struct gui_data *)data;
921 int shift, ctrl, alt, x, y, button;
923 show_mouseptr(inst, 1);
925 shift = event->state & GDK_SHIFT_MASK;
926 ctrl = event->state & GDK_CONTROL_MASK;
927 alt = event->state & GDK_MOD1_MASK;
928 if (event->state & GDK_BUTTON1_MASK)
930 else if (event->state & GDK_BUTTON2_MASK)
932 else if (event->state & GDK_BUTTON3_MASK)
935 return FALSE; /* don't even know what button! */
937 x = (event->x - cfg.window_border) / inst->font_width;
938 y = (event->y - cfg.window_border) / inst->font_height;
940 term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
945 void done_with_pty(struct gui_data *inst)
947 extern void pty_close(void);
949 if (inst->master_fd >= 0) {
951 inst->master_fd = -1;
952 gtk_input_remove(inst->master_func_id);
955 if (!inst->exited && inst->back->exitcode(inst->backhandle) >= 0) {
956 int exitcode = inst->back->exitcode(inst->backhandle);
959 clean = WIFEXITED(exitcode) && (WEXITSTATUS(exitcode) == 0);
962 * Terminate now, if the Close On Exit setting is
965 if (cfg.close_on_exit == COE_ALWAYS ||
966 (cfg.close_on_exit == COE_NORMAL && clean))
970 * Otherwise, output an indication that the session has
975 if (WIFEXITED(exitcode))
976 sprintf(message, "\r\n[pterm: process terminated with exit"
977 " code %d]\r\n", WEXITSTATUS(exitcode));
978 else if (WIFSIGNALED(exitcode))
979 #ifdef HAVE_NO_STRSIGNAL
980 sprintf(message, "\r\n[pterm: process terminated on signal"
981 " %d]\r\n", WTERMSIG(exitcode));
983 sprintf(message, "\r\n[pterm: process terminated on signal"
984 " %d (%.400s)]\r\n", WTERMSIG(exitcode),
985 strsignal(WTERMSIG(exitcode)));
987 from_backend((void *)inst->term, 0, message, strlen(message));
993 void frontend_keypress(void *handle)
995 struct gui_data *inst = (struct gui_data *)handle;
998 * If our child process has exited but not closed, terminate on
1005 gint timer_func(gpointer data)
1007 struct gui_data *inst = (struct gui_data *)data;
1009 if (inst->back->exitcode(inst->backhandle) >= 0) {
1011 * The primary child process died. We could keep the
1012 * terminal open for remaining subprocesses to output to,
1013 * but conventional wisdom seems to feel that that's the
1014 * Wrong Thing for an xterm-alike, so we bail out now
1015 * (though we don't necessarily _close_ the window,
1016 * depending on the state of Close On Exit). This would be
1017 * easy enough to change or make configurable if necessary.
1019 done_with_pty(inst);
1022 term_update(inst->term);
1023 term_blink(inst->term, 0);
1027 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1029 struct gui_data *inst = (struct gui_data *)data;
1033 ret = read(sourcefd, buf, sizeof(buf));
1036 * Clean termination condition is that either ret == 0, or ret
1037 * < 0 and errno == EIO. Not sure why the latter, but it seems
1040 if (ret == 0 || (ret < 0 && errno == EIO)) {
1041 done_with_pty(inst);
1042 } else if (ret < 0) {
1043 perror("read pty master");
1046 from_backend(inst->term, 0, buf, ret);
1047 term_blink(inst->term, 1);
1048 term_out(inst->term);
1051 void destroy(GtkWidget *widget, gpointer data)
1056 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1058 struct gui_data *inst = (struct gui_data *)data;
1059 inst->term->has_focus = event->in;
1060 term_out(inst->term);
1061 term_update(inst->term);
1062 show_mouseptr(inst, 1);
1067 * set or clear the "raw mouse message" mode
1069 void set_raw_mouse_mode(void *frontend, int activate)
1071 struct gui_data *inst = (struct gui_data *)frontend;
1072 activate = activate && !cfg.no_mouse_rep;
1073 send_raw_mouse = activate;
1075 inst->currcursor = inst->rawcursor;
1077 inst->currcursor = inst->textcursor;
1078 show_mouseptr(inst, inst->mouseptr_visible);
1081 void request_resize(void *frontend, int w, int h)
1083 struct gui_data *inst = (struct gui_data *)frontend;
1084 int large_x, large_y;
1085 int offset_x, offset_y;
1087 GtkRequisition inner, outer;
1090 * This is a heinous hack dreamed up by the gnome-terminal
1091 * people to get around a limitation in gtk. The problem is
1092 * that in order to set the size correctly we really need to be
1093 * calling gtk_window_resize - but that needs to know the size
1094 * of the _whole window_, not the drawing area. So what we do
1095 * is to set an artificially huge size request on the drawing
1096 * area, recompute the resulting size request on the window,
1097 * and look at the difference between the two. That gives us
1098 * the x and y offsets we need to translate drawing area size
1099 * into window size for real, and then we call
1100 * gtk_window_resize.
1104 * We start by retrieving the current size of the whole window.
1105 * Adding a bit to _that_ will give us a value we can use as a
1106 * bogus size request which guarantees to be bigger than the
1107 * current size of the drawing area.
1109 get_window_pixels(inst, &large_x, &large_y);
1113 #if GTK_CHECK_VERSION(2,0,0)
1114 gtk_widget_set_size_request(inst->area, large_x, large_y);
1116 gtk_widget_set_usize(inst->area, large_x, large_y);
1118 gtk_widget_size_request(inst->area, &inner);
1119 gtk_widget_size_request(inst->window, &outer);
1121 offset_x = outer.width - inner.width;
1122 offset_y = outer.height - inner.height;
1124 area_x = inst->font_width * w + 2*cfg.window_border;
1125 area_y = inst->font_height * h + 2*cfg.window_border;
1128 * Now we must set the size request on the drawing area back to
1129 * something sensible before we commit the real resize. Best
1130 * way to do this, I think, is to set it to what the size is
1131 * really going to end up being.
1133 #if GTK_CHECK_VERSION(2,0,0)
1134 gtk_widget_set_size_request(inst->area, area_x, area_y);
1136 gtk_widget_set_usize(inst->area, area_x, area_y);
1139 #if GTK_CHECK_VERSION(2,0,0)
1140 gtk_window_resize(GTK_WINDOW(inst->window),
1141 area_x + offset_x, area_y + offset_y);
1143 gdk_window_resize(inst->window->window,
1144 area_x + offset_x, area_y + offset_y);
1148 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1150 gboolean success[1];
1152 inst->cols[n].red = r * 0x0101;
1153 inst->cols[n].green = g * 0x0101;
1154 inst->cols[n].blue = b * 0x0101;
1156 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1157 FALSE, FALSE, success);
1159 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1163 void set_window_background(struct gui_data *inst)
1165 if (inst->area && inst->area->window)
1166 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1167 if (inst->window && inst->window->window)
1168 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1171 void palette_set(void *frontend, int n, int r, int g, int b)
1173 struct gui_data *inst = (struct gui_data *)frontend;
1174 static const int first[21] = {
1175 0, 2, 4, 6, 8, 10, 12, 14,
1176 1, 3, 5, 7, 9, 11, 13, 15,
1179 real_palette_set(inst, first[n], r, g, b);
1181 real_palette_set(inst, first[n] + 1, r, g, b);
1183 set_window_background(inst);
1186 void palette_reset(void *frontend)
1188 struct gui_data *inst = (struct gui_data *)frontend;
1189 /* This maps colour indices in cfg to those used in inst->cols. */
1190 static const int ww[] = {
1191 6, 7, 8, 9, 10, 11, 12, 13,
1192 14, 15, 16, 17, 18, 19, 20, 21,
1195 gboolean success[NCOLOURS];
1198 assert(lenof(ww) == NCOLOURS);
1200 if (!inst->colmap) {
1201 inst->colmap = gdk_colormap_get_system();
1203 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1206 for (i = 0; i < NCOLOURS; i++) {
1207 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1208 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1209 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1212 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1213 FALSE, FALSE, success);
1214 for (i = 0; i < NCOLOURS; i++) {
1216 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1217 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1220 set_window_background(inst);
1223 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1225 struct gui_data *inst = (struct gui_data *)frontend;
1226 if (inst->pasteout_data)
1227 sfree(inst->pasteout_data);
1228 if (inst->pasteout_data_utf8)
1229 sfree(inst->pasteout_data_utf8);
1232 * Set up UTF-8 paste data. This only happens if we aren't in
1233 * direct-to-font mode using the D800 hack.
1235 if (!inst->direct_to_font) {
1236 wchar_t *tmp = data;
1239 inst->pasteout_data_utf8 = smalloc(len*6);
1240 inst->pasteout_data_utf8_len = len*6;
1241 inst->pasteout_data_utf8_len =
1242 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1243 inst->pasteout_data_utf8_len,
1244 CS_UTF8, NULL, NULL, 0);
1245 if (inst->pasteout_data_utf8_len == 0) {
1246 sfree(inst->pasteout_data_utf8);
1247 inst->pasteout_data_utf8 = NULL;
1249 inst->pasteout_data_utf8 =
1250 srealloc(inst->pasteout_data_utf8,
1251 inst->pasteout_data_utf8_len);
1254 inst->pasteout_data_utf8 = NULL;
1255 inst->pasteout_data_utf8_len = 0;
1258 inst->pasteout_data = smalloc(len*6);
1259 inst->pasteout_data_len = len*6;
1260 inst->pasteout_data_len = wc_to_mb(line_codepage, 0, data, len,
1261 inst->pasteout_data,
1262 inst->pasteout_data_len, NULL, NULL);
1263 if (inst->pasteout_data_len == 0) {
1264 sfree(inst->pasteout_data);
1265 inst->pasteout_data = NULL;
1267 inst->pasteout_data =
1268 srealloc(inst->pasteout_data, inst->pasteout_data_len);
1271 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1272 GDK_CURRENT_TIME)) {
1273 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1274 GDK_SELECTION_TYPE_STRING, 1);
1275 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1276 inst->compound_text_atom, 1);
1277 if (inst->pasteout_data_utf8)
1278 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1279 inst->utf8_string_atom, 1);
1283 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1284 guint info, guint time_stamp, gpointer data)
1286 struct gui_data *inst = (struct gui_data *)data;
1287 if (seldata->target == inst->utf8_string_atom)
1288 gtk_selection_data_set(seldata, seldata->target, 8,
1289 inst->pasteout_data_utf8,
1290 inst->pasteout_data_utf8_len);
1292 gtk_selection_data_set(seldata, seldata->target, 8,
1293 inst->pasteout_data, inst->pasteout_data_len);
1296 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1299 struct gui_data *inst = (struct gui_data *)data;
1300 term_deselect(inst->term);
1301 if (inst->pasteout_data)
1302 sfree(inst->pasteout_data);
1303 if (inst->pasteout_data_utf8)
1304 sfree(inst->pasteout_data_utf8);
1305 inst->pasteout_data = NULL;
1306 inst->pasteout_data_len = 0;
1307 inst->pasteout_data_utf8 = NULL;
1308 inst->pasteout_data_utf8_len = 0;
1312 void request_paste(void *frontend)
1314 struct gui_data *inst = (struct gui_data *)frontend;
1316 * In Unix, pasting is asynchronous: all we can do at the
1317 * moment is to call gtk_selection_convert(), and when the data
1318 * comes back _then_ we can call term_do_paste().
1321 if (!inst->direct_to_font) {
1323 * First we attempt to retrieve the selection as a UTF-8
1324 * string (which we will convert to the correct code page
1325 * before sending to the session, of course). If that
1326 * fails, selection_received() will be informed and will
1327 * fall back to an ordinary string.
1329 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1330 inst->utf8_string_atom, GDK_CURRENT_TIME);
1333 * If we're in direct-to-font mode, we disable UTF-8
1334 * pasting, and go straight to ordinary string data.
1336 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1337 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1341 gint idle_paste_func(gpointer data); /* forward ref */
1343 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1344 guint time, gpointer data)
1346 struct gui_data *inst = (struct gui_data *)data;
1348 if (seldata->target == inst->utf8_string_atom && seldata->length <= 0) {
1350 * Failed to get a UTF-8 selection string. Try an ordinary
1353 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1354 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1359 * Any other failure should just go foom.
1361 if (seldata->length <= 0 ||
1362 (seldata->type != GDK_SELECTION_TYPE_STRING &&
1363 seldata->type != inst->utf8_string_atom))
1364 return; /* Nothing happens. */
1366 if (inst->pastein_data)
1367 sfree(inst->pastein_data);
1369 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1370 inst->pastein_data_len = seldata->length;
1371 inst->pastein_data_len =
1372 mb_to_wc((seldata->type == inst->utf8_string_atom ?
1373 CS_UTF8 : line_codepage),
1374 0, seldata->data, seldata->length,
1375 inst->pastein_data, inst->pastein_data_len);
1377 term_do_paste(inst->term);
1379 if (term_paste_pending(inst->term))
1380 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1383 gint idle_paste_func(gpointer data)
1385 struct gui_data *inst = (struct gui_data *)data;
1387 if (term_paste_pending(inst->term))
1388 term_paste(inst->term);
1390 gtk_idle_remove(inst->term_paste_idle_id);
1396 void get_clip(void *frontend, wchar_t ** p, int *len)
1398 struct gui_data *inst = (struct gui_data *)frontend;
1401 *p = inst->pastein_data;
1402 *len = inst->pastein_data_len;
1406 void set_title(void *frontend, char *title)
1408 struct gui_data *inst = (struct gui_data *)frontend;
1409 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1410 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1411 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1414 void set_icon(void *frontend, char *title)
1416 struct gui_data *inst = (struct gui_data *)frontend;
1417 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1418 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1419 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1422 void set_sbar(void *frontend, int total, int start, int page)
1424 struct gui_data *inst = (struct gui_data *)frontend;
1427 inst->sbar_adjust->lower = 0;
1428 inst->sbar_adjust->upper = total;
1429 inst->sbar_adjust->value = start;
1430 inst->sbar_adjust->page_size = page;
1431 inst->sbar_adjust->step_increment = 1;
1432 inst->sbar_adjust->page_increment = page/2;
1433 inst->ignore_sbar = TRUE;
1434 gtk_adjustment_changed(inst->sbar_adjust);
1435 inst->ignore_sbar = FALSE;
1438 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1440 struct gui_data *inst = (struct gui_data *)data;
1444 if (!inst->ignore_sbar)
1445 term_scroll(inst->term, 1, (int)adj->value);
1448 void sys_cursor(void *frontend, int x, int y)
1451 * This is meaningless under X.
1456 * This is still called when mode==BELL_VISUAL, even though the
1457 * visual bell is handled entirely within terminal.c, because we
1458 * may want to perform additional actions on any kind of bell (for
1459 * example, taskbar flashing in Windows).
1461 void beep(void *frontend, int mode)
1463 if (mode != BELL_VISUAL)
1467 int char_width(Context ctx, int uc)
1470 * Under X, any fixed-width font really _is_ fixed-width.
1471 * Double-width characters will be dealt with using a separate
1472 * font. For the moment we can simply return 1.
1477 Context get_ctx(void *frontend)
1479 struct gui_data *inst = (struct gui_data *)frontend;
1480 struct draw_ctx *dctx;
1482 if (!inst->area->window)
1485 dctx = smalloc(sizeof(*dctx));
1487 dctx->gc = gdk_gc_new(inst->area->window);
1491 void free_ctx(Context ctx)
1493 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1494 /* struct gui_data *inst = dctx->inst; */
1495 GdkGC *gc = dctx->gc;
1501 * Draw a line of text in the window, at given character
1502 * coordinates, in given attributes.
1504 * We are allowed to fiddle with the contents of `text'.
1506 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1507 unsigned long attr, int lattr)
1509 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1510 struct gui_data *inst = dctx->inst;
1511 GdkGC *gc = dctx->gc;
1513 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1515 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1516 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1517 if (attr & ATTR_REVERSE) {
1522 if (cfg.bold_colour && (attr & ATTR_BOLD))
1524 if (cfg.bold_colour && (attr & ATTR_BLINK))
1526 if (attr & TATTR_ACTCURS) {
1531 fontid = shadow = 0;
1533 if (attr & ATTR_WIDE) {
1540 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1541 if (inst->fonts[fontid | 1])
1547 if (lattr != LATTR_NORM) {
1549 if (x >= inst->term->cols)
1551 if (x + len*2*widefactor > inst->term->cols)
1552 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1560 r.x = x*inst->font_width+cfg.window_border;
1561 r.y = y*inst->font_height+cfg.window_border;
1562 r.width = rlen*widefactor*inst->font_width;
1563 r.height = inst->font_height;
1564 gdk_gc_set_clip_rectangle(gc, &r);
1567 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1568 gdk_draw_rectangle(inst->pixmap, gc, 1,
1569 x*inst->font_width+cfg.window_border,
1570 y*inst->font_height+cfg.window_border,
1571 rlen*widefactor*inst->font_width, inst->font_height);
1573 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1580 wcs = smalloc(sizeof(wchar_t) * (len+1));
1581 for (i = 0; i < len; i++) {
1582 wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
1585 if (inst->fonts[fontid] == NULL) {
1587 * The font for this contingency does not exist.
1588 * Typically this means we've been given ATTR_WIDE
1589 * character and have no wide font. So we display
1590 * nothing at all; such is life.
1592 } else if (inst->fontinfo[fontid].is_wide) {
1594 * At least one version of gdk_draw_text_wc() has a
1595 * weird bug whereby it reads `len' elements of the
1596 * input string, but only draws `len/2'. Hence I'm
1597 * going to make its input array twice as long as it
1598 * theoretically needs to be, and pass in twice the
1599 * actual number of characters. If a fixed gdk actually
1600 * takes the doubled length seriously, then (a) the
1601 * array will stand scrutiny up to the full length, (b)
1602 * the spare elements of the array are full of zeroes
1603 * which will probably be an empty glyph in the font,
1604 * and (c) the clip rectangle should prevent it causing
1607 gwcs = smalloc(sizeof(GdkWChar) * (len*2+1));
1608 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
1610 * FIXME: when we have a wide-char equivalent of
1611 * from_unicode, use it instead of this.
1613 for (i = 0; i <= len; i++)
1615 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
1616 x*inst->font_width+cfg.window_border,
1617 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1621 gcs = smalloc(sizeof(GdkWChar) * (len+1));
1622 wc_to_mb(inst->fontinfo[fontid].charset, 0,
1623 wcs, len, gcs, len, ".", NULL);
1624 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1625 x*inst->font_width+cfg.window_border,
1626 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1634 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1635 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1636 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1640 if (attr & ATTR_UNDER) {
1641 int uheight = inst->fonts[0]->ascent + 1;
1642 if (uheight >= inst->font_height)
1643 uheight = inst->font_height - 1;
1644 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1645 y*inst->font_height + uheight + cfg.window_border,
1646 (x+len)*widefactor*inst->font_width-1+cfg.window_border,
1647 y*inst->font_height + uheight + cfg.window_border);
1650 if (lattr != LATTR_NORM) {
1652 * I can't find any plausible StretchBlt equivalent in the
1653 * X server, so I'm going to do this the slow and painful
1654 * way. This will involve repeated calls to
1655 * gdk_draw_pixmap() to stretch the text horizontally. It's
1656 * O(N^2) in time and O(N) in network bandwidth, but you
1657 * try thinking of a better way. :-(
1660 for (i = 0; i < len * widefactor * inst->font_width; i++) {
1661 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1662 x*inst->font_width+cfg.window_border + 2*i,
1663 y*inst->font_height+cfg.window_border,
1664 x*inst->font_width+cfg.window_border + 2*i+1,
1665 y*inst->font_height+cfg.window_border,
1666 len * inst->font_width - i, inst->font_height);
1669 if (lattr != LATTR_WIDE) {
1671 /* Now stretch vertically, in the same way. */
1672 if (lattr == LATTR_BOT)
1676 for (i = 0; i < inst->font_height; i+=2) {
1677 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1678 x*inst->font_width+cfg.window_border,
1679 y*inst->font_height+cfg.window_border+dt*i+db,
1680 x*widefactor*inst->font_width+cfg.window_border,
1681 y*inst->font_height+cfg.window_border+dt*(i+1),
1682 len * inst->font_width, inst->font_height-i-1);
1688 void do_text(Context ctx, int x, int y, char *text, int len,
1689 unsigned long attr, int lattr)
1691 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1692 struct gui_data *inst = dctx->inst;
1693 GdkGC *gc = dctx->gc;
1696 do_text_internal(ctx, x, y, text, len, attr, lattr);
1698 if (attr & ATTR_WIDE) {
1704 if (lattr != LATTR_NORM) {
1706 if (x >= inst->term->cols)
1708 if (x + len*2*widefactor > inst->term->cols)
1709 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1713 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1714 x*inst->font_width+cfg.window_border,
1715 y*inst->font_height+cfg.window_border,
1716 x*inst->font_width+cfg.window_border,
1717 y*inst->font_height+cfg.window_border,
1718 len*widefactor*inst->font_width, inst->font_height);
1721 void do_cursor(Context ctx, int x, int y, char *text, int len,
1722 unsigned long attr, int lattr)
1724 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1725 struct gui_data *inst = dctx->inst;
1726 GdkGC *gc = dctx->gc;
1728 int passive, widefactor;
1730 if (attr & TATTR_PASCURS) {
1731 attr &= ~TATTR_PASCURS;
1735 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1736 attr &= ~TATTR_ACTCURS;
1738 do_text_internal(ctx, x, y, text, len, attr, lattr);
1740 if (attr & ATTR_WIDE) {
1746 if (lattr != LATTR_NORM) {
1748 if (x >= inst->term->cols)
1750 if (x + len*2*widefactor > inst->term->cols)
1751 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1755 if (cfg.cursor_type == 0) {
1757 * An active block cursor will already have been done by
1758 * the above do_text call, so we only need to do anything
1762 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1763 gdk_draw_rectangle(inst->pixmap, gc, 0,
1764 x*inst->font_width+cfg.window_border,
1765 y*inst->font_height+cfg.window_border,
1766 len*inst->font_width-1, inst->font_height-1);
1770 int startx, starty, dx, dy, length, i;
1774 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1775 char_width = 2*inst->font_width;
1777 char_width = inst->font_width;
1779 if (cfg.cursor_type == 1) {
1780 uheight = inst->fonts[0]->ascent + 1;
1781 if (uheight >= inst->font_height)
1782 uheight = inst->font_height - 1;
1784 startx = x * inst->font_width + cfg.window_border;
1785 starty = y * inst->font_height + cfg.window_border + uheight;
1788 length = len * char_width;
1791 if (attr & TATTR_RIGHTCURS)
1792 xadjust = char_width - 1;
1793 startx = x * inst->font_width + cfg.window_border + xadjust;
1794 starty = y * inst->font_height + cfg.window_border;
1797 length = inst->font_height;
1800 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1802 for (i = 0; i < length; i++) {
1804 gdk_draw_point(inst->pixmap, gc, startx, starty);
1810 gdk_draw_line(inst->pixmap, gc, startx, starty,
1811 startx + (length-1) * dx, starty + (length-1) * dy);
1815 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1816 x*inst->font_width+cfg.window_border,
1817 y*inst->font_height+cfg.window_border,
1818 x*inst->font_width+cfg.window_border,
1819 y*inst->font_height+cfg.window_border,
1820 len*widefactor*inst->font_width, inst->font_height);
1823 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1826 * Truly hideous hack: GTK doesn't allow us to set the mouse
1827 * cursor foreground and background colours unless we've _also_
1828 * created our own cursor from bitmaps. Therefore, I need to
1829 * load the `cursor' font and draw glyphs from it on to
1830 * pixmaps, in order to construct my cursors with the fg and bg
1831 * I want. This is a gross hack, but it's more self-contained
1832 * than linking in Xlib to find the X window handle to
1833 * inst->area and calling XRecolorCursor, and it's more
1834 * futureproof than hard-coding the shapes as bitmap arrays.
1836 static GdkFont *cursor_font = NULL;
1837 GdkPixmap *source, *mask;
1839 GdkColor cfg = { 0, 65535, 65535, 65535 };
1840 GdkColor cbg = { 0, 0, 0, 0 };
1841 GdkColor dfg = { 1, 65535, 65535, 65535 };
1842 GdkColor dbg = { 0, 0, 0, 0 };
1845 gint lb, rb, wid, asc, desc, w, h, x, y;
1847 if (cursor_val == -2) {
1848 gdk_font_unref(cursor_font);
1852 if (cursor_val >= 0 && !cursor_font)
1853 cursor_font = gdk_font_load("cursor");
1856 * Get the text extent of the cursor in question. We use the
1857 * mask character for this, because it's typically slightly
1858 * bigger than the main character.
1860 if (cursor_val >= 0) {
1862 text[0] = (char)cursor_val + 1;
1863 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1864 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1870 source = gdk_pixmap_new(NULL, w, h, 1);
1871 mask = gdk_pixmap_new(NULL, w, h, 1);
1874 * Draw the mask character on the mask pixmap.
1876 gc = gdk_gc_new(mask);
1877 gdk_gc_set_foreground(gc, &dbg);
1878 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1879 if (cursor_val >= 0) {
1881 text[0] = (char)cursor_val + 1;
1882 gdk_gc_set_foreground(gc, &dfg);
1883 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1888 * Draw the main character on the source pixmap.
1890 gc = gdk_gc_new(source);
1891 gdk_gc_set_foreground(gc, &dbg);
1892 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1893 if (cursor_val >= 0) {
1895 text[0] = (char)cursor_val;
1896 gdk_gc_set_foreground(gc, &dfg);
1897 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1902 * Create the cursor.
1904 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1909 gdk_pixmap_unref(source);
1910 gdk_pixmap_unref(mask);
1915 void modalfatalbox(char *p, ...)
1918 fprintf(stderr, "FATAL ERROR: ");
1920 vfprintf(stderr, p, ap);
1922 fputc('\n', stderr);
1926 char *get_x_display(void *frontend)
1928 return gdk_get_display();
1931 static void help(FILE *fp) {
1933 "pterm option summary:\n"
1935 " --display DISPLAY Specify X display to use (note '--')\n"
1936 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1937 " -fn FONT Normal text font\n"
1938 " -fb FONT Bold text font\n"
1939 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1940 " -sl LINES Number of lines of scrollback\n"
1941 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1942 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1943 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1944 " -T TITLE Window title\n"
1945 " -ut, +ut Do(default) or do not update utmp\n"
1946 " -ls, +ls Do(default) or do not make shell a login shell\n"
1947 " -sb, +sb Do(default) or do not display a scrollbar\n"
1948 " -log PATH Log all output to a file\n"
1949 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1950 " -xrm RESOURCE-STRING Set an X resource\n"
1951 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1952 ) < 0 || fflush(fp) < 0) {
1953 perror("output error");
1958 int do_cmdline(int argc, char **argv, int do_everything)
1961 extern char **pty_argv; /* declared in pty.c */
1964 * Macros to make argument handling easier. Note that because
1965 * they need to call `continue', they cannot be contained in
1966 * the usual do {...} while (0) wrapper to make them
1967 * syntactically single statements; hence it is not legal to
1968 * use one of these macros as an unbraced statement between
1971 #define EXPECTS_ARG { \
1972 if (--argc <= 0) { \
1974 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1979 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
1988 while (--argc > 0) {
1990 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1993 strncpy(cfg.font, val, sizeof(cfg.font));
1994 cfg.font[sizeof(cfg.font)-1] = '\0';
1996 } else if (!strcmp(p, "-fb")) {
1999 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
2000 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
2002 } else if (!strcmp(p, "-fw")) {
2005 strncpy(cfg.widefont, val, sizeof(cfg.widefont));
2006 cfg.widefont[sizeof(cfg.widefont)-1] = '\0';
2008 } else if (!strcmp(p, "-fwb")) {
2011 strncpy(cfg.wideboldfont, val, sizeof(cfg.wideboldfont));
2012 cfg.wideboldfont[sizeof(cfg.wideboldfont)-1] = '\0';
2014 } else if (!strcmp(p, "-cs")) {
2017 strncpy(cfg.line_codepage, val, sizeof(cfg.line_codepage));
2018 cfg.line_codepage[sizeof(cfg.line_codepage)-1] = '\0';
2020 } else if (!strcmp(p, "-geometry")) {
2021 int flags, x, y, w, h;
2025 flags = XParseGeometry(val, &x, &y, &w, &h);
2026 if (flags & WidthValue)
2028 if (flags & HeightValue)
2032 * Apparently setting the initial window position is
2033 * difficult in GTK 1.2. Not entirely sure why this
2034 * should be. 2.0 has gtk_window_parse_geometry(),
2035 * which would help... For the moment, though, I can't
2036 * be bothered with this.
2039 } else if (!strcmp(p, "-sl")) {
2042 cfg.savelines = atoi(val);
2044 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2045 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2046 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2051 if (!gdk_color_parse(val, &col)) {
2053 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
2056 index = (!strcmp(p, "-fg") ? 0 :
2057 !strcmp(p, "-bg") ? 2 :
2058 !strcmp(p, "-bfg") ? 1 :
2059 !strcmp(p, "-bbg") ? 3 :
2060 !strcmp(p, "-cfg") ? 4 :
2061 !strcmp(p, "-cbg") ? 5 : -1);
2062 assert(index != -1);
2063 cfg.colours[index][0] = col.red / 256;
2064 cfg.colours[index][1] = col.green / 256;
2065 cfg.colours[index][2] = col.blue / 256;
2068 } else if (!strcmp(p, "-e")) {
2069 /* This option swallows all further arguments. */
2075 pty_argv = smalloc((argc+1) * sizeof(char *));
2077 for (i = 0; i < argc; i++)
2078 pty_argv[i] = argv[i];
2079 pty_argv[argc] = NULL;
2080 break; /* finished command-line processing */
2082 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
2084 } else if (!strcmp(p, "-T")) {
2087 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
2088 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
2090 } else if (!strcmp(p, "-log")) {
2093 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
2094 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
2095 cfg.logtype = LGTYP_DEBUG;
2097 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2101 } else if (!strcmp(p, "-ut")) {
2105 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2107 cfg.login_shell = 0;
2109 } else if (!strcmp(p, "-ls")) {
2111 cfg.login_shell = 1;
2113 } else if (!strcmp(p, "-nethack")) {
2115 cfg.nethack_keypad = 1;
2117 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2121 } else if (!strcmp(p, "-sb")) {
2125 } else if (!strcmp(p, "-name")) {
2129 } else if (!strcmp(p, "-xrm")) {
2131 provide_xrm_string(val);
2133 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2139 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
2146 static void block_signal(int sig, int block_it) {
2150 sigaddset(&ss, sig);
2151 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2152 perror("sigprocmask");
2158 * This function retrieves the character set encoding of a font. It
2159 * returns the character set without the X11 hack (in case the user
2160 * asks to use the font's own encoding).
2162 static int set_font_info(struct gui_data *inst, int fontid)
2164 GdkFont *font = inst->fonts[fontid];
2165 XFontStruct *xfs = GDK_FONT_XFONT(font);
2166 Display *disp = GDK_FONT_XDISPLAY(font);
2167 Atom charset_registry, charset_encoding;
2168 unsigned long registry_ret, encoding_ret;
2169 int retval = CS_NONE;
2171 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2172 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2173 inst->fontinfo[fontid].charset = CS_NONE;
2174 inst->fontinfo[fontid].is_wide = 0;
2175 if (XGetFontProperty(xfs, charset_registry, ®istry_ret) &&
2176 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2178 reg = XGetAtomName(disp, (Atom)registry_ret);
2179 enc = XGetAtomName(disp, (Atom)encoding_ret);
2181 char *encoding = dupcat(reg, "-", enc, NULL);
2182 retval = inst->fontinfo[fontid].charset =
2183 charset_from_xenc(encoding);
2184 /* FIXME: when libcharset supports wide encodings fix this. */
2185 if (!strcasecmp(encoding, "iso10646-1")) {
2186 inst->fontinfo[fontid].is_wide = 1;
2191 * Hack for X line-drawing characters: if the primary
2192 * font is encoded as ISO-8859-anything, and has valid
2193 * glyphs in the first 32 char positions, it is assumed
2194 * that those glyphs are the VT100 line-drawing
2197 * Actually, we'll hack even harder by only checking
2198 * position 0x19 (vertical line, VT100 linedrawing
2199 * `x'). Then we can check it easily by seeing if the
2200 * ascent and descent differ.
2202 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2203 int lb, rb, wid, asc, desc;
2208 gdk_string_extents(inst->fonts[fontid], text,
2209 &lb, &rb, &wid, &asc, &desc);
2211 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2221 int main(int argc, char **argv)
2223 extern int pty_master_fd; /* declared in pty.c */
2224 extern void pty_pre_init(void); /* declared in pty.c */
2225 struct gui_data *inst;
2228 /* defer any child exit handling until we're ready to deal with
2230 block_signal(SIGCHLD, 1);
2234 gtk_init(&argc, &argv);
2236 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
2238 do_defaults(NULL, &cfg);
2239 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
2243 * Create an instance structure and initialise to zeroes
2245 inst = smalloc(sizeof(*inst));
2246 memset(inst, 0, sizeof(*inst));
2247 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
2249 inst->fonts[0] = gdk_font_load(cfg.font);
2250 if (!inst->fonts[0]) {
2251 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
2254 font_charset = set_font_info(inst, 0);
2255 if (cfg.boldfont[0]) {
2256 inst->fonts[1] = gdk_font_load(cfg.boldfont);
2257 if (!inst->fonts[1]) {
2258 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
2262 set_font_info(inst, 1);
2264 inst->fonts[1] = NULL;
2265 if (cfg.widefont[0]) {
2266 inst->fonts[2] = gdk_font_load(cfg.widefont);
2267 if (!inst->fonts[2]) {
2268 fprintf(stderr, "pterm: unable to load wide font \"%s\"\n",
2272 set_font_info(inst, 2);
2274 inst->fonts[2] = NULL;
2275 if (cfg.wideboldfont[0]) {
2276 inst->fonts[3] = gdk_font_load(cfg.wideboldfont);
2277 if (!inst->fonts[3]) {
2278 fprintf(stderr, "pterm: unable to load wide/bold font \"%s\"\n",
2282 set_font_info(inst, 3);
2284 inst->fonts[3] = NULL;
2286 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2287 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2289 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2290 inst->utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
2292 inst->direct_to_font = init_ucs(font_charset);
2294 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2296 if (cfg.wintitle[0])
2297 set_title(inst, cfg.wintitle);
2299 set_title(inst, "pterm");
2302 * Set up the colour map.
2304 palette_reset(inst);
2306 inst->area = gtk_drawing_area_new();
2307 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
2308 inst->font_width * cfg.width + 2*cfg.window_border,
2309 inst->font_height * cfg.height + 2*cfg.window_border);
2310 if (cfg.scrollbar) {
2311 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2312 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2314 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2315 if (cfg.scrollbar) {
2316 if (cfg.scrollbar_on_left)
2317 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2319 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2321 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2323 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2327 geom.min_width = inst->font_width + 2*cfg.window_border;
2328 geom.min_height = inst->font_height + 2*cfg.window_border;
2329 geom.max_width = geom.max_height = -1;
2330 geom.base_width = 2*cfg.window_border;
2331 geom.base_height = 2*cfg.window_border;
2332 geom.width_inc = inst->font_width;
2333 geom.height_inc = inst->font_height;
2334 geom.min_aspect = geom.max_aspect = 0;
2335 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2336 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2337 GDK_HINT_RESIZE_INC);
2340 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2341 GTK_SIGNAL_FUNC(destroy), inst);
2342 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2343 GTK_SIGNAL_FUNC(delete_window), inst);
2344 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2345 GTK_SIGNAL_FUNC(key_event), inst);
2346 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2347 GTK_SIGNAL_FUNC(key_event), inst);
2348 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2349 GTK_SIGNAL_FUNC(focus_event), inst);
2350 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2351 GTK_SIGNAL_FUNC(focus_event), inst);
2352 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2353 GTK_SIGNAL_FUNC(configure_area), inst);
2354 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2355 GTK_SIGNAL_FUNC(expose_area), inst);
2356 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2357 GTK_SIGNAL_FUNC(button_event), inst);
2358 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2359 GTK_SIGNAL_FUNC(button_event), inst);
2360 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2361 GTK_SIGNAL_FUNC(motion_event), inst);
2362 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2363 GTK_SIGNAL_FUNC(selection_received), inst);
2364 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2365 GTK_SIGNAL_FUNC(selection_get), inst);
2366 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2367 GTK_SIGNAL_FUNC(selection_clear), inst);
2369 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2370 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2371 gtk_timeout_add(20, timer_func, inst);
2372 gtk_widget_add_events(GTK_WIDGET(inst->area),
2373 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2374 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2375 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2377 gtk_widget_show(inst->area);
2379 gtk_widget_show(inst->sbar);
2380 gtk_widget_show(GTK_WIDGET(inst->hbox));
2381 gtk_widget_show(inst->window);
2383 set_window_background(inst);
2385 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2386 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2387 inst->blankcursor = make_mouse_ptr(inst, -1);
2388 make_mouse_ptr(inst, -2); /* clean up cursor font */
2389 inst->currcursor = inst->textcursor;
2390 show_mouseptr(inst, 1);
2392 inst->term = term_init(&cfg, inst);
2393 inst->logctx = log_init(inst);
2394 term_provide_logctx(inst->term, inst->logctx);
2396 inst->back = &pty_backend;
2397 inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
2398 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2400 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2402 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
2405 ldisc_create(&cfg, inst->term, inst->back, inst->backhandle, inst);
2406 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2408 inst->master_fd = pty_master_fd;
2409 inst->exited = FALSE;
2410 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
2411 pty_input_func, inst);
2413 /* now we're reday to deal with the child exit handler being
2415 block_signal(SIGCHLD, 0);