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