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