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