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