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.
17 #include <sys/types.h>
20 #include <gdk/gdkkeysyms.h>
23 #include <X11/Xutil.h>
25 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
29 #define CAT2(x,y) x ## y
30 #define CAT(x,y) CAT2(x,y)
31 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
33 #define NCOLOURS (lenof(((Config *)0)->colours))
36 GtkWidget *window, *area, *sbar;
38 GtkAdjustment *sbar_adjust;
40 GdkFont *fonts[2]; /* normal and bold (for now!) */
41 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
42 GdkColor cols[NCOLOURS];
44 wchar_t *pastein_data;
47 int pasteout_data_len;
48 int font_width, font_height;
51 guint term_paste_idle_id;
52 GdkAtom compound_text_atom;
55 char wintitle[sizeof(((Config *)0)->wintitle)];
56 char icontitle[sizeof(((Config *)0)->wintitle)];
57 int master_fd, master_func_id, exited;
67 struct gui_data *inst;
70 static int send_raw_mouse;
72 static char *app_name = "pterm";
74 char *x_get_default(char *key)
76 return XGetDefault(GDK_DISPLAY(), app_name, key);
79 void ldisc_update(void *frontend, int echo, int edit)
82 * This is a stub in pterm. If I ever produce a Unix
83 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
84 * then it will require some termios manoeuvring analogous to
85 * that in the Windows plink.c, but here it's meaningless.
89 int askappend(void *frontend, char *filename)
92 * Logging in an xterm-alike is liable to be something you only
93 * do at serious diagnostic need. Hence, I'm going to take the
94 * easy option for now and assume we always want to overwrite
95 * log files. I can always make it properly configurable later.
100 void logevent(void *frontend, char *string)
103 * This is not a very helpful function: events are logged
104 * pretty much exclusively by the back end, and our pty back
105 * end is self-contained. So we need do nothing.
109 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
111 struct gui_data *inst = (struct gui_data *)frontend;
114 return inst->font_height;
116 return inst->font_width;
120 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
121 * into a cooked one (SELECT, EXTEND, PASTE).
123 * In Unix, this is not configurable; the X button arrangement is
124 * rock-solid across all applications, everyone has a three-button
125 * mouse or a means of faking it, and there is no need to switch
126 * buttons around at all.
128 Mouse_Button translate_button(void *frontend, Mouse_Button button)
130 /* struct gui_data *inst = (struct gui_data *)frontend; */
132 if (button == MBT_LEFT)
134 if (button == MBT_MIDDLE)
136 if (button == MBT_RIGHT)
138 return 0; /* shouldn't happen */
142 * Minimise or restore the window in response to a server-side
145 void set_iconic(void *frontend, int iconic)
148 * GTK 1.2 doesn't know how to do this.
150 #if GTK_CHECK_VERSION(2,0,0)
151 struct gui_data *inst = (struct gui_data *)frontend;
153 gtk_window_iconify(GTK_WINDOW(inst->window));
155 gtk_window_deiconify(GTK_WINDOW(inst->window));
160 * Move the window in response to a server-side request.
162 void move_window(void *frontend, int x, int y)
164 struct gui_data *inst = (struct gui_data *)frontend;
166 * I assume that when the GTK version of this call is available
167 * we should use it. Not sure how it differs from the GDK one,
170 #if GTK_CHECK_VERSION(2,0,0)
171 gtk_window_move(GTK_WINDOW(inst->window), x, y);
173 gdk_window_move(inst->window->window, x, y);
178 * Move the window to the top or bottom of the z-order in response
179 * to a server-side request.
181 void set_zorder(void *frontend, int top)
183 struct gui_data *inst = (struct gui_data *)frontend;
185 gdk_window_raise(inst->window->window);
187 gdk_window_lower(inst->window->window);
191 * Refresh the window in response to a server-side request.
193 void refresh_window(void *frontend)
195 struct gui_data *inst = (struct gui_data *)frontend;
196 term_invalidate(inst->term);
200 * Maximise or restore the window in response to a server-side
203 void set_zoomed(void *frontend, int zoomed)
206 * GTK 1.2 doesn't know how to do this.
208 #if GTK_CHECK_VERSION(2,0,0)
209 struct gui_data *inst = (struct gui_data *)frontend;
211 gtk_window_maximize(GTK_WINDOW(inst->window));
213 gtk_window_unmaximize(GTK_WINDOW(inst->window));
218 * Report whether the window is iconic, for terminal reports.
220 int is_iconic(void *frontend)
222 struct gui_data *inst = (struct gui_data *)frontend;
223 return !gdk_window_is_viewable(inst->window->window);
227 * Report the window's position, for terminal reports.
229 void get_window_pos(void *frontend, int *x, int *y)
231 struct gui_data *inst = (struct gui_data *)frontend;
233 * I assume that when the GTK version of this call is available
234 * we should use it. Not sure how it differs from the GDK one,
237 #if GTK_CHECK_VERSION(2,0,0)
238 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
240 gdk_window_get_position(inst->window->window, x, y);
245 * Report the window's pixel size, for terminal reports.
247 void get_window_pixels(void *frontend, int *x, int *y)
249 struct gui_data *inst = (struct gui_data *)frontend;
251 * I assume that when the GTK version of this call is available
252 * we should use it. Not sure how it differs from the GDK one,
255 #if GTK_CHECK_VERSION(2,0,0)
256 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
258 gdk_window_get_size(inst->window->window, x, y);
263 * Return the window or icon title.
265 char *get_window_title(void *frontend, int icon)
267 struct gui_data *inst = (struct gui_data *)frontend;
268 return icon ? inst->wintitle : inst->icontitle;
271 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
274 * We could implement warn-on-close here if we really wanted
280 static void show_mouseptr(struct gui_data *inst, int show)
282 if (!cfg.hide_mouseptr)
285 gdk_window_set_cursor(inst->area->window, inst->currcursor);
287 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
288 inst->mouseptr_visible = show;
291 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
293 struct gui_data *inst = (struct gui_data *)data;
294 int w, h, need_size = 0;
296 w = (event->width - 2*cfg.window_border) / inst->font_width;
297 h = (event->height - 2*cfg.window_border) / inst->font_height;
299 if (w != cfg.width || h != cfg.height) {
301 gdk_pixmap_unref(inst->pixmap);
311 inst->pixmap = gdk_pixmap_new(widget->window,
312 (cfg.width * inst->font_width +
313 2*cfg.window_border),
314 (cfg.height * inst->font_height +
315 2*cfg.window_border), -1);
317 gc = gdk_gc_new(inst->area->window);
318 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
319 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
320 cfg.width * inst->font_width + 2*cfg.window_border,
321 cfg.height * inst->font_height + 2*cfg.window_border);
326 term_size(inst->term, h, w, cfg.savelines);
332 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
334 struct gui_data *inst = (struct gui_data *)data;
337 * Pass the exposed rectangle to terminal.c, which will call us
338 * back to do the actual painting.
341 gdk_draw_pixmap(widget->window,
342 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
344 event->area.x, event->area.y,
345 event->area.x, event->area.y,
346 event->area.width, event->area.height);
351 #define KEY_PRESSED(k) \
352 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
354 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
356 struct gui_data *inst = (struct gui_data *)data;
360 /* By default, nothing is generated. */
364 * If Alt is being released after typing an Alt+numberpad
365 * sequence, we should generate the code that was typed.
367 * Note that we only do this if more than one key was actually
368 * pressed - I don't think Alt+NumPad4 should be ^D or that
369 * Alt+NumPad3 should be ^C, for example. There's no serious
370 * inconvenience in having to type a zero before a single-digit
373 if (event->type == GDK_KEY_RELEASE &&
374 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
375 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
376 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
378 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
380 output[0] = inst->alt_keycode;
385 if (event->type == GDK_KEY_PRESS) {
389 printf("keypress: keyval = %04x, state = %08x; string =",
390 event->keyval, event->state);
391 for (i = 0; event->string[i]; i++)
392 printf(" %02x", (unsigned char) event->string[i]);
398 * NYI: Compose key (!!! requires Unicode faff before even trying)
402 * If Alt has just been pressed, we start potentially
403 * accumulating an Alt+numberpad code. We do this by
404 * setting alt_keycode to -1 (nothing yet but plausible).
406 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
407 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
408 inst->alt_keycode = -1;
409 inst->alt_digits = 0;
410 goto done; /* this generates nothing else */
414 * If we're seeing a numberpad key press with Mod1 down,
415 * consider adding it to alt_keycode if that's sensible.
416 * Anything _else_ with Mod1 down cancels any possibility
417 * of an ALT keycode: we set alt_keycode to -2.
419 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
421 switch (event->keyval) {
422 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
423 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
424 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
425 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
426 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
427 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
428 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
429 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
430 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
431 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
434 inst->alt_keycode = -2; /* it's invalid */
437 printf("Adding digit %d to keycode %d", digit,
440 if (inst->alt_keycode == -1)
441 inst->alt_keycode = digit; /* one-digit code */
443 inst->alt_keycode = inst->alt_keycode * 10 + digit;
446 printf(" gives new code %d\n", inst->alt_keycode);
448 /* Having used this digit, we now do nothing more with it. */
454 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
457 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
458 term_scroll(inst->term, 0, -cfg.height/2);
461 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
462 term_scroll(inst->term, 0, +cfg.height/2);
467 * Neither does Shift-Ins.
469 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
474 /* ALT+things gives leading Escape. */
476 strncpy(output+1, event->string, 31);
478 end = strlen(output);
479 if (event->state & GDK_MOD1_MASK) {
481 if (end == 1) end = 0;
485 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
486 if (!event->string[0] && event->keyval == '`' &&
487 (event->state & GDK_CONTROL_MASK)) {
492 /* Control-Break is the same as Control-C */
493 if (event->keyval == GDK_Break &&
494 (event->state & GDK_CONTROL_MASK)) {
499 /* Control-2, Control-Space and Control-@ are NUL */
500 if (!event->string[0] &&
501 (event->keyval == ' ' || event->keyval == '2' ||
502 event->keyval == '@') &&
503 (event->state & (GDK_SHIFT_MASK |
504 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
509 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
510 if (!event->string[0] && event->keyval == ' ' &&
511 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
512 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
517 /* We don't let GTK tell us what Backspace is! We know better. */
518 if (event->keyval == GDK_BackSpace &&
519 !(event->state & GDK_SHIFT_MASK)) {
520 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
523 /* For Shift Backspace, do opposite of what is configured. */
524 if (event->keyval == GDK_BackSpace &&
525 (event->state & GDK_SHIFT_MASK)) {
526 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
530 /* Shift-Tab is ESC [ Z */
531 if (event->keyval == GDK_ISO_Left_Tab ||
532 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
533 end = 1 + sprintf(output+1, "\033[Z");
537 * NetHack keypad mode.
539 if (cfg.nethack_keypad) {
541 switch (event->keyval) {
542 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
543 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
544 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
545 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
546 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
547 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
548 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
549 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
550 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
554 if (event->state & GDK_SHIFT_MASK)
563 * Application keypad mode.
565 if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
567 switch (event->keyval) {
568 case GDK_Num_Lock: xkey = 'P'; break;
569 case GDK_KP_Divide: xkey = 'Q'; break;
570 case GDK_KP_Multiply: xkey = 'R'; break;
571 case GDK_KP_Subtract: xkey = 'S'; break;
573 * Keypad + is tricky. It covers a space that would
574 * be taken up on the VT100 by _two_ keys; so we
575 * let Shift select between the two. Worse still,
576 * in xterm function key mode we change which two...
579 if (cfg.funky_type == 2) {
580 if (event->state & GDK_SHIFT_MASK)
584 } else if (event->state & GDK_SHIFT_MASK)
589 case GDK_KP_Enter: xkey = 'M'; break;
590 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
591 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
592 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
593 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
594 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
595 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
596 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
597 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
598 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
599 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
600 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
603 if (inst->term->vt52_mode) {
604 if (xkey >= 'P' && xkey <= 'S')
605 end = 1 + sprintf(output+1, "\033%c", xkey);
607 end = 1 + sprintf(output+1, "\033?%c", xkey);
609 end = 1 + sprintf(output+1, "\033O%c", xkey);
615 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
616 * for integer decimal nn.)
618 * We also deal with the weird ones here. Linux VCs replace F1
619 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
620 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
625 switch (event->keyval) {
627 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
630 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
633 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
636 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
639 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
642 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
645 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
648 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
651 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
654 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
687 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
688 case GDK_Home: case GDK_KP_Home:
691 case GDK_Insert: case GDK_KP_Insert:
694 case GDK_Delete: case GDK_KP_Delete:
697 case GDK_End: case GDK_KP_End:
700 case GDK_Page_Up: case GDK_KP_Page_Up:
703 case GDK_Page_Down: case GDK_KP_Page_Down:
707 /* Reorder edit keys to physical order */
708 if (cfg.funky_type == 3 && code <= 6)
709 code = "\0\2\1\4\5\3\6"[code];
711 if (inst->term->vt52_mode && code > 0 && code <= 6) {
712 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
716 if (cfg.funky_type == 5 && /* SCO function keys */
717 code >= 11 && code <= 34) {
718 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
720 switch (event->keyval) {
721 case GDK_F1: index = 0; break;
722 case GDK_F2: index = 1; break;
723 case GDK_F3: index = 2; break;
724 case GDK_F4: index = 3; break;
725 case GDK_F5: index = 4; break;
726 case GDK_F6: index = 5; break;
727 case GDK_F7: index = 6; break;
728 case GDK_F8: index = 7; break;
729 case GDK_F9: index = 8; break;
730 case GDK_F10: index = 9; break;
731 case GDK_F11: index = 10; break;
732 case GDK_F12: index = 11; break;
734 if (event->state & GDK_SHIFT_MASK) index += 12;
735 if (event->state & GDK_CONTROL_MASK) index += 24;
736 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
739 if (cfg.funky_type == 5 && /* SCO small keypad */
740 code >= 1 && code <= 6) {
741 char codes[] = "HL.FIG";
746 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
750 if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
751 code >= 11 && code <= 24) {
757 if (inst->term->vt52_mode)
758 end = 1 + sprintf(output+1,
759 "\x1B%c", code + 'P' - 11 - offt);
761 end = 1 + sprintf(output+1,
762 "\x1BO%c", code + 'P' - 11 - offt);
765 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
766 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
769 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
770 if (inst->term->vt52_mode)
771 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
773 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
776 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
777 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
781 end = 1 + sprintf(output+1, "\x1B[%d~", code);
787 * Cursor keys. (This includes the numberpad cursor keys,
788 * if we haven't already done them due to app keypad mode.)
790 * Here we also process un-numlocked un-appkeypadded KP5,
791 * which sends ESC [ G.
795 switch (event->keyval) {
796 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
797 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
798 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
799 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
800 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
804 * The arrow keys normally do ESC [ A and so on. In
805 * app cursor keys mode they do ESC O A instead.
806 * Ctrl toggles the two modes.
808 if (inst->term->vt52_mode) {
809 end = 1 + sprintf(output+1, "\033%c", xkey);
810 } else if (!inst->term->app_cursor_keys ^
811 !(event->state & GDK_CONTROL_MASK)) {
812 end = 1 + sprintf(output+1, "\033O%c", xkey);
814 end = 1 + sprintf(output+1, "\033[%c", xkey);
827 printf("generating sequence:");
828 for (i = start; i < end; i++)
829 printf(" %02x", (unsigned char) output[i]);
833 ldisc_send(inst->ldisc, output+start, end-start, 1);
834 show_mouseptr(inst, 0);
835 term_seen_key_event(inst->term);
836 term_out(inst->term);
842 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
844 struct gui_data *inst = (struct gui_data *)data;
845 int shift, ctrl, alt, x, y, button, act;
847 show_mouseptr(inst, 1);
849 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
850 term_scroll(inst->term, 0, -5);
853 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
854 term_scroll(inst->term, 0, +5);
858 shift = event->state & GDK_SHIFT_MASK;
859 ctrl = event->state & GDK_CONTROL_MASK;
860 alt = event->state & GDK_MOD1_MASK;
861 if (event->button == 1)
863 else if (event->button == 2)
865 else if (event->button == 3)
868 return FALSE; /* don't even know what button! */
870 switch (event->type) {
871 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
872 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
873 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
874 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
875 default: return FALSE; /* don't know this event type */
878 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
879 act != MA_CLICK && act != MA_RELEASE)
880 return TRUE; /* we ignore these in raw mouse mode */
882 x = (event->x - cfg.window_border) / inst->font_width;
883 y = (event->y - cfg.window_border) / inst->font_height;
885 term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
890 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
892 struct gui_data *inst = (struct gui_data *)data;
893 int shift, ctrl, alt, x, y, button;
895 show_mouseptr(inst, 1);
897 shift = event->state & GDK_SHIFT_MASK;
898 ctrl = event->state & GDK_CONTROL_MASK;
899 alt = event->state & GDK_MOD1_MASK;
900 if (event->state & GDK_BUTTON1_MASK)
902 else if (event->state & GDK_BUTTON2_MASK)
904 else if (event->state & GDK_BUTTON3_MASK)
907 return FALSE; /* don't even know what button! */
909 x = (event->x - cfg.window_border) / inst->font_width;
910 y = (event->y - cfg.window_border) / inst->font_height;
912 term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
917 void done_with_pty(struct gui_data *inst)
919 extern void pty_close(void);
921 if (inst->master_fd >= 0) {
923 inst->master_fd = -1;
924 gtk_input_remove(inst->master_func_id);
927 if (!inst->exited && inst->back->exitcode(inst->backhandle) >= 0) {
928 int exitcode = inst->back->exitcode(inst->backhandle);
931 clean = WIFEXITED(exitcode) && (WEXITSTATUS(exitcode) == 0);
934 * Terminate now, if the Close On Exit setting is
937 if (cfg.close_on_exit == COE_ALWAYS ||
938 (cfg.close_on_exit == COE_NORMAL && clean))
942 * Otherwise, output an indication that the session has
947 if (WIFEXITED(exitcode))
948 sprintf(message, "\r\n[pterm: process terminated with exit"
949 " code %d]\r\n", WEXITSTATUS(exitcode));
950 else if (WIFSIGNALED(exitcode))
951 #ifdef HAVE_NO_STRSIGNAL
952 sprintf(message, "\r\n[pterm: process terminated on signal"
953 " %d]\r\n", WTERMSIG(exitcode));
955 sprintf(message, "\r\n[pterm: process terminated on signal"
956 " %d (%.400s)]\r\n", WTERMSIG(exitcode),
957 strsignal(WTERMSIG(exitcode)));
959 from_backend((void *)inst->term, 0, message, strlen(message));
965 void frontend_keypress(void *handle)
967 struct gui_data *inst = (struct gui_data *)handle;
970 * If our child process has exited but not closed, terminate on
977 gint timer_func(gpointer data)
979 struct gui_data *inst = (struct gui_data *)data;
981 if (inst->back->exitcode(inst->backhandle) >= 0) {
983 * The primary child process died. We could keep the
984 * terminal open for remaining subprocesses to output to,
985 * but conventional wisdom seems to feel that that's the
986 * Wrong Thing for an xterm-alike, so we bail out now
987 * (though we don't necessarily _close_ the window,
988 * depending on the state of Close On Exit). This would be
989 * easy enough to change or make configurable if necessary.
994 term_update(inst->term);
995 term_blink(inst->term, 0);
999 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
1001 struct gui_data *inst = (struct gui_data *)data;
1005 ret = read(sourcefd, buf, sizeof(buf));
1008 * Clean termination condition is that either ret == 0, or ret
1009 * < 0 and errno == EIO. Not sure why the latter, but it seems
1012 if (ret == 0 || (ret < 0 && errno == EIO)) {
1013 done_with_pty(inst);
1014 } else if (ret < 0) {
1015 perror("read pty master");
1018 from_backend(inst->term, 0, buf, ret);
1019 term_blink(inst->term, 1);
1020 term_out(inst->term);
1023 void destroy(GtkWidget *widget, gpointer data)
1028 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1030 struct gui_data *inst = (struct gui_data *)data;
1031 inst->term->has_focus = event->in;
1032 term_out(inst->term);
1033 term_update(inst->term);
1034 show_mouseptr(inst, 1);
1039 * set or clear the "raw mouse message" mode
1041 void set_raw_mouse_mode(void *frontend, int activate)
1043 struct gui_data *inst = (struct gui_data *)frontend;
1044 activate = activate && !cfg.no_mouse_rep;
1045 send_raw_mouse = activate;
1047 inst->currcursor = inst->rawcursor;
1049 inst->currcursor = inst->textcursor;
1050 show_mouseptr(inst, inst->mouseptr_visible);
1053 void request_resize(void *frontend, int w, int h)
1055 struct gui_data *inst = (struct gui_data *)frontend;
1056 int large_x, large_y;
1057 int offset_x, offset_y;
1059 GtkRequisition inner, outer;
1062 * This is a heinous hack dreamed up by the gnome-terminal
1063 * people to get around a limitation in gtk. The problem is
1064 * that in order to set the size correctly we really need to be
1065 * calling gtk_window_resize - but that needs to know the size
1066 * of the _whole window_, not the drawing area. So what we do
1067 * is to set an artificially huge size request on the drawing
1068 * area, recompute the resulting size request on the window,
1069 * and look at the difference between the two. That gives us
1070 * the x and y offsets we need to translate drawing area size
1071 * into window size for real, and then we call
1072 * gtk_window_resize.
1076 * We start by retrieving the current size of the whole window.
1077 * Adding a bit to _that_ will give us a value we can use as a
1078 * bogus size request which guarantees to be bigger than the
1079 * current size of the drawing area.
1081 get_window_pixels(inst, &large_x, &large_y);
1085 #if GTK_CHECK_VERSION(2,0,0)
1086 gtk_widget_set_size_request(inst->area, large_x, large_y);
1088 gtk_widget_set_usize(inst->area, large_x, large_y);
1090 gtk_widget_size_request(inst->area, &inner);
1091 gtk_widget_size_request(inst->window, &outer);
1093 offset_x = outer.width - inner.width;
1094 offset_y = outer.height - inner.height;
1096 area_x = inst->font_width * w + 2*cfg.window_border;
1097 area_y = inst->font_height * h + 2*cfg.window_border;
1100 * Now we must set the size request on the drawing area back to
1101 * something sensible before we commit the real resize. Best
1102 * way to do this, I think, is to set it to what the size is
1103 * really going to end up being.
1105 #if GTK_CHECK_VERSION(2,0,0)
1106 gtk_widget_set_size_request(inst->area, area_x, area_y);
1108 gtk_widget_set_usize(inst->area, area_x, area_y);
1111 #if GTK_CHECK_VERSION(2,0,0)
1112 gtk_window_resize(GTK_WINDOW(inst->window),
1113 area_x + offset_x, area_y + offset_y);
1115 gdk_window_resize(inst->window->window,
1116 area_x + offset_x, area_y + offset_y);
1120 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1122 gboolean success[1];
1124 inst->cols[n].red = r * 0x0101;
1125 inst->cols[n].green = g * 0x0101;
1126 inst->cols[n].blue = b * 0x0101;
1128 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1129 FALSE, FALSE, success);
1131 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1135 void set_window_background(struct gui_data *inst)
1137 if (inst->area && inst->area->window)
1138 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1139 if (inst->window && inst->window->window)
1140 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1143 void palette_set(void *frontend, int n, int r, int g, int b)
1145 struct gui_data *inst = (struct gui_data *)frontend;
1146 static const int first[21] = {
1147 0, 2, 4, 6, 8, 10, 12, 14,
1148 1, 3, 5, 7, 9, 11, 13, 15,
1151 real_palette_set(inst, first[n], r, g, b);
1153 real_palette_set(inst, first[n] + 1, r, g, b);
1155 set_window_background(inst);
1158 void palette_reset(void *frontend)
1160 struct gui_data *inst = (struct gui_data *)frontend;
1161 /* This maps colour indices in cfg to those used in inst->cols. */
1162 static const int ww[] = {
1163 6, 7, 8, 9, 10, 11, 12, 13,
1164 14, 15, 16, 17, 18, 19, 20, 21,
1167 gboolean success[NCOLOURS];
1170 assert(lenof(ww) == NCOLOURS);
1172 if (!inst->colmap) {
1173 inst->colmap = gdk_colormap_get_system();
1175 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1178 for (i = 0; i < NCOLOURS; i++) {
1179 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1180 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1181 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1184 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1185 FALSE, FALSE, success);
1186 for (i = 0; i < NCOLOURS; i++) {
1188 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1189 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1192 set_window_background(inst);
1195 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1197 struct gui_data *inst = (struct gui_data *)frontend;
1198 if (inst->pasteout_data)
1199 sfree(inst->pasteout_data);
1200 inst->pasteout_data = smalloc(len);
1201 inst->pasteout_data_len = len;
1202 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1205 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1206 GDK_CURRENT_TIME)) {
1207 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1208 GDK_SELECTION_TYPE_STRING, 1);
1209 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1210 inst->compound_text_atom, 1);
1214 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1215 guint info, guint time_stamp, gpointer data)
1217 struct gui_data *inst = (struct gui_data *)data;
1218 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1219 inst->pasteout_data, inst->pasteout_data_len);
1222 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1225 struct gui_data *inst = (struct gui_data *)data;
1226 term_deselect(inst->term);
1227 if (inst->pasteout_data)
1228 sfree(inst->pasteout_data);
1229 inst->pasteout_data = NULL;
1230 inst->pasteout_data_len = 0;
1234 void request_paste(void *frontend)
1236 struct gui_data *inst = (struct gui_data *)frontend;
1238 * In Unix, pasting is asynchronous: all we can do at the
1239 * moment is to call gtk_selection_convert(), and when the data
1240 * comes back _then_ we can call term_do_paste().
1242 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1243 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1246 gint idle_paste_func(gpointer data); /* forward ref */
1248 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1249 guint time, gpointer data)
1251 struct gui_data *inst = (struct gui_data *)data;
1253 if (seldata->length <= 0 ||
1254 seldata->type != GDK_SELECTION_TYPE_STRING)
1255 return; /* Nothing happens. */
1257 if (inst->pastein_data)
1258 sfree(inst->pastein_data);
1260 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1261 inst->pastein_data_len = seldata->length;
1262 mb_to_wc(0, 0, seldata->data, seldata->length,
1263 inst->pastein_data, inst->pastein_data_len);
1265 term_do_paste(inst->term);
1267 if (term_paste_pending(inst->term))
1268 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1271 gint idle_paste_func(gpointer data)
1273 struct gui_data *inst = (struct gui_data *)data;
1275 if (term_paste_pending(inst->term))
1276 term_paste(inst->term);
1278 gtk_idle_remove(inst->term_paste_idle_id);
1284 void get_clip(void *frontend, wchar_t ** p, int *len)
1286 struct gui_data *inst = (struct gui_data *)frontend;
1289 *p = inst->pastein_data;
1290 *len = inst->pastein_data_len;
1294 void set_title(void *frontend, char *title)
1296 struct gui_data *inst = (struct gui_data *)frontend;
1297 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1298 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1299 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1302 void set_icon(void *frontend, char *title)
1304 struct gui_data *inst = (struct gui_data *)frontend;
1305 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1306 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1307 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1310 void set_sbar(void *frontend, int total, int start, int page)
1312 struct gui_data *inst = (struct gui_data *)frontend;
1315 inst->sbar_adjust->lower = 0;
1316 inst->sbar_adjust->upper = total;
1317 inst->sbar_adjust->value = start;
1318 inst->sbar_adjust->page_size = page;
1319 inst->sbar_adjust->step_increment = 1;
1320 inst->sbar_adjust->page_increment = page/2;
1321 inst->ignore_sbar = TRUE;
1322 gtk_adjustment_changed(inst->sbar_adjust);
1323 inst->ignore_sbar = FALSE;
1326 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1328 struct gui_data *inst = (struct gui_data *)data;
1332 if (!inst->ignore_sbar)
1333 term_scroll(inst->term, 1, (int)adj->value);
1336 void sys_cursor(void *frontend, int x, int y)
1339 * This is meaningless under X.
1344 * This is still called when mode==BELL_VISUAL, even though the
1345 * visual bell is handled entirely within terminal.c, because we
1346 * may want to perform additional actions on any kind of bell (for
1347 * example, taskbar flashing in Windows).
1349 void beep(void *frontend, int mode)
1351 if (mode != BELL_VISUAL)
1355 int char_width(Context ctx, int uc)
1358 * Under X, any fixed-width font really _is_ fixed-width.
1359 * Double-width characters will be dealt with using a separate
1360 * font. For the moment we can simply return 1.
1365 Context get_ctx(void *frontend)
1367 struct gui_data *inst = (struct gui_data *)frontend;
1368 struct draw_ctx *dctx;
1370 if (!inst->area->window)
1373 dctx = smalloc(sizeof(*dctx));
1375 dctx->gc = gdk_gc_new(inst->area->window);
1379 void free_ctx(Context ctx)
1381 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1382 /* struct gui_data *inst = dctx->inst; */
1383 GdkGC *gc = dctx->gc;
1389 * Draw a line of text in the window, at given character
1390 * coordinates, in given attributes.
1392 * We are allowed to fiddle with the contents of `text'.
1394 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1395 unsigned long attr, int lattr)
1397 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1398 struct gui_data *inst = dctx->inst;
1399 GdkGC *gc = dctx->gc;
1401 int nfg, nbg, t, fontid, shadow, rlen;
1405 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1408 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1409 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1410 if (attr & ATTR_REVERSE) {
1415 if (cfg.bold_colour && (attr & ATTR_BOLD))
1417 if (cfg.bold_colour && (attr & ATTR_BLINK))
1419 if (attr & TATTR_ACTCURS) {
1424 fontid = shadow = 0;
1425 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1432 if (lattr != LATTR_NORM) {
1434 if (x >= inst->term->cols)
1436 if (x + len*2 > inst->term->cols)
1437 len = (inst->term->cols-x)/2; /* trim to LH half */
1445 r.x = x*inst->font_width+cfg.window_border;
1446 r.y = y*inst->font_height+cfg.window_border;
1447 r.width = rlen*inst->font_width;
1448 r.height = inst->font_height;
1449 gdk_gc_set_clip_rectangle(gc, &r);
1452 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1453 gdk_draw_rectangle(inst->pixmap, gc, 1,
1454 x*inst->font_width+cfg.window_border,
1455 y*inst->font_height+cfg.window_border,
1456 rlen*inst->font_width, inst->font_height);
1458 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1459 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1460 x*inst->font_width+cfg.window_border,
1461 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1465 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1466 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1467 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1471 if (attr & ATTR_UNDER) {
1472 int uheight = inst->fonts[0]->ascent + 1;
1473 if (uheight >= inst->font_height)
1474 uheight = inst->font_height - 1;
1475 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1476 y*inst->font_height + uheight + cfg.window_border,
1477 (x+len)*inst->font_width-1+cfg.window_border,
1478 y*inst->font_height + uheight + cfg.window_border);
1481 if (lattr != LATTR_NORM) {
1483 * I can't find any plausible StretchBlt equivalent in the
1484 * X server, so I'm going to do this the slow and painful
1485 * way. This will involve repeated calls to
1486 * gdk_draw_pixmap() to stretch the text horizontally. It's
1487 * O(N^2) in time and O(N) in network bandwidth, but you
1488 * try thinking of a better way. :-(
1491 for (i = 0; i < len * inst->font_width; i++) {
1492 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1493 x*inst->font_width+cfg.window_border + 2*i,
1494 y*inst->font_height+cfg.window_border,
1495 x*inst->font_width+cfg.window_border + 2*i+1,
1496 y*inst->font_height+cfg.window_border,
1497 len * inst->font_width - i, inst->font_height);
1500 if (lattr != LATTR_WIDE) {
1502 /* Now stretch vertically, in the same way. */
1503 if (lattr == LATTR_BOT)
1507 for (i = 0; i < inst->font_height; i+=2) {
1508 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1509 x*inst->font_width+cfg.window_border,
1510 y*inst->font_height+cfg.window_border+dt*i+db,
1511 x*inst->font_width+cfg.window_border,
1512 y*inst->font_height+cfg.window_border+dt*(i+1),
1513 len * inst->font_width, inst->font_height-i-1);
1519 void do_text(Context ctx, int x, int y, char *text, int len,
1520 unsigned long attr, int lattr)
1522 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1523 struct gui_data *inst = dctx->inst;
1524 GdkGC *gc = dctx->gc;
1526 do_text_internal(ctx, x, y, text, len, attr, lattr);
1528 if (lattr != LATTR_NORM) {
1530 if (x >= inst->term->cols)
1532 if (x + len*2 > inst->term->cols)
1533 len = (inst->term->cols-x)/2; /* trim to LH half */
1537 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1538 x*inst->font_width+cfg.window_border,
1539 y*inst->font_height+cfg.window_border,
1540 x*inst->font_width+cfg.window_border,
1541 y*inst->font_height+cfg.window_border,
1542 len*inst->font_width, inst->font_height);
1545 void do_cursor(Context ctx, int x, int y, char *text, int len,
1546 unsigned long attr, int lattr)
1548 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1549 struct gui_data *inst = dctx->inst;
1550 GdkGC *gc = dctx->gc;
1554 if (attr & TATTR_PASCURS) {
1555 attr &= ~TATTR_PASCURS;
1559 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1560 attr &= ~TATTR_ACTCURS;
1562 do_text_internal(ctx, x, y, text, len, attr, lattr);
1564 if (lattr != LATTR_NORM) {
1566 if (x >= inst->term->cols)
1568 if (x + len*2 > inst->term->cols)
1569 len = (inst->term->cols-x)/2; /* trim to LH half */
1573 if (cfg.cursor_type == 0) {
1575 * An active block cursor will already have been done by
1576 * the above do_text call, so we only need to do anything
1580 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1581 gdk_draw_rectangle(inst->pixmap, gc, 0,
1582 x*inst->font_width+cfg.window_border,
1583 y*inst->font_height+cfg.window_border,
1584 len*inst->font_width-1, inst->font_height-1);
1588 int startx, starty, dx, dy, length, i;
1592 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1593 char_width = 2*inst->font_width;
1595 char_width = inst->font_width;
1597 if (cfg.cursor_type == 1) {
1598 uheight = inst->fonts[0]->ascent + 1;
1599 if (uheight >= inst->font_height)
1600 uheight = inst->font_height - 1;
1602 startx = x * inst->font_width + cfg.window_border;
1603 starty = y * inst->font_height + cfg.window_border + uheight;
1606 length = len * char_width;
1609 if (attr & TATTR_RIGHTCURS)
1610 xadjust = char_width - 1;
1611 startx = x * inst->font_width + cfg.window_border + xadjust;
1612 starty = y * inst->font_height + cfg.window_border;
1615 length = inst->font_height;
1618 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1620 for (i = 0; i < length; i++) {
1622 gdk_draw_point(inst->pixmap, gc, startx, starty);
1628 gdk_draw_line(inst->pixmap, gc, startx, starty,
1629 startx + (length-1) * dx, starty + (length-1) * dy);
1633 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1634 x*inst->font_width+cfg.window_border,
1635 y*inst->font_height+cfg.window_border,
1636 x*inst->font_width+cfg.window_border,
1637 y*inst->font_height+cfg.window_border,
1638 len*inst->font_width, inst->font_height);
1641 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1644 * Truly hideous hack: GTK doesn't allow us to set the mouse
1645 * cursor foreground and background colours unless we've _also_
1646 * created our own cursor from bitmaps. Therefore, I need to
1647 * load the `cursor' font and draw glyphs from it on to
1648 * pixmaps, in order to construct my cursors with the fg and bg
1649 * I want. This is a gross hack, but it's more self-contained
1650 * than linking in Xlib to find the X window handle to
1651 * inst->area and calling XRecolorCursor, and it's more
1652 * futureproof than hard-coding the shapes as bitmap arrays.
1654 static GdkFont *cursor_font = NULL;
1655 GdkPixmap *source, *mask;
1657 GdkColor cfg = { 0, 65535, 65535, 65535 };
1658 GdkColor cbg = { 0, 0, 0, 0 };
1659 GdkColor dfg = { 1, 65535, 65535, 65535 };
1660 GdkColor dbg = { 0, 0, 0, 0 };
1663 gint lb, rb, wid, asc, desc, w, h, x, y;
1665 if (cursor_val == -2) {
1666 gdk_font_unref(cursor_font);
1670 if (cursor_val >= 0 && !cursor_font)
1671 cursor_font = gdk_font_load("cursor");
1674 * Get the text extent of the cursor in question. We use the
1675 * mask character for this, because it's typically slightly
1676 * bigger than the main character.
1678 if (cursor_val >= 0) {
1680 text[0] = (char)cursor_val + 1;
1681 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1682 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1688 source = gdk_pixmap_new(NULL, w, h, 1);
1689 mask = gdk_pixmap_new(NULL, w, h, 1);
1692 * Draw the mask character on the mask pixmap.
1694 gc = gdk_gc_new(mask);
1695 gdk_gc_set_foreground(gc, &dbg);
1696 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1697 if (cursor_val >= 0) {
1699 text[0] = (char)cursor_val + 1;
1700 gdk_gc_set_foreground(gc, &dfg);
1701 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1706 * Draw the main character on the source pixmap.
1708 gc = gdk_gc_new(source);
1709 gdk_gc_set_foreground(gc, &dbg);
1710 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1711 if (cursor_val >= 0) {
1713 text[0] = (char)cursor_val;
1714 gdk_gc_set_foreground(gc, &dfg);
1715 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1720 * Create the cursor.
1722 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1727 gdk_pixmap_unref(source);
1728 gdk_pixmap_unref(mask);
1733 void modalfatalbox(char *p, ...)
1736 fprintf(stderr, "FATAL ERROR: ");
1738 vfprintf(stderr, p, ap);
1740 fputc('\n', stderr);
1744 char *get_x_display(void *frontend)
1746 return gdk_get_display();
1749 static void help(FILE *fp) {
1751 "pterm option summary:\n"
1753 " --display DISPLAY Specify X display to use (note '--')\n"
1754 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1755 " -fn FONT Normal text font\n"
1756 " -fb FONT Bold text font\n"
1757 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1758 " -sl LINES Number of lines of scrollback\n"
1759 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1760 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1761 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1762 " -T TITLE Window title\n"
1763 " -ut, +ut Do(default) or do not update utmp\n"
1764 " -ls, +ls Do(default) or do not make shell a login shell\n"
1765 " -sb, +sb Do(default) or do not display a scrollbar\n"
1766 " -log PATH Log all output to a file\n"
1767 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1768 " -xrm RESOURCE-STRING Set an X resource\n"
1769 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1770 ) < 0 || fflush(fp) < 0) {
1771 perror("output error");
1776 int do_cmdline(int argc, char **argv, int do_everything)
1779 extern char **pty_argv; /* declared in pty.c */
1782 * Macros to make argument handling easier. Note that because
1783 * they need to call `continue', they cannot be contained in
1784 * the usual do {...} while (0) wrapper to make them
1785 * syntactically single statements; hence it is not legal to
1786 * use one of these macros as an unbraced statement between
1789 #define EXPECTS_ARG { \
1790 if (--argc <= 0) { \
1792 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1797 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
1806 while (--argc > 0) {
1808 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1811 strncpy(cfg.font, val, sizeof(cfg.font));
1812 cfg.font[sizeof(cfg.font)-1] = '\0';
1814 } else if (!strcmp(p, "-fb")) {
1817 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1818 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1820 } else if (!strcmp(p, "-geometry")) {
1821 int flags, x, y, w, h;
1825 flags = XParseGeometry(val, &x, &y, &w, &h);
1826 if (flags & WidthValue)
1828 if (flags & HeightValue)
1832 * Apparently setting the initial window position is
1833 * difficult in GTK 1.2. Not entirely sure why this
1834 * should be. 2.0 has gtk_window_parse_geometry(),
1835 * which would help... For the moment, though, I can't
1836 * be bothered with this.
1839 } else if (!strcmp(p, "-sl")) {
1842 cfg.savelines = atoi(val);
1844 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1845 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1846 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1851 if (!gdk_color_parse(val, &col)) {
1853 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1856 index = (!strcmp(p, "-fg") ? 0 :
1857 !strcmp(p, "-bg") ? 2 :
1858 !strcmp(p, "-bfg") ? 1 :
1859 !strcmp(p, "-bbg") ? 3 :
1860 !strcmp(p, "-cfg") ? 4 :
1861 !strcmp(p, "-cbg") ? 5 : -1);
1862 assert(index != -1);
1863 cfg.colours[index][0] = col.red / 256;
1864 cfg.colours[index][1] = col.green / 256;
1865 cfg.colours[index][2] = col.blue / 256;
1868 } else if (!strcmp(p, "-e")) {
1869 /* This option swallows all further arguments. */
1875 pty_argv = smalloc((argc+1) * sizeof(char *));
1877 for (i = 0; i < argc; i++)
1878 pty_argv[i] = argv[i];
1879 pty_argv[argc] = NULL;
1880 break; /* finished command-line processing */
1882 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1884 } else if (!strcmp(p, "-T")) {
1887 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1888 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1890 } else if (!strcmp(p, "-log")) {
1893 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1894 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1895 cfg.logtype = LGTYP_DEBUG;
1897 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1901 } else if (!strcmp(p, "-ut")) {
1905 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1907 cfg.login_shell = 0;
1909 } else if (!strcmp(p, "-ls")) {
1911 cfg.login_shell = 1;
1913 } else if (!strcmp(p, "-nethack")) {
1915 cfg.nethack_keypad = 1;
1917 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1921 } else if (!strcmp(p, "-sb")) {
1925 } else if (!strcmp(p, "-name")) {
1929 } else if (!strcmp(p, "-xrm")) {
1931 provide_xrm_string(val);
1933 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
1939 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
1946 static void block_signal(int sig, int block_it) {
1950 sigaddset(&ss, sig);
1951 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
1952 perror("sigprocmask");
1957 int main(int argc, char **argv)
1959 extern int pty_master_fd; /* declared in pty.c */
1960 extern void pty_pre_init(void); /* declared in pty.c */
1961 struct gui_data *inst;
1963 /* defer any child exit handling until we're ready to deal with
1965 block_signal(SIGCHLD, 1);
1969 gtk_init(&argc, &argv);
1971 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
1973 do_defaults(NULL, &cfg);
1974 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
1978 * Create an instance structure and initialise to zeroes
1980 inst = smalloc(sizeof(*inst));
1981 memset(inst, 0, sizeof(*inst));
1982 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
1984 inst->fonts[0] = gdk_font_load(cfg.font);
1985 if (!inst->fonts[0]) {
1986 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1989 if (cfg.boldfont[0]) {
1990 inst->fonts[1] = gdk_font_load(cfg.boldfont);
1991 if (!inst->fonts[1]) {
1992 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1997 inst->fonts[1] = NULL;
1999 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2000 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2002 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2006 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2008 if (cfg.wintitle[0])
2009 set_title(inst, cfg.wintitle);
2011 set_title(inst, "pterm");
2014 * Set up the colour map.
2016 palette_reset(inst);
2018 inst->area = gtk_drawing_area_new();
2019 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
2020 inst->font_width * cfg.width + 2*cfg.window_border,
2021 inst->font_height * cfg.height + 2*cfg.window_border);
2022 if (cfg.scrollbar) {
2023 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2024 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2026 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
2027 if (cfg.scrollbar) {
2028 if (cfg.scrollbar_on_left)
2029 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2031 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2033 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2035 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2039 geom.min_width = inst->font_width + 2*cfg.window_border;
2040 geom.min_height = inst->font_height + 2*cfg.window_border;
2041 geom.max_width = geom.max_height = -1;
2042 geom.base_width = 2*cfg.window_border;
2043 geom.base_height = 2*cfg.window_border;
2044 geom.width_inc = inst->font_width;
2045 geom.height_inc = inst->font_height;
2046 geom.min_aspect = geom.max_aspect = 0;
2047 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2048 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2049 GDK_HINT_RESIZE_INC);
2052 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2053 GTK_SIGNAL_FUNC(destroy), inst);
2054 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2055 GTK_SIGNAL_FUNC(delete_window), inst);
2056 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2057 GTK_SIGNAL_FUNC(key_event), inst);
2058 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2059 GTK_SIGNAL_FUNC(key_event), inst);
2060 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2061 GTK_SIGNAL_FUNC(focus_event), inst);
2062 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2063 GTK_SIGNAL_FUNC(focus_event), inst);
2064 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2065 GTK_SIGNAL_FUNC(configure_area), inst);
2066 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2067 GTK_SIGNAL_FUNC(expose_area), inst);
2068 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2069 GTK_SIGNAL_FUNC(button_event), inst);
2070 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2071 GTK_SIGNAL_FUNC(button_event), inst);
2072 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2073 GTK_SIGNAL_FUNC(motion_event), inst);
2074 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2075 GTK_SIGNAL_FUNC(selection_received), inst);
2076 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2077 GTK_SIGNAL_FUNC(selection_get), inst);
2078 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2079 GTK_SIGNAL_FUNC(selection_clear), inst);
2081 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2082 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2083 gtk_timeout_add(20, timer_func, inst);
2084 gtk_widget_add_events(GTK_WIDGET(inst->area),
2085 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2086 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2087 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2089 gtk_widget_show(inst->area);
2091 gtk_widget_show(inst->sbar);
2092 gtk_widget_show(GTK_WIDGET(inst->hbox));
2093 gtk_widget_show(inst->window);
2095 set_window_background(inst);
2097 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2098 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2099 inst->blankcursor = make_mouse_ptr(inst, -1);
2100 make_mouse_ptr(inst, -2); /* clean up cursor font */
2101 inst->currcursor = inst->textcursor;
2102 show_mouseptr(inst, 1);
2104 inst->term = term_init(&cfg, inst);
2105 inst->logctx = log_init(inst);
2106 term_provide_logctx(inst->term, inst->logctx);
2108 inst->back = &pty_backend;
2109 inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
2110 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2112 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2114 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
2116 inst->ldisc = ldisc_create(inst->term, inst->back, inst->backhandle, inst);
2117 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2119 inst->master_fd = pty_master_fd;
2120 inst->exited = FALSE;
2121 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
2122 pty_input_func, inst);
2124 /* now we're reday to deal with the child exit handler being
2126 block_signal(SIGCHLD, 0);