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