Event Log for Unix PuTTY. Doesn't yet allow X selection of its
[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
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])
1185 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
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])
1242 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
3ea863a3 1243 i, inst->cfg.colours[i][0], inst->cfg.colours[i][1], inst->cfg.colours[i][2]);
26b8e7cc 1244 }
7428c509 1245
a8327734 1246 set_window_background(inst);
1709795f 1247}
1248
a8327734 1249void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
1709795f 1250{
a8327734 1251 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1252 if (inst->pasteout_data)
1253 sfree(inst->pasteout_data);
2dc6356a 1254 if (inst->pasteout_data_utf8)
1255 sfree(inst->pasteout_data_utf8);
1256
facd762c 1257 /*
1258 * Set up UTF-8 paste data. This only happens if we aren't in
1259 * direct-to-font mode using the D800 hack.
1260 */
085f4a68 1261 if (!inst->direct_to_font) {
2dc6356a 1262 wchar_t *tmp = data;
1263 int tmplen = len;
facd762c 1264
3d88e64d 1265 inst->pasteout_data_utf8 = snewn(len*6, char);
facd762c 1266 inst->pasteout_data_utf8_len = len*6;
2dc6356a 1267 inst->pasteout_data_utf8_len =
1268 charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8,
1269 inst->pasteout_data_utf8_len,
1270 CS_UTF8, NULL, NULL, 0);
085f4a68 1271 if (inst->pasteout_data_utf8_len == 0) {
1272 sfree(inst->pasteout_data_utf8);
1273 inst->pasteout_data_utf8 = NULL;
1274 } else {
1275 inst->pasteout_data_utf8 =
3d88e64d 1276 sresize(inst->pasteout_data_utf8,
1277 inst->pasteout_data_utf8_len, char);
085f4a68 1278 }
facd762c 1279 } else {
1280 inst->pasteout_data_utf8 = NULL;
1281 inst->pasteout_data_utf8_len = 0;
2dc6356a 1282 }
1283
3d88e64d 1284 inst->pasteout_data = snewn(len*6, char);
085f4a68 1285 inst->pasteout_data_len = len*6;
21d2b241 1286 inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0,
1287 data, len, inst->pasteout_data,
1288 inst->pasteout_data_len,
1289 NULL, NULL, NULL);
085f4a68 1290 if (inst->pasteout_data_len == 0) {
1291 sfree(inst->pasteout_data);
1292 inst->pasteout_data = NULL;
1293 } else {
1294 inst->pasteout_data =
3d88e64d 1295 sresize(inst->pasteout_data, inst->pasteout_data_len, char);
085f4a68 1296 }
e6346999 1297
1298 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
1299 GDK_CURRENT_TIME)) {
1300 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1301 GDK_SELECTION_TYPE_STRING, 1);
dd72dfa3 1302 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1303 inst->compound_text_atom, 1);
facd762c 1304 if (inst->pasteout_data_utf8)
1305 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
1306 inst->utf8_string_atom, 1);
e6346999 1307 }
1308}
1309
1310void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
1311 guint info, guint time_stamp, gpointer data)
1312{
a8327734 1313 struct gui_data *inst = (struct gui_data *)data;
2dc6356a 1314 if (seldata->target == inst->utf8_string_atom)
1315 gtk_selection_data_set(seldata, seldata->target, 8,
1316 inst->pasteout_data_utf8,
1317 inst->pasteout_data_utf8_len);
1318 else
1319 gtk_selection_data_set(seldata, seldata->target, 8,
1320 inst->pasteout_data, inst->pasteout_data_len);
e6346999 1321}
1322
1323gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
1324 gpointer data)
1325{
a8327734 1326 struct gui_data *inst = (struct gui_data *)data;
5def7522 1327 term_deselect(inst->term);
e6346999 1328 if (inst->pasteout_data)
1329 sfree(inst->pasteout_data);
2dc6356a 1330 if (inst->pasteout_data_utf8)
1331 sfree(inst->pasteout_data_utf8);
e6346999 1332 inst->pasteout_data = NULL;
1333 inst->pasteout_data_len = 0;
2dc6356a 1334 inst->pasteout_data_utf8 = NULL;
1335 inst->pasteout_data_utf8_len = 0;
e6346999 1336 return TRUE;
1337}
1338
a8327734 1339void request_paste(void *frontend)
e6346999 1340{
a8327734 1341 struct gui_data *inst = (struct gui_data *)frontend;
e6346999 1342 /*
1343 * In Unix, pasting is asynchronous: all we can do at the
1344 * moment is to call gtk_selection_convert(), and when the data
1345 * comes back _then_ we can call term_do_paste().
1346 */
2dc6356a 1347
085f4a68 1348 if (!inst->direct_to_font) {
facd762c 1349 /*
1350 * First we attempt to retrieve the selection as a UTF-8
1351 * string (which we will convert to the correct code page
1352 * before sending to the session, of course). If that
1353 * fails, selection_received() will be informed and will
1354 * fall back to an ordinary string.
1355 */
1356 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1357 inst->utf8_string_atom, GDK_CURRENT_TIME);
1358 } else {
1359 /*
1360 * If we're in direct-to-font mode, we disable UTF-8
1361 * pasting, and go straight to ordinary string data.
1362 */
1363 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1364 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1365 }
e6346999 1366}
1367
0f660c8f 1368gint idle_paste_func(gpointer data); /* forward ref */
1369
e6346999 1370void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
27651070 1371 guint time, gpointer data)
e6346999 1372{
a8327734 1373 struct gui_data *inst = (struct gui_data *)data;
1374
2dc6356a 1375 if (seldata->target == inst->utf8_string_atom && seldata->length <= 0) {
1376 /*
1377 * Failed to get a UTF-8 selection string. Try an ordinary
1378 * string.
1379 */
1380 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
1381 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
1382 return;
1383 }
1384
1385 /*
1386 * Any other failure should just go foom.
1387 */
e6346999 1388 if (seldata->length <= 0 ||
2dc6356a 1389 (seldata->type != GDK_SELECTION_TYPE_STRING &&
1390 seldata->type != inst->utf8_string_atom))
e6346999 1391 return; /* Nothing happens. */
1392
1393 if (inst->pastein_data)
1394 sfree(inst->pastein_data);
1395
3d88e64d 1396 inst->pastein_data = snewn(seldata->length, wchar_t);
e6346999 1397 inst->pastein_data_len = seldata->length;
2dc6356a 1398 inst->pastein_data_len =
1399 mb_to_wc((seldata->type == inst->utf8_string_atom ?
21d2b241 1400 CS_UTF8 : inst->ucsdata.line_codepage),
2dc6356a 1401 0, seldata->data, seldata->length,
1402 inst->pastein_data, inst->pastein_data_len);
e6346999 1403
5def7522 1404 term_do_paste(inst->term);
0f660c8f 1405
5def7522 1406 if (term_paste_pending(inst->term))
0f660c8f 1407 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
1408}
1409
1410gint idle_paste_func(gpointer data)
1411{
1412 struct gui_data *inst = (struct gui_data *)data;
1413
5def7522 1414 if (term_paste_pending(inst->term))
1415 term_paste(inst->term);
0f660c8f 1416 else
1417 gtk_idle_remove(inst->term_paste_idle_id);
1418
1419 return TRUE;
1709795f 1420}
1421
0f660c8f 1422
a8327734 1423void get_clip(void *frontend, wchar_t ** p, int *len)
1709795f 1424{
a8327734 1425 struct gui_data *inst = (struct gui_data *)frontend;
1426
1709795f 1427 if (p) {
e6346999 1428 *p = inst->pastein_data;
1429 *len = inst->pastein_data_len;
1709795f 1430 }
1431}
1432
a8327734 1433void set_title(void *frontend, char *title)
1709795f 1434{
a8327734 1435 struct gui_data *inst = (struct gui_data *)frontend;
12d200b1 1436 strncpy(inst->wintitle, title, lenof(inst->wintitle));
1437 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
1438 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1709795f 1439}
1440
a8327734 1441void set_icon(void *frontend, char *title)
1709795f 1442{
a8327734 1443 struct gui_data *inst = (struct gui_data *)frontend;
16891265 1444 strncpy(inst->icontitle, title, lenof(inst->icontitle));
1445 inst->icontitle[lenof(inst->icontitle)-1] = '\0';
1446 gdk_window_set_icon_name(inst->window->window, inst->icontitle);
1709795f 1447}
1448
a8327734 1449void set_sbar(void *frontend, int total, int start, int page)
1709795f 1450{
a8327734 1451 struct gui_data *inst = (struct gui_data *)frontend;
3ea863a3 1452 if (!inst->cfg.scrollbar)
330f49be 1453 return;
6a5e84dd 1454 inst->sbar_adjust->lower = 0;
1455 inst->sbar_adjust->upper = total;
1456 inst->sbar_adjust->value = start;
1457 inst->sbar_adjust->page_size = page;
1458 inst->sbar_adjust->step_increment = 1;
1459 inst->sbar_adjust->page_increment = page/2;
88e6b9ca 1460 inst->ignore_sbar = TRUE;
6a5e84dd 1461 gtk_adjustment_changed(inst->sbar_adjust);
88e6b9ca 1462 inst->ignore_sbar = FALSE;
6a5e84dd 1463}
1464
1465void scrollbar_moved(GtkAdjustment *adj, gpointer data)
1466{
a8327734 1467 struct gui_data *inst = (struct gui_data *)data;
1468
3ea863a3 1469 if (!inst->cfg.scrollbar)
330f49be 1470 return;
88e6b9ca 1471 if (!inst->ignore_sbar)
5def7522 1472 term_scroll(inst->term, 1, (int)adj->value);
1709795f 1473}
1474
a8327734 1475void sys_cursor(void *frontend, int x, int y)
1709795f 1476{
1477 /*
1478 * This is meaningless under X.
1479 */
1480}
1481
0ce89525 1482/*
1483 * This is still called when mode==BELL_VISUAL, even though the
1484 * visual bell is handled entirely within terminal.c, because we
1485 * may want to perform additional actions on any kind of bell (for
1486 * example, taskbar flashing in Windows).
1487 */
a8327734 1488void beep(void *frontend, int mode)
1709795f 1489{
0ce89525 1490 if (mode != BELL_VISUAL)
1491 gdk_beep();
1709795f 1492}
1493
2102eb8a 1494int char_width(Context ctx, int uc)
1709795f 1495{
1496 /*
1497 * Under X, any fixed-width font really _is_ fixed-width.
1498 * Double-width characters will be dealt with using a separate
1499 * font. For the moment we can simply return 1.
1500 */
1501 return 1;
1502}
1503
a8327734 1504Context get_ctx(void *frontend)
1709795f 1505{
a8327734 1506 struct gui_data *inst = (struct gui_data *)frontend;
1507 struct draw_ctx *dctx;
1508
d64838e1 1509 if (!inst->area->window)
1510 return NULL;
a8327734 1511
3d88e64d 1512 dctx = snew(struct draw_ctx);
a8327734 1513 dctx->inst = inst;
1514 dctx->gc = gdk_gc_new(inst->area->window);
1515 return dctx;
1709795f 1516}
1517
1518void free_ctx(Context ctx)
1519{
a8327734 1520 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1521 /* struct gui_data *inst = dctx->inst; */
1522 GdkGC *gc = dctx->gc;
1709795f 1523 gdk_gc_unref(gc);
a8327734 1524 sfree(dctx);
1709795f 1525}
1526
1527/*
1528 * Draw a line of text in the window, at given character
1529 * coordinates, in given attributes.
1530 *
1531 * We are allowed to fiddle with the contents of `text'.
1532 */
1a675941 1533void do_text_internal(Context ctx, int x, int y, char *text, int len,
1534 unsigned long attr, int lattr)
1709795f 1535{
a8327734 1536 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1537 struct gui_data *inst = dctx->inst;
1538 GdkGC *gc = dctx->gc;
1539
006238cb 1540 int nfg, nbg, t, fontid, shadow, rlen, widefactor;
d64838e1 1541
37ca32ed 1542 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1543 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
1544 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1545 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
d64838e1 1546 if (attr & ATTR_REVERSE) {
1547 t = nfg;
1548 nfg = nbg;
1549 nbg = t;
1550 }
3ea863a3 1551 if (inst->cfg.bold_colour && (attr & ATTR_BOLD))
37ca32ed 1552 nfg |= 1;
3ea863a3 1553 if (inst->cfg.bold_colour && (attr & ATTR_BLINK))
37ca32ed 1554 nbg |= 1;
d64838e1 1555 if (attr & TATTR_ACTCURS) {
1556 nfg = NCOLOURS-2;
1557 nbg = NCOLOURS-1;
1558 }
1559
5bf50ab2 1560 fontid = shadow = 0;
006238cb 1561
1562 if (attr & ATTR_WIDE) {
1563 widefactor = 2;
1564 fontid |= 2;
1565 } else {
1566 widefactor = 1;
1567 }
1568
3ea863a3 1569 if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) {
006238cb 1570 if (inst->fonts[fontid | 1])
1571 fontid |= 1;
5bf50ab2 1572 else
1573 shadow = 1;
1574 }
1575
f34df346 1576 if (lattr != LATTR_NORM) {
f34df346 1577 x *= 2;
5def7522 1578 if (x >= inst->term->cols)
614bb468 1579 return;
006238cb 1580 if (x + len*2*widefactor > inst->term->cols)
1581 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
623f81b7 1582 rlen = len * 2;
1583 } else
1584 rlen = len;
1585
1586 {
1587 GdkRectangle r;
1588
3ea863a3 1589 r.x = x*inst->font_width+inst->cfg.window_border;
1590 r.y = y*inst->font_height+inst->cfg.window_border;
006238cb 1591 r.width = rlen*widefactor*inst->font_width;
623f81b7 1592 r.height = inst->font_height;
1593 gdk_gc_set_clip_rectangle(gc, &r);
f34df346 1594 }
1595
d64838e1 1596 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
83616aab 1597 gdk_draw_rectangle(inst->pixmap, gc, 1,
3ea863a3 1598 x*inst->font_width+inst->cfg.window_border,
1599 y*inst->font_height+inst->cfg.window_border,
006238cb 1600 rlen*widefactor*inst->font_width, inst->font_height);
6a5e84dd 1601
d64838e1 1602 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
2dc6356a 1603 {
1604 GdkWChar *gwcs;
1605 gchar *gcs;
1606 wchar_t *wcs;
1607 int i;
1608
3d88e64d 1609 wcs = snewn(len+1, wchar_t);
2dc6356a 1610 for (i = 0; i < len; i++) {
1611 wcs[i] = (wchar_t) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
1612 }
1613
006238cb 1614 if (inst->fonts[fontid] == NULL) {
1615 /*
1616 * The font for this contingency does not exist.
1617 * Typically this means we've been given ATTR_WIDE
1618 * character and have no wide font. So we display
1619 * nothing at all; such is life.
1620 */
1621 } else if (inst->fontinfo[fontid].is_wide) {
aa153a7f 1622 /*
1623 * At least one version of gdk_draw_text_wc() has a
1624 * weird bug whereby it reads `len' elements of the
1625 * input string, but only draws `len/2'. Hence I'm
1626 * going to make its input array twice as long as it
1627 * theoretically needs to be, and pass in twice the
1628 * actual number of characters. If a fixed gdk actually
1629 * takes the doubled length seriously, then (a) the
1630 * array will stand scrutiny up to the full length, (b)
1631 * the spare elements of the array are full of zeroes
1632 * which will probably be an empty glyph in the font,
1633 * and (c) the clip rectangle should prevent it causing
1634 * trouble anyway.
1635 */
3d88e64d 1636 gwcs = snewn(len*2+1, GdkWChar);
aa153a7f 1637 memset(gwcs, 0, sizeof(GdkWChar) * (len*2+1));
2dc6356a 1638 /*
1639 * FIXME: when we have a wide-char equivalent of
1640 * from_unicode, use it instead of this.
1641 */
1642 for (i = 0; i <= len; i++)
1643 gwcs[i] = wcs[i];
1644 gdk_draw_text_wc(inst->pixmap, inst->fonts[fontid], gc,
3ea863a3 1645 x*inst->font_width+inst->cfg.window_border,
1646 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2dc6356a 1647 gwcs, len*2);
1648 sfree(gwcs);
1649 } else {
3d88e64d 1650 gcs = snewn(len+1, gchar);
facd762c 1651 wc_to_mb(inst->fontinfo[fontid].charset, 0,
21d2b241 1652 wcs, len, gcs, len, ".", NULL, NULL);
2dc6356a 1653 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
3ea863a3 1654 x*inst->font_width+inst->cfg.window_border,
1655 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
2dc6356a 1656 gcs, len);
1657 sfree(gcs);
1658 }
1659 sfree(wcs);
1660 }
d64838e1 1661
5bf50ab2 1662 if (shadow) {
1663 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
3ea863a3 1664 x*inst->font_width+inst->cfg.window_border + inst->cfg.shadowboldoffset,
1665 y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent,
5bf50ab2 1666 text, len);
1667 }
1668
d64838e1 1669 if (attr & ATTR_UNDER) {
1670 int uheight = inst->fonts[0]->ascent + 1;
83616aab 1671 if (uheight >= inst->font_height)
1672 uheight = inst->font_height - 1;
3ea863a3 1673 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border,
1674 y*inst->font_height + uheight + inst->cfg.window_border,
1675 (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border,
1676 y*inst->font_height + uheight + inst->cfg.window_border);
d64838e1 1677 }
1678
f34df346 1679 if (lattr != LATTR_NORM) {
1680 /*
1681 * I can't find any plausible StretchBlt equivalent in the
1682 * X server, so I'm going to do this the slow and painful
1683 * way. This will involve repeated calls to
1684 * gdk_draw_pixmap() to stretch the text horizontally. It's
1685 * O(N^2) in time and O(N) in network bandwidth, but you
1686 * try thinking of a better way. :-(
1687 */
1688 int i;
006238cb 1689 for (i = 0; i < len * widefactor * inst->font_width; i++) {
f34df346 1690 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 1691 x*inst->font_width+inst->cfg.window_border + 2*i,
1692 y*inst->font_height+inst->cfg.window_border,
1693 x*inst->font_width+inst->cfg.window_border + 2*i+1,
1694 y*inst->font_height+inst->cfg.window_border,
f34df346 1695 len * inst->font_width - i, inst->font_height);
1696 }
1697 len *= 2;
1698 if (lattr != LATTR_WIDE) {
1699 int dt, db;
1700 /* Now stretch vertically, in the same way. */
1701 if (lattr == LATTR_BOT)
1702 dt = 0, db = 1;
1703 else
1704 dt = 1, db = 0;
1705 for (i = 0; i < inst->font_height; i+=2) {
1706 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
3ea863a3 1707 x*inst->font_width+inst->cfg.window_border,
1708 y*inst->font_height+inst->cfg.window_border+dt*i+db,
1709 x*widefactor*inst->font_width+inst->cfg.window_border,
1710 y*inst->font_height+inst->cfg.window_border+dt*(i+1),
f34df346 1711 len * inst->font_width, inst->font_height-i-1);
1712 }
1713 }
1a675941 1714 }
1715}
1716
1717void do_text(Context ctx, int x, int y, char *text, int len,
1718 unsigned long attr, int lattr)
1719{
a8327734 1720 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1721 struct gui_data *inst = dctx->inst;
1722 GdkGC *gc = dctx->gc;
006238cb 1723 int widefactor;
1a675941 1724
1725 do_text_internal(ctx, x, y, text, len, attr, lattr);
1726
006238cb 1727 if (attr & ATTR_WIDE) {
1728 widefactor = 2;
1729 } else {
1730 widefactor = 1;
1731 }
1732
1a675941 1733 if (lattr != LATTR_NORM) {
1734 x *= 2;
5def7522 1735 if (x >= inst->term->cols)
1a675941 1736 return;
006238cb 1737 if (x + len*2*widefactor > inst->term->cols)
1738 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
f34df346 1739 len *= 2;
1740 }
1741
d64838e1 1742 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 1743 x*inst->font_width+inst->cfg.window_border,
1744 y*inst->font_height+inst->cfg.window_border,
1745 x*inst->font_width+inst->cfg.window_border,
1746 y*inst->font_height+inst->cfg.window_border,
006238cb 1747 len*widefactor*inst->font_width, inst->font_height);
1709795f 1748}
1749
1750void do_cursor(Context ctx, int x, int y, char *text, int len,
1751 unsigned long attr, int lattr)
1752{
a8327734 1753 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1754 struct gui_data *inst = dctx->inst;
1755 GdkGC *gc = dctx->gc;
1756
006238cb 1757 int passive, widefactor;
d64838e1 1758
1709795f 1759 if (attr & TATTR_PASCURS) {
1760 attr &= ~TATTR_PASCURS;
d64838e1 1761 passive = 1;
1762 } else
1763 passive = 0;
3ea863a3 1764 if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) {
1a675941 1765 attr &= ~TATTR_ACTCURS;
1766 }
1767 do_text_internal(ctx, x, y, text, len, attr, lattr);
1768
006238cb 1769 if (attr & ATTR_WIDE) {
1770 widefactor = 2;
1771 } else {
1772 widefactor = 1;
1773 }
1774
1a675941 1775 if (lattr != LATTR_NORM) {
1776 x *= 2;
5def7522 1777 if (x >= inst->term->cols)
1a675941 1778 return;
006238cb 1779 if (x + len*2*widefactor > inst->term->cols)
1780 len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */
1a675941 1781 len *= 2;
1782 }
1783
3ea863a3 1784 if (inst->cfg.cursor_type == 0) {
1a675941 1785 /*
1786 * An active block cursor will already have been done by
1787 * the above do_text call, so we only need to do anything
1788 * if it's passive.
1789 */
1790 if (passive) {
1791 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1792 gdk_draw_rectangle(inst->pixmap, gc, 0,
3ea863a3 1793 x*inst->font_width+inst->cfg.window_border,
1794 y*inst->font_height+inst->cfg.window_border,
1a675941 1795 len*inst->font_width-1, inst->font_height-1);
1796 }
1797 } else {
1798 int uheight;
1799 int startx, starty, dx, dy, length, i;
1800
1801 int char_width;
1802
1803 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1804 char_width = 2*inst->font_width;
1805 else
1806 char_width = inst->font_width;
1807
3ea863a3 1808 if (inst->cfg.cursor_type == 1) {
1a675941 1809 uheight = inst->fonts[0]->ascent + 1;
1810 if (uheight >= inst->font_height)
1811 uheight = inst->font_height - 1;
1812
3ea863a3 1813 startx = x * inst->font_width + inst->cfg.window_border;
1814 starty = y * inst->font_height + inst->cfg.window_border + uheight;
1a675941 1815 dx = 1;
1816 dy = 0;
1817 length = len * char_width;
1818 } else {
1819 int xadjust = 0;
1820 if (attr & TATTR_RIGHTCURS)
1821 xadjust = char_width - 1;
3ea863a3 1822 startx = x * inst->font_width + inst->cfg.window_border + xadjust;
1823 starty = y * inst->font_height + inst->cfg.window_border;
1a675941 1824 dx = 0;
1825 dy = 1;
1826 length = inst->font_height;
1827 }
1828
d64838e1 1829 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1a675941 1830 if (passive) {
1831 for (i = 0; i < length; i++) {
1832 if (i % 2 == 0) {
1833 gdk_draw_point(inst->pixmap, gc, startx, starty);
1834 }
1835 startx += dx;
1836 starty += dy;
1837 }
1838 } else {
1839 gdk_draw_line(inst->pixmap, gc, startx, starty,
1840 startx + (length-1) * dx, starty + (length-1) * dy);
1841 }
d64838e1 1842 }
1a675941 1843
1844 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
3ea863a3 1845 x*inst->font_width+inst->cfg.window_border,
1846 y*inst->font_height+inst->cfg.window_border,
1847 x*inst->font_width+inst->cfg.window_border,
1848 y*inst->font_height+inst->cfg.window_border,
006238cb 1849 len*widefactor*inst->font_width, inst->font_height);
1709795f 1850}
1851
a8327734 1852GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
7798fa56 1853{
1854 /*
1855 * Truly hideous hack: GTK doesn't allow us to set the mouse
1856 * cursor foreground and background colours unless we've _also_
1857 * created our own cursor from bitmaps. Therefore, I need to
1858 * load the `cursor' font and draw glyphs from it on to
1859 * pixmaps, in order to construct my cursors with the fg and bg
1860 * I want. This is a gross hack, but it's more self-contained
1861 * than linking in Xlib to find the X window handle to
1862 * inst->area and calling XRecolorCursor, and it's more
1863 * futureproof than hard-coding the shapes as bitmap arrays.
1864 */
1865 static GdkFont *cursor_font = NULL;
1866 GdkPixmap *source, *mask;
1867 GdkGC *gc;
1868 GdkColor cfg = { 0, 65535, 65535, 65535 };
1869 GdkColor cbg = { 0, 0, 0, 0 };
1870 GdkColor dfg = { 1, 65535, 65535, 65535 };
1871 GdkColor dbg = { 0, 0, 0, 0 };
1872 GdkCursor *ret;
1873 gchar text[2];
1874 gint lb, rb, wid, asc, desc, w, h, x, y;
1875
57e636ca 1876 if (cursor_val == -2) {
7798fa56 1877 gdk_font_unref(cursor_font);
1878 return NULL;
1879 }
1880
57e636ca 1881 if (cursor_val >= 0 && !cursor_font)
7798fa56 1882 cursor_font = gdk_font_load("cursor");
1883
1884 /*
1885 * Get the text extent of the cursor in question. We use the
1886 * mask character for this, because it's typically slightly
1887 * bigger than the main character.
1888 */
57e636ca 1889 if (cursor_val >= 0) {
1890 text[1] = '\0';
1891 text[0] = (char)cursor_val + 1;
1892 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1893 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1894 } else {
1895 w = h = 1;
1896 x = y = 0;
1897 }
7798fa56 1898
1899 source = gdk_pixmap_new(NULL, w, h, 1);
1900 mask = gdk_pixmap_new(NULL, w, h, 1);
1901
1902 /*
1903 * Draw the mask character on the mask pixmap.
1904 */
7798fa56 1905 gc = gdk_gc_new(mask);
1906 gdk_gc_set_foreground(gc, &dbg);
1907 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
57e636ca 1908 if (cursor_val >= 0) {
1909 text[1] = '\0';
1910 text[0] = (char)cursor_val + 1;
1911 gdk_gc_set_foreground(gc, &dfg);
1912 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1913 }
7798fa56 1914 gdk_gc_unref(gc);
1915
1916 /*
1917 * Draw the main character on the source pixmap.
1918 */
7798fa56 1919 gc = gdk_gc_new(source);
1920 gdk_gc_set_foreground(gc, &dbg);
1921 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
57e636ca 1922 if (cursor_val >= 0) {
1923 text[1] = '\0';
1924 text[0] = (char)cursor_val;
1925 gdk_gc_set_foreground(gc, &dfg);
1926 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1927 }
7798fa56 1928 gdk_gc_unref(gc);
1929
1930 /*
1931 * Create the cursor.
1932 */
1933 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1934
1935 /*
1936 * Clean up.
1937 */
1938 gdk_pixmap_unref(source);
1939 gdk_pixmap_unref(mask);
1940
1941 return ret;
1942}
1943
1709795f 1944void modalfatalbox(char *p, ...)
1945{
1946 va_list ap;
1947 fprintf(stderr, "FATAL ERROR: ");
1948 va_start(ap, p);
1949 vfprintf(stderr, p, ap);
1950 va_end(ap);
1951 fputc('\n', stderr);
1952 exit(1);
f7f27309 1953}
1954
46a3419b 1955void cmdline_error(char *p, ...)
1956{
1957 va_list ap;
1958 fprintf(stderr, "plink: ");
1959 va_start(ap, p);
1960 vfprintf(stderr, p, ap);
1961 va_end(ap);
1962 fputc('\n', stderr);
1963 exit(1);
1964}
1965
a8327734 1966char *get_x_display(void *frontend)
755a6d84 1967{
1968 return gdk_get_display();
1969}
1970
7af753e6 1971long get_windowid(void *frontend)
1972{
1973 Terminal *term = (Terminal *)frontend;
1974 struct gui_data *inst = (struct gui_data *)(term->frontend);
1975 return (long)GDK_WINDOW_XWINDOW(inst->area->window);
1976}
1977
96add444 1978static void help(FILE *fp) {
1979 if(fprintf(fp,
1980"pterm option summary:\n"
1981"\n"
1982" --display DISPLAY Specify X display to use (note '--')\n"
1983" -name PREFIX Prefix when looking up resources (default: pterm)\n"
1984" -fn FONT Normal text font\n"
1985" -fb FONT Bold text font\n"
aca2a702 1986" -geometry GEOMETRY Position and size of window (size in characters)\n"
96add444 1987" -sl LINES Number of lines of scrollback\n"
1988" -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1989" -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1990" -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1991" -T TITLE Window title\n"
1992" -ut, +ut Do(default) or do not update utmp\n"
1993" -ls, +ls Do(default) or do not make shell a login shell\n"
1994" -sb, +sb Do(default) or do not display a scrollbar\n"
1995" -log PATH Log all output to a file\n"
1996" -nethack Map numeric keypad to hjklyubn direction keys\n"
1997" -xrm RESOURCE-STRING Set an X resource\n"
1998" -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1999 ) < 0 || fflush(fp) < 0) {
2000 perror("output error");
2001 exit(1);
2002 }
2003}
2004
aca2a702 2005int do_cmdline(int argc, char **argv, int do_everything,
2006 struct gui_data *inst, Config *cfg)
f7f27309 2007{
6169c758 2008 int err = 0;
faec60ed 2009 extern char **pty_argv; /* declared in pty.c */
1d009ae7 2010 extern int use_pty_argv;
f7f27309 2011
faec60ed 2012 /*
b89ee4f3 2013 * Macros to make argument handling easier. Note that because
2014 * they need to call `continue', they cannot be contained in
2015 * the usual do {...} while (0) wrapper to make them
2016 * syntactically single statements; hence it is not legal to
2017 * use one of these macros as an unbraced statement between
2018 * `if' and `else'.
faec60ed 2019 */
b89ee4f3 2020#define EXPECTS_ARG { \
faec60ed 2021 if (--argc <= 0) { \
2022 err = 1; \
2023 fprintf(stderr, "pterm: %s expects an argument\n", p); \
b89ee4f3 2024 continue; \
faec60ed 2025 } else \
2026 val = *++argv; \
b89ee4f3 2027}
2028#define SECOND_PASS_ONLY { if (!do_everything) continue; }
faec60ed 2029
2030 char *val;
6169c758 2031 while (--argc > 0) {
2032 char *p = *++argv;
46a3419b 2033 int ret;
2034
2035 ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
2036 do_everything ? 1 : -1, cfg);
2037
2038 if (ret == -2) {
2039 cmdline_error("option \"%s\" requires an argument", p);
2040 } else if (ret == 2) {
2041 --argc, ++argv; /* skip next argument */
2042 continue;
2043 } else if (ret == 1) {
2044 continue;
2045 }
2046
8e20db05 2047 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
faec60ed 2048 EXPECTS_ARG;
2049 SECOND_PASS_ONLY;
9a30e26b 2050 strncpy(cfg->font.name, val, sizeof(cfg->font.name));
2051 cfg->font.name[sizeof(cfg->font.name)-1] = '\0';
faec60ed 2052
2053 } else if (!strcmp(p, "-fb")) {
2054 EXPECTS_ARG;
2055 SECOND_PASS_ONLY;
9a30e26b 2056 strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name));
2057 cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0';
faec60ed 2058
006238cb 2059 } else if (!strcmp(p, "-fw")) {
2060 EXPECTS_ARG;
2061 SECOND_PASS_ONLY;
9a30e26b 2062 strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name));
2063 cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0';
006238cb 2064
2065 } else if (!strcmp(p, "-fwb")) {
2066 EXPECTS_ARG;
2067 SECOND_PASS_ONLY;
9a30e26b 2068 strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name));
2069 cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0';
006238cb 2070
2dc6356a 2071 } else if (!strcmp(p, "-cs")) {
2072 EXPECTS_ARG;
2073 SECOND_PASS_ONLY;
3ea863a3 2074 strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage));
2075 cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0';
2dc6356a 2076
8e20db05 2077 } else if (!strcmp(p, "-geometry")) {
2078 int flags, x, y, w, h;
2079 EXPECTS_ARG;
2080 SECOND_PASS_ONLY;
2081
2082 flags = XParseGeometry(val, &x, &y, &w, &h);
2083 if (flags & WidthValue)
3ea863a3 2084 cfg->width = w;
8e20db05 2085 if (flags & HeightValue)
3ea863a3 2086 cfg->height = h;
8e20db05 2087
aca2a702 2088 if (flags & (XValue | YValue)) {
2089 inst->xpos = x;
2090 inst->ypos = y;
2091 inst->gotpos = TRUE;
2092 inst->gravity = ((flags & XNegative ? 1 : 0) |
2093 (flags & YNegative ? 2 : 0));
2094 }
8e20db05 2095
2096 } else if (!strcmp(p, "-sl")) {
2097 EXPECTS_ARG;
2098 SECOND_PASS_ONLY;
3ea863a3 2099 cfg->savelines = atoi(val);
8e20db05 2100
2101 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
2102 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
b60ca991 2103 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
8e20db05 2104 GdkColor col;
2105
2106 EXPECTS_ARG;
2107 SECOND_PASS_ONLY;
2108 if (!gdk_color_parse(val, &col)) {
2109 err = 1;
2110 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
2111 } else {
2112 int index;
2113 index = (!strcmp(p, "-fg") ? 0 :
2114 !strcmp(p, "-bg") ? 2 :
2115 !strcmp(p, "-bfg") ? 1 :
2116 !strcmp(p, "-bbg") ? 3 :
b60ca991 2117 !strcmp(p, "-cfg") ? 4 :
8e20db05 2118 !strcmp(p, "-cbg") ? 5 : -1);
2119 assert(index != -1);
3ea863a3 2120 cfg->colours[index][0] = col.red / 256;
2121 cfg->colours[index][1] = col.green / 256;
2122 cfg->colours[index][2] = col.blue / 256;
8e20db05 2123 }
2124
1d009ae7 2125 } else if (use_pty_argv && !strcmp(p, "-e")) {
faec60ed 2126 /* This option swallows all further arguments. */
2127 if (!do_everything)
2128 break;
2129
6169c758 2130 if (--argc > 0) {
2131 int i;
3d88e64d 2132 pty_argv = snewn(argc+1, char *);
6169c758 2133 ++argv;
2134 for (i = 0; i < argc; i++)
2135 pty_argv[i] = argv[i];
2136 pty_argv[argc] = NULL;
2137 break; /* finished command-line processing */
2138 } else
2139 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
faec60ed 2140
486c8271 2141 } else if (!strcmp(p, "-title")) {
faec60ed 2142 EXPECTS_ARG;
2143 SECOND_PASS_ONLY;
3ea863a3 2144 strncpy(cfg->wintitle, val, sizeof(cfg->wintitle));
2145 cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0';
faec60ed 2146
2147 } else if (!strcmp(p, "-log")) {
2148 EXPECTS_ARG;
2149 SECOND_PASS_ONLY;
9a30e26b 2150 strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path));
2151 cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0';
3ea863a3 2152 cfg->logtype = LGTYP_DEBUG;
faec60ed 2153
8e20db05 2154 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
faec60ed 2155 SECOND_PASS_ONLY;
3ea863a3 2156 cfg->stamp_utmp = 0;
faec60ed 2157
8e20db05 2158 } else if (!strcmp(p, "-ut")) {
faec60ed 2159 SECOND_PASS_ONLY;
3ea863a3 2160 cfg->stamp_utmp = 1;
faec60ed 2161
8e20db05 2162 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
faec60ed 2163 SECOND_PASS_ONLY;
3ea863a3 2164 cfg->login_shell = 0;
faec60ed 2165
8e20db05 2166 } else if (!strcmp(p, "-ls")) {
2167 SECOND_PASS_ONLY;
3ea863a3 2168 cfg->login_shell = 1;
8e20db05 2169
faec60ed 2170 } else if (!strcmp(p, "-nethack")) {
2171 SECOND_PASS_ONLY;
3ea863a3 2172 cfg->nethack_keypad = 1;
faec60ed 2173
8e20db05 2174 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
2175 SECOND_PASS_ONLY;
3ea863a3 2176 cfg->scrollbar = 0;
8e20db05 2177
2178 } else if (!strcmp(p, "-sb")) {
faec60ed 2179 SECOND_PASS_ONLY;
3ea863a3 2180 cfg->scrollbar = 0;
faec60ed 2181
2182 } else if (!strcmp(p, "-name")) {
2183 EXPECTS_ARG;
2184 app_name = val;
0ac15bdc 2185
2186 } else if (!strcmp(p, "-xrm")) {
2187 EXPECTS_ARG;
2188 provide_xrm_string(val);
2189
96add444 2190 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
2191 help(stdout);
2192 exit(0);
2193
46a3419b 2194 } else if(p[0] != '-' && (!do_everything ||
2195 process_nonoption_arg(p, cfg))) {
2196 /* do nothing */
2197
edc73959 2198 } else {
2199 err = 1;
2200 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
330f49be 2201 }
6169c758 2202 }
2203
faec60ed 2204 return err;
2205}
2206
0f33f9d1 2207static void block_signal(int sig, int block_it) {
2208 sigset_t ss;
2209
2210 sigemptyset(&ss);
2211 sigaddset(&ss, sig);
2212 if(sigprocmask(block_it ? SIG_BLOCK : SIG_UNBLOCK, &ss, 0) < 0) {
2213 perror("sigprocmask");
2214 exit(1);
2215 }
2216}
2217
facd762c 2218/*
2219 * This function retrieves the character set encoding of a font. It
2220 * returns the character set without the X11 hack (in case the user
2221 * asks to use the font's own encoding).
2222 */
2223static int set_font_info(struct gui_data *inst, int fontid)
2dc6356a 2224{
2225 GdkFont *font = inst->fonts[fontid];
2226 XFontStruct *xfs = GDK_FONT_XFONT(font);
2227 Display *disp = GDK_FONT_XDISPLAY(font);
2228 Atom charset_registry, charset_encoding;
2229 unsigned long registry_ret, encoding_ret;
facd762c 2230 int retval = CS_NONE;
2231
2dc6356a 2232 charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
2233 charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
2234 inst->fontinfo[fontid].charset = CS_NONE;
2235 inst->fontinfo[fontid].is_wide = 0;
2236 if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
2237 XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
2238 char *reg, *enc;
2239 reg = XGetAtomName(disp, (Atom)registry_ret);
2240 enc = XGetAtomName(disp, (Atom)encoding_ret);
2241 if (reg && enc) {
2242 char *encoding = dupcat(reg, "-", enc, NULL);
facd762c 2243 retval = inst->fontinfo[fontid].charset =
2244 charset_from_xenc(encoding);
2dc6356a 2245 /* FIXME: when libcharset supports wide encodings fix this. */
facd762c 2246 if (!strcasecmp(encoding, "iso10646-1")) {
2dc6356a 2247 inst->fontinfo[fontid].is_wide = 1;
facd762c 2248 retval = CS_UTF8;
2249 }
2dc6356a 2250
2251 /*
2252 * Hack for X line-drawing characters: if the primary
2253 * font is encoded as ISO-8859-anything, and has valid
2254 * glyphs in the first 32 char positions, it is assumed
2255 * that those glyphs are the VT100 line-drawing
2256 * character set.
2257 *
2258 * Actually, we'll hack even harder by only checking
2259 * position 0x19 (vertical line, VT100 linedrawing
2260 * `x'). Then we can check it easily by seeing if the
2261 * ascent and descent differ.
2262 */
2263 if (inst->fontinfo[fontid].charset == CS_ISO8859_1) {
2264 int lb, rb, wid, asc, desc;
2265 gchar text[2];
2266
2267 text[1] = '\0';
2268 text[0] = '\x12';
2269 gdk_string_extents(inst->fonts[fontid], text,
2270 &lb, &rb, &wid, &asc, &desc);
2271 if (asc != desc)
2272 inst->fontinfo[fontid].charset = CS_ISO8859_1_X11;
2273 }
2274
2dc6356a 2275 sfree(encoding);
2276 }
2277 }
facd762c 2278
2279 return retval;
2dc6356a 2280}
2281
74aca06d 2282int uxsel_input_add(int fd, int rwx) {
2283 int flags = 0;
2284 if (rwx & 1) flags |= GDK_INPUT_READ;
2285 if (rwx & 2) flags |= GDK_INPUT_WRITE;
2286 if (rwx & 4) flags |= GDK_INPUT_EXCEPTION;
2287 return gdk_input_add(fd, flags, fd_input_func, NULL);
2288}
2289
2290void uxsel_input_remove(int id) {
2291 gdk_input_remove(id);
2292}
2293
47e4e735 2294void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data)
2295{
2296 struct gui_data *inst = (struct gui_data *)data;
2297 term_clrsb(inst->term);
2298}
2299
2300void reset_terminal_menuitem(GtkMenuItem *item, gpointer data)
2301{
2302 struct gui_data *inst = (struct gui_data *)data;
2303 term_pwron(inst->term);
2304 ldisc_send(inst->ldisc, NULL, 0, 0);
2305}
2306
2307void special_menuitem(GtkMenuItem *item, gpointer data)
2308{
2309 struct gui_data *inst = (struct gui_data *)data;
2310 int code = (int)gtk_object_get_data(GTK_OBJECT(item), "user-data");
2311
2312 inst->back->special(inst->backhandle, code);
2313}
2314
2315void about_menuitem(GtkMenuItem *item, gpointer data)
2316{
2317 /* struct gui_data *inst = (struct gui_data *)data; */
2318 about_box();
2319}
2320
8eed910d 2321void event_log_menuitem(GtkMenuItem *item, gpointer data)
2322{
2323 struct gui_data *inst = (struct gui_data *)data;
2324 showeventlog(inst->eventlogstuff, inst->window);
2325}
2326
47e4e735 2327void update_specials_menu(void *frontend)
2328{
2329 Terminal *term = (Terminal *)frontend;
2330 struct gui_data *inst = (struct gui_data *)term->frontend;
2331
2332 const struct telnet_special *specials;
2333
2334 specials = inst->back->get_specials(inst->backhandle);
2335 gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu),
2336 (GtkCallback)gtk_widget_destroy, NULL);
2337 if (specials) {
2338 int i;
2339 GtkWidget *menuitem;
2340 for (i = 0; specials[i].name; i++) {
2341 if (*specials[i].name) {
2342 menuitem = gtk_menu_item_new_with_label(specials[i].name);
2343 gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
2344 (gpointer)specials[i].code);
2345 gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
2346 GTK_SIGNAL_FUNC(special_menuitem), inst);
2347 } else
2348 menuitem = gtk_menu_item_new();
2349 gtk_container_add(GTK_CONTAINER(inst->specialsmenu), menuitem);
2350 gtk_widget_show(menuitem);
2351 }
2352 gtk_widget_show(inst->specialsitem1);
2353 gtk_widget_show(inst->specialsitem2);
2354 } else {
2355 gtk_widget_hide(inst->specialsitem1);
2356 gtk_widget_hide(inst->specialsitem2);
2357 }
2358}
2359
1d009ae7 2360int pt_main(int argc, char **argv)
faec60ed 2361{
1d009ae7 2362 extern Backend *select_backend(Config *cfg);
2363 extern int cfgbox(Config *cfg);
a8327734 2364 struct gui_data *inst;
facd762c 2365 int font_charset;
faec60ed 2366
0f33f9d1 2367 /* defer any child exit handling until we're ready to deal with
2368 * it */
2369 block_signal(SIGCHLD, 1);
2370
faec60ed 2371 gtk_init(&argc, &argv);
2372
7428c509 2373 /*
a8327734 2374 * Create an instance structure and initialise to zeroes
7428c509 2375 */
3d88e64d 2376 inst = snew(struct gui_data);
7428c509 2377 memset(inst, 0, sizeof(*inst));
045f707b 2378 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
7428c509 2379
aca2a702 2380 if (do_cmdline(argc, argv, 0, inst, &inst->cfg))
9dc625f0 2381 exit(1); /* pre-defaults pass to get -class */
2382 do_defaults(NULL, &inst->cfg);
aca2a702 2383 if (do_cmdline(argc, argv, 1, inst, &inst->cfg))
9dc625f0 2384 exit(1); /* post-defaults, do everything */
2385
46a3419b 2386 cmdline_run_saved(&inst->cfg);
2387
2388 if (!*inst->cfg.host && !cfgbox(&inst->cfg))
1d009ae7 2389 exit(0); /* config box hit Cancel */
2390
9a30e26b 2391 inst->fonts[0] = gdk_font_load(inst->cfg.font.name);
8b618a06 2392 if (!inst->fonts[0]) {
9a30e26b 2393 fprintf(stderr, "pterm: unable to load font \"%s\"\n",
2394 inst->cfg.font.name);
8b618a06 2395 exit(1);
2396 }
facd762c 2397 font_charset = set_font_info(inst, 0);
9a30e26b 2398 if (inst->cfg.boldfont.name[0]) {
2399 inst->fonts[1] = gdk_font_load(inst->cfg.boldfont.name);
5bf50ab2 2400 if (!inst->fonts[1]) {
2401 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
9a30e26b 2402 inst->cfg.boldfont.name);
5bf50ab2 2403 exit(1);
2404 }
2dc6356a 2405 set_font_info(inst, 1);
5bf50ab2 2406 } else
2407 inst->fonts[1] = NULL;
9a30e26b 2408 if (inst->cfg.widefont.name[0]) {
2409 inst->fonts[2] = gdk_font_load(inst->cfg.widefont.name);
006238cb 2410 if (!inst->fonts[2]) {
2411 fprintf(stderr, "pterm: unable to load wide font \"%s\"\n",
9a30e26b 2412 inst->cfg.widefont.name);
006238cb 2413 exit(1);
2414 }
2415 set_font_info(inst, 2);
2416 } else
2417 inst->fonts[2] = NULL;
9a30e26b 2418 if (inst->cfg.wideboldfont.name[0]) {
2419 inst->fonts[3] = gdk_font_load(inst->cfg.wideboldfont.name);
006238cb 2420 if (!inst->fonts[3]) {
2421 fprintf(stderr, "pterm: unable to load wide/bold font \"%s\"\n",
9a30e26b 2422 inst->cfg.wideboldfont.name);
006238cb 2423 exit(1);
2424 }
2425 set_font_info(inst, 3);
2426 } else
2427 inst->fonts[3] = NULL;
5bf50ab2 2428
83616aab 2429 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
2430 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
2431
dd72dfa3 2432 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
2dc6356a 2433 inst->utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
dd72dfa3 2434
21d2b241 2435 inst->direct_to_font = init_ucs(&inst->ucsdata,
2436 inst->cfg.line_codepage, font_charset);
1709795f 2437
12d200b1 2438 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2439
16891265 2440 /*
2441 * Set up the colour map.
2442 */
a8327734 2443 palette_reset(inst);
16891265 2444
f7f27309 2445 inst->area = gtk_drawing_area_new();
2446 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
3ea863a3 2447 inst->font_width * inst->cfg.width + 2*inst->cfg.window_border,
2448 inst->font_height * inst->cfg.height + 2*inst->cfg.window_border);
2449 if (inst->cfg.scrollbar) {
330f49be 2450 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2451 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
2452 }
6a5e84dd 2453 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
3ea863a3 2454 if (inst->cfg.scrollbar) {
2455 if (inst->cfg.scrollbar_on_left)
4dc4f9c4 2456 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2457 else
2458 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2459 }
88e6b9ca 2460 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
6a5e84dd 2461
12d200b1 2462 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
f7f27309 2463
6a5e84dd 2464 {
2465 GdkGeometry geom;
3ea863a3 2466 geom.min_width = inst->font_width + 2*inst->cfg.window_border;
2467 geom.min_height = inst->font_height + 2*inst->cfg.window_border;
6a5e84dd 2468 geom.max_width = geom.max_height = -1;
3ea863a3 2469 geom.base_width = 2*inst->cfg.window_border;
2470 geom.base_height = 2*inst->cfg.window_border;
6a5e84dd 2471 geom.width_inc = inst->font_width;
2472 geom.height_inc = inst->font_height;
2473 geom.min_aspect = geom.max_aspect = 0;
12d200b1 2474 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
6a5e84dd 2475 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2476 GDK_HINT_RESIZE_INC);
6a5e84dd 2477 }
f7f27309 2478
aca2a702 2479 gtk_widget_show(inst->area);
2480 if (inst->cfg.scrollbar)
2481 gtk_widget_show(inst->sbar);
2482 gtk_widget_show(GTK_WIDGET(inst->hbox));
2483
2484 if (inst->gotpos) {
2485 int x = inst->xpos, y = inst->ypos;
2486 GtkRequisition req;
2487 gtk_widget_size_request(GTK_WIDGET(inst->window), &req);
2488 if (inst->gravity & 1) x += gdk_screen_width() - req.width;
2489 if (inst->gravity & 2) y += gdk_screen_height() - req.height;
2490 gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE);
2491 gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y);
2492 }
2493
12d200b1 2494 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
f7f27309 2495 GTK_SIGNAL_FUNC(destroy), inst);
12d200b1 2496 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
f7f27309 2497 GTK_SIGNAL_FUNC(delete_window), inst);
12d200b1 2498 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
f7f27309 2499 GTK_SIGNAL_FUNC(key_event), inst);
c33c3d76 2500 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2501 GTK_SIGNAL_FUNC(key_event), inst);
12d200b1 2502 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
f7f27309 2503 GTK_SIGNAL_FUNC(focus_event), inst);
12d200b1 2504 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
f7f27309 2505 GTK_SIGNAL_FUNC(focus_event), inst);
2506 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2507 GTK_SIGNAL_FUNC(configure_area), inst);
2508 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2509 GTK_SIGNAL_FUNC(expose_area), inst);
e6346999 2510 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2511 GTK_SIGNAL_FUNC(button_event), inst);
2512 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2513 GTK_SIGNAL_FUNC(button_event), inst);
2514 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2515 GTK_SIGNAL_FUNC(motion_event), inst);
2516 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2517 GTK_SIGNAL_FUNC(selection_received), inst);
2518 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2519 GTK_SIGNAL_FUNC(selection_get), inst);
2520 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2521 GTK_SIGNAL_FUNC(selection_clear), inst);
3ea863a3 2522 if (inst->cfg.scrollbar)
330f49be 2523 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2524 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
f7f27309 2525 gtk_timeout_add(20, timer_func, inst);
2526 gtk_widget_add_events(GTK_WIDGET(inst->area),
e6346999 2527 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2528 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
57e636ca 2529 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
f7f27309 2530
12d200b1 2531 gtk_widget_show(inst->window);
f7f27309 2532
a8327734 2533 set_window_background(inst);
7428c509 2534
47e4e735 2535 /*
2536 * Set up the Ctrl+rightclick context menu.
2537 */
2538 {
2539 GtkWidget *menuitem;
2540 char *s;
8eed910d 2541 extern const int use_event_log;
47e4e735 2542
2543 inst->menu = gtk_menu_new();
2544
2545#define MKMENUITEM(title, func) do { \
2546 menuitem = title ? gtk_menu_item_new_with_label(title) : \
2547 gtk_menu_item_new(); \
2548 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
2549 gtk_widget_show(menuitem); \
2550 if (func != NULL) \
2551 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
2552 GTK_SIGNAL_FUNC(func), inst); \
2553} while (0)
8eed910d 2554 if (use_event_log)
2555 MKMENUITEM("Event Log", event_log_menuitem);
47e4e735 2556 MKMENUITEM("Special Commands", NULL);
2557 inst->specialsmenu = gtk_menu_new();
2558 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu);
2559 inst->specialsitem1 = menuitem;
2560 MKMENUITEM(NULL, NULL);
2561 inst->specialsitem2 = menuitem;
2562 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem);
2563 MKMENUITEM("Reset Terminal", reset_terminal_menuitem);
2564 MKMENUITEM(NULL, NULL);
2565 s = dupcat("About ", appname, NULL);
2566 MKMENUITEM(s, about_menuitem);
2567 sfree(s);
2568#undef MKMENUITEM
2569 }
2570
a8327734 2571 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2572 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2573 inst->blankcursor = make_mouse_ptr(inst, -1);
2574 make_mouse_ptr(inst, -2); /* clean up cursor font */
57e636ca 2575 inst->currcursor = inst->textcursor;
a8327734 2576 show_mouseptr(inst, 1);
d64838e1 2577
8eed910d 2578 inst->eventlogstuff = eventlogstuff_new();
2579
21d2b241 2580 inst->term = term_init(&inst->cfg, &inst->ucsdata, inst);
3ea863a3 2581 inst->logctx = log_init(inst, &inst->cfg);
a8327734 2582 term_provide_logctx(inst->term, inst->logctx);
887035a5 2583
74aca06d 2584 uxsel_init();
2585
3f935d5b 2586 term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines);
2587
1d009ae7 2588 inst->back = select_backend(&inst->cfg);
2589 {
3f935d5b 2590 char *realhost, *error;
2591
2592 error = inst->back->init((void *)inst->term, &inst->backhandle,
2593 &inst->cfg, inst->cfg.host, inst->cfg.port,
2594 &realhost, inst->cfg.tcp_nodelay);
2595
2596 if (error) {
2597 char *msg = dupprintf("Unable to open connection to %s:\n%s",
2598 inst->cfg.host, error);
2599 inst->exited = TRUE;
2600 fatal_message_box(inst->window, msg);
2601 sfree(msg);
2602 return 0;
2603 }
10705014 2604
2605 if (inst->cfg.wintitle[0])
2606 set_title(inst, inst->cfg.wintitle);
2607 else {
2608 char *title = make_default_wintitle(realhost);
2609 set_title(inst, title);
2610 sfree(title);
2611 }
1d009ae7 2612 }
a8327734 2613 inst->back->provide_logctx(inst->backhandle, inst->logctx);
47e4e735 2614 update_specials_menu(inst->term);
755a6d84 2615
5def7522 2616 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
78843a7c 2617
fe5634f6 2618 inst->ldisc =
3ea863a3 2619 ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle, inst);
b9d7bcad 2620 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
755a6d84 2621
0f33f9d1 2622 /* now we're reday to deal with the child exit handler being
2623 * called */
2624 block_signal(SIGCHLD, 0);
74aca06d 2625
2626 inst->exited = FALSE;
2627
f7f27309 2628 gtk_main();
2629
2630 return 0;
2631}