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