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