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;
44 char wintitle[sizeof(((Config *)0)->wintitle)];
45 char icontitle[sizeof(((Config *)0)->wintitle)];
48 static struct gui_data the_inst;
49 static struct gui_data *inst = &the_inst; /* so we always write `inst->' */
50 static int send_raw_mouse;
52 void ldisc_update(int echo, int edit)
55 * This is a stub in pterm. If I ever produce a Unix
56 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
57 * then it will require some termios manoeuvring analogous to
58 * that in the Windows plink.c, but here it's meaningless.
62 int askappend(char *filename)
65 * Logging in an xterm-alike is liable to be something you only
66 * do at serious diagnostic need. Hence, I'm going to take the
67 * easy option for now and assume we always want to overwrite
68 * log files. I can always make it properly configurable later.
73 void logevent(char *string)
76 * This is not a very helpful function: events are logged
77 * pretty much exclusively by the back end, and our pty back
78 * end is self-contained. So we need do nothing.
82 int font_dimension(int which) /* 0 for width, 1 for height */
85 return inst->font_height;
87 return inst->font_width;
91 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
92 * into a cooked one (SELECT, EXTEND, PASTE).
94 * In Unix, this is not configurable; the X button arrangement is
95 * rock-solid across all applications, everyone has a three-button
96 * mouse or a means of faking it, and there is no need to switch
97 * buttons around at all.
99 Mouse_Button translate_button(Mouse_Button button)
101 if (button == MBT_LEFT)
103 if (button == MBT_MIDDLE)
105 if (button == MBT_RIGHT)
107 return 0; /* shouldn't happen */
111 * Minimise or restore the window in response to a server-side
114 void set_iconic(int iconic)
117 * GTK 1.2 doesn't know how to do this.
119 #if GTK_CHECK_VERSION(2,0,0)
121 gtk_window_iconify(GTK_WINDOW(inst->window));
123 gtk_window_deiconify(GTK_WINDOW(inst->window));
128 * Move the window in response to a server-side request.
130 void move_window(int x, int y)
133 * I assume that when the GTK version of this call is available
134 * we should use it. Not sure how it differs from the GDK one,
137 #if GTK_CHECK_VERSION(2,0,0)
138 gtk_window_move(GTK_WINDOW(inst->window), x, y);
140 gdk_window_move(inst->window->window, x, y);
145 * Move the window to the top or bottom of the z-order in response
146 * to a server-side request.
148 void set_zorder(int top)
151 gdk_window_raise(inst->window->window);
153 gdk_window_lower(inst->window->window);
157 * Refresh the window in response to a server-side request.
159 void refresh_window(void)
165 * Maximise or restore the window in response to a server-side
168 void set_zoomed(int zoomed)
171 * GTK 1.2 doesn't know how to do this.
173 #if GTK_CHECK_VERSION(2,0,0)
175 gtk_window_maximize(GTK_WINDOW(inst->window));
177 gtk_window_unmaximize(GTK_WINDOW(inst->window));
182 * Report whether the window is iconic, for terminal reports.
186 return !gdk_window_is_viewable(inst->window->window);
190 * Report the window's position, for terminal reports.
192 void get_window_pos(int *x, int *y)
195 * I assume that when the GTK version of this call is available
196 * we should use it. Not sure how it differs from the GDK one,
199 #if GTK_CHECK_VERSION(2,0,0)
200 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
202 gdk_window_get_position(inst->window->window, x, y);
207 * Report the window's pixel size, for terminal reports.
209 void get_window_pixels(int *x, int *y)
212 * I assume that when the GTK version of this call is available
213 * we should use it. Not sure how it differs from the GDK one,
216 #if GTK_CHECK_VERSION(2,0,0)
217 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
219 gdk_window_get_size(inst->window->window, x, y);
224 * Return the window or icon title.
226 char *get_window_title(int icon)
228 return icon ? inst->wintitle : inst->icontitle;
231 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
234 * We could implement warn-on-close here if we really wanted
240 void show_mouseptr(int show)
242 if (!cfg.hide_mouseptr)
245 gdk_window_set_cursor(inst->area->window, inst->currcursor);
247 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
248 inst->mouseptr_visible = show;
251 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
253 struct gui_data *inst = (struct gui_data *)data;
254 int w, h, need_size = 0;
256 w = (event->width - 2*cfg.window_border) / inst->font_width;
257 h = (event->height - 2*cfg.window_border) / inst->font_height;
259 if (w != cfg.width || h != cfg.height) {
261 gdk_pixmap_unref(inst->pixmap);
271 inst->pixmap = gdk_pixmap_new(widget->window,
272 (cfg.width * inst->font_width +
273 2*cfg.window_border),
274 (cfg.height * inst->font_height +
275 2*cfg.window_border), -1);
277 gc = gdk_gc_new(inst->area->window);
278 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
279 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
280 cfg.width * inst->font_width + 2*cfg.window_border,
281 cfg.height * inst->font_height + 2*cfg.window_border);
286 term_size(h, w, cfg.savelines);
292 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
294 /* struct gui_data *inst = (struct gui_data *)data; */
297 * Pass the exposed rectangle to terminal.c, which will call us
298 * back to do the actual painting.
301 gdk_draw_pixmap(widget->window,
302 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
304 event->area.x, event->area.y,
305 event->area.x, event->area.y,
306 event->area.width, event->area.height);
311 #define KEY_PRESSED(k) \
312 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
314 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
316 /* struct gui_data *inst = (struct gui_data *)data; */
320 if (event->type == GDK_KEY_PRESS) {
324 printf("keypress: keyval = %04x, state = %08x; string =",
325 event->keyval, event->state);
326 for (i = 0; event->string[i]; i++)
327 printf(" %02x", (unsigned char) event->string[i]);
335 * - Compose key (!!! requires Unicode faff before even trying)
339 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
342 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
343 term_scroll(0, -cfg.height/2);
346 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
347 term_scroll(0, +cfg.height/2);
352 * Neither does Shift-Ins.
354 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
359 /* ALT+things gives leading Escape. */
361 strncpy(output+1, event->string, 31);
363 end = strlen(output);
364 if (event->state & GDK_MOD1_MASK)
369 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
370 if (!event->string[0] && event->keyval == '`' &&
371 (event->state & GDK_CONTROL_MASK)) {
376 /* Control-Break is the same as Control-C */
377 if (event->keyval == GDK_Break &&
378 (event->state & GDK_CONTROL_MASK)) {
383 /* Control-2, Control-Space and Control-@ are NUL */
384 if (!event->string[0] &&
385 (event->keyval == ' ' || event->keyval == '2' ||
386 event->keyval == '@') &&
387 (event->state & (GDK_SHIFT_MASK |
388 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
393 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
394 if (!event->string[0] && event->keyval == ' ' &&
395 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
396 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
401 /* We don't let GTK tell us what Backspace is! We know better. */
402 if (event->keyval == GDK_BackSpace &&
403 !(event->state & GDK_SHIFT_MASK)) {
404 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
408 /* Shift-Tab is ESC [ Z */
409 if (event->keyval == GDK_ISO_Left_Tab ||
410 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
411 end = 1 + sprintf(output+1, "\033[Z");
415 * NetHack keypad mode.
417 if (cfg.nethack_keypad) {
419 switch (event->keyval) {
420 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
421 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
422 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
423 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
424 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
425 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
426 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
427 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
428 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
432 if (event->state & GDK_SHIFT_MASK)
441 * Application keypad mode.
443 if (app_keypad_keys && !cfg.no_applic_k) {
445 switch (event->keyval) {
446 case GDK_Num_Lock: xkey = 'P'; break;
447 case GDK_KP_Divide: xkey = 'Q'; break;
448 case GDK_KP_Multiply: xkey = 'R'; break;
449 case GDK_KP_Subtract: xkey = 'S'; break;
451 * Keypad + is tricky. It covers a space that would
452 * be taken up on the VT100 by _two_ keys; so we
453 * let Shift select between the two. Worse still,
454 * in xterm function key mode we change which two...
457 if (cfg.funky_type == 2) {
458 if (event->state & GDK_SHIFT_MASK)
462 } else if (event->state & GDK_SHIFT_MASK)
467 case GDK_KP_Enter: xkey = 'M'; break;
468 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
469 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
470 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
471 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
472 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
473 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
474 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
475 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
476 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
477 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
478 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
482 if (xkey >= 'P' && xkey <= 'S')
483 end = 1 + sprintf(output+1, "\033%c", xkey);
485 end = 1 + sprintf(output+1, "\033?%c", xkey);
487 end = 1 + sprintf(output+1, "\033O%c", xkey);
493 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
494 * for integer decimal nn.)
496 * We also deal with the weird ones here. Linux VCs replace F1
497 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
498 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
503 switch (event->keyval) {
505 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
508 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
511 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
514 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
517 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
520 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
523 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
526 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
529 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
532 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
565 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
566 case GDK_Home: case GDK_KP_Home:
569 case GDK_Insert: case GDK_KP_Insert:
572 case GDK_Delete: case GDK_KP_Delete:
575 case GDK_End: case GDK_KP_End:
578 case GDK_Page_Up: case GDK_KP_Page_Up:
581 case GDK_Page_Down: case GDK_KP_Page_Down:
585 /* Reorder edit keys to physical order */
586 if (cfg.funky_type == 3 && code <= 6)
587 code = "\0\2\1\4\5\3\6"[code];
589 if (vt52_mode && code > 0 && code <= 6) {
590 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
594 if (cfg.funky_type == 5 && /* SCO function keys */
595 code >= 11 && code <= 34) {
596 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
598 switch (event->keyval) {
599 case GDK_F1: index = 0; break;
600 case GDK_F2: index = 1; break;
601 case GDK_F3: index = 2; break;
602 case GDK_F4: index = 3; break;
603 case GDK_F5: index = 4; break;
604 case GDK_F6: index = 5; break;
605 case GDK_F7: index = 6; break;
606 case GDK_F8: index = 7; break;
607 case GDK_F9: index = 8; break;
608 case GDK_F10: index = 9; break;
609 case GDK_F11: index = 10; break;
610 case GDK_F12: index = 11; break;
612 if (event->state & GDK_SHIFT_MASK) index += 12;
613 if (event->state & GDK_CONTROL_MASK) index += 24;
614 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
617 if (cfg.funky_type == 5 && /* SCO small keypad */
618 code >= 1 && code <= 6) {
619 char codes[] = "HL.FIG";
624 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
628 if ((vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
635 end = 1 + sprintf(output+1,
636 "\x1B%c", code + 'P' - 11 - offt);
638 end = 1 + sprintf(output+1,
639 "\x1BO%c", code + 'P' - 11 - offt);
642 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
643 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
646 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
648 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
650 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
653 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
654 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
658 end = 1 + sprintf(output+1, "\x1B[%d~", code);
664 * Cursor keys. (This includes the numberpad cursor keys,
665 * if we haven't already done them due to app keypad mode.)
667 * Here we also process un-numlocked un-appkeypadded KP5,
668 * which sends ESC [ G.
672 switch (event->keyval) {
673 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
674 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
675 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
676 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
677 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
681 * The arrow keys normally do ESC [ A and so on. In
682 * app cursor keys mode they do ESC O A instead.
683 * Ctrl toggles the two modes.
686 end = 1 + sprintf(output+1, "\033%c", xkey);
687 } else if (!app_cursor_keys ^
688 !(event->state & GDK_CONTROL_MASK)) {
689 end = 1 + sprintf(output+1, "\033O%c", xkey);
691 end = 1 + sprintf(output+1, "\033[%c", xkey);
702 printf("generating sequence:");
703 for (i = start; i < end; i++)
704 printf(" %02x", (unsigned char) output[i]);
709 ldisc_send(output+start, end-start, 1);
718 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
720 struct gui_data *inst = (struct gui_data *)data;
721 int shift, ctrl, alt, x, y, button, act;
725 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
729 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
734 shift = event->state & GDK_SHIFT_MASK;
735 ctrl = event->state & GDK_CONTROL_MASK;
736 alt = event->state & GDK_MOD1_MASK;
737 if (event->button == 1)
739 else if (event->button == 2)
741 else if (event->button == 3)
744 return FALSE; /* don't even know what button! */
746 switch (event->type) {
747 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
748 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
749 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
750 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
751 default: return FALSE; /* don't know this event type */
754 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
755 act != MA_CLICK && act != MA_RELEASE)
756 return TRUE; /* we ignore these in raw mouse mode */
758 x = (event->x - cfg.window_border) / inst->font_width;
759 y = (event->y - cfg.window_border) / inst->font_height;
761 term_mouse(button, act, x, y, shift, ctrl, alt);
766 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
768 struct gui_data *inst = (struct gui_data *)data;
769 int shift, ctrl, alt, x, y, button;
773 shift = event->state & GDK_SHIFT_MASK;
774 ctrl = event->state & GDK_CONTROL_MASK;
775 alt = event->state & GDK_MOD1_MASK;
776 if (event->state & GDK_BUTTON1_MASK)
778 else if (event->state & GDK_BUTTON2_MASK)
780 else if (event->state & GDK_BUTTON3_MASK)
783 return FALSE; /* don't even know what button! */
785 x = (event->x - cfg.window_border) / inst->font_width;
786 y = (event->y - cfg.window_border) / inst->font_height;
788 term_mouse(button, MA_DRAG, x, y, shift, ctrl, alt);
793 gint timer_func(gpointer data)
795 /* struct gui_data *inst = (struct gui_data *)data; */
796 extern int pty_child_is_dead(); /* declared in pty.c */
798 if (pty_child_is_dead()) {
800 * The primary child process died. We could keep the
801 * terminal open for remaining subprocesses to output to,
802 * but conventional wisdom seems to feel that that's the
803 * Wrong Thing for an xterm-alike, so we bail out now. This
804 * would be easy enough to change or make configurable if
815 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
817 /* struct gui_data *inst = (struct gui_data *)data; */
821 ret = read(sourcefd, buf, sizeof(buf));
824 * Clean termination condition is that either ret == 0, or ret
825 * < 0 and errno == EIO. Not sure why the latter, but it seems
828 if (ret == 0 || (ret < 0 && errno == EIO)) {
833 perror("read pty master");
837 from_backend(0, buf, ret);
842 void destroy(GtkWidget *widget, gpointer data)
847 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
849 has_focus = event->in;
857 * set or clear the "raw mouse message" mode
859 void set_raw_mouse_mode(int activate)
861 activate = activate && !cfg.no_mouse_rep;
862 send_raw_mouse = activate;
864 inst->currcursor = inst->rawcursor;
866 inst->currcursor = inst->textcursor;
867 show_mouseptr(inst->mouseptr_visible);
870 void request_resize(int w, int h)
872 int large_x, large_y;
873 int offset_x, offset_y;
875 GtkRequisition inner, outer;
878 * This is a heinous hack dreamed up by the gnome-terminal
879 * people to get around a limitation in gtk. The problem is
880 * that in order to set the size correctly we really need to be
881 * calling gtk_window_resize - but that needs to know the size
882 * of the _whole window_, not the drawing area. So what we do
883 * is to set an artificially huge size request on the drawing
884 * area, recompute the resulting size request on the window,
885 * and look at the difference between the two. That gives us
886 * the x and y offsets we need to translate drawing area size
887 * into window size for real, and then we call
892 * We start by retrieving the current size of the whole window.
893 * Adding a bit to _that_ will give us a value we can use as a
894 * bogus size request which guarantees to be bigger than the
895 * current size of the drawing area.
897 get_window_pixels(&large_x, &large_y);
901 #if GTK_CHECK_VERSION(2,0,0)
902 gtk_widget_set_size_request(inst->area, large_x, large_y);
904 gtk_widget_set_usize(inst->area, large_x, large_y);
906 gtk_widget_size_request(inst->area, &inner);
907 gtk_widget_size_request(inst->window, &outer);
909 offset_x = outer.width - inner.width;
910 offset_y = outer.height - inner.height;
912 area_x = inst->font_width * w + 2*cfg.window_border;
913 area_y = inst->font_height * h + 2*cfg.window_border;
916 * Now we must set the size request on the drawing area back to
917 * something sensible before we commit the real resize. Best
918 * way to do this, I think, is to set it to what the size is
919 * really going to end up being.
921 #if GTK_CHECK_VERSION(2,0,0)
922 gtk_widget_set_size_request(inst->area, area_x, area_y);
924 gtk_widget_set_usize(inst->area, area_x, area_y);
927 #if GTK_CHECK_VERSION(2,0,0)
928 gtk_window_resize(GTK_WINDOW(inst->window),
929 area_x + offset_x, area_y + offset_y);
931 gdk_window_resize(inst->window->window,
932 area_x + offset_x, area_y + offset_y);
936 void real_palette_set(int n, int r, int g, int b)
940 inst->cols[n].red = r * 0x0101;
941 inst->cols[n].green = g * 0x0101;
942 inst->cols[n].blue = b * 0x0101;
944 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
945 FALSE, FALSE, success);
947 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
951 void palette_set(int n, int r, int g, int b)
953 static const int first[21] = {
954 0, 2, 4, 6, 8, 10, 12, 14,
955 1, 3, 5, 7, 9, 11, 13, 15,
958 real_palette_set(first[n], r, g, b);
960 real_palette_set(first[n] + 1, r, g, b);
963 void palette_reset(void)
965 /* This maps colour indices in cfg to those used in inst->cols. */
966 static const int ww[] = {
967 6, 7, 8, 9, 10, 11, 12, 13,
968 14, 15, 16, 17, 18, 19, 20, 21,
971 gboolean success[NCOLOURS];
974 assert(lenof(ww) == NCOLOURS);
977 inst->colmap = gdk_colormap_get_system();
979 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
982 for (i = 0; i < NCOLOURS; i++) {
983 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
984 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
985 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
988 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
989 FALSE, FALSE, success);
990 for (i = 0; i < NCOLOURS; i++) {
992 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
993 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
997 void write_clip(wchar_t * data, int len, int must_deselect)
999 if (inst->pasteout_data)
1000 sfree(inst->pasteout_data);
1001 inst->pasteout_data = smalloc(len);
1002 inst->pasteout_data_len = len;
1003 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1006 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1007 GDK_CURRENT_TIME)) {
1008 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1009 GDK_SELECTION_TYPE_STRING, 1);
1010 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1011 inst->compound_text_atom, 1);
1015 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1016 guint info, guint time_stamp, gpointer data)
1018 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1019 inst->pasteout_data, inst->pasteout_data_len);
1022 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1026 if (inst->pasteout_data)
1027 sfree(inst->pasteout_data);
1028 inst->pasteout_data = NULL;
1029 inst->pasteout_data_len = 0;
1033 void request_paste(void)
1036 * In Unix, pasting is asynchronous: all we can do at the
1037 * moment is to call gtk_selection_convert(), and when the data
1038 * comes back _then_ we can call term_do_paste().
1040 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1041 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1044 gint idle_paste_func(gpointer data); /* forward ref */
1046 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1049 if (seldata->length <= 0 ||
1050 seldata->type != GDK_SELECTION_TYPE_STRING)
1051 return; /* Nothing happens. */
1053 if (inst->pastein_data)
1054 sfree(inst->pastein_data);
1056 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1057 inst->pastein_data_len = seldata->length;
1058 mb_to_wc(0, 0, seldata->data, seldata->length,
1059 inst->pastein_data, inst->pastein_data_len);
1063 if (term_paste_pending())
1064 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1067 gint idle_paste_func(gpointer data)
1069 struct gui_data *inst = (struct gui_data *)data;
1071 if (term_paste_pending())
1074 gtk_idle_remove(inst->term_paste_idle_id);
1080 void get_clip(wchar_t ** p, int *len)
1083 *p = inst->pastein_data;
1084 *len = inst->pastein_data_len;
1088 void set_title(char *title)
1090 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1091 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1092 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1095 void set_icon(char *title)
1097 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1098 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1099 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1102 void set_sbar(int total, int start, int page)
1106 inst->sbar_adjust->lower = 0;
1107 inst->sbar_adjust->upper = total;
1108 inst->sbar_adjust->value = start;
1109 inst->sbar_adjust->page_size = page;
1110 inst->sbar_adjust->step_increment = 1;
1111 inst->sbar_adjust->page_increment = page/2;
1112 inst->ignore_sbar = TRUE;
1113 gtk_adjustment_changed(inst->sbar_adjust);
1114 inst->ignore_sbar = FALSE;
1117 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1121 if (!inst->ignore_sbar)
1122 term_scroll(1, (int)adj->value);
1125 void sys_cursor(int x, int y)
1128 * This is meaningless under X.
1137 int CharWidth(Context ctx, int uc)
1140 * Under X, any fixed-width font really _is_ fixed-width.
1141 * Double-width characters will be dealt with using a separate
1142 * font. For the moment we can simply return 1.
1147 Context get_ctx(void)
1150 if (!inst->area->window)
1152 gc = gdk_gc_new(inst->area->window);
1156 void free_ctx(Context ctx)
1158 GdkGC *gc = (GdkGC *)ctx;
1163 * Draw a line of text in the window, at given character
1164 * coordinates, in given attributes.
1166 * We are allowed to fiddle with the contents of `text'.
1168 void do_text(Context ctx, int x, int y, char *text, int len,
1169 unsigned long attr, int lattr)
1172 GdkGC *gc = (GdkGC *)ctx;
1176 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1177 * - cursor shapes other than block
1181 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1182 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1183 if (attr & ATTR_REVERSE) {
1188 if (cfg.bold_colour && (attr & ATTR_BOLD))
1190 if (cfg.bold_colour && (attr & ATTR_BLINK))
1192 if (attr & TATTR_ACTCURS) {
1197 if (lattr != LATTR_NORM) {
1201 if (x + len*2 > cols)
1202 len = (cols-x)/2; /* trim to LH half */
1205 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1206 gdk_draw_rectangle(inst->pixmap, gc, 1,
1207 x*inst->font_width+cfg.window_border,
1208 y*inst->font_height+cfg.window_border,
1209 len*inst->font_width, inst->font_height);
1211 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1212 gdk_draw_text(inst->pixmap, inst->fonts[0], gc,
1213 x*inst->font_width+cfg.window_border,
1214 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1217 if (attr & ATTR_UNDER) {
1218 int uheight = inst->fonts[0]->ascent + 1;
1219 if (uheight >= inst->font_height)
1220 uheight = inst->font_height - 1;
1221 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1222 y*inst->font_height + uheight + cfg.window_border,
1223 (x+len)*inst->font_width-1+cfg.window_border,
1224 y*inst->font_height + uheight + cfg.window_border);
1227 if (lattr != LATTR_NORM) {
1229 * I can't find any plausible StretchBlt equivalent in the
1230 * X server, so I'm going to do this the slow and painful
1231 * way. This will involve repeated calls to
1232 * gdk_draw_pixmap() to stretch the text horizontally. It's
1233 * O(N^2) in time and O(N) in network bandwidth, but you
1234 * try thinking of a better way. :-(
1237 for (i = 0; i < len * inst->font_width; i++) {
1238 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1239 x*inst->font_width+cfg.window_border + 2*i,
1240 y*inst->font_height+cfg.window_border,
1241 x*inst->font_width+cfg.window_border + 2*i+1,
1242 y*inst->font_height+cfg.window_border,
1243 len * inst->font_width - i, inst->font_height);
1246 if (lattr != LATTR_WIDE) {
1248 /* Now stretch vertically, in the same way. */
1249 if (lattr == LATTR_BOT)
1253 for (i = 0; i < inst->font_height; i+=2) {
1254 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1255 x*inst->font_width+cfg.window_border,
1256 y*inst->font_height+cfg.window_border+dt*i+db,
1257 x*inst->font_width+cfg.window_border,
1258 y*inst->font_height+cfg.window_border+dt*(i+1),
1259 len * inst->font_width, inst->font_height-i-1);
1265 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1266 x*inst->font_width+cfg.window_border,
1267 y*inst->font_height+cfg.window_border,
1268 x*inst->font_width+cfg.window_border,
1269 y*inst->font_height+cfg.window_border,
1270 len*inst->font_width, inst->font_height);
1273 void do_cursor(Context ctx, int x, int y, char *text, int len,
1274 unsigned long attr, int lattr)
1277 GdkGC *gc = (GdkGC *)ctx;
1280 * NYI: cursor shapes other than block
1282 if (attr & TATTR_PASCURS) {
1283 attr &= ~TATTR_PASCURS;
1287 do_text(ctx, x, y, text, len, attr, lattr);
1289 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1290 gdk_draw_rectangle(inst->pixmap, gc, 0,
1291 x*inst->font_width+cfg.window_border,
1292 y*inst->font_height+cfg.window_border,
1293 len*inst->font_width-1, inst->font_height-1);
1294 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1295 x*inst->font_width+cfg.window_border,
1296 y*inst->font_height+cfg.window_border,
1297 x*inst->font_width+cfg.window_border,
1298 y*inst->font_height+cfg.window_border,
1299 len*inst->font_width, inst->font_height);
1303 GdkCursor *make_mouse_ptr(int cursor_val)
1306 * Truly hideous hack: GTK doesn't allow us to set the mouse
1307 * cursor foreground and background colours unless we've _also_
1308 * created our own cursor from bitmaps. Therefore, I need to
1309 * load the `cursor' font and draw glyphs from it on to
1310 * pixmaps, in order to construct my cursors with the fg and bg
1311 * I want. This is a gross hack, but it's more self-contained
1312 * than linking in Xlib to find the X window handle to
1313 * inst->area and calling XRecolorCursor, and it's more
1314 * futureproof than hard-coding the shapes as bitmap arrays.
1316 static GdkFont *cursor_font = NULL;
1317 GdkPixmap *source, *mask;
1319 GdkColor cfg = { 0, 65535, 65535, 65535 };
1320 GdkColor cbg = { 0, 0, 0, 0 };
1321 GdkColor dfg = { 1, 65535, 65535, 65535 };
1322 GdkColor dbg = { 0, 0, 0, 0 };
1325 gint lb, rb, wid, asc, desc, w, h, x, y;
1327 if (cursor_val == -2) {
1328 gdk_font_unref(cursor_font);
1332 if (cursor_val >= 0 && !cursor_font)
1333 cursor_font = gdk_font_load("cursor");
1336 * Get the text extent of the cursor in question. We use the
1337 * mask character for this, because it's typically slightly
1338 * bigger than the main character.
1340 if (cursor_val >= 0) {
1342 text[0] = (char)cursor_val + 1;
1343 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1344 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1350 source = gdk_pixmap_new(NULL, w, h, 1);
1351 mask = gdk_pixmap_new(NULL, w, h, 1);
1354 * Draw the mask character on the mask pixmap.
1356 gc = gdk_gc_new(mask);
1357 gdk_gc_set_foreground(gc, &dbg);
1358 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1359 if (cursor_val >= 0) {
1361 text[0] = (char)cursor_val + 1;
1362 gdk_gc_set_foreground(gc, &dfg);
1363 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1368 * Draw the main character on the source pixmap.
1370 gc = gdk_gc_new(source);
1371 gdk_gc_set_foreground(gc, &dbg);
1372 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1373 if (cursor_val >= 0) {
1375 text[0] = (char)cursor_val;
1376 gdk_gc_set_foreground(gc, &dfg);
1377 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1382 * Create the cursor.
1384 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1389 gdk_pixmap_unref(source);
1390 gdk_pixmap_unref(mask);
1395 void modalfatalbox(char *p, ...)
1398 fprintf(stderr, "FATAL ERROR: ");
1400 vfprintf(stderr, p, ap);
1402 fputc('\n', stderr);
1406 char *get_x_display(void)
1408 return gdk_get_display();
1411 int main(int argc, char **argv)
1413 extern int pty_master_fd; /* declared in pty.c */
1414 extern char **pty_argv; /* declared in pty.c */
1417 gtk_init(&argc, &argv);
1419 do_defaults(NULL, &cfg);
1421 while (--argc > 0) {
1423 if (!strcmp(p, "-fn")) {
1425 strncpy(cfg.font, *++argv, sizeof(cfg.font));
1426 cfg.font[sizeof(cfg.font)-1] = '\0';
1428 err = 1, fprintf(stderr, "pterm: -fn expects an argument\n");
1430 if (!strcmp(p, "-e")) {
1433 pty_argv = smalloc((argc+1) * sizeof(char *));
1435 for (i = 0; i < argc; i++)
1436 pty_argv[i] = argv[i];
1437 pty_argv[argc] = NULL;
1438 break; /* finished command-line processing */
1440 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1442 if (!strcmp(p, "-T")) {
1444 strncpy(cfg.wintitle, *++argv, sizeof(cfg.wintitle));
1445 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1447 err = 1, fprintf(stderr, "pterm: -T expects an argument\n");
1449 if (!strcmp(p, "-log")) {
1451 strncpy(cfg.logfilename, *++argv, sizeof(cfg.logfilename));
1452 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1453 cfg.logtype = LGTYP_DEBUG;
1455 err = 1, fprintf(stderr, "pterm: -log expects an argument\n");
1457 if (!strcmp(p, "-hide")) {
1458 cfg.hide_mouseptr = 1;
1460 if (!strcmp(p, "-ut-")) {
1463 if (!strcmp(p, "-ls-")) {
1464 cfg.login_shell = 0;
1466 if (!strcmp(p, "-nethack")) {
1467 cfg.nethack_keypad = 1;
1469 if (!strcmp(p, "-sb-")) {
1474 inst->fonts[0] = gdk_font_load(cfg.font);
1475 inst->fonts[1] = NULL; /* FIXME: what about bold font? */
1476 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1477 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1479 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1483 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1485 if (cfg.wintitle[0])
1486 set_title(cfg.wintitle);
1491 * Set up the colour map.
1495 inst->area = gtk_drawing_area_new();
1496 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1497 inst->font_width * cfg.width + 2*cfg.window_border,
1498 inst->font_height * cfg.height + 2*cfg.window_border);
1499 if (cfg.scrollbar) {
1500 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1501 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1503 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1504 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1506 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1508 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1512 geom.min_width = inst->font_width + 2*cfg.window_border;
1513 geom.min_height = inst->font_height + 2*cfg.window_border;
1514 geom.max_width = geom.max_height = -1;
1515 geom.base_width = 2*cfg.window_border;
1516 geom.base_height = 2*cfg.window_border;
1517 geom.width_inc = inst->font_width;
1518 geom.height_inc = inst->font_height;
1519 geom.min_aspect = geom.max_aspect = 0;
1520 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1521 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1522 GDK_HINT_RESIZE_INC);
1525 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1526 GTK_SIGNAL_FUNC(destroy), inst);
1527 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1528 GTK_SIGNAL_FUNC(delete_window), inst);
1529 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1530 GTK_SIGNAL_FUNC(key_event), inst);
1531 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1532 GTK_SIGNAL_FUNC(focus_event), inst);
1533 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1534 GTK_SIGNAL_FUNC(focus_event), inst);
1535 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1536 GTK_SIGNAL_FUNC(configure_area), inst);
1537 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1538 GTK_SIGNAL_FUNC(expose_area), inst);
1539 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1540 GTK_SIGNAL_FUNC(button_event), inst);
1541 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1542 GTK_SIGNAL_FUNC(button_event), inst);
1543 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1544 GTK_SIGNAL_FUNC(motion_event), inst);
1545 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1546 GTK_SIGNAL_FUNC(selection_received), inst);
1547 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1548 GTK_SIGNAL_FUNC(selection_get), inst);
1549 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1550 GTK_SIGNAL_FUNC(selection_clear), inst);
1552 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1553 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
1554 gtk_timeout_add(20, timer_func, inst);
1555 gtk_widget_add_events(GTK_WIDGET(inst->area),
1556 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1557 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1558 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
1560 gtk_widget_show(inst->area);
1562 gtk_widget_show(inst->sbar);
1563 gtk_widget_show(GTK_WIDGET(inst->hbox));
1564 gtk_widget_show(inst->window);
1566 inst->textcursor = make_mouse_ptr(GDK_XTERM);
1567 inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
1568 inst->blankcursor = make_mouse_ptr(-1);
1569 make_mouse_ptr(-2); /* clean up cursor font */
1570 inst->currcursor = inst->textcursor;
1573 back = &pty_backend;
1574 back->init(NULL, 0, NULL, 0);
1576 gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
1579 term_size(cfg.height, cfg.width, cfg.savelines);