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