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