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