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 <gdk/gdkkeysyms.h>
17 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
20 #define CAT2(x,y) x ## y
21 #define CAT(x,y) CAT2(x,y)
22 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
24 #define NCOLOURS (lenof(((Config *)0)->colours))
27 GtkWidget *window, *area, *sbar;
29 GtkAdjustment *sbar_adjust;
31 GdkFont *fonts[2]; /* normal and bold (for now!) */
32 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
33 GdkColor cols[NCOLOURS];
35 wchar_t *pastein_data;
38 int pasteout_data_len;
39 int font_width, font_height;
42 guint term_paste_idle_id;
43 GdkAtom compound_text_atom;
45 char wintitle[sizeof(((Config *)0)->wintitle)];
46 char icontitle[sizeof(((Config *)0)->wintitle)];
49 static struct gui_data the_inst;
50 static struct gui_data *inst = &the_inst; /* so we always write `inst->' */
51 static int send_raw_mouse;
53 void ldisc_update(int echo, int edit)
56 * This is a stub in pterm. If I ever produce a Unix
57 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
58 * then it will require some termios manoeuvring analogous to
59 * that in the Windows plink.c, but here it's meaningless.
63 int askappend(char *filename)
66 * Logging in an xterm-alike is liable to be something you only
67 * do at serious diagnostic need. Hence, I'm going to take the
68 * easy option for now and assume we always want to overwrite
69 * log files. I can always make it properly configurable later.
74 void logevent(char *string)
77 * This is not a very helpful function: events are logged
78 * pretty much exclusively by the back end, and our pty back
79 * end is self-contained. So we need do nothing.
83 int font_dimension(int which) /* 0 for width, 1 for height */
86 return inst->font_height;
88 return inst->font_width;
92 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
93 * into a cooked one (SELECT, EXTEND, PASTE).
95 * In Unix, this is not configurable; the X button arrangement is
96 * rock-solid across all applications, everyone has a three-button
97 * mouse or a means of faking it, and there is no need to switch
98 * buttons around at all.
100 Mouse_Button translate_button(Mouse_Button button)
102 if (button == MBT_LEFT)
104 if (button == MBT_MIDDLE)
106 if (button == MBT_RIGHT)
108 return 0; /* shouldn't happen */
112 * Minimise or restore the window in response to a server-side
115 void set_iconic(int iconic)
118 * GTK 1.2 doesn't know how to do this.
120 #if GTK_CHECK_VERSION(2,0,0)
122 gtk_window_iconify(GTK_WINDOW(inst->window));
124 gtk_window_deiconify(GTK_WINDOW(inst->window));
129 * Move the window in response to a server-side request.
131 void move_window(int x, int y)
134 * I assume that when the GTK version of this call is available
135 * we should use it. Not sure how it differs from the GDK one,
138 #if GTK_CHECK_VERSION(2,0,0)
139 gtk_window_move(GTK_WINDOW(inst->window), x, y);
141 gdk_window_move(inst->window->window, x, y);
146 * Move the window to the top or bottom of the z-order in response
147 * to a server-side request.
149 void set_zorder(int top)
152 gdk_window_raise(inst->window->window);
154 gdk_window_lower(inst->window->window);
158 * Refresh the window in response to a server-side request.
160 void refresh_window(void)
166 * Maximise or restore the window in response to a server-side
169 void set_zoomed(int zoomed)
172 * GTK 1.2 doesn't know how to do this.
174 #if GTK_CHECK_VERSION(2,0,0)
176 gtk_window_maximize(GTK_WINDOW(inst->window));
178 gtk_window_unmaximize(GTK_WINDOW(inst->window));
183 * Report whether the window is iconic, for terminal reports.
187 return !gdk_window_is_viewable(inst->window->window);
191 * Report the window's position, for terminal reports.
193 void get_window_pos(int *x, int *y)
196 * I assume that when the GTK version of this call is available
197 * we should use it. Not sure how it differs from the GDK one,
200 #if GTK_CHECK_VERSION(2,0,0)
201 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
203 gdk_window_get_position(inst->window->window, x, y);
208 * Report the window's pixel size, for terminal reports.
210 void get_window_pixels(int *x, int *y)
213 * I assume that when the GTK version of this call is available
214 * we should use it. Not sure how it differs from the GDK one,
217 #if GTK_CHECK_VERSION(2,0,0)
218 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
220 gdk_window_get_size(inst->window->window, x, y);
225 * Return the window or icon title.
227 char *get_window_title(int icon)
229 return icon ? inst->wintitle : inst->icontitle;
232 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
235 * We could implement warn-on-close here if we really wanted
241 void show_mouseptr(int show)
243 if (!cfg.hide_mouseptr)
246 gdk_window_set_cursor(inst->area->window, inst->currcursor);
248 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
249 inst->mouseptr_visible = show;
252 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
254 struct gui_data *inst = (struct gui_data *)data;
255 int w, h, need_size = 0;
257 w = (event->width - 2*cfg.window_border) / inst->font_width;
258 h = (event->height - 2*cfg.window_border) / inst->font_height;
260 if (w != cfg.width || h != cfg.height) {
262 gdk_pixmap_unref(inst->pixmap);
272 inst->pixmap = gdk_pixmap_new(widget->window,
273 (cfg.width * inst->font_width +
274 2*cfg.window_border),
275 (cfg.height * inst->font_height +
276 2*cfg.window_border), -1);
278 gc = gdk_gc_new(inst->area->window);
279 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
280 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
281 cfg.width * inst->font_width + 2*cfg.window_border,
282 cfg.height * inst->font_height + 2*cfg.window_border);
287 term_size(h, w, cfg.savelines);
293 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
295 /* struct gui_data *inst = (struct gui_data *)data; */
298 * Pass the exposed rectangle to terminal.c, which will call us
299 * back to do the actual painting.
302 gdk_draw_pixmap(widget->window,
303 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
305 event->area.x, event->area.y,
306 event->area.x, event->area.y,
307 event->area.width, event->area.height);
312 #define KEY_PRESSED(k) \
313 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
315 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
317 /* struct gui_data *inst = (struct gui_data *)data; */
321 /* By default, nothing is generated. */
325 * If Alt is being released after typing an Alt+numberpad
326 * sequence, we should generate the code that was typed.
328 if (event->type == GDK_KEY_RELEASE &&
329 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
330 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
331 inst->alt_keycode >= 0) {
333 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
335 output[0] = inst->alt_keycode;
340 if (event->type == GDK_KEY_PRESS) {
344 printf("keypress: keyval = %04x, state = %08x; string =",
345 event->keyval, event->state);
346 for (i = 0; event->string[i]; i++)
347 printf(" %02x", (unsigned char) event->string[i]);
353 * NYI: Compose key (!!! requires Unicode faff before even trying)
357 * If Alt has just been pressed, we start potentially
358 * accumulating an Alt+numberpad code. We do this by
359 * setting alt_keycode to -1 (nothing yet but plausible).
361 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
362 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
363 inst->alt_keycode = -1;
364 goto done; /* this generates nothing else */
368 * If we're seeing a numberpad key press with Mod1 down,
369 * consider adding it to alt_keycode if that's sensible.
370 * Anything _else_ with Mod1 down cancels any possibility
371 * of an ALT keycode: we set alt_keycode to -2.
373 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
375 switch (event->keyval) {
376 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
377 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
378 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
379 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
380 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
381 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
382 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
383 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
384 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
385 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
388 inst->alt_keycode = -2; /* it's invalid */
391 printf("Adding digit %d to keycode %d", digit,
394 if (inst->alt_keycode == -1)
395 inst->alt_keycode = digit; /* one-digit code */
397 inst->alt_keycode = inst->alt_keycode * 10 + digit;
399 printf(" gives new code %d\n", inst->alt_keycode);
401 /* Having used this digit, we now do nothing more with it. */
407 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
410 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
411 term_scroll(0, -cfg.height/2);
414 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
415 term_scroll(0, +cfg.height/2);
420 * Neither does Shift-Ins.
422 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
427 /* ALT+things gives leading Escape. */
429 strncpy(output+1, event->string, 31);
431 end = strlen(output);
432 if (event->state & GDK_MOD1_MASK)
437 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
438 if (!event->string[0] && event->keyval == '`' &&
439 (event->state & GDK_CONTROL_MASK)) {
444 /* Control-Break is the same as Control-C */
445 if (event->keyval == GDK_Break &&
446 (event->state & GDK_CONTROL_MASK)) {
451 /* Control-2, Control-Space and Control-@ are NUL */
452 if (!event->string[0] &&
453 (event->keyval == ' ' || event->keyval == '2' ||
454 event->keyval == '@') &&
455 (event->state & (GDK_SHIFT_MASK |
456 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
461 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
462 if (!event->string[0] && event->keyval == ' ' &&
463 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
464 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
469 /* We don't let GTK tell us what Backspace is! We know better. */
470 if (event->keyval == GDK_BackSpace &&
471 !(event->state & GDK_SHIFT_MASK)) {
472 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
476 /* Shift-Tab is ESC [ Z */
477 if (event->keyval == GDK_ISO_Left_Tab ||
478 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
479 end = 1 + sprintf(output+1, "\033[Z");
483 * NetHack keypad mode.
485 if (cfg.nethack_keypad) {
487 switch (event->keyval) {
488 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
489 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
490 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
491 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
492 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
493 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
494 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
495 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
496 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
500 if (event->state & GDK_SHIFT_MASK)
509 * Application keypad mode.
511 if (app_keypad_keys && !cfg.no_applic_k) {
513 switch (event->keyval) {
514 case GDK_Num_Lock: xkey = 'P'; break;
515 case GDK_KP_Divide: xkey = 'Q'; break;
516 case GDK_KP_Multiply: xkey = 'R'; break;
517 case GDK_KP_Subtract: xkey = 'S'; break;
519 * Keypad + is tricky. It covers a space that would
520 * be taken up on the VT100 by _two_ keys; so we
521 * let Shift select between the two. Worse still,
522 * in xterm function key mode we change which two...
525 if (cfg.funky_type == 2) {
526 if (event->state & GDK_SHIFT_MASK)
530 } else if (event->state & GDK_SHIFT_MASK)
535 case GDK_KP_Enter: xkey = 'M'; break;
536 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
537 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
538 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
539 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
540 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
541 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
542 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
543 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
544 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
545 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
546 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
550 if (xkey >= 'P' && xkey <= 'S')
551 end = 1 + sprintf(output+1, "\033%c", xkey);
553 end = 1 + sprintf(output+1, "\033?%c", xkey);
555 end = 1 + sprintf(output+1, "\033O%c", xkey);
561 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
562 * for integer decimal nn.)
564 * We also deal with the weird ones here. Linux VCs replace F1
565 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
566 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
571 switch (event->keyval) {
573 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
576 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
579 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
582 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
585 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
588 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
591 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
594 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
597 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
600 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
633 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
634 case GDK_Home: case GDK_KP_Home:
637 case GDK_Insert: case GDK_KP_Insert:
640 case GDK_Delete: case GDK_KP_Delete:
643 case GDK_End: case GDK_KP_End:
646 case GDK_Page_Up: case GDK_KP_Page_Up:
649 case GDK_Page_Down: case GDK_KP_Page_Down:
653 /* Reorder edit keys to physical order */
654 if (cfg.funky_type == 3 && code <= 6)
655 code = "\0\2\1\4\5\3\6"[code];
657 if (vt52_mode && code > 0 && code <= 6) {
658 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
662 if (cfg.funky_type == 5 && /* SCO function keys */
663 code >= 11 && code <= 34) {
664 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
666 switch (event->keyval) {
667 case GDK_F1: index = 0; break;
668 case GDK_F2: index = 1; break;
669 case GDK_F3: index = 2; break;
670 case GDK_F4: index = 3; break;
671 case GDK_F5: index = 4; break;
672 case GDK_F6: index = 5; break;
673 case GDK_F7: index = 6; break;
674 case GDK_F8: index = 7; break;
675 case GDK_F9: index = 8; break;
676 case GDK_F10: index = 9; break;
677 case GDK_F11: index = 10; break;
678 case GDK_F12: index = 11; break;
680 if (event->state & GDK_SHIFT_MASK) index += 12;
681 if (event->state & GDK_CONTROL_MASK) index += 24;
682 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
685 if (cfg.funky_type == 5 && /* SCO small keypad */
686 code >= 1 && code <= 6) {
687 char codes[] = "HL.FIG";
692 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
696 if ((vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
703 end = 1 + sprintf(output+1,
704 "\x1B%c", code + 'P' - 11 - offt);
706 end = 1 + sprintf(output+1,
707 "\x1BO%c", code + 'P' - 11 - offt);
710 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
711 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
714 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
716 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
718 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
721 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
722 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
726 end = 1 + sprintf(output+1, "\x1B[%d~", code);
732 * Cursor keys. (This includes the numberpad cursor keys,
733 * if we haven't already done them due to app keypad mode.)
735 * Here we also process un-numlocked un-appkeypadded KP5,
736 * which sends ESC [ G.
740 switch (event->keyval) {
741 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
742 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
743 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
744 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
745 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
749 * The arrow keys normally do ESC [ A and so on. In
750 * app cursor keys mode they do ESC O A instead.
751 * Ctrl toggles the two modes.
754 end = 1 + sprintf(output+1, "\033%c", xkey);
755 } else if (!app_cursor_keys ^
756 !(event->state & GDK_CONTROL_MASK)) {
757 end = 1 + sprintf(output+1, "\033O%c", xkey);
759 end = 1 + sprintf(output+1, "\033[%c", xkey);
772 printf("generating sequence:");
773 for (i = start; i < end; i++)
774 printf(" %02x", (unsigned char) output[i]);
778 ldisc_send(output+start, end-start, 1);
786 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
788 struct gui_data *inst = (struct gui_data *)data;
789 int shift, ctrl, alt, x, y, button, act;
793 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
797 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
802 shift = event->state & GDK_SHIFT_MASK;
803 ctrl = event->state & GDK_CONTROL_MASK;
804 alt = event->state & GDK_MOD1_MASK;
805 if (event->button == 1)
807 else if (event->button == 2)
809 else if (event->button == 3)
812 return FALSE; /* don't even know what button! */
814 switch (event->type) {
815 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
816 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
817 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
818 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
819 default: return FALSE; /* don't know this event type */
822 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
823 act != MA_CLICK && act != MA_RELEASE)
824 return TRUE; /* we ignore these in raw mouse mode */
826 x = (event->x - cfg.window_border) / inst->font_width;
827 y = (event->y - cfg.window_border) / inst->font_height;
829 term_mouse(button, act, x, y, shift, ctrl, alt);
834 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
836 struct gui_data *inst = (struct gui_data *)data;
837 int shift, ctrl, alt, x, y, button;
841 shift = event->state & GDK_SHIFT_MASK;
842 ctrl = event->state & GDK_CONTROL_MASK;
843 alt = event->state & GDK_MOD1_MASK;
844 if (event->state & GDK_BUTTON1_MASK)
846 else if (event->state & GDK_BUTTON2_MASK)
848 else if (event->state & GDK_BUTTON3_MASK)
851 return FALSE; /* don't even know what button! */
853 x = (event->x - cfg.window_border) / inst->font_width;
854 y = (event->y - cfg.window_border) / inst->font_height;
856 term_mouse(button, MA_DRAG, x, y, shift, ctrl, alt);
861 gint timer_func(gpointer data)
863 /* struct gui_data *inst = (struct gui_data *)data; */
864 extern int pty_child_is_dead(); /* declared in pty.c */
866 if (pty_child_is_dead()) {
868 * The primary child process died. We could keep the
869 * terminal open for remaining subprocesses to output to,
870 * but conventional wisdom seems to feel that that's the
871 * Wrong Thing for an xterm-alike, so we bail out now. This
872 * would be easy enough to change or make configurable if
883 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
885 /* struct gui_data *inst = (struct gui_data *)data; */
889 ret = read(sourcefd, buf, sizeof(buf));
892 * Clean termination condition is that either ret == 0, or ret
893 * < 0 and errno == EIO. Not sure why the latter, but it seems
896 if (ret == 0 || (ret < 0 && errno == EIO)) {
901 perror("read pty master");
905 from_backend(0, buf, ret);
910 void destroy(GtkWidget *widget, gpointer data)
915 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
917 has_focus = event->in;
925 * set or clear the "raw mouse message" mode
927 void set_raw_mouse_mode(int activate)
929 activate = activate && !cfg.no_mouse_rep;
930 send_raw_mouse = activate;
932 inst->currcursor = inst->rawcursor;
934 inst->currcursor = inst->textcursor;
935 show_mouseptr(inst->mouseptr_visible);
938 void request_resize(int w, int h)
940 int large_x, large_y;
941 int offset_x, offset_y;
943 GtkRequisition inner, outer;
946 * This is a heinous hack dreamed up by the gnome-terminal
947 * people to get around a limitation in gtk. The problem is
948 * that in order to set the size correctly we really need to be
949 * calling gtk_window_resize - but that needs to know the size
950 * of the _whole window_, not the drawing area. So what we do
951 * is to set an artificially huge size request on the drawing
952 * area, recompute the resulting size request on the window,
953 * and look at the difference between the two. That gives us
954 * the x and y offsets we need to translate drawing area size
955 * into window size for real, and then we call
960 * We start by retrieving the current size of the whole window.
961 * Adding a bit to _that_ will give us a value we can use as a
962 * bogus size request which guarantees to be bigger than the
963 * current size of the drawing area.
965 get_window_pixels(&large_x, &large_y);
969 #if GTK_CHECK_VERSION(2,0,0)
970 gtk_widget_set_size_request(inst->area, large_x, large_y);
972 gtk_widget_set_usize(inst->area, large_x, large_y);
974 gtk_widget_size_request(inst->area, &inner);
975 gtk_widget_size_request(inst->window, &outer);
977 offset_x = outer.width - inner.width;
978 offset_y = outer.height - inner.height;
980 area_x = inst->font_width * w + 2*cfg.window_border;
981 area_y = inst->font_height * h + 2*cfg.window_border;
984 * Now we must set the size request on the drawing area back to
985 * something sensible before we commit the real resize. Best
986 * way to do this, I think, is to set it to what the size is
987 * really going to end up being.
989 #if GTK_CHECK_VERSION(2,0,0)
990 gtk_widget_set_size_request(inst->area, area_x, area_y);
992 gtk_widget_set_usize(inst->area, area_x, area_y);
995 #if GTK_CHECK_VERSION(2,0,0)
996 gtk_window_resize(GTK_WINDOW(inst->window),
997 area_x + offset_x, area_y + offset_y);
999 gdk_window_resize(inst->window->window,
1000 area_x + offset_x, area_y + offset_y);
1004 void real_palette_set(int n, int r, int g, int b)
1006 gboolean success[1];
1008 inst->cols[n].red = r * 0x0101;
1009 inst->cols[n].green = g * 0x0101;
1010 inst->cols[n].blue = b * 0x0101;
1012 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1013 FALSE, FALSE, success);
1015 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1019 void palette_set(int n, int r, int g, int b)
1021 static const int first[21] = {
1022 0, 2, 4, 6, 8, 10, 12, 14,
1023 1, 3, 5, 7, 9, 11, 13, 15,
1026 real_palette_set(first[n], r, g, b);
1028 real_palette_set(first[n] + 1, r, g, b);
1031 void palette_reset(void)
1033 /* This maps colour indices in cfg to those used in inst->cols. */
1034 static const int ww[] = {
1035 6, 7, 8, 9, 10, 11, 12, 13,
1036 14, 15, 16, 17, 18, 19, 20, 21,
1039 gboolean success[NCOLOURS];
1042 assert(lenof(ww) == NCOLOURS);
1044 if (!inst->colmap) {
1045 inst->colmap = gdk_colormap_get_system();
1047 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1050 for (i = 0; i < NCOLOURS; i++) {
1051 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1052 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1053 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1056 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1057 FALSE, FALSE, success);
1058 for (i = 0; i < NCOLOURS; i++) {
1060 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1061 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1065 void write_clip(wchar_t * data, int len, int must_deselect)
1067 if (inst->pasteout_data)
1068 sfree(inst->pasteout_data);
1069 inst->pasteout_data = smalloc(len);
1070 inst->pasteout_data_len = len;
1071 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1074 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1075 GDK_CURRENT_TIME)) {
1076 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1077 GDK_SELECTION_TYPE_STRING, 1);
1078 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1079 inst->compound_text_atom, 1);
1083 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1084 guint info, guint time_stamp, gpointer data)
1086 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1087 inst->pasteout_data, inst->pasteout_data_len);
1090 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1094 if (inst->pasteout_data)
1095 sfree(inst->pasteout_data);
1096 inst->pasteout_data = NULL;
1097 inst->pasteout_data_len = 0;
1101 void request_paste(void)
1104 * In Unix, pasting is asynchronous: all we can do at the
1105 * moment is to call gtk_selection_convert(), and when the data
1106 * comes back _then_ we can call term_do_paste().
1108 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1109 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1112 gint idle_paste_func(gpointer data); /* forward ref */
1114 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1117 if (seldata->length <= 0 ||
1118 seldata->type != GDK_SELECTION_TYPE_STRING)
1119 return; /* Nothing happens. */
1121 if (inst->pastein_data)
1122 sfree(inst->pastein_data);
1124 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1125 inst->pastein_data_len = seldata->length;
1126 mb_to_wc(0, 0, seldata->data, seldata->length,
1127 inst->pastein_data, inst->pastein_data_len);
1131 if (term_paste_pending())
1132 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1135 gint idle_paste_func(gpointer data)
1137 struct gui_data *inst = (struct gui_data *)data;
1139 if (term_paste_pending())
1142 gtk_idle_remove(inst->term_paste_idle_id);
1148 void get_clip(wchar_t ** p, int *len)
1151 *p = inst->pastein_data;
1152 *len = inst->pastein_data_len;
1156 void set_title(char *title)
1158 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1159 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1160 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1163 void set_icon(char *title)
1165 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1166 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1167 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1170 void set_sbar(int total, int start, int page)
1174 inst->sbar_adjust->lower = 0;
1175 inst->sbar_adjust->upper = total;
1176 inst->sbar_adjust->value = start;
1177 inst->sbar_adjust->page_size = page;
1178 inst->sbar_adjust->step_increment = 1;
1179 inst->sbar_adjust->page_increment = page/2;
1180 inst->ignore_sbar = TRUE;
1181 gtk_adjustment_changed(inst->sbar_adjust);
1182 inst->ignore_sbar = FALSE;
1185 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1189 if (!inst->ignore_sbar)
1190 term_scroll(1, (int)adj->value);
1193 void sys_cursor(int x, int y)
1196 * This is meaningless under X.
1205 int CharWidth(Context ctx, int uc)
1208 * Under X, any fixed-width font really _is_ fixed-width.
1209 * Double-width characters will be dealt with using a separate
1210 * font. For the moment we can simply return 1.
1215 Context get_ctx(void)
1218 if (!inst->area->window)
1220 gc = gdk_gc_new(inst->area->window);
1224 void free_ctx(Context ctx)
1226 GdkGC *gc = (GdkGC *)ctx;
1231 * Draw a line of text in the window, at given character
1232 * coordinates, in given attributes.
1234 * We are allowed to fiddle with the contents of `text'.
1236 void do_text(Context ctx, int x, int y, char *text, int len,
1237 unsigned long attr, int lattr)
1240 GdkGC *gc = (GdkGC *)ctx;
1244 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1245 * - cursor shapes other than block
1249 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1250 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1251 if (attr & ATTR_REVERSE) {
1256 if (cfg.bold_colour && (attr & ATTR_BOLD))
1258 if (cfg.bold_colour && (attr & ATTR_BLINK))
1260 if (attr & TATTR_ACTCURS) {
1265 if (lattr != LATTR_NORM) {
1269 if (x + len*2 > cols)
1270 len = (cols-x)/2; /* trim to LH half */
1273 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1274 gdk_draw_rectangle(inst->pixmap, gc, 1,
1275 x*inst->font_width+cfg.window_border,
1276 y*inst->font_height+cfg.window_border,
1277 len*inst->font_width, inst->font_height);
1279 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1280 gdk_draw_text(inst->pixmap, inst->fonts[0], gc,
1281 x*inst->font_width+cfg.window_border,
1282 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1285 if (attr & ATTR_UNDER) {
1286 int uheight = inst->fonts[0]->ascent + 1;
1287 if (uheight >= inst->font_height)
1288 uheight = inst->font_height - 1;
1289 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1290 y*inst->font_height + uheight + cfg.window_border,
1291 (x+len)*inst->font_width-1+cfg.window_border,
1292 y*inst->font_height + uheight + cfg.window_border);
1295 if (lattr != LATTR_NORM) {
1297 * I can't find any plausible StretchBlt equivalent in the
1298 * X server, so I'm going to do this the slow and painful
1299 * way. This will involve repeated calls to
1300 * gdk_draw_pixmap() to stretch the text horizontally. It's
1301 * O(N^2) in time and O(N) in network bandwidth, but you
1302 * try thinking of a better way. :-(
1305 for (i = 0; i < len * inst->font_width; i++) {
1306 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1307 x*inst->font_width+cfg.window_border + 2*i,
1308 y*inst->font_height+cfg.window_border,
1309 x*inst->font_width+cfg.window_border + 2*i+1,
1310 y*inst->font_height+cfg.window_border,
1311 len * inst->font_width - i, inst->font_height);
1314 if (lattr != LATTR_WIDE) {
1316 /* Now stretch vertically, in the same way. */
1317 if (lattr == LATTR_BOT)
1321 for (i = 0; i < inst->font_height; i+=2) {
1322 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1323 x*inst->font_width+cfg.window_border,
1324 y*inst->font_height+cfg.window_border+dt*i+db,
1325 x*inst->font_width+cfg.window_border,
1326 y*inst->font_height+cfg.window_border+dt*(i+1),
1327 len * inst->font_width, inst->font_height-i-1);
1333 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1334 x*inst->font_width+cfg.window_border,
1335 y*inst->font_height+cfg.window_border,
1336 x*inst->font_width+cfg.window_border,
1337 y*inst->font_height+cfg.window_border,
1338 len*inst->font_width, inst->font_height);
1341 void do_cursor(Context ctx, int x, int y, char *text, int len,
1342 unsigned long attr, int lattr)
1345 GdkGC *gc = (GdkGC *)ctx;
1348 * NYI: cursor shapes other than block
1350 if (attr & TATTR_PASCURS) {
1351 attr &= ~TATTR_PASCURS;
1355 do_text(ctx, x, y, text, len, attr, lattr);
1357 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1358 gdk_draw_rectangle(inst->pixmap, gc, 0,
1359 x*inst->font_width+cfg.window_border,
1360 y*inst->font_height+cfg.window_border,
1361 len*inst->font_width-1, inst->font_height-1);
1362 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1363 x*inst->font_width+cfg.window_border,
1364 y*inst->font_height+cfg.window_border,
1365 x*inst->font_width+cfg.window_border,
1366 y*inst->font_height+cfg.window_border,
1367 len*inst->font_width, inst->font_height);
1371 GdkCursor *make_mouse_ptr(int cursor_val)
1374 * Truly hideous hack: GTK doesn't allow us to set the mouse
1375 * cursor foreground and background colours unless we've _also_
1376 * created our own cursor from bitmaps. Therefore, I need to
1377 * load the `cursor' font and draw glyphs from it on to
1378 * pixmaps, in order to construct my cursors with the fg and bg
1379 * I want. This is a gross hack, but it's more self-contained
1380 * than linking in Xlib to find the X window handle to
1381 * inst->area and calling XRecolorCursor, and it's more
1382 * futureproof than hard-coding the shapes as bitmap arrays.
1384 static GdkFont *cursor_font = NULL;
1385 GdkPixmap *source, *mask;
1387 GdkColor cfg = { 0, 65535, 65535, 65535 };
1388 GdkColor cbg = { 0, 0, 0, 0 };
1389 GdkColor dfg = { 1, 65535, 65535, 65535 };
1390 GdkColor dbg = { 0, 0, 0, 0 };
1393 gint lb, rb, wid, asc, desc, w, h, x, y;
1395 if (cursor_val == -2) {
1396 gdk_font_unref(cursor_font);
1400 if (cursor_val >= 0 && !cursor_font)
1401 cursor_font = gdk_font_load("cursor");
1404 * Get the text extent of the cursor in question. We use the
1405 * mask character for this, because it's typically slightly
1406 * bigger than the main character.
1408 if (cursor_val >= 0) {
1410 text[0] = (char)cursor_val + 1;
1411 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1412 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1418 source = gdk_pixmap_new(NULL, w, h, 1);
1419 mask = gdk_pixmap_new(NULL, w, h, 1);
1422 * Draw the mask character on the mask pixmap.
1424 gc = gdk_gc_new(mask);
1425 gdk_gc_set_foreground(gc, &dbg);
1426 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1427 if (cursor_val >= 0) {
1429 text[0] = (char)cursor_val + 1;
1430 gdk_gc_set_foreground(gc, &dfg);
1431 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1436 * Draw the main character on the source pixmap.
1438 gc = gdk_gc_new(source);
1439 gdk_gc_set_foreground(gc, &dbg);
1440 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1441 if (cursor_val >= 0) {
1443 text[0] = (char)cursor_val;
1444 gdk_gc_set_foreground(gc, &dfg);
1445 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1450 * Create the cursor.
1452 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1457 gdk_pixmap_unref(source);
1458 gdk_pixmap_unref(mask);
1463 void modalfatalbox(char *p, ...)
1466 fprintf(stderr, "FATAL ERROR: ");
1468 vfprintf(stderr, p, ap);
1470 fputc('\n', stderr);
1474 char *get_x_display(void)
1476 return gdk_get_display();
1479 int main(int argc, char **argv)
1481 extern int pty_master_fd; /* declared in pty.c */
1482 extern char **pty_argv; /* declared in pty.c */
1485 gtk_init(&argc, &argv);
1487 do_defaults(NULL, &cfg);
1489 while (--argc > 0) {
1491 if (!strcmp(p, "-fn")) {
1493 strncpy(cfg.font, *++argv, sizeof(cfg.font));
1494 cfg.font[sizeof(cfg.font)-1] = '\0';
1496 err = 1, fprintf(stderr, "pterm: -fn expects an argument\n");
1498 if (!strcmp(p, "-e")) {
1501 pty_argv = smalloc((argc+1) * sizeof(char *));
1503 for (i = 0; i < argc; i++)
1504 pty_argv[i] = argv[i];
1505 pty_argv[argc] = NULL;
1506 break; /* finished command-line processing */
1508 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1510 if (!strcmp(p, "-T")) {
1512 strncpy(cfg.wintitle, *++argv, sizeof(cfg.wintitle));
1513 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1515 err = 1, fprintf(stderr, "pterm: -T expects an argument\n");
1517 if (!strcmp(p, "-log")) {
1519 strncpy(cfg.logfilename, *++argv, sizeof(cfg.logfilename));
1520 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1521 cfg.logtype = LGTYP_DEBUG;
1523 err = 1, fprintf(stderr, "pterm: -log expects an argument\n");
1525 if (!strcmp(p, "-hide")) {
1526 cfg.hide_mouseptr = 1;
1528 if (!strcmp(p, "-ut-")) {
1531 if (!strcmp(p, "-ls-")) {
1532 cfg.login_shell = 0;
1534 if (!strcmp(p, "-nethack")) {
1535 cfg.nethack_keypad = 1;
1537 if (!strcmp(p, "-sb-")) {
1542 inst->fonts[0] = gdk_font_load(cfg.font);
1543 inst->fonts[1] = NULL; /* FIXME: what about bold font? */
1544 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1545 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1547 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1551 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1553 if (cfg.wintitle[0])
1554 set_title(cfg.wintitle);
1559 * Set up the colour map.
1563 inst->area = gtk_drawing_area_new();
1564 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1565 inst->font_width * cfg.width + 2*cfg.window_border,
1566 inst->font_height * cfg.height + 2*cfg.window_border);
1567 if (cfg.scrollbar) {
1568 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1569 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1571 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1572 if (cfg.scrollbar) {
1573 if (cfg.scrollbar_on_left)
1574 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1576 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1578 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1580 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1584 geom.min_width = inst->font_width + 2*cfg.window_border;
1585 geom.min_height = inst->font_height + 2*cfg.window_border;
1586 geom.max_width = geom.max_height = -1;
1587 geom.base_width = 2*cfg.window_border;
1588 geom.base_height = 2*cfg.window_border;
1589 geom.width_inc = inst->font_width;
1590 geom.height_inc = inst->font_height;
1591 geom.min_aspect = geom.max_aspect = 0;
1592 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1593 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1594 GDK_HINT_RESIZE_INC);
1597 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1598 GTK_SIGNAL_FUNC(destroy), inst);
1599 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1600 GTK_SIGNAL_FUNC(delete_window), inst);
1601 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1602 GTK_SIGNAL_FUNC(key_event), inst);
1603 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
1604 GTK_SIGNAL_FUNC(key_event), inst);
1605 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1606 GTK_SIGNAL_FUNC(focus_event), inst);
1607 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1608 GTK_SIGNAL_FUNC(focus_event), inst);
1609 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1610 GTK_SIGNAL_FUNC(configure_area), inst);
1611 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1612 GTK_SIGNAL_FUNC(expose_area), inst);
1613 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1614 GTK_SIGNAL_FUNC(button_event), inst);
1615 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1616 GTK_SIGNAL_FUNC(button_event), inst);
1617 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1618 GTK_SIGNAL_FUNC(motion_event), inst);
1619 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1620 GTK_SIGNAL_FUNC(selection_received), inst);
1621 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1622 GTK_SIGNAL_FUNC(selection_get), inst);
1623 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1624 GTK_SIGNAL_FUNC(selection_clear), inst);
1626 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1627 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
1628 gtk_timeout_add(20, timer_func, inst);
1629 gtk_widget_add_events(GTK_WIDGET(inst->area),
1630 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1631 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1632 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
1634 gtk_widget_show(inst->area);
1636 gtk_widget_show(inst->sbar);
1637 gtk_widget_show(GTK_WIDGET(inst->hbox));
1638 gtk_widget_show(inst->window);
1640 inst->textcursor = make_mouse_ptr(GDK_XTERM);
1641 inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
1642 inst->blankcursor = make_mouse_ptr(-1);
1643 make_mouse_ptr(-2); /* clean up cursor font */
1644 inst->currcursor = inst->textcursor;
1647 back = &pty_backend;
1648 back->init(NULL, 0, NULL, 0);
1650 gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
1653 term_size(cfg.height, cfg.width, cfg.savelines);