Introduce the ability to control whether the shell run in pterm is a
[sgt/putty] / unix / pterm.c
CommitLineData
f7f27309 1/*
2 * pterm - a fusion of the PuTTY terminal emulator with a Unix pty
3 * back end, all running as a GTK application. Wish me luck.
4 */
5
6#include <string.h>
d64838e1 7#include <assert.h>
f7f27309 8#include <stdlib.h>
1709795f 9#include <stdio.h>
f7f27309 10#include <time.h>
054d8535 11#include <errno.h>
12#include <fcntl.h>
13#include <unistd.h>
f7f27309 14#include <gtk/gtk.h>
a57cd64b 15#include <gdk/gdkkeysyms.h>
f7f27309 16
1709795f 17#define PUTTY_DO_GLOBALS /* actually _define_ globals */
18#include "putty.h"
19
f7f27309 20#define CAT2(x,y) x ## y
21#define CAT(x,y) CAT2(x,y)
22#define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
23
d64838e1 24#define NCOLOURS (lenof(((Config *)0)->colours))
25
26struct gui_data {
12d200b1 27 GtkWidget *window, *area, *sbar;
6a5e84dd 28 GtkBox *hbox;
29 GtkAdjustment *sbar_adjust;
d64838e1 30 GdkPixmap *pixmap;
31 GdkFont *fonts[2]; /* normal and bold (for now!) */
57e636ca 32 GdkCursor *rawcursor, *textcursor, *blankcursor, *currcursor;
d64838e1 33 GdkColor cols[NCOLOURS];
34 GdkColormap *colmap;
e6346999 35 wchar_t *pastein_data;
36 int pastein_data_len;
37 char *pasteout_data;
38 int pasteout_data_len;
83616aab 39 int font_width, font_height;
88e6b9ca 40 int ignore_sbar;
b3530065 41 int mouseptr_visible;
0f660c8f 42 guint term_paste_idle_id;
dd72dfa3 43 GdkAtom compound_text_atom;
12d200b1 44 char wintitle[sizeof(((Config *)0)->wintitle)];
d64838e1 45};
46
47static struct gui_data the_inst;
48static struct gui_data *inst = &the_inst; /* so we always write `inst->' */
49static int send_raw_mouse;
50
1709795f 51void ldisc_update(int echo, int edit)
52{
53 /*
54 * This is a stub in pterm. If I ever produce a Unix
55 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
56 * then it will require some termios manoeuvring analogous to
57 * that in the Windows plink.c, but here it's meaningless.
58 */
59}
60
61int askappend(char *filename)
62{
63 /*
4c0901ed 64 * Logging in an xterm-alike is liable to be something you only
65 * do at serious diagnostic need. Hence, I'm going to take the
66 * easy option for now and assume we always want to overwrite
67 * log files. I can always make it properly configurable later.
1709795f 68 */
69 return 2;
70}
71
72void logevent(char *string)
73{
74 /*
57e636ca 75 * This is not a very helpful function: events are logged
76 * pretty much exclusively by the back end, and our pty back
77 * end is self-contained. So we need do nothing.
1709795f 78 */
79}
80
81/*
82 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
83 * into a cooked one (SELECT, EXTEND, PASTE).
84 *
85 * In Unix, this is not configurable; the X button arrangement is
86 * rock-solid across all applications, everyone has a three-button
87 * mouse or a means of faking it, and there is no need to switch
88 * buttons around at all.
89 */
90Mouse_Button translate_button(Mouse_Button button)
91{
92 if (button == MBT_LEFT)
93 return MBT_SELECT;
94 if (button == MBT_MIDDLE)
95 return MBT_PASTE;
96 if (button == MBT_RIGHT)
97 return MBT_EXTEND;
98 return 0; /* shouldn't happen */
99}
100
101/*
102 * Minimise or restore the window in response to a server-side
103 * request.
104 */
105void set_iconic(int iconic)
106{
107 /* FIXME: currently ignored */
108}
109
110/*
111 * Move the window in response to a server-side request.
112 */
113void move_window(int x, int y)
114{
115 /* FIXME: currently ignored */
116}
117
118/*
119 * Move the window to the top or bottom of the z-order in response
120 * to a server-side request.
121 */
122void set_zorder(int top)
123{
124 /* FIXME: currently ignored */
125}
126
127/*
128 * Refresh the window in response to a server-side request.
129 */
130void refresh_window(void)
131{
132 /* FIXME: currently ignored */
133}
134
135/*
136 * Maximise or restore the window in response to a server-side
137 * request.
138 */
139void set_zoomed(int zoomed)
140{
141 /* FIXME: currently ignored */
142}
143
144/*
145 * Report whether the window is iconic, for terminal reports.
146 */
147int is_iconic(void)
148{
149 return 0; /* FIXME */
150}
151
152/*
153 * Report the window's position, for terminal reports.
154 */
155void get_window_pos(int *x, int *y)
156{
157 *x = 3; *y = 4; /* FIXME */
158}
159
160/*
161 * Report the window's pixel size, for terminal reports.
162 */
163void get_window_pixels(int *x, int *y)
164{
165 *x = 1; *y = 2; /* FIXME */
166}
167
168/*
169 * Return the window or icon title.
170 */
171char *get_window_title(int icon)
172{
12d200b1 173 return inst->wintitle;
1709795f 174}
f7f27309 175
f7f27309 176gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
177{
178 /*
57e636ca 179 * We could implement warn-on-close here if we really wanted
180 * to.
f7f27309 181 */
182 return FALSE;
183}
184
57e636ca 185void show_mouseptr(int show)
186{
187 if (!cfg.hide_mouseptr)
188 show = 1;
189 if (show)
190 gdk_window_set_cursor(inst->area->window, inst->currcursor);
191 else
192 gdk_window_set_cursor(inst->area->window, inst->blankcursor);
b3530065 193 inst->mouseptr_visible = show;
57e636ca 194}
195
f7f27309 196gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
197{
198 struct gui_data *inst = (struct gui_data *)data;
88e6b9ca 199 int w, h, need_size = 0;
f7f27309 200
60b55c07 201 /*
202 * Set up the colour map.
203 */
204 palette_reset();
205
88e6b9ca 206 w = (event->width - 2*cfg.window_border) / inst->font_width;
207 h = (event->height - 2*cfg.window_border) / inst->font_height;
d64838e1 208
88e6b9ca 209 if (w != cfg.width || h != cfg.height) {
210 if (inst->pixmap) {
211 gdk_pixmap_unref(inst->pixmap);
212 inst->pixmap = NULL;
213 }
214 cfg.width = w;
215 cfg.height = h;
216 need_size = 1;
217 }
218 if (!inst->pixmap) {
6a5e84dd 219 GdkGC *gc;
88e6b9ca 220
221 inst->pixmap = gdk_pixmap_new(widget->window,
222 (cfg.width * inst->font_width +
223 2*cfg.window_border),
224 (cfg.height * inst->font_height +
225 2*cfg.window_border), -1);
226
6a5e84dd 227 gc = gdk_gc_new(inst->area->window);
228 gdk_gc_set_foreground(gc, &inst->cols[18]); /* default background */
229 gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0,
230 cfg.width * inst->font_width + 2*cfg.window_border,
231 cfg.height * inst->font_height + 2*cfg.window_border);
232 gdk_gc_unref(gc);
233 }
f7f27309 234
88e6b9ca 235 if (need_size) {
236 term_size(h, w, cfg.savelines);
237 }
238
f7f27309 239 return TRUE;
240}
241
242gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
243{
1709795f 244 /* struct gui_data *inst = (struct gui_data *)data; */
f7f27309 245
246 /*
1709795f 247 * Pass the exposed rectangle to terminal.c, which will call us
248 * back to do the actual painting.
f7f27309 249 */
6a5e84dd 250 if (inst->pixmap) {
251 gdk_draw_pixmap(widget->window,
252 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
253 inst->pixmap,
254 event->area.x, event->area.y,
255 event->area.x, event->area.y,
256 event->area.width, event->area.height);
257 }
1709795f 258 return TRUE;
f7f27309 259}
260
261#define KEY_PRESSED(k) \
262 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
263
264gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
265{
1709795f 266 /* struct gui_data *inst = (struct gui_data *)data; */
a57cd64b 267 char output[32];
268 int start, end;
f7f27309 269
1709795f 270 if (event->type == GDK_KEY_PRESS) {
a57cd64b 271#ifdef KEY_DEBUGGING
272 {
273 int i;
274 printf("keypress: keyval = %04x, state = %08x; string =",
275 event->keyval, event->state);
276 for (i = 0; event->string[i]; i++)
277 printf(" %02x", (unsigned char) event->string[i]);
278 printf("\n");
279 }
280#endif
281
282 /*
283 * NYI:
a57cd64b 284 * - alt+numpad
285 * - Compose key (!!! requires Unicode faff before even trying)
a57cd64b 286 */
287
6a5e84dd 288 /*
289 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
290 * at all.
291 */
292 if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) {
293 term_scroll(0, -cfg.height/2);
294 return TRUE;
295 }
296 if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) {
297 term_scroll(0, +cfg.height/2);
298 return TRUE;
299 }
300
2a2c1973 301 /*
302 * Neither does Shift-Ins.
303 */
304 if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) {
305 request_paste();
306 return TRUE;
307 }
308
a57cd64b 309 /* ALT+things gives leading Escape. */
310 output[0] = '\033';
311 strncpy(output+1, event->string, 31);
312 output[31] = '\0';
313 end = strlen(output);
314 if (event->state & GDK_MOD1_MASK)
315 start = 0;
316 else
317 start = 1;
318
319 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
320 if (!event->string[0] && event->keyval == '`' &&
321 (event->state & GDK_CONTROL_MASK)) {
322 output[1] = '\x1C';
323 end = 2;
324 }
325
326 /* Control-Break is the same as Control-C */
327 if (event->keyval == GDK_Break &&
328 (event->state & GDK_CONTROL_MASK)) {
329 output[1] = '\003';
330 end = 2;
331 }
332
333 /* Control-2, Control-Space and Control-@ are NUL */
334 if (!event->string[0] &&
335 (event->keyval == ' ' || event->keyval == '2' ||
336 event->keyval == '@') &&
337 (event->state & (GDK_SHIFT_MASK |
338 GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
339 output[1] = '\0';
340 end = 2;
341 }
342
343 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
344 if (!event->string[0] && event->keyval == ' ' &&
345 (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
346 (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
347 output[1] = '\240';
348 end = 2;
349 }
350
351 /* We don't let GTK tell us what Backspace is! We know better. */
352 if (event->keyval == GDK_BackSpace &&
353 !(event->state & GDK_SHIFT_MASK)) {
354 output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
355 end = 2;
356 }
357
358 /* Shift-Tab is ESC [ Z */
359 if (event->keyval == GDK_ISO_Left_Tab ||
360 (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
361 end = 1 + sprintf(output+1, "\033[Z");
362 }
363
364 /*
8b1fcd56 365 * NetHack keypad mode.
366 */
367 if (cfg.nethack_keypad) {
368 char *keys = NULL;
369 switch (event->keyval) {
370 case GDK_KP_1: case GDK_KP_End: keys = "bB"; break;
371 case GDK_KP_2: case GDK_KP_Down: keys = "jJ"; break;
372 case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN"; break;
373 case GDK_KP_4: case GDK_KP_Left: keys = "hH"; break;
374 case GDK_KP_5: case GDK_KP_Begin: keys = ".."; break;
375 case GDK_KP_6: case GDK_KP_Right: keys = "lL"; break;
376 case GDK_KP_7: case GDK_KP_Home: keys = "yY"; break;
377 case GDK_KP_8: case GDK_KP_Up: keys = "kK"; break;
378 case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU"; break;
379 }
380 if (keys) {
381 end = 2;
382 if (event->state & GDK_SHIFT_MASK)
383 output[1] = keys[1];
384 else
385 output[1] = keys[0];
386 goto done;
387 }
388 }
389
390 /*
a57cd64b 391 * Application keypad mode.
392 */
393 if (app_keypad_keys && !cfg.no_applic_k) {
394 int xkey = 0;
395 switch (event->keyval) {
396 case GDK_Num_Lock: xkey = 'P'; break;
397 case GDK_KP_Divide: xkey = 'Q'; break;
398 case GDK_KP_Multiply: xkey = 'R'; break;
399 case GDK_KP_Subtract: xkey = 'S'; break;
400 /*
401 * Keypad + is tricky. It covers a space that would
402 * be taken up on the VT100 by _two_ keys; so we
403 * let Shift select between the two. Worse still,
404 * in xterm function key mode we change which two...
405 */
406 case GDK_KP_Add:
407 if (cfg.funky_type == 2) {
408 if (event->state & GDK_SHIFT_MASK)
409 xkey = 'l';
410 else
411 xkey = 'k';
412 } else if (event->state & GDK_SHIFT_MASK)
413 xkey = 'm';
414 else
415 xkey = 'l';
416 break;
417 case GDK_KP_Enter: xkey = 'M'; break;
418 case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break;
419 case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break;
420 case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break;
421 case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break;
422 case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break;
423 case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break;
424 case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break;
425 case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break;
426 case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break;
427 case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break;
428 case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break;
429 }
430 if (xkey) {
431 if (vt52_mode) {
432 if (xkey >= 'P' && xkey <= 'S')
433 end = 1 + sprintf(output+1, "\033%c", xkey);
434 else
435 end = 1 + sprintf(output+1, "\033?%c", xkey);
436 } else
437 end = 1 + sprintf(output+1, "\033O%c", xkey);
438 goto done;
439 }
440 }
441
442 /*
443 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
444 * for integer decimal nn.)
445 *
446 * We also deal with the weird ones here. Linux VCs replace F1
447 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
448 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
449 * respectively.
450 */
451 {
452 int code = 0;
453 switch (event->keyval) {
454 case GDK_F1:
455 code = (event->state & GDK_SHIFT_MASK ? 23 : 11);
456 break;
457 case GDK_F2:
458 code = (event->state & GDK_SHIFT_MASK ? 24 : 12);
459 break;
460 case GDK_F3:
461 code = (event->state & GDK_SHIFT_MASK ? 25 : 13);
462 break;
463 case GDK_F4:
464 code = (event->state & GDK_SHIFT_MASK ? 26 : 14);
465 break;
466 case GDK_F5:
467 code = (event->state & GDK_SHIFT_MASK ? 28 : 15);
468 break;
469 case GDK_F6:
470 code = (event->state & GDK_SHIFT_MASK ? 29 : 17);
471 break;
472 case GDK_F7:
473 code = (event->state & GDK_SHIFT_MASK ? 31 : 18);
474 break;
475 case GDK_F8:
476 code = (event->state & GDK_SHIFT_MASK ? 32 : 19);
477 break;
478 case GDK_F9:
479 code = (event->state & GDK_SHIFT_MASK ? 33 : 20);
480 break;
481 case GDK_F10:
482 code = (event->state & GDK_SHIFT_MASK ? 34 : 21);
483 break;
484 case GDK_F11:
485 code = 23;
486 break;
487 case GDK_F12:
488 code = 24;
489 break;
490 case GDK_F13:
491 code = 25;
492 break;
493 case GDK_F14:
494 code = 26;
495 break;
496 case GDK_F15:
497 code = 28;
498 break;
499 case GDK_F16:
500 code = 29;
501 break;
502 case GDK_F17:
503 code = 31;
504 break;
505 case GDK_F18:
506 code = 32;
507 break;
508 case GDK_F19:
509 code = 33;
510 break;
511 case GDK_F20:
512 code = 34;
513 break;
514 }
515 if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) {
516 case GDK_Home: case GDK_KP_Home:
517 code = 1;
518 break;
519 case GDK_Insert: case GDK_KP_Insert:
520 code = 2;
521 break;
522 case GDK_Delete: case GDK_KP_Delete:
523 code = 3;
524 break;
525 case GDK_End: case GDK_KP_End:
526 code = 4;
527 break;
528 case GDK_Page_Up: case GDK_KP_Page_Up:
529 code = 5;
530 break;
531 case GDK_Page_Down: case GDK_KP_Page_Down:
532 code = 6;
533 break;
534 }
535 /* Reorder edit keys to physical order */
536 if (cfg.funky_type == 3 && code <= 6)
537 code = "\0\2\1\4\5\3\6"[code];
538
539 if (vt52_mode && code > 0 && code <= 6) {
540 end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
541 goto done;
542 }
543
544 if (cfg.funky_type == 5 && /* SCO function keys */
545 code >= 11 && code <= 34) {
546 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
547 int index = 0;
548 switch (event->keyval) {
549 case GDK_F1: index = 0; break;
550 case GDK_F2: index = 1; break;
551 case GDK_F3: index = 2; break;
552 case GDK_F4: index = 3; break;
553 case GDK_F5: index = 4; break;
554 case GDK_F6: index = 5; break;
555 case GDK_F7: index = 6; break;
556 case GDK_F8: index = 7; break;
557 case GDK_F9: index = 8; break;
558 case GDK_F10: index = 9; break;
559 case GDK_F11: index = 10; break;
560 case GDK_F12: index = 11; break;
561 }
562 if (event->state & GDK_SHIFT_MASK) index += 12;
563 if (event->state & GDK_CONTROL_MASK) index += 24;
564 end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
565 goto done;
566 }
567 if (cfg.funky_type == 5 && /* SCO small keypad */
568 code >= 1 && code <= 6) {
569 char codes[] = "HL.FIG";
570 if (code == 3) {
571 output[1] = '\x7F';
572 end = 2;
573 } else {
574 end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
575 }
576 goto done;
577 }
578 if ((vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
579 int offt = 0;
580 if (code > 15)
581 offt++;
582 if (code > 21)
583 offt++;
584 if (vt52_mode)
585 end = 1 + sprintf(output+1,
586 "\x1B%c", code + 'P' - 11 - offt);
587 else
588 end = 1 + sprintf(output+1,
589 "\x1BO%c", code + 'P' - 11 - offt);
590 goto done;
591 }
592 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
593 end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
594 goto done;
595 }
596 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
597 if (vt52_mode)
598 end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
599 else
600 end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
601 goto done;
602 }
603 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
604 end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
605 goto done;
606 }
607 if (code) {
608 end = 1 + sprintf(output+1, "\x1B[%d~", code);
609 goto done;
610 }
611 }
612
613 /*
614 * Cursor keys. (This includes the numberpad cursor keys,
615 * if we haven't already done them due to app keypad mode.)
616 *
617 * Here we also process un-numlocked un-appkeypadded KP5,
618 * which sends ESC [ G.
619 */
620 {
621 int xkey = 0;
622 switch (event->keyval) {
623 case GDK_Up: case GDK_KP_Up: xkey = 'A'; break;
624 case GDK_Down: case GDK_KP_Down: xkey = 'B'; break;
625 case GDK_Right: case GDK_KP_Right: xkey = 'C'; break;
626 case GDK_Left: case GDK_KP_Left: xkey = 'D'; break;
627 case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break;
628 }
629 if (xkey) {
630 /*
631 * The arrow keys normally do ESC [ A and so on. In
632 * app cursor keys mode they do ESC O A instead.
633 * Ctrl toggles the two modes.
634 */
635 if (vt52_mode) {
636 end = 1 + sprintf(output+1, "\033%c", xkey);
637 } else if (!app_cursor_keys ^
638 !(event->state & GDK_CONTROL_MASK)) {
639 end = 1 + sprintf(output+1, "\033O%c", xkey);
640 } else {
641 end = 1 + sprintf(output+1, "\033[%c", xkey);
642 }
643 goto done;
644 }
645 }
646
647 done:
648
649#ifdef KEY_DEBUGGING
650 {
651 int i;
652 printf("generating sequence:");
653 for (i = start; i < end; i++)
654 printf(" %02x", (unsigned char) output[i]);
655 printf("\n");
656 }
657#endif
57e636ca 658 if (end-start > 0) {
659 ldisc_send(output+start, end-start, 1);
660 show_mouseptr(0);
661 term_out();
662 }
1709795f 663 }
664
665 return TRUE;
f7f27309 666}
667
e6346999 668gint button_event(GtkWidget *widget, GdkEventButton *event, gpointer data)
669{
670 struct gui_data *inst = (struct gui_data *)data;
671 int shift, ctrl, alt, x, y, button, act;
672
57e636ca 673 show_mouseptr(1);
674
e6346999 675 shift = event->state & GDK_SHIFT_MASK;
676 ctrl = event->state & GDK_CONTROL_MASK;
677 alt = event->state & GDK_MOD1_MASK;
678 if (event->button == 1)
679 button = MBT_LEFT;
680 else if (event->button == 2)
681 button = MBT_MIDDLE;
682 else if (event->button == 3)
683 button = MBT_RIGHT;
684 else
685 return FALSE; /* don't even know what button! */
686
687 switch (event->type) {
688 case GDK_BUTTON_PRESS: act = MA_CLICK; break;
689 case GDK_BUTTON_RELEASE: act = MA_RELEASE; break;
690 case GDK_2BUTTON_PRESS: act = MA_2CLK; break;
691 case GDK_3BUTTON_PRESS: act = MA_3CLK; break;
692 default: return FALSE; /* don't know this event type */
693 }
694
695 if (send_raw_mouse && !(cfg.mouse_override && shift) &&
696 act != MA_CLICK && act != MA_RELEASE)
697 return TRUE; /* we ignore these in raw mouse mode */
698
699 x = (event->x - cfg.window_border) / inst->font_width;
700 y = (event->y - cfg.window_border) / inst->font_height;
701
702 term_mouse(button, act, x, y, shift, ctrl, alt);
703
704 return TRUE;
705}
706
707gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data)
708{
709 struct gui_data *inst = (struct gui_data *)data;
710 int shift, ctrl, alt, x, y, button;
711
57e636ca 712 show_mouseptr(1);
713
e6346999 714 shift = event->state & GDK_SHIFT_MASK;
715 ctrl = event->state & GDK_CONTROL_MASK;
716 alt = event->state & GDK_MOD1_MASK;
717 if (event->state & GDK_BUTTON1_MASK)
718 button = MBT_LEFT;
719 else if (event->state & GDK_BUTTON2_MASK)
720 button = MBT_MIDDLE;
721 else if (event->state & GDK_BUTTON3_MASK)
722 button = MBT_RIGHT;
723 else
724 return FALSE; /* don't even know what button! */
725
726 x = (event->x - cfg.window_border) / inst->font_width;
727 y = (event->y - cfg.window_border) / inst->font_height;
728
729 term_mouse(button, MA_DRAG, x, y, shift, ctrl, alt);
730
731 return TRUE;
732}
733
f7f27309 734gint timer_func(gpointer data)
735{
1709795f 736 /* struct gui_data *inst = (struct gui_data *)data; */
a0e16eb1 737 extern int pty_child_is_dead(); /* declared in pty.c */
738
739 if (pty_child_is_dead()) {
740 /*
741 * The primary child process died. We could keep the
742 * terminal open for remaining subprocesses to output to,
743 * but conventional wisdom seems to feel that that's the
744 * Wrong Thing for an xterm-alike, so we bail out now. This
745 * would be easy enough to change or make configurable if
746 * necessary.
747 */
748 exit(0);
749 }
f7f27309 750
1709795f 751 term_update();
f7f27309 752 return TRUE;
753}
754
054d8535 755void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
756{
757 /* struct gui_data *inst = (struct gui_data *)data; */
758 char buf[4096];
759 int ret;
760
761 ret = read(sourcefd, buf, sizeof(buf));
762
763 /*
764 * Clean termination condition is that either ret == 0, or ret
765 * < 0 and errno == EIO. Not sure why the latter, but it seems
766 * to happen. Boo.
767 */
768 if (ret == 0 || (ret < 0 && errno == EIO)) {
769 exit(0);
770 }
771
772 if (ret < 0) {
773 perror("read pty master");
774 exit(1);
775 }
776 if (ret > 0)
777 from_backend(0, buf, ret);
778 term_out();
779}
780
f7f27309 781void destroy(GtkWidget *widget, gpointer data)
782{
783 gtk_main_quit();
784}
785
786gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
787{
d64838e1 788 has_focus = event->in;
789 term_out();
790 term_update();
57e636ca 791 show_mouseptr(1);
1709795f 792 return FALSE;
793}
794
795/*
796 * set or clear the "raw mouse message" mode
797 */
798void set_raw_mouse_mode(int activate)
799{
d64838e1 800 activate = activate && !cfg.no_mouse_rep;
801 send_raw_mouse = activate;
802 if (send_raw_mouse)
57e636ca 803 inst->currcursor = inst->rawcursor;
d64838e1 804 else
57e636ca 805 inst->currcursor = inst->textcursor;
b3530065 806 show_mouseptr(inst->mouseptr_visible);
1709795f 807}
808
809void request_resize(int w, int h)
810{
811 /* FIXME: currently ignored */
812}
813
26b8e7cc 814void real_palette_set(int n, int r, int g, int b)
815{
816 gboolean success[1];
817
818 inst->cols[n].red = r * 0x0101;
819 inst->cols[n].green = g * 0x0101;
820 inst->cols[n].blue = b * 0x0101;
821
822 gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
823 FALSE, FALSE, success);
824 if (!success[0])
825 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
826 n, r, g, b);
827}
828
1709795f 829void palette_set(int n, int r, int g, int b)
830{
26b8e7cc 831 static const int first[21] = {
832 0, 2, 4, 6, 8, 10, 12, 14,
833 1, 3, 5, 7, 9, 11, 13, 15,
834 16, 17, 18, 20, 22
835 };
836 real_palette_set(first[n], r, g, b);
837 if (first[n] >= 18)
838 real_palette_set(first[n] + 1, r, g, b);
1709795f 839}
26b8e7cc 840
1709795f 841void palette_reset(void)
842{
26b8e7cc 843 /* This maps colour indices in cfg to those used in inst->cols. */
844 static const int ww[] = {
845 6, 7, 8, 9, 10, 11, 12, 13,
846 14, 15, 16, 17, 18, 19, 20, 21,
847 0, 1, 2, 3, 4, 5
848 };
849 gboolean success[NCOLOURS];
850 int i;
851
852 assert(lenof(ww) == NCOLOURS);
853
854 if (!inst->colmap) {
855 inst->colmap = gdk_colormap_get_system();
856 } else {
857 gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
858 }
859
860 for (i = 0; i < NCOLOURS; i++) {
861 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
862 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
863 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
864 }
865
866 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
867 FALSE, FALSE, success);
868 for (i = 0; i < NCOLOURS; i++) {
869 if (!success[i])
870 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
871 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
872 }
1709795f 873}
874
875void write_clip(wchar_t * data, int len, int must_deselect)
876{
e6346999 877 if (inst->pasteout_data)
878 sfree(inst->pasteout_data);
879 inst->pasteout_data = smalloc(len);
880 inst->pasteout_data_len = len;
881 wc_to_mb(0, 0, data, len, inst->pasteout_data, inst->pasteout_data_len,
882 NULL, NULL);
883
884 if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY,
885 GDK_CURRENT_TIME)) {
886 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
887 GDK_SELECTION_TYPE_STRING, 1);
dd72dfa3 888 gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY,
889 inst->compound_text_atom, 1);
e6346999 890 }
891}
892
893void selection_get(GtkWidget *widget, GtkSelectionData *seldata,
894 guint info, guint time_stamp, gpointer data)
895{
896 gtk_selection_data_set(seldata, GDK_SELECTION_TYPE_STRING, 8,
897 inst->pasteout_data, inst->pasteout_data_len);
898}
899
900gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
901 gpointer data)
902{
903 term_deselect();
904 if (inst->pasteout_data)
905 sfree(inst->pasteout_data);
906 inst->pasteout_data = NULL;
907 inst->pasteout_data_len = 0;
908 return TRUE;
909}
910
911void request_paste(void)
912{
913 /*
914 * In Unix, pasting is asynchronous: all we can do at the
915 * moment is to call gtk_selection_convert(), and when the data
916 * comes back _then_ we can call term_do_paste().
917 */
918 gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY,
919 GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
920}
921
0f660c8f 922gint idle_paste_func(gpointer data); /* forward ref */
923
e6346999 924void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
925 gpointer data)
926{
927 if (seldata->length <= 0 ||
928 seldata->type != GDK_SELECTION_TYPE_STRING)
929 return; /* Nothing happens. */
930
931 if (inst->pastein_data)
932 sfree(inst->pastein_data);
933
934 inst->pastein_data = smalloc(seldata->length * sizeof(wchar_t));
935 inst->pastein_data_len = seldata->length;
936 mb_to_wc(0, 0, seldata->data, seldata->length,
937 inst->pastein_data, inst->pastein_data_len);
938
939 term_do_paste();
0f660c8f 940
941 if (term_paste_pending())
942 inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
943}
944
945gint idle_paste_func(gpointer data)
946{
947 struct gui_data *inst = (struct gui_data *)data;
948
949 if (term_paste_pending())
950 term_paste();
951 else
952 gtk_idle_remove(inst->term_paste_idle_id);
953
954 return TRUE;
1709795f 955}
956
0f660c8f 957
1709795f 958void get_clip(wchar_t ** p, int *len)
959{
960 if (p) {
e6346999 961 *p = inst->pastein_data;
962 *len = inst->pastein_data_len;
1709795f 963 }
964}
965
966void set_title(char *title)
967{
12d200b1 968 strncpy(inst->wintitle, title, lenof(inst->wintitle));
969 inst->wintitle[lenof(inst->wintitle)-1] = '\0';
970 gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
1709795f 971}
972
973void set_icon(char *title)
974{
975 /* FIXME: currently ignored */
976}
977
978void set_sbar(int total, int start, int page)
979{
6a5e84dd 980 inst->sbar_adjust->lower = 0;
981 inst->sbar_adjust->upper = total;
982 inst->sbar_adjust->value = start;
983 inst->sbar_adjust->page_size = page;
984 inst->sbar_adjust->step_increment = 1;
985 inst->sbar_adjust->page_increment = page/2;
88e6b9ca 986 inst->ignore_sbar = TRUE;
6a5e84dd 987 gtk_adjustment_changed(inst->sbar_adjust);
88e6b9ca 988 inst->ignore_sbar = FALSE;
6a5e84dd 989}
990
991void scrollbar_moved(GtkAdjustment *adj, gpointer data)
992{
88e6b9ca 993 if (!inst->ignore_sbar)
994 term_scroll(1, (int)adj->value);
1709795f 995}
996
997void sys_cursor(int x, int y)
998{
999 /*
1000 * This is meaningless under X.
1001 */
1002}
1003
1004void beep(int mode)
1005{
1006 gdk_beep();
1007}
1008
1009int CharWidth(Context ctx, int uc)
1010{
1011 /*
1012 * Under X, any fixed-width font really _is_ fixed-width.
1013 * Double-width characters will be dealt with using a separate
1014 * font. For the moment we can simply return 1.
1015 */
1016 return 1;
1017}
1018
1019Context get_ctx(void)
1020{
d64838e1 1021 GdkGC *gc;
1022 if (!inst->area->window)
1023 return NULL;
1024 gc = gdk_gc_new(inst->area->window);
1709795f 1025 return gc;
1026}
1027
1028void free_ctx(Context ctx)
1029{
1030 GdkGC *gc = (GdkGC *)ctx;
1031 gdk_gc_unref(gc);
1032}
1033
1034/*
1035 * Draw a line of text in the window, at given character
1036 * coordinates, in given attributes.
1037 *
1038 * We are allowed to fiddle with the contents of `text'.
1039 */
1040void do_text(Context ctx, int x, int y, char *text, int len,
1041 unsigned long attr, int lattr)
1042{
d64838e1 1043 int nfg, nbg, t;
1709795f 1044 GdkGC *gc = (GdkGC *)ctx;
1709795f 1045
d64838e1 1046 /*
1047 * NYI:
57e636ca 1048 * - Unicode, code pages, and ATTR_WIDE for CJK support.
d64838e1 1049 * - cursor shapes other than block
d64838e1 1050 * - shadow bolding
d64838e1 1051 */
1052
1053 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
1054 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
1055 if (attr & ATTR_REVERSE) {
1056 t = nfg;
1057 nfg = nbg;
1058 nbg = t;
1059 }
1060 if (cfg.bold_colour && (attr & ATTR_BOLD))
1061 nfg++;
1062 if (cfg.bold_colour && (attr & ATTR_BLINK))
1063 nbg++;
1064 if (attr & TATTR_ACTCURS) {
1065 nfg = NCOLOURS-2;
1066 nbg = NCOLOURS-1;
1067 }
1068
f34df346 1069 if (lattr != LATTR_NORM) {
f34df346 1070 x *= 2;
614bb468 1071 if (x >= cols)
1072 return;
1073 if (x + len*2 > cols)
1074 len = (cols-x)/2; /* trim to LH half */
f34df346 1075 }
1076
d64838e1 1077 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
83616aab 1078 gdk_draw_rectangle(inst->pixmap, gc, 1,
6a5e84dd 1079 x*inst->font_width+cfg.window_border,
1080 y*inst->font_height+cfg.window_border,
83616aab 1081 len*inst->font_width, inst->font_height);
6a5e84dd 1082
d64838e1 1083 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
6a5e84dd 1084 gdk_draw_text(inst->pixmap, inst->fonts[0], gc,
1085 x*inst->font_width+cfg.window_border,
1086 y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
1087 text, len);
d64838e1 1088
1089 if (attr & ATTR_UNDER) {
1090 int uheight = inst->fonts[0]->ascent + 1;
83616aab 1091 if (uheight >= inst->font_height)
1092 uheight = inst->font_height - 1;
6a5e84dd 1093 gdk_draw_line(inst->pixmap, gc, x*inst->font_width+cfg.window_border,
1094 y*inst->font_height + uheight + cfg.window_border,
8252e709 1095 (x+len)*inst->font_width-1+cfg.window_border,
1096 y*inst->font_height + uheight + cfg.window_border);
d64838e1 1097 }
1098
f34df346 1099 if (lattr != LATTR_NORM) {
1100 /*
1101 * I can't find any plausible StretchBlt equivalent in the
1102 * X server, so I'm going to do this the slow and painful
1103 * way. This will involve repeated calls to
1104 * gdk_draw_pixmap() to stretch the text horizontally. It's
1105 * O(N^2) in time and O(N) in network bandwidth, but you
1106 * try thinking of a better way. :-(
1107 */
1108 int i;
1109 for (i = 0; i < len * inst->font_width; i++) {
1110 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1111 x*inst->font_width+cfg.window_border + 2*i,
1112 y*inst->font_height+cfg.window_border,
1113 x*inst->font_width+cfg.window_border + 2*i+1,
1114 y*inst->font_height+cfg.window_border,
1115 len * inst->font_width - i, inst->font_height);
1116 }
1117 len *= 2;
1118 if (lattr != LATTR_WIDE) {
1119 int dt, db;
1120 /* Now stretch vertically, in the same way. */
1121 if (lattr == LATTR_BOT)
1122 dt = 0, db = 1;
1123 else
1124 dt = 1, db = 0;
1125 for (i = 0; i < inst->font_height; i+=2) {
1126 gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
1127 x*inst->font_width+cfg.window_border,
1128 y*inst->font_height+cfg.window_border+dt*i+db,
1129 x*inst->font_width+cfg.window_border,
1130 y*inst->font_height+cfg.window_border+dt*(i+1),
1131 len * inst->font_width, inst->font_height-i-1);
1132 }
1133 }
1134 len *= 2;
1135 }
1136
d64838e1 1137 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
6a5e84dd 1138 x*inst->font_width+cfg.window_border,
1139 y*inst->font_height+cfg.window_border,
1140 x*inst->font_width+cfg.window_border,
1141 y*inst->font_height+cfg.window_border,
83616aab 1142 len*inst->font_width, inst->font_height);
1709795f 1143}
1144
1145void do_cursor(Context ctx, int x, int y, char *text, int len,
1146 unsigned long attr, int lattr)
1147{
d64838e1 1148 int passive;
1149 GdkGC *gc = (GdkGC *)ctx;
1150
1151 /*
1152 * NYI: cursor shapes other than block
1153 */
1709795f 1154 if (attr & TATTR_PASCURS) {
1155 attr &= ~TATTR_PASCURS;
d64838e1 1156 passive = 1;
1157 } else
1158 passive = 0;
1709795f 1159 do_text(ctx, x, y, text, len, attr, lattr);
d64838e1 1160 if (passive) {
1161 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
83616aab 1162 gdk_draw_rectangle(inst->pixmap, gc, 0,
6a5e84dd 1163 x*inst->font_width+cfg.window_border,
1164 y*inst->font_height+cfg.window_border,
83616aab 1165 len*inst->font_width-1, inst->font_height-1);
d64838e1 1166 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
6a5e84dd 1167 x*inst->font_width+cfg.window_border,
1168 y*inst->font_height+cfg.window_border,
1169 x*inst->font_width+cfg.window_border,
1170 y*inst->font_height+cfg.window_border,
83616aab 1171 len*inst->font_width, inst->font_height);
d64838e1 1172 }
1709795f 1173}
1174
7798fa56 1175GdkCursor *make_mouse_ptr(int cursor_val)
1176{
1177 /*
1178 * Truly hideous hack: GTK doesn't allow us to set the mouse
1179 * cursor foreground and background colours unless we've _also_
1180 * created our own cursor from bitmaps. Therefore, I need to
1181 * load the `cursor' font and draw glyphs from it on to
1182 * pixmaps, in order to construct my cursors with the fg and bg
1183 * I want. This is a gross hack, but it's more self-contained
1184 * than linking in Xlib to find the X window handle to
1185 * inst->area and calling XRecolorCursor, and it's more
1186 * futureproof than hard-coding the shapes as bitmap arrays.
1187 */
1188 static GdkFont *cursor_font = NULL;
1189 GdkPixmap *source, *mask;
1190 GdkGC *gc;
1191 GdkColor cfg = { 0, 65535, 65535, 65535 };
1192 GdkColor cbg = { 0, 0, 0, 0 };
1193 GdkColor dfg = { 1, 65535, 65535, 65535 };
1194 GdkColor dbg = { 0, 0, 0, 0 };
1195 GdkCursor *ret;
1196 gchar text[2];
1197 gint lb, rb, wid, asc, desc, w, h, x, y;
1198
57e636ca 1199 if (cursor_val == -2) {
7798fa56 1200 gdk_font_unref(cursor_font);
1201 return NULL;
1202 }
1203
57e636ca 1204 if (cursor_val >= 0 && !cursor_font)
7798fa56 1205 cursor_font = gdk_font_load("cursor");
1206
1207 /*
1208 * Get the text extent of the cursor in question. We use the
1209 * mask character for this, because it's typically slightly
1210 * bigger than the main character.
1211 */
57e636ca 1212 if (cursor_val >= 0) {
1213 text[1] = '\0';
1214 text[0] = (char)cursor_val + 1;
1215 gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc);
1216 w = rb-lb; h = asc+desc; x = -lb; y = asc;
1217 } else {
1218 w = h = 1;
1219 x = y = 0;
1220 }
7798fa56 1221
1222 source = gdk_pixmap_new(NULL, w, h, 1);
1223 mask = gdk_pixmap_new(NULL, w, h, 1);
1224
1225 /*
1226 * Draw the mask character on the mask pixmap.
1227 */
7798fa56 1228 gc = gdk_gc_new(mask);
1229 gdk_gc_set_foreground(gc, &dbg);
1230 gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h);
57e636ca 1231 if (cursor_val >= 0) {
1232 text[1] = '\0';
1233 text[0] = (char)cursor_val + 1;
1234 gdk_gc_set_foreground(gc, &dfg);
1235 gdk_draw_text(mask, cursor_font, gc, x, y, text, 1);
1236 }
7798fa56 1237 gdk_gc_unref(gc);
1238
1239 /*
1240 * Draw the main character on the source pixmap.
1241 */
7798fa56 1242 gc = gdk_gc_new(source);
1243 gdk_gc_set_foreground(gc, &dbg);
1244 gdk_draw_rectangle(source, gc, 1, 0, 0, w, h);
57e636ca 1245 if (cursor_val >= 0) {
1246 text[1] = '\0';
1247 text[0] = (char)cursor_val;
1248 gdk_gc_set_foreground(gc, &dfg);
1249 gdk_draw_text(source, cursor_font, gc, x, y, text, 1);
1250 }
7798fa56 1251 gdk_gc_unref(gc);
1252
1253 /*
1254 * Create the cursor.
1255 */
1256 ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y);
1257
1258 /*
1259 * Clean up.
1260 */
1261 gdk_pixmap_unref(source);
1262 gdk_pixmap_unref(mask);
1263
1264 return ret;
1265}
1266
1709795f 1267void modalfatalbox(char *p, ...)
1268{
1269 va_list ap;
1270 fprintf(stderr, "FATAL ERROR: ");
1271 va_start(ap, p);
1272 vfprintf(stderr, p, ap);
1273 va_end(ap);
1274 fputc('\n', stderr);
1275 exit(1);
f7f27309 1276}
1277
755a6d84 1278char *get_x_display(void)
1279{
1280 return gdk_get_display();
1281}
1282
f7f27309 1283int main(int argc, char **argv)
1284{
054d8535 1285 extern int pty_master_fd; /* declared in pty.c */
6169c758 1286 extern char **pty_argv; /* declared in pty.c */
1287 int err = 0;
f7f27309 1288
1289 gtk_init(&argc, &argv);
1290
1709795f 1291 do_defaults(NULL, &cfg);
1292
6169c758 1293 while (--argc > 0) {
1294 char *p = *++argv;
1295 if (!strcmp(p, "-fn")) {
1296 if (--argc > 0) {
1297 strncpy(cfg.font, *++argv, sizeof(cfg.font));
1298 cfg.font[sizeof(cfg.font)-1] = '\0';
1299 } else
1300 err = 1, fprintf(stderr, "pterm: -fn expects an argument\n");
1301 }
1302 if (!strcmp(p, "-e")) {
1303 if (--argc > 0) {
1304 int i;
1305 pty_argv = smalloc((argc+1) * sizeof(char *));
1306 ++argv;
1307 for (i = 0; i < argc; i++)
1308 pty_argv[i] = argv[i];
1309 pty_argv[argc] = NULL;
1310 break; /* finished command-line processing */
1311 } else
1312 err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
1313 }
12d200b1 1314 if (!strcmp(p, "-T")) {
1315 if (--argc > 0) {
1316 strncpy(cfg.wintitle, *++argv, sizeof(cfg.wintitle));
1317 cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
1318 } else
1319 err = 1, fprintf(stderr, "pterm: -T expects an argument\n");
1320 }
4c0901ed 1321 if (!strcmp(p, "-log")) {
1322 if (--argc > 0) {
1323 strncpy(cfg.logfilename, *++argv, sizeof(cfg.logfilename));
1324 cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
1325 cfg.logtype = LGTYP_DEBUG;
1326 } else
1327 err = 1, fprintf(stderr, "pterm: -log expects an argument\n");
1328 }
57e636ca 1329 if (!strcmp(p, "-hide")) {
1330 cfg.hide_mouseptr = 1;
1331 }
f51dc031 1332 if (!strcmp(p, "-ut-")) {
c8ee61b9 1333 cfg.stamp_utmp = 0;
1334 }
1335 if (!strcmp(p, "-ls-")) {
1336 cfg.login_shell = 0;
f51dc031 1337 }
8b1fcd56 1338 if (!strcmp(p, "-nethack")) {
1339 cfg.nethack_keypad = 1;
1340 }
6169c758 1341 }
1342
83616aab 1343 inst->fonts[0] = gdk_font_load(cfg.font);
1344 inst->fonts[1] = NULL; /* FIXME: what about bold font? */
1345 inst->font_width = gdk_char_width(inst->fonts[0], ' ');
1346 inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
1347
dd72dfa3 1348 inst->compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
1349
1709795f 1350 init_ucs();
1351
12d200b1 1352 inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1353
1354 if (cfg.wintitle[0])
1355 set_title(cfg.wintitle);
1356 else
1357 set_title("pterm");
1358
f7f27309 1359 inst->area = gtk_drawing_area_new();
1360 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
6a5e84dd 1361 inst->font_width * cfg.width + 2*cfg.window_border,
1362 inst->font_height * cfg.height + 2*cfg.window_border);
1363 inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 0, 0, 0));
1364 inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust);
1365 inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0));
88e6b9ca 1366 gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0);
1367 gtk_box_pack_end(inst->hbox, inst->sbar, FALSE, FALSE, 0);
6a5e84dd 1368
12d200b1 1369 gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox));
f7f27309 1370
6a5e84dd 1371 {
1372 GdkGeometry geom;
1373 geom.min_width = inst->font_width + 2*cfg.window_border;
1374 geom.min_height = inst->font_height + 2*cfg.window_border;
1375 geom.max_width = geom.max_height = -1;
1376 geom.base_width = 2*cfg.window_border;
1377 geom.base_height = 2*cfg.window_border;
1378 geom.width_inc = inst->font_width;
1379 geom.height_inc = inst->font_height;
1380 geom.min_aspect = geom.max_aspect = 0;
12d200b1 1381 gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom,
6a5e84dd 1382 GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE |
1383 GDK_HINT_RESIZE_INC);
6a5e84dd 1384 }
f7f27309 1385
12d200b1 1386 gtk_signal_connect(GTK_OBJECT(inst->window), "destroy",
f7f27309 1387 GTK_SIGNAL_FUNC(destroy), inst);
12d200b1 1388 gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event",
f7f27309 1389 GTK_SIGNAL_FUNC(delete_window), inst);
12d200b1 1390 gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event",
f7f27309 1391 GTK_SIGNAL_FUNC(key_event), inst);
12d200b1 1392 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event",
f7f27309 1393 GTK_SIGNAL_FUNC(focus_event), inst);
12d200b1 1394 gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event",
f7f27309 1395 GTK_SIGNAL_FUNC(focus_event), inst);
1396 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
1397 GTK_SIGNAL_FUNC(configure_area), inst);
1398 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
1399 GTK_SIGNAL_FUNC(expose_area), inst);
e6346999 1400 gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event",
1401 GTK_SIGNAL_FUNC(button_event), inst);
1402 gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event",
1403 GTK_SIGNAL_FUNC(button_event), inst);
1404 gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event",
1405 GTK_SIGNAL_FUNC(motion_event), inst);
1406 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received",
1407 GTK_SIGNAL_FUNC(selection_received), inst);
1408 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get",
1409 GTK_SIGNAL_FUNC(selection_get), inst);
1410 gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event",
1411 GTK_SIGNAL_FUNC(selection_clear), inst);
6a5e84dd 1412 gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
1413 GTK_SIGNAL_FUNC(scrollbar_moved), inst);
f7f27309 1414 gtk_timeout_add(20, timer_func, inst);
1415 gtk_widget_add_events(GTK_WIDGET(inst->area),
e6346999 1416 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK |
1417 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
57e636ca 1418 GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK);
f7f27309 1419
1420 gtk_widget_show(inst->area);
6a5e84dd 1421 gtk_widget_show(inst->sbar);
1422 gtk_widget_show(GTK_WIDGET(inst->hbox));
12d200b1 1423 gtk_widget_show(inst->window);
f7f27309 1424
7798fa56 1425 inst->textcursor = make_mouse_ptr(GDK_XTERM);
1426 inst->rawcursor = make_mouse_ptr(GDK_LEFT_PTR);
57e636ca 1427 inst->blankcursor = make_mouse_ptr(-1);
1428 make_mouse_ptr(-2); /* clean up cursor font */
1429 inst->currcursor = inst->textcursor;
1430 show_mouseptr(1);
d64838e1 1431
755a6d84 1432 back = &pty_backend;
1433 back->init(NULL, 0, NULL, 0);
1434
1435 gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
1436
1709795f 1437 term_init();
1438 term_size(24, 80, 2000);
1439
f7f27309 1440 gtk_main();
1441
1442 return 0;
1443}