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