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