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