]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/pterm.c
ebdc5a8eee39e0e7276952752e21a624fdf68db2
[PuTTY.git] / unix / pterm.c
1 /*
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.
4  */
5
6 #include <string.h>
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <time.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <X11/Xlib.h>
15 #include <X11/Xutil.h>
16 #include <gtk/gtk.h>
17 #include <gdk/gdkkeysyms.h>
18
19 #define PUTTY_DO_GLOBALS               /* actually _define_ globals */
20 #include "putty.h"
21
22 #define CAT2(x,y) x ## y
23 #define CAT(x,y) CAT2(x,y)
24 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
25
26 #define NCOLOURS (lenof(((Config *)0)->colours))
27
28 struct gui_data {
29     GtkWidget *window, *area, *sbar;
30     GtkBox *hbox;
31     GtkAdjustment *sbar_adjust;
32     GdkPixmap *pixmap;
33     GdkFont *fonts[2];                 /* normal and bold (for now!) */
34     GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
35     GdkColor cols[NCOLOURS];
36     GdkColormap *colmap;
37     wchar_t *pastein_data;
38     int pastein_data_len;
39     char *pasteout_data;
40     int pasteout_data_len;
41     int font_width, font_height;
42     int ignore_sbar;
43     int mouseptr_visible;
44     guint term_paste_idle_id;
45     GdkAtom compound_text_atom;
46     int alt_keycode;
47     char wintitle[sizeof(((Config *)0)->wintitle)];
48     char icontitle[sizeof(((Config *)0)->wintitle)];
49 };
50
51 static struct gui_data the_inst;
52 static struct gui_data *inst = &the_inst;   /* so we always write `inst->' */
53 static int send_raw_mouse;
54
55 void ldisc_update(int echo, int edit)
56 {
57     /*
58      * This is a stub in pterm. If I ever produce a Unix
59      * command-line ssh/telnet/rlogin client (i.e. a port of plink)
60      * then it will require some termios manoeuvring analogous to
61      * that in the Windows plink.c, but here it's meaningless.
62      */
63 }
64
65 int askappend(char *filename)
66 {
67     /*
68      * Logging in an xterm-alike is liable to be something you only
69      * do at serious diagnostic need. Hence, I'm going to take the
70      * easy option for now and assume we always want to overwrite
71      * log files. I can always make it properly configurable later.
72      */
73     return 2;
74 }
75
76 void logevent(char *string)
77 {
78     /*
79      * This is not a very helpful function: events are logged
80      * pretty much exclusively by the back end, and our pty back
81      * end is self-contained. So we need do nothing.
82      */
83 }
84
85 int font_dimension(int which)          /* 0 for width, 1 for height */
86 {
87     if (which)
88         return inst->font_height;
89     else
90         return inst->font_width;
91 }
92
93 /*
94  * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
95  * into a cooked one (SELECT, EXTEND, PASTE).
96  * 
97  * In Unix, this is not configurable; the X button arrangement is
98  * rock-solid across all applications, everyone has a three-button
99  * mouse or a means of faking it, and there is no need to switch
100  * buttons around at all.
101  */
102 Mouse_Button translate_button(Mouse_Button button)
103 {
104     if (button == MBT_LEFT)
105         return MBT_SELECT;
106     if (button == MBT_MIDDLE)
107         return MBT_PASTE;
108     if (button == MBT_RIGHT)
109         return MBT_EXTEND;
110     return 0;                          /* shouldn't happen */
111 }
112
113 /*
114  * Minimise or restore the window in response to a server-side
115  * request.
116  */
117 void set_iconic(int iconic)
118 {
119     /*
120      * GTK 1.2 doesn't know how to do this.
121      */
122 #if GTK_CHECK_VERSION(2,0,0)
123     if (iconic)
124         gtk_window_iconify(GTK_WINDOW(inst->window));
125     else
126         gtk_window_deiconify(GTK_WINDOW(inst->window));
127 #endif
128 }
129
130 /*
131  * Move the window in response to a server-side request.
132  */
133 void move_window(int x, int y)
134 {
135     /*
136      * I assume that when the GTK version of this call is available
137      * we should use it. Not sure how it differs from the GDK one,
138      * though.
139      */
140 #if GTK_CHECK_VERSION(2,0,0)
141     gtk_window_move(GTK_WINDOW(inst->window), x, y);
142 #else
143     gdk_window_move(inst->window->window, x, y);
144 #endif
145 }
146
147 /*
148  * Move the window to the top or bottom of the z-order in response
149  * to a server-side request.
150  */
151 void set_zorder(int top)
152 {
153     if (top)
154         gdk_window_raise(inst->window->window);
155     else
156         gdk_window_lower(inst->window->window);
157 }
158
159 /*
160  * Refresh the window in response to a server-side request.
161  */
162 void refresh_window(void)
163 {
164     term_invalidate();
165 }
166
167 /*
168  * Maximise or restore the window in response to a server-side
169  * request.
170  */
171 void set_zoomed(int zoomed)
172 {
173     /*
174      * GTK 1.2 doesn't know how to do this.
175      */
176 #if GTK_CHECK_VERSION(2,0,0)
177     if (iconic)
178         gtk_window_maximize(GTK_WINDOW(inst->window));
179     else
180         gtk_window_unmaximize(GTK_WINDOW(inst->window));
181 #endif
182 }
183
184 /*
185  * Report whether the window is iconic, for terminal reports.
186  */
187 int is_iconic(void)
188 {
189     return !gdk_window_is_viewable(inst->window->window);
190 }
191
192 /*
193  * Report the window's position, for terminal reports.
194  */
195 void get_window_pos(int *x, int *y)
196 {
197     /*
198      * I assume that when the GTK version of this call is available
199      * we should use it. Not sure how it differs from the GDK one,
200      * though.
201      */
202 #if GTK_CHECK_VERSION(2,0,0)
203     gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
204 #else
205     gdk_window_get_position(inst->window->window, x, y);
206 #endif
207 }
208
209 /*
210  * Report the window's pixel size, for terminal reports.
211  */
212 void get_window_pixels(int *x, int *y)
213 {
214     /*
215      * I assume that when the GTK version of this call is available
216      * we should use it. Not sure how it differs from the GDK one,
217      * though.
218      */
219 #if GTK_CHECK_VERSION(2,0,0)
220     gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
221 #else
222     gdk_window_get_size(inst->window->window, x, y);
223 #endif
224 }
225
226 /*
227  * Return the window or icon title.
228  */
229 char *get_window_title(int icon)
230 {
231     return icon ? inst->wintitle : inst->icontitle;
232 }
233
234 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
235 {
236     /*
237      * We could implement warn-on-close here if we really wanted
238      * to.
239      */
240     return FALSE;
241 }
242
243 void show_mouseptr(int show)
244 {
245     if (!cfg.hide_mouseptr)
246         show = 1;
247     if (show)
248         gdk_window_set_cursor(inst->area->window, inst->currcursor);
249     else
250         gdk_window_set_cursor(inst->area->window, inst->blankcursor);
251     inst->mouseptr_visible = show;
252 }
253
254 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
255 {
256     struct gui_data *inst = (struct gui_data *)data;
257     int w, h, need_size = 0;
258
259     w = (event->width - 2*cfg.window_border) / inst->font_width;
260     h = (event->height - 2*cfg.window_border) / inst->font_height;
261
262     if (w != cfg.width || h != cfg.height) {
263         if (inst->pixmap) {
264             gdk_pixmap_unref(inst->pixmap);
265             inst->pixmap = NULL;
266         }
267         cfg.width = w;
268         cfg.height = h;
269         need_size = 1;
270     }
271     if (!inst->pixmap) {
272         GdkGC *gc;
273
274         inst->pixmap = gdk_pixmap_new(widget->window,
275                                       (cfg.width * inst->font_width +
276                                        2*cfg.window_border),
277                                       (cfg.height * inst->font_height +
278                                        2*cfg.window_border), -1);
279
280         gc = gdk_gc_new(inst->area->window);
281         gdk_gc_set_foreground(gc, &inst->cols[18]);   /* default background */
282         gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
283                            cfg.width * inst->font_width + 2*cfg.window_border,
284                            cfg.height * inst->font_height + 2*cfg.window_border);
285         gdk_gc_unref(gc);
286     }
287
288     if (need_size) {
289         term_size(h, w, cfg.savelines);
290     }
291
292     return TRUE;
293 }
294
295 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
296 {
297     /* struct gui_data *inst = (struct gui_data *)data; */
298
299     /*
300      * Pass the exposed rectangle to terminal.c, which will call us
301      * back to do the actual painting.
302      */
303     if (inst->pixmap) {
304         gdk_draw_pixmap(widget->window,
305                         widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
306                         inst->pixmap,
307                         event->area.x, event->area.y,
308                         event->area.x, event->area.y,
309                         event->area.width, event->area.height);
310     }
311     return TRUE;
312 }
313
314 #define KEY_PRESSED(k) \
315     (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
316
317 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
318 {
319     /* struct gui_data *inst = (struct gui_data *)data; */
320     char output[32];
321     int start, end;
322
323     /* By default, nothing is generated. */
324     end = start = 0;
325
326     /*
327      * If Alt is being released after typing an Alt+numberpad
328      * sequence, we should generate the code that was typed.
329      */
330     if (event->type == GDK_KEY_RELEASE &&
331         (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
332          event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
333         inst->alt_keycode >= 0) {
334 #ifdef KEY_DEBUGGING
335         printf("Alt key up, keycode = %d\n", inst->alt_keycode);
336 #endif
337         output[0] = inst->alt_keycode;
338         end = 1;
339         goto done;
340     }
341
342     if (event->type == GDK_KEY_PRESS) {
343 #ifdef KEY_DEBUGGING
344         {
345             int i;
346             printf("keypress: keyval = %04x, state = %08x; string =",
347                    event->keyval, event->state);
348             for (i = 0; event->string[i]; i++)
349                 printf(" %02x", (unsigned char) event->string[i]);
350             printf("\n");
351         }
352 #endif
353
354         /*
355          * NYI: Compose key (!!! requires Unicode faff before even trying)
356          */
357
358         /*
359          * If Alt has just been pressed, we start potentially
360          * accumulating an Alt+numberpad code. We do this by
361          * setting alt_keycode to -1 (nothing yet but plausible).
362          */
363         if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
364              event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
365             inst->alt_keycode = -1;
366             goto done;                 /* this generates nothing else */
367         }
368
369         /*
370          * If we're seeing a numberpad key press with Mod1 down,
371          * consider adding it to alt_keycode if that's sensible.
372          * Anything _else_ with Mod1 down cancels any possibility
373          * of an ALT keycode: we set alt_keycode to -2.
374          */
375         if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
376             int digit = -1;
377             switch (event->keyval) {
378               case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
379               case GDK_KP_1: case GDK_KP_End: digit = 1; break;
380               case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
381               case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
382               case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
383               case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
384               case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
385               case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
386               case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
387               case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
388             }
389             if (digit < 0)
390                 inst->alt_keycode = -2;   /* it's invalid */
391             else {
392 #ifdef KEY_DEBUGGING
393                 printf("Adding digit %d to keycode %d", digit,
394                        inst->alt_keycode);
395 #endif
396                 if (inst->alt_keycode == -1)
397                     inst->alt_keycode = digit;   /* one-digit code */
398                 else
399                     inst->alt_keycode = inst->alt_keycode * 10 + digit;
400 #ifdef KEY_DEBUGGING
401                 printf(" gives new code %d\n", inst->alt_keycode);
402 #endif
403                 /* Having used this digit, we now do nothing more with it. */
404                 goto done;
405             }
406         }
407
408         /*
409          * Shift-PgUp and Shift-PgDn don't even generate keystrokes
410          * at all.
411          */
412         if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
413             term_scroll(0, -cfg.height/2);
414             return TRUE;
415         }
416         if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
417             term_scroll(0, +cfg.height/2);
418             return TRUE;
419         }
420
421         /*
422          * Neither does Shift-Ins.
423          */
424         if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
425             request_paste();
426             return TRUE;
427         }
428
429         /* ALT+things gives leading Escape. */
430         output[0] = '\033';
431         strncpy(output+1, event->string, 31);
432         output[31] = '\0';
433         end = strlen(output);
434         if (event->state & GDK_MOD1_MASK)
435             start = end = 0;
436         else
437             start = end = 1;
438
439         /* Control-` is the same as Control-\ (unless gtk has a better idea) */
440         if (!event->string[0] && event->keyval == '`' &&
441             (event->state & GDK_CONTROL_MASK)) {
442             output[1] = '\x1C';
443             end = 2;
444         }
445
446         /* Control-Break is the same as Control-C */
447         if (event->keyval == GDK_Break &&
448             (event->state & GDK_CONTROL_MASK)) {
449             output[1] = '\003';
450             end = 2;
451         }
452
453         /* Control-2, Control-Space and Control-@ are NUL */
454         if (!event->string[0] &&
455             (event->keyval == ' ' || event->keyval == '2' ||
456              event->keyval == '@') &&
457             (event->state & (GDK_SHIFT_MASK |
458                              GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
459             output[1] = '\0';
460             end = 2;
461         }
462
463         /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
464         if (!event->string[0] && event->keyval == ' ' &&
465             (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
466             (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
467             output[1] = '\240';
468             end = 2;
469         }
470
471         /* We don't let GTK tell us what Backspace is! We know better. */
472         if (event->keyval == GDK_BackSpace &&
473             !(event->state & GDK_SHIFT_MASK)) {
474             output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
475             end = 2;
476         }
477         /* For Shift Backspace, do opposite of what is configured. */
478         if (event->keyval == GDK_BackSpace &&
479             (event->state & GDK_SHIFT_MASK)) {
480             output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
481             end = 2;
482         }
483
484         /* Shift-Tab is ESC [ Z */
485         if (event->keyval == GDK_ISO_Left_Tab ||
486             (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
487             end = 1 + sprintf(output+1, "\033[Z");
488         }
489
490         /*
491          * NetHack keypad mode.
492          */
493         if (cfg.nethack_keypad) {
494             char *keys = NULL;
495             switch (event->keyval) {
496               case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
497               case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
498               case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
499               case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
500               case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
501               case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
502               case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
503               case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
504               case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
505             }
506             if (keys) {
507                 end = 2;
508                 if (event->state & GDK_SHIFT_MASK)
509                     output[1] = keys[1];
510                 else
511                     output[1] = keys[0];
512                 goto done;
513             }
514         }
515
516         /*
517          * Application keypad mode.
518          */
519         if (app_keypad_keys && !cfg.no_applic_k) {
520             int xkey = 0;
521             switch (event->keyval) {
522               case GDK_Num_Lock: xkey = 'P'; break;
523               case GDK_KP_Divide: xkey = 'Q'; break;
524               case GDK_KP_Multiply: xkey = 'R'; break;
525               case GDK_KP_Subtract: xkey = 'S'; break;
526                 /*
527                  * Keypad + is tricky. It covers a space that would
528                  * be taken up on the VT100 by _two_ keys; so we
529                  * let Shift select between the two. Worse still,
530                  * in xterm function key mode we change which two...
531                  */
532               case GDK_KP_Add:
533                 if (cfg.funky_type == 2) {
534                     if (event->state & GDK_SHIFT_MASK)
535                         xkey = 'l';
536                     else
537                         xkey = 'k';
538                 } else if (event->state & GDK_SHIFT_MASK)
539                         xkey = 'm';
540                 else
541                     xkey = 'l';
542                 break;
543               case GDK_KP_Enter: xkey = 'M'; break;
544               case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
545               case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
546               case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
547               case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
548               case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
549               case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
550               case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
551               case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
552               case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
553               case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
554               case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
555             }
556             if (xkey) {
557                 if (vt52_mode) {
558                     if (xkey >= 'P' && xkey <= 'S')
559                         end = 1 + sprintf(output+1, "\033%c", xkey);
560                     else
561                         end = 1 + sprintf(output+1, "\033?%c", xkey);
562                 } else
563                     end = 1 + sprintf(output+1, "\033O%c", xkey);
564                 goto done;
565             }
566         }
567
568         /*
569          * Next, all the keys that do tilde codes. (ESC '[' nn '~',
570          * for integer decimal nn.)
571          *
572          * We also deal with the weird ones here. Linux VCs replace F1
573          * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
574          * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
575          * respectively.
576          */
577         {
578             int code = 0;
579             switch (event->keyval) {
580               case GDK_F1:
581                 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
582                 break;
583               case GDK_F2:
584                 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
585                 break;
586               case GDK_F3:
587                 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
588                 break;
589               case GDK_F4:
590                 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
591                 break;
592               case GDK_F5:
593                 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
594                 break;
595               case GDK_F6:
596                 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
597                 break;
598               case GDK_F7:
599                 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
600                 break;
601               case GDK_F8:
602                 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
603                 break;
604               case GDK_F9:
605                 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
606                 break;
607               case GDK_F10:
608                 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
609                 break;
610               case GDK_F11:
611                 code = 23;
612                 break;
613               case GDK_F12:
614                 code = 24;
615                 break;
616               case GDK_F13:
617                 code = 25;
618                 break;
619               case GDK_F14:
620                 code = 26;
621                 break;
622               case GDK_F15:
623                 code = 28;
624                 break;
625               case GDK_F16:
626                 code = 29;
627                 break;
628               case GDK_F17:
629                 code = 31;
630                 break;
631               case GDK_F18:
632                 code = 32;
633                 break;
634               case GDK_F19:
635                 code = 33;
636                 break;
637               case GDK_F20:
638                 code = 34;
639                 break;
640             }
641             if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
642               case GDK_Home: case GDK_KP_Home:
643                 code = 1;
644                 break;
645               case GDK_Insert: case GDK_KP_Insert:
646                 code = 2;
647                 break;
648               case GDK_Delete: case GDK_KP_Delete:
649                 code = 3;
650                 break;
651               case GDK_End: case GDK_KP_End:
652                 code = 4;
653                 break;
654               case GDK_Page_Up: case GDK_KP_Page_Up:
655                 code = 5;
656                 break;
657               case GDK_Page_Down: case GDK_KP_Page_Down:
658                 code = 6;
659                 break;
660             }
661             /* Reorder edit keys to physical order */
662             if (cfg.funky_type == 3 && code <= 6)
663                 code = "\0\2\1\4\5\3\6"[code];
664
665             if (vt52_mode && code > 0 && code <= 6) {
666                 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
667                 goto done;
668             }
669
670             if (cfg.funky_type == 5 &&     /* SCO function keys */
671                 code >= 11 && code <= 34) {
672                 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
673                 int index = 0;
674                 switch (event->keyval) {
675                   case GDK_F1: index = 0; break;
676                   case GDK_F2: index = 1; break;
677                   case GDK_F3: index = 2; break;
678                   case GDK_F4: index = 3; break;
679                   case GDK_F5: index = 4; break;
680                   case GDK_F6: index = 5; break;
681                   case GDK_F7: index = 6; break;
682                   case GDK_F8: index = 7; break;
683                   case GDK_F9: index = 8; break;
684                   case GDK_F10: index = 9; break;
685                   case GDK_F11: index = 10; break;
686                   case GDK_F12: index = 11; break;
687                 }
688                 if (event->state & GDK_SHIFT_MASK) index += 12;
689                 if (event->state & GDK_CONTROL_MASK) index += 24;
690                 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
691                 goto done;
692             }
693             if (cfg.funky_type == 5 &&     /* SCO small keypad */
694                 code >= 1 && code <= 6) {
695                 char codes[] = "HL.FIG";
696                 if (code == 3) {
697                     output[1] = '\x7F';
698                     end = 2;
699                 } else {
700                     end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
701                 }
702                 goto done;
703             }
704             if ((vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
705                 int offt = 0;
706                 if (code > 15)
707                     offt++;
708                 if (code > 21)
709                     offt++;
710                 if (vt52_mode)
711                     end = 1 + sprintf(output+1,
712                                       "\x1B%c", code + 'P' - 11 - offt);
713                 else
714                     end = 1 + sprintf(output+1,
715                                       "\x1BO%c", code + 'P' - 11 - offt);
716                 goto done;
717             }
718             if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
719                 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
720                 goto done;
721             }
722             if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
723                 if (vt52_mode)
724                     end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
725                 else
726                     end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
727                 goto done;
728             }
729             if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
730                 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
731                 goto done;
732             }
733             if (code) {
734                 end = 1 + sprintf(output+1, "\x1B[%d~", code);
735                 goto done;
736             }
737         }
738
739         /*
740          * Cursor keys. (This includes the numberpad cursor keys,
741          * if we haven't already done them due to app keypad mode.)
742          * 
743          * Here we also process un-numlocked un-appkeypadded KP5,
744          * which sends ESC [ G.
745          */
746         {
747             int xkey = 0;
748             switch (event->keyval) {
749               case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
750               case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
751               case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
752               case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
753               case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
754             }
755             if (xkey) {
756                 /*
757                  * The arrow keys normally do ESC [ A and so on. In
758                  * app cursor keys mode they do ESC O A instead.
759                  * Ctrl toggles the two modes.
760                  */
761                 if (vt52_mode) {
762                     end = 1 + sprintf(output+1, "\033%c", xkey);
763                 } else if (!app_cursor_keys ^
764                            !(event->state & GDK_CONTROL_MASK)) {
765                     end = 1 + sprintf(output+1, "\033O%c", xkey);
766                 } else {                    
767                     end = 1 + sprintf(output+1, "\033[%c", xkey);
768                 }
769                 goto done;
770             }
771         }
772         goto done;
773     }
774
775     done:
776
777     if (end-start > 0) {
778 #ifdef KEY_DEBUGGING
779         int i;
780         printf("generating sequence:");
781         for (i = start; i < end; i++)
782             printf(" %02x", (unsigned char) output[i]);
783         printf("\n");
784 #endif
785
786         ldisc_send(output+start, end-start, 1);
787         show_mouseptr(0);
788         term_out();
789     }
790
791     return TRUE;
792 }
793
794 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
795 {
796     struct gui_data *inst = (struct gui_data *)data;
797     int shift, ctrl, alt, x, y, button, act;
798
799     show_mouseptr(1);
800
801     if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
802         term_scroll(0, -5);
803         return TRUE;
804     }
805     if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
806         term_scroll(0, +5);
807         return TRUE;
808     }
809
810     shift = event->state & GDK_SHIFT_MASK;
811     ctrl = event->state & GDK_CONTROL_MASK;
812     alt = event->state & GDK_MOD1_MASK;
813     if (event->button == 1)
814         button = MBT_LEFT;
815     else if (event->button == 2)
816         button = MBT_MIDDLE;
817     else if (event->button == 3)
818         button = MBT_RIGHT;
819     else
820         return FALSE;                  /* don't even know what button! */
821
822     switch (event->type) {
823       case GDK_BUTTON_PRESS: act = MA_CLICK; break;
824       case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
825       case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
826       case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
827       default: return FALSE;           /* don't know this event type */
828     }
829
830     if (send_raw_mouse && !(cfg.mouse_override && shift) &&
831         act != MA_CLICK && act != MA_RELEASE)
832         return TRUE;                   /* we ignore these in raw mouse mode */
833
834     x = (event->x - cfg.window_border) / inst->font_width;
835     y = (event->y - cfg.window_border) / inst->font_height;
836
837     term_mouse(button, act, x, y, shift, ctrl, alt);
838
839     return TRUE;
840 }
841
842 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
843 {
844     struct gui_data *inst = (struct gui_data *)data;
845     int shift, ctrl, alt, x, y, button;
846
847     show_mouseptr(1);
848
849     shift = event->state & GDK_SHIFT_MASK;
850     ctrl = event->state & GDK_CONTROL_MASK;
851     alt = event->state & GDK_MOD1_MASK;
852     if (event->state & GDK_BUTTON1_MASK)
853         button = MBT_LEFT;
854     else if (event->state & GDK_BUTTON2_MASK)
855         button = MBT_MIDDLE;
856     else if (event->state & GDK_BUTTON3_MASK)
857         button = MBT_RIGHT;
858     else
859         return FALSE;                  /* don't even know what button! */
860
861     x = (event->x - cfg.window_border) / inst->font_width;
862     y = (event->y - cfg.window_border) / inst->font_height;
863
864     term_mouse(button, MA_DRAG, x, y, shift, ctrl, alt);
865
866     return TRUE;
867 }
868
869 gint timer_func(gpointer data)
870 {
871     /* struct gui_data *inst = (struct gui_data *)data; */
872     extern int pty_child_is_dead();  /* declared in pty.c */
873
874     if (pty_child_is_dead()) {
875         /*
876          * The primary child process died. We could keep the
877          * terminal open for remaining subprocesses to output to,
878          * but conventional wisdom seems to feel that that's the
879          * Wrong Thing for an xterm-alike, so we bail out now. This
880          * would be easy enough to change or make configurable if
881          * necessary.
882          */
883         exit(0);
884     }
885
886     term_update();
887     term_blink(0);
888     return TRUE;
889 }
890
891 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
892 {
893     /* struct gui_data *inst = (struct gui_data *)data; */
894     char buf[4096];
895     int ret;
896
897     ret = read(sourcefd, buf, sizeof(buf));
898
899     /*
900      * Clean termination condition is that either ret == 0, or ret
901      * < 0 and errno == EIO. Not sure why the latter, but it seems
902      * to happen. Boo.
903      */
904     if (ret == 0 || (ret < 0 && errno == EIO)) {
905         exit(0);
906     }
907
908     if (ret < 0) {
909         perror("read pty master");
910         exit(1);
911     }
912     if (ret > 0)
913         from_backend(0, buf, ret);
914     term_blink(1);
915     term_out();
916 }
917
918 void destroy(GtkWidget *widget, gpointer data)
919 {
920     gtk_main_quit();
921 }
922
923 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
924 {
925     has_focus = event->in;
926     term_out();
927     term_update();
928     show_mouseptr(1);
929     return FALSE;
930 }
931
932 /*
933  * set or clear the "raw mouse message" mode
934  */
935 void set_raw_mouse_mode(int activate)
936 {
937     activate = activate && !cfg.no_mouse_rep;
938     send_raw_mouse = activate;
939     if (send_raw_mouse)
940         inst->currcursor = inst->rawcursor;
941     else
942         inst->currcursor = inst->textcursor;
943     show_mouseptr(inst->mouseptr_visible);
944 }
945
946 void request_resize(int w, int h)
947 {
948     int large_x, large_y;
949     int offset_x, offset_y;
950     int area_x, area_y;
951     GtkRequisition inner, outer;
952
953     /*
954      * This is a heinous hack dreamed up by the gnome-terminal
955      * people to get around a limitation in gtk. The problem is
956      * that in order to set the size correctly we really need to be
957      * calling gtk_window_resize - but that needs to know the size
958      * of the _whole window_, not the drawing area. So what we do
959      * is to set an artificially huge size request on the drawing
960      * area, recompute the resulting size request on the window,
961      * and look at the difference between the two. That gives us
962      * the x and y offsets we need to translate drawing area size
963      * into window size for real, and then we call
964      * gtk_window_resize.
965      */
966
967     /*
968      * We start by retrieving the current size of the whole window.
969      * Adding a bit to _that_ will give us a value we can use as a
970      * bogus size request which guarantees to be bigger than the
971      * current size of the drawing area.
972      */
973     get_window_pixels(&large_x, &large_y);
974     large_x += 32;
975     large_y += 32;
976
977 #if GTK_CHECK_VERSION(2,0,0)
978     gtk_widget_set_size_request(inst->area, large_x, large_y);
979 #else
980     gtk_widget_set_usize(inst->area, large_x, large_y);
981 #endif
982     gtk_widget_size_request(inst->area, &inner);
983     gtk_widget_size_request(inst->window, &outer);
984
985     offset_x = outer.width - inner.width;
986     offset_y = outer.height - inner.height;
987
988     area_x = inst->font_width * w + 2*cfg.window_border;
989     area_y = inst->font_height * h + 2*cfg.window_border;
990
991     /*
992      * Now we must set the size request on the drawing area back to
993      * something sensible before we commit the real resize. Best
994      * way to do this, I think, is to set it to what the size is
995      * really going to end up being.
996      */
997 #if GTK_CHECK_VERSION(2,0,0)
998     gtk_widget_set_size_request(inst->area, area_x, area_y);
999 #else
1000     gtk_widget_set_usize(inst->area, area_x, area_y);
1001 #endif
1002
1003 #if GTK_CHECK_VERSION(2,0,0)
1004     gtk_window_resize(GTK_WINDOW(inst->window),
1005                       area_x + offset_x, area_y + offset_y);
1006 #else
1007     gdk_window_resize(inst->window->window,
1008                       area_x + offset_x, area_y + offset_y);
1009 #endif
1010 }
1011
1012 void real_palette_set(int n, int r, int g, int b)
1013 {
1014     gboolean success[1];
1015
1016     inst->cols[n].red = r * 0x0101;
1017     inst->cols[n].green = g * 0x0101;
1018     inst->cols[n].blue = b * 0x0101;
1019
1020     gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1021                               FALSE, FALSE, success);
1022     if (!success[0])
1023         g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1024                 n, r, g, b);
1025 }
1026
1027 void palette_set(int n, int r, int g, int b)
1028 {
1029     static const int first[21] = {
1030         0, 2, 4, 6, 8, 10, 12, 14,
1031         1, 3, 5, 7, 9, 11, 13, 15,
1032         16, 17, 18, 20, 22
1033     };
1034     real_palette_set(first[n], r, g, b);
1035     if (first[n] >= 18)
1036         real_palette_set(first[n] + 1, r, g, b);
1037 }
1038
1039 void palette_reset(void)
1040 {
1041     /* This maps colour indices in cfg to those used in inst->cols. */
1042     static const int ww[] = {
1043         6, 7, 8, 9, 10, 11, 12, 13,
1044         14, 15, 16, 17, 18, 19, 20, 21,
1045         0, 1, 2, 3, 4, 5
1046     };
1047     gboolean success[NCOLOURS];
1048     int i;
1049
1050     assert(lenof(ww) == NCOLOURS);
1051
1052     if (!inst->colmap) {
1053         inst->colmap = gdk_colormap_get_system();
1054     } else {
1055         gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1056     }
1057
1058     for (i = 0; i < NCOLOURS; i++) {
1059         inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
1060         inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
1061         inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
1062     }
1063
1064     gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1065                               FALSE, FALSE, success);
1066     for (i = 0; i < NCOLOURS; i++) {
1067         if (!success[i])
1068             g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1069                     i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
1070     }
1071 }
1072
1073 void write_clip(wchar_t * data, int len, int must_deselect)
1074 {
1075     if (inst->pasteout_data)
1076         sfree(inst->pasteout_data);
1077     inst->pasteout_data = smalloc(len);
1078     inst->pasteout_data_len = len;
1079     wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
1080              NULL, NULL);
1081
1082     if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1083                                 GDK_CURRENT_TIME)) {
1084         gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1085                                  GDK_SELECTION_TYPE_STRING, 1);
1086         gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1087                                  inst->compound_text_atom, 1);
1088     }
1089 }
1090
1091 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1092                    guint info, guint time_stamp, gpointer data)
1093 {
1094     gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
1095                            inst->pasteout_data, inst->pasteout_data_len);
1096 }
1097
1098 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1099                      gpointer data)
1100 {
1101     term_deselect();
1102     if (inst->pasteout_data)
1103         sfree(inst->pasteout_data);
1104     inst->pasteout_data = NULL;
1105     inst->pasteout_data_len = 0;
1106     return TRUE;
1107 }
1108
1109 void request_paste(void)
1110 {
1111     /*
1112      * In Unix, pasting is asynchronous: all we can do at the
1113      * moment is to call gtk_selection_convert(), and when the data
1114      * comes back _then_ we can call term_do_paste().
1115      */
1116     gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1117                           GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1118 }
1119
1120 gint idle_paste_func(gpointer data);   /* forward ref */
1121
1122 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1123                         gpointer data)
1124 {
1125     if (seldata->length <= 0 ||
1126         seldata->type != GDK_SELECTION_TYPE_STRING)
1127         return;                        /* Nothing happens. */
1128
1129     if (inst->pastein_data)
1130         sfree(inst->pastein_data);
1131
1132     inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
1133     inst->pastein_data_len = seldata->length;
1134     mb_to_wc(0, 0, seldata->data, seldata->length,
1135              inst->pastein_data, inst->pastein_data_len);
1136
1137     term_do_paste();
1138
1139     if (term_paste_pending())
1140         inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1141 }
1142
1143 gint idle_paste_func(gpointer data)
1144 {
1145     struct gui_data *inst = (struct gui_data *)data;
1146
1147     if (term_paste_pending())
1148         term_paste();
1149     else
1150         gtk_idle_remove(inst->term_paste_idle_id);
1151
1152     return TRUE;
1153 }
1154
1155
1156 void get_clip(wchar_t ** p, int *len)
1157 {
1158     if (p) {
1159         *p = inst->pastein_data;
1160         *len = inst->pastein_data_len;
1161     }
1162 }
1163
1164 void set_title(char *title)
1165 {
1166     strncpy(inst->wintitle, title, lenof(inst->wintitle));
1167     inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1168     gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1169 }
1170
1171 void set_icon(char *title)
1172 {
1173     strncpy(inst->icontitle, title, lenof(inst->icontitle));
1174     inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1175     gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1176 }
1177
1178 void set_sbar(int total, int start, int page)
1179 {
1180     if (!cfg.scrollbar)
1181         return;
1182     inst->sbar_adjust->lower = 0;
1183     inst->sbar_adjust->upper = total;
1184     inst->sbar_adjust->value = start;
1185     inst->sbar_adjust->page_size = page;
1186     inst->sbar_adjust->step_increment = 1;
1187     inst->sbar_adjust->page_increment = page/2;
1188     inst->ignore_sbar = TRUE;
1189     gtk_adjustment_changed(inst->sbar_adjust);
1190     inst->ignore_sbar = FALSE;
1191 }
1192
1193 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1194 {
1195     if (!cfg.scrollbar)
1196         return;
1197     if (!inst->ignore_sbar)
1198         term_scroll(1, (int)adj->value);
1199 }
1200
1201 void sys_cursor(int x, int y)
1202 {
1203     /*
1204      * This is meaningless under X.
1205      */
1206 }
1207
1208 void beep(int mode)
1209 {
1210     gdk_beep();
1211 }
1212
1213 int CharWidth(Context ctx, int uc)
1214 {
1215     /*
1216      * Under X, any fixed-width font really _is_ fixed-width.
1217      * Double-width characters will be dealt with using a separate
1218      * font. For the moment we can simply return 1.
1219      */
1220     return 1;
1221 }
1222
1223 Context get_ctx(void)
1224 {
1225     GdkGC *gc;
1226     if (!inst->area->window)
1227         return NULL;
1228     gc = gdk_gc_new(inst->area->window);
1229     return gc;
1230 }
1231
1232 void free_ctx(Context ctx)
1233 {
1234     GdkGC *gc = (GdkGC *)ctx;
1235     gdk_gc_unref(gc);
1236 }
1237
1238 /*
1239  * Draw a line of text in the window, at given character
1240  * coordinates, in given attributes.
1241  *
1242  * We are allowed to fiddle with the contents of `text'.
1243  */
1244 void do_text_internal(Context ctx, int x, int y, char *text, int len,
1245                       unsigned long attr, int lattr)
1246 {
1247     int nfg, nbg, t, fontid, shadow;
1248     GdkGC *gc = (GdkGC *)ctx;
1249
1250     /*
1251      * NYI:
1252      *  - Unicode, code pages, and ATTR_WIDE for CJK support.
1253      */
1254
1255     nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1256     nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1257     if (attr & ATTR_REVERSE) {
1258         t = nfg;
1259         nfg = nbg;
1260         nbg = t;
1261     }
1262     if (cfg.bold_colour && (attr & ATTR_BOLD))
1263         nfg++;
1264     if (cfg.bold_colour && (attr & ATTR_BLINK))
1265         nbg++;
1266     if (attr & TATTR_ACTCURS) {
1267         nfg = NCOLOURS-2;
1268         nbg = NCOLOURS-1;
1269     }
1270
1271     fontid = shadow = 0;
1272     if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
1273         if (inst->fonts[1])
1274             fontid = 1;
1275         else
1276             shadow = 1;
1277     }
1278
1279     if (lattr != LATTR_NORM) {
1280         x *= 2;
1281         if (x >= cols)
1282             return;
1283         if (x + len*2 > cols)
1284             len = (cols-x)/2;          /* trim to LH half */
1285     }
1286
1287     gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1288     gdk_draw_rectangle(inst->pixmap, gc, 1,
1289                        x*inst->font_width+cfg.window_border,
1290                        y*inst->font_height+cfg.window_border,
1291                        len*inst->font_width, inst->font_height);
1292
1293     gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1294     gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1295                   x*inst->font_width+cfg.window_border,
1296                   y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1297                   text, len);
1298
1299     /*
1300      * X fonts seem to be pretty consistent about leaving the
1301      * _left_ pixel of the cell blank rather than the right. Hence
1302      * I'm going to hard-code shadow bolding as displaying one
1303      * pixel to the left rather than try to work out whether it
1304      * should be left or right.
1305      */
1306     if (shadow) {
1307         gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1308                       x*inst->font_width+cfg.window_border - 1,
1309                       y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1310                       text, len);
1311     }
1312
1313     if (attr & ATTR_UNDER) {
1314         int uheight = inst->fonts[0]->ascent + 1;
1315         if (uheight >= inst->font_height)
1316             uheight = inst->font_height - 1;
1317         gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1318                       y*inst->font_height + uheight + cfg.window_border,
1319                       (x+len)*inst->font_width-1+cfg.window_border,
1320                       y*inst->font_height + uheight + cfg.window_border);
1321     }
1322
1323     if (lattr != LATTR_NORM) {
1324         /*
1325          * I can't find any plausible StretchBlt equivalent in the
1326          * X server, so I'm going to do this the slow and painful
1327          * way. This will involve repeated calls to
1328          * gdk_draw_pixmap() to stretch the text horizontally. It's
1329          * O(N^2) in time and O(N) in network bandwidth, but you
1330          * try thinking of a better way. :-(
1331          */
1332         int i;
1333         for (i = 0; i < len * inst->font_width; i++) {
1334             gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1335                             x*inst->font_width+cfg.window_border + 2*i,
1336                             y*inst->font_height+cfg.window_border,
1337                             x*inst->font_width+cfg.window_border + 2*i+1,
1338                             y*inst->font_height+cfg.window_border,
1339                             len * inst->font_width - i, inst->font_height);
1340         }
1341         len *= 2;
1342         if (lattr != LATTR_WIDE) {
1343             int dt, db;
1344             /* Now stretch vertically, in the same way. */
1345             if (lattr == LATTR_BOT)
1346                 dt = 0, db = 1;
1347             else
1348                 dt = 1, db = 0;
1349             for (i = 0; i < inst->font_height; i+=2) {
1350                 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1351                                 x*inst->font_width+cfg.window_border,
1352                                 y*inst->font_height+cfg.window_border+dt*i+db,
1353                                 x*inst->font_width+cfg.window_border,
1354                                 y*inst->font_height+cfg.window_border+dt*(i+1),
1355                                 len * inst->font_width, inst->font_height-i-1);
1356             }
1357         }
1358     }
1359 }
1360
1361 void do_text(Context ctx, int x, int y, char *text, int len,
1362              unsigned long attr, int lattr)
1363 {
1364     GdkGC *gc = (GdkGC *)ctx;
1365
1366     do_text_internal(ctx, x, y, text, len, attr, lattr);
1367
1368     if (lattr != LATTR_NORM) {
1369         x *= 2;
1370         if (x >= cols)
1371             return;
1372         if (x + len*2 > cols)
1373             len = (cols-x)/2;          /* trim to LH half */
1374         len *= 2;
1375     }
1376
1377     gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1378                     x*inst->font_width+cfg.window_border,
1379                     y*inst->font_height+cfg.window_border,
1380                     x*inst->font_width+cfg.window_border,
1381                     y*inst->font_height+cfg.window_border,
1382                     len*inst->font_width, inst->font_height);
1383 }
1384
1385 void do_cursor(Context ctx, int x, int y, char *text, int len,
1386                unsigned long attr, int lattr)
1387 {
1388     int passive;
1389     GdkGC *gc = (GdkGC *)ctx;
1390
1391     if (attr & TATTR_PASCURS) {
1392         attr &= ~TATTR_PASCURS;
1393         passive = 1;
1394     } else
1395         passive = 0;
1396     if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1397         attr &= ~TATTR_ACTCURS;
1398     }
1399     do_text_internal(ctx, x, y, text, len, attr, lattr);
1400
1401     if (lattr != LATTR_NORM) {
1402         x *= 2;
1403         if (x >= cols)
1404             return;
1405         if (x + len*2 > cols)
1406             len = (cols-x)/2;          /* trim to LH half */
1407         len *= 2;
1408     }
1409
1410     if (cfg.cursor_type == 0) {
1411         /*
1412          * An active block cursor will already have been done by
1413          * the above do_text call, so we only need to do anything
1414          * if it's passive.
1415          */
1416         if (passive) {
1417             gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1418             gdk_draw_rectangle(inst->pixmap, gc, 0,
1419                                x*inst->font_width+cfg.window_border,
1420                                y*inst->font_height+cfg.window_border,
1421                                len*inst->font_width-1, inst->font_height-1);
1422         }
1423     } else {
1424         int uheight;
1425         int startx, starty, dx, dy, length, i;
1426
1427         int char_width;
1428
1429         if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1430             char_width = 2*inst->font_width;
1431         else
1432             char_width = inst->font_width;
1433
1434         if (cfg.cursor_type == 1) {
1435             uheight = inst->fonts[0]->ascent + 1;
1436             if (uheight >= inst->font_height)
1437                 uheight = inst->font_height - 1;
1438
1439             startx = x * inst->font_width + cfg.window_border;
1440             starty = y * inst->font_height + cfg.window_border + uheight;
1441             dx = 1;
1442             dy = 0;
1443             length = len * char_width;
1444         } else {
1445             int xadjust = 0;
1446             if (attr & TATTR_RIGHTCURS)
1447                 xadjust = char_width - 1;
1448             startx = x * inst->font_width + cfg.window_border + xadjust;
1449             starty = y * inst->font_height + cfg.window_border;
1450             dx = 0;
1451             dy = 1;
1452             length = inst->font_height;
1453         }
1454
1455         gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1456         if (passive) {
1457             for (i = 0; i < length; i++) {
1458                 if (i % 2 == 0) {
1459                     gdk_draw_point(inst->pixmap, gc, startx, starty);
1460                 }
1461                 startx += dx;
1462                 starty += dy;
1463             }
1464         } else {
1465             gdk_draw_line(inst->pixmap, gc, startx, starty,
1466                           startx + (length-1) * dx, starty + (length-1) * dy);
1467         }
1468     }
1469
1470     gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1471                     x*inst->font_width+cfg.window_border,
1472                     y*inst->font_height+cfg.window_border,
1473                     x*inst->font_width+cfg.window_border,
1474                     y*inst->font_height+cfg.window_border,
1475                     len*inst->font_width, inst->font_height);
1476 }
1477
1478 GdkCursor *make_mouse_ptr(int cursor_val)
1479 {
1480     /*
1481      * Truly hideous hack: GTK doesn't allow us to set the mouse
1482      * cursor foreground and background colours unless we've _also_
1483      * created our own cursor from bitmaps. Therefore, I need to
1484      * load the `cursor' font and draw glyphs from it on to
1485      * pixmaps, in order to construct my cursors with the fg and bg
1486      * I want. This is a gross hack, but it's more self-contained
1487      * than linking in Xlib to find the X window handle to
1488      * inst->area and calling XRecolorCursor, and it's more
1489      * futureproof than hard-coding the shapes as bitmap arrays.
1490      */
1491     static GdkFont *cursor_font = NULL;
1492     GdkPixmap *source, *mask;
1493     GdkGC *gc;
1494     GdkColor cfg = { 0, 65535, 65535, 65535 };
1495     GdkColor cbg = { 0, 0, 0, 0 };
1496     GdkColor dfg = { 1, 65535, 65535, 65535 };
1497     GdkColor dbg = { 0, 0, 0, 0 };
1498     GdkCursor *ret;
1499     gchar text[2];
1500     gint lb, rb, wid, asc, desc, w, h, x, y;
1501
1502     if (cursor_val == -2) {
1503         gdk_font_unref(cursor_font);
1504         return NULL;
1505     }
1506
1507     if (cursor_val >= 0 && !cursor_font)
1508         cursor_font = gdk_font_load("cursor");
1509
1510     /*
1511      * Get the text extent of the cursor in question. We use the
1512      * mask character for this, because it's typically slightly
1513      * bigger than the main character.
1514      */
1515     if (cursor_val >= 0) {
1516         text[1] = '\0';
1517         text[0] = (char)cursor_val + 1;
1518         gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1519         w = rb-lb; h = asc+desc; x = -lb; y = asc;
1520     } else {
1521         w = h = 1;
1522         x = y = 0;
1523     }
1524
1525     source = gdk_pixmap_new(NULL, w, h, 1);
1526     mask = gdk_pixmap_new(NULL, w, h, 1);
1527
1528     /*
1529      * Draw the mask character on the mask pixmap.
1530      */
1531     gc = gdk_gc_new(mask);
1532     gdk_gc_set_foreground(gc, &dbg);
1533     gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1534     if (cursor_val >= 0) {
1535         text[1] = '\0';
1536         text[0] = (char)cursor_val + 1;
1537         gdk_gc_set_foreground(gc, &dfg);
1538         gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1539     }
1540     gdk_gc_unref(gc);
1541
1542     /*
1543      * Draw the main character on the source pixmap.
1544      */
1545     gc = gdk_gc_new(source);
1546     gdk_gc_set_foreground(gc, &dbg);
1547     gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1548     if (cursor_val >= 0) {
1549         text[1] = '\0';
1550         text[0] = (char)cursor_val;
1551         gdk_gc_set_foreground(gc, &dfg);
1552         gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1553     }
1554     gdk_gc_unref(gc);
1555
1556     /*
1557      * Create the cursor.
1558      */
1559     ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1560
1561     /*
1562      * Clean up.
1563      */
1564     gdk_pixmap_unref(source);
1565     gdk_pixmap_unref(mask);
1566
1567     return ret;
1568 }
1569
1570 void modalfatalbox(char *p, ...)
1571 {
1572     va_list ap;
1573     fprintf(stderr, "FATAL ERROR: ");
1574     va_start(ap, p);
1575     vfprintf(stderr, p, ap);
1576     va_end(ap);
1577     fputc('\n', stderr);
1578     exit(1);
1579 }
1580
1581 char *get_x_display(void)
1582 {
1583     return gdk_get_display();
1584 }
1585
1586 char *app_name = "pterm";
1587
1588 int do_cmdline(int argc, char **argv, int do_everything)
1589 {
1590     int err = 0;
1591     extern char **pty_argv;            /* declared in pty.c */
1592
1593     /*
1594      * Macros to make argument handling easier.
1595      */
1596 #define EXPECTS_ARG do { \
1597     if (--argc <= 0) { \
1598         err = 1; \
1599         fprintf(stderr, "pterm: %s expects an argument\n", p); \
1600     } else \
1601         val = *++argv; \
1602 } while (0)
1603 #define SECOND_PASS_ONLY do { \
1604     if (!do_everything) continue; \
1605 } while (0)
1606
1607     /*
1608      * TODO:
1609      * 
1610      * finish -geometry
1611      */
1612
1613     char *val;
1614     while (--argc > 0) {
1615         char *p = *++argv;
1616         if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1617             EXPECTS_ARG;
1618             SECOND_PASS_ONLY;
1619             strncpy(cfg.font, val, sizeof(cfg.font));
1620             cfg.font[sizeof(cfg.font)-1] = '\0';
1621
1622         } else if (!strcmp(p, "-fb")) {
1623             EXPECTS_ARG;
1624             SECOND_PASS_ONLY;
1625             strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1626             cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1627
1628         } else if (!strcmp(p, "-geometry")) {
1629             int flags, x, y, w, h;
1630             EXPECTS_ARG;
1631             SECOND_PASS_ONLY;
1632
1633             flags = XParseGeometry(val, &x, &y, &w, &h);
1634             if (flags & WidthValue)
1635                 cfg.width = w;
1636             if (flags & HeightValue)
1637                 cfg.height = h;
1638
1639             /*
1640              * Apparently setting the initial window position is
1641              * difficult in GTK 1.2. Not entirely sure why this
1642              * should be. 2.0 has gtk_window_parse_geometry(),
1643              * which would help... For the moment, though, I can't
1644              * be bothered with this.
1645              */
1646
1647         } else if (!strcmp(p, "-sl")) {
1648             EXPECTS_ARG;
1649             SECOND_PASS_ONLY;
1650             cfg.savelines = atoi(val);
1651
1652         } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1653                    !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1654                    !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1655             GdkColor col;
1656
1657             EXPECTS_ARG;
1658             SECOND_PASS_ONLY;
1659             if (!gdk_color_parse(val, &col)) {
1660                 err = 1;
1661                 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1662             } else {
1663                 int index;
1664                 index = (!strcmp(p, "-fg") ? 0 :
1665                          !strcmp(p, "-bg") ? 2 :
1666                          !strcmp(p, "-bfg") ? 1 :
1667                          !strcmp(p, "-bbg") ? 3 :
1668                          !strcmp(p, "-cfg") ? 4 :
1669                          !strcmp(p, "-cbg") ? 5 : -1);
1670                 assert(index != -1);
1671                 cfg.colours[index][0] = col.red / 256;
1672                 cfg.colours[index][1] = col.green / 256;
1673                 cfg.colours[index][2] = col.blue / 256;
1674             }
1675
1676         } else if (!strcmp(p, "-e")) {
1677             /* This option swallows all further arguments. */
1678             if (!do_everything)
1679                 break;
1680
1681             if (--argc > 0) {
1682                 int i;
1683                 pty_argv = smalloc((argc+1) * sizeof(char *));
1684                 ++argv;
1685                 for (i = 0; i < argc; i++)
1686                     pty_argv[i] = argv[i];
1687                 pty_argv[argc] = NULL;
1688                 break;                 /* finished command-line processing */
1689             } else
1690                 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1691
1692         } else if (!strcmp(p, "-T")) {
1693             EXPECTS_ARG;
1694             SECOND_PASS_ONLY;
1695             strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1696             cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1697
1698         } else if (!strcmp(p, "-log")) {
1699             EXPECTS_ARG;
1700             SECOND_PASS_ONLY;
1701             strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1702             cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1703             cfg.logtype = LGTYP_DEBUG;
1704
1705         } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1706             SECOND_PASS_ONLY;
1707             cfg.stamp_utmp = 0;
1708
1709         } else if (!strcmp(p, "-ut")) {
1710             SECOND_PASS_ONLY;
1711             cfg.stamp_utmp = 0;
1712
1713         } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1714             SECOND_PASS_ONLY;
1715             cfg.login_shell = 0;
1716
1717         } else if (!strcmp(p, "-ls")) {
1718             SECOND_PASS_ONLY;
1719             cfg.login_shell = 1;
1720
1721         } else if (!strcmp(p, "-nethack")) {
1722             SECOND_PASS_ONLY;
1723             cfg.nethack_keypad = 1;
1724
1725         } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1726             SECOND_PASS_ONLY;
1727             cfg.scrollbar = 0;
1728
1729         } else if (!strcmp(p, "-sb")) {
1730             SECOND_PASS_ONLY;
1731             cfg.scrollbar = 0;
1732
1733         } else if (!strcmp(p, "-name")) {
1734             EXPECTS_ARG;
1735             app_name = val;
1736
1737         } else if (!strcmp(p, "-xrm")) {
1738             EXPECTS_ARG;
1739             provide_xrm_string(val);
1740
1741         }
1742     }
1743
1744     return err;
1745 }
1746
1747 int main(int argc, char **argv)
1748 {
1749     extern int pty_master_fd;          /* declared in pty.c */
1750     extern void pty_pre_init(void);    /* declared in pty.c */
1751
1752     pty_pre_init();
1753
1754     gtk_init(&argc, &argv);
1755
1756     if (do_cmdline(argc, argv, 0))     /* pre-defaults pass to get -class */
1757         exit(1);
1758     do_defaults(NULL, &cfg);
1759     if (do_cmdline(argc, argv, 1))     /* post-defaults, do everything */
1760         exit(1);
1761
1762     inst->fonts[0] = gdk_font_load(cfg.font);
1763     if (!inst->fonts[0]) {
1764         fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1765         exit(1);
1766     }
1767     if (cfg.boldfont[0]) {
1768         inst->fonts[1] = gdk_font_load(cfg.boldfont);
1769         if (!inst->fonts[1]) {
1770             fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1771                     cfg.boldfont);
1772             exit(1);
1773         }
1774     } else
1775         inst->fonts[1] = NULL;
1776
1777     inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1778     inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1779
1780     inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1781
1782     init_ucs();
1783
1784     inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1785
1786     if (cfg.wintitle[0])
1787         set_title(cfg.wintitle);
1788     else
1789         set_title("pterm");
1790
1791     /*
1792      * Set up the colour map.
1793      */
1794     palette_reset();
1795
1796     inst->area = gtk_drawing_area_new();
1797     gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1798                           inst->font_width * cfg.width + 2*cfg.window_border,
1799                           inst->font_height * cfg.height + 2*cfg.window_border);
1800     if (cfg.scrollbar) {
1801         inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1802         inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1803     }
1804     inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1805     if (cfg.scrollbar) {
1806         if (cfg.scrollbar_on_left)
1807             gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1808         else
1809             gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1810     }
1811     gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1812
1813     gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
1814
1815     {
1816         GdkGeometry geom;
1817         geom.min_width = inst->font_width + 2*cfg.window_border;
1818         geom.min_height = inst->font_height + 2*cfg.window_border;
1819         geom.max_width = geom.max_height = -1;
1820         geom.base_width = 2*cfg.window_border;
1821         geom.base_height = 2*cfg.window_border;
1822         geom.width_inc = inst->font_width;
1823         geom.height_inc = inst->font_height;
1824         geom.min_aspect = geom.max_aspect = 0;
1825         gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
1826                                       GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1827                                       GDK_HINT_RESIZE_INC);
1828     }
1829
1830     gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
1831                        GTK_SIGNAL_FUNC(destroy), inst);
1832     gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
1833                        GTK_SIGNAL_FUNC(delete_window), inst);
1834     gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
1835                        GTK_SIGNAL_FUNC(key_event), inst);
1836     gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
1837                        GTK_SIGNAL_FUNC(key_event), inst);
1838     gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
1839                        GTK_SIGNAL_FUNC(focus_event), inst);
1840     gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
1841                        GTK_SIGNAL_FUNC(focus_event), inst);
1842     gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1843                        GTK_SIGNAL_FUNC(configure_area), inst);
1844     gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1845                        GTK_SIGNAL_FUNC(expose_area), inst);
1846     gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1847                        GTK_SIGNAL_FUNC(button_event), inst);
1848     gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1849                        GTK_SIGNAL_FUNC(button_event), inst);
1850     gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1851                        GTK_SIGNAL_FUNC(motion_event), inst);
1852     gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1853                        GTK_SIGNAL_FUNC(selection_received), inst);
1854     gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1855                        GTK_SIGNAL_FUNC(selection_get), inst);
1856     gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1857                        GTK_SIGNAL_FUNC(selection_clear), inst);
1858     if (cfg.scrollbar)
1859         gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1860                            GTK_SIGNAL_FUNC(scrollbar_moved), inst);
1861     gtk_timeout_add(20, timer_func, inst);
1862     gtk_widget_add_events(GTK_WIDGET(inst->area),
1863                           GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1864                           GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1865                           GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
1866
1867     gtk_widget_show(inst->area);
1868     if (cfg.scrollbar)
1869         gtk_widget_show(inst->sbar);
1870     gtk_widget_show(GTK_WIDGET(inst->hbox));
1871     gtk_widget_show(inst->window);
1872
1873     inst->textcursor = make_mouse_ptr(GDK_XTERM);
1874     inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
1875     inst->blankcursor = make_mouse_ptr(-1);
1876     make_mouse_ptr(-2);                /* clean up cursor font */
1877     inst->currcursor = inst->textcursor;
1878     show_mouseptr(1);
1879
1880     back = &pty_backend;
1881     back->init(NULL, 0, NULL, 0);
1882
1883     gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
1884
1885     term_init();
1886     term_size(cfg.height, cfg.width, cfg.savelines);
1887
1888     gtk_main();
1889
1890     return 0;
1891 }