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