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