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