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