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