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