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