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