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.
18 #include <sys/types.h>
21 #include <gdk/gdkkeysyms.h>
24 #include <X11/Xutil.h>
26 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
31 #define CAT2(x,y) x ## y
32 #define CAT(x,y) CAT2(x,y)
33 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
35 #define NCOLOURS (lenof(((Config *)0)->colours))
37 GdkAtom compound_text_atom
, utf8_string_atom
;
39 extern char **pty_argv
; /* declared in pty.c */
40 extern int use_pty_argv
;
43 GtkWidget
*window
, *area
, *sbar
;
45 GtkAdjustment
*sbar_adjust
;
46 GtkWidget
*menu
, *specialsmenu
, *specialsitem1
, *specialsitem2
;
47 GtkWidget
*sessionsmenu
;
49 GdkFont
*fonts
[4]; /* normal, bold, wide, widebold */
54 int xpos
, ypos
, gotpos
, gravity
;
55 GdkCursor
*rawcursor
, *textcursor
, *blankcursor
, *currcursor
;
56 GdkColor cols
[NCOLOURS
];
58 wchar_t *pastein_data
;
61 char *pasteout_data
, *pasteout_data_utf8
;
62 int pasteout_data_len
, pasteout_data_utf8_len
;
63 int font_width
, font_height
;
67 guint term_paste_idle_id
;
70 char wintitle
[sizeof(((Config
*)0)->wintitle
)];
71 char icontitle
[sizeof(((Config
*)0)->wintitle
)];
72 int master_fd
, master_func_id
;
79 struct unicode_data ucsdata
;
82 char *progname
, **gtkargvstart
;
88 struct gui_data
*inst
;
91 static int send_raw_mouse
;
93 static char *app_name
= "pterm";
95 char *x_get_default(const char *key
)
97 return XGetDefault(GDK_DISPLAY(), app_name
, key
);
100 void connection_fatal(void *frontend
, char *p
, ...)
102 struct gui_data
*inst
= (struct gui_data
*)frontend
;
107 msg
= dupvprintf(p
, ap
);
110 fatal_message_box(inst
->window
, msg
);
112 if (inst
->cfg
.close_on_exit
== FORCE_ON
)
117 * Default settings that are specific to pterm.
119 FontSpec
platform_default_fontspec(const char *name
)
122 if (!strcmp(name
, "Font"))
123 strcpy(ret
.name
, "fixed");
129 Filename
platform_default_filename(const char *name
)
132 if (!strcmp(name
, "LogFileName"))
133 strcpy(ret
.path
, "putty.log");
139 char *platform_default_s(const char *name
)
144 int platform_default_i(const char *name
, int def
)
146 if (!strcmp(name
, "CloseOnExit"))
147 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
148 if (!strcmp(name
, "WinNameAlways"))
149 return 0; /* X natively supports icon titles, so use 'em by default */
153 void ldisc_update(void *frontend
, int echo
, int edit
)
156 * This is a stub in pterm. If I ever produce a Unix
157 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
158 * then it will require some termios manoeuvring analogous to
159 * that in the Windows plink.c, but here it's meaningless.
163 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
165 struct gui_data
*inst
= (struct gui_data
*)frontend
;
166 return term_data(inst
->term
, is_stderr
, data
, len
);
169 void logevent(void *frontend
, char *string
)
171 struct gui_data
*inst
= (struct gui_data
*)frontend
;
173 log_eventlog(inst
->logctx
, string
);
175 logevent_dlg(inst
->eventlogstuff
, string
);
178 int font_dimension(void *frontend
, int which
)/* 0 for width, 1 for height */
180 struct gui_data
*inst
= (struct gui_data
*)frontend
;
183 return inst
->font_height
;
185 return inst
->font_width
;
189 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
190 * into a cooked one (SELECT, EXTEND, PASTE).
192 * In Unix, this is not configurable; the X button arrangement is
193 * rock-solid across all applications, everyone has a three-button
194 * mouse or a means of faking it, and there is no need to switch
195 * buttons around at all.
197 static Mouse_Button
translate_button(Mouse_Button button
)
199 /* struct gui_data *inst = (struct gui_data *)frontend; */
201 if (button
== MBT_LEFT
)
203 if (button
== MBT_MIDDLE
)
205 if (button
== MBT_RIGHT
)
207 return 0; /* shouldn't happen */
211 * Return the top-level GtkWindow associated with a particular
212 * front end instance.
214 void *get_window(void *frontend
)
216 struct gui_data
*inst
= (struct gui_data
*)frontend
;
221 * Minimise or restore the window in response to a server-side
224 void set_iconic(void *frontend
, int iconic
)
227 * GTK 1.2 doesn't know how to do this.
229 #if GTK_CHECK_VERSION(2,0,0)
230 struct gui_data
*inst
= (struct gui_data
*)frontend
;
232 gtk_window_iconify(GTK_WINDOW(inst
->window
));
234 gtk_window_deiconify(GTK_WINDOW(inst
->window
));
239 * Move the window in response to a server-side request.
241 void move_window(void *frontend
, int x
, int y
)
243 struct gui_data
*inst
= (struct gui_data
*)frontend
;
245 * I assume that when the GTK version of this call is available
246 * we should use it. Not sure how it differs from the GDK one,
249 #if GTK_CHECK_VERSION(2,0,0)
250 gtk_window_move(GTK_WINDOW(inst
->window
), x
, y
);
252 gdk_window_move(inst
->window
->window
, x
, y
);
257 * Move the window to the top or bottom of the z-order in response
258 * to a server-side request.
260 void set_zorder(void *frontend
, int top
)
262 struct gui_data
*inst
= (struct gui_data
*)frontend
;
264 gdk_window_raise(inst
->window
->window
);
266 gdk_window_lower(inst
->window
->window
);
270 * Refresh the window in response to a server-side request.
272 void refresh_window(void *frontend
)
274 struct gui_data
*inst
= (struct gui_data
*)frontend
;
275 term_invalidate(inst
->term
);
279 * Maximise or restore the window in response to a server-side
282 void set_zoomed(void *frontend
, int zoomed
)
285 * GTK 1.2 doesn't know how to do this.
287 #if GTK_CHECK_VERSION(2,0,0)
288 struct gui_data
*inst
= (struct gui_data
*)frontend
;
290 gtk_window_maximize(GTK_WINDOW(inst
->window
));
292 gtk_window_unmaximize(GTK_WINDOW(inst
->window
));
297 * Report whether the window is iconic, for terminal reports.
299 int is_iconic(void *frontend
)
301 struct gui_data
*inst
= (struct gui_data
*)frontend
;
302 return !gdk_window_is_viewable(inst
->window
->window
);
306 * Report the window's position, for terminal reports.
308 void get_window_pos(void *frontend
, int *x
, int *y
)
310 struct gui_data
*inst
= (struct gui_data
*)frontend
;
312 * I assume that when the GTK version of this call is available
313 * we should use it. Not sure how it differs from the GDK one,
316 #if GTK_CHECK_VERSION(2,0,0)
317 gtk_window_get_position(GTK_WINDOW(inst
->window
), x
, y
);
319 gdk_window_get_position(inst
->window
->window
, x
, y
);
324 * Report the window's pixel size, for terminal reports.
326 void get_window_pixels(void *frontend
, int *x
, int *y
)
328 struct gui_data
*inst
= (struct gui_data
*)frontend
;
330 * I assume that when the GTK version of this call is available
331 * we should use it. Not sure how it differs from the GDK one,
334 #if GTK_CHECK_VERSION(2,0,0)
335 gtk_window_get_size(GTK_WINDOW(inst
->window
), x
, y
);
337 gdk_window_get_size(inst
->window
->window
, x
, y
);
342 * Return the window or icon title.
344 char *get_window_title(void *frontend
, int icon
)
346 struct gui_data
*inst
= (struct gui_data
*)frontend
;
347 return icon ? inst
->wintitle
: inst
->icontitle
;
350 gint
delete_window(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
352 struct gui_data
*inst
= (struct gui_data
*)data
;
353 if (!inst
->exited
&& inst
->cfg
.warn_on_close
) {
354 if (!reallyclose(inst
))
360 static void show_mouseptr(struct gui_data
*inst
, int show
)
362 if (!inst
->cfg
.hide_mouseptr
)
365 gdk_window_set_cursor(inst
->area
->window
, inst
->currcursor
);
367 gdk_window_set_cursor(inst
->area
->window
, inst
->blankcursor
);
368 inst
->mouseptr_visible
= show
;
371 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
373 struct gui_data
*inst
= (struct gui_data
*)data
;
374 int w
, h
, need_size
= 0;
378 * See if the terminal size has changed, in which case we must
379 * let the terminal know.
381 w
= (event
->width
- 2*inst
->cfg
.window_border
) / inst
->font_width
;
382 h
= (event
->height
- 2*inst
->cfg
.window_border
) / inst
->font_height
;
383 if (w
!= inst
->width
|| h
!= inst
->height
) {
384 inst
->cfg
.width
= inst
->width
= w
;
385 inst
->cfg
.height
= inst
->height
= h
;
390 gdk_pixmap_unref(inst
->pixmap
);
394 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
395 (inst
->cfg
.width
* inst
->font_width
+
396 2*inst
->cfg
.window_border
),
397 (inst
->cfg
.height
* inst
->font_height
+
398 2*inst
->cfg
.window_border
), -1);
400 gc
= gdk_gc_new(inst
->area
->window
);
401 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
402 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
403 inst
->cfg
.width
* inst
->font_width
+ 2*inst
->cfg
.window_border
,
404 inst
->cfg
.height
* inst
->font_height
+ 2*inst
->cfg
.window_border
);
407 if (need_size
&& inst
->term
) {
408 term_size(inst
->term
, h
, w
, inst
->cfg
.savelines
);
412 term_invalidate(inst
->term
);
417 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
419 struct gui_data
*inst
= (struct gui_data
*)data
;
422 * Pass the exposed rectangle to terminal.c, which will call us
423 * back to do the actual painting.
426 gdk_draw_pixmap(widget
->window
,
427 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
429 event
->area
.x
, event
->area
.y
,
430 event
->area
.x
, event
->area
.y
,
431 event
->area
.width
, event
->area
.height
);
436 #define KEY_PRESSED(k) \
437 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
439 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
441 struct gui_data
*inst
= (struct gui_data
*)data
;
443 int start
, end
, special
;
445 /* By default, nothing is generated. */
450 * If Alt is being released after typing an Alt+numberpad
451 * sequence, we should generate the code that was typed.
453 * Note that we only do this if more than one key was actually
454 * pressed - I don't think Alt+NumPad4 should be ^D or that
455 * Alt+NumPad3 should be ^C, for example. There's no serious
456 * inconvenience in having to type a zero before a single-digit
459 if (event
->type
== GDK_KEY_RELEASE
&&
460 (event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
461 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
) &&
462 inst
->alt_keycode
>= 0 && inst
->alt_digits
> 1) {
464 printf("Alt key up, keycode = %d\n", inst
->alt_keycode
);
466 output
[0] = inst
->alt_keycode
;
471 if (event
->type
== GDK_KEY_PRESS
) {
475 printf("keypress: keyval = %04x, state = %08x; string =",
476 event
->keyval
, event
->state
);
477 for (i
= 0; event
->string
[i
]; i
++)
478 printf(" %02x", (unsigned char) event
->string
[i
]);
484 * NYI: Compose key (!!! requires Unicode faff before even trying)
488 * If Alt has just been pressed, we start potentially
489 * accumulating an Alt+numberpad code. We do this by
490 * setting alt_keycode to -1 (nothing yet but plausible).
492 if ((event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
493 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
)) {
494 inst
->alt_keycode
= -1;
495 inst
->alt_digits
= 0;
496 goto done
; /* this generates nothing else */
500 * If we're seeing a numberpad key press with Mod1 down,
501 * consider adding it to alt_keycode if that's sensible.
502 * Anything _else_ with Mod1 down cancels any possibility
503 * of an ALT keycode: we set alt_keycode to -2.
505 if ((event
->state
& GDK_MOD1_MASK
) && inst
->alt_keycode
!= -2) {
507 switch (event
->keyval
) {
508 case GDK_KP_0
: case GDK_KP_Insert
: digit
= 0; break;
509 case GDK_KP_1
: case GDK_KP_End
: digit
= 1; break;
510 case GDK_KP_2
: case GDK_KP_Down
: digit
= 2; break;
511 case GDK_KP_3
: case GDK_KP_Page_Down
: digit
= 3; break;
512 case GDK_KP_4
: case GDK_KP_Left
: digit
= 4; break;
513 case GDK_KP_5
: case GDK_KP_Begin
: digit
= 5; break;
514 case GDK_KP_6
: case GDK_KP_Right
: digit
= 6; break;
515 case GDK_KP_7
: case GDK_KP_Home
: digit
= 7; break;
516 case GDK_KP_8
: case GDK_KP_Up
: digit
= 8; break;
517 case GDK_KP_9
: case GDK_KP_Page_Up
: digit
= 9; break;
520 inst
->alt_keycode
= -2; /* it's invalid */
523 printf("Adding digit %d to keycode %d", digit
,
526 if (inst
->alt_keycode
== -1)
527 inst
->alt_keycode
= digit
; /* one-digit code */
529 inst
->alt_keycode
= inst
->alt_keycode
* 10 + digit
;
532 printf(" gives new code %d\n", inst
->alt_keycode
);
534 /* Having used this digit, we now do nothing more with it. */
540 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
543 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
544 term_scroll(inst
->term
, 0, -inst
->cfg
.height
/2);
547 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_CONTROL_MASK
)) {
548 term_scroll(inst
->term
, 0, -1);
551 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
552 term_scroll(inst
->term
, 0, +inst
->cfg
.height
/2);
555 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_CONTROL_MASK
)) {
556 term_scroll(inst
->term
, 0, +1);
561 * Neither does Shift-Ins.
563 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
570 /* ALT+things gives leading Escape. */
572 strncpy(output
+1, event
->string
, 31);
574 end
= strlen(output
);
575 if (event
->state
& GDK_MOD1_MASK
) {
577 if (end
== 1) end
= 0;
581 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
582 if (!event
->string
[0] && event
->keyval
== '`' &&
583 (event
->state
& GDK_CONTROL_MASK
)) {
588 /* Control-Break is the same as Control-C */
589 if (event
->keyval
== GDK_Break
&&
590 (event
->state
& GDK_CONTROL_MASK
)) {
596 /* We handle Return ourselves, because it needs to be flagged as
597 * special to ldisc. */
598 if (event
->keyval
== GDK_Return
) {
604 /* Control-2, Control-Space and Control-@ are NUL */
605 if (!event
->string
[0] &&
606 (event
->keyval
== ' ' || event
->keyval
== '2' ||
607 event
->keyval
== '@') &&
608 (event
->state
& (GDK_SHIFT_MASK
|
609 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
614 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
615 if (!event
->string
[0] && event
->keyval
== ' ' &&
616 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
617 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
622 /* We don't let GTK tell us what Backspace is! We know better. */
623 if (event
->keyval
== GDK_BackSpace
&&
624 !(event
->state
& GDK_SHIFT_MASK
)) {
625 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x7F' : '\x08';
629 /* For Shift Backspace, do opposite of what is configured. */
630 if (event
->keyval
== GDK_BackSpace
&&
631 (event
->state
& GDK_SHIFT_MASK
)) {
632 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x08' : '\x7F';
637 /* Shift-Tab is ESC [ Z */
638 if (event
->keyval
== GDK_ISO_Left_Tab
||
639 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
640 end
= 1 + sprintf(output
+1, "\033[Z");
644 * NetHack keypad mode.
646 if (inst
->cfg
.nethack_keypad
) {
648 switch (event
->keyval
) {
649 case GDK_KP_1
: case GDK_KP_End
: keys
= "bB"; break;
650 case GDK_KP_2
: case GDK_KP_Down
: keys
= "jJ"; break;
651 case GDK_KP_3
: case GDK_KP_Page_Down
: keys
= "nN"; break;
652 case GDK_KP_4
: case GDK_KP_Left
: keys
= "hH"; break;
653 case GDK_KP_5
: case GDK_KP_Begin
: keys
= ".."; break;
654 case GDK_KP_6
: case GDK_KP_Right
: keys
= "lL"; break;
655 case GDK_KP_7
: case GDK_KP_Home
: keys
= "yY"; break;
656 case GDK_KP_8
: case GDK_KP_Up
: keys
= "kK"; break;
657 case GDK_KP_9
: case GDK_KP_Page_Up
: keys
= "uU"; break;
661 if (event
->state
& GDK_SHIFT_MASK
)
670 * Application keypad mode.
672 if (inst
->term
->app_keypad_keys
&& !inst
->cfg
.no_applic_k
) {
674 switch (event
->keyval
) {
675 case GDK_Num_Lock
: xkey
= 'P'; break;
676 case GDK_KP_Divide
: xkey
= 'Q'; break;
677 case GDK_KP_Multiply
: xkey
= 'R'; break;
678 case GDK_KP_Subtract
: xkey
= 'S'; break;
680 * Keypad + is tricky. It covers a space that would
681 * be taken up on the VT100 by _two_ keys; so we
682 * let Shift select between the two. Worse still,
683 * in xterm function key mode we change which two...
686 if (inst
->cfg
.funky_type
== 2) {
687 if (event
->state
& GDK_SHIFT_MASK
)
691 } else if (event
->state
& GDK_SHIFT_MASK
)
696 case GDK_KP_Enter
: xkey
= 'M'; break;
697 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
698 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
699 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
700 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
701 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
702 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
703 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
704 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
705 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
706 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
707 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
710 if (inst
->term
->vt52_mode
) {
711 if (xkey
>= 'P' && xkey
<= 'S')
712 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
714 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
716 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
722 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
723 * for integer decimal nn.)
725 * We also deal with the weird ones here. Linux VCs replace F1
726 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
727 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
732 switch (event
->keyval
) {
734 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
737 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
740 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
743 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
746 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
749 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
752 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
755 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
758 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
761 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
794 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
795 case GDK_Home
: case GDK_KP_Home
:
798 case GDK_Insert
: case GDK_KP_Insert
:
801 case GDK_Delete
: case GDK_KP_Delete
:
804 case GDK_End
: case GDK_KP_End
:
807 case GDK_Page_Up
: case GDK_KP_Page_Up
:
810 case GDK_Page_Down
: case GDK_KP_Page_Down
:
814 /* Reorder edit keys to physical order */
815 if (inst
->cfg
.funky_type
== 3 && code
<= 6)
816 code
= "\0\2\1\4\5\3\6"[code
];
818 if (inst
->term
->vt52_mode
&& code
> 0 && code
<= 6) {
819 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
823 if (inst
->cfg
.funky_type
== 5 && /* SCO function keys */
824 code
>= 11 && code
<= 34) {
825 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
827 switch (event
->keyval
) {
828 case GDK_F1
: index
= 0; break;
829 case GDK_F2
: index
= 1; break;
830 case GDK_F3
: index
= 2; break;
831 case GDK_F4
: index
= 3; break;
832 case GDK_F5
: index
= 4; break;
833 case GDK_F6
: index
= 5; break;
834 case GDK_F7
: index
= 6; break;
835 case GDK_F8
: index
= 7; break;
836 case GDK_F9
: index
= 8; break;
837 case GDK_F10
: index
= 9; break;
838 case GDK_F11
: index
= 10; break;
839 case GDK_F12
: index
= 11; break;
841 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
842 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
843 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
846 if (inst
->cfg
.funky_type
== 5 && /* SCO small keypad */
847 code
>= 1 && code
<= 6) {
848 char codes
[] = "HL.FIG";
853 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
857 if ((inst
->term
->vt52_mode
|| inst
->cfg
.funky_type
== 4) &&
858 code
>= 11 && code
<= 24) {
864 if (inst
->term
->vt52_mode
)
865 end
= 1 + sprintf(output
+1,
866 "\x1B%c", code
+ 'P' - 11 - offt
);
868 end
= 1 + sprintf(output
+1,
869 "\x1BO%c", code
+ 'P' - 11 - offt
);
872 if (inst
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
873 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
876 if (inst
->cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
877 if (inst
->term
->vt52_mode
)
878 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
880 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
883 if (inst
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
884 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
888 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
894 * Cursor keys. (This includes the numberpad cursor keys,
895 * if we haven't already done them due to app keypad mode.)
897 * Here we also process un-numlocked un-appkeypadded KP5,
898 * which sends ESC [ G.
902 switch (event
->keyval
) {
903 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
904 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
905 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
906 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
907 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
911 * The arrow keys normally do ESC [ A and so on. In
912 * app cursor keys mode they do ESC O A instead.
913 * Ctrl toggles the two modes.
915 if (inst
->term
->vt52_mode
) {
916 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
917 } else if (!inst
->term
->app_cursor_keys
^
918 !(event
->state
& GDK_CONTROL_MASK
)) {
919 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
921 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
934 printf("generating sequence:");
935 for (i
= start
; i
< end
; i
++)
936 printf(" %02x", (unsigned char) output
[i
]);
942 * For special control characters, the character set
943 * should never matter.
945 output
[end
] = '\0'; /* NUL-terminate */
946 ldisc_send(inst
->ldisc
, output
+start
, -2, 1);
947 } else if (!inst
->direct_to_font
) {
949 * The stuff we've just generated is assumed to be
950 * ISO-8859-1! This sounds insane, but `man
951 * XLookupString' agrees: strings of this type returned
952 * from the X server are hardcoded to 8859-1. Strictly
953 * speaking we should be doing this using some sort of
954 * GtkIMContext, which (if we're lucky) would give us
955 * our data directly in Unicode; but that's not
956 * supported in GTK 1.2 as far as I can tell, and it's
957 * poorly documented even in 2.0, so it'll have to
960 lpage_send(inst
->ldisc
, CS_ISO8859_1
, output
+start
, end
-start
, 1);
963 * In direct-to-font mode, we just send the string
964 * exactly as we received it.
966 ldisc_send(inst
->ldisc
, output
+start
, end
-start
, 1);
969 show_mouseptr(inst
, 0);
970 term_seen_key_event(inst
->term
);
971 term_out(inst
->term
);
977 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
979 struct gui_data
*inst
= (struct gui_data
*)data
;
980 int shift
, ctrl
, alt
, x
, y
, button
, act
;
982 show_mouseptr(inst
, 1);
984 if (event
->button
== 4 && event
->type
== GDK_BUTTON_PRESS
) {
985 term_scroll(inst
->term
, 0, -5);
988 if (event
->button
== 5 && event
->type
== GDK_BUTTON_PRESS
) {
989 term_scroll(inst
->term
, 0, +5);
993 shift
= event
->state
& GDK_SHIFT_MASK
;
994 ctrl
= event
->state
& GDK_CONTROL_MASK
;
995 alt
= event
->state
& GDK_MOD1_MASK
;
997 if (event
->button
== 3 && ctrl
) {
998 gtk_menu_popup(GTK_MENU(inst
->menu
), NULL
, NULL
, NULL
, NULL
,
999 event
->button
, event
->time
);
1003 if (event
->button
== 1)
1005 else if (event
->button
== 2)
1006 button
= MBT_MIDDLE
;
1007 else if (event
->button
== 3)
1010 return FALSE
; /* don't even know what button! */
1012 switch (event
->type
) {
1013 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
1014 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
1015 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
1016 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
1017 default: return FALSE
; /* don't know this event type */
1020 if (send_raw_mouse
&& !(inst
->cfg
.mouse_override
&& shift
) &&
1021 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
1022 return TRUE
; /* we ignore these in raw mouse mode */
1024 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1025 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1027 term_mouse(inst
->term
, button
, translate_button(button
), act
,
1028 x
, y
, shift
, ctrl
, alt
);
1033 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
1035 struct gui_data
*inst
= (struct gui_data
*)data
;
1036 int shift
, ctrl
, alt
, x
, y
, button
;
1038 show_mouseptr(inst
, 1);
1040 shift
= event
->state
& GDK_SHIFT_MASK
;
1041 ctrl
= event
->state
& GDK_CONTROL_MASK
;
1042 alt
= event
->state
& GDK_MOD1_MASK
;
1043 if (event
->state
& GDK_BUTTON1_MASK
)
1045 else if (event
->state
& GDK_BUTTON2_MASK
)
1046 button
= MBT_MIDDLE
;
1047 else if (event
->state
& GDK_BUTTON3_MASK
)
1050 return FALSE
; /* don't even know what button! */
1052 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1053 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1055 term_mouse(inst
->term
, button
, translate_button(button
), MA_DRAG
,
1056 x
, y
, shift
, ctrl
, alt
);
1061 void frontend_keypress(void *handle
)
1063 struct gui_data
*inst
= (struct gui_data
*)handle
;
1066 * If our child process has exited but not closed, terminate on
1073 gint
timer_func(gpointer data
)
1075 struct gui_data
*inst
= (struct gui_data
*)data
;
1078 if (!inst
->exited
&&
1079 (exitcode
= inst
->back
->exitcode(inst
->backhandle
)) >= 0) {
1080 inst
->exited
= TRUE
;
1081 if (inst
->cfg
.close_on_exit
== FORCE_ON
||
1082 (inst
->cfg
.close_on_exit
== AUTO
&& exitcode
== 0))
1083 exit(0); /* just go. */
1086 term_update(inst
->term
);
1087 term_blink(inst
->term
, 0);
1091 void fd_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
1094 * We must process exceptional notifications before ordinary
1095 * readability ones, or we may go straight past the urgent
1098 if (condition
& GDK_INPUT_EXCEPTION
)
1099 select_result(sourcefd
, 4);
1100 if (condition
& GDK_INPUT_READ
)
1101 select_result(sourcefd
, 1);
1102 if (condition
& GDK_INPUT_WRITE
)
1103 select_result(sourcefd
, 2);
1106 void destroy(GtkWidget
*widget
, gpointer data
)
1111 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
1113 struct gui_data
*inst
= (struct gui_data
*)data
;
1114 inst
->term
->has_focus
= event
->in
;
1115 term_out(inst
->term
);
1116 term_update(inst
->term
);
1117 show_mouseptr(inst
, 1);
1122 * set or clear the "raw mouse message" mode
1124 void set_raw_mouse_mode(void *frontend
, int activate
)
1126 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1127 activate
= activate
&& !inst
->cfg
.no_mouse_rep
;
1128 send_raw_mouse
= activate
;
1130 inst
->currcursor
= inst
->rawcursor
;
1132 inst
->currcursor
= inst
->textcursor
;
1133 show_mouseptr(inst
, inst
->mouseptr_visible
);
1136 void request_resize(void *frontend
, int w
, int h
)
1138 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1139 int large_x
, large_y
;
1140 int offset_x
, offset_y
;
1142 GtkRequisition inner
, outer
;
1145 * This is a heinous hack dreamed up by the gnome-terminal
1146 * people to get around a limitation in gtk. The problem is
1147 * that in order to set the size correctly we really need to be
1148 * calling gtk_window_resize - but that needs to know the size
1149 * of the _whole window_, not the drawing area. So what we do
1150 * is to set an artificially huge size request on the drawing
1151 * area, recompute the resulting size request on the window,
1152 * and look at the difference between the two. That gives us
1153 * the x and y offsets we need to translate drawing area size
1154 * into window size for real, and then we call
1155 * gtk_window_resize.
1159 * We start by retrieving the current size of the whole window.
1160 * Adding a bit to _that_ will give us a value we can use as a
1161 * bogus size request which guarantees to be bigger than the
1162 * current size of the drawing area.
1164 get_window_pixels(inst
, &large_x
, &large_y
);
1168 #if GTK_CHECK_VERSION(2,0,0)
1169 gtk_widget_set_size_request(inst
->area
, large_x
, large_y
);
1171 gtk_widget_set_usize(inst
->area
, large_x
, large_y
);
1173 gtk_widget_size_request(inst
->area
, &inner
);
1174 gtk_widget_size_request(inst
->window
, &outer
);
1176 offset_x
= outer
.width
- inner
.width
;
1177 offset_y
= outer
.height
- inner
.height
;
1179 area_x
= inst
->font_width
* w
+ 2*inst
->cfg
.window_border
;
1180 area_y
= inst
->font_height
* h
+ 2*inst
->cfg
.window_border
;
1183 * Now we must set the size request on the drawing area back to
1184 * something sensible before we commit the real resize. Best
1185 * way to do this, I think, is to set it to what the size is
1186 * really going to end up being.
1188 #if GTK_CHECK_VERSION(2,0,0)
1189 gtk_widget_set_size_request(inst
->area
, area_x
, area_y
);
1191 gtk_widget_set_usize(inst
->area
, area_x
, area_y
);
1192 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
), area_x
, area_y
);
1195 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst
->window
));
1197 #if GTK_CHECK_VERSION(2,0,0)
1198 gtk_window_resize(GTK_WINDOW(inst
->window
),
1199 area_x
+ offset_x
, area_y
+ offset_y
);
1201 gdk_window_resize(inst
->window
->window
,
1202 area_x
+ offset_x
, area_y
+ offset_y
);
1206 static void real_palette_set(struct gui_data
*inst
, int n
, int r
, int g
, int b
)
1208 gboolean success
[1];
1210 inst
->cols
[n
].red
= r
* 0x0101;
1211 inst
->cols
[n
].green
= g
* 0x0101;
1212 inst
->cols
[n
].blue
= b
* 0x0101;
1214 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
+ n
, 1);
1215 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
+ n
, 1,
1216 FALSE
, FALSE
, success
);
1218 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname
,
1222 void set_window_background(struct gui_data
*inst
)
1224 if (inst
->area
&& inst
->area
->window
)
1225 gdk_window_set_background(inst
->area
->window
, &inst
->cols
[18]);
1226 if (inst
->window
&& inst
->window
->window
)
1227 gdk_window_set_background(inst
->window
->window
, &inst
->cols
[18]);
1230 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1232 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1233 static const int first
[21] = {
1234 0, 2, 4, 6, 8, 10, 12, 14,
1235 1, 3, 5, 7, 9, 11, 13, 15,
1238 real_palette_set(inst
, first
[n
], r
, g
, b
);
1240 real_palette_set(inst
, first
[n
] + 1, r
, g
, b
);
1242 set_window_background(inst
);
1245 void palette_reset(void *frontend
)
1247 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1248 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1249 static const int ww
[] = {
1250 6, 7, 8, 9, 10, 11, 12, 13,
1251 14, 15, 16, 17, 18, 19, 20, 21,
1254 gboolean success
[NCOLOURS
];
1257 assert(lenof(ww
) == NCOLOURS
);
1259 if (!inst
->colmap
) {
1260 inst
->colmap
= gdk_colormap_get_system();
1262 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
, NCOLOURS
);
1265 for (i
= 0; i
< NCOLOURS
; i
++) {
1266 inst
->cols
[i
].red
= inst
->cfg
.colours
[ww
[i
]][0] * 0x0101;
1267 inst
->cols
[i
].green
= inst
->cfg
.colours
[ww
[i
]][1] * 0x0101;
1268 inst
->cols
[i
].blue
= inst
->cfg
.colours
[ww
[i
]][2] * 0x0101;
1271 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
1272 FALSE
, FALSE
, success
);
1273 for (i
= 0; i
< NCOLOURS
; i
++) {
1275 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1276 appname
, i
, inst
->cfg
.colours
[i
][0],
1277 inst
->cfg
.colours
[i
][1], inst
->cfg
.colours
[i
][2]);
1280 set_window_background(inst
);
1283 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
1285 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1286 if (inst
->pasteout_data
)
1287 sfree(inst
->pasteout_data
);
1288 if (inst
->pasteout_data_utf8
)
1289 sfree(inst
->pasteout_data_utf8
);
1292 * Set up UTF-8 paste data. This only happens if we aren't in
1293 * direct-to-font mode using the D800 hack.
1295 if (!inst
->direct_to_font
) {
1296 wchar_t *tmp
= data
;
1299 inst
->pasteout_data_utf8
= snewn(len
*6, char);
1300 inst
->pasteout_data_utf8_len
= len
*6;
1301 inst
->pasteout_data_utf8_len
=
1302 charset_from_unicode(&tmp
, &tmplen
, inst
->pasteout_data_utf8
,
1303 inst
->pasteout_data_utf8_len
,
1304 CS_UTF8
, NULL
, NULL
, 0);
1305 if (inst
->pasteout_data_utf8_len
== 0) {
1306 sfree(inst
->pasteout_data_utf8
);
1307 inst
->pasteout_data_utf8
= NULL
;
1309 inst
->pasteout_data_utf8
=
1310 sresize(inst
->pasteout_data_utf8
,
1311 inst
->pasteout_data_utf8_len
, char);
1314 inst
->pasteout_data_utf8
= NULL
;
1315 inst
->pasteout_data_utf8_len
= 0;
1318 inst
->pasteout_data
= snewn(len
*6, char);
1319 inst
->pasteout_data_len
= len
*6;
1320 inst
->pasteout_data_len
= wc_to_mb(inst
->ucsdata
.line_codepage
, 0,
1321 data
, len
, inst
->pasteout_data
,
1322 inst
->pasteout_data_len
,
1324 if (inst
->pasteout_data_len
== 0) {
1325 sfree(inst
->pasteout_data
);
1326 inst
->pasteout_data
= NULL
;
1328 inst
->pasteout_data
=
1329 sresize(inst
->pasteout_data
, inst
->pasteout_data_len
, char);
1332 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
1333 GDK_CURRENT_TIME
)) {
1334 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1335 GDK_SELECTION_TYPE_STRING
, 1);
1336 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1337 compound_text_atom
, 1);
1338 if (inst
->pasteout_data_utf8
)
1339 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1340 utf8_string_atom
, 1);
1344 term_deselect(inst
->term
);
1347 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1348 guint info
, guint time_stamp
, gpointer data
)
1350 struct gui_data
*inst
= (struct gui_data
*)data
;
1351 if (seldata
->target
== utf8_string_atom
)
1352 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1353 inst
->pasteout_data_utf8
,
1354 inst
->pasteout_data_utf8_len
);
1356 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1357 inst
->pasteout_data
, inst
->pasteout_data_len
);
1360 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1363 struct gui_data
*inst
= (struct gui_data
*)data
;
1364 term_deselect(inst
->term
);
1365 if (inst
->pasteout_data
)
1366 sfree(inst
->pasteout_data
);
1367 if (inst
->pasteout_data_utf8
)
1368 sfree(inst
->pasteout_data_utf8
);
1369 inst
->pasteout_data
= NULL
;
1370 inst
->pasteout_data_len
= 0;
1371 inst
->pasteout_data_utf8
= NULL
;
1372 inst
->pasteout_data_utf8_len
= 0;
1376 void request_paste(void *frontend
)
1378 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1380 * In Unix, pasting is asynchronous: all we can do at the
1381 * moment is to call gtk_selection_convert(), and when the data
1382 * comes back _then_ we can call term_do_paste().
1385 if (!inst
->direct_to_font
) {
1387 * First we attempt to retrieve the selection as a UTF-8
1388 * string (which we will convert to the correct code page
1389 * before sending to the session, of course). If that
1390 * fails, selection_received() will be informed and will
1391 * fall back to an ordinary string.
1393 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1394 utf8_string_atom
, GDK_CURRENT_TIME
);
1397 * If we're in direct-to-font mode, we disable UTF-8
1398 * pasting, and go straight to ordinary string data.
1400 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1401 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1405 gint
idle_paste_func(gpointer data
); /* forward ref */
1407 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1408 guint time
, gpointer data
)
1410 struct gui_data
*inst
= (struct gui_data
*)data
;
1412 if (seldata
->target
== utf8_string_atom
&& seldata
->length
<= 0) {
1414 * Failed to get a UTF-8 selection string. Try an ordinary
1417 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1418 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1423 * Any other failure should just go foom.
1425 if (seldata
->length
<= 0 ||
1426 (seldata
->type
!= GDK_SELECTION_TYPE_STRING
&&
1427 seldata
->type
!= utf8_string_atom
))
1428 return; /* Nothing happens. */
1430 if (inst
->pastein_data
)
1431 sfree(inst
->pastein_data
);
1433 inst
->pastein_data
= snewn(seldata
->length
, wchar_t);
1434 inst
->pastein_data_len
= seldata
->length
;
1435 inst
->pastein_data_len
=
1436 mb_to_wc((seldata
->type
== utf8_string_atom ?
1437 CS_UTF8
: inst
->ucsdata
.line_codepage
),
1438 0, seldata
->data
, seldata
->length
,
1439 inst
->pastein_data
, inst
->pastein_data_len
);
1441 term_do_paste(inst
->term
);
1443 if (term_paste_pending(inst
->term
))
1444 inst
->term_paste_idle_id
= gtk_idle_add(idle_paste_func
, inst
);
1447 gint
idle_paste_func(gpointer data
)
1449 struct gui_data
*inst
= (struct gui_data
*)data
;
1451 if (term_paste_pending(inst
->term
))
1452 term_paste(inst
->term
);
1454 gtk_idle_remove(inst
->term_paste_idle_id
);
1460 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
1462 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1465 *p
= inst
->pastein_data
;
1466 *len
= inst
->pastein_data_len
;
1470 static void set_window_titles(struct gui_data
*inst
)
1473 * We must always call set_icon_name after calling set_title,
1474 * since set_title will write both names. Irritating, but such
1477 gtk_window_set_title(GTK_WINDOW(inst
->window
), inst
->wintitle
);
1478 if (!inst
->cfg
.win_name_always
)
1479 gdk_window_set_icon_name(inst
->window
->window
, inst
->icontitle
);
1482 void set_title(void *frontend
, char *title
)
1484 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1485 strncpy(inst
->wintitle
, title
, lenof(inst
->wintitle
));
1486 inst
->wintitle
[lenof(inst
->wintitle
)-1] = '\0';
1487 set_window_titles(inst
);
1490 void set_icon(void *frontend
, char *title
)
1492 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1493 strncpy(inst
->icontitle
, title
, lenof(inst
->icontitle
));
1494 inst
->icontitle
[lenof(inst
->icontitle
)-1] = '\0';
1495 set_window_titles(inst
);
1498 void set_sbar(void *frontend
, int total
, int start
, int page
)
1500 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1501 if (!inst
->cfg
.scrollbar
)
1503 inst
->sbar_adjust
->lower
= 0;
1504 inst
->sbar_adjust
->upper
= total
;
1505 inst
->sbar_adjust
->value
= start
;
1506 inst
->sbar_adjust
->page_size
= page
;
1507 inst
->sbar_adjust
->step_increment
= 1;
1508 inst
->sbar_adjust
->page_increment
= page
/2;
1509 inst
->ignore_sbar
= TRUE
;
1510 gtk_adjustment_changed(inst
->sbar_adjust
);
1511 inst
->ignore_sbar
= FALSE
;
1514 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
1516 struct gui_data
*inst
= (struct gui_data
*)data
;
1518 if (!inst
->cfg
.scrollbar
)
1520 if (!inst
->ignore_sbar
)
1521 term_scroll(inst
->term
, 1, (int)adj
->value
);
1524 void sys_cursor(void *frontend
, int x
, int y
)
1527 * This is meaningless under X.
1532 * This is still called when mode==BELL_VISUAL, even though the
1533 * visual bell is handled entirely within terminal.c, because we
1534 * may want to perform additional actions on any kind of bell (for
1535 * example, taskbar flashing in Windows).
1537 void beep(void *frontend
, int mode
)
1539 if (mode
!= BELL_VISUAL
)
1543 int char_width(Context ctx
, int uc
)
1546 * Under X, any fixed-width font really _is_ fixed-width.
1547 * Double-width characters will be dealt with using a separate
1548 * font. For the moment we can simply return 1.
1553 Context
get_ctx(void *frontend
)
1555 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1556 struct draw_ctx
*dctx
;
1558 if (!inst
->area
->window
)
1561 dctx
= snew(struct draw_ctx
);
1563 dctx
->gc
= gdk_gc_new(inst
->area
->window
);
1567 void free_ctx(Context ctx
)
1569 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1570 /* struct gui_data *inst = dctx->inst; */
1571 GdkGC
*gc
= dctx
->gc
;
1577 * Draw a line of text in the window, at given character
1578 * coordinates, in given attributes.
1580 * We are allowed to fiddle with the contents of `text'.
1582 void do_text_internal(Context ctx
, int x
, int y
, char *text
, int len
,
1583 unsigned long attr
, int lattr
)
1585 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1586 struct gui_data
*inst
= dctx
->inst
;
1587 GdkGC
*gc
= dctx
->gc
;
1589 int nfg
, nbg
, t
, fontid
, shadow
, rlen
, widefactor
;
1591 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1592 nfg
= 2 * (nfg
& 0xF) + (nfg
& 0x10 ?
1 : 0);
1593 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1594 nbg
= 2 * (nbg
& 0xF) + (nbg
& 0x10 ?
1 : 0);
1595 if (attr
& ATTR_REVERSE
) {
1600 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
1602 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
1604 if (attr
& TATTR_ACTCURS
) {
1609 fontid
= shadow
= 0;
1611 if (attr
& ATTR_WIDE
) {
1618 if ((attr
& ATTR_BOLD
) && !inst
->cfg
.bold_colour
) {
1619 if (inst
->fonts
[fontid
| 1])
1625 if (lattr
!= LATTR_NORM
) {
1627 if (x
>= inst
->term
->cols
)
1629 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1630 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1638 r
.x
= x
*inst
->font_width
+inst
->cfg
.window_border
;
1639 r
.y
= y
*inst
->font_height
+inst
->cfg
.window_border
;
1640 r
.width
= rlen
*widefactor
*inst
->font_width
;
1641 r
.height
= inst
->font_height
;
1642 gdk_gc_set_clip_rectangle(gc
, &r
);
1645 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
1646 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
1647 x
*inst
->font_width
+inst
->cfg
.window_border
,
1648 y
*inst
->font_height
+inst
->cfg
.window_border
,
1649 rlen
*widefactor
*inst
->font_width
, inst
->font_height
);
1651 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
1658 wcs
= snewn(len
+1, wchar_t);
1659 for (i
= 0; i
< len
; i
++) {
1660 wcs
[i
] = (wchar_t) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
1663 if (inst
->fonts
[fontid
] == NULL
) {
1665 * The font for this contingency does not exist.
1666 * Typically this means we've been given ATTR_WIDE
1667 * character and have no wide font. So we display
1668 * nothing at all; such is life.
1670 } else if (inst
->fontinfo
[fontid
].is_wide
) {
1672 * At least one version of gdk_draw_text_wc() has a
1673 * weird bug whereby it reads `len' elements of the
1674 * input string, but only draws `len/2'. Hence I'm
1675 * going to make its input array twice as long as it
1676 * theoretically needs to be, and pass in twice the
1677 * actual number of characters. If a fixed gdk actually
1678 * takes the doubled length seriously, then (a) the
1679 * array will stand scrutiny up to the full length, (b)
1680 * the spare elements of the array are full of zeroes
1681 * which will probably be an empty glyph in the font,
1682 * and (c) the clip rectangle should prevent it causing
1685 gwcs
= snewn(len
*2+1, GdkWChar
);
1686 memset(gwcs
, 0, sizeof(GdkWChar
) * (len
*2+1));
1688 * FIXME: when we have a wide-char equivalent of
1689 * from_unicode, use it instead of this.
1691 for (i
= 0; i
<= len
; i
++)
1693 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1694 x
*inst
->font_width
+inst
->cfg
.window_border
,
1695 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1699 gcs
= snewn(len
+1, gchar
);
1700 wc_to_mb(inst
->fontinfo
[fontid
].charset
, 0,
1701 wcs
, len
, gcs
, len
, ".", NULL
, NULL
);
1702 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1703 x
*inst
->font_width
+inst
->cfg
.window_border
,
1704 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1712 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1713 x
*inst
->font_width
+inst
->cfg
.window_border
+ inst
->cfg
.shadowboldoffset
,
1714 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1718 if (attr
& ATTR_UNDER
) {
1719 int uheight
= inst
->fonts
[0]->ascent
+ 1;
1720 if (uheight
>= inst
->font_height
)
1721 uheight
= inst
->font_height
- 1;
1722 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+inst
->cfg
.window_border
,
1723 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
,
1724 (x
+len
)*widefactor
*inst
->font_width
-1+inst
->cfg
.window_border
,
1725 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
);
1728 if (lattr
!= LATTR_NORM
) {
1730 * I can't find any plausible StretchBlt equivalent in the
1731 * X server, so I'm going to do this the slow and painful
1732 * way. This will involve repeated calls to
1733 * gdk_draw_pixmap() to stretch the text horizontally. It's
1734 * O(N^2) in time and O(N) in network bandwidth, but you
1735 * try thinking of a better way. :-(
1738 for (i
= 0; i
< len
* widefactor
* inst
->font_width
; i
++) {
1739 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1740 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
,
1741 y
*inst
->font_height
+inst
->cfg
.window_border
,
1742 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
+1,
1743 y
*inst
->font_height
+inst
->cfg
.window_border
,
1744 len
* inst
->font_width
- i
, inst
->font_height
);
1747 if (lattr
!= LATTR_WIDE
) {
1749 /* Now stretch vertically, in the same way. */
1750 if (lattr
== LATTR_BOT
)
1754 for (i
= 0; i
< inst
->font_height
; i
+=2) {
1755 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1756 x
*inst
->font_width
+inst
->cfg
.window_border
,
1757 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*i
+db
,
1758 x
*widefactor
*inst
->font_width
+inst
->cfg
.window_border
,
1759 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*(i
+1),
1760 len
* inst
->font_width
, inst
->font_height
-i
-1);
1766 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
1767 unsigned long attr
, int lattr
)
1769 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1770 struct gui_data
*inst
= dctx
->inst
;
1771 GdkGC
*gc
= dctx
->gc
;
1774 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1776 if (attr
& ATTR_WIDE
) {
1782 if (lattr
!= LATTR_NORM
) {
1784 if (x
>= inst
->term
->cols
)
1786 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1787 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1791 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1792 x
*inst
->font_width
+inst
->cfg
.window_border
,
1793 y
*inst
->font_height
+inst
->cfg
.window_border
,
1794 x
*inst
->font_width
+inst
->cfg
.window_border
,
1795 y
*inst
->font_height
+inst
->cfg
.window_border
,
1796 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1799 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1800 unsigned long attr
, int lattr
)
1802 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1803 struct gui_data
*inst
= dctx
->inst
;
1804 GdkGC
*gc
= dctx
->gc
;
1806 int passive
, widefactor
;
1808 if (attr
& TATTR_PASCURS
) {
1809 attr
&= ~TATTR_PASCURS
;
1813 if ((attr
& TATTR_ACTCURS
) && inst
->cfg
.cursor_type
!= 0) {
1814 attr
&= ~TATTR_ACTCURS
;
1816 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1818 if (attr
& ATTR_WIDE
) {
1824 if (lattr
!= LATTR_NORM
) {
1826 if (x
>= inst
->term
->cols
)
1828 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1829 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1833 if (inst
->cfg
.cursor_type
== 0) {
1835 * An active block cursor will already have been done by
1836 * the above do_text call, so we only need to do anything
1840 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1841 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
1842 x
*inst
->font_width
+inst
->cfg
.window_border
,
1843 y
*inst
->font_height
+inst
->cfg
.window_border
,
1844 len
*inst
->font_width
-1, inst
->font_height
-1);
1848 int startx
, starty
, dx
, dy
, length
, i
;
1852 if ((attr
& ATTR_WIDE
) || lattr
!= LATTR_NORM
)
1853 char_width
= 2*inst
->font_width
;
1855 char_width
= inst
->font_width
;
1857 if (inst
->cfg
.cursor_type
== 1) {
1858 uheight
= inst
->fonts
[0]->ascent
+ 1;
1859 if (uheight
>= inst
->font_height
)
1860 uheight
= inst
->font_height
- 1;
1862 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
;
1863 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
+ uheight
;
1866 length
= len
* char_width
;
1869 if (attr
& TATTR_RIGHTCURS
)
1870 xadjust
= char_width
- 1;
1871 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
+ xadjust
;
1872 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
;
1875 length
= inst
->font_height
;
1878 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1880 for (i
= 0; i
< length
; i
++) {
1882 gdk_draw_point(inst
->pixmap
, gc
, startx
, starty
);
1888 gdk_draw_line(inst
->pixmap
, gc
, startx
, starty
,
1889 startx
+ (length
-1) * dx
, starty
+ (length
-1) * dy
);
1893 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1894 x
*inst
->font_width
+inst
->cfg
.window_border
,
1895 y
*inst
->font_height
+inst
->cfg
.window_border
,
1896 x
*inst
->font_width
+inst
->cfg
.window_border
,
1897 y
*inst
->font_height
+inst
->cfg
.window_border
,
1898 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1901 GdkCursor
*make_mouse_ptr(struct gui_data
*inst
, int cursor_val
)
1904 * Truly hideous hack: GTK doesn't allow us to set the mouse
1905 * cursor foreground and background colours unless we've _also_
1906 * created our own cursor from bitmaps. Therefore, I need to
1907 * load the `cursor' font and draw glyphs from it on to
1908 * pixmaps, in order to construct my cursors with the fg and bg
1909 * I want. This is a gross hack, but it's more self-contained
1910 * than linking in Xlib to find the X window handle to
1911 * inst->area and calling XRecolorCursor, and it's more
1912 * futureproof than hard-coding the shapes as bitmap arrays.
1914 static GdkFont
*cursor_font
= NULL
;
1915 GdkPixmap
*source
, *mask
;
1917 GdkColor cfg
= { 0, 65535, 65535, 65535 };
1918 GdkColor cbg
= { 0, 0, 0, 0 };
1919 GdkColor dfg
= { 1, 65535, 65535, 65535 };
1920 GdkColor dbg
= { 0, 0, 0, 0 };
1923 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
1925 if (cursor_val
== -2) {
1926 gdk_font_unref(cursor_font
);
1930 if (cursor_val
>= 0 && !cursor_font
)
1931 cursor_font
= gdk_font_load("cursor");
1934 * Get the text extent of the cursor in question. We use the
1935 * mask character for this, because it's typically slightly
1936 * bigger than the main character.
1938 if (cursor_val
>= 0) {
1940 text
[0] = (char)cursor_val
+ 1;
1941 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
1942 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
1948 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
1949 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
1952 * Draw the mask character on the mask pixmap.
1954 gc
= gdk_gc_new(mask
);
1955 gdk_gc_set_foreground(gc
, &dbg
);
1956 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
1957 if (cursor_val
>= 0) {
1959 text
[0] = (char)cursor_val
+ 1;
1960 gdk_gc_set_foreground(gc
, &dfg
);
1961 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
1966 * Draw the main character on the source pixmap.
1968 gc
= gdk_gc_new(source
);
1969 gdk_gc_set_foreground(gc
, &dbg
);
1970 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
1971 if (cursor_val
>= 0) {
1973 text
[0] = (char)cursor_val
;
1974 gdk_gc_set_foreground(gc
, &dfg
);
1975 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
1980 * Create the cursor.
1982 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
1987 gdk_pixmap_unref(source
);
1988 gdk_pixmap_unref(mask
);
1993 void modalfatalbox(char *p
, ...)
1996 fprintf(stderr
, "FATAL ERROR: ");
1998 vfprintf(stderr
, p
, ap
);
2000 fputc('\n', stderr
);
2004 void cmdline_error(char *p
, ...)
2007 fprintf(stderr
, "%s: ", appname
);
2009 vfprintf(stderr
, p
, ap
);
2011 fputc('\n', stderr
);
2015 char *get_x_display(void *frontend
)
2017 return gdk_get_display();
2020 long get_windowid(void *frontend
)
2022 struct gui_data
*inst
= (struct gui_data
*)frontend
;
2023 return (long)GDK_WINDOW_XWINDOW(inst
->area
->window
);
2026 static void help(FILE *fp
) {
2028 "pterm option summary:\n"
2030 " --display DISPLAY Specify X display to use (note '--')\n"
2031 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2032 " -fn FONT Normal text font\n"
2033 " -fb FONT Bold text font\n"
2034 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2035 " -sl LINES Number of lines of scrollback\n"
2036 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2037 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2038 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2039 " -T TITLE Window title\n"
2040 " -ut, +ut Do(default) or do not update utmp\n"
2041 " -ls, +ls Do(default) or do not make shell a login shell\n"
2042 " -sb, +sb Do(default) or do not display a scrollbar\n"
2043 " -log PATH Log all output to a file\n"
2044 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2045 " -xrm RESOURCE-STRING Set an X resource\n"
2046 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2047 ) < 0 || fflush(fp
) < 0) {
2048 perror("output error");
2053 int do_cmdline(int argc
, char **argv
, int do_everything
,
2054 struct gui_data
*inst
, Config
*cfg
)
2060 * Macros to make argument handling easier. Note that because
2061 * they need to call `continue', they cannot be contained in
2062 * the usual do {...} while (0) wrapper to make them
2063 * syntactically single statements; hence it is not legal to
2064 * use one of these macros as an unbraced statement between
2067 #define EXPECTS_ARG { \
2068 if (--argc <= 0) { \
2070 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2075 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2077 while (--argc
> 0) {
2081 ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
2082 do_everything ?
1 : -1, cfg
);
2085 cmdline_error("option \"%s\" requires an argument", p
);
2086 } else if (ret
== 2) {
2087 --argc
, ++argv
; /* skip next argument */
2089 } else if (ret
== 1) {
2093 if (!strcmp(p
, "-fn") || !strcmp(p
, "-font")) {
2096 strncpy(cfg
->font
.name
, val
, sizeof(cfg
->font
.name
));
2097 cfg
->font
.name
[sizeof(cfg
->font
.name
)-1] = '\0';
2099 } else if (!strcmp(p
, "-fb")) {
2102 strncpy(cfg
->boldfont
.name
, val
, sizeof(cfg
->boldfont
.name
));
2103 cfg
->boldfont
.name
[sizeof(cfg
->boldfont
.name
)-1] = '\0';
2105 } else if (!strcmp(p
, "-fw")) {
2108 strncpy(cfg
->widefont
.name
, val
, sizeof(cfg
->widefont
.name
));
2109 cfg
->widefont
.name
[sizeof(cfg
->widefont
.name
)-1] = '\0';
2111 } else if (!strcmp(p
, "-fwb")) {
2114 strncpy(cfg
->wideboldfont
.name
, val
, sizeof(cfg
->wideboldfont
.name
));
2115 cfg
->wideboldfont
.name
[sizeof(cfg
->wideboldfont
.name
)-1] = '\0';
2117 } else if (!strcmp(p
, "-cs")) {
2120 strncpy(cfg
->line_codepage
, val
, sizeof(cfg
->line_codepage
));
2121 cfg
->line_codepage
[sizeof(cfg
->line_codepage
)-1] = '\0';
2123 } else if (!strcmp(p
, "-geometry")) {
2124 int flags
, x
, y
, w
, h
;
2128 flags
= XParseGeometry(val
, &x
, &y
, &w
, &h
);
2129 if (flags
& WidthValue
)
2131 if (flags
& HeightValue
)
2134 if (flags
& (XValue
| YValue
)) {
2137 inst
->gotpos
= TRUE
;
2138 inst
->gravity
= ((flags
& XNegative ?
1 : 0) |
2139 (flags
& YNegative ?
2 : 0));
2142 } else if (!strcmp(p
, "-sl")) {
2145 cfg
->savelines
= atoi(val
);
2147 } else if (!strcmp(p
, "-fg") || !strcmp(p
, "-bg") ||
2148 !strcmp(p
, "-bfg") || !strcmp(p
, "-bbg") ||
2149 !strcmp(p
, "-cfg") || !strcmp(p
, "-cbg")) {
2154 if (!gdk_color_parse(val
, &col
)) {
2156 fprintf(stderr
, "%s: unable to parse colour \"%s\"\n",
2160 index
= (!strcmp(p
, "-fg") ?
0 :
2161 !strcmp(p
, "-bg") ?
2 :
2162 !strcmp(p
, "-bfg") ?
1 :
2163 !strcmp(p
, "-bbg") ?
3 :
2164 !strcmp(p
, "-cfg") ?
4 :
2165 !strcmp(p
, "-cbg") ?
5 : -1);
2166 assert(index
!= -1);
2167 cfg
->colours
[index
][0] = col
.red
/ 256;
2168 cfg
->colours
[index
][1] = col
.green
/ 256;
2169 cfg
->colours
[index
][2] = col
.blue
/ 256;
2172 } else if (use_pty_argv
&& !strcmp(p
, "-e")) {
2173 /* This option swallows all further arguments. */
2179 pty_argv
= snewn(argc
+1, char *);
2181 for (i
= 0; i
< argc
; i
++)
2182 pty_argv
[i
] = argv
[i
];
2183 pty_argv
[argc
] = NULL
;
2184 break; /* finished command-line processing */
2186 err
= 1, fprintf(stderr
, "%s: -e expects an argument\n",
2189 } else if (!strcmp(p
, "-title")) {
2192 strncpy(cfg
->wintitle
, val
, sizeof(cfg
->wintitle
));
2193 cfg
->wintitle
[sizeof(cfg
->wintitle
)-1] = '\0';
2195 } else if (!strcmp(p
, "-log")) {
2198 strncpy(cfg
->logfilename
.path
, val
, sizeof(cfg
->logfilename
.path
));
2199 cfg
->logfilename
.path
[sizeof(cfg
->logfilename
.path
)-1] = '\0';
2200 cfg
->logtype
= LGTYP_DEBUG
;
2202 } else if (!strcmp(p
, "-ut-") || !strcmp(p
, "+ut")) {
2204 cfg
->stamp_utmp
= 0;
2206 } else if (!strcmp(p
, "-ut")) {
2208 cfg
->stamp_utmp
= 1;
2210 } else if (!strcmp(p
, "-ls-") || !strcmp(p
, "+ls")) {
2212 cfg
->login_shell
= 0;
2214 } else if (!strcmp(p
, "-ls")) {
2216 cfg
->login_shell
= 1;
2218 } else if (!strcmp(p
, "-nethack")) {
2220 cfg
->nethack_keypad
= 1;
2222 } else if (!strcmp(p
, "-sb-") || !strcmp(p
, "+sb")) {
2226 } else if (!strcmp(p
, "-sb")) {
2230 } else if (!strcmp(p
, "-name")) {
2234 } else if (!strcmp(p
, "-xrm")) {
2236 provide_xrm_string(val
);
2238 } else if(!strcmp(p
, "-help") || !strcmp(p
, "--help")) {
2242 } else if(p
[0] != '-' && (!do_everything
||
2243 process_nonoption_arg(p
, cfg
))) {
2248 fprintf(stderr
, "%s: unrecognized option '%s'\n", appname
, p
);
2255 static void block_signal(int sig
, int block_it
) {
2259 sigaddset(&ss
, sig
);
2260 if(sigprocmask(block_it ? SIG_BLOCK
: SIG_UNBLOCK
, &ss
, 0) < 0) {
2261 perror("sigprocmask");
2267 * This function retrieves the character set encoding of a font. It
2268 * returns the character set without the X11 hack (in case the user
2269 * asks to use the font's own encoding).
2271 static int set_font_info(struct gui_data
*inst
, int fontid
)
2273 GdkFont
*font
= inst
->fonts
[fontid
];
2274 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2275 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2276 Atom charset_registry
, charset_encoding
;
2277 unsigned long registry_ret
, encoding_ret
;
2278 int retval
= CS_NONE
;
2280 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
2281 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
2282 inst
->fontinfo
[fontid
].charset
= CS_NONE
;
2283 inst
->fontinfo
[fontid
].is_wide
= 0;
2284 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
2285 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
2287 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
2288 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
2290 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
2291 retval
= inst
->fontinfo
[fontid
].charset
=
2292 charset_from_xenc(encoding
);
2293 /* FIXME: when libcharset supports wide encodings fix this. */
2294 if (!strcasecmp(encoding
, "iso10646-1")) {
2295 inst
->fontinfo
[fontid
].is_wide
= 1;
2300 * Hack for X line-drawing characters: if the primary
2301 * font is encoded as ISO-8859-anything, and has valid
2302 * glyphs in the first 32 char positions, it is assumed
2303 * that those glyphs are the VT100 line-drawing
2306 * Actually, we'll hack even harder by only checking
2307 * position 0x19 (vertical line, VT100 linedrawing
2308 * `x'). Then we can check it easily by seeing if the
2309 * ascent and descent differ.
2311 if (inst
->fontinfo
[fontid
].charset
== CS_ISO8859_1
) {
2312 int lb
, rb
, wid
, asc
, desc
;
2317 gdk_string_extents(inst
->fonts
[fontid
], text
,
2318 &lb
, &rb
, &wid
, &asc
, &desc
);
2320 inst
->fontinfo
[fontid
].charset
= CS_ISO8859_1_X11
;
2330 int uxsel_input_add(int fd
, int rwx
) {
2332 if (rwx
& 1) flags
|= GDK_INPUT_READ
;
2333 if (rwx
& 2) flags
|= GDK_INPUT_WRITE
;
2334 if (rwx
& 4) flags
|= GDK_INPUT_EXCEPTION
;
2335 return gdk_input_add(fd
, flags
, fd_input_func
, NULL
);
2338 void uxsel_input_remove(int id
) {
2339 gdk_input_remove(id
);
2342 void setup_fonts_ucs(struct gui_data
*inst
)
2347 gdk_font_unref(inst
->fonts
[0]);
2349 gdk_font_unref(inst
->fonts
[1]);
2351 gdk_font_unref(inst
->fonts
[2]);
2353 gdk_font_unref(inst
->fonts
[3]);
2355 inst
->fonts
[0] = gdk_font_load(inst
->cfg
.font
.name
);
2356 if (!inst
->fonts
[0]) {
2357 fprintf(stderr
, "%s: unable to load font \"%s\"\n", appname
,
2358 inst
->cfg
.font
.name
);
2361 font_charset
= set_font_info(inst
, 0);
2362 if (inst
->cfg
.boldfont
.name
[0]) {
2363 inst
->fonts
[1] = gdk_font_load(inst
->cfg
.boldfont
.name
);
2364 if (!inst
->fonts
[1]) {
2365 fprintf(stderr
, "%s: unable to load bold font \"%s\"\n", appname
,
2366 inst
->cfg
.boldfont
.name
);
2369 set_font_info(inst
, 1);
2371 inst
->fonts
[1] = NULL
;
2372 if (inst
->cfg
.widefont
.name
[0]) {
2373 inst
->fonts
[2] = gdk_font_load(inst
->cfg
.widefont
.name
);
2374 if (!inst
->fonts
[2]) {
2375 fprintf(stderr
, "%s: unable to load wide font \"%s\"\n", appname
,
2376 inst
->cfg
.widefont
.name
);
2379 set_font_info(inst
, 2);
2381 inst
->fonts
[2] = NULL
;
2382 if (inst
->cfg
.wideboldfont
.name
[0]) {
2383 inst
->fonts
[3] = gdk_font_load(inst
->cfg
.wideboldfont
.name
);
2384 if (!inst
->fonts
[3]) {
2385 fprintf(stderr
, "%s: unable to load wide/bold font \"%s\"\n",
2386 appname
, inst
->cfg
.wideboldfont
.name
);
2389 set_font_info(inst
, 3);
2391 inst
->fonts
[3] = NULL
;
2393 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
2394 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
2396 inst
->direct_to_font
= init_ucs(&inst
->ucsdata
,
2397 inst
->cfg
.line_codepage
, font_charset
,
2401 void set_geom_hints(struct gui_data
*inst
)
2404 geom
.min_width
= inst
->font_width
+ 2*inst
->cfg
.window_border
;
2405 geom
.min_height
= inst
->font_height
+ 2*inst
->cfg
.window_border
;
2406 geom
.max_width
= geom
.max_height
= -1;
2407 geom
.base_width
= 2*inst
->cfg
.window_border
;
2408 geom
.base_height
= 2*inst
->cfg
.window_border
;
2409 geom
.width_inc
= inst
->font_width
;
2410 geom
.height_inc
= inst
->font_height
;
2411 geom
.min_aspect
= geom
.max_aspect
= 0;
2412 gtk_window_set_geometry_hints(GTK_WINDOW(inst
->window
), inst
->area
, &geom
,
2413 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
2414 GDK_HINT_RESIZE_INC
);
2417 void clear_scrollback_menuitem(GtkMenuItem
*item
, gpointer data
)
2419 struct gui_data
*inst
= (struct gui_data
*)data
;
2420 term_clrsb(inst
->term
);
2423 void reset_terminal_menuitem(GtkMenuItem
*item
, gpointer data
)
2425 struct gui_data
*inst
= (struct gui_data
*)data
;
2426 term_pwron(inst
->term
);
2427 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2430 void copy_all_menuitem(GtkMenuItem
*item
, gpointer data
)
2432 struct gui_data
*inst
= (struct gui_data
*)data
;
2433 term_copyall(inst
->term
);
2436 void special_menuitem(GtkMenuItem
*item
, gpointer data
)
2438 struct gui_data
*inst
= (struct gui_data
*)data
;
2439 int code
= (int)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
2441 inst
->back
->special(inst
->backhandle
, code
);
2444 void about_menuitem(GtkMenuItem
*item
, gpointer data
)
2446 struct gui_data
*inst
= (struct gui_data
*)data
;
2447 about_box(inst
->window
);
2450 void event_log_menuitem(GtkMenuItem
*item
, gpointer data
)
2452 struct gui_data
*inst
= (struct gui_data
*)data
;
2453 showeventlog(inst
->eventlogstuff
, inst
->window
);
2456 void change_settings_menuitem(GtkMenuItem
*item
, gpointer data
)
2458 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2459 static const int ww
[] = {
2460 6, 7, 8, 9, 10, 11, 12, 13,
2461 14, 15, 16, 17, 18, 19, 20, 21,
2464 struct gui_data
*inst
= (struct gui_data
*)data
;
2465 char *title
= dupcat(appname
, " Reconfiguration", NULL
);
2466 Config cfg2
, oldcfg
;
2469 cfg2
= inst
->cfg
; /* structure copy */
2471 if (do_config_box(title
, &cfg2
, 1)) {
2473 oldcfg
= inst
->cfg
; /* structure copy */
2474 inst
->cfg
= cfg2
; /* structure copy */
2476 /* Pass new config data to the logging module */
2477 log_reconfig(inst
->logctx
, &cfg2
);
2479 * Flush the line discipline's edit buffer in the case
2480 * where local editing has just been disabled.
2482 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2483 /* Pass new config data to the terminal */
2484 term_reconfig(inst
->term
, &cfg2
);
2485 /* Pass new config data to the back end */
2486 inst
->back
->reconfig(inst
->backhandle
, &cfg2
);
2489 * Just setting inst->cfg is sufficient to cause colour
2490 * setting changes to appear on the next ESC]R palette
2491 * reset. But we should also check whether any colour
2492 * settings have been changed, and revert the ones that
2493 * have to the new default, on the assumption that the user
2494 * is most likely to want an immediate update.
2496 for (i
= 0; i
< NCOLOURS
; i
++) {
2497 if (oldcfg
.colours
[ww
[i
]][0] != cfg2
.colours
[ww
[i
]][0] ||
2498 oldcfg
.colours
[ww
[i
]][1] != cfg2
.colours
[ww
[i
]][1] ||
2499 oldcfg
.colours
[ww
[i
]][2] != cfg2
.colours
[ww
[i
]][2])
2500 real_palette_set(inst
, i
, cfg2
.colours
[ww
[i
]][0],
2501 cfg2
.colours
[ww
[i
]][1],
2502 cfg2
.colours
[ww
[i
]][2]);
2506 * If the scrollbar needs to be shown, hidden, or moved
2507 * from one end to the other of the window, do so now.
2509 if (oldcfg
.scrollbar
!= cfg2
.scrollbar
) {
2511 gtk_widget_show(inst
->sbar
);
2513 gtk_widget_hide(inst
->sbar
);
2515 if (oldcfg
.scrollbar_on_left
!= cfg2
.scrollbar_on_left
) {
2516 gtk_box_reorder_child(inst
->hbox
, inst
->sbar
,
2517 cfg2
.scrollbar_on_left ?
0 : 1);
2521 * Change the window title, if required.
2523 if (strcmp(oldcfg
.wintitle
, cfg2
.wintitle
))
2524 set_title(inst
, cfg2
.wintitle
);
2525 set_window_titles(inst
);
2528 * Redo the whole tangled fonts and Unicode mess if
2531 if (strcmp(oldcfg
.font
.name
, cfg2
.font
.name
) ||
2532 strcmp(oldcfg
.boldfont
.name
, cfg2
.boldfont
.name
) ||
2533 strcmp(oldcfg
.widefont
.name
, cfg2
.widefont
.name
) ||
2534 strcmp(oldcfg
.wideboldfont
.name
, cfg2
.wideboldfont
.name
) ||
2535 strcmp(oldcfg
.line_codepage
, cfg2
.line_codepage
) ||
2536 oldcfg
.vtmode
!= cfg2
.vtmode
) {
2537 setup_fonts_ucs(inst
);
2543 * Resize the window.
2545 if (oldcfg
.width
!= cfg2
.width
|| oldcfg
.height
!= cfg2
.height
||
2546 oldcfg
.window_border
!= cfg2
.window_border
|| need_size
) {
2547 set_geom_hints(inst
);
2548 request_resize(inst
, cfg2
.width
, cfg2
.height
);
2551 * The above will have caused a call to term_size() for
2552 * us if it happened. If the user has fiddled with only
2553 * the scrollback size, the above will not have
2554 * happened and we will need an explicit term_size()
2557 if (oldcfg
.savelines
!= cfg2
.savelines
)
2558 term_size(inst
->term
, inst
->term
->rows
, inst
->term
->cols
,
2562 term_invalidate(inst
->term
);
2567 void fork_and_exec_self(struct gui_data
*inst
, int fd_to_close
, ...)
2570 * Re-execing ourself is not an exact science under Unix. I do
2571 * the best I can by using /proc/self/exe if available and by
2572 * assuming argv[0] can be found on $PATH if not.
2574 * Note that we also have to reconstruct the elements of the
2575 * original argv which gtk swallowed, since the user wants the
2576 * new session to appear on the same X display as the old one.
2584 * Collect the arguments with which to re-exec ourself.
2586 va_start(ap
, fd_to_close
);
2587 n
= 2; /* progname and terminating NULL */
2588 n
+= inst
->ngtkargs
;
2589 while (va_arg(ap
, char *) != NULL
)
2593 args
= snewn(n
, char *);
2594 args
[0] = inst
->progname
;
2596 for (i
= 0; i
< inst
->ngtkargs
; i
++)
2597 args
[i
+1] = inst
->gtkargvstart
[i
];
2600 va_start(ap
, fd_to_close
);
2601 while ((args
[i
++] = va_arg(ap
, char *)) != NULL
);
2607 * Do the double fork.
2620 } else if (pid2
> 0) {
2622 * First child has successfully forked second child. My
2623 * Work Here Is Done. Note the use of _exit rather than
2624 * exit: the latter appears to cause destroy messages
2625 * to be sent to the X server. I suspect gtk uses
2632 * If we reach here, we are the second child, so we now
2633 * actually perform the exec.
2635 if (fd_to_close
>= 0)
2638 execv("/proc/self/exe", args
);
2639 execvp(inst
->progname
, args
);
2645 waitpid(pid
, &status
, 0);
2650 void dup_session_menuitem(GtkMenuItem
*item
, gpointer gdata
)
2652 struct gui_data
*inst
= (struct gui_data
*)gdata
;
2654 * For this feature we must marshal cfg and (possibly) pty_argv
2655 * into a byte stream, create a pipe, and send this byte stream
2656 * to the child through the pipe.
2663 if (pipe(pipefd
) < 0) {
2668 size
= sizeof(inst
->cfg
);
2669 if (use_pty_argv
&& pty_argv
) {
2670 for (i
= 0; pty_argv
[i
]; i
++)
2671 size
+= strlen(pty_argv
[i
]) + 1;
2674 data
= snewn(size
, char);
2675 memcpy(data
, &inst
->cfg
, sizeof(inst
->cfg
));
2676 if (use_pty_argv
&& pty_argv
) {
2677 int p
= sizeof(inst
->cfg
);
2678 for (i
= 0; pty_argv
[i
]; i
++) {
2679 strcpy(data
+ p
, pty_argv
[i
]);
2680 p
+= strlen(pty_argv
[i
]) + 1;
2685 sprintf(option
, "---[%d,%d]", pipefd
[0], size
);
2686 fcntl(pipefd
[0], F_SETFD
, 0);
2687 fork_and_exec_self(inst
, pipefd
[1], option
, NULL
);
2691 while (i
< size
&& (ret
= write(pipefd
[1], data
+ i
, size
- i
)) > 0)
2694 perror("write to pipe");
2699 int read_dupsession_data(struct gui_data
*inst
, Config
*cfg
, char *arg
)
2701 int fd
, i
, ret
, size
;
2704 if (sscanf(arg
, "---[%d,%d]", &fd
, &size
) != 2) {
2705 fprintf(stderr
, "%s: malformed magic argument `%s'\n", appname
, arg
);
2709 data
= snewn(size
, char);
2711 while (i
< size
&& (ret
= read(fd
, data
+ i
, size
- i
)) > 0)
2714 perror("read from pipe");
2716 } else if (i
< size
) {
2717 fprintf(stderr
, "%s: unexpected EOF in Duplicate Session data\n",
2722 memcpy(cfg
, data
, sizeof(Config
));
2723 if (use_pty_argv
&& size
> sizeof(Config
)) {
2727 while (i
< size
&& data
[i
]) i
++;
2729 fprintf(stderr
, "%s: malformed Duplicate Session data\n",
2736 pty_argv
= snewn(n
+1, char *);
2742 while (i
< size
&& data
[i
]) i
++;
2745 pty_argv
[n
++] = dupstr(p
);
2752 void new_session_menuitem(GtkMenuItem
*item
, gpointer data
)
2754 struct gui_data
*inst
= (struct gui_data
*)data
;
2756 fork_and_exec_self(inst
, -1, NULL
);
2759 void saved_session_menuitem(GtkMenuItem
*item
, gpointer data
)
2761 struct gui_data
*inst
= (struct gui_data
*)data
;
2762 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
2764 fork_and_exec_self(inst
, -1, "-load", str
, NULL
);
2767 void saved_session_freedata(GtkMenuItem
*item
, gpointer data
)
2769 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
2774 void update_specials_menu(void *frontend
)
2776 struct gui_data
*inst
= (struct gui_data
*)frontend
;
2778 const struct telnet_special
*specials
;
2780 specials
= inst
->back
->get_specials(inst
->backhandle
);
2781 gtk_container_foreach(GTK_CONTAINER(inst
->specialsmenu
),
2782 (GtkCallback
)gtk_widget_destroy
, NULL
);
2785 GtkWidget
*menuitem
;
2786 for (i
= 0; specials
[i
].name
; i
++) {
2787 if (*specials
[i
].name
) {
2788 menuitem
= gtk_menu_item_new_with_label(specials
[i
].name
);
2789 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2790 (gpointer
)specials
[i
].code
);
2791 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2792 GTK_SIGNAL_FUNC(special_menuitem
), inst
);
2794 menuitem
= gtk_menu_item_new();
2795 gtk_container_add(GTK_CONTAINER(inst
->specialsmenu
), menuitem
);
2796 gtk_widget_show(menuitem
);
2798 gtk_widget_show(inst
->specialsitem1
);
2799 gtk_widget_show(inst
->specialsitem2
);
2801 gtk_widget_hide(inst
->specialsitem1
);
2802 gtk_widget_hide(inst
->specialsitem2
);
2806 int pt_main(int argc
, char **argv
)
2808 extern Backend
*select_backend(Config
*cfg
);
2809 extern int cfgbox(Config
*cfg
);
2810 struct gui_data
*inst
;
2813 * Create an instance structure and initialise to zeroes
2815 inst
= snew(struct gui_data
);
2816 memset(inst
, 0, sizeof(*inst
));
2817 inst
->alt_keycode
= -1; /* this one needs _not_ to be zero */
2819 /* defer any child exit handling until we're ready to deal with
2821 block_signal(SIGCHLD
, 1);
2823 inst
->progname
= argv
[0];
2825 * Copy the original argv before letting gtk_init fiddle with
2826 * it. It will be required later.
2830 inst
->gtkargvstart
= snewn(argc
-1, char *);
2831 for (i
= 1; i
< argc
; i
++)
2832 inst
->gtkargvstart
[i
-1] = dupstr(argv
[i
]);
2834 gtk_init(&argc
, &argv
);
2835 inst
->ngtkargs
= oldargc
- argc
;
2838 if (argc
> 1 && !strncmp(argv
[1], "---", 3)) {
2839 read_dupsession_data(inst
, &inst
->cfg
, argv
[1]);
2840 /* Splatter this argument so it doesn't clutter a ps listing */
2841 memset(argv
[1], 0, strlen(argv
[1]));
2843 if (do_cmdline(argc
, argv
, 0, inst
, &inst
->cfg
))
2844 exit(1); /* pre-defaults pass to get -class */
2845 do_defaults(NULL
, &inst
->cfg
);
2846 if (do_cmdline(argc
, argv
, 1, inst
, &inst
->cfg
))
2847 exit(1); /* post-defaults, do everything */
2849 cmdline_run_saved(&inst
->cfg
);
2851 if (!*inst
->cfg
.host
&& !cfgbox(&inst
->cfg
))
2852 exit(0); /* config box hit Cancel */
2855 if (!compound_text_atom
)
2856 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
2857 if (!utf8_string_atom
)
2858 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
2860 setup_fonts_ucs(inst
);
2862 inst
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2865 * Set up the colour map.
2867 palette_reset(inst
);
2869 inst
->width
= inst
->cfg
.width
;
2870 inst
->height
= inst
->cfg
.height
;
2872 inst
->area
= gtk_drawing_area_new();
2873 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
2874 inst
->font_width
* inst
->cfg
.width
+ 2*inst
->cfg
.window_border
,
2875 inst
->font_height
* inst
->cfg
.height
+ 2*inst
->cfg
.window_border
);
2876 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2877 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
2878 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
2880 * We always create the scrollbar; it remains invisible if
2881 * unwanted, so we can pop it up quickly if it suddenly becomes
2884 if (inst
->cfg
.scrollbar_on_left
)
2885 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2886 gtk_box_pack_start(inst
->hbox
, inst
->area
, TRUE
, TRUE
, 0);
2887 if (!inst
->cfg
.scrollbar_on_left
)
2888 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2890 gtk_container_add(GTK_CONTAINER(inst
->window
), GTK_WIDGET(inst
->hbox
));
2892 set_geom_hints(inst
);
2894 gtk_widget_show(inst
->area
);
2895 if (inst
->cfg
.scrollbar
)
2896 gtk_widget_show(inst
->sbar
);
2898 gtk_widget_hide(inst
->sbar
);
2899 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
2902 int x
= inst
->xpos
, y
= inst
->ypos
;
2904 gtk_widget_size_request(GTK_WIDGET(inst
->window
), &req
);
2905 if (inst
->gravity
& 1) x
+= gdk_screen_width() - req
.width
;
2906 if (inst
->gravity
& 2) y
+= gdk_screen_height() - req
.height
;
2907 gtk_window_set_position(GTK_WINDOW(inst
->window
), GTK_WIN_POS_NONE
);
2908 gtk_widget_set_uposition(GTK_WIDGET(inst
->window
), x
, y
);
2911 gtk_signal_connect(GTK_OBJECT(inst
->window
), "destroy",
2912 GTK_SIGNAL_FUNC(destroy
), inst
);
2913 gtk_signal_connect(GTK_OBJECT(inst
->window
), "delete_event",
2914 GTK_SIGNAL_FUNC(delete_window
), inst
);
2915 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_press_event",
2916 GTK_SIGNAL_FUNC(key_event
), inst
);
2917 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_release_event",
2918 GTK_SIGNAL_FUNC(key_event
), inst
);
2919 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_in_event",
2920 GTK_SIGNAL_FUNC(focus_event
), inst
);
2921 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_out_event",
2922 GTK_SIGNAL_FUNC(focus_event
), inst
);
2923 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
2924 GTK_SIGNAL_FUNC(configure_area
), inst
);
2925 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
2926 GTK_SIGNAL_FUNC(expose_area
), inst
);
2927 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
2928 GTK_SIGNAL_FUNC(button_event
), inst
);
2929 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
2930 GTK_SIGNAL_FUNC(button_event
), inst
);
2931 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
2932 GTK_SIGNAL_FUNC(motion_event
), inst
);
2933 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
2934 GTK_SIGNAL_FUNC(selection_received
), inst
);
2935 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
2936 GTK_SIGNAL_FUNC(selection_get
), inst
);
2937 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
2938 GTK_SIGNAL_FUNC(selection_clear
), inst
);
2939 if (inst
->cfg
.scrollbar
)
2940 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
2941 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
2942 gtk_timeout_add(20, timer_func
, inst
);
2943 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
2944 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
2945 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
2946 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_MOTION_MASK
);
2948 gtk_widget_show(inst
->window
);
2950 set_window_background(inst
);
2953 * Set up the Ctrl+rightclick context menu.
2956 GtkWidget
*menuitem
;
2958 extern const int use_event_log
, new_session
, saved_sessions
;
2960 inst
->menu
= gtk_menu_new();
2962 #define MKMENUITEM(title, func) do { \
2963 menuitem = title ? gtk_menu_item_new_with_label(title) : \
2964 gtk_menu_item_new(); \
2965 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
2966 gtk_widget_show(menuitem); \
2968 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
2969 GTK_SIGNAL_FUNC(func), inst); \
2972 MKMENUITEM("New Session", new_session_menuitem
);
2973 MKMENUITEM("Duplicate Session", dup_session_menuitem
);
2974 if (saved_sessions
) {
2975 struct sesslist sesslist
;
2978 inst
->sessionsmenu
= gtk_menu_new();
2980 get_sesslist(&sesslist
, TRUE
);
2981 for (i
= 1; i
< sesslist
.nsessions
; i
++) {
2982 menuitem
= gtk_menu_item_new_with_label(sesslist
.sessions
[i
]);
2983 gtk_container_add(GTK_CONTAINER(inst
->sessionsmenu
), menuitem
);
2984 gtk_widget_show(menuitem
);
2985 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2986 dupstr(sesslist
.sessions
[i
]));
2987 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2988 GTK_SIGNAL_FUNC(saved_session_menuitem
),
2990 gtk_signal_connect(GTK_OBJECT(menuitem
), "destroy",
2991 GTK_SIGNAL_FUNC(saved_session_freedata
),
2994 get_sesslist(&sesslist
, FALSE
);
2996 MKMENUITEM("Saved Sessions", NULL
);
2997 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
),
2998 inst
->sessionsmenu
);
3000 MKMENUITEM(NULL
, NULL
);
3001 MKMENUITEM("Change Settings", change_settings_menuitem
);
3002 MKMENUITEM(NULL
, NULL
);
3004 MKMENUITEM("Event Log", event_log_menuitem
);
3005 MKMENUITEM("Special Commands", NULL
);
3006 inst
->specialsmenu
= gtk_menu_new();
3007 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), inst
->specialsmenu
);
3008 inst
->specialsitem1
= menuitem
;
3009 MKMENUITEM(NULL
, NULL
);
3010 inst
->specialsitem2
= menuitem
;
3011 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem
);
3012 MKMENUITEM("Reset Terminal", reset_terminal_menuitem
);
3013 MKMENUITEM("Copy All", copy_all_menuitem
);
3014 MKMENUITEM(NULL
, NULL
);
3015 s
= dupcat("About ", appname
, NULL
);
3016 MKMENUITEM(s
, about_menuitem
);
3021 inst
->textcursor
= make_mouse_ptr(inst
, GDK_XTERM
);
3022 inst
->rawcursor
= make_mouse_ptr(inst
, GDK_LEFT_PTR
);
3023 inst
->blankcursor
= make_mouse_ptr(inst
, -1);
3024 make_mouse_ptr(inst
, -2); /* clean up cursor font */
3025 inst
->currcursor
= inst
->textcursor
;
3026 show_mouseptr(inst
, 1);
3028 inst
->eventlogstuff
= eventlogstuff_new();
3030 inst
->term
= term_init(&inst
->cfg
, &inst
->ucsdata
, inst
);
3031 inst
->logctx
= log_init(inst
, &inst
->cfg
);
3032 term_provide_logctx(inst
->term
, inst
->logctx
);
3036 term_size(inst
->term
, inst
->cfg
.height
, inst
->cfg
.width
, inst
->cfg
.savelines
);
3038 inst
->back
= select_backend(&inst
->cfg
);
3040 char *realhost
, *error
;
3042 error
= inst
->back
->init((void *)inst
, &inst
->backhandle
,
3043 &inst
->cfg
, inst
->cfg
.host
, inst
->cfg
.port
,
3044 &realhost
, inst
->cfg
.tcp_nodelay
);
3047 char *msg
= dupprintf("Unable to open connection to %s:\n%s",
3048 inst
->cfg
.host
, error
);
3049 inst
->exited
= TRUE
;
3050 fatal_message_box(inst
->window
, msg
);
3055 if (inst
->cfg
.wintitle
[0]) {
3056 set_title(inst
, inst
->cfg
.wintitle
);
3057 set_icon(inst
, inst
->cfg
.wintitle
);
3059 char *title
= make_default_wintitle(realhost
);
3060 set_title(inst
, title
);
3061 set_icon(inst
, title
);
3065 inst
->back
->provide_logctx(inst
->backhandle
, inst
->logctx
);
3066 update_specials_menu(inst
);
3068 term_provide_resize_fn(inst
->term
, inst
->back
->size
, inst
->backhandle
);
3071 ldisc_create(&inst
->cfg
, inst
->term
, inst
->back
, inst
->backhandle
, inst
);
3072 ldisc_send(inst
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
3074 /* now we're reday to deal with the child exit handler being
3076 block_signal(SIGCHLD
, 0);
3079 * Block SIGPIPE: if we attempt Duplicate Session or similar
3080 * and it falls over in some way, we certainly don't want
3081 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3082 * this _after_ (at least pterm) forks off its child process,
3083 * since the child wants SIGPIPE handled in the usual way.
3085 block_signal(SIGPIPE
, 1);
3087 inst
->exited
= FALSE
;