f7f27309 |
1 | /* |
4397c725 |
2 | * gtkwin.c: the main code that runs a PuTTY terminal emulator and |
3 | * backend in a GTK window. |
f7f27309 |
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> |
3aac0d35 |
25 | #include <X11/Xatom.h> |
f7f27309 |
26 | |
1709795f |
27 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
2dc6356a |
28 | |
1709795f |
29 | #include "putty.h" |
887035a5 |
30 | #include "terminal.h" |
f160b7b8 |
31 | #include "gtkfont.h" |
1709795f |
32 | |
f7f27309 |
33 | #define CAT2(x,y) x ## y |
34 | #define CAT(x,y) CAT2(x,y) |
35 | #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)} |
36 | |
cecb13f6 |
37 | /* Colours come in two flavours: configurable, and xterm-extended. */ |
38 | #define NCFGCOLOURS (lenof(((Config *)0)->colours)) |
39 | #define NEXTCOLOURS 240 /* 216 colour-cube plus 24 shades of grey */ |
40 | #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS) |
d64838e1 |
41 | |
8c161662 |
42 | GdkAtom compound_text_atom, utf8_string_atom; |
43 | |
56801e3d |
44 | extern char **pty_argv; /* declared in pty.c */ |
45 | extern int use_pty_argv; |
46 | |
39934deb |
47 | /* |
48 | * Timers are global across all sessions (even if we were handling |
49 | * multiple sessions, which we aren't), so the current timer ID is |
50 | * a global variable. |
51 | */ |
52 | static guint timer_id = 0; |
53 | |
d64838e1 |
54 | struct gui_data { |
12d200b1 |
55 | GtkWidget *window, *area, *sbar; |
6a5e84dd |
56 | GtkBox *hbox; |
57 | GtkAdjustment *sbar_adjust; |
e3be8de5 |
58 | GtkWidget *menu, *specialsmenu, *specialsitem1, *specialsitem2, |
59 | *restartitem; |
56801e3d |
60 | GtkWidget *sessionsmenu; |
d64838e1 |
61 | GdkPixmap *pixmap; |
f160b7b8 |
62 | unifont *fonts[4]; /* normal, bold, wide, widebold */ |
aca2a702 |
63 | int xpos, ypos, gotpos, gravity; |
755e0524 |
64 | GdkCursor *rawcursor, *textcursor, *blankcursor, *waitcursor, *currcursor; |
cecb13f6 |
65 | GdkColor cols[NALLCOLOURS]; |
d64838e1 |
66 | GdkColormap *colmap; |
e6346999 |
67 | wchar_t *pastein_data; |
085f4a68 |
68 | int direct_to_font; |
e6346999 |
69 | int pastein_data_len; |
48f1bc44 |
70 | char *pasteout_data, *pasteout_data_ctext, *pasteout_data_utf8; |
71 | int pasteout_data_len, pasteout_data_ctext_len, pasteout_data_utf8_len; |
83616aab |
72 | int font_width, font_height; |
56b9b9a7 |
73 | int width, height; |
88e6b9ca |
74 | int ignore_sbar; |
b3530065 |
75 | int mouseptr_visible; |
755e0524 |
76 | int busy_status; |
0f660c8f |
77 | guint term_paste_idle_id; |
ee5e66d8 |
78 | guint term_exit_idle_id; |
c33c3d76 |
79 | int alt_keycode; |
045f707b |
80 | int alt_digits; |
12d200b1 |
81 | char wintitle[sizeof(((Config *)0)->wintitle)]; |
16891265 |
82 | char icontitle[sizeof(((Config *)0)->wintitle)]; |
74aca06d |
83 | int master_fd, master_func_id; |
b9d7bcad |
84 | void *ldisc; |
6b78788a |
85 | Backend *back; |
86 | void *backhandle; |
5def7522 |
87 | Terminal *term; |
a8327734 |
88 | void *logctx; |
74aca06d |
89 | int exited; |
21d2b241 |
90 | struct unicode_data ucsdata; |
3ea863a3 |
91 | Config cfg; |
8eed910d |
92 | void *eventlogstuff; |
56801e3d |
93 | char *progname, **gtkargvstart; |
94 | int ngtkargs; |
d0a039b1 |
95 | guint32 input_event_time; /* Timestamp of the most recent input event. */ |
0b3c373d |
96 | int reconfiguring; |
a8327734 |
97 | }; |
98 | |
99 | struct draw_ctx { |
100 | GdkGC *gc; |
101 | struct gui_data *inst; |
d64838e1 |
102 | }; |
103 | |
d64838e1 |
104 | static int send_raw_mouse; |
105 | |
c5e438ec |
106 | static char *app_name = "pterm"; |
107 | |
e3be8de5 |
108 | static void start_backend(struct gui_data *inst); |
109 | |
c85623f9 |
110 | char *x_get_default(const char *key) |
c5e438ec |
111 | { |
112 | return XGetDefault(GDK_DISPLAY(), app_name, key); |
113 | } |
114 | |
3f935d5b |
115 | void connection_fatal(void *frontend, char *p, ...) |
116 | { |
fbf6cb3b |
117 | struct gui_data *inst = (struct gui_data *)frontend; |
3f935d5b |
118 | |
119 | va_list ap; |
120 | char *msg; |
121 | va_start(ap, p); |
122 | msg = dupvprintf(p, ap); |
123 | va_end(ap); |
124 | inst->exited = TRUE; |
125 | fatal_message_box(inst->window, msg); |
126 | sfree(msg); |
127 | if (inst->cfg.close_on_exit == FORCE_ON) |
128 | cleanup_exit(1); |
129 | } |
130 | |
5a9eb105 |
131 | /* |
132 | * Default settings that are specific to pterm. |
133 | */ |
9a30e26b |
134 | FontSpec platform_default_fontspec(const char *name) |
5a9eb105 |
135 | { |
9a30e26b |
136 | FontSpec ret; |
5a9eb105 |
137 | if (!strcmp(name, "Font")) |
f160b7b8 |
138 | strcpy(ret.name, "server:fixed"); |
9a30e26b |
139 | else |
140 | *ret.name = '\0'; |
141 | return ret; |
142 | } |
143 | |
144 | Filename platform_default_filename(const char *name) |
145 | { |
146 | Filename ret; |
147 | if (!strcmp(name, "LogFileName")) |
148 | strcpy(ret.path, "putty.log"); |
149 | else |
150 | *ret.path = '\0'; |
151 | return ret; |
152 | } |
153 | |
154 | char *platform_default_s(const char *name) |
155 | { |
aef05b78 |
156 | if (!strcmp(name, "SerialLine")) |
157 | return dupstr("/dev/ttyS0"); |
5a9eb105 |
158 | return NULL; |
159 | } |
160 | |
c85623f9 |
161 | int platform_default_i(const char *name, int def) |
5a9eb105 |
162 | { |
163 | if (!strcmp(name, "CloseOnExit")) |
c888fed4 |
164 | return 2; /* maps to FORCE_ON after painful rearrangement :-( */ |
6e785e0c |
165 | if (!strcmp(name, "WinNameAlways")) |
166 | return 0; /* X natively supports icon titles, so use 'em by default */ |
5a9eb105 |
167 | return def; |
168 | } |
169 | |
b9d7bcad |
170 | void ldisc_update(void *frontend, int echo, int edit) |
1709795f |
171 | { |
172 | /* |
173 | * This is a stub in pterm. If I ever produce a Unix |
174 | * command-line ssh/telnet/rlogin client (i.e. a port of plink) |
175 | * then it will require some termios manoeuvring analogous to |
176 | * that in the Windows plink.c, but here it's meaningless. |
177 | */ |
178 | } |
179 | |
c6ccd5c2 |
180 | char *get_ttymode(void *frontend, const char *mode) |
181 | { |
182 | struct gui_data *inst = (struct gui_data *)frontend; |
183 | return term_get_ttymode(inst->term, mode); |
184 | } |
185 | |
fbf6cb3b |
186 | int from_backend(void *frontend, int is_stderr, const char *data, int len) |
187 | { |
188 | struct gui_data *inst = (struct gui_data *)frontend; |
189 | return term_data(inst->term, is_stderr, data, len); |
190 | } |
191 | |
edd0cb8a |
192 | int from_backend_untrusted(void *frontend, const char *data, int len) |
193 | { |
194 | struct gui_data *inst = (struct gui_data *)frontend; |
195 | return term_data_untrusted(inst->term, data, len); |
196 | } |
197 | |
198 | int get_userpass_input(prompts_t *p, unsigned char *in, int inlen) |
199 | { |
200 | struct gui_data *inst = (struct gui_data *)p->frontend; |
201 | int ret; |
202 | ret = cmdline_get_passwd_input(p, in, inlen); |
203 | if (ret == -1) |
204 | ret = term_get_userpass_input(inst->term, p, in, inlen); |
205 | return ret; |
206 | } |
207 | |
cbe2d68f |
208 | void logevent(void *frontend, const char *string) |
1709795f |
209 | { |
fbf6cb3b |
210 | struct gui_data *inst = (struct gui_data *)frontend; |
8eed910d |
211 | |
212 | log_eventlog(inst->logctx, string); |
213 | |
214 | logevent_dlg(inst->eventlogstuff, string); |
1709795f |
215 | } |
216 | |
a8327734 |
217 | int font_dimension(void *frontend, int which)/* 0 for width, 1 for height */ |
e9aef757 |
218 | { |
a8327734 |
219 | struct gui_data *inst = (struct gui_data *)frontend; |
220 | |
e9aef757 |
221 | if (which) |
222 | return inst->font_height; |
223 | else |
224 | return inst->font_width; |
225 | } |
226 | |
1709795f |
227 | /* |
228 | * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT) |
229 | * into a cooked one (SELECT, EXTEND, PASTE). |
230 | * |
231 | * In Unix, this is not configurable; the X button arrangement is |
232 | * rock-solid across all applications, everyone has a three-button |
233 | * mouse or a means of faking it, and there is no need to switch |
234 | * buttons around at all. |
235 | */ |
c387aa9f |
236 | static Mouse_Button translate_button(Mouse_Button button) |
1709795f |
237 | { |
a8327734 |
238 | /* struct gui_data *inst = (struct gui_data *)frontend; */ |
239 | |
1709795f |
240 | if (button == MBT_LEFT) |
241 | return MBT_SELECT; |
242 | if (button == MBT_MIDDLE) |
243 | return MBT_PASTE; |
244 | if (button == MBT_RIGHT) |
245 | return MBT_EXTEND; |
246 | return 0; /* shouldn't happen */ |
247 | } |
248 | |
249 | /* |
5bf9955d |
250 | * Return the top-level GtkWindow associated with a particular |
251 | * front end instance. |
252 | */ |
253 | void *get_window(void *frontend) |
254 | { |
fbf6cb3b |
255 | struct gui_data *inst = (struct gui_data *)frontend; |
5bf9955d |
256 | return inst->window; |
257 | } |
258 | |
259 | /* |
1709795f |
260 | * Minimise or restore the window in response to a server-side |
261 | * request. |
262 | */ |
a8327734 |
263 | void set_iconic(void *frontend, int iconic) |
1709795f |
264 | { |
16891265 |
265 | /* |
266 | * GTK 1.2 doesn't know how to do this. |
267 | */ |
268 | #if GTK_CHECK_VERSION(2,0,0) |
a8327734 |
269 | struct gui_data *inst = (struct gui_data *)frontend; |
16891265 |
270 | if (iconic) |
271 | gtk_window_iconify(GTK_WINDOW(inst->window)); |
272 | else |
273 | gtk_window_deiconify(GTK_WINDOW(inst->window)); |
274 | #endif |
1709795f |
275 | } |
276 | |
277 | /* |
278 | * Move the window in response to a server-side request. |
279 | */ |
a8327734 |
280 | void move_window(void *frontend, int x, int y) |
1709795f |
281 | { |
a8327734 |
282 | struct gui_data *inst = (struct gui_data *)frontend; |
16891265 |
283 | /* |
284 | * I assume that when the GTK version of this call is available |
285 | * we should use it. Not sure how it differs from the GDK one, |
286 | * though. |
287 | */ |
288 | #if GTK_CHECK_VERSION(2,0,0) |
289 | gtk_window_move(GTK_WINDOW(inst->window), x, y); |
290 | #else |
291 | gdk_window_move(inst->window->window, x, y); |
292 | #endif |
1709795f |
293 | } |
294 | |
295 | /* |
296 | * Move the window to the top or bottom of the z-order in response |
297 | * to a server-side request. |
298 | */ |
a8327734 |
299 | void set_zorder(void *frontend, int top) |
1709795f |
300 | { |
a8327734 |
301 | struct gui_data *inst = (struct gui_data *)frontend; |
16891265 |
302 | if (top) |
303 | gdk_window_raise(inst->window->window); |
304 | else |
305 | gdk_window_lower(inst->window->window); |
1709795f |
306 | } |
307 | |
308 | /* |
309 | * Refresh the window in response to a server-side request. |
310 | */ |
a8327734 |
311 | void refresh_window(void *frontend) |
1709795f |
312 | { |
a8327734 |
313 | struct gui_data *inst = (struct gui_data *)frontend; |
5def7522 |
314 | term_invalidate(inst->term); |
1709795f |
315 | } |
316 | |
317 | /* |
318 | * Maximise or restore the window in response to a server-side |
319 | * request. |
320 | */ |
a8327734 |
321 | void set_zoomed(void *frontend, int zoomed) |
1709795f |
322 | { |
16891265 |
323 | /* |
324 | * GTK 1.2 doesn't know how to do this. |
325 | */ |
326 | #if GTK_CHECK_VERSION(2,0,0) |
a8327734 |
327 | struct gui_data *inst = (struct gui_data *)frontend; |
f160b7b8 |
328 | if (zoomed) |
16891265 |
329 | gtk_window_maximize(GTK_WINDOW(inst->window)); |
330 | else |
331 | gtk_window_unmaximize(GTK_WINDOW(inst->window)); |
332 | #endif |
1709795f |
333 | } |
334 | |
335 | /* |
336 | * Report whether the window is iconic, for terminal reports. |
337 | */ |
a8327734 |
338 | int is_iconic(void *frontend) |
1709795f |
339 | { |
a8327734 |
340 | struct gui_data *inst = (struct gui_data *)frontend; |
16891265 |
341 | return !gdk_window_is_viewable(inst->window->window); |
1709795f |
342 | } |
343 | |
344 | /* |
345 | * Report the window's position, for terminal reports. |
346 | */ |
a8327734 |
347 | void get_window_pos(void *frontend, int *x, int *y) |
1709795f |
348 | { |
a8327734 |
349 | struct gui_data *inst = (struct gui_data *)frontend; |
16891265 |
350 | /* |
351 | * I assume that when the GTK version of this call is available |
352 | * we should use it. Not sure how it differs from the GDK one, |
353 | * though. |
354 | */ |
355 | #if GTK_CHECK_VERSION(2,0,0) |
356 | gtk_window_get_position(GTK_WINDOW(inst->window), x, y); |
357 | #else |
358 | gdk_window_get_position(inst->window->window, x, y); |
359 | #endif |
1709795f |
360 | } |
361 | |
362 | /* |
363 | * Report the window's pixel size, for terminal reports. |
364 | */ |
a8327734 |
365 | void get_window_pixels(void *frontend, int *x, int *y) |
1709795f |
366 | { |
a8327734 |
367 | struct gui_data *inst = (struct gui_data *)frontend; |
16891265 |
368 | /* |
369 | * I assume that when the GTK version of this call is available |
370 | * we should use it. Not sure how it differs from the GDK one, |
371 | * though. |
372 | */ |
373 | #if GTK_CHECK_VERSION(2,0,0) |
374 | gtk_window_get_size(GTK_WINDOW(inst->window), x, y); |
375 | #else |
376 | gdk_window_get_size(inst->window->window, x, y); |
377 | #endif |
1709795f |
378 | } |
379 | |
380 | /* |
381 | * Return the window or icon title. |
382 | */ |
a8327734 |
383 | char *get_window_title(void *frontend, int icon) |
1709795f |
384 | { |
a8327734 |
385 | struct gui_data *inst = (struct gui_data *)frontend; |
5446ac11 |
386 | return icon ? inst->icontitle : inst->wintitle; |
1709795f |
387 | } |
f7f27309 |
388 | |
f7f27309 |
389 | gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data) |
390 | { |
abb6895f |
391 | struct gui_data *inst = (struct gui_data *)data; |
6b0eeb4e |
392 | if (!inst->exited && inst->cfg.warn_on_close) { |
fbf6cb3b |
393 | if (!reallyclose(inst)) |
abb6895f |
394 | return TRUE; |
395 | } |
f7f27309 |
396 | return FALSE; |
397 | } |
398 | |
755e0524 |
399 | static void update_mouseptr(struct gui_data *inst) |
400 | { |
401 | switch (inst->busy_status) { |
402 | case BUSY_NOT: |
403 | if (!inst->mouseptr_visible) { |
404 | gdk_window_set_cursor(inst->area->window, inst->blankcursor); |
405 | } else if (send_raw_mouse) { |
406 | gdk_window_set_cursor(inst->area->window, inst->rawcursor); |
407 | } else { |
408 | gdk_window_set_cursor(inst->area->window, inst->textcursor); |
409 | } |
410 | break; |
411 | case BUSY_WAITING: /* XXX can we do better? */ |
412 | case BUSY_CPU: |
413 | /* We always display these cursors. */ |
414 | gdk_window_set_cursor(inst->area->window, inst->waitcursor); |
415 | break; |
416 | default: |
417 | assert(0); |
418 | } |
419 | } |
420 | |
a8327734 |
421 | static void show_mouseptr(struct gui_data *inst, int show) |
57e636ca |
422 | { |
3ea863a3 |
423 | if (!inst->cfg.hide_mouseptr) |
57e636ca |
424 | show = 1; |
b3530065 |
425 | inst->mouseptr_visible = show; |
755e0524 |
426 | update_mouseptr(inst); |
57e636ca |
427 | } |
428 | |
db26b3af |
429 | void draw_backing_rect(struct gui_data *inst) |
430 | { |
431 | GdkGC *gc = gdk_gc_new(inst->area->window); |
037bd3d3 |
432 | gdk_gc_set_foreground(gc, &inst->cols[258]); /* default background */ |
db26b3af |
433 | gdk_draw_rectangle(inst->pixmap, gc, 1, 0, 0, |
434 | inst->cfg.width * inst->font_width + 2*inst->cfg.window_border, |
435 | inst->cfg.height * inst->font_height + 2*inst->cfg.window_border); |
436 | gdk_gc_unref(gc); |
437 | } |
438 | |
f7f27309 |
439 | gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data) |
440 | { |
441 | struct gui_data *inst = (struct gui_data *)data; |
88e6b9ca |
442 | int w, h, need_size = 0; |
f7f27309 |
443 | |
e7ec3df1 |
444 | /* |
445 | * See if the terminal size has changed, in which case we must |
446 | * let the terminal know. |
447 | */ |
3ea863a3 |
448 | w = (event->width - 2*inst->cfg.window_border) / inst->font_width; |
449 | h = (event->height - 2*inst->cfg.window_border) / inst->font_height; |
56b9b9a7 |
450 | if (w != inst->width || h != inst->height) { |
56b9b9a7 |
451 | inst->cfg.width = inst->width = w; |
452 | inst->cfg.height = inst->height = h; |
88e6b9ca |
453 | need_size = 1; |
454 | } |
e7ec3df1 |
455 | |
456 | if (inst->pixmap) { |
457 | gdk_pixmap_unref(inst->pixmap); |
458 | inst->pixmap = NULL; |
6a5e84dd |
459 | } |
f7f27309 |
460 | |
e7ec3df1 |
461 | inst->pixmap = gdk_pixmap_new(widget->window, |
462 | (inst->cfg.width * inst->font_width + |
463 | 2*inst->cfg.window_border), |
464 | (inst->cfg.height * inst->font_height + |
465 | 2*inst->cfg.window_border), -1); |
466 | |
db26b3af |
467 | draw_backing_rect(inst); |
e7ec3df1 |
468 | |
469 | if (need_size && inst->term) { |
3ea863a3 |
470 | term_size(inst->term, h, w, inst->cfg.savelines); |
88e6b9ca |
471 | } |
472 | |
e7ec3df1 |
473 | if (inst->term) |
474 | term_invalidate(inst->term); |
475 | |
f7f27309 |
476 | return TRUE; |
477 | } |
478 | |
479 | gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data) |
480 | { |
a8327734 |
481 | struct gui_data *inst = (struct gui_data *)data; |
f7f27309 |
482 | |
483 | /* |
1709795f |
484 | * Pass the exposed rectangle to terminal.c, which will call us |
485 | * back to do the actual painting. |
f7f27309 |
486 | */ |
6a5e84dd |
487 | if (inst->pixmap) { |
488 | gdk_draw_pixmap(widget->window, |
489 | widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
490 | inst->pixmap, |
491 | event->area.x, event->area.y, |
492 | event->area.x, event->area.y, |
493 | event->area.width, event->area.height); |
494 | } |
1709795f |
495 | return TRUE; |
f7f27309 |
496 | } |
497 | |
498 | #define KEY_PRESSED(k) \ |
499 | (inst->keystate[(k) / 32] & (1 << ((k) % 32))) |
500 | |
501 | gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data) |
502 | { |
a8327734 |
503 | struct gui_data *inst = (struct gui_data *)data; |
a57cd64b |
504 | char output[32]; |
f5dd8adb |
505 | wchar_t ucsoutput[2]; |
506 | int ucsval, start, end, special, use_ucsoutput; |
f7f27309 |
507 | |
d0a039b1 |
508 | /* Remember the timestamp. */ |
509 | inst->input_event_time = event->time; |
510 | |
c33c3d76 |
511 | /* By default, nothing is generated. */ |
512 | end = start = 0; |
f5dd8adb |
513 | special = use_ucsoutput = FALSE; |
c33c3d76 |
514 | |
515 | /* |
516 | * If Alt is being released after typing an Alt+numberpad |
517 | * sequence, we should generate the code that was typed. |
045f707b |
518 | * |
519 | * Note that we only do this if more than one key was actually |
520 | * pressed - I don't think Alt+NumPad4 should be ^D or that |
521 | * Alt+NumPad3 should be ^C, for example. There's no serious |
522 | * inconvenience in having to type a zero before a single-digit |
523 | * character code. |
c33c3d76 |
524 | */ |
525 | if (event->type == GDK_KEY_RELEASE && |
526 | (event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L || |
527 | event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R) && |
045f707b |
528 | inst->alt_keycode >= 0 && inst->alt_digits > 1) { |
c33c3d76 |
529 | #ifdef KEY_DEBUGGING |
530 | printf("Alt key up, keycode = %d\n", inst->alt_keycode); |
531 | #endif |
532 | output[0] = inst->alt_keycode; |
533 | end = 1; |
534 | goto done; |
535 | } |
536 | |
1709795f |
537 | if (event->type == GDK_KEY_PRESS) { |
a57cd64b |
538 | #ifdef KEY_DEBUGGING |
539 | { |
540 | int i; |
541 | printf("keypress: keyval = %04x, state = %08x; string =", |
542 | event->keyval, event->state); |
543 | for (i = 0; event->string[i]; i++) |
544 | printf(" %02x", (unsigned char) event->string[i]); |
545 | printf("\n"); |
546 | } |
547 | #endif |
548 | |
549 | /* |
c33c3d76 |
550 | * NYI: Compose key (!!! requires Unicode faff before even trying) |
a57cd64b |
551 | */ |
552 | |
6a5e84dd |
553 | /* |
c33c3d76 |
554 | * If Alt has just been pressed, we start potentially |
555 | * accumulating an Alt+numberpad code. We do this by |
556 | * setting alt_keycode to -1 (nothing yet but plausible). |
557 | */ |
558 | if ((event->keyval == GDK_Meta_L || event->keyval == GDK_Alt_L || |
559 | event->keyval == GDK_Meta_R || event->keyval == GDK_Alt_R)) { |
560 | inst->alt_keycode = -1; |
045f707b |
561 | inst->alt_digits = 0; |
c33c3d76 |
562 | goto done; /* this generates nothing else */ |
563 | } |
564 | |
565 | /* |
566 | * If we're seeing a numberpad key press with Mod1 down, |
567 | * consider adding it to alt_keycode if that's sensible. |
568 | * Anything _else_ with Mod1 down cancels any possibility |
569 | * of an ALT keycode: we set alt_keycode to -2. |
570 | */ |
571 | if ((event->state & GDK_MOD1_MASK) && inst->alt_keycode != -2) { |
572 | int digit = -1; |
573 | switch (event->keyval) { |
574 | case GDK_KP_0: case GDK_KP_Insert: digit = 0; break; |
575 | case GDK_KP_1: case GDK_KP_End: digit = 1; break; |
576 | case GDK_KP_2: case GDK_KP_Down: digit = 2; break; |
577 | case GDK_KP_3: case GDK_KP_Page_Down: digit = 3; break; |
578 | case GDK_KP_4: case GDK_KP_Left: digit = 4; break; |
579 | case GDK_KP_5: case GDK_KP_Begin: digit = 5; break; |
580 | case GDK_KP_6: case GDK_KP_Right: digit = 6; break; |
581 | case GDK_KP_7: case GDK_KP_Home: digit = 7; break; |
582 | case GDK_KP_8: case GDK_KP_Up: digit = 8; break; |
583 | case GDK_KP_9: case GDK_KP_Page_Up: digit = 9; break; |
584 | } |
585 | if (digit < 0) |
586 | inst->alt_keycode = -2; /* it's invalid */ |
587 | else { |
588 | #ifdef KEY_DEBUGGING |
589 | printf("Adding digit %d to keycode %d", digit, |
590 | inst->alt_keycode); |
591 | #endif |
592 | if (inst->alt_keycode == -1) |
593 | inst->alt_keycode = digit; /* one-digit code */ |
594 | else |
595 | inst->alt_keycode = inst->alt_keycode * 10 + digit; |
045f707b |
596 | inst->alt_digits++; |
c33c3d76 |
597 | #ifdef KEY_DEBUGGING |
598 | printf(" gives new code %d\n", inst->alt_keycode); |
599 | #endif |
600 | /* Having used this digit, we now do nothing more with it. */ |
601 | goto done; |
602 | } |
603 | } |
604 | |
605 | /* |
6a5e84dd |
606 | * Shift-PgUp and Shift-PgDn don't even generate keystrokes |
607 | * at all. |
608 | */ |
609 | if (event->keyval == GDK_Page_Up && (event->state & GDK_SHIFT_MASK)) { |
3ea863a3 |
610 | term_scroll(inst->term, 0, -inst->cfg.height/2); |
6a5e84dd |
611 | return TRUE; |
612 | } |
153580da |
613 | if (event->keyval == GDK_Page_Up && (event->state & GDK_CONTROL_MASK)) { |
614 | term_scroll(inst->term, 0, -1); |
615 | return TRUE; |
616 | } |
6a5e84dd |
617 | if (event->keyval == GDK_Page_Down && (event->state & GDK_SHIFT_MASK)) { |
3ea863a3 |
618 | term_scroll(inst->term, 0, +inst->cfg.height/2); |
6a5e84dd |
619 | return TRUE; |
620 | } |
153580da |
621 | if (event->keyval == GDK_Page_Down && (event->state & GDK_CONTROL_MASK)) { |
622 | term_scroll(inst->term, 0, +1); |
623 | return TRUE; |
624 | } |
6a5e84dd |
625 | |
2a2c1973 |
626 | /* |
627 | * Neither does Shift-Ins. |
628 | */ |
629 | if (event->keyval == GDK_Insert && (event->state & GDK_SHIFT_MASK)) { |
a8327734 |
630 | request_paste(inst); |
2a2c1973 |
631 | return TRUE; |
632 | } |
633 | |
3ec1de33 |
634 | special = FALSE; |
f5dd8adb |
635 | use_ucsoutput = FALSE; |
3ec1de33 |
636 | |
a57cd64b |
637 | /* ALT+things gives leading Escape. */ |
638 | output[0] = '\033'; |
639 | strncpy(output+1, event->string, 31); |
f5dd8adb |
640 | if (!*event->string && |
641 | (ucsval = keysym_to_unicode(event->keyval)) >= 0) { |
642 | ucsoutput[0] = '\033'; |
643 | ucsoutput[1] = ucsval; |
644 | use_ucsoutput = TRUE; |
645 | end = 2; |
646 | } else { |
647 | output[31] = '\0'; |
648 | end = strlen(output); |
649 | } |
5c0ea258 |
650 | if (event->state & GDK_MOD1_MASK) { |
651 | start = 0; |
652 | if (end == 1) end = 0; |
653 | } else |
654 | start = 1; |
a57cd64b |
655 | |
656 | /* Control-` is the same as Control-\ (unless gtk has a better idea) */ |
657 | if (!event->string[0] && event->keyval == '`' && |
658 | (event->state & GDK_CONTROL_MASK)) { |
659 | output[1] = '\x1C'; |
f5dd8adb |
660 | use_ucsoutput = FALSE; |
a57cd64b |
661 | end = 2; |
662 | } |
663 | |
58c840dc |
664 | /* Control-Break sends a Break special to the backend */ |
a57cd64b |
665 | if (event->keyval == GDK_Break && |
666 | (event->state & GDK_CONTROL_MASK)) { |
58c840dc |
667 | if (inst->back) |
668 | inst->back->special(inst->backhandle, TS_BRK); |
669 | return TRUE; |
3ec1de33 |
670 | } |
671 | |
672 | /* We handle Return ourselves, because it needs to be flagged as |
673 | * special to ldisc. */ |
674 | if (event->keyval == GDK_Return) { |
675 | output[1] = '\015'; |
f5dd8adb |
676 | use_ucsoutput = FALSE; |
3ec1de33 |
677 | end = 2; |
678 | special = TRUE; |
a57cd64b |
679 | } |
680 | |
681 | /* Control-2, Control-Space and Control-@ are NUL */ |
682 | if (!event->string[0] && |
683 | (event->keyval == ' ' || event->keyval == '2' || |
684 | event->keyval == '@') && |
685 | (event->state & (GDK_SHIFT_MASK | |
686 | GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) { |
687 | output[1] = '\0'; |
f5dd8adb |
688 | use_ucsoutput = FALSE; |
a57cd64b |
689 | end = 2; |
690 | } |
691 | |
692 | /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */ |
693 | if (!event->string[0] && event->keyval == ' ' && |
694 | (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) == |
695 | (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) { |
696 | output[1] = '\240'; |
f5dd8adb |
697 | use_ucsoutput = FALSE; |
a57cd64b |
698 | end = 2; |
699 | } |
700 | |
701 | /* We don't let GTK tell us what Backspace is! We know better. */ |
702 | if (event->keyval == GDK_BackSpace && |
703 | !(event->state & GDK_SHIFT_MASK)) { |
3ea863a3 |
704 | output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08'; |
f5dd8adb |
705 | use_ucsoutput = FALSE; |
a57cd64b |
706 | end = 2; |
3ec1de33 |
707 | special = TRUE; |
a57cd64b |
708 | } |
e8e8d6e2 |
709 | /* For Shift Backspace, do opposite of what is configured. */ |
710 | if (event->keyval == GDK_BackSpace && |
711 | (event->state & GDK_SHIFT_MASK)) { |
3ea863a3 |
712 | output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F'; |
f5dd8adb |
713 | use_ucsoutput = FALSE; |
e8e8d6e2 |
714 | end = 2; |
3ec1de33 |
715 | special = TRUE; |
e8e8d6e2 |
716 | } |
a57cd64b |
717 | |
718 | /* Shift-Tab is ESC [ Z */ |
719 | if (event->keyval == GDK_ISO_Left_Tab || |
720 | (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) { |
721 | end = 1 + sprintf(output+1, "\033[Z"); |
f5dd8adb |
722 | use_ucsoutput = FALSE; |
a57cd64b |
723 | } |
e6968b5e |
724 | /* And normal Tab is Tab, if the keymap hasn't already told us. |
725 | * (Curiously, at least one version of the MacOS 10.5 X server |
726 | * doesn't translate Tab for us. */ |
727 | if (event->keyval == GDK_Tab && end <= 1) { |
728 | output[1] = '\t'; |
729 | end = 2; |
730 | } |
a57cd64b |
731 | |
732 | /* |
8b1fcd56 |
733 | * NetHack keypad mode. |
734 | */ |
3ea863a3 |
735 | if (inst->cfg.nethack_keypad) { |
8b1fcd56 |
736 | char *keys = NULL; |
737 | switch (event->keyval) { |
6e67448b |
738 | case GDK_KP_1: case GDK_KP_End: keys = "bB\002"; break; |
739 | case GDK_KP_2: case GDK_KP_Down: keys = "jJ\012"; break; |
740 | case GDK_KP_3: case GDK_KP_Page_Down: keys = "nN\016"; break; |
741 | case GDK_KP_4: case GDK_KP_Left: keys = "hH\010"; break; |
742 | case GDK_KP_5: case GDK_KP_Begin: keys = "..."; break; |
743 | case GDK_KP_6: case GDK_KP_Right: keys = "lL\014"; break; |
744 | case GDK_KP_7: case GDK_KP_Home: keys = "yY\031"; break; |
745 | case GDK_KP_8: case GDK_KP_Up: keys = "kK\013"; break; |
746 | case GDK_KP_9: case GDK_KP_Page_Up: keys = "uU\025"; break; |
8b1fcd56 |
747 | } |
748 | if (keys) { |
749 | end = 2; |
6e67448b |
750 | if (event->state & GDK_CONTROL_MASK) |
751 | output[1] = keys[2]; |
752 | else if (event->state & GDK_SHIFT_MASK) |
8b1fcd56 |
753 | output[1] = keys[1]; |
754 | else |
755 | output[1] = keys[0]; |
f5dd8adb |
756 | use_ucsoutput = FALSE; |
8b1fcd56 |
757 | goto done; |
758 | } |
759 | } |
760 | |
761 | /* |
a57cd64b |
762 | * Application keypad mode. |
763 | */ |
3ea863a3 |
764 | if (inst->term->app_keypad_keys && !inst->cfg.no_applic_k) { |
a57cd64b |
765 | int xkey = 0; |
766 | switch (event->keyval) { |
767 | case GDK_Num_Lock: xkey = 'P'; break; |
768 | case GDK_KP_Divide: xkey = 'Q'; break; |
769 | case GDK_KP_Multiply: xkey = 'R'; break; |
770 | case GDK_KP_Subtract: xkey = 'S'; break; |
771 | /* |
772 | * Keypad + is tricky. It covers a space that would |
773 | * be taken up on the VT100 by _two_ keys; so we |
774 | * let Shift select between the two. Worse still, |
775 | * in xterm function key mode we change which two... |
776 | */ |
777 | case GDK_KP_Add: |
5789bc94 |
778 | if (inst->cfg.funky_type == FUNKY_XTERM) { |
a57cd64b |
779 | if (event->state & GDK_SHIFT_MASK) |
780 | xkey = 'l'; |
781 | else |
782 | xkey = 'k'; |
783 | } else if (event->state & GDK_SHIFT_MASK) |
784 | xkey = 'm'; |
785 | else |
786 | xkey = 'l'; |
787 | break; |
788 | case GDK_KP_Enter: xkey = 'M'; break; |
789 | case GDK_KP_0: case GDK_KP_Insert: xkey = 'p'; break; |
790 | case GDK_KP_1: case GDK_KP_End: xkey = 'q'; break; |
791 | case GDK_KP_2: case GDK_KP_Down: xkey = 'r'; break; |
792 | case GDK_KP_3: case GDK_KP_Page_Down: xkey = 's'; break; |
793 | case GDK_KP_4: case GDK_KP_Left: xkey = 't'; break; |
794 | case GDK_KP_5: case GDK_KP_Begin: xkey = 'u'; break; |
795 | case GDK_KP_6: case GDK_KP_Right: xkey = 'v'; break; |
796 | case GDK_KP_7: case GDK_KP_Home: xkey = 'w'; break; |
797 | case GDK_KP_8: case GDK_KP_Up: xkey = 'x'; break; |
798 | case GDK_KP_9: case GDK_KP_Page_Up: xkey = 'y'; break; |
799 | case GDK_KP_Decimal: case GDK_KP_Delete: xkey = 'n'; break; |
800 | } |
801 | if (xkey) { |
5def7522 |
802 | if (inst->term->vt52_mode) { |
a57cd64b |
803 | if (xkey >= 'P' && xkey <= 'S') |
804 | end = 1 + sprintf(output+1, "\033%c", xkey); |
805 | else |
806 | end = 1 + sprintf(output+1, "\033?%c", xkey); |
807 | } else |
808 | end = 1 + sprintf(output+1, "\033O%c", xkey); |
f5dd8adb |
809 | use_ucsoutput = FALSE; |
a57cd64b |
810 | goto done; |
811 | } |
812 | } |
813 | |
814 | /* |
815 | * Next, all the keys that do tilde codes. (ESC '[' nn '~', |
816 | * for integer decimal nn.) |
817 | * |
818 | * We also deal with the weird ones here. Linux VCs replace F1 |
819 | * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but |
820 | * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w |
821 | * respectively. |
822 | */ |
823 | { |
824 | int code = 0; |
825 | switch (event->keyval) { |
826 | case GDK_F1: |
827 | code = (event->state & GDK_SHIFT_MASK ? 23 : 11); |
828 | break; |
829 | case GDK_F2: |
830 | code = (event->state & GDK_SHIFT_MASK ? 24 : 12); |
831 | break; |
832 | case GDK_F3: |
833 | code = (event->state & GDK_SHIFT_MASK ? 25 : 13); |
834 | break; |
835 | case GDK_F4: |
836 | code = (event->state & GDK_SHIFT_MASK ? 26 : 14); |
837 | break; |
838 | case GDK_F5: |
839 | code = (event->state & GDK_SHIFT_MASK ? 28 : 15); |
840 | break; |
841 | case GDK_F6: |
842 | code = (event->state & GDK_SHIFT_MASK ? 29 : 17); |
843 | break; |
844 | case GDK_F7: |
845 | code = (event->state & GDK_SHIFT_MASK ? 31 : 18); |
846 | break; |
847 | case GDK_F8: |
848 | code = (event->state & GDK_SHIFT_MASK ? 32 : 19); |
849 | break; |
850 | case GDK_F9: |
851 | code = (event->state & GDK_SHIFT_MASK ? 33 : 20); |
852 | break; |
853 | case GDK_F10: |
854 | code = (event->state & GDK_SHIFT_MASK ? 34 : 21); |
855 | break; |
856 | case GDK_F11: |
857 | code = 23; |
858 | break; |
859 | case GDK_F12: |
860 | code = 24; |
861 | break; |
862 | case GDK_F13: |
863 | code = 25; |
864 | break; |
865 | case GDK_F14: |
866 | code = 26; |
867 | break; |
868 | case GDK_F15: |
869 | code = 28; |
870 | break; |
871 | case GDK_F16: |
872 | code = 29; |
873 | break; |
874 | case GDK_F17: |
875 | code = 31; |
876 | break; |
877 | case GDK_F18: |
878 | code = 32; |
879 | break; |
880 | case GDK_F19: |
881 | code = 33; |
882 | break; |
883 | case GDK_F20: |
884 | code = 34; |
885 | break; |
886 | } |
887 | if (!(event->state & GDK_CONTROL_MASK)) switch (event->keyval) { |
888 | case GDK_Home: case GDK_KP_Home: |
889 | code = 1; |
890 | break; |
891 | case GDK_Insert: case GDK_KP_Insert: |
892 | code = 2; |
893 | break; |
894 | case GDK_Delete: case GDK_KP_Delete: |
895 | code = 3; |
896 | break; |
897 | case GDK_End: case GDK_KP_End: |
898 | code = 4; |
899 | break; |
900 | case GDK_Page_Up: case GDK_KP_Page_Up: |
901 | code = 5; |
902 | break; |
903 | case GDK_Page_Down: case GDK_KP_Page_Down: |
904 | code = 6; |
905 | break; |
906 | } |
907 | /* Reorder edit keys to physical order */ |
5789bc94 |
908 | if (inst->cfg.funky_type == FUNKY_VT400 && code <= 6) |
a57cd64b |
909 | code = "\0\2\1\4\5\3\6"[code]; |
910 | |
5def7522 |
911 | if (inst->term->vt52_mode && code > 0 && code <= 6) { |
a57cd64b |
912 | end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]); |
f5dd8adb |
913 | use_ucsoutput = FALSE; |
a57cd64b |
914 | goto done; |
915 | } |
916 | |
5789bc94 |
917 | if (inst->cfg.funky_type == FUNKY_SCO && /* SCO function keys */ |
a57cd64b |
918 | code >= 11 && code <= 34) { |
919 | char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{"; |
920 | int index = 0; |
921 | switch (event->keyval) { |
922 | case GDK_F1: index = 0; break; |
923 | case GDK_F2: index = 1; break; |
924 | case GDK_F3: index = 2; break; |
925 | case GDK_F4: index = 3; break; |
926 | case GDK_F5: index = 4; break; |
927 | case GDK_F6: index = 5; break; |
928 | case GDK_F7: index = 6; break; |
929 | case GDK_F8: index = 7; break; |
930 | case GDK_F9: index = 8; break; |
931 | case GDK_F10: index = 9; break; |
932 | case GDK_F11: index = 10; break; |
933 | case GDK_F12: index = 11; break; |
934 | } |
935 | if (event->state & GDK_SHIFT_MASK) index += 12; |
936 | if (event->state & GDK_CONTROL_MASK) index += 24; |
937 | end = 1 + sprintf(output+1, "\x1B[%c", codes[index]); |
f5dd8adb |
938 | use_ucsoutput = FALSE; |
a57cd64b |
939 | goto done; |
940 | } |
5789bc94 |
941 | if (inst->cfg.funky_type == FUNKY_SCO && /* SCO small keypad */ |
a57cd64b |
942 | code >= 1 && code <= 6) { |
943 | char codes[] = "HL.FIG"; |
944 | if (code == 3) { |
945 | output[1] = '\x7F'; |
946 | end = 2; |
947 | } else { |
948 | end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]); |
949 | } |
f5dd8adb |
950 | use_ucsoutput = FALSE; |
a57cd64b |
951 | goto done; |
952 | } |
5789bc94 |
953 | if ((inst->term->vt52_mode || inst->cfg.funky_type == FUNKY_VT100P) && |
887035a5 |
954 | code >= 11 && code <= 24) { |
a57cd64b |
955 | int offt = 0; |
956 | if (code > 15) |
957 | offt++; |
958 | if (code > 21) |
959 | offt++; |
5def7522 |
960 | if (inst->term->vt52_mode) |
a57cd64b |
961 | end = 1 + sprintf(output+1, |
962 | "\x1B%c", code + 'P' - 11 - offt); |
963 | else |
964 | end = 1 + sprintf(output+1, |
965 | "\x1BO%c", code + 'P' - 11 - offt); |
f5dd8adb |
966 | use_ucsoutput = FALSE; |
a57cd64b |
967 | goto done; |
968 | } |
5789bc94 |
969 | if (inst->cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) { |
a57cd64b |
970 | end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11); |
f5dd8adb |
971 | use_ucsoutput = FALSE; |
a57cd64b |
972 | goto done; |
973 | } |
5789bc94 |
974 | if (inst->cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) { |
5def7522 |
975 | if (inst->term->vt52_mode) |
a57cd64b |
976 | end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11); |
977 | else |
978 | end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11); |
f5dd8adb |
979 | use_ucsoutput = FALSE; |
a57cd64b |
980 | goto done; |
981 | } |
3ea863a3 |
982 | if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) { |
a57cd64b |
983 | end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw"); |
f5dd8adb |
984 | use_ucsoutput = FALSE; |
a57cd64b |
985 | goto done; |
986 | } |
987 | if (code) { |
988 | end = 1 + sprintf(output+1, "\x1B[%d~", code); |
f5dd8adb |
989 | use_ucsoutput = FALSE; |
a57cd64b |
990 | goto done; |
991 | } |
992 | } |
993 | |
994 | /* |
995 | * Cursor keys. (This includes the numberpad cursor keys, |
996 | * if we haven't already done them due to app keypad mode.) |
997 | * |
998 | * Here we also process un-numlocked un-appkeypadded KP5, |
999 | * which sends ESC [ G. |
1000 | */ |
1001 | { |
1002 | int xkey = 0; |
1003 | switch (event->keyval) { |
1004 | case GDK_Up: case GDK_KP_Up: xkey = 'A'; break; |
1005 | case GDK_Down: case GDK_KP_Down: xkey = 'B'; break; |
1006 | case GDK_Right: case GDK_KP_Right: xkey = 'C'; break; |
1007 | case GDK_Left: case GDK_KP_Left: xkey = 'D'; break; |
1008 | case GDK_Begin: case GDK_KP_Begin: xkey = 'G'; break; |
1009 | } |
1010 | if (xkey) { |
1011 | /* |
1012 | * The arrow keys normally do ESC [ A and so on. In |
1013 | * app cursor keys mode they do ESC O A instead. |
1014 | * Ctrl toggles the two modes. |
1015 | */ |
5def7522 |
1016 | if (inst->term->vt52_mode) { |
a57cd64b |
1017 | end = 1 + sprintf(output+1, "\033%c", xkey); |
5def7522 |
1018 | } else if (!inst->term->app_cursor_keys ^ |
a57cd64b |
1019 | !(event->state & GDK_CONTROL_MASK)) { |
1020 | end = 1 + sprintf(output+1, "\033O%c", xkey); |
1021 | } else { |
1022 | end = 1 + sprintf(output+1, "\033[%c", xkey); |
1023 | } |
f5dd8adb |
1024 | use_ucsoutput = FALSE; |
a57cd64b |
1025 | goto done; |
1026 | } |
1027 | } |
c33c3d76 |
1028 | goto done; |
1029 | } |
a57cd64b |
1030 | |
c33c3d76 |
1031 | done: |
a57cd64b |
1032 | |
c33c3d76 |
1033 | if (end-start > 0) { |
a57cd64b |
1034 | #ifdef KEY_DEBUGGING |
c33c3d76 |
1035 | int i; |
1036 | printf("generating sequence:"); |
1037 | for (i = start; i < end; i++) |
1038 | printf(" %02x", (unsigned char) output[i]); |
1039 | printf("\n"); |
a57cd64b |
1040 | #endif |
c33c3d76 |
1041 | |
3ec1de33 |
1042 | if (special) { |
1043 | /* |
1044 | * For special control characters, the character set |
1045 | * should never matter. |
1046 | */ |
1047 | output[end] = '\0'; /* NUL-terminate */ |
e3be8de5 |
1048 | if (inst->ldisc) |
1049 | ldisc_send(inst->ldisc, output+start, -2, 1); |
3ec1de33 |
1050 | } else if (!inst->direct_to_font) { |
f5dd8adb |
1051 | if (!use_ucsoutput) { |
1052 | /* |
1053 | * The stuff we've just generated is assumed to be |
1054 | * ISO-8859-1! This sounds insane, but `man |
1055 | * XLookupString' agrees: strings of this type |
1056 | * returned from the X server are hardcoded to |
1057 | * 8859-1. Strictly speaking we should be doing |
1058 | * this using some sort of GtkIMContext, which (if |
1059 | * we're lucky) would give us our data directly in |
1060 | * Unicode; but that's not supported in GTK 1.2 as |
1061 | * far as I can tell, and it's poorly documented |
1062 | * even in 2.0, so it'll have to wait. |
1063 | */ |
e3be8de5 |
1064 | if (inst->ldisc) |
1065 | lpage_send(inst->ldisc, CS_ISO8859_1, output+start, |
1066 | end-start, 1); |
f5dd8adb |
1067 | } else { |
1068 | /* |
1069 | * We generated our own Unicode key data from the |
1070 | * keysym, so use that instead. |
1071 | */ |
e3be8de5 |
1072 | if (inst->ldisc) |
1073 | luni_send(inst->ldisc, ucsoutput+start, end-start, 1); |
f5dd8adb |
1074 | } |
facd762c |
1075 | } else { |
1076 | /* |
1077 | * In direct-to-font mode, we just send the string |
1078 | * exactly as we received it. |
1079 | */ |
e3be8de5 |
1080 | if (inst->ldisc) |
1081 | ldisc_send(inst->ldisc, output+start, end-start, 1); |
facd762c |
1082 | } |
2dc6356a |
1083 | |
a8327734 |
1084 | show_mouseptr(inst, 0); |
5def7522 |
1085 | term_seen_key_event(inst->term); |
1709795f |
1086 | } |
1087 | |
1088 | return TRUE; |
f7f27309 |
1089 | } |
1090 | |
2e563db5 |
1091 | gboolean button_internal(struct gui_data *inst, guint32 timestamp, |
1092 | GdkEventType type, guint ebutton, guint state, |
1093 | gdouble ex, gdouble ey) |
e6346999 |
1094 | { |
e6346999 |
1095 | int shift, ctrl, alt, x, y, button, act; |
1096 | |
d0a039b1 |
1097 | /* Remember the timestamp. */ |
2e563db5 |
1098 | inst->input_event_time = timestamp; |
d0a039b1 |
1099 | |
a8327734 |
1100 | show_mouseptr(inst, 1); |
57e636ca |
1101 | |
2e563db5 |
1102 | if (ebutton == 4 && type == GDK_BUTTON_PRESS) { |
5def7522 |
1103 | term_scroll(inst->term, 0, -5); |
bab6e572 |
1104 | return TRUE; |
1105 | } |
2e563db5 |
1106 | if (ebutton == 5 && type == GDK_BUTTON_PRESS) { |
5def7522 |
1107 | term_scroll(inst->term, 0, +5); |
bab6e572 |
1108 | return TRUE; |
1109 | } |
1110 | |
2e563db5 |
1111 | shift = state & GDK_SHIFT_MASK; |
1112 | ctrl = state & GDK_CONTROL_MASK; |
1113 | alt = state & GDK_MOD1_MASK; |
47e4e735 |
1114 | |
2e563db5 |
1115 | if (ebutton == 3 && ctrl) { |
47e4e735 |
1116 | gtk_menu_popup(GTK_MENU(inst->menu), NULL, NULL, NULL, NULL, |
2e563db5 |
1117 | ebutton, timestamp); |
47e4e735 |
1118 | return TRUE; |
1119 | } |
1120 | |
2e563db5 |
1121 | if (ebutton == 1) |
e6346999 |
1122 | button = MBT_LEFT; |
2e563db5 |
1123 | else if (ebutton == 2) |
e6346999 |
1124 | button = MBT_MIDDLE; |
2e563db5 |
1125 | else if (ebutton == 3) |
e6346999 |
1126 | button = MBT_RIGHT; |
1127 | else |
1128 | return FALSE; /* don't even know what button! */ |
1129 | |
2e563db5 |
1130 | switch (type) { |
e6346999 |
1131 | case GDK_BUTTON_PRESS: act = MA_CLICK; break; |
1132 | case GDK_BUTTON_RELEASE: act = MA_RELEASE; break; |
1133 | case GDK_2BUTTON_PRESS: act = MA_2CLK; break; |
1134 | case GDK_3BUTTON_PRESS: act = MA_3CLK; break; |
1135 | default: return FALSE; /* don't know this event type */ |
1136 | } |
1137 | |
3ea863a3 |
1138 | if (send_raw_mouse && !(inst->cfg.mouse_override && shift) && |
e6346999 |
1139 | act != MA_CLICK && act != MA_RELEASE) |
1140 | return TRUE; /* we ignore these in raw mouse mode */ |
1141 | |
2e563db5 |
1142 | x = (ex - inst->cfg.window_border) / inst->font_width; |
1143 | y = (ey - inst->cfg.window_border) / inst->font_height; |
e6346999 |
1144 | |
fc5b0934 |
1145 | term_mouse(inst->term, button, translate_button(button), act, |
1146 | x, y, shift, ctrl, alt); |
e6346999 |
1147 | |
1148 | return TRUE; |
1149 | } |
1150 | |
2e563db5 |
1151 | gboolean button_event(GtkWidget *widget, GdkEventButton *event, gpointer data) |
1152 | { |
1153 | struct gui_data *inst = (struct gui_data *)data; |
1154 | return button_internal(inst, event->time, event->type, event->button, |
1155 | event->state, event->x, event->y); |
1156 | } |
1157 | |
1158 | #if GTK_CHECK_VERSION(2,0,0) |
1159 | /* |
1160 | * In GTK 2, mouse wheel events have become a new type of event. |
1161 | * This handler translates them back into button-4 and button-5 |
1162 | * presses so that I don't have to change my old code too much :-) |
1163 | */ |
1164 | gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer data) |
1165 | { |
1166 | struct gui_data *inst = (struct gui_data *)data; |
1167 | guint button; |
1168 | |
1169 | if (event->direction == GDK_SCROLL_UP) |
1170 | button = 4; |
1171 | else if (event->direction == GDK_SCROLL_DOWN) |
1172 | button = 5; |
1173 | else |
1174 | return FALSE; |
1175 | |
1176 | return button_internal(inst, event->time, GDK_BUTTON_PRESS, |
1177 | button, event->state, event->x, event->y); |
1178 | } |
1179 | #endif |
1180 | |
e6346999 |
1181 | gint motion_event(GtkWidget *widget, GdkEventMotion *event, gpointer data) |
1182 | { |
1183 | struct gui_data *inst = (struct gui_data *)data; |
1184 | int shift, ctrl, alt, x, y, button; |
1185 | |
d0a039b1 |
1186 | /* Remember the timestamp. */ |
1187 | inst->input_event_time = event->time; |
1188 | |
a8327734 |
1189 | show_mouseptr(inst, 1); |
57e636ca |
1190 | |
e6346999 |
1191 | shift = event->state & GDK_SHIFT_MASK; |
1192 | ctrl = event->state & GDK_CONTROL_MASK; |
1193 | alt = event->state & GDK_MOD1_MASK; |
1194 | if (event->state & GDK_BUTTON1_MASK) |
1195 | button = MBT_LEFT; |
1196 | else if (event->state & GDK_BUTTON2_MASK) |
1197 | button = MBT_MIDDLE; |
1198 | else if (event->state & GDK_BUTTON3_MASK) |
1199 | button = MBT_RIGHT; |
1200 | else |
1201 | return FALSE; /* don't even know what button! */ |
1202 | |
3ea863a3 |
1203 | x = (event->x - inst->cfg.window_border) / inst->font_width; |
1204 | y = (event->y - inst->cfg.window_border) / inst->font_height; |
e6346999 |
1205 | |
fc5b0934 |
1206 | term_mouse(inst->term, button, translate_button(button), MA_DRAG, |
1207 | x, y, shift, ctrl, alt); |
e6346999 |
1208 | |
1209 | return TRUE; |
1210 | } |
1211 | |
b9d7bcad |
1212 | void frontend_keypress(void *handle) |
90cfd8f4 |
1213 | { |
b9d7bcad |
1214 | struct gui_data *inst = (struct gui_data *)handle; |
1215 | |
90cfd8f4 |
1216 | /* |
1217 | * If our child process has exited but not closed, terminate on |
1218 | * any keypress. |
1219 | */ |
1220 | if (inst->exited) |
1221 | exit(0); |
1222 | } |
1223 | |
ee5e66d8 |
1224 | static gint idle_exit_func(gpointer data) |
f7f27309 |
1225 | { |
ee5e66d8 |
1226 | struct gui_data *inst = (struct gui_data *)data; |
74aca06d |
1227 | int exitcode; |
a0e16eb1 |
1228 | |
3f935d5b |
1229 | if (!inst->exited && |
1230 | (exitcode = inst->back->exitcode(inst->backhandle)) >= 0) { |
74aca06d |
1231 | inst->exited = TRUE; |
1232 | if (inst->cfg.close_on_exit == FORCE_ON || |
1233 | (inst->cfg.close_on_exit == AUTO && exitcode == 0)) |
2425184b |
1234 | gtk_main_quit(); /* just go */ |
e3be8de5 |
1235 | if (inst->ldisc) { |
1236 | ldisc_free(inst->ldisc); |
1237 | inst->ldisc = NULL; |
1238 | } |
1239 | if (inst->back) { |
1240 | inst->back->free(inst->backhandle); |
1241 | inst->backhandle = NULL; |
1242 | inst->back = NULL; |
c1190260 |
1243 | term_provide_resize_fn(inst->term, NULL, NULL); |
e3be8de5 |
1244 | update_specials_menu(inst); |
1245 | } |
1246 | gtk_widget_show(inst->restartitem); |
a0e16eb1 |
1247 | } |
ee5e66d8 |
1248 | |
1249 | gtk_idle_remove(inst->term_exit_idle_id); |
1250 | return TRUE; |
1251 | } |
1252 | |
1253 | void notify_remote_exit(void *frontend) |
1254 | { |
1255 | struct gui_data *inst = (struct gui_data *)frontend; |
1256 | |
1257 | inst->term_exit_idle_id = gtk_idle_add(idle_exit_func, inst); |
39934deb |
1258 | } |
f7f27309 |
1259 | |
39934deb |
1260 | static gint timer_trigger(gpointer data) |
1261 | { |
1262 | long now = GPOINTER_TO_INT(data); |
1263 | long next; |
1264 | long ticks; |
1265 | |
1266 | if (run_timers(now, &next)) { |
1267 | ticks = next - GETTICKCOUNT(); |
1268 | timer_id = gtk_timeout_add(ticks > 0 ? ticks : 1, timer_trigger, |
1269 | GINT_TO_POINTER(next)); |
1270 | } |
1271 | |
1272 | /* |
1273 | * Never let a timer resume. If we need another one, we've |
1274 | * asked for it explicitly above. |
1275 | */ |
1276 | return FALSE; |
1277 | } |
1278 | |
1279 | void timer_change_notify(long next) |
1280 | { |
1281 | long ticks; |
1282 | |
1283 | if (timer_id) |
1284 | gtk_timeout_remove(timer_id); |
1285 | |
1286 | ticks = next - GETTICKCOUNT(); |
1287 | if (ticks <= 0) |
1288 | ticks = 1; /* just in case */ |
1289 | |
1290 | timer_id = gtk_timeout_add(ticks, timer_trigger, |
1291 | GINT_TO_POINTER(next)); |
f7f27309 |
1292 | } |
1293 | |
74aca06d |
1294 | void fd_input_func(gpointer data, gint sourcefd, GdkInputCondition condition) |
054d8535 |
1295 | { |
3f935d5b |
1296 | /* |
1297 | * We must process exceptional notifications before ordinary |
1298 | * readability ones, or we may go straight past the urgent |
1299 | * marker. |
1300 | */ |
1301 | if (condition & GDK_INPUT_EXCEPTION) |
1302 | select_result(sourcefd, 4); |
1303 | if (condition & GDK_INPUT_READ) |
1304 | select_result(sourcefd, 1); |
1305 | if (condition & GDK_INPUT_WRITE) |
1306 | select_result(sourcefd, 2); |
054d8535 |
1307 | } |
1308 | |
f7f27309 |
1309 | void destroy(GtkWidget *widget, gpointer data) |
1310 | { |
1311 | gtk_main_quit(); |
1312 | } |
1313 | |
1314 | gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data) |
1315 | { |
a8327734 |
1316 | struct gui_data *inst = (struct gui_data *)data; |
1cff1320 |
1317 | term_set_focus(inst->term, event->in); |
5def7522 |
1318 | term_update(inst->term); |
a8327734 |
1319 | show_mouseptr(inst, 1); |
1709795f |
1320 | return FALSE; |
1321 | } |
1322 | |
755e0524 |
1323 | void set_busy_status(void *frontend, int status) |
1324 | { |
1325 | struct gui_data *inst = (struct gui_data *)frontend; |
1326 | inst->busy_status = status; |
1327 | update_mouseptr(inst); |
1328 | } |
1329 | |
1709795f |
1330 | /* |
1331 | * set or clear the "raw mouse message" mode |
1332 | */ |
a8327734 |
1333 | void set_raw_mouse_mode(void *frontend, int activate) |
1709795f |
1334 | { |
a8327734 |
1335 | struct gui_data *inst = (struct gui_data *)frontend; |
3ea863a3 |
1336 | activate = activate && !inst->cfg.no_mouse_rep; |
d64838e1 |
1337 | send_raw_mouse = activate; |
755e0524 |
1338 | update_mouseptr(inst); |
1709795f |
1339 | } |
1340 | |
a8327734 |
1341 | void request_resize(void *frontend, int w, int h) |
1709795f |
1342 | { |
a8327734 |
1343 | struct gui_data *inst = (struct gui_data *)frontend; |
51b13830 |
1344 | int large_x, large_y; |
1345 | int offset_x, offset_y; |
1346 | int area_x, area_y; |
1347 | GtkRequisition inner, outer; |
1348 | |
1349 | /* |
1350 | * This is a heinous hack dreamed up by the gnome-terminal |
1351 | * people to get around a limitation in gtk. The problem is |
1352 | * that in order to set the size correctly we really need to be |
1353 | * calling gtk_window_resize - but that needs to know the size |
1354 | * of the _whole window_, not the drawing area. So what we do |
1355 | * is to set an artificially huge size request on the drawing |
1356 | * area, recompute the resulting size request on the window, |
1357 | * and look at the difference between the two. That gives us |
1358 | * the x and y offsets we need to translate drawing area size |
1359 | * into window size for real, and then we call |
1360 | * gtk_window_resize. |
1361 | */ |
1362 | |
1363 | /* |
1364 | * We start by retrieving the current size of the whole window. |
1365 | * Adding a bit to _that_ will give us a value we can use as a |
1366 | * bogus size request which guarantees to be bigger than the |
1367 | * current size of the drawing area. |
1368 | */ |
a8327734 |
1369 | get_window_pixels(inst, &large_x, &large_y); |
51b13830 |
1370 | large_x += 32; |
1371 | large_y += 32; |
1372 | |
1373 | #if GTK_CHECK_VERSION(2,0,0) |
1374 | gtk_widget_set_size_request(inst->area, large_x, large_y); |
1375 | #else |
1376 | gtk_widget_set_usize(inst->area, large_x, large_y); |
1377 | #endif |
1378 | gtk_widget_size_request(inst->area, &inner); |
1379 | gtk_widget_size_request(inst->window, &outer); |
1380 | |
1381 | offset_x = outer.width - inner.width; |
1382 | offset_y = outer.height - inner.height; |
1383 | |
3ea863a3 |
1384 | area_x = inst->font_width * w + 2*inst->cfg.window_border; |
1385 | area_y = inst->font_height * h + 2*inst->cfg.window_border; |
51b13830 |
1386 | |
1387 | /* |
1388 | * Now we must set the size request on the drawing area back to |
1389 | * something sensible before we commit the real resize. Best |
1390 | * way to do this, I think, is to set it to what the size is |
1391 | * really going to end up being. |
1392 | */ |
1393 | #if GTK_CHECK_VERSION(2,0,0) |
1394 | gtk_widget_set_size_request(inst->area, area_x, area_y); |
f160b7b8 |
1395 | gtk_window_resize(GTK_WINDOW(inst->window), |
1396 | area_x + offset_x, area_y + offset_y); |
51b13830 |
1397 | #else |
1398 | gtk_widget_set_usize(inst->area, area_x, area_y); |
56b9b9a7 |
1399 | gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), area_x, area_y); |
f160b7b8 |
1400 | /* |
1401 | * I can no longer remember what this call to |
1402 | * gtk_container_dequeue_resize_handler is for. It was |
1403 | * introduced in r3092 with no comment, and the commit log |
1404 | * message was uninformative. I'm _guessing_ its purpose is to |
1405 | * prevent gratuitous resize processing on the window given |
1406 | * that we're about to resize it anyway, but I have no idea |
1407 | * why that's so incredibly vital. |
1408 | * |
1409 | * I've tried removing the call, and nothing seems to go |
1410 | * wrong. I've backtracked to r3092 and tried removing the |
1411 | * call there, and still nothing goes wrong. So I'm going to |
1412 | * adopt the working hypothesis that it's superfluous; I won't |
1413 | * actually remove it from the GTK 1.2 code, but I won't |
1414 | * attempt to replicate its functionality in the GTK 2 code |
1415 | * above. |
1416 | */ |
56b9b9a7 |
1417 | gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst->window)); |
51b13830 |
1418 | gdk_window_resize(inst->window->window, |
1419 | area_x + offset_x, area_y + offset_y); |
1420 | #endif |
1709795f |
1421 | } |
1422 | |
a8327734 |
1423 | static void real_palette_set(struct gui_data *inst, int n, int r, int g, int b) |
26b8e7cc |
1424 | { |
1425 | gboolean success[1]; |
1426 | |
1427 | inst->cols[n].red = r * 0x0101; |
1428 | inst->cols[n].green = g * 0x0101; |
1429 | inst->cols[n].blue = b * 0x0101; |
1430 | |
56b9b9a7 |
1431 | gdk_colormap_free_colors(inst->colmap, inst->cols + n, 1); |
26b8e7cc |
1432 | gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1, |
1eea099f |
1433 | FALSE, TRUE, success); |
26b8e7cc |
1434 | if (!success[0]) |
a4e8ffb0 |
1435 | g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname, |
26b8e7cc |
1436 | n, r, g, b); |
1437 | } |
1438 | |
a8327734 |
1439 | void set_window_background(struct gui_data *inst) |
7428c509 |
1440 | { |
1441 | if (inst->area && inst->area->window) |
ee077769 |
1442 | gdk_window_set_background(inst->area->window, &inst->cols[258]); |
7428c509 |
1443 | if (inst->window && inst->window->window) |
ee077769 |
1444 | gdk_window_set_background(inst->window->window, &inst->cols[258]); |
7428c509 |
1445 | } |
1446 | |
a8327734 |
1447 | void palette_set(void *frontend, int n, int r, int g, int b) |
1709795f |
1448 | { |
a8327734 |
1449 | struct gui_data *inst = (struct gui_data *)frontend; |
cecb13f6 |
1450 | if (n >= 16) |
1451 | n += 256 - 16; |
1452 | if (n > NALLCOLOURS) |
1453 | return; |
1454 | real_palette_set(inst, n, r, g, b); |
8d3b4d68 |
1455 | if (n == 258) { |
1456 | /* Default Background changed. Ensure space between text area and |
1457 | * window border is redrawn */ |
a8327734 |
1458 | set_window_background(inst); |
8d3b4d68 |
1459 | draw_backing_rect(inst); |
1460 | gtk_widget_queue_draw(inst->area); |
1461 | } |
1709795f |
1462 | } |
26b8e7cc |
1463 | |
a8327734 |
1464 | void palette_reset(void *frontend) |
1709795f |
1465 | { |
a8327734 |
1466 | struct gui_data *inst = (struct gui_data *)frontend; |
3ea863a3 |
1467 | /* This maps colour indices in inst->cfg to those used in inst->cols. */ |
26b8e7cc |
1468 | static const int ww[] = { |
cecb13f6 |
1469 | 256, 257, 258, 259, 260, 261, |
1470 | 0, 8, 1, 9, 2, 10, 3, 11, |
1471 | 4, 12, 5, 13, 6, 14, 7, 15 |
26b8e7cc |
1472 | }; |
cecb13f6 |
1473 | gboolean success[NALLCOLOURS]; |
26b8e7cc |
1474 | int i; |
1475 | |
cecb13f6 |
1476 | assert(lenof(ww) == NCFGCOLOURS); |
26b8e7cc |
1477 | |
1478 | if (!inst->colmap) { |
1479 | inst->colmap = gdk_colormap_get_system(); |
1480 | } else { |
cecb13f6 |
1481 | gdk_colormap_free_colors(inst->colmap, inst->cols, NALLCOLOURS); |
26b8e7cc |
1482 | } |
1483 | |
cecb13f6 |
1484 | for (i = 0; i < NCFGCOLOURS; i++) { |
1485 | inst->cols[ww[i]].red = inst->cfg.colours[i][0] * 0x0101; |
1486 | inst->cols[ww[i]].green = inst->cfg.colours[i][1] * 0x0101; |
1487 | inst->cols[ww[i]].blue = inst->cfg.colours[i][2] * 0x0101; |
26b8e7cc |
1488 | } |
1489 | |
cecb13f6 |
1490 | for (i = 0; i < NEXTCOLOURS; i++) { |
1491 | if (i < 216) { |
1492 | int r = i / 36, g = (i / 6) % 6, b = i % 6; |
8098d585 |
1493 | inst->cols[i+16].red = r ? r * 0x2828 + 0x3737 : 0; |
1494 | inst->cols[i+16].green = g ? g * 0x2828 + 0x3737 : 0; |
2733b5cb |
1495 | inst->cols[i+16].blue = b ? b * 0x2828 + 0x3737 : 0; |
cecb13f6 |
1496 | } else { |
1497 | int shade = i - 216; |
8098d585 |
1498 | shade = shade * 0x0a0a + 0x0808; |
cecb13f6 |
1499 | inst->cols[i+16].red = inst->cols[i+16].green = |
1500 | inst->cols[i+16].blue = shade; |
1501 | } |
1502 | } |
1503 | |
1504 | gdk_colormap_alloc_colors(inst->colmap, inst->cols, NALLCOLOURS, |
35914df1 |
1505 | FALSE, TRUE, success); |
cecb13f6 |
1506 | for (i = 0; i < NALLCOLOURS; i++) { |
26b8e7cc |
1507 | if (!success[i]) |
a4e8ffb0 |
1508 | g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", |
1509 | appname, i, inst->cfg.colours[i][0], |
1510 | inst->cfg.colours[i][1], inst->cfg.colours[i][2]); |
26b8e7cc |
1511 | } |
7428c509 |
1512 | |
8d3b4d68 |
1513 | /* Since Default Background may have changed, ensure that space |
1514 | * between text area and window border is refreshed. */ |
a8327734 |
1515 | set_window_background(inst); |
f160b7b8 |
1516 | if (inst->area && inst->area->window) { |
8d3b4d68 |
1517 | draw_backing_rect(inst); |
1518 | gtk_widget_queue_draw(inst->area); |
1519 | } |
1709795f |
1520 | } |
1521 | |
3aac0d35 |
1522 | /* Ensure that all the cut buffers exist - according to the ICCCM, we must |
1523 | * do this before we start using cut buffers. |
1524 | */ |
1525 | void init_cutbuffers() |
1526 | { |
8a9977e5 |
1527 | unsigned char empty[] = ""; |
3aac0d35 |
1528 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1529 | XA_CUT_BUFFER0, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1530 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1531 | XA_CUT_BUFFER1, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1532 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1533 | XA_CUT_BUFFER2, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1534 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1535 | XA_CUT_BUFFER3, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1536 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1537 | XA_CUT_BUFFER4, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1538 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1539 | XA_CUT_BUFFER5, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1540 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1541 | XA_CUT_BUFFER6, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1542 | XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(), |
8a9977e5 |
1543 | XA_CUT_BUFFER7, XA_STRING, 8, PropModeAppend, empty, 0); |
3aac0d35 |
1544 | } |
1545 | |
1546 | /* Store the data in a cut-buffer. */ |
1547 | void store_cutbuffer(char * ptr, int len) |
1548 | { |
1549 | /* ICCCM says we must rotate the buffers before storing to buffer 0. */ |
1550 | XRotateBuffers(GDK_DISPLAY(), 1); |
1551 | XStoreBytes(GDK_DISPLAY(), ptr, len); |
1552 | } |
1553 | |
1554 | /* Retrieve data from a cut-buffer. |
1555 | * Returned data needs to be freed with XFree(). |
1556 | */ |
1557 | char * retrieve_cutbuffer(int * nbytes) |
1558 | { |
1559 | char * ptr; |
1560 | ptr = XFetchBytes(GDK_DISPLAY(), nbytes); |
05971030 |
1561 | if (*nbytes <= 0 && ptr != 0) { |
3aac0d35 |
1562 | XFree(ptr); |
1563 | ptr = 0; |
1564 | } |
1565 | return ptr; |
1566 | } |
1567 | |
c83c79bd |
1568 | void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect) |
1709795f |
1569 | { |
a8327734 |
1570 | struct gui_data *inst = (struct gui_data *)frontend; |
e6346999 |
1571 | if (inst->pasteout_data) |
1572 | sfree(inst->pasteout_data); |
48f1bc44 |
1573 | if (inst->pasteout_data_ctext) |
1574 | sfree(inst->pasteout_data_ctext); |
2dc6356a |
1575 | if (inst->pasteout_data_utf8) |
1576 | sfree(inst->pasteout_data_utf8); |
1577 | |
facd762c |
1578 | /* |
48f1bc44 |
1579 | * Set up UTF-8 and compound text paste data. This only happens |
1580 | * if we aren't in direct-to-font mode using the D800 hack. |
facd762c |
1581 | */ |
085f4a68 |
1582 | if (!inst->direct_to_font) { |
2dc6356a |
1583 | wchar_t *tmp = data; |
1584 | int tmplen = len; |
48f1bc44 |
1585 | XTextProperty tp; |
1586 | char *list[1]; |
facd762c |
1587 | |
3d88e64d |
1588 | inst->pasteout_data_utf8 = snewn(len*6, char); |
facd762c |
1589 | inst->pasteout_data_utf8_len = len*6; |
2dc6356a |
1590 | inst->pasteout_data_utf8_len = |
1591 | charset_from_unicode(&tmp, &tmplen, inst->pasteout_data_utf8, |
1592 | inst->pasteout_data_utf8_len, |
1593 | CS_UTF8, NULL, NULL, 0); |
085f4a68 |
1594 | if (inst->pasteout_data_utf8_len == 0) { |
1595 | sfree(inst->pasteout_data_utf8); |
1596 | inst->pasteout_data_utf8 = NULL; |
1597 | } else { |
1598 | inst->pasteout_data_utf8 = |
3d88e64d |
1599 | sresize(inst->pasteout_data_utf8, |
48f1bc44 |
1600 | inst->pasteout_data_utf8_len + 1, char); |
1601 | inst->pasteout_data_utf8[inst->pasteout_data_utf8_len] = '\0'; |
1602 | } |
1603 | |
1604 | /* |
1605 | * Now let Xlib convert our UTF-8 data into compound text. |
1606 | */ |
1607 | list[0] = inst->pasteout_data_utf8; |
1608 | if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list, 1, |
1609 | XCompoundTextStyle, &tp) == 0) { |
1610 | inst->pasteout_data_ctext = snewn(tp.nitems+1, char); |
1611 | memcpy(inst->pasteout_data_ctext, tp.value, tp.nitems); |
1612 | inst->pasteout_data_ctext_len = tp.nitems; |
1613 | XFree(tp.value); |
d1929fa4 |
1614 | } else { |
1615 | inst->pasteout_data_ctext = NULL; |
1616 | inst->pasteout_data_ctext_len = 0; |
1617 | } |
facd762c |
1618 | } else { |
1619 | inst->pasteout_data_utf8 = NULL; |
1620 | inst->pasteout_data_utf8_len = 0; |
48f1bc44 |
1621 | inst->pasteout_data_ctext = NULL; |
1622 | inst->pasteout_data_ctext_len = 0; |
2dc6356a |
1623 | } |
1624 | |
3d88e64d |
1625 | inst->pasteout_data = snewn(len*6, char); |
085f4a68 |
1626 | inst->pasteout_data_len = len*6; |
21d2b241 |
1627 | inst->pasteout_data_len = wc_to_mb(inst->ucsdata.line_codepage, 0, |
1628 | data, len, inst->pasteout_data, |
1629 | inst->pasteout_data_len, |
1630 | NULL, NULL, NULL); |
085f4a68 |
1631 | if (inst->pasteout_data_len == 0) { |
1632 | sfree(inst->pasteout_data); |
1633 | inst->pasteout_data = NULL; |
1634 | } else { |
1635 | inst->pasteout_data = |
3d88e64d |
1636 | sresize(inst->pasteout_data, inst->pasteout_data_len, char); |
085f4a68 |
1637 | } |
e6346999 |
1638 | |
3aac0d35 |
1639 | store_cutbuffer(inst->pasteout_data, inst->pasteout_data_len); |
1640 | |
e6346999 |
1641 | if (gtk_selection_owner_set(inst->area, GDK_SELECTION_PRIMARY, |
d0a039b1 |
1642 | inst->input_event_time)) { |
d0c8fc87 |
1643 | #if GTK_CHECK_VERSION(2,0,0) |
f56d3125 |
1644 | gtk_selection_clear_targets(inst->area, GDK_SELECTION_PRIMARY); |
d0c8fc87 |
1645 | #endif |
e6346999 |
1646 | gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY, |
1647 | GDK_SELECTION_TYPE_STRING, 1); |
48f1bc44 |
1648 | if (inst->pasteout_data_ctext) |
1649 | gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY, |
1650 | compound_text_atom, 1); |
facd762c |
1651 | if (inst->pasteout_data_utf8) |
1652 | gtk_selection_add_target(inst->area, GDK_SELECTION_PRIMARY, |
8c161662 |
1653 | utf8_string_atom, 1); |
e6346999 |
1654 | } |
c076966d |
1655 | |
1656 | if (must_deselect) |
1657 | term_deselect(inst->term); |
e6346999 |
1658 | } |
1659 | |
1660 | void selection_get(GtkWidget *widget, GtkSelectionData *seldata, |
1661 | guint info, guint time_stamp, gpointer data) |
1662 | { |
a8327734 |
1663 | struct gui_data *inst = (struct gui_data *)data; |
8c161662 |
1664 | if (seldata->target == utf8_string_atom) |
2dc6356a |
1665 | gtk_selection_data_set(seldata, seldata->target, 8, |
8a9977e5 |
1666 | (unsigned char *)inst->pasteout_data_utf8, |
2dc6356a |
1667 | inst->pasteout_data_utf8_len); |
48f1bc44 |
1668 | else if (seldata->target == compound_text_atom) |
1669 | gtk_selection_data_set(seldata, seldata->target, 8, |
8a9977e5 |
1670 | (unsigned char *)inst->pasteout_data_ctext, |
48f1bc44 |
1671 | inst->pasteout_data_ctext_len); |
2dc6356a |
1672 | else |
1673 | gtk_selection_data_set(seldata, seldata->target, 8, |
8a9977e5 |
1674 | (unsigned char *)inst->pasteout_data, |
1675 | inst->pasteout_data_len); |
e6346999 |
1676 | } |
1677 | |
1678 | gint selection_clear(GtkWidget *widget, GdkEventSelection *seldata, |
1679 | gpointer data) |
1680 | { |
a8327734 |
1681 | struct gui_data *inst = (struct gui_data *)data; |
d0a039b1 |
1682 | |
5def7522 |
1683 | term_deselect(inst->term); |
e6346999 |
1684 | if (inst->pasteout_data) |
1685 | sfree(inst->pasteout_data); |
48f1bc44 |
1686 | if (inst->pasteout_data_ctext) |
1687 | sfree(inst->pasteout_data_ctext); |
2dc6356a |
1688 | if (inst->pasteout_data_utf8) |
1689 | sfree(inst->pasteout_data_utf8); |
e6346999 |
1690 | inst->pasteout_data = NULL; |
1691 | inst->pasteout_data_len = 0; |
48f1bc44 |
1692 | inst->pasteout_data_ctext = NULL; |
1693 | inst->pasteout_data_ctext_len = 0; |
2dc6356a |
1694 | inst->pasteout_data_utf8 = NULL; |
1695 | inst->pasteout_data_utf8_len = 0; |
e6346999 |
1696 | return TRUE; |
1697 | } |
1698 | |
a8327734 |
1699 | void request_paste(void *frontend) |
e6346999 |
1700 | { |
a8327734 |
1701 | struct gui_data *inst = (struct gui_data *)frontend; |
e6346999 |
1702 | /* |
1703 | * In Unix, pasting is asynchronous: all we can do at the |
1704 | * moment is to call gtk_selection_convert(), and when the data |
1705 | * comes back _then_ we can call term_do_paste(). |
1706 | */ |
2dc6356a |
1707 | |
085f4a68 |
1708 | if (!inst->direct_to_font) { |
facd762c |
1709 | /* |
1710 | * First we attempt to retrieve the selection as a UTF-8 |
1711 | * string (which we will convert to the correct code page |
1712 | * before sending to the session, of course). If that |
1713 | * fails, selection_received() will be informed and will |
1714 | * fall back to an ordinary string. |
1715 | */ |
1716 | gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY, |
d0a039b1 |
1717 | utf8_string_atom, |
1718 | inst->input_event_time); |
facd762c |
1719 | } else { |
1720 | /* |
1721 | * If we're in direct-to-font mode, we disable UTF-8 |
1722 | * pasting, and go straight to ordinary string data. |
1723 | */ |
1724 | gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY, |
d0a039b1 |
1725 | GDK_SELECTION_TYPE_STRING, |
1726 | inst->input_event_time); |
facd762c |
1727 | } |
e6346999 |
1728 | } |
1729 | |
0f660c8f |
1730 | gint idle_paste_func(gpointer data); /* forward ref */ |
1731 | |
e6346999 |
1732 | void selection_received(GtkWidget *widget, GtkSelectionData *seldata, |
27651070 |
1733 | guint time, gpointer data) |
e6346999 |
1734 | { |
a8327734 |
1735 | struct gui_data *inst = (struct gui_data *)data; |
48f1bc44 |
1736 | XTextProperty tp; |
1737 | char **list; |
1738 | char *text; |
1739 | int length, count, ret; |
1740 | int free_list_required = 0; |
3aac0d35 |
1741 | int free_required = 0; |
48f1bc44 |
1742 | int charset; |
a8327734 |
1743 | |
8c161662 |
1744 | if (seldata->target == utf8_string_atom && seldata->length <= 0) { |
2dc6356a |
1745 | /* |
eaa07514 |
1746 | * Failed to get a UTF-8 selection string. Try compound |
1747 | * text next. |
1748 | */ |
1749 | gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY, |
d0a039b1 |
1750 | compound_text_atom, |
1751 | inst->input_event_time); |
eaa07514 |
1752 | return; |
1753 | } |
1754 | |
1755 | if (seldata->target == compound_text_atom && seldata->length <= 0) { |
1756 | /* |
1757 | * Failed to get UTF-8 or compound text. Try an ordinary |
2dc6356a |
1758 | * string. |
1759 | */ |
1760 | gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY, |
d0a039b1 |
1761 | GDK_SELECTION_TYPE_STRING, |
1762 | inst->input_event_time); |
2dc6356a |
1763 | return; |
1764 | } |
1765 | |
1766 | /* |
3aac0d35 |
1767 | * If we have data, but it's not of a type we can deal with, |
1768 | * we have to ignore the data. |
2dc6356a |
1769 | */ |
3aac0d35 |
1770 | if (seldata->length > 0 && |
1771 | seldata->type != GDK_SELECTION_TYPE_STRING && |
1772 | seldata->type != compound_text_atom && |
1773 | seldata->type != utf8_string_atom) |
1774 | return; |
48f1bc44 |
1775 | |
1776 | /* |
3aac0d35 |
1777 | * If we have no data, try looking in a cut buffer. |
1778 | */ |
1779 | if (seldata->length <= 0) { |
1780 | text = retrieve_cutbuffer(&length); |
1781 | if (length == 0) |
1782 | return; |
1783 | /* Xterm is rumoured to expect Latin-1, though I havn't checked the |
1784 | * source, so use that as a de-facto standard. */ |
1785 | charset = CS_ISO8859_1; |
1786 | free_required = 1; |
1787 | } else { |
76129c71 |
1788 | /* |
1789 | * Convert COMPOUND_TEXT into UTF-8. |
1790 | */ |
1791 | if (seldata->type == compound_text_atom) { |
1792 | tp.value = seldata->data; |
1793 | tp.encoding = (Atom) seldata->type; |
1794 | tp.format = seldata->format; |
1795 | tp.nitems = seldata->length; |
1796 | ret = Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp, |
1797 | &list, &count); |
1798 | if (ret != 0 || count != 1) { |
1799 | /* |
1800 | * Compound text failed; fall back to STRING. |
1801 | */ |
1802 | gtk_selection_convert(inst->area, GDK_SELECTION_PRIMARY, |
1803 | GDK_SELECTION_TYPE_STRING, |
1804 | inst->input_event_time); |
1805 | return; |
1806 | } |
1807 | text = list[0]; |
1808 | length = strlen(list[0]); |
1809 | charset = CS_UTF8; |
1810 | free_list_required = 1; |
1811 | } else { |
1812 | text = (char *)seldata->data; |
1813 | length = seldata->length; |
1814 | charset = (seldata->type == utf8_string_atom ? |
1815 | CS_UTF8 : inst->ucsdata.line_codepage); |
48f1bc44 |
1816 | } |
3aac0d35 |
1817 | } |
48f1bc44 |
1818 | |
e6346999 |
1819 | if (inst->pastein_data) |
1820 | sfree(inst->pastein_data); |
1821 | |
48f1bc44 |
1822 | inst->pastein_data = snewn(length, wchar_t); |
1823 | inst->pastein_data_len = length; |
2dc6356a |
1824 | inst->pastein_data_len = |
48f1bc44 |
1825 | mb_to_wc(charset, 0, text, length, |
2dc6356a |
1826 | inst->pastein_data, inst->pastein_data_len); |
e6346999 |
1827 | |
5def7522 |
1828 | term_do_paste(inst->term); |
0f660c8f |
1829 | |
5def7522 |
1830 | if (term_paste_pending(inst->term)) |
0f660c8f |
1831 | inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst); |
48f1bc44 |
1832 | |
1833 | if (free_list_required) |
1834 | XFreeStringList(list); |
3aac0d35 |
1835 | if (free_required) |
1836 | XFree(text); |
0f660c8f |
1837 | } |
1838 | |
1839 | gint idle_paste_func(gpointer data) |
1840 | { |
1841 | struct gui_data *inst = (struct gui_data *)data; |
1842 | |
5def7522 |
1843 | if (term_paste_pending(inst->term)) |
1844 | term_paste(inst->term); |
0f660c8f |
1845 | else |
1846 | gtk_idle_remove(inst->term_paste_idle_id); |
1847 | |
1848 | return TRUE; |
1709795f |
1849 | } |
1850 | |
0f660c8f |
1851 | |
a8327734 |
1852 | void get_clip(void *frontend, wchar_t ** p, int *len) |
1709795f |
1853 | { |
a8327734 |
1854 | struct gui_data *inst = (struct gui_data *)frontend; |
1855 | |
1709795f |
1856 | if (p) { |
e6346999 |
1857 | *p = inst->pastein_data; |
1858 | *len = inst->pastein_data_len; |
1709795f |
1859 | } |
1860 | } |
1861 | |
6e785e0c |
1862 | static void set_window_titles(struct gui_data *inst) |
1863 | { |
1864 | /* |
1865 | * We must always call set_icon_name after calling set_title, |
1866 | * since set_title will write both names. Irritating, but such |
1867 | * is life. |
1868 | */ |
1869 | gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle); |
1870 | if (!inst->cfg.win_name_always) |
1871 | gdk_window_set_icon_name(inst->window->window, inst->icontitle); |
1872 | } |
1873 | |
a8327734 |
1874 | void set_title(void *frontend, char *title) |
1709795f |
1875 | { |
a8327734 |
1876 | struct gui_data *inst = (struct gui_data *)frontend; |
12d200b1 |
1877 | strncpy(inst->wintitle, title, lenof(inst->wintitle)); |
1878 | inst->wintitle[lenof(inst->wintitle)-1] = '\0'; |
6e785e0c |
1879 | set_window_titles(inst); |
1709795f |
1880 | } |
1881 | |
a8327734 |
1882 | void set_icon(void *frontend, char *title) |
1709795f |
1883 | { |
a8327734 |
1884 | struct gui_data *inst = (struct gui_data *)frontend; |
16891265 |
1885 | strncpy(inst->icontitle, title, lenof(inst->icontitle)); |
1886 | inst->icontitle[lenof(inst->icontitle)-1] = '\0'; |
6e785e0c |
1887 | set_window_titles(inst); |
1709795f |
1888 | } |
1889 | |
a8327734 |
1890 | void set_sbar(void *frontend, int total, int start, int page) |
1709795f |
1891 | { |
a8327734 |
1892 | struct gui_data *inst = (struct gui_data *)frontend; |
3ea863a3 |
1893 | if (!inst->cfg.scrollbar) |
330f49be |
1894 | return; |
6a5e84dd |
1895 | inst->sbar_adjust->lower = 0; |
1896 | inst->sbar_adjust->upper = total; |
1897 | inst->sbar_adjust->value = start; |
1898 | inst->sbar_adjust->page_size = page; |
1899 | inst->sbar_adjust->step_increment = 1; |
1900 | inst->sbar_adjust->page_increment = page/2; |
88e6b9ca |
1901 | inst->ignore_sbar = TRUE; |
6a5e84dd |
1902 | gtk_adjustment_changed(inst->sbar_adjust); |
88e6b9ca |
1903 | inst->ignore_sbar = FALSE; |
6a5e84dd |
1904 | } |
1905 | |
1906 | void scrollbar_moved(GtkAdjustment *adj, gpointer data) |
1907 | { |
a8327734 |
1908 | struct gui_data *inst = (struct gui_data *)data; |
1909 | |
3ea863a3 |
1910 | if (!inst->cfg.scrollbar) |
330f49be |
1911 | return; |
88e6b9ca |
1912 | if (!inst->ignore_sbar) |
5def7522 |
1913 | term_scroll(inst->term, 1, (int)adj->value); |
1709795f |
1914 | } |
1915 | |
a8327734 |
1916 | void sys_cursor(void *frontend, int x, int y) |
1709795f |
1917 | { |
1918 | /* |
1919 | * This is meaningless under X. |
1920 | */ |
1921 | } |
1922 | |
0ce89525 |
1923 | /* |
1924 | * This is still called when mode==BELL_VISUAL, even though the |
1925 | * visual bell is handled entirely within terminal.c, because we |
1926 | * may want to perform additional actions on any kind of bell (for |
1927 | * example, taskbar flashing in Windows). |
1928 | */ |
860a34f8 |
1929 | void do_beep(void *frontend, int mode) |
1709795f |
1930 | { |
c683b25a |
1931 | if (mode == BELL_DEFAULT) |
0ce89525 |
1932 | gdk_beep(); |
1709795f |
1933 | } |
1934 | |
2102eb8a |
1935 | int char_width(Context ctx, int uc) |
1709795f |
1936 | { |
1937 | /* |
1938 | * Under X, any fixed-width font really _is_ fixed-width. |
1939 | * Double-width characters will be dealt with using a separate |
1940 | * font. For the moment we can simply return 1. |
f160b7b8 |
1941 | * |
1942 | * FIXME: but is that also true of Pango? |
1709795f |
1943 | */ |
1944 | return 1; |
1945 | } |
1946 | |
a8327734 |
1947 | Context get_ctx(void *frontend) |
1709795f |
1948 | { |
a8327734 |
1949 | struct gui_data *inst = (struct gui_data *)frontend; |
1950 | struct draw_ctx *dctx; |
1951 | |
d64838e1 |
1952 | if (!inst->area->window) |
1953 | return NULL; |
a8327734 |
1954 | |
3d88e64d |
1955 | dctx = snew(struct draw_ctx); |
a8327734 |
1956 | dctx->inst = inst; |
1957 | dctx->gc = gdk_gc_new(inst->area->window); |
1958 | return dctx; |
1709795f |
1959 | } |
1960 | |
1961 | void free_ctx(Context ctx) |
1962 | { |
a8327734 |
1963 | struct draw_ctx *dctx = (struct draw_ctx *)ctx; |
1964 | /* struct gui_data *inst = dctx->inst; */ |
1965 | GdkGC *gc = dctx->gc; |
1709795f |
1966 | gdk_gc_unref(gc); |
a8327734 |
1967 | sfree(dctx); |
1709795f |
1968 | } |
1969 | |
1970 | /* |
1971 | * Draw a line of text in the window, at given character |
1972 | * coordinates, in given attributes. |
1973 | * |
1974 | * We are allowed to fiddle with the contents of `text'. |
1975 | */ |
36566009 |
1976 | void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len, |
1a675941 |
1977 | unsigned long attr, int lattr) |
1709795f |
1978 | { |
a8327734 |
1979 | struct draw_ctx *dctx = (struct draw_ctx *)ctx; |
1980 | struct gui_data *inst = dctx->inst; |
1981 | GdkGC *gc = dctx->gc; |
c6958dfe |
1982 | int ncombining, combining; |
f160b7b8 |
1983 | int nfg, nbg, t, fontid, shadow, rlen, widefactor, bold; |
bb859601 |
1984 | int monochrome = gtk_widget_get_visual(inst->area)->depth == 1; |
d64838e1 |
1985 | |
c6958dfe |
1986 | if (attr & TATTR_COMBINING) { |
1987 | ncombining = len; |
1988 | len = 1; |
1989 | } else |
1990 | ncombining = 1; |
1991 | |
6800a371 |
1992 | nfg = ((monochrome ? ATTR_DEFFG : (attr & ATTR_FGMASK)) >> ATTR_FGSHIFT); |
1993 | nbg = ((monochrome ? ATTR_DEFBG : (attr & ATTR_BGMASK)) >> ATTR_BGSHIFT); |
f579cda8 |
1994 | if (!!(attr & ATTR_REVERSE) ^ (monochrome && (attr & TATTR_ACTCURS))) { |
d64838e1 |
1995 | t = nfg; |
1996 | nfg = nbg; |
1997 | nbg = t; |
1998 | } |
cecb13f6 |
1999 | if (inst->cfg.bold_colour && (attr & ATTR_BOLD)) { |
2000 | if (nfg < 16) nfg |= 8; |
2001 | else if (nfg >= 256) nfg |= 1; |
2002 | } |
2003 | if (inst->cfg.bold_colour && (attr & ATTR_BLINK)) { |
2004 | if (nbg < 16) nbg |= 8; |
2005 | else if (nbg >= 256) nbg |= 1; |
2006 | } |
f579cda8 |
2007 | if ((attr & TATTR_ACTCURS) && !monochrome) { |
cecb13f6 |
2008 | nfg = 260; |
2009 | nbg = 261; |
d64838e1 |
2010 | } |
2011 | |
5bf50ab2 |
2012 | fontid = shadow = 0; |
006238cb |
2013 | |
2014 | if (attr & ATTR_WIDE) { |
2015 | widefactor = 2; |
2016 | fontid |= 2; |
2017 | } else { |
2018 | widefactor = 1; |
2019 | } |
2020 | |
3ea863a3 |
2021 | if ((attr & ATTR_BOLD) && !inst->cfg.bold_colour) { |
f160b7b8 |
2022 | bold = 1; |
2023 | fontid |= 1; |
2024 | } else { |
2025 | bold = 0; |
2026 | } |
2027 | |
2028 | if (!inst->fonts[fontid]) { |
2029 | int i; |
2030 | /* |
2031 | * Fall back through font ids with subsets of this one's |
2032 | * set bits, in order. |
2033 | */ |
2034 | for (i = fontid; i-- > 0 ;) { |
2035 | if (i & ~fontid) |
2036 | continue; /* some other bit is set */ |
2037 | if (inst->fonts[i]) { |
2038 | fontid = i; |
2039 | break; |
2040 | } |
2041 | } |
2042 | assert(inst->fonts[fontid]); /* we should at least have hit zero */ |
5bf50ab2 |
2043 | } |
2044 | |
36566009 |
2045 | if ((lattr & LATTR_MODE) != LATTR_NORM) { |
f34df346 |
2046 | x *= 2; |
5def7522 |
2047 | if (x >= inst->term->cols) |
614bb468 |
2048 | return; |
006238cb |
2049 | if (x + len*2*widefactor > inst->term->cols) |
2050 | len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */ |
623f81b7 |
2051 | rlen = len * 2; |
2052 | } else |
2053 | rlen = len; |
2054 | |
2055 | { |
2056 | GdkRectangle r; |
2057 | |
3ea863a3 |
2058 | r.x = x*inst->font_width+inst->cfg.window_border; |
2059 | r.y = y*inst->font_height+inst->cfg.window_border; |
006238cb |
2060 | r.width = rlen*widefactor*inst->font_width; |
623f81b7 |
2061 | r.height = inst->font_height; |
2062 | gdk_gc_set_clip_rectangle(gc, &r); |
f34df346 |
2063 | } |
2064 | |
d64838e1 |
2065 | gdk_gc_set_foreground(gc, &inst->cols[nbg]); |
83616aab |
2066 | gdk_draw_rectangle(inst->pixmap, gc, 1, |
3ea863a3 |
2067 | x*inst->font_width+inst->cfg.window_border, |
2068 | y*inst->font_height+inst->cfg.window_border, |
006238cb |
2069 | rlen*widefactor*inst->font_width, inst->font_height); |
6a5e84dd |
2070 | |
d64838e1 |
2071 | gdk_gc_set_foreground(gc, &inst->cols[nfg]); |
2dc6356a |
2072 | { |
2dc6356a |
2073 | gchar *gcs; |
2dc6356a |
2074 | |
f160b7b8 |
2075 | /* |
2076 | * FIXME: this length is hardwired on the assumption that |
2077 | * conversions from wide to multibyte characters will |
2078 | * never generate more than 10 bytes for a single wide |
2079 | * character. |
2080 | */ |
2081 | gcs = snewn(len*10+1, gchar); |
1c3d42bc |
2082 | |
f160b7b8 |
2083 | for (combining = 0; combining < ncombining; combining++) { |
2084 | int mblen = wc_to_mb(inst->fonts[fontid]->real_charset, 0, |
2085 | text + combining, len, gcs, len*10+1, ".", |
2086 | NULL, NULL); |
2087 | unifont_draw_text(inst->pixmap, gc, inst->fonts[fontid], |
c6958dfe |
2088 | x*inst->font_width+inst->cfg.window_border, |
36566009 |
2089 | y*inst->font_height+inst->cfg.window_border+inst->fonts[0]->ascent, |
f160b7b8 |
2090 | gcs, mblen, widefactor > 1, bold, inst->font_width); |
2dc6356a |
2091 | } |
f160b7b8 |
2092 | |
2093 | sfree(gcs); |
2dc6356a |
2094 | } |
d64838e1 |
2095 | |
2096 | if (attr & ATTR_UNDER) { |
2097 | int uheight = inst->fonts[0]->ascent + 1; |
83616aab |
2098 | if (uheight >= inst->font_height) |
2099 | uheight = inst->font_height - 1; |
3ea863a3 |
2100 | gdk_draw_line(inst->pixmap, gc, x*inst->font_width+inst->cfg.window_border, |
2101 | y*inst->font_height + uheight + inst->cfg.window_border, |
2102 | (x+len)*widefactor*inst->font_width-1+inst->cfg.window_border, |
2103 | y*inst->font_height + uheight + inst->cfg.window_border); |
d64838e1 |
2104 | } |
2105 | |
36566009 |
2106 | if ((lattr & LATTR_MODE) != LATTR_NORM) { |
f34df346 |
2107 | /* |
2108 | * I can't find any plausible StretchBlt equivalent in the |
2109 | * X server, so I'm going to do this the slow and painful |
2110 | * way. This will involve repeated calls to |
2111 | * gdk_draw_pixmap() to stretch the text horizontally. It's |
2112 | * O(N^2) in time and O(N) in network bandwidth, but you |
2113 | * try thinking of a better way. :-( |
2114 | */ |
2115 | int i; |
006238cb |
2116 | for (i = 0; i < len * widefactor * inst->font_width; i++) { |
f34df346 |
2117 | gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap, |
3ea863a3 |
2118 | x*inst->font_width+inst->cfg.window_border + 2*i, |
2119 | y*inst->font_height+inst->cfg.window_border, |
2120 | x*inst->font_width+inst->cfg.window_border + 2*i+1, |
2121 | y*inst->font_height+inst->cfg.window_border, |
7a216d7f |
2122 | len * widefactor * inst->font_width - i, inst->font_height); |
f34df346 |
2123 | } |
2124 | len *= 2; |
36566009 |
2125 | if ((lattr & LATTR_MODE) != LATTR_WIDE) { |
f34df346 |
2126 | int dt, db; |
2127 | /* Now stretch vertically, in the same way. */ |
36566009 |
2128 | if ((lattr & LATTR_MODE) == LATTR_BOT) |
f34df346 |
2129 | dt = 0, db = 1; |
2130 | else |
2131 | dt = 1, db = 0; |
2132 | for (i = 0; i < inst->font_height; i+=2) { |
2133 | gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap, |
3ea863a3 |
2134 | x*inst->font_width+inst->cfg.window_border, |
2135 | y*inst->font_height+inst->cfg.window_border+dt*i+db, |
7a216d7f |
2136 | x*inst->font_width+inst->cfg.window_border, |
3ea863a3 |
2137 | y*inst->font_height+inst->cfg.window_border+dt*(i+1), |
7a216d7f |
2138 | len * widefactor * inst->font_width, inst->font_height-i-1); |
f34df346 |
2139 | } |
2140 | } |
1a675941 |
2141 | } |
2142 | } |
2143 | |
36566009 |
2144 | void do_text(Context ctx, int x, int y, wchar_t *text, int len, |
1a675941 |
2145 | unsigned long attr, int lattr) |
2146 | { |
a8327734 |
2147 | struct draw_ctx *dctx = (struct draw_ctx *)ctx; |
2148 | struct gui_data *inst = dctx->inst; |
2149 | GdkGC *gc = dctx->gc; |
006238cb |
2150 | int widefactor; |
1a675941 |
2151 | |
2152 | do_text_internal(ctx, x, y, text, len, attr, lattr); |
2153 | |
006238cb |
2154 | if (attr & ATTR_WIDE) { |
2155 | widefactor = 2; |
2156 | } else { |
2157 | widefactor = 1; |
2158 | } |
2159 | |
36566009 |
2160 | if ((lattr & LATTR_MODE) != LATTR_NORM) { |
1a675941 |
2161 | x *= 2; |
5def7522 |
2162 | if (x >= inst->term->cols) |
1a675941 |
2163 | return; |
006238cb |
2164 | if (x + len*2*widefactor > inst->term->cols) |
2165 | len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */ |
f34df346 |
2166 | len *= 2; |
2167 | } |
2168 | |
d64838e1 |
2169 | gdk_draw_pixmap(inst->area->window, gc, inst->pixmap, |
3ea863a3 |
2170 | x*inst->font_width+inst->cfg.window_border, |
2171 | y*inst->font_height+inst->cfg.window_border, |
2172 | x*inst->font_width+inst->cfg.window_border, |
2173 | y*inst->font_height+inst->cfg.window_border, |
006238cb |
2174 | len*widefactor*inst->font_width, inst->font_height); |
1709795f |
2175 | } |
2176 | |
36566009 |
2177 | void do_cursor(Context ctx, int x, int y, wchar_t *text, int len, |
1709795f |
2178 | unsigned long attr, int lattr) |
2179 | { |
a8327734 |
2180 | struct draw_ctx *dctx = (struct draw_ctx *)ctx; |
2181 | struct gui_data *inst = dctx->inst; |
2182 | GdkGC *gc = dctx->gc; |
2183 | |
a6db1e95 |
2184 | int active, passive, widefactor; |
d64838e1 |
2185 | |
1709795f |
2186 | if (attr & TATTR_PASCURS) { |
2187 | attr &= ~TATTR_PASCURS; |
d64838e1 |
2188 | passive = 1; |
2189 | } else |
2190 | passive = 0; |
3ea863a3 |
2191 | if ((attr & TATTR_ACTCURS) && inst->cfg.cursor_type != 0) { |
1a675941 |
2192 | attr &= ~TATTR_ACTCURS; |
a6db1e95 |
2193 | active = 1; |
2194 | } else |
2195 | active = 0; |
1a675941 |
2196 | do_text_internal(ctx, x, y, text, len, attr, lattr); |
2197 | |
ac059c2e |
2198 | if (attr & TATTR_COMBINING) |
2199 | len = 1; |
2200 | |
006238cb |
2201 | if (attr & ATTR_WIDE) { |
2202 | widefactor = 2; |
2203 | } else { |
2204 | widefactor = 1; |
2205 | } |
2206 | |
36566009 |
2207 | if ((lattr & LATTR_MODE) != LATTR_NORM) { |
1a675941 |
2208 | x *= 2; |
5def7522 |
2209 | if (x >= inst->term->cols) |
1a675941 |
2210 | return; |
006238cb |
2211 | if (x + len*2*widefactor > inst->term->cols) |
2212 | len = (inst->term->cols-x)/2/widefactor;/* trim to LH half */ |
1a675941 |
2213 | len *= 2; |
2214 | } |
2215 | |
3ea863a3 |
2216 | if (inst->cfg.cursor_type == 0) { |
1a675941 |
2217 | /* |
2218 | * An active block cursor will already have been done by |
2219 | * the above do_text call, so we only need to do anything |
2220 | * if it's passive. |
2221 | */ |
2222 | if (passive) { |
cecb13f6 |
2223 | gdk_gc_set_foreground(gc, &inst->cols[261]); |
1a675941 |
2224 | gdk_draw_rectangle(inst->pixmap, gc, 0, |
3ea863a3 |
2225 | x*inst->font_width+inst->cfg.window_border, |
2226 | y*inst->font_height+inst->cfg.window_border, |
35100bc3 |
2227 | len*widefactor*inst->font_width-1, inst->font_height-1); |
1a675941 |
2228 | } |
2229 | } else { |
2230 | int uheight; |
2231 | int startx, starty, dx, dy, length, i; |
2232 | |
2233 | int char_width; |
2234 | |
36566009 |
2235 | if ((attr & ATTR_WIDE) || (lattr & LATTR_MODE) != LATTR_NORM) |
1a675941 |
2236 | char_width = 2*inst->font_width; |
2237 | else |
2238 | char_width = inst->font_width; |
2239 | |
3ea863a3 |
2240 | if (inst->cfg.cursor_type == 1) { |
1a675941 |
2241 | uheight = inst->fonts[0]->ascent + 1; |
2242 | if (uheight >= inst->font_height) |
2243 | uheight = inst->font_height - 1; |
2244 | |
3ea863a3 |
2245 | startx = x * inst->font_width + inst->cfg.window_border; |
2246 | starty = y * inst->font_height + inst->cfg.window_border + uheight; |
1a675941 |
2247 | dx = 1; |
2248 | dy = 0; |
35100bc3 |
2249 | length = len * widefactor * char_width; |
1a675941 |
2250 | } else { |
2251 | int xadjust = 0; |
2252 | if (attr & TATTR_RIGHTCURS) |
2253 | xadjust = char_width - 1; |
3ea863a3 |
2254 | startx = x * inst->font_width + inst->cfg.window_border + xadjust; |
2255 | starty = y * inst->font_height + inst->cfg.window_border; |
1a675941 |
2256 | dx = 0; |
2257 | dy = 1; |
2258 | length = inst->font_height; |
2259 | } |
2260 | |
fdc94dd1 |
2261 | gdk_gc_set_foreground(gc, &inst->cols[261]); |
1a675941 |
2262 | if (passive) { |
2263 | for (i = 0; i < length; i++) { |
2264 | if (i % 2 == 0) { |
2265 | gdk_draw_point(inst->pixmap, gc, startx, starty); |
2266 | } |
2267 | startx += dx; |
2268 | starty += dy; |
2269 | } |
a6db1e95 |
2270 | } else if (active) { |
1a675941 |
2271 | gdk_draw_line(inst->pixmap, gc, startx, starty, |
2272 | startx + (length-1) * dx, starty + (length-1) * dy); |
a6db1e95 |
2273 | } /* else no cursor (e.g., blinked off) */ |
d64838e1 |
2274 | } |
1a675941 |
2275 | |
2276 | gdk_draw_pixmap(inst->area->window, gc, inst->pixmap, |
3ea863a3 |
2277 | x*inst->font_width+inst->cfg.window_border, |
2278 | y*inst->font_height+inst->cfg.window_border, |
2279 | x*inst->font_width+inst->cfg.window_border, |
2280 | y*inst->font_height+inst->cfg.window_border, |
006238cb |
2281 | len*widefactor*inst->font_width, inst->font_height); |
1709795f |
2282 | } |
2283 | |
a8327734 |
2284 | GdkCursor *make_mouse_ptr(struct gui_data *inst, int cursor_val) |
7798fa56 |
2285 | { |
2286 | /* |
2287 | * Truly hideous hack: GTK doesn't allow us to set the mouse |
2288 | * cursor foreground and background colours unless we've _also_ |
2289 | * created our own cursor from bitmaps. Therefore, I need to |
2290 | * load the `cursor' font and draw glyphs from it on to |
2291 | * pixmaps, in order to construct my cursors with the fg and bg |
2292 | * I want. This is a gross hack, but it's more self-contained |
2293 | * than linking in Xlib to find the X window handle to |
2294 | * inst->area and calling XRecolorCursor, and it's more |
2295 | * futureproof than hard-coding the shapes as bitmap arrays. |
2296 | */ |
2297 | static GdkFont *cursor_font = NULL; |
2298 | GdkPixmap *source, *mask; |
2299 | GdkGC *gc; |
2300 | GdkColor cfg = { 0, 65535, 65535, 65535 }; |
2301 | GdkColor cbg = { 0, 0, 0, 0 }; |
2302 | GdkColor dfg = { 1, 65535, 65535, 65535 }; |
2303 | GdkColor dbg = { 0, 0, 0, 0 }; |
2304 | GdkCursor *ret; |
2305 | gchar text[2]; |
2306 | gint lb, rb, wid, asc, desc, w, h, x, y; |
2307 | |
57e636ca |
2308 | if (cursor_val == -2) { |
7798fa56 |
2309 | gdk_font_unref(cursor_font); |
2310 | return NULL; |
2311 | } |
2312 | |
f160b7b8 |
2313 | if (cursor_val >= 0 && !cursor_font) { |
7798fa56 |
2314 | cursor_font = gdk_font_load("cursor"); |
f160b7b8 |
2315 | if (cursor_font) |
2316 | gdk_font_ref(cursor_font); |
2317 | } |
7798fa56 |
2318 | |
2319 | /* |
2320 | * Get the text extent of the cursor in question. We use the |
2321 | * mask character for this, because it's typically slightly |
2322 | * bigger than the main character. |
2323 | */ |
57e636ca |
2324 | if (cursor_val >= 0) { |
2325 | text[1] = '\0'; |
2326 | text[0] = (char)cursor_val + 1; |
2327 | gdk_string_extents(cursor_font, text, &lb, &rb, &wid, &asc, &desc); |
2328 | w = rb-lb; h = asc+desc; x = -lb; y = asc; |
2329 | } else { |
2330 | w = h = 1; |
2331 | x = y = 0; |
2332 | } |
7798fa56 |
2333 | |
2334 | source = gdk_pixmap_new(NULL, w, h, 1); |
2335 | mask = gdk_pixmap_new(NULL, w, h, 1); |
2336 | |
2337 | /* |
2338 | * Draw the mask character on the mask pixmap. |
2339 | */ |
7798fa56 |
2340 | gc = gdk_gc_new(mask); |
2341 | gdk_gc_set_foreground(gc, &dbg); |
2342 | gdk_draw_rectangle(mask, gc, 1, 0, 0, w, h); |
57e636ca |
2343 | if (cursor_val >= 0) { |
2344 | text[1] = '\0'; |
2345 | text[0] = (char)cursor_val + 1; |
2346 | gdk_gc_set_foreground(gc, &dfg); |
2347 | gdk_draw_text(mask, cursor_font, gc, x, y, text, 1); |
2348 | } |
7798fa56 |
2349 | gdk_gc_unref(gc); |
2350 | |
2351 | /* |
2352 | * Draw the main character on the source pixmap. |
2353 | */ |
7798fa56 |
2354 | gc = gdk_gc_new(source); |
2355 | gdk_gc_set_foreground(gc, &dbg); |
2356 | gdk_draw_rectangle(source, gc, 1, 0, 0, w, h); |
57e636ca |
2357 | if (cursor_val >= 0) { |
2358 | text[1] = '\0'; |
2359 | text[0] = (char)cursor_val; |
2360 | gdk_gc_set_foreground(gc, &dfg); |
2361 | gdk_draw_text(source, cursor_font, gc, x, y, text, 1); |
2362 | } |
7798fa56 |
2363 | gdk_gc_unref(gc); |
2364 | |
2365 | /* |
2366 | * Create the cursor. |
2367 | */ |
2368 | ret = gdk_cursor_new_from_pixmap(source, mask, &cfg, &cbg, x, y); |
2369 | |
2370 | /* |
2371 | * Clean up. |
2372 | */ |
2373 | gdk_pixmap_unref(source); |
2374 | gdk_pixmap_unref(mask); |
2375 | |
2376 | return ret; |
2377 | } |
2378 | |
1709795f |
2379 | void modalfatalbox(char *p, ...) |
2380 | { |
2381 | va_list ap; |
2382 | fprintf(stderr, "FATAL ERROR: "); |
2383 | va_start(ap, p); |
2384 | vfprintf(stderr, p, ap); |
2385 | va_end(ap); |
2386 | fputc('\n', stderr); |
2387 | exit(1); |
f7f27309 |
2388 | } |
2389 | |
46a3419b |
2390 | void cmdline_error(char *p, ...) |
2391 | { |
2392 | va_list ap; |
a4e8ffb0 |
2393 | fprintf(stderr, "%s: ", appname); |
46a3419b |
2394 | va_start(ap, p); |
2395 | vfprintf(stderr, p, ap); |
2396 | va_end(ap); |
2397 | fputc('\n', stderr); |
2398 | exit(1); |
2399 | } |
2400 | |
a8327734 |
2401 | char *get_x_display(void *frontend) |
755a6d84 |
2402 | { |
2403 | return gdk_get_display(); |
2404 | } |
2405 | |
7af753e6 |
2406 | long get_windowid(void *frontend) |
2407 | { |
fbf6cb3b |
2408 | struct gui_data *inst = (struct gui_data *)frontend; |
7af753e6 |
2409 | return (long)GDK_WINDOW_XWINDOW(inst->area->window); |
2410 | } |
2411 | |
96add444 |
2412 | static void help(FILE *fp) { |
2413 | if(fprintf(fp, |
2414 | "pterm option summary:\n" |
2415 | "\n" |
2416 | " --display DISPLAY Specify X display to use (note '--')\n" |
2417 | " -name PREFIX Prefix when looking up resources (default: pterm)\n" |
2418 | " -fn FONT Normal text font\n" |
2419 | " -fb FONT Bold text font\n" |
aca2a702 |
2420 | " -geometry GEOMETRY Position and size of window (size in characters)\n" |
96add444 |
2421 | " -sl LINES Number of lines of scrollback\n" |
2422 | " -fg COLOUR, -bg COLOUR Foreground/background colour\n" |
2423 | " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n" |
2424 | " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n" |
2425 | " -T TITLE Window title\n" |
2426 | " -ut, +ut Do(default) or do not update utmp\n" |
2427 | " -ls, +ls Do(default) or do not make shell a login shell\n" |
2428 | " -sb, +sb Do(default) or do not display a scrollbar\n" |
2429 | " -log PATH Log all output to a file\n" |
2430 | " -nethack Map numeric keypad to hjklyubn direction keys\n" |
2431 | " -xrm RESOURCE-STRING Set an X resource\n" |
2432 | " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n" |
2433 | ) < 0 || fflush(fp) < 0) { |
2434 | perror("output error"); |
2435 | exit(1); |
2436 | } |
2437 | } |
2438 | |
df7c7e52 |
2439 | int do_cmdline(int argc, char **argv, int do_everything, int *allow_launch, |
aca2a702 |
2440 | struct gui_data *inst, Config *cfg) |
f7f27309 |
2441 | { |
6169c758 |
2442 | int err = 0; |
56801e3d |
2443 | char *val; |
f7f27309 |
2444 | |
faec60ed |
2445 | /* |
b89ee4f3 |
2446 | * Macros to make argument handling easier. Note that because |
2447 | * they need to call `continue', they cannot be contained in |
2448 | * the usual do {...} while (0) wrapper to make them |
2449 | * syntactically single statements; hence it is not legal to |
2450 | * use one of these macros as an unbraced statement between |
2451 | * `if' and `else'. |
faec60ed |
2452 | */ |
b89ee4f3 |
2453 | #define EXPECTS_ARG { \ |
faec60ed |
2454 | if (--argc <= 0) { \ |
2455 | err = 1; \ |
a4e8ffb0 |
2456 | fprintf(stderr, "%s: %s expects an argument\n", appname, p); \ |
b89ee4f3 |
2457 | continue; \ |
faec60ed |
2458 | } else \ |
2459 | val = *++argv; \ |
b89ee4f3 |
2460 | } |
2461 | #define SECOND_PASS_ONLY { if (!do_everything) continue; } |
faec60ed |
2462 | |
6169c758 |
2463 | while (--argc > 0) { |
2464 | char *p = *++argv; |
46a3419b |
2465 | int ret; |
2466 | |
fb9bf82f |
2467 | /* |
2468 | * Shameless cheating. Debian requires all X terminal |
2469 | * emulators to support `-T title'; but |
2470 | * cmdline_process_param will eat -T (it means no-pty) and |
2471 | * complain that pterm doesn't support it. So, in pterm |
2472 | * only, we convert -T into -title. |
2473 | */ |
2474 | if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) && |
2475 | !strcmp(p, "-T")) |
2476 | p = "-title"; |
2477 | |
46a3419b |
2478 | ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
2479 | do_everything ? 1 : -1, cfg); |
2480 | |
2481 | if (ret == -2) { |
2482 | cmdline_error("option \"%s\" requires an argument", p); |
2483 | } else if (ret == 2) { |
2484 | --argc, ++argv; /* skip next argument */ |
2485 | continue; |
2486 | } else if (ret == 1) { |
2487 | continue; |
2488 | } |
2489 | |
8e20db05 |
2490 | if (!strcmp(p, "-fn") || !strcmp(p, "-font")) { |
faec60ed |
2491 | EXPECTS_ARG; |
2492 | SECOND_PASS_ONLY; |
9a30e26b |
2493 | strncpy(cfg->font.name, val, sizeof(cfg->font.name)); |
2494 | cfg->font.name[sizeof(cfg->font.name)-1] = '\0'; |
faec60ed |
2495 | |
2496 | } else if (!strcmp(p, "-fb")) { |
2497 | EXPECTS_ARG; |
2498 | SECOND_PASS_ONLY; |
9a30e26b |
2499 | strncpy(cfg->boldfont.name, val, sizeof(cfg->boldfont.name)); |
2500 | cfg->boldfont.name[sizeof(cfg->boldfont.name)-1] = '\0'; |
faec60ed |
2501 | |
006238cb |
2502 | } else if (!strcmp(p, "-fw")) { |
2503 | EXPECTS_ARG; |
2504 | SECOND_PASS_ONLY; |
9a30e26b |
2505 | strncpy(cfg->widefont.name, val, sizeof(cfg->widefont.name)); |
2506 | cfg->widefont.name[sizeof(cfg->widefont.name)-1] = '\0'; |
006238cb |
2507 | |
2508 | } else if (!strcmp(p, "-fwb")) { |
2509 | EXPECTS_ARG; |
2510 | SECOND_PASS_ONLY; |
9a30e26b |
2511 | strncpy(cfg->wideboldfont.name, val, sizeof(cfg->wideboldfont.name)); |
2512 | cfg->wideboldfont.name[sizeof(cfg->wideboldfont.name)-1] = '\0'; |
006238cb |
2513 | |
2dc6356a |
2514 | } else if (!strcmp(p, "-cs")) { |
2515 | EXPECTS_ARG; |
2516 | SECOND_PASS_ONLY; |
3ea863a3 |
2517 | strncpy(cfg->line_codepage, val, sizeof(cfg->line_codepage)); |
2518 | cfg->line_codepage[sizeof(cfg->line_codepage)-1] = '\0'; |
2dc6356a |
2519 | |
8e20db05 |
2520 | } else if (!strcmp(p, "-geometry")) { |
8a9977e5 |
2521 | int flags, x, y; |
2522 | unsigned int w, h; |
8e20db05 |
2523 | EXPECTS_ARG; |
2524 | SECOND_PASS_ONLY; |
2525 | |
2526 | flags = XParseGeometry(val, &x, &y, &w, &h); |
2527 | if (flags & WidthValue) |
8a9977e5 |
2528 | cfg->width = (int)w; |
8e20db05 |
2529 | if (flags & HeightValue) |
8a9977e5 |
2530 | cfg->height = (int)h; |
8e20db05 |
2531 | |
aca2a702 |
2532 | if (flags & (XValue | YValue)) { |
2533 | inst->xpos = x; |
2534 | inst->ypos = y; |
2535 | inst->gotpos = TRUE; |
2536 | inst->gravity = ((flags & XNegative ? 1 : 0) | |
2537 | (flags & YNegative ? 2 : 0)); |
2538 | } |
8e20db05 |
2539 | |
2540 | } else if (!strcmp(p, "-sl")) { |
2541 | EXPECTS_ARG; |
2542 | SECOND_PASS_ONLY; |
3ea863a3 |
2543 | cfg->savelines = atoi(val); |
8e20db05 |
2544 | |
2545 | } else if (!strcmp(p, "-fg") || !strcmp(p, "-bg") || |
2546 | !strcmp(p, "-bfg") || !strcmp(p, "-bbg") || |
b60ca991 |
2547 | !strcmp(p, "-cfg") || !strcmp(p, "-cbg")) { |
8e20db05 |
2548 | GdkColor col; |
2549 | |
2550 | EXPECTS_ARG; |
2551 | SECOND_PASS_ONLY; |
2552 | if (!gdk_color_parse(val, &col)) { |
2553 | err = 1; |
a4e8ffb0 |
2554 | fprintf(stderr, "%s: unable to parse colour \"%s\"\n", |
2555 | appname, val); |
8e20db05 |
2556 | } else { |
2557 | int index; |
2558 | index = (!strcmp(p, "-fg") ? 0 : |
2559 | !strcmp(p, "-bg") ? 2 : |
2560 | !strcmp(p, "-bfg") ? 1 : |
2561 | !strcmp(p, "-bbg") ? 3 : |
b60ca991 |
2562 | !strcmp(p, "-cfg") ? 4 : |
8e20db05 |
2563 | !strcmp(p, "-cbg") ? 5 : -1); |
2564 | assert(index != -1); |
3ea863a3 |
2565 | cfg->colours[index][0] = col.red / 256; |
2566 | cfg->colours[index][1] = col.green / 256; |
2567 | cfg->colours[index][2] = col.blue / 256; |
8e20db05 |
2568 | } |
2569 | |
1d009ae7 |
2570 | } else if (use_pty_argv && !strcmp(p, "-e")) { |
faec60ed |
2571 | /* This option swallows all further arguments. */ |
2572 | if (!do_everything) |
2573 | break; |
2574 | |
6169c758 |
2575 | if (--argc > 0) { |
2576 | int i; |
3d88e64d |
2577 | pty_argv = snewn(argc+1, char *); |
6169c758 |
2578 | ++argv; |
2579 | for (i = 0; i < argc; i++) |
2580 | pty_argv[i] = argv[i]; |
2581 | pty_argv[argc] = NULL; |
2582 | break; /* finished command-line processing */ |
2583 | } else |
a4e8ffb0 |
2584 | err = 1, fprintf(stderr, "%s: -e expects an argument\n", |
2585 | appname); |
faec60ed |
2586 | |
486c8271 |
2587 | } else if (!strcmp(p, "-title")) { |
faec60ed |
2588 | EXPECTS_ARG; |
2589 | SECOND_PASS_ONLY; |
3ea863a3 |
2590 | strncpy(cfg->wintitle, val, sizeof(cfg->wintitle)); |
2591 | cfg->wintitle[sizeof(cfg->wintitle)-1] = '\0'; |
faec60ed |
2592 | |
2593 | } else if (!strcmp(p, "-log")) { |
2594 | EXPECTS_ARG; |
2595 | SECOND_PASS_ONLY; |
9a30e26b |
2596 | strncpy(cfg->logfilename.path, val, sizeof(cfg->logfilename.path)); |
2597 | cfg->logfilename.path[sizeof(cfg->logfilename.path)-1] = '\0'; |
3ea863a3 |
2598 | cfg->logtype = LGTYP_DEBUG; |
faec60ed |
2599 | |
8e20db05 |
2600 | } else if (!strcmp(p, "-ut-") || !strcmp(p, "+ut")) { |
faec60ed |
2601 | SECOND_PASS_ONLY; |
3ea863a3 |
2602 | cfg->stamp_utmp = 0; |
faec60ed |
2603 | |
8e20db05 |
2604 | } else if (!strcmp(p, "-ut")) { |
faec60ed |
2605 | SECOND_PASS_ONLY; |
3ea863a3 |
2606 | cfg->stamp_utmp = 1; |
faec60ed |
2607 | |
8e20db05 |
2608 | } else if (!strcmp(p, "-ls-") || !strcmp(p, "+ls")) { |
faec60ed |
2609 | SECOND_PASS_ONLY; |
3ea863a3 |
2610 | cfg->login_shell = 0; |
faec60ed |
2611 | |
8e20db05 |
2612 | } else if (!strcmp(p, "-ls")) { |
2613 | SECOND_PASS_ONLY; |
3ea863a3 |
2614 | cfg->login_shell = 1; |
8e20db05 |
2615 | |
faec60ed |
2616 | } else if (!strcmp(p, "-nethack")) { |
2617 | SECOND_PASS_ONLY; |
3ea863a3 |
2618 | cfg->nethack_keypad = 1; |
faec60ed |
2619 | |
8e20db05 |
2620 | } else if (!strcmp(p, "-sb-") || !strcmp(p, "+sb")) { |
2621 | SECOND_PASS_ONLY; |
3ea863a3 |
2622 | cfg->scrollbar = 0; |
8e20db05 |
2623 | |
2624 | } else if (!strcmp(p, "-sb")) { |
faec60ed |
2625 | SECOND_PASS_ONLY; |
3ea863a3 |
2626 | cfg->scrollbar = 0; |
faec60ed |
2627 | |
2628 | } else if (!strcmp(p, "-name")) { |
2629 | EXPECTS_ARG; |
2630 | app_name = val; |
0ac15bdc |
2631 | |
2632 | } else if (!strcmp(p, "-xrm")) { |
2633 | EXPECTS_ARG; |
2634 | provide_xrm_string(val); |
2635 | |
96add444 |
2636 | } else if(!strcmp(p, "-help") || !strcmp(p, "--help")) { |
2637 | help(stdout); |
2638 | exit(0); |
2285d016 |
2639 | |
2640 | } else if (!strcmp(p, "-pgpfp")) { |
2641 | pgp_fingerprints(); |
2642 | exit(1); |
2643 | |
46a3419b |
2644 | } else if(p[0] != '-' && (!do_everything || |
df7c7e52 |
2645 | process_nonoption_arg(p, cfg, |
2646 | allow_launch))) { |
46a3419b |
2647 | /* do nothing */ |
2648 | |
edc73959 |
2649 | } else { |
2650 | err = 1; |
a4e8ffb0 |
2651 | fprintf(stderr, "%s: unrecognized option '%s'\n", appname, p); |
330f49be |
2652 | } |
6169c758 |
2653 | } |
2654 | |
faec60ed |
2655 | return err; |
2656 | } |
2657 | |
74aca06d |
2658 | int uxsel_input_add(int fd, int rwx) { |
2659 | int flags = 0; |
2660 | if (rwx & 1) flags |= GDK_INPUT_READ; |
2661 | if (rwx & 2) flags |= GDK_INPUT_WRITE; |
2662 | if (rwx & 4) flags |= GDK_INPUT_EXCEPTION; |
f160b7b8 |
2663 | assert(flags); |
74aca06d |
2664 | return gdk_input_add(fd, flags, fd_input_func, NULL); |
2665 | } |
2666 | |
2667 | void uxsel_input_remove(int id) { |
2668 | gdk_input_remove(id); |
2669 | } |
2670 | |
56b9b9a7 |
2671 | void setup_fonts_ucs(struct gui_data *inst) |
2672 | { |
56b9b9a7 |
2673 | if (inst->fonts[0]) |
f160b7b8 |
2674 | unifont_destroy(inst->fonts[0]); |
56b9b9a7 |
2675 | if (inst->fonts[1]) |
f160b7b8 |
2676 | unifont_destroy(inst->fonts[1]); |
56b9b9a7 |
2677 | if (inst->fonts[2]) |
f160b7b8 |
2678 | unifont_destroy(inst->fonts[2]); |
56b9b9a7 |
2679 | if (inst->fonts[3]) |
f160b7b8 |
2680 | unifont_destroy(inst->fonts[3]); |
56b9b9a7 |
2681 | |
f160b7b8 |
2682 | inst->fonts[0] = unifont_create(inst->area, inst->cfg.font.name, |
2683 | FALSE, FALSE, |
2684 | inst->cfg.shadowboldoffset, |
2685 | inst->cfg.shadowbold); |
56b9b9a7 |
2686 | if (!inst->fonts[0]) { |
2687 | fprintf(stderr, "%s: unable to load font \"%s\"\n", appname, |
2688 | inst->cfg.font.name); |
2689 | exit(1); |
2690 | } |
f87534ba |
2691 | |
f160b7b8 |
2692 | if (inst->cfg.shadowbold || !inst->cfg.boldfont.name[0]) { |
bf133a73 |
2693 | inst->fonts[1] = NULL; |
f87534ba |
2694 | } else { |
f160b7b8 |
2695 | inst->fonts[1] = unifont_create(inst->area, inst->cfg.boldfont.name, |
2696 | FALSE, TRUE, |
2697 | inst->cfg.shadowboldoffset, |
2698 | inst->cfg.shadowbold); |
2699 | if (!inst->fonts[1]) { |
bf133a73 |
2700 | fprintf(stderr, "%s: unable to load bold font \"%s\"\n", appname, |
2701 | inst->cfg.boldfont.name); |
2702 | exit(1); |
2703 | } |
f87534ba |
2704 | } |
f87534ba |
2705 | |
56b9b9a7 |
2706 | if (inst->cfg.widefont.name[0]) { |
f160b7b8 |
2707 | inst->fonts[2] = unifont_create(inst->area, inst->cfg.widefont.name, |
2708 | TRUE, FALSE, |
2709 | inst->cfg.shadowboldoffset, |
2710 | inst->cfg.shadowbold); |
2711 | if (!inst->fonts[2]) { |
2712 | fprintf(stderr, "%s: unable to load wide font \"%s\"\n", appname, |
2713 | inst->cfg.widefont.name); |
2714 | exit(1); |
2715 | } |
f87534ba |
2716 | } else { |
f160b7b8 |
2717 | inst->fonts[2] = NULL; |
f87534ba |
2718 | } |
f87534ba |
2719 | |
f160b7b8 |
2720 | if (inst->cfg.shadowbold || !inst->cfg.wideboldfont.name[0]) { |
bf133a73 |
2721 | inst->fonts[3] = NULL; |
f87534ba |
2722 | } else { |
f160b7b8 |
2723 | inst->fonts[3] = unifont_create(inst->area, |
2724 | inst->cfg.wideboldfont.name, TRUE, |
2725 | TRUE, inst->cfg.shadowboldoffset, |
2726 | inst->cfg.shadowbold); |
2727 | if (!inst->fonts[3]) { |
2728 | fprintf(stderr, "%s: unable to load wide bold font \"%s\"\n", appname, |
2729 | inst->cfg.boldfont.name); |
bf133a73 |
2730 | exit(1); |
2731 | } |
f87534ba |
2732 | } |
56b9b9a7 |
2733 | |
f160b7b8 |
2734 | inst->font_width = inst->fonts[0]->width; |
2735 | inst->font_height = inst->fonts[0]->height; |
56b9b9a7 |
2736 | |
6ac7f054 |
2737 | inst->direct_to_font = init_ucs(&inst->ucsdata, inst->cfg.line_codepage, |
f160b7b8 |
2738 | inst->cfg.utf8_override, |
2739 | inst->fonts[0]->public_charset, |
3900c2d6 |
2740 | inst->cfg.vtmode); |
56b9b9a7 |
2741 | } |
2742 | |
2743 | void set_geom_hints(struct gui_data *inst) |
2744 | { |
2745 | GdkGeometry geom; |
2746 | geom.min_width = inst->font_width + 2*inst->cfg.window_border; |
2747 | geom.min_height = inst->font_height + 2*inst->cfg.window_border; |
2748 | geom.max_width = geom.max_height = -1; |
2749 | geom.base_width = 2*inst->cfg.window_border; |
2750 | geom.base_height = 2*inst->cfg.window_border; |
2751 | geom.width_inc = inst->font_width; |
2752 | geom.height_inc = inst->font_height; |
2753 | geom.min_aspect = geom.max_aspect = 0; |
2754 | gtk_window_set_geometry_hints(GTK_WINDOW(inst->window), inst->area, &geom, |
2755 | GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | |
2756 | GDK_HINT_RESIZE_INC); |
2757 | } |
2758 | |
47e4e735 |
2759 | void clear_scrollback_menuitem(GtkMenuItem *item, gpointer data) |
2760 | { |
2761 | struct gui_data *inst = (struct gui_data *)data; |
2762 | term_clrsb(inst->term); |
2763 | } |
2764 | |
2765 | void reset_terminal_menuitem(GtkMenuItem *item, gpointer data) |
2766 | { |
2767 | struct gui_data *inst = (struct gui_data *)data; |
900f9aca |
2768 | term_pwron(inst->term, TRUE); |
e3be8de5 |
2769 | if (inst->ldisc) |
2770 | ldisc_send(inst->ldisc, NULL, 0, 0); |
47e4e735 |
2771 | } |
2772 | |
d9e5c333 |
2773 | void copy_all_menuitem(GtkMenuItem *item, gpointer data) |
2774 | { |
2775 | struct gui_data *inst = (struct gui_data *)data; |
2776 | term_copyall(inst->term); |
2777 | } |
2778 | |
47e4e735 |
2779 | void special_menuitem(GtkMenuItem *item, gpointer data) |
2780 | { |
2781 | struct gui_data *inst = (struct gui_data *)data; |
f7f9fb5c |
2782 | int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), |
2783 | "user-data")); |
47e4e735 |
2784 | |
e3be8de5 |
2785 | if (inst->back) |
2786 | inst->back->special(inst->backhandle, code); |
47e4e735 |
2787 | } |
2788 | |
2789 | void about_menuitem(GtkMenuItem *item, gpointer data) |
2790 | { |
7718480a |
2791 | struct gui_data *inst = (struct gui_data *)data; |
2792 | about_box(inst->window); |
47e4e735 |
2793 | } |
2794 | |
8eed910d |
2795 | void event_log_menuitem(GtkMenuItem *item, gpointer data) |
2796 | { |
2797 | struct gui_data *inst = (struct gui_data *)data; |
2798 | showeventlog(inst->eventlogstuff, inst->window); |
2799 | } |
2800 | |
56b9b9a7 |
2801 | void change_settings_menuitem(GtkMenuItem *item, gpointer data) |
2802 | { |
2803 | /* This maps colour indices in inst->cfg to those used in inst->cols. */ |
2804 | static const int ww[] = { |
cecb13f6 |
2805 | 256, 257, 258, 259, 260, 261, |
2806 | 0, 8, 1, 9, 2, 10, 3, 11, |
2807 | 4, 12, 5, 13, 6, 14, 7, 15 |
56b9b9a7 |
2808 | }; |
2809 | struct gui_data *inst = (struct gui_data *)data; |
2810 | char *title = dupcat(appname, " Reconfiguration", NULL); |
2811 | Config cfg2, oldcfg; |
2812 | int i, need_size; |
2813 | |
cecb13f6 |
2814 | assert(lenof(ww) == NCFGCOLOURS); |
2815 | |
0b3c373d |
2816 | if (inst->reconfiguring) |
2817 | return; |
2818 | else |
2819 | inst->reconfiguring = TRUE; |
2820 | |
56b9b9a7 |
2821 | cfg2 = inst->cfg; /* structure copy */ |
2822 | |
f89c3294 |
2823 | if (do_config_box(title, &cfg2, 1, |
2824 | inst->back?inst->back->cfg_info(inst->backhandle):0)) { |
56b9b9a7 |
2825 | |
2826 | oldcfg = inst->cfg; /* structure copy */ |
2827 | inst->cfg = cfg2; /* structure copy */ |
2828 | |
2829 | /* Pass new config data to the logging module */ |
2830 | log_reconfig(inst->logctx, &cfg2); |
2831 | /* |
2832 | * Flush the line discipline's edit buffer in the case |
2833 | * where local editing has just been disabled. |
2834 | */ |
e3be8de5 |
2835 | if (inst->ldisc) |
2836 | ldisc_send(inst->ldisc, NULL, 0, 0); |
56b9b9a7 |
2837 | /* Pass new config data to the terminal */ |
2838 | term_reconfig(inst->term, &cfg2); |
2839 | /* Pass new config data to the back end */ |
e3be8de5 |
2840 | if (inst->back) |
2841 | inst->back->reconfig(inst->backhandle, &cfg2); |
56b9b9a7 |
2842 | |
2843 | /* |
2844 | * Just setting inst->cfg is sufficient to cause colour |
2845 | * setting changes to appear on the next ESC]R palette |
2846 | * reset. But we should also check whether any colour |
2847 | * settings have been changed, and revert the ones that |
2848 | * have to the new default, on the assumption that the user |
2849 | * is most likely to want an immediate update. |
2850 | */ |
cecb13f6 |
2851 | for (i = 0; i < NCFGCOLOURS; i++) { |
2852 | if (oldcfg.colours[i][0] != cfg2.colours[i][0] || |
2853 | oldcfg.colours[i][1] != cfg2.colours[i][1] || |
2854 | oldcfg.colours[i][2] != cfg2.colours[i][2]) { |
2855 | real_palette_set(inst, ww[i], cfg2.colours[i][0], |
2856 | cfg2.colours[i][1], |
2857 | cfg2.colours[i][2]); |
db26b3af |
2858 | |
2859 | /* |
2860 | * If the default background has changed, we must |
2861 | * repaint the space in between the window border |
2862 | * and the text area. |
2863 | */ |
cecb13f6 |
2864 | if (i == 258) { |
db26b3af |
2865 | set_window_background(inst); |
2866 | draw_backing_rect(inst); |
2867 | } |
2868 | } |
56b9b9a7 |
2869 | } |
2870 | |
2871 | /* |
2872 | * If the scrollbar needs to be shown, hidden, or moved |
2873 | * from one end to the other of the window, do so now. |
2874 | */ |
2875 | if (oldcfg.scrollbar != cfg2.scrollbar) { |
2876 | if (cfg2.scrollbar) |
2877 | gtk_widget_show(inst->sbar); |
2878 | else |
2879 | gtk_widget_hide(inst->sbar); |
2880 | } |
2881 | if (oldcfg.scrollbar_on_left != cfg2.scrollbar_on_left) { |
2882 | gtk_box_reorder_child(inst->hbox, inst->sbar, |
2883 | cfg2.scrollbar_on_left ? 0 : 1); |
2884 | } |
2885 | |
2886 | /* |
2887 | * Change the window title, if required. |
2888 | */ |
2889 | if (strcmp(oldcfg.wintitle, cfg2.wintitle)) |
2890 | set_title(inst, cfg2.wintitle); |
6e785e0c |
2891 | set_window_titles(inst); |
56b9b9a7 |
2892 | |
2893 | /* |
2894 | * Redo the whole tangled fonts and Unicode mess if |
2895 | * necessary. |
2896 | */ |
2897 | if (strcmp(oldcfg.font.name, cfg2.font.name) || |
2898 | strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) || |
2899 | strcmp(oldcfg.widefont.name, cfg2.widefont.name) || |
2900 | strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) || |
3900c2d6 |
2901 | strcmp(oldcfg.line_codepage, cfg2.line_codepage) || |
bf133a73 |
2902 | oldcfg.vtmode != cfg2.vtmode || |
2903 | oldcfg.shadowbold != cfg2.shadowbold) { |
56b9b9a7 |
2904 | setup_fonts_ucs(inst); |
2905 | need_size = 1; |
2906 | } else |
2907 | need_size = 0; |
2908 | |
2909 | /* |
2910 | * Resize the window. |
2911 | */ |
2912 | if (oldcfg.width != cfg2.width || oldcfg.height != cfg2.height || |
2913 | oldcfg.window_border != cfg2.window_border || need_size) { |
2914 | set_geom_hints(inst); |
2915 | request_resize(inst, cfg2.width, cfg2.height); |
ec9f4fc6 |
2916 | } else { |
2917 | /* |
2918 | * The above will have caused a call to term_size() for |
2919 | * us if it happened. If the user has fiddled with only |
2920 | * the scrollback size, the above will not have |
2921 | * happened and we will need an explicit term_size() |
2922 | * here. |
2923 | */ |
2924 | if (oldcfg.savelines != cfg2.savelines) |
2925 | term_size(inst->term, inst->term->rows, inst->term->cols, |
2926 | cfg2.savelines); |
2927 | } |
56b9b9a7 |
2928 | |
2929 | term_invalidate(inst->term); |
db26b3af |
2930 | |
2931 | /* |
2932 | * We do an explicit full redraw here to ensure the window |
2933 | * border has been redrawn as well as the text area. |
2934 | */ |
2935 | gtk_widget_queue_draw(inst->area); |
56b9b9a7 |
2936 | } |
2937 | sfree(title); |
0b3c373d |
2938 | inst->reconfiguring = FALSE; |
56b9b9a7 |
2939 | } |
2940 | |
56801e3d |
2941 | void fork_and_exec_self(struct gui_data *inst, int fd_to_close, ...) |
2942 | { |
2943 | /* |
2944 | * Re-execing ourself is not an exact science under Unix. I do |
2945 | * the best I can by using /proc/self/exe if available and by |
2946 | * assuming argv[0] can be found on $PATH if not. |
2947 | * |
2948 | * Note that we also have to reconstruct the elements of the |
2949 | * original argv which gtk swallowed, since the user wants the |
2950 | * new session to appear on the same X display as the old one. |
2951 | */ |
2952 | char **args; |
2953 | va_list ap; |
2954 | int i, n; |
2955 | int pid; |
2956 | |
2957 | /* |
2958 | * Collect the arguments with which to re-exec ourself. |
2959 | */ |
2960 | va_start(ap, fd_to_close); |
2961 | n = 2; /* progname and terminating NULL */ |
2962 | n += inst->ngtkargs; |
2963 | while (va_arg(ap, char *) != NULL) |
2964 | n++; |
2965 | va_end(ap); |
2966 | |
2967 | args = snewn(n, char *); |
2968 | args[0] = inst->progname; |
2969 | args[n-1] = NULL; |
2970 | for (i = 0; i < inst->ngtkargs; i++) |
2971 | args[i+1] = inst->gtkargvstart[i]; |
2972 | |
2973 | i++; |
2974 | va_start(ap, fd_to_close); |
2975 | while ((args[i++] = va_arg(ap, char *)) != NULL); |
2976 | va_end(ap); |
2977 | |
2978 | assert(i == n); |
2979 | |
2980 | /* |
2981 | * Do the double fork. |
2982 | */ |
2983 | pid = fork(); |
2984 | if (pid < 0) { |
2985 | perror("fork"); |
2986 | return; |
2987 | } |
2988 | |
2989 | if (pid == 0) { |
2990 | int pid2 = fork(); |
2991 | if (pid2 < 0) { |
2992 | perror("fork"); |
2993 | _exit(1); |
2994 | } else if (pid2 > 0) { |
2995 | /* |
2996 | * First child has successfully forked second child. My |
2997 | * Work Here Is Done. Note the use of _exit rather than |
2998 | * exit: the latter appears to cause destroy messages |
2999 | * to be sent to the X server. I suspect gtk uses |
3000 | * atexit. |
3001 | */ |
3002 | _exit(0); |
3003 | } |
3004 | |
3005 | /* |
3006 | * If we reach here, we are the second child, so we now |
3007 | * actually perform the exec. |
3008 | */ |
3009 | if (fd_to_close >= 0) |
3010 | close(fd_to_close); |
3011 | |
3012 | execv("/proc/self/exe", args); |
3013 | execvp(inst->progname, args); |
3014 | perror("exec"); |
3015 | _exit(127); |
3016 | |
3017 | } else { |
3018 | int status; |
3019 | waitpid(pid, &status, 0); |
3020 | } |
3021 | |
3022 | } |
3023 | |
3024 | void dup_session_menuitem(GtkMenuItem *item, gpointer gdata) |
3025 | { |
3026 | struct gui_data *inst = (struct gui_data *)gdata; |
3027 | /* |
3028 | * For this feature we must marshal cfg and (possibly) pty_argv |
3029 | * into a byte stream, create a pipe, and send this byte stream |
3030 | * to the child through the pipe. |
3031 | */ |
3032 | int i, ret, size; |
3033 | char *data; |
3034 | char option[80]; |
3035 | int pipefd[2]; |
3036 | |
3037 | if (pipe(pipefd) < 0) { |
3038 | perror("pipe"); |
3039 | return; |
3040 | } |
3041 | |
3042 | size = sizeof(inst->cfg); |
3043 | if (use_pty_argv && pty_argv) { |
3044 | for (i = 0; pty_argv[i]; i++) |
3045 | size += strlen(pty_argv[i]) + 1; |
3046 | } |
3047 | |
3048 | data = snewn(size, char); |
3049 | memcpy(data, &inst->cfg, sizeof(inst->cfg)); |
3050 | if (use_pty_argv && pty_argv) { |
3051 | int p = sizeof(inst->cfg); |
3052 | for (i = 0; pty_argv[i]; i++) { |
3053 | strcpy(data + p, pty_argv[i]); |
3054 | p += strlen(pty_argv[i]) + 1; |
3055 | } |
3056 | assert(p == size); |
3057 | } |
3058 | |
3059 | sprintf(option, "---[%d,%d]", pipefd[0], size); |
3060 | fcntl(pipefd[0], F_SETFD, 0); |
3061 | fork_and_exec_self(inst, pipefd[1], option, NULL); |
3062 | close(pipefd[0]); |
3063 | |
7ffdbc1a |
3064 | i = ret = 0; |
56801e3d |
3065 | while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0) |
3066 | i += ret; |
3067 | if (ret < 0) |
3068 | perror("write to pipe"); |
3069 | close(pipefd[1]); |
3070 | sfree(data); |
3071 | } |
3072 | |
3073 | int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg) |
3074 | { |
3075 | int fd, i, ret, size; |
3076 | char *data; |
3077 | |
3078 | if (sscanf(arg, "---[%d,%d]", &fd, &size) != 2) { |
3079 | fprintf(stderr, "%s: malformed magic argument `%s'\n", appname, arg); |
3080 | exit(1); |
3081 | } |
3082 | |
3083 | data = snewn(size, char); |
7ffdbc1a |
3084 | i = ret = 0; |
56801e3d |
3085 | while (i < size && (ret = read(fd, data + i, size - i)) > 0) |
3086 | i += ret; |
3087 | if (ret < 0) { |
3088 | perror("read from pipe"); |
3089 | exit(1); |
3090 | } else if (i < size) { |
3091 | fprintf(stderr, "%s: unexpected EOF in Duplicate Session data\n", |
3092 | appname); |
3093 | exit(1); |
3094 | } |
3095 | |
3096 | memcpy(cfg, data, sizeof(Config)); |
3097 | if (use_pty_argv && size > sizeof(Config)) { |
3098 | int n = 0; |
3099 | i = sizeof(Config); |
3100 | while (i < size) { |
3101 | while (i < size && data[i]) i++; |
3102 | if (i >= size) { |
3103 | fprintf(stderr, "%s: malformed Duplicate Session data\n", |
3104 | appname); |
3105 | exit(1); |
3106 | } |
3107 | i++; |
3108 | n++; |
3109 | } |
3110 | pty_argv = snewn(n+1, char *); |
3111 | pty_argv[n] = NULL; |
3112 | n = 0; |
3113 | i = sizeof(Config); |
3114 | while (i < size) { |
3115 | char *p = data + i; |
3116 | while (i < size && data[i]) i++; |
3117 | assert(i < size); |
3118 | i++; |
3119 | pty_argv[n++] = dupstr(p); |
3120 | } |
3121 | } |
3122 | |
3123 | return 0; |
3124 | } |
3125 | |
3126 | void new_session_menuitem(GtkMenuItem *item, gpointer data) |
3127 | { |
3128 | struct gui_data *inst = (struct gui_data *)data; |
3129 | |
3130 | fork_and_exec_self(inst, -1, NULL); |
3131 | } |
3132 | |
e3be8de5 |
3133 | void restart_session_menuitem(GtkMenuItem *item, gpointer data) |
3134 | { |
3135 | struct gui_data *inst = (struct gui_data *)data; |
3136 | |
3137 | if (!inst->back) { |
3138 | logevent(inst, "----- Session restarted -----"); |
900f9aca |
3139 | term_pwron(inst->term, FALSE); |
e3be8de5 |
3140 | start_backend(inst); |
3141 | inst->exited = FALSE; |
3142 | } |
3143 | } |
3144 | |
56801e3d |
3145 | void saved_session_menuitem(GtkMenuItem *item, gpointer data) |
3146 | { |
3147 | struct gui_data *inst = (struct gui_data *)data; |
3148 | char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data"); |
3149 | |
3150 | fork_and_exec_self(inst, -1, "-load", str, NULL); |
3151 | } |
3152 | |
3153 | void saved_session_freedata(GtkMenuItem *item, gpointer data) |
3154 | { |
3155 | char *str = (char *)gtk_object_get_data(GTK_OBJECT(item), "user-data"); |
3156 | |
3157 | sfree(str); |
3158 | } |
3159 | |
12745e35 |
3160 | static void update_savedsess_menu(GtkMenuItem *menuitem, gpointer data) |
3161 | { |
3162 | struct gui_data *inst = (struct gui_data *)data; |
3163 | struct sesslist sesslist; |
3164 | int i; |
3165 | |
3166 | gtk_container_foreach(GTK_CONTAINER(inst->sessionsmenu), |
3167 | (GtkCallback)gtk_widget_destroy, NULL); |
3168 | |
3169 | get_sesslist(&sesslist, TRUE); |
3170 | for (i = 1; i < sesslist.nsessions; i++) { |
3171 | GtkWidget *menuitem = |
3172 | gtk_menu_item_new_with_label(sesslist.sessions[i]); |
3173 | gtk_container_add(GTK_CONTAINER(inst->sessionsmenu), menuitem); |
3174 | gtk_widget_show(menuitem); |
3175 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
3176 | dupstr(sesslist.sessions[i])); |
3177 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
3178 | GTK_SIGNAL_FUNC(saved_session_menuitem), |
3179 | inst); |
3180 | gtk_signal_connect(GTK_OBJECT(menuitem), "destroy", |
3181 | GTK_SIGNAL_FUNC(saved_session_freedata), |
3182 | inst); |
3183 | } |
3184 | get_sesslist(&sesslist, FALSE); /* free up */ |
3185 | } |
3186 | |
85b1c49f |
3187 | void set_window_icon(GtkWidget *window, const char *const *const *icon, |
3188 | int n_icon) |
3189 | { |
3190 | GdkPixmap *iconpm; |
ae50c930 |
3191 | GdkBitmap *iconmask; |
85b1c49f |
3192 | #if GTK_CHECK_VERSION(2,0,0) |
3193 | GList *iconlist; |
3194 | int n; |
3195 | #endif |
3196 | |
3197 | if (!n_icon) |
3198 | return; |
3199 | |
3200 | gtk_widget_realize(window); |
ae50c930 |
3201 | iconpm = gdk_pixmap_create_from_xpm_d(window->window, &iconmask, |
85b1c49f |
3202 | NULL, (gchar **)icon[0]); |
ae50c930 |
3203 | gdk_window_set_icon(window->window, NULL, iconpm, iconmask); |
85b1c49f |
3204 | |
3205 | #if GTK_CHECK_VERSION(2,0,0) |
3206 | iconlist = NULL; |
3207 | for (n = 0; n < n_icon; n++) { |
3208 | iconlist = |
3209 | g_list_append(iconlist, |
3210 | gdk_pixbuf_new_from_xpm_data((const gchar **) |
3211 | icon[n])); |
3212 | } |
3213 | gdk_window_set_icon_list(window->window, iconlist); |
3214 | #endif |
3215 | } |
3216 | |
47e4e735 |
3217 | void update_specials_menu(void *frontend) |
3218 | { |
fbf6cb3b |
3219 | struct gui_data *inst = (struct gui_data *)frontend; |
47e4e735 |
3220 | |
3221 | const struct telnet_special *specials; |
3222 | |
e3be8de5 |
3223 | if (inst->back) |
3224 | specials = inst->back->get_specials(inst->backhandle); |
3225 | else |
3226 | specials = NULL; |
3227 | |
6f2d0cde |
3228 | /* I believe this disposes of submenus too. */ |
47e4e735 |
3229 | gtk_container_foreach(GTK_CONTAINER(inst->specialsmenu), |
3230 | (GtkCallback)gtk_widget_destroy, NULL); |
3231 | if (specials) { |
3232 | int i; |
6f2d0cde |
3233 | GtkWidget *menu = inst->specialsmenu; |
3234 | /* A lame "stack" for submenus that will do for now. */ |
3235 | GtkWidget *saved_menu = NULL; |
3236 | int nesting = 1; |
3237 | for (i = 0; nesting > 0; i++) { |
3238 | GtkWidget *menuitem = NULL; |
3239 | switch (specials[i].code) { |
3240 | case TS_SUBMENU: |
3241 | assert (nesting < 2); |
3242 | saved_menu = menu; /* XXX lame stacking */ |
3243 | menu = gtk_menu_new(); |
3244 | menuitem = gtk_menu_item_new_with_label(specials[i].name); |
3245 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), menu); |
3246 | gtk_container_add(GTK_CONTAINER(saved_menu), menuitem); |
3247 | gtk_widget_show(menuitem); |
3248 | menuitem = NULL; |
3249 | nesting++; |
3250 | break; |
3251 | case TS_EXITMENU: |
3252 | nesting--; |
3253 | if (nesting) { |
3254 | menu = saved_menu; /* XXX lame stacking */ |
3255 | saved_menu = NULL; |
3256 | } |
3257 | break; |
3258 | case TS_SEP: |
3259 | menuitem = gtk_menu_item_new(); |
3260 | break; |
3261 | default: |
47e4e735 |
3262 | menuitem = gtk_menu_item_new_with_label(specials[i].name); |
3263 | gtk_object_set_data(GTK_OBJECT(menuitem), "user-data", |
f7f9fb5c |
3264 | GINT_TO_POINTER(specials[i].code)); |
47e4e735 |
3265 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", |
3266 | GTK_SIGNAL_FUNC(special_menuitem), inst); |
6f2d0cde |
3267 | break; |
3268 | } |
3269 | if (menuitem) { |
3270 | gtk_container_add(GTK_CONTAINER(menu), menuitem); |
3271 | gtk_widget_show(menuitem); |
3272 | } |
47e4e735 |
3273 | } |
3274 | gtk_widget_show(inst->specialsitem1); |
3275 | gtk_widget_show(inst->specialsitem2); |
3276 | } else { |
3277 | gtk_widget_hide(inst->specialsitem1); |
3278 | gtk_widget_hide(inst->specialsitem2); |
3279 | } |
3280 | } |
3281 | |
e3be8de5 |
3282 | static void start_backend(struct gui_data *inst) |
faec60ed |
3283 | { |
1d009ae7 |
3284 | extern Backend *select_backend(Config *cfg); |
e3be8de5 |
3285 | char *realhost; |
3286 | const char *error; |
3287 | |
3288 | inst->back = select_backend(&inst->cfg); |
3289 | |
3290 | error = inst->back->init((void *)inst, &inst->backhandle, |
3291 | &inst->cfg, inst->cfg.host, inst->cfg.port, |
3292 | &realhost, inst->cfg.tcp_nodelay, |
3293 | inst->cfg.tcp_keepalives); |
3294 | |
3295 | if (error) { |
3296 | char *msg = dupprintf("Unable to open connection to %s:\n%s", |
3297 | inst->cfg.host, error); |
3298 | inst->exited = TRUE; |
3299 | fatal_message_box(inst->window, msg); |
3300 | sfree(msg); |
3301 | exit(0); |
3302 | } |
3303 | |
3304 | if (inst->cfg.wintitle[0]) { |
3305 | set_title(inst, inst->cfg.wintitle); |
3306 | set_icon(inst, inst->cfg.wintitle); |
3307 | } else { |
3308 | char *title = make_default_wintitle(realhost); |
3309 | set_title(inst, title); |
3310 | set_icon(inst, title); |
3311 | sfree(title); |
3312 | } |
820934c1 |
3313 | sfree(realhost); |
3314 | |
e3be8de5 |
3315 | inst->back->provide_logctx(inst->backhandle, inst->logctx); |
e3be8de5 |
3316 | |
3317 | term_provide_resize_fn(inst->term, inst->back->size, inst->backhandle); |
3318 | |
3319 | inst->ldisc = |
3320 | ldisc_create(&inst->cfg, inst->term, inst->back, inst->backhandle, |
3321 | inst); |
3322 | |
3323 | gtk_widget_hide(inst->restartitem); |
3324 | } |
3325 | |
3326 | int pt_main(int argc, char **argv) |
3327 | { |
1d009ae7 |
3328 | extern int cfgbox(Config *cfg); |
a8327734 |
3329 | struct gui_data *inst; |
faec60ed |
3330 | |
56801e3d |
3331 | /* |
3332 | * Create an instance structure and initialise to zeroes |
3333 | */ |
3334 | inst = snew(struct gui_data); |
3335 | memset(inst, 0, sizeof(*inst)); |
3336 | inst->alt_keycode = -1; /* this one needs _not_ to be zero */ |
755e0524 |
3337 | inst->busy_status = BUSY_NOT; |
56801e3d |
3338 | |
0f33f9d1 |
3339 | /* defer any child exit handling until we're ready to deal with |
3340 | * it */ |
3341 | block_signal(SIGCHLD, 1); |
3342 | |
56801e3d |
3343 | inst->progname = argv[0]; |
7428c509 |
3344 | /* |
56801e3d |
3345 | * Copy the original argv before letting gtk_init fiddle with |
3346 | * it. It will be required later. |
7428c509 |
3347 | */ |
56801e3d |
3348 | { |
3349 | int i, oldargc; |
3350 | inst->gtkargvstart = snewn(argc-1, char *); |
3351 | for (i = 1; i < argc; i++) |
3352 | inst->gtkargvstart[i-1] = dupstr(argv[i]); |
3353 | oldargc = argc; |
3354 | gtk_init(&argc, &argv); |
3355 | inst->ngtkargs = oldargc - argc; |
3356 | } |
7428c509 |
3357 | |
56801e3d |
3358 | if (argc > 1 && !strncmp(argv[1], "---", 3)) { |
3359 | read_dupsession_data(inst, &inst->cfg, argv[1]); |
3360 | /* Splatter this argument so it doesn't clutter a ps listing */ |
3361 | memset(argv[1], 0, strlen(argv[1])); |
3362 | } else { |
df7c7e52 |
3363 | /* By default, we bring up the config dialog, rather than launching |
3364 | * a session. This gets set to TRUE if something happens to change |
3365 | * that (e.g., a hostname is specified on the command-line). */ |
3366 | int allow_launch = FALSE; |
3367 | if (do_cmdline(argc, argv, 0, &allow_launch, inst, &inst->cfg)) |
56801e3d |
3368 | exit(1); /* pre-defaults pass to get -class */ |
3369 | do_defaults(NULL, &inst->cfg); |
df7c7e52 |
3370 | if (do_cmdline(argc, argv, 1, &allow_launch, inst, &inst->cfg)) |
56801e3d |
3371 | exit(1); /* post-defaults, do everything */ |
9dc625f0 |
3372 | |
56801e3d |
3373 | cmdline_run_saved(&inst->cfg); |
46a3419b |
3374 | |
df7c7e52 |
3375 | if (loaded_session) |
3376 | allow_launch = TRUE; |
3377 | |
3378 | if ((!allow_launch || !cfg_launchable(&inst->cfg)) && |
95a9448f |
3379 | !cfgbox(&inst->cfg)) |
56801e3d |
3380 | exit(0); /* config box hit Cancel */ |
3381 | } |
1d009ae7 |
3382 | |
8c161662 |
3383 | if (!compound_text_atom) |
3384 | compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE); |
3385 | if (!utf8_string_atom) |
3386 | utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE); |
dd72dfa3 |
3387 | |
f160b7b8 |
3388 | inst->area = gtk_drawing_area_new(); |
3389 | |
56b9b9a7 |
3390 | setup_fonts_ucs(inst); |
3aac0d35 |
3391 | init_cutbuffers(); |
1709795f |
3392 | |
12d200b1 |
3393 | inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
3394 | |
16891265 |
3395 | /* |
3396 | * Set up the colour map. |
3397 | */ |
a8327734 |
3398 | palette_reset(inst); |
16891265 |
3399 | |
56b9b9a7 |
3400 | inst->width = inst->cfg.width; |
3401 | inst->height = inst->cfg.height; |
3402 | |
f7f27309 |
3403 | gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area), |
3ea863a3 |
3404 | inst->font_width * inst->cfg.width + 2*inst->cfg.window_border, |
3405 | inst->font_height * inst->cfg.height + 2*inst->cfg.window_border); |
56b9b9a7 |
3406 | inst->sbar_adjust = GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0)); |
3407 | inst->sbar = gtk_vscrollbar_new(inst->sbar_adjust); |
6a5e84dd |
3408 | inst->hbox = GTK_BOX(gtk_hbox_new(FALSE, 0)); |
56b9b9a7 |
3409 | /* |
3410 | * We always create the scrollbar; it remains invisible if |
3411 | * unwanted, so we can pop it up quickly if it suddenly becomes |
3412 | * desirable. |
3413 | */ |
3414 | if (inst->cfg.scrollbar_on_left) |
3415 | gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0); |
88e6b9ca |
3416 | gtk_box_pack_start(inst->hbox, inst->area, TRUE, TRUE, 0); |
56b9b9a7 |
3417 | if (!inst->cfg.scrollbar_on_left) |
3418 | gtk_box_pack_start(inst->hbox, inst->sbar, FALSE, FALSE, 0); |
6a5e84dd |
3419 | |
12d200b1 |
3420 | gtk_container_add(GTK_CONTAINER(inst->window), GTK_WIDGET(inst->hbox)); |
f7f27309 |
3421 | |
56b9b9a7 |
3422 | set_geom_hints(inst); |
f7f27309 |
3423 | |
aca2a702 |
3424 | gtk_widget_show(inst->area); |
3425 | if (inst->cfg.scrollbar) |
3426 | gtk_widget_show(inst->sbar); |
56b9b9a7 |
3427 | else |
3428 | gtk_widget_hide(inst->sbar); |
aca2a702 |
3429 | gtk_widget_show(GTK_WIDGET(inst->hbox)); |
3430 | |
3431 | if (inst->gotpos) { |
3432 | int x = inst->xpos, y = inst->ypos; |
3433 | GtkRequisition req; |
3434 | gtk_widget_size_request(GTK_WIDGET(inst->window), &req); |
3435 | if (inst->gravity & 1) x += gdk_screen_width() - req.width; |
3436 | if (inst->gravity & 2) y += gdk_screen_height() - req.height; |
3437 | gtk_window_set_position(GTK_WINDOW(inst->window), GTK_WIN_POS_NONE); |
3438 | gtk_widget_set_uposition(GTK_WIDGET(inst->window), x, y); |
3439 | } |
3440 | |
12d200b1 |
3441 | gtk_signal_connect(GTK_OBJECT(inst->window), "destroy", |
f7f27309 |
3442 | GTK_SIGNAL_FUNC(destroy), inst); |
12d200b1 |
3443 | gtk_signal_connect(GTK_OBJECT(inst->window), "delete_event", |
f7f27309 |
3444 | GTK_SIGNAL_FUNC(delete_window), inst); |
12d200b1 |
3445 | gtk_signal_connect(GTK_OBJECT(inst->window), "key_press_event", |
f7f27309 |
3446 | GTK_SIGNAL_FUNC(key_event), inst); |
c33c3d76 |
3447 | gtk_signal_connect(GTK_OBJECT(inst->window), "key_release_event", |
3448 | GTK_SIGNAL_FUNC(key_event), inst); |
12d200b1 |
3449 | gtk_signal_connect(GTK_OBJECT(inst->window), "focus_in_event", |
f7f27309 |
3450 | GTK_SIGNAL_FUNC(focus_event), inst); |
12d200b1 |
3451 | gtk_signal_connect(GTK_OBJECT(inst->window), "focus_out_event", |
f7f27309 |
3452 | GTK_SIGNAL_FUNC(focus_event), inst); |
3453 | gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event", |
3454 | GTK_SIGNAL_FUNC(configure_area), inst); |
3455 | gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event", |
3456 | GTK_SIGNAL_FUNC(expose_area), inst); |
e6346999 |
3457 | gtk_signal_connect(GTK_OBJECT(inst->area), "button_press_event", |
3458 | GTK_SIGNAL_FUNC(button_event), inst); |
3459 | gtk_signal_connect(GTK_OBJECT(inst->area), "button_release_event", |
3460 | GTK_SIGNAL_FUNC(button_event), inst); |
2e563db5 |
3461 | #if GTK_CHECK_VERSION(2,0,0) |
3462 | gtk_signal_connect(GTK_OBJECT(inst->area), "scroll_event", |
3463 | GTK_SIGNAL_FUNC(scroll_event), inst); |
3464 | #endif |
e6346999 |
3465 | gtk_signal_connect(GTK_OBJECT(inst->area), "motion_notify_event", |
3466 | GTK_SIGNAL_FUNC(motion_event), inst); |
3467 | gtk_signal_connect(GTK_OBJECT(inst->area), "selection_received", |
3468 | GTK_SIGNAL_FUNC(selection_received), inst); |
3469 | gtk_signal_connect(GTK_OBJECT(inst->area), "selection_get", |
3470 | GTK_SIGNAL_FUNC(selection_get), inst); |
3471 | gtk_signal_connect(GTK_OBJECT(inst->area), "selection_clear_event", |
3472 | GTK_SIGNAL_FUNC(selection_clear), inst); |
3ea863a3 |
3473 | if (inst->cfg.scrollbar) |
330f49be |
3474 | gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed", |
3475 | GTK_SIGNAL_FUNC(scrollbar_moved), inst); |
f7f27309 |
3476 | gtk_widget_add_events(GTK_WIDGET(inst->area), |
e6346999 |
3477 | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | |
3478 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | |
57e636ca |
3479 | GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK); |
f7f27309 |
3480 | |
85b1c49f |
3481 | { |
3482 | extern const char *const *const main_icon[]; |
3483 | extern const int n_main_icon; |
3484 | set_window_icon(inst->window, main_icon, n_main_icon); |
3485 | } |
3486 | |
12d200b1 |
3487 | gtk_widget_show(inst->window); |
f7f27309 |
3488 | |
a8327734 |
3489 | set_window_background(inst); |
7428c509 |
3490 | |
47e4e735 |
3491 | /* |
3492 | * Set up the Ctrl+rightclick context menu. |
3493 | */ |
3494 | { |
3495 | GtkWidget *menuitem; |
3496 | char *s; |
56801e3d |
3497 | extern const int use_event_log, new_session, saved_sessions; |
47e4e735 |
3498 | |
3499 | inst->menu = gtk_menu_new(); |
3500 | |
3501 | #define MKMENUITEM(title, func) do { \ |
3502 | menuitem = title ? gtk_menu_item_new_with_label(title) : \ |
3503 | gtk_menu_item_new(); \ |
3504 | gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \ |
3505 | gtk_widget_show(menuitem); \ |
3506 | if (func != NULL) \ |
3507 | gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \ |
3508 | GTK_SIGNAL_FUNC(func), inst); \ |
3509 | } while (0) |
56801e3d |
3510 | if (new_session) |
3511 | MKMENUITEM("New Session", new_session_menuitem); |
e3be8de5 |
3512 | MKMENUITEM("Restart Session", restart_session_menuitem); |
3513 | inst->restartitem = menuitem; |
3514 | gtk_widget_hide(inst->restartitem); |
56801e3d |
3515 | MKMENUITEM("Duplicate Session", dup_session_menuitem); |
3516 | if (saved_sessions) { |
56801e3d |
3517 | inst->sessionsmenu = gtk_menu_new(); |
12745e35 |
3518 | /* sessionsmenu will be updated when it's invoked */ |
3519 | /* XXX is this the right way to do dynamic menus in Gtk? */ |
3520 | MKMENUITEM("Saved Sessions", update_savedsess_menu); |
56801e3d |
3521 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), |
3522 | inst->sessionsmenu); |
3523 | } |
3524 | MKMENUITEM(NULL, NULL); |
56b9b9a7 |
3525 | MKMENUITEM("Change Settings", change_settings_menuitem); |
3526 | MKMENUITEM(NULL, NULL); |
8eed910d |
3527 | if (use_event_log) |
3528 | MKMENUITEM("Event Log", event_log_menuitem); |
47e4e735 |
3529 | MKMENUITEM("Special Commands", NULL); |
3530 | inst->specialsmenu = gtk_menu_new(); |
3531 | gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), inst->specialsmenu); |
3532 | inst->specialsitem1 = menuitem; |
3533 | MKMENUITEM(NULL, NULL); |
3534 | inst->specialsitem2 = menuitem; |
533b1743 |
3535 | gtk_widget_hide(inst->specialsitem1); |
3536 | gtk_widget_hide(inst->specialsitem2); |
47e4e735 |
3537 | MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem); |
3538 | MKMENUITEM("Reset Terminal", reset_terminal_menuitem); |
d9e5c333 |
3539 | MKMENUITEM("Copy All", copy_all_menuitem); |
47e4e735 |
3540 | MKMENUITEM(NULL, NULL); |
3541 | s = dupcat("About ", appname, NULL); |
3542 | MKMENUITEM(s, about_menuitem); |
3543 | sfree(s); |
3544 | #undef MKMENUITEM |
3545 | } |
3546 | |
a8327734 |
3547 | inst->textcursor = make_mouse_ptr(inst, GDK_XTERM); |
3548 | inst->rawcursor = make_mouse_ptr(inst, GDK_LEFT_PTR); |
755e0524 |
3549 | inst->waitcursor = make_mouse_ptr(inst, GDK_WATCH); |
a8327734 |
3550 | inst->blankcursor = make_mouse_ptr(inst, -1); |
3551 | make_mouse_ptr(inst, -2); /* clean up cursor font */ |
57e636ca |
3552 | inst->currcursor = inst->textcursor; |
a8327734 |
3553 | show_mouseptr(inst, 1); |
d64838e1 |
3554 | |
8eed910d |
3555 | inst->eventlogstuff = eventlogstuff_new(); |
3556 | |
21d2b241 |
3557 | inst->term = term_init(&inst->cfg, &inst->ucsdata, inst); |
3ea863a3 |
3558 | inst->logctx = log_init(inst, &inst->cfg); |
a8327734 |
3559 | term_provide_logctx(inst->term, inst->logctx); |
887035a5 |
3560 | |
74aca06d |
3561 | uxsel_init(); |
3562 | |
3f935d5b |
3563 | term_size(inst->term, inst->cfg.height, inst->cfg.width, inst->cfg.savelines); |
3564 | |
e3be8de5 |
3565 | start_backend(inst); |
755a6d84 |
3566 | |
b9d7bcad |
3567 | ldisc_send(inst->ldisc, NULL, 0, 0);/* cause ldisc to notice changes */ |
755a6d84 |
3568 | |
0f33f9d1 |
3569 | /* now we're reday to deal with the child exit handler being |
3570 | * called */ |
3571 | block_signal(SIGCHLD, 0); |
74aca06d |
3572 | |
755e29c6 |
3573 | /* |
3574 | * Block SIGPIPE: if we attempt Duplicate Session or similar |
3575 | * and it falls over in some way, we certainly don't want |
3576 | * SIGPIPE terminating the main pterm/PuTTY. Note that we do |
3577 | * this _after_ (at least pterm) forks off its child process, |
3578 | * since the child wants SIGPIPE handled in the usual way. |
3579 | */ |
3580 | block_signal(SIGPIPE, 1); |
3581 | |
74aca06d |
3582 | inst->exited = FALSE; |
3583 | |
f7f27309 |
3584 | gtk_main(); |
3585 | |
3586 | return 0; |
3587 | } |