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