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