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