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