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