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