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