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