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