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