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