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