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