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.
15 #include <X11/Xutil.h>
17 #include <gdk/gdkkeysyms.h>
19 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
22 #define CAT2(x,y) x ## y
23 #define CAT(x,y) CAT2(x,y)
24 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
26 #define NCOLOURS (lenof(((Config *)0)->colours))
29 GtkWidget *window, *area, *sbar;
31 GtkAdjustment *sbar_adjust;
33 GdkFont *fonts[2]; /* normal and bold (for now!) */
34 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
35 GdkColor cols[NCOLOURS];
37 wchar_t *pastein_data;
40 int pasteout_data_len;
41 int font_width, font_height;
44 guint term_paste_idle_id;
45 GdkAtom compound_text_atom;
47 char wintitle[sizeof(((Config *)0)->wintitle)];
48 char icontitle[sizeof(((Config *)0)->wintitle)];
51 static struct gui_data the_inst;
52 static struct gui_data *inst = &the_inst; /* so we always write `inst->' */
53 static int send_raw_mouse;
55 void ldisc_update(int echo, int edit)
58 * This is a stub in pterm. If I ever produce a Unix
59 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
60 * then it will require some termios manoeuvring analogous to
61 * that in the Windows plink.c, but here it's meaningless.
65 int askappend(char *filename)
68 * Logging in an xterm-alike is liable to be something you only
69 * do at serious diagnostic need. Hence, I'm going to take the
70 * easy option for now and assume we always want to overwrite
71 * log files. I can always make it properly configurable later.
76 void logevent(char *string)
79 * This is not a very helpful function: events are logged
80 * pretty much exclusively by the back end, and our pty back
81 * end is self-contained. So we need do nothing.
85 int font_dimension(int which) /* 0 for width, 1 for height */
88 return inst->font_height;
90 return inst->font_width;
94 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
95 * into a cooked one (SELECT, EXTEND, PASTE).
97 * In Unix, this is not configurable; the X button arrangement is
98 * rock-solid across all applications, everyone has a three-button
99 * mouse or a means of faking it, and there is no need to switch
100 * buttons around at all.
102 Mouse_Button translate_button(Mouse_Button button)
104 if (button == MBT_LEFT)
106 if (button == MBT_MIDDLE)
108 if (button == MBT_RIGHT)
110 return 0; /* shouldn't happen */
114 * Minimise or restore the window in response to a server-side
117 void set_iconic(int iconic)
120 * GTK 1.2 doesn't know how to do this.
122 #if GTK_CHECK_VERSION(2,0,0)
124 gtk_window_iconify(GTK_WINDOW(inst->window));
126 gtk_window_deiconify(GTK_WINDOW(inst->window));
131 * Move the window in response to a server-side request.
133 void move_window(int x, int y)
136 * I assume that when the GTK version of this call is available
137 * we should use it. Not sure how it differs from the GDK one,
140 #if GTK_CHECK_VERSION(2,0,0)
141 gtk_window_move(GTK_WINDOW(inst->window), x, y);
143 gdk_window_move(inst->window->window, x, y);
148 * Move the window to the top or bottom of the z-order in response
149 * to a server-side request.
151 void set_zorder(int top)
154 gdk_window_raise(inst->window->window);
156 gdk_window_lower(inst->window->window);
160 * Refresh the window in response to a server-side request.
162 void refresh_window(void)
168 * Maximise or restore the window in response to a server-side
171 void set_zoomed(int zoomed)
174 * GTK 1.2 doesn't know how to do this.
176 #if GTK_CHECK_VERSION(2,0,0)
178 gtk_window_maximize(GTK_WINDOW(inst->window));
180 gtk_window_unmaximize(GTK_WINDOW(inst->window));
185 * Report whether the window is iconic, for terminal reports.
189 return !gdk_window_is_viewable(inst->window->window);
193 * Report the window's position, for terminal reports.
195 void get_window_pos(int *x, int *y)
198 * I assume that when the GTK version of this call is available
199 * we should use it. Not sure how it differs from the GDK one,
202 #if GTK_CHECK_VERSION(2,0,0)
203 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
205 gdk_window_get_position(inst->window->window, x, y);
210 * Report the window's pixel size, for terminal reports.
212 void get_window_pixels(int *x, int *y)
215 * I assume that when the GTK version of this call is available
216 * we should use it. Not sure how it differs from the GDK one,
219 #if GTK_CHECK_VERSION(2,0,0)
220 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
222 gdk_window_get_size(inst->window->window, x, y);
227 * Return the window or icon title.
229 char *get_window_title(int icon)
231 return icon ? inst->wintitle : inst->icontitle;
234 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
237 * We could implement warn-on-close here if we really wanted
243 void show_mouseptr(int show)
245 if (!cfg.hide_mouseptr)
248 gdk_window_set_cursor(inst->area->window, inst->currcursor);
250 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
251 inst->mouseptr_visible = show;
254 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
256 struct gui_data *inst = (struct gui_data *)data;
257 int w, h, need_size = 0;
259 w = (event->width - 2*cfg.window_border) / inst->font_width;
260 h = (event->height - 2*cfg.window_border) / inst->font_height;
262 if (w != cfg.width || h != cfg.height) {
264 gdk_pixmap_unref(inst->pixmap);
274 inst->pixmap = gdk_pixmap_new(widget->window,
275 (cfg.width * inst->font_width +
276 2*cfg.window_border),
277 (cfg.height * inst->font_height +
278 2*cfg.window_border), -1);
280 gc = gdk_gc_new(inst->area->window);
281 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
282 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
283 cfg.width * inst->font_width + 2*cfg.window_border,
284 cfg.height * inst->font_height + 2*cfg.window_border);
289 term_size(h, w, cfg.savelines);
295 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
297 /* struct gui_data *inst = (struct gui_data *)data; */
300 * Pass the exposed rectangle to terminal.c, which will call us
301 * back to do the actual painting.
304 gdk_draw_pixmap(widget->window,
305 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
307 event->area.x, event->area.y,
308 event->area.x, event->area.y,
309 event->area.width, event->area.height);
314 #define KEY_PRESSED(k) \
315 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
317 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
319 /* struct gui_data *inst = (struct gui_data *)data; */
323 /* By default, nothing is generated. */
327 * If Alt is being released after typing an Alt+numberpad
328 * sequence, we should generate the code that was typed.
330 if (event->type == GDK_KEY_RELEASE &&
331 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
332 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
333 inst->alt_keycode >= 0) {
335 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
337 output[0] = inst->alt_keycode;
342 if (event->type == GDK_KEY_PRESS) {
346 printf("keypress: keyval = %04x, state = %08x; string =",
347 event->keyval, event->state);
348 for (i = 0; event->string[i]; i++)
349 printf(" %02x", (unsigned char) event->string[i]);
355 * NYI: Compose key (!!! requires Unicode faff before even trying)
359 * If Alt has just been pressed, we start potentially
360 * accumulating an Alt+numberpad code. We do this by
361 * setting alt_keycode to -1 (nothing yet but plausible).
363 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
364 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
365 inst->alt_keycode = -1;
366 goto done; /* this generates nothing else */
370 * If we're seeing a numberpad key press with Mod1 down,
371 * consider adding it to alt_keycode if that's sensible.
372 * Anything _else_ with Mod1 down cancels any possibility
373 * of an ALT keycode: we set alt_keycode to -2.
375 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
377 switch (event->keyval) {
378 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
379 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
380 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
381 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
382 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
383 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
384 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
385 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
386 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
387 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
390 inst->alt_keycode = -2; /* it's invalid */
393 printf("Adding digit %d to keycode %d", digit,
396 if (inst->alt_keycode == -1)
397 inst->alt_keycode = digit; /* one-digit code */
399 inst->alt_keycode = inst->alt_keycode * 10 + digit;
401 printf(" gives new code %d\n", inst->alt_keycode);
403 /* Having used this digit, we now do nothing more with it. */
409 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
412 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
413 term_scroll(0, -cfg.height/2);
416 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
417 term_scroll(0, +cfg.height/2);
422 * Neither does Shift-Ins.
424 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
429 /* ALT+things gives leading Escape. */
431 strncpy(output+1, event->string, 31);
433 end = strlen(output);
434 if (event->state & GDK_MOD1_MASK) {
436 if (end == 1) end = 0;
440 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
441 if (!event->string[0] && event->keyval == '`' &&
442 (event->state & GDK_CONTROL_MASK)) {
447 /* Control-Break is the same as Control-C */
448 if (event->keyval == GDK_Break &&
449 (event->state & GDK_CONTROL_MASK)) {
454 /* Control-2, Control-Space and Control-@ are NUL */
455 if (!event->string[0] &&
456 (event->keyval == ' ' || event->keyval == '2' ||
457 event->keyval == '@') &&
458 (event->state & (GDK_SHIFT_MASK |
459 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
464 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
465 if (!event->string[0] && event->keyval == ' ' &&
466 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
467 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
472 /* We don't let GTK tell us what Backspace is! We know better. */
473 if (event->keyval == GDK_BackSpace &&
474 !(event->state & GDK_SHIFT_MASK)) {
475 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
478 /* For Shift Backspace, do opposite of what is configured. */
479 if (event->keyval == GDK_BackSpace &&
480 (event->state & GDK_SHIFT_MASK)) {
481 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
485 /* Shift-Tab is ESC [ Z */
486 if (event->keyval == GDK_ISO_Left_Tab ||
487 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
488 end = 1 + sprintf(output+1, "\033[Z");
492 * NetHack keypad mode.
494 if (cfg.nethack_keypad) {
496 switch (event->keyval) {
497 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
498 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
499 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
500 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
501 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
502 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
503 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
504 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
505 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
509 if (event->state & GDK_SHIFT_MASK)
518 * Application keypad mode.
520 if (app_keypad_keys && !cfg.no_applic_k) {
522 switch (event->keyval) {
523 case GDK_Num_Lock: xkey = 'P'; break;
524 case GDK_KP_Divide: xkey = 'Q'; break;
525 case GDK_KP_Multiply: xkey = 'R'; break;
526 case GDK_KP_Subtract: xkey = 'S'; break;
528 * Keypad + is tricky. It covers a space that would
529 * be taken up on the VT100 by _two_ keys; so we
530 * let Shift select between the two. Worse still,
531 * in xterm function key mode we change which two...
534 if (cfg.funky_type == 2) {
535 if (event->state & GDK_SHIFT_MASK)
539 } else if (event->state & GDK_SHIFT_MASK)
544 case GDK_KP_Enter: xkey = 'M'; break;
545 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
546 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
547 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
548 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
549 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
550 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
551 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
552 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
553 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
554 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
555 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
559 if (xkey >= 'P' && xkey <= 'S')
560 end = 1 + sprintf(output+1, "\033%c", xkey);
562 end = 1 + sprintf(output+1, "\033?%c", xkey);
564 end = 1 + sprintf(output+1, "\033O%c", xkey);
570 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
571 * for integer decimal nn.)
573 * We also deal with the weird ones here. Linux VCs replace F1
574 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
575 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
580 switch (event->keyval) {
582 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
585 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
588 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
591 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
594 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
597 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
600 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
603 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
606 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
609 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
642 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
643 case GDK_Home: case GDK_KP_Home:
646 case GDK_Insert: case GDK_KP_Insert:
649 case GDK_Delete: case GDK_KP_Delete:
652 case GDK_End: case GDK_KP_End:
655 case GDK_Page_Up: case GDK_KP_Page_Up:
658 case GDK_Page_Down: case GDK_KP_Page_Down:
662 /* Reorder edit keys to physical order */
663 if (cfg.funky_type == 3 && code <= 6)
664 code = "\0\2\1\4\5\3\6"[code];
666 if (vt52_mode && code > 0 && code <= 6) {
667 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
671 if (cfg.funky_type == 5 && /* SCO function keys */
672 code >= 11 && code <= 34) {
673 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
675 switch (event->keyval) {
676 case GDK_F1: index = 0; break;
677 case GDK_F2: index = 1; break;
678 case GDK_F3: index = 2; break;
679 case GDK_F4: index = 3; break;
680 case GDK_F5: index = 4; break;
681 case GDK_F6: index = 5; break;
682 case GDK_F7: index = 6; break;
683 case GDK_F8: index = 7; break;
684 case GDK_F9: index = 8; break;
685 case GDK_F10: index = 9; break;
686 case GDK_F11: index = 10; break;
687 case GDK_F12: index = 11; break;
689 if (event->state & GDK_SHIFT_MASK) index += 12;
690 if (event->state & GDK_CONTROL_MASK) index += 24;
691 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
694 if (cfg.funky_type == 5 && /* SCO small keypad */
695 code >= 1 && code <= 6) {
696 char codes[] = "HL.FIG";
701 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
705 if ((vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
712 end = 1 + sprintf(output+1,
713 "\x1B%c", code + 'P' - 11 - offt);
715 end = 1 + sprintf(output+1,
716 "\x1BO%c", code + 'P' - 11 - offt);
719 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
720 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
723 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
725 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
727 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
730 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
731 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
735 end = 1 + sprintf(output+1, "\x1B[%d~", code);
741 * Cursor keys. (This includes the numberpad cursor keys,
742 * if we haven't already done them due to app keypad mode.)
744 * Here we also process un-numlocked un-appkeypadded KP5,
745 * which sends ESC [ G.
749 switch (event->keyval) {
750 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
751 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
752 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
753 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
754 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
758 * The arrow keys normally do ESC [ A and so on. In
759 * app cursor keys mode they do ESC O A instead.
760 * Ctrl toggles the two modes.
763 end = 1 + sprintf(output+1, "\033%c", xkey);
764 } else if (!app_cursor_keys ^
765 !(event->state & GDK_CONTROL_MASK)) {
766 end = 1 + sprintf(output+1, "\033O%c", xkey);
768 end = 1 + sprintf(output+1, "\033[%c", xkey);
781 printf("generating sequence:");
782 for (i = start; i < end; i++)
783 printf(" %02x", (unsigned char) output[i]);
787 ldisc_send(output+start, end-start, 1);
795 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
797 struct gui_data *inst = (struct gui_data *)data;
798 int shift, ctrl, alt, x, y, button, act;
802 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
806 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
811 shift = event->state & GDK_SHIFT_MASK;
812 ctrl = event->state & GDK_CONTROL_MASK;
813 alt = event->state & GDK_MOD1_MASK;
814 if (event->button == 1)
816 else if (event->button == 2)
818 else if (event->button == 3)
821 return FALSE; /* don't even know what button! */
823 switch (event->type) {
824 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
825 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
826 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
827 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
828 default: return FALSE; /* don't know this event type */
831 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
832 act != MA_CLICK && act != MA_RELEASE)
833 return TRUE; /* we ignore these in raw mouse mode */
835 x = (event->x - cfg.window_border) / inst->font_width;
836 y = (event->y - cfg.window_border) / inst->font_height;
838 term_mouse(button, act, x, y, shift, ctrl, alt);
843 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
845 struct gui_data *inst = (struct gui_data *)data;
846 int shift, ctrl, alt, x, y, button;
850 shift = event->state & GDK_SHIFT_MASK;
851 ctrl = event->state & GDK_CONTROL_MASK;
852 alt = event->state & GDK_MOD1_MASK;
853 if (event->state & GDK_BUTTON1_MASK)
855 else if (event->state & GDK_BUTTON2_MASK)
857 else if (event->state & GDK_BUTTON3_MASK)
860 return FALSE; /* don't even know what button! */
862 x = (event->x - cfg.window_border) / inst->font_width;
863 y = (event->y - cfg.window_border) / inst->font_height;
865 term_mouse(button, MA_DRAG, x, y, shift, ctrl, alt);
870 gint timer_func(gpointer data)
872 /* struct gui_data *inst = (struct gui_data *)data; */
873 extern int pty_child_is_dead(); /* declared in pty.c */
875 if (pty_child_is_dead()) {
877 * The primary child process died. We could keep the
878 * terminal open for remaining subprocesses to output to,
879 * but conventional wisdom seems to feel that that's the
880 * Wrong Thing for an xterm-alike, so we bail out now. This
881 * would be easy enough to change or make configurable if
892 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
894 /* struct gui_data *inst = (struct gui_data *)data; */
898 ret = read(sourcefd, buf, sizeof(buf));
901 * Clean termination condition is that either ret == 0, or ret
902 * < 0 and errno == EIO. Not sure why the latter, but it seems
905 if (ret == 0 || (ret < 0 && errno == EIO)) {
910 perror("read pty master");
914 from_backend(0, buf, ret);
919 void destroy(GtkWidget *widget, gpointer data)
924 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
926 has_focus = event->in;
934 * set or clear the "raw mouse message" mode
936 void set_raw_mouse_mode(int activate)
938 activate = activate && !cfg.no_mouse_rep;
939 send_raw_mouse = activate;
941 inst->currcursor = inst->rawcursor;
943 inst->currcursor = inst->textcursor;
944 show_mouseptr(inst->mouseptr_visible);
947 void request_resize(int w, int h)
949 int large_x, large_y;
950 int offset_x, offset_y;
952 GtkRequisition inner, outer;
955 * This is a heinous hack dreamed up by the gnome-terminal
956 * people to get around a limitation in gtk. The problem is
957 * that in order to set the size correctly we really need to be
958 * calling gtk_window_resize - but that needs to know the size
959 * of the _whole window_, not the drawing area. So what we do
960 * is to set an artificially huge size request on the drawing
961 * area, recompute the resulting size request on the window,
962 * and look at the difference between the two. That gives us
963 * the x and y offsets we need to translate drawing area size
964 * into window size for real, and then we call
969 * We start by retrieving the current size of the whole window.
970 * Adding a bit to _that_ will give us a value we can use as a
971 * bogus size request which guarantees to be bigger than the
972 * current size of the drawing area.
974 get_window_pixels(&large_x, &large_y);
978 #if GTK_CHECK_VERSION(2,0,0)
979 gtk_widget_set_size_request(inst->area, large_x, large_y);
981 gtk_widget_set_usize(inst->area, large_x, large_y);
983 gtk_widget_size_request(inst->area, &inner);
984 gtk_widget_size_request(inst->window, &outer);
986 offset_x = outer.width - inner.width;
987 offset_y = outer.height - inner.height;
989 area_x = inst->font_width * w + 2*cfg.window_border;
990 area_y = inst->font_height * h + 2*cfg.window_border;
993 * Now we must set the size request on the drawing area back to
994 * something sensible before we commit the real resize. Best
995 * way to do this, I think, is to set it to what the size is
996 * really going to end up being.
998 #if GTK_CHECK_VERSION(2,0,0)
999 gtk_widget_set_size_request(inst->area, area_x, area_y);
1001 gtk_widget_set_usize(inst->area, area_x, area_y);
1004 #if GTK_CHECK_VERSION(2,0,0)
1005 gtk_window_resize(GTK_WINDOW(inst->window),
1006 area_x + offset_x, area_y + offset_y);
1008 gdk_window_resize(inst->window->window,
1009 area_x + offset_x, area_y + offset_y);
1013 void real_palette_set(int n, int r, int g, int b)
1015 gboolean success[1];
1017 inst->cols[n].red = r * 0x0101;
1018 inst->cols[n].green = g * 0x0101;
1019 inst->cols[n].blue = b * 0x0101;
1021 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1022 FALSE, FALSE, success);
1024 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1028 void palette_set(int n, int r, int g, int b)
1030 static const int first[21] = {
1031 0, 2, 4, 6, 8, 10, 12, 14,
1032 1, 3, 5, 7, 9, 11, 13, 15,
1035 real_palette_set(first[n], r, g, b);
1037 real_palette_set(first[n] + 1, r, g, b);
1040 void palette_reset(void)
1042 /* This maps colour indices in cfg to those used in inst->cols. */
1043 static const int ww[] = {
1044 6, 7, 8, 9, 10, 11, 12, 13,
1045 14, 15, 16, 17, 18, 19, 20, 21,
1048 gboolean success[NCOLOURS];
1051 assert(lenof(ww) == NCOLOURS);
1053 if (!inst->colmap) {
1054 inst->colmap = gdk_colormap_get_system();
1056 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1059 for (i = 0; i < NCOLOURS; i++) {
1060 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1061 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1062 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1065 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1066 FALSE, FALSE, success);
1067 for (i = 0; i < NCOLOURS; i++) {
1069 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1070 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1074 void write_clip(wchar_t * data, int len, int must_deselect)
1076 if (inst->pasteout_data)
1077 sfree(inst->pasteout_data);
1078 inst->pasteout_data = smalloc(len);
1079 inst->pasteout_data_len = len;
1080 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1083 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1084 GDK_CURRENT_TIME)) {
1085 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1086 GDK_SELECTION_TYPE_STRING, 1);
1087 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1088 inst->compound_text_atom, 1);
1092 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1093 guint info, guint time_stamp, gpointer data)
1095 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1096 inst->pasteout_data, inst->pasteout_data_len);
1099 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1103 if (inst->pasteout_data)
1104 sfree(inst->pasteout_data);
1105 inst->pasteout_data = NULL;
1106 inst->pasteout_data_len = 0;
1110 void request_paste(void)
1113 * In Unix, pasting is asynchronous: all we can do at the
1114 * moment is to call gtk_selection_convert(), and when the data
1115 * comes back _then_ we can call term_do_paste().
1117 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1118 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1121 gint idle_paste_func(gpointer data); /* forward ref */
1123 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1126 if (seldata->length <= 0 ||
1127 seldata->type != GDK_SELECTION_TYPE_STRING)
1128 return; /* Nothing happens. */
1130 if (inst->pastein_data)
1131 sfree(inst->pastein_data);
1133 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1134 inst->pastein_data_len = seldata->length;
1135 mb_to_wc(0, 0, seldata->data, seldata->length,
1136 inst->pastein_data, inst->pastein_data_len);
1140 if (term_paste_pending())
1141 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1144 gint idle_paste_func(gpointer data)
1146 struct gui_data *inst = (struct gui_data *)data;
1148 if (term_paste_pending())
1151 gtk_idle_remove(inst->term_paste_idle_id);
1157 void get_clip(wchar_t ** p, int *len)
1160 *p = inst->pastein_data;
1161 *len = inst->pastein_data_len;
1165 void set_title(char *title)
1167 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1168 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1169 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1172 void set_icon(char *title)
1174 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1175 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1176 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1179 void set_sbar(int total, int start, int page)
1183 inst->sbar_adjust->lower = 0;
1184 inst->sbar_adjust->upper = total;
1185 inst->sbar_adjust->value = start;
1186 inst->sbar_adjust->page_size = page;
1187 inst->sbar_adjust->step_increment = 1;
1188 inst->sbar_adjust->page_increment = page/2;
1189 inst->ignore_sbar = TRUE;
1190 gtk_adjustment_changed(inst->sbar_adjust);
1191 inst->ignore_sbar = FALSE;
1194 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1198 if (!inst->ignore_sbar)
1199 term_scroll(1, (int)adj->value);
1202 void sys_cursor(int x, int y)
1205 * This is meaningless under X.
1214 int CharWidth(Context ctx, int uc)
1217 * Under X, any fixed-width font really _is_ fixed-width.
1218 * Double-width characters will be dealt with using a separate
1219 * font. For the moment we can simply return 1.
1224 Context get_ctx(void)
1227 if (!inst->area->window)
1229 gc = gdk_gc_new(inst->area->window);
1233 void free_ctx(Context ctx)
1235 GdkGC *gc = (GdkGC *)ctx;
1240 * Draw a line of text in the window, at given character
1241 * coordinates, in given attributes.
1243 * We are allowed to fiddle with the contents of `text'.
1245 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1246 unsigned long attr, int lattr)
1248 int nfg, nbg, t, fontid, shadow;
1249 GdkGC *gc = (GdkGC *)ctx;
1253 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1256 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1257 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1258 if (attr & ATTR_REVERSE) {
1263 if (cfg.bold_colour && (attr & ATTR_BOLD))
1265 if (cfg.bold_colour && (attr & ATTR_BLINK))
1267 if (attr & TATTR_ACTCURS) {
1272 fontid = shadow = 0;
1273 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1280 if (lattr != LATTR_NORM) {
1284 if (x + len*2 > cols)
1285 len = (cols-x)/2; /* trim to LH half */
1288 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1289 gdk_draw_rectangle(inst->pixmap, gc, 1,
1290 x*inst->font_width+cfg.window_border,
1291 y*inst->font_height+cfg.window_border,
1292 len*inst->font_width, inst->font_height);
1294 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1295 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1296 x*inst->font_width+cfg.window_border,
1297 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1301 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1302 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1303 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1307 if (attr & ATTR_UNDER) {
1308 int uheight = inst->fonts[0]->ascent + 1;
1309 if (uheight >= inst->font_height)
1310 uheight = inst->font_height - 1;
1311 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1312 y*inst->font_height + uheight + cfg.window_border,
1313 (x+len)*inst->font_width-1+cfg.window_border,
1314 y*inst->font_height + uheight + cfg.window_border);
1317 if (lattr != LATTR_NORM) {
1319 * I can't find any plausible StretchBlt equivalent in the
1320 * X server, so I'm going to do this the slow and painful
1321 * way. This will involve repeated calls to
1322 * gdk_draw_pixmap() to stretch the text horizontally. It's
1323 * O(N^2) in time and O(N) in network bandwidth, but you
1324 * try thinking of a better way. :-(
1327 for (i = 0; i < len * inst->font_width; i++) {
1328 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1329 x*inst->font_width+cfg.window_border + 2*i,
1330 y*inst->font_height+cfg.window_border,
1331 x*inst->font_width+cfg.window_border + 2*i+1,
1332 y*inst->font_height+cfg.window_border,
1333 len * inst->font_width - i, inst->font_height);
1336 if (lattr != LATTR_WIDE) {
1338 /* Now stretch vertically, in the same way. */
1339 if (lattr == LATTR_BOT)
1343 for (i = 0; i < inst->font_height; i+=2) {
1344 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1345 x*inst->font_width+cfg.window_border,
1346 y*inst->font_height+cfg.window_border+dt*i+db,
1347 x*inst->font_width+cfg.window_border,
1348 y*inst->font_height+cfg.window_border+dt*(i+1),
1349 len * inst->font_width, inst->font_height-i-1);
1355 void do_text(Context ctx, int x, int y, char *text, int len,
1356 unsigned long attr, int lattr)
1358 GdkGC *gc = (GdkGC *)ctx;
1360 do_text_internal(ctx, x, y, text, len, attr, lattr);
1362 if (lattr != LATTR_NORM) {
1366 if (x + len*2 > cols)
1367 len = (cols-x)/2; /* trim to LH half */
1371 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1372 x*inst->font_width+cfg.window_border,
1373 y*inst->font_height+cfg.window_border,
1374 x*inst->font_width+cfg.window_border,
1375 y*inst->font_height+cfg.window_border,
1376 len*inst->font_width, inst->font_height);
1379 void do_cursor(Context ctx, int x, int y, char *text, int len,
1380 unsigned long attr, int lattr)
1383 GdkGC *gc = (GdkGC *)ctx;
1385 if (attr & TATTR_PASCURS) {
1386 attr &= ~TATTR_PASCURS;
1390 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1391 attr &= ~TATTR_ACTCURS;
1393 do_text_internal(ctx, x, y, text, len, attr, lattr);
1395 if (lattr != LATTR_NORM) {
1399 if (x + len*2 > cols)
1400 len = (cols-x)/2; /* trim to LH half */
1404 if (cfg.cursor_type == 0) {
1406 * An active block cursor will already have been done by
1407 * the above do_text call, so we only need to do anything
1411 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1412 gdk_draw_rectangle(inst->pixmap, gc, 0,
1413 x*inst->font_width+cfg.window_border,
1414 y*inst->font_height+cfg.window_border,
1415 len*inst->font_width-1, inst->font_height-1);
1419 int startx, starty, dx, dy, length, i;
1423 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1424 char_width = 2*inst->font_width;
1426 char_width = inst->font_width;
1428 if (cfg.cursor_type == 1) {
1429 uheight = inst->fonts[0]->ascent + 1;
1430 if (uheight >= inst->font_height)
1431 uheight = inst->font_height - 1;
1433 startx = x * inst->font_width + cfg.window_border;
1434 starty = y * inst->font_height + cfg.window_border + uheight;
1437 length = len * char_width;
1440 if (attr & TATTR_RIGHTCURS)
1441 xadjust = char_width - 1;
1442 startx = x * inst->font_width + cfg.window_border + xadjust;
1443 starty = y * inst->font_height + cfg.window_border;
1446 length = inst->font_height;
1449 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1451 for (i = 0; i < length; i++) {
1453 gdk_draw_point(inst->pixmap, gc, startx, starty);
1459 gdk_draw_line(inst->pixmap, gc, startx, starty,
1460 startx + (length-1) * dx, starty + (length-1) * dy);
1464 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1465 x*inst->font_width+cfg.window_border,
1466 y*inst->font_height+cfg.window_border,
1467 x*inst->font_width+cfg.window_border,
1468 y*inst->font_height+cfg.window_border,
1469 len*inst->font_width, inst->font_height);
1472 GdkCursor *make_mouse_ptr(int cursor_val)
1475 * Truly hideous hack: GTK doesn't allow us to set the mouse
1476 * cursor foreground and background colours unless we've _also_
1477 * created our own cursor from bitmaps. Therefore, I need to
1478 * load the `cursor' font and draw glyphs from it on to
1479 * pixmaps, in order to construct my cursors with the fg and bg
1480 * I want. This is a gross hack, but it's more self-contained
1481 * than linking in Xlib to find the X window handle to
1482 * inst->area and calling XRecolorCursor, and it's more
1483 * futureproof than hard-coding the shapes as bitmap arrays.
1485 static GdkFont *cursor_font = NULL;
1486 GdkPixmap *source, *mask;
1488 GdkColor cfg = { 0, 65535, 65535, 65535 };
1489 GdkColor cbg = { 0, 0, 0, 0 };
1490 GdkColor dfg = { 1, 65535, 65535, 65535 };
1491 GdkColor dbg = { 0, 0, 0, 0 };
1494 gint lb, rb, wid, asc, desc, w, h, x, y;
1496 if (cursor_val == -2) {
1497 gdk_font_unref(cursor_font);
1501 if (cursor_val >= 0 && !cursor_font)
1502 cursor_font = gdk_font_load("cursor");
1505 * Get the text extent of the cursor in question. We use the
1506 * mask character for this, because it's typically slightly
1507 * bigger than the main character.
1509 if (cursor_val >= 0) {
1511 text[0] = (char)cursor_val + 1;
1512 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1513 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1519 source = gdk_pixmap_new(NULL, w, h, 1);
1520 mask = gdk_pixmap_new(NULL, w, h, 1);
1523 * Draw the mask character on the mask pixmap.
1525 gc = gdk_gc_new(mask);
1526 gdk_gc_set_foreground(gc, &dbg);
1527 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1528 if (cursor_val >= 0) {
1530 text[0] = (char)cursor_val + 1;
1531 gdk_gc_set_foreground(gc, &dfg);
1532 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1537 * Draw the main character on the source pixmap.
1539 gc = gdk_gc_new(source);
1540 gdk_gc_set_foreground(gc, &dbg);
1541 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1542 if (cursor_val >= 0) {
1544 text[0] = (char)cursor_val;
1545 gdk_gc_set_foreground(gc, &dfg);
1546 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1551 * Create the cursor.
1553 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1558 gdk_pixmap_unref(source);
1559 gdk_pixmap_unref(mask);
1564 void modalfatalbox(char *p, ...)
1567 fprintf(stderr, "FATAL ERROR: ");
1569 vfprintf(stderr, p, ap);
1571 fputc('\n', stderr);
1575 char *get_x_display(void)
1577 return gdk_get_display();
1580 char *app_name = "pterm";
1582 int do_cmdline(int argc, char **argv, int do_everything)
1585 extern char **pty_argv; /* declared in pty.c */
1588 * Macros to make argument handling easier.
1590 #define EXPECTS_ARG do { \
1591 if (--argc <= 0) { \
1593 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1597 #define SECOND_PASS_ONLY do { \
1598 if (!do_everything) continue; \
1608 while (--argc > 0) {
1610 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1613 strncpy(cfg.font, val, sizeof(cfg.font));
1614 cfg.font[sizeof(cfg.font)-1] = '\0';
1616 } else if (!strcmp(p, "-fb")) {
1619 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1620 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1622 } else if (!strcmp(p, "-geometry")) {
1623 int flags, x, y, w, h;
1627 flags = XParseGeometry(val, &x, &y, &w, &h);
1628 if (flags & WidthValue)
1630 if (flags & HeightValue)
1634 * Apparently setting the initial window position is
1635 * difficult in GTK 1.2. Not entirely sure why this
1636 * should be. 2.0 has gtk_window_parse_geometry(),
1637 * which would help... For the moment, though, I can't
1638 * be bothered with this.
1641 } else if (!strcmp(p, "-sl")) {
1644 cfg.savelines = atoi(val);
1646 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1647 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1648 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1653 if (!gdk_color_parse(val, &col)) {
1655 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1658 index = (!strcmp(p, "-fg") ? 0 :
1659 !strcmp(p, "-bg") ? 2 :
1660 !strcmp(p, "-bfg") ? 1 :
1661 !strcmp(p, "-bbg") ? 3 :
1662 !strcmp(p, "-cfg") ? 4 :
1663 !strcmp(p, "-cbg") ? 5 : -1);
1664 assert(index != -1);
1665 cfg.colours[index][0] = col.red / 256;
1666 cfg.colours[index][1] = col.green / 256;
1667 cfg.colours[index][2] = col.blue / 256;
1670 } else if (!strcmp(p, "-e")) {
1671 /* This option swallows all further arguments. */
1677 pty_argv = smalloc((argc+1) * sizeof(char *));
1679 for (i = 0; i < argc; i++)
1680 pty_argv[i] = argv[i];
1681 pty_argv[argc] = NULL;
1682 break; /* finished command-line processing */
1684 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1686 } else if (!strcmp(p, "-T")) {
1689 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1690 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1692 } else if (!strcmp(p, "-log")) {
1695 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1696 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1697 cfg.logtype = LGTYP_DEBUG;
1699 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1703 } else if (!strcmp(p, "-ut")) {
1707 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1709 cfg.login_shell = 0;
1711 } else if (!strcmp(p, "-ls")) {
1713 cfg.login_shell = 1;
1715 } else if (!strcmp(p, "-nethack")) {
1717 cfg.nethack_keypad = 1;
1719 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1723 } else if (!strcmp(p, "-sb")) {
1727 } else if (!strcmp(p, "-name")) {
1731 } else if (!strcmp(p, "-xrm")) {
1733 provide_xrm_string(val);
1741 int main(int argc, char **argv)
1743 extern int pty_master_fd; /* declared in pty.c */
1744 extern void pty_pre_init(void); /* declared in pty.c */
1748 gtk_init(&argc, &argv);
1750 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
1752 do_defaults(NULL, &cfg);
1753 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
1756 inst->fonts[0] = gdk_font_load(cfg.font);
1757 if (!inst->fonts[0]) {
1758 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1761 if (cfg.boldfont[0]) {
1762 inst->fonts[1] = gdk_font_load(cfg.boldfont);
1763 if (!inst->fonts[1]) {
1764 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1769 inst->fonts[1] = NULL;
1771 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1772 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1774 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1778 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1780 if (cfg.wintitle[0])
1781 set_title(cfg.wintitle);
1786 * Set up the colour map.
1790 inst->area = gtk_drawing_area_new();
1791 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1792 inst->font_width * cfg.width + 2*cfg.window_border,
1793 inst->font_height * cfg.height + 2*cfg.window_border);
1794 if (cfg.scrollbar) {
1795 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1796 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1798 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1799 if (cfg.scrollbar) {
1800 if (cfg.scrollbar_on_left)
1801 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1803 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1805 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1807 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1811 geom.min_width = inst->font_width + 2*cfg.window_border;
1812 geom.min_height = inst->font_height + 2*cfg.window_border;
1813 geom.max_width = geom.max_height = -1;
1814 geom.base_width = 2*cfg.window_border;
1815 geom.base_height = 2*cfg.window_border;
1816 geom.width_inc = inst->font_width;
1817 geom.height_inc = inst->font_height;
1818 geom.min_aspect = geom.max_aspect = 0;
1819 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1820 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1821 GDK_HINT_RESIZE_INC);
1824 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1825 GTK_SIGNAL_FUNC(destroy), inst);
1826 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1827 GTK_SIGNAL_FUNC(delete_window), inst);
1828 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1829 GTK_SIGNAL_FUNC(key_event), inst);
1830 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
1831 GTK_SIGNAL_FUNC(key_event), inst);
1832 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1833 GTK_SIGNAL_FUNC(focus_event), inst);
1834 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1835 GTK_SIGNAL_FUNC(focus_event), inst);
1836 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1837 GTK_SIGNAL_FUNC(configure_area), inst);
1838 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1839 GTK_SIGNAL_FUNC(expose_area), inst);
1840 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1841 GTK_SIGNAL_FUNC(button_event), inst);
1842 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1843 GTK_SIGNAL_FUNC(button_event), inst);
1844 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1845 GTK_SIGNAL_FUNC(motion_event), inst);
1846 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1847 GTK_SIGNAL_FUNC(selection_received), inst);
1848 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1849 GTK_SIGNAL_FUNC(selection_get), inst);
1850 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1851 GTK_SIGNAL_FUNC(selection_clear), inst);
1853 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1854 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
1855 gtk_timeout_add(20, timer_func, inst);
1856 gtk_widget_add_events(GTK_WIDGET(inst->area),
1857 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1858 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1859 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
1861 gtk_widget_show(inst->area);
1863 gtk_widget_show(inst->sbar);
1864 gtk_widget_show(GTK_WIDGET(inst->hbox));
1865 gtk_widget_show(inst->window);
1867 inst->textcursor = make_mouse_ptr(GDK_XTERM);
1868 inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
1869 inst->blankcursor = make_mouse_ptr(-1);
1870 make_mouse_ptr(-2); /* clean up cursor font */
1871 inst->currcursor = inst->textcursor;
1874 back = &pty_backend;
1875 back->init(NULL, 0, NULL, 0);
1877 gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
1880 term_size(cfg.height, cfg.width, cfg.savelines);