First attempt at a Unix port of Plink. Seems to basically work;
[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 CharWidth(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;
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 }
1439
1440 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
1441 gdk_draw_rectangle(inst->pixmap, gc, 1,
1442 x*inst->font_width+cfg.window_border,
1443 y*inst->font_height+cfg.window_border,
1444 len*inst->font_width, inst->font_height);
1445
1446 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
1447 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1448 x*inst->font_width+cfg.window_border,
1449 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1450 text, len);
1451
1452 if (shadow) {
1453 gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
1454 x*inst->font_width+cfg.window_border + cfg.shadowboldoffset,
1455 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1456 text, len);
1457 }
1458
1459 if (attr & ATTR_UNDER) {
1460 int uheight = inst->fonts[0]->ascent + 1;
1461 if (uheight >= inst->font_height)
1462 uheight = inst->font_height - 1;
1463 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1464 y*inst->font_height + uheight + cfg.window_border,
1465 (x+len)*inst->font_width-1+cfg.window_border,
1466 y*inst->font_height + uheight + cfg.window_border);
1467 }
1468
1469 if (lattr != LATTR_NORM) {
1470 /*
1471 * I can't find any plausible StretchBlt equivalent in the
1472 * X server, so I'm going to do this the slow and painful
1473 * way. This will involve repeated calls to
1474 * gdk_draw_pixmap() to stretch the text horizontally. It's
1475 * O(N^2) in time and O(N) in network bandwidth, but you
1476 * try thinking of a better way. :-(
1477 */
1478 int i;
1479 for (i = 0; i < len * inst->font_width; i++) {
1480 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1481 x*inst->font_width+cfg.window_border + 2*i,
1482 y*inst->font_height+cfg.window_border,
1483 x*inst->font_width+cfg.window_border + 2*i+1,
1484 y*inst->font_height+cfg.window_border,
1485 len * inst->font_width - i, inst->font_height);
1486 }
1487 len *= 2;
1488 if (lattr != LATTR_WIDE) {
1489 int dt, db;
1490 /* Now stretch vertically, in the same way. */
1491 if (lattr == LATTR_BOT)
1492 dt = 0, db = 1;
1493 else
1494 dt = 1, db = 0;
1495 for (i = 0; i < inst->font_height; i+=2) {
1496 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1497 x*inst->font_width+cfg.window_border,
1498 y*inst->font_height+cfg.window_border+dt*i+db,
1499 x*inst->font_width+cfg.window_border,
1500 y*inst->font_height+cfg.window_border+dt*(i+1),
1501 len * inst->font_width, inst->font_height-i-1);
1502 }
1503 }
1504 }
1505 }
1506
1507 void do_text(Context ctx, int x, int y, char *text, int len,
1508 unsigned long attr, int lattr)
1509 {
1510 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1511 struct gui_data *inst = dctx->inst;
1512 GdkGC *gc = dctx->gc;
1513
1514 do_text_internal(ctx, x, y, text, len, attr, lattr);
1515
1516 if (lattr != LATTR_NORM) {
1517 x *= 2;
1518 if (x >= inst->term->cols)
1519 return;
1520 if (x + len*2 > inst->term->cols)
1521 len = (inst->term->cols-x)/2; /* trim to LH half */
1522 len *= 2;
1523 }
1524
1525 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1526 x*inst->font_width+cfg.window_border,
1527 y*inst->font_height+cfg.window_border,
1528 x*inst->font_width+cfg.window_border,
1529 y*inst->font_height+cfg.window_border,
1530 len*inst->font_width, inst->font_height);
1531 }
1532
1533 void do_cursor(Context ctx, int x, int y, char *text, int len,
1534 unsigned long attr, int lattr)
1535 {
1536 struct draw_ctx *dctx = (struct draw_ctx *)ctx;
1537 struct gui_data *inst = dctx->inst;
1538 GdkGC *gc = dctx->gc;
1539
1540 int passive;
1541
1542 if (attr & TATTR_PASCURS) {
1543 attr &= ~TATTR_PASCURS;
1544 passive = 1;
1545 } else
1546 passive = 0;
1547 if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
1548 attr &= ~TATTR_ACTCURS;
1549 }
1550 do_text_internal(ctx, x, y, text, len, attr, lattr);
1551
1552 if (lattr != LATTR_NORM) {
1553 x *= 2;
1554 if (x >= inst->term->cols)
1555 return;
1556 if (x + len*2 > inst->term->cols)
1557 len = (inst->term->cols-x)/2; /* trim to LH half */
1558 len *= 2;
1559 }
1560
1561 if (cfg.cursor_type == 0) {
1562 /*
1563 * An active block cursor will already have been done by
1564 * the above do_text call, so we only need to do anything
1565 * if it's passive.
1566 */
1567 if (passive) {
1568 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1569 gdk_draw_rectangle(inst->pixmap, gc, 0,
1570 x*inst->font_width+cfg.window_border,
1571 y*inst->font_height+cfg.window_border,
1572 len*inst->font_width-1, inst->font_height-1);
1573 }
1574 } else {
1575 int uheight;
1576 int startx, starty, dx, dy, length, i;
1577
1578 int char_width;
1579
1580 if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
1581 char_width = 2*inst->font_width;
1582 else
1583 char_width = inst->font_width;
1584
1585 if (cfg.cursor_type == 1) {
1586 uheight = inst->fonts[0]->ascent + 1;
1587 if (uheight >= inst->font_height)
1588 uheight = inst->font_height - 1;
1589
1590 startx = x * inst->font_width + cfg.window_border;
1591 starty = y * inst->font_height + cfg.window_border + uheight;
1592 dx = 1;
1593 dy = 0;
1594 length = len * char_width;
1595 } else {
1596 int xadjust = 0;
1597 if (attr & TATTR_RIGHTCURS)
1598 xadjust = char_width - 1;
1599 startx = x * inst->font_width + cfg.window_border + xadjust;
1600 starty = y * inst->font_height + cfg.window_border;
1601 dx = 0;
1602 dy = 1;
1603 length = inst->font_height;
1604 }
1605
1606 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
1607 if (passive) {
1608 for (i = 0; i < length; i++) {
1609 if (i % 2 == 0) {
1610 gdk_draw_point(inst->pixmap, gc, startx, starty);
1611 }
1612 startx += dx;
1613 starty += dy;
1614 }
1615 } else {
1616 gdk_draw_line(inst->pixmap, gc, startx, starty,
1617 startx + (length-1) * dx, starty + (length-1) * dy);
1618 }
1619 }
1620
1621 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
1622 x*inst->font_width+cfg.window_border,
1623 y*inst->font_height+cfg.window_border,
1624 x*inst->font_width+cfg.window_border,
1625 y*inst->font_height+cfg.window_border,
1626 len*inst->font_width, inst->font_height);
1627 }
1628
1629 GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val)
1630 {
1631 /*
1632 * Truly hideous hack: GTK doesn't allow us to set the mouse
1633 * cursor foreground and background colours unless we've _also_
1634 * created our own cursor from bitmaps. Therefore, I need to
1635 * load the `cursor' font and draw glyphs from it on to
1636 * pixmaps, in order to construct my cursors with the fg and bg
1637 * I want. This is a gross hack, but it's more self-contained
1638 * than linking in Xlib to find the X window handle to
1639 * inst->area and calling XRecolorCursor, and it's more
1640 * futureproof than hard-coding the shapes as bitmap arrays.
1641 */
1642 static GdkFont *cursor_font = NULL;
1643 GdkPixmap *source, *mask;
1644 GdkGC *gc;
1645 GdkColor cfg = { 0, 65535, 65535, 65535 };
1646 GdkColor cbg = { 0, 0, 0, 0 };
1647 GdkColor dfg = { 1, 65535, 65535, 65535 };
1648 GdkColor dbg = { 0, 0, 0, 0 };
1649 GdkCursor *ret;
1650 gchar text[2];
1651 gint lb, rb, wid, asc, desc, w, h, x, y;
1652
1653 if (cursor_val == -2) {
1654 gdk_font_unref(cursor_font);
1655 return NULL;
1656 }
1657
1658 if (cursor_val >= 0 && !cursor_font)
1659 cursor_font = gdk_font_load("cursor");
1660
1661 /*
1662 * Get the text extent of the cursor in question. We use the
1663 * mask character for this, because it's typically slightly
1664 * bigger than the main character.
1665 */
1666 if (cursor_val >= 0) {
1667 text[1] = '\0';
1668 text[0] = (char)cursor_val + 1;
1669 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1670 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1671 } else {
1672 w = h = 1;
1673 x = y = 0;
1674 }
1675
1676 source = gdk_pixmap_new(NULL, w, h, 1);
1677 mask = gdk_pixmap_new(NULL, w, h, 1);
1678
1679 /*
1680 * Draw the mask character on the mask pixmap.
1681 */
1682 gc = gdk_gc_new(mask);
1683 gdk_gc_set_foreground(gc, &dbg);
1684 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
1685 if (cursor_val >= 0) {
1686 text[1] = '\0';
1687 text[0] = (char)cursor_val + 1;
1688 gdk_gc_set_foreground(gc, &dfg);
1689 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1690 }
1691 gdk_gc_unref(gc);
1692
1693 /*
1694 * Draw the main character on the source pixmap.
1695 */
1696 gc = gdk_gc_new(source);
1697 gdk_gc_set_foreground(gc, &dbg);
1698 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
1699 if (cursor_val >= 0) {
1700 text[1] = '\0';
1701 text[0] = (char)cursor_val;
1702 gdk_gc_set_foreground(gc, &dfg);
1703 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1704 }
1705 gdk_gc_unref(gc);
1706
1707 /*
1708 * Create the cursor.
1709 */
1710 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1711
1712 /*
1713 * Clean up.
1714 */
1715 gdk_pixmap_unref(source);
1716 gdk_pixmap_unref(mask);
1717
1718 return ret;
1719 }
1720
1721 void modalfatalbox(char *p, ...)
1722 {
1723 va_list ap;
1724 fprintf(stderr, "FATAL ERROR: ");
1725 va_start(ap, p);
1726 vfprintf(stderr, p, ap);
1727 va_end(ap);
1728 fputc('\n', stderr);
1729 exit(1);
1730 }
1731
1732 char *get_x_display(void *frontend)
1733 {
1734 return gdk_get_display();
1735 }
1736
1737 static void help(FILE *fp) {
1738 if(fprintf(fp,
1739 "pterm option summary:\n"
1740 "\n"
1741 " --display DISPLAY Specify X display to use (note '--')\n"
1742 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1743 " -fn FONT Normal text font\n"
1744 " -fb FONT Bold text font\n"
1745 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1746 " -sl LINES Number of lines of scrollback\n"
1747 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1748 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1749 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1750 " -T TITLE Window title\n"
1751 " -ut, +ut Do(default) or do not update utmp\n"
1752 " -ls, +ls Do(default) or do not make shell a login shell\n"
1753 " -sb, +sb Do(default) or do not display a scrollbar\n"
1754 " -log PATH Log all output to a file\n"
1755 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1756 " -xrm RESOURCE-STRING Set an X resource\n"
1757 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1758 ) < 0 || fflush(fp) < 0) {
1759 perror("output error");
1760 exit(1);
1761 }
1762 }
1763
1764 int do_cmdline(int argc, char **argv, int do_everything)
1765 {
1766 int err = 0;
1767 extern char **pty_argv; /* declared in pty.c */
1768
1769 /*
1770 * Macros to make argument handling easier.
1771 */
1772 #define EXPECTS_ARG do { \
1773 if (--argc <= 0) { \
1774 err = 1; \
1775 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1776 } else \
1777 val = *++argv; \
1778 } while (0)
1779 #define SECOND_PASS_ONLY do { \
1780 if (!do_everything) continue; \
1781 } while (0)
1782
1783 /*
1784 * TODO:
1785 *
1786 * finish -geometry
1787 */
1788
1789 char *val;
1790 while (--argc > 0) {
1791 char *p = *++argv;
1792 if (!strcmp(p, "-fn") || !strcmp(p, "-font")) {
1793 EXPECTS_ARG;
1794 SECOND_PASS_ONLY;
1795 strncpy(cfg.font, val, sizeof(cfg.font));
1796 cfg.font[sizeof(cfg.font)-1] = '\0';
1797
1798 } else if (!strcmp(p, "-fb")) {
1799 EXPECTS_ARG;
1800 SECOND_PASS_ONLY;
1801 strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
1802 cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
1803
1804 } else if (!strcmp(p, "-geometry")) {
1805 int flags, x, y, w, h;
1806 EXPECTS_ARG;
1807 SECOND_PASS_ONLY;
1808
1809 flags = XParseGeometry(val, &x, &y, &w, &h);
1810 if (flags & WidthValue)
1811 cfg.width = w;
1812 if (flags & HeightValue)
1813 cfg.height = h;
1814
1815 /*
1816 * Apparently setting the initial window position is
1817 * difficult in GTK 1.2. Not entirely sure why this
1818 * should be. 2.0 has gtk_window_parse_geometry(),
1819 * which would help... For the moment, though, I can't
1820 * be bothered with this.
1821 */
1822
1823 } else if (!strcmp(p, "-sl")) {
1824 EXPECTS_ARG;
1825 SECOND_PASS_ONLY;
1826 cfg.savelines = atoi(val);
1827
1828 } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") ||
1829 !strcmp(p, "-bfg") || !strcmp(p, "-bbg") ||
1830 !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) {
1831 GdkColor col;
1832
1833 EXPECTS_ARG;
1834 SECOND_PASS_ONLY;
1835 if (!gdk_color_parse(val, &col)) {
1836 err = 1;
1837 fprintf(stderr, "pterm: unable to parse colour \"%s\"\n", val);
1838 } else {
1839 int index;
1840 index = (!strcmp(p, "-fg") ? 0 :
1841 !strcmp(p, "-bg") ? 2 :
1842 !strcmp(p, "-bfg") ? 1 :
1843 !strcmp(p, "-bbg") ? 3 :
1844 !strcmp(p, "-cfg") ? 4 :
1845 !strcmp(p, "-cbg") ? 5 : -1);
1846 assert(index != -1);
1847 cfg.colours[index][0] = col.red / 256;
1848 cfg.colours[index][1] = col.green / 256;
1849 cfg.colours[index][2] = col.blue / 256;
1850 }
1851
1852 } else if (!strcmp(p, "-e")) {
1853 /* This option swallows all further arguments. */
1854 if (!do_everything)
1855 break;
1856
1857 if (--argc > 0) {
1858 int i;
1859 pty_argv = smalloc((argc+1) * sizeof(char *));
1860 ++argv;
1861 for (i = 0; i < argc; i++)
1862 pty_argv[i] = argv[i];
1863 pty_argv[argc] = NULL;
1864 break; /* finished command-line processing */
1865 } else
1866 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1867
1868 } else if (!strcmp(p, "-T")) {
1869 EXPECTS_ARG;
1870 SECOND_PASS_ONLY;
1871 strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
1872 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1873
1874 } else if (!strcmp(p, "-log")) {
1875 EXPECTS_ARG;
1876 SECOND_PASS_ONLY;
1877 strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
1878 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1879 cfg.logtype = LGTYP_DEBUG;
1880
1881 } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) {
1882 SECOND_PASS_ONLY;
1883 cfg.stamp_utmp = 0;
1884
1885 } else if (!strcmp(p, "-ut")) {
1886 SECOND_PASS_ONLY;
1887 cfg.stamp_utmp = 1;
1888
1889 } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) {
1890 SECOND_PASS_ONLY;
1891 cfg.login_shell = 0;
1892
1893 } else if (!strcmp(p, "-ls")) {
1894 SECOND_PASS_ONLY;
1895 cfg.login_shell = 1;
1896
1897 } else if (!strcmp(p, "-nethack")) {
1898 SECOND_PASS_ONLY;
1899 cfg.nethack_keypad = 1;
1900
1901 } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) {
1902 SECOND_PASS_ONLY;
1903 cfg.scrollbar = 0;
1904
1905 } else if (!strcmp(p, "-sb")) {
1906 SECOND_PASS_ONLY;
1907 cfg.scrollbar = 0;
1908
1909 } else if (!strcmp(p, "-name")) {
1910 EXPECTS_ARG;
1911 app_name = val;
1912
1913 } else if (!strcmp(p, "-xrm")) {
1914 EXPECTS_ARG;
1915 provide_xrm_string(val);
1916
1917 } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) {
1918 help(stdout);
1919 exit(0);
1920
1921 } else {
1922 err = 1;
1923 fprintf(stderr, "pterm: unrecognized option '%s'\n", p);
1924 }
1925 }
1926
1927 return err;
1928 }
1929
1930 int main(int argc, char **argv)
1931 {
1932 extern int pty_master_fd; /* declared in pty.c */
1933 extern void pty_pre_init(void); /* declared in pty.c */
1934 struct gui_data *inst;
1935
1936 pty_pre_init();
1937
1938 gtk_init(&argc, &argv);
1939
1940 if (do_cmdline(argc, argv, 0)) /* pre-defaults pass to get -class */
1941 exit(1);
1942 do_defaults(NULL, &cfg);
1943 if (do_cmdline(argc, argv, 1)) /* post-defaults, do everything */
1944 exit(1);
1945
1946 /*
1947 * Create an instance structure and initialise to zeroes
1948 */
1949 inst = smalloc(sizeof(*inst));
1950 memset(inst, 0, sizeof(*inst));
1951 inst->alt_keycode = -1; /* this one needs _not_ to be zero */
1952
1953 inst->fonts[0] = gdk_font_load(cfg.font);
1954 if (!inst->fonts[0]) {
1955 fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
1956 exit(1);
1957 }
1958 if (cfg.boldfont[0]) {
1959 inst->fonts[1] = gdk_font_load(cfg.boldfont);
1960 if (!inst->fonts[1]) {
1961 fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
1962 cfg.boldfont);
1963 exit(1);
1964 }
1965 } else
1966 inst->fonts[1] = NULL;
1967
1968 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1969 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1970
1971 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1972
1973 init_ucs();
1974
1975 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1976
1977 if (cfg.wintitle[0])
1978 set_title(inst, cfg.wintitle);
1979 else
1980 set_title(inst, "pterm");
1981
1982 /*
1983 * Set up the colour map.
1984 */
1985 palette_reset(inst);
1986
1987 inst->area = gtk_drawing_area_new();
1988 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
1989 inst->font_width * cfg.width + 2*cfg.window_border,
1990 inst->font_height * cfg.height + 2*cfg.window_border);
1991 if (cfg.scrollbar) {
1992 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
1993 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1994 }
1995 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
1996 if (cfg.scrollbar) {
1997 if (cfg.scrollbar_on_left)
1998 gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0);
1999 else
2000 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
2001 }
2002 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
2003
2004 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
2005
2006 {
2007 GdkGeometry geom;
2008 geom.min_width = inst->font_width + 2*cfg.window_border;
2009 geom.min_height = inst->font_height + 2*cfg.window_border;
2010 geom.max_width = geom.max_height = -1;
2011 geom.base_width = 2*cfg.window_border;
2012 geom.base_height = 2*cfg.window_border;
2013 geom.width_inc = inst->font_width;
2014 geom.height_inc = inst->font_height;
2015 geom.min_aspect = geom.max_aspect = 0;
2016 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
2017 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
2018 GDK_HINT_RESIZE_INC);
2019 }
2020
2021 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
2022 GTK_SIGNAL_FUNC(destroy), inst);
2023 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
2024 GTK_SIGNAL_FUNC(delete_window), inst);
2025 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
2026 GTK_SIGNAL_FUNC(key_event), inst);
2027 gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event",
2028 GTK_SIGNAL_FUNC(key_event), inst);
2029 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
2030 GTK_SIGNAL_FUNC(focus_event), inst);
2031 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
2032 GTK_SIGNAL_FUNC(focus_event), inst);
2033 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
2034 GTK_SIGNAL_FUNC(configure_area), inst);
2035 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
2036 GTK_SIGNAL_FUNC(expose_area), inst);
2037 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
2038 GTK_SIGNAL_FUNC(button_event), inst);
2039 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
2040 GTK_SIGNAL_FUNC(button_event), inst);
2041 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
2042 GTK_SIGNAL_FUNC(motion_event), inst);
2043 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
2044 GTK_SIGNAL_FUNC(selection_received), inst);
2045 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
2046 GTK_SIGNAL_FUNC(selection_get), inst);
2047 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
2048 GTK_SIGNAL_FUNC(selection_clear), inst);
2049 if (cfg.scrollbar)
2050 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
2051 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
2052 gtk_timeout_add(20, timer_func, inst);
2053 gtk_widget_add_events(GTK_WIDGET(inst->area),
2054 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
2055 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
2056 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
2057
2058 gtk_widget_show(inst->area);
2059 if (cfg.scrollbar)
2060 gtk_widget_show(inst->sbar);
2061 gtk_widget_show(GTK_WIDGET(inst->hbox));
2062 gtk_widget_show(inst->window);
2063
2064 set_window_background(inst);
2065
2066 inst->textcursor = make_mouse_ptr(inst, GDK_XTERM);
2067 inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR);
2068 inst->blankcursor = make_mouse_ptr(inst, -1);
2069 make_mouse_ptr(inst, -2); /* clean up cursor font */
2070 inst->currcursor = inst->textcursor;
2071 show_mouseptr(inst, 1);
2072
2073 inst->term = term_init(inst);
2074 inst->logctx = log_init(inst);
2075 term_provide_logctx(inst->term, inst->logctx);
2076
2077 inst->back = &pty_backend;
2078 inst->back->init((void *)inst->term, &inst->backhandle, NULL, 0, NULL, 0);
2079 inst->back->provide_logctx(inst->backhandle, inst->logctx);
2080
2081 term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle);
2082
2083 term_size(inst->term, cfg.height, cfg.width, cfg.savelines);
2084
2085 inst->ldisc = ldisc_create(inst->term, inst->back, inst->backhandle, inst);
2086 ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */
2087
2088 inst->master_fd = pty_master_fd;
2089 inst->exited = FALSE;
2090 inst->master_func_id = gdk_input_add(pty_master_fd, GDK_INPUT_READ,
2091 pty_input_func, inst);
2092
2093 gtk_main();
2094
2095 return 0;
2096 }