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