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