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