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