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