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