2 * pterm - a fusion of the PuTTY terminal emulator with a Unix pty
3 * back end, all running as a GTK application. Wish me luck.
18 #include <sys/types.h>
21 #include <gdk/gdkkeysyms.h>
24 #include <X11/Xutil.h>
26 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
30 #define CAT2(x,y) x ## y
31 #define CAT(x,y) CAT2(x,y)
32 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
34 #define NCOLOURS (lenof(((Config *)0)->colours))
37 GtkWidget *window, *area, *sbar;
39 GtkAdjustment *sbar_adjust;
41 GdkFont *fonts[2]; /* normal and bold (for now!) */
42 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
43 GdkColor cols[NCOLOURS];
45 wchar_t *pastein_data;
48 int pasteout_data_len;
49 int font_width, font_height;
52 guint term_paste_idle_id;
53 GdkAtom compound_text_atom;
56 char wintitle[sizeof(((Config *)0)->wintitle)];
57 char icontitle[sizeof(((Config *)0)->wintitle)];
58 int master_fd, master_func_id, exited;
68 struct gui_data *inst;
71 static int send_raw_mouse;
73 static char *app_name = "pterm";
75 char *x_get_default(char *key)
77 return XGetDefault(GDK_DISPLAY(), app_name, key);
80 void ldisc_update(void *frontend, int echo, int edit)
83 * This is a stub in pterm. If I ever produce a Unix
84 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
85 * then it will require some termios manoeuvring analogous to
86 * that in the Windows plink.c, but here it's meaningless.
90 int askappend(void *frontend, char *filename)
93 * Logging in an xterm-alike is liable to be something you only
94 * do at serious diagnostic need. Hence, I'm going to take the
95 * easy option for now and assume we always want to overwrite
96 * log files. I can always make it properly configurable later.
101 void logevent(void *frontend, char *string)
104 * This is not a very helpful function: events are logged
105 * pretty much exclusively by the back end, and our pty back
106 * end is self-contained. So we need do nothing.
110 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
112 struct gui_data *inst = (struct gui_data *)frontend;
115 return inst->font_height;
117 return inst->font_width;
121 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
122 * into a cooked one (SELECT, EXTEND, PASTE).
124 * In Unix, this is not configurable; the X button arrangement is
125 * rock-solid across all applications, everyone has a three-button
126 * mouse or a means of faking it, and there is no need to switch
127 * buttons around at all.
129 Mouse_Button translate_button(void *frontend, Mouse_Button button)
131 /* struct gui_data *inst = (struct gui_data *)frontend; */
133 if (button == MBT_LEFT)
135 if (button == MBT_MIDDLE)
137 if (button == MBT_RIGHT)
139 return 0; /* shouldn't happen */
143 * Minimise or restore the window in response to a server-side
146 void set_iconic(void *frontend, int iconic)
149 * GTK 1.2 doesn't know how to do this.
151 #if GTK_CHECK_VERSION(2,0,0)
152 struct gui_data *inst = (struct gui_data *)frontend;
154 gtk_window_iconify(GTK_WINDOW(inst->window));
156 gtk_window_deiconify(GTK_WINDOW(inst->window));
161 * Move the window in response to a server-side request.
163 void move_window(void *frontend, int x, int y)
165 struct gui_data *inst = (struct gui_data *)frontend;
167 * I assume that when the GTK version of this call is available
168 * we should use it. Not sure how it differs from the GDK one,
171 #if GTK_CHECK_VERSION(2,0,0)
172 gtk_window_move(GTK_WINDOW(inst->window), x, y);
174 gdk_window_move(inst->window->window, x, y);
179 * Move the window to the top or bottom of the z-order in response
180 * to a server-side request.
182 void set_zorder(void *frontend, int top)
184 struct gui_data *inst = (struct gui_data *)frontend;
186 gdk_window_raise(inst->window->window);
188 gdk_window_lower(inst->window->window);
192 * Refresh the window in response to a server-side request.
194 void refresh_window(void *frontend)
196 struct gui_data *inst = (struct gui_data *)frontend;
197 term_invalidate(inst->term);
201 * Maximise or restore the window in response to a server-side
204 void set_zoomed(void *frontend, int zoomed)
207 * GTK 1.2 doesn't know how to do this.
209 #if GTK_CHECK_VERSION(2,0,0)
210 struct gui_data *inst = (struct gui_data *)frontend;
212 gtk_window_maximize(GTK_WINDOW(inst->window));
214 gtk_window_unmaximize(GTK_WINDOW(inst->window));
219 * Report whether the window is iconic, for terminal reports.
221 int is_iconic(void *frontend)
223 struct gui_data *inst = (struct gui_data *)frontend;
224 return !gdk_window_is_viewable(inst->window->window);
228 * Report the window's position, for terminal reports.
230 void get_window_pos(void *frontend, int *x, int *y)
232 struct gui_data *inst = (struct gui_data *)frontend;
234 * I assume that when the GTK version of this call is available
235 * we should use it. Not sure how it differs from the GDK one,
238 #if GTK_CHECK_VERSION(2,0,0)
239 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
241 gdk_window_get_position(inst->window->window, x, y);
246 * Report the window's pixel size, for terminal reports.
248 void get_window_pixels(void *frontend, int *x, int *y)
250 struct gui_data *inst = (struct gui_data *)frontend;
252 * I assume that when the GTK version of this call is available
253 * we should use it. Not sure how it differs from the GDK one,
256 #if GTK_CHECK_VERSION(2,0,0)
257 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
259 gdk_window_get_size(inst->window->window, x, y);
264 * Return the window or icon title.
266 char *get_window_title(void *frontend, int icon)
268 struct gui_data *inst = (struct gui_data *)frontend;
269 return icon ? inst->wintitle : inst->icontitle;
272 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
275 * We could implement warn-on-close here if we really wanted
281 static void show_mouseptr(struct gui_data *inst, int show)
283 if (!cfg.hide_mouseptr)
286 gdk_window_set_cursor(inst->area->window, inst->currcursor);
288 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
289 inst->mouseptr_visible = show;
292 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
294 struct gui_data *inst = (struct gui_data *)data;
295 int w, h, need_size = 0;
297 w = (event->width - 2*cfg.window_border) / inst->font_width;
298 h = (event->height - 2*cfg.window_border) / inst->font_height;
300 if (w != cfg.width || h != cfg.height) {
302 gdk_pixmap_unref(inst->pixmap);
312 inst->pixmap = gdk_pixmap_new(widget->window,
313 (cfg.width * inst->font_width +
314 2*cfg.window_border),
315 (cfg.height * inst->font_height +
316 2*cfg.window_border), -1);
318 gc = gdk_gc_new(inst->area->window);
319 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
320 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
321 cfg.width * inst->font_width + 2*cfg.window_border,
322 cfg.height * inst->font_height + 2*cfg.window_border);
327 term_size(inst->term, h, w, cfg.savelines);
333 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
335 struct gui_data *inst = (struct gui_data *)data;
338 * Pass the exposed rectangle to terminal.c, which will call us
339 * back to do the actual painting.
342 gdk_draw_pixmap(widget->window,
343 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
345 event->area.x, event->area.y,
346 event->area.x, event->area.y,
347 event->area.width, event->area.height);
352 #define KEY_PRESSED(k) \
353 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
355 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
357 struct gui_data *inst = (struct gui_data *)data;
361 /* By default, nothing is generated. */
365 * If Alt is being released after typing an Alt+numberpad
366 * sequence, we should generate the code that was typed.
368 * Note that we only do this if more than one key was actually
369 * pressed - I don't think Alt+NumPad4 should be ^D or that
370 * Alt+NumPad3 should be ^C, for example. There's no serious
371 * inconvenience in having to type a zero before a single-digit
374 if (event->type == GDK_KEY_RELEASE &&
375 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
376 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
377 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
379 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
381 output[0] = inst->alt_keycode;
386 if (event->type == GDK_KEY_PRESS) {
390 printf("keypress: keyval = %04x, state = %08x; string =",
391 event->keyval, event->state);
392 for (i = 0; event->string[i]; i++)
393 printf(" %02x", (unsigned char) event->string[i]);
399 * NYI: Compose key (!!! requires Unicode faff before even trying)
403 * If Alt has just been pressed, we start potentially
404 * accumulating an Alt+numberpad code. We do this by
405 * setting alt_keycode to -1 (nothing yet but plausible).
407 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
408 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
409 inst->alt_keycode = -1;
410 inst->alt_digits = 0;
411 goto done; /* this generates nothing else */
415 * If we're seeing a numberpad key press with Mod1 down,
416 * consider adding it to alt_keycode if that's sensible.
417 * Anything _else_ with Mod1 down cancels any possibility
418 * of an ALT keycode: we set alt_keycode to -2.
420 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
422 switch (event->keyval) {
423 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
424 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
425 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
426 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
427 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
428 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
429 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
430 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
431 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
432 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
435 inst->alt_keycode = -2; /* it's invalid */
438 printf("Adding digit %d to keycode %d", digit,
441 if (inst->alt_keycode == -1)
442 inst->alt_keycode = digit; /* one-digit code */
444 inst->alt_keycode = inst->alt_keycode * 10 + digit;
447 printf(" gives new code %d\n", inst->alt_keycode);
449 /* Having used this digit, we now do nothing more with it. */
455 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
458 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
459 term_scroll(inst->term, 0, -cfg.height/2);
462 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
463 term_scroll(inst->term, 0, +cfg.height/2);
468 * Neither does Shift-Ins.
470 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
475 /* ALT+things gives leading Escape. */
477 strncpy(output+1, event->string, 31);
479 end = strlen(output);
480 if (event->state & GDK_MOD1_MASK) {
482 if (end == 1) end = 0;
486 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
487 if (!event->string[0] && event->keyval == '`' &&
488 (event->state & GDK_CONTROL_MASK)) {
493 /* Control-Break is the same as Control-C */
494 if (event->keyval == GDK_Break &&
495 (event->state & GDK_CONTROL_MASK)) {
500 /* Control-2, Control-Space and Control-@ are NUL */
501 if (!event->string[0] &&
502 (event->keyval == ' ' || event->keyval == '2' ||
503 event->keyval == '@') &&
504 (event->state & (GDK_SHIFT_MASK |
505 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
510 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
511 if (!event->string[0] && event->keyval == ' ' &&
512 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
513 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
518 /* We don't let GTK tell us what Backspace is! We know better. */
519 if (event->keyval == GDK_BackSpace &&
520 !(event->state & GDK_SHIFT_MASK)) {
521 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
524 /* For Shift Backspace, do opposite of what is configured. */
525 if (event->keyval == GDK_BackSpace &&
526 (event->state & GDK_SHIFT_MASK)) {
527 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
531 /* Shift-Tab is ESC [ Z */
532 if (event->keyval == GDK_ISO_Left_Tab ||
533 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
534 end = 1 + sprintf(output+1, "\033[Z");
538 * NetHack keypad mode.
540 if (cfg.nethack_keypad) {
542 switch (event->keyval) {
543 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
544 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
545 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
546 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
547 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
548 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
549 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
550 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
551 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
555 if (event->state & GDK_SHIFT_MASK)
564 * Application keypad mode.
566 if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
568 switch (event->keyval) {
569 case GDK_Num_Lock: xkey = 'P'; break;
570 case GDK_KP_Divide: xkey = 'Q'; break;
571 case GDK_KP_Multiply: xkey = 'R'; break;
572 case GDK_KP_Subtract: xkey = 'S'; break;
574 * Keypad + is tricky. It covers a space that would
575 * be taken up on the VT100 by _two_ keys; so we
576 * let Shift select between the two. Worse still,
577 * in xterm function key mode we change which two...
580 if (cfg.funky_type == 2) {
581 if (event->state & GDK_SHIFT_MASK)
585 } else if (event->state & GDK_SHIFT_MASK)
590 case GDK_KP_Enter: xkey = 'M'; break;
591 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
592 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
593 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
594 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
595 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
596 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
597 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
598 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
599 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
600 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
601 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
604 if (inst->term->vt52_mode) {
605 if (xkey >= 'P' && xkey <= 'S')
606 end = 1 + sprintf(output+1, "\033%c", xkey);
608 end = 1 + sprintf(output+1, "\033?%c", xkey);
610 end = 1 + sprintf(output+1, "\033O%c", xkey);
616 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
617 * for integer decimal nn.)
619 * We also deal with the weird ones here. Linux VCs replace F1
620 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
621 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
626 switch (event->keyval) {
628 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
631 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
634 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
637 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
640 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
643 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
646 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
649 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
652 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
655 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
688 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
689 case GDK_Home: case GDK_KP_Home:
692 case GDK_Insert: case GDK_KP_Insert:
695 case GDK_Delete: case GDK_KP_Delete:
698 case GDK_End: case GDK_KP_End:
701 case GDK_Page_Up: case GDK_KP_Page_Up:
704 case GDK_Page_Down: case GDK_KP_Page_Down:
708 /* Reorder edit keys to physical order */
709 if (cfg.funky_type == 3 && code <= 6)
710 code = "\0\2\1\4\5\3\6"[code];
712 if (inst->term->vt52_mode && code > 0 && code <= 6) {
713 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
717 if (cfg.funky_type == 5 && /* SCO function keys */
718 code >= 11 && code <= 34) {
719 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
721 switch (event->keyval) {
722 case GDK_F1: index = 0; break;
723 case GDK_F2: index = 1; break;
724 case GDK_F3: index = 2; break;
725 case GDK_F4: index = 3; break;
726 case GDK_F5: index = 4; break;
727 case GDK_F6: index = 5; break;
728 case GDK_F7: index = 6; break;
729 case GDK_F8: index = 7; break;
730 case GDK_F9: index = 8; break;
731 case GDK_F10: index = 9; break;
732 case GDK_F11: index = 10; break;
733 case GDK_F12: index = 11; break;
735 if (event->state & GDK_SHIFT_MASK) index += 12;
736 if (event->state & GDK_CONTROL_MASK) index += 24;
737 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
740 if (cfg.funky_type == 5 && /* SCO small keypad */
741 code >= 1 && code <= 6) {
742 char codes[] = "HL.FIG";
747 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
751 if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
752 code >= 11 && code <= 24) {
758 if (inst->term->vt52_mode)
759 end = 1 + sprintf(output+1,
760 "\x1B%c", code + 'P' - 11 - offt);
762 end = 1 + sprintf(output+1,
763 "\x1BO%c", code + 'P' - 11 - offt);
766 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
767 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
770 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
771 if (inst->term->vt52_mode)
772 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
774 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
777 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
778 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
782 end = 1 + sprintf(output+1, "\x1B[%d~", code);
788 * Cursor keys. (This includes the numberpad cursor keys,
789 * if we haven't already done them due to app keypad mode.)
791 * Here we also process un-numlocked un-appkeypadded KP5,
792 * which sends ESC [ G.
796 switch (event->keyval) {
797 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
798 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
799 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
800 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
801 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
805 * The arrow keys normally do ESC [ A and so on. In
806 * app cursor keys mode they do ESC O A instead.
807 * Ctrl toggles the two modes.
809 if (inst->term->vt52_mode) {
810 end = 1 + sprintf(output+1, "\033%c", xkey);
811 } else if (!inst->term->app_cursor_keys ^
812 !(event->state & GDK_CONTROL_MASK)) {
813 end = 1 + sprintf(output+1, "\033O%c", xkey);
815 end = 1 + sprintf(output+1, "\033[%c", xkey);
828 printf("generating sequence:");
829 for (i = start; i < end; i++)
830 printf(" %02x", (unsigned char) output[i]);
834 ldisc_send(inst->ldisc, output+start, end-start, 1);
835 show_mouseptr(inst, 0);
836 term_seen_key_event(inst->term);
837 term_out(inst->term);
843 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
845 struct gui_data *inst = (struct gui_data *)data;
846 int shift, ctrl, alt, x, y, button, act;
848 show_mouseptr(inst, 1);
850 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
851 term_scroll(inst->term, 0, -5);
854 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
855 term_scroll(inst->term, 0, +5);
859 shift = event->state & GDK_SHIFT_MASK;
860 ctrl = event->state & GDK_CONTROL_MASK;
861 alt = event->state & GDK_MOD1_MASK;
862 if (event->button == 1)
864 else if (event->button == 2)
866 else if (event->button == 3)
869 return FALSE; /* don't even know what button! */
871 switch (event->type) {
872 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
873 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
874 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
875 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
876 default: return FALSE; /* don't know this event type */
879 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
880 act != MA_CLICK && act != MA_RELEASE)
881 return TRUE; /* we ignore these in raw mouse mode */
883 x = (event->x - cfg.window_border) / inst->font_width;
884 y = (event->y - cfg.window_border) / inst->font_height;
886 term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
891 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
893 struct gui_data *inst = (struct gui_data *)data;
894 int shift, ctrl, alt, x, y, button;
896 show_mouseptr(inst, 1);
898 shift = event->state & GDK_SHIFT_MASK;
899 ctrl = event->state & GDK_CONTROL_MASK;
900 alt = event->state & GDK_MOD1_MASK;
901 if (event->state & GDK_BUTTON1_MASK)
903 else if (event->state & GDK_BUTTON2_MASK)
905 else if (event->state & GDK_BUTTON3_MASK)
908 return FALSE; /* don't even know what button! */
910 x = (event->x - cfg.window_border) / inst->font_width;
911 y = (event->y - cfg.window_border) / inst->font_height;
913 term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
918 void done_with_pty(struct gui_data *inst)
920 extern void pty_close(void);
922 if (inst->master_fd >= 0) {
924 inst->master_fd = -1;
925 gtk_input_remove(inst->master_func_id);
928 if (!inst->exited && inst->back->exitcode(inst->backhandle) >= 0) {
929 int exitcode = inst->back->exitcode(inst->backhandle);
932 clean = WIFEXITED(exitcode) && (WEXITSTATUS(exitcode) == 0);
935 * Terminate now, if the Close On Exit setting is
938 if (cfg.close_on_exit == COE_ALWAYS ||
939 (cfg.close_on_exit == COE_NORMAL && clean))
943 * Otherwise, output an indication that the session has
948 if (WIFEXITED(exitcode))
949 sprintf(message, "\r\n[pterm: process terminated with exit"
950 " code %d]\r\n", WEXITSTATUS(exitcode));
951 else if (WIFSIGNALED(exitcode))
952 #ifdef HAVE_NO_STRSIGNAL
953 sprintf(message, "\r\n[pterm: process terminated on signal"
954 " %d]\r\n", WTERMSIG(exitcode));
956 sprintf(message, "\r\n[pterm: process terminated on signal"
957 " %d (%.400s)]\r\n", WTERMSIG(exitcode),
958 strsignal(WTERMSIG(exitcode)));
960 from_backend((void *)inst->term, 0, message, strlen(message));
966 void frontend_keypress(void *handle)
968 struct gui_data *inst = (struct gui_data *)handle;
971 * If our child process has exited but not closed, terminate on
978 gint timer_func(gpointer data)
980 struct gui_data *inst = (struct gui_data *)data;
982 if (inst->back->exitcode(inst->backhandle) >= 0) {
984 * The primary child process died. We could keep the
985 * terminal open for remaining subprocesses to output to,
986 * but conventional wisdom seems to feel that that's the
987 * Wrong Thing for an xterm-alike, so we bail out now
988 * (though we don't necessarily _close_ the window,
989 * depending on the state of Close On Exit). This would be
990 * easy enough to change or make configurable if necessary.
995 term_update(inst->term);
996 term_blink(inst->term, 0);
1000 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1002 struct gui_data *inst = (struct gui_data *)data;
1006 ret = read(sourcefd, buf, sizeof(buf));
1009 * Clean termination condition is that either ret == 0, or ret
1010 * < 0 and errno == EIO. Not sure why the latter, but it seems
1013 if (ret == 0 || (ret < 0 && errno == EIO)) {
1014 done_with_pty(inst);
1015 } else if (ret < 0) {
1016 perror("read pty master");
1019 from_backend(inst->term, 0, buf, ret);
1020 term_blink(inst->term, 1);
1021 term_out(inst->term);
1024 void destroy(GtkWidget *widget, gpointer data)
1029 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1031 struct gui_data *inst = (struct gui_data *)data;
1032 inst->term->has_focus = event->in;
1033 term_out(inst->term);
1034 term_update(inst->term);
1035 show_mouseptr(inst, 1);
1040 * set or clear the "raw mouse message" mode
1042 void set_raw_mouse_mode(void *frontend, int activate)
1044 struct gui_data *inst = (struct gui_data *)frontend;
1045 activate = activate && !cfg.no_mouse_rep;
1046 send_raw_mouse = activate;
1048 inst->currcursor = inst->rawcursor;
1050 inst->currcursor = inst->textcursor;
1051 show_mouseptr(inst, inst->mouseptr_visible);
1054 void request_resize(void *frontend, int w, int h)
1056 struct gui_data *inst = (struct gui_data *)frontend;
1057 int large_x, large_y;
1058 int offset_x, offset_y;
1060 GtkRequisition inner, outer;
1063 * This is a heinous hack dreamed up by the gnome-terminal
1064 * people to get around a limitation in gtk. The problem is
1065 * that in order to set the size correctly we really need to be
1066 * calling gtk_window_resize - but that needs to know the size
1067 * of the _whole window_, not the drawing area. So what we do
1068 * is to set an artificially huge size request on the drawing
1069 * area, recompute the resulting size request on the window,
1070 * and look at the difference between the two. That gives us
1071 * the x and y offsets we need to translate drawing area size
1072 * into window size for real, and then we call
1073 * gtk_window_resize.
1077 * We start by retrieving the current size of the whole window.
1078 * Adding a bit to _that_ will give us a value we can use as a
1079 * bogus size request which guarantees to be bigger than the
1080 * current size of the drawing area.
1082 get_window_pixels(inst, &large_x, &large_y);
1086 #if GTK_CHECK_VERSION(2,0,0)
1087 gtk_widget_set_size_request(inst->area, large_x, large_y);
1089 gtk_widget_set_usize(inst->area, large_x, large_y);
1091 gtk_widget_size_request(inst->area, &inner);
1092 gtk_widget_size_request(inst->window, &outer);
1094 offset_x = outer.width - inner.width;
1095 offset_y = outer.height - inner.height;
1097 area_x = inst->font_width * w + 2*cfg.window_border;
1098 area_y = inst->font_height * h + 2*cfg.window_border;
1101 * Now we must set the size request on the drawing area back to
1102 * something sensible before we commit the real resize. Best
1103 * way to do this, I think, is to set it to what the size is
1104 * really going to end up being.
1106 #if GTK_CHECK_VERSION(2,0,0)
1107 gtk_widget_set_size_request(inst->area, area_x, area_y);
1109 gtk_widget_set_usize(inst->area, area_x, area_y);
1112 #if GTK_CHECK_VERSION(2,0,0)
1113 gtk_window_resize(GTK_WINDOW(inst->window),
1114 area_x + offset_x, area_y + offset_y);
1116 gdk_window_resize(inst->window->window,
1117 area_x + offset_x, area_y + offset_y);
1121 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1123 gboolean success[1];
1125 inst->cols[n].red = r * 0x0101;
1126 inst->cols[n].green = g * 0x0101;
1127 inst->cols[n].blue = b * 0x0101;
1129 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1130 FALSE, FALSE, success);
1132 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1136 void set_window_background(struct gui_data *inst)
1138 if (inst->area && inst->area->window)
1139 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1140 if (inst->window && inst->window->window)
1141 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1144 void palette_set(void *frontend, int n, int r, int g, int b)
1146 struct gui_data *inst = (struct gui_data *)frontend;
1147 static const int first[21] = {
1148 0, 2, 4, 6, 8, 10, 12, 14,
1149 1, 3, 5, 7, 9, 11, 13, 15,
1152 real_palette_set(inst, first[n], r, g, b);
1154 real_palette_set(inst, first[n] + 1, r, g, b);
1156 set_window_background(inst);
1159 void palette_reset(void *frontend)
1161 struct gui_data *inst = (struct gui_data *)frontend;
1162 /* This maps colour indices in cfg to those used in inst->cols. */
1163 static const int ww[] = {
1164 6, 7, 8, 9, 10, 11, 12, 13,
1165 14, 15, 16, 17, 18, 19, 20, 21,
1168 gboolean success[NCOLOURS];
1171 assert(lenof(ww) == NCOLOURS);
1173 if (!inst->colmap) {
1174 inst->colmap = gdk_colormap_get_system();
1176 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1179 for (i = 0; i < NCOLOURS; i++) {
1180 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1181 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1182 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1185 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1186 FALSE, FALSE, success);
1187 for (i = 0; i < NCOLOURS; i++) {
1189 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1190 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1193 set_window_background(inst);
1196 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1198 struct gui_data *inst = (struct gui_data *)frontend;
1199 if (inst->pasteout_data)
1200 sfree(inst->pasteout_data);
1201 inst->pasteout_data = smalloc(len);
1202 inst->pasteout_data_len = len;
1203 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1206 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1207 GDK_CURRENT_TIME)) {
1208 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1209 GDK_SELECTION_TYPE_STRING, 1);
1210 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1211 inst->compound_text_atom, 1);
1215 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1216 guint info, guint time_stamp, gpointer data)
1218 struct gui_data *inst = (struct gui_data *)data;
1219 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1220 inst->pasteout_data, inst->pasteout_data_len);
1223 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1226 struct gui_data *inst = (struct gui_data *)data;
1227 term_deselect(inst->term);
1228 if (inst->pasteout_data)
1229 sfree(inst->pasteout_data);
1230 inst->pasteout_data = NULL;
1231 inst->pasteout_data_len = 0;
1235 void request_paste(void *frontend)
1237 struct gui_data *inst = (struct gui_data *)frontend;
1239 * In Unix, pasting is asynchronous: all we can do at the
1240 * moment is to call gtk_selection_convert(), and when the data
1241 * comes back _then_ we can call term_do_paste().
1243 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1244 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1247 gint idle_paste_func(gpointer data); /* forward ref */
1249 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1250 guint time, gpointer data)
1252 struct gui_data *inst = (struct gui_data *)data;
1254 if (seldata->length <= 0 ||
1255 seldata->type != GDK_SELECTION_TYPE_STRING)
1256 return; /* Nothing happens. */
1258 if (inst->pastein_data)
1259 sfree(inst->pastein_data);
1261 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1262 inst->pastein_data_len = seldata->length;
1263 mb_to_wc(0, 0, seldata->data, seldata->length,
1264 inst->pastein_data, inst->pastein_data_len);
1266 term_do_paste(inst->term);
1268 if (term_paste_pending(inst->term))
1269 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1272 gint idle_paste_func(gpointer data)
1274 struct gui_data *inst = (struct gui_data *)data;
1276 if (term_paste_pending(inst->term))
1277 term_paste(inst->term);
1279 gtk_idle_remove(inst->term_paste_idle_id);
1285 void get_clip(void *frontend, wchar_t ** p, int *len)
1287 struct gui_data *inst = (struct gui_data *)frontend;
1290 *p = inst->pastein_data;
1291 *len = inst->pastein_data_len;
1295 void set_title(void *frontend, char *title)
1297 struct gui_data *inst = (struct gui_data *)frontend;
1298 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1299 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1300 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1303 void set_icon(void *frontend, char *title)
1305 struct gui_data *inst = (struct gui_data *)frontend;
1306 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1307 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1308 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1311 void set_sbar(void *frontend, int total, int start, int page)
1313 struct gui_data *inst = (struct gui_data *)frontend;
1316 inst->sbar_adjust->lower = 0;
1317 inst->sbar_adjust->upper = total;
1318 inst->sbar_adjust->value = start;
1319 inst->sbar_adjust->page_size = page;
1320 inst->sbar_adjust->step_increment = 1;
1321 inst->sbar_adjust->page_increment = page/2;
1322 inst->ignore_sbar = TRUE;
1323 gtk_adjustment_changed(inst->sbar_adjust);
1324 inst->ignore_sbar = FALSE;
1327 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1329 struct gui_data *inst = (struct gui_data *)data;
1333 if (!inst->ignore_sbar)
1334 term_scroll(inst->term, 1, (int)adj->value);
1337 void sys_cursor(void *frontend, int x, int y)
1340 * This is meaningless under X.
1345 * This is still called when mode==BELL_VISUAL, even though the
1346 * visual bell is handled entirely within terminal.c, because we
1347 * may want to perform additional actions on any kind of bell (for
1348 * example, taskbar flashing in Windows).
1350 void beep(void *frontend, int mode)
1352 if (mode != BELL_VISUAL)
1356 int char_width(Context ctx, int uc)
1359 * Under X, any fixed-width font really _is_ fixed-width.
1360 * Double-width characters will be dealt with using a separate
1361 * font. For the moment we can simply return 1.
1366 Context get_ctx(void *frontend)
1368 struct gui_data *inst = (struct gui_data *)frontend;
1369 struct draw_ctx *dctx;
1371 if (!inst->area->window)
1374 dctx = smalloc(sizeof(*dctx));
1376 dctx->gc = gdk_gc_new(inst->area->window);
1380 void free_ctx(Context ctx)
1382 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1383 /* struct gui_data *inst = dctx->inst; */
1384 GdkGC *gc = dctx->gc;
1390 * Draw a line of text in the window, at given character
1391 * coordinates, in given attributes.
1393 * We are allowed to fiddle with the contents of `text'.
1395 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1396 unsigned long attr, int lattr)
1398 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1399 struct gui_data *inst = dctx->inst;
1400 GdkGC *gc = dctx->gc;
1402 int nfg, nbg, t, fontid, shadow, rlen;
1406 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1409 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1410 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1411 if (attr & ATTR_REVERSE) {
1416 if (cfg.bold_colour && (attr & ATTR_BOLD))
1418 if (cfg.bold_colour && (attr & ATTR_BLINK))
1420 if (attr & TATTR_ACTCURS) {
1425 fontid = shadow = 0;
1426 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1433 if (lattr != LATTR_NORM) {
1435 if (x >= inst->term->cols)
1437 if (x + len*2 > inst->term->cols)
1438 len = (inst->term->cols-x)/2; /* trim to LH half */
1446 r.x = x*inst->font_width+cfg.window_border;
1447 r.y = y*inst->font_height+cfg.window_border;
1448 r.width = rlen*inst->font_width;
1449 r.height = inst->font_height;
1450 gdk_gc_set_clip_rectangle(gc, &r);
1453 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1454 gdk_draw_rectangle(inst->pixmap, gc, 1,
1455 x*inst->font_width+cfg.window_border,
1456 y*inst->font_height+cfg.window_border,
1457 rlen*inst->font_width, inst->font_height);
1459 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1460 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1461 x*inst->font_width+cfg.window_border,
1462 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1466 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1467 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1468 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1472 if (attr & ATTR_UNDER) {
1473 int uheight = inst->fonts[0]->ascent + 1;
1474 if (uheight >= inst->font_height)
1475 uheight = inst->font_height - 1;
1476 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1477 y*inst->font_height + uheight + cfg.window_border,
1478 (x+len)*inst->font_width-1+cfg.window_border,
1479 y*inst->font_height + uheight + cfg.window_border);
1482 if (lattr != LATTR_NORM) {
1484 * I can't find any plausible StretchBlt equivalent in the
1485 * X server, so I'm going to do this the slow and painful
1486 * way. This will involve repeated calls to
1487 * gdk_draw_pixmap() to stretch the text horizontally. It's
1488 * O(N^2) in time and O(N) in network bandwidth, but you
1489 * try thinking of a better way. :-(
1492 for (i = 0; i < len * inst->font_width; i++) {
1493 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1494 x*inst->font_width+cfg.window_border + 2*i,
1495 y*inst->font_height+cfg.window_border,
1496 x*inst->font_width+cfg.window_border + 2*i+1,
1497 y*inst->font_height+cfg.window_border,
1498 len * inst->font_width - i, inst->font_height);
1501 if (lattr != LATTR_WIDE) {
1503 /* Now stretch vertically, in the same way. */
1504 if (lattr == LATTR_BOT)
1508 for (i = 0; i < inst->font_height; i+=2) {
1509 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1510 x*inst->font_width+cfg.window_border,
1511 y*inst->font_height+cfg.window_border+dt*i+db,
1512 x*inst->font_width+cfg.window_border,
1513 y*inst->font_height+cfg.window_border+dt*(i+1),
1514 len * inst->font_width, inst->font_height-i-1);
1520 void do_text(Context ctx, int x, int y, char *text, int len,
1521 unsigned long attr, int lattr)
1523 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1524 struct gui_data *inst = dctx->inst;
1525 GdkGC *gc = dctx->gc;
1527 do_text_internal(ctx, x, y, text, len, attr, lattr);
1529 if (lattr != LATTR_NORM) {
1531 if (x >= inst->term->cols)
1533 if (x + len*2 > inst->term->cols)
1534 len = (inst->term->cols-x)/2; /* trim to LH half */
1538 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1539 x*inst->font_width+cfg.window_border,
1540 y*inst->font_height+cfg.window_border,
1541 x*inst->font_width+cfg.window_border,
1542 y*inst->font_height+cfg.window_border,
1543 len*inst->font_width, inst->font_height);
1546 void do_cursor(Context ctx, int x, int y, char *text, int len,
1547 unsigned long attr, int lattr)
1549 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1550 struct gui_data *inst = dctx->inst;
1551 GdkGC *gc = dctx->gc;
1555 if (attr & TATTR_PASCURS) {
1556 attr &= ~TATTR_PASCURS;
1560 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1561 attr &= ~TATTR_ACTCURS;
1563 do_text_internal(ctx, x, y, text, len, attr, lattr);
1565 if (lattr != LATTR_NORM) {
1567 if (x >= inst->term->cols)
1569 if (x + len*2 > inst->term->cols)
1570 len = (inst->term->cols-x)/2; /* trim to LH half */
1574 if (cfg.cursor_type == 0) {
1576 * An active block cursor will already have been done by
1577 * the above do_text call, so we only need to do anything
1581 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1582 gdk_draw_rectangle(inst->pixmap, gc, 0,
1583 x*inst->font_width+cfg.window_border,
1584 y*inst->font_height+cfg.window_border,
1585 len*inst->font_width-1, inst->font_height-1);
1589 int startx, starty, dx, dy, length, i;
1593 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1594 char_width = 2*inst->font_width;
1596 char_width = inst->font_width;
1598 if (cfg.cursor_type == 1) {
1599 uheight = inst->fonts[0]->ascent + 1;
1600 if (uheight >= inst->font_height)
1601 uheight = inst->font_height - 1;
1603 startx = x * inst->font_width + cfg.window_border;
1604 starty = y * inst->font_height + cfg.window_border + uheight;
1607 length = len * char_width;
1610 if (attr & TATTR_RIGHTCURS)
1611 xadjust = char_width - 1;
1612 startx = x * inst->font_width + cfg.window_border + xadjust;
1613 starty = y * inst->font_height + cfg.window_border;
1616 length = inst->font_height;
1619 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1621 for (i = 0; i < length; i++) {
1623 gdk_draw_point(inst->pixmap, gc, startx, starty);
1629 gdk_draw_line(inst->pixmap, gc, startx, starty,
1630 startx + (length-1) * dx, starty + (length-1) * dy);
1634 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1635 x*inst->font_width+cfg.window_border,
1636 y*inst->font_height+cfg.window_border,
1637 x*inst->font_width+cfg.window_border,
1638 y*inst->font_height+cfg.window_border,
1639 len*inst->font_width, inst->font_height);
1642 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1645 * Truly hideous hack: GTK doesn't allow us to set the mouse
1646 * cursor foreground and background colours unless we've _also_
1647 * created our own cursor from bitmaps. Therefore, I need to
1648 * load the `cursor' font and draw glyphs from it on to
1649 * pixmaps, in order to construct my cursors with the fg and bg
1650 * I want. This is a gross hack, but it's more self-contained
1651 * than linking in Xlib to find the X window handle to
1652 * inst->area and calling XRecolorCursor, and it's more
1653 * futureproof than hard-coding the shapes as bitmap arrays.
1655 static GdkFont *cursor_font = NULL;
1656 GdkPixmap *source, *mask;
1658 GdkColor cfg = { 0, 65535, 65535, 65535 };
1659 GdkColor cbg = { 0, 0, 0, 0 };
1660 GdkColor dfg = { 1, 65535, 65535, 65535 };
1661 GdkColor dbg = { 0, 0, 0, 0 };
1664 gint lb, rb, wid, asc, desc, w, h, x, y;
1666 if (cursor_val == -2) {
1667 gdk_font_unref(cursor_font);
1671 if (cursor_val >= 0 && !cursor_font)
1672 cursor_font = gdk_font_load("cursor");
1675 * Get the text extent of the cursor in question. We use the
1676 * mask character for this, because it's typically slightly
1677 * bigger than the main character.
1679 if (cursor_val >= 0) {
1681 text[0] = (char)cursor_val + 1;
1682 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1683 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1689 source = gdk_pixmap_new(NULL, w, h, 1);
1690 mask = gdk_pixmap_new(NULL, w, h, 1);
1693 * Draw the mask character on the mask pixmap.
1695 gc = gdk_gc_new(mask);
1696 gdk_gc_set_foreground(gc, &dbg);
1697 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1698 if (cursor_val >= 0) {
1700 text[0] = (char)cursor_val + 1;
1701 gdk_gc_set_foreground(gc, &dfg);
1702 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1707 * Draw the main character on the source pixmap.
1709 gc = gdk_gc_new(source);
1710 gdk_gc_set_foreground(gc, &dbg);
1711 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1712 if (cursor_val >= 0) {
1714 text[0] = (char)cursor_val;
1715 gdk_gc_set_foreground(gc, &dfg);
1716 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1721 * Create the cursor.
1723 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1728 gdk_pixmap_unref(source);
1729 gdk_pixmap_unref(mask);
1734 void modalfatalbox(char *p, ...)
1737 fprintf(stderr, "FATAL ERROR: ");
1739 vfprintf(stderr, p, ap);
1741 fputc('\n', stderr);
1745 char *get_x_display(void *frontend)
1747 return gdk_get_display();
1750 static void help(FILE *fp) {
1752 "pterm option summary:\n"
1754 " --display DISPLAY Specify X display to use (note '--')\n"
1755 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1756 " -fn FONT Normal text font\n"
1757 " -fb FONT Bold text font\n"
1758 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1759 " -sl LINES Number of lines of scrollback\n"
1760 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1761 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1762 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1763 " -T TITLE Window title\n"
1764 " -ut, +ut Do(default) or do not update utmp\n"
1765 " -ls, +ls Do(default) or do not make shell a login shell\n"
1766 " -sb, +sb Do(default) or do not display a scrollbar\n"
1767 " -log PATH Log all output to a file\n"
1768 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1769 " -xrm RESOURCE-STRING Set an X resource\n"
1770 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1771 ) < 0 || fflush(fp) < 0) {
1772 perror("output error");
1777 int do_cmdline(int argc, char **argv, int do_everything)
1780 extern char **pty_argv; /* declared in pty.c */
1783 * Macros to make argument handling easier. Note that because
1784 * they need to call `continue', they cannot be contained in
1785 * the usual do {...} while (0) wrapper to make them
1786 * syntactically single statements; hence it is not legal to
1787 * use one of these macros as an unbraced statement between
1790 #define EXPECTS_ARG { \
1791 if (--argc <= 0) { \
1793 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1798 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
1807 while (--argc > 0) {
1809 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1812 strncpy(cfg.font, val, sizeof(cfg.font));
1813 cfg.font[sizeof(cfg.font)-1] = '\0';
1815 } else if (!strcmp(p, "-fb")) {
1818 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1819 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1821 } else if (!strcmp(p, "-geometry")) {
1822 int flags, x, y, w, h;
1826 flags = XParseGeometry(val, &x, &y, &w, &h);
1827 if (flags & WidthValue)
1829 if (flags & HeightValue)
1833 * Apparently setting the initial window position is
1834 * difficult in GTK 1.2. Not entirely sure why this
1835 * should be. 2.0 has gtk_window_parse_geometry(),
1836 * which would help... For the moment, though, I can't
1837 * be bothered with this.
1840 } else if (!strcmp(p, "-sl")) {
1843 cfg.savelines = atoi(val);
1845 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1846 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1847 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1852 if (!gdk_color_parse(val, &col)) {
1854 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1857 index = (!strcmp(p, "-fg") ? 0 :
1858 !strcmp(p, "-bg") ? 2 :
1859 !strcmp(p, "-bfg") ? 1 :
1860 !strcmp(p, "-bbg") ? 3 :
1861 !strcmp(p, "-cfg") ? 4 :
1862 !strcmp(p, "-cbg") ? 5 : -1);
1863 assert(index != -1);
1864 cfg.colours[index][0] = col.red / 256;
1865 cfg.colours[index][1] = col.green / 256;
1866 cfg.colours[index][2] = col.blue / 256;
1869 } else if (!strcmp(p, "-e")) {
1870 /* This option swallows all further arguments. */
1876 pty_argv = smalloc((argc+1) * sizeof(char *));
1878 for (i = 0; i < argc; i++)
1879 pty_argv[i] = argv[i];
1880 pty_argv[argc] = NULL;
1881 break; /* finished command-line processing */
1883 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1885 } else if (!strcmp(p, "-T")) {
1888 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1889 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1891 } else if (!strcmp(p, "-log")) {
1894 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1895 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1896 cfg.logtype = LGTYP_DEBUG;
1898 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1902 } else if (!strcmp(p, "-ut")) {
1906 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1908 cfg.login_shell = 0;
1910 } else if (!strcmp(p, "-ls")) {
1912 cfg.login_shell = 1;
1914 } else if (!strcmp(p, "-nethack")) {
1916 cfg.nethack_keypad = 1;
1918 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1922 } else if (!strcmp(p, "-sb")) {
1926 } else if (!strcmp(p, "-name")) {
1930 } else if (!strcmp(p, "-xrm")) {
1932 provide_xrm_string(val);
1934 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
1940 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
1947 static void block_signal(int sig, int block_it) {
1951 sigaddset(&ss, sig);
1952 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
1953 perror("sigprocmask");
1958 int main(int argc, char **argv)
1960 extern int pty_master_fd; /* declared in pty.c */
1961 extern void pty_pre_init(void); /* declared in pty.c */
1962 struct gui_data *inst;
1964 /* defer any child exit handling until we're ready to deal with
1966 block_signal(SIGCHLD, 1);
1970 gtk_init(&argc, &argv);
1972 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
1974 do_defaults(NULL, &cfg);
1975 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
1979 * Create an instance structure and initialise to zeroes
1981 inst = smalloc(sizeof(*inst));
1982 memset(inst, 0, sizeof(*inst));
1983 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
1985 inst->fonts[0] = gdk_font_load(cfg.font);
1986 if (!inst->fonts[0]) {
1987 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1990 if (cfg.boldfont[0]) {
1991 inst->fonts[1] = gdk_font_load(cfg.boldfont);
1992 if (!inst->fonts[1]) {
1993 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1998 inst->fonts[1] = NULL;
2000 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2001 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2003 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2007 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2009 if (cfg.wintitle[0])
2010 set_title(inst, cfg.wintitle);
2012 set_title(inst, "pterm");
2015 * Set up the colour map.
2017 palette_reset(inst);
2019 inst->area = gtk_drawing_area_new();
2020 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
2021 inst->font_width * cfg.width + 2*cfg.window_border,
2022 inst->font_height * cfg.height + 2*cfg.window_border);
2023 if (cfg.scrollbar) {
2024 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2025 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2027 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2028 if (cfg.scrollbar) {
2029 if (cfg.scrollbar_on_left)
2030 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2032 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2034 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2036 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2040 geom.min_width = inst->font_width + 2*cfg.window_border;
2041 geom.min_height = inst->font_height + 2*cfg.window_border;
2042 geom.max_width = geom.max_height = -1;
2043 geom.base_width = 2*cfg.window_border;
2044 geom.base_height = 2*cfg.window_border;
2045 geom.width_inc = inst->font_width;
2046 geom.height_inc = inst->font_height;
2047 geom.min_aspect = geom.max_aspect = 0;
2048 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2049 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2050 GDK_HINT_RESIZE_INC);
2053 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2054 GTK_SIGNAL_FUNC(destroy), inst);
2055 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2056 GTK_SIGNAL_FUNC(delete_window), inst);
2057 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2058 GTK_SIGNAL_FUNC(key_event), inst);
2059 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2060 GTK_SIGNAL_FUNC(key_event), inst);
2061 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2062 GTK_SIGNAL_FUNC(focus_event), inst);
2063 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2064 GTK_SIGNAL_FUNC(focus_event), inst);
2065 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2066 GTK_SIGNAL_FUNC(configure_area), inst);
2067 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2068 GTK_SIGNAL_FUNC(expose_area), inst);
2069 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2070 GTK_SIGNAL_FUNC(button_event), inst);
2071 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2072 GTK_SIGNAL_FUNC(button_event), inst);
2073 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2074 GTK_SIGNAL_FUNC(motion_event), inst);
2075 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2076 GTK_SIGNAL_FUNC(selection_received), inst);
2077 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2078 GTK_SIGNAL_FUNC(selection_get), inst);
2079 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2080 GTK_SIGNAL_FUNC(selection_clear), inst);
2082 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2083 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2084 gtk_timeout_add(20, timer_func, inst);
2085 gtk_widget_add_events(GTK_WIDGET(inst->area),
2086 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2087 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2088 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2090 gtk_widget_show(inst->area);
2092 gtk_widget_show(inst->sbar);
2093 gtk_widget_show(GTK_WIDGET(inst->hbox));
2094 gtk_widget_show(inst->window);
2096 set_window_background(inst);
2098 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2099 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2100 inst->blankcursor = make_mouse_ptr(inst, -1);
2101 make_mouse_ptr(inst, -2); /* clean up cursor font */
2102 inst->currcursor = inst->textcursor;
2103 show_mouseptr(inst, 1);
2105 inst->term = term_init(&cfg, inst);
2106 inst->logctx = log_init(inst);
2107 term_provide_logctx(inst->term, inst->logctx);
2109 inst->back = &pty_backend;
2110 inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
2111 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2113 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2115 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
2118 ldisc_create(&cfg, inst->term, inst->back, inst->backhandle, inst);
2119 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2121 inst->master_fd = pty_master_fd;
2122 inst->exited = FALSE;
2123 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
2124 pty_input_func, inst);
2126 /* now we're reday to deal with the child exit handler being
2128 block_signal(SIGCHLD, 0);