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