`Copy All' ought to de-highlight any existing selection, in line
[u/mdw/putty] / unix / pterm.c
CommitLineData
f7f27309 1/*
2 * pterm - a fusion of the PuTTY terminal emulator with a Unix pty
3 * back end, all running as a GTK application. Wish me luck.
4 */
5
90cfd8f4 6#define _GNU_SOURCE
7
f7f27309 8#include <string.h>
d64838e1 9#include <assert.h>
f7f27309 10#include <stdlib.h>
90cfd8f4 11#include <string.h>
8caf7aed 12#include <signal.h>
1709795f 13#include <stdio.h>
f7f27309 14#include <time.h>
054d8535 15#include <errno.h>
16#include <fcntl.h>
17#include <unistd.h>
90cfd8f4 18#include <sys/types.h>
19#include <sys/wait.h>
f7f27309 20#include <gtk/gtk.h>
a57cd64b 21#include <gdk/gdkkeysyms.h>
c5e438ec 22#include <gdk/gdkx.h>
23#include <X11/Xlib.h>
24#include <X11/Xutil.h>
f7f27309 25
1709795f 26#define PUTTY_DO_GLOBALS /* actually _define_ globals */
2dc6356a 27
1709795f 28#include "putty.h"
887035a5 29#include "terminal.h"
1709795f 30
f7f27309 31#define CAT2(x,y) x ## y
32#define CAT(x,y) CAT2(x,y)
33#define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
34
d64838e1 35#define NCOLOURS (lenof(((Config *)0)->colours))
36
8c161662 37GdkAtom compound_text_atom, utf8_string_atom;
38
d64838e1 39struct gui_data {
12d200b1 40 GtkWidget *window, *area, *sbar;
6a5e84dd 41 GtkBox *hbox;
42 GtkAdjustment *sbar_adjust;
47e4e735 43 GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2;
d64838e1 44 GdkPixmap *pixmap;
006238cb 45 GdkFont *fonts[4]; /* normal, bold, wide, widebold */
2dc6356a 46 struct {
47 int charset;
48 int is_wide;
006238cb 49 } fontinfo[4];
aca2a702 50 int xpos, ypos, gotpos, gravity;
57e636ca 51 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
d64838e1 52 GdkColor cols[NCOLOURS];
53 GdkColormap *colmap;
e6346999 54 wchar_t *pastein_data;
085f4a68 55 int direct_to_font;
e6346999 56 int pastein_data_len;
2dc6356a 57 char *pasteout_data, *pasteout_data_utf8;
58 int pasteout_data_len, pasteout_data_utf8_len;
83616aab 59 int font_width, font_height;
56b9b9a7 60 int width, height;
88e6b9ca 61 int ignore_sbar;
b3530065 62 int mouseptr_visible;
0f660c8f 63 guint term_paste_idle_id;
c33c3d76 64 int alt_keycode;
045f707b 65 int alt_digits;
12d200b1 66 char wintitle[sizeof(((Config *)0)->wintitle)];
16891265 67 char icontitle[sizeof(((Config *)0)->wintitle)];
74aca06d 68 int master_fd, master_func_id;
b9d7bcad 69 void *ldisc;
6b78788a 70 Backend *back;
71 void *backhandle;
5def7522 72 Terminal *term;
a8327734 73 void *logctx;
74aca06d 74 int exited;
21d2b241 75 struct unicode_data ucsdata;
3ea863a3 76 Config cfg;
8eed910d 77 void *eventlogstuff;
a8327734 78};
79
80struct draw_ctx {
81 GdkGC *gc;
82 struct gui_data *inst;
d64838e1 83};
84
d64838e1 85static int send_raw_mouse;
86
c5e438ec 87static char *app_name = "pterm";
88
c85623f9 89char *x_get_default(const char *key)
c5e438ec 90{
91 return XGetDefault(GDK_DISPLAY(), app_name, key);
92}
93
3f935d5b 94void connection_fatal(void *frontend, char *p, ...)
95{
96 Terminal *term = (Terminal *)frontend;
97 struct gui_data *inst = (struct gui_data *)term->frontend;
98
99 va_list ap;
100 char *msg;
101 va_start(ap, p);
102 msg = dupvprintf(p, ap);
103 va_end(ap);
104 inst->exited = TRUE;
105 fatal_message_box(inst->window, msg);
106 sfree(msg);
107 if (inst->cfg.close_on_exit == FORCE_ON)
108 cleanup_exit(1);
109}
110
5a9eb105 111/*
112 * Default settings that are specific to pterm.
113 */
9a30e26b 114FontSpec platform_default_fontspec(const char *name)
5a9eb105 115{
9a30e26b 116 FontSpec ret;
5a9eb105 117 if (!strcmp(name, "Font"))
9a30e26b 118 strcpy(ret.name, "fixed");
119 else
120 *ret.name = '\0';
121 return ret;
122}
123
124Filename platform_default_filename(const char *name)
125{
126 Filename ret;
127 if (!strcmp(name, "LogFileName"))
128 strcpy(ret.path, "putty.log");
129 else
130 *ret.path = '\0';
131 return ret;
132}
133
134char *platform_default_s(const char *name)
135{
5a9eb105 136 return NULL;
137}
138
c85623f9 139int platform_default_i(const char *name, int def)
5a9eb105 140{
141 if (!strcmp(name, "CloseOnExit"))
c888fed4 142 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
5a9eb105 143 return def;
144}
145
b9d7bcad 146void ldisc_update(void *frontend, int echo, int edit)
1709795f 147{
148 /*
149 * This is a stub in pterm. If I ever produce a Unix
150 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
151 * then it will require some termios manoeuvring analogous to
152 * that in the Windows plink.c, but here it's meaningless.
153 */
154}
155
9a30e26b 156int askappend(void *frontend, Filename filename)
1709795f 157{
158 /*
4c0901ed 159 * Logging in an xterm-alike is liable to be something you only
160 * do at serious diagnostic need. Hence, I'm going to take the
161 * easy option for now and assume we always want to overwrite
162 * log files. I can always make it properly configurable later.
1709795f 163 */
164 return 2;
165}
166
a8327734 167void logevent(void *frontend, char *string)
1709795f 168{
8eed910d 169 Terminal *term = (Terminal *)frontend;
170 struct gui_data *inst = (struct gui_data *)term->frontend;
171
172 log_eventlog(inst->logctx, string);
173
174 logevent_dlg(inst->eventlogstuff, string);
1709795f 175}
176
a8327734 177int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */
e9aef757 178{
a8327734 179 struct gui_data *inst = (struct gui_data *)frontend;
180
e9aef757 181 if (which)
182 return inst->font_height;
183 else
184 return inst->font_width;
185}
186
1709795f 187/*
188 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
189 * into a cooked one (SELECT, EXTEND, PASTE).
190 *
191 * In Unix, this is not configurable; the X button arrangement is
192 * rock-solid across all applications, everyone has a three-button
193 * mouse or a means of faking it, and there is no need to switch
194 * buttons around at all.
195 */
c387aa9f 196static Mouse_Button translate_button(Mouse_Button button)
1709795f 197{
a8327734 198 /* struct gui_data *inst = (struct gui_data *)frontend; */
199
1709795f 200 if (button == MBT_LEFT)
201 return MBT_SELECT;
202 if (button == MBT_MIDDLE)
203 return MBT_PASTE;
204 if (button == MBT_RIGHT)
205 return MBT_EXTEND;
206 return 0; /* shouldn't happen */
207}
208
209/*
5bf9955d 210 * Return the top-level GtkWindow associated with a particular
211 * front end instance.
212 */
213void *get_window(void *frontend)
214{
215 Terminal *term = (Terminal *)frontend;
216 struct gui_data *inst = (struct gui_data *)term->frontend;
217 return inst->window;
218}
219
220/*
1709795f 221 * Minimise or restore the window in response to a server-side
222 * request.
223 */
a8327734 224void set_iconic(void *frontend, int iconic)
1709795f 225{
16891265 226 /*
227 * GTK 1.2 doesn't know how to do this.
228 */
229#if GTK_CHECK_VERSION(2,0,0)
a8327734 230 struct gui_data *inst = (struct gui_data *)frontend;
16891265 231 if (iconic)
232 gtk_window_iconify(GTK_WINDOW(inst->window));
233 else
234 gtk_window_deiconify(GTK_WINDOW(inst->window));
235#endif
1709795f 236}
237
238/*
239 * Move the window in response to a server-side request.
240 */
a8327734 241void move_window(void *frontend, int x, int y)
1709795f 242{
a8327734 243 struct gui_data *inst = (struct gui_data *)frontend;
16891265 244 /*
245 * I assume that when the GTK version of this call is available
246 * we should use it. Not sure how it differs from the GDK one,
247 * though.
248 */
249#if GTK_CHECK_VERSION(2,0,0)
250 gtk_window_move(GTK_WINDOW(inst->window), x, y);
251#else
252 gdk_window_move(inst->window->window, x, y);
253#endif
1709795f 254}
255
256/*
257 * Move the window to the top or bottom of the z-order in response
258 * to a server-side request.
259 */
a8327734 260void set_zorder(void *frontend, int top)
1709795f 261{
a8327734 262 struct gui_data *inst = (struct gui_data *)frontend;
16891265 263 if (top)
264 gdk_window_raise(inst->window->window);
265 else
266 gdk_window_lower(inst->window->window);
1709795f 267}
268
269/*
270 * Refresh the window in response to a server-side request.
271 */
a8327734 272void refresh_window(void *frontend)
1709795f 273{
a8327734 274 struct gui_data *inst = (struct gui_data *)frontend;
5def7522 275 term_invalidate(inst->term);
1709795f 276}
277
278/*
279 * Maximise or restore the window in response to a server-side
280 * request.
281 */
a8327734 282void set_zoomed(void *frontend, int zoomed)
1709795f 283{
16891265 284 /*
285 * GTK 1.2 doesn't know how to do this.
286 */
287#if GTK_CHECK_VERSION(2,0,0)
a8327734 288 struct gui_data *inst = (struct gui_data *)frontend;
16891265 289 if (iconic)
290 gtk_window_maximize(GTK_WINDOW(inst->window));
291 else
292 gtk_window_unmaximize(GTK_WINDOW(inst->window));
293#endif
1709795f 294}
295
296/*
297 * Report whether the window is iconic, for terminal reports.
298 */
a8327734 299int is_iconic(void *frontend)
1709795f 300{
a8327734 301 struct gui_data *inst = (struct gui_data *)frontend;
16891265 302 return !gdk_window_is_viewable(inst->window->window);
1709795f 303}
304
305/*
306 * Report the window's position, for terminal reports.
307 */
a8327734 308void get_window_pos(void *frontend, int *x, int *y)
1709795f 309{
a8327734 310 struct gui_data *inst = (struct gui_data *)frontend;
16891265 311 /*
312 * I assume that when the GTK version of this call is available
313 * we should use it. Not sure how it differs from the GDK one,
314 * though.
315 */
316#if GTK_CHECK_VERSION(2,0,0)
317 gtk_window_get_position(GTK_WINDOW(inst->window), x, y);
318#else
319 gdk_window_get_position(inst->window->window, x, y);
320#endif
1709795f 321}
322
323/*
324 * Report the window's pixel size, for terminal reports.
325 */
a8327734 326void get_window_pixels(void *frontend, int *x, int *y)
1709795f 327{
a8327734 328 struct gui_data *inst = (struct gui_data *)frontend;
16891265 329 /*
330 * I assume that when the GTK version of this call is available
331 * we should use it. Not sure how it differs from the GDK one,
332 * though.
333 */
334#if GTK_CHECK_VERSION(2,0,0)
335 gtk_window_get_size(GTK_WINDOW(inst->window), x, y);
336#else
337 gdk_window_get_size(inst->window->window, x, y);
338#endif
1709795f 339}
340
341/*
342 * Return the window or icon title.
343 */
a8327734 344char *get_window_title(void *frontend, int icon)
1709795f 345{
a8327734 346 struct gui_data *inst = (struct gui_data *)frontend;
16891265 347 return icon ? inst->wintitle : inst->icontitle;
1709795f 348}
f7f27309 349
f7f27309 350gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
351{
352 /*
57e636ca 353 * We could implement warn-on-close here if we really wanted
354 * to.
f7f27309 355 */
356 return FALSE;
357}
358
a8327734 359static void show_mouseptr(struct gui_data *inst, int show)
57e636ca 360{
3ea863a3 361 if (!inst->cfg.hide_mouseptr)
57e636ca 362 show = 1;
363 if (show)
364 gdk_window_set_cursor(inst->area->window, inst->currcursor);
365 else
366 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
b3530065 367 inst->mouseptr_visible = show;
57e636ca 368}
369
f7f27309 370gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
371{
372 struct gui_data *inst = (struct gui_data *)data;
88e6b9ca 373 int w, h, need_size = 0;
f7f27309 374
3ea863a3 375 w = (event->width - 2*inst->cfg.window_border) / inst->font_width;
376 h = (event->height - 2*inst->cfg.window_border) / inst->font_height;
d64838e1 377
56b9b9a7 378 if (w != inst->width || h != inst->height) {
88e6b9ca 379 if (inst->pixmap) {
380 gdk_pixmap_unref(inst->pixmap);
381 inst->pixmap = NULL;
382 }
56b9b9a7 383 inst->cfg.width = inst->width = w;
384 inst->cfg.height = inst->height = h;
88e6b9ca 385 need_size = 1;
386 }
387 if (!inst->pixmap) {
6a5e84dd 388 GdkGC *gc;
88e6b9ca 389
390 inst->pixmap = gdk_pixmap_new(widget->window,
3ea863a3 391 (inst->cfg.width * inst->font_width +
392 2*inst->cfg.window_border),
393 (inst->cfg.height * inst->font_height +
394 2*inst->cfg.window_border), -1);
88e6b9ca 395
6a5e84dd 396 gc = gdk_gc_new(inst->area->window);
397 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
398 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
3ea863a3 399 inst->cfg.width * inst->font_width + 2*inst->cfg.window_border,
400 inst->cfg.height * inst->font_height + 2*inst->cfg.window_border);
6a5e84dd 401 gdk_gc_unref(gc);
402 }
f7f27309 403
88e6b9ca 404 if (need_size) {
3ea863a3 405 term_size(inst->term, h, w, inst->cfg.savelines);
88e6b9ca 406 }
407
f7f27309 408 return TRUE;
409}
410
411gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
412{
a8327734 413 struct gui_data *inst = (struct gui_data *)data;
f7f27309 414
415 /*
1709795f 416 * Pass the exposed rectangle to terminal.c, which will call us
417 * back to do the actual painting.
f7f27309 418 */
6a5e84dd 419 if (inst->pixmap) {
420 gdk_draw_pixmap(widget->window,
421 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
422 inst->pixmap,
423 event->area.x, event->area.y,
424 event->area.x, event->area.y,
425 event->area.width, event->area.height);
426 }
1709795f 427 return TRUE;
f7f27309 428}
429
430#define KEY_PRESSED(k) \
431 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
432
433gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
434{
a8327734 435 struct gui_data *inst = (struct gui_data *)data;
a57cd64b 436 char output[32];
437 int start, end;
f7f27309 438
c33c3d76 439 /* By default, nothing is generated. */
440 end = start = 0;
441
442 /*
443 * If Alt is being released after typing an Alt+numberpad
444 * sequence, we should generate the code that was typed.
045f707b 445 *
446 * Note that we only do this if more than one key was actually
447 * pressed - I don't think Alt+NumPad4 should be ^D or that
448 * Alt+NumPad3 should be ^C, for example. There's no serious
449 * inconvenience in having to type a zero before a single-digit
450 * character code.
c33c3d76 451 */
452 if (event->type == GDK_KEY_RELEASE &&
453 (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
454 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) &&
045f707b 455 inst->alt_keycode >= 0 && inst->alt_digits > 1) {
c33c3d76 456#ifdef KEY_DEBUGGING
457 printf("Alt key up, keycode = %d\n", inst->alt_keycode);
458#endif
459 output[0] = inst->alt_keycode;
460 end = 1;
461 goto done;
462 }
463
1709795f 464 if (event->type == GDK_KEY_PRESS) {
a57cd64b 465#ifdef KEY_DEBUGGING
466 {
467 int i;
468 printf("keypress: keyval = %04x, state = %08x; string =",
469 event->keyval, event->state);
470 for (i = 0; event->string[i]; i++)
471 printf(" %02x", (unsigned char) event->string[i]);
472 printf("\n");
473 }
474#endif
475
476 /*
c33c3d76 477 * NYI: Compose key (!!! requires Unicode faff before even trying)
a57cd64b 478 */
479
6a5e84dd 480 /*
c33c3d76 481 * If Alt has just been pressed, we start potentially
482 * accumulating an Alt+numberpad code. We do this by
483 * setting alt_keycode to -1 (nothing yet but plausible).
484 */
485 if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L ||
486 event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) {
487 inst->alt_keycode = -1;
045f707b 488 inst->alt_digits = 0;
c33c3d76 489 goto done; /* this generates nothing else */
490 }
491
492 /*
493 * If we're seeing a numberpad key press with Mod1 down,
494 * consider adding it to alt_keycode if that's sensible.
495 * Anything _else_ with Mod1 down cancels any possibility
496 * of an ALT keycode: we set alt_keycode to -2.
497 */
498 if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) {
499 int digit = -1;
500 switch (event->keyval) {
501 case GDK_KP_0: case GDK_KP_Insert: digit = 0; break;
502 case GDK_KP_1: case GDK_KP_End: digit = 1; break;
503 case GDK_KP_2: case GDK_KP_Down: digit = 2; break;
504 case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break;
505 case GDK_KP_4: case GDK_KP_Left: digit = 4; break;
506 case GDK_KP_5: case GDK_KP_Begin: digit = 5; break;
507 case GDK_KP_6: case GDK_KP_Right: digit = 6; break;
508 case GDK_KP_7: case GDK_KP_Home: digit = 7; break;
509 case GDK_KP_8: case GDK_KP_Up: digit = 8; break;
510 case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break;
511 }
512 if (digit < 0)
513 inst->alt_keycode = -2; /* it's invalid */
514 else {
515#ifdef KEY_DEBUGGING
516 printf("Adding digit %d to keycode %d", digit,
517 inst->alt_keycode);
518#endif
519 if (inst->alt_keycode == -1)
520 inst->alt_keycode = digit; /* one-digit code */
521 else
522 inst->alt_keycode = inst->alt_keycode * 10 + digit;
045f707b 523 inst->alt_digits++;
c33c3d76 524#ifdef KEY_DEBUGGING
525 printf(" gives new code %d\n", inst->alt_keycode);
526#endif
527 /* Having used this digit, we now do nothing more with it. */
528 goto done;
529 }
530 }
531
532 /*
6a5e84dd 533 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
534 * at all.
535 */
536 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
3ea863a3 537 term_scroll(inst->term, 0, -inst->cfg.height/2);
6a5e84dd 538 return TRUE;
539 }
153580da 540 if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) {
541 term_scroll(inst->term, 0, -1);
542 return TRUE;
543 }
6a5e84dd 544 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
3ea863a3 545 term_scroll(inst->term, 0, +inst->cfg.height/2);
6a5e84dd 546 return TRUE;
547 }
153580da 548 if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) {
549 term_scroll(inst->term, 0, +1);
550 return TRUE;
551 }
6a5e84dd 552
2a2c1973 553 /*
554 * Neither does Shift-Ins.
555 */
556 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
a8327734 557 request_paste(inst);
2a2c1973 558 return TRUE;
559 }
560
a57cd64b 561 /* ALT+things gives leading Escape. */
562 output[0] = '\033';
563 strncpy(output+1, event->string, 31);
564 output[31] = '\0';
565 end = strlen(output);
5c0ea258 566 if (event->state & GDK_MOD1_MASK) {
567 start = 0;
568 if (end == 1) end = 0;
569 } else
570 start = 1;
a57cd64b 571
572 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
573 if (!event->string[0] && event->keyval == '`' &&
574 (event->state & GDK_CONTROL_MASK)) {
575 output[1] = '\x1C';
576 end = 2;
577 }
578
579 /* Control-Break is the same as Control-C */
580 if (event->keyval == GDK_Break &&
581 (event->state & GDK_CONTROL_MASK)) {
582 output[1] = '\003';
583 end = 2;
584 }
585
586 /* Control-2, Control-Space and Control-@ are NUL */
587 if (!event->string[0] &&
588 (event->keyval == ' ' || event->keyval == '2' ||
589 event->keyval == '@') &&
590 (event->state & (GDK_SHIFT_MASK |
591 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
592 output[1] = '\0';
593 end = 2;
594 }
595
596 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
597 if (!event->string[0] && event->keyval == ' ' &&
598 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
599 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
600 output[1] = '\240';
601 end = 2;
602 }
603
604 /* We don't let GTK tell us what Backspace is! We know better. */
605 if (event->keyval == GDK_BackSpace &&
606 !(event->state & GDK_SHIFT_MASK)) {
3ea863a3 607 output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
a57cd64b 608 end = 2;
609 }
e8e8d6e2 610 /* For Shift Backspace, do opposite of what is configured. */
611 if (event->keyval == GDK_BackSpace &&
612 (event->state & GDK_SHIFT_MASK)) {
3ea863a3 613 output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
e8e8d6e2 614 end = 2;
615 }
a57cd64b 616
617 /* Shift-Tab is ESC [ Z */
618 if (event->keyval == GDK_ISO_Left_Tab ||
619 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
620 end = 1 + sprintf(output+1, "\033[Z");
621 }
622
623 /*
8b1fcd56 624 * NetHack keypad mode.
625 */
3ea863a3 626 if (inst->cfg.nethack_keypad) {
8b1fcd56 627 char *keys = NULL;
628 switch (event->keyval) {
629 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
630 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
631 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
632 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
633 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
634 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
635 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
636 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
637 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
638 }
639 if (keys) {
640 end = 2;
641 if (event->state & GDK_SHIFT_MASK)
642 output[1] = keys[1];
643 else
644 output[1] = keys[0];
645 goto done;
646 }
647 }
648
649 /*
a57cd64b 650 * Application keypad mode.
651 */
3ea863a3 652 if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) {
a57cd64b 653 int xkey = 0;
654 switch (event->keyval) {
655 case GDK_Num_Lock: xkey = 'P'; break;
656 case GDK_KP_Divide: xkey = 'Q'; break;
657 case GDK_KP_Multiply: xkey = 'R'; break;
658 case GDK_KP_Subtract: xkey = 'S'; break;
659 /*
660 * Keypad + is tricky. It covers a space that would
661 * be taken up on the VT100 by _two_ keys; so we
662 * let Shift select between the two. Worse still,
663 * in xterm function key mode we change which two...
664 */
665 case GDK_KP_Add:
3ea863a3 666 if (inst->cfg.funky_type == 2) {
a57cd64b 667 if (event->state & GDK_SHIFT_MASK)
668 xkey = 'l';
669 else
670 xkey = 'k';
671 } else if (event->state & GDK_SHIFT_MASK)
672 xkey = 'm';
673 else
674 xkey = 'l';
675 break;
676 case GDK_KP_Enter: xkey = 'M'; break;
677 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
678 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
679 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
680 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
681 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
682 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
683 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
684 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
685 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
686 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
687 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
688 }
689 if (xkey) {
5def7522 690 if (inst->term->vt52_mode) {
a57cd64b 691 if (xkey >= 'P' && xkey <= 'S')
692 end = 1 + sprintf(output+1, "\033%c", xkey);
693 else
694 end = 1 + sprintf(output+1, "\033?%c", xkey);
695 } else
696 end = 1 + sprintf(output+1, "\033O%c", xkey);
697 goto done;
698 }
699 }
700
701 /*
702 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
703 * for integer decimal nn.)
704 *
705 * We also deal with the weird ones here. Linux VCs replace F1
706 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
707 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
708 * respectively.
709 */
710 {
711 int code = 0;
712 switch (event->keyval) {
713 case GDK_F1:
714 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
715 break;
716 case GDK_F2:
717 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
718 break;
719 case GDK_F3:
720 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
721 break;
722 case GDK_F4:
723 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
724 break;
725 case GDK_F5:
726 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
727 break;
728 case GDK_F6:
729 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
730 break;
731 case GDK_F7:
732 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
733 break;
734 case GDK_F8:
735 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
736 break;
737 case GDK_F9:
738 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
739 break;
740 case GDK_F10:
741 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
742 break;
743 case GDK_F11:
744 code = 23;
745 break;
746 case GDK_F12:
747 code = 24;
748 break;
749 case GDK_F13:
750 code = 25;
751 break;
752 case GDK_F14:
753 code = 26;
754 break;
755 case GDK_F15:
756 code = 28;
757 break;
758 case GDK_F16:
759 code = 29;
760 break;
761 case GDK_F17:
762 code = 31;
763 break;
764 case GDK_F18:
765 code = 32;
766 break;
767 case GDK_F19:
768 code = 33;
769 break;
770 case GDK_F20:
771 code = 34;
772 break;
773 }
774 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
775 case GDK_Home: case GDK_KP_Home:
776 code = 1;
777 break;
778 case GDK_Insert: case GDK_KP_Insert:
779 code = 2;
780 break;
781 case GDK_Delete: case GDK_KP_Delete:
782 code = 3;
783 break;
784 case GDK_End: case GDK_KP_End:
785 code = 4;
786 break;
787 case GDK_Page_Up: case GDK_KP_Page_Up:
788 code = 5;
789 break;
790 case GDK_Page_Down: case GDK_KP_Page_Down:
791 code = 6;
792 break;
793 }
794 /* Reorder edit keys to physical order */
3ea863a3 795 if (inst->cfg.funky_type == 3 && code <= 6)
a57cd64b 796 code = "\0\2\1\4\5\3\6"[code];
797
5def7522 798 if (inst->term->vt52_mode && code > 0 && code <= 6) {
a57cd64b 799 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
800 goto done;
801 }
802
3ea863a3 803 if (inst->cfg.funky_type == 5 && /* SCO function keys */
a57cd64b 804 code >= 11 && code <= 34) {
805 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
806 int index = 0;
807 switch (event->keyval) {
808 case GDK_F1: index = 0; break;
809 case GDK_F2: index = 1; break;
810 case GDK_F3: index = 2; break;
811 case GDK_F4: index = 3; break;
812 case GDK_F5: index = 4; break;
813 case GDK_F6: index = 5; break;
814 case GDK_F7: index = 6; break;
815 case GDK_F8: index = 7; break;
816 case GDK_F9: index = 8; break;
817 case GDK_F10: index = 9; break;
818 case GDK_F11: index = 10; break;
819 case GDK_F12: index = 11; break;
820 }
821 if (event->state & GDK_SHIFT_MASK) index += 12;
822 if (event->state & GDK_CONTROL_MASK) index += 24;
823 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
824 goto done;
825 }
3ea863a3 826 if (inst->cfg.funky_type == 5 && /* SCO small keypad */
a57cd64b 827 code >= 1 && code <= 6) {
828 char codes[] = "HL.FIG";
829 if (code == 3) {
830 output[1] = '\x7F';
831 end = 2;
832 } else {
833 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
834 }
835 goto done;
836 }
3ea863a3 837 if ((inst->term->vt52_mode || inst->cfg.funky_type == 4) &&
887035a5 838 code >= 11 && code <= 24) {
a57cd64b 839 int offt = 0;
840 if (code > 15)
841 offt++;
842 if (code > 21)
843 offt++;
5def7522 844 if (inst->term->vt52_mode)
a57cd64b 845 end = 1 + sprintf(output+1,
846 "\x1B%c", code + 'P' - 11 - offt);
847 else
848 end = 1 + sprintf(output+1,
849 "\x1BO%c", code + 'P' - 11 - offt);
850 goto done;
851 }
3ea863a3 852 if (inst->cfg.funky_type == 1 && code >= 11 && code <= 15) {
a57cd64b 853 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
854 goto done;
855 }
3ea863a3 856 if (inst->cfg.funky_type == 2 && code >= 11 && code <= 14) {
5def7522 857 if (inst->term->vt52_mode)
a57cd64b 858 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
859 else
860 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
861 goto done;
862 }
3ea863a3 863 if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
a57cd64b 864 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
865 goto done;
866 }
867 if (code) {
868 end = 1 + sprintf(output+1, "\x1B[%d~", code);
869 goto done;
870 }
871 }
872
873 /*
874 * Cursor keys. (This includes the numberpad cursor keys,
875 * if we haven't already done them due to app keypad mode.)
876 *
877 * Here we also process un-numlocked un-appkeypadded KP5,
878 * which sends ESC [ G.
879 */
880 {
881 int xkey = 0;
882 switch (event->keyval) {
883 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
884 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
885 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
886 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
887 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
888 }
889 if (xkey) {
890 /*
891 * The arrow keys normally do ESC [ A and so on. In
892 * app cursor keys mode they do ESC O A instead.
893 * Ctrl toggles the two modes.
894 */
5def7522 895 if (inst->term->vt52_mode) {
a57cd64b 896 end = 1 + sprintf(output+1, "\033%c", xkey);
5def7522 897 } else if (!inst->term->app_cursor_keys ^
a57cd64b 898 !(event->state & GDK_CONTROL_MASK)) {
899 end = 1 + sprintf(output+1, "\033O%c", xkey);
900 } else {
901 end = 1 + sprintf(output+1, "\033[%c", xkey);
902 }
903 goto done;
904 }
905 }
c33c3d76 906 goto done;
907 }
a57cd64b 908
c33c3d76 909 done:
a57cd64b 910
c33c3d76 911 if (end-start > 0) {
a57cd64b 912#ifdef KEY_DEBUGGING
c33c3d76 913 int i;
914 printf("generating sequence:");
915 for (i = start; i < end; i++)
916 printf(" %02x", (unsigned char) output[i]);
917 printf("\n");
a57cd64b 918#endif
c33c3d76 919
085f4a68 920 if (!inst->direct_to_font) {
facd762c 921 /*
922 * The stuff we've just generated is assumed to be
923 * ISO-8859-1! This sounds insane, but `man
924 * XLookupString' agrees: strings of this type returned
925 * from the X server are hardcoded to 8859-1. Strictly
926 * speaking we should be doing this using some sort of
927 * GtkIMContext, which (if we're lucky) would give us
928 * our data directly in Unicode; but that's not
929 * supported in GTK 1.2 as far as I can tell, and it's
930 * poorly documented even in 2.0, so it'll have to
931 * wait.
932 */
933 lpage_send(inst->ldisc, CS_ISO8859_1, output+start, end-start, 1);
934 } else {
935 /*
936 * In direct-to-font mode, we just send the string
937 * exactly as we received it.
938 */
939 ldisc_send(inst->ldisc, output+start, end-start, 1);
940 }
2dc6356a 941
a8327734 942 show_mouseptr(inst, 0);
5def7522 943 term_seen_key_event(inst->term);
944 term_out(inst->term);
1709795f 945 }
946
947 return TRUE;
f7f27309 948}
949
e6346999 950gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
951{
952 struct gui_data *inst = (struct gui_data *)data;
953 int shift, ctrl, alt, x, y, button, act;
954
a8327734 955 show_mouseptr(inst, 1);
57e636ca 956
bab6e572 957 if (event->button == 4 && event->type == GDK_BUTTON_PRESS) {
5def7522 958 term_scroll(inst->term, 0, -5);
bab6e572 959 return TRUE;
960 }
961 if (event->button == 5 && event->type == GDK_BUTTON_PRESS) {
5def7522 962 term_scroll(inst->term, 0, +5);
bab6e572 963 return TRUE;
964 }
965
e6346999 966 shift = event->state & GDK_SHIFT_MASK;
967 ctrl = event->state & GDK_CONTROL_MASK;
968 alt = event->state & GDK_MOD1_MASK;
47e4e735 969
970 if (event->button == 3 && ctrl) {
971 gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL,
972 event->button, event->time);
973 return TRUE;
974 }
975
e6346999 976 if (event->button == 1)
977 button = MBT_LEFT;
978 else if (event->button == 2)
979 button = MBT_MIDDLE;
980 else if (event->button == 3)
981 button = MBT_RIGHT;
982 else
983 return FALSE; /* don't even know what button! */
984
985 switch (event->type) {
986 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
987 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
988 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
989 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
990 default: return FALSE; /* don't know this event type */
991 }
992
3ea863a3 993 if (send_raw_mouse && !(inst->cfg.mouse_override && shift) &&
e6346999 994 act != MA_CLICK && act != MA_RELEASE)
995 return TRUE; /* we ignore these in raw mouse mode */
996
3ea863a3 997 x = (event->x - inst->cfg.window_border) / inst->font_width;
998 y = (event->y - inst->cfg.window_border) / inst->font_height;
e6346999 999
fc5b0934 1000 term_mouse(inst->term, button, translate_button(button), act,
1001 x, y, shift, ctrl, alt);
e6346999 1002
1003 return TRUE;
1004}
1005
1006gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1007{
1008 struct gui_data *inst = (struct gui_data *)data;
1009 int shift, ctrl, alt, x, y, button;
1010
a8327734 1011 show_mouseptr(inst, 1);
57e636ca 1012
e6346999 1013 shift = event->state & GDK_SHIFT_MASK;
1014 ctrl = event->state & GDK_CONTROL_MASK;
1015 alt = event->state & GDK_MOD1_MASK;
1016 if (event->state & GDK_BUTTON1_MASK)
1017 button = MBT_LEFT;
1018 else if (event->state & GDK_BUTTON2_MASK)
1019 button = MBT_MIDDLE;
1020 else if (event->state & GDK_BUTTON3_MASK)
1021 button = MBT_RIGHT;
1022 else
1023 return FALSE; /* don't even know what button! */
1024
3ea863a3 1025 x = (event->x - inst->cfg.window_border) / inst->font_width;
1026 y = (event->y - inst->cfg.window_border) / inst->font_height;
e6346999 1027
fc5b0934 1028 term_mouse(inst->term, button, translate_button(button), MA_DRAG,
1029 x, y, shift, ctrl, alt);
e6346999 1030
1031 return TRUE;
1032}
1033
b9d7bcad 1034void frontend_keypress(void *handle)
90cfd8f4 1035{
b9d7bcad 1036 struct gui_data *inst = (struct gui_data *)handle;
1037
90cfd8f4 1038 /*
1039 * If our child process has exited but not closed, terminate on
1040 * any keypress.
1041 */
1042 if (inst->exited)
1043 exit(0);
1044}
1045
f7f27309 1046gint timer_func(gpointer data)
1047{
90cfd8f4 1048 struct gui_data *inst = (struct gui_data *)data;
74aca06d 1049 int exitcode;
a0e16eb1 1050
3f935d5b 1051 if (!inst->exited &&
1052 (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) {
74aca06d 1053 inst->exited = TRUE;
1054 if (inst->cfg.close_on_exit == FORCE_ON ||
1055 (inst->cfg.close_on_exit == AUTO && exitcode == 0))
1056 exit(0); /* just go. */
a0e16eb1 1057 }
f7f27309 1058
5def7522 1059 term_update(inst->term);
1060 term_blink(inst->term, 0);
f7f27309 1061 return TRUE;
1062}
1063
74aca06d 1064void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
054d8535 1065{
3f935d5b 1066 /*
1067 * We must process exceptional notifications before ordinary
1068 * readability ones, or we may go straight past the urgent
1069 * marker.
1070 */
1071 if (condition & GDK_INPUT_EXCEPTION)
1072 select_result(sourcefd, 4);
1073 if (condition & GDK_INPUT_READ)
1074 select_result(sourcefd, 1);
1075 if (condition & GDK_INPUT_WRITE)
1076 select_result(sourcefd, 2);
054d8535 1077}
1078
f7f27309 1079void destroy(GtkWidget *widget, gpointer data)
1080{
1081 gtk_main_quit();
1082}
1083
1084gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
1085{
a8327734 1086 struct gui_data *inst = (struct gui_data *)data;
5def7522 1087 inst->term->has_focus = event->in;
1088 term_out(inst->term);
1089 term_update(inst->term);
a8327734 1090 show_mouseptr(inst, 1);
1709795f 1091 return FALSE;
1092}
1093
1094/*
1095 * set or clear the "raw mouse message" mode
1096 */
a8327734 1097void set_raw_mouse_mode(void *frontend, int activate)
1709795f 1098{
a8327734 1099 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1100 activate = activate && !inst->cfg.no_mouse_rep;
d64838e1 1101 send_raw_mouse = activate;
1102 if (send_raw_mouse)
57e636ca 1103 inst->currcursor = inst->rawcursor;
d64838e1 1104 else
57e636ca 1105 inst->currcursor = inst->textcursor;
a8327734 1106 show_mouseptr(inst, inst->mouseptr_visible);
1709795f 1107}
1108
a8327734 1109void request_resize(void *frontend, int w, int h)
1709795f 1110{
a8327734 1111 struct gui_data *inst = (struct gui_data *)frontend;
51b13830 1112 int large_x, large_y;
1113 int offset_x, offset_y;
1114 int area_x, area_y;
1115 GtkRequisition inner, outer;
1116
1117 /*
1118 * This is a heinous hack dreamed up by the gnome-terminal
1119 * people to get around a limitation in gtk. The problem is
1120 * that in order to set the size correctly we really need to be
1121 * calling gtk_window_resize - but that needs to know the size
1122 * of the _whole window_, not the drawing area. So what we do
1123 * is to set an artificially huge size request on the drawing
1124 * area, recompute the resulting size request on the window,
1125 * and look at the difference between the two. That gives us
1126 * the x and y offsets we need to translate drawing area size
1127 * into window size for real, and then we call
1128 * gtk_window_resize.
1129 */
1130
1131 /*
1132 * We start by retrieving the current size of the whole window.
1133 * Adding a bit to _that_ will give us a value we can use as a
1134 * bogus size request which guarantees to be bigger than the
1135 * current size of the drawing area.
1136 */
a8327734 1137 get_window_pixels(inst, &large_x, &large_y);
51b13830 1138 large_x += 32;
1139 large_y += 32;
1140
1141#if GTK_CHECK_VERSION(2,0,0)
1142 gtk_widget_set_size_request(inst->area, large_x, large_y);
1143#else
1144 gtk_widget_set_usize(inst->area, large_x, large_y);
1145#endif
1146 gtk_widget_size_request(inst->area, &inner);
1147 gtk_widget_size_request(inst->window, &outer);
1148
1149 offset_x = outer.width - inner.width;
1150 offset_y = outer.height - inner.height;
1151
3ea863a3 1152 area_x = inst->font_width * w + 2*inst->cfg.window_border;
1153 area_y = inst->font_height * h + 2*inst->cfg.window_border;
51b13830 1154
1155 /*
1156 * Now we must set the size request on the drawing area back to
1157 * something sensible before we commit the real resize. Best
1158 * way to do this, I think, is to set it to what the size is
1159 * really going to end up being.
1160 */
1161#if GTK_CHECK_VERSION(2,0,0)
1162 gtk_widget_set_size_request(inst->area, area_x, area_y);
1163#else
1164 gtk_widget_set_usize(inst->area, area_x, area_y);
56b9b9a7 1165 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y);
51b13830 1166#endif
1167
56b9b9a7 1168 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window));
1169
51b13830 1170#if GTK_CHECK_VERSION(2,0,0)
1171 gtk_window_resize(GTK_WINDOW(inst->window),
1172 area_x + offset_x, area_y + offset_y);
1173#else
1174 gdk_window_resize(inst->window->window,
1175 area_x + offset_x, area_y + offset_y);
1176#endif
1709795f 1177}
1178
a8327734 1179static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b)
26b8e7cc 1180{
1181 gboolean success[1];
1182
1183 inst->cols[n].red = r * 0x0101;
1184 inst->cols[n].green = g * 0x0101;
1185 inst->cols[n].blue = b * 0x0101;
1186
56b9b9a7 1187 gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1);
26b8e7cc 1188 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
1189 FALSE, FALSE, success);
1190 if (!success[0])
a4e8ffb0 1191 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname,
26b8e7cc 1192 n, r, g, b);
1193}
1194
a8327734 1195void set_window_background(struct gui_data *inst)
7428c509 1196{
1197 if (inst->area && inst->area->window)
1198 gdk_window_set_background(inst->area->window, &inst->cols[18]);
1199 if (inst->window && inst->window->window)
1200 gdk_window_set_background(inst->window->window, &inst->cols[18]);
1201}
1202
a8327734 1203void palette_set(void *frontend, int n, int r, int g, int b)
1709795f 1204{
a8327734 1205 struct gui_data *inst = (struct gui_data *)frontend;
26b8e7cc 1206 static const int first[21] = {
1207 0, 2, 4, 6, 8, 10, 12, 14,
1208 1, 3, 5, 7, 9, 11, 13, 15,
1209 16, 17, 18, 20, 22
1210 };
a8327734 1211 real_palette_set(inst, first[n], r, g, b);
26b8e7cc 1212 if (first[n] >= 18)
a8327734 1213 real_palette_set(inst, first[n] + 1, r, g, b);
7428c509 1214 if (first[n] == 18)
a8327734 1215 set_window_background(inst);
1709795f 1216}
26b8e7cc 1217
a8327734 1218void palette_reset(void *frontend)
1709795f 1219{
a8327734 1220 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1221 /* This maps colour indices in inst->cfg to those used in inst->cols. */
26b8e7cc 1222 static const int ww[] = {
1223 6, 7, 8, 9, 10, 11, 12, 13,
1224 14, 15, 16, 17, 18, 19, 20, 21,
1225 0, 1, 2, 3, 4, 5
1226 };
1227 gboolean success[NCOLOURS];
1228 int i;
1229
1230 assert(lenof(ww) == NCOLOURS);
1231
1232 if (!inst->colmap) {
1233 inst->colmap = gdk_colormap_get_system();
1234 } else {
1235 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
1236 }
1237
1238 for (i = 0; i < NCOLOURS; i++) {
3ea863a3 1239 inst->cols[i].red = inst->cfg.colours[ww[i]][0] * 0x0101;
1240 inst->cols[i].green = inst->cfg.colours[ww[i]][1] * 0x0101;
1241 inst->cols[i].blue = inst->cfg.colours[ww[i]][2] * 0x0101;
26b8e7cc 1242 }
1243
1244 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
1245 FALSE, FALSE, success);
1246 for (i = 0; i < NCOLOURS; i++) {
1247 if (!success[i])
a4e8ffb0 1248 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1249 appname, i, inst->cfg.colours[i][0],
1250 inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
26b8e7cc 1251 }
7428c509 1252
a8327734 1253 set_window_background(inst);
1709795f 1254}
1255
a8327734 1256void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1709795f 1257{
a8327734 1258 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1259 if (inst->pasteout_data)
1260 sfree(inst->pasteout_data);
2dc6356a 1261 if (inst->pasteout_data_utf8)
1262 sfree(inst->pasteout_data_utf8);
1263
facd762c 1264 /*
1265 * Set up UTF-8 paste data. This only happens if we aren't in
1266 * direct-to-font mode using the D800 hack.
1267 */
085f4a68 1268 if (!inst->direct_to_font) {
2dc6356a 1269 wchar_t *tmp = data;
1270 int tmplen = len;
facd762c 1271
3d88e64d 1272 inst->pasteout_data_utf8 = snewn(len*6, char);
facd762c 1273 inst->pasteout_data_utf8_len = len*6;
2dc6356a 1274 inst->pasteout_data_utf8_len =
1275 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1276 inst->pasteout_data_utf8_len,
1277 CS_UTF8, NULL, NULL, 0);
085f4a68 1278 if (inst->pasteout_data_utf8_len == 0) {
1279 sfree(inst->pasteout_data_utf8);
1280 inst->pasteout_data_utf8 = NULL;
1281 } else {
1282 inst->pasteout_data_utf8 =
3d88e64d 1283 sresize(inst->pasteout_data_utf8,
1284 inst->pasteout_data_utf8_len, char);
085f4a68 1285 }
facd762c 1286 } else {
1287 inst->pasteout_data_utf8 = NULL;
1288 inst->pasteout_data_utf8_len = 0;
2dc6356a 1289 }
1290
3d88e64d 1291 inst->pasteout_data = snewn(len*6, char);
085f4a68 1292 inst->pasteout_data_len = len*6;
21d2b241 1293 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1294 data, len, inst->pasteout_data,
1295 inst->pasteout_data_len,
1296 NULL, NULL, NULL);
085f4a68 1297 if (inst->pasteout_data_len == 0) {
1298 sfree(inst->pasteout_data);
1299 inst->pasteout_data = NULL;
1300 } else {
1301 inst->pasteout_data =
3d88e64d 1302 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
085f4a68 1303 }
e6346999 1304
1305 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1306 GDK_CURRENT_TIME)) {
1307 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1308 GDK_SELECTION_TYPE_STRING, 1);
dd72dfa3 1309 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
8c161662 1310 compound_text_atom, 1);
facd762c 1311 if (inst->pasteout_data_utf8)
1312 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
8c161662 1313 utf8_string_atom, 1);
e6346999 1314 }
c076966d 1315
1316 if (must_deselect)
1317 term_deselect(inst->term);
e6346999 1318}
1319
1320void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1321 guint info, guint time_stamp, gpointer data)
1322{
a8327734 1323 struct gui_data *inst = (struct gui_data *)data;
8c161662 1324 if (seldata->target == utf8_string_atom)
2dc6356a 1325 gtk_selection_data_set(seldata, seldata->target, 8,
1326 inst->pasteout_data_utf8,
1327 inst->pasteout_data_utf8_len);
1328 else
1329 gtk_selection_data_set(seldata, seldata->target, 8,
1330 inst->pasteout_data, inst->pasteout_data_len);
e6346999 1331}
1332
1333gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1334 gpointer data)
1335{
a8327734 1336 struct gui_data *inst = (struct gui_data *)data;
5def7522 1337 term_deselect(inst->term);
e6346999 1338 if (inst->pasteout_data)
1339 sfree(inst->pasteout_data);
2dc6356a 1340 if (inst->pasteout_data_utf8)
1341 sfree(inst->pasteout_data_utf8);
e6346999 1342 inst->pasteout_data = NULL;
1343 inst->pasteout_data_len = 0;
2dc6356a 1344 inst->pasteout_data_utf8 = NULL;
1345 inst->pasteout_data_utf8_len = 0;
e6346999 1346 return TRUE;
1347}
1348
a8327734 1349void request_paste(void *frontend)
e6346999 1350{
a8327734 1351 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1352 /*
1353 * In Unix, pasting is asynchronous: all we can do at the
1354 * moment is to call gtk_selection_convert(), and when the data
1355 * comes back _then_ we can call term_do_paste().
1356 */
2dc6356a 1357
085f4a68 1358 if (!inst->direct_to_font) {
facd762c 1359 /*
1360 * First we attempt to retrieve the selection as a UTF-8
1361 * string (which we will convert to the correct code page
1362 * before sending to the session, of course). If that
1363 * fails, selection_received() will be informed and will
1364 * fall back to an ordinary string.
1365 */
1366 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
8c161662 1367 utf8_string_atom, GDK_CURRENT_TIME);
facd762c 1368 } else {
1369 /*
1370 * If we're in direct-to-font mode, we disable UTF-8
1371 * pasting, and go straight to ordinary string data.
1372 */
1373 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1374 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1375 }
e6346999 1376}
1377
0f660c8f 1378gint idle_paste_func(gpointer data); /* forward ref */
1379
e6346999 1380void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
27651070 1381 guint time, gpointer data)
e6346999 1382{
a8327734 1383 struct gui_data *inst = (struct gui_data *)data;
1384
8c161662 1385 if (seldata->target == utf8_string_atom && seldata->length <= 0) {
2dc6356a 1386 /*
1387 * Failed to get a UTF-8 selection string. Try an ordinary
1388 * string.
1389 */
1390 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1391 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1392 return;
1393 }
1394
1395 /*
1396 * Any other failure should just go foom.
1397 */
e6346999 1398 if (seldata->length <= 0 ||
2dc6356a 1399 (seldata->type != GDK_SELECTION_TYPE_STRING &&
8c161662 1400 seldata->type != utf8_string_atom))
e6346999 1401 return; /* Nothing happens. */
1402
1403 if (inst->pastein_data)
1404 sfree(inst->pastein_data);
1405
3d88e64d 1406 inst->pastein_data = snewn(seldata->length, wchar_t);
e6346999 1407 inst->pastein_data_len = seldata->length;
2dc6356a 1408 inst->pastein_data_len =
8c161662 1409 mb_to_wc((seldata->type == utf8_string_atom ?
21d2b241 1410 CS_UTF8 : inst->ucsdata.line_codepage),
2dc6356a 1411 0, seldata->data, seldata->length,
1412 inst->pastein_data, inst->pastein_data_len);
e6346999 1413
5def7522 1414 term_do_paste(inst->term);
0f660c8f 1415
5def7522 1416 if (term_paste_pending(inst->term))
0f660c8f 1417 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1418}
1419
1420gint idle_paste_func(gpointer data)
1421{
1422 struct gui_data *inst = (struct gui_data *)data;
1423
5def7522 1424 if (term_paste_pending(inst->term))
1425 term_paste(inst->term);
0f660c8f 1426 else
1427 gtk_idle_remove(inst->term_paste_idle_id);
1428
1429 return TRUE;
1709795f 1430}
1431
0f660c8f 1432
a8327734 1433void get_clip(void *frontend, wchar_t ** p, int *len)
1709795f 1434{
a8327734 1435 struct gui_data *inst = (struct gui_data *)frontend;
1436
1709795f 1437 if (p) {
e6346999 1438 *p = inst->pastein_data;
1439 *len = inst->pastein_data_len;
1709795f 1440 }
1441}
1442
a8327734 1443void set_title(void *frontend, char *title)
1709795f 1444{
a8327734 1445 struct gui_data *inst = (struct gui_data *)frontend;
12d200b1 1446 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1447 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1448 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1709795f 1449}
1450
a8327734 1451void set_icon(void *frontend, char *title)
1709795f 1452{
a8327734 1453 struct gui_data *inst = (struct gui_data *)frontend;
16891265 1454 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1455 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1456 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1709795f 1457}
1458
a8327734 1459void set_sbar(void *frontend, int total, int start, int page)
1709795f 1460{
a8327734 1461 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1462 if (!inst->cfg.scrollbar)
330f49be 1463 return;
6a5e84dd 1464 inst->sbar_adjust->lower = 0;
1465 inst->sbar_adjust->upper = total;
1466 inst->sbar_adjust->value = start;
1467 inst->sbar_adjust->page_size = page;
1468 inst->sbar_adjust->step_increment = 1;
1469 inst->sbar_adjust->page_increment = page/2;
88e6b9ca 1470 inst->ignore_sbar = TRUE;
6a5e84dd 1471 gtk_adjustment_changed(inst->sbar_adjust);
88e6b9ca 1472 inst->ignore_sbar = FALSE;
6a5e84dd 1473}
1474
1475void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1476{
a8327734 1477 struct gui_data *inst = (struct gui_data *)data;
1478
3ea863a3 1479 if (!inst->cfg.scrollbar)
330f49be 1480 return;
88e6b9ca 1481 if (!inst->ignore_sbar)
5def7522 1482 term_scroll(inst->term, 1, (int)adj->value);
1709795f 1483}
1484
a8327734 1485void sys_cursor(void *frontend, int x, int y)
1709795f 1486{
1487 /*
1488 * This is meaningless under X.
1489 */
1490}
1491
0ce89525 1492/*
1493 * This is still called when mode==BELL_VISUAL, even though the
1494 * visual bell is handled entirely within terminal.c, because we
1495 * may want to perform additional actions on any kind of bell (for
1496 * example, taskbar flashing in Windows).
1497 */
a8327734 1498void beep(void *frontend, int mode)
1709795f 1499{
0ce89525 1500 if (mode != BELL_VISUAL)
1501 gdk_beep();
1709795f 1502}
1503
2102eb8a 1504int char_width(Context ctx, int uc)
1709795f 1505{
1506 /*
1507 * Under X, any fixed-width font really _is_ fixed-width.
1508 * Double-width characters will be dealt with using a separate
1509 * font. For the moment we can simply return 1.
1510 */
1511 return 1;
1512}
1513
a8327734 1514Context get_ctx(void *frontend)
1709795f 1515{
a8327734 1516 struct gui_data *inst = (struct gui_data *)frontend;
1517 struct draw_ctx *dctx;
1518
d64838e1 1519 if (!inst->area->window)
1520 return NULL;
a8327734 1521
3d88e64d 1522 dctx = snew(struct draw_ctx);
a8327734 1523 dctx->inst = inst;
1524 dctx->gc = gdk_gc_new(inst->area->window);
1525 return dctx;
1709795f 1526}
1527
1528void free_ctx(Context ctx)
1529{
a8327734 1530 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1531 /* struct gui_data *inst = dctx->inst; */
1532 GdkGC *gc = dctx->gc;
1709795f 1533 gdk_gc_unref(gc);
a8327734 1534 sfree(dctx);
1709795f 1535}
1536
1537/*
1538 * Draw a line of text in the window, at given character
1539 * coordinates, in given attributes.
1540 *
1541 * We are allowed to fiddle with the contents of `text'.
1542 */
1a675941 1543void do_text_internal(Context ctx, int x, int y, char *text, int len,
1544 unsigned long attr, int lattr)
1709795f 1545{
a8327734 1546 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1547 struct gui_data *inst = dctx->inst;
1548 GdkGC *gc = dctx->gc;
1549
006238cb 1550 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
d64838e1 1551
37ca32ed 1552 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1553 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
1554 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1555 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
d64838e1 1556 if (attr & ATTR_REVERSE) {
1557 t = nfg;
1558 nfg = nbg;
1559 nbg = t;
1560 }
3ea863a3 1561 if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
37ca32ed 1562 nfg |= 1;
3ea863a3 1563 if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
37ca32ed 1564 nbg |= 1;
d64838e1 1565 if (attr & TATTR_ACTCURS) {
1566 nfg = NCOLOURS-2;
1567 nbg = NCOLOURS-1;
1568 }
1569
5bf50ab2 1570 fontid = shadow = 0;
006238cb 1571
1572 if (attr & ATTR_WIDE) {
1573 widefactor = 2;
1574 fontid |= 2;
1575 } else {
1576 widefactor = 1;
1577 }
1578
3ea863a3 1579 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
006238cb 1580 if (inst->fonts[fontid | 1])
1581 fontid |= 1;
5bf50ab2 1582 else
1583 shadow = 1;
1584 }
1585
f34df346 1586 if (lattr != LATTR_NORM) {
f34df346 1587 x *= 2;
5def7522 1588 if (x >= inst->term->cols)
614bb468 1589 return;
006238cb 1590 if (x + len*2*widefactor > inst->term->cols)
1591 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
623f81b7 1592 rlen = len * 2;
1593 } else
1594 rlen = len;
1595
1596 {
1597 GdkRectangle r;
1598
3ea863a3 1599 r.x = x*inst->font_width+inst->cfg.window_border;
1600 r.y = y*inst->font_height+inst->cfg.window_border;
006238cb 1601 r.width = rlen*widefactor*inst->font_width;
623f81b7 1602 r.height = inst->font_height;
1603 gdk_gc_set_clip_rectangle(gc, &r);
f34df346 1604 }
1605
d64838e1 1606 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
83616aab 1607 gdk_draw_rectangle(inst->pixmap, gc, 1,
3ea863a3 1608 x*inst->font_width+inst->cfg.window_border,
1609 y*inst->font_height+inst->cfg.window_border,
006238cb 1610 rlen*widefactor*inst->font_width, inst->font_height);
6a5e84dd 1611
d64838e1 1612 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2dc6356a 1613 {
1614 GdkWChar *gwcs;
1615 gchar *gcs;
1616 wchar_t *wcs;
1617 int i;
1618
3d88e64d 1619 wcs = snewn(len+1, wchar_t);
2dc6356a 1620 for (i = 0; i < len; i++) {
1621 wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
1622 }
1623
006238cb 1624 if (inst->fonts[fontid] == NULL) {
1625 /*
1626 * The font for this contingency does not exist.
1627 * Typically this means we've been given ATTR_WIDE
1628 * character and have no wide font. So we display
1629 * nothing at all; such is life.
1630 */
1631 } else if (inst->fontinfo[fontid].is_wide) {
aa153a7f 1632 /*
1633 * At least one version of gdk_draw_text_wc() has a
1634 * weird bug whereby it reads `len' elements of the
1635 * input string, but only draws `len/2'. Hence I'm
1636 * going to make its input array twice as long as it
1637 * theoretically needs to be, and pass in twice the
1638 * actual number of characters. If a fixed gdk actually
1639 * takes the doubled length seriously, then (a) the
1640 * array will stand scrutiny up to the full length, (b)
1641 * the spare elements of the array are full of zeroes
1642 * which will probably be an empty glyph in the font,
1643 * and (c) the clip rectangle should prevent it causing
1644 * trouble anyway.
1645 */
3d88e64d 1646 gwcs = snewn(len*2+1, GdkWChar);
aa153a7f 1647 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2dc6356a 1648 /*
1649 * FIXME: when we have a wide-char equivalent of
1650 * from_unicode, use it instead of this.
1651 */
1652 for (i = 0; i <= len; i++)
1653 gwcs[i] = wcs[i];
1654 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
3ea863a3 1655 x*inst->font_width+inst->cfg.window_border,
1656 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2dc6356a 1657 gwcs, len*2);
1658 sfree(gwcs);
1659 } else {
3d88e64d 1660 gcs = snewn(len+1, gchar);
facd762c 1661 wc_to_mb(inst->fontinfo[fontid].charset, 0,
21d2b241 1662 wcs, len, gcs, len, ".", NULL, NULL);
2dc6356a 1663 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
3ea863a3 1664 x*inst->font_width+inst->cfg.window_border,
1665 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2dc6356a 1666 gcs, len);
1667 sfree(gcs);
1668 }
1669 sfree(wcs);
1670 }
d64838e1 1671
5bf50ab2 1672 if (shadow) {
1673 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
3ea863a3 1674 x*inst->font_width+inst->cfg.window_border + inst->cfg.shadowboldoffset,
1675 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
5bf50ab2 1676 text, len);
1677 }
1678
d64838e1 1679 if (attr & ATTR_UNDER) {
1680 int uheight = inst->fonts[0]->ascent + 1;
83616aab 1681 if (uheight >= inst->font_height)
1682 uheight = inst->font_height - 1;
3ea863a3 1683 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
1684 y*inst->font_height + uheight + inst->cfg.window_border,
1685 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
1686 y*inst->font_height + uheight + inst->cfg.window_border);
d64838e1 1687 }
1688
f34df346 1689 if (lattr != LATTR_NORM) {
1690 /*
1691 * I can't find any plausible StretchBlt equivalent in the
1692 * X server, so I'm going to do this the slow and painful
1693 * way. This will involve repeated calls to
1694 * gdk_draw_pixmap() to stretch the text horizontally. It's
1695 * O(N^2) in time and O(N) in network bandwidth, but you
1696 * try thinking of a better way. :-(
1697 */
1698 int i;
006238cb 1699 for (i = 0; i < len * widefactor * inst->font_width; i++) {
f34df346 1700 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 1701 x*inst->font_width+inst->cfg.window_border + 2*i,
1702 y*inst->font_height+inst->cfg.window_border,
1703 x*inst->font_width+inst->cfg.window_border + 2*i+1,
1704 y*inst->font_height+inst->cfg.window_border,
f34df346 1705 len * inst->font_width - i, inst->font_height);
1706 }
1707 len *= 2;
1708 if (lattr != LATTR_WIDE) {
1709 int dt, db;
1710 /* Now stretch vertically, in the same way. */
1711 if (lattr == LATTR_BOT)
1712 dt = 0, db = 1;
1713 else
1714 dt = 1, db = 0;
1715 for (i = 0; i < inst->font_height; i+=2) {
1716 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 1717 x*inst->font_width+inst->cfg.window_border,
1718 y*inst->font_height+inst->cfg.window_border+dt*i+db,
1719 x*widefactor*inst->font_width+inst->cfg.window_border,
1720 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
f34df346 1721 len * inst->font_width, inst->font_height-i-1);
1722 }
1723 }
1a675941 1724 }
1725}
1726
1727void do_text(Context ctx, int x, int y, char *text, int len,
1728 unsigned long attr, int lattr)
1729{
a8327734 1730 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1731 struct gui_data *inst = dctx->inst;
1732 GdkGC *gc = dctx->gc;
006238cb 1733 int widefactor;
1a675941 1734
1735 do_text_internal(ctx, x, y, text, len, attr, lattr);
1736
006238cb 1737 if (attr & ATTR_WIDE) {
1738 widefactor = 2;
1739 } else {
1740 widefactor = 1;
1741 }
1742
1a675941 1743 if (lattr != LATTR_NORM) {
1744 x *= 2;
5def7522 1745 if (x >= inst->term->cols)
1a675941 1746 return;
006238cb 1747 if (x + len*2*widefactor > inst->term->cols)
1748 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
f34df346 1749 len *= 2;
1750 }
1751
d64838e1 1752 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 1753 x*inst->font_width+inst->cfg.window_border,
1754 y*inst->font_height+inst->cfg.window_border,
1755 x*inst->font_width+inst->cfg.window_border,
1756 y*inst->font_height+inst->cfg.window_border,
006238cb 1757 len*widefactor*inst->font_width, inst->font_height);
1709795f 1758}
1759
1760void do_cursor(Context ctx, int x, int y, char *text, int len,
1761 unsigned long attr, int lattr)
1762{
a8327734 1763 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1764 struct gui_data *inst = dctx->inst;
1765 GdkGC *gc = dctx->gc;
1766
006238cb 1767 int passive, widefactor;
d64838e1 1768
1709795f 1769 if (attr & TATTR_PASCURS) {
1770 attr &= ~TATTR_PASCURS;
d64838e1 1771 passive = 1;
1772 } else
1773 passive = 0;
3ea863a3 1774 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
1a675941 1775 attr &= ~TATTR_ACTCURS;
1776 }
1777 do_text_internal(ctx, x, y, text, len, attr, lattr);
1778
006238cb 1779 if (attr & ATTR_WIDE) {
1780 widefactor = 2;
1781 } else {
1782 widefactor = 1;
1783 }
1784
1a675941 1785 if (lattr != LATTR_NORM) {
1786 x *= 2;
5def7522 1787 if (x >= inst->term->cols)
1a675941 1788 return;
006238cb 1789 if (x + len*2*widefactor > inst->term->cols)
1790 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1a675941 1791 len *= 2;
1792 }
1793
3ea863a3 1794 if (inst->cfg.cursor_type == 0) {
1a675941 1795 /*
1796 * An active block cursor will already have been done by
1797 * the above do_text call, so we only need to do anything
1798 * if it's passive.
1799 */
1800 if (passive) {
1801 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1802 gdk_draw_rectangle(inst->pixmap, gc, 0,
3ea863a3 1803 x*inst->font_width+inst->cfg.window_border,
1804 y*inst->font_height+inst->cfg.window_border,
1a675941 1805 len*inst->font_width-1, inst->font_height-1);
1806 }
1807 } else {
1808 int uheight;
1809 int startx, starty, dx, dy, length, i;
1810
1811 int char_width;
1812
1813 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1814 char_width = 2*inst->font_width;
1815 else
1816 char_width = inst->font_width;
1817
3ea863a3 1818 if (inst->cfg.cursor_type == 1) {
1a675941 1819 uheight = inst->fonts[0]->ascent + 1;
1820 if (uheight >= inst->font_height)
1821 uheight = inst->font_height - 1;
1822
3ea863a3 1823 startx = x * inst->font_width + inst->cfg.window_border;
1824 starty = y * inst->font_height + inst->cfg.window_border + uheight;
1a675941 1825 dx = 1;
1826 dy = 0;
1827 length = len * char_width;
1828 } else {
1829 int xadjust = 0;
1830 if (attr & TATTR_RIGHTCURS)
1831 xadjust = char_width - 1;
3ea863a3 1832 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
1833 starty = y * inst->font_height + inst->cfg.window_border;
1a675941 1834 dx = 0;
1835 dy = 1;
1836 length = inst->font_height;
1837 }
1838
d64838e1 1839 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1a675941 1840 if (passive) {
1841 for (i = 0; i < length; i++) {
1842 if (i % 2 == 0) {
1843 gdk_draw_point(inst->pixmap, gc, startx, starty);
1844 }
1845 startx += dx;
1846 starty += dy;
1847 }
1848 } else {
1849 gdk_draw_line(inst->pixmap, gc, startx, starty,
1850 startx + (length-1) * dx, starty + (length-1) * dy);
1851 }
d64838e1 1852 }
1a675941 1853
1854 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 1855 x*inst->font_width+inst->cfg.window_border,
1856 y*inst->font_height+inst->cfg.window_border,
1857 x*inst->font_width+inst->cfg.window_border,
1858 y*inst->font_height+inst->cfg.window_border,
006238cb 1859 len*widefactor*inst->font_width, inst->font_height);
1709795f 1860}
1861
a8327734 1862GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
7798fa56 1863{
1864 /*
1865 * Truly hideous hack: GTK doesn't allow us to set the mouse
1866 * cursor foreground and background colours unless we've _also_
1867 * created our own cursor from bitmaps. Therefore, I need to
1868 * load the `cursor' font and draw glyphs from it on to
1869 * pixmaps, in order to construct my cursors with the fg and bg
1870 * I want. This is a gross hack, but it's more self-contained
1871 * than linking in Xlib to find the X window handle to
1872 * inst->area and calling XRecolorCursor, and it's more
1873 * futureproof than hard-coding the shapes as bitmap arrays.
1874 */
1875 static GdkFont *cursor_font = NULL;
1876 GdkPixmap *source, *mask;
1877 GdkGC *gc;
1878 GdkColor cfg = { 0, 65535, 65535, 65535 };
1879 GdkColor cbg = { 0, 0, 0, 0 };
1880 GdkColor dfg = { 1, 65535, 65535, 65535 };
1881 GdkColor dbg = { 0, 0, 0, 0 };
1882 GdkCursor *ret;
1883 gchar text[2];
1884 gint lb, rb, wid, asc, desc, w, h, x, y;
1885
57e636ca 1886 if (cursor_val == -2) {
7798fa56 1887 gdk_font_unref(cursor_font);
1888 return NULL;
1889 }
1890
57e636ca 1891 if (cursor_val >= 0 && !cursor_font)
7798fa56 1892 cursor_font = gdk_font_load("cursor");
1893
1894 /*
1895 * Get the text extent of the cursor in question. We use the
1896 * mask character for this, because it's typically slightly
1897 * bigger than the main character.
1898 */
57e636ca 1899 if (cursor_val >= 0) {
1900 text[1] = '\0';
1901 text[0] = (char)cursor_val + 1;
1902 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1903 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1904 } else {
1905 w = h = 1;
1906 x = y = 0;
1907 }
7798fa56 1908
1909 source = gdk_pixmap_new(NULL, w, h, 1);
1910 mask = gdk_pixmap_new(NULL, w, h, 1);
1911
1912 /*
1913 * Draw the mask character on the mask pixmap.
1914 */
7798fa56 1915 gc = gdk_gc_new(mask);
1916 gdk_gc_set_foreground(gc, &dbg);
1917 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
57e636ca 1918 if (cursor_val >= 0) {
1919 text[1] = '\0';
1920 text[0] = (char)cursor_val + 1;
1921 gdk_gc_set_foreground(gc, &dfg);
1922 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1923 }
7798fa56 1924 gdk_gc_unref(gc);
1925
1926 /*
1927 * Draw the main character on the source pixmap.
1928 */
7798fa56 1929 gc = gdk_gc_new(source);
1930 gdk_gc_set_foreground(gc, &dbg);
1931 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
57e636ca 1932 if (cursor_val >= 0) {
1933 text[1] = '\0';
1934 text[0] = (char)cursor_val;
1935 gdk_gc_set_foreground(gc, &dfg);
1936 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1937 }
7798fa56 1938 gdk_gc_unref(gc);
1939
1940 /*
1941 * Create the cursor.
1942 */
1943 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1944
1945 /*
1946 * Clean up.
1947 */
1948 gdk_pixmap_unref(source);
1949 gdk_pixmap_unref(mask);
1950
1951 return ret;
1952}
1953
1709795f 1954void modalfatalbox(char *p, ...)
1955{
1956 va_list ap;
1957 fprintf(stderr, "FATAL ERROR: ");
1958 va_start(ap, p);
1959 vfprintf(stderr, p, ap);
1960 va_end(ap);
1961 fputc('\n', stderr);
1962 exit(1);
f7f27309 1963}
1964
46a3419b 1965void cmdline_error(char *p, ...)
1966{
1967 va_list ap;
a4e8ffb0 1968 fprintf(stderr, "%s: ", appname);
46a3419b 1969 va_start(ap, p);
1970 vfprintf(stderr, p, ap);
1971 va_end(ap);
1972 fputc('\n', stderr);
1973 exit(1);
1974}
1975
a8327734 1976char *get_x_display(void *frontend)
755a6d84 1977{
1978 return gdk_get_display();
1979}
1980
7af753e6 1981long get_windowid(void *frontend)
1982{
1983 Terminal *term = (Terminal *)frontend;
1984 struct gui_data *inst = (struct gui_data *)(term->frontend);
1985 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
1986}
1987
96add444 1988static void help(FILE *fp) {
1989 if(fprintf(fp,
1990"pterm option summary:\n"
1991"\n"
1992" --display DISPLAY Specify X display to use (note '--')\n"
1993" -name PREFIX Prefix when looking up resources (default: pterm)\n"
1994" -fn FONT Normal text font\n"
1995" -fb FONT Bold text font\n"
aca2a702 1996" -geometry GEOMETRY Position and size of window (size in characters)\n"
96add444 1997" -sl LINES Number of lines of scrollback\n"
1998" -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1999" -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2000" -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2001" -T TITLE Window title\n"
2002" -ut, +ut Do(default) or do not update utmp\n"
2003" -ls, +ls Do(default) or do not make shell a login shell\n"
2004" -sb, +sb Do(default) or do not display a scrollbar\n"
2005" -log PATH Log all output to a file\n"
2006" -nethack Map numeric keypad to hjklyubn direction keys\n"
2007" -xrm RESOURCE-STRING Set an X resource\n"
2008" -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2009 ) < 0 || fflush(fp) < 0) {
2010 perror("output error");
2011 exit(1);
2012 }
2013}
2014
aca2a702 2015int do_cmdline(int argc, char **argv, int do_everything,
2016 struct gui_data *inst, Config *cfg)
f7f27309 2017{
6169c758 2018 int err = 0;
faec60ed 2019 extern char **pty_argv; /* declared in pty.c */
1d009ae7 2020 extern int use_pty_argv;
f7f27309 2021
faec60ed 2022 /*
b89ee4f3 2023 * Macros to make argument handling easier. Note that because
2024 * they need to call `continue', they cannot be contained in
2025 * the usual do {...} while (0) wrapper to make them
2026 * syntactically single statements; hence it is not legal to
2027 * use one of these macros as an unbraced statement between
2028 * `if' and `else'.
faec60ed 2029 */
b89ee4f3 2030#define EXPECTS_ARG { \
faec60ed 2031 if (--argc <= 0) { \
2032 err = 1; \
a4e8ffb0 2033 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
b89ee4f3 2034 continue; \
faec60ed 2035 } else \
2036 val = *++argv; \
b89ee4f3 2037}
2038#define SECOND_PASS_ONLY { if (!do_everything) continue; }
faec60ed 2039
2040 char *val;
6169c758 2041 while (--argc > 0) {
2042 char *p = *++argv;
46a3419b 2043 int ret;
2044
2045 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2046 do_everything ? 1 : -1, cfg);
2047
2048 if (ret == -2) {
2049 cmdline_error("option \"%s\" requires an argument", p);
2050 } else if (ret == 2) {
2051 --argc, ++argv; /* skip next argument */
2052 continue;
2053 } else if (ret == 1) {
2054 continue;
2055 }
2056
8e20db05 2057 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
faec60ed 2058 EXPECTS_ARG;
2059 SECOND_PASS_ONLY;
9a30e26b 2060 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2061 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
faec60ed 2062
2063 } else if (!strcmp(p, "-fb")) {
2064 EXPECTS_ARG;
2065 SECOND_PASS_ONLY;
9a30e26b 2066 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2067 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
faec60ed 2068
006238cb 2069 } else if (!strcmp(p, "-fw")) {
2070 EXPECTS_ARG;
2071 SECOND_PASS_ONLY;
9a30e26b 2072 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2073 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
006238cb 2074
2075 } else if (!strcmp(p, "-fwb")) {
2076 EXPECTS_ARG;
2077 SECOND_PASS_ONLY;
9a30e26b 2078 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2079 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
006238cb 2080
2dc6356a 2081 } else if (!strcmp(p, "-cs")) {
2082 EXPECTS_ARG;
2083 SECOND_PASS_ONLY;
3ea863a3 2084 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2085 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2dc6356a 2086
8e20db05 2087 } else if (!strcmp(p, "-geometry")) {
2088 int flags, x, y, w, h;
2089 EXPECTS_ARG;
2090 SECOND_PASS_ONLY;
2091
2092 flags = XParseGeometry(val, &x, &y, &w, &h);
2093 if (flags & WidthValue)
3ea863a3 2094 cfg->width = w;
8e20db05 2095 if (flags & HeightValue)
3ea863a3 2096 cfg->height = h;
8e20db05 2097
aca2a702 2098 if (flags & (XValue | YValue)) {
2099 inst->xpos = x;
2100 inst->ypos = y;
2101 inst->gotpos = TRUE;
2102 inst->gravity = ((flags & XNegative ? 1 : 0) |
2103 (flags & YNegative ? 2 : 0));
2104 }
8e20db05 2105
2106 } else if (!strcmp(p, "-sl")) {
2107 EXPECTS_ARG;
2108 SECOND_PASS_ONLY;
3ea863a3 2109 cfg->savelines = atoi(val);
8e20db05 2110
2111 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2112 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
b60ca991 2113 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
8e20db05 2114 GdkColor col;
2115
2116 EXPECTS_ARG;
2117 SECOND_PASS_ONLY;
2118 if (!gdk_color_parse(val, &col)) {
2119 err = 1;
a4e8ffb0 2120 fprintf(stderr, "%s: unable to parse colour \"%s\"\n",
2121 appname, val);
8e20db05 2122 } else {
2123 int index;
2124 index = (!strcmp(p, "-fg") ? 0 :
2125 !strcmp(p, "-bg") ? 2 :
2126 !strcmp(p, "-bfg") ? 1 :
2127 !strcmp(p, "-bbg") ? 3 :
b60ca991 2128 !strcmp(p, "-cfg") ? 4 :
8e20db05 2129 !strcmp(p, "-cbg") ? 5 : -1);
2130 assert(index != -1);
3ea863a3 2131 cfg->colours[index][0] = col.red / 256;
2132 cfg->colours[index][1] = col.green / 256;
2133 cfg->colours[index][2] = col.blue / 256;
8e20db05 2134 }
2135
1d009ae7 2136 } else if (use_pty_argv && !strcmp(p, "-e")) {
faec60ed 2137 /* This option swallows all further arguments. */
2138 if (!do_everything)
2139 break;
2140
6169c758 2141 if (--argc > 0) {
2142 int i;
3d88e64d 2143 pty_argv = snewn(argc+1, char *);
6169c758 2144 ++argv;
2145 for (i = 0; i < argc; i++)
2146 pty_argv[i] = argv[i];
2147 pty_argv[argc] = NULL;
2148 break; /* finished command-line processing */
2149 } else
a4e8ffb0 2150 err = 1, fprintf(stderr, "%s: -e expects an argument\n",
2151 appname);
faec60ed 2152
486c8271 2153 } else if (!strcmp(p, "-title")) {
faec60ed 2154 EXPECTS_ARG;
2155 SECOND_PASS_ONLY;
3ea863a3 2156 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2157 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
faec60ed 2158
2159 } else if (!strcmp(p, "-log")) {
2160 EXPECTS_ARG;
2161 SECOND_PASS_ONLY;
9a30e26b 2162 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2163 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
3ea863a3 2164 cfg->logtype = LGTYP_DEBUG;
faec60ed 2165
8e20db05 2166 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
faec60ed 2167 SECOND_PASS_ONLY;
3ea863a3 2168 cfg->stamp_utmp = 0;
faec60ed 2169
8e20db05 2170 } else if (!strcmp(p, "-ut")) {
faec60ed 2171 SECOND_PASS_ONLY;
3ea863a3 2172 cfg->stamp_utmp = 1;
faec60ed 2173
8e20db05 2174 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
faec60ed 2175 SECOND_PASS_ONLY;
3ea863a3 2176 cfg->login_shell = 0;
faec60ed 2177
8e20db05 2178 } else if (!strcmp(p, "-ls")) {
2179 SECOND_PASS_ONLY;
3ea863a3 2180 cfg->login_shell = 1;
8e20db05 2181
faec60ed 2182 } else if (!strcmp(p, "-nethack")) {
2183 SECOND_PASS_ONLY;
3ea863a3 2184 cfg->nethack_keypad = 1;
faec60ed 2185
8e20db05 2186 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2187 SECOND_PASS_ONLY;
3ea863a3 2188 cfg->scrollbar = 0;
8e20db05 2189
2190 } else if (!strcmp(p, "-sb")) {
faec60ed 2191 SECOND_PASS_ONLY;
3ea863a3 2192 cfg->scrollbar = 0;
faec60ed 2193
2194 } else if (!strcmp(p, "-name")) {
2195 EXPECTS_ARG;
2196 app_name = val;
0ac15bdc 2197
2198 } else if (!strcmp(p, "-xrm")) {
2199 EXPECTS_ARG;
2200 provide_xrm_string(val);
2201
96add444 2202 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2203 help(stdout);
2204 exit(0);
2205
46a3419b 2206 } else if(p[0] != '-' && (!do_everything ||
2207 process_nonoption_arg(p, cfg))) {
2208 /* do nothing */
2209
edc73959 2210 } else {
2211 err = 1;
a4e8ffb0 2212 fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p);
330f49be 2213 }
6169c758 2214 }
2215
faec60ed 2216 return err;
2217}
2218
0f33f9d1 2219static void block_signal(int sig, int block_it) {
2220 sigset_t ss;
2221
2222 sigemptyset(&ss);
2223 sigaddset(&ss, sig);
2224 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2225 perror("sigprocmask");
2226 exit(1);
2227 }
2228}
2229
facd762c 2230/*
2231 * This function retrieves the character set encoding of a font. It
2232 * returns the character set without the X11 hack (in case the user
2233 * asks to use the font's own encoding).
2234 */
2235static int set_font_info(struct gui_data *inst, int fontid)
2dc6356a 2236{
2237 GdkFont *font = inst->fonts[fontid];
2238 XFontStruct *xfs = GDK_FONT_XFONT(font);
2239 Display *disp = GDK_FONT_XDISPLAY(font);
2240 Atom charset_registry, charset_encoding;
2241 unsigned long registry_ret, encoding_ret;
facd762c 2242 int retval = CS_NONE;
2243
2dc6356a 2244 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2245 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2246 inst->fontinfo[fontid].charset = CS_NONE;
2247 inst->fontinfo[fontid].is_wide = 0;
2248 if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
2249 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2250 char *reg, *enc;
2251 reg = XGetAtomName(disp, (Atom)registry_ret);
2252 enc = XGetAtomName(disp, (Atom)encoding_ret);
2253 if (reg && enc) {
2254 char *encoding = dupcat(reg, "-", enc, NULL);
facd762c 2255 retval = inst->fontinfo[fontid].charset =
2256 charset_from_xenc(encoding);
2dc6356a 2257 /* FIXME: when libcharset supports wide encodings fix this. */
facd762c 2258 if (!strcasecmp(encoding, "iso10646-1")) {
2dc6356a 2259 inst->fontinfo[fontid].is_wide = 1;
facd762c 2260 retval = CS_UTF8;
2261 }
2dc6356a 2262
2263 /*
2264 * Hack for X line-drawing characters: if the primary
2265 * font is encoded as ISO-8859-anything, and has valid
2266 * glyphs in the first 32 char positions, it is assumed
2267 * that those glyphs are the VT100 line-drawing
2268 * character set.
2269 *
2270 * Actually, we'll hack even harder by only checking
2271 * position 0x19 (vertical line, VT100 linedrawing
2272 * `x'). Then we can check it easily by seeing if the
2273 * ascent and descent differ.
2274 */
2275 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2276 int lb, rb, wid, asc, desc;
2277 gchar text[2];
2278
2279 text[1] = '\0';
2280 text[0] = '\x12';
2281 gdk_string_extents(inst->fonts[fontid], text,
2282 &lb, &rb, &wid, &asc, &desc);
2283 if (asc != desc)
2284 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2285 }
2286
2dc6356a 2287 sfree(encoding);
2288 }
2289 }
facd762c 2290
2291 return retval;
2dc6356a 2292}
2293
74aca06d 2294int uxsel_input_add(int fd, int rwx) {
2295 int flags = 0;
2296 if (rwx & 1) flags |= GDK_INPUT_READ;
2297 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2298 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2299 return gdk_input_add(fd, flags, fd_input_func, NULL);
2300}
2301
2302void uxsel_input_remove(int id) {
2303 gdk_input_remove(id);
2304}
2305
56b9b9a7 2306void setup_fonts_ucs(struct gui_data *inst)
2307{
2308 int font_charset;
2309
2310 if (inst->fonts[0])
2311 gdk_font_unref(inst->fonts[0]);
2312 if (inst->fonts[1])
2313 gdk_font_unref(inst->fonts[1]);
2314 if (inst->fonts[2])
2315 gdk_font_unref(inst->fonts[2]);
2316 if (inst->fonts[3])
2317 gdk_font_unref(inst->fonts[3]);
2318
2319 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
2320 if (!inst->fonts[0]) {
2321 fprintf(stderr, "%s: unable to load font \"%s\"\n", appname,
2322 inst->cfg.font.name);
2323 exit(1);
2324 }
2325 font_charset = set_font_info(inst, 0);
2326 if (inst->cfg.boldfont.name[0]) {
2327 inst->fonts[1] = gdk_font_load(inst->cfg.boldfont.name);
2328 if (!inst->fonts[1]) {
2329 fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname,
2330 inst->cfg.boldfont.name);
2331 exit(1);
2332 }
2333 set_font_info(inst, 1);
2334 } else
2335 inst->fonts[1] = NULL;
2336 if (inst->cfg.widefont.name[0]) {
2337 inst->fonts[2] = gdk_font_load(inst->cfg.widefont.name);
2338 if (!inst->fonts[2]) {
2339 fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname,
2340 inst->cfg.widefont.name);
2341 exit(1);
2342 }
2343 set_font_info(inst, 2);
2344 } else
2345 inst->fonts[2] = NULL;
2346 if (inst->cfg.wideboldfont.name[0]) {
2347 inst->fonts[3] = gdk_font_load(inst->cfg.wideboldfont.name);
2348 if (!inst->fonts[3]) {
2349 fprintf(stderr, "%s: unable to load wide/bold font \"%s\"\n",
2350 appname, inst->cfg.wideboldfont.name);
2351 exit(1);
2352 }
2353 set_font_info(inst, 3);
2354 } else
2355 inst->fonts[3] = NULL;
2356
2357 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2358 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2359
2360 inst->direct_to_font = init_ucs(&inst->ucsdata,
2361 inst->cfg.line_codepage, font_charset);
2362}
2363
2364void set_geom_hints(struct gui_data *inst)
2365{
2366 GdkGeometry geom;
2367 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2368 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
2369 geom.max_width = geom.max_height = -1;
2370 geom.base_width = 2*inst->cfg.window_border;
2371 geom.base_height = 2*inst->cfg.window_border;
2372 geom.width_inc = inst->font_width;
2373 geom.height_inc = inst->font_height;
2374 geom.min_aspect = geom.max_aspect = 0;
2375 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2376 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2377 GDK_HINT_RESIZE_INC);
2378}
2379
47e4e735 2380void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2381{
2382 struct gui_data *inst = (struct gui_data *)data;
2383 term_clrsb(inst->term);
2384}
2385
2386void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2387{
2388 struct gui_data *inst = (struct gui_data *)data;
2389 term_pwron(inst->term);
2390 ldisc_send(inst->ldisc, NULL, 0, 0);
2391}
2392
d9e5c333 2393void copy_all_menuitem(GtkMenuItem *item, gpointer data)
2394{
2395 struct gui_data *inst = (struct gui_data *)data;
2396 term_copyall(inst->term);
2397}
2398
47e4e735 2399void special_menuitem(GtkMenuItem *item, gpointer data)
2400{
2401 struct gui_data *inst = (struct gui_data *)data;
2402 int code = (int)gtk_object_get_data(GTK_OBJECT(item), "user-data");
2403
2404 inst->back->special(inst->backhandle, code);
2405}
2406
2407void about_menuitem(GtkMenuItem *item, gpointer data)
2408{
7718480a 2409 struct gui_data *inst = (struct gui_data *)data;
2410 about_box(inst->window);
47e4e735 2411}
2412
8eed910d 2413void event_log_menuitem(GtkMenuItem *item, gpointer data)
2414{
2415 struct gui_data *inst = (struct gui_data *)data;
2416 showeventlog(inst->eventlogstuff, inst->window);
2417}
2418
56b9b9a7 2419void change_settings_menuitem(GtkMenuItem *item, gpointer data)
2420{
2421 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2422 static const int ww[] = {
2423 6, 7, 8, 9, 10, 11, 12, 13,
2424 14, 15, 16, 17, 18, 19, 20, 21,
2425 0, 1, 2, 3, 4, 5
2426 };
2427 struct gui_data *inst = (struct gui_data *)data;
2428 char *title = dupcat(appname, " Reconfiguration", NULL);
2429 Config cfg2, oldcfg;
2430 int i, need_size;
2431
2432 cfg2 = inst->cfg; /* structure copy */
2433
2434 if (do_config_box(title, &cfg2, 1)) {
2435
2436 oldcfg = inst->cfg; /* structure copy */
2437 inst->cfg = cfg2; /* structure copy */
2438
2439 /* Pass new config data to the logging module */
2440 log_reconfig(inst->logctx, &cfg2);
2441 /*
2442 * Flush the line discipline's edit buffer in the case
2443 * where local editing has just been disabled.
2444 */
2445 ldisc_send(inst->ldisc, NULL, 0, 0);
2446 /* Pass new config data to the terminal */
2447 term_reconfig(inst->term, &cfg2);
2448 /* Pass new config data to the back end */
2449 inst->back->reconfig(inst->backhandle, &cfg2);
2450
2451 /*
2452 * Just setting inst->cfg is sufficient to cause colour
2453 * setting changes to appear on the next ESC]R palette
2454 * reset. But we should also check whether any colour
2455 * settings have been changed, and revert the ones that
2456 * have to the new default, on the assumption that the user
2457 * is most likely to want an immediate update.
2458 */
2459 for (i = 0; i < NCOLOURS; i++) {
2460 if (oldcfg.colours[ww[i]][0] != cfg2.colours[ww[i]][0] ||
2461 oldcfg.colours[ww[i]][1] != cfg2.colours[ww[i]][1] ||
2462 oldcfg.colours[ww[i]][2] != cfg2.colours[ww[i]][2])
2463 real_palette_set(inst, i, cfg2.colours[ww[i]][0],
2464 cfg2.colours[ww[i]][1],
2465 cfg2.colours[ww[i]][2]);
2466 }
2467
2468 /*
2469 * If the scrollbar needs to be shown, hidden, or moved
2470 * from one end to the other of the window, do so now.
2471 */
2472 if (oldcfg.scrollbar != cfg2.scrollbar) {
2473 if (cfg2.scrollbar)
2474 gtk_widget_show(inst->sbar);
2475 else
2476 gtk_widget_hide(inst->sbar);
2477 }
2478 if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) {
2479 gtk_box_reorder_child(inst->hbox, inst->sbar,
2480 cfg2.scrollbar_on_left ? 0 : 1);
2481 }
2482
2483 /*
2484 * Change the window title, if required.
2485 */
2486 if (strcmp(oldcfg.wintitle, cfg2.wintitle))
2487 set_title(inst, cfg2.wintitle);
2488
2489 /*
2490 * Redo the whole tangled fonts and Unicode mess if
2491 * necessary.
2492 */
2493 if (strcmp(oldcfg.font.name, cfg2.font.name) ||
2494 strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
2495 strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
2496 strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
2497 strcmp(oldcfg.line_codepage, cfg2.line_codepage)) {
2498 setup_fonts_ucs(inst);
2499 need_size = 1;
2500 } else
2501 need_size = 0;
2502
2503 /*
2504 * Resize the window.
2505 */
2506 if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height ||
2507 oldcfg.window_border != cfg2.window_border || need_size) {
2508 set_geom_hints(inst);
2509 request_resize(inst, cfg2.width, cfg2.height);
2510 //term_size(inst->term, cfg2.height, cfg2.width, cfg2.savelines);
2511 // where TF is our configure event going?!
2512 }
2513
2514 term_invalidate(inst->term);
2515 }
2516 sfree(title);
2517}
2518
47e4e735 2519void update_specials_menu(void *frontend)
2520{
2521 Terminal *term = (Terminal *)frontend;
2522 struct gui_data *inst = (struct gui_data *)term->frontend;
2523
2524 const struct telnet_special *specials;
2525
2526 specials = inst->back->get_specials(inst->backhandle);
2527 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
2528 (GtkCallback)gtk_widget_destroy, NULL);
2529 if (specials) {
2530 int i;
2531 GtkWidget *menuitem;
2532 for (i = 0; specials[i].name; i++) {
2533 if (*specials[i].name) {
2534 menuitem = gtk_menu_item_new_with_label(specials[i].name);
2535 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2536 (gpointer)specials[i].code);
2537 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2538 GTK_SIGNAL_FUNC(special_menuitem), inst);
2539 } else
2540 menuitem = gtk_menu_item_new();
2541 gtk_container_add(GTK_CONTAINER(inst->specialsmenu), menuitem);
2542 gtk_widget_show(menuitem);
2543 }
2544 gtk_widget_show(inst->specialsitem1);
2545 gtk_widget_show(inst->specialsitem2);
2546 } else {
2547 gtk_widget_hide(inst->specialsitem1);
2548 gtk_widget_hide(inst->specialsitem2);
2549 }
2550}
2551
1d009ae7 2552int pt_main(int argc, char **argv)
faec60ed 2553{
1d009ae7 2554 extern Backend *select_backend(Config *cfg);
2555 extern int cfgbox(Config *cfg);
a8327734 2556 struct gui_data *inst;
faec60ed 2557
0f33f9d1 2558 /* defer any child exit handling until we're ready to deal with
2559 * it */
2560 block_signal(SIGCHLD, 1);
2561
faec60ed 2562 gtk_init(&argc, &argv);
2563
7428c509 2564 /*
a8327734 2565 * Create an instance structure and initialise to zeroes
7428c509 2566 */
3d88e64d 2567 inst = snew(struct gui_data);
7428c509 2568 memset(inst, 0, sizeof(*inst));
045f707b 2569 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
7428c509 2570
aca2a702 2571 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
9dc625f0 2572 exit(1); /* pre-defaults pass to get -class */
2573 do_defaults(NULL, &inst->cfg);
aca2a702 2574 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
9dc625f0 2575 exit(1); /* post-defaults, do everything */
2576
46a3419b 2577 cmdline_run_saved(&inst->cfg);
2578
2579 if (!*inst->cfg.host && !cfgbox(&inst->cfg))
1d009ae7 2580 exit(0); /* config box hit Cancel */
2581
8c161662 2582 if (!compound_text_atom)
2583 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2584 if (!utf8_string_atom)
2585 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
dd72dfa3 2586
56b9b9a7 2587 setup_fonts_ucs(inst);
1709795f 2588
12d200b1 2589 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2590
16891265 2591 /*
2592 * Set up the colour map.
2593 */
a8327734 2594 palette_reset(inst);
16891265 2595
56b9b9a7 2596 inst->width = inst->cfg.width;
2597 inst->height = inst->cfg.height;
2598
f7f27309 2599 inst->area = gtk_drawing_area_new();
2600 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3ea863a3 2601 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
2602 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
56b9b9a7 2603 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2604 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
6a5e84dd 2605 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
56b9b9a7 2606 /*
2607 * We always create the scrollbar; it remains invisible if
2608 * unwanted, so we can pop it up quickly if it suddenly becomes
2609 * desirable.
2610 */
2611 if (inst->cfg.scrollbar_on_left)
2612 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
88e6b9ca 2613 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
56b9b9a7 2614 if (!inst->cfg.scrollbar_on_left)
2615 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
6a5e84dd 2616
12d200b1 2617 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
f7f27309 2618
56b9b9a7 2619 set_geom_hints(inst);
f7f27309 2620
aca2a702 2621 gtk_widget_show(inst->area);
2622 if (inst->cfg.scrollbar)
2623 gtk_widget_show(inst->sbar);
56b9b9a7 2624 else
2625 gtk_widget_hide(inst->sbar);
aca2a702 2626 gtk_widget_show(GTK_WIDGET(inst->hbox));
2627
2628 if (inst->gotpos) {
2629 int x = inst->xpos, y = inst->ypos;
2630 GtkRequisition req;
2631 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
2632 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
2633 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
2634 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
2635 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
2636 }
2637
12d200b1 2638 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
f7f27309 2639 GTK_SIGNAL_FUNC(destroy), inst);
12d200b1 2640 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
f7f27309 2641 GTK_SIGNAL_FUNC(delete_window), inst);
12d200b1 2642 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
f7f27309 2643 GTK_SIGNAL_FUNC(key_event), inst);
c33c3d76 2644 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2645 GTK_SIGNAL_FUNC(key_event), inst);
12d200b1 2646 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
f7f27309 2647 GTK_SIGNAL_FUNC(focus_event), inst);
12d200b1 2648 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
f7f27309 2649 GTK_SIGNAL_FUNC(focus_event), inst);
2650 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2651 GTK_SIGNAL_FUNC(configure_area), inst);
2652 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2653 GTK_SIGNAL_FUNC(expose_area), inst);
e6346999 2654 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2655 GTK_SIGNAL_FUNC(button_event), inst);
2656 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2657 GTK_SIGNAL_FUNC(button_event), inst);
2658 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2659 GTK_SIGNAL_FUNC(motion_event), inst);
2660 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2661 GTK_SIGNAL_FUNC(selection_received), inst);
2662 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2663 GTK_SIGNAL_FUNC(selection_get), inst);
2664 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2665 GTK_SIGNAL_FUNC(selection_clear), inst);
3ea863a3 2666 if (inst->cfg.scrollbar)
330f49be 2667 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2668 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
f7f27309 2669 gtk_timeout_add(20, timer_func, inst);
2670 gtk_widget_add_events(GTK_WIDGET(inst->area),
e6346999 2671 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2672 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
57e636ca 2673 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
f7f27309 2674
12d200b1 2675 gtk_widget_show(inst->window);
f7f27309 2676
a8327734 2677 set_window_background(inst);
7428c509 2678
47e4e735 2679 /*
2680 * Set up the Ctrl+rightclick context menu.
2681 */
2682 {
2683 GtkWidget *menuitem;
2684 char *s;
8eed910d 2685 extern const int use_event_log;
47e4e735 2686
2687 inst->menu = gtk_menu_new();
2688
2689#define MKMENUITEM(title, func) do { \
2690 menuitem = title ? gtk_menu_item_new_with_label(title) : \
2691 gtk_menu_item_new(); \
2692 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
2693 gtk_widget_show(menuitem); \
2694 if (func != NULL) \
2695 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
2696 GTK_SIGNAL_FUNC(func), inst); \
2697} while (0)
56b9b9a7 2698 MKMENUITEM("Change Settings", change_settings_menuitem);
2699 MKMENUITEM(NULL, NULL);
8eed910d 2700 if (use_event_log)
2701 MKMENUITEM("Event Log", event_log_menuitem);
47e4e735 2702 MKMENUITEM("Special Commands", NULL);
2703 inst->specialsmenu = gtk_menu_new();
2704 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
2705 inst->specialsitem1 = menuitem;
2706 MKMENUITEM(NULL, NULL);
2707 inst->specialsitem2 = menuitem;
2708 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
2709 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
d9e5c333 2710 MKMENUITEM("Copy All", copy_all_menuitem);
47e4e735 2711 MKMENUITEM(NULL, NULL);
2712 s = dupcat("About ", appname, NULL);
2713 MKMENUITEM(s, about_menuitem);
2714 sfree(s);
2715#undef MKMENUITEM
2716 }
2717
a8327734 2718 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2719 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2720 inst->blankcursor = make_mouse_ptr(inst, -1);
2721 make_mouse_ptr(inst, -2); /* clean up cursor font */
57e636ca 2722 inst->currcursor = inst->textcursor;
a8327734 2723 show_mouseptr(inst, 1);
d64838e1 2724
8eed910d 2725 inst->eventlogstuff = eventlogstuff_new();
2726
21d2b241 2727 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3ea863a3 2728 inst->logctx = log_init(inst, &inst->cfg);
a8327734 2729 term_provide_logctx(inst->term, inst->logctx);
887035a5 2730
74aca06d 2731 uxsel_init();
2732
3f935d5b 2733 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
2734
1d009ae7 2735 inst->back = select_backend(&inst->cfg);
2736 {
3f935d5b 2737 char *realhost, *error;
2738
2739 error = inst->back->init((void *)inst->term, &inst->backhandle,
2740 &inst->cfg, inst->cfg.host, inst->cfg.port,
2741 &realhost, inst->cfg.tcp_nodelay);
2742
2743 if (error) {
2744 char *msg = dupprintf("Unable to open connection to %s:\n%s",
2745 inst->cfg.host, error);
2746 inst->exited = TRUE;
2747 fatal_message_box(inst->window, msg);
2748 sfree(msg);
2749 return 0;
2750 }
10705014 2751
2752 if (inst->cfg.wintitle[0])
2753 set_title(inst, inst->cfg.wintitle);
2754 else {
2755 char *title = make_default_wintitle(realhost);
2756 set_title(inst, title);
2757 sfree(title);
2758 }
1d009ae7 2759 }
a8327734 2760 inst->back->provide_logctx(inst->backhandle, inst->logctx);
47e4e735 2761 update_specials_menu(inst->term);
755a6d84 2762
5def7522 2763 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
78843a7c 2764
fe5634f6 2765 inst->ldisc =
3ea863a3 2766 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle, inst);
b9d7bcad 2767 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
755a6d84 2768
0f33f9d1 2769 /* now we're reday to deal with the child exit handler being
2770 * called */
2771 block_signal(SIGCHLD, 0);
74aca06d 2772
2773 inst->exited = FALSE;
2774
f7f27309 2775 gtk_main();
2776
2777 return 0;
2778}