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