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