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