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