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