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