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