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