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