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