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