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