We shouldn't offer the Break special when we don't have a session to send it
[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>
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>
3aac0d35 25#include <X11/Xatom.h>
f7f27309 26
1709795f 27#define PUTTY_DO_GLOBALS /* actually _define_ globals */
2dc6356a 28
1709795f 29#include "putty.h"
887035a5 30#include "terminal.h"
1709795f 31
f7f27309 32#define CAT2(x,y) x ## y
33#define CAT(x,y) CAT2(x,y)
34#define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
35
d64838e1 36#define NCOLOURS (lenof(((Config *)0)->colours))
37
8c161662 38GdkAtom compound_text_atom, utf8_string_atom;
39
56801e3d 40extern char **pty_argv; /* declared in pty.c */
41extern int use_pty_argv;
42
d64838e1 43struct gui_data {
12d200b1 44 GtkWidget *window, *area, *sbar;
6a5e84dd 45 GtkBox *hbox;
46 GtkAdjustment *sbar_adjust;
e3be8de5 47 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
48 *restartitem;
56801e3d 49 GtkWidget *sessionsmenu;
d64838e1 50 GdkPixmap *pixmap;
006238cb 51 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
2dc6356a 52 struct {
53 int charset;
54 int is_wide;
006238cb 55 } fontinfo[4];
aca2a702 56 int xpos, ypos, gotpos, gravity;
57e636ca 57 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
d64838e1 58 GdkColor cols[NCOLOURS];
59 GdkColormap *colmap;
e6346999 60 wchar_t *pastein_data;
085f4a68 61 int direct_to_font;
e6346999 62 int pastein_data_len;
48f1bc44 63 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
64 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
83616aab 65 int font_width, font_height;
56b9b9a7 66 int width, height;
88e6b9ca 67 int ignore_sbar;
b3530065 68 int mouseptr_visible;
0f660c8f 69 guint term_paste_idle_id;
c33c3d76 70 int alt_keycode;
045f707b 71 int alt_digits;
12d200b1 72 char wintitle[sizeof(((Config *)0)->wintitle)];
16891265 73 char icontitle[sizeof(((Config *)0)->wintitle)];
74aca06d 74 int master_fd, master_func_id;
b9d7bcad 75 void *ldisc;
6b78788a 76 Backend *back;
77 void *backhandle;
5def7522 78 Terminal *term;
a8327734 79 void *logctx;
74aca06d 80 int exited;
21d2b241 81 struct unicode_data ucsdata;
3ea863a3 82 Config cfg;
8eed910d 83 void *eventlogstuff;
56801e3d 84 char *progname, **gtkargvstart;
85 int ngtkargs;
d0a039b1 86 guint32 input_event_time; /* Timestamp of the most recent input event. */
a8327734 87};
88
89struct draw_ctx {
90 GdkGC *gc;
91 struct gui_data *inst;
d64838e1 92};
93
d64838e1 94static int send_raw_mouse;
95
c5e438ec 96static char *app_name = "pterm";
97
e3be8de5 98static void start_backend(struct gui_data *inst);
99
c85623f9 100char *x_get_default(const char *key)
c5e438ec 101{
102 return XGetDefault(GDK_DISPLAY(), app_name, key);
103}
104
3f935d5b 105void connection_fatal(void *frontend, char *p, ...)
106{
fbf6cb3b 107 struct gui_data *inst = (struct gui_data *)frontend;
3f935d5b 108
109 va_list ap;
110 char *msg;
111 va_start(ap, p);
112 msg = dupvprintf(p, ap);
113 va_end(ap);
114 inst->exited = TRUE;
115 fatal_message_box(inst->window, msg);
116 sfree(msg);
117 if (inst->cfg.close_on_exit == FORCE_ON)
118 cleanup_exit(1);
119}
120
5a9eb105 121/*
122 * Default settings that are specific to pterm.
123 */
9a30e26b 124FontSpec platform_default_fontspec(const char *name)
5a9eb105 125{
9a30e26b 126 FontSpec ret;
5a9eb105 127 if (!strcmp(name, "Font"))
9a30e26b 128 strcpy(ret.name, "fixed");
129 else
130 *ret.name = '\0';
131 return ret;
132}
133
134Filename platform_default_filename(const char *name)
135{
136 Filename ret;
137 if (!strcmp(name, "LogFileName"))
138 strcpy(ret.path, "putty.log");
139 else
140 *ret.path = '\0';
141 return ret;
142}
143
144char *platform_default_s(const char *name)
145{
5a9eb105 146 return NULL;
147}
148
c85623f9 149int platform_default_i(const char *name, int def)
5a9eb105 150{
151 if (!strcmp(name, "CloseOnExit"))
c888fed4 152 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
6e785e0c 153 if (!strcmp(name, "WinNameAlways"))
154 return 0; /* X natively supports icon titles, so use 'em by default */
5a9eb105 155 return def;
156}
157
b9d7bcad 158void ldisc_update(void *frontend, int echo, int edit)
1709795f 159{
160 /*
161 * This is a stub in pterm. If I ever produce a Unix
162 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
163 * then it will require some termios manoeuvring analogous to
164 * that in the Windows plink.c, but here it's meaningless.
165 */
166}
167
fbf6cb3b 168int from_backend(void *frontend, int is_stderr, const char *data, int len)
169{
170 struct gui_data *inst = (struct gui_data *)frontend;
171 return term_data(inst->term, is_stderr, data, len);
172}
173
cbe2d68f 174void logevent(void *frontend, const char *string)
1709795f 175{
fbf6cb3b 176 struct gui_data *inst = (struct gui_data *)frontend;
8eed910d 177
178 log_eventlog(inst->logctx, string);
179
180 logevent_dlg(inst->eventlogstuff, string);
1709795f 181}
182
a8327734 183int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
e9aef757 184{
a8327734 185 struct gui_data *inst = (struct gui_data *)frontend;
186
e9aef757 187 if (which)
188 return inst->font_height;
189 else
190 return inst->font_width;
191}
192
1709795f 193/*
194 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
195 * into a cooked one (SELECT, EXTEND, PASTE).
196 *
197 * In Unix, this is not configurable; the X button arrangement is
198 * rock-solid across all applications, everyone has a three-button
199 * mouse or a means of faking it, and there is no need to switch
200 * buttons around at all.
201 */
c387aa9f 202static Mouse_Button translate_button(Mouse_Button button)
1709795f 203{
a8327734 204 /* struct gui_data *inst = (struct gui_data *)frontend; */
205
1709795f 206 if (button == MBT_LEFT)
207 return MBT_SELECT;
208 if (button == MBT_MIDDLE)
209 return MBT_PASTE;
210 if (button == MBT_RIGHT)
211 return MBT_EXTEND;
212 return 0; /* shouldn't happen */
213}
214
215/*
5bf9955d 216 * Return the top-level GtkWindow associated with a particular
217 * front end instance.
218 */
219void *get_window(void *frontend)
220{
fbf6cb3b 221 struct gui_data *inst = (struct gui_data *)frontend;
5bf9955d 222 return inst->window;
223}
224
225/*
1709795f 226 * Minimise or restore the window in response to a server-side
227 * request.
228 */
a8327734 229void set_iconic(void *frontend, int iconic)
1709795f 230{
16891265 231 /*
232 * GTK 1.2 doesn't know how to do this.
233 */
234#if GTK_CHECK_VERSION(2,0,0)
a8327734 235 struct gui_data *inst = (struct gui_data *)frontend;
16891265 236 if (iconic)
237 gtk_window_iconify(GTK_WINDOW(inst->window));
238 else
239 gtk_window_deiconify(GTK_WINDOW(inst->window));
240#endif
1709795f 241}
242
243/*
244 * Move the window in response to a server-side request.
245 */
a8327734 246void move_window(void *frontend, int x, int y)
1709795f 247{
a8327734 248 struct gui_data *inst = (struct gui_data *)frontend;
16891265 249 /*
250 * I assume that when the GTK version of this call is available
251 * we should use it. Not sure how it differs from the GDK one,
252 * though.
253 */
254#if GTK_CHECK_VERSION(2,0,0)
255 gtk_window_move(GTK_WINDOW(inst->window), x, y);
256#else
257 gdk_window_move(inst->window->window, x, y);
258#endif
1709795f 259}
260
261/*
262 * Move the window to the top or bottom of the z-order in response
263 * to a server-side request.
264 */
a8327734 265void set_zorder(void *frontend, int top)
1709795f 266{
a8327734 267 struct gui_data *inst = (struct gui_data *)frontend;
16891265 268 if (top)
269 gdk_window_raise(inst->window->window);
270 else
271 gdk_window_lower(inst->window->window);
1709795f 272}
273
274/*
275 * Refresh the window in response to a server-side request.
276 */
a8327734 277void refresh_window(void *frontend)
1709795f 278{
a8327734 279 struct gui_data *inst = (struct gui_data *)frontend;
5def7522 280 term_invalidate(inst->term);
1709795f 281}
282
283/*
284 * Maximise or restore the window in response to a server-side
285 * request.
286 */
a8327734 287void set_zoomed(void *frontend, int zoomed)
1709795f 288{
16891265 289 /*
290 * GTK 1.2 doesn't know how to do this.
291 */
292#if GTK_CHECK_VERSION(2,0,0)
a8327734 293 struct gui_data *inst = (struct gui_data *)frontend;
16891265 294 if (iconic)
295 gtk_window_maximize(GTK_WINDOW(inst->window));
296 else
297 gtk_window_unmaximize(GTK_WINDOW(inst->window));
298#endif
1709795f 299}
300
301/*
302 * Report whether the window is iconic, for terminal reports.
303 */
a8327734 304int is_iconic(void *frontend)
1709795f 305{
a8327734 306 struct gui_data *inst = (struct gui_data *)frontend;
16891265 307 return !gdk_window_is_viewable(inst->window->window);
1709795f 308}
309
310/*
311 * Report the window's position, for terminal reports.
312 */
a8327734 313void get_window_pos(void *frontend, int *x, int *y)
1709795f 314{
a8327734 315 struct gui_data *inst = (struct gui_data *)frontend;
16891265 316 /*
317 * I assume that when the GTK version of this call is available
318 * we should use it. Not sure how it differs from the GDK one,
319 * though.
320 */
321#if GTK_CHECK_VERSION(2,0,0)
322 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
323#else
324 gdk_window_get_position(inst->window->window, x, y);
325#endif
1709795f 326}
327
328/*
329 * Report the window's pixel size, for terminal reports.
330 */
a8327734 331void get_window_pixels(void *frontend, int *x, int *y)
1709795f 332{
a8327734 333 struct gui_data *inst = (struct gui_data *)frontend;
16891265 334 /*
335 * I assume that when the GTK version of this call is available
336 * we should use it. Not sure how it differs from the GDK one,
337 * though.
338 */
339#if GTK_CHECK_VERSION(2,0,0)
340 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
341#else
342 gdk_window_get_size(inst->window->window, x, y);
343#endif
1709795f 344}
345
346/*
347 * Return the window or icon title.
348 */
a8327734 349char *get_window_title(void *frontend, int icon)
1709795f 350{
a8327734 351 struct gui_data *inst = (struct gui_data *)frontend;
5446ac11 352 return icon ? inst->icontitle : inst->wintitle;
1709795f 353}
f7f27309 354
f7f27309 355gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
356{
abb6895f 357 struct gui_data *inst = (struct gui_data *)data;
6b0eeb4e 358 if (!inst->exited && inst->cfg.warn_on_close) {
fbf6cb3b 359 if (!reallyclose(inst))
abb6895f 360 return TRUE;
361 }
f7f27309 362 return FALSE;
363}
364
a8327734 365static void show_mouseptr(struct gui_data *inst, int show)
57e636ca 366{
3ea863a3 367 if (!inst->cfg.hide_mouseptr)
57e636ca 368 show = 1;
369 if (show)
370 gdk_window_set_cursor(inst->area->window, inst->currcursor);
371 else
372 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
b3530065 373 inst->mouseptr_visible = show;
57e636ca 374}
375
db26b3af 376void draw_backing_rect(struct gui_data *inst)
377{
378 GdkGC *gc = gdk_gc_new(inst->area->window);
379 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
380 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
381 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
382 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
383 gdk_gc_unref(gc);
384}
385
f7f27309 386gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
387{
388 struct gui_data *inst = (struct gui_data *)data;
88e6b9ca 389 int w, h, need_size = 0;
f7f27309 390
e7ec3df1 391 /*
392 * See if the terminal size has changed, in which case we must
393 * let the terminal know.
394 */
3ea863a3 395 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
396 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
56b9b9a7 397 if (w != inst->width || h != inst->height) {
56b9b9a7 398 inst->cfg.width = inst->width = w;
399 inst->cfg.height = inst->height = h;
88e6b9ca 400 need_size = 1;
401 }
e7ec3df1 402
403 if (inst->pixmap) {
404 gdk_pixmap_unref(inst->pixmap);
405 inst->pixmap = NULL;
6a5e84dd 406 }
f7f27309 407
e7ec3df1 408 inst->pixmap = gdk_pixmap_new(widget->window,
409 (inst->cfg.width * inst->font_width +
410 2*inst->cfg.window_border),
411 (inst->cfg.height * inst->font_height +
412 2*inst->cfg.window_border), -1);
413
db26b3af 414 draw_backing_rect(inst);
e7ec3df1 415
416 if (need_size && inst->term) {
3ea863a3 417 term_size(inst->term, h, w, inst->cfg.savelines);
88e6b9ca 418 }
419
e7ec3df1 420 if (inst->term)
421 term_invalidate(inst->term);
422
f7f27309 423 return TRUE;
424}
425
426gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
427{
a8327734 428 struct gui_data *inst = (struct gui_data *)data;
f7f27309 429
430 /*
1709795f 431 * Pass the exposed rectangle to terminal.c, which will call us
432 * back to do the actual painting.
f7f27309 433 */
6a5e84dd 434 if (inst->pixmap) {
435 gdk_draw_pixmap(widget->window,
436 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
437 inst->pixmap,
438 event->area.x, event->area.y,
439 event->area.x, event->area.y,
440 event->area.width, event->area.height);
441 }
1709795f 442 return TRUE;
f7f27309 443}
444
445#define KEY_PRESSED(k) \
446 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
447
448gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
449{
a8327734 450 struct gui_data *inst = (struct gui_data *)data;
a57cd64b 451 char output[32];
f5dd8adb 452 wchar_t ucsoutput[2];
453 int ucsval, start, end, special, use_ucsoutput;
f7f27309 454
d0a039b1 455 /* Remember the timestamp. */
456 inst->input_event_time = event->time;
457
c33c3d76 458 /* By default, nothing is generated. */
459 end = start = 0;
f5dd8adb 460 special = use_ucsoutput = FALSE;
c33c3d76 461
462 /*
463 * If Alt is being released after typing an Alt+numberpad
464 * sequence, we should generate the code that was typed.
045f707b 465 *
466 * Note that we only do this if more than one key was actually
467 * pressed - I don't think Alt+NumPad4 should be ^D or that
468 * Alt+NumPad3 should be ^C, for example. There's no serious
469 * inconvenience in having to type a zero before a single-digit
470 * character code.
c33c3d76 471 */
472 if (event->type == GDK_KEY_RELEASE &&
473 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
474 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
045f707b 475 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
c33c3d76 476#ifdef KEY_DEBUGGING
477 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
478#endif
479 output[0] = inst->alt_keycode;
480 end = 1;
481 goto done;
482 }
483
1709795f 484 if (event->type == GDK_KEY_PRESS) {
a57cd64b 485#ifdef KEY_DEBUGGING
486 {
487 int i;
488 printf("keypress: keyval = %04x, state = %08x; string =",
489 event->keyval, event->state);
490 for (i = 0; event->string[i]; i++)
491 printf(" %02x", (unsigned char) event->string[i]);
492 printf("\n");
493 }
494#endif
495
496 /*
c33c3d76 497 * NYI: Compose key (!!! requires Unicode faff before even trying)
a57cd64b 498 */
499
6a5e84dd 500 /*
c33c3d76 501 * If Alt has just been pressed, we start potentially
502 * accumulating an Alt+numberpad code. We do this by
503 * setting alt_keycode to -1 (nothing yet but plausible).
504 */
505 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
506 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
507 inst->alt_keycode = -1;
045f707b 508 inst->alt_digits = 0;
c33c3d76 509 goto done; /* this generates nothing else */
510 }
511
512 /*
513 * If we're seeing a numberpad key press with Mod1 down,
514 * consider adding it to alt_keycode if that's sensible.
515 * Anything _else_ with Mod1 down cancels any possibility
516 * of an ALT keycode: we set alt_keycode to -2.
517 */
518 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
519 int digit = -1;
520 switch (event->keyval) {
521 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
522 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
523 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
524 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
525 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
526 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
527 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
528 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
529 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
530 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
531 }
532 if (digit < 0)
533 inst->alt_keycode = -2; /* it's invalid */
534 else {
535#ifdef KEY_DEBUGGING
536 printf("Adding digit %d to keycode %d", digit,
537 inst->alt_keycode);
538#endif
539 if (inst->alt_keycode == -1)
540 inst->alt_keycode = digit; /* one-digit code */
541 else
542 inst->alt_keycode = inst->alt_keycode * 10 + digit;
045f707b 543 inst->alt_digits++;
c33c3d76 544#ifdef KEY_DEBUGGING
545 printf(" gives new code %d\n", inst->alt_keycode);
546#endif
547 /* Having used this digit, we now do nothing more with it. */
548 goto done;
549 }
550 }
551
552 /*
6a5e84dd 553 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
554 * at all.
555 */
556 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
3ea863a3 557 term_scroll(inst->term, 0, -inst->cfg.height/2);
6a5e84dd 558 return TRUE;
559 }
153580da 560 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
561 term_scroll(inst->term, 0, -1);
562 return TRUE;
563 }
6a5e84dd 564 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
3ea863a3 565 term_scroll(inst->term, 0, +inst->cfg.height/2);
6a5e84dd 566 return TRUE;
567 }
153580da 568 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
569 term_scroll(inst->term, 0, +1);
570 return TRUE;
571 }
6a5e84dd 572
2a2c1973 573 /*
574 * Neither does Shift-Ins.
575 */
576 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
a8327734 577 request_paste(inst);
2a2c1973 578 return TRUE;
579 }
580
3ec1de33 581 special = FALSE;
f5dd8adb 582 use_ucsoutput = FALSE;
3ec1de33 583
a57cd64b 584 /* ALT+things gives leading Escape. */
585 output[0] = '\033';
586 strncpy(output+1, event->string, 31);
f5dd8adb 587 if (!*event->string &&
588 (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
589 ucsoutput[0] = '\033';
590 ucsoutput[1] = ucsval;
591 use_ucsoutput = TRUE;
592 end = 2;
593 } else {
594 output[31] = '\0';
595 end = strlen(output);
596 }
5c0ea258 597 if (event->state & GDK_MOD1_MASK) {
598 start = 0;
599 if (end == 1) end = 0;
600 } else
601 start = 1;
a57cd64b 602
603 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
604 if (!event->string[0] && event->keyval == '`' &&
605 (event->state & GDK_CONTROL_MASK)) {
606 output[1] = '\x1C';
f5dd8adb 607 use_ucsoutput = FALSE;
a57cd64b 608 end = 2;
609 }
610
611 /* Control-Break is the same as Control-C */
612 if (event->keyval == GDK_Break &&
613 (event->state & GDK_CONTROL_MASK)) {
614 output[1] = '\003';
f5dd8adb 615 use_ucsoutput = FALSE;
a57cd64b 616 end = 2;
3ec1de33 617 special = TRUE;
618 }
619
620 /* We handle Return ourselves, because it needs to be flagged as
621 * special to ldisc. */
622 if (event->keyval == GDK_Return) {
623 output[1] = '\015';
f5dd8adb 624 use_ucsoutput = FALSE;
3ec1de33 625 end = 2;
626 special = TRUE;
a57cd64b 627 }
628
629 /* Control-2, Control-Space and Control-@ are NUL */
630 if (!event->string[0] &&
631 (event->keyval == ' ' || event->keyval == '2' ||
632 event->keyval == '@') &&
633 (event->state & (GDK_SHIFT_MASK |
634 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
635 output[1] = '\0';
f5dd8adb 636 use_ucsoutput = FALSE;
a57cd64b 637 end = 2;
638 }
639
640 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
641 if (!event->string[0] && event->keyval == ' ' &&
642 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
643 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
644 output[1] = '\240';
f5dd8adb 645 use_ucsoutput = FALSE;
a57cd64b 646 end = 2;
647 }
648
649 /* We don't let GTK tell us what Backspace is! We know better. */
650 if (event->keyval == GDK_BackSpace &&
651 !(event->state & GDK_SHIFT_MASK)) {
3ea863a3 652 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
f5dd8adb 653 use_ucsoutput = FALSE;
a57cd64b 654 end = 2;
3ec1de33 655 special = TRUE;
a57cd64b 656 }
e8e8d6e2 657 /* For Shift Backspace, do opposite of what is configured. */
658 if (event->keyval == GDK_BackSpace &&
659 (event->state & GDK_SHIFT_MASK)) {
3ea863a3 660 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
f5dd8adb 661 use_ucsoutput = FALSE;
e8e8d6e2 662 end = 2;
3ec1de33 663 special = TRUE;
e8e8d6e2 664 }
a57cd64b 665
666 /* Shift-Tab is ESC [ Z */
667 if (event->keyval == GDK_ISO_Left_Tab ||
668 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
669 end = 1 + sprintf(output+1, "\033[Z");
f5dd8adb 670 use_ucsoutput = FALSE;
a57cd64b 671 }
672
673 /*
8b1fcd56 674 * NetHack keypad mode.
675 */
3ea863a3 676 if (inst->cfg.nethack_keypad) {
8b1fcd56 677 char *keys = NULL;
678 switch (event->keyval) {
679 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
680 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
681 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
682 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
683 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
684 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
685 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
686 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
687 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
688 }
689 if (keys) {
690 end = 2;
691 if (event->state & GDK_SHIFT_MASK)
692 output[1] = keys[1];
693 else
694 output[1] = keys[0];
f5dd8adb 695 use_ucsoutput = FALSE;
8b1fcd56 696 goto done;
697 }
698 }
699
700 /*
a57cd64b 701 * Application keypad mode.
702 */
3ea863a3 703 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
a57cd64b 704 int xkey = 0;
705 switch (event->keyval) {
706 case GDK_Num_Lock: xkey = 'P'; break;
707 case GDK_KP_Divide: xkey = 'Q'; break;
708 case GDK_KP_Multiply: xkey = 'R'; break;
709 case GDK_KP_Subtract: xkey = 'S'; break;
710 /*
711 * Keypad + is tricky. It covers a space that would
712 * be taken up on the VT100 by _two_ keys; so we
713 * let Shift select between the two. Worse still,
714 * in xterm function key mode we change which two...
715 */
716 case GDK_KP_Add:
5789bc94 717 if (inst->cfg.funky_type == FUNKY_XTERM) {
a57cd64b 718 if (event->state & GDK_SHIFT_MASK)
719 xkey = 'l';
720 else
721 xkey = 'k';
722 } else if (event->state & GDK_SHIFT_MASK)
723 xkey = 'm';
724 else
725 xkey = 'l';
726 break;
727 case GDK_KP_Enter: xkey = 'M'; break;
728 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
729 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
730 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
731 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
732 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
733 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
734 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
735 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
736 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
737 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
738 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
739 }
740 if (xkey) {
5def7522 741 if (inst->term->vt52_mode) {
a57cd64b 742 if (xkey >= 'P' && xkey <= 'S')
743 end = 1 + sprintf(output+1, "\033%c", xkey);
744 else
745 end = 1 + sprintf(output+1, "\033?%c", xkey);
746 } else
747 end = 1 + sprintf(output+1, "\033O%c", xkey);
f5dd8adb 748 use_ucsoutput = FALSE;
a57cd64b 749 goto done;
750 }
751 }
752
753 /*
754 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
755 * for integer decimal nn.)
756 *
757 * We also deal with the weird ones here. Linux VCs replace F1
758 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
759 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
760 * respectively.
761 */
762 {
763 int code = 0;
764 switch (event->keyval) {
765 case GDK_F1:
766 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
767 break;
768 case GDK_F2:
769 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
770 break;
771 case GDK_F3:
772 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
773 break;
774 case GDK_F4:
775 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
776 break;
777 case GDK_F5:
778 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
779 break;
780 case GDK_F6:
781 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
782 break;
783 case GDK_F7:
784 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
785 break;
786 case GDK_F8:
787 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
788 break;
789 case GDK_F9:
790 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
791 break;
792 case GDK_F10:
793 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
794 break;
795 case GDK_F11:
796 code = 23;
797 break;
798 case GDK_F12:
799 code = 24;
800 break;
801 case GDK_F13:
802 code = 25;
803 break;
804 case GDK_F14:
805 code = 26;
806 break;
807 case GDK_F15:
808 code = 28;
809 break;
810 case GDK_F16:
811 code = 29;
812 break;
813 case GDK_F17:
814 code = 31;
815 break;
816 case GDK_F18:
817 code = 32;
818 break;
819 case GDK_F19:
820 code = 33;
821 break;
822 case GDK_F20:
823 code = 34;
824 break;
825 }
826 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
827 case GDK_Home: case GDK_KP_Home:
828 code = 1;
829 break;
830 case GDK_Insert: case GDK_KP_Insert:
831 code = 2;
832 break;
833 case GDK_Delete: case GDK_KP_Delete:
834 code = 3;
835 break;
836 case GDK_End: case GDK_KP_End:
837 code = 4;
838 break;
839 case GDK_Page_Up: case GDK_KP_Page_Up:
840 code = 5;
841 break;
842 case GDK_Page_Down: case GDK_KP_Page_Down:
843 code = 6;
844 break;
845 }
846 /* Reorder edit keys to physical order */
5789bc94 847 if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
a57cd64b 848 code = "\0\2\1\4\5\3\6"[code];
849
5def7522 850 if (inst->term->vt52_mode && code > 0 && code <= 6) {
a57cd64b 851 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
f5dd8adb 852 use_ucsoutput = FALSE;
a57cd64b 853 goto done;
854 }
855
5789bc94 856 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
a57cd64b 857 code >= 11 && code <= 34) {
858 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
859 int index = 0;
860 switch (event->keyval) {
861 case GDK_F1: index = 0; break;
862 case GDK_F2: index = 1; break;
863 case GDK_F3: index = 2; break;
864 case GDK_F4: index = 3; break;
865 case GDK_F5: index = 4; break;
866 case GDK_F6: index = 5; break;
867 case GDK_F7: index = 6; break;
868 case GDK_F8: index = 7; break;
869 case GDK_F9: index = 8; break;
870 case GDK_F10: index = 9; break;
871 case GDK_F11: index = 10; break;
872 case GDK_F12: index = 11; break;
873 }
874 if (event->state & GDK_SHIFT_MASK) index += 12;
875 if (event->state & GDK_CONTROL_MASK) index += 24;
876 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
f5dd8adb 877 use_ucsoutput = FALSE;
a57cd64b 878 goto done;
879 }
5789bc94 880 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
a57cd64b 881 code >= 1 && code <= 6) {
882 char codes[] = "HL.FIG";
883 if (code == 3) {
884 output[1] = '\x7F';
885 end = 2;
886 } else {
887 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
888 }
f5dd8adb 889 use_ucsoutput = FALSE;
a57cd64b 890 goto done;
891 }
5789bc94 892 if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
887035a5 893 code >= 11 && code <= 24) {
a57cd64b 894 int offt = 0;
895 if (code > 15)
896 offt++;
897 if (code > 21)
898 offt++;
5def7522 899 if (inst->term->vt52_mode)
a57cd64b 900 end = 1 + sprintf(output+1,
901 "\x1B%c", code + 'P' - 11 - offt);
902 else
903 end = 1 + sprintf(output+1,
904 "\x1BO%c", code + 'P' - 11 - offt);
f5dd8adb 905 use_ucsoutput = FALSE;
a57cd64b 906 goto done;
907 }
5789bc94 908 if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
a57cd64b 909 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
f5dd8adb 910 use_ucsoutput = FALSE;
a57cd64b 911 goto done;
912 }
5789bc94 913 if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
5def7522 914 if (inst->term->vt52_mode)
a57cd64b 915 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
916 else
917 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
f5dd8adb 918 use_ucsoutput = FALSE;
a57cd64b 919 goto done;
920 }
3ea863a3 921 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
a57cd64b 922 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
f5dd8adb 923 use_ucsoutput = FALSE;
a57cd64b 924 goto done;
925 }
926 if (code) {
927 end = 1 + sprintf(output+1, "\x1B[%d~", code);
f5dd8adb 928 use_ucsoutput = FALSE;
a57cd64b 929 goto done;
930 }
931 }
932
933 /*
934 * Cursor keys. (This includes the numberpad cursor keys,
935 * if we haven't already done them due to app keypad mode.)
936 *
937 * Here we also process un-numlocked un-appkeypadded KP5,
938 * which sends ESC [ G.
939 */
940 {
941 int xkey = 0;
942 switch (event->keyval) {
943 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
944 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
945 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
946 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
947 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
948 }
949 if (xkey) {
950 /*
951 * The arrow keys normally do ESC [ A and so on. In
952 * app cursor keys mode they do ESC O A instead.
953 * Ctrl toggles the two modes.
954 */
5def7522 955 if (inst->term->vt52_mode) {
a57cd64b 956 end = 1 + sprintf(output+1, "\033%c", xkey);
5def7522 957 } else if (!inst->term->app_cursor_keys ^
a57cd64b 958 !(event->state & GDK_CONTROL_MASK)) {
959 end = 1 + sprintf(output+1, "\033O%c", xkey);
960 } else {
961 end = 1 + sprintf(output+1, "\033[%c", xkey);
962 }
f5dd8adb 963 use_ucsoutput = FALSE;
a57cd64b 964 goto done;
965 }
966 }
c33c3d76 967 goto done;
968 }
a57cd64b 969
c33c3d76 970 done:
a57cd64b 971
c33c3d76 972 if (end-start > 0) {
a57cd64b 973#ifdef KEY_DEBUGGING
c33c3d76 974 int i;
975 printf("generating sequence:");
976 for (i = start; i < end; i++)
977 printf(" %02x", (unsigned char) output[i]);
978 printf("\n");
a57cd64b 979#endif
c33c3d76 980
3ec1de33 981 if (special) {
982 /*
983 * For special control characters, the character set
984 * should never matter.
985 */
986 output[end] = '\0'; /* NUL-terminate */
e3be8de5 987 if (inst->ldisc)
988 ldisc_send(inst->ldisc, output+start, -2, 1);
3ec1de33 989 } else if (!inst->direct_to_font) {
f5dd8adb 990 if (!use_ucsoutput) {
991 /*
992 * The stuff we've just generated is assumed to be
993 * ISO-8859-1! This sounds insane, but `man
994 * XLookupString' agrees: strings of this type
995 * returned from the X server are hardcoded to
996 * 8859-1. Strictly speaking we should be doing
997 * this using some sort of GtkIMContext, which (if
998 * we're lucky) would give us our data directly in
999 * Unicode; but that's not supported in GTK 1.2 as
1000 * far as I can tell, and it's poorly documented
1001 * even in 2.0, so it'll have to wait.
1002 */
e3be8de5 1003 if (inst->ldisc)
1004 lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
1005 end-start, 1);
f5dd8adb 1006 } else {
1007 /*
1008 * We generated our own Unicode key data from the
1009 * keysym, so use that instead.
1010 */
e3be8de5 1011 if (inst->ldisc)
1012 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
f5dd8adb 1013 }
facd762c 1014 } else {
1015 /*
1016 * In direct-to-font mode, we just send the string
1017 * exactly as we received it.
1018 */
e3be8de5 1019 if (inst->ldisc)
1020 ldisc_send(inst->ldisc, output+start, end-start, 1);
facd762c 1021 }
2dc6356a 1022
a8327734 1023 show_mouseptr(inst, 0);
5def7522 1024 term_seen_key_event(inst->term);
1025 term_out(inst->term);
1709795f 1026 }
1027
1028 return TRUE;
f7f27309 1029}
1030
e6346999 1031gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1032{
1033 struct gui_data *inst = (struct gui_data *)data;
1034 int shift, ctrl, alt, x, y, button, act;
1035
d0a039b1 1036 /* Remember the timestamp. */
1037 inst->input_event_time = event->time;
1038
a8327734 1039 show_mouseptr(inst, 1);
57e636ca 1040
bab6e572 1041 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
5def7522 1042 term_scroll(inst->term, 0, -5);
bab6e572 1043 return TRUE;
1044 }
1045 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
5def7522 1046 term_scroll(inst->term, 0, +5);
bab6e572 1047 return TRUE;
1048 }
1049
e6346999 1050 shift = event->state & GDK_SHIFT_MASK;
1051 ctrl = event->state & GDK_CONTROL_MASK;
1052 alt = event->state & GDK_MOD1_MASK;
47e4e735 1053
1054 if (event->button == 3 && ctrl) {
1055 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1056 event->button, event->time);
1057 return TRUE;
1058 }
1059
e6346999 1060 if (event->button == 1)
1061 button = MBT_LEFT;
1062 else if (event->button == 2)
1063 button = MBT_MIDDLE;
1064 else if (event->button == 3)
1065 button = MBT_RIGHT;
1066 else
1067 return FALSE; /* don't even know what button! */
1068
1069 switch (event->type) {
1070 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1071 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1072 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1073 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1074 default: return FALSE; /* don't know this event type */
1075 }
1076
3ea863a3 1077 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
e6346999 1078 act != MA_CLICK && act != MA_RELEASE)
1079 return TRUE; /* we ignore these in raw mouse mode */
1080
3ea863a3 1081 x = (event->x - inst->cfg.window_border) / inst->font_width;
1082 y = (event->y - inst->cfg.window_border) / inst->font_height;
e6346999 1083
fc5b0934 1084 term_mouse(inst->term, button, translate_button(button), act,
1085 x, y, shift, ctrl, alt);
e6346999 1086
1087 return TRUE;
1088}
1089
1090gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1091{
1092 struct gui_data *inst = (struct gui_data *)data;
1093 int shift, ctrl, alt, x, y, button;
1094
d0a039b1 1095 /* Remember the timestamp. */
1096 inst->input_event_time = event->time;
1097
a8327734 1098 show_mouseptr(inst, 1);
57e636ca 1099
e6346999 1100 shift = event->state & GDK_SHIFT_MASK;
1101 ctrl = event->state & GDK_CONTROL_MASK;
1102 alt = event->state & GDK_MOD1_MASK;
1103 if (event->state & GDK_BUTTON1_MASK)
1104 button = MBT_LEFT;
1105 else if (event->state & GDK_BUTTON2_MASK)
1106 button = MBT_MIDDLE;
1107 else if (event->state & GDK_BUTTON3_MASK)
1108 button = MBT_RIGHT;
1109 else
1110 return FALSE; /* don't even know what button! */
1111
3ea863a3 1112 x = (event->x - inst->cfg.window_border) / inst->font_width;
1113 y = (event->y - inst->cfg.window_border) / inst->font_height;
e6346999 1114
fc5b0934 1115 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1116 x, y, shift, ctrl, alt);
e6346999 1117
1118 return TRUE;
1119}
1120
b9d7bcad 1121void frontend_keypress(void *handle)
90cfd8f4 1122{
b9d7bcad 1123 struct gui_data *inst = (struct gui_data *)handle;
1124
90cfd8f4 1125 /*
1126 * If our child process has exited but not closed, terminate on
1127 * any keypress.
1128 */
1129 if (inst->exited)
1130 exit(0);
1131}
1132
f7f27309 1133gint timer_func(gpointer data)
1134{
90cfd8f4 1135 struct gui_data *inst = (struct gui_data *)data;
74aca06d 1136 int exitcode;
a0e16eb1 1137
3f935d5b 1138 if (!inst->exited &&
1139 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
74aca06d 1140 inst->exited = TRUE;
1141 if (inst->cfg.close_on_exit == FORCE_ON ||
1142 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1143 exit(0); /* just go. */
e3be8de5 1144 if (inst->ldisc) {
1145 ldisc_free(inst->ldisc);
1146 inst->ldisc = NULL;
1147 }
1148 if (inst->back) {
1149 inst->back->free(inst->backhandle);
1150 inst->backhandle = NULL;
1151 inst->back = NULL;
1152 update_specials_menu(inst);
1153 }
1154 gtk_widget_show(inst->restartitem);
a0e16eb1 1155 }
f7f27309 1156
5def7522 1157 term_update(inst->term);
1158 term_blink(inst->term, 0);
f7f27309 1159 return TRUE;
1160}
1161
74aca06d 1162void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
054d8535 1163{
3f935d5b 1164 /*
1165 * We must process exceptional notifications before ordinary
1166 * readability ones, or we may go straight past the urgent
1167 * marker.
1168 */
1169 if (condition & GDK_INPUT_EXCEPTION)
1170 select_result(sourcefd, 4);
1171 if (condition & GDK_INPUT_READ)
1172 select_result(sourcefd, 1);
1173 if (condition & GDK_INPUT_WRITE)
1174 select_result(sourcefd, 2);
054d8535 1175}
1176
f7f27309 1177void destroy(GtkWidget *widget, gpointer data)
1178{
1179 gtk_main_quit();
1180}
1181
1182gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1183{
a8327734 1184 struct gui_data *inst = (struct gui_data *)data;
5def7522 1185 inst->term->has_focus = event->in;
1186 term_out(inst->term);
1187 term_update(inst->term);
a8327734 1188 show_mouseptr(inst, 1);
1709795f 1189 return FALSE;
1190}
1191
1192/*
1193 * set or clear the "raw mouse message" mode
1194 */
a8327734 1195void set_raw_mouse_mode(void *frontend, int activate)
1709795f 1196{
a8327734 1197 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1198 activate = activate && !inst->cfg.no_mouse_rep;
d64838e1 1199 send_raw_mouse = activate;
1200 if (send_raw_mouse)
57e636ca 1201 inst->currcursor = inst->rawcursor;
d64838e1 1202 else
57e636ca 1203 inst->currcursor = inst->textcursor;
a8327734 1204 show_mouseptr(inst, inst->mouseptr_visible);
1709795f 1205}
1206
a8327734 1207void request_resize(void *frontend, int w, int h)
1709795f 1208{
a8327734 1209 struct gui_data *inst = (struct gui_data *)frontend;
51b13830 1210 int large_x, large_y;
1211 int offset_x, offset_y;
1212 int area_x, area_y;
1213 GtkRequisition inner, outer;
1214
1215 /*
1216 * This is a heinous hack dreamed up by the gnome-terminal
1217 * people to get around a limitation in gtk. The problem is
1218 * that in order to set the size correctly we really need to be
1219 * calling gtk_window_resize - but that needs to know the size
1220 * of the _whole window_, not the drawing area. So what we do
1221 * is to set an artificially huge size request on the drawing
1222 * area, recompute the resulting size request on the window,
1223 * and look at the difference between the two. That gives us
1224 * the x and y offsets we need to translate drawing area size
1225 * into window size for real, and then we call
1226 * gtk_window_resize.
1227 */
1228
1229 /*
1230 * We start by retrieving the current size of the whole window.
1231 * Adding a bit to _that_ will give us a value we can use as a
1232 * bogus size request which guarantees to be bigger than the
1233 * current size of the drawing area.
1234 */
a8327734 1235 get_window_pixels(inst, &large_x, &large_y);
51b13830 1236 large_x += 32;
1237 large_y += 32;
1238
1239#if GTK_CHECK_VERSION(2,0,0)
1240 gtk_widget_set_size_request(inst->area, large_x, large_y);
1241#else
1242 gtk_widget_set_usize(inst->area, large_x, large_y);
1243#endif
1244 gtk_widget_size_request(inst->area, &inner);
1245 gtk_widget_size_request(inst->window, &outer);
1246
1247 offset_x = outer.width - inner.width;
1248 offset_y = outer.height - inner.height;
1249
3ea863a3 1250 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1251 area_y = inst->font_height * h + 2*inst->cfg.window_border;
51b13830 1252
1253 /*
1254 * Now we must set the size request on the drawing area back to
1255 * something sensible before we commit the real resize. Best
1256 * way to do this, I think, is to set it to what the size is
1257 * really going to end up being.
1258 */
1259#if GTK_CHECK_VERSION(2,0,0)
1260 gtk_widget_set_size_request(inst->area, area_x, area_y);
1261#else
1262 gtk_widget_set_usize(inst->area, area_x, area_y);
56b9b9a7 1263 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
51b13830 1264#endif
1265
56b9b9a7 1266 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1267
51b13830 1268#if GTK_CHECK_VERSION(2,0,0)
1269 gtk_window_resize(GTK_WINDOW(inst->window),
1270 area_x + offset_x, area_y + offset_y);
1271#else
1272 gdk_window_resize(inst->window->window,
1273 area_x + offset_x, area_y + offset_y);
1274#endif
1709795f 1275}
1276
a8327734 1277static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
26b8e7cc 1278{
1279 gboolean success[1];
1280
1281 inst->cols[n].red = r * 0x0101;
1282 inst->cols[n].green = g * 0x0101;
1283 inst->cols[n].blue = b * 0x0101;
1284
56b9b9a7 1285 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
26b8e7cc 1286 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1287 FALSE, FALSE, success);
1288 if (!success[0])
a4e8ffb0 1289 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
26b8e7cc 1290 n, r, g, b);
1291}
1292
a8327734 1293void set_window_background(struct gui_data *inst)
7428c509 1294{
1295 if (inst->area && inst->area->window)
1296 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1297 if (inst->window && inst->window->window)
1298 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1299}
1300
a8327734 1301void palette_set(void *frontend, int n, int r, int g, int b)
1709795f 1302{
a8327734 1303 struct gui_data *inst = (struct gui_data *)frontend;
26b8e7cc 1304 static const int first[21] = {
1305 0, 2, 4, 6, 8, 10, 12, 14,
1306 1, 3, 5, 7, 9, 11, 13, 15,
1307 16, 17, 18, 20, 22
1308 };
a8327734 1309 real_palette_set(inst, first[n], r, g, b);
26b8e7cc 1310 if (first[n] >= 18)
a8327734 1311 real_palette_set(inst, first[n] + 1, r, g, b);
7428c509 1312 if (first[n] == 18)
a8327734 1313 set_window_background(inst);
1709795f 1314}
26b8e7cc 1315
a8327734 1316void palette_reset(void *frontend)
1709795f 1317{
a8327734 1318 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1319 /* This maps colour indices in inst->cfg to those used in inst->cols. */
26b8e7cc 1320 static const int ww[] = {
1321 6, 7, 8, 9, 10, 11, 12, 13,
1322 14, 15, 16, 17, 18, 19, 20, 21,
1323 0, 1, 2, 3, 4, 5
1324 };
1325 gboolean success[NCOLOURS];
1326 int i;
1327
1328 assert(lenof(ww) == NCOLOURS);
1329
1330 if (!inst->colmap) {
1331 inst->colmap = gdk_colormap_get_system();
1332 } else {
1333 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1334 }
1335
1336 for (i = 0; i < NCOLOURS; i++) {
3ea863a3 1337 inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
1338 inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
1339 inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
26b8e7cc 1340 }
1341
1342 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1343 FALSE, FALSE, success);
1344 for (i = 0; i < NCOLOURS; i++) {
1345 if (!success[i])
a4e8ffb0 1346 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1347 appname, i, inst->cfg.colours[i][0],
1348 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
26b8e7cc 1349 }
7428c509 1350
a8327734 1351 set_window_background(inst);
1709795f 1352}
1353
3aac0d35 1354/* Ensure that all the cut buffers exist - according to the ICCCM, we must
1355 * do this before we start using cut buffers.
1356 */
1357void init_cutbuffers()
1358{
1359 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1360 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, "", 0);
1361 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1362 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, "", 0);
1363 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1364 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, "", 0);
1365 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1366 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, "", 0);
1367 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1368 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, "", 0);
1369 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1370 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, "", 0);
1371 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1372 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, "", 0);
1373 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1374 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, "", 0);
1375}
1376
1377/* Store the data in a cut-buffer. */
1378void store_cutbuffer(char * ptr, int len)
1379{
1380 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1381 XRotateBuffers(GDK_DISPLAY(), 1);
1382 XStoreBytes(GDK_DISPLAY(), ptr, len);
1383}
1384
1385/* Retrieve data from a cut-buffer.
1386 * Returned data needs to be freed with XFree().
1387 */
1388char * retrieve_cutbuffer(int * nbytes)
1389{
1390 char * ptr;
1391 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
05971030 1392 if (*nbytes <= 0 && ptr != 0) {
3aac0d35 1393 XFree(ptr);
1394 ptr = 0;
1395 }
1396 return ptr;
1397}
1398
a8327734 1399void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1709795f 1400{
a8327734 1401 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1402 if (inst->pasteout_data)
1403 sfree(inst->pasteout_data);
48f1bc44 1404 if (inst->pasteout_data_ctext)
1405 sfree(inst->pasteout_data_ctext);
2dc6356a 1406 if (inst->pasteout_data_utf8)
1407 sfree(inst->pasteout_data_utf8);
1408
facd762c 1409 /*
48f1bc44 1410 * Set up UTF-8 and compound text paste data. This only happens
1411 * if we aren't in direct-to-font mode using the D800 hack.
facd762c 1412 */
085f4a68 1413 if (!inst->direct_to_font) {
2dc6356a 1414 wchar_t *tmp = data;
1415 int tmplen = len;
48f1bc44 1416 XTextProperty tp;
1417 char *list[1];
facd762c 1418
3d88e64d 1419 inst->pasteout_data_utf8 = snewn(len*6, char);
facd762c 1420 inst->pasteout_data_utf8_len = len*6;
2dc6356a 1421 inst->pasteout_data_utf8_len =
1422 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1423 inst->pasteout_data_utf8_len,
1424 CS_UTF8, NULL, NULL, 0);
085f4a68 1425 if (inst->pasteout_data_utf8_len == 0) {
1426 sfree(inst->pasteout_data_utf8);
1427 inst->pasteout_data_utf8 = NULL;
1428 } else {
1429 inst->pasteout_data_utf8 =
3d88e64d 1430 sresize(inst->pasteout_data_utf8,
48f1bc44 1431 inst->pasteout_data_utf8_len + 1, char);
1432 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1433 }
1434
1435 /*
1436 * Now let Xlib convert our UTF-8 data into compound text.
1437 */
1438 list[0] = inst->pasteout_data_utf8;
1439 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1440 XCompoundTextStyle, &tp) == 0) {
1441 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1442 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1443 inst->pasteout_data_ctext_len = tp.nitems;
1444 XFree(tp.value);
d1929fa4 1445 } else {
1446 inst->pasteout_data_ctext = NULL;
1447 inst->pasteout_data_ctext_len = 0;
1448 }
facd762c 1449 } else {
1450 inst->pasteout_data_utf8 = NULL;
1451 inst->pasteout_data_utf8_len = 0;
48f1bc44 1452 inst->pasteout_data_ctext = NULL;
1453 inst->pasteout_data_ctext_len = 0;
2dc6356a 1454 }
1455
3d88e64d 1456 inst->pasteout_data = snewn(len*6, char);
085f4a68 1457 inst->pasteout_data_len = len*6;
21d2b241 1458 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1459 data, len, inst->pasteout_data,
1460 inst->pasteout_data_len,
1461 NULL, NULL, NULL);
085f4a68 1462 if (inst->pasteout_data_len == 0) {
1463 sfree(inst->pasteout_data);
1464 inst->pasteout_data = NULL;
1465 } else {
1466 inst->pasteout_data =
3d88e64d 1467 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
085f4a68 1468 }
e6346999 1469
3aac0d35 1470 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1471
e6346999 1472 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1473 inst->input_event_time)) {
e6346999 1474 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1475 GDK_SELECTION_TYPE_STRING, 1);
48f1bc44 1476 if (inst->pasteout_data_ctext)
1477 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1478 compound_text_atom, 1);
facd762c 1479 if (inst->pasteout_data_utf8)
1480 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
8c161662 1481 utf8_string_atom, 1);
e6346999 1482 }
c076966d 1483
1484 if (must_deselect)
1485 term_deselect(inst->term);
e6346999 1486}
1487
1488void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1489 guint info, guint time_stamp, gpointer data)
1490{
a8327734 1491 struct gui_data *inst = (struct gui_data *)data;
8c161662 1492 if (seldata->target == utf8_string_atom)
2dc6356a 1493 gtk_selection_data_set(seldata, seldata->target, 8,
1494 inst->pasteout_data_utf8,
1495 inst->pasteout_data_utf8_len);
48f1bc44 1496 else if (seldata->target == compound_text_atom)
1497 gtk_selection_data_set(seldata, seldata->target, 8,
1498 inst->pasteout_data_ctext,
1499 inst->pasteout_data_ctext_len);
2dc6356a 1500 else
1501 gtk_selection_data_set(seldata, seldata->target, 8,
1502 inst->pasteout_data, inst->pasteout_data_len);
e6346999 1503}
1504
1505gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1506 gpointer data)
1507{
a8327734 1508 struct gui_data *inst = (struct gui_data *)data;
d0a039b1 1509
5def7522 1510 term_deselect(inst->term);
e6346999 1511 if (inst->pasteout_data)
1512 sfree(inst->pasteout_data);
48f1bc44 1513 if (inst->pasteout_data_ctext)
1514 sfree(inst->pasteout_data_ctext);
2dc6356a 1515 if (inst->pasteout_data_utf8)
1516 sfree(inst->pasteout_data_utf8);
e6346999 1517 inst->pasteout_data = NULL;
1518 inst->pasteout_data_len = 0;
48f1bc44 1519 inst->pasteout_data_ctext = NULL;
1520 inst->pasteout_data_ctext_len = 0;
2dc6356a 1521 inst->pasteout_data_utf8 = NULL;
1522 inst->pasteout_data_utf8_len = 0;
e6346999 1523 return TRUE;
1524}
1525
a8327734 1526void request_paste(void *frontend)
e6346999 1527{
a8327734 1528 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1529 /*
1530 * In Unix, pasting is asynchronous: all we can do at the
1531 * moment is to call gtk_selection_convert(), and when the data
1532 * comes back _then_ we can call term_do_paste().
1533 */
2dc6356a 1534
085f4a68 1535 if (!inst->direct_to_font) {
facd762c 1536 /*
1537 * First we attempt to retrieve the selection as a UTF-8
1538 * string (which we will convert to the correct code page
1539 * before sending to the session, of course). If that
1540 * fails, selection_received() will be informed and will
1541 * fall back to an ordinary string.
1542 */
1543 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1544 utf8_string_atom,
1545 inst->input_event_time);
facd762c 1546 } else {
1547 /*
1548 * If we're in direct-to-font mode, we disable UTF-8
1549 * pasting, and go straight to ordinary string data.
1550 */
1551 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1552 GDK_SELECTION_TYPE_STRING,
1553 inst->input_event_time);
facd762c 1554 }
e6346999 1555}
1556
0f660c8f 1557gint idle_paste_func(gpointer data); /* forward ref */
1558
e6346999 1559void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
27651070 1560 guint time, gpointer data)
e6346999 1561{
a8327734 1562 struct gui_data *inst = (struct gui_data *)data;
48f1bc44 1563 XTextProperty tp;
1564 char **list;
1565 char *text;
1566 int length, count, ret;
1567 int free_list_required = 0;
3aac0d35 1568 int free_required = 0;
48f1bc44 1569 int charset;
a8327734 1570
8c161662 1571 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
2dc6356a 1572 /*
eaa07514 1573 * Failed to get a UTF-8 selection string. Try compound
1574 * text next.
1575 */
1576 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1577 compound_text_atom,
1578 inst->input_event_time);
eaa07514 1579 return;
1580 }
1581
1582 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1583 /*
1584 * Failed to get UTF-8 or compound text. Try an ordinary
2dc6356a 1585 * string.
1586 */
1587 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
d0a039b1 1588 GDK_SELECTION_TYPE_STRING,
1589 inst->input_event_time);
2dc6356a 1590 return;
1591 }
1592
1593 /*
3aac0d35 1594 * If we have data, but it's not of a type we can deal with,
1595 * we have to ignore the data.
2dc6356a 1596 */
3aac0d35 1597 if (seldata->length > 0 &&
1598 seldata->type != GDK_SELECTION_TYPE_STRING &&
1599 seldata->type != compound_text_atom &&
1600 seldata->type != utf8_string_atom)
1601 return;
48f1bc44 1602
1603 /*
3aac0d35 1604 * If we have no data, try looking in a cut buffer.
1605 */
1606 if (seldata->length <= 0) {
1607 text = retrieve_cutbuffer(&length);
1608 if (length == 0)
1609 return;
1610 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1611 * source, so use that as a de-facto standard. */
1612 charset = CS_ISO8859_1;
1613 free_required = 1;
1614 } else {
76129c71 1615 /*
1616 * Convert COMPOUND_TEXT into UTF-8.
1617 */
1618 if (seldata->type == compound_text_atom) {
1619 tp.value = seldata->data;
1620 tp.encoding = (Atom) seldata->type;
1621 tp.format = seldata->format;
1622 tp.nitems = seldata->length;
1623 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1624 &list, &count);
1625 if (ret != 0 || count != 1) {
1626 /*
1627 * Compound text failed; fall back to STRING.
1628 */
1629 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1630 GDK_SELECTION_TYPE_STRING,
1631 inst->input_event_time);
1632 return;
1633 }
1634 text = list[0];
1635 length = strlen(list[0]);
1636 charset = CS_UTF8;
1637 free_list_required = 1;
1638 } else {
1639 text = (char *)seldata->data;
1640 length = seldata->length;
1641 charset = (seldata->type == utf8_string_atom ?
1642 CS_UTF8 : inst->ucsdata.line_codepage);
48f1bc44 1643 }
3aac0d35 1644 }
48f1bc44 1645
e6346999 1646 if (inst->pastein_data)
1647 sfree(inst->pastein_data);
1648
48f1bc44 1649 inst->pastein_data = snewn(length, wchar_t);
1650 inst->pastein_data_len = length;
2dc6356a 1651 inst->pastein_data_len =
48f1bc44 1652 mb_to_wc(charset, 0, text, length,
2dc6356a 1653 inst->pastein_data, inst->pastein_data_len);
e6346999 1654
5def7522 1655 term_do_paste(inst->term);
0f660c8f 1656
5def7522 1657 if (term_paste_pending(inst->term))
0f660c8f 1658 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
48f1bc44 1659
1660 if (free_list_required)
1661 XFreeStringList(list);
3aac0d35 1662 if (free_required)
1663 XFree(text);
0f660c8f 1664}
1665
1666gint idle_paste_func(gpointer data)
1667{
1668 struct gui_data *inst = (struct gui_data *)data;
1669
5def7522 1670 if (term_paste_pending(inst->term))
1671 term_paste(inst->term);
0f660c8f 1672 else
1673 gtk_idle_remove(inst->term_paste_idle_id);
1674
1675 return TRUE;
1709795f 1676}
1677
0f660c8f 1678
a8327734 1679void get_clip(void *frontend, wchar_t ** p, int *len)
1709795f 1680{
a8327734 1681 struct gui_data *inst = (struct gui_data *)frontend;
1682
1709795f 1683 if (p) {
e6346999 1684 *p = inst->pastein_data;
1685 *len = inst->pastein_data_len;
1709795f 1686 }
1687}
1688
6e785e0c 1689static void set_window_titles(struct gui_data *inst)
1690{
1691 /*
1692 * We must always call set_icon_name after calling set_title,
1693 * since set_title will write both names. Irritating, but such
1694 * is life.
1695 */
1696 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1697 if (!inst->cfg.win_name_always)
1698 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1699}
1700
a8327734 1701void set_title(void *frontend, char *title)
1709795f 1702{
a8327734 1703 struct gui_data *inst = (struct gui_data *)frontend;
12d200b1 1704 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1705 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
6e785e0c 1706 set_window_titles(inst);
1709795f 1707}
1708
a8327734 1709void set_icon(void *frontend, char *title)
1709795f 1710{
a8327734 1711 struct gui_data *inst = (struct gui_data *)frontend;
16891265 1712 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1713 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
6e785e0c 1714 set_window_titles(inst);
1709795f 1715}
1716
a8327734 1717void set_sbar(void *frontend, int total, int start, int page)
1709795f 1718{
a8327734 1719 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1720 if (!inst->cfg.scrollbar)
330f49be 1721 return;
6a5e84dd 1722 inst->sbar_adjust->lower = 0;
1723 inst->sbar_adjust->upper = total;
1724 inst->sbar_adjust->value = start;
1725 inst->sbar_adjust->page_size = page;
1726 inst->sbar_adjust->step_increment = 1;
1727 inst->sbar_adjust->page_increment = page/2;
88e6b9ca 1728 inst->ignore_sbar = TRUE;
6a5e84dd 1729 gtk_adjustment_changed(inst->sbar_adjust);
88e6b9ca 1730 inst->ignore_sbar = FALSE;
6a5e84dd 1731}
1732
1733void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1734{
a8327734 1735 struct gui_data *inst = (struct gui_data *)data;
1736
3ea863a3 1737 if (!inst->cfg.scrollbar)
330f49be 1738 return;
88e6b9ca 1739 if (!inst->ignore_sbar)
5def7522 1740 term_scroll(inst->term, 1, (int)adj->value);
1709795f 1741}
1742
a8327734 1743void sys_cursor(void *frontend, int x, int y)
1709795f 1744{
1745 /*
1746 * This is meaningless under X.
1747 */
1748}
1749
0ce89525 1750/*
1751 * This is still called when mode==BELL_VISUAL, even though the
1752 * visual bell is handled entirely within terminal.c, because we
1753 * may want to perform additional actions on any kind of bell (for
1754 * example, taskbar flashing in Windows).
1755 */
a8327734 1756void beep(void *frontend, int mode)
1709795f 1757{
0ce89525 1758 if (mode != BELL_VISUAL)
1759 gdk_beep();
1709795f 1760}
1761
2102eb8a 1762int char_width(Context ctx, int uc)
1709795f 1763{
1764 /*
1765 * Under X, any fixed-width font really _is_ fixed-width.
1766 * Double-width characters will be dealt with using a separate
1767 * font. For the moment we can simply return 1.
1768 */
1769 return 1;
1770}
1771
a8327734 1772Context get_ctx(void *frontend)
1709795f 1773{
a8327734 1774 struct gui_data *inst = (struct gui_data *)frontend;
1775 struct draw_ctx *dctx;
1776
d64838e1 1777 if (!inst->area->window)
1778 return NULL;
a8327734 1779
3d88e64d 1780 dctx = snew(struct draw_ctx);
a8327734 1781 dctx->inst = inst;
1782 dctx->gc = gdk_gc_new(inst->area->window);
1783 return dctx;
1709795f 1784}
1785
1786void free_ctx(Context ctx)
1787{
a8327734 1788 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1789 /* struct gui_data *inst = dctx->inst; */
1790 GdkGC *gc = dctx->gc;
1709795f 1791 gdk_gc_unref(gc);
a8327734 1792 sfree(dctx);
1709795f 1793}
1794
1795/*
1796 * Draw a line of text in the window, at given character
1797 * coordinates, in given attributes.
1798 *
1799 * We are allowed to fiddle with the contents of `text'.
1800 */
36566009 1801void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1a675941 1802 unsigned long attr, int lattr)
1709795f 1803{
a8327734 1804 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1805 struct gui_data *inst = dctx->inst;
1806 GdkGC *gc = dctx->gc;
c6958dfe 1807 int ncombining, combining;
006238cb 1808 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
d64838e1 1809
c6958dfe 1810 if (attr & TATTR_COMBINING) {
1811 ncombining = len;
1812 len = 1;
1813 } else
1814 ncombining = 1;
1815
37ca32ed 1816 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1817 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
1818 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1819 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
d64838e1 1820 if (attr & ATTR_REVERSE) {
1821 t = nfg;
1822 nfg = nbg;
1823 nbg = t;
1824 }
3ea863a3 1825 if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
37ca32ed 1826 nfg |= 1;
3ea863a3 1827 if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
37ca32ed 1828 nbg |= 1;
d64838e1 1829 if (attr & TATTR_ACTCURS) {
1830 nfg = NCOLOURS-2;
1831 nbg = NCOLOURS-1;
1832 }
1833
5bf50ab2 1834 fontid = shadow = 0;
006238cb 1835
1836 if (attr & ATTR_WIDE) {
1837 widefactor = 2;
1838 fontid |= 2;
1839 } else {
1840 widefactor = 1;
1841 }
1842
3ea863a3 1843 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
006238cb 1844 if (inst->fonts[fontid | 1])
1845 fontid |= 1;
5bf50ab2 1846 else
1847 shadow = 1;
1848 }
1849
36566009 1850 if ((lattr & LATTR_MODE) != LATTR_NORM) {
f34df346 1851 x *= 2;
5def7522 1852 if (x >= inst->term->cols)
614bb468 1853 return;
006238cb 1854 if (x + len*2*widefactor > inst->term->cols)
1855 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
623f81b7 1856 rlen = len * 2;
1857 } else
1858 rlen = len;
1859
1860 {
1861 GdkRectangle r;
1862
3ea863a3 1863 r.x = x*inst->font_width+inst->cfg.window_border;
1864 r.y = y*inst->font_height+inst->cfg.window_border;
006238cb 1865 r.width = rlen*widefactor*inst->font_width;
623f81b7 1866 r.height = inst->font_height;
1867 gdk_gc_set_clip_rectangle(gc, &r);
f34df346 1868 }
1869
d64838e1 1870 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
83616aab 1871 gdk_draw_rectangle(inst->pixmap, gc, 1,
3ea863a3 1872 x*inst->font_width+inst->cfg.window_border,
1873 y*inst->font_height+inst->cfg.window_border,
006238cb 1874 rlen*widefactor*inst->font_width, inst->font_height);
6a5e84dd 1875
d64838e1 1876 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2dc6356a 1877 {
1878 GdkWChar *gwcs;
1879 gchar *gcs;
1880 wchar_t *wcs;
1881 int i;
1882
c6958dfe 1883 wcs = snewn(len*ncombining+1, wchar_t);
1884 for (i = 0; i < len*ncombining; i++) {
36566009 1885 wcs[i] = text[i];
2dc6356a 1886 }
1887
006238cb 1888 if (inst->fonts[fontid] == NULL) {
1889 /*
1890 * The font for this contingency does not exist.
1891 * Typically this means we've been given ATTR_WIDE
1892 * character and have no wide font. So we display
1893 * nothing at all; such is life.
1894 */
1895 } else if (inst->fontinfo[fontid].is_wide) {
aa153a7f 1896 /*
1897 * At least one version of gdk_draw_text_wc() has a
1898 * weird bug whereby it reads `len' elements of the
1899 * input string, but only draws `len/2'. Hence I'm
1900 * going to make its input array twice as long as it
1901 * theoretically needs to be, and pass in twice the
1902 * actual number of characters. If a fixed gdk actually
1903 * takes the doubled length seriously, then (a) the
1904 * array will stand scrutiny up to the full length, (b)
1905 * the spare elements of the array are full of zeroes
1906 * which will probably be an empty glyph in the font,
1907 * and (c) the clip rectangle should prevent it causing
1908 * trouble anyway.
1909 */
3d88e64d 1910 gwcs = snewn(len*2+1, GdkWChar);
aa153a7f 1911 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2dc6356a 1912 /*
1913 * FIXME: when we have a wide-char equivalent of
1914 * from_unicode, use it instead of this.
1915 */
c6958dfe 1916 for (combining = 0; combining < ncombining; combining++) {
1917 for (i = 0; i <= len; i++)
1918 gwcs[i] = wcs[i + combining];
36566009 1919 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
c6958dfe 1920 x*inst->font_width+inst->cfg.window_border,
36566009 1921 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1922 gwcs, len*2);
c6958dfe 1923 if (shadow)
1924 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
1925 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
1926 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1927 gwcs, len*2);
1928 }
2dc6356a 1929 sfree(gwcs);
1930 } else {
3d88e64d 1931 gcs = snewn(len+1, gchar);
c6958dfe 1932 for (combining = 0; combining < ncombining; combining++) {
1933 wc_to_mb(inst->fontinfo[fontid].charset, 0,
1934 wcs + combining, len, gcs, len, ".", NULL, NULL);
36566009 1935 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
c6958dfe 1936 x*inst->font_width+inst->cfg.window_border,
36566009 1937 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1938 gcs, len);
c6958dfe 1939 if (shadow)
1940 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1941 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
1942 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
1943 gcs, len);
1944 }
2dc6356a 1945 sfree(gcs);
1946 }
1947 sfree(wcs);
1948 }
d64838e1 1949
1950 if (attr & ATTR_UNDER) {
1951 int uheight = inst->fonts[0]->ascent + 1;
83616aab 1952 if (uheight >= inst->font_height)
1953 uheight = inst->font_height - 1;
3ea863a3 1954 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
1955 y*inst->font_height + uheight + inst->cfg.window_border,
1956 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
1957 y*inst->font_height + uheight + inst->cfg.window_border);
d64838e1 1958 }
1959
36566009 1960 if ((lattr & LATTR_MODE) != LATTR_NORM) {
f34df346 1961 /*
1962 * I can't find any plausible StretchBlt equivalent in the
1963 * X server, so I'm going to do this the slow and painful
1964 * way. This will involve repeated calls to
1965 * gdk_draw_pixmap() to stretch the text horizontally. It's
1966 * O(N^2) in time and O(N) in network bandwidth, but you
1967 * try thinking of a better way. :-(
1968 */
1969 int i;
006238cb 1970 for (i = 0; i < len * widefactor * inst->font_width; i++) {
f34df346 1971 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 1972 x*inst->font_width+inst->cfg.window_border + 2*i,
1973 y*inst->font_height+inst->cfg.window_border,
1974 x*inst->font_width+inst->cfg.window_border + 2*i+1,
1975 y*inst->font_height+inst->cfg.window_border,
7a216d7f 1976 len * widefactor * inst->font_width - i, inst->font_height);
f34df346 1977 }
1978 len *= 2;
36566009 1979 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
f34df346 1980 int dt, db;
1981 /* Now stretch vertically, in the same way. */
36566009 1982 if ((lattr & LATTR_MODE) == LATTR_BOT)
f34df346 1983 dt = 0, db = 1;
1984 else
1985 dt = 1, db = 0;
1986 for (i = 0; i < inst->font_height; i+=2) {
1987 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 1988 x*inst->font_width+inst->cfg.window_border,
1989 y*inst->font_height+inst->cfg.window_border+dt*i+db,
7a216d7f 1990 x*inst->font_width+inst->cfg.window_border,
3ea863a3 1991 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
7a216d7f 1992 len * widefactor * inst->font_width, inst->font_height-i-1);
f34df346 1993 }
1994 }
1a675941 1995 }
1996}
1997
36566009 1998void do_text(Context ctx, int x, int y, wchar_t *text, int len,
1a675941 1999 unsigned long attr, int lattr)
2000{
a8327734 2001 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2002 struct gui_data *inst = dctx->inst;
2003 GdkGC *gc = dctx->gc;
006238cb 2004 int widefactor;
1a675941 2005
2006 do_text_internal(ctx, x, y, text, len, attr, lattr);
2007
006238cb 2008 if (attr & ATTR_WIDE) {
2009 widefactor = 2;
2010 } else {
2011 widefactor = 1;
2012 }
2013
36566009 2014 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1a675941 2015 x *= 2;
5def7522 2016 if (x >= inst->term->cols)
1a675941 2017 return;
006238cb 2018 if (x + len*2*widefactor > inst->term->cols)
2019 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
f34df346 2020 len *= 2;
2021 }
2022
d64838e1 2023 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 2024 x*inst->font_width+inst->cfg.window_border,
2025 y*inst->font_height+inst->cfg.window_border,
2026 x*inst->font_width+inst->cfg.window_border,
2027 y*inst->font_height+inst->cfg.window_border,
006238cb 2028 len*widefactor*inst->font_width, inst->font_height);
1709795f 2029}
2030
36566009 2031void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
1709795f 2032 unsigned long attr, int lattr)
2033{
a8327734 2034 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2035 struct gui_data *inst = dctx->inst;
2036 GdkGC *gc = dctx->gc;
2037
a6db1e95 2038 int active, passive, widefactor;
d64838e1 2039
1709795f 2040 if (attr & TATTR_PASCURS) {
2041 attr &= ~TATTR_PASCURS;
d64838e1 2042 passive = 1;
2043 } else
2044 passive = 0;
3ea863a3 2045 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
1a675941 2046 attr &= ~TATTR_ACTCURS;
a6db1e95 2047 active = 1;
2048 } else
2049 active = 0;
1a675941 2050 do_text_internal(ctx, x, y, text, len, attr, lattr);
2051
ac059c2e 2052 if (attr & TATTR_COMBINING)
2053 len = 1;
2054
006238cb 2055 if (attr & ATTR_WIDE) {
2056 widefactor = 2;
2057 } else {
2058 widefactor = 1;
2059 }
2060
36566009 2061 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1a675941 2062 x *= 2;
5def7522 2063 if (x >= inst->term->cols)
1a675941 2064 return;
006238cb 2065 if (x + len*2*widefactor > inst->term->cols)
2066 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1a675941 2067 len *= 2;
2068 }
2069
3ea863a3 2070 if (inst->cfg.cursor_type == 0) {
1a675941 2071 /*
2072 * An active block cursor will already have been done by
2073 * the above do_text call, so we only need to do anything
2074 * if it's passive.
2075 */
2076 if (passive) {
2077 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
2078 gdk_draw_rectangle(inst->pixmap, gc, 0,
3ea863a3 2079 x*inst->font_width+inst->cfg.window_border,
2080 y*inst->font_height+inst->cfg.window_border,
35100bc3 2081 len*widefactor*inst->font_width-1, inst->font_height-1);
1a675941 2082 }
2083 } else {
2084 int uheight;
2085 int startx, starty, dx, dy, length, i;
2086
2087 int char_width;
2088
36566009 2089 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
1a675941 2090 char_width = 2*inst->font_width;
2091 else
2092 char_width = inst->font_width;
2093
3ea863a3 2094 if (inst->cfg.cursor_type == 1) {
1a675941 2095 uheight = inst->fonts[0]->ascent + 1;
2096 if (uheight >= inst->font_height)
2097 uheight = inst->font_height - 1;
2098
3ea863a3 2099 startx = x * inst->font_width + inst->cfg.window_border;
2100 starty = y * inst->font_height + inst->cfg.window_border + uheight;
1a675941 2101 dx = 1;
2102 dy = 0;
35100bc3 2103 length = len * widefactor * char_width;
1a675941 2104 } else {
2105 int xadjust = 0;
2106 if (attr & TATTR_RIGHTCURS)
2107 xadjust = char_width - 1;
3ea863a3 2108 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2109 starty = y * inst->font_height + inst->cfg.window_border;
1a675941 2110 dx = 0;
2111 dy = 1;
2112 length = inst->font_height;
2113 }
2114
d64838e1 2115 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1a675941 2116 if (passive) {
2117 for (i = 0; i < length; i++) {
2118 if (i % 2 == 0) {
2119 gdk_draw_point(inst->pixmap, gc, startx, starty);
2120 }
2121 startx += dx;
2122 starty += dy;
2123 }
a6db1e95 2124 } else if (active) {
1a675941 2125 gdk_draw_line(inst->pixmap, gc, startx, starty,
2126 startx + (length-1) * dx, starty + (length-1) * dy);
a6db1e95 2127 } /* else no cursor (e.g., blinked off) */
d64838e1 2128 }
1a675941 2129
2130 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 2131 x*inst->font_width+inst->cfg.window_border,
2132 y*inst->font_height+inst->cfg.window_border,
2133 x*inst->font_width+inst->cfg.window_border,
2134 y*inst->font_height+inst->cfg.window_border,
006238cb 2135 len*widefactor*inst->font_width, inst->font_height);
1709795f 2136}
2137
a8327734 2138GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
7798fa56 2139{
2140 /*
2141 * Truly hideous hack: GTK doesn't allow us to set the mouse
2142 * cursor foreground and background colours unless we've _also_
2143 * created our own cursor from bitmaps. Therefore, I need to
2144 * load the `cursor' font and draw glyphs from it on to
2145 * pixmaps, in order to construct my cursors with the fg and bg
2146 * I want. This is a gross hack, but it's more self-contained
2147 * than linking in Xlib to find the X window handle to
2148 * inst->area and calling XRecolorCursor, and it's more
2149 * futureproof than hard-coding the shapes as bitmap arrays.
2150 */
2151 static GdkFont *cursor_font = NULL;
2152 GdkPixmap *source, *mask;
2153 GdkGC *gc;
2154 GdkColor cfg = { 0, 65535, 65535, 65535 };
2155 GdkColor cbg = { 0, 0, 0, 0 };
2156 GdkColor dfg = { 1, 65535, 65535, 65535 };
2157 GdkColor dbg = { 0, 0, 0, 0 };
2158 GdkCursor *ret;
2159 gchar text[2];
2160 gint lb, rb, wid, asc, desc, w, h, x, y;
2161
57e636ca 2162 if (cursor_val == -2) {
7798fa56 2163 gdk_font_unref(cursor_font);
2164 return NULL;
2165 }
2166
57e636ca 2167 if (cursor_val >= 0 && !cursor_font)
7798fa56 2168 cursor_font = gdk_font_load("cursor");
2169
2170 /*
2171 * Get the text extent of the cursor in question. We use the
2172 * mask character for this, because it's typically slightly
2173 * bigger than the main character.
2174 */
57e636ca 2175 if (cursor_val >= 0) {
2176 text[1] = '\0';
2177 text[0] = (char)cursor_val + 1;
2178 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2179 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2180 } else {
2181 w = h = 1;
2182 x = y = 0;
2183 }
7798fa56 2184
2185 source = gdk_pixmap_new(NULL, w, h, 1);
2186 mask = gdk_pixmap_new(NULL, w, h, 1);
2187
2188 /*
2189 * Draw the mask character on the mask pixmap.
2190 */
7798fa56 2191 gc = gdk_gc_new(mask);
2192 gdk_gc_set_foreground(gc, &dbg);
2193 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
57e636ca 2194 if (cursor_val >= 0) {
2195 text[1] = '\0';
2196 text[0] = (char)cursor_val + 1;
2197 gdk_gc_set_foreground(gc, &dfg);
2198 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2199 }
7798fa56 2200 gdk_gc_unref(gc);
2201
2202 /*
2203 * Draw the main character on the source pixmap.
2204 */
7798fa56 2205 gc = gdk_gc_new(source);
2206 gdk_gc_set_foreground(gc, &dbg);
2207 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
57e636ca 2208 if (cursor_val >= 0) {
2209 text[1] = '\0';
2210 text[0] = (char)cursor_val;
2211 gdk_gc_set_foreground(gc, &dfg);
2212 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2213 }
7798fa56 2214 gdk_gc_unref(gc);
2215
2216 /*
2217 * Create the cursor.
2218 */
2219 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2220
2221 /*
2222 * Clean up.
2223 */
2224 gdk_pixmap_unref(source);
2225 gdk_pixmap_unref(mask);
2226
2227 return ret;
2228}
2229
1709795f 2230void modalfatalbox(char *p, ...)
2231{
2232 va_list ap;
2233 fprintf(stderr, "FATAL ERROR: ");
2234 va_start(ap, p);
2235 vfprintf(stderr, p, ap);
2236 va_end(ap);
2237 fputc('\n', stderr);
2238 exit(1);
f7f27309 2239}
2240
46a3419b 2241void cmdline_error(char *p, ...)
2242{
2243 va_list ap;
a4e8ffb0 2244 fprintf(stderr, "%s: ", appname);
46a3419b 2245 va_start(ap, p);
2246 vfprintf(stderr, p, ap);
2247 va_end(ap);
2248 fputc('\n', stderr);
2249 exit(1);
2250}
2251
a8327734 2252char *get_x_display(void *frontend)
755a6d84 2253{
2254 return gdk_get_display();
2255}
2256
7af753e6 2257long get_windowid(void *frontend)
2258{
fbf6cb3b 2259 struct gui_data *inst = (struct gui_data *)frontend;
7af753e6 2260 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2261}
2262
96add444 2263static void help(FILE *fp) {
2264 if(fprintf(fp,
2265"pterm option summary:\n"
2266"\n"
2267" --display DISPLAY Specify X display to use (note '--')\n"
2268" -name PREFIX Prefix when looking up resources (default: pterm)\n"
2269" -fn FONT Normal text font\n"
2270" -fb FONT Bold text font\n"
aca2a702 2271" -geometry GEOMETRY Position and size of window (size in characters)\n"
96add444 2272" -sl LINES Number of lines of scrollback\n"
2273" -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2274" -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2275" -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2276" -T TITLE Window title\n"
2277" -ut, +ut Do(default) or do not update utmp\n"
2278" -ls, +ls Do(default) or do not make shell a login shell\n"
2279" -sb, +sb Do(default) or do not display a scrollbar\n"
2280" -log PATH Log all output to a file\n"
2281" -nethack Map numeric keypad to hjklyubn direction keys\n"
2282" -xrm RESOURCE-STRING Set an X resource\n"
2283" -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2284 ) < 0 || fflush(fp) < 0) {
2285 perror("output error");
2286 exit(1);
2287 }
2288}
2289
aca2a702 2290int do_cmdline(int argc, char **argv, int do_everything,
2291 struct gui_data *inst, Config *cfg)
f7f27309 2292{
6169c758 2293 int err = 0;
56801e3d 2294 char *val;
f7f27309 2295
faec60ed 2296 /*
b89ee4f3 2297 * Macros to make argument handling easier. Note that because
2298 * they need to call `continue', they cannot be contained in
2299 * the usual do {...} while (0) wrapper to make them
2300 * syntactically single statements; hence it is not legal to
2301 * use one of these macros as an unbraced statement between
2302 * `if' and `else'.
faec60ed 2303 */
b89ee4f3 2304#define EXPECTS_ARG { \
faec60ed 2305 if (--argc <= 0) { \
2306 err = 1; \
a4e8ffb0 2307 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
b89ee4f3 2308 continue; \
faec60ed 2309 } else \
2310 val = *++argv; \
b89ee4f3 2311}
2312#define SECOND_PASS_ONLY { if (!do_everything) continue; }
faec60ed 2313
6169c758 2314 while (--argc > 0) {
2315 char *p = *++argv;
46a3419b 2316 int ret;
2317
fb9bf82f 2318 /*
2319 * Shameless cheating. Debian requires all X terminal
2320 * emulators to support `-T title'; but
2321 * cmdline_process_param will eat -T (it means no-pty) and
2322 * complain that pterm doesn't support it. So, in pterm
2323 * only, we convert -T into -title.
2324 */
2325 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2326 !strcmp(p, "-T"))
2327 p = "-title";
2328
46a3419b 2329 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2330 do_everything ? 1 : -1, cfg);
2331
2332 if (ret == -2) {
2333 cmdline_error("option \"%s\" requires an argument", p);
2334 } else if (ret == 2) {
2335 --argc, ++argv; /* skip next argument */
2336 continue;
2337 } else if (ret == 1) {
2338 continue;
2339 }
2340
8e20db05 2341 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
faec60ed 2342 EXPECTS_ARG;
2343 SECOND_PASS_ONLY;
9a30e26b 2344 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2345 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
faec60ed 2346
2347 } else if (!strcmp(p, "-fb")) {
2348 EXPECTS_ARG;
2349 SECOND_PASS_ONLY;
9a30e26b 2350 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2351 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
faec60ed 2352
006238cb 2353 } else if (!strcmp(p, "-fw")) {
2354 EXPECTS_ARG;
2355 SECOND_PASS_ONLY;
9a30e26b 2356 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2357 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
006238cb 2358
2359 } else if (!strcmp(p, "-fwb")) {
2360 EXPECTS_ARG;
2361 SECOND_PASS_ONLY;
9a30e26b 2362 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2363 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
006238cb 2364
2dc6356a 2365 } else if (!strcmp(p, "-cs")) {
2366 EXPECTS_ARG;
2367 SECOND_PASS_ONLY;
3ea863a3 2368 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2369 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2dc6356a 2370
8e20db05 2371 } else if (!strcmp(p, "-geometry")) {
2372 int flags, x, y, w, h;
2373 EXPECTS_ARG;
2374 SECOND_PASS_ONLY;
2375
2376 flags = XParseGeometry(val, &x, &y, &w, &h);
2377 if (flags & WidthValue)
3ea863a3 2378 cfg->width = w;
8e20db05 2379 if (flags & HeightValue)
3ea863a3 2380 cfg->height = h;
8e20db05 2381
aca2a702 2382 if (flags & (XValue | YValue)) {
2383 inst->xpos = x;
2384 inst->ypos = y;
2385 inst->gotpos = TRUE;
2386 inst->gravity = ((flags & XNegative ? 1 : 0) |
2387 (flags & YNegative ? 2 : 0));
2388 }
8e20db05 2389
2390 } else if (!strcmp(p, "-sl")) {
2391 EXPECTS_ARG;
2392 SECOND_PASS_ONLY;
3ea863a3 2393 cfg->savelines = atoi(val);
8e20db05 2394
2395 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2396 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
b60ca991 2397 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
8e20db05 2398 GdkColor col;
2399
2400 EXPECTS_ARG;
2401 SECOND_PASS_ONLY;
2402 if (!gdk_color_parse(val, &col)) {
2403 err = 1;
a4e8ffb0 2404 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2405 appname, val);
8e20db05 2406 } else {
2407 int index;
2408 index = (!strcmp(p, "-fg") ? 0 :
2409 !strcmp(p, "-bg") ? 2 :
2410 !strcmp(p, "-bfg") ? 1 :
2411 !strcmp(p, "-bbg") ? 3 :
b60ca991 2412 !strcmp(p, "-cfg") ? 4 :
8e20db05 2413 !strcmp(p, "-cbg") ? 5 : -1);
2414 assert(index != -1);
3ea863a3 2415 cfg->colours[index][0] = col.red / 256;
2416 cfg->colours[index][1] = col.green / 256;
2417 cfg->colours[index][2] = col.blue / 256;
8e20db05 2418 }
2419
1d009ae7 2420 } else if (use_pty_argv && !strcmp(p, "-e")) {
faec60ed 2421 /* This option swallows all further arguments. */
2422 if (!do_everything)
2423 break;
2424
6169c758 2425 if (--argc > 0) {
2426 int i;
3d88e64d 2427 pty_argv = snewn(argc+1, char *);
6169c758 2428 ++argv;
2429 for (i = 0; i < argc; i++)
2430 pty_argv[i] = argv[i];
2431 pty_argv[argc] = NULL;
2432 break; /* finished command-line processing */
2433 } else
a4e8ffb0 2434 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2435 appname);
faec60ed 2436
486c8271 2437 } else if (!strcmp(p, "-title")) {
faec60ed 2438 EXPECTS_ARG;
2439 SECOND_PASS_ONLY;
3ea863a3 2440 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2441 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
faec60ed 2442
2443 } else if (!strcmp(p, "-log")) {
2444 EXPECTS_ARG;
2445 SECOND_PASS_ONLY;
9a30e26b 2446 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2447 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
3ea863a3 2448 cfg->logtype = LGTYP_DEBUG;
faec60ed 2449
8e20db05 2450 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
faec60ed 2451 SECOND_PASS_ONLY;
3ea863a3 2452 cfg->stamp_utmp = 0;
faec60ed 2453
8e20db05 2454 } else if (!strcmp(p, "-ut")) {
faec60ed 2455 SECOND_PASS_ONLY;
3ea863a3 2456 cfg->stamp_utmp = 1;
faec60ed 2457
8e20db05 2458 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
faec60ed 2459 SECOND_PASS_ONLY;
3ea863a3 2460 cfg->login_shell = 0;
faec60ed 2461
8e20db05 2462 } else if (!strcmp(p, "-ls")) {
2463 SECOND_PASS_ONLY;
3ea863a3 2464 cfg->login_shell = 1;
8e20db05 2465
faec60ed 2466 } else if (!strcmp(p, "-nethack")) {
2467 SECOND_PASS_ONLY;
3ea863a3 2468 cfg->nethack_keypad = 1;
faec60ed 2469
8e20db05 2470 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2471 SECOND_PASS_ONLY;
3ea863a3 2472 cfg->scrollbar = 0;
8e20db05 2473
2474 } else if (!strcmp(p, "-sb")) {
faec60ed 2475 SECOND_PASS_ONLY;
3ea863a3 2476 cfg->scrollbar = 0;
faec60ed 2477
2478 } else if (!strcmp(p, "-name")) {
2479 EXPECTS_ARG;
2480 app_name = val;
0ac15bdc 2481
2482 } else if (!strcmp(p, "-xrm")) {
2483 EXPECTS_ARG;
2484 provide_xrm_string(val);
2485
96add444 2486 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2487 help(stdout);
2488 exit(0);
2489
46a3419b 2490 } else if(p[0] != '-' && (!do_everything ||
2491 process_nonoption_arg(p, cfg))) {
2492 /* do nothing */
2493
edc73959 2494 } else {
2495 err = 1;
a4e8ffb0 2496 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
330f49be 2497 }
6169c758 2498 }
2499
faec60ed 2500 return err;
2501}
2502
0f33f9d1 2503static void block_signal(int sig, int block_it) {
2504 sigset_t ss;
2505
2506 sigemptyset(&ss);
2507 sigaddset(&ss, sig);
2508 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2509 perror("sigprocmask");
2510 exit(1);
2511 }
2512}
2513
facd762c 2514/*
2515 * This function retrieves the character set encoding of a font. It
2516 * returns the character set without the X11 hack (in case the user
2517 * asks to use the font's own encoding).
2518 */
2519static int set_font_info(struct gui_data *inst, int fontid)
2dc6356a 2520{
2521 GdkFont *font = inst->fonts[fontid];
2522 XFontStruct *xfs = GDK_FONT_XFONT(font);
2523 Display *disp = GDK_FONT_XDISPLAY(font);
2524 Atom charset_registry, charset_encoding;
2525 unsigned long registry_ret, encoding_ret;
facd762c 2526 int retval = CS_NONE;
2527
2dc6356a 2528 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2529 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2530 inst->fontinfo[fontid].charset = CS_NONE;
2531 inst->fontinfo[fontid].is_wide = 0;
2532 if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
2533 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2534 char *reg, *enc;
2535 reg = XGetAtomName(disp, (Atom)registry_ret);
2536 enc = XGetAtomName(disp, (Atom)encoding_ret);
2537 if (reg && enc) {
2538 char *encoding = dupcat(reg, "-", enc, NULL);
facd762c 2539 retval = inst->fontinfo[fontid].charset =
2540 charset_from_xenc(encoding);
2dc6356a 2541 /* FIXME: when libcharset supports wide encodings fix this. */
facd762c 2542 if (!strcasecmp(encoding, "iso10646-1")) {
2dc6356a 2543 inst->fontinfo[fontid].is_wide = 1;
facd762c 2544 retval = CS_UTF8;
2545 }
2dc6356a 2546
2547 /*
2548 * Hack for X line-drawing characters: if the primary
2549 * font is encoded as ISO-8859-anything, and has valid
2550 * glyphs in the first 32 char positions, it is assumed
2551 * that those glyphs are the VT100 line-drawing
2552 * character set.
2553 *
2554 * Actually, we'll hack even harder by only checking
2555 * position 0x19 (vertical line, VT100 linedrawing
2556 * `x'). Then we can check it easily by seeing if the
2557 * ascent and descent differ.
2558 */
2559 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2560 int lb, rb, wid, asc, desc;
2561 gchar text[2];
2562
2563 text[1] = '\0';
2564 text[0] = '\x12';
2565 gdk_string_extents(inst->fonts[fontid], text,
2566 &lb, &rb, &wid, &asc, &desc);
2567 if (asc != desc)
2568 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2569 }
2570
2dc6356a 2571 sfree(encoding);
2572 }
2573 }
facd762c 2574
2575 return retval;
2dc6356a 2576}
2577
74aca06d 2578int uxsel_input_add(int fd, int rwx) {
2579 int flags = 0;
2580 if (rwx & 1) flags |= GDK_INPUT_READ;
2581 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2582 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2583 return gdk_input_add(fd, flags, fd_input_func, NULL);
2584}
2585
2586void uxsel_input_remove(int id) {
2587 gdk_input_remove(id);
2588}
2589
f87534ba 2590char *guess_derived_font_name(GdkFont *font, int bold, int wide)
2591{
2592 XFontStruct *xfs = GDK_FONT_XFONT(font);
2593 Display *disp = GDK_FONT_XDISPLAY(font);
2594 Atom fontprop = XInternAtom(disp, "FONT", False);
2595 unsigned long ret;
2596 if (XGetFontProperty(xfs, fontprop, &ret)) {
2597 char *name = XGetAtomName(disp, (Atom)ret);
2598 if (name && name[0] == '-') {
2599 char *strings[13];
2600 char *dupname, *extrafree = NULL, *ret;
2601 char *p, *q;
2602 int nstr;
2603
2604 p = q = dupname = dupstr(name); /* skip initial minus */
2605 nstr = 0;
2606
2607 while (*p && nstr < lenof(strings)) {
2608 if (*p == '-') {
2609 *p = '\0';
2610 strings[nstr++] = p+1;
2611 }
2612 p++;
2613 }
2614
2615 if (nstr < lenof(strings))
2616 return NULL; /* XLFD was malformed */
2617
2618 if (bold)
2619 strings[2] = "bold";
2620
2621 if (wide) {
2622 /* 4 is `wideness', which obviously may have changed. */
2623 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2624 strings[4] = strings[5] = "*";
2625 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
2626 }
2627
2628 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
2629 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
2630 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
2631 "-", strings[ 9], "-", strings[10], "-", strings[11],
2632 "-", strings[12], NULL);
2633 sfree(extrafree);
2634 sfree(dupname);
2635
2636 return ret;
2637 }
2638 }
2639 return NULL;
2640}
2641
56b9b9a7 2642void setup_fonts_ucs(struct gui_data *inst)
2643{
2644 int font_charset;
f87534ba 2645 char *name;
2646 int guessed;
56b9b9a7 2647
2648 if (inst->fonts[0])
2649 gdk_font_unref(inst->fonts[0]);
2650 if (inst->fonts[1])
2651 gdk_font_unref(inst->fonts[1]);
2652 if (inst->fonts[2])
2653 gdk_font_unref(inst->fonts[2]);
2654 if (inst->fonts[3])
2655 gdk_font_unref(inst->fonts[3]);
2656
2657 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2658 if (!inst->fonts[0]) {
2659 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2660 inst->cfg.font.name);
2661 exit(1);
2662 }
2663 font_charset = set_font_info(inst, 0);
f87534ba 2664
bf133a73 2665 if (inst->cfg.shadowbold) {
2666 inst->fonts[1] = NULL;
f87534ba 2667 } else {
bf133a73 2668 if (inst->cfg.boldfont.name[0]) {
2669 name = inst->cfg.boldfont.name;
2670 guessed = FALSE;
2671 } else {
2672 name = guess_derived_font_name(inst->fonts[0], TRUE, FALSE);
2673 guessed = TRUE;
2674 }
2675 inst->fonts[1] = name ? gdk_font_load(name) : NULL;
2676 if (inst->fonts[1]) {
2677 set_font_info(inst, 1);
2678 } else if (!guessed) {
2679 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2680 inst->cfg.boldfont.name);
2681 exit(1);
2682 }
2683 if (guessed)
2684 sfree(name);
f87534ba 2685 }
f87534ba 2686
56b9b9a7 2687 if (inst->cfg.widefont.name[0]) {
f87534ba 2688 name = inst->cfg.widefont.name;
2689 guessed = FALSE;
2690 } else {
2691 name = guess_derived_font_name(inst->fonts[0], FALSE, TRUE);
2692 guessed = TRUE;
2693 }
2694 inst->fonts[2] = name ? gdk_font_load(name) : NULL;
2695 if (inst->fonts[2]) {
56b9b9a7 2696 set_font_info(inst, 2);
f87534ba 2697 } else if (!guessed) {
2698 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2699 inst->cfg.widefont.name);
2700 exit(1);
2701 }
2702 if (guessed)
2703 sfree(name);
2704
bf133a73 2705 if (inst->cfg.shadowbold) {
2706 inst->fonts[3] = NULL;
f87534ba 2707 } else {
bf133a73 2708 if (inst->cfg.wideboldfont.name[0]) {
2709 name = inst->cfg.wideboldfont.name;
2710 guessed = FALSE;
2711 } else {
2712 /*
2713 * Here we have some choices. We can widen the bold font,
2714 * bolden the wide font, or widen and bolden the standard
2715 * font. Try them all, in that order!
2716 */
2717 if (inst->cfg.widefont.name[0])
2718 name = guess_derived_font_name(inst->fonts[2], TRUE, FALSE);
2719 else if (inst->cfg.boldfont.name[0])
2720 name = guess_derived_font_name(inst->fonts[1], FALSE, TRUE);
2721 else
2722 name = guess_derived_font_name(inst->fonts[0], TRUE, TRUE);
2723 guessed = TRUE;
2724 }
2725 inst->fonts[3] = name ? gdk_font_load(name) : NULL;
2726 if (inst->fonts[3]) {
2727 set_font_info(inst, 3);
2728 } else if (!guessed) {
2729 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n", appname,
2730 inst->cfg.wideboldfont.name);
2731 exit(1);
2732 }
2733 if (guessed)
2734 sfree(name);
f87534ba 2735 }
56b9b9a7 2736
2737 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2738 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2739
6ac7f054 2740 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2741 inst->cfg.utf8_override, font_charset,
3900c2d6 2742 inst->cfg.vtmode);
56b9b9a7 2743}
2744
2745void set_geom_hints(struct gui_data *inst)
2746{
2747 GdkGeometry geom;
2748 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2749 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2750 geom.max_width = geom.max_height = -1;
2751 geom.base_width = 2*inst->cfg.window_border;
2752 geom.base_height = 2*inst->cfg.window_border;
2753 geom.width_inc = inst->font_width;
2754 geom.height_inc = inst->font_height;
2755 geom.min_aspect = geom.max_aspect = 0;
2756 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2757 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2758 GDK_HINT_RESIZE_INC);
2759}
2760
47e4e735 2761void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2762{
2763 struct gui_data *inst = (struct gui_data *)data;
2764 term_clrsb(inst->term);
2765}
2766
2767void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2768{
2769 struct gui_data *inst = (struct gui_data *)data;
2770 term_pwron(inst->term);
e3be8de5 2771 if (inst->ldisc)
2772 ldisc_send(inst->ldisc, NULL, 0, 0);
47e4e735 2773}
2774
d9e5c333 2775void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2776{
2777 struct gui_data *inst = (struct gui_data *)data;
2778 term_copyall(inst->term);
2779}
2780
47e4e735 2781void special_menuitem(GtkMenuItem *item, gpointer data)
2782{
2783 struct gui_data *inst = (struct gui_data *)data;
f7f9fb5c 2784 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2785 "user-data"));
47e4e735 2786
e3be8de5 2787 if (inst->back)
2788 inst->back->special(inst->backhandle, code);
47e4e735 2789}
2790
2791void about_menuitem(GtkMenuItem *item, gpointer data)
2792{
7718480a 2793 struct gui_data *inst = (struct gui_data *)data;
2794 about_box(inst->window);
47e4e735 2795}
2796
8eed910d 2797void event_log_menuitem(GtkMenuItem *item, gpointer data)
2798{
2799 struct gui_data *inst = (struct gui_data *)data;
2800 showeventlog(inst->eventlogstuff, inst->window);
2801}
2802
56b9b9a7 2803void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2804{
2805 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2806 static const int ww[] = {
2807 6, 7, 8, 9, 10, 11, 12, 13,
2808 14, 15, 16, 17, 18, 19, 20, 21,
2809 0, 1, 2, 3, 4, 5
2810 };
2811 struct gui_data *inst = (struct gui_data *)data;
2812 char *title = dupcat(appname, " Reconfiguration", NULL);
2813 Config cfg2, oldcfg;
2814 int i, need_size;
2815
2816 cfg2 = inst->cfg; /* structure copy */
2817
2818 if (do_config_box(title, &cfg2, 1)) {
2819
2820 oldcfg = inst->cfg; /* structure copy */
2821 inst->cfg = cfg2; /* structure copy */
2822
2823 /* Pass new config data to the logging module */
2824 log_reconfig(inst->logctx, &cfg2);
2825 /*
2826 * Flush the line discipline's edit buffer in the case
2827 * where local editing has just been disabled.
2828 */
e3be8de5 2829 if (inst->ldisc)
2830 ldisc_send(inst->ldisc, NULL, 0, 0);
56b9b9a7 2831 /* Pass new config data to the terminal */
2832 term_reconfig(inst->term, &cfg2);
2833 /* Pass new config data to the back end */
e3be8de5 2834 if (inst->back)
2835 inst->back->reconfig(inst->backhandle, &cfg2);
56b9b9a7 2836
2837 /*
2838 * Just setting inst->cfg is sufficient to cause colour
2839 * setting changes to appear on the next ESC]R palette
2840 * reset. But we should also check whether any colour
2841 * settings have been changed, and revert the ones that
2842 * have to the new default, on the assumption that the user
2843 * is most likely to want an immediate update.
2844 */
2845 for (i = 0; i < NCOLOURS; i++) {
2846 if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
2847 oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
db26b3af 2848 oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2]) {
56b9b9a7 2849 real_palette_set(inst, i, cfg2.colours[ww[i]][0],
2850 cfg2.colours[ww[i]][1],
2851 cfg2.colours[ww[i]][2]);
db26b3af 2852
2853 /*
2854 * If the default background has changed, we must
2855 * repaint the space in between the window border
2856 * and the text area.
2857 */
2858 if (i == 18) {
2859 set_window_background(inst);
2860 draw_backing_rect(inst);
2861 }
2862 }
56b9b9a7 2863 }
2864
2865 /*
2866 * If the scrollbar needs to be shown, hidden, or moved
2867 * from one end to the other of the window, do so now.
2868 */
2869 if (oldcfg.scrollbar != cfg2.scrollbar) {
2870 if (cfg2.scrollbar)
2871 gtk_widget_show(inst->sbar);
2872 else
2873 gtk_widget_hide(inst->sbar);
2874 }
2875 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2876 gtk_box_reorder_child(inst->hbox, inst->sbar,
2877 cfg2.scrollbar_on_left ? 0 : 1);
2878 }
2879
2880 /*
2881 * Change the window title, if required.
2882 */
2883 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2884 set_title(inst, cfg2.wintitle);
6e785e0c 2885 set_window_titles(inst);
56b9b9a7 2886
2887 /*
2888 * Redo the whole tangled fonts and Unicode mess if
2889 * necessary.
2890 */
2891 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2892 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2893 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2894 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
3900c2d6 2895 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
bf133a73 2896 oldcfg.vtmode != cfg2.vtmode ||
2897 oldcfg.shadowbold != cfg2.shadowbold) {
56b9b9a7 2898 setup_fonts_ucs(inst);
2899 need_size = 1;
2900 } else
2901 need_size = 0;
2902
2903 /*
2904 * Resize the window.
2905 */
2906 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2907 oldcfg.window_border != cfg2.window_border || need_size) {
2908 set_geom_hints(inst);
2909 request_resize(inst, cfg2.width, cfg2.height);
ec9f4fc6 2910 } else {
2911 /*
2912 * The above will have caused a call to term_size() for
2913 * us if it happened. If the user has fiddled with only
2914 * the scrollback size, the above will not have
2915 * happened and we will need an explicit term_size()
2916 * here.
2917 */
2918 if (oldcfg.savelines != cfg2.savelines)
2919 term_size(inst->term, inst->term->rows, inst->term->cols,
2920 cfg2.savelines);
2921 }
56b9b9a7 2922
2923 term_invalidate(inst->term);
db26b3af 2924
2925 /*
2926 * We do an explicit full redraw here to ensure the window
2927 * border has been redrawn as well as the text area.
2928 */
2929 gtk_widget_queue_draw(inst->area);
56b9b9a7 2930 }
2931 sfree(title);
2932}
2933
56801e3d 2934void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
2935{
2936 /*
2937 * Re-execing ourself is not an exact science under Unix. I do
2938 * the best I can by using /proc/self/exe if available and by
2939 * assuming argv[0] can be found on $PATH if not.
2940 *
2941 * Note that we also have to reconstruct the elements of the
2942 * original argv which gtk swallowed, since the user wants the
2943 * new session to appear on the same X display as the old one.
2944 */
2945 char **args;
2946 va_list ap;
2947 int i, n;
2948 int pid;
2949
2950 /*
2951 * Collect the arguments with which to re-exec ourself.
2952 */
2953 va_start(ap, fd_to_close);
2954 n = 2; /* progname and terminating NULL */
2955 n += inst->ngtkargs;
2956 while (va_arg(ap, char *) != NULL)
2957 n++;
2958 va_end(ap);
2959
2960 args = snewn(n, char *);
2961 args[0] = inst->progname;
2962 args[n-1] = NULL;
2963 for (i = 0; i < inst->ngtkargs; i++)
2964 args[i+1] = inst->gtkargvstart[i];
2965
2966 i++;
2967 va_start(ap, fd_to_close);
2968 while ((args[i++] = va_arg(ap, char *)) != NULL);
2969 va_end(ap);
2970
2971 assert(i == n);
2972
2973 /*
2974 * Do the double fork.
2975 */
2976 pid = fork();
2977 if (pid < 0) {
2978 perror("fork");
2979 return;
2980 }
2981
2982 if (pid == 0) {
2983 int pid2 = fork();
2984 if (pid2 < 0) {
2985 perror("fork");
2986 _exit(1);
2987 } else if (pid2 > 0) {
2988 /*
2989 * First child has successfully forked second child. My
2990 * Work Here Is Done. Note the use of _exit rather than
2991 * exit: the latter appears to cause destroy messages
2992 * to be sent to the X server. I suspect gtk uses
2993 * atexit.
2994 */
2995 _exit(0);
2996 }
2997
2998 /*
2999 * If we reach here, we are the second child, so we now
3000 * actually perform the exec.
3001 */
3002 if (fd_to_close >= 0)
3003 close(fd_to_close);
3004
3005 execv("/proc/self/exe", args);
3006 execvp(inst->progname, args);
3007 perror("exec");
3008 _exit(127);
3009
3010 } else {
3011 int status;
3012 waitpid(pid, &status, 0);
3013 }
3014
3015}
3016
3017void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3018{
3019 struct gui_data *inst = (struct gui_data *)gdata;
3020 /*
3021 * For this feature we must marshal cfg and (possibly) pty_argv
3022 * into a byte stream, create a pipe, and send this byte stream
3023 * to the child through the pipe.
3024 */
3025 int i, ret, size;
3026 char *data;
3027 char option[80];
3028 int pipefd[2];
3029
3030 if (pipe(pipefd) < 0) {
3031 perror("pipe");
3032 return;
3033 }
3034
3035 size = sizeof(inst->cfg);
3036 if (use_pty_argv && pty_argv) {
3037 for (i = 0; pty_argv[i]; i++)
3038 size += strlen(pty_argv[i]) + 1;
3039 }
3040
3041 data = snewn(size, char);
3042 memcpy(data, &inst->cfg, sizeof(inst->cfg));
3043 if (use_pty_argv && pty_argv) {
3044 int p = sizeof(inst->cfg);
3045 for (i = 0; pty_argv[i]; i++) {
3046 strcpy(data + p, pty_argv[i]);
3047 p += strlen(pty_argv[i]) + 1;
3048 }
3049 assert(p == size);
3050 }
3051
3052 sprintf(option, "---[%d,%d]", pipefd[0], size);
3053 fcntl(pipefd[0], F_SETFD, 0);
3054 fork_and_exec_self(inst, pipefd[1], option, NULL);
3055 close(pipefd[0]);
3056
7ffdbc1a 3057 i = ret = 0;
56801e3d 3058 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3059 i += ret;
3060 if (ret < 0)
3061 perror("write to pipe");
3062 close(pipefd[1]);
3063 sfree(data);
3064}
3065
3066int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3067{
3068 int fd, i, ret, size;
3069 char *data;
3070
3071 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3072 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3073 exit(1);
3074 }
3075
3076 data = snewn(size, char);
7ffdbc1a 3077 i = ret = 0;
56801e3d 3078 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3079 i += ret;
3080 if (ret < 0) {
3081 perror("read from pipe");
3082 exit(1);
3083 } else if (i < size) {
3084 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3085 appname);
3086 exit(1);
3087 }
3088
3089 memcpy(cfg, data, sizeof(Config));
3090 if (use_pty_argv && size > sizeof(Config)) {
3091 int n = 0;
3092 i = sizeof(Config);
3093 while (i < size) {
3094 while (i < size && data[i]) i++;
3095 if (i >= size) {
3096 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3097 appname);
3098 exit(1);
3099 }
3100 i++;
3101 n++;
3102 }
3103 pty_argv = snewn(n+1, char *);
3104 pty_argv[n] = NULL;
3105 n = 0;
3106 i = sizeof(Config);
3107 while (i < size) {
3108 char *p = data + i;
3109 while (i < size && data[i]) i++;
3110 assert(i < size);
3111 i++;
3112 pty_argv[n++] = dupstr(p);
3113 }
3114 }
3115
3116 return 0;
3117}
3118
3119void new_session_menuitem(GtkMenuItem *item, gpointer data)
3120{
3121 struct gui_data *inst = (struct gui_data *)data;
3122
3123 fork_and_exec_self(inst, -1, NULL);
3124}
3125
e3be8de5 3126void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3127{
3128 struct gui_data *inst = (struct gui_data *)data;
3129
3130 if (!inst->back) {
3131 logevent(inst, "----- Session restarted -----");
3132 start_backend(inst);
3133 inst->exited = FALSE;
3134 }
3135}
3136
56801e3d 3137void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3138{
3139 struct gui_data *inst = (struct gui_data *)data;
3140 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3141
3142 fork_and_exec_self(inst, -1, "-load", str, NULL);
3143}
3144
3145void saved_session_freedata(GtkMenuItem *item, gpointer data)
3146{
3147 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3148
3149 sfree(str);
3150}
3151
47e4e735 3152void update_specials_menu(void *frontend)
3153{
fbf6cb3b 3154 struct gui_data *inst = (struct gui_data *)frontend;
47e4e735 3155
3156 const struct telnet_special *specials;
3157
e3be8de5 3158 if (inst->back)
3159 specials = inst->back->get_specials(inst->backhandle);
3160 else
3161 specials = NULL;
3162
47e4e735 3163 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3164 (GtkCallback)gtk_widget_destroy, NULL);
3165 if (specials) {
3166 int i;
3167 GtkWidget *menuitem;
3168 for (i = 0; specials[i].name; i++) {
3169 if (*specials[i].name) {
3170 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3171 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
f7f9fb5c 3172 GINT_TO_POINTER(specials[i].code));
47e4e735 3173 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3174 GTK_SIGNAL_FUNC(special_menuitem), inst);
3175 } else
3176 menuitem = gtk_menu_item_new();
3177 gtk_container_add(GTK_CONTAINER(inst->specialsmenu), menuitem);
3178 gtk_widget_show(menuitem);
3179 }
3180 gtk_widget_show(inst->specialsitem1);
3181 gtk_widget_show(inst->specialsitem2);
3182 } else {
3183 gtk_widget_hide(inst->specialsitem1);
3184 gtk_widget_hide(inst->specialsitem2);
3185 }
3186}
3187
e3be8de5 3188static void start_backend(struct gui_data *inst)
faec60ed 3189{
1d009ae7 3190 extern Backend *select_backend(Config *cfg);
e3be8de5 3191 char *realhost;
3192 const char *error;
3193
3194 inst->back = select_backend(&inst->cfg);
3195
3196 error = inst->back->init((void *)inst, &inst->backhandle,
3197 &inst->cfg, inst->cfg.host, inst->cfg.port,
3198 &realhost, inst->cfg.tcp_nodelay,
3199 inst->cfg.tcp_keepalives);
3200
3201 if (error) {
3202 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3203 inst->cfg.host, error);
3204 inst->exited = TRUE;
3205 fatal_message_box(inst->window, msg);
3206 sfree(msg);
3207 exit(0);
3208 }
3209
3210 if (inst->cfg.wintitle[0]) {
3211 set_title(inst, inst->cfg.wintitle);
3212 set_icon(inst, inst->cfg.wintitle);
3213 } else {
3214 char *title = make_default_wintitle(realhost);
3215 set_title(inst, title);
3216 set_icon(inst, title);
3217 sfree(title);
3218 }
3219 inst->back->provide_logctx(inst->backhandle, inst->logctx);
e3be8de5 3220
3221 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3222
3223 inst->ldisc =
3224 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3225 inst);
3226
3227 gtk_widget_hide(inst->restartitem);
3228}
3229
3230int pt_main(int argc, char **argv)
3231{
1d009ae7 3232 extern int cfgbox(Config *cfg);
a8327734 3233 struct gui_data *inst;
faec60ed 3234
56801e3d 3235 /*
3236 * Create an instance structure and initialise to zeroes
3237 */
3238 inst = snew(struct gui_data);
3239 memset(inst, 0, sizeof(*inst));
3240 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3241
0f33f9d1 3242 /* defer any child exit handling until we're ready to deal with
3243 * it */
3244 block_signal(SIGCHLD, 1);
3245
56801e3d 3246 inst->progname = argv[0];
7428c509 3247 /*
56801e3d 3248 * Copy the original argv before letting gtk_init fiddle with
3249 * it. It will be required later.
7428c509 3250 */
56801e3d 3251 {
3252 int i, oldargc;
3253 inst->gtkargvstart = snewn(argc-1, char *);
3254 for (i = 1; i < argc; i++)
3255 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3256 oldargc = argc;
3257 gtk_init(&argc, &argv);
3258 inst->ngtkargs = oldargc - argc;
3259 }
7428c509 3260
56801e3d 3261 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3262 read_dupsession_data(inst, &inst->cfg, argv[1]);
3263 /* Splatter this argument so it doesn't clutter a ps listing */
3264 memset(argv[1], 0, strlen(argv[1]));
3265 } else {
3266 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
3267 exit(1); /* pre-defaults pass to get -class */
3268 do_defaults(NULL, &inst->cfg);
3269 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
3270 exit(1); /* post-defaults, do everything */
9dc625f0 3271
56801e3d 3272 cmdline_run_saved(&inst->cfg);
46a3419b 3273
56801e3d 3274 if (!*inst->cfg.host && !cfgbox(&inst->cfg))
3275 exit(0); /* config box hit Cancel */
3276 }
1d009ae7 3277
8c161662 3278 if (!compound_text_atom)
3279 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3280 if (!utf8_string_atom)
3281 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
dd72dfa3 3282
56b9b9a7 3283 setup_fonts_ucs(inst);
3aac0d35 3284 init_cutbuffers();
1709795f 3285
12d200b1 3286 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3287
16891265 3288 /*
3289 * Set up the colour map.
3290 */
a8327734 3291 palette_reset(inst);
16891265 3292
56b9b9a7 3293 inst->width = inst->cfg.width;
3294 inst->height = inst->cfg.height;
3295
f7f27309 3296 inst->area = gtk_drawing_area_new();
3297 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3ea863a3 3298 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3299 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
56b9b9a7 3300 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3301 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
6a5e84dd 3302 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
56b9b9a7 3303 /*
3304 * We always create the scrollbar; it remains invisible if
3305 * unwanted, so we can pop it up quickly if it suddenly becomes
3306 * desirable.
3307 */
3308 if (inst->cfg.scrollbar_on_left)
3309 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
88e6b9ca 3310 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
56b9b9a7 3311 if (!inst->cfg.scrollbar_on_left)
3312 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
6a5e84dd 3313
12d200b1 3314 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
f7f27309 3315
56b9b9a7 3316 set_geom_hints(inst);
f7f27309 3317
aca2a702 3318 gtk_widget_show(inst->area);
3319 if (inst->cfg.scrollbar)
3320 gtk_widget_show(inst->sbar);
56b9b9a7 3321 else
3322 gtk_widget_hide(inst->sbar);
aca2a702 3323 gtk_widget_show(GTK_WIDGET(inst->hbox));
3324
3325 if (inst->gotpos) {
3326 int x = inst->xpos, y = inst->ypos;
3327 GtkRequisition req;
3328 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3329 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3330 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3331 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3332 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3333 }
3334
12d200b1 3335 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
f7f27309 3336 GTK_SIGNAL_FUNC(destroy), inst);
12d200b1 3337 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
f7f27309 3338 GTK_SIGNAL_FUNC(delete_window), inst);
12d200b1 3339 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
f7f27309 3340 GTK_SIGNAL_FUNC(key_event), inst);
c33c3d76 3341 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3342 GTK_SIGNAL_FUNC(key_event), inst);
12d200b1 3343 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
f7f27309 3344 GTK_SIGNAL_FUNC(focus_event), inst);
12d200b1 3345 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
f7f27309 3346 GTK_SIGNAL_FUNC(focus_event), inst);
3347 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3348 GTK_SIGNAL_FUNC(configure_area), inst);
3349 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3350 GTK_SIGNAL_FUNC(expose_area), inst);
e6346999 3351 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3352 GTK_SIGNAL_FUNC(button_event), inst);
3353 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3354 GTK_SIGNAL_FUNC(button_event), inst);
3355 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3356 GTK_SIGNAL_FUNC(motion_event), inst);
3357 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3358 GTK_SIGNAL_FUNC(selection_received), inst);
3359 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3360 GTK_SIGNAL_FUNC(selection_get), inst);
3361 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3362 GTK_SIGNAL_FUNC(selection_clear), inst);
3ea863a3 3363 if (inst->cfg.scrollbar)
330f49be 3364 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3365 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
f7f27309 3366 gtk_timeout_add(20, timer_func, inst);
3367 gtk_widget_add_events(GTK_WIDGET(inst->area),
e6346999 3368 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3369 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
57e636ca 3370 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
f7f27309 3371
12d200b1 3372 gtk_widget_show(inst->window);
f7f27309 3373
a8327734 3374 set_window_background(inst);
7428c509 3375
47e4e735 3376 /*
3377 * Set up the Ctrl+rightclick context menu.
3378 */
3379 {
3380 GtkWidget *menuitem;
3381 char *s;
56801e3d 3382 extern const int use_event_log, new_session, saved_sessions;
47e4e735 3383
3384 inst->menu = gtk_menu_new();
3385
3386#define MKMENUITEM(title, func) do { \
3387 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3388 gtk_menu_item_new(); \
3389 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3390 gtk_widget_show(menuitem); \
3391 if (func != NULL) \
3392 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3393 GTK_SIGNAL_FUNC(func), inst); \
3394} while (0)
56801e3d 3395 if (new_session)
3396 MKMENUITEM("New Session", new_session_menuitem);
e3be8de5 3397 MKMENUITEM("Restart Session", restart_session_menuitem);
3398 inst->restartitem = menuitem;
3399 gtk_widget_hide(inst->restartitem);
56801e3d 3400 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3401 if (saved_sessions) {
3402 struct sesslist sesslist;
3403 int i;
3404
3405 inst->sessionsmenu = gtk_menu_new();
3406
3407 get_sesslist(&sesslist, TRUE);
3408 for (i = 1; i < sesslist.nsessions; i++) {
3409 menuitem = gtk_menu_item_new_with_label(sesslist.sessions[i]);
3410 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3411 gtk_widget_show(menuitem);
3412 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3413 dupstr(sesslist.sessions[i]));
3414 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3415 GTK_SIGNAL_FUNC(saved_session_menuitem),
3416 inst);
3417 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3418 GTK_SIGNAL_FUNC(saved_session_freedata),
3419 inst);
3420 }
3421 get_sesslist(&sesslist, FALSE);
3422
3423 MKMENUITEM("Saved Sessions", NULL);
3424 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3425 inst->sessionsmenu);
3426 }
3427 MKMENUITEM(NULL, NULL);
56b9b9a7 3428 MKMENUITEM("Change Settings", change_settings_menuitem);
3429 MKMENUITEM(NULL, NULL);
8eed910d 3430 if (use_event_log)
3431 MKMENUITEM("Event Log", event_log_menuitem);
47e4e735 3432 MKMENUITEM("Special Commands", NULL);
3433 inst->specialsmenu = gtk_menu_new();
3434 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3435 inst->specialsitem1 = menuitem;
3436 MKMENUITEM(NULL, NULL);
3437 inst->specialsitem2 = menuitem;
533b1743 3438 gtk_widget_hide(inst->specialsitem1);
3439 gtk_widget_hide(inst->specialsitem2);
47e4e735 3440 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3441 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
d9e5c333 3442 MKMENUITEM("Copy All", copy_all_menuitem);
47e4e735 3443 MKMENUITEM(NULL, NULL);
3444 s = dupcat("About ", appname, NULL);
3445 MKMENUITEM(s, about_menuitem);
3446 sfree(s);
3447#undef MKMENUITEM
3448 }
3449
a8327734 3450 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3451 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3452 inst->blankcursor = make_mouse_ptr(inst, -1);
3453 make_mouse_ptr(inst, -2); /* clean up cursor font */
57e636ca 3454 inst->currcursor = inst->textcursor;
a8327734 3455 show_mouseptr(inst, 1);
d64838e1 3456
8eed910d 3457 inst->eventlogstuff = eventlogstuff_new();
3458
21d2b241 3459 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3ea863a3 3460 inst->logctx = log_init(inst, &inst->cfg);
a8327734 3461 term_provide_logctx(inst->term, inst->logctx);
887035a5 3462
74aca06d 3463 uxsel_init();
3464
3f935d5b 3465 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3466
e3be8de5 3467 start_backend(inst);
755a6d84 3468
b9d7bcad 3469 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
755a6d84 3470
0f33f9d1 3471 /* now we're reday to deal with the child exit handler being
3472 * called */
3473 block_signal(SIGCHLD, 0);
74aca06d 3474
755e29c6 3475 /*
3476 * Block SIGPIPE: if we attempt Duplicate Session or similar
3477 * and it falls over in some way, we certainly don't want
3478 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3479 * this _after_ (at least pterm) forks off its child process,
3480 * since the child wants SIGPIPE handled in the usual way.
3481 */
3482 block_signal(SIGPIPE, 1);
3483
74aca06d 3484 inst->exited = FALSE;
3485
f7f27309 3486 gtk_main();
3487
3488 return 0;
3489}