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 <X11/Xutil.h>
22 #include <gdk/gdkkeysyms.h>
24 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
28 #define CAT2(x,y) x ## y
29 #define CAT(x,y) CAT2(x,y)
30 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
32 #define NCOLOURS (lenof(((Config *)0)->colours))
35 GtkWidget *window, *area, *sbar;
37 GtkAdjustment *sbar_adjust;
39 GdkFont *fonts[2]; /* normal and bold (for now!) */
40 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
41 GdkColor cols[NCOLOURS];
43 wchar_t *pastein_data;
46 int pasteout_data_len;
47 int font_width, font_height;
50 guint term_paste_idle_id;
51 GdkAtom compound_text_atom;
54 char wintitle[sizeof(((Config *)0)->wintitle)];
55 char icontitle[sizeof(((Config *)0)->wintitle)];
56 int master_fd, master_func_id, exited;
66 struct gui_data *inst;
69 static int send_raw_mouse;
71 void ldisc_update(void *frontend, int echo, int edit)
74 * This is a stub in pterm. If I ever produce a Unix
75 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
76 * then it will require some termios manoeuvring analogous to
77 * that in the Windows plink.c, but here it's meaningless.
81 int askappend(void *frontend, char *filename)
84 * Logging in an xterm-alike is liable to be something you only
85 * do at serious diagnostic need. Hence, I'm going to take the
86 * easy option for now and assume we always want to overwrite
87 * log files. I can always make it properly configurable later.
92 void logevent(void *frontend, char *string)
95 * This is not a very helpful function: events are logged
96 * pretty much exclusively by the back end, and our pty back
97 * end is self-contained. So we need do nothing.
101 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
103 struct gui_data *inst = (struct gui_data *)frontend;
106 return inst->font_height;
108 return inst->font_width;
112 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
113 * into a cooked one (SELECT, EXTEND, PASTE).
115 * In Unix, this is not configurable; the X button arrangement is
116 * rock-solid across all applications, everyone has a three-button
117 * mouse or a means of faking it, and there is no need to switch
118 * buttons around at all.
120 Mouse_Button translate_button(void *frontend, Mouse_Button button)
122 /* struct gui_data *inst = (struct gui_data *)frontend; */
124 if (button == MBT_LEFT)
126 if (button == MBT_MIDDLE)
128 if (button == MBT_RIGHT)
130 return 0; /* shouldn't happen */
134 * Minimise or restore the window in response to a server-side
137 void set_iconic(void *frontend, int iconic)
140 * GTK 1.2 doesn't know how to do this.
142 #if GTK_CHECK_VERSION(2,0,0)
143 struct gui_data *inst = (struct gui_data *)frontend;
145 gtk_window_iconify(GTK_WINDOW(inst->window));
147 gtk_window_deiconify(GTK_WINDOW(inst->window));
152 * Move the window in response to a server-side request.
154 void move_window(void *frontend, int x, int y)
156 struct gui_data *inst = (struct gui_data *)frontend;
158 * I assume that when the GTK version of this call is available
159 * we should use it. Not sure how it differs from the GDK one,
162 #if GTK_CHECK_VERSION(2,0,0)
163 gtk_window_move(GTK_WINDOW(inst->window), x, y);
165 gdk_window_move(inst->window->window, x, y);
170 * Move the window to the top or bottom of the z-order in response
171 * to a server-side request.
173 void set_zorder(void *frontend, int top)
175 struct gui_data *inst = (struct gui_data *)frontend;
177 gdk_window_raise(inst->window->window);
179 gdk_window_lower(inst->window->window);
183 * Refresh the window in response to a server-side request.
185 void refresh_window(void *frontend)
187 struct gui_data *inst = (struct gui_data *)frontend;
188 term_invalidate(inst->term);
192 * Maximise or restore the window in response to a server-side
195 void set_zoomed(void *frontend, int zoomed)
198 * GTK 1.2 doesn't know how to do this.
200 #if GTK_CHECK_VERSION(2,0,0)
201 struct gui_data *inst = (struct gui_data *)frontend;
203 gtk_window_maximize(GTK_WINDOW(inst->window));
205 gtk_window_unmaximize(GTK_WINDOW(inst->window));
210 * Report whether the window is iconic, for terminal reports.
212 int is_iconic(void *frontend)
214 struct gui_data *inst = (struct gui_data *)frontend;
215 return !gdk_window_is_viewable(inst->window->window);
219 * Report the window's position, for terminal reports.
221 void get_window_pos(void *frontend, int *x, int *y)
223 struct gui_data *inst = (struct gui_data *)frontend;
225 * I assume that when the GTK version of this call is available
226 * we should use it. Not sure how it differs from the GDK one,
229 #if GTK_CHECK_VERSION(2,0,0)
230 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
232 gdk_window_get_position(inst->window->window, x, y);
237 * Report the window's pixel size, for terminal reports.
239 void get_window_pixels(void *frontend, int *x, int *y)
241 struct gui_data *inst = (struct gui_data *)frontend;
243 * I assume that when the GTK version of this call is available
244 * we should use it. Not sure how it differs from the GDK one,
247 #if GTK_CHECK_VERSION(2,0,0)
248 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
250 gdk_window_get_size(inst->window->window, x, y);
255 * Return the window or icon title.
257 char *get_window_title(void *frontend, int icon)
259 struct gui_data *inst = (struct gui_data *)frontend;
260 return icon ? inst->wintitle : inst->icontitle;
263 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
266 * We could implement warn-on-close here if we really wanted
272 static void show_mouseptr(struct gui_data *inst, int show)
274 if (!cfg.hide_mouseptr)
277 gdk_window_set_cursor(inst->area->window, inst->currcursor);
279 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
280 inst->mouseptr_visible = show;
283 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
285 struct gui_data *inst = (struct gui_data *)data;
286 int w, h, need_size = 0;
288 w = (event->width - 2*cfg.window_border) / inst->font_width;
289 h = (event->height - 2*cfg.window_border) / inst->font_height;
291 if (w != cfg.width || h != cfg.height) {
293 gdk_pixmap_unref(inst->pixmap);
303 inst->pixmap = gdk_pixmap_new(widget->window,
304 (cfg.width * inst->font_width +
305 2*cfg.window_border),
306 (cfg.height * inst->font_height +
307 2*cfg.window_border), -1);
309 gc = gdk_gc_new(inst->area->window);
310 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
311 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
312 cfg.width * inst->font_width + 2*cfg.window_border,
313 cfg.height * inst->font_height + 2*cfg.window_border);
318 term_size(inst->term, h, w, cfg.savelines);
324 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
326 struct gui_data *inst = (struct gui_data *)data;
329 * Pass the exposed rectangle to terminal.c, which will call us
330 * back to do the actual painting.
333 gdk_draw_pixmap(widget->window,
334 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
336 event->area.x, event->area.y,
337 event->area.x, event->area.y,
338 event->area.width, event->area.height);
343 #define KEY_PRESSED(k) \
344 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
346 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
348 struct gui_data *inst = (struct gui_data *)data;
352 /* By default, nothing is generated. */
356 * If Alt is being released after typing an Alt+numberpad
357 * sequence, we should generate the code that was typed.
359 * Note that we only do this if more than one key was actually
360 * pressed - I don't think Alt+NumPad4 should be ^D or that
361 * Alt+NumPad3 should be ^C, for example. There's no serious
362 * inconvenience in having to type a zero before a single-digit
365 if (event->type == GDK_KEY_RELEASE &&
366 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
367 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
368 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
370 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
372 output[0] = inst->alt_keycode;
377 if (event->type == GDK_KEY_PRESS) {
381 printf("keypress: keyval = %04x, state = %08x; string =",
382 event->keyval, event->state);
383 for (i = 0; event->string[i]; i++)
384 printf(" %02x", (unsigned char) event->string[i]);
390 * NYI: Compose key (!!! requires Unicode faff before even trying)
394 * If Alt has just been pressed, we start potentially
395 * accumulating an Alt+numberpad code. We do this by
396 * setting alt_keycode to -1 (nothing yet but plausible).
398 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
399 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
400 inst->alt_keycode = -1;
401 inst->alt_digits = 0;
402 goto done; /* this generates nothing else */
406 * If we're seeing a numberpad key press with Mod1 down,
407 * consider adding it to alt_keycode if that's sensible.
408 * Anything _else_ with Mod1 down cancels any possibility
409 * of an ALT keycode: we set alt_keycode to -2.
411 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
413 switch (event->keyval) {
414 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
415 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
416 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
417 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
418 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
419 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
420 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
421 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
422 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
423 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
426 inst->alt_keycode = -2; /* it's invalid */
429 printf("Adding digit %d to keycode %d", digit,
432 if (inst->alt_keycode == -1)
433 inst->alt_keycode = digit; /* one-digit code */
435 inst->alt_keycode = inst->alt_keycode * 10 + digit;
438 printf(" gives new code %d\n", inst->alt_keycode);
440 /* Having used this digit, we now do nothing more with it. */
446 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
449 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
450 term_scroll(inst->term, 0, -cfg.height/2);
453 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
454 term_scroll(inst->term, 0, +cfg.height/2);
459 * Neither does Shift-Ins.
461 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
466 /* ALT+things gives leading Escape. */
468 strncpy(output+1, event->string, 31);
470 end = strlen(output);
471 if (event->state & GDK_MOD1_MASK) {
473 if (end == 1) end = 0;
477 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
478 if (!event->string[0] && event->keyval == '`' &&
479 (event->state & GDK_CONTROL_MASK)) {
484 /* Control-Break is the same as Control-C */
485 if (event->keyval == GDK_Break &&
486 (event->state & GDK_CONTROL_MASK)) {
491 /* Control-2, Control-Space and Control-@ are NUL */
492 if (!event->string[0] &&
493 (event->keyval == ' ' || event->keyval == '2' ||
494 event->keyval == '@') &&
495 (event->state & (GDK_SHIFT_MASK |
496 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
501 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
502 if (!event->string[0] && event->keyval == ' ' &&
503 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
504 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
509 /* We don't let GTK tell us what Backspace is! We know better. */
510 if (event->keyval == GDK_BackSpace &&
511 !(event->state & GDK_SHIFT_MASK)) {
512 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
515 /* For Shift Backspace, do opposite of what is configured. */
516 if (event->keyval == GDK_BackSpace &&
517 (event->state & GDK_SHIFT_MASK)) {
518 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
522 /* Shift-Tab is ESC [ Z */
523 if (event->keyval == GDK_ISO_Left_Tab ||
524 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
525 end = 1 + sprintf(output+1, "\033[Z");
529 * NetHack keypad mode.
531 if (cfg.nethack_keypad) {
533 switch (event->keyval) {
534 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
535 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
536 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
537 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
538 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
539 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
540 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
541 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
542 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
546 if (event->state & GDK_SHIFT_MASK)
555 * Application keypad mode.
557 if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
559 switch (event->keyval) {
560 case GDK_Num_Lock: xkey = 'P'; break;
561 case GDK_KP_Divide: xkey = 'Q'; break;
562 case GDK_KP_Multiply: xkey = 'R'; break;
563 case GDK_KP_Subtract: xkey = 'S'; break;
565 * Keypad + is tricky. It covers a space that would
566 * be taken up on the VT100 by _two_ keys; so we
567 * let Shift select between the two. Worse still,
568 * in xterm function key mode we change which two...
571 if (cfg.funky_type == 2) {
572 if (event->state & GDK_SHIFT_MASK)
576 } else if (event->state & GDK_SHIFT_MASK)
581 case GDK_KP_Enter: xkey = 'M'; break;
582 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
583 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
584 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
585 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
586 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
587 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
588 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
589 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
590 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
591 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
592 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
595 if (inst->term->vt52_mode) {
596 if (xkey >= 'P' && xkey <= 'S')
597 end = 1 + sprintf(output+1, "\033%c", xkey);
599 end = 1 + sprintf(output+1, "\033?%c", xkey);
601 end = 1 + sprintf(output+1, "\033O%c", xkey);
607 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
608 * for integer decimal nn.)
610 * We also deal with the weird ones here. Linux VCs replace F1
611 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
612 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
617 switch (event->keyval) {
619 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
622 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
625 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
628 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
631 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
634 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
637 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
640 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
643 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
646 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
679 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
680 case GDK_Home: case GDK_KP_Home:
683 case GDK_Insert: case GDK_KP_Insert:
686 case GDK_Delete: case GDK_KP_Delete:
689 case GDK_End: case GDK_KP_End:
692 case GDK_Page_Up: case GDK_KP_Page_Up:
695 case GDK_Page_Down: case GDK_KP_Page_Down:
699 /* Reorder edit keys to physical order */
700 if (cfg.funky_type == 3 && code <= 6)
701 code = "\0\2\1\4\5\3\6"[code];
703 if (inst->term->vt52_mode && code > 0 && code <= 6) {
704 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
708 if (cfg.funky_type == 5 && /* SCO function keys */
709 code >= 11 && code <= 34) {
710 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
712 switch (event->keyval) {
713 case GDK_F1: index = 0; break;
714 case GDK_F2: index = 1; break;
715 case GDK_F3: index = 2; break;
716 case GDK_F4: index = 3; break;
717 case GDK_F5: index = 4; break;
718 case GDK_F6: index = 5; break;
719 case GDK_F7: index = 6; break;
720 case GDK_F8: index = 7; break;
721 case GDK_F9: index = 8; break;
722 case GDK_F10: index = 9; break;
723 case GDK_F11: index = 10; break;
724 case GDK_F12: index = 11; break;
726 if (event->state & GDK_SHIFT_MASK) index += 12;
727 if (event->state & GDK_CONTROL_MASK) index += 24;
728 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
731 if (cfg.funky_type == 5 && /* SCO small keypad */
732 code >= 1 && code <= 6) {
733 char codes[] = "HL.FIG";
738 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
742 if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
743 code >= 11 && code <= 24) {
749 if (inst->term->vt52_mode)
750 end = 1 + sprintf(output+1,
751 "\x1B%c", code + 'P' - 11 - offt);
753 end = 1 + sprintf(output+1,
754 "\x1BO%c", code + 'P' - 11 - offt);
757 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
758 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
761 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
762 if (inst->term->vt52_mode)
763 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
765 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
768 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
769 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
773 end = 1 + sprintf(output+1, "\x1B[%d~", code);
779 * Cursor keys. (This includes the numberpad cursor keys,
780 * if we haven't already done them due to app keypad mode.)
782 * Here we also process un-numlocked un-appkeypadded KP5,
783 * which sends ESC [ G.
787 switch (event->keyval) {
788 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
789 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
790 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
791 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
792 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
796 * The arrow keys normally do ESC [ A and so on. In
797 * app cursor keys mode they do ESC O A instead.
798 * Ctrl toggles the two modes.
800 if (inst->term->vt52_mode) {
801 end = 1 + sprintf(output+1, "\033%c", xkey);
802 } else if (!inst->term->app_cursor_keys ^
803 !(event->state & GDK_CONTROL_MASK)) {
804 end = 1 + sprintf(output+1, "\033O%c", xkey);
806 end = 1 + sprintf(output+1, "\033[%c", xkey);
819 printf("generating sequence:");
820 for (i = start; i < end; i++)
821 printf(" %02x", (unsigned char) output[i]);
825 ldisc_send(inst->ldisc, output+start, end-start, 1);
826 show_mouseptr(inst, 0);
827 term_seen_key_event(inst->term);
828 term_out(inst->term);
834 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
836 struct gui_data *inst = (struct gui_data *)data;
837 int shift, ctrl, alt, x, y, button, act;
839 show_mouseptr(inst, 1);
841 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
842 term_scroll(inst->term, 0, -5);
845 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
846 term_scroll(inst->term, 0, +5);
850 shift = event->state & GDK_SHIFT_MASK;
851 ctrl = event->state & GDK_CONTROL_MASK;
852 alt = event->state & GDK_MOD1_MASK;
853 if (event->button == 1)
855 else if (event->button == 2)
857 else if (event->button == 3)
860 return FALSE; /* don't even know what button! */
862 switch (event->type) {
863 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
864 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
865 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
866 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
867 default: return FALSE; /* don't know this event type */
870 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
871 act != MA_CLICK && act != MA_RELEASE)
872 return TRUE; /* we ignore these in raw mouse mode */
874 x = (event->x - cfg.window_border) / inst->font_width;
875 y = (event->y - cfg.window_border) / inst->font_height;
877 term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
882 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
884 struct gui_data *inst = (struct gui_data *)data;
885 int shift, ctrl, alt, x, y, button;
887 show_mouseptr(inst, 1);
889 shift = event->state & GDK_SHIFT_MASK;
890 ctrl = event->state & GDK_CONTROL_MASK;
891 alt = event->state & GDK_MOD1_MASK;
892 if (event->state & GDK_BUTTON1_MASK)
894 else if (event->state & GDK_BUTTON2_MASK)
896 else if (event->state & GDK_BUTTON3_MASK)
899 return FALSE; /* don't even know what button! */
901 x = (event->x - cfg.window_border) / inst->font_width;
902 y = (event->y - cfg.window_border) / inst->font_height;
904 term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
909 void done_with_pty(struct gui_data *inst)
911 extern void pty_close(void);
913 if (inst->master_fd >= 0) {
915 inst->master_fd = -1;
916 gtk_input_remove(inst->master_func_id);
919 if (!inst->exited && inst->back->exitcode(inst->backhandle) >= 0) {
920 int exitcode = inst->back->exitcode(inst->backhandle);
923 clean = WIFEXITED(exitcode) && (WEXITSTATUS(exitcode) == 0);
926 * Terminate now, if the Close On Exit setting is
929 if (cfg.close_on_exit == COE_ALWAYS ||
930 (cfg.close_on_exit == COE_NORMAL && clean))
934 * Otherwise, output an indication that the session has
939 if (WIFEXITED(exitcode))
940 sprintf(message, "\r\n[pterm: process terminated with exit"
941 " code %d]\r\n", WEXITSTATUS(exitcode));
942 else if (WIFSIGNALED(exitcode))
943 #ifdef HAVE_NO_STRSIGNAL
944 sprintf(message, "\r\n[pterm: process terminated on signal"
945 " %d]\r\n", WTERMSIG(exitcode));
947 sprintf(message, "\r\n[pterm: process terminated on signal"
948 " %d (%.400s)]\r\n", WTERMSIG(exitcode),
949 strsignal(WTERMSIG(exitcode)));
951 from_backend((void *)inst->term, 0, message, strlen(message));
957 void frontend_keypress(void *handle)
959 struct gui_data *inst = (struct gui_data *)handle;
962 * If our child process has exited but not closed, terminate on
969 gint timer_func(gpointer data)
971 struct gui_data *inst = (struct gui_data *)data;
973 if (inst->back->exitcode(inst->backhandle) >= 0) {
975 * The primary child process died. We could keep the
976 * terminal open for remaining subprocesses to output to,
977 * but conventional wisdom seems to feel that that's the
978 * Wrong Thing for an xterm-alike, so we bail out now
979 * (though we don't necessarily _close_ the window,
980 * depending on the state of Close On Exit). This would be
981 * easy enough to change or make configurable if necessary.
986 term_update(inst->term);
987 term_blink(inst->term, 0);
991 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
993 struct gui_data *inst = (struct gui_data *)data;
997 ret = read(sourcefd, buf, sizeof(buf));
1000 * Clean termination condition is that either ret == 0, or ret
1001 * < 0 and errno == EIO. Not sure why the latter, but it seems
1004 if (ret == 0 || (ret < 0 && errno == EIO)) {
1005 done_with_pty(inst);
1006 } else if (ret < 0) {
1007 perror("read pty master");
1010 from_backend(inst->term, 0, buf, ret);
1011 term_blink(inst->term, 1);
1012 term_out(inst->term);
1015 void destroy(GtkWidget *widget, gpointer data)
1020 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1022 struct gui_data *inst = (struct gui_data *)data;
1023 inst->term->has_focus = event->in;
1024 term_out(inst->term);
1025 term_update(inst->term);
1026 show_mouseptr(inst, 1);
1031 * set or clear the "raw mouse message" mode
1033 void set_raw_mouse_mode(void *frontend, int activate)
1035 struct gui_data *inst = (struct gui_data *)frontend;
1036 activate = activate && !cfg.no_mouse_rep;
1037 send_raw_mouse = activate;
1039 inst->currcursor = inst->rawcursor;
1041 inst->currcursor = inst->textcursor;
1042 show_mouseptr(inst, inst->mouseptr_visible);
1045 void request_resize(void *frontend, int w, int h)
1047 struct gui_data *inst = (struct gui_data *)frontend;
1048 int large_x, large_y;
1049 int offset_x, offset_y;
1051 GtkRequisition inner, outer;
1054 * This is a heinous hack dreamed up by the gnome-terminal
1055 * people to get around a limitation in gtk. The problem is
1056 * that in order to set the size correctly we really need to be
1057 * calling gtk_window_resize - but that needs to know the size
1058 * of the _whole window_, not the drawing area. So what we do
1059 * is to set an artificially huge size request on the drawing
1060 * area, recompute the resulting size request on the window,
1061 * and look at the difference between the two. That gives us
1062 * the x and y offsets we need to translate drawing area size
1063 * into window size for real, and then we call
1064 * gtk_window_resize.
1068 * We start by retrieving the current size of the whole window.
1069 * Adding a bit to _that_ will give us a value we can use as a
1070 * bogus size request which guarantees to be bigger than the
1071 * current size of the drawing area.
1073 get_window_pixels(inst, &large_x, &large_y);
1077 #if GTK_CHECK_VERSION(2,0,0)
1078 gtk_widget_set_size_request(inst->area, large_x, large_y);
1080 gtk_widget_set_usize(inst->area, large_x, large_y);
1082 gtk_widget_size_request(inst->area, &inner);
1083 gtk_widget_size_request(inst->window, &outer);
1085 offset_x = outer.width - inner.width;
1086 offset_y = outer.height - inner.height;
1088 area_x = inst->font_width * w + 2*cfg.window_border;
1089 area_y = inst->font_height * h + 2*cfg.window_border;
1092 * Now we must set the size request on the drawing area back to
1093 * something sensible before we commit the real resize. Best
1094 * way to do this, I think, is to set it to what the size is
1095 * really going to end up being.
1097 #if GTK_CHECK_VERSION(2,0,0)
1098 gtk_widget_set_size_request(inst->area, area_x, area_y);
1100 gtk_widget_set_usize(inst->area, area_x, area_y);
1103 #if GTK_CHECK_VERSION(2,0,0)
1104 gtk_window_resize(GTK_WINDOW(inst->window),
1105 area_x + offset_x, area_y + offset_y);
1107 gdk_window_resize(inst->window->window,
1108 area_x + offset_x, area_y + offset_y);
1112 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1114 gboolean success[1];
1116 inst->cols[n].red = r * 0x0101;
1117 inst->cols[n].green = g * 0x0101;
1118 inst->cols[n].blue = b * 0x0101;
1120 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1121 FALSE, FALSE, success);
1123 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1127 void set_window_background(struct gui_data *inst)
1129 if (inst->area && inst->area->window)
1130 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1131 if (inst->window && inst->window->window)
1132 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1135 void palette_set(void *frontend, int n, int r, int g, int b)
1137 struct gui_data *inst = (struct gui_data *)frontend;
1138 static const int first[21] = {
1139 0, 2, 4, 6, 8, 10, 12, 14,
1140 1, 3, 5, 7, 9, 11, 13, 15,
1143 real_palette_set(inst, first[n], r, g, b);
1145 real_palette_set(inst, first[n] + 1, r, g, b);
1147 set_window_background(inst);
1150 void palette_reset(void *frontend)
1152 struct gui_data *inst = (struct gui_data *)frontend;
1153 /* This maps colour indices in cfg to those used in inst->cols. */
1154 static const int ww[] = {
1155 6, 7, 8, 9, 10, 11, 12, 13,
1156 14, 15, 16, 17, 18, 19, 20, 21,
1159 gboolean success[NCOLOURS];
1162 assert(lenof(ww) == NCOLOURS);
1164 if (!inst->colmap) {
1165 inst->colmap = gdk_colormap_get_system();
1167 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1170 for (i = 0; i < NCOLOURS; i++) {
1171 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1172 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1173 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1176 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1177 FALSE, FALSE, success);
1178 for (i = 0; i < NCOLOURS; i++) {
1180 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1181 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1184 set_window_background(inst);
1187 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1189 struct gui_data *inst = (struct gui_data *)frontend;
1190 if (inst->pasteout_data)
1191 sfree(inst->pasteout_data);
1192 inst->pasteout_data = smalloc(len);
1193 inst->pasteout_data_len = len;
1194 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1197 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1198 GDK_CURRENT_TIME)) {
1199 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1200 GDK_SELECTION_TYPE_STRING, 1);
1201 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1202 inst->compound_text_atom, 1);
1206 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1207 guint info, guint time_stamp, gpointer data)
1209 struct gui_data *inst = (struct gui_data *)data;
1210 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1211 inst->pasteout_data, inst->pasteout_data_len);
1214 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1217 struct gui_data *inst = (struct gui_data *)data;
1218 term_deselect(inst->term);
1219 if (inst->pasteout_data)
1220 sfree(inst->pasteout_data);
1221 inst->pasteout_data = NULL;
1222 inst->pasteout_data_len = 0;
1226 void request_paste(void *frontend)
1228 struct gui_data *inst = (struct gui_data *)frontend;
1230 * In Unix, pasting is asynchronous: all we can do at the
1231 * moment is to call gtk_selection_convert(), and when the data
1232 * comes back _then_ we can call term_do_paste().
1234 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1235 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1238 gint idle_paste_func(gpointer data); /* forward ref */
1240 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1241 guint time, gpointer data)
1243 struct gui_data *inst = (struct gui_data *)data;
1245 if (seldata->length <= 0 ||
1246 seldata->type != GDK_SELECTION_TYPE_STRING)
1247 return; /* Nothing happens. */
1249 if (inst->pastein_data)
1250 sfree(inst->pastein_data);
1252 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1253 inst->pastein_data_len = seldata->length;
1254 mb_to_wc(0, 0, seldata->data, seldata->length,
1255 inst->pastein_data, inst->pastein_data_len);
1257 term_do_paste(inst->term);
1259 if (term_paste_pending(inst->term))
1260 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1263 gint idle_paste_func(gpointer data)
1265 struct gui_data *inst = (struct gui_data *)data;
1267 if (term_paste_pending(inst->term))
1268 term_paste(inst->term);
1270 gtk_idle_remove(inst->term_paste_idle_id);
1276 void get_clip(void *frontend, wchar_t ** p, int *len)
1278 struct gui_data *inst = (struct gui_data *)frontend;
1281 *p = inst->pastein_data;
1282 *len = inst->pastein_data_len;
1286 void set_title(void *frontend, char *title)
1288 struct gui_data *inst = (struct gui_data *)frontend;
1289 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1290 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1291 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1294 void set_icon(void *frontend, char *title)
1296 struct gui_data *inst = (struct gui_data *)frontend;
1297 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1298 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1299 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1302 void set_sbar(void *frontend, int total, int start, int page)
1304 struct gui_data *inst = (struct gui_data *)frontend;
1307 inst->sbar_adjust->lower = 0;
1308 inst->sbar_adjust->upper = total;
1309 inst->sbar_adjust->value = start;
1310 inst->sbar_adjust->page_size = page;
1311 inst->sbar_adjust->step_increment = 1;
1312 inst->sbar_adjust->page_increment = page/2;
1313 inst->ignore_sbar = TRUE;
1314 gtk_adjustment_changed(inst->sbar_adjust);
1315 inst->ignore_sbar = FALSE;
1318 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1320 struct gui_data *inst = (struct gui_data *)data;
1324 if (!inst->ignore_sbar)
1325 term_scroll(inst->term, 1, (int)adj->value);
1328 void sys_cursor(void *frontend, int x, int y)
1331 * This is meaningless under X.
1336 * This is still called when mode==BELL_VISUAL, even though the
1337 * visual bell is handled entirely within terminal.c, because we
1338 * may want to perform additional actions on any kind of bell (for
1339 * example, taskbar flashing in Windows).
1341 void beep(void *frontend, int mode)
1343 if (mode != BELL_VISUAL)
1347 int CharWidth(Context ctx, int uc)
1350 * Under X, any fixed-width font really _is_ fixed-width.
1351 * Double-width characters will be dealt with using a separate
1352 * font. For the moment we can simply return 1.
1357 Context get_ctx(void *frontend)
1359 struct gui_data *inst = (struct gui_data *)frontend;
1360 struct draw_ctx *dctx;
1362 if (!inst->area->window)
1365 dctx = smalloc(sizeof(*dctx));
1367 dctx->gc = gdk_gc_new(inst->area->window);
1371 void free_ctx(Context ctx)
1373 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1374 /* struct gui_data *inst = dctx->inst; */
1375 GdkGC *gc = dctx->gc;
1381 * Draw a line of text in the window, at given character
1382 * coordinates, in given attributes.
1384 * We are allowed to fiddle with the contents of `text'.
1386 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1387 unsigned long attr, int lattr)
1389 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1390 struct gui_data *inst = dctx->inst;
1391 GdkGC *gc = dctx->gc;
1393 int nfg, nbg, t, fontid, shadow;
1397 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1400 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1401 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1402 if (attr & ATTR_REVERSE) {
1407 if (cfg.bold_colour && (attr & ATTR_BOLD))
1409 if (cfg.bold_colour && (attr & ATTR_BLINK))
1411 if (attr & TATTR_ACTCURS) {
1416 fontid = shadow = 0;
1417 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1424 if (lattr != LATTR_NORM) {
1426 if (x >= inst->term->cols)
1428 if (x + len*2 > inst->term->cols)
1429 len = (inst->term->cols-x)/2; /* trim to LH half */
1432 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1433 gdk_draw_rectangle(inst->pixmap, gc, 1,
1434 x*inst->font_width+cfg.window_border,
1435 y*inst->font_height+cfg.window_border,
1436 len*inst->font_width, inst->font_height);
1438 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1439 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1440 x*inst->font_width+cfg.window_border,
1441 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1445 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1446 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1447 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1451 if (attr & ATTR_UNDER) {
1452 int uheight = inst->fonts[0]->ascent + 1;
1453 if (uheight >= inst->font_height)
1454 uheight = inst->font_height - 1;
1455 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1456 y*inst->font_height + uheight + cfg.window_border,
1457 (x+len)*inst->font_width-1+cfg.window_border,
1458 y*inst->font_height + uheight + cfg.window_border);
1461 if (lattr != LATTR_NORM) {
1463 * I can't find any plausible StretchBlt equivalent in the
1464 * X server, so I'm going to do this the slow and painful
1465 * way. This will involve repeated calls to
1466 * gdk_draw_pixmap() to stretch the text horizontally. It's
1467 * O(N^2) in time and O(N) in network bandwidth, but you
1468 * try thinking of a better way. :-(
1471 for (i = 0; i < len * inst->font_width; i++) {
1472 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1473 x*inst->font_width+cfg.window_border + 2*i,
1474 y*inst->font_height+cfg.window_border,
1475 x*inst->font_width+cfg.window_border + 2*i+1,
1476 y*inst->font_height+cfg.window_border,
1477 len * inst->font_width - i, inst->font_height);
1480 if (lattr != LATTR_WIDE) {
1482 /* Now stretch vertically, in the same way. */
1483 if (lattr == LATTR_BOT)
1487 for (i = 0; i < inst->font_height; i+=2) {
1488 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1489 x*inst->font_width+cfg.window_border,
1490 y*inst->font_height+cfg.window_border+dt*i+db,
1491 x*inst->font_width+cfg.window_border,
1492 y*inst->font_height+cfg.window_border+dt*(i+1),
1493 len * inst->font_width, inst->font_height-i-1);
1499 void do_text(Context ctx, int x, int y, char *text, int len,
1500 unsigned long attr, int lattr)
1502 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1503 struct gui_data *inst = dctx->inst;
1504 GdkGC *gc = dctx->gc;
1506 do_text_internal(ctx, x, y, text, len, attr, lattr);
1508 if (lattr != LATTR_NORM) {
1510 if (x >= inst->term->cols)
1512 if (x + len*2 > inst->term->cols)
1513 len = (inst->term->cols-x)/2; /* trim to LH half */
1517 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1518 x*inst->font_width+cfg.window_border,
1519 y*inst->font_height+cfg.window_border,
1520 x*inst->font_width+cfg.window_border,
1521 y*inst->font_height+cfg.window_border,
1522 len*inst->font_width, inst->font_height);
1525 void do_cursor(Context ctx, int x, int y, char *text, int len,
1526 unsigned long attr, int lattr)
1528 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1529 struct gui_data *inst = dctx->inst;
1530 GdkGC *gc = dctx->gc;
1534 if (attr & TATTR_PASCURS) {
1535 attr &= ~TATTR_PASCURS;
1539 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1540 attr &= ~TATTR_ACTCURS;
1542 do_text_internal(ctx, x, y, text, len, attr, lattr);
1544 if (lattr != LATTR_NORM) {
1546 if (x >= inst->term->cols)
1548 if (x + len*2 > inst->term->cols)
1549 len = (inst->term->cols-x)/2; /* trim to LH half */
1553 if (cfg.cursor_type == 0) {
1555 * An active block cursor will already have been done by
1556 * the above do_text call, so we only need to do anything
1560 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1561 gdk_draw_rectangle(inst->pixmap, gc, 0,
1562 x*inst->font_width+cfg.window_border,
1563 y*inst->font_height+cfg.window_border,
1564 len*inst->font_width-1, inst->font_height-1);
1568 int startx, starty, dx, dy, length, i;
1572 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1573 char_width = 2*inst->font_width;
1575 char_width = inst->font_width;
1577 if (cfg.cursor_type == 1) {
1578 uheight = inst->fonts[0]->ascent + 1;
1579 if (uheight >= inst->font_height)
1580 uheight = inst->font_height - 1;
1582 startx = x * inst->font_width + cfg.window_border;
1583 starty = y * inst->font_height + cfg.window_border + uheight;
1586 length = len * char_width;
1589 if (attr & TATTR_RIGHTCURS)
1590 xadjust = char_width - 1;
1591 startx = x * inst->font_width + cfg.window_border + xadjust;
1592 starty = y * inst->font_height + cfg.window_border;
1595 length = inst->font_height;
1598 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1600 for (i = 0; i < length; i++) {
1602 gdk_draw_point(inst->pixmap, gc, startx, starty);
1608 gdk_draw_line(inst->pixmap, gc, startx, starty,
1609 startx + (length-1) * dx, starty + (length-1) * dy);
1613 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1614 x*inst->font_width+cfg.window_border,
1615 y*inst->font_height+cfg.window_border,
1616 x*inst->font_width+cfg.window_border,
1617 y*inst->font_height+cfg.window_border,
1618 len*inst->font_width, inst->font_height);
1621 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1624 * Truly hideous hack: GTK doesn't allow us to set the mouse
1625 * cursor foreground and background colours unless we've _also_
1626 * created our own cursor from bitmaps. Therefore, I need to
1627 * load the `cursor' font and draw glyphs from it on to
1628 * pixmaps, in order to construct my cursors with the fg and bg
1629 * I want. This is a gross hack, but it's more self-contained
1630 * than linking in Xlib to find the X window handle to
1631 * inst->area and calling XRecolorCursor, and it's more
1632 * futureproof than hard-coding the shapes as bitmap arrays.
1634 static GdkFont *cursor_font = NULL;
1635 GdkPixmap *source, *mask;
1637 GdkColor cfg = { 0, 65535, 65535, 65535 };
1638 GdkColor cbg = { 0, 0, 0, 0 };
1639 GdkColor dfg = { 1, 65535, 65535, 65535 };
1640 GdkColor dbg = { 0, 0, 0, 0 };
1643 gint lb, rb, wid, asc, desc, w, h, x, y;
1645 if (cursor_val == -2) {
1646 gdk_font_unref(cursor_font);
1650 if (cursor_val >= 0 && !cursor_font)
1651 cursor_font = gdk_font_load("cursor");
1654 * Get the text extent of the cursor in question. We use the
1655 * mask character for this, because it's typically slightly
1656 * bigger than the main character.
1658 if (cursor_val >= 0) {
1660 text[0] = (char)cursor_val + 1;
1661 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1662 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1668 source = gdk_pixmap_new(NULL, w, h, 1);
1669 mask = gdk_pixmap_new(NULL, w, h, 1);
1672 * Draw the mask character on the mask pixmap.
1674 gc = gdk_gc_new(mask);
1675 gdk_gc_set_foreground(gc, &dbg);
1676 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1677 if (cursor_val >= 0) {
1679 text[0] = (char)cursor_val + 1;
1680 gdk_gc_set_foreground(gc, &dfg);
1681 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1686 * Draw the main character on the source pixmap.
1688 gc = gdk_gc_new(source);
1689 gdk_gc_set_foreground(gc, &dbg);
1690 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1691 if (cursor_val >= 0) {
1693 text[0] = (char)cursor_val;
1694 gdk_gc_set_foreground(gc, &dfg);
1695 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1700 * Create the cursor.
1702 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1707 gdk_pixmap_unref(source);
1708 gdk_pixmap_unref(mask);
1713 void modalfatalbox(char *p, ...)
1716 fprintf(stderr, "FATAL ERROR: ");
1718 vfprintf(stderr, p, ap);
1720 fputc('\n', stderr);
1724 char *get_x_display(void *frontend)
1726 return gdk_get_display();
1729 char *app_name = "pterm";
1731 int do_cmdline(int argc, char **argv, int do_everything)
1734 extern char **pty_argv; /* declared in pty.c */
1737 * Macros to make argument handling easier.
1739 #define EXPECTS_ARG do { \
1740 if (--argc <= 0) { \
1742 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1746 #define SECOND_PASS_ONLY do { \
1747 if (!do_everything) continue; \
1757 while (--argc > 0) {
1759 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1762 strncpy(cfg.font, val, sizeof(cfg.font));
1763 cfg.font[sizeof(cfg.font)-1] = '\0';
1765 } else if (!strcmp(p, "-fb")) {
1768 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1769 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1771 } else if (!strcmp(p, "-geometry")) {
1772 int flags, x, y, w, h;
1776 flags = XParseGeometry(val, &x, &y, &w, &h);
1777 if (flags & WidthValue)
1779 if (flags & HeightValue)
1783 * Apparently setting the initial window position is
1784 * difficult in GTK 1.2. Not entirely sure why this
1785 * should be. 2.0 has gtk_window_parse_geometry(),
1786 * which would help... For the moment, though, I can't
1787 * be bothered with this.
1790 } else if (!strcmp(p, "-sl")) {
1793 cfg.savelines = atoi(val);
1795 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1796 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1797 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1802 if (!gdk_color_parse(val, &col)) {
1804 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1807 index = (!strcmp(p, "-fg") ? 0 :
1808 !strcmp(p, "-bg") ? 2 :
1809 !strcmp(p, "-bfg") ? 1 :
1810 !strcmp(p, "-bbg") ? 3 :
1811 !strcmp(p, "-cfg") ? 4 :
1812 !strcmp(p, "-cbg") ? 5 : -1);
1813 assert(index != -1);
1814 cfg.colours[index][0] = col.red / 256;
1815 cfg.colours[index][1] = col.green / 256;
1816 cfg.colours[index][2] = col.blue / 256;
1819 } else if (!strcmp(p, "-e")) {
1820 /* This option swallows all further arguments. */
1826 pty_argv = smalloc((argc+1) * sizeof(char *));
1828 for (i = 0; i < argc; i++)
1829 pty_argv[i] = argv[i];
1830 pty_argv[argc] = NULL;
1831 break; /* finished command-line processing */
1833 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1835 } else if (!strcmp(p, "-T")) {
1838 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1839 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1841 } else if (!strcmp(p, "-log")) {
1844 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1845 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1846 cfg.logtype = LGTYP_DEBUG;
1848 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1852 } else if (!strcmp(p, "-ut")) {
1856 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1858 cfg.login_shell = 0;
1860 } else if (!strcmp(p, "-ls")) {
1862 cfg.login_shell = 1;
1864 } else if (!strcmp(p, "-nethack")) {
1866 cfg.nethack_keypad = 1;
1868 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1872 } else if (!strcmp(p, "-sb")) {
1876 } else if (!strcmp(p, "-name")) {
1880 } else if (!strcmp(p, "-xrm")) {
1882 provide_xrm_string(val);
1886 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
1893 int main(int argc, char **argv)
1895 extern int pty_master_fd; /* declared in pty.c */
1896 extern void pty_pre_init(void); /* declared in pty.c */
1897 struct gui_data *inst;
1901 gtk_init(&argc, &argv);
1903 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
1905 do_defaults(NULL, &cfg);
1906 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
1910 * Create an instance structure and initialise to zeroes
1912 inst = smalloc(sizeof(*inst));
1913 memset(inst, 0, sizeof(*inst));
1914 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
1916 inst->fonts[0] = gdk_font_load(cfg.font);
1917 if (!inst->fonts[0]) {
1918 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1921 if (cfg.boldfont[0]) {
1922 inst->fonts[1] = gdk_font_load(cfg.boldfont);
1923 if (!inst->fonts[1]) {
1924 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1929 inst->fonts[1] = NULL;
1931 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1932 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1934 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1938 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1940 if (cfg.wintitle[0])
1941 set_title(inst, cfg.wintitle);
1943 set_title(inst, "pterm");
1946 * Set up the colour map.
1948 palette_reset(inst);
1950 inst->area = gtk_drawing_area_new();
1951 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1952 inst->font_width * cfg.width + 2*cfg.window_border,
1953 inst->font_height * cfg.height + 2*cfg.window_border);
1954 if (cfg.scrollbar) {
1955 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1956 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1958 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1959 if (cfg.scrollbar) {
1960 if (cfg.scrollbar_on_left)
1961 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1963 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1965 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1967 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1971 geom.min_width = inst->font_width + 2*cfg.window_border;
1972 geom.min_height = inst->font_height + 2*cfg.window_border;
1973 geom.max_width = geom.max_height = -1;
1974 geom.base_width = 2*cfg.window_border;
1975 geom.base_height = 2*cfg.window_border;
1976 geom.width_inc = inst->font_width;
1977 geom.height_inc = inst->font_height;
1978 geom.min_aspect = geom.max_aspect = 0;
1979 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1980 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1981 GDK_HINT_RESIZE_INC);
1984 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1985 GTK_SIGNAL_FUNC(destroy), inst);
1986 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1987 GTK_SIGNAL_FUNC(delete_window), inst);
1988 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1989 GTK_SIGNAL_FUNC(key_event), inst);
1990 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
1991 GTK_SIGNAL_FUNC(key_event), inst);
1992 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1993 GTK_SIGNAL_FUNC(focus_event), inst);
1994 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1995 GTK_SIGNAL_FUNC(focus_event), inst);
1996 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1997 GTK_SIGNAL_FUNC(configure_area), inst);
1998 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1999 GTK_SIGNAL_FUNC(expose_area), inst);
2000 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2001 GTK_SIGNAL_FUNC(button_event), inst);
2002 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2003 GTK_SIGNAL_FUNC(button_event), inst);
2004 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2005 GTK_SIGNAL_FUNC(motion_event), inst);
2006 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2007 GTK_SIGNAL_FUNC(selection_received), inst);
2008 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2009 GTK_SIGNAL_FUNC(selection_get), inst);
2010 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2011 GTK_SIGNAL_FUNC(selection_clear), inst);
2013 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2014 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2015 gtk_timeout_add(20, timer_func, inst);
2016 gtk_widget_add_events(GTK_WIDGET(inst->area),
2017 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2018 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2019 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2021 gtk_widget_show(inst->area);
2023 gtk_widget_show(inst->sbar);
2024 gtk_widget_show(GTK_WIDGET(inst->hbox));
2025 gtk_widget_show(inst->window);
2027 set_window_background(inst);
2029 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2030 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2031 inst->blankcursor = make_mouse_ptr(inst, -1);
2032 make_mouse_ptr(inst, -2); /* clean up cursor font */
2033 inst->currcursor = inst->textcursor;
2034 show_mouseptr(inst, 1);
2036 inst->term = term_init(inst);
2037 inst->logctx = log_init(inst);
2038 term_provide_logctx(inst->term, inst->logctx);
2040 inst->back = &pty_backend;
2041 inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
2042 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2044 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2046 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
2048 inst->ldisc = ldisc_create(inst->term, inst->back, inst->backhandle, inst);
2049 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2051 inst->master_fd = pty_master_fd;
2052 inst->exited = FALSE;
2053 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
2054 pty_input_func, inst);