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;
41 GdkAtom compound_text_atom;
42 char wintitle[sizeof(((Config *)0)->wintitle)];
45 static struct gui_data the_inst;
46 static struct gui_data *inst = &the_inst; /* so we always write `inst->' */
47 static int send_raw_mouse;
49 void ldisc_update(int echo, int edit)
52 * This is a stub in pterm. If I ever produce a Unix
53 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
54 * then it will require some termios manoeuvring analogous to
55 * that in the Windows plink.c, but here it's meaningless.
59 int askappend(char *filename)
62 * FIXME: for the moment we just wipe the log file. Since I
63 * haven't yet enabled logging, this shouldn't matter yet!
68 void logevent(char *string)
71 * This is not a very helpful function: events are logged
72 * pretty much exclusively by the back end, and our pty back
73 * end is self-contained. So we need do nothing.
78 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
79 * into a cooked one (SELECT, EXTEND, PASTE).
81 * In Unix, this is not configurable; the X button arrangement is
82 * rock-solid across all applications, everyone has a three-button
83 * mouse or a means of faking it, and there is no need to switch
84 * buttons around at all.
86 Mouse_Button translate_button(Mouse_Button button)
88 if (button == MBT_LEFT)
90 if (button == MBT_MIDDLE)
92 if (button == MBT_RIGHT)
94 return 0; /* shouldn't happen */
98 * Minimise or restore the window in response to a server-side
101 void set_iconic(int iconic)
103 /* FIXME: currently ignored */
107 * Move the window in response to a server-side request.
109 void move_window(int x, int y)
111 /* FIXME: currently ignored */
115 * Move the window to the top or bottom of the z-order in response
116 * to a server-side request.
118 void set_zorder(int top)
120 /* FIXME: currently ignored */
124 * Refresh the window in response to a server-side request.
126 void refresh_window(void)
128 /* FIXME: currently ignored */
132 * Maximise or restore the window in response to a server-side
135 void set_zoomed(int zoomed)
137 /* FIXME: currently ignored */
141 * Report whether the window is iconic, for terminal reports.
145 return 0; /* FIXME */
149 * Report the window's position, for terminal reports.
151 void get_window_pos(int *x, int *y)
153 *x = 3; *y = 4; /* FIXME */
157 * Report the window's pixel size, for terminal reports.
159 void get_window_pixels(int *x, int *y)
161 *x = 1; *y = 2; /* FIXME */
165 * Return the window or icon title.
167 char *get_window_title(int icon)
169 return inst->wintitle;
172 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
175 * We could implement warn-on-close here if we really wanted
181 void show_mouseptr(int show)
183 if (!cfg.hide_mouseptr)
186 gdk_window_set_cursor(inst->area->window, inst->currcursor);
188 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
191 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
193 struct gui_data *inst = (struct gui_data *)data;
194 int w, h, need_size = 0;
196 w = (event->width - 2*cfg.window_border) / inst->font_width;
197 h = (event->height - 2*cfg.window_border) / inst->font_height;
199 if (w != cfg.width || h != cfg.height) {
201 gdk_pixmap_unref(inst->pixmap);
211 inst->pixmap = gdk_pixmap_new(widget->window,
212 (cfg.width * inst->font_width +
213 2*cfg.window_border),
214 (cfg.height * inst->font_height +
215 2*cfg.window_border), -1);
217 gc = gdk_gc_new(inst->area->window);
218 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
219 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
220 cfg.width * inst->font_width + 2*cfg.window_border,
221 cfg.height * inst->font_height + 2*cfg.window_border);
226 term_size(h, w, cfg.savelines);
230 * Set up the colour map.
233 static const int ww[] = {
234 6, 7, 8, 9, 10, 11, 12, 13,
235 14, 15, 16, 17, 18, 19, 20, 21,
238 gboolean success[NCOLOURS];
241 inst->colmap = gdk_colormap_get_system();
243 assert(lenof(ww) == NCOLOURS);
245 for (i = 0; i < NCOLOURS; i++) {
246 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
247 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
248 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
251 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
252 FALSE, FALSE, success);
253 for (i = 0; i < NCOLOURS; i++) {
255 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
256 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
263 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
265 /* struct gui_data *inst = (struct gui_data *)data; */
268 * Pass the exposed rectangle to terminal.c, which will call us
269 * back to do the actual painting.
272 gdk_draw_pixmap(widget->window,
273 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
275 event->area.x, event->area.y,
276 event->area.x, event->area.y,
277 event->area.width, event->area.height);
282 #define KEY_PRESSED(k) \
283 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
285 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
287 /* struct gui_data *inst = (struct gui_data *)data; */
291 if (event->type == GDK_KEY_PRESS) {
295 printf("keypress: keyval = %04x, state = %08x; string =",
296 event->keyval, event->state);
297 for (i = 0; event->string[i]; i++)
298 printf(" %02x", (unsigned char) event->string[i]);
307 * - Compose key (!!! requires Unicode faff before even trying)
311 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
314 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
315 term_scroll(0, -cfg.height/2);
318 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
319 term_scroll(0, +cfg.height/2);
324 * Neither does Shift-Ins.
326 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
331 /* ALT+things gives leading Escape. */
333 strncpy(output+1, event->string, 31);
335 end = strlen(output);
336 if (event->state & GDK_MOD1_MASK)
341 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
342 if (!event->string[0] && event->keyval == '`' &&
343 (event->state & GDK_CONTROL_MASK)) {
348 /* Control-Break is the same as Control-C */
349 if (event->keyval == GDK_Break &&
350 (event->state & GDK_CONTROL_MASK)) {
355 /* Control-2, Control-Space and Control-@ are NUL */
356 if (!event->string[0] &&
357 (event->keyval == ' ' || event->keyval == '2' ||
358 event->keyval == '@') &&
359 (event->state & (GDK_SHIFT_MASK |
360 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
365 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
366 if (!event->string[0] && event->keyval == ' ' &&
367 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
368 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
373 /* We don't let GTK tell us what Backspace is! We know better. */
374 if (event->keyval == GDK_BackSpace &&
375 !(event->state & GDK_SHIFT_MASK)) {
376 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
380 /* Shift-Tab is ESC [ Z */
381 if (event->keyval == GDK_ISO_Left_Tab ||
382 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
383 end = 1 + sprintf(output+1, "\033[Z");
387 * Application keypad mode.
389 if (app_keypad_keys && !cfg.no_applic_k) {
391 switch (event->keyval) {
392 case GDK_Num_Lock: xkey = 'P'; break;
393 case GDK_KP_Divide: xkey = 'Q'; break;
394 case GDK_KP_Multiply: xkey = 'R'; break;
395 case GDK_KP_Subtract: xkey = 'S'; break;
397 * Keypad + is tricky. It covers a space that would
398 * be taken up on the VT100 by _two_ keys; so we
399 * let Shift select between the two. Worse still,
400 * in xterm function key mode we change which two...
403 if (cfg.funky_type == 2) {
404 if (event->state & GDK_SHIFT_MASK)
408 } else if (event->state & GDK_SHIFT_MASK)
413 case GDK_KP_Enter: xkey = 'M'; break;
414 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
415 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
416 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
417 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
418 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
419 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
420 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
421 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
422 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
423 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
424 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
428 if (xkey >= 'P' && xkey <= 'S')
429 end = 1 + sprintf(output+1, "\033%c", xkey);
431 end = 1 + sprintf(output+1, "\033?%c", xkey);
433 end = 1 + sprintf(output+1, "\033O%c", xkey);
439 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
440 * for integer decimal nn.)
442 * We also deal with the weird ones here. Linux VCs replace F1
443 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
444 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
449 switch (event->keyval) {
451 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
454 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
457 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
460 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
463 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
466 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
469 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
472 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
475 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
478 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
511 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
512 case GDK_Home: case GDK_KP_Home:
515 case GDK_Insert: case GDK_KP_Insert:
518 case GDK_Delete: case GDK_KP_Delete:
521 case GDK_End: case GDK_KP_End:
524 case GDK_Page_Up: case GDK_KP_Page_Up:
527 case GDK_Page_Down: case GDK_KP_Page_Down:
531 /* Reorder edit keys to physical order */
532 if (cfg.funky_type == 3 && code <= 6)
533 code = "\0\2\1\4\5\3\6"[code];
535 if (vt52_mode && code > 0 && code <= 6) {
536 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
540 if (cfg.funky_type == 5 && /* SCO function keys */
541 code >= 11 && code <= 34) {
542 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
544 switch (event->keyval) {
545 case GDK_F1: index = 0; break;
546 case GDK_F2: index = 1; break;
547 case GDK_F3: index = 2; break;
548 case GDK_F4: index = 3; break;
549 case GDK_F5: index = 4; break;
550 case GDK_F6: index = 5; break;
551 case GDK_F7: index = 6; break;
552 case GDK_F8: index = 7; break;
553 case GDK_F9: index = 8; break;
554 case GDK_F10: index = 9; break;
555 case GDK_F11: index = 10; break;
556 case GDK_F12: index = 11; break;
558 if (event->state & GDK_SHIFT_MASK) index += 12;
559 if (event->state & GDK_CONTROL_MASK) index += 24;
560 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
563 if (cfg.funky_type == 5 && /* SCO small keypad */
564 code >= 1 && code <= 6) {
565 char codes[] = "HL.FIG";
570 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
574 if ((vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
581 end = 1 + sprintf(output+1,
582 "\x1B%c", code + 'P' - 11 - offt);
584 end = 1 + sprintf(output+1,
585 "\x1BO%c", code + 'P' - 11 - offt);
588 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
589 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
592 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
594 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
596 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
599 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
600 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
604 end = 1 + sprintf(output+1, "\x1B[%d~", code);
610 * Cursor keys. (This includes the numberpad cursor keys,
611 * if we haven't already done them due to app keypad mode.)
613 * Here we also process un-numlocked un-appkeypadded KP5,
614 * which sends ESC [ G.
618 switch (event->keyval) {
619 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
620 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
621 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
622 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
623 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
627 * The arrow keys normally do ESC [ A and so on. In
628 * app cursor keys mode they do ESC O A instead.
629 * Ctrl toggles the two modes.
632 end = 1 + sprintf(output+1, "\033%c", xkey);
633 } else if (!app_cursor_keys ^
634 !(event->state & GDK_CONTROL_MASK)) {
635 end = 1 + sprintf(output+1, "\033O%c", xkey);
637 end = 1 + sprintf(output+1, "\033[%c", xkey);
648 printf("generating sequence:");
649 for (i = start; i < end; i++)
650 printf(" %02x", (unsigned char) output[i]);
655 ldisc_send(output+start, end-start, 1);
664 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
666 struct gui_data *inst = (struct gui_data *)data;
667 int shift, ctrl, alt, x, y, button, act;
671 shift = event->state & GDK_SHIFT_MASK;
672 ctrl = event->state & GDK_CONTROL_MASK;
673 alt = event->state & GDK_MOD1_MASK;
674 if (event->button == 1)
676 else if (event->button == 2)
678 else if (event->button == 3)
681 return FALSE; /* don't even know what button! */
683 switch (event->type) {
684 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
685 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
686 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
687 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
688 default: return FALSE; /* don't know this event type */
691 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
692 act != MA_CLICK && act != MA_RELEASE)
693 return TRUE; /* we ignore these in raw mouse mode */
695 x = (event->x - cfg.window_border) / inst->font_width;
696 y = (event->y - cfg.window_border) / inst->font_height;
698 term_mouse(button, act, x, y, shift, ctrl, alt);
703 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
705 struct gui_data *inst = (struct gui_data *)data;
706 int shift, ctrl, alt, x, y, button;
710 shift = event->state & GDK_SHIFT_MASK;
711 ctrl = event->state & GDK_CONTROL_MASK;
712 alt = event->state & GDK_MOD1_MASK;
713 if (event->state & GDK_BUTTON1_MASK)
715 else if (event->state & GDK_BUTTON2_MASK)
717 else if (event->state & GDK_BUTTON3_MASK)
720 return FALSE; /* don't even know what button! */
722 x = (event->x - cfg.window_border) / inst->font_width;
723 y = (event->y - cfg.window_border) / inst->font_height;
725 term_mouse(button, MA_DRAG, x, y, shift, ctrl, alt);
730 gint timer_func(gpointer data)
732 /* struct gui_data *inst = (struct gui_data *)data; */
733 extern int pty_child_is_dead(); /* declared in pty.c */
735 if (pty_child_is_dead()) {
737 * The primary child process died. We could keep the
738 * terminal open for remaining subprocesses to output to,
739 * but conventional wisdom seems to feel that that's the
740 * Wrong Thing for an xterm-alike, so we bail out now. This
741 * would be easy enough to change or make configurable if
751 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
753 /* struct gui_data *inst = (struct gui_data *)data; */
757 ret = read(sourcefd, buf, sizeof(buf));
760 * Clean termination condition is that either ret == 0, or ret
761 * < 0 and errno == EIO. Not sure why the latter, but it seems
764 if (ret == 0 || (ret < 0 && errno == EIO)) {
769 perror("read pty master");
773 from_backend(0, buf, ret);
777 void destroy(GtkWidget *widget, gpointer data)
782 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
784 has_focus = event->in;
792 * set or clear the "raw mouse message" mode
794 void set_raw_mouse_mode(int activate)
796 activate = activate && !cfg.no_mouse_rep;
797 send_raw_mouse = activate;
799 inst->currcursor = inst->rawcursor;
801 inst->currcursor = inst->textcursor;
805 void request_resize(int w, int h)
807 /* FIXME: currently ignored */
810 void palette_set(int n, int r, int g, int b)
812 /* FIXME: currently ignored */
814 void palette_reset(void)
816 /* FIXME: currently ignored */
819 void write_clip(wchar_t * data, int len, int must_deselect)
821 if (inst->pasteout_data)
822 sfree(inst->pasteout_data);
823 inst->pasteout_data = smalloc(len);
824 inst->pasteout_data_len = len;
825 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
828 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
830 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
831 GDK_SELECTION_TYPE_STRING, 1);
832 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
833 inst->compound_text_atom, 1);
837 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
838 guint info, guint time_stamp, gpointer data)
840 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
841 inst->pasteout_data, inst->pasteout_data_len);
844 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
848 if (inst->pasteout_data)
849 sfree(inst->pasteout_data);
850 inst->pasteout_data = NULL;
851 inst->pasteout_data_len = 0;
855 void request_paste(void)
858 * In Unix, pasting is asynchronous: all we can do at the
859 * moment is to call gtk_selection_convert(), and when the data
860 * comes back _then_ we can call term_do_paste().
862 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
863 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
866 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
869 if (seldata->length <= 0 ||
870 seldata->type != GDK_SELECTION_TYPE_STRING)
871 return; /* Nothing happens. */
873 if (inst->pastein_data)
874 sfree(inst->pastein_data);
876 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
877 inst->pastein_data_len = seldata->length;
878 mb_to_wc(0, 0, seldata->data, seldata->length,
879 inst->pastein_data, inst->pastein_data_len);
884 void get_clip(wchar_t ** p, int *len)
887 *p = inst->pastein_data;
888 *len = inst->pastein_data_len;
892 void set_title(char *title)
894 strncpy(inst->wintitle, title, lenof(inst->wintitle));
895 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
896 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
899 void set_icon(char *title)
901 /* FIXME: currently ignored */
904 void set_sbar(int total, int start, int page)
906 inst->sbar_adjust->lower = 0;
907 inst->sbar_adjust->upper = total;
908 inst->sbar_adjust->value = start;
909 inst->sbar_adjust->page_size = page;
910 inst->sbar_adjust->step_increment = 1;
911 inst->sbar_adjust->page_increment = page/2;
912 inst->ignore_sbar = TRUE;
913 gtk_adjustment_changed(inst->sbar_adjust);
914 inst->ignore_sbar = FALSE;
917 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
919 if (!inst->ignore_sbar)
920 term_scroll(1, (int)adj->value);
923 void sys_cursor(int x, int y)
926 * This is meaningless under X.
935 int CharWidth(Context ctx, int uc)
938 * Under X, any fixed-width font really _is_ fixed-width.
939 * Double-width characters will be dealt with using a separate
940 * font. For the moment we can simply return 1.
945 Context get_ctx(void)
948 if (!inst->area->window)
950 gc = gdk_gc_new(inst->area->window);
954 void free_ctx(Context ctx)
956 GdkGC *gc = (GdkGC *)ctx;
961 * Draw a line of text in the window, at given character
962 * coordinates, in given attributes.
964 * We are allowed to fiddle with the contents of `text'.
966 void do_text(Context ctx, int x, int y, char *text, int len,
967 unsigned long attr, int lattr)
970 GdkGC *gc = (GdkGC *)ctx;
974 * - Unicode, code pages, and ATTR_WIDE for CJK support.
975 * - LATTR_* (ESC # 4 double-width and double-height stuff)
976 * - cursor shapes other than block
980 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
981 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
982 if (attr & ATTR_REVERSE) {
987 if (cfg.bold_colour && (attr & ATTR_BOLD))
989 if (cfg.bold_colour && (attr & ATTR_BLINK))
991 if (attr & TATTR_ACTCURS) {
996 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
997 gdk_draw_rectangle(inst->pixmap, gc, 1,
998 x*inst->font_width+cfg.window_border,
999 y*inst->font_height+cfg.window_border,
1000 len*inst->font_width, inst->font_height);
1002 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1003 gdk_draw_text(inst->pixmap, inst->fonts[0], gc,
1004 x*inst->font_width+cfg.window_border,
1005 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1008 if (attr & ATTR_UNDER) {
1009 int uheight = inst->fonts[0]->ascent + 1;
1010 if (uheight >= inst->font_height)
1011 uheight = inst->font_height - 1;
1012 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1013 y*inst->font_height + uheight + cfg.window_border,
1014 (x+len)*inst->font_width-1+cfg.window_border,
1015 y*inst->font_height + uheight + cfg.window_border);
1018 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1019 x*inst->font_width+cfg.window_border,
1020 y*inst->font_height+cfg.window_border,
1021 x*inst->font_width+cfg.window_border,
1022 y*inst->font_height+cfg.window_border,
1023 len*inst->font_width, inst->font_height);
1026 void do_cursor(Context ctx, int x, int y, char *text, int len,
1027 unsigned long attr, int lattr)
1030 GdkGC *gc = (GdkGC *)ctx;
1033 * NYI: cursor shapes other than block
1035 if (attr & TATTR_PASCURS) {
1036 attr &= ~TATTR_PASCURS;
1040 do_text(ctx, x, y, text, len, attr, lattr);
1042 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1043 gdk_draw_rectangle(inst->pixmap, gc, 0,
1044 x*inst->font_width+cfg.window_border,
1045 y*inst->font_height+cfg.window_border,
1046 len*inst->font_width-1, inst->font_height-1);
1047 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1048 x*inst->font_width+cfg.window_border,
1049 y*inst->font_height+cfg.window_border,
1050 x*inst->font_width+cfg.window_border,
1051 y*inst->font_height+cfg.window_border,
1052 len*inst->font_width, inst->font_height);
1056 GdkCursor *make_mouse_ptr(int cursor_val)
1059 * Truly hideous hack: GTK doesn't allow us to set the mouse
1060 * cursor foreground and background colours unless we've _also_
1061 * created our own cursor from bitmaps. Therefore, I need to
1062 * load the `cursor' font and draw glyphs from it on to
1063 * pixmaps, in order to construct my cursors with the fg and bg
1064 * I want. This is a gross hack, but it's more self-contained
1065 * than linking in Xlib to find the X window handle to
1066 * inst->area and calling XRecolorCursor, and it's more
1067 * futureproof than hard-coding the shapes as bitmap arrays.
1069 static GdkFont *cursor_font = NULL;
1070 GdkPixmap *source, *mask;
1072 GdkColor cfg = { 0, 65535, 65535, 65535 };
1073 GdkColor cbg = { 0, 0, 0, 0 };
1074 GdkColor dfg = { 1, 65535, 65535, 65535 };
1075 GdkColor dbg = { 0, 0, 0, 0 };
1078 gint lb, rb, wid, asc, desc, w, h, x, y;
1080 if (cursor_val == -2) {
1081 gdk_font_unref(cursor_font);
1085 if (cursor_val >= 0 && !cursor_font)
1086 cursor_font = gdk_font_load("cursor");
1089 * Get the text extent of the cursor in question. We use the
1090 * mask character for this, because it's typically slightly
1091 * bigger than the main character.
1093 if (cursor_val >= 0) {
1095 text[0] = (char)cursor_val + 1;
1096 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1097 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1103 source = gdk_pixmap_new(NULL, w, h, 1);
1104 mask = gdk_pixmap_new(NULL, w, h, 1);
1107 * Draw the mask character on the mask pixmap.
1109 gc = gdk_gc_new(mask);
1110 gdk_gc_set_foreground(gc, &dbg);
1111 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1112 if (cursor_val >= 0) {
1114 text[0] = (char)cursor_val + 1;
1115 gdk_gc_set_foreground(gc, &dfg);
1116 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1121 * Draw the main character on the source pixmap.
1123 gc = gdk_gc_new(source);
1124 gdk_gc_set_foreground(gc, &dbg);
1125 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1126 if (cursor_val >= 0) {
1128 text[0] = (char)cursor_val;
1129 gdk_gc_set_foreground(gc, &dfg);
1130 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1135 * Create the cursor.
1137 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1142 gdk_pixmap_unref(source);
1143 gdk_pixmap_unref(mask);
1148 void modalfatalbox(char *p, ...)
1151 fprintf(stderr, "FATAL ERROR: ");
1153 vfprintf(stderr, p, ap);
1155 fputc('\n', stderr);
1159 int main(int argc, char **argv)
1161 extern int pty_master_fd; /* declared in pty.c */
1162 extern char **pty_argv; /* declared in pty.c */
1165 gtk_init(&argc, &argv);
1167 do_defaults(NULL, &cfg);
1169 while (--argc > 0) {
1171 if (!strcmp(p, "-fn")) {
1173 strncpy(cfg.font, *++argv, sizeof(cfg.font));
1174 cfg.font[sizeof(cfg.font)-1] = '\0';
1176 err = 1, fprintf(stderr, "pterm: -fn expects an argument\n");
1178 if (!strcmp(p, "-e")) {
1181 pty_argv = smalloc((argc+1) * sizeof(char *));
1183 for (i = 0; i < argc; i++)
1184 pty_argv[i] = argv[i];
1185 pty_argv[argc] = NULL;
1186 break; /* finished command-line processing */
1188 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1190 if (!strcmp(p, "-T")) {
1192 strncpy(cfg.wintitle, *++argv, sizeof(cfg.wintitle));
1193 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1195 err = 1, fprintf(stderr, "pterm: -T expects an argument\n");
1197 if (!strcmp(p, "-hide")) {
1198 cfg.hide_mouseptr = 1;
1202 inst->fonts[0] = gdk_font_load(cfg.font);
1203 inst->fonts[1] = NULL; /* FIXME: what about bold font? */
1204 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1205 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1207 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1211 back = &pty_backend;
1212 back->init(NULL, 0, NULL, 0);
1214 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1216 if (cfg.wintitle[0])
1217 set_title(cfg.wintitle);
1221 inst->area = gtk_drawing_area_new();
1222 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1223 inst->font_width * cfg.width + 2*cfg.window_border,
1224 inst->font_height * cfg.height + 2*cfg.window_border);
1225 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 0, 0, 0));
1226 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1227 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1228 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1229 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1231 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1235 geom.min_width = inst->font_width + 2*cfg.window_border;
1236 geom.min_height = inst->font_height + 2*cfg.window_border;
1237 geom.max_width = geom.max_height = -1;
1238 geom.base_width = 2*cfg.window_border;
1239 geom.base_height = 2*cfg.window_border;
1240 geom.width_inc = inst->font_width;
1241 geom.height_inc = inst->font_height;
1242 geom.min_aspect = geom.max_aspect = 0;
1243 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1244 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1245 GDK_HINT_RESIZE_INC);
1248 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1249 GTK_SIGNAL_FUNC(destroy), inst);
1250 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1251 GTK_SIGNAL_FUNC(delete_window), inst);
1252 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1253 GTK_SIGNAL_FUNC(key_event), inst);
1254 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1255 GTK_SIGNAL_FUNC(focus_event), inst);
1256 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1257 GTK_SIGNAL_FUNC(focus_event), inst);
1258 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1259 GTK_SIGNAL_FUNC(configure_area), inst);
1260 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1261 GTK_SIGNAL_FUNC(expose_area), inst);
1262 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1263 GTK_SIGNAL_FUNC(button_event), inst);
1264 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1265 GTK_SIGNAL_FUNC(button_event), inst);
1266 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1267 GTK_SIGNAL_FUNC(motion_event), inst);
1268 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1269 GTK_SIGNAL_FUNC(selection_received), inst);
1270 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1271 GTK_SIGNAL_FUNC(selection_get), inst);
1272 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1273 GTK_SIGNAL_FUNC(selection_clear), inst);
1274 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1275 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
1276 gtk_timeout_add(20, timer_func, inst);
1277 gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
1278 gtk_widget_add_events(GTK_WIDGET(inst->area),
1279 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1280 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1281 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
1283 gtk_widget_show(inst->area);
1284 gtk_widget_show(inst->sbar);
1285 gtk_widget_show(GTK_WIDGET(inst->hbox));
1286 gtk_widget_show(inst->window);
1288 inst->textcursor = make_mouse_ptr(GDK_XTERM);
1289 inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
1290 inst->blankcursor = make_mouse_ptr(-1);
1291 make_mouse_ptr(-2); /* clean up cursor font */
1292 inst->currcursor = inst->textcursor;
1296 term_size(24, 80, 2000);