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;
53 char wintitle[sizeof(((Config *)0)->wintitle)];
54 char icontitle[sizeof(((Config *)0)->wintitle)];
55 int master_fd, master_func_id, exited;
65 struct gui_data *inst;
68 static int send_raw_mouse;
70 void ldisc_update(void *frontend, int echo, int edit)
73 * This is a stub in pterm. If I ever produce a Unix
74 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
75 * then it will require some termios manoeuvring analogous to
76 * that in the Windows plink.c, but here it's meaningless.
80 int askappend(void *frontend, char *filename)
83 * Logging in an xterm-alike is liable to be something you only
84 * do at serious diagnostic need. Hence, I'm going to take the
85 * easy option for now and assume we always want to overwrite
86 * log files. I can always make it properly configurable later.
91 void logevent(void *frontend, char *string)
94 * This is not a very helpful function: events are logged
95 * pretty much exclusively by the back end, and our pty back
96 * end is self-contained. So we need do nothing.
100 int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
102 struct gui_data *inst = (struct gui_data *)frontend;
105 return inst->font_height;
107 return inst->font_width;
111 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
112 * into a cooked one (SELECT, EXTEND, PASTE).
114 * In Unix, this is not configurable; the X button arrangement is
115 * rock-solid across all applications, everyone has a three-button
116 * mouse or a means of faking it, and there is no need to switch
117 * buttons around at all.
119 Mouse_Button translate_button(void *frontend, Mouse_Button button)
121 /* struct gui_data *inst = (struct gui_data *)frontend; */
123 if (button == MBT_LEFT)
125 if (button == MBT_MIDDLE)
127 if (button == MBT_RIGHT)
129 return 0; /* shouldn't happen */
133 * Minimise or restore the window in response to a server-side
136 void set_iconic(void *frontend, int iconic)
139 * GTK 1.2 doesn't know how to do this.
141 #if GTK_CHECK_VERSION(2,0,0)
142 struct gui_data *inst = (struct gui_data *)frontend;
144 gtk_window_iconify(GTK_WINDOW(inst->window));
146 gtk_window_deiconify(GTK_WINDOW(inst->window));
151 * Move the window in response to a server-side request.
153 void move_window(void *frontend, int x, int y)
155 struct gui_data *inst = (struct gui_data *)frontend;
157 * I assume that when the GTK version of this call is available
158 * we should use it. Not sure how it differs from the GDK one,
161 #if GTK_CHECK_VERSION(2,0,0)
162 gtk_window_move(GTK_WINDOW(inst->window), x, y);
164 gdk_window_move(inst->window->window, x, y);
169 * Move the window to the top or bottom of the z-order in response
170 * to a server-side request.
172 void set_zorder(void *frontend, int top)
174 struct gui_data *inst = (struct gui_data *)frontend;
176 gdk_window_raise(inst->window->window);
178 gdk_window_lower(inst->window->window);
182 * Refresh the window in response to a server-side request.
184 void refresh_window(void *frontend)
186 struct gui_data *inst = (struct gui_data *)frontend;
187 term_invalidate(inst->term);
191 * Maximise or restore the window in response to a server-side
194 void set_zoomed(void *frontend, int zoomed)
197 * GTK 1.2 doesn't know how to do this.
199 #if GTK_CHECK_VERSION(2,0,0)
200 struct gui_data *inst = (struct gui_data *)frontend;
202 gtk_window_maximize(GTK_WINDOW(inst->window));
204 gtk_window_unmaximize(GTK_WINDOW(inst->window));
209 * Report whether the window is iconic, for terminal reports.
211 int is_iconic(void *frontend)
213 struct gui_data *inst = (struct gui_data *)frontend;
214 return !gdk_window_is_viewable(inst->window->window);
218 * Report the window's position, for terminal reports.
220 void get_window_pos(void *frontend, int *x, int *y)
222 struct gui_data *inst = (struct gui_data *)frontend;
224 * I assume that when the GTK version of this call is available
225 * we should use it. Not sure how it differs from the GDK one,
228 #if GTK_CHECK_VERSION(2,0,0)
229 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
231 gdk_window_get_position(inst->window->window, x, y);
236 * Report the window's pixel size, for terminal reports.
238 void get_window_pixels(void *frontend, int *x, int *y)
240 struct gui_data *inst = (struct gui_data *)frontend;
242 * I assume that when the GTK version of this call is available
243 * we should use it. Not sure how it differs from the GDK one,
246 #if GTK_CHECK_VERSION(2,0,0)
247 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
249 gdk_window_get_size(inst->window->window, x, y);
254 * Return the window or icon title.
256 char *get_window_title(void *frontend, int icon)
258 struct gui_data *inst = (struct gui_data *)frontend;
259 return icon ? inst->wintitle : inst->icontitle;
262 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
265 * We could implement warn-on-close here if we really wanted
271 static void show_mouseptr(struct gui_data *inst, int show)
273 if (!cfg.hide_mouseptr)
276 gdk_window_set_cursor(inst->area->window, inst->currcursor);
278 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
279 inst->mouseptr_visible = show;
282 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
284 struct gui_data *inst = (struct gui_data *)data;
285 int w, h, need_size = 0;
287 w = (event->width - 2*cfg.window_border) / inst->font_width;
288 h = (event->height - 2*cfg.window_border) / inst->font_height;
290 if (w != cfg.width || h != cfg.height) {
292 gdk_pixmap_unref(inst->pixmap);
302 inst->pixmap = gdk_pixmap_new(widget->window,
303 (cfg.width * inst->font_width +
304 2*cfg.window_border),
305 (cfg.height * inst->font_height +
306 2*cfg.window_border), -1);
308 gc = gdk_gc_new(inst->area->window);
309 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
310 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
311 cfg.width * inst->font_width + 2*cfg.window_border,
312 cfg.height * inst->font_height + 2*cfg.window_border);
317 term_size(inst->term, h, w, cfg.savelines);
323 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
325 struct gui_data *inst = (struct gui_data *)data;
328 * Pass the exposed rectangle to terminal.c, which will call us
329 * back to do the actual painting.
332 gdk_draw_pixmap(widget->window,
333 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
335 event->area.x, event->area.y,
336 event->area.x, event->area.y,
337 event->area.width, event->area.height);
342 #define KEY_PRESSED(k) \
343 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
345 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
347 struct gui_data *inst = (struct gui_data *)data;
351 /* By default, nothing is generated. */
355 * If Alt is being released after typing an Alt+numberpad
356 * sequence, we should generate the code that was typed.
358 if (event->type == GDK_KEY_RELEASE &&
359 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
360 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
361 inst->alt_keycode >= 0) {
363 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
365 output[0] = inst->alt_keycode;
370 if (event->type == GDK_KEY_PRESS) {
374 printf("keypress: keyval = %04x, state = %08x; string =",
375 event->keyval, event->state);
376 for (i = 0; event->string[i]; i++)
377 printf(" %02x", (unsigned char) event->string[i]);
383 * NYI: Compose key (!!! requires Unicode faff before even trying)
387 * If Alt has just been pressed, we start potentially
388 * accumulating an Alt+numberpad code. We do this by
389 * setting alt_keycode to -1 (nothing yet but plausible).
391 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
392 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
393 inst->alt_keycode = -1;
394 goto done; /* this generates nothing else */
398 * If we're seeing a numberpad key press with Mod1 down,
399 * consider adding it to alt_keycode if that's sensible.
400 * Anything _else_ with Mod1 down cancels any possibility
401 * of an ALT keycode: we set alt_keycode to -2.
403 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
405 switch (event->keyval) {
406 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
407 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
408 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
409 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
410 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
411 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
412 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
413 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
414 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
415 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
418 inst->alt_keycode = -2; /* it's invalid */
421 printf("Adding digit %d to keycode %d", digit,
424 if (inst->alt_keycode == -1)
425 inst->alt_keycode = digit; /* one-digit code */
427 inst->alt_keycode = inst->alt_keycode * 10 + digit;
429 printf(" gives new code %d\n", inst->alt_keycode);
431 /* Having used this digit, we now do nothing more with it. */
437 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
440 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
441 term_scroll(inst->term, 0, -cfg.height/2);
444 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
445 term_scroll(inst->term, 0, +cfg.height/2);
450 * Neither does Shift-Ins.
452 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
457 /* ALT+things gives leading Escape. */
459 strncpy(output+1, event->string, 31);
461 end = strlen(output);
462 if (event->state & GDK_MOD1_MASK) {
464 if (end == 1) end = 0;
468 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
469 if (!event->string[0] && event->keyval == '`' &&
470 (event->state & GDK_CONTROL_MASK)) {
475 /* Control-Break is the same as Control-C */
476 if (event->keyval == GDK_Break &&
477 (event->state & GDK_CONTROL_MASK)) {
482 /* Control-2, Control-Space and Control-@ are NUL */
483 if (!event->string[0] &&
484 (event->keyval == ' ' || event->keyval == '2' ||
485 event->keyval == '@') &&
486 (event->state & (GDK_SHIFT_MASK |
487 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
492 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
493 if (!event->string[0] && event->keyval == ' ' &&
494 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
495 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
500 /* We don't let GTK tell us what Backspace is! We know better. */
501 if (event->keyval == GDK_BackSpace &&
502 !(event->state & GDK_SHIFT_MASK)) {
503 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
506 /* For Shift Backspace, do opposite of what is configured. */
507 if (event->keyval == GDK_BackSpace &&
508 (event->state & GDK_SHIFT_MASK)) {
509 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
513 /* Shift-Tab is ESC [ Z */
514 if (event->keyval == GDK_ISO_Left_Tab ||
515 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
516 end = 1 + sprintf(output+1, "\033[Z");
520 * NetHack keypad mode.
522 if (cfg.nethack_keypad) {
524 switch (event->keyval) {
525 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
526 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
527 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
528 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
529 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
530 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
531 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
532 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
533 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
537 if (event->state & GDK_SHIFT_MASK)
546 * Application keypad mode.
548 if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
550 switch (event->keyval) {
551 case GDK_Num_Lock: xkey = 'P'; break;
552 case GDK_KP_Divide: xkey = 'Q'; break;
553 case GDK_KP_Multiply: xkey = 'R'; break;
554 case GDK_KP_Subtract: xkey = 'S'; break;
556 * Keypad + is tricky. It covers a space that would
557 * be taken up on the VT100 by _two_ keys; so we
558 * let Shift select between the two. Worse still,
559 * in xterm function key mode we change which two...
562 if (cfg.funky_type == 2) {
563 if (event->state & GDK_SHIFT_MASK)
567 } else if (event->state & GDK_SHIFT_MASK)
572 case GDK_KP_Enter: xkey = 'M'; break;
573 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
574 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
575 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
576 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
577 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
578 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
579 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
580 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
581 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
582 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
583 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
586 if (inst->term->vt52_mode) {
587 if (xkey >= 'P' && xkey <= 'S')
588 end = 1 + sprintf(output+1, "\033%c", xkey);
590 end = 1 + sprintf(output+1, "\033?%c", xkey);
592 end = 1 + sprintf(output+1, "\033O%c", xkey);
598 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
599 * for integer decimal nn.)
601 * We also deal with the weird ones here. Linux VCs replace F1
602 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
603 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
608 switch (event->keyval) {
610 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
613 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
616 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
619 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
622 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
625 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
628 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
631 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
634 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
637 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
670 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
671 case GDK_Home: case GDK_KP_Home:
674 case GDK_Insert: case GDK_KP_Insert:
677 case GDK_Delete: case GDK_KP_Delete:
680 case GDK_End: case GDK_KP_End:
683 case GDK_Page_Up: case GDK_KP_Page_Up:
686 case GDK_Page_Down: case GDK_KP_Page_Down:
690 /* Reorder edit keys to physical order */
691 if (cfg.funky_type == 3 && code <= 6)
692 code = "\0\2\1\4\5\3\6"[code];
694 if (inst->term->vt52_mode && code > 0 && code <= 6) {
695 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
699 if (cfg.funky_type == 5 && /* SCO function keys */
700 code >= 11 && code <= 34) {
701 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
703 switch (event->keyval) {
704 case GDK_F1: index = 0; break;
705 case GDK_F2: index = 1; break;
706 case GDK_F3: index = 2; break;
707 case GDK_F4: index = 3; break;
708 case GDK_F5: index = 4; break;
709 case GDK_F6: index = 5; break;
710 case GDK_F7: index = 6; break;
711 case GDK_F8: index = 7; break;
712 case GDK_F9: index = 8; break;
713 case GDK_F10: index = 9; break;
714 case GDK_F11: index = 10; break;
715 case GDK_F12: index = 11; break;
717 if (event->state & GDK_SHIFT_MASK) index += 12;
718 if (event->state & GDK_CONTROL_MASK) index += 24;
719 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
722 if (cfg.funky_type == 5 && /* SCO small keypad */
723 code >= 1 && code <= 6) {
724 char codes[] = "HL.FIG";
729 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
733 if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
734 code >= 11 && code <= 24) {
740 if (inst->term->vt52_mode)
741 end = 1 + sprintf(output+1,
742 "\x1B%c", code + 'P' - 11 - offt);
744 end = 1 + sprintf(output+1,
745 "\x1BO%c", code + 'P' - 11 - offt);
748 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
749 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
752 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
753 if (inst->term->vt52_mode)
754 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
756 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
759 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
760 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
764 end = 1 + sprintf(output+1, "\x1B[%d~", code);
770 * Cursor keys. (This includes the numberpad cursor keys,
771 * if we haven't already done them due to app keypad mode.)
773 * Here we also process un-numlocked un-appkeypadded KP5,
774 * which sends ESC [ G.
778 switch (event->keyval) {
779 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
780 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
781 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
782 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
783 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
787 * The arrow keys normally do ESC [ A and so on. In
788 * app cursor keys mode they do ESC O A instead.
789 * Ctrl toggles the two modes.
791 if (inst->term->vt52_mode) {
792 end = 1 + sprintf(output+1, "\033%c", xkey);
793 } else if (!inst->term->app_cursor_keys ^
794 !(event->state & GDK_CONTROL_MASK)) {
795 end = 1 + sprintf(output+1, "\033O%c", xkey);
797 end = 1 + sprintf(output+1, "\033[%c", xkey);
810 printf("generating sequence:");
811 for (i = start; i < end; i++)
812 printf(" %02x", (unsigned char) output[i]);
816 ldisc_send(inst->ldisc, output+start, end-start, 1);
817 show_mouseptr(inst, 0);
818 term_seen_key_event(inst->term);
819 term_out(inst->term);
825 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
827 struct gui_data *inst = (struct gui_data *)data;
828 int shift, ctrl, alt, x, y, button, act;
830 show_mouseptr(inst, 1);
832 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
833 term_scroll(inst->term, 0, -5);
836 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
837 term_scroll(inst->term, 0, +5);
841 shift = event->state & GDK_SHIFT_MASK;
842 ctrl = event->state & GDK_CONTROL_MASK;
843 alt = event->state & GDK_MOD1_MASK;
844 if (event->button == 1)
846 else if (event->button == 2)
848 else if (event->button == 3)
851 return FALSE; /* don't even know what button! */
853 switch (event->type) {
854 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
855 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
856 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
857 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
858 default: return FALSE; /* don't know this event type */
861 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
862 act != MA_CLICK && act != MA_RELEASE)
863 return TRUE; /* we ignore these in raw mouse mode */
865 x = (event->x - cfg.window_border) / inst->font_width;
866 y = (event->y - cfg.window_border) / inst->font_height;
868 term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
873 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
875 struct gui_data *inst = (struct gui_data *)data;
876 int shift, ctrl, alt, x, y, button;
878 show_mouseptr(inst, 1);
880 shift = event->state & GDK_SHIFT_MASK;
881 ctrl = event->state & GDK_CONTROL_MASK;
882 alt = event->state & GDK_MOD1_MASK;
883 if (event->state & GDK_BUTTON1_MASK)
885 else if (event->state & GDK_BUTTON2_MASK)
887 else if (event->state & GDK_BUTTON3_MASK)
890 return FALSE; /* don't even know what button! */
892 x = (event->x - cfg.window_border) / inst->font_width;
893 y = (event->y - cfg.window_border) / inst->font_height;
895 term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
900 void done_with_pty(struct gui_data *inst)
902 extern void pty_close(void);
904 if (inst->master_fd >= 0) {
906 inst->master_fd = -1;
907 gtk_input_remove(inst->master_func_id);
910 if (!inst->exited && inst->back->exitcode(inst->backhandle) >= 0) {
911 int exitcode = inst->back->exitcode(inst->backhandle);
914 clean = WIFEXITED(exitcode) && (WEXITSTATUS(exitcode) == 0);
917 * Terminate now, if the Close On Exit setting is
920 if (cfg.close_on_exit == COE_ALWAYS ||
921 (cfg.close_on_exit == COE_NORMAL && clean))
925 * Otherwise, output an indication that the session has
930 if (WIFEXITED(exitcode))
931 sprintf(message, "\r\n[pterm: process terminated with exit"
932 " code %d]\r\n", WEXITSTATUS(exitcode));
933 else if (WIFSIGNALED(exitcode))
934 #ifdef HAVE_NO_STRSIGNAL
935 sprintf(message, "\r\n[pterm: process terminated on signal"
936 " %d]\r\n", WTERMSIG(exitcode));
938 sprintf(message, "\r\n[pterm: process terminated on signal"
939 " %d (%.400s)]\r\n", WTERMSIG(exitcode),
940 strsignal(WTERMSIG(exitcode)));
942 from_backend((void *)inst->term, 0, message, strlen(message));
948 void frontend_keypress(void *handle)
950 struct gui_data *inst = (struct gui_data *)handle;
953 * If our child process has exited but not closed, terminate on
960 gint timer_func(gpointer data)
962 struct gui_data *inst = (struct gui_data *)data;
964 if (inst->back->exitcode(inst->backhandle) >= 0) {
966 * The primary child process died. We could keep the
967 * terminal open for remaining subprocesses to output to,
968 * but conventional wisdom seems to feel that that's the
969 * Wrong Thing for an xterm-alike, so we bail out now
970 * (though we don't necessarily _close_ the window,
971 * depending on the state of Close On Exit). This would be
972 * easy enough to change or make configurable if necessary.
977 term_update(inst->term);
978 term_blink(inst->term, 0);
982 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
984 struct gui_data *inst = (struct gui_data *)data;
988 ret = read(sourcefd, buf, sizeof(buf));
991 * Clean termination condition is that either ret == 0, or ret
992 * < 0 and errno == EIO. Not sure why the latter, but it seems
995 if (ret == 0 || (ret < 0 && errno == EIO)) {
997 } else if (ret < 0) {
998 perror("read pty master");
1001 from_backend(inst->term, 0, buf, ret);
1002 term_blink(inst->term, 1);
1003 term_out(inst->term);
1006 void destroy(GtkWidget *widget, gpointer data)
1011 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1013 struct gui_data *inst = (struct gui_data *)data;
1014 inst->term->has_focus = event->in;
1015 term_out(inst->term);
1016 term_update(inst->term);
1017 show_mouseptr(inst, 1);
1022 * set or clear the "raw mouse message" mode
1024 void set_raw_mouse_mode(void *frontend, int activate)
1026 struct gui_data *inst = (struct gui_data *)frontend;
1027 activate = activate && !cfg.no_mouse_rep;
1028 send_raw_mouse = activate;
1030 inst->currcursor = inst->rawcursor;
1032 inst->currcursor = inst->textcursor;
1033 show_mouseptr(inst, inst->mouseptr_visible);
1036 void request_resize(void *frontend, int w, int h)
1038 struct gui_data *inst = (struct gui_data *)frontend;
1039 int large_x, large_y;
1040 int offset_x, offset_y;
1042 GtkRequisition inner, outer;
1045 * This is a heinous hack dreamed up by the gnome-terminal
1046 * people to get around a limitation in gtk. The problem is
1047 * that in order to set the size correctly we really need to be
1048 * calling gtk_window_resize - but that needs to know the size
1049 * of the _whole window_, not the drawing area. So what we do
1050 * is to set an artificially huge size request on the drawing
1051 * area, recompute the resulting size request on the window,
1052 * and look at the difference between the two. That gives us
1053 * the x and y offsets we need to translate drawing area size
1054 * into window size for real, and then we call
1055 * gtk_window_resize.
1059 * We start by retrieving the current size of the whole window.
1060 * Adding a bit to _that_ will give us a value we can use as a
1061 * bogus size request which guarantees to be bigger than the
1062 * current size of the drawing area.
1064 get_window_pixels(inst, &large_x, &large_y);
1068 #if GTK_CHECK_VERSION(2,0,0)
1069 gtk_widget_set_size_request(inst->area, large_x, large_y);
1071 gtk_widget_set_usize(inst->area, large_x, large_y);
1073 gtk_widget_size_request(inst->area, &inner);
1074 gtk_widget_size_request(inst->window, &outer);
1076 offset_x = outer.width - inner.width;
1077 offset_y = outer.height - inner.height;
1079 area_x = inst->font_width * w + 2*cfg.window_border;
1080 area_y = inst->font_height * h + 2*cfg.window_border;
1083 * Now we must set the size request on the drawing area back to
1084 * something sensible before we commit the real resize. Best
1085 * way to do this, I think, is to set it to what the size is
1086 * really going to end up being.
1088 #if GTK_CHECK_VERSION(2,0,0)
1089 gtk_widget_set_size_request(inst->area, area_x, area_y);
1091 gtk_widget_set_usize(inst->area, area_x, area_y);
1094 #if GTK_CHECK_VERSION(2,0,0)
1095 gtk_window_resize(GTK_WINDOW(inst->window),
1096 area_x + offset_x, area_y + offset_y);
1098 gdk_window_resize(inst->window->window,
1099 area_x + offset_x, area_y + offset_y);
1103 static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
1105 gboolean success[1];
1107 inst->cols[n].red = r * 0x0101;
1108 inst->cols[n].green = g * 0x0101;
1109 inst->cols[n].blue = b * 0x0101;
1111 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1112 FALSE, FALSE, success);
1114 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1118 void set_window_background(struct gui_data *inst)
1120 if (inst->area && inst->area->window)
1121 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1122 if (inst->window && inst->window->window)
1123 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1126 void palette_set(void *frontend, int n, int r, int g, int b)
1128 struct gui_data *inst = (struct gui_data *)frontend;
1129 static const int first[21] = {
1130 0, 2, 4, 6, 8, 10, 12, 14,
1131 1, 3, 5, 7, 9, 11, 13, 15,
1134 real_palette_set(inst, first[n], r, g, b);
1136 real_palette_set(inst, first[n] + 1, r, g, b);
1138 set_window_background(inst);
1141 void palette_reset(void *frontend)
1143 struct gui_data *inst = (struct gui_data *)frontend;
1144 /* This maps colour indices in cfg to those used in inst->cols. */
1145 static const int ww[] = {
1146 6, 7, 8, 9, 10, 11, 12, 13,
1147 14, 15, 16, 17, 18, 19, 20, 21,
1150 gboolean success[NCOLOURS];
1153 assert(lenof(ww) == NCOLOURS);
1155 if (!inst->colmap) {
1156 inst->colmap = gdk_colormap_get_system();
1158 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1161 for (i = 0; i < NCOLOURS; i++) {
1162 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1163 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1164 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1167 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1168 FALSE, FALSE, success);
1169 for (i = 0; i < NCOLOURS; i++) {
1171 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1172 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1175 set_window_background(inst);
1178 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1180 struct gui_data *inst = (struct gui_data *)frontend;
1181 if (inst->pasteout_data)
1182 sfree(inst->pasteout_data);
1183 inst->pasteout_data = smalloc(len);
1184 inst->pasteout_data_len = len;
1185 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1188 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1189 GDK_CURRENT_TIME)) {
1190 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1191 GDK_SELECTION_TYPE_STRING, 1);
1192 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1193 inst->compound_text_atom, 1);
1197 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1198 guint info, guint time_stamp, gpointer data)
1200 struct gui_data *inst = (struct gui_data *)data;
1201 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1202 inst->pasteout_data, inst->pasteout_data_len);
1205 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1208 struct gui_data *inst = (struct gui_data *)data;
1209 term_deselect(inst->term);
1210 if (inst->pasteout_data)
1211 sfree(inst->pasteout_data);
1212 inst->pasteout_data = NULL;
1213 inst->pasteout_data_len = 0;
1217 void request_paste(void *frontend)
1219 struct gui_data *inst = (struct gui_data *)frontend;
1221 * In Unix, pasting is asynchronous: all we can do at the
1222 * moment is to call gtk_selection_convert(), and when the data
1223 * comes back _then_ we can call term_do_paste().
1225 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1226 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1229 gint idle_paste_func(gpointer data); /* forward ref */
1231 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1234 struct gui_data *inst = (struct gui_data *)data;
1236 if (seldata->length <= 0 ||
1237 seldata->type != GDK_SELECTION_TYPE_STRING)
1238 return; /* Nothing happens. */
1240 if (inst->pastein_data)
1241 sfree(inst->pastein_data);
1243 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1244 inst->pastein_data_len = seldata->length;
1245 mb_to_wc(0, 0, seldata->data, seldata->length,
1246 inst->pastein_data, inst->pastein_data_len);
1248 term_do_paste(inst->term);
1250 if (term_paste_pending(inst->term))
1251 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1254 gint idle_paste_func(gpointer data)
1256 struct gui_data *inst = (struct gui_data *)data;
1258 if (term_paste_pending(inst->term))
1259 term_paste(inst->term);
1261 gtk_idle_remove(inst->term_paste_idle_id);
1267 void get_clip(void *frontend, wchar_t ** p, int *len)
1269 struct gui_data *inst = (struct gui_data *)frontend;
1272 *p = inst->pastein_data;
1273 *len = inst->pastein_data_len;
1277 void set_title(void *frontend, char *title)
1279 struct gui_data *inst = (struct gui_data *)frontend;
1280 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1281 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1282 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1285 void set_icon(void *frontend, char *title)
1287 struct gui_data *inst = (struct gui_data *)frontend;
1288 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1289 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1290 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1293 void set_sbar(void *frontend, int total, int start, int page)
1295 struct gui_data *inst = (struct gui_data *)frontend;
1298 inst->sbar_adjust->lower = 0;
1299 inst->sbar_adjust->upper = total;
1300 inst->sbar_adjust->value = start;
1301 inst->sbar_adjust->page_size = page;
1302 inst->sbar_adjust->step_increment = 1;
1303 inst->sbar_adjust->page_increment = page/2;
1304 inst->ignore_sbar = TRUE;
1305 gtk_adjustment_changed(inst->sbar_adjust);
1306 inst->ignore_sbar = FALSE;
1309 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1311 struct gui_data *inst = (struct gui_data *)data;
1315 if (!inst->ignore_sbar)
1316 term_scroll(inst->term, 1, (int)adj->value);
1319 void sys_cursor(void *frontend, int x, int y)
1322 * This is meaningless under X.
1326 void beep(void *frontend, int mode)
1331 int CharWidth(Context ctx, int uc)
1334 * Under X, any fixed-width font really _is_ fixed-width.
1335 * Double-width characters will be dealt with using a separate
1336 * font. For the moment we can simply return 1.
1341 Context get_ctx(void *frontend)
1343 struct gui_data *inst = (struct gui_data *)frontend;
1344 struct draw_ctx *dctx;
1346 if (!inst->area->window)
1349 dctx = smalloc(sizeof(*dctx));
1351 dctx->gc = gdk_gc_new(inst->area->window);
1355 void free_ctx(Context ctx)
1357 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1358 /* struct gui_data *inst = dctx->inst; */
1359 GdkGC *gc = dctx->gc;
1365 * Draw a line of text in the window, at given character
1366 * coordinates, in given attributes.
1368 * We are allowed to fiddle with the contents of `text'.
1370 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1371 unsigned long attr, int lattr)
1373 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1374 struct gui_data *inst = dctx->inst;
1375 GdkGC *gc = dctx->gc;
1377 int nfg, nbg, t, fontid, shadow;
1381 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1384 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1385 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1386 if (attr & ATTR_REVERSE) {
1391 if (cfg.bold_colour && (attr & ATTR_BOLD))
1393 if (cfg.bold_colour && (attr & ATTR_BLINK))
1395 if (attr & TATTR_ACTCURS) {
1400 fontid = shadow = 0;
1401 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1408 if (lattr != LATTR_NORM) {
1410 if (x >= inst->term->cols)
1412 if (x + len*2 > inst->term->cols)
1413 len = (inst->term->cols-x)/2; /* trim to LH half */
1416 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1417 gdk_draw_rectangle(inst->pixmap, gc, 1,
1418 x*inst->font_width+cfg.window_border,
1419 y*inst->font_height+cfg.window_border,
1420 len*inst->font_width, inst->font_height);
1422 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1423 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1424 x*inst->font_width+cfg.window_border,
1425 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1429 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1430 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1431 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1435 if (attr & ATTR_UNDER) {
1436 int uheight = inst->fonts[0]->ascent + 1;
1437 if (uheight >= inst->font_height)
1438 uheight = inst->font_height - 1;
1439 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1440 y*inst->font_height + uheight + cfg.window_border,
1441 (x+len)*inst->font_width-1+cfg.window_border,
1442 y*inst->font_height + uheight + cfg.window_border);
1445 if (lattr != LATTR_NORM) {
1447 * I can't find any plausible StretchBlt equivalent in the
1448 * X server, so I'm going to do this the slow and painful
1449 * way. This will involve repeated calls to
1450 * gdk_draw_pixmap() to stretch the text horizontally. It's
1451 * O(N^2) in time and O(N) in network bandwidth, but you
1452 * try thinking of a better way. :-(
1455 for (i = 0; i < len * inst->font_width; i++) {
1456 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1457 x*inst->font_width+cfg.window_border + 2*i,
1458 y*inst->font_height+cfg.window_border,
1459 x*inst->font_width+cfg.window_border + 2*i+1,
1460 y*inst->font_height+cfg.window_border,
1461 len * inst->font_width - i, inst->font_height);
1464 if (lattr != LATTR_WIDE) {
1466 /* Now stretch vertically, in the same way. */
1467 if (lattr == LATTR_BOT)
1471 for (i = 0; i < inst->font_height; i+=2) {
1472 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1473 x*inst->font_width+cfg.window_border,
1474 y*inst->font_height+cfg.window_border+dt*i+db,
1475 x*inst->font_width+cfg.window_border,
1476 y*inst->font_height+cfg.window_border+dt*(i+1),
1477 len * inst->font_width, inst->font_height-i-1);
1483 void do_text(Context ctx, int x, int y, char *text, int len,
1484 unsigned long attr, int lattr)
1486 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1487 struct gui_data *inst = dctx->inst;
1488 GdkGC *gc = dctx->gc;
1490 do_text_internal(ctx, x, y, text, len, attr, lattr);
1492 if (lattr != LATTR_NORM) {
1494 if (x >= inst->term->cols)
1496 if (x + len*2 > inst->term->cols)
1497 len = (inst->term->cols-x)/2; /* trim to LH half */
1501 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1502 x*inst->font_width+cfg.window_border,
1503 y*inst->font_height+cfg.window_border,
1504 x*inst->font_width+cfg.window_border,
1505 y*inst->font_height+cfg.window_border,
1506 len*inst->font_width, inst->font_height);
1509 void do_cursor(Context ctx, int x, int y, char *text, int len,
1510 unsigned long attr, int lattr)
1512 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1513 struct gui_data *inst = dctx->inst;
1514 GdkGC *gc = dctx->gc;
1518 if (attr & TATTR_PASCURS) {
1519 attr &= ~TATTR_PASCURS;
1523 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1524 attr &= ~TATTR_ACTCURS;
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 if (cfg.cursor_type == 0) {
1539 * An active block cursor will already have been done by
1540 * the above do_text call, so we only need to do anything
1544 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1545 gdk_draw_rectangle(inst->pixmap, gc, 0,
1546 x*inst->font_width+cfg.window_border,
1547 y*inst->font_height+cfg.window_border,
1548 len*inst->font_width-1, inst->font_height-1);
1552 int startx, starty, dx, dy, length, i;
1556 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1557 char_width = 2*inst->font_width;
1559 char_width = inst->font_width;
1561 if (cfg.cursor_type == 1) {
1562 uheight = inst->fonts[0]->ascent + 1;
1563 if (uheight >= inst->font_height)
1564 uheight = inst->font_height - 1;
1566 startx = x * inst->font_width + cfg.window_border;
1567 starty = y * inst->font_height + cfg.window_border + uheight;
1570 length = len * char_width;
1573 if (attr & TATTR_RIGHTCURS)
1574 xadjust = char_width - 1;
1575 startx = x * inst->font_width + cfg.window_border + xadjust;
1576 starty = y * inst->font_height + cfg.window_border;
1579 length = inst->font_height;
1582 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1584 for (i = 0; i < length; i++) {
1586 gdk_draw_point(inst->pixmap, gc, startx, starty);
1592 gdk_draw_line(inst->pixmap, gc, startx, starty,
1593 startx + (length-1) * dx, starty + (length-1) * dy);
1597 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1598 x*inst->font_width+cfg.window_border,
1599 y*inst->font_height+cfg.window_border,
1600 x*inst->font_width+cfg.window_border,
1601 y*inst->font_height+cfg.window_border,
1602 len*inst->font_width, inst->font_height);
1605 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1608 * Truly hideous hack: GTK doesn't allow us to set the mouse
1609 * cursor foreground and background colours unless we've _also_
1610 * created our own cursor from bitmaps. Therefore, I need to
1611 * load the `cursor' font and draw glyphs from it on to
1612 * pixmaps, in order to construct my cursors with the fg and bg
1613 * I want. This is a gross hack, but it's more self-contained
1614 * than linking in Xlib to find the X window handle to
1615 * inst->area and calling XRecolorCursor, and it's more
1616 * futureproof than hard-coding the shapes as bitmap arrays.
1618 static GdkFont *cursor_font = NULL;
1619 GdkPixmap *source, *mask;
1621 GdkColor cfg = { 0, 65535, 65535, 65535 };
1622 GdkColor cbg = { 0, 0, 0, 0 };
1623 GdkColor dfg = { 1, 65535, 65535, 65535 };
1624 GdkColor dbg = { 0, 0, 0, 0 };
1627 gint lb, rb, wid, asc, desc, w, h, x, y;
1629 if (cursor_val == -2) {
1630 gdk_font_unref(cursor_font);
1634 if (cursor_val >= 0 && !cursor_font)
1635 cursor_font = gdk_font_load("cursor");
1638 * Get the text extent of the cursor in question. We use the
1639 * mask character for this, because it's typically slightly
1640 * bigger than the main character.
1642 if (cursor_val >= 0) {
1644 text[0] = (char)cursor_val + 1;
1645 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1646 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1652 source = gdk_pixmap_new(NULL, w, h, 1);
1653 mask = gdk_pixmap_new(NULL, w, h, 1);
1656 * Draw the mask character on the mask pixmap.
1658 gc = gdk_gc_new(mask);
1659 gdk_gc_set_foreground(gc, &dbg);
1660 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1661 if (cursor_val >= 0) {
1663 text[0] = (char)cursor_val + 1;
1664 gdk_gc_set_foreground(gc, &dfg);
1665 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1670 * Draw the main character on the source pixmap.
1672 gc = gdk_gc_new(source);
1673 gdk_gc_set_foreground(gc, &dbg);
1674 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1675 if (cursor_val >= 0) {
1677 text[0] = (char)cursor_val;
1678 gdk_gc_set_foreground(gc, &dfg);
1679 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1684 * Create the cursor.
1686 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1691 gdk_pixmap_unref(source);
1692 gdk_pixmap_unref(mask);
1697 void modalfatalbox(char *p, ...)
1700 fprintf(stderr, "FATAL ERROR: ");
1702 vfprintf(stderr, p, ap);
1704 fputc('\n', stderr);
1708 char *get_x_display(void *frontend)
1710 return gdk_get_display();
1713 char *app_name = "pterm";
1715 int do_cmdline(int argc, char **argv, int do_everything)
1718 extern char **pty_argv; /* declared in pty.c */
1721 * Macros to make argument handling easier.
1723 #define EXPECTS_ARG do { \
1724 if (--argc <= 0) { \
1726 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1730 #define SECOND_PASS_ONLY do { \
1731 if (!do_everything) continue; \
1741 while (--argc > 0) {
1743 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1746 strncpy(cfg.font, val, sizeof(cfg.font));
1747 cfg.font[sizeof(cfg.font)-1] = '\0';
1749 } else if (!strcmp(p, "-fb")) {
1752 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1753 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1755 } else if (!strcmp(p, "-geometry")) {
1756 int flags, x, y, w, h;
1760 flags = XParseGeometry(val, &x, &y, &w, &h);
1761 if (flags & WidthValue)
1763 if (flags & HeightValue)
1767 * Apparently setting the initial window position is
1768 * difficult in GTK 1.2. Not entirely sure why this
1769 * should be. 2.0 has gtk_window_parse_geometry(),
1770 * which would help... For the moment, though, I can't
1771 * be bothered with this.
1774 } else if (!strcmp(p, "-sl")) {
1777 cfg.savelines = atoi(val);
1779 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1780 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1781 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1786 if (!gdk_color_parse(val, &col)) {
1788 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1791 index = (!strcmp(p, "-fg") ? 0 :
1792 !strcmp(p, "-bg") ? 2 :
1793 !strcmp(p, "-bfg") ? 1 :
1794 !strcmp(p, "-bbg") ? 3 :
1795 !strcmp(p, "-cfg") ? 4 :
1796 !strcmp(p, "-cbg") ? 5 : -1);
1797 assert(index != -1);
1798 cfg.colours[index][0] = col.red / 256;
1799 cfg.colours[index][1] = col.green / 256;
1800 cfg.colours[index][2] = col.blue / 256;
1803 } else if (!strcmp(p, "-e")) {
1804 /* This option swallows all further arguments. */
1810 pty_argv = smalloc((argc+1) * sizeof(char *));
1812 for (i = 0; i < argc; i++)
1813 pty_argv[i] = argv[i];
1814 pty_argv[argc] = NULL;
1815 break; /* finished command-line processing */
1817 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1819 } else if (!strcmp(p, "-T")) {
1822 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1823 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1825 } else if (!strcmp(p, "-log")) {
1828 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1829 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1830 cfg.logtype = LGTYP_DEBUG;
1832 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1836 } else if (!strcmp(p, "-ut")) {
1840 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1842 cfg.login_shell = 0;
1844 } else if (!strcmp(p, "-ls")) {
1846 cfg.login_shell = 1;
1848 } else if (!strcmp(p, "-nethack")) {
1850 cfg.nethack_keypad = 1;
1852 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1856 } else if (!strcmp(p, "-sb")) {
1860 } else if (!strcmp(p, "-name")) {
1864 } else if (!strcmp(p, "-xrm")) {
1866 provide_xrm_string(val);
1870 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
1877 int main(int argc, char **argv)
1879 extern int pty_master_fd; /* declared in pty.c */
1880 extern void pty_pre_init(void); /* declared in pty.c */
1881 struct gui_data *inst;
1885 gtk_init(&argc, &argv);
1887 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
1889 do_defaults(NULL, &cfg);
1890 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
1894 * Create an instance structure and initialise to zeroes
1896 inst = smalloc(sizeof(*inst));
1897 memset(inst, 0, sizeof(*inst));
1899 inst->fonts[0] = gdk_font_load(cfg.font);
1900 if (!inst->fonts[0]) {
1901 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1904 if (cfg.boldfont[0]) {
1905 inst->fonts[1] = gdk_font_load(cfg.boldfont);
1906 if (!inst->fonts[1]) {
1907 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1912 inst->fonts[1] = NULL;
1914 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1915 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1917 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1921 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1923 if (cfg.wintitle[0])
1924 set_title(inst, cfg.wintitle);
1926 set_title(inst, "pterm");
1929 * Set up the colour map.
1931 palette_reset(inst);
1933 inst->area = gtk_drawing_area_new();
1934 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1935 inst->font_width * cfg.width + 2*cfg.window_border,
1936 inst->font_height * cfg.height + 2*cfg.window_border);
1937 if (cfg.scrollbar) {
1938 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1939 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1941 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1942 if (cfg.scrollbar) {
1943 if (cfg.scrollbar_on_left)
1944 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1946 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1948 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1950 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1954 geom.min_width = inst->font_width + 2*cfg.window_border;
1955 geom.min_height = inst->font_height + 2*cfg.window_border;
1956 geom.max_width = geom.max_height = -1;
1957 geom.base_width = 2*cfg.window_border;
1958 geom.base_height = 2*cfg.window_border;
1959 geom.width_inc = inst->font_width;
1960 geom.height_inc = inst->font_height;
1961 geom.min_aspect = geom.max_aspect = 0;
1962 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1963 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1964 GDK_HINT_RESIZE_INC);
1967 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1968 GTK_SIGNAL_FUNC(destroy), inst);
1969 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1970 GTK_SIGNAL_FUNC(delete_window), inst);
1971 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1972 GTK_SIGNAL_FUNC(key_event), inst);
1973 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
1974 GTK_SIGNAL_FUNC(key_event), inst);
1975 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1976 GTK_SIGNAL_FUNC(focus_event), inst);
1977 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1978 GTK_SIGNAL_FUNC(focus_event), inst);
1979 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1980 GTK_SIGNAL_FUNC(configure_area), inst);
1981 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1982 GTK_SIGNAL_FUNC(expose_area), inst);
1983 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1984 GTK_SIGNAL_FUNC(button_event), inst);
1985 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1986 GTK_SIGNAL_FUNC(button_event), inst);
1987 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1988 GTK_SIGNAL_FUNC(motion_event), inst);
1989 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1990 GTK_SIGNAL_FUNC(selection_received), inst);
1991 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1992 GTK_SIGNAL_FUNC(selection_get), inst);
1993 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1994 GTK_SIGNAL_FUNC(selection_clear), inst);
1996 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1997 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
1998 gtk_timeout_add(20, timer_func, inst);
1999 gtk_widget_add_events(GTK_WIDGET(inst->area),
2000 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2001 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2002 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2004 gtk_widget_show(inst->area);
2006 gtk_widget_show(inst->sbar);
2007 gtk_widget_show(GTK_WIDGET(inst->hbox));
2008 gtk_widget_show(inst->window);
2010 set_window_background(inst);
2012 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2013 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2014 inst->blankcursor = make_mouse_ptr(inst, -1);
2015 make_mouse_ptr(inst, -2); /* clean up cursor font */
2016 inst->currcursor = inst->textcursor;
2017 show_mouseptr(inst, 1);
2019 inst->term = term_init(inst);
2020 inst->logctx = log_init(inst);
2021 term_provide_logctx(inst->term, inst->logctx);
2023 inst->back = &pty_backend;
2024 inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
2025 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2027 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2029 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
2031 inst->ldisc = ldisc_create(inst->term, inst->back, inst->backhandle, inst);
2032 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2034 inst->master_fd = pty_master_fd;
2035 inst->exited = FALSE;
2036 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
2037 pty_input_func, inst);