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