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
, const 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
->icontitle
: inst
->wintitle
;
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 void draw_backing_rect(struct gui_data
*inst
)
373 GdkGC
*gc
= gdk_gc_new(inst
->area
->window
);
374 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
375 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
376 inst
->cfg
.width
* inst
->font_width
+ 2*inst
->cfg
.window_border
,
377 inst
->cfg
.height
* inst
->font_height
+ 2*inst
->cfg
.window_border
);
381 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
383 struct gui_data
*inst
= (struct gui_data
*)data
;
384 int w
, h
, need_size
= 0;
387 * See if the terminal size has changed, in which case we must
388 * let the terminal know.
390 w
= (event
->width
- 2*inst
->cfg
.window_border
) / inst
->font_width
;
391 h
= (event
->height
- 2*inst
->cfg
.window_border
) / inst
->font_height
;
392 if (w
!= inst
->width
|| h
!= inst
->height
) {
393 inst
->cfg
.width
= inst
->width
= w
;
394 inst
->cfg
.height
= inst
->height
= h
;
399 gdk_pixmap_unref(inst
->pixmap
);
403 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
404 (inst
->cfg
.width
* inst
->font_width
+
405 2*inst
->cfg
.window_border
),
406 (inst
->cfg
.height
* inst
->font_height
+
407 2*inst
->cfg
.window_border
), -1);
409 draw_backing_rect(inst
);
411 if (need_size
&& inst
->term
) {
412 term_size(inst
->term
, h
, w
, inst
->cfg
.savelines
);
416 term_invalidate(inst
->term
);
421 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
423 struct gui_data
*inst
= (struct gui_data
*)data
;
426 * Pass the exposed rectangle to terminal.c, which will call us
427 * back to do the actual painting.
430 gdk_draw_pixmap(widget
->window
,
431 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
433 event
->area
.x
, event
->area
.y
,
434 event
->area
.x
, event
->area
.y
,
435 event
->area
.width
, event
->area
.height
);
440 #define KEY_PRESSED(k) \
441 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
443 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
445 struct gui_data
*inst
= (struct gui_data
*)data
;
447 wchar_t ucsoutput
[2];
448 int ucsval
, start
, end
, special
, use_ucsoutput
;
450 /* By default, nothing is generated. */
452 special
= use_ucsoutput
= FALSE
;
455 * If Alt is being released after typing an Alt+numberpad
456 * sequence, we should generate the code that was typed.
458 * Note that we only do this if more than one key was actually
459 * pressed - I don't think Alt+NumPad4 should be ^D or that
460 * Alt+NumPad3 should be ^C, for example. There's no serious
461 * inconvenience in having to type a zero before a single-digit
464 if (event
->type
== GDK_KEY_RELEASE
&&
465 (event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
466 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
) &&
467 inst
->alt_keycode
>= 0 && inst
->alt_digits
> 1) {
469 printf("Alt key up, keycode = %d\n", inst
->alt_keycode
);
471 output
[0] = inst
->alt_keycode
;
476 if (event
->type
== GDK_KEY_PRESS
) {
480 printf("keypress: keyval = %04x, state = %08x; string =",
481 event
->keyval
, event
->state
);
482 for (i
= 0; event
->string
[i
]; i
++)
483 printf(" %02x", (unsigned char) event
->string
[i
]);
489 * NYI: Compose key (!!! requires Unicode faff before even trying)
493 * If Alt has just been pressed, we start potentially
494 * accumulating an Alt+numberpad code. We do this by
495 * setting alt_keycode to -1 (nothing yet but plausible).
497 if ((event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
498 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
)) {
499 inst
->alt_keycode
= -1;
500 inst
->alt_digits
= 0;
501 goto done
; /* this generates nothing else */
505 * If we're seeing a numberpad key press with Mod1 down,
506 * consider adding it to alt_keycode if that's sensible.
507 * Anything _else_ with Mod1 down cancels any possibility
508 * of an ALT keycode: we set alt_keycode to -2.
510 if ((event
->state
& GDK_MOD1_MASK
) && inst
->alt_keycode
!= -2) {
512 switch (event
->keyval
) {
513 case GDK_KP_0
: case GDK_KP_Insert
: digit
= 0; break;
514 case GDK_KP_1
: case GDK_KP_End
: digit
= 1; break;
515 case GDK_KP_2
: case GDK_KP_Down
: digit
= 2; break;
516 case GDK_KP_3
: case GDK_KP_Page_Down
: digit
= 3; break;
517 case GDK_KP_4
: case GDK_KP_Left
: digit
= 4; break;
518 case GDK_KP_5
: case GDK_KP_Begin
: digit
= 5; break;
519 case GDK_KP_6
: case GDK_KP_Right
: digit
= 6; break;
520 case GDK_KP_7
: case GDK_KP_Home
: digit
= 7; break;
521 case GDK_KP_8
: case GDK_KP_Up
: digit
= 8; break;
522 case GDK_KP_9
: case GDK_KP_Page_Up
: digit
= 9; break;
525 inst
->alt_keycode
= -2; /* it's invalid */
528 printf("Adding digit %d to keycode %d", digit
,
531 if (inst
->alt_keycode
== -1)
532 inst
->alt_keycode
= digit
; /* one-digit code */
534 inst
->alt_keycode
= inst
->alt_keycode
* 10 + digit
;
537 printf(" gives new code %d\n", inst
->alt_keycode
);
539 /* Having used this digit, we now do nothing more with it. */
545 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
548 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
549 term_scroll(inst
->term
, 0, -inst
->cfg
.height
/2);
552 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_CONTROL_MASK
)) {
553 term_scroll(inst
->term
, 0, -1);
556 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
557 term_scroll(inst
->term
, 0, +inst
->cfg
.height
/2);
560 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_CONTROL_MASK
)) {
561 term_scroll(inst
->term
, 0, +1);
566 * Neither does Shift-Ins.
568 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
574 use_ucsoutput
= FALSE
;
576 /* ALT+things gives leading Escape. */
578 strncpy(output
+1, event
->string
, 31);
579 if (!*event
->string
&&
580 (ucsval
= keysym_to_unicode(event
->keyval
)) >= 0) {
581 ucsoutput
[0] = '\033';
582 ucsoutput
[1] = ucsval
;
583 use_ucsoutput
= TRUE
;
587 end
= strlen(output
);
589 if (event
->state
& GDK_MOD1_MASK
) {
591 if (end
== 1) end
= 0;
595 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
596 if (!event
->string
[0] && event
->keyval
== '`' &&
597 (event
->state
& GDK_CONTROL_MASK
)) {
599 use_ucsoutput
= FALSE
;
603 /* Control-Break is the same as Control-C */
604 if (event
->keyval
== GDK_Break
&&
605 (event
->state
& GDK_CONTROL_MASK
)) {
607 use_ucsoutput
= FALSE
;
612 /* We handle Return ourselves, because it needs to be flagged as
613 * special to ldisc. */
614 if (event
->keyval
== GDK_Return
) {
616 use_ucsoutput
= FALSE
;
621 /* Control-2, Control-Space and Control-@ are NUL */
622 if (!event
->string
[0] &&
623 (event
->keyval
== ' ' || event
->keyval
== '2' ||
624 event
->keyval
== '@') &&
625 (event
->state
& (GDK_SHIFT_MASK
|
626 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
628 use_ucsoutput
= FALSE
;
632 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
633 if (!event
->string
[0] && event
->keyval
== ' ' &&
634 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
635 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
637 use_ucsoutput
= FALSE
;
641 /* We don't let GTK tell us what Backspace is! We know better. */
642 if (event
->keyval
== GDK_BackSpace
&&
643 !(event
->state
& GDK_SHIFT_MASK
)) {
644 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x7F' : '\x08';
645 use_ucsoutput
= FALSE
;
649 /* For Shift Backspace, do opposite of what is configured. */
650 if (event
->keyval
== GDK_BackSpace
&&
651 (event
->state
& GDK_SHIFT_MASK
)) {
652 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x08' : '\x7F';
653 use_ucsoutput
= FALSE
;
658 /* Shift-Tab is ESC [ Z */
659 if (event
->keyval
== GDK_ISO_Left_Tab
||
660 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
661 end
= 1 + sprintf(output
+1, "\033[Z");
662 use_ucsoutput
= FALSE
;
666 * NetHack keypad mode.
668 if (inst
->cfg
.nethack_keypad
) {
670 switch (event
->keyval
) {
671 case GDK_KP_1
: case GDK_KP_End
: keys
= "bB"; break;
672 case GDK_KP_2
: case GDK_KP_Down
: keys
= "jJ"; break;
673 case GDK_KP_3
: case GDK_KP_Page_Down
: keys
= "nN"; break;
674 case GDK_KP_4
: case GDK_KP_Left
: keys
= "hH"; break;
675 case GDK_KP_5
: case GDK_KP_Begin
: keys
= ".."; break;
676 case GDK_KP_6
: case GDK_KP_Right
: keys
= "lL"; break;
677 case GDK_KP_7
: case GDK_KP_Home
: keys
= "yY"; break;
678 case GDK_KP_8
: case GDK_KP_Up
: keys
= "kK"; break;
679 case GDK_KP_9
: case GDK_KP_Page_Up
: keys
= "uU"; break;
683 if (event
->state
& GDK_SHIFT_MASK
)
687 use_ucsoutput
= FALSE
;
693 * Application keypad mode.
695 if (inst
->term
->app_keypad_keys
&& !inst
->cfg
.no_applic_k
) {
697 switch (event
->keyval
) {
698 case GDK_Num_Lock
: xkey
= 'P'; break;
699 case GDK_KP_Divide
: xkey
= 'Q'; break;
700 case GDK_KP_Multiply
: xkey
= 'R'; break;
701 case GDK_KP_Subtract
: xkey
= 'S'; break;
703 * Keypad + is tricky. It covers a space that would
704 * be taken up on the VT100 by _two_ keys; so we
705 * let Shift select between the two. Worse still,
706 * in xterm function key mode we change which two...
709 if (inst
->cfg
.funky_type
== 2) {
710 if (event
->state
& GDK_SHIFT_MASK
)
714 } else if (event
->state
& GDK_SHIFT_MASK
)
719 case GDK_KP_Enter
: xkey
= 'M'; break;
720 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
721 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
722 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
723 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
724 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
725 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
726 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
727 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
728 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
729 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
730 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
733 if (inst
->term
->vt52_mode
) {
734 if (xkey
>= 'P' && xkey
<= 'S')
735 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
737 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
739 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
740 use_ucsoutput
= FALSE
;
746 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
747 * for integer decimal nn.)
749 * We also deal with the weird ones here. Linux VCs replace F1
750 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
751 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
756 switch (event
->keyval
) {
758 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
761 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
764 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
767 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
770 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
773 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
776 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
779 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
782 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
785 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
818 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
819 case GDK_Home
: case GDK_KP_Home
:
822 case GDK_Insert
: case GDK_KP_Insert
:
825 case GDK_Delete
: case GDK_KP_Delete
:
828 case GDK_End
: case GDK_KP_End
:
831 case GDK_Page_Up
: case GDK_KP_Page_Up
:
834 case GDK_Page_Down
: case GDK_KP_Page_Down
:
838 /* Reorder edit keys to physical order */
839 if (inst
->cfg
.funky_type
== 3 && code
<= 6)
840 code
= "\0\2\1\4\5\3\6"[code
];
842 if (inst
->term
->vt52_mode
&& code
> 0 && code
<= 6) {
843 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
844 use_ucsoutput
= FALSE
;
848 if (inst
->cfg
.funky_type
== 5 && /* SCO function keys */
849 code
>= 11 && code
<= 34) {
850 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
852 switch (event
->keyval
) {
853 case GDK_F1
: index
= 0; break;
854 case GDK_F2
: index
= 1; break;
855 case GDK_F3
: index
= 2; break;
856 case GDK_F4
: index
= 3; break;
857 case GDK_F5
: index
= 4; break;
858 case GDK_F6
: index
= 5; break;
859 case GDK_F7
: index
= 6; break;
860 case GDK_F8
: index
= 7; break;
861 case GDK_F9
: index
= 8; break;
862 case GDK_F10
: index
= 9; break;
863 case GDK_F11
: index
= 10; break;
864 case GDK_F12
: index
= 11; break;
866 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
867 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
868 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
869 use_ucsoutput
= FALSE
;
872 if (inst
->cfg
.funky_type
== 5 && /* SCO small keypad */
873 code
>= 1 && code
<= 6) {
874 char codes
[] = "HL.FIG";
879 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
881 use_ucsoutput
= FALSE
;
884 if ((inst
->term
->vt52_mode
|| inst
->cfg
.funky_type
== 4) &&
885 code
>= 11 && code
<= 24) {
891 if (inst
->term
->vt52_mode
)
892 end
= 1 + sprintf(output
+1,
893 "\x1B%c", code
+ 'P' - 11 - offt
);
895 end
= 1 + sprintf(output
+1,
896 "\x1BO%c", code
+ 'P' - 11 - offt
);
897 use_ucsoutput
= FALSE
;
900 if (inst
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
901 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
902 use_ucsoutput
= FALSE
;
905 if (inst
->cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
906 if (inst
->term
->vt52_mode
)
907 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
909 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
910 use_ucsoutput
= FALSE
;
913 if (inst
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
914 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
915 use_ucsoutput
= FALSE
;
919 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
920 use_ucsoutput
= FALSE
;
926 * Cursor keys. (This includes the numberpad cursor keys,
927 * if we haven't already done them due to app keypad mode.)
929 * Here we also process un-numlocked un-appkeypadded KP5,
930 * which sends ESC [ G.
934 switch (event
->keyval
) {
935 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
936 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
937 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
938 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
939 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
943 * The arrow keys normally do ESC [ A and so on. In
944 * app cursor keys mode they do ESC O A instead.
945 * Ctrl toggles the two modes.
947 if (inst
->term
->vt52_mode
) {
948 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
949 } else if (!inst
->term
->app_cursor_keys
^
950 !(event
->state
& GDK_CONTROL_MASK
)) {
951 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
953 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
955 use_ucsoutput
= FALSE
;
967 printf("generating sequence:");
968 for (i
= start
; i
< end
; i
++)
969 printf(" %02x", (unsigned char) output
[i
]);
975 * For special control characters, the character set
976 * should never matter.
978 output
[end
] = '\0'; /* NUL-terminate */
979 ldisc_send(inst
->ldisc
, output
+start
, -2, 1);
980 } else if (!inst
->direct_to_font
) {
981 if (!use_ucsoutput
) {
983 * The stuff we've just generated is assumed to be
984 * ISO-8859-1! This sounds insane, but `man
985 * XLookupString' agrees: strings of this type
986 * returned from the X server are hardcoded to
987 * 8859-1. Strictly speaking we should be doing
988 * this using some sort of GtkIMContext, which (if
989 * we're lucky) would give us our data directly in
990 * Unicode; but that's not supported in GTK 1.2 as
991 * far as I can tell, and it's poorly documented
992 * even in 2.0, so it'll have to wait.
994 lpage_send(inst
->ldisc
, CS_ISO8859_1
, output
+start
,
998 * We generated our own Unicode key data from the
999 * keysym, so use that instead.
1001 luni_send(inst
->ldisc
, ucsoutput
+start
, end
-start
, 1);
1005 * In direct-to-font mode, we just send the string
1006 * exactly as we received it.
1008 ldisc_send(inst
->ldisc
, output
+start
, end
-start
, 1);
1011 show_mouseptr(inst
, 0);
1012 term_seen_key_event(inst
->term
);
1013 term_out(inst
->term
);
1019 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
1021 struct gui_data
*inst
= (struct gui_data
*)data
;
1022 int shift
, ctrl
, alt
, x
, y
, button
, act
;
1024 show_mouseptr(inst
, 1);
1026 if (event
->button
== 4 && event
->type
== GDK_BUTTON_PRESS
) {
1027 term_scroll(inst
->term
, 0, -5);
1030 if (event
->button
== 5 && event
->type
== GDK_BUTTON_PRESS
) {
1031 term_scroll(inst
->term
, 0, +5);
1035 shift
= event
->state
& GDK_SHIFT_MASK
;
1036 ctrl
= event
->state
& GDK_CONTROL_MASK
;
1037 alt
= event
->state
& GDK_MOD1_MASK
;
1039 if (event
->button
== 3 && ctrl
) {
1040 gtk_menu_popup(GTK_MENU(inst
->menu
), NULL
, NULL
, NULL
, NULL
,
1041 event
->button
, event
->time
);
1045 if (event
->button
== 1)
1047 else if (event
->button
== 2)
1048 button
= MBT_MIDDLE
;
1049 else if (event
->button
== 3)
1052 return FALSE
; /* don't even know what button! */
1054 switch (event
->type
) {
1055 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
1056 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
1057 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
1058 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
1059 default: return FALSE
; /* don't know this event type */
1062 if (send_raw_mouse
&& !(inst
->cfg
.mouse_override
&& shift
) &&
1063 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
1064 return TRUE
; /* we ignore these in raw mouse mode */
1066 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1067 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1069 term_mouse(inst
->term
, button
, translate_button(button
), act
,
1070 x
, y
, shift
, ctrl
, alt
);
1075 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
1077 struct gui_data
*inst
= (struct gui_data
*)data
;
1078 int shift
, ctrl
, alt
, x
, y
, button
;
1080 show_mouseptr(inst
, 1);
1082 shift
= event
->state
& GDK_SHIFT_MASK
;
1083 ctrl
= event
->state
& GDK_CONTROL_MASK
;
1084 alt
= event
->state
& GDK_MOD1_MASK
;
1085 if (event
->state
& GDK_BUTTON1_MASK
)
1087 else if (event
->state
& GDK_BUTTON2_MASK
)
1088 button
= MBT_MIDDLE
;
1089 else if (event
->state
& GDK_BUTTON3_MASK
)
1092 return FALSE
; /* don't even know what button! */
1094 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1095 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1097 term_mouse(inst
->term
, button
, translate_button(button
), MA_DRAG
,
1098 x
, y
, shift
, ctrl
, alt
);
1103 void frontend_keypress(void *handle
)
1105 struct gui_data
*inst
= (struct gui_data
*)handle
;
1108 * If our child process has exited but not closed, terminate on
1115 gint
timer_func(gpointer data
)
1117 struct gui_data
*inst
= (struct gui_data
*)data
;
1120 if (!inst
->exited
&&
1121 (exitcode
= inst
->back
->exitcode(inst
->backhandle
)) >= 0) {
1122 inst
->exited
= TRUE
;
1123 if (inst
->cfg
.close_on_exit
== FORCE_ON
||
1124 (inst
->cfg
.close_on_exit
== AUTO
&& exitcode
== 0))
1125 exit(0); /* just go. */
1128 term_update(inst
->term
);
1129 term_blink(inst
->term
, 0);
1133 void fd_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
1136 * We must process exceptional notifications before ordinary
1137 * readability ones, or we may go straight past the urgent
1140 if (condition
& GDK_INPUT_EXCEPTION
)
1141 select_result(sourcefd
, 4);
1142 if (condition
& GDK_INPUT_READ
)
1143 select_result(sourcefd
, 1);
1144 if (condition
& GDK_INPUT_WRITE
)
1145 select_result(sourcefd
, 2);
1148 void destroy(GtkWidget
*widget
, gpointer data
)
1153 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
1155 struct gui_data
*inst
= (struct gui_data
*)data
;
1156 inst
->term
->has_focus
= event
->in
;
1157 term_out(inst
->term
);
1158 term_update(inst
->term
);
1159 show_mouseptr(inst
, 1);
1164 * set or clear the "raw mouse message" mode
1166 void set_raw_mouse_mode(void *frontend
, int activate
)
1168 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1169 activate
= activate
&& !inst
->cfg
.no_mouse_rep
;
1170 send_raw_mouse
= activate
;
1172 inst
->currcursor
= inst
->rawcursor
;
1174 inst
->currcursor
= inst
->textcursor
;
1175 show_mouseptr(inst
, inst
->mouseptr_visible
);
1178 void request_resize(void *frontend
, int w
, int h
)
1180 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1181 int large_x
, large_y
;
1182 int offset_x
, offset_y
;
1184 GtkRequisition inner
, outer
;
1187 * This is a heinous hack dreamed up by the gnome-terminal
1188 * people to get around a limitation in gtk. The problem is
1189 * that in order to set the size correctly we really need to be
1190 * calling gtk_window_resize - but that needs to know the size
1191 * of the _whole window_, not the drawing area. So what we do
1192 * is to set an artificially huge size request on the drawing
1193 * area, recompute the resulting size request on the window,
1194 * and look at the difference between the two. That gives us
1195 * the x and y offsets we need to translate drawing area size
1196 * into window size for real, and then we call
1197 * gtk_window_resize.
1201 * We start by retrieving the current size of the whole window.
1202 * Adding a bit to _that_ will give us a value we can use as a
1203 * bogus size request which guarantees to be bigger than the
1204 * current size of the drawing area.
1206 get_window_pixels(inst
, &large_x
, &large_y
);
1210 #if GTK_CHECK_VERSION(2,0,0)
1211 gtk_widget_set_size_request(inst
->area
, large_x
, large_y
);
1213 gtk_widget_set_usize(inst
->area
, large_x
, large_y
);
1215 gtk_widget_size_request(inst
->area
, &inner
);
1216 gtk_widget_size_request(inst
->window
, &outer
);
1218 offset_x
= outer
.width
- inner
.width
;
1219 offset_y
= outer
.height
- inner
.height
;
1221 area_x
= inst
->font_width
* w
+ 2*inst
->cfg
.window_border
;
1222 area_y
= inst
->font_height
* h
+ 2*inst
->cfg
.window_border
;
1225 * Now we must set the size request on the drawing area back to
1226 * something sensible before we commit the real resize. Best
1227 * way to do this, I think, is to set it to what the size is
1228 * really going to end up being.
1230 #if GTK_CHECK_VERSION(2,0,0)
1231 gtk_widget_set_size_request(inst
->area
, area_x
, area_y
);
1233 gtk_widget_set_usize(inst
->area
, area_x
, area_y
);
1234 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
), area_x
, area_y
);
1237 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst
->window
));
1239 #if GTK_CHECK_VERSION(2,0,0)
1240 gtk_window_resize(GTK_WINDOW(inst
->window
),
1241 area_x
+ offset_x
, area_y
+ offset_y
);
1243 gdk_window_resize(inst
->window
->window
,
1244 area_x
+ offset_x
, area_y
+ offset_y
);
1248 static void real_palette_set(struct gui_data
*inst
, int n
, int r
, int g
, int b
)
1250 gboolean success
[1];
1252 inst
->cols
[n
].red
= r
* 0x0101;
1253 inst
->cols
[n
].green
= g
* 0x0101;
1254 inst
->cols
[n
].blue
= b
* 0x0101;
1256 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
+ n
, 1);
1257 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
+ n
, 1,
1258 FALSE
, FALSE
, success
);
1260 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname
,
1264 void set_window_background(struct gui_data
*inst
)
1266 if (inst
->area
&& inst
->area
->window
)
1267 gdk_window_set_background(inst
->area
->window
, &inst
->cols
[18]);
1268 if (inst
->window
&& inst
->window
->window
)
1269 gdk_window_set_background(inst
->window
->window
, &inst
->cols
[18]);
1272 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1274 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1275 static const int first
[21] = {
1276 0, 2, 4, 6, 8, 10, 12, 14,
1277 1, 3, 5, 7, 9, 11, 13, 15,
1280 real_palette_set(inst
, first
[n
], r
, g
, b
);
1282 real_palette_set(inst
, first
[n
] + 1, r
, g
, b
);
1284 set_window_background(inst
);
1287 void palette_reset(void *frontend
)
1289 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1290 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1291 static const int ww
[] = {
1292 6, 7, 8, 9, 10, 11, 12, 13,
1293 14, 15, 16, 17, 18, 19, 20, 21,
1296 gboolean success
[NCOLOURS
];
1299 assert(lenof(ww
) == NCOLOURS
);
1301 if (!inst
->colmap
) {
1302 inst
->colmap
= gdk_colormap_get_system();
1304 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
, NCOLOURS
);
1307 for (i
= 0; i
< NCOLOURS
; i
++) {
1308 inst
->cols
[i
].red
= inst
->cfg
.colours
[ww
[i
]][0] * 0x0101;
1309 inst
->cols
[i
].green
= inst
->cfg
.colours
[ww
[i
]][1] * 0x0101;
1310 inst
->cols
[i
].blue
= inst
->cfg
.colours
[ww
[i
]][2] * 0x0101;
1313 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
1314 FALSE
, FALSE
, success
);
1315 for (i
= 0; i
< NCOLOURS
; i
++) {
1317 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1318 appname
, i
, inst
->cfg
.colours
[i
][0],
1319 inst
->cfg
.colours
[i
][1], inst
->cfg
.colours
[i
][2]);
1322 set_window_background(inst
);
1325 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
1327 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1328 if (inst
->pasteout_data
)
1329 sfree(inst
->pasteout_data
);
1330 if (inst
->pasteout_data_utf8
)
1331 sfree(inst
->pasteout_data_utf8
);
1334 * Set up UTF-8 paste data. This only happens if we aren't in
1335 * direct-to-font mode using the D800 hack.
1337 if (!inst
->direct_to_font
) {
1338 wchar_t *tmp
= data
;
1341 inst
->pasteout_data_utf8
= snewn(len
*6, char);
1342 inst
->pasteout_data_utf8_len
= len
*6;
1343 inst
->pasteout_data_utf8_len
=
1344 charset_from_unicode(&tmp
, &tmplen
, inst
->pasteout_data_utf8
,
1345 inst
->pasteout_data_utf8_len
,
1346 CS_UTF8
, NULL
, NULL
, 0);
1347 if (inst
->pasteout_data_utf8_len
== 0) {
1348 sfree(inst
->pasteout_data_utf8
);
1349 inst
->pasteout_data_utf8
= NULL
;
1351 inst
->pasteout_data_utf8
=
1352 sresize(inst
->pasteout_data_utf8
,
1353 inst
->pasteout_data_utf8_len
, char);
1356 inst
->pasteout_data_utf8
= NULL
;
1357 inst
->pasteout_data_utf8_len
= 0;
1360 inst
->pasteout_data
= snewn(len
*6, char);
1361 inst
->pasteout_data_len
= len
*6;
1362 inst
->pasteout_data_len
= wc_to_mb(inst
->ucsdata
.line_codepage
, 0,
1363 data
, len
, inst
->pasteout_data
,
1364 inst
->pasteout_data_len
,
1366 if (inst
->pasteout_data_len
== 0) {
1367 sfree(inst
->pasteout_data
);
1368 inst
->pasteout_data
= NULL
;
1370 inst
->pasteout_data
=
1371 sresize(inst
->pasteout_data
, inst
->pasteout_data_len
, char);
1374 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
1375 GDK_CURRENT_TIME
)) {
1376 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1377 GDK_SELECTION_TYPE_STRING
, 1);
1378 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1379 compound_text_atom
, 1);
1380 if (inst
->pasteout_data_utf8
)
1381 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1382 utf8_string_atom
, 1);
1386 term_deselect(inst
->term
);
1389 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1390 guint info
, guint time_stamp
, gpointer data
)
1392 struct gui_data
*inst
= (struct gui_data
*)data
;
1393 if (seldata
->target
== utf8_string_atom
)
1394 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1395 inst
->pasteout_data_utf8
,
1396 inst
->pasteout_data_utf8_len
);
1398 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1399 inst
->pasteout_data
, inst
->pasteout_data_len
);
1402 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1405 struct gui_data
*inst
= (struct gui_data
*)data
;
1406 term_deselect(inst
->term
);
1407 if (inst
->pasteout_data
)
1408 sfree(inst
->pasteout_data
);
1409 if (inst
->pasteout_data_utf8
)
1410 sfree(inst
->pasteout_data_utf8
);
1411 inst
->pasteout_data
= NULL
;
1412 inst
->pasteout_data_len
= 0;
1413 inst
->pasteout_data_utf8
= NULL
;
1414 inst
->pasteout_data_utf8_len
= 0;
1418 void request_paste(void *frontend
)
1420 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1422 * In Unix, pasting is asynchronous: all we can do at the
1423 * moment is to call gtk_selection_convert(), and when the data
1424 * comes back _then_ we can call term_do_paste().
1427 if (!inst
->direct_to_font
) {
1429 * First we attempt to retrieve the selection as a UTF-8
1430 * string (which we will convert to the correct code page
1431 * before sending to the session, of course). If that
1432 * fails, selection_received() will be informed and will
1433 * fall back to an ordinary string.
1435 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1436 utf8_string_atom
, GDK_CURRENT_TIME
);
1439 * If we're in direct-to-font mode, we disable UTF-8
1440 * pasting, and go straight to ordinary string data.
1442 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1443 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1447 gint
idle_paste_func(gpointer data
); /* forward ref */
1449 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1450 guint time
, gpointer data
)
1452 struct gui_data
*inst
= (struct gui_data
*)data
;
1454 if (seldata
->target
== utf8_string_atom
&& seldata
->length
<= 0) {
1456 * Failed to get a UTF-8 selection string. Try an ordinary
1459 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1460 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1465 * Any other failure should just go foom.
1467 if (seldata
->length
<= 0 ||
1468 (seldata
->type
!= GDK_SELECTION_TYPE_STRING
&&
1469 seldata
->type
!= utf8_string_atom
))
1470 return; /* Nothing happens. */
1472 if (inst
->pastein_data
)
1473 sfree(inst
->pastein_data
);
1475 inst
->pastein_data
= snewn(seldata
->length
, wchar_t);
1476 inst
->pastein_data_len
= seldata
->length
;
1477 inst
->pastein_data_len
=
1478 mb_to_wc((seldata
->type
== utf8_string_atom ?
1479 CS_UTF8
: inst
->ucsdata
.line_codepage
),
1480 0, seldata
->data
, seldata
->length
,
1481 inst
->pastein_data
, inst
->pastein_data_len
);
1483 term_do_paste(inst
->term
);
1485 if (term_paste_pending(inst
->term
))
1486 inst
->term_paste_idle_id
= gtk_idle_add(idle_paste_func
, inst
);
1489 gint
idle_paste_func(gpointer data
)
1491 struct gui_data
*inst
= (struct gui_data
*)data
;
1493 if (term_paste_pending(inst
->term
))
1494 term_paste(inst
->term
);
1496 gtk_idle_remove(inst
->term_paste_idle_id
);
1502 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
1504 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1507 *p
= inst
->pastein_data
;
1508 *len
= inst
->pastein_data_len
;
1512 static void set_window_titles(struct gui_data
*inst
)
1515 * We must always call set_icon_name after calling set_title,
1516 * since set_title will write both names. Irritating, but such
1519 gtk_window_set_title(GTK_WINDOW(inst
->window
), inst
->wintitle
);
1520 if (!inst
->cfg
.win_name_always
)
1521 gdk_window_set_icon_name(inst
->window
->window
, inst
->icontitle
);
1524 void set_title(void *frontend
, char *title
)
1526 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1527 strncpy(inst
->wintitle
, title
, lenof(inst
->wintitle
));
1528 inst
->wintitle
[lenof(inst
->wintitle
)-1] = '\0';
1529 set_window_titles(inst
);
1532 void set_icon(void *frontend
, char *title
)
1534 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1535 strncpy(inst
->icontitle
, title
, lenof(inst
->icontitle
));
1536 inst
->icontitle
[lenof(inst
->icontitle
)-1] = '\0';
1537 set_window_titles(inst
);
1540 void set_sbar(void *frontend
, int total
, int start
, int page
)
1542 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1543 if (!inst
->cfg
.scrollbar
)
1545 inst
->sbar_adjust
->lower
= 0;
1546 inst
->sbar_adjust
->upper
= total
;
1547 inst
->sbar_adjust
->value
= start
;
1548 inst
->sbar_adjust
->page_size
= page
;
1549 inst
->sbar_adjust
->step_increment
= 1;
1550 inst
->sbar_adjust
->page_increment
= page
/2;
1551 inst
->ignore_sbar
= TRUE
;
1552 gtk_adjustment_changed(inst
->sbar_adjust
);
1553 inst
->ignore_sbar
= FALSE
;
1556 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
1558 struct gui_data
*inst
= (struct gui_data
*)data
;
1560 if (!inst
->cfg
.scrollbar
)
1562 if (!inst
->ignore_sbar
)
1563 term_scroll(inst
->term
, 1, (int)adj
->value
);
1566 void sys_cursor(void *frontend
, int x
, int y
)
1569 * This is meaningless under X.
1574 * This is still called when mode==BELL_VISUAL, even though the
1575 * visual bell is handled entirely within terminal.c, because we
1576 * may want to perform additional actions on any kind of bell (for
1577 * example, taskbar flashing in Windows).
1579 void beep(void *frontend
, int mode
)
1581 if (mode
!= BELL_VISUAL
)
1585 int char_width(Context ctx
, int uc
)
1588 * Under X, any fixed-width font really _is_ fixed-width.
1589 * Double-width characters will be dealt with using a separate
1590 * font. For the moment we can simply return 1.
1595 Context
get_ctx(void *frontend
)
1597 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1598 struct draw_ctx
*dctx
;
1600 if (!inst
->area
->window
)
1603 dctx
= snew(struct draw_ctx
);
1605 dctx
->gc
= gdk_gc_new(inst
->area
->window
);
1609 void free_ctx(Context ctx
)
1611 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1612 /* struct gui_data *inst = dctx->inst; */
1613 GdkGC
*gc
= dctx
->gc
;
1619 * Draw a line of text in the window, at given character
1620 * coordinates, in given attributes.
1622 * We are allowed to fiddle with the contents of `text'.
1624 void do_text_internal(Context ctx
, int x
, int y
, char *text
, int len
,
1625 unsigned long attr
, int lattr
)
1627 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1628 struct gui_data
*inst
= dctx
->inst
;
1629 GdkGC
*gc
= dctx
->gc
;
1631 int nfg
, nbg
, t
, fontid
, shadow
, rlen
, widefactor
;
1633 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1634 nfg
= 2 * (nfg
& 0xF) + (nfg
& 0x10 ?
1 : 0);
1635 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1636 nbg
= 2 * (nbg
& 0xF) + (nbg
& 0x10 ?
1 : 0);
1637 if (attr
& ATTR_REVERSE
) {
1642 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
1644 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
1646 if (attr
& TATTR_ACTCURS
) {
1651 fontid
= shadow
= 0;
1653 if (attr
& ATTR_WIDE
) {
1660 if ((attr
& ATTR_BOLD
) && !inst
->cfg
.bold_colour
) {
1661 if (inst
->fonts
[fontid
| 1])
1667 if (lattr
!= LATTR_NORM
) {
1669 if (x
>= inst
->term
->cols
)
1671 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1672 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1680 r
.x
= x
*inst
->font_width
+inst
->cfg
.window_border
;
1681 r
.y
= y
*inst
->font_height
+inst
->cfg
.window_border
;
1682 r
.width
= rlen
*widefactor
*inst
->font_width
;
1683 r
.height
= inst
->font_height
;
1684 gdk_gc_set_clip_rectangle(gc
, &r
);
1687 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
1688 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
1689 x
*inst
->font_width
+inst
->cfg
.window_border
,
1690 y
*inst
->font_height
+inst
->cfg
.window_border
,
1691 rlen
*widefactor
*inst
->font_width
, inst
->font_height
);
1693 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
1700 wcs
= snewn(len
+1, wchar_t);
1701 for (i
= 0; i
< len
; i
++) {
1702 wcs
[i
] = (wchar_t) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
1705 if (inst
->fonts
[fontid
] == NULL
) {
1707 * The font for this contingency does not exist.
1708 * Typically this means we've been given ATTR_WIDE
1709 * character and have no wide font. So we display
1710 * nothing at all; such is life.
1712 } else if (inst
->fontinfo
[fontid
].is_wide
) {
1714 * At least one version of gdk_draw_text_wc() has a
1715 * weird bug whereby it reads `len' elements of the
1716 * input string, but only draws `len/2'. Hence I'm
1717 * going to make its input array twice as long as it
1718 * theoretically needs to be, and pass in twice the
1719 * actual number of characters. If a fixed gdk actually
1720 * takes the doubled length seriously, then (a) the
1721 * array will stand scrutiny up to the full length, (b)
1722 * the spare elements of the array are full of zeroes
1723 * which will probably be an empty glyph in the font,
1724 * and (c) the clip rectangle should prevent it causing
1727 gwcs
= snewn(len
*2+1, GdkWChar
);
1728 memset(gwcs
, 0, sizeof(GdkWChar
) * (len
*2+1));
1730 * FIXME: when we have a wide-char equivalent of
1731 * from_unicode, use it instead of this.
1733 for (i
= 0; i
<= len
; i
++)
1735 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1736 x
*inst
->font_width
+inst
->cfg
.window_border
,
1737 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1741 gcs
= snewn(len
+1, gchar
);
1742 wc_to_mb(inst
->fontinfo
[fontid
].charset
, 0,
1743 wcs
, len
, gcs
, len
, ".", NULL
, NULL
);
1744 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1745 x
*inst
->font_width
+inst
->cfg
.window_border
,
1746 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1754 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1755 x
*inst
->font_width
+inst
->cfg
.window_border
+ inst
->cfg
.shadowboldoffset
,
1756 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1760 if (attr
& ATTR_UNDER
) {
1761 int uheight
= inst
->fonts
[0]->ascent
+ 1;
1762 if (uheight
>= inst
->font_height
)
1763 uheight
= inst
->font_height
- 1;
1764 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+inst
->cfg
.window_border
,
1765 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
,
1766 (x
+len
)*widefactor
*inst
->font_width
-1+inst
->cfg
.window_border
,
1767 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
);
1770 if (lattr
!= LATTR_NORM
) {
1772 * I can't find any plausible StretchBlt equivalent in the
1773 * X server, so I'm going to do this the slow and painful
1774 * way. This will involve repeated calls to
1775 * gdk_draw_pixmap() to stretch the text horizontally. It's
1776 * O(N^2) in time and O(N) in network bandwidth, but you
1777 * try thinking of a better way. :-(
1780 for (i
= 0; i
< len
* widefactor
* inst
->font_width
; i
++) {
1781 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1782 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
,
1783 y
*inst
->font_height
+inst
->cfg
.window_border
,
1784 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
+1,
1785 y
*inst
->font_height
+inst
->cfg
.window_border
,
1786 len
* inst
->font_width
- i
, inst
->font_height
);
1789 if (lattr
!= LATTR_WIDE
) {
1791 /* Now stretch vertically, in the same way. */
1792 if (lattr
== LATTR_BOT
)
1796 for (i
= 0; i
< inst
->font_height
; i
+=2) {
1797 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1798 x
*inst
->font_width
+inst
->cfg
.window_border
,
1799 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*i
+db
,
1800 x
*widefactor
*inst
->font_width
+inst
->cfg
.window_border
,
1801 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*(i
+1),
1802 len
* inst
->font_width
, inst
->font_height
-i
-1);
1808 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
1809 unsigned long attr
, int lattr
)
1811 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1812 struct gui_data
*inst
= dctx
->inst
;
1813 GdkGC
*gc
= dctx
->gc
;
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 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1834 x
*inst
->font_width
+inst
->cfg
.window_border
,
1835 y
*inst
->font_height
+inst
->cfg
.window_border
,
1836 x
*inst
->font_width
+inst
->cfg
.window_border
,
1837 y
*inst
->font_height
+inst
->cfg
.window_border
,
1838 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1841 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1842 unsigned long attr
, int lattr
)
1844 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1845 struct gui_data
*inst
= dctx
->inst
;
1846 GdkGC
*gc
= dctx
->gc
;
1848 int passive
, widefactor
;
1850 if (attr
& TATTR_PASCURS
) {
1851 attr
&= ~TATTR_PASCURS
;
1855 if ((attr
& TATTR_ACTCURS
) && inst
->cfg
.cursor_type
!= 0) {
1856 attr
&= ~TATTR_ACTCURS
;
1858 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1860 if (attr
& ATTR_WIDE
) {
1866 if (lattr
!= LATTR_NORM
) {
1868 if (x
>= inst
->term
->cols
)
1870 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1871 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1875 if (inst
->cfg
.cursor_type
== 0) {
1877 * An active block cursor will already have been done by
1878 * the above do_text call, so we only need to do anything
1882 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1883 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
1884 x
*inst
->font_width
+inst
->cfg
.window_border
,
1885 y
*inst
->font_height
+inst
->cfg
.window_border
,
1886 len
*inst
->font_width
-1, inst
->font_height
-1);
1890 int startx
, starty
, dx
, dy
, length
, i
;
1894 if ((attr
& ATTR_WIDE
) || lattr
!= LATTR_NORM
)
1895 char_width
= 2*inst
->font_width
;
1897 char_width
= inst
->font_width
;
1899 if (inst
->cfg
.cursor_type
== 1) {
1900 uheight
= inst
->fonts
[0]->ascent
+ 1;
1901 if (uheight
>= inst
->font_height
)
1902 uheight
= inst
->font_height
- 1;
1904 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
;
1905 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
+ uheight
;
1908 length
= len
* char_width
;
1911 if (attr
& TATTR_RIGHTCURS
)
1912 xadjust
= char_width
- 1;
1913 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
+ xadjust
;
1914 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
;
1917 length
= inst
->font_height
;
1920 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1922 for (i
= 0; i
< length
; i
++) {
1924 gdk_draw_point(inst
->pixmap
, gc
, startx
, starty
);
1930 gdk_draw_line(inst
->pixmap
, gc
, startx
, starty
,
1931 startx
+ (length
-1) * dx
, starty
+ (length
-1) * dy
);
1935 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1936 x
*inst
->font_width
+inst
->cfg
.window_border
,
1937 y
*inst
->font_height
+inst
->cfg
.window_border
,
1938 x
*inst
->font_width
+inst
->cfg
.window_border
,
1939 y
*inst
->font_height
+inst
->cfg
.window_border
,
1940 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1943 GdkCursor
*make_mouse_ptr(struct gui_data
*inst
, int cursor_val
)
1946 * Truly hideous hack: GTK doesn't allow us to set the mouse
1947 * cursor foreground and background colours unless we've _also_
1948 * created our own cursor from bitmaps. Therefore, I need to
1949 * load the `cursor' font and draw glyphs from it on to
1950 * pixmaps, in order to construct my cursors with the fg and bg
1951 * I want. This is a gross hack, but it's more self-contained
1952 * than linking in Xlib to find the X window handle to
1953 * inst->area and calling XRecolorCursor, and it's more
1954 * futureproof than hard-coding the shapes as bitmap arrays.
1956 static GdkFont
*cursor_font
= NULL
;
1957 GdkPixmap
*source
, *mask
;
1959 GdkColor cfg
= { 0, 65535, 65535, 65535 };
1960 GdkColor cbg
= { 0, 0, 0, 0 };
1961 GdkColor dfg
= { 1, 65535, 65535, 65535 };
1962 GdkColor dbg
= { 0, 0, 0, 0 };
1965 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
1967 if (cursor_val
== -2) {
1968 gdk_font_unref(cursor_font
);
1972 if (cursor_val
>= 0 && !cursor_font
)
1973 cursor_font
= gdk_font_load("cursor");
1976 * Get the text extent of the cursor in question. We use the
1977 * mask character for this, because it's typically slightly
1978 * bigger than the main character.
1980 if (cursor_val
>= 0) {
1982 text
[0] = (char)cursor_val
+ 1;
1983 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
1984 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
1990 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
1991 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
1994 * Draw the mask character on the mask pixmap.
1996 gc
= gdk_gc_new(mask
);
1997 gdk_gc_set_foreground(gc
, &dbg
);
1998 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
1999 if (cursor_val
>= 0) {
2001 text
[0] = (char)cursor_val
+ 1;
2002 gdk_gc_set_foreground(gc
, &dfg
);
2003 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
2008 * Draw the main character on the source pixmap.
2010 gc
= gdk_gc_new(source
);
2011 gdk_gc_set_foreground(gc
, &dbg
);
2012 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
2013 if (cursor_val
>= 0) {
2015 text
[0] = (char)cursor_val
;
2016 gdk_gc_set_foreground(gc
, &dfg
);
2017 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
2022 * Create the cursor.
2024 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
2029 gdk_pixmap_unref(source
);
2030 gdk_pixmap_unref(mask
);
2035 void modalfatalbox(char *p
, ...)
2038 fprintf(stderr
, "FATAL ERROR: ");
2040 vfprintf(stderr
, p
, ap
);
2042 fputc('\n', stderr
);
2046 void cmdline_error(char *p
, ...)
2049 fprintf(stderr
, "%s: ", appname
);
2051 vfprintf(stderr
, p
, ap
);
2053 fputc('\n', stderr
);
2057 char *get_x_display(void *frontend
)
2059 return gdk_get_display();
2062 long get_windowid(void *frontend
)
2064 struct gui_data
*inst
= (struct gui_data
*)frontend
;
2065 return (long)GDK_WINDOW_XWINDOW(inst
->area
->window
);
2068 static void help(FILE *fp
) {
2070 "pterm option summary:\n"
2072 " --display DISPLAY Specify X display to use (note '--')\n"
2073 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2074 " -fn FONT Normal text font\n"
2075 " -fb FONT Bold text font\n"
2076 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2077 " -sl LINES Number of lines of scrollback\n"
2078 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2079 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2080 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2081 " -T TITLE Window title\n"
2082 " -ut, +ut Do(default) or do not update utmp\n"
2083 " -ls, +ls Do(default) or do not make shell a login shell\n"
2084 " -sb, +sb Do(default) or do not display a scrollbar\n"
2085 " -log PATH Log all output to a file\n"
2086 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2087 " -xrm RESOURCE-STRING Set an X resource\n"
2088 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2089 ) < 0 || fflush(fp
) < 0) {
2090 perror("output error");
2095 int do_cmdline(int argc
, char **argv
, int do_everything
,
2096 struct gui_data
*inst
, Config
*cfg
)
2102 * Macros to make argument handling easier. Note that because
2103 * they need to call `continue', they cannot be contained in
2104 * the usual do {...} while (0) wrapper to make them
2105 * syntactically single statements; hence it is not legal to
2106 * use one of these macros as an unbraced statement between
2109 #define EXPECTS_ARG { \
2110 if (--argc <= 0) { \
2112 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2117 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2119 while (--argc
> 0) {
2124 * Shameless cheating. Debian requires all X terminal
2125 * emulators to support `-T title'; but
2126 * cmdline_process_param will eat -T (it means no-pty) and
2127 * complain that pterm doesn't support it. So, in pterm
2128 * only, we convert -T into -title.
2130 if ((cmdline_tooltype
& TOOLTYPE_NONNETWORK
) &&
2134 ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
2135 do_everything ?
1 : -1, cfg
);
2138 cmdline_error("option \"%s\" requires an argument", p
);
2139 } else if (ret
== 2) {
2140 --argc
, ++argv
; /* skip next argument */
2142 } else if (ret
== 1) {
2146 if (!strcmp(p
, "-fn") || !strcmp(p
, "-font")) {
2149 strncpy(cfg
->font
.name
, val
, sizeof(cfg
->font
.name
));
2150 cfg
->font
.name
[sizeof(cfg
->font
.name
)-1] = '\0';
2152 } else if (!strcmp(p
, "-fb")) {
2155 strncpy(cfg
->boldfont
.name
, val
, sizeof(cfg
->boldfont
.name
));
2156 cfg
->boldfont
.name
[sizeof(cfg
->boldfont
.name
)-1] = '\0';
2158 } else if (!strcmp(p
, "-fw")) {
2161 strncpy(cfg
->widefont
.name
, val
, sizeof(cfg
->widefont
.name
));
2162 cfg
->widefont
.name
[sizeof(cfg
->widefont
.name
)-1] = '\0';
2164 } else if (!strcmp(p
, "-fwb")) {
2167 strncpy(cfg
->wideboldfont
.name
, val
, sizeof(cfg
->wideboldfont
.name
));
2168 cfg
->wideboldfont
.name
[sizeof(cfg
->wideboldfont
.name
)-1] = '\0';
2170 } else if (!strcmp(p
, "-cs")) {
2173 strncpy(cfg
->line_codepage
, val
, sizeof(cfg
->line_codepage
));
2174 cfg
->line_codepage
[sizeof(cfg
->line_codepage
)-1] = '\0';
2176 } else if (!strcmp(p
, "-geometry")) {
2177 int flags
, x
, y
, w
, h
;
2181 flags
= XParseGeometry(val
, &x
, &y
, &w
, &h
);
2182 if (flags
& WidthValue
)
2184 if (flags
& HeightValue
)
2187 if (flags
& (XValue
| YValue
)) {
2190 inst
->gotpos
= TRUE
;
2191 inst
->gravity
= ((flags
& XNegative ?
1 : 0) |
2192 (flags
& YNegative ?
2 : 0));
2195 } else if (!strcmp(p
, "-sl")) {
2198 cfg
->savelines
= atoi(val
);
2200 } else if (!strcmp(p
, "-fg") || !strcmp(p
, "-bg") ||
2201 !strcmp(p
, "-bfg") || !strcmp(p
, "-bbg") ||
2202 !strcmp(p
, "-cfg") || !strcmp(p
, "-cbg")) {
2207 if (!gdk_color_parse(val
, &col
)) {
2209 fprintf(stderr
, "%s: unable to parse colour \"%s\"\n",
2213 index
= (!strcmp(p
, "-fg") ?
0 :
2214 !strcmp(p
, "-bg") ?
2 :
2215 !strcmp(p
, "-bfg") ?
1 :
2216 !strcmp(p
, "-bbg") ?
3 :
2217 !strcmp(p
, "-cfg") ?
4 :
2218 !strcmp(p
, "-cbg") ?
5 : -1);
2219 assert(index
!= -1);
2220 cfg
->colours
[index
][0] = col
.red
/ 256;
2221 cfg
->colours
[index
][1] = col
.green
/ 256;
2222 cfg
->colours
[index
][2] = col
.blue
/ 256;
2225 } else if (use_pty_argv
&& !strcmp(p
, "-e")) {
2226 /* This option swallows all further arguments. */
2232 pty_argv
= snewn(argc
+1, char *);
2234 for (i
= 0; i
< argc
; i
++)
2235 pty_argv
[i
] = argv
[i
];
2236 pty_argv
[argc
] = NULL
;
2237 break; /* finished command-line processing */
2239 err
= 1, fprintf(stderr
, "%s: -e expects an argument\n",
2242 } else if (!strcmp(p
, "-title")) {
2245 strncpy(cfg
->wintitle
, val
, sizeof(cfg
->wintitle
));
2246 cfg
->wintitle
[sizeof(cfg
->wintitle
)-1] = '\0';
2248 } else if (!strcmp(p
, "-log")) {
2251 strncpy(cfg
->logfilename
.path
, val
, sizeof(cfg
->logfilename
.path
));
2252 cfg
->logfilename
.path
[sizeof(cfg
->logfilename
.path
)-1] = '\0';
2253 cfg
->logtype
= LGTYP_DEBUG
;
2255 } else if (!strcmp(p
, "-ut-") || !strcmp(p
, "+ut")) {
2257 cfg
->stamp_utmp
= 0;
2259 } else if (!strcmp(p
, "-ut")) {
2261 cfg
->stamp_utmp
= 1;
2263 } else if (!strcmp(p
, "-ls-") || !strcmp(p
, "+ls")) {
2265 cfg
->login_shell
= 0;
2267 } else if (!strcmp(p
, "-ls")) {
2269 cfg
->login_shell
= 1;
2271 } else if (!strcmp(p
, "-nethack")) {
2273 cfg
->nethack_keypad
= 1;
2275 } else if (!strcmp(p
, "-sb-") || !strcmp(p
, "+sb")) {
2279 } else if (!strcmp(p
, "-sb")) {
2283 } else if (!strcmp(p
, "-name")) {
2287 } else if (!strcmp(p
, "-xrm")) {
2289 provide_xrm_string(val
);
2291 } else if(!strcmp(p
, "-help") || !strcmp(p
, "--help")) {
2295 } else if(p
[0] != '-' && (!do_everything
||
2296 process_nonoption_arg(p
, cfg
))) {
2301 fprintf(stderr
, "%s: unrecognized option '%s'\n", appname
, p
);
2308 static void block_signal(int sig
, int block_it
) {
2312 sigaddset(&ss
, sig
);
2313 if(sigprocmask(block_it ? SIG_BLOCK
: SIG_UNBLOCK
, &ss
, 0) < 0) {
2314 perror("sigprocmask");
2320 * This function retrieves the character set encoding of a font. It
2321 * returns the character set without the X11 hack (in case the user
2322 * asks to use the font's own encoding).
2324 static int set_font_info(struct gui_data
*inst
, int fontid
)
2326 GdkFont
*font
= inst
->fonts
[fontid
];
2327 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2328 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2329 Atom charset_registry
, charset_encoding
;
2330 unsigned long registry_ret
, encoding_ret
;
2331 int retval
= CS_NONE
;
2333 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
2334 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
2335 inst
->fontinfo
[fontid
].charset
= CS_NONE
;
2336 inst
->fontinfo
[fontid
].is_wide
= 0;
2337 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
2338 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
2340 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
2341 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
2343 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
2344 retval
= inst
->fontinfo
[fontid
].charset
=
2345 charset_from_xenc(encoding
);
2346 /* FIXME: when libcharset supports wide encodings fix this. */
2347 if (!strcasecmp(encoding
, "iso10646-1")) {
2348 inst
->fontinfo
[fontid
].is_wide
= 1;
2353 * Hack for X line-drawing characters: if the primary
2354 * font is encoded as ISO-8859-anything, and has valid
2355 * glyphs in the first 32 char positions, it is assumed
2356 * that those glyphs are the VT100 line-drawing
2359 * Actually, we'll hack even harder by only checking
2360 * position 0x19 (vertical line, VT100 linedrawing
2361 * `x'). Then we can check it easily by seeing if the
2362 * ascent and descent differ.
2364 if (inst
->fontinfo
[fontid
].charset
== CS_ISO8859_1
) {
2365 int lb
, rb
, wid
, asc
, desc
;
2370 gdk_string_extents(inst
->fonts
[fontid
], text
,
2371 &lb
, &rb
, &wid
, &asc
, &desc
);
2373 inst
->fontinfo
[fontid
].charset
= CS_ISO8859_1_X11
;
2383 int uxsel_input_add(int fd
, int rwx
) {
2385 if (rwx
& 1) flags
|= GDK_INPUT_READ
;
2386 if (rwx
& 2) flags
|= GDK_INPUT_WRITE
;
2387 if (rwx
& 4) flags
|= GDK_INPUT_EXCEPTION
;
2388 return gdk_input_add(fd
, flags
, fd_input_func
, NULL
);
2391 void uxsel_input_remove(int id
) {
2392 gdk_input_remove(id
);
2395 char *guess_derived_font_name(GdkFont
*font
, int bold
, int wide
)
2397 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2398 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2399 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
2401 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
2402 char *name
= XGetAtomName(disp
, (Atom
)ret
);
2403 if (name
&& name
[0] == '-') {
2405 char *dupname
, *extrafree
= NULL
, *ret
;
2409 p
= q
= dupname
= dupstr(name
); /* skip initial minus */
2412 while (*p
&& nstr
< lenof(strings
)) {
2415 strings
[nstr
++] = p
+1;
2420 if (nstr
< lenof(strings
))
2421 return NULL
; /* XLFD was malformed */
2424 strings
[2] = "bold";
2427 /* 4 is `wideness', which obviously may have changed. */
2428 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2429 strings
[4] = strings
[5] = "*";
2430 strings
[11] = extrafree
= dupprintf("%d", 2*atoi(strings
[11]));
2433 ret
= dupcat("-", strings
[ 0], "-", strings
[ 1], "-", strings
[ 2],
2434 "-", strings
[ 3], "-", strings
[ 4], "-", strings
[ 5],
2435 "-", strings
[ 6], "-", strings
[ 7], "-", strings
[ 8],
2436 "-", strings
[ 9], "-", strings
[10], "-", strings
[11],
2437 "-", strings
[12], NULL
);
2447 void setup_fonts_ucs(struct gui_data
*inst
)
2454 gdk_font_unref(inst
->fonts
[0]);
2456 gdk_font_unref(inst
->fonts
[1]);
2458 gdk_font_unref(inst
->fonts
[2]);
2460 gdk_font_unref(inst
->fonts
[3]);
2462 inst
->fonts
[0] = gdk_font_load(inst
->cfg
.font
.name
);
2463 if (!inst
->fonts
[0]) {
2464 fprintf(stderr
, "%s: unable to load font \"%s\"\n", appname
,
2465 inst
->cfg
.font
.name
);
2468 font_charset
= set_font_info(inst
, 0);
2470 if (inst
->cfg
.boldfont
.name
[0]) {
2471 name
= inst
->cfg
.boldfont
.name
;
2474 name
= guess_derived_font_name(inst
->fonts
[0], TRUE
, FALSE
);
2477 inst
->fonts
[1] = name ?
gdk_font_load(name
) : NULL
;
2478 if (inst
->fonts
[1]) {
2479 set_font_info(inst
, 1);
2480 } else if (!guessed
) {
2481 fprintf(stderr
, "%s: unable to load bold font \"%s\"\n", appname
,
2482 inst
->cfg
.boldfont
.name
);
2488 if (inst
->cfg
.widefont
.name
[0]) {
2489 name
= inst
->cfg
.widefont
.name
;
2492 name
= guess_derived_font_name(inst
->fonts
[0], FALSE
, TRUE
);
2495 inst
->fonts
[2] = name ?
gdk_font_load(name
) : NULL
;
2496 if (inst
->fonts
[2]) {
2497 set_font_info(inst
, 2);
2498 } else if (!guessed
) {
2499 fprintf(stderr
, "%s: unable to load wide font \"%s\"\n", appname
,
2500 inst
->cfg
.widefont
.name
);
2506 if (inst
->cfg
.wideboldfont
.name
[0]) {
2507 name
= inst
->cfg
.wideboldfont
.name
;
2511 * Here we have some choices. We can widen the bold font,
2512 * bolden the wide font, or widen and bolden the standard
2513 * font. Try them all, in that order!
2515 if (inst
->cfg
.widefont
.name
[0])
2516 name
= guess_derived_font_name(inst
->fonts
[2], TRUE
, FALSE
);
2517 else if (inst
->cfg
.boldfont
.name
[0])
2518 name
= guess_derived_font_name(inst
->fonts
[1], FALSE
, TRUE
);
2520 name
= guess_derived_font_name(inst
->fonts
[0], TRUE
, TRUE
);
2523 inst
->fonts
[3] = name ?
gdk_font_load(name
) : NULL
;
2524 if (inst
->fonts
[3]) {
2525 set_font_info(inst
, 3);
2526 } else if (!guessed
) {
2527 fprintf(stderr
, "%s: unable to load wide/bold font \"%s\"\n", appname
,
2528 inst
->cfg
.wideboldfont
.name
);
2534 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
2535 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
2537 inst
->direct_to_font
= init_ucs(&inst
->ucsdata
,
2538 inst
->cfg
.line_codepage
, font_charset
,
2542 void set_geom_hints(struct gui_data
*inst
)
2545 geom
.min_width
= inst
->font_width
+ 2*inst
->cfg
.window_border
;
2546 geom
.min_height
= inst
->font_height
+ 2*inst
->cfg
.window_border
;
2547 geom
.max_width
= geom
.max_height
= -1;
2548 geom
.base_width
= 2*inst
->cfg
.window_border
;
2549 geom
.base_height
= 2*inst
->cfg
.window_border
;
2550 geom
.width_inc
= inst
->font_width
;
2551 geom
.height_inc
= inst
->font_height
;
2552 geom
.min_aspect
= geom
.max_aspect
= 0;
2553 gtk_window_set_geometry_hints(GTK_WINDOW(inst
->window
), inst
->area
, &geom
,
2554 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
2555 GDK_HINT_RESIZE_INC
);
2558 void clear_scrollback_menuitem(GtkMenuItem
*item
, gpointer data
)
2560 struct gui_data
*inst
= (struct gui_data
*)data
;
2561 term_clrsb(inst
->term
);
2564 void reset_terminal_menuitem(GtkMenuItem
*item
, gpointer data
)
2566 struct gui_data
*inst
= (struct gui_data
*)data
;
2567 term_pwron(inst
->term
);
2568 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2571 void copy_all_menuitem(GtkMenuItem
*item
, gpointer data
)
2573 struct gui_data
*inst
= (struct gui_data
*)data
;
2574 term_copyall(inst
->term
);
2577 void special_menuitem(GtkMenuItem
*item
, gpointer data
)
2579 struct gui_data
*inst
= (struct gui_data
*)data
;
2580 int code
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
2583 inst
->back
->special(inst
->backhandle
, code
);
2586 void about_menuitem(GtkMenuItem
*item
, gpointer data
)
2588 struct gui_data
*inst
= (struct gui_data
*)data
;
2589 about_box(inst
->window
);
2592 void event_log_menuitem(GtkMenuItem
*item
, gpointer data
)
2594 struct gui_data
*inst
= (struct gui_data
*)data
;
2595 showeventlog(inst
->eventlogstuff
, inst
->window
);
2598 void change_settings_menuitem(GtkMenuItem
*item
, gpointer data
)
2600 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2601 static const int ww
[] = {
2602 6, 7, 8, 9, 10, 11, 12, 13,
2603 14, 15, 16, 17, 18, 19, 20, 21,
2606 struct gui_data
*inst
= (struct gui_data
*)data
;
2607 char *title
= dupcat(appname
, " Reconfiguration", NULL
);
2608 Config cfg2
, oldcfg
;
2611 cfg2
= inst
->cfg
; /* structure copy */
2613 if (do_config_box(title
, &cfg2
, 1)) {
2615 oldcfg
= inst
->cfg
; /* structure copy */
2616 inst
->cfg
= cfg2
; /* structure copy */
2618 /* Pass new config data to the logging module */
2619 log_reconfig(inst
->logctx
, &cfg2
);
2621 * Flush the line discipline's edit buffer in the case
2622 * where local editing has just been disabled.
2624 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2625 /* Pass new config data to the terminal */
2626 term_reconfig(inst
->term
, &cfg2
);
2627 /* Pass new config data to the back end */
2628 inst
->back
->reconfig(inst
->backhandle
, &cfg2
);
2631 * Just setting inst->cfg is sufficient to cause colour
2632 * setting changes to appear on the next ESC]R palette
2633 * reset. But we should also check whether any colour
2634 * settings have been changed, and revert the ones that
2635 * have to the new default, on the assumption that the user
2636 * is most likely to want an immediate update.
2638 for (i
= 0; i
< NCOLOURS
; i
++) {
2639 if (oldcfg
.colours
[ww
[i
]][0] != cfg2
.colours
[ww
[i
]][0] ||
2640 oldcfg
.colours
[ww
[i
]][1] != cfg2
.colours
[ww
[i
]][1] ||
2641 oldcfg
.colours
[ww
[i
]][2] != cfg2
.colours
[ww
[i
]][2]) {
2642 real_palette_set(inst
, i
, cfg2
.colours
[ww
[i
]][0],
2643 cfg2
.colours
[ww
[i
]][1],
2644 cfg2
.colours
[ww
[i
]][2]);
2647 * If the default background has changed, we must
2648 * repaint the space in between the window border
2649 * and the text area.
2652 set_window_background(inst
);
2653 draw_backing_rect(inst
);
2659 * If the scrollbar needs to be shown, hidden, or moved
2660 * from one end to the other of the window, do so now.
2662 if (oldcfg
.scrollbar
!= cfg2
.scrollbar
) {
2664 gtk_widget_show(inst
->sbar
);
2666 gtk_widget_hide(inst
->sbar
);
2668 if (oldcfg
.scrollbar_on_left
!= cfg2
.scrollbar_on_left
) {
2669 gtk_box_reorder_child(inst
->hbox
, inst
->sbar
,
2670 cfg2
.scrollbar_on_left ?
0 : 1);
2674 * Change the window title, if required.
2676 if (strcmp(oldcfg
.wintitle
, cfg2
.wintitle
))
2677 set_title(inst
, cfg2
.wintitle
);
2678 set_window_titles(inst
);
2681 * Redo the whole tangled fonts and Unicode mess if
2684 if (strcmp(oldcfg
.font
.name
, cfg2
.font
.name
) ||
2685 strcmp(oldcfg
.boldfont
.name
, cfg2
.boldfont
.name
) ||
2686 strcmp(oldcfg
.widefont
.name
, cfg2
.widefont
.name
) ||
2687 strcmp(oldcfg
.wideboldfont
.name
, cfg2
.wideboldfont
.name
) ||
2688 strcmp(oldcfg
.line_codepage
, cfg2
.line_codepage
) ||
2689 oldcfg
.vtmode
!= cfg2
.vtmode
) {
2690 setup_fonts_ucs(inst
);
2696 * Resize the window.
2698 if (oldcfg
.width
!= cfg2
.width
|| oldcfg
.height
!= cfg2
.height
||
2699 oldcfg
.window_border
!= cfg2
.window_border
|| need_size
) {
2700 set_geom_hints(inst
);
2701 request_resize(inst
, cfg2
.width
, cfg2
.height
);
2704 * The above will have caused a call to term_size() for
2705 * us if it happened. If the user has fiddled with only
2706 * the scrollback size, the above will not have
2707 * happened and we will need an explicit term_size()
2710 if (oldcfg
.savelines
!= cfg2
.savelines
)
2711 term_size(inst
->term
, inst
->term
->rows
, inst
->term
->cols
,
2715 term_invalidate(inst
->term
);
2718 * We do an explicit full redraw here to ensure the window
2719 * border has been redrawn as well as the text area.
2721 gtk_widget_queue_draw(inst
->area
);
2726 void fork_and_exec_self(struct gui_data
*inst
, int fd_to_close
, ...)
2729 * Re-execing ourself is not an exact science under Unix. I do
2730 * the best I can by using /proc/self/exe if available and by
2731 * assuming argv[0] can be found on $PATH if not.
2733 * Note that we also have to reconstruct the elements of the
2734 * original argv which gtk swallowed, since the user wants the
2735 * new session to appear on the same X display as the old one.
2743 * Collect the arguments with which to re-exec ourself.
2745 va_start(ap
, fd_to_close
);
2746 n
= 2; /* progname and terminating NULL */
2747 n
+= inst
->ngtkargs
;
2748 while (va_arg(ap
, char *) != NULL
)
2752 args
= snewn(n
, char *);
2753 args
[0] = inst
->progname
;
2755 for (i
= 0; i
< inst
->ngtkargs
; i
++)
2756 args
[i
+1] = inst
->gtkargvstart
[i
];
2759 va_start(ap
, fd_to_close
);
2760 while ((args
[i
++] = va_arg(ap
, char *)) != NULL
);
2766 * Do the double fork.
2779 } else if (pid2
> 0) {
2781 * First child has successfully forked second child. My
2782 * Work Here Is Done. Note the use of _exit rather than
2783 * exit: the latter appears to cause destroy messages
2784 * to be sent to the X server. I suspect gtk uses
2791 * If we reach here, we are the second child, so we now
2792 * actually perform the exec.
2794 if (fd_to_close
>= 0)
2797 execv("/proc/self/exe", args
);
2798 execvp(inst
->progname
, args
);
2804 waitpid(pid
, &status
, 0);
2809 void dup_session_menuitem(GtkMenuItem
*item
, gpointer gdata
)
2811 struct gui_data
*inst
= (struct gui_data
*)gdata
;
2813 * For this feature we must marshal cfg and (possibly) pty_argv
2814 * into a byte stream, create a pipe, and send this byte stream
2815 * to the child through the pipe.
2822 if (pipe(pipefd
) < 0) {
2827 size
= sizeof(inst
->cfg
);
2828 if (use_pty_argv
&& pty_argv
) {
2829 for (i
= 0; pty_argv
[i
]; i
++)
2830 size
+= strlen(pty_argv
[i
]) + 1;
2833 data
= snewn(size
, char);
2834 memcpy(data
, &inst
->cfg
, sizeof(inst
->cfg
));
2835 if (use_pty_argv
&& pty_argv
) {
2836 int p
= sizeof(inst
->cfg
);
2837 for (i
= 0; pty_argv
[i
]; i
++) {
2838 strcpy(data
+ p
, pty_argv
[i
]);
2839 p
+= strlen(pty_argv
[i
]) + 1;
2844 sprintf(option
, "---[%d,%d]", pipefd
[0], size
);
2845 fcntl(pipefd
[0], F_SETFD
, 0);
2846 fork_and_exec_self(inst
, pipefd
[1], option
, NULL
);
2850 while (i
< size
&& (ret
= write(pipefd
[1], data
+ i
, size
- i
)) > 0)
2853 perror("write to pipe");
2858 int read_dupsession_data(struct gui_data
*inst
, Config
*cfg
, char *arg
)
2860 int fd
, i
, ret
, size
;
2863 if (sscanf(arg
, "---[%d,%d]", &fd
, &size
) != 2) {
2864 fprintf(stderr
, "%s: malformed magic argument `%s'\n", appname
, arg
);
2868 data
= snewn(size
, char);
2870 while (i
< size
&& (ret
= read(fd
, data
+ i
, size
- i
)) > 0)
2873 perror("read from pipe");
2875 } else if (i
< size
) {
2876 fprintf(stderr
, "%s: unexpected EOF in Duplicate Session data\n",
2881 memcpy(cfg
, data
, sizeof(Config
));
2882 if (use_pty_argv
&& size
> sizeof(Config
)) {
2886 while (i
< size
&& data
[i
]) i
++;
2888 fprintf(stderr
, "%s: malformed Duplicate Session data\n",
2895 pty_argv
= snewn(n
+1, char *);
2901 while (i
< size
&& data
[i
]) i
++;
2904 pty_argv
[n
++] = dupstr(p
);
2911 void new_session_menuitem(GtkMenuItem
*item
, gpointer data
)
2913 struct gui_data
*inst
= (struct gui_data
*)data
;
2915 fork_and_exec_self(inst
, -1, NULL
);
2918 void saved_session_menuitem(GtkMenuItem
*item
, gpointer data
)
2920 struct gui_data
*inst
= (struct gui_data
*)data
;
2921 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
2923 fork_and_exec_self(inst
, -1, "-load", str
, NULL
);
2926 void saved_session_freedata(GtkMenuItem
*item
, gpointer data
)
2928 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
2933 void update_specials_menu(void *frontend
)
2935 struct gui_data
*inst
= (struct gui_data
*)frontend
;
2937 const struct telnet_special
*specials
;
2939 specials
= inst
->back
->get_specials(inst
->backhandle
);
2940 gtk_container_foreach(GTK_CONTAINER(inst
->specialsmenu
),
2941 (GtkCallback
)gtk_widget_destroy
, NULL
);
2944 GtkWidget
*menuitem
;
2945 for (i
= 0; specials
[i
].name
; i
++) {
2946 if (*specials
[i
].name
) {
2947 menuitem
= gtk_menu_item_new_with_label(specials
[i
].name
);
2948 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
2949 GINT_TO_POINTER(specials
[i
].code
));
2950 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
2951 GTK_SIGNAL_FUNC(special_menuitem
), inst
);
2953 menuitem
= gtk_menu_item_new();
2954 gtk_container_add(GTK_CONTAINER(inst
->specialsmenu
), menuitem
);
2955 gtk_widget_show(menuitem
);
2957 gtk_widget_show(inst
->specialsitem1
);
2958 gtk_widget_show(inst
->specialsitem2
);
2960 gtk_widget_hide(inst
->specialsitem1
);
2961 gtk_widget_hide(inst
->specialsitem2
);
2965 int pt_main(int argc
, char **argv
)
2967 extern Backend
*select_backend(Config
*cfg
);
2968 extern int cfgbox(Config
*cfg
);
2969 struct gui_data
*inst
;
2972 * Create an instance structure and initialise to zeroes
2974 inst
= snew(struct gui_data
);
2975 memset(inst
, 0, sizeof(*inst
));
2976 inst
->alt_keycode
= -1; /* this one needs _not_ to be zero */
2978 /* defer any child exit handling until we're ready to deal with
2980 block_signal(SIGCHLD
, 1);
2982 inst
->progname
= argv
[0];
2984 * Copy the original argv before letting gtk_init fiddle with
2985 * it. It will be required later.
2989 inst
->gtkargvstart
= snewn(argc
-1, char *);
2990 for (i
= 1; i
< argc
; i
++)
2991 inst
->gtkargvstart
[i
-1] = dupstr(argv
[i
]);
2993 gtk_init(&argc
, &argv
);
2994 inst
->ngtkargs
= oldargc
- argc
;
2997 if (argc
> 1 && !strncmp(argv
[1], "---", 3)) {
2998 read_dupsession_data(inst
, &inst
->cfg
, argv
[1]);
2999 /* Splatter this argument so it doesn't clutter a ps listing */
3000 memset(argv
[1], 0, strlen(argv
[1]));
3002 if (do_cmdline(argc
, argv
, 0, inst
, &inst
->cfg
))
3003 exit(1); /* pre-defaults pass to get -class */
3004 do_defaults(NULL
, &inst
->cfg
);
3005 if (do_cmdline(argc
, argv
, 1, inst
, &inst
->cfg
))
3006 exit(1); /* post-defaults, do everything */
3008 cmdline_run_saved(&inst
->cfg
);
3010 if (!*inst
->cfg
.host
&& !cfgbox(&inst
->cfg
))
3011 exit(0); /* config box hit Cancel */
3014 if (!compound_text_atom
)
3015 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
3016 if (!utf8_string_atom
)
3017 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
3019 setup_fonts_ucs(inst
);
3021 inst
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
3024 * Set up the colour map.
3026 palette_reset(inst
);
3028 inst
->width
= inst
->cfg
.width
;
3029 inst
->height
= inst
->cfg
.height
;
3031 inst
->area
= gtk_drawing_area_new();
3032 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
3033 inst
->font_width
* inst
->cfg
.width
+ 2*inst
->cfg
.window_border
,
3034 inst
->font_height
* inst
->cfg
.height
+ 2*inst
->cfg
.window_border
);
3035 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3036 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
3037 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
3039 * We always create the scrollbar; it remains invisible if
3040 * unwanted, so we can pop it up quickly if it suddenly becomes
3043 if (inst
->cfg
.scrollbar_on_left
)
3044 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
3045 gtk_box_pack_start(inst
->hbox
, inst
->area
, TRUE
, TRUE
, 0);
3046 if (!inst
->cfg
.scrollbar_on_left
)
3047 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
3049 gtk_container_add(GTK_CONTAINER(inst
->window
), GTK_WIDGET(inst
->hbox
));
3051 set_geom_hints(inst
);
3053 gtk_widget_show(inst
->area
);
3054 if (inst
->cfg
.scrollbar
)
3055 gtk_widget_show(inst
->sbar
);
3057 gtk_widget_hide(inst
->sbar
);
3058 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
3061 int x
= inst
->xpos
, y
= inst
->ypos
;
3063 gtk_widget_size_request(GTK_WIDGET(inst
->window
), &req
);
3064 if (inst
->gravity
& 1) x
+= gdk_screen_width() - req
.width
;
3065 if (inst
->gravity
& 2) y
+= gdk_screen_height() - req
.height
;
3066 gtk_window_set_position(GTK_WINDOW(inst
->window
), GTK_WIN_POS_NONE
);
3067 gtk_widget_set_uposition(GTK_WIDGET(inst
->window
), x
, y
);
3070 gtk_signal_connect(GTK_OBJECT(inst
->window
), "destroy",
3071 GTK_SIGNAL_FUNC(destroy
), inst
);
3072 gtk_signal_connect(GTK_OBJECT(inst
->window
), "delete_event",
3073 GTK_SIGNAL_FUNC(delete_window
), inst
);
3074 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_press_event",
3075 GTK_SIGNAL_FUNC(key_event
), inst
);
3076 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_release_event",
3077 GTK_SIGNAL_FUNC(key_event
), inst
);
3078 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_in_event",
3079 GTK_SIGNAL_FUNC(focus_event
), inst
);
3080 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_out_event",
3081 GTK_SIGNAL_FUNC(focus_event
), inst
);
3082 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
3083 GTK_SIGNAL_FUNC(configure_area
), inst
);
3084 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
3085 GTK_SIGNAL_FUNC(expose_area
), inst
);
3086 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
3087 GTK_SIGNAL_FUNC(button_event
), inst
);
3088 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
3089 GTK_SIGNAL_FUNC(button_event
), inst
);
3090 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
3091 GTK_SIGNAL_FUNC(motion_event
), inst
);
3092 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
3093 GTK_SIGNAL_FUNC(selection_received
), inst
);
3094 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
3095 GTK_SIGNAL_FUNC(selection_get
), inst
);
3096 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
3097 GTK_SIGNAL_FUNC(selection_clear
), inst
);
3098 if (inst
->cfg
.scrollbar
)
3099 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
3100 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
3101 gtk_timeout_add(20, timer_func
, inst
);
3102 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
3103 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
3104 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
3105 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_MOTION_MASK
);
3107 gtk_widget_show(inst
->window
);
3109 set_window_background(inst
);
3112 * Set up the Ctrl+rightclick context menu.
3115 GtkWidget
*menuitem
;
3117 extern const int use_event_log
, new_session
, saved_sessions
;
3119 inst
->menu
= gtk_menu_new();
3121 #define MKMENUITEM(title, func) do { \
3122 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3123 gtk_menu_item_new(); \
3124 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3125 gtk_widget_show(menuitem); \
3127 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3128 GTK_SIGNAL_FUNC(func), inst); \
3131 MKMENUITEM("New Session", new_session_menuitem
);
3132 MKMENUITEM("Duplicate Session", dup_session_menuitem
);
3133 if (saved_sessions
) {
3134 struct sesslist sesslist
;
3137 inst
->sessionsmenu
= gtk_menu_new();
3139 get_sesslist(&sesslist
, TRUE
);
3140 for (i
= 1; i
< sesslist
.nsessions
; i
++) {
3141 menuitem
= gtk_menu_item_new_with_label(sesslist
.sessions
[i
]);
3142 gtk_container_add(GTK_CONTAINER(inst
->sessionsmenu
), menuitem
);
3143 gtk_widget_show(menuitem
);
3144 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
3145 dupstr(sesslist
.sessions
[i
]));
3146 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
3147 GTK_SIGNAL_FUNC(saved_session_menuitem
),
3149 gtk_signal_connect(GTK_OBJECT(menuitem
), "destroy",
3150 GTK_SIGNAL_FUNC(saved_session_freedata
),
3153 get_sesslist(&sesslist
, FALSE
);
3155 MKMENUITEM("Saved Sessions", NULL
);
3156 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
),
3157 inst
->sessionsmenu
);
3159 MKMENUITEM(NULL
, NULL
);
3160 MKMENUITEM("Change Settings", change_settings_menuitem
);
3161 MKMENUITEM(NULL
, NULL
);
3163 MKMENUITEM("Event Log", event_log_menuitem
);
3164 MKMENUITEM("Special Commands", NULL
);
3165 inst
->specialsmenu
= gtk_menu_new();
3166 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), inst
->specialsmenu
);
3167 inst
->specialsitem1
= menuitem
;
3168 MKMENUITEM(NULL
, NULL
);
3169 inst
->specialsitem2
= menuitem
;
3170 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem
);
3171 MKMENUITEM("Reset Terminal", reset_terminal_menuitem
);
3172 MKMENUITEM("Copy All", copy_all_menuitem
);
3173 MKMENUITEM(NULL
, NULL
);
3174 s
= dupcat("About ", appname
, NULL
);
3175 MKMENUITEM(s
, about_menuitem
);
3180 inst
->textcursor
= make_mouse_ptr(inst
, GDK_XTERM
);
3181 inst
->rawcursor
= make_mouse_ptr(inst
, GDK_LEFT_PTR
);
3182 inst
->blankcursor
= make_mouse_ptr(inst
, -1);
3183 make_mouse_ptr(inst
, -2); /* clean up cursor font */
3184 inst
->currcursor
= inst
->textcursor
;
3185 show_mouseptr(inst
, 1);
3187 inst
->eventlogstuff
= eventlogstuff_new();
3189 inst
->term
= term_init(&inst
->cfg
, &inst
->ucsdata
, inst
);
3190 inst
->logctx
= log_init(inst
, &inst
->cfg
);
3191 term_provide_logctx(inst
->term
, inst
->logctx
);
3195 term_size(inst
->term
, inst
->cfg
.height
, inst
->cfg
.width
, inst
->cfg
.savelines
);
3197 inst
->back
= select_backend(&inst
->cfg
);
3202 error
= inst
->back
->init((void *)inst
, &inst
->backhandle
,
3203 &inst
->cfg
, inst
->cfg
.host
, inst
->cfg
.port
,
3204 &realhost
, inst
->cfg
.tcp_nodelay
);
3207 char *msg
= dupprintf("Unable to open connection to %s:\n%s",
3208 inst
->cfg
.host
, error
);
3209 inst
->exited
= TRUE
;
3210 fatal_message_box(inst
->window
, msg
);
3215 if (inst
->cfg
.wintitle
[0]) {
3216 set_title(inst
, inst
->cfg
.wintitle
);
3217 set_icon(inst
, inst
->cfg
.wintitle
);
3219 char *title
= make_default_wintitle(realhost
);
3220 set_title(inst
, title
);
3221 set_icon(inst
, title
);
3225 inst
->back
->provide_logctx(inst
->backhandle
, inst
->logctx
);
3226 update_specials_menu(inst
);
3228 term_provide_resize_fn(inst
->term
, inst
->back
->size
, inst
->backhandle
);
3231 ldisc_create(&inst
->cfg
, inst
->term
, inst
->back
, inst
->backhandle
, inst
);
3232 ldisc_send(inst
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
3234 /* now we're reday to deal with the child exit handler being
3236 block_signal(SIGCHLD
, 0);
3239 * Block SIGPIPE: if we attempt Duplicate Session or similar
3240 * and it falls over in some way, we certainly don't want
3241 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3242 * this _after_ (at least pterm) forks off its child process,
3243 * since the child wants SIGPIPE handled in the usual way.
3245 block_signal(SIGPIPE
, 1);
3247 inst
->exited
= FALSE
;