Since r7265, a user could not launch a PuTTY session to a specific host by
[u/mdw/putty] / unix / gtkwin.c
1 /*
2 * gtkwin.c: the main code that runs a PuTTY terminal emulator and
3 * backend in a GTK window.
4 */
5
6 #define _GNU_SOURCE
7
8 #include <string.h>
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <time.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
22 #include <gdk/gdkx.h>
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
26
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
28
29 #include "putty.h"
30 #include "terminal.h"
31
32 #define CAT2(x,y) x ## y
33 #define CAT(x,y) CAT2(x,y)
34 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
35
36 /* Colours come in two flavours: configurable, and xterm-extended. */
37 #define NCFGCOLOURS (lenof(((Config *)0)->colours))
38 #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */
39 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
40
41 GdkAtom compound_text_atom, utf8_string_atom;
42
43 extern char **pty_argv; /* declared in pty.c */
44 extern int use_pty_argv;
45
46 /*
47 * Timers are global across all sessions (even if we were handling
48 * multiple sessions, which we aren't), so the current timer ID is
49 * a global variable.
50 */
51 static guint timer_id = 0;
52
53 struct gui_data {
54 GtkWidget *window, *area, *sbar;
55 GtkBox *hbox;
56 GtkAdjustment *sbar_adjust;
57 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2,
58 *restartitem;
59 GtkWidget *sessionsmenu;
60 GdkPixmap *pixmap;
61 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
62 struct {
63 int charset;
64 int is_wide;
65 } fontinfo[4];
66 int xpos, ypos, gotpos, gravity;
67 GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor;
68 GdkColor cols[NALLCOLOURS];
69 GdkColormap *colmap;
70 wchar_t *pastein_data;
71 int direct_to_font;
72 int pastein_data_len;
73 char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8;
74 int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len;
75 int font_width, font_height;
76 int width, height;
77 int ignore_sbar;
78 int mouseptr_visible;
79 int busy_status;
80 guint term_paste_idle_id;
81 int alt_keycode;
82 int alt_digits;
83 char wintitle[sizeof(((Config *)0)->wintitle)];
84 char icontitle[sizeof(((Config *)0)->wintitle)];
85 int master_fd, master_func_id;
86 void *ldisc;
87 Backend *back;
88 void *backhandle;
89 Terminal *term;
90 void *logctx;
91 int exited;
92 struct unicode_data ucsdata;
93 Config cfg;
94 void *eventlogstuff;
95 char *progname, **gtkargvstart;
96 int ngtkargs;
97 guint32 input_event_time; /* Timestamp of the most recent input event. */
98 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 sends a Break special to the backend */
667 if (event->keyval == GDK_Break &&
668 (event->state & GDK_CONTROL_MASK)) {
669 if (inst->back)
670 inst->back->special(inst->backhandle, TS_BRK);
671 return TRUE;
672 }
673
674 /* We handle Return ourselves, because it needs to be flagged as
675 * special to ldisc. */
676 if (event->keyval == GDK_Return) {
677 output[1] = '\015';
678 use_ucsoutput = FALSE;
679 end = 2;
680 special = TRUE;
681 }
682
683 /* Control-2, Control-Space and Control-@ are NUL */
684 if (!event->string[0] &&
685 (event->keyval == ' ' || event->keyval == '2' ||
686 event->keyval == '@') &&
687 (event->state & (GDK_SHIFT_MASK |
688 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
689 output[1] = '\0';
690 use_ucsoutput = FALSE;
691 end = 2;
692 }
693
694 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
695 if (!event->string[0] && event->keyval == ' ' &&
696 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
697 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
698 output[1] = '\240';
699 use_ucsoutput = FALSE;
700 end = 2;
701 }
702
703 /* We don't let GTK tell us what Backspace is! We know better. */
704 if (event->keyval == GDK_BackSpace &&
705 !(event->state & GDK_SHIFT_MASK)) {
706 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
707 use_ucsoutput = FALSE;
708 end = 2;
709 special = TRUE;
710 }
711 /* For Shift Backspace, do opposite of what is configured. */
712 if (event->keyval == GDK_BackSpace &&
713 (event->state & GDK_SHIFT_MASK)) {
714 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
715 use_ucsoutput = FALSE;
716 end = 2;
717 special = TRUE;
718 }
719
720 /* Shift-Tab is ESC [ Z */
721 if (event->keyval == GDK_ISO_Left_Tab ||
722 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
723 end = 1 + sprintf(output+1, "\033[Z");
724 use_ucsoutput = FALSE;
725 }
726
727 /*
728 * NetHack keypad mode.
729 */
730 if (inst->cfg.nethack_keypad) {
731 char *keys = NULL;
732 switch (event->keyval) {
733 case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break;
734 case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break;
735 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break;
736 case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break;
737 case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break;
738 case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break;
739 case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break;
740 case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break;
741 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break;
742 }
743 if (keys) {
744 end = 2;
745 if (event->state & GDK_CONTROL_MASK)
746 output[1] = keys[2];
747 else if (event->state & GDK_SHIFT_MASK)
748 output[1] = keys[1];
749 else
750 output[1] = keys[0];
751 use_ucsoutput = FALSE;
752 goto done;
753 }
754 }
755
756 /*
757 * Application keypad mode.
758 */
759 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
760 int xkey = 0;
761 switch (event->keyval) {
762 case GDK_Num_Lock: xkey = 'P'; break;
763 case GDK_KP_Divide: xkey = 'Q'; break;
764 case GDK_KP_Multiply: xkey = 'R'; break;
765 case GDK_KP_Subtract: xkey = 'S'; break;
766 /*
767 * Keypad + is tricky. It covers a space that would
768 * be taken up on the VT100 by _two_ keys; so we
769 * let Shift select between the two. Worse still,
770 * in xterm function key mode we change which two...
771 */
772 case GDK_KP_Add:
773 if (inst->cfg.funky_type == FUNKY_XTERM) {
774 if (event->state & GDK_SHIFT_MASK)
775 xkey = 'l';
776 else
777 xkey = 'k';
778 } else if (event->state & GDK_SHIFT_MASK)
779 xkey = 'm';
780 else
781 xkey = 'l';
782 break;
783 case GDK_KP_Enter: xkey = 'M'; break;
784 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
785 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
786 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
787 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
788 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
789 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
790 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
791 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
792 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
793 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
794 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
795 }
796 if (xkey) {
797 if (inst->term->vt52_mode) {
798 if (xkey >= 'P' && xkey <= 'S')
799 end = 1 + sprintf(output+1, "\033%c", xkey);
800 else
801 end = 1 + sprintf(output+1, "\033?%c", xkey);
802 } else
803 end = 1 + sprintf(output+1, "\033O%c", xkey);
804 use_ucsoutput = FALSE;
805 goto done;
806 }
807 }
808
809 /*
810 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
811 * for integer decimal nn.)
812 *
813 * We also deal with the weird ones here. Linux VCs replace F1
814 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
815 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
816 * respectively.
817 */
818 {
819 int code = 0;
820 switch (event->keyval) {
821 case GDK_F1:
822 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
823 break;
824 case GDK_F2:
825 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
826 break;
827 case GDK_F3:
828 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
829 break;
830 case GDK_F4:
831 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
832 break;
833 case GDK_F5:
834 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
835 break;
836 case GDK_F6:
837 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
838 break;
839 case GDK_F7:
840 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
841 break;
842 case GDK_F8:
843 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
844 break;
845 case GDK_F9:
846 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
847 break;
848 case GDK_F10:
849 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
850 break;
851 case GDK_F11:
852 code = 23;
853 break;
854 case GDK_F12:
855 code = 24;
856 break;
857 case GDK_F13:
858 code = 25;
859 break;
860 case GDK_F14:
861 code = 26;
862 break;
863 case GDK_F15:
864 code = 28;
865 break;
866 case GDK_F16:
867 code = 29;
868 break;
869 case GDK_F17:
870 code = 31;
871 break;
872 case GDK_F18:
873 code = 32;
874 break;
875 case GDK_F19:
876 code = 33;
877 break;
878 case GDK_F20:
879 code = 34;
880 break;
881 }
882 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
883 case GDK_Home: case GDK_KP_Home:
884 code = 1;
885 break;
886 case GDK_Insert: case GDK_KP_Insert:
887 code = 2;
888 break;
889 case GDK_Delete: case GDK_KP_Delete:
890 code = 3;
891 break;
892 case GDK_End: case GDK_KP_End:
893 code = 4;
894 break;
895 case GDK_Page_Up: case GDK_KP_Page_Up:
896 code = 5;
897 break;
898 case GDK_Page_Down: case GDK_KP_Page_Down:
899 code = 6;
900 break;
901 }
902 /* Reorder edit keys to physical order */
903 if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6)
904 code = "\0\2\1\4\5\3\6"[code];
905
906 if (inst->term->vt52_mode && code > 0 && code <= 6) {
907 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
908 use_ucsoutput = FALSE;
909 goto done;
910 }
911
912 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */
913 code >= 11 && code <= 34) {
914 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
915 int index = 0;
916 switch (event->keyval) {
917 case GDK_F1: index = 0; break;
918 case GDK_F2: index = 1; break;
919 case GDK_F3: index = 2; break;
920 case GDK_F4: index = 3; break;
921 case GDK_F5: index = 4; break;
922 case GDK_F6: index = 5; break;
923 case GDK_F7: index = 6; break;
924 case GDK_F8: index = 7; break;
925 case GDK_F9: index = 8; break;
926 case GDK_F10: index = 9; break;
927 case GDK_F11: index = 10; break;
928 case GDK_F12: index = 11; break;
929 }
930 if (event->state & GDK_SHIFT_MASK) index += 12;
931 if (event->state & GDK_CONTROL_MASK) index += 24;
932 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
933 use_ucsoutput = FALSE;
934 goto done;
935 }
936 if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */
937 code >= 1 && code <= 6) {
938 char codes[] = "HL.FIG";
939 if (code == 3) {
940 output[1] = '\x7F';
941 end = 2;
942 } else {
943 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
944 }
945 use_ucsoutput = FALSE;
946 goto done;
947 }
948 if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) &&
949 code >= 11 && code <= 24) {
950 int offt = 0;
951 if (code > 15)
952 offt++;
953 if (code > 21)
954 offt++;
955 if (inst->term->vt52_mode)
956 end = 1 + sprintf(output+1,
957 "\x1B%c", code + 'P' - 11 - offt);
958 else
959 end = 1 + sprintf(output+1,
960 "\x1BO%c", code + 'P' - 11 - offt);
961 use_ucsoutput = FALSE;
962 goto done;
963 }
964 if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
965 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
966 use_ucsoutput = FALSE;
967 goto done;
968 }
969 if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
970 if (inst->term->vt52_mode)
971 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
972 else
973 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
974 use_ucsoutput = FALSE;
975 goto done;
976 }
977 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
978 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
979 use_ucsoutput = FALSE;
980 goto done;
981 }
982 if (code) {
983 end = 1 + sprintf(output+1, "\x1B[%d~", code);
984 use_ucsoutput = FALSE;
985 goto done;
986 }
987 }
988
989 /*
990 * Cursor keys. (This includes the numberpad cursor keys,
991 * if we haven't already done them due to app keypad mode.)
992 *
993 * Here we also process un-numlocked un-appkeypadded KP5,
994 * which sends ESC [ G.
995 */
996 {
997 int xkey = 0;
998 switch (event->keyval) {
999 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
1000 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
1001 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
1002 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
1003 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
1004 }
1005 if (xkey) {
1006 /*
1007 * The arrow keys normally do ESC [ A and so on. In
1008 * app cursor keys mode they do ESC O A instead.
1009 * Ctrl toggles the two modes.
1010 */
1011 if (inst->term->vt52_mode) {
1012 end = 1 + sprintf(output+1, "\033%c", xkey);
1013 } else if (!inst->term->app_cursor_keys ^
1014 !(event->state & GDK_CONTROL_MASK)) {
1015 end = 1 + sprintf(output+1, "\033O%c", xkey);
1016 } else {
1017 end = 1 + sprintf(output+1, "\033[%c", xkey);
1018 }
1019 use_ucsoutput = FALSE;
1020 goto done;
1021 }
1022 }
1023 goto done;
1024 }
1025
1026 done:
1027
1028 if (end-start > 0) {
1029 #ifdef KEY_DEBUGGING
1030 int i;
1031 printf("generating sequence:");
1032 for (i = start; i < end; i++)
1033 printf(" %02x", (unsigned char) output[i]);
1034 printf("\n");
1035 #endif
1036
1037 if (special) {
1038 /*
1039 * For special control characters, the character set
1040 * should never matter.
1041 */
1042 output[end] = '\0'; /* NUL-terminate */
1043 if (inst->ldisc)
1044 ldisc_send(inst->ldisc, output+start, -2, 1);
1045 } else if (!inst->direct_to_font) {
1046 if (!use_ucsoutput) {
1047 /*
1048 * The stuff we've just generated is assumed to be
1049 * ISO-8859-1! This sounds insane, but `man
1050 * XLookupString' agrees: strings of this type
1051 * returned from the X server are hardcoded to
1052 * 8859-1. Strictly speaking we should be doing
1053 * this using some sort of GtkIMContext, which (if
1054 * we're lucky) would give us our data directly in
1055 * Unicode; but that's not supported in GTK 1.2 as
1056 * far as I can tell, and it's poorly documented
1057 * even in 2.0, so it'll have to wait.
1058 */
1059 if (inst->ldisc)
1060 lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
1061 end-start, 1);
1062 } else {
1063 /*
1064 * We generated our own Unicode key data from the
1065 * keysym, so use that instead.
1066 */
1067 if (inst->ldisc)
1068 luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
1069 }
1070 } else {
1071 /*
1072 * In direct-to-font mode, we just send the string
1073 * exactly as we received it.
1074 */
1075 if (inst->ldisc)
1076 ldisc_send(inst->ldisc, output+start, end-start, 1);
1077 }
1078
1079 show_mouseptr(inst, 0);
1080 term_seen_key_event(inst->term);
1081 }
1082
1083 return TRUE;
1084 }
1085
1086 gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
1087 {
1088 struct gui_data *inst = (struct gui_data *)data;
1089 int shift, ctrl, alt, x, y, button, act;
1090
1091 /* Remember the timestamp. */
1092 inst->input_event_time = event->time;
1093
1094 show_mouseptr(inst, 1);
1095
1096 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
1097 term_scroll(inst->term, 0, -5);
1098 return TRUE;
1099 }
1100 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
1101 term_scroll(inst->term, 0, +5);
1102 return TRUE;
1103 }
1104
1105 shift = event->state & GDK_SHIFT_MASK;
1106 ctrl = event->state & GDK_CONTROL_MASK;
1107 alt = event->state & GDK_MOD1_MASK;
1108
1109 if (event->button == 3 && ctrl) {
1110 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
1111 event->button, event->time);
1112 return TRUE;
1113 }
1114
1115 if (event->button == 1)
1116 button = MBT_LEFT;
1117 else if (event->button == 2)
1118 button = MBT_MIDDLE;
1119 else if (event->button == 3)
1120 button = MBT_RIGHT;
1121 else
1122 return FALSE; /* don't even know what button! */
1123
1124 switch (event->type) {
1125 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
1126 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
1127 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
1128 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
1129 default: return FALSE; /* don't know this event type */
1130 }
1131
1132 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
1133 act != MA_CLICK && act != MA_RELEASE)
1134 return TRUE; /* we ignore these in raw mouse mode */
1135
1136 x = (event->x - inst->cfg.window_border) / inst->font_width;
1137 y = (event->y - inst->cfg.window_border) / inst->font_height;
1138
1139 term_mouse(inst->term, button, translate_button(button), act,
1140 x, y, shift, ctrl, alt);
1141
1142 return TRUE;
1143 }
1144
1145 gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1146 {
1147 struct gui_data *inst = (struct gui_data *)data;
1148 int shift, ctrl, alt, x, y, button;
1149
1150 /* Remember the timestamp. */
1151 inst->input_event_time = event->time;
1152
1153 show_mouseptr(inst, 1);
1154
1155 shift = event->state & GDK_SHIFT_MASK;
1156 ctrl = event->state & GDK_CONTROL_MASK;
1157 alt = event->state & GDK_MOD1_MASK;
1158 if (event->state & GDK_BUTTON1_MASK)
1159 button = MBT_LEFT;
1160 else if (event->state & GDK_BUTTON2_MASK)
1161 button = MBT_MIDDLE;
1162 else if (event->state & GDK_BUTTON3_MASK)
1163 button = MBT_RIGHT;
1164 else
1165 return FALSE; /* don't even know what button! */
1166
1167 x = (event->x - inst->cfg.window_border) / inst->font_width;
1168 y = (event->y - inst->cfg.window_border) / inst->font_height;
1169
1170 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1171 x, y, shift, ctrl, alt);
1172
1173 return TRUE;
1174 }
1175
1176 void frontend_keypress(void *handle)
1177 {
1178 struct gui_data *inst = (struct gui_data *)handle;
1179
1180 /*
1181 * If our child process has exited but not closed, terminate on
1182 * any keypress.
1183 */
1184 if (inst->exited)
1185 exit(0);
1186 }
1187
1188 void notify_remote_exit(void *frontend)
1189 {
1190 struct gui_data *inst = (struct gui_data *)frontend;
1191 int exitcode;
1192
1193 if (!inst->exited &&
1194 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
1195 inst->exited = TRUE;
1196 if (inst->cfg.close_on_exit == FORCE_ON ||
1197 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1198 gtk_main_quit(); /* just go */
1199 if (inst->ldisc) {
1200 ldisc_free(inst->ldisc);
1201 inst->ldisc = NULL;
1202 }
1203 if (inst->back) {
1204 inst->back->free(inst->backhandle);
1205 inst->backhandle = NULL;
1206 inst->back = NULL;
1207 term_provide_resize_fn(inst->term, NULL, 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 unsigned char empty[] = "";
1470 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1471 XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0);
1472 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1473 XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0);
1474 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1475 XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0);
1476 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1477 XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0);
1478 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1479 XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0);
1480 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1481 XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0);
1482 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1483 XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0);
1484 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1485 XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0);
1486 }
1487
1488 /* Store the data in a cut-buffer. */
1489 void store_cutbuffer(char * ptr, int len)
1490 {
1491 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1492 XRotateBuffers(GDK_DISPLAY(), 1);
1493 XStoreBytes(GDK_DISPLAY(), ptr, len);
1494 }
1495
1496 /* Retrieve data from a cut-buffer.
1497 * Returned data needs to be freed with XFree().
1498 */
1499 char * retrieve_cutbuffer(int * nbytes)
1500 {
1501 char * ptr;
1502 ptr = XFetchBytes(GDK_DISPLAY(), nbytes);
1503 if (*nbytes <= 0 && ptr != 0) {
1504 XFree(ptr);
1505 ptr = 0;
1506 }
1507 return ptr;
1508 }
1509
1510 void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
1511 {
1512 struct gui_data *inst = (struct gui_data *)frontend;
1513 if (inst->pasteout_data)
1514 sfree(inst->pasteout_data);
1515 if (inst->pasteout_data_ctext)
1516 sfree(inst->pasteout_data_ctext);
1517 if (inst->pasteout_data_utf8)
1518 sfree(inst->pasteout_data_utf8);
1519
1520 /*
1521 * Set up UTF-8 and compound text paste data. This only happens
1522 * if we aren't in direct-to-font mode using the D800 hack.
1523 */
1524 if (!inst->direct_to_font) {
1525 wchar_t *tmp = data;
1526 int tmplen = len;
1527 XTextProperty tp;
1528 char *list[1];
1529
1530 inst->pasteout_data_utf8 = snewn(len*6, char);
1531 inst->pasteout_data_utf8_len = len*6;
1532 inst->pasteout_data_utf8_len =
1533 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1534 inst->pasteout_data_utf8_len,
1535 CS_UTF8, NULL, NULL, 0);
1536 if (inst->pasteout_data_utf8_len == 0) {
1537 sfree(inst->pasteout_data_utf8);
1538 inst->pasteout_data_utf8 = NULL;
1539 } else {
1540 inst->pasteout_data_utf8 =
1541 sresize(inst->pasteout_data_utf8,
1542 inst->pasteout_data_utf8_len + 1, char);
1543 inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0';
1544 }
1545
1546 /*
1547 * Now let Xlib convert our UTF-8 data into compound text.
1548 */
1549 list[0] = inst->pasteout_data_utf8;
1550 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1,
1551 XCompoundTextStyle, &tp) == 0) {
1552 inst->pasteout_data_ctext = snewn(tp.nitems+1, char);
1553 memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems);
1554 inst->pasteout_data_ctext_len = tp.nitems;
1555 XFree(tp.value);
1556 } else {
1557 inst->pasteout_data_ctext = NULL;
1558 inst->pasteout_data_ctext_len = 0;
1559 }
1560 } else {
1561 inst->pasteout_data_utf8 = NULL;
1562 inst->pasteout_data_utf8_len = 0;
1563 inst->pasteout_data_ctext = NULL;
1564 inst->pasteout_data_ctext_len = 0;
1565 }
1566
1567 inst->pasteout_data = snewn(len*6, char);
1568 inst->pasteout_data_len = len*6;
1569 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1570 data, len, inst->pasteout_data,
1571 inst->pasteout_data_len,
1572 NULL, NULL, NULL);
1573 if (inst->pasteout_data_len == 0) {
1574 sfree(inst->pasteout_data);
1575 inst->pasteout_data = NULL;
1576 } else {
1577 inst->pasteout_data =
1578 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
1579 }
1580
1581 store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len);
1582
1583 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1584 inst->input_event_time)) {
1585 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1586 GDK_SELECTION_TYPE_STRING, 1);
1587 if (inst->pasteout_data_ctext)
1588 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1589 compound_text_atom, 1);
1590 if (inst->pasteout_data_utf8)
1591 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1592 utf8_string_atom, 1);
1593 }
1594
1595 if (must_deselect)
1596 term_deselect(inst->term);
1597 }
1598
1599 void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1600 guint info, guint time_stamp, gpointer data)
1601 {
1602 struct gui_data *inst = (struct gui_data *)data;
1603 if (seldata->target == utf8_string_atom)
1604 gtk_selection_data_set(seldata, seldata->target, 8,
1605 (unsigned char *)inst->pasteout_data_utf8,
1606 inst->pasteout_data_utf8_len);
1607 else if (seldata->target == compound_text_atom)
1608 gtk_selection_data_set(seldata, seldata->target, 8,
1609 (unsigned char *)inst->pasteout_data_ctext,
1610 inst->pasteout_data_ctext_len);
1611 else
1612 gtk_selection_data_set(seldata, seldata->target, 8,
1613 (unsigned char *)inst->pasteout_data,
1614 inst->pasteout_data_len);
1615 }
1616
1617 gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1618 gpointer data)
1619 {
1620 struct gui_data *inst = (struct gui_data *)data;
1621
1622 term_deselect(inst->term);
1623 if (inst->pasteout_data)
1624 sfree(inst->pasteout_data);
1625 if (inst->pasteout_data_ctext)
1626 sfree(inst->pasteout_data_ctext);
1627 if (inst->pasteout_data_utf8)
1628 sfree(inst->pasteout_data_utf8);
1629 inst->pasteout_data = NULL;
1630 inst->pasteout_data_len = 0;
1631 inst->pasteout_data_ctext = NULL;
1632 inst->pasteout_data_ctext_len = 0;
1633 inst->pasteout_data_utf8 = NULL;
1634 inst->pasteout_data_utf8_len = 0;
1635 return TRUE;
1636 }
1637
1638 void request_paste(void *frontend)
1639 {
1640 struct gui_data *inst = (struct gui_data *)frontend;
1641 /*
1642 * In Unix, pasting is asynchronous: all we can do at the
1643 * moment is to call gtk_selection_convert(), and when the data
1644 * comes back _then_ we can call term_do_paste().
1645 */
1646
1647 if (!inst->direct_to_font) {
1648 /*
1649 * First we attempt to retrieve the selection as a UTF-8
1650 * string (which we will convert to the correct code page
1651 * before sending to the session, of course). If that
1652 * fails, selection_received() will be informed and will
1653 * fall back to an ordinary string.
1654 */
1655 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1656 utf8_string_atom,
1657 inst->input_event_time);
1658 } else {
1659 /*
1660 * If we're in direct-to-font mode, we disable UTF-8
1661 * pasting, and go straight to ordinary string data.
1662 */
1663 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1664 GDK_SELECTION_TYPE_STRING,
1665 inst->input_event_time);
1666 }
1667 }
1668
1669 gint idle_paste_func(gpointer data); /* forward ref */
1670
1671 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
1672 guint time, gpointer data)
1673 {
1674 struct gui_data *inst = (struct gui_data *)data;
1675 XTextProperty tp;
1676 char **list;
1677 char *text;
1678 int length, count, ret;
1679 int free_list_required = 0;
1680 int free_required = 0;
1681 int charset;
1682
1683 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
1684 /*
1685 * Failed to get a UTF-8 selection string. Try compound
1686 * text next.
1687 */
1688 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1689 compound_text_atom,
1690 inst->input_event_time);
1691 return;
1692 }
1693
1694 if (seldata->target == compound_text_atom && seldata->length <= 0) {
1695 /*
1696 * Failed to get UTF-8 or compound text. Try an ordinary
1697 * string.
1698 */
1699 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1700 GDK_SELECTION_TYPE_STRING,
1701 inst->input_event_time);
1702 return;
1703 }
1704
1705 /*
1706 * If we have data, but it's not of a type we can deal with,
1707 * we have to ignore the data.
1708 */
1709 if (seldata->length > 0 &&
1710 seldata->type != GDK_SELECTION_TYPE_STRING &&
1711 seldata->type != compound_text_atom &&
1712 seldata->type != utf8_string_atom)
1713 return;
1714
1715 /*
1716 * If we have no data, try looking in a cut buffer.
1717 */
1718 if (seldata->length <= 0) {
1719 text = retrieve_cutbuffer(&length);
1720 if (length == 0)
1721 return;
1722 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1723 * source, so use that as a de-facto standard. */
1724 charset = CS_ISO8859_1;
1725 free_required = 1;
1726 } else {
1727 /*
1728 * Convert COMPOUND_TEXT into UTF-8.
1729 */
1730 if (seldata->type == compound_text_atom) {
1731 tp.value = seldata->data;
1732 tp.encoding = (Atom) seldata->type;
1733 tp.format = seldata->format;
1734 tp.nitems = seldata->length;
1735 ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp,
1736 &list, &count);
1737 if (ret != 0 || count != 1) {
1738 /*
1739 * Compound text failed; fall back to STRING.
1740 */
1741 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1742 GDK_SELECTION_TYPE_STRING,
1743 inst->input_event_time);
1744 return;
1745 }
1746 text = list[0];
1747 length = strlen(list[0]);
1748 charset = CS_UTF8;
1749 free_list_required = 1;
1750 } else {
1751 text = (char *)seldata->data;
1752 length = seldata->length;
1753 charset = (seldata->type == utf8_string_atom ?
1754 CS_UTF8 : inst->ucsdata.line_codepage);
1755 }
1756 }
1757
1758 if (inst->pastein_data)
1759 sfree(inst->pastein_data);
1760
1761 inst->pastein_data = snewn(length, wchar_t);
1762 inst->pastein_data_len = length;
1763 inst->pastein_data_len =
1764 mb_to_wc(charset, 0, text, length,
1765 inst->pastein_data, inst->pastein_data_len);
1766
1767 term_do_paste(inst->term);
1768
1769 if (term_paste_pending(inst->term))
1770 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1771
1772 if (free_list_required)
1773 XFreeStringList(list);
1774 if (free_required)
1775 XFree(text);
1776 }
1777
1778 gint idle_paste_func(gpointer data)
1779 {
1780 struct gui_data *inst = (struct gui_data *)data;
1781
1782 if (term_paste_pending(inst->term))
1783 term_paste(inst->term);
1784 else
1785 gtk_idle_remove(inst->term_paste_idle_id);
1786
1787 return TRUE;
1788 }
1789
1790
1791 void get_clip(void *frontend, wchar_t ** p, int *len)
1792 {
1793 struct gui_data *inst = (struct gui_data *)frontend;
1794
1795 if (p) {
1796 *p = inst->pastein_data;
1797 *len = inst->pastein_data_len;
1798 }
1799 }
1800
1801 static void set_window_titles(struct gui_data *inst)
1802 {
1803 /*
1804 * We must always call set_icon_name after calling set_title,
1805 * since set_title will write both names. Irritating, but such
1806 * is life.
1807 */
1808 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1809 if (!inst->cfg.win_name_always)
1810 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1811 }
1812
1813 void set_title(void *frontend, char *title)
1814 {
1815 struct gui_data *inst = (struct gui_data *)frontend;
1816 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1817 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1818 set_window_titles(inst);
1819 }
1820
1821 void set_icon(void *frontend, char *title)
1822 {
1823 struct gui_data *inst = (struct gui_data *)frontend;
1824 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1825 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1826 set_window_titles(inst);
1827 }
1828
1829 void set_sbar(void *frontend, int total, int start, int page)
1830 {
1831 struct gui_data *inst = (struct gui_data *)frontend;
1832 if (!inst->cfg.scrollbar)
1833 return;
1834 inst->sbar_adjust->lower = 0;
1835 inst->sbar_adjust->upper = total;
1836 inst->sbar_adjust->value = start;
1837 inst->sbar_adjust->page_size = page;
1838 inst->sbar_adjust->step_increment = 1;
1839 inst->sbar_adjust->page_increment = page/2;
1840 inst->ignore_sbar = TRUE;
1841 gtk_adjustment_changed(inst->sbar_adjust);
1842 inst->ignore_sbar = FALSE;
1843 }
1844
1845 void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1846 {
1847 struct gui_data *inst = (struct gui_data *)data;
1848
1849 if (!inst->cfg.scrollbar)
1850 return;
1851 if (!inst->ignore_sbar)
1852 term_scroll(inst->term, 1, (int)adj->value);
1853 }
1854
1855 void sys_cursor(void *frontend, int x, int y)
1856 {
1857 /*
1858 * This is meaningless under X.
1859 */
1860 }
1861
1862 /*
1863 * This is still called when mode==BELL_VISUAL, even though the
1864 * visual bell is handled entirely within terminal.c, because we
1865 * may want to perform additional actions on any kind of bell (for
1866 * example, taskbar flashing in Windows).
1867 */
1868 void do_beep(void *frontend, int mode)
1869 {
1870 if (mode == BELL_DEFAULT)
1871 gdk_beep();
1872 }
1873
1874 int char_width(Context ctx, int uc)
1875 {
1876 /*
1877 * Under X, any fixed-width font really _is_ fixed-width.
1878 * Double-width characters will be dealt with using a separate
1879 * font. For the moment we can simply return 1.
1880 */
1881 return 1;
1882 }
1883
1884 Context get_ctx(void *frontend)
1885 {
1886 struct gui_data *inst = (struct gui_data *)frontend;
1887 struct draw_ctx *dctx;
1888
1889 if (!inst->area->window)
1890 return NULL;
1891
1892 dctx = snew(struct draw_ctx);
1893 dctx->inst = inst;
1894 dctx->gc = gdk_gc_new(inst->area->window);
1895 return dctx;
1896 }
1897
1898 void free_ctx(Context ctx)
1899 {
1900 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1901 /* struct gui_data *inst = dctx->inst; */
1902 GdkGC *gc = dctx->gc;
1903 gdk_gc_unref(gc);
1904 sfree(dctx);
1905 }
1906
1907 /*
1908 * Draw a line of text in the window, at given character
1909 * coordinates, in given attributes.
1910 *
1911 * We are allowed to fiddle with the contents of `text'.
1912 */
1913 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
1914 unsigned long attr, int lattr)
1915 {
1916 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1917 struct gui_data *inst = dctx->inst;
1918 GdkGC *gc = dctx->gc;
1919 int ncombining, combining;
1920 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
1921 int monochrome = gtk_widget_get_visual(inst->area)->depth == 1;
1922
1923 if (attr & TATTR_COMBINING) {
1924 ncombining = len;
1925 len = 1;
1926 } else
1927 ncombining = 1;
1928
1929 nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT);
1930 nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT);
1931 if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) {
1932 t = nfg;
1933 nfg = nbg;
1934 nbg = t;
1935 }
1936 if (inst->cfg.bold_colour && (attr & ATTR_BOLD)) {
1937 if (nfg < 16) nfg |= 8;
1938 else if (nfg >= 256) nfg |= 1;
1939 }
1940 if (inst->cfg.bold_colour && (attr & ATTR_BLINK)) {
1941 if (nbg < 16) nbg |= 8;
1942 else if (nbg >= 256) nbg |= 1;
1943 }
1944 if ((attr & TATTR_ACTCURS) && !monochrome) {
1945 nfg = 260;
1946 nbg = 261;
1947 }
1948
1949 fontid = shadow = 0;
1950
1951 if (attr & ATTR_WIDE) {
1952 widefactor = 2;
1953 fontid |= 2;
1954 } else {
1955 widefactor = 1;
1956 }
1957
1958 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
1959 if (inst->fonts[fontid | 1])
1960 fontid |= 1;
1961 else
1962 shadow = 1;
1963 }
1964
1965 if ((lattr & LATTR_MODE) != LATTR_NORM) {
1966 x *= 2;
1967 if (x >= inst->term->cols)
1968 return;
1969 if (x + len*2*widefactor > inst->term->cols)
1970 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1971 rlen = len * 2;
1972 } else
1973 rlen = len;
1974
1975 {
1976 GdkRectangle r;
1977
1978 r.x = x*inst->font_width+inst->cfg.window_border;
1979 r.y = y*inst->font_height+inst->cfg.window_border;
1980 r.width = rlen*widefactor*inst->font_width;
1981 r.height = inst->font_height;
1982 gdk_gc_set_clip_rectangle(gc, &r);
1983 }
1984
1985 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1986 gdk_draw_rectangle(inst->pixmap, gc, 1,
1987 x*inst->font_width+inst->cfg.window_border,
1988 y*inst->font_height+inst->cfg.window_border,
1989 rlen*widefactor*inst->font_width, inst->font_height);
1990
1991 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1992 {
1993 GdkWChar *gwcs;
1994 gchar *gcs;
1995 wchar_t *wcs;
1996 int i;
1997
1998 wcs = snewn(len*ncombining+1, wchar_t);
1999 for (i = 0; i < len*ncombining; i++) {
2000 wcs[i] = text[i];
2001 }
2002
2003 if (inst->fonts[fontid] == NULL && (fontid & 2)) {
2004 /*
2005 * We've been given ATTR_WIDE, but have no wide font.
2006 * Fall back to the non-wide font.
2007 */
2008 fontid &= ~2;
2009 }
2010
2011 if (inst->fonts[fontid] == NULL) {
2012 /*
2013 * The font for this contingency does not exist. So we
2014 * display nothing at all; such is life.
2015 */
2016 } else if (inst->fontinfo[fontid].is_wide) {
2017 /*
2018 * At least one version of gdk_draw_text_wc() has a
2019 * weird bug whereby it reads `len' elements of the
2020 * input string, but only draws `len/2'. Hence I'm
2021 * going to make its input array twice as long as it
2022 * theoretically needs to be, and pass in twice the
2023 * actual number of characters. If a fixed gdk actually
2024 * takes the doubled length seriously, then (a) the
2025 * array will stand scrutiny up to the full length, (b)
2026 * the spare elements of the array are full of zeroes
2027 * which will probably be an empty glyph in the font,
2028 * and (c) the clip rectangle should prevent it causing
2029 * trouble anyway.
2030 */
2031 gwcs = snewn(len*2+1, GdkWChar);
2032 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2033 /*
2034 * FIXME: when we have a wide-char equivalent of
2035 * from_unicode, use it instead of this.
2036 */
2037 for (combining = 0; combining < ncombining; combining++) {
2038 for (i = 0; i <= len; i++)
2039 gwcs[i] = wcs[i + combining];
2040 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2041 x*inst->font_width+inst->cfg.window_border,
2042 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2043 gwcs, len*2);
2044 if (shadow)
2045 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
2046 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2047 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2048 gwcs, len*2);
2049 }
2050 sfree(gwcs);
2051 } else {
2052 gcs = snewn(len+1, gchar);
2053 for (combining = 0; combining < ncombining; combining++) {
2054 wc_to_mb(inst->fontinfo[fontid].charset, 0,
2055 wcs + combining, len, gcs, len, ".", NULL, NULL);
2056 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2057 x*inst->font_width+inst->cfg.window_border,
2058 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2059 gcs, len);
2060 if (shadow)
2061 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
2062 x*inst->font_width+inst->cfg.window_border+inst->cfg.shadowboldoffset,
2063 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2064 gcs, len);
2065 }
2066 sfree(gcs);
2067 }
2068 sfree(wcs);
2069 }
2070
2071 if (attr & ATTR_UNDER) {
2072 int uheight = inst->fonts[0]->ascent + 1;
2073 if (uheight >= inst->font_height)
2074 uheight = inst->font_height - 1;
2075 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
2076 y*inst->font_height + uheight + inst->cfg.window_border,
2077 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
2078 y*inst->font_height + uheight + inst->cfg.window_border);
2079 }
2080
2081 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2082 /*
2083 * I can't find any plausible StretchBlt equivalent in the
2084 * X server, so I'm going to do this the slow and painful
2085 * way. This will involve repeated calls to
2086 * gdk_draw_pixmap() to stretch the text horizontally. It's
2087 * O(N^2) in time and O(N) in network bandwidth, but you
2088 * try thinking of a better way. :-(
2089 */
2090 int i;
2091 for (i = 0; i < len * widefactor * inst->font_width; i++) {
2092 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2093 x*inst->font_width+inst->cfg.window_border + 2*i,
2094 y*inst->font_height+inst->cfg.window_border,
2095 x*inst->font_width+inst->cfg.window_border + 2*i+1,
2096 y*inst->font_height+inst->cfg.window_border,
2097 len * widefactor * inst->font_width - i, inst->font_height);
2098 }
2099 len *= 2;
2100 if ((lattr & LATTR_MODE) != LATTR_WIDE) {
2101 int dt, db;
2102 /* Now stretch vertically, in the same way. */
2103 if ((lattr & LATTR_MODE) == LATTR_BOT)
2104 dt = 0, db = 1;
2105 else
2106 dt = 1, db = 0;
2107 for (i = 0; i < inst->font_height; i+=2) {
2108 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
2109 x*inst->font_width+inst->cfg.window_border,
2110 y*inst->font_height+inst->cfg.window_border+dt*i+db,
2111 x*inst->font_width+inst->cfg.window_border,
2112 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
2113 len * widefactor * inst->font_width, inst->font_height-i-1);
2114 }
2115 }
2116 }
2117 }
2118
2119 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
2120 unsigned long attr, int lattr)
2121 {
2122 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2123 struct gui_data *inst = dctx->inst;
2124 GdkGC *gc = dctx->gc;
2125 int widefactor;
2126
2127 do_text_internal(ctx, x, y, text, len, attr, lattr);
2128
2129 if (attr & ATTR_WIDE) {
2130 widefactor = 2;
2131 } else {
2132 widefactor = 1;
2133 }
2134
2135 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2136 x *= 2;
2137 if (x >= inst->term->cols)
2138 return;
2139 if (x + len*2*widefactor > inst->term->cols)
2140 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2141 len *= 2;
2142 }
2143
2144 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2145 x*inst->font_width+inst->cfg.window_border,
2146 y*inst->font_height+inst->cfg.window_border,
2147 x*inst->font_width+inst->cfg.window_border,
2148 y*inst->font_height+inst->cfg.window_border,
2149 len*widefactor*inst->font_width, inst->font_height);
2150 }
2151
2152 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
2153 unsigned long attr, int lattr)
2154 {
2155 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
2156 struct gui_data *inst = dctx->inst;
2157 GdkGC *gc = dctx->gc;
2158
2159 int active, passive, widefactor;
2160
2161 if (attr & TATTR_PASCURS) {
2162 attr &= ~TATTR_PASCURS;
2163 passive = 1;
2164 } else
2165 passive = 0;
2166 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
2167 attr &= ~TATTR_ACTCURS;
2168 active = 1;
2169 } else
2170 active = 0;
2171 do_text_internal(ctx, x, y, text, len, attr, lattr);
2172
2173 if (attr & TATTR_COMBINING)
2174 len = 1;
2175
2176 if (attr & ATTR_WIDE) {
2177 widefactor = 2;
2178 } else {
2179 widefactor = 1;
2180 }
2181
2182 if ((lattr & LATTR_MODE) != LATTR_NORM) {
2183 x *= 2;
2184 if (x >= inst->term->cols)
2185 return;
2186 if (x + len*2*widefactor > inst->term->cols)
2187 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
2188 len *= 2;
2189 }
2190
2191 if (inst->cfg.cursor_type == 0) {
2192 /*
2193 * An active block cursor will already have been done by
2194 * the above do_text call, so we only need to do anything
2195 * if it's passive.
2196 */
2197 if (passive) {
2198 gdk_gc_set_foreground(gc, &inst->cols[261]);
2199 gdk_draw_rectangle(inst->pixmap, gc, 0,
2200 x*inst->font_width+inst->cfg.window_border,
2201 y*inst->font_height+inst->cfg.window_border,
2202 len*widefactor*inst->font_width-1, inst->font_height-1);
2203 }
2204 } else {
2205 int uheight;
2206 int startx, starty, dx, dy, length, i;
2207
2208 int char_width;
2209
2210 if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM)
2211 char_width = 2*inst->font_width;
2212 else
2213 char_width = inst->font_width;
2214
2215 if (inst->cfg.cursor_type == 1) {
2216 uheight = inst->fonts[0]->ascent + 1;
2217 if (uheight >= inst->font_height)
2218 uheight = inst->font_height - 1;
2219
2220 startx = x * inst->font_width + inst->cfg.window_border;
2221 starty = y * inst->font_height + inst->cfg.window_border + uheight;
2222 dx = 1;
2223 dy = 0;
2224 length = len * widefactor * char_width;
2225 } else {
2226 int xadjust = 0;
2227 if (attr & TATTR_RIGHTCURS)
2228 xadjust = char_width - 1;
2229 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
2230 starty = y * inst->font_height + inst->cfg.window_border;
2231 dx = 0;
2232 dy = 1;
2233 length = inst->font_height;
2234 }
2235
2236 gdk_gc_set_foreground(gc, &inst->cols[261]);
2237 if (passive) {
2238 for (i = 0; i < length; i++) {
2239 if (i % 2 == 0) {
2240 gdk_draw_point(inst->pixmap, gc, startx, starty);
2241 }
2242 startx += dx;
2243 starty += dy;
2244 }
2245 } else if (active) {
2246 gdk_draw_line(inst->pixmap, gc, startx, starty,
2247 startx + (length-1) * dx, starty + (length-1) * dy);
2248 } /* else no cursor (e.g., blinked off) */
2249 }
2250
2251 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
2252 x*inst->font_width+inst->cfg.window_border,
2253 y*inst->font_height+inst->cfg.window_border,
2254 x*inst->font_width+inst->cfg.window_border,
2255 y*inst->font_height+inst->cfg.window_border,
2256 len*widefactor*inst->font_width, inst->font_height);
2257 }
2258
2259 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
2260 {
2261 /*
2262 * Truly hideous hack: GTK doesn't allow us to set the mouse
2263 * cursor foreground and background colours unless we've _also_
2264 * created our own cursor from bitmaps. Therefore, I need to
2265 * load the `cursor' font and draw glyphs from it on to
2266 * pixmaps, in order to construct my cursors with the fg and bg
2267 * I want. This is a gross hack, but it's more self-contained
2268 * than linking in Xlib to find the X window handle to
2269 * inst->area and calling XRecolorCursor, and it's more
2270 * futureproof than hard-coding the shapes as bitmap arrays.
2271 */
2272 static GdkFont *cursor_font = NULL;
2273 GdkPixmap *source, *mask;
2274 GdkGC *gc;
2275 GdkColor cfg = { 0, 65535, 65535, 65535 };
2276 GdkColor cbg = { 0, 0, 0, 0 };
2277 GdkColor dfg = { 1, 65535, 65535, 65535 };
2278 GdkColor dbg = { 0, 0, 0, 0 };
2279 GdkCursor *ret;
2280 gchar text[2];
2281 gint lb, rb, wid, asc, desc, w, h, x, y;
2282
2283 if (cursor_val == -2) {
2284 gdk_font_unref(cursor_font);
2285 return NULL;
2286 }
2287
2288 if (cursor_val >= 0 && !cursor_font)
2289 cursor_font = gdk_font_load("cursor");
2290
2291 /*
2292 * Get the text extent of the cursor in question. We use the
2293 * mask character for this, because it's typically slightly
2294 * bigger than the main character.
2295 */
2296 if (cursor_val >= 0) {
2297 text[1] = '\0';
2298 text[0] = (char)cursor_val + 1;
2299 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
2300 w = rb-lb; h = asc+desc; x = -lb; y = asc;
2301 } else {
2302 w = h = 1;
2303 x = y = 0;
2304 }
2305
2306 source = gdk_pixmap_new(NULL, w, h, 1);
2307 mask = gdk_pixmap_new(NULL, w, h, 1);
2308
2309 /*
2310 * Draw the mask character on the mask pixmap.
2311 */
2312 gc = gdk_gc_new(mask);
2313 gdk_gc_set_foreground(gc, &dbg);
2314 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
2315 if (cursor_val >= 0) {
2316 text[1] = '\0';
2317 text[0] = (char)cursor_val + 1;
2318 gdk_gc_set_foreground(gc, &dfg);
2319 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
2320 }
2321 gdk_gc_unref(gc);
2322
2323 /*
2324 * Draw the main character on the source pixmap.
2325 */
2326 gc = gdk_gc_new(source);
2327 gdk_gc_set_foreground(gc, &dbg);
2328 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
2329 if (cursor_val >= 0) {
2330 text[1] = '\0';
2331 text[0] = (char)cursor_val;
2332 gdk_gc_set_foreground(gc, &dfg);
2333 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
2334 }
2335 gdk_gc_unref(gc);
2336
2337 /*
2338 * Create the cursor.
2339 */
2340 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
2341
2342 /*
2343 * Clean up.
2344 */
2345 gdk_pixmap_unref(source);
2346 gdk_pixmap_unref(mask);
2347
2348 return ret;
2349 }
2350
2351 void modalfatalbox(char *p, ...)
2352 {
2353 va_list ap;
2354 fprintf(stderr, "FATAL ERROR: ");
2355 va_start(ap, p);
2356 vfprintf(stderr, p, ap);
2357 va_end(ap);
2358 fputc('\n', stderr);
2359 exit(1);
2360 }
2361
2362 void cmdline_error(char *p, ...)
2363 {
2364 va_list ap;
2365 fprintf(stderr, "%s: ", appname);
2366 va_start(ap, p);
2367 vfprintf(stderr, p, ap);
2368 va_end(ap);
2369 fputc('\n', stderr);
2370 exit(1);
2371 }
2372
2373 char *get_x_display(void *frontend)
2374 {
2375 return gdk_get_display();
2376 }
2377
2378 long get_windowid(void *frontend)
2379 {
2380 struct gui_data *inst = (struct gui_data *)frontend;
2381 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
2382 }
2383
2384 static void help(FILE *fp) {
2385 if(fprintf(fp,
2386 "pterm option summary:\n"
2387 "\n"
2388 " --display DISPLAY Specify X display to use (note '--')\n"
2389 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2390 " -fn FONT Normal text font\n"
2391 " -fb FONT Bold text font\n"
2392 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2393 " -sl LINES Number of lines of scrollback\n"
2394 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2395 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2396 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2397 " -T TITLE Window title\n"
2398 " -ut, +ut Do(default) or do not update utmp\n"
2399 " -ls, +ls Do(default) or do not make shell a login shell\n"
2400 " -sb, +sb Do(default) or do not display a scrollbar\n"
2401 " -log PATH Log all output to a file\n"
2402 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2403 " -xrm RESOURCE-STRING Set an X resource\n"
2404 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2405 ) < 0 || fflush(fp) < 0) {
2406 perror("output error");
2407 exit(1);
2408 }
2409 }
2410
2411 int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch,
2412 struct gui_data *inst, Config *cfg)
2413 {
2414 int err = 0;
2415 char *val;
2416
2417 /*
2418 * Macros to make argument handling easier. Note that because
2419 * they need to call `continue', they cannot be contained in
2420 * the usual do {...} while (0) wrapper to make them
2421 * syntactically single statements; hence it is not legal to
2422 * use one of these macros as an unbraced statement between
2423 * `if' and `else'.
2424 */
2425 #define EXPECTS_ARG { \
2426 if (--argc <= 0) { \
2427 err = 1; \
2428 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2429 continue; \
2430 } else \
2431 val = *++argv; \
2432 }
2433 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2434
2435 while (--argc > 0) {
2436 char *p = *++argv;
2437 int ret;
2438
2439 /*
2440 * Shameless cheating. Debian requires all X terminal
2441 * emulators to support `-T title'; but
2442 * cmdline_process_param will eat -T (it means no-pty) and
2443 * complain that pterm doesn't support it. So, in pterm
2444 * only, we convert -T into -title.
2445 */
2446 if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
2447 !strcmp(p, "-T"))
2448 p = "-title";
2449
2450 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2451 do_everything ? 1 : -1, cfg);
2452
2453 if (ret == -2) {
2454 cmdline_error("option \"%s\" requires an argument", p);
2455 } else if (ret == 2) {
2456 --argc, ++argv; /* skip next argument */
2457 continue;
2458 } else if (ret == 1) {
2459 continue;
2460 }
2461
2462 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
2463 EXPECTS_ARG;
2464 SECOND_PASS_ONLY;
2465 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2466 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
2467
2468 } else if (!strcmp(p, "-fb")) {
2469 EXPECTS_ARG;
2470 SECOND_PASS_ONLY;
2471 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2472 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
2473
2474 } else if (!strcmp(p, "-fw")) {
2475 EXPECTS_ARG;
2476 SECOND_PASS_ONLY;
2477 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2478 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
2479
2480 } else if (!strcmp(p, "-fwb")) {
2481 EXPECTS_ARG;
2482 SECOND_PASS_ONLY;
2483 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2484 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
2485
2486 } else if (!strcmp(p, "-cs")) {
2487 EXPECTS_ARG;
2488 SECOND_PASS_ONLY;
2489 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2490 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2491
2492 } else if (!strcmp(p, "-geometry")) {
2493 int flags, x, y;
2494 unsigned int w, h;
2495 EXPECTS_ARG;
2496 SECOND_PASS_ONLY;
2497
2498 flags = XParseGeometry(val, &x, &y, &w, &h);
2499 if (flags & WidthValue)
2500 cfg->width = (int)w;
2501 if (flags & HeightValue)
2502 cfg->height = (int)h;
2503
2504 if (flags & (XValue | YValue)) {
2505 inst->xpos = x;
2506 inst->ypos = y;
2507 inst->gotpos = TRUE;
2508 inst->gravity = ((flags & XNegative ? 1 : 0) |
2509 (flags & YNegative ? 2 : 0));
2510 }
2511
2512 } else if (!strcmp(p, "-sl")) {
2513 EXPECTS_ARG;
2514 SECOND_PASS_ONLY;
2515 cfg->savelines = atoi(val);
2516
2517 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2518 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
2519 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
2520 GdkColor col;
2521
2522 EXPECTS_ARG;
2523 SECOND_PASS_ONLY;
2524 if (!gdk_color_parse(val, &col)) {
2525 err = 1;
2526 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2527 appname, val);
2528 } else {
2529 int index;
2530 index = (!strcmp(p, "-fg") ? 0 :
2531 !strcmp(p, "-bg") ? 2 :
2532 !strcmp(p, "-bfg") ? 1 :
2533 !strcmp(p, "-bbg") ? 3 :
2534 !strcmp(p, "-cfg") ? 4 :
2535 !strcmp(p, "-cbg") ? 5 : -1);
2536 assert(index != -1);
2537 cfg->colours[index][0] = col.red / 256;
2538 cfg->colours[index][1] = col.green / 256;
2539 cfg->colours[index][2] = col.blue / 256;
2540 }
2541
2542 } else if (use_pty_argv && !strcmp(p, "-e")) {
2543 /* This option swallows all further arguments. */
2544 if (!do_everything)
2545 break;
2546
2547 if (--argc > 0) {
2548 int i;
2549 pty_argv = snewn(argc+1, char *);
2550 ++argv;
2551 for (i = 0; i < argc; i++)
2552 pty_argv[i] = argv[i];
2553 pty_argv[argc] = NULL;
2554 break; /* finished command-line processing */
2555 } else
2556 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2557 appname);
2558
2559 } else if (!strcmp(p, "-title")) {
2560 EXPECTS_ARG;
2561 SECOND_PASS_ONLY;
2562 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2563 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
2564
2565 } else if (!strcmp(p, "-log")) {
2566 EXPECTS_ARG;
2567 SECOND_PASS_ONLY;
2568 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2569 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
2570 cfg->logtype = LGTYP_DEBUG;
2571
2572 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
2573 SECOND_PASS_ONLY;
2574 cfg->stamp_utmp = 0;
2575
2576 } else if (!strcmp(p, "-ut")) {
2577 SECOND_PASS_ONLY;
2578 cfg->stamp_utmp = 1;
2579
2580 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
2581 SECOND_PASS_ONLY;
2582 cfg->login_shell = 0;
2583
2584 } else if (!strcmp(p, "-ls")) {
2585 SECOND_PASS_ONLY;
2586 cfg->login_shell = 1;
2587
2588 } else if (!strcmp(p, "-nethack")) {
2589 SECOND_PASS_ONLY;
2590 cfg->nethack_keypad = 1;
2591
2592 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2593 SECOND_PASS_ONLY;
2594 cfg->scrollbar = 0;
2595
2596 } else if (!strcmp(p, "-sb")) {
2597 SECOND_PASS_ONLY;
2598 cfg->scrollbar = 0;
2599
2600 } else if (!strcmp(p, "-name")) {
2601 EXPECTS_ARG;
2602 app_name = val;
2603
2604 } else if (!strcmp(p, "-xrm")) {
2605 EXPECTS_ARG;
2606 provide_xrm_string(val);
2607
2608 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2609 help(stdout);
2610 exit(0);
2611
2612 } else if (!strcmp(p, "-pgpfp")) {
2613 pgp_fingerprints();
2614 exit(1);
2615
2616 } else if(p[0] != '-' && (!do_everything ||
2617 process_nonoption_arg(p, cfg,
2618 allow_launch))) {
2619 /* do nothing */
2620
2621 } else {
2622 err = 1;
2623 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
2624 }
2625 }
2626
2627 return err;
2628 }
2629
2630 /*
2631 * This function retrieves the character set encoding of a font. It
2632 * returns the character set without the X11 hack (in case the user
2633 * asks to use the font's own encoding).
2634 */
2635 static int set_font_info(struct gui_data *inst, int fontid)
2636 {
2637 GdkFont *font = inst->fonts[fontid];
2638 XFontStruct *xfs = GDK_FONT_XFONT(font);
2639 Display *disp = GDK_FONT_XDISPLAY(font);
2640 Atom charset_registry, charset_encoding;
2641 unsigned long registry_ret, encoding_ret;
2642 int retval = CS_NONE;
2643
2644 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2645 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2646 inst->fontinfo[fontid].charset = CS_NONE;
2647 inst->fontinfo[fontid].is_wide = 0;
2648 if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
2649 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2650 char *reg, *enc;
2651 reg = XGetAtomName(disp, (Atom)registry_ret);
2652 enc = XGetAtomName(disp, (Atom)encoding_ret);
2653 if (reg && enc) {
2654 char *encoding = dupcat(reg, "-", enc, NULL);
2655 retval = inst->fontinfo[fontid].charset =
2656 charset_from_xenc(encoding);
2657 /* FIXME: when libcharset supports wide encodings fix this. */
2658 if (!strcasecmp(encoding, "iso10646-1")) {
2659 inst->fontinfo[fontid].is_wide = 1;
2660 retval = CS_UTF8;
2661 }
2662
2663 /*
2664 * Hack for X line-drawing characters: if the primary
2665 * font is encoded as ISO-8859-anything, and has valid
2666 * glyphs in the first 32 char positions, it is assumed
2667 * that those glyphs are the VT100 line-drawing
2668 * character set.
2669 *
2670 * Actually, we'll hack even harder by only checking
2671 * position 0x19 (vertical line, VT100 linedrawing
2672 * `x'). Then we can check it easily by seeing if the
2673 * ascent and descent differ.
2674 */
2675 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2676 int lb, rb, wid, asc, desc;
2677 gchar text[2];
2678
2679 text[1] = '\0';
2680 text[0] = '\x12';
2681 gdk_string_extents(inst->fonts[fontid], text,
2682 &lb, &rb, &wid, &asc, &desc);
2683 if (asc != desc)
2684 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2685 }
2686
2687 sfree(encoding);
2688 }
2689 }
2690
2691 return retval;
2692 }
2693
2694 int uxsel_input_add(int fd, int rwx) {
2695 int flags = 0;
2696 if (rwx & 1) flags |= GDK_INPUT_READ;
2697 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2698 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2699 return gdk_input_add(fd, flags, fd_input_func, NULL);
2700 }
2701
2702 void uxsel_input_remove(int id) {
2703 gdk_input_remove(id);
2704 }
2705
2706 char *guess_derived_font_name(GdkFont *font, int bold, int wide)
2707 {
2708 XFontStruct *xfs = GDK_FONT_XFONT(font);
2709 Display *disp = GDK_FONT_XDISPLAY(font);
2710 Atom fontprop = XInternAtom(disp, "FONT", False);
2711 unsigned long ret;
2712 if (XGetFontProperty(xfs, fontprop, &ret)) {
2713 char *name = XGetAtomName(disp, (Atom)ret);
2714 if (name && name[0] == '-') {
2715 char *strings[13];
2716 char *dupname, *extrafree = NULL, *ret;
2717 char *p, *q;
2718 int nstr;
2719
2720 p = q = dupname = dupstr(name); /* skip initial minus */
2721 nstr = 0;
2722
2723 while (*p && nstr < lenof(strings)) {
2724 if (*p == '-') {
2725 *p = '\0';
2726 strings[nstr++] = p+1;
2727 }
2728 p++;
2729 }
2730
2731 if (nstr < lenof(strings))
2732 return NULL; /* XLFD was malformed */
2733
2734 if (bold)
2735 strings[2] = "bold";
2736
2737 if (wide) {
2738 /* 4 is `wideness', which obviously may have changed. */
2739 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2740 strings[4] = strings[5] = "*";
2741 strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
2742 }
2743
2744 ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
2745 "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
2746 "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
2747 "-", strings[ 9], "-", strings[10], "-", strings[11],
2748 "-", strings[12], NULL);
2749 sfree(extrafree);
2750 sfree(dupname);
2751
2752 return ret;
2753 }
2754 }
2755 return NULL;
2756 }
2757
2758 void setup_fonts_ucs(struct gui_data *inst)
2759 {
2760 int font_charset;
2761 char *name;
2762 int guessed;
2763
2764 if (inst->fonts[0])
2765 gdk_font_unref(inst->fonts[0]);
2766 if (inst->fonts[1])
2767 gdk_font_unref(inst->fonts[1]);
2768 if (inst->fonts[2])
2769 gdk_font_unref(inst->fonts[2]);
2770 if (inst->fonts[3])
2771 gdk_font_unref(inst->fonts[3]);
2772
2773 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2774 if (!inst->fonts[0]) {
2775 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2776 inst->cfg.font.name);
2777 exit(1);
2778 }
2779 font_charset = set_font_info(inst, 0);
2780
2781 if (inst->cfg.shadowbold) {
2782 inst->fonts[1] = NULL;
2783 } else {
2784 if (inst->cfg.boldfont.name[0]) {
2785 name = inst->cfg.boldfont.name;
2786 guessed = FALSE;
2787 } else {
2788 name = guess_derived_font_name(inst->fonts[0], TRUE, FALSE);
2789 guessed = TRUE;
2790 }
2791 inst->fonts[1] = name ? gdk_font_load(name) : NULL;
2792 if (inst->fonts[1]) {
2793 set_font_info(inst, 1);
2794 } else if (!guessed) {
2795 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2796 inst->cfg.boldfont.name);
2797 exit(1);
2798 }
2799 if (guessed)
2800 sfree(name);
2801 }
2802
2803 if (inst->cfg.widefont.name[0]) {
2804 name = inst->cfg.widefont.name;
2805 guessed = FALSE;
2806 } else {
2807 name = guess_derived_font_name(inst->fonts[0], FALSE, TRUE);
2808 guessed = TRUE;
2809 }
2810 inst->fonts[2] = name ? gdk_font_load(name) : NULL;
2811 if (inst->fonts[2]) {
2812 set_font_info(inst, 2);
2813 } else if (!guessed) {
2814 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2815 inst->cfg.widefont.name);
2816 exit(1);
2817 }
2818 if (guessed)
2819 sfree(name);
2820
2821 if (inst->cfg.shadowbold) {
2822 inst->fonts[3] = NULL;
2823 } else {
2824 if (inst->cfg.wideboldfont.name[0]) {
2825 name = inst->cfg.wideboldfont.name;
2826 guessed = FALSE;
2827 } else {
2828 /*
2829 * Here we have some choices. We can widen the bold font,
2830 * bolden the wide font, or widen and bolden the standard
2831 * font. Try them all, in that order!
2832 */
2833 if (inst->cfg.widefont.name[0])
2834 name = guess_derived_font_name(inst->fonts[2], TRUE, FALSE);
2835 else if (inst->cfg.boldfont.name[0])
2836 name = guess_derived_font_name(inst->fonts[1], FALSE, TRUE);
2837 else
2838 name = guess_derived_font_name(inst->fonts[0], TRUE, TRUE);
2839 guessed = TRUE;
2840 }
2841 inst->fonts[3] = name ? gdk_font_load(name) : NULL;
2842 if (inst->fonts[3]) {
2843 set_font_info(inst, 3);
2844 } else if (!guessed) {
2845 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n", appname,
2846 inst->cfg.wideboldfont.name);
2847 exit(1);
2848 }
2849 if (guessed)
2850 sfree(name);
2851 }
2852
2853 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2854 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2855
2856 inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage,
2857 inst->cfg.utf8_override, font_charset,
2858 inst->cfg.vtmode);
2859 }
2860
2861 void set_geom_hints(struct gui_data *inst)
2862 {
2863 GdkGeometry geom;
2864 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2865 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2866 geom.max_width = geom.max_height = -1;
2867 geom.base_width = 2*inst->cfg.window_border;
2868 geom.base_height = 2*inst->cfg.window_border;
2869 geom.width_inc = inst->font_width;
2870 geom.height_inc = inst->font_height;
2871 geom.min_aspect = geom.max_aspect = 0;
2872 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2873 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2874 GDK_HINT_RESIZE_INC);
2875 }
2876
2877 void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2878 {
2879 struct gui_data *inst = (struct gui_data *)data;
2880 term_clrsb(inst->term);
2881 }
2882
2883 void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2884 {
2885 struct gui_data *inst = (struct gui_data *)data;
2886 term_pwron(inst->term, TRUE);
2887 if (inst->ldisc)
2888 ldisc_send(inst->ldisc, NULL, 0, 0);
2889 }
2890
2891 void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2892 {
2893 struct gui_data *inst = (struct gui_data *)data;
2894 term_copyall(inst->term);
2895 }
2896
2897 void special_menuitem(GtkMenuItem *item, gpointer data)
2898 {
2899 struct gui_data *inst = (struct gui_data *)data;
2900 int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
2901 "user-data"));
2902
2903 if (inst->back)
2904 inst->back->special(inst->backhandle, code);
2905 }
2906
2907 void about_menuitem(GtkMenuItem *item, gpointer data)
2908 {
2909 struct gui_data *inst = (struct gui_data *)data;
2910 about_box(inst->window);
2911 }
2912
2913 void event_log_menuitem(GtkMenuItem *item, gpointer data)
2914 {
2915 struct gui_data *inst = (struct gui_data *)data;
2916 showeventlog(inst->eventlogstuff, inst->window);
2917 }
2918
2919 void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2920 {
2921 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2922 static const int ww[] = {
2923 256, 257, 258, 259, 260, 261,
2924 0, 8, 1, 9, 2, 10, 3, 11,
2925 4, 12, 5, 13, 6, 14, 7, 15
2926 };
2927 struct gui_data *inst = (struct gui_data *)data;
2928 char *title = dupcat(appname, " Reconfiguration", NULL);
2929 Config cfg2, oldcfg;
2930 int i, need_size;
2931
2932 assert(lenof(ww) == NCFGCOLOURS);
2933
2934 if (inst->reconfiguring)
2935 return;
2936 else
2937 inst->reconfiguring = TRUE;
2938
2939 cfg2 = inst->cfg; /* structure copy */
2940
2941 if (do_config_box(title, &cfg2, 1,
2942 inst->back?inst->back->cfg_info(inst->backhandle):0)) {
2943
2944 oldcfg = inst->cfg; /* structure copy */
2945 inst->cfg = cfg2; /* structure copy */
2946
2947 /* Pass new config data to the logging module */
2948 log_reconfig(inst->logctx, &cfg2);
2949 /*
2950 * Flush the line discipline's edit buffer in the case
2951 * where local editing has just been disabled.
2952 */
2953 if (inst->ldisc)
2954 ldisc_send(inst->ldisc, NULL, 0, 0);
2955 /* Pass new config data to the terminal */
2956 term_reconfig(inst->term, &cfg2);
2957 /* Pass new config data to the back end */
2958 if (inst->back)
2959 inst->back->reconfig(inst->backhandle, &cfg2);
2960
2961 /*
2962 * Just setting inst->cfg is sufficient to cause colour
2963 * setting changes to appear on the next ESC]R palette
2964 * reset. But we should also check whether any colour
2965 * settings have been changed, and revert the ones that
2966 * have to the new default, on the assumption that the user
2967 * is most likely to want an immediate update.
2968 */
2969 for (i = 0; i < NCFGCOLOURS; i++) {
2970 if (oldcfg.colours[i][0] != cfg2.colours[i][0] ||
2971 oldcfg.colours[i][1] != cfg2.colours[i][1] ||
2972 oldcfg.colours[i][2] != cfg2.colours[i][2]) {
2973 real_palette_set(inst, ww[i], cfg2.colours[i][0],
2974 cfg2.colours[i][1],
2975 cfg2.colours[i][2]);
2976
2977 /*
2978 * If the default background has changed, we must
2979 * repaint the space in between the window border
2980 * and the text area.
2981 */
2982 if (i == 258) {
2983 set_window_background(inst);
2984 draw_backing_rect(inst);
2985 }
2986 }
2987 }
2988
2989 /*
2990 * If the scrollbar needs to be shown, hidden, or moved
2991 * from one end to the other of the window, do so now.
2992 */
2993 if (oldcfg.scrollbar != cfg2.scrollbar) {
2994 if (cfg2.scrollbar)
2995 gtk_widget_show(inst->sbar);
2996 else
2997 gtk_widget_hide(inst->sbar);
2998 }
2999 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
3000 gtk_box_reorder_child(inst->hbox, inst->sbar,
3001 cfg2.scrollbar_on_left ? 0 : 1);
3002 }
3003
3004 /*
3005 * Change the window title, if required.
3006 */
3007 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
3008 set_title(inst, cfg2.wintitle);
3009 set_window_titles(inst);
3010
3011 /*
3012 * Redo the whole tangled fonts and Unicode mess if
3013 * necessary.
3014 */
3015 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
3016 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
3017 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
3018 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
3019 strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
3020 oldcfg.vtmode != cfg2.vtmode ||
3021 oldcfg.shadowbold != cfg2.shadowbold) {
3022 setup_fonts_ucs(inst);
3023 need_size = 1;
3024 } else
3025 need_size = 0;
3026
3027 /*
3028 * Resize the window.
3029 */
3030 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
3031 oldcfg.window_border != cfg2.window_border || need_size) {
3032 set_geom_hints(inst);
3033 request_resize(inst, cfg2.width, cfg2.height);
3034 } else {
3035 /*
3036 * The above will have caused a call to term_size() for
3037 * us if it happened. If the user has fiddled with only
3038 * the scrollback size, the above will not have
3039 * happened and we will need an explicit term_size()
3040 * here.
3041 */
3042 if (oldcfg.savelines != cfg2.savelines)
3043 term_size(inst->term, inst->term->rows, inst->term->cols,
3044 cfg2.savelines);
3045 }
3046
3047 term_invalidate(inst->term);
3048
3049 /*
3050 * We do an explicit full redraw here to ensure the window
3051 * border has been redrawn as well as the text area.
3052 */
3053 gtk_widget_queue_draw(inst->area);
3054 }
3055 sfree(title);
3056 inst->reconfiguring = FALSE;
3057 }
3058
3059 void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...)
3060 {
3061 /*
3062 * Re-execing ourself is not an exact science under Unix. I do
3063 * the best I can by using /proc/self/exe if available and by
3064 * assuming argv[0] can be found on $PATH if not.
3065 *
3066 * Note that we also have to reconstruct the elements of the
3067 * original argv which gtk swallowed, since the user wants the
3068 * new session to appear on the same X display as the old one.
3069 */
3070 char **args;
3071 va_list ap;
3072 int i, n;
3073 int pid;
3074
3075 /*
3076 * Collect the arguments with which to re-exec ourself.
3077 */
3078 va_start(ap, fd_to_close);
3079 n = 2; /* progname and terminating NULL */
3080 n += inst->ngtkargs;
3081 while (va_arg(ap, char *) != NULL)
3082 n++;
3083 va_end(ap);
3084
3085 args = snewn(n, char *);
3086 args[0] = inst->progname;
3087 args[n-1] = NULL;
3088 for (i = 0; i < inst->ngtkargs; i++)
3089 args[i+1] = inst->gtkargvstart[i];
3090
3091 i++;
3092 va_start(ap, fd_to_close);
3093 while ((args[i++] = va_arg(ap, char *)) != NULL);
3094 va_end(ap);
3095
3096 assert(i == n);
3097
3098 /*
3099 * Do the double fork.
3100 */
3101 pid = fork();
3102 if (pid < 0) {
3103 perror("fork");
3104 return;
3105 }
3106
3107 if (pid == 0) {
3108 int pid2 = fork();
3109 if (pid2 < 0) {
3110 perror("fork");
3111 _exit(1);
3112 } else if (pid2 > 0) {
3113 /*
3114 * First child has successfully forked second child. My
3115 * Work Here Is Done. Note the use of _exit rather than
3116 * exit: the latter appears to cause destroy messages
3117 * to be sent to the X server. I suspect gtk uses
3118 * atexit.
3119 */
3120 _exit(0);
3121 }
3122
3123 /*
3124 * If we reach here, we are the second child, so we now
3125 * actually perform the exec.
3126 */
3127 if (fd_to_close >= 0)
3128 close(fd_to_close);
3129
3130 execv("/proc/self/exe", args);
3131 execvp(inst->progname, args);
3132 perror("exec");
3133 _exit(127);
3134
3135 } else {
3136 int status;
3137 waitpid(pid, &status, 0);
3138 }
3139
3140 }
3141
3142 void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
3143 {
3144 struct gui_data *inst = (struct gui_data *)gdata;
3145 /*
3146 * For this feature we must marshal cfg and (possibly) pty_argv
3147 * into a byte stream, create a pipe, and send this byte stream
3148 * to the child through the pipe.
3149 */
3150 int i, ret, size;
3151 char *data;
3152 char option[80];
3153 int pipefd[2];
3154
3155 if (pipe(pipefd) < 0) {
3156 perror("pipe");
3157 return;
3158 }
3159
3160 size = sizeof(inst->cfg);
3161 if (use_pty_argv && pty_argv) {
3162 for (i = 0; pty_argv[i]; i++)
3163 size += strlen(pty_argv[i]) + 1;
3164 }
3165
3166 data = snewn(size, char);
3167 memcpy(data, &inst->cfg, sizeof(inst->cfg));
3168 if (use_pty_argv && pty_argv) {
3169 int p = sizeof(inst->cfg);
3170 for (i = 0; pty_argv[i]; i++) {
3171 strcpy(data + p, pty_argv[i]);
3172 p += strlen(pty_argv[i]) + 1;
3173 }
3174 assert(p == size);
3175 }
3176
3177 sprintf(option, "---[%d,%d]", pipefd[0], size);
3178 fcntl(pipefd[0], F_SETFD, 0);
3179 fork_and_exec_self(inst, pipefd[1], option, NULL);
3180 close(pipefd[0]);
3181
3182 i = ret = 0;
3183 while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
3184 i += ret;
3185 if (ret < 0)
3186 perror("write to pipe");
3187 close(pipefd[1]);
3188 sfree(data);
3189 }
3190
3191 int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
3192 {
3193 int fd, i, ret, size;
3194 char *data;
3195
3196 if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) {
3197 fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg);
3198 exit(1);
3199 }
3200
3201 data = snewn(size, char);
3202 i = ret = 0;
3203 while (i < size && (ret = read(fd, data + i, size - i)) > 0)
3204 i += ret;
3205 if (ret < 0) {
3206 perror("read from pipe");
3207 exit(1);
3208 } else if (i < size) {
3209 fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n",
3210 appname);
3211 exit(1);
3212 }
3213
3214 memcpy(cfg, data, sizeof(Config));
3215 if (use_pty_argv && size > sizeof(Config)) {
3216 int n = 0;
3217 i = sizeof(Config);
3218 while (i < size) {
3219 while (i < size && data[i]) i++;
3220 if (i >= size) {
3221 fprintf(stderr, "%s: malformed Duplicate Session data\n",
3222 appname);
3223 exit(1);
3224 }
3225 i++;
3226 n++;
3227 }
3228 pty_argv = snewn(n+1, char *);
3229 pty_argv[n] = NULL;
3230 n = 0;
3231 i = sizeof(Config);
3232 while (i < size) {
3233 char *p = data + i;
3234 while (i < size && data[i]) i++;
3235 assert(i < size);
3236 i++;
3237 pty_argv[n++] = dupstr(p);
3238 }
3239 }
3240
3241 return 0;
3242 }
3243
3244 void new_session_menuitem(GtkMenuItem *item, gpointer data)
3245 {
3246 struct gui_data *inst = (struct gui_data *)data;
3247
3248 fork_and_exec_self(inst, -1, NULL);
3249 }
3250
3251 void restart_session_menuitem(GtkMenuItem *item, gpointer data)
3252 {
3253 struct gui_data *inst = (struct gui_data *)data;
3254
3255 if (!inst->back) {
3256 logevent(inst, "----- Session restarted -----");
3257 term_pwron(inst->term, FALSE);
3258 start_backend(inst);
3259 inst->exited = FALSE;
3260 }
3261 }
3262
3263 void saved_session_menuitem(GtkMenuItem *item, gpointer data)
3264 {
3265 struct gui_data *inst = (struct gui_data *)data;
3266 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3267
3268 fork_and_exec_self(inst, -1, "-load", str, NULL);
3269 }
3270
3271 void saved_session_freedata(GtkMenuItem *item, gpointer data)
3272 {
3273 char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data");
3274
3275 sfree(str);
3276 }
3277
3278 static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data)
3279 {
3280 struct gui_data *inst = (struct gui_data *)data;
3281 struct sesslist sesslist;
3282 int i;
3283
3284 gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu),
3285 (GtkCallback)gtk_widget_destroy, NULL);
3286
3287 get_sesslist(&sesslist, TRUE);
3288 for (i = 1; i < sesslist.nsessions; i++) {
3289 GtkWidget *menuitem =
3290 gtk_menu_item_new_with_label(sesslist.sessions[i]);
3291 gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem);
3292 gtk_widget_show(menuitem);
3293 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3294 dupstr(sesslist.sessions[i]));
3295 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3296 GTK_SIGNAL_FUNC(saved_session_menuitem),
3297 inst);
3298 gtk_signal_connect(GTK_OBJECT(menuitem), "destroy",
3299 GTK_SIGNAL_FUNC(saved_session_freedata),
3300 inst);
3301 }
3302 get_sesslist(&sesslist, FALSE); /* free up */
3303 }
3304
3305 void set_window_icon(GtkWidget *window, const char *const *const *icon,
3306 int n_icon)
3307 {
3308 GdkPixmap *iconpm;
3309 GdkBitmap *iconmask;
3310 #if GTK_CHECK_VERSION(2,0,0)
3311 GList *iconlist;
3312 int n;
3313 #endif
3314
3315 if (!n_icon)
3316 return;
3317
3318 gtk_widget_realize(window);
3319 iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask,
3320 NULL, (gchar **)icon[0]);
3321 gdk_window_set_icon(window->window, NULL, iconpm, iconmask);
3322
3323 #if GTK_CHECK_VERSION(2,0,0)
3324 iconlist = NULL;
3325 for (n = 0; n < n_icon; n++) {
3326 iconlist =
3327 g_list_append(iconlist,
3328 gdk_pixbuf_new_from_xpm_data((const gchar **)
3329 icon[n]));
3330 }
3331 gdk_window_set_icon_list(window->window, iconlist);
3332 #endif
3333 }
3334
3335 void update_specials_menu(void *frontend)
3336 {
3337 struct gui_data *inst = (struct gui_data *)frontend;
3338
3339 const struct telnet_special *specials;
3340
3341 if (inst->back)
3342 specials = inst->back->get_specials(inst->backhandle);
3343 else
3344 specials = NULL;
3345
3346 /* I believe this disposes of submenus too. */
3347 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
3348 (GtkCallback)gtk_widget_destroy, NULL);
3349 if (specials) {
3350 int i;
3351 GtkWidget *menu = inst->specialsmenu;
3352 /* A lame "stack" for submenus that will do for now. */
3353 GtkWidget *saved_menu = NULL;
3354 int nesting = 1;
3355 for (i = 0; nesting > 0; i++) {
3356 GtkWidget *menuitem = NULL;
3357 switch (specials[i].code) {
3358 case TS_SUBMENU:
3359 assert (nesting < 2);
3360 saved_menu = menu; /* XXX lame stacking */
3361 menu = gtk_menu_new();
3362 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3363 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu);
3364 gtk_container_add(GTK_CONTAINER(saved_menu), menuitem);
3365 gtk_widget_show(menuitem);
3366 menuitem = NULL;
3367 nesting++;
3368 break;
3369 case TS_EXITMENU:
3370 nesting--;
3371 if (nesting) {
3372 menu = saved_menu; /* XXX lame stacking */
3373 saved_menu = NULL;
3374 }
3375 break;
3376 case TS_SEP:
3377 menuitem = gtk_menu_item_new();
3378 break;
3379 default:
3380 menuitem = gtk_menu_item_new_with_label(specials[i].name);
3381 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
3382 GINT_TO_POINTER(specials[i].code));
3383 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
3384 GTK_SIGNAL_FUNC(special_menuitem), inst);
3385 break;
3386 }
3387 if (menuitem) {
3388 gtk_container_add(GTK_CONTAINER(menu), menuitem);
3389 gtk_widget_show(menuitem);
3390 }
3391 }
3392 gtk_widget_show(inst->specialsitem1);
3393 gtk_widget_show(inst->specialsitem2);
3394 } else {
3395 gtk_widget_hide(inst->specialsitem1);
3396 gtk_widget_hide(inst->specialsitem2);
3397 }
3398 }
3399
3400 static void start_backend(struct gui_data *inst)
3401 {
3402 extern Backend *select_backend(Config *cfg);
3403 char *realhost;
3404 const char *error;
3405
3406 inst->back = select_backend(&inst->cfg);
3407
3408 error = inst->back->init((void *)inst, &inst->backhandle,
3409 &inst->cfg, inst->cfg.host, inst->cfg.port,
3410 &realhost, inst->cfg.tcp_nodelay,
3411 inst->cfg.tcp_keepalives);
3412
3413 if (error) {
3414 char *msg = dupprintf("Unable to open connection to %s:\n%s",
3415 inst->cfg.host, error);
3416 inst->exited = TRUE;
3417 fatal_message_box(inst->window, msg);
3418 sfree(msg);
3419 exit(0);
3420 }
3421
3422 if (inst->cfg.wintitle[0]) {
3423 set_title(inst, inst->cfg.wintitle);
3424 set_icon(inst, inst->cfg.wintitle);
3425 } else {
3426 char *title = make_default_wintitle(realhost);
3427 set_title(inst, title);
3428 set_icon(inst, title);
3429 sfree(title);
3430 }
3431 sfree(realhost);
3432
3433 inst->back->provide_logctx(inst->backhandle, inst->logctx);
3434
3435 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
3436
3437 inst->ldisc =
3438 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle,
3439 inst);
3440
3441 gtk_widget_hide(inst->restartitem);
3442 }
3443
3444 int pt_main(int argc, char **argv)
3445 {
3446 extern int cfgbox(Config *cfg);
3447 struct gui_data *inst;
3448
3449 /*
3450 * Create an instance structure and initialise to zeroes
3451 */
3452 inst = snew(struct gui_data);
3453 memset(inst, 0, sizeof(*inst));
3454 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
3455 inst->busy_status = BUSY_NOT;
3456
3457 /* defer any child exit handling until we're ready to deal with
3458 * it */
3459 block_signal(SIGCHLD, 1);
3460
3461 inst->progname = argv[0];
3462 /*
3463 * Copy the original argv before letting gtk_init fiddle with
3464 * it. It will be required later.
3465 */
3466 {
3467 int i, oldargc;
3468 inst->gtkargvstart = snewn(argc-1, char *);
3469 for (i = 1; i < argc; i++)
3470 inst->gtkargvstart[i-1] = dupstr(argv[i]);
3471 oldargc = argc;
3472 gtk_init(&argc, &argv);
3473 inst->ngtkargs = oldargc - argc;
3474 }
3475
3476 if (argc > 1 && !strncmp(argv[1], "---", 3)) {
3477 read_dupsession_data(inst, &inst->cfg, argv[1]);
3478 /* Splatter this argument so it doesn't clutter a ps listing */
3479 memset(argv[1], 0, strlen(argv[1]));
3480 } else {
3481 /* By default, we bring up the config dialog, rather than launching
3482 * a session. This gets set to TRUE if something happens to change
3483 * that (e.g., a hostname is specified on the command-line). */
3484 int allow_launch = FALSE;
3485 if (do_cmdline(argc, argv, 0, &allow_launch, inst, &inst->cfg))
3486 exit(1); /* pre-defaults pass to get -class */
3487 do_defaults(NULL, &inst->cfg);
3488 if (do_cmdline(argc, argv, 1, &allow_launch, inst, &inst->cfg))
3489 exit(1); /* post-defaults, do everything */
3490
3491 cmdline_run_saved(&inst->cfg);
3492
3493 if (loaded_session)
3494 allow_launch = TRUE;
3495
3496 if ((!allow_launch || !cfg_launchable(&inst->cfg)) &&
3497 !cfgbox(&inst->cfg))
3498 exit(0); /* config box hit Cancel */
3499 }
3500
3501 if (!compound_text_atom)
3502 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3503 if (!utf8_string_atom)
3504 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3505
3506 setup_fonts_ucs(inst);
3507 init_cutbuffers();
3508
3509 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3510
3511 /*
3512 * Set up the colour map.
3513 */
3514 palette_reset(inst);
3515
3516 inst->width = inst->cfg.width;
3517 inst->height = inst->cfg.height;
3518
3519 inst->area = gtk_drawing_area_new();
3520 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3521 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
3522 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
3523 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3524 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
3525 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3526 /*
3527 * We always create the scrollbar; it remains invisible if
3528 * unwanted, so we can pop it up quickly if it suddenly becomes
3529 * desirable.
3530 */
3531 if (inst->cfg.scrollbar_on_left)
3532 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3533 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
3534 if (!inst->cfg.scrollbar_on_left)
3535 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
3536
3537 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
3538
3539 set_geom_hints(inst);
3540
3541 gtk_widget_show(inst->area);
3542 if (inst->cfg.scrollbar)
3543 gtk_widget_show(inst->sbar);
3544 else
3545 gtk_widget_hide(inst->sbar);
3546 gtk_widget_show(GTK_WIDGET(inst->hbox));
3547
3548 if (inst->gotpos) {
3549 int x = inst->xpos, y = inst->ypos;
3550 GtkRequisition req;
3551 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
3552 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
3553 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
3554 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
3555 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
3556 }
3557
3558 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
3559 GTK_SIGNAL_FUNC(destroy), inst);
3560 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
3561 GTK_SIGNAL_FUNC(delete_window), inst);
3562 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
3563 GTK_SIGNAL_FUNC(key_event), inst);
3564 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
3565 GTK_SIGNAL_FUNC(key_event), inst);
3566 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
3567 GTK_SIGNAL_FUNC(focus_event), inst);
3568 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
3569 GTK_SIGNAL_FUNC(focus_event), inst);
3570 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
3571 GTK_SIGNAL_FUNC(configure_area), inst);
3572 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
3573 GTK_SIGNAL_FUNC(expose_area), inst);
3574 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
3575 GTK_SIGNAL_FUNC(button_event), inst);
3576 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
3577 GTK_SIGNAL_FUNC(button_event), inst);
3578 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
3579 GTK_SIGNAL_FUNC(motion_event), inst);
3580 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
3581 GTK_SIGNAL_FUNC(selection_received), inst);
3582 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
3583 GTK_SIGNAL_FUNC(selection_get), inst);
3584 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
3585 GTK_SIGNAL_FUNC(selection_clear), inst);
3586 if (inst->cfg.scrollbar)
3587 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
3588 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
3589 gtk_widget_add_events(GTK_WIDGET(inst->area),
3590 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
3591 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
3592 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
3593
3594 {
3595 extern const char *const *const main_icon[];
3596 extern const int n_main_icon;
3597 set_window_icon(inst->window, main_icon, n_main_icon);
3598 }
3599
3600 gtk_widget_show(inst->window);
3601
3602 set_window_background(inst);
3603
3604 /*
3605 * Set up the Ctrl+rightclick context menu.
3606 */
3607 {
3608 GtkWidget *menuitem;
3609 char *s;
3610 extern const int use_event_log, new_session, saved_sessions;
3611
3612 inst->menu = gtk_menu_new();
3613
3614 #define MKMENUITEM(title, func) do { \
3615 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3616 gtk_menu_item_new(); \
3617 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3618 gtk_widget_show(menuitem); \
3619 if (func != NULL) \
3620 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3621 GTK_SIGNAL_FUNC(func), inst); \
3622 } while (0)
3623 if (new_session)
3624 MKMENUITEM("New Session", new_session_menuitem);
3625 MKMENUITEM("Restart Session", restart_session_menuitem);
3626 inst->restartitem = menuitem;
3627 gtk_widget_hide(inst->restartitem);
3628 MKMENUITEM("Duplicate Session", dup_session_menuitem);
3629 if (saved_sessions) {
3630 inst->sessionsmenu = gtk_menu_new();
3631 /* sessionsmenu will be updated when it's invoked */
3632 /* XXX is this the right way to do dynamic menus in Gtk? */
3633 MKMENUITEM("Saved Sessions", update_savedsess_menu);
3634 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem),
3635 inst->sessionsmenu);
3636 }
3637 MKMENUITEM(NULL, NULL);
3638 MKMENUITEM("Change Settings", change_settings_menuitem);
3639 MKMENUITEM(NULL, NULL);
3640 if (use_event_log)
3641 MKMENUITEM("Event Log", event_log_menuitem);
3642 MKMENUITEM("Special Commands", NULL);
3643 inst->specialsmenu = gtk_menu_new();
3644 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
3645 inst->specialsitem1 = menuitem;
3646 MKMENUITEM(NULL, NULL);
3647 inst->specialsitem2 = menuitem;
3648 gtk_widget_hide(inst->specialsitem1);
3649 gtk_widget_hide(inst->specialsitem2);
3650 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
3651 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
3652 MKMENUITEM("Copy All", copy_all_menuitem);
3653 MKMENUITEM(NULL, NULL);
3654 s = dupcat("About ", appname, NULL);
3655 MKMENUITEM(s, about_menuitem);
3656 sfree(s);
3657 #undef MKMENUITEM
3658 }
3659
3660 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
3661 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
3662 inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH);
3663 inst->blankcursor = make_mouse_ptr(inst, -1);
3664 make_mouse_ptr(inst, -2); /* clean up cursor font */
3665 inst->currcursor = inst->textcursor;
3666 show_mouseptr(inst, 1);
3667
3668 inst->eventlogstuff = eventlogstuff_new();
3669
3670 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3671 inst->logctx = log_init(inst, &inst->cfg);
3672 term_provide_logctx(inst->term, inst->logctx);
3673
3674 uxsel_init();
3675
3676 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
3677
3678 start_backend(inst);
3679
3680 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
3681
3682 /* now we're reday to deal with the child exit handler being
3683 * called */
3684 block_signal(SIGCHLD, 0);
3685
3686 /*
3687 * Block SIGPIPE: if we attempt Duplicate Session or similar
3688 * and it falls over in some way, we certainly don't want
3689 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3690 * this _after_ (at least pterm) forks off its child process,
3691 * since the child wants SIGPIPE handled in the usual way.
3692 */
3693 block_signal(SIGPIPE, 1);
3694
3695 inst->exited = FALSE;
3696
3697 gtk_main();
3698
3699 return 0;
3700 }