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