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