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