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;
62 static struct gui_data the_inst;
63 static struct gui_data *inst = &the_inst; /* so we always write `inst->' */
64 static int send_raw_mouse;
66 void ldisc_update(void *frontend, int echo, int edit)
69 * This is a stub in pterm. If I ever produce a Unix
70 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
71 * then it will require some termios manoeuvring analogous to
72 * that in the Windows plink.c, but here it's meaningless.
76 int askappend(char *filename)
79 * Logging in an xterm-alike is liable to be something you only
80 * do at serious diagnostic need. Hence, I'm going to take the
81 * easy option for now and assume we always want to overwrite
82 * log files. I can always make it properly configurable later.
87 void logevent(char *string)
90 * This is not a very helpful function: events are logged
91 * pretty much exclusively by the back end, and our pty back
92 * end is self-contained. So we need do nothing.
96 int font_dimension(int which) /* 0 for width, 1 for height */
99 return inst->font_height;
101 return inst->font_width;
105 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
106 * into a cooked one (SELECT, EXTEND, PASTE).
108 * In Unix, this is not configurable; the X button arrangement is
109 * rock-solid across all applications, everyone has a three-button
110 * mouse or a means of faking it, and there is no need to switch
111 * buttons around at all.
113 Mouse_Button translate_button(Mouse_Button button)
115 if (button == MBT_LEFT)
117 if (button == MBT_MIDDLE)
119 if (button == MBT_RIGHT)
121 return 0; /* shouldn't happen */
125 * Minimise or restore the window in response to a server-side
128 void set_iconic(int iconic)
131 * GTK 1.2 doesn't know how to do this.
133 #if GTK_CHECK_VERSION(2,0,0)
135 gtk_window_iconify(GTK_WINDOW(inst->window));
137 gtk_window_deiconify(GTK_WINDOW(inst->window));
142 * Move the window in response to a server-side request.
144 void move_window(int x, int y)
147 * I assume that when the GTK version of this call is available
148 * we should use it. Not sure how it differs from the GDK one,
151 #if GTK_CHECK_VERSION(2,0,0)
152 gtk_window_move(GTK_WINDOW(inst->window), x, y);
154 gdk_window_move(inst->window->window, x, y);
159 * Move the window to the top or bottom of the z-order in response
160 * to a server-side request.
162 void set_zorder(int top)
165 gdk_window_raise(inst->window->window);
167 gdk_window_lower(inst->window->window);
171 * Refresh the window in response to a server-side request.
173 void refresh_window(void)
175 term_invalidate(inst->term);
179 * Maximise or restore the window in response to a server-side
182 void set_zoomed(int zoomed)
185 * GTK 1.2 doesn't know how to do this.
187 #if GTK_CHECK_VERSION(2,0,0)
189 gtk_window_maximize(GTK_WINDOW(inst->window));
191 gtk_window_unmaximize(GTK_WINDOW(inst->window));
196 * Report whether the window is iconic, for terminal reports.
200 return !gdk_window_is_viewable(inst->window->window);
204 * Report the window's position, for terminal reports.
206 void get_window_pos(int *x, int *y)
209 * I assume that when the GTK version of this call is available
210 * we should use it. Not sure how it differs from the GDK one,
213 #if GTK_CHECK_VERSION(2,0,0)
214 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
216 gdk_window_get_position(inst->window->window, x, y);
221 * Report the window's pixel size, for terminal reports.
223 void get_window_pixels(int *x, int *y)
226 * I assume that when the GTK version of this call is available
227 * we should use it. Not sure how it differs from the GDK one,
230 #if GTK_CHECK_VERSION(2,0,0)
231 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
233 gdk_window_get_size(inst->window->window, x, y);
238 * Return the window or icon title.
240 char *get_window_title(int icon)
242 return icon ? inst->wintitle : inst->icontitle;
245 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
248 * We could implement warn-on-close here if we really wanted
254 void show_mouseptr(int show)
256 if (!cfg.hide_mouseptr)
259 gdk_window_set_cursor(inst->area->window, inst->currcursor);
261 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
262 inst->mouseptr_visible = show;
265 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
267 struct gui_data *inst = (struct gui_data *)data;
268 int w, h, need_size = 0;
270 w = (event->width - 2*cfg.window_border) / inst->font_width;
271 h = (event->height - 2*cfg.window_border) / inst->font_height;
273 if (w != cfg.width || h != cfg.height) {
275 gdk_pixmap_unref(inst->pixmap);
285 inst->pixmap = gdk_pixmap_new(widget->window,
286 (cfg.width * inst->font_width +
287 2*cfg.window_border),
288 (cfg.height * inst->font_height +
289 2*cfg.window_border), -1);
291 gc = gdk_gc_new(inst->area->window);
292 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
293 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
294 cfg.width * inst->font_width + 2*cfg.window_border,
295 cfg.height * inst->font_height + 2*cfg.window_border);
300 term_size(inst->term, h, w, cfg.savelines);
306 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
308 /* struct gui_data *inst = (struct gui_data *)data; */
311 * Pass the exposed rectangle to terminal.c, which will call us
312 * back to do the actual painting.
315 gdk_draw_pixmap(widget->window,
316 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
318 event->area.x, event->area.y,
319 event->area.x, event->area.y,
320 event->area.width, event->area.height);
325 #define KEY_PRESSED(k) \
326 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
328 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
330 /* struct gui_data *inst = (struct gui_data *)data; */
334 /* By default, nothing is generated. */
338 * If Alt is being released after typing an Alt+numberpad
339 * sequence, we should generate the code that was typed.
341 if (event->type == GDK_KEY_RELEASE &&
342 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
343 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
344 inst->alt_keycode >= 0) {
346 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
348 output[0] = inst->alt_keycode;
353 if (event->type == GDK_KEY_PRESS) {
357 printf("keypress: keyval = %04x, state = %08x; string =",
358 event->keyval, event->state);
359 for (i = 0; event->string[i]; i++)
360 printf(" %02x", (unsigned char) event->string[i]);
366 * NYI: Compose key (!!! requires Unicode faff before even trying)
370 * If Alt has just been pressed, we start potentially
371 * accumulating an Alt+numberpad code. We do this by
372 * setting alt_keycode to -1 (nothing yet but plausible).
374 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
375 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
376 inst->alt_keycode = -1;
377 goto done; /* this generates nothing else */
381 * If we're seeing a numberpad key press with Mod1 down,
382 * consider adding it to alt_keycode if that's sensible.
383 * Anything _else_ with Mod1 down cancels any possibility
384 * of an ALT keycode: we set alt_keycode to -2.
386 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
388 switch (event->keyval) {
389 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
390 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
391 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
392 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
393 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
394 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
395 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
396 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
397 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
398 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
401 inst->alt_keycode = -2; /* it's invalid */
404 printf("Adding digit %d to keycode %d", digit,
407 if (inst->alt_keycode == -1)
408 inst->alt_keycode = digit; /* one-digit code */
410 inst->alt_keycode = inst->alt_keycode * 10 + digit;
412 printf(" gives new code %d\n", inst->alt_keycode);
414 /* Having used this digit, we now do nothing more with it. */
420 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
423 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
424 term_scroll(inst->term, 0, -cfg.height/2);
427 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
428 term_scroll(inst->term, 0, +cfg.height/2);
433 * Neither does Shift-Ins.
435 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
440 /* ALT+things gives leading Escape. */
442 strncpy(output+1, event->string, 31);
444 end = strlen(output);
445 if (event->state & GDK_MOD1_MASK) {
447 if (end == 1) end = 0;
451 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
452 if (!event->string[0] && event->keyval == '`' &&
453 (event->state & GDK_CONTROL_MASK)) {
458 /* Control-Break is the same as Control-C */
459 if (event->keyval == GDK_Break &&
460 (event->state & GDK_CONTROL_MASK)) {
465 /* Control-2, Control-Space and Control-@ are NUL */
466 if (!event->string[0] &&
467 (event->keyval == ' ' || event->keyval == '2' ||
468 event->keyval == '@') &&
469 (event->state & (GDK_SHIFT_MASK |
470 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
475 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
476 if (!event->string[0] && event->keyval == ' ' &&
477 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
478 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
483 /* We don't let GTK tell us what Backspace is! We know better. */
484 if (event->keyval == GDK_BackSpace &&
485 !(event->state & GDK_SHIFT_MASK)) {
486 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
489 /* For Shift Backspace, do opposite of what is configured. */
490 if (event->keyval == GDK_BackSpace &&
491 (event->state & GDK_SHIFT_MASK)) {
492 output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
496 /* Shift-Tab is ESC [ Z */
497 if (event->keyval == GDK_ISO_Left_Tab ||
498 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
499 end = 1 + sprintf(output+1, "\033[Z");
503 * NetHack keypad mode.
505 if (cfg.nethack_keypad) {
507 switch (event->keyval) {
508 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
509 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
510 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
511 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
512 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
513 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
514 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
515 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
516 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
520 if (event->state & GDK_SHIFT_MASK)
529 * Application keypad mode.
531 if (inst->term->app_keypad_keys && !cfg.no_applic_k) {
533 switch (event->keyval) {
534 case GDK_Num_Lock: xkey = 'P'; break;
535 case GDK_KP_Divide: xkey = 'Q'; break;
536 case GDK_KP_Multiply: xkey = 'R'; break;
537 case GDK_KP_Subtract: xkey = 'S'; break;
539 * Keypad + is tricky. It covers a space that would
540 * be taken up on the VT100 by _two_ keys; so we
541 * let Shift select between the two. Worse still,
542 * in xterm function key mode we change which two...
545 if (cfg.funky_type == 2) {
546 if (event->state & GDK_SHIFT_MASK)
550 } else if (event->state & GDK_SHIFT_MASK)
555 case GDK_KP_Enter: xkey = 'M'; break;
556 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
557 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
558 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
559 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
560 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
561 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
562 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
563 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
564 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
565 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
566 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
569 if (inst->term->vt52_mode) {
570 if (xkey >= 'P' && xkey <= 'S')
571 end = 1 + sprintf(output+1, "\033%c", xkey);
573 end = 1 + sprintf(output+1, "\033?%c", xkey);
575 end = 1 + sprintf(output+1, "\033O%c", xkey);
581 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
582 * for integer decimal nn.)
584 * We also deal with the weird ones here. Linux VCs replace F1
585 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
586 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
591 switch (event->keyval) {
593 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
596 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
599 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
602 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
605 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
608 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
611 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
614 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
617 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
620 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
653 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
654 case GDK_Home: case GDK_KP_Home:
657 case GDK_Insert: case GDK_KP_Insert:
660 case GDK_Delete: case GDK_KP_Delete:
663 case GDK_End: case GDK_KP_End:
666 case GDK_Page_Up: case GDK_KP_Page_Up:
669 case GDK_Page_Down: case GDK_KP_Page_Down:
673 /* Reorder edit keys to physical order */
674 if (cfg.funky_type == 3 && code <= 6)
675 code = "\0\2\1\4\5\3\6"[code];
677 if (inst->term->vt52_mode && code > 0 && code <= 6) {
678 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
682 if (cfg.funky_type == 5 && /* SCO function keys */
683 code >= 11 && code <= 34) {
684 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
686 switch (event->keyval) {
687 case GDK_F1: index = 0; break;
688 case GDK_F2: index = 1; break;
689 case GDK_F3: index = 2; break;
690 case GDK_F4: index = 3; break;
691 case GDK_F5: index = 4; break;
692 case GDK_F6: index = 5; break;
693 case GDK_F7: index = 6; break;
694 case GDK_F8: index = 7; break;
695 case GDK_F9: index = 8; break;
696 case GDK_F10: index = 9; break;
697 case GDK_F11: index = 10; break;
698 case GDK_F12: index = 11; break;
700 if (event->state & GDK_SHIFT_MASK) index += 12;
701 if (event->state & GDK_CONTROL_MASK) index += 24;
702 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
705 if (cfg.funky_type == 5 && /* SCO small keypad */
706 code >= 1 && code <= 6) {
707 char codes[] = "HL.FIG";
712 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
716 if ((inst->term->vt52_mode || cfg.funky_type == 4) &&
717 code >= 11 && code <= 24) {
723 if (inst->term->vt52_mode)
724 end = 1 + sprintf(output+1,
725 "\x1B%c", code + 'P' - 11 - offt);
727 end = 1 + sprintf(output+1,
728 "\x1BO%c", code + 'P' - 11 - offt);
731 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
732 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
735 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
736 if (inst->term->vt52_mode)
737 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
739 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
742 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
743 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
747 end = 1 + sprintf(output+1, "\x1B[%d~", code);
753 * Cursor keys. (This includes the numberpad cursor keys,
754 * if we haven't already done them due to app keypad mode.)
756 * Here we also process un-numlocked un-appkeypadded KP5,
757 * which sends ESC [ G.
761 switch (event->keyval) {
762 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
763 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
764 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
765 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
766 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
770 * The arrow keys normally do ESC [ A and so on. In
771 * app cursor keys mode they do ESC O A instead.
772 * Ctrl toggles the two modes.
774 if (inst->term->vt52_mode) {
775 end = 1 + sprintf(output+1, "\033%c", xkey);
776 } else if (!inst->term->app_cursor_keys ^
777 !(event->state & GDK_CONTROL_MASK)) {
778 end = 1 + sprintf(output+1, "\033O%c", xkey);
780 end = 1 + sprintf(output+1, "\033[%c", xkey);
793 printf("generating sequence:");
794 for (i = start; i < end; i++)
795 printf(" %02x", (unsigned char) output[i]);
799 ldisc_send(inst->ldisc, output+start, end-start, 1);
801 term_seen_key_event(inst->term);
802 term_out(inst->term);
808 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
810 struct gui_data *inst = (struct gui_data *)data;
811 int shift, ctrl, alt, x, y, button, act;
815 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
816 term_scroll(inst->term, 0, -5);
819 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
820 term_scroll(inst->term, 0, +5);
824 shift = event->state & GDK_SHIFT_MASK;
825 ctrl = event->state & GDK_CONTROL_MASK;
826 alt = event->state & GDK_MOD1_MASK;
827 if (event->button == 1)
829 else if (event->button == 2)
831 else if (event->button == 3)
834 return FALSE; /* don't even know what button! */
836 switch (event->type) {
837 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
838 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
839 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
840 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
841 default: return FALSE; /* don't know this event type */
844 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
845 act != MA_CLICK && act != MA_RELEASE)
846 return TRUE; /* we ignore these in raw mouse mode */
848 x = (event->x - cfg.window_border) / inst->font_width;
849 y = (event->y - cfg.window_border) / inst->font_height;
851 term_mouse(inst->term, button, act, x, y, shift, ctrl, alt);
856 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
858 struct gui_data *inst = (struct gui_data *)data;
859 int shift, ctrl, alt, x, y, button;
863 shift = event->state & GDK_SHIFT_MASK;
864 ctrl = event->state & GDK_CONTROL_MASK;
865 alt = event->state & GDK_MOD1_MASK;
866 if (event->state & GDK_BUTTON1_MASK)
868 else if (event->state & GDK_BUTTON2_MASK)
870 else if (event->state & GDK_BUTTON3_MASK)
873 return FALSE; /* don't even know what button! */
875 x = (event->x - cfg.window_border) / inst->font_width;
876 y = (event->y - cfg.window_border) / inst->font_height;
878 term_mouse(inst->term, button, MA_DRAG, x, y, shift, ctrl, alt);
883 void done_with_pty(struct gui_data *inst)
885 extern void pty_close(void);
887 if (inst->master_fd >= 0) {
889 inst->master_fd = -1;
890 gtk_input_remove(inst->master_func_id);
893 if (!inst->exited && inst->back->exitcode(inst->backhandle) >= 0) {
894 int exitcode = inst->back->exitcode(inst->backhandle);
897 clean = WIFEXITED(exitcode) && (WEXITSTATUS(exitcode) == 0);
900 * Terminate now, if the Close On Exit setting is
903 if (cfg.close_on_exit == COE_ALWAYS ||
904 (cfg.close_on_exit == COE_NORMAL && clean))
908 * Otherwise, output an indication that the session has
913 if (WIFEXITED(exitcode))
914 sprintf(message, "\r\n[pterm: process terminated with exit"
915 " code %d]\r\n", WEXITSTATUS(exitcode));
916 else if (WIFSIGNALED(exitcode))
917 #ifdef HAVE_NO_STRSIGNAL
918 sprintf(message, "\r\n[pterm: process terminated on signal"
919 " %d]\r\n", WTERMSIG(exitcode));
921 sprintf(message, "\r\n[pterm: process terminated on signal"
922 " %d (%.400s)]\r\n", WTERMSIG(exitcode),
923 strsignal(WTERMSIG(exitcode)));
925 from_backend((void *)inst->term, 0, message, strlen(message));
931 void frontend_keypress(void *handle)
933 struct gui_data *inst = (struct gui_data *)handle;
936 * If our child process has exited but not closed, terminate on
943 gint timer_func(gpointer data)
945 struct gui_data *inst = (struct gui_data *)data;
947 if (inst->back->exitcode(inst->backhandle) >= 0) {
949 * The primary child process died. We could keep the
950 * terminal open for remaining subprocesses to output to,
951 * but conventional wisdom seems to feel that that's the
952 * Wrong Thing for an xterm-alike, so we bail out now
953 * (though we don't necessarily _close_ the window,
954 * depending on the state of Close On Exit). This would be
955 * easy enough to change or make configurable if necessary.
960 term_update(inst->term);
961 term_blink(inst->term, 0);
965 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
967 struct gui_data *inst = (struct gui_data *)data;
971 ret = read(sourcefd, buf, sizeof(buf));
974 * Clean termination condition is that either ret == 0, or ret
975 * < 0 and errno == EIO. Not sure why the latter, but it seems
978 if (ret == 0 || (ret < 0 && errno == EIO)) {
980 } else if (ret < 0) {
981 perror("read pty master");
984 from_backend(inst->term, 0, buf, ret);
985 term_blink(inst->term, 1);
986 term_out(inst->term);
989 void destroy(GtkWidget *widget, gpointer data)
994 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
996 inst->term->has_focus = event->in;
997 term_out(inst->term);
998 term_update(inst->term);
1004 * set or clear the "raw mouse message" mode
1006 void set_raw_mouse_mode(int activate)
1008 activate = activate && !cfg.no_mouse_rep;
1009 send_raw_mouse = activate;
1011 inst->currcursor = inst->rawcursor;
1013 inst->currcursor = inst->textcursor;
1014 show_mouseptr(inst->mouseptr_visible);
1017 void request_resize(int w, int h)
1019 int large_x, large_y;
1020 int offset_x, offset_y;
1022 GtkRequisition inner, outer;
1025 * This is a heinous hack dreamed up by the gnome-terminal
1026 * people to get around a limitation in gtk. The problem is
1027 * that in order to set the size correctly we really need to be
1028 * calling gtk_window_resize - but that needs to know the size
1029 * of the _whole window_, not the drawing area. So what we do
1030 * is to set an artificially huge size request on the drawing
1031 * area, recompute the resulting size request on the window,
1032 * and look at the difference between the two. That gives us
1033 * the x and y offsets we need to translate drawing area size
1034 * into window size for real, and then we call
1035 * gtk_window_resize.
1039 * We start by retrieving the current size of the whole window.
1040 * Adding a bit to _that_ will give us a value we can use as a
1041 * bogus size request which guarantees to be bigger than the
1042 * current size of the drawing area.
1044 get_window_pixels(&large_x, &large_y);
1048 #if GTK_CHECK_VERSION(2,0,0)
1049 gtk_widget_set_size_request(inst->area, large_x, large_y);
1051 gtk_widget_set_usize(inst->area, large_x, large_y);
1053 gtk_widget_size_request(inst->area, &inner);
1054 gtk_widget_size_request(inst->window, &outer);
1056 offset_x = outer.width - inner.width;
1057 offset_y = outer.height - inner.height;
1059 area_x = inst->font_width * w + 2*cfg.window_border;
1060 area_y = inst->font_height * h + 2*cfg.window_border;
1063 * Now we must set the size request on the drawing area back to
1064 * something sensible before we commit the real resize. Best
1065 * way to do this, I think, is to set it to what the size is
1066 * really going to end up being.
1068 #if GTK_CHECK_VERSION(2,0,0)
1069 gtk_widget_set_size_request(inst->area, area_x, area_y);
1071 gtk_widget_set_usize(inst->area, area_x, area_y);
1074 #if GTK_CHECK_VERSION(2,0,0)
1075 gtk_window_resize(GTK_WINDOW(inst->window),
1076 area_x + offset_x, area_y + offset_y);
1078 gdk_window_resize(inst->window->window,
1079 area_x + offset_x, area_y + offset_y);
1083 void real_palette_set(int n, int r, int g, int b)
1085 gboolean success[1];
1087 inst->cols[n].red = r * 0x0101;
1088 inst->cols[n].green = g * 0x0101;
1089 inst->cols[n].blue = b * 0x0101;
1091 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1092 FALSE, FALSE, success);
1094 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1098 void set_window_background(void)
1100 if (inst->area && inst->area->window)
1101 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1102 if (inst->window && inst->window->window)
1103 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1106 void palette_set(int n, int r, int g, int b)
1108 static const int first[21] = {
1109 0, 2, 4, 6, 8, 10, 12, 14,
1110 1, 3, 5, 7, 9, 11, 13, 15,
1113 real_palette_set(first[n], r, g, b);
1115 real_palette_set(first[n] + 1, r, g, b);
1117 set_window_background();
1120 void palette_reset(void)
1122 /* This maps colour indices in cfg to those used in inst->cols. */
1123 static const int ww[] = {
1124 6, 7, 8, 9, 10, 11, 12, 13,
1125 14, 15, 16, 17, 18, 19, 20, 21,
1128 gboolean success[NCOLOURS];
1131 assert(lenof(ww) == NCOLOURS);
1133 if (!inst->colmap) {
1134 inst->colmap = gdk_colormap_get_system();
1136 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1139 for (i = 0; i < NCOLOURS; i++) {
1140 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1141 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1142 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1145 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1146 FALSE, FALSE, success);
1147 for (i = 0; i < NCOLOURS; i++) {
1149 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1150 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1153 set_window_background();
1156 void write_clip(wchar_t * data, int len, int must_deselect)
1158 if (inst->pasteout_data)
1159 sfree(inst->pasteout_data);
1160 inst->pasteout_data = smalloc(len);
1161 inst->pasteout_data_len = len;
1162 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1165 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1166 GDK_CURRENT_TIME)) {
1167 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1168 GDK_SELECTION_TYPE_STRING, 1);
1169 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1170 inst->compound_text_atom, 1);
1174 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1175 guint info, guint time_stamp, gpointer data)
1177 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1178 inst->pasteout_data, inst->pasteout_data_len);
1181 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1184 term_deselect(inst->term);
1185 if (inst->pasteout_data)
1186 sfree(inst->pasteout_data);
1187 inst->pasteout_data = NULL;
1188 inst->pasteout_data_len = 0;
1192 void request_paste(void)
1195 * In Unix, pasting is asynchronous: all we can do at the
1196 * moment is to call gtk_selection_convert(), and when the data
1197 * comes back _then_ we can call term_do_paste().
1199 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1200 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1203 gint idle_paste_func(gpointer data); /* forward ref */
1205 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1208 if (seldata->length <= 0 ||
1209 seldata->type != GDK_SELECTION_TYPE_STRING)
1210 return; /* Nothing happens. */
1212 if (inst->pastein_data)
1213 sfree(inst->pastein_data);
1215 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1216 inst->pastein_data_len = seldata->length;
1217 mb_to_wc(0, 0, seldata->data, seldata->length,
1218 inst->pastein_data, inst->pastein_data_len);
1220 term_do_paste(inst->term);
1222 if (term_paste_pending(inst->term))
1223 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1226 gint idle_paste_func(gpointer data)
1228 struct gui_data *inst = (struct gui_data *)data;
1230 if (term_paste_pending(inst->term))
1231 term_paste(inst->term);
1233 gtk_idle_remove(inst->term_paste_idle_id);
1239 void get_clip(wchar_t ** p, int *len)
1242 *p = inst->pastein_data;
1243 *len = inst->pastein_data_len;
1247 void set_title(char *title)
1249 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1250 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1251 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1254 void set_icon(char *title)
1256 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1257 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1258 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1261 void set_sbar(int total, int start, int page)
1265 inst->sbar_adjust->lower = 0;
1266 inst->sbar_adjust->upper = total;
1267 inst->sbar_adjust->value = start;
1268 inst->sbar_adjust->page_size = page;
1269 inst->sbar_adjust->step_increment = 1;
1270 inst->sbar_adjust->page_increment = page/2;
1271 inst->ignore_sbar = TRUE;
1272 gtk_adjustment_changed(inst->sbar_adjust);
1273 inst->ignore_sbar = FALSE;
1276 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1280 if (!inst->ignore_sbar)
1281 term_scroll(inst->term, 1, (int)adj->value);
1284 void sys_cursor(int x, int y)
1287 * This is meaningless under X.
1296 int CharWidth(Context ctx, int uc)
1299 * Under X, any fixed-width font really _is_ fixed-width.
1300 * Double-width characters will be dealt with using a separate
1301 * font. For the moment we can simply return 1.
1306 Context get_ctx(void)
1309 if (!inst->area->window)
1311 gc = gdk_gc_new(inst->area->window);
1315 void free_ctx(Context ctx)
1317 GdkGC *gc = (GdkGC *)ctx;
1322 * Draw a line of text in the window, at given character
1323 * coordinates, in given attributes.
1325 * We are allowed to fiddle with the contents of `text'.
1327 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1328 unsigned long attr, int lattr)
1330 int nfg, nbg, t, fontid, shadow;
1331 GdkGC *gc = (GdkGC *)ctx;
1335 * - Unicode, code pages, and ATTR_WIDE for CJK support.
1338 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1339 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1340 if (attr & ATTR_REVERSE) {
1345 if (cfg.bold_colour && (attr & ATTR_BOLD))
1347 if (cfg.bold_colour && (attr & ATTR_BLINK))
1349 if (attr & TATTR_ACTCURS) {
1354 fontid = shadow = 0;
1355 if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1362 if (lattr != LATTR_NORM) {
1364 if (x >= inst->term->cols)
1366 if (x + len*2 > inst->term->cols)
1367 len = (inst->term->cols-x)/2; /* trim to LH half */
1370 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1371 gdk_draw_rectangle(inst->pixmap, gc, 1,
1372 x*inst->font_width+cfg.window_border,
1373 y*inst->font_height+cfg.window_border,
1374 len*inst->font_width, inst->font_height);
1376 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1377 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1378 x*inst->font_width+cfg.window_border,
1379 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1383 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1384 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1385 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1389 if (attr & ATTR_UNDER) {
1390 int uheight = inst->fonts[0]->ascent + 1;
1391 if (uheight >= inst->font_height)
1392 uheight = inst->font_height - 1;
1393 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1394 y*inst->font_height + uheight + cfg.window_border,
1395 (x+len)*inst->font_width-1+cfg.window_border,
1396 y*inst->font_height + uheight + cfg.window_border);
1399 if (lattr != LATTR_NORM) {
1401 * I can't find any plausible StretchBlt equivalent in the
1402 * X server, so I'm going to do this the slow and painful
1403 * way. This will involve repeated calls to
1404 * gdk_draw_pixmap() to stretch the text horizontally. It's
1405 * O(N^2) in time and O(N) in network bandwidth, but you
1406 * try thinking of a better way. :-(
1409 for (i = 0; i < len * inst->font_width; i++) {
1410 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1411 x*inst->font_width+cfg.window_border + 2*i,
1412 y*inst->font_height+cfg.window_border,
1413 x*inst->font_width+cfg.window_border + 2*i+1,
1414 y*inst->font_height+cfg.window_border,
1415 len * inst->font_width - i, inst->font_height);
1418 if (lattr != LATTR_WIDE) {
1420 /* Now stretch vertically, in the same way. */
1421 if (lattr == LATTR_BOT)
1425 for (i = 0; i < inst->font_height; i+=2) {
1426 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1427 x*inst->font_width+cfg.window_border,
1428 y*inst->font_height+cfg.window_border+dt*i+db,
1429 x*inst->font_width+cfg.window_border,
1430 y*inst->font_height+cfg.window_border+dt*(i+1),
1431 len * inst->font_width, inst->font_height-i-1);
1437 void do_text(Context ctx, int x, int y, char *text, int len,
1438 unsigned long attr, int lattr)
1440 GdkGC *gc = (GdkGC *)ctx;
1442 do_text_internal(ctx, x, y, text, len, attr, lattr);
1444 if (lattr != LATTR_NORM) {
1446 if (x >= inst->term->cols)
1448 if (x + len*2 > inst->term->cols)
1449 len = (inst->term->cols-x)/2; /* trim to LH half */
1453 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1454 x*inst->font_width+cfg.window_border,
1455 y*inst->font_height+cfg.window_border,
1456 x*inst->font_width+cfg.window_border,
1457 y*inst->font_height+cfg.window_border,
1458 len*inst->font_width, inst->font_height);
1461 void do_cursor(Context ctx, int x, int y, char *text, int len,
1462 unsigned long attr, int lattr)
1465 GdkGC *gc = (GdkGC *)ctx;
1467 if (attr & TATTR_PASCURS) {
1468 attr &= ~TATTR_PASCURS;
1472 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1473 attr &= ~TATTR_ACTCURS;
1475 do_text_internal(ctx, x, y, text, len, attr, lattr);
1477 if (lattr != LATTR_NORM) {
1479 if (x >= inst->term->cols)
1481 if (x + len*2 > inst->term->cols)
1482 len = (inst->term->cols-x)/2; /* trim to LH half */
1486 if (cfg.cursor_type == 0) {
1488 * An active block cursor will already have been done by
1489 * the above do_text call, so we only need to do anything
1493 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1494 gdk_draw_rectangle(inst->pixmap, gc, 0,
1495 x*inst->font_width+cfg.window_border,
1496 y*inst->font_height+cfg.window_border,
1497 len*inst->font_width-1, inst->font_height-1);
1501 int startx, starty, dx, dy, length, i;
1505 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1506 char_width = 2*inst->font_width;
1508 char_width = inst->font_width;
1510 if (cfg.cursor_type == 1) {
1511 uheight = inst->fonts[0]->ascent + 1;
1512 if (uheight >= inst->font_height)
1513 uheight = inst->font_height - 1;
1515 startx = x * inst->font_width + cfg.window_border;
1516 starty = y * inst->font_height + cfg.window_border + uheight;
1519 length = len * char_width;
1522 if (attr & TATTR_RIGHTCURS)
1523 xadjust = char_width - 1;
1524 startx = x * inst->font_width + cfg.window_border + xadjust;
1525 starty = y * inst->font_height + cfg.window_border;
1528 length = inst->font_height;
1531 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1533 for (i = 0; i < length; i++) {
1535 gdk_draw_point(inst->pixmap, gc, startx, starty);
1541 gdk_draw_line(inst->pixmap, gc, startx, starty,
1542 startx + (length-1) * dx, starty + (length-1) * dy);
1546 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1547 x*inst->font_width+cfg.window_border,
1548 y*inst->font_height+cfg.window_border,
1549 x*inst->font_width+cfg.window_border,
1550 y*inst->font_height+cfg.window_border,
1551 len*inst->font_width, inst->font_height);
1554 GdkCursor *make_mouse_ptr(int cursor_val)
1557 * Truly hideous hack: GTK doesn't allow us to set the mouse
1558 * cursor foreground and background colours unless we've _also_
1559 * created our own cursor from bitmaps. Therefore, I need to
1560 * load the `cursor' font and draw glyphs from it on to
1561 * pixmaps, in order to construct my cursors with the fg and bg
1562 * I want. This is a gross hack, but it's more self-contained
1563 * than linking in Xlib to find the X window handle to
1564 * inst->area and calling XRecolorCursor, and it's more
1565 * futureproof than hard-coding the shapes as bitmap arrays.
1567 static GdkFont *cursor_font = NULL;
1568 GdkPixmap *source, *mask;
1570 GdkColor cfg = { 0, 65535, 65535, 65535 };
1571 GdkColor cbg = { 0, 0, 0, 0 };
1572 GdkColor dfg = { 1, 65535, 65535, 65535 };
1573 GdkColor dbg = { 0, 0, 0, 0 };
1576 gint lb, rb, wid, asc, desc, w, h, x, y;
1578 if (cursor_val == -2) {
1579 gdk_font_unref(cursor_font);
1583 if (cursor_val >= 0 && !cursor_font)
1584 cursor_font = gdk_font_load("cursor");
1587 * Get the text extent of the cursor in question. We use the
1588 * mask character for this, because it's typically slightly
1589 * bigger than the main character.
1591 if (cursor_val >= 0) {
1593 text[0] = (char)cursor_val + 1;
1594 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1595 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1601 source = gdk_pixmap_new(NULL, w, h, 1);
1602 mask = gdk_pixmap_new(NULL, w, h, 1);
1605 * Draw the mask character on the mask pixmap.
1607 gc = gdk_gc_new(mask);
1608 gdk_gc_set_foreground(gc, &dbg);
1609 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1610 if (cursor_val >= 0) {
1612 text[0] = (char)cursor_val + 1;
1613 gdk_gc_set_foreground(gc, &dfg);
1614 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1619 * Draw the main character on the source pixmap.
1621 gc = gdk_gc_new(source);
1622 gdk_gc_set_foreground(gc, &dbg);
1623 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1624 if (cursor_val >= 0) {
1626 text[0] = (char)cursor_val;
1627 gdk_gc_set_foreground(gc, &dfg);
1628 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1633 * Create the cursor.
1635 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1640 gdk_pixmap_unref(source);
1641 gdk_pixmap_unref(mask);
1646 void modalfatalbox(char *p, ...)
1649 fprintf(stderr, "FATAL ERROR: ");
1651 vfprintf(stderr, p, ap);
1653 fputc('\n', stderr);
1657 char *get_x_display(void)
1659 return gdk_get_display();
1662 char *app_name = "pterm";
1664 int do_cmdline(int argc, char **argv, int do_everything)
1667 extern char **pty_argv; /* declared in pty.c */
1670 * Macros to make argument handling easier.
1672 #define EXPECTS_ARG do { \
1673 if (--argc <= 0) { \
1675 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1679 #define SECOND_PASS_ONLY do { \
1680 if (!do_everything) continue; \
1690 while (--argc > 0) {
1692 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1695 strncpy(cfg.font, val, sizeof(cfg.font));
1696 cfg.font[sizeof(cfg.font)-1] = '\0';
1698 } else if (!strcmp(p, "-fb")) {
1701 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1702 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1704 } else if (!strcmp(p, "-geometry")) {
1705 int flags, x, y, w, h;
1709 flags = XParseGeometry(val, &x, &y, &w, &h);
1710 if (flags & WidthValue)
1712 if (flags & HeightValue)
1716 * Apparently setting the initial window position is
1717 * difficult in GTK 1.2. Not entirely sure why this
1718 * should be. 2.0 has gtk_window_parse_geometry(),
1719 * which would help... For the moment, though, I can't
1720 * be bothered with this.
1723 } else if (!strcmp(p, "-sl")) {
1726 cfg.savelines = atoi(val);
1728 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1729 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1730 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1735 if (!gdk_color_parse(val, &col)) {
1737 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1740 index = (!strcmp(p, "-fg") ? 0 :
1741 !strcmp(p, "-bg") ? 2 :
1742 !strcmp(p, "-bfg") ? 1 :
1743 !strcmp(p, "-bbg") ? 3 :
1744 !strcmp(p, "-cfg") ? 4 :
1745 !strcmp(p, "-cbg") ? 5 : -1);
1746 assert(index != -1);
1747 cfg.colours[index][0] = col.red / 256;
1748 cfg.colours[index][1] = col.green / 256;
1749 cfg.colours[index][2] = col.blue / 256;
1752 } else if (!strcmp(p, "-e")) {
1753 /* This option swallows all further arguments. */
1759 pty_argv = smalloc((argc+1) * sizeof(char *));
1761 for (i = 0; i < argc; i++)
1762 pty_argv[i] = argv[i];
1763 pty_argv[argc] = NULL;
1764 break; /* finished command-line processing */
1766 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1768 } else if (!strcmp(p, "-T")) {
1771 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1772 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1774 } else if (!strcmp(p, "-log")) {
1777 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1778 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1779 cfg.logtype = LGTYP_DEBUG;
1781 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1785 } else if (!strcmp(p, "-ut")) {
1789 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1791 cfg.login_shell = 0;
1793 } else if (!strcmp(p, "-ls")) {
1795 cfg.login_shell = 1;
1797 } else if (!strcmp(p, "-nethack")) {
1799 cfg.nethack_keypad = 1;
1801 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1805 } else if (!strcmp(p, "-sb")) {
1809 } else if (!strcmp(p, "-name")) {
1813 } else if (!strcmp(p, "-xrm")) {
1815 provide_xrm_string(val);
1819 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
1826 int main(int argc, char **argv)
1828 extern int pty_master_fd; /* declared in pty.c */
1829 extern void pty_pre_init(void); /* declared in pty.c */
1833 gtk_init(&argc, &argv);
1835 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
1837 do_defaults(NULL, &cfg);
1838 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
1842 * Initialise the whole instance structure to zeroes
1844 memset(inst, 0, sizeof(*inst));
1846 inst->fonts[0] = gdk_font_load(cfg.font);
1847 if (!inst->fonts[0]) {
1848 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1851 if (cfg.boldfont[0]) {
1852 inst->fonts[1] = gdk_font_load(cfg.boldfont);
1853 if (!inst->fonts[1]) {
1854 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1859 inst->fonts[1] = NULL;
1861 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1862 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1864 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1868 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1870 if (cfg.wintitle[0])
1871 set_title(cfg.wintitle);
1876 * Set up the colour map.
1880 inst->area = gtk_drawing_area_new();
1881 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1882 inst->font_width * cfg.width + 2*cfg.window_border,
1883 inst->font_height * cfg.height + 2*cfg.window_border);
1884 if (cfg.scrollbar) {
1885 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1886 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1888 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1889 if (cfg.scrollbar) {
1890 if (cfg.scrollbar_on_left)
1891 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1893 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1895 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1897 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1901 geom.min_width = inst->font_width + 2*cfg.window_border;
1902 geom.min_height = inst->font_height + 2*cfg.window_border;
1903 geom.max_width = geom.max_height = -1;
1904 geom.base_width = 2*cfg.window_border;
1905 geom.base_height = 2*cfg.window_border;
1906 geom.width_inc = inst->font_width;
1907 geom.height_inc = inst->font_height;
1908 geom.min_aspect = geom.max_aspect = 0;
1909 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1910 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1911 GDK_HINT_RESIZE_INC);
1914 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1915 GTK_SIGNAL_FUNC(destroy), inst);
1916 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1917 GTK_SIGNAL_FUNC(delete_window), inst);
1918 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1919 GTK_SIGNAL_FUNC(key_event), inst);
1920 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
1921 GTK_SIGNAL_FUNC(key_event), inst);
1922 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1923 GTK_SIGNAL_FUNC(focus_event), inst);
1924 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1925 GTK_SIGNAL_FUNC(focus_event), inst);
1926 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1927 GTK_SIGNAL_FUNC(configure_area), inst);
1928 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1929 GTK_SIGNAL_FUNC(expose_area), inst);
1930 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1931 GTK_SIGNAL_FUNC(button_event), inst);
1932 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1933 GTK_SIGNAL_FUNC(button_event), inst);
1934 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1935 GTK_SIGNAL_FUNC(motion_event), inst);
1936 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1937 GTK_SIGNAL_FUNC(selection_received), inst);
1938 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1939 GTK_SIGNAL_FUNC(selection_get), inst);
1940 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1941 GTK_SIGNAL_FUNC(selection_clear), inst);
1943 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1944 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
1945 gtk_timeout_add(20, timer_func, inst);
1946 gtk_widget_add_events(GTK_WIDGET(inst->area),
1947 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1948 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1949 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
1951 gtk_widget_show(inst->area);
1953 gtk_widget_show(inst->sbar);
1954 gtk_widget_show(GTK_WIDGET(inst->hbox));
1955 gtk_widget_show(inst->window);
1957 set_window_background();
1959 inst->textcursor = make_mouse_ptr(GDK_XTERM);
1960 inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
1961 inst->blankcursor = make_mouse_ptr(-1);
1962 make_mouse_ptr(-2); /* clean up cursor font */
1963 inst->currcursor = inst->textcursor;
1966 inst->term = term_init();
1968 inst->back = &pty_backend;
1969 inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
1971 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
1973 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
1975 inst->ldisc = ldisc_create(inst->term, inst->back, inst->backhandle, inst);
1976 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
1978 inst->master_fd = pty_master_fd;
1979 inst->exited = FALSE;
1980 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
1981 pty_input_func, inst);