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>
25 #include <X11/Xatom.h>
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
32 #define CAT2(x,y) x ## y
33 #define CAT(x,y) CAT2(x,y)
34 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
36 #define NCOLOURS (lenof(((Config *)0)->colours))
38 GdkAtom compound_text_atom
, utf8_string_atom
;
40 extern char **pty_argv
; /* declared in pty.c */
41 extern int use_pty_argv
;
44 GtkWidget
*window
, *area
, *sbar
;
46 GtkAdjustment
*sbar_adjust
;
47 GtkWidget
*menu
, *specialsmenu
, *specialsitem1
, *specialsitem2
;
48 GtkWidget
*sessionsmenu
;
50 GdkFont
*fonts
[4]; /* normal, bold, wide, widebold */
55 int xpos
, ypos
, gotpos
, gravity
;
56 GdkCursor
*rawcursor
, *textcursor
, *blankcursor
, *currcursor
;
57 GdkColor cols
[NCOLOURS
];
59 wchar_t *pastein_data
;
62 char *pasteout_data
, *pasteout_data_ctext
, *pasteout_data_utf8
;
63 int pasteout_data_len
, pasteout_data_ctext_len
, pasteout_data_utf8_len
;
64 int font_width
, font_height
;
68 guint term_paste_idle_id
;
71 char wintitle
[sizeof(((Config
*)0)->wintitle
)];
72 char icontitle
[sizeof(((Config
*)0)->wintitle
)];
73 int master_fd
, master_func_id
;
80 struct unicode_data ucsdata
;
83 char *progname
, **gtkargvstart
;
85 guint32 input_event_time
; /* Timestamp of the most recent input event. */
90 struct gui_data
*inst
;
93 static int send_raw_mouse
;
95 static char *app_name
= "pterm";
97 char *x_get_default(const char *key
)
99 return XGetDefault(GDK_DISPLAY(), app_name
, key
);
102 void connection_fatal(void *frontend
, char *p
, ...)
104 struct gui_data
*inst
= (struct gui_data
*)frontend
;
109 msg
= dupvprintf(p
, ap
);
112 fatal_message_box(inst
->window
, msg
);
114 if (inst
->cfg
.close_on_exit
== FORCE_ON
)
119 * Default settings that are specific to pterm.
121 FontSpec
platform_default_fontspec(const char *name
)
124 if (!strcmp(name
, "Font"))
125 strcpy(ret
.name
, "fixed");
131 Filename
platform_default_filename(const char *name
)
134 if (!strcmp(name
, "LogFileName"))
135 strcpy(ret
.path
, "putty.log");
141 char *platform_default_s(const char *name
)
146 int platform_default_i(const char *name
, int def
)
148 if (!strcmp(name
, "CloseOnExit"))
149 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
150 if (!strcmp(name
, "WinNameAlways"))
151 return 0; /* X natively supports icon titles, so use 'em by default */
155 void ldisc_update(void *frontend
, int echo
, int edit
)
158 * This is a stub in pterm. If I ever produce a Unix
159 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
160 * then it will require some termios manoeuvring analogous to
161 * that in the Windows plink.c, but here it's meaningless.
165 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
167 struct gui_data
*inst
= (struct gui_data
*)frontend
;
168 return term_data(inst
->term
, is_stderr
, data
, len
);
171 void logevent(void *frontend
, const char *string
)
173 struct gui_data
*inst
= (struct gui_data
*)frontend
;
175 log_eventlog(inst
->logctx
, string
);
177 logevent_dlg(inst
->eventlogstuff
, string
);
180 int font_dimension(void *frontend
, int which
)/* 0 for width, 1 for height */
182 struct gui_data
*inst
= (struct gui_data
*)frontend
;
185 return inst
->font_height
;
187 return inst
->font_width
;
191 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
192 * into a cooked one (SELECT, EXTEND, PASTE).
194 * In Unix, this is not configurable; the X button arrangement is
195 * rock-solid across all applications, everyone has a three-button
196 * mouse or a means of faking it, and there is no need to switch
197 * buttons around at all.
199 static Mouse_Button
translate_button(Mouse_Button button
)
201 /* struct gui_data *inst = (struct gui_data *)frontend; */
203 if (button
== MBT_LEFT
)
205 if (button
== MBT_MIDDLE
)
207 if (button
== MBT_RIGHT
)
209 return 0; /* shouldn't happen */
213 * Return the top-level GtkWindow associated with a particular
214 * front end instance.
216 void *get_window(void *frontend
)
218 struct gui_data
*inst
= (struct gui_data
*)frontend
;
223 * Minimise or restore the window in response to a server-side
226 void set_iconic(void *frontend
, int iconic
)
229 * GTK 1.2 doesn't know how to do this.
231 #if GTK_CHECK_VERSION(2,0,0)
232 struct gui_data
*inst
= (struct gui_data
*)frontend
;
234 gtk_window_iconify(GTK_WINDOW(inst
->window
));
236 gtk_window_deiconify(GTK_WINDOW(inst
->window
));
241 * Move the window in response to a server-side request.
243 void move_window(void *frontend
, int x
, int y
)
245 struct gui_data
*inst
= (struct gui_data
*)frontend
;
247 * I assume that when the GTK version of this call is available
248 * we should use it. Not sure how it differs from the GDK one,
251 #if GTK_CHECK_VERSION(2,0,0)
252 gtk_window_move(GTK_WINDOW(inst
->window
), x
, y
);
254 gdk_window_move(inst
->window
->window
, x
, y
);
259 * Move the window to the top or bottom of the z-order in response
260 * to a server-side request.
262 void set_zorder(void *frontend
, int top
)
264 struct gui_data
*inst
= (struct gui_data
*)frontend
;
266 gdk_window_raise(inst
->window
->window
);
268 gdk_window_lower(inst
->window
->window
);
272 * Refresh the window in response to a server-side request.
274 void refresh_window(void *frontend
)
276 struct gui_data
*inst
= (struct gui_data
*)frontend
;
277 term_invalidate(inst
->term
);
281 * Maximise or restore the window in response to a server-side
284 void set_zoomed(void *frontend
, int zoomed
)
287 * GTK 1.2 doesn't know how to do this.
289 #if GTK_CHECK_VERSION(2,0,0)
290 struct gui_data
*inst
= (struct gui_data
*)frontend
;
292 gtk_window_maximize(GTK_WINDOW(inst
->window
));
294 gtk_window_unmaximize(GTK_WINDOW(inst
->window
));
299 * Report whether the window is iconic, for terminal reports.
301 int is_iconic(void *frontend
)
303 struct gui_data
*inst
= (struct gui_data
*)frontend
;
304 return !gdk_window_is_viewable(inst
->window
->window
);
308 * Report the window's position, for terminal reports.
310 void get_window_pos(void *frontend
, int *x
, int *y
)
312 struct gui_data
*inst
= (struct gui_data
*)frontend
;
314 * I assume that when the GTK version of this call is available
315 * we should use it. Not sure how it differs from the GDK one,
318 #if GTK_CHECK_VERSION(2,0,0)
319 gtk_window_get_position(GTK_WINDOW(inst
->window
), x
, y
);
321 gdk_window_get_position(inst
->window
->window
, x
, y
);
326 * Report the window's pixel size, for terminal reports.
328 void get_window_pixels(void *frontend
, int *x
, int *y
)
330 struct gui_data
*inst
= (struct gui_data
*)frontend
;
332 * I assume that when the GTK version of this call is available
333 * we should use it. Not sure how it differs from the GDK one,
336 #if GTK_CHECK_VERSION(2,0,0)
337 gtk_window_get_size(GTK_WINDOW(inst
->window
), x
, y
);
339 gdk_window_get_size(inst
->window
->window
, x
, y
);
344 * Return the window or icon title.
346 char *get_window_title(void *frontend
, int icon
)
348 struct gui_data
*inst
= (struct gui_data
*)frontend
;
349 return icon ? inst
->icontitle
: inst
->wintitle
;
352 gint
delete_window(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
354 struct gui_data
*inst
= (struct gui_data
*)data
;
355 if (!inst
->exited
&& inst
->cfg
.warn_on_close
) {
356 if (!reallyclose(inst
))
362 static void show_mouseptr(struct gui_data
*inst
, int show
)
364 if (!inst
->cfg
.hide_mouseptr
)
367 gdk_window_set_cursor(inst
->area
->window
, inst
->currcursor
);
369 gdk_window_set_cursor(inst
->area
->window
, inst
->blankcursor
);
370 inst
->mouseptr_visible
= show
;
373 void draw_backing_rect(struct gui_data
*inst
)
375 GdkGC
*gc
= gdk_gc_new(inst
->area
->window
);
376 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
377 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
378 inst
->cfg
.width
* inst
->font_width
+ 2*inst
->cfg
.window_border
,
379 inst
->cfg
.height
* inst
->font_height
+ 2*inst
->cfg
.window_border
);
383 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
385 struct gui_data
*inst
= (struct gui_data
*)data
;
386 int w
, h
, need_size
= 0;
389 * See if the terminal size has changed, in which case we must
390 * let the terminal know.
392 w
= (event
->width
- 2*inst
->cfg
.window_border
) / inst
->font_width
;
393 h
= (event
->height
- 2*inst
->cfg
.window_border
) / inst
->font_height
;
394 if (w
!= inst
->width
|| h
!= inst
->height
) {
395 inst
->cfg
.width
= inst
->width
= w
;
396 inst
->cfg
.height
= inst
->height
= h
;
401 gdk_pixmap_unref(inst
->pixmap
);
405 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
406 (inst
->cfg
.width
* inst
->font_width
+
407 2*inst
->cfg
.window_border
),
408 (inst
->cfg
.height
* inst
->font_height
+
409 2*inst
->cfg
.window_border
), -1);
411 draw_backing_rect(inst
);
413 if (need_size
&& inst
->term
) {
414 term_size(inst
->term
, h
, w
, inst
->cfg
.savelines
);
418 term_invalidate(inst
->term
);
423 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
425 struct gui_data
*inst
= (struct gui_data
*)data
;
428 * Pass the exposed rectangle to terminal.c, which will call us
429 * back to do the actual painting.
432 gdk_draw_pixmap(widget
->window
,
433 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
435 event
->area
.x
, event
->area
.y
,
436 event
->area
.x
, event
->area
.y
,
437 event
->area
.width
, event
->area
.height
);
442 #define KEY_PRESSED(k) \
443 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
445 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
447 struct gui_data
*inst
= (struct gui_data
*)data
;
449 wchar_t ucsoutput
[2];
450 int ucsval
, start
, end
, special
, use_ucsoutput
;
452 /* Remember the timestamp. */
453 inst
->input_event_time
= event
->time
;
455 /* By default, nothing is generated. */
457 special
= use_ucsoutput
= FALSE
;
460 * If Alt is being released after typing an Alt+numberpad
461 * sequence, we should generate the code that was typed.
463 * Note that we only do this if more than one key was actually
464 * pressed - I don't think Alt+NumPad4 should be ^D or that
465 * Alt+NumPad3 should be ^C, for example. There's no serious
466 * inconvenience in having to type a zero before a single-digit
469 if (event
->type
== GDK_KEY_RELEASE
&&
470 (event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
471 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
) &&
472 inst
->alt_keycode
>= 0 && inst
->alt_digits
> 1) {
474 printf("Alt key up, keycode = %d\n", inst
->alt_keycode
);
476 output
[0] = inst
->alt_keycode
;
481 if (event
->type
== GDK_KEY_PRESS
) {
485 printf("keypress: keyval = %04x, state = %08x; string =",
486 event
->keyval
, event
->state
);
487 for (i
= 0; event
->string
[i
]; i
++)
488 printf(" %02x", (unsigned char) event
->string
[i
]);
494 * NYI: Compose key (!!! requires Unicode faff before even trying)
498 * If Alt has just been pressed, we start potentially
499 * accumulating an Alt+numberpad code. We do this by
500 * setting alt_keycode to -1 (nothing yet but plausible).
502 if ((event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
503 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
)) {
504 inst
->alt_keycode
= -1;
505 inst
->alt_digits
= 0;
506 goto done
; /* this generates nothing else */
510 * If we're seeing a numberpad key press with Mod1 down,
511 * consider adding it to alt_keycode if that's sensible.
512 * Anything _else_ with Mod1 down cancels any possibility
513 * of an ALT keycode: we set alt_keycode to -2.
515 if ((event
->state
& GDK_MOD1_MASK
) && inst
->alt_keycode
!= -2) {
517 switch (event
->keyval
) {
518 case GDK_KP_0
: case GDK_KP_Insert
: digit
= 0; break;
519 case GDK_KP_1
: case GDK_KP_End
: digit
= 1; break;
520 case GDK_KP_2
: case GDK_KP_Down
: digit
= 2; break;
521 case GDK_KP_3
: case GDK_KP_Page_Down
: digit
= 3; break;
522 case GDK_KP_4
: case GDK_KP_Left
: digit
= 4; break;
523 case GDK_KP_5
: case GDK_KP_Begin
: digit
= 5; break;
524 case GDK_KP_6
: case GDK_KP_Right
: digit
= 6; break;
525 case GDK_KP_7
: case GDK_KP_Home
: digit
= 7; break;
526 case GDK_KP_8
: case GDK_KP_Up
: digit
= 8; break;
527 case GDK_KP_9
: case GDK_KP_Page_Up
: digit
= 9; break;
530 inst
->alt_keycode
= -2; /* it's invalid */
533 printf("Adding digit %d to keycode %d", digit
,
536 if (inst
->alt_keycode
== -1)
537 inst
->alt_keycode
= digit
; /* one-digit code */
539 inst
->alt_keycode
= inst
->alt_keycode
* 10 + digit
;
542 printf(" gives new code %d\n", inst
->alt_keycode
);
544 /* Having used this digit, we now do nothing more with it. */
550 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
553 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
554 term_scroll(inst
->term
, 0, -inst
->cfg
.height
/2);
557 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_CONTROL_MASK
)) {
558 term_scroll(inst
->term
, 0, -1);
561 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
562 term_scroll(inst
->term
, 0, +inst
->cfg
.height
/2);
565 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_CONTROL_MASK
)) {
566 term_scroll(inst
->term
, 0, +1);
571 * Neither does Shift-Ins.
573 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
579 use_ucsoutput
= FALSE
;
581 /* ALT+things gives leading Escape. */
583 strncpy(output
+1, event
->string
, 31);
584 if (!*event
->string
&&
585 (ucsval
= keysym_to_unicode(event
->keyval
)) >= 0) {
586 ucsoutput
[0] = '\033';
587 ucsoutput
[1] = ucsval
;
588 use_ucsoutput
= TRUE
;
592 end
= strlen(output
);
594 if (event
->state
& GDK_MOD1_MASK
) {
596 if (end
== 1) end
= 0;
600 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
601 if (!event
->string
[0] && event
->keyval
== '`' &&
602 (event
->state
& GDK_CONTROL_MASK
)) {
604 use_ucsoutput
= FALSE
;
608 /* Control-Break is the same as Control-C */
609 if (event
->keyval
== GDK_Break
&&
610 (event
->state
& GDK_CONTROL_MASK
)) {
612 use_ucsoutput
= FALSE
;
617 /* We handle Return ourselves, because it needs to be flagged as
618 * special to ldisc. */
619 if (event
->keyval
== GDK_Return
) {
621 use_ucsoutput
= FALSE
;
626 /* Control-2, Control-Space and Control-@ are NUL */
627 if (!event
->string
[0] &&
628 (event
->keyval
== ' ' || event
->keyval
== '2' ||
629 event
->keyval
== '@') &&
630 (event
->state
& (GDK_SHIFT_MASK
|
631 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
633 use_ucsoutput
= FALSE
;
637 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
638 if (!event
->string
[0] && event
->keyval
== ' ' &&
639 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
640 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
642 use_ucsoutput
= FALSE
;
646 /* We don't let GTK tell us what Backspace is! We know better. */
647 if (event
->keyval
== GDK_BackSpace
&&
648 !(event
->state
& GDK_SHIFT_MASK
)) {
649 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x7F' : '\x08';
650 use_ucsoutput
= FALSE
;
654 /* For Shift Backspace, do opposite of what is configured. */
655 if (event
->keyval
== GDK_BackSpace
&&
656 (event
->state
& GDK_SHIFT_MASK
)) {
657 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x08' : '\x7F';
658 use_ucsoutput
= FALSE
;
663 /* Shift-Tab is ESC [ Z */
664 if (event
->keyval
== GDK_ISO_Left_Tab
||
665 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
666 end
= 1 + sprintf(output
+1, "\033[Z");
667 use_ucsoutput
= FALSE
;
671 * NetHack keypad mode.
673 if (inst
->cfg
.nethack_keypad
) {
675 switch (event
->keyval
) {
676 case GDK_KP_1
: case GDK_KP_End
: keys
= "bB"; break;
677 case GDK_KP_2
: case GDK_KP_Down
: keys
= "jJ"; break;
678 case GDK_KP_3
: case GDK_KP_Page_Down
: keys
= "nN"; break;
679 case GDK_KP_4
: case GDK_KP_Left
: keys
= "hH"; break;
680 case GDK_KP_5
: case GDK_KP_Begin
: keys
= ".."; break;
681 case GDK_KP_6
: case GDK_KP_Right
: keys
= "lL"; break;
682 case GDK_KP_7
: case GDK_KP_Home
: keys
= "yY"; break;
683 case GDK_KP_8
: case GDK_KP_Up
: keys
= "kK"; break;
684 case GDK_KP_9
: case GDK_KP_Page_Up
: keys
= "uU"; break;
688 if (event
->state
& GDK_SHIFT_MASK
)
692 use_ucsoutput
= FALSE
;
698 * Application keypad mode.
700 if (inst
->term
->app_keypad_keys
&& !inst
->cfg
.no_applic_k
) {
702 switch (event
->keyval
) {
703 case GDK_Num_Lock
: xkey
= 'P'; break;
704 case GDK_KP_Divide
: xkey
= 'Q'; break;
705 case GDK_KP_Multiply
: xkey
= 'R'; break;
706 case GDK_KP_Subtract
: xkey
= 'S'; break;
708 * Keypad + is tricky. It covers a space that would
709 * be taken up on the VT100 by _two_ keys; so we
710 * let Shift select between the two. Worse still,
711 * in xterm function key mode we change which two...
714 if (inst
->cfg
.funky_type
== 2) {
715 if (event
->state
& GDK_SHIFT_MASK
)
719 } else if (event
->state
& GDK_SHIFT_MASK
)
724 case GDK_KP_Enter
: xkey
= 'M'; break;
725 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
726 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
727 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
728 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
729 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
730 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
731 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
732 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
733 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
734 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
735 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
738 if (inst
->term
->vt52_mode
) {
739 if (xkey
>= 'P' && xkey
<= 'S')
740 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
742 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
744 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
745 use_ucsoutput
= FALSE
;
751 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
752 * for integer decimal nn.)
754 * We also deal with the weird ones here. Linux VCs replace F1
755 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
756 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
761 switch (event
->keyval
) {
763 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
766 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
769 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
772 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
775 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
778 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
781 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
784 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
787 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
790 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
823 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
824 case GDK_Home
: case GDK_KP_Home
:
827 case GDK_Insert
: case GDK_KP_Insert
:
830 case GDK_Delete
: case GDK_KP_Delete
:
833 case GDK_End
: case GDK_KP_End
:
836 case GDK_Page_Up
: case GDK_KP_Page_Up
:
839 case GDK_Page_Down
: case GDK_KP_Page_Down
:
843 /* Reorder edit keys to physical order */
844 if (inst
->cfg
.funky_type
== 3 && code
<= 6)
845 code
= "\0\2\1\4\5\3\6"[code
];
847 if (inst
->term
->vt52_mode
&& code
> 0 && code
<= 6) {
848 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
849 use_ucsoutput
= FALSE
;
853 if (inst
->cfg
.funky_type
== 5 && /* SCO function keys */
854 code
>= 11 && code
<= 34) {
855 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
857 switch (event
->keyval
) {
858 case GDK_F1
: index
= 0; break;
859 case GDK_F2
: index
= 1; break;
860 case GDK_F3
: index
= 2; break;
861 case GDK_F4
: index
= 3; break;
862 case GDK_F5
: index
= 4; break;
863 case GDK_F6
: index
= 5; break;
864 case GDK_F7
: index
= 6; break;
865 case GDK_F8
: index
= 7; break;
866 case GDK_F9
: index
= 8; break;
867 case GDK_F10
: index
= 9; break;
868 case GDK_F11
: index
= 10; break;
869 case GDK_F12
: index
= 11; break;
871 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
872 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
873 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
874 use_ucsoutput
= FALSE
;
877 if (inst
->cfg
.funky_type
== 5 && /* SCO small keypad */
878 code
>= 1 && code
<= 6) {
879 char codes
[] = "HL.FIG";
884 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
886 use_ucsoutput
= FALSE
;
889 if ((inst
->term
->vt52_mode
|| inst
->cfg
.funky_type
== 4) &&
890 code
>= 11 && code
<= 24) {
896 if (inst
->term
->vt52_mode
)
897 end
= 1 + sprintf(output
+1,
898 "\x1B%c", code
+ 'P' - 11 - offt
);
900 end
= 1 + sprintf(output
+1,
901 "\x1BO%c", code
+ 'P' - 11 - offt
);
902 use_ucsoutput
= FALSE
;
905 if (inst
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
906 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
907 use_ucsoutput
= FALSE
;
910 if (inst
->cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
911 if (inst
->term
->vt52_mode
)
912 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
914 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
915 use_ucsoutput
= FALSE
;
918 if (inst
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
919 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
920 use_ucsoutput
= FALSE
;
924 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
925 use_ucsoutput
= FALSE
;
931 * Cursor keys. (This includes the numberpad cursor keys,
932 * if we haven't already done them due to app keypad mode.)
934 * Here we also process un-numlocked un-appkeypadded KP5,
935 * which sends ESC [ G.
939 switch (event
->keyval
) {
940 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
941 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
942 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
943 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
944 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
948 * The arrow keys normally do ESC [ A and so on. In
949 * app cursor keys mode they do ESC O A instead.
950 * Ctrl toggles the two modes.
952 if (inst
->term
->vt52_mode
) {
953 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
954 } else if (!inst
->term
->app_cursor_keys
^
955 !(event
->state
& GDK_CONTROL_MASK
)) {
956 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
958 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
960 use_ucsoutput
= FALSE
;
972 printf("generating sequence:");
973 for (i
= start
; i
< end
; i
++)
974 printf(" %02x", (unsigned char) output
[i
]);
980 * For special control characters, the character set
981 * should never matter.
983 output
[end
] = '\0'; /* NUL-terminate */
984 ldisc_send(inst
->ldisc
, output
+start
, -2, 1);
985 } else if (!inst
->direct_to_font
) {
986 if (!use_ucsoutput
) {
988 * The stuff we've just generated is assumed to be
989 * ISO-8859-1! This sounds insane, but `man
990 * XLookupString' agrees: strings of this type
991 * returned from the X server are hardcoded to
992 * 8859-1. Strictly speaking we should be doing
993 * this using some sort of GtkIMContext, which (if
994 * we're lucky) would give us our data directly in
995 * Unicode; but that's not supported in GTK 1.2 as
996 * far as I can tell, and it's poorly documented
997 * even in 2.0, so it'll have to wait.
999 lpage_send(inst
->ldisc
, CS_ISO8859_1
, output
+start
,
1003 * We generated our own Unicode key data from the
1004 * keysym, so use that instead.
1006 luni_send(inst
->ldisc
, ucsoutput
+start
, end
-start
, 1);
1010 * In direct-to-font mode, we just send the string
1011 * exactly as we received it.
1013 ldisc_send(inst
->ldisc
, output
+start
, end
-start
, 1);
1016 show_mouseptr(inst
, 0);
1017 term_seen_key_event(inst
->term
);
1018 term_out(inst
->term
);
1024 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
1026 struct gui_data
*inst
= (struct gui_data
*)data
;
1027 int shift
, ctrl
, alt
, x
, y
, button
, act
;
1029 /* Remember the timestamp. */
1030 inst
->input_event_time
= event
->time
;
1032 show_mouseptr(inst
, 1);
1034 if (event
->button
== 4 && event
->type
== GDK_BUTTON_PRESS
) {
1035 term_scroll(inst
->term
, 0, -5);
1038 if (event
->button
== 5 && event
->type
== GDK_BUTTON_PRESS
) {
1039 term_scroll(inst
->term
, 0, +5);
1043 shift
= event
->state
& GDK_SHIFT_MASK
;
1044 ctrl
= event
->state
& GDK_CONTROL_MASK
;
1045 alt
= event
->state
& GDK_MOD1_MASK
;
1047 if (event
->button
== 3 && ctrl
) {
1048 gtk_menu_popup(GTK_MENU(inst
->menu
), NULL
, NULL
, NULL
, NULL
,
1049 event
->button
, event
->time
);
1053 if (event
->button
== 1)
1055 else if (event
->button
== 2)
1056 button
= MBT_MIDDLE
;
1057 else if (event
->button
== 3)
1060 return FALSE
; /* don't even know what button! */
1062 switch (event
->type
) {
1063 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
1064 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
1065 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
1066 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
1067 default: return FALSE
; /* don't know this event type */
1070 if (send_raw_mouse
&& !(inst
->cfg
.mouse_override
&& shift
) &&
1071 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
1072 return TRUE
; /* we ignore these in raw mouse mode */
1074 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1075 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1077 term_mouse(inst
->term
, button
, translate_button(button
), act
,
1078 x
, y
, shift
, ctrl
, alt
);
1083 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
1085 struct gui_data
*inst
= (struct gui_data
*)data
;
1086 int shift
, ctrl
, alt
, x
, y
, button
;
1088 /* Remember the timestamp. */
1089 inst
->input_event_time
= event
->time
;
1091 show_mouseptr(inst
, 1);
1093 shift
= event
->state
& GDK_SHIFT_MASK
;
1094 ctrl
= event
->state
& GDK_CONTROL_MASK
;
1095 alt
= event
->state
& GDK_MOD1_MASK
;
1096 if (event
->state
& GDK_BUTTON1_MASK
)
1098 else if (event
->state
& GDK_BUTTON2_MASK
)
1099 button
= MBT_MIDDLE
;
1100 else if (event
->state
& GDK_BUTTON3_MASK
)
1103 return FALSE
; /* don't even know what button! */
1105 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1106 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1108 term_mouse(inst
->term
, button
, translate_button(button
), MA_DRAG
,
1109 x
, y
, shift
, ctrl
, alt
);
1114 void frontend_keypress(void *handle
)
1116 struct gui_data
*inst
= (struct gui_data
*)handle
;
1119 * If our child process has exited but not closed, terminate on
1126 gint
timer_func(gpointer data
)
1128 struct gui_data
*inst
= (struct gui_data
*)data
;
1131 if (!inst
->exited
&&
1132 (exitcode
= inst
->back
->exitcode(inst
->backhandle
)) >= 0) {
1133 inst
->exited
= TRUE
;
1134 if (inst
->cfg
.close_on_exit
== FORCE_ON
||
1135 (inst
->cfg
.close_on_exit
== AUTO
&& exitcode
== 0))
1136 exit(0); /* just go. */
1139 term_update(inst
->term
);
1140 term_blink(inst
->term
, 0);
1144 void fd_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
1147 * We must process exceptional notifications before ordinary
1148 * readability ones, or we may go straight past the urgent
1151 if (condition
& GDK_INPUT_EXCEPTION
)
1152 select_result(sourcefd
, 4);
1153 if (condition
& GDK_INPUT_READ
)
1154 select_result(sourcefd
, 1);
1155 if (condition
& GDK_INPUT_WRITE
)
1156 select_result(sourcefd
, 2);
1159 void destroy(GtkWidget
*widget
, gpointer data
)
1164 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
1166 struct gui_data
*inst
= (struct gui_data
*)data
;
1167 inst
->term
->has_focus
= event
->in
;
1168 term_out(inst
->term
);
1169 term_update(inst
->term
);
1170 show_mouseptr(inst
, 1);
1175 * set or clear the "raw mouse message" mode
1177 void set_raw_mouse_mode(void *frontend
, int activate
)
1179 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1180 activate
= activate
&& !inst
->cfg
.no_mouse_rep
;
1181 send_raw_mouse
= activate
;
1183 inst
->currcursor
= inst
->rawcursor
;
1185 inst
->currcursor
= inst
->textcursor
;
1186 show_mouseptr(inst
, inst
->mouseptr_visible
);
1189 void request_resize(void *frontend
, int w
, int h
)
1191 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1192 int large_x
, large_y
;
1193 int offset_x
, offset_y
;
1195 GtkRequisition inner
, outer
;
1198 * This is a heinous hack dreamed up by the gnome-terminal
1199 * people to get around a limitation in gtk. The problem is
1200 * that in order to set the size correctly we really need to be
1201 * calling gtk_window_resize - but that needs to know the size
1202 * of the _whole window_, not the drawing area. So what we do
1203 * is to set an artificially huge size request on the drawing
1204 * area, recompute the resulting size request on the window,
1205 * and look at the difference between the two. That gives us
1206 * the x and y offsets we need to translate drawing area size
1207 * into window size for real, and then we call
1208 * gtk_window_resize.
1212 * We start by retrieving the current size of the whole window.
1213 * Adding a bit to _that_ will give us a value we can use as a
1214 * bogus size request which guarantees to be bigger than the
1215 * current size of the drawing area.
1217 get_window_pixels(inst
, &large_x
, &large_y
);
1221 #if GTK_CHECK_VERSION(2,0,0)
1222 gtk_widget_set_size_request(inst
->area
, large_x
, large_y
);
1224 gtk_widget_set_usize(inst
->area
, large_x
, large_y
);
1226 gtk_widget_size_request(inst
->area
, &inner
);
1227 gtk_widget_size_request(inst
->window
, &outer
);
1229 offset_x
= outer
.width
- inner
.width
;
1230 offset_y
= outer
.height
- inner
.height
;
1232 area_x
= inst
->font_width
* w
+ 2*inst
->cfg
.window_border
;
1233 area_y
= inst
->font_height
* h
+ 2*inst
->cfg
.window_border
;
1236 * Now we must set the size request on the drawing area back to
1237 * something sensible before we commit the real resize. Best
1238 * way to do this, I think, is to set it to what the size is
1239 * really going to end up being.
1241 #if GTK_CHECK_VERSION(2,0,0)
1242 gtk_widget_set_size_request(inst
->area
, area_x
, area_y
);
1244 gtk_widget_set_usize(inst
->area
, area_x
, area_y
);
1245 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
), area_x
, area_y
);
1248 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst
->window
));
1250 #if GTK_CHECK_VERSION(2,0,0)
1251 gtk_window_resize(GTK_WINDOW(inst
->window
),
1252 area_x
+ offset_x
, area_y
+ offset_y
);
1254 gdk_window_resize(inst
->window
->window
,
1255 area_x
+ offset_x
, area_y
+ offset_y
);
1259 static void real_palette_set(struct gui_data
*inst
, int n
, int r
, int g
, int b
)
1261 gboolean success
[1];
1263 inst
->cols
[n
].red
= r
* 0x0101;
1264 inst
->cols
[n
].green
= g
* 0x0101;
1265 inst
->cols
[n
].blue
= b
* 0x0101;
1267 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
+ n
, 1);
1268 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
+ n
, 1,
1269 FALSE
, FALSE
, success
);
1271 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname
,
1275 void set_window_background(struct gui_data
*inst
)
1277 if (inst
->area
&& inst
->area
->window
)
1278 gdk_window_set_background(inst
->area
->window
, &inst
->cols
[18]);
1279 if (inst
->window
&& inst
->window
->window
)
1280 gdk_window_set_background(inst
->window
->window
, &inst
->cols
[18]);
1283 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1285 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1286 static const int first
[21] = {
1287 0, 2, 4, 6, 8, 10, 12, 14,
1288 1, 3, 5, 7, 9, 11, 13, 15,
1291 real_palette_set(inst
, first
[n
], r
, g
, b
);
1293 real_palette_set(inst
, first
[n
] + 1, r
, g
, b
);
1295 set_window_background(inst
);
1298 void palette_reset(void *frontend
)
1300 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1301 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1302 static const int ww
[] = {
1303 6, 7, 8, 9, 10, 11, 12, 13,
1304 14, 15, 16, 17, 18, 19, 20, 21,
1307 gboolean success
[NCOLOURS
];
1310 assert(lenof(ww
) == NCOLOURS
);
1312 if (!inst
->colmap
) {
1313 inst
->colmap
= gdk_colormap_get_system();
1315 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
, NCOLOURS
);
1318 for (i
= 0; i
< NCOLOURS
; i
++) {
1319 inst
->cols
[i
].red
= inst
->cfg
.colours
[ww
[i
]][0] * 0x0101;
1320 inst
->cols
[i
].green
= inst
->cfg
.colours
[ww
[i
]][1] * 0x0101;
1321 inst
->cols
[i
].blue
= inst
->cfg
.colours
[ww
[i
]][2] * 0x0101;
1324 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
1325 FALSE
, FALSE
, success
);
1326 for (i
= 0; i
< NCOLOURS
; i
++) {
1328 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1329 appname
, i
, inst
->cfg
.colours
[i
][0],
1330 inst
->cfg
.colours
[i
][1], inst
->cfg
.colours
[i
][2]);
1333 set_window_background(inst
);
1336 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1337 * do this before we start using cut buffers.
1339 void init_cutbuffers()
1341 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1342 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, "", 0);
1343 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1344 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, "", 0);
1345 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1346 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, "", 0);
1347 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1348 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, "", 0);
1349 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1350 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, "", 0);
1351 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1352 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, "", 0);
1353 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1354 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, "", 0);
1355 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1356 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, "", 0);
1359 /* Store the data in a cut-buffer. */
1360 void store_cutbuffer(char * ptr
, int len
)
1362 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1363 XRotateBuffers(GDK_DISPLAY(), 1);
1364 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1367 /* Retrieve data from a cut-buffer.
1368 * Returned data needs to be freed with XFree().
1370 char * retrieve_cutbuffer(int * nbytes
)
1373 ptr
= XFetchBytes(GDK_DISPLAY(), nbytes
);
1374 if (nbytes
<= 0 && ptr
!= 0) {
1381 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
1383 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1384 if (inst
->pasteout_data
)
1385 sfree(inst
->pasteout_data
);
1386 if (inst
->pasteout_data_ctext
)
1387 sfree(inst
->pasteout_data_ctext
);
1388 if (inst
->pasteout_data_utf8
)
1389 sfree(inst
->pasteout_data_utf8
);
1392 * Set up UTF-8 and compound text paste data. This only happens
1393 * if we aren't in direct-to-font mode using the D800 hack.
1395 if (!inst
->direct_to_font
) {
1396 wchar_t *tmp
= data
;
1401 inst
->pasteout_data_utf8
= snewn(len
*6, char);
1402 inst
->pasteout_data_utf8_len
= len
*6;
1403 inst
->pasteout_data_utf8_len
=
1404 charset_from_unicode(&tmp
, &tmplen
, inst
->pasteout_data_utf8
,
1405 inst
->pasteout_data_utf8_len
,
1406 CS_UTF8
, NULL
, NULL
, 0);
1407 if (inst
->pasteout_data_utf8_len
== 0) {
1408 sfree(inst
->pasteout_data_utf8
);
1409 inst
->pasteout_data_utf8
= NULL
;
1411 inst
->pasteout_data_utf8
=
1412 sresize(inst
->pasteout_data_utf8
,
1413 inst
->pasteout_data_utf8_len
+ 1, char);
1414 inst
->pasteout_data_utf8
[inst
->pasteout_data_utf8_len
] = '\0';
1418 * Now let Xlib convert our UTF-8 data into compound text.
1420 list
[0] = inst
->pasteout_data_utf8
;
1421 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list
, 1,
1422 XCompoundTextStyle
, &tp
) == 0) {
1423 inst
->pasteout_data_ctext
= snewn(tp
.nitems
+1, char);
1424 memcpy(inst
->pasteout_data_ctext
, tp
.value
, tp
.nitems
);
1425 inst
->pasteout_data_ctext_len
= tp
.nitems
;
1429 inst
->pasteout_data_utf8
= NULL
;
1430 inst
->pasteout_data_utf8_len
= 0;
1431 inst
->pasteout_data_ctext
= NULL
;
1432 inst
->pasteout_data_ctext_len
= 0;
1435 inst
->pasteout_data
= snewn(len
*6, char);
1436 inst
->pasteout_data_len
= len
*6;
1437 inst
->pasteout_data_len
= wc_to_mb(inst
->ucsdata
.line_codepage
, 0,
1438 data
, len
, inst
->pasteout_data
,
1439 inst
->pasteout_data_len
,
1441 if (inst
->pasteout_data_len
== 0) {
1442 sfree(inst
->pasteout_data
);
1443 inst
->pasteout_data
= NULL
;
1445 inst
->pasteout_data
=
1446 sresize(inst
->pasteout_data
, inst
->pasteout_data_len
, char);
1449 store_cutbuffer(inst
->pasteout_data
, inst
->pasteout_data_len
);
1451 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
1452 inst
->input_event_time
)) {
1453 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1454 GDK_SELECTION_TYPE_STRING
, 1);
1455 if (inst
->pasteout_data_ctext
)
1456 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1457 compound_text_atom
, 1);
1458 if (inst
->pasteout_data_utf8
)
1459 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1460 utf8_string_atom
, 1);
1464 term_deselect(inst
->term
);
1467 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1468 guint info
, guint time_stamp
, gpointer data
)
1470 struct gui_data
*inst
= (struct gui_data
*)data
;
1471 if (seldata
->target
== utf8_string_atom
)
1472 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1473 inst
->pasteout_data_utf8
,
1474 inst
->pasteout_data_utf8_len
);
1475 else if (seldata
->target
== compound_text_atom
)
1476 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1477 inst
->pasteout_data_ctext
,
1478 inst
->pasteout_data_ctext_len
);
1480 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1481 inst
->pasteout_data
, inst
->pasteout_data_len
);
1484 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1487 struct gui_data
*inst
= (struct gui_data
*)data
;
1489 term_deselect(inst
->term
);
1490 if (inst
->pasteout_data
)
1491 sfree(inst
->pasteout_data
);
1492 if (inst
->pasteout_data_ctext
)
1493 sfree(inst
->pasteout_data_ctext
);
1494 if (inst
->pasteout_data_utf8
)
1495 sfree(inst
->pasteout_data_utf8
);
1496 inst
->pasteout_data
= NULL
;
1497 inst
->pasteout_data_len
= 0;
1498 inst
->pasteout_data_ctext
= NULL
;
1499 inst
->pasteout_data_ctext_len
= 0;
1500 inst
->pasteout_data_utf8
= NULL
;
1501 inst
->pasteout_data_utf8_len
= 0;
1505 void request_paste(void *frontend
)
1507 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1509 * In Unix, pasting is asynchronous: all we can do at the
1510 * moment is to call gtk_selection_convert(), and when the data
1511 * comes back _then_ we can call term_do_paste().
1514 if (!inst
->direct_to_font
) {
1516 * First we attempt to retrieve the selection as a UTF-8
1517 * string (which we will convert to the correct code page
1518 * before sending to the session, of course). If that
1519 * fails, selection_received() will be informed and will
1520 * fall back to an ordinary string.
1522 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1524 inst
->input_event_time
);
1527 * If we're in direct-to-font mode, we disable UTF-8
1528 * pasting, and go straight to ordinary string data.
1530 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1531 GDK_SELECTION_TYPE_STRING
,
1532 inst
->input_event_time
);
1536 gint
idle_paste_func(gpointer data
); /* forward ref */
1538 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1539 guint time
, gpointer data
)
1541 struct gui_data
*inst
= (struct gui_data
*)data
;
1545 int length
, count
, ret
;
1546 int free_list_required
= 0;
1547 int free_required
= 0;
1550 if (seldata
->target
== utf8_string_atom
&& seldata
->length
<= 0) {
1552 * Failed to get a UTF-8 selection string. Try compound
1555 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1557 inst
->input_event_time
);
1561 if (seldata
->target
== compound_text_atom
&& seldata
->length
<= 0) {
1563 * Failed to get UTF-8 or compound text. Try an ordinary
1566 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1567 GDK_SELECTION_TYPE_STRING
,
1568 inst
->input_event_time
);
1573 * If we have data, but it's not of a type we can deal with,
1574 * we have to ignore the data.
1576 if (seldata
->length
> 0 &&
1577 seldata
->type
!= GDK_SELECTION_TYPE_STRING
&&
1578 seldata
->type
!= compound_text_atom
&&
1579 seldata
->type
!= utf8_string_atom
)
1583 * If we have no data, try looking in a cut buffer.
1585 if (seldata
->length
<= 0) {
1586 text
= retrieve_cutbuffer(&length
);
1589 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1590 * source, so use that as a de-facto standard. */
1591 charset
= CS_ISO8859_1
;
1595 * Convert COMPOUND_TEXT into UTF-8.
1597 if (seldata
->type
== compound_text_atom
) {
1598 tp
.value
= seldata
->data
;
1599 tp
.encoding
= (Atom
) seldata
->type
;
1600 tp
.format
= seldata
->format
;
1601 tp
.nitems
= seldata
->length
;
1602 ret
= Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp
,
1604 if (ret
!= 0 || count
!= 1) {
1606 * Compound text failed; fall back to STRING.
1608 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1609 GDK_SELECTION_TYPE_STRING
,
1610 inst
->input_event_time
);
1614 length
= strlen(list
[0]);
1616 free_list_required
= 1;
1618 text
= (char *)seldata
->data
;
1619 length
= seldata
->length
;
1620 charset
= (seldata
->type
== utf8_string_atom ?
1621 CS_UTF8
: inst
->ucsdata
.line_codepage
);
1625 if (inst
->pastein_data
)
1626 sfree(inst
->pastein_data
);
1628 inst
->pastein_data
= snewn(length
, wchar_t);
1629 inst
->pastein_data_len
= length
;
1630 inst
->pastein_data_len
=
1631 mb_to_wc(charset
, 0, text
, length
,
1632 inst
->pastein_data
, inst
->pastein_data_len
);
1634 term_do_paste(inst
->term
);
1636 if (term_paste_pending(inst
->term
))
1637 inst
->term_paste_idle_id
= gtk_idle_add(idle_paste_func
, inst
);
1639 if (free_list_required
)
1640 XFreeStringList(list
);
1645 gint
idle_paste_func(gpointer data
)
1647 struct gui_data
*inst
= (struct gui_data
*)data
;
1649 if (term_paste_pending(inst
->term
))
1650 term_paste(inst
->term
);
1652 gtk_idle_remove(inst
->term_paste_idle_id
);
1658 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
1660 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1663 *p
= inst
->pastein_data
;
1664 *len
= inst
->pastein_data_len
;
1668 static void set_window_titles(struct gui_data
*inst
)
1671 * We must always call set_icon_name after calling set_title,
1672 * since set_title will write both names. Irritating, but such
1675 gtk_window_set_title(GTK_WINDOW(inst
->window
), inst
->wintitle
);
1676 if (!inst
->cfg
.win_name_always
)
1677 gdk_window_set_icon_name(inst
->window
->window
, inst
->icontitle
);
1680 void set_title(void *frontend
, char *title
)
1682 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1683 strncpy(inst
->wintitle
, title
, lenof(inst
->wintitle
));
1684 inst
->wintitle
[lenof(inst
->wintitle
)-1] = '\0';
1685 set_window_titles(inst
);
1688 void set_icon(void *frontend
, char *title
)
1690 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1691 strncpy(inst
->icontitle
, title
, lenof(inst
->icontitle
));
1692 inst
->icontitle
[lenof(inst
->icontitle
)-1] = '\0';
1693 set_window_titles(inst
);
1696 void set_sbar(void *frontend
, int total
, int start
, int page
)
1698 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1699 if (!inst
->cfg
.scrollbar
)
1701 inst
->sbar_adjust
->lower
= 0;
1702 inst
->sbar_adjust
->upper
= total
;
1703 inst
->sbar_adjust
->value
= start
;
1704 inst
->sbar_adjust
->page_size
= page
;
1705 inst
->sbar_adjust
->step_increment
= 1;
1706 inst
->sbar_adjust
->page_increment
= page
/2;
1707 inst
->ignore_sbar
= TRUE
;
1708 gtk_adjustment_changed(inst
->sbar_adjust
);
1709 inst
->ignore_sbar
= FALSE
;
1712 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
1714 struct gui_data
*inst
= (struct gui_data
*)data
;
1716 if (!inst
->cfg
.scrollbar
)
1718 if (!inst
->ignore_sbar
)
1719 term_scroll(inst
->term
, 1, (int)adj
->value
);
1722 void sys_cursor(void *frontend
, int x
, int y
)
1725 * This is meaningless under X.
1730 * This is still called when mode==BELL_VISUAL, even though the
1731 * visual bell is handled entirely within terminal.c, because we
1732 * may want to perform additional actions on any kind of bell (for
1733 * example, taskbar flashing in Windows).
1735 void beep(void *frontend
, int mode
)
1737 if (mode
!= BELL_VISUAL
)
1741 int char_width(Context ctx
, int uc
)
1744 * Under X, any fixed-width font really _is_ fixed-width.
1745 * Double-width characters will be dealt with using a separate
1746 * font. For the moment we can simply return 1.
1751 Context
get_ctx(void *frontend
)
1753 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1754 struct draw_ctx
*dctx
;
1756 if (!inst
->area
->window
)
1759 dctx
= snew(struct draw_ctx
);
1761 dctx
->gc
= gdk_gc_new(inst
->area
->window
);
1765 void free_ctx(Context ctx
)
1767 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1768 /* struct gui_data *inst = dctx->inst; */
1769 GdkGC
*gc
= dctx
->gc
;
1775 * Draw a line of text in the window, at given character
1776 * coordinates, in given attributes.
1778 * We are allowed to fiddle with the contents of `text'.
1780 void do_text_internal(Context ctx
, int x
, int y
, char *text
, int len
,
1781 unsigned long attr
, int lattr
)
1783 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1784 struct gui_data
*inst
= dctx
->inst
;
1785 GdkGC
*gc
= dctx
->gc
;
1787 int nfg
, nbg
, t
, fontid
, shadow
, rlen
, widefactor
;
1789 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1790 nfg
= 2 * (nfg
& 0xF) + (nfg
& 0x10 ?
1 : 0);
1791 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1792 nbg
= 2 * (nbg
& 0xF) + (nbg
& 0x10 ?
1 : 0);
1793 if (attr
& ATTR_REVERSE
) {
1798 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
1800 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
1802 if (attr
& TATTR_ACTCURS
) {
1807 fontid
= shadow
= 0;
1809 if (attr
& ATTR_WIDE
) {
1816 if ((attr
& ATTR_BOLD
) && !inst
->cfg
.bold_colour
) {
1817 if (inst
->fonts
[fontid
| 1])
1823 if (lattr
!= LATTR_NORM
) {
1825 if (x
>= inst
->term
->cols
)
1827 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1828 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1836 r
.x
= x
*inst
->font_width
+inst
->cfg
.window_border
;
1837 r
.y
= y
*inst
->font_height
+inst
->cfg
.window_border
;
1838 r
.width
= rlen
*widefactor
*inst
->font_width
;
1839 r
.height
= inst
->font_height
;
1840 gdk_gc_set_clip_rectangle(gc
, &r
);
1843 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
1844 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
1845 x
*inst
->font_width
+inst
->cfg
.window_border
,
1846 y
*inst
->font_height
+inst
->cfg
.window_border
,
1847 rlen
*widefactor
*inst
->font_width
, inst
->font_height
);
1849 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
1856 wcs
= snewn(len
+1, wchar_t);
1857 for (i
= 0; i
< len
; i
++) {
1858 wcs
[i
] = (wchar_t) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
1861 if (inst
->fonts
[fontid
] == NULL
) {
1863 * The font for this contingency does not exist.
1864 * Typically this means we've been given ATTR_WIDE
1865 * character and have no wide font. So we display
1866 * nothing at all; such is life.
1868 } else if (inst
->fontinfo
[fontid
].is_wide
) {
1870 * At least one version of gdk_draw_text_wc() has a
1871 * weird bug whereby it reads `len' elements of the
1872 * input string, but only draws `len/2'. Hence I'm
1873 * going to make its input array twice as long as it
1874 * theoretically needs to be, and pass in twice the
1875 * actual number of characters. If a fixed gdk actually
1876 * takes the doubled length seriously, then (a) the
1877 * array will stand scrutiny up to the full length, (b)
1878 * the spare elements of the array are full of zeroes
1879 * which will probably be an empty glyph in the font,
1880 * and (c) the clip rectangle should prevent it causing
1883 gwcs
= snewn(len
*2+1, GdkWChar
);
1884 memset(gwcs
, 0, sizeof(GdkWChar
) * (len
*2+1));
1886 * FIXME: when we have a wide-char equivalent of
1887 * from_unicode, use it instead of this.
1889 for (i
= 0; i
<= len
; i
++)
1891 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1892 x
*inst
->font_width
+inst
->cfg
.window_border
,
1893 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1897 gcs
= snewn(len
+1, gchar
);
1898 wc_to_mb(inst
->fontinfo
[fontid
].charset
, 0,
1899 wcs
, len
, gcs
, len
, ".", NULL
, NULL
);
1900 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1901 x
*inst
->font_width
+inst
->cfg
.window_border
,
1902 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1910 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1911 x
*inst
->font_width
+inst
->cfg
.window_border
+ inst
->cfg
.shadowboldoffset
,
1912 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1916 if (attr
& ATTR_UNDER
) {
1917 int uheight
= inst
->fonts
[0]->ascent
+ 1;
1918 if (uheight
>= inst
->font_height
)
1919 uheight
= inst
->font_height
- 1;
1920 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+inst
->cfg
.window_border
,
1921 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
,
1922 (x
+len
)*widefactor
*inst
->font_width
-1+inst
->cfg
.window_border
,
1923 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
);
1926 if (lattr
!= LATTR_NORM
) {
1928 * I can't find any plausible StretchBlt equivalent in the
1929 * X server, so I'm going to do this the slow and painful
1930 * way. This will involve repeated calls to
1931 * gdk_draw_pixmap() to stretch the text horizontally. It's
1932 * O(N^2) in time and O(N) in network bandwidth, but you
1933 * try thinking of a better way. :-(
1936 for (i
= 0; i
< len
* widefactor
* inst
->font_width
; i
++) {
1937 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1938 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
,
1939 y
*inst
->font_height
+inst
->cfg
.window_border
,
1940 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
+1,
1941 y
*inst
->font_height
+inst
->cfg
.window_border
,
1942 len
* inst
->font_width
- i
, inst
->font_height
);
1945 if (lattr
!= LATTR_WIDE
) {
1947 /* Now stretch vertically, in the same way. */
1948 if (lattr
== LATTR_BOT
)
1952 for (i
= 0; i
< inst
->font_height
; i
+=2) {
1953 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1954 x
*inst
->font_width
+inst
->cfg
.window_border
,
1955 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*i
+db
,
1956 x
*widefactor
*inst
->font_width
+inst
->cfg
.window_border
,
1957 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*(i
+1),
1958 len
* inst
->font_width
, inst
->font_height
-i
-1);
1964 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
1965 unsigned long attr
, int lattr
)
1967 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1968 struct gui_data
*inst
= dctx
->inst
;
1969 GdkGC
*gc
= dctx
->gc
;
1972 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1974 if (attr
& ATTR_WIDE
) {
1980 if (lattr
!= LATTR_NORM
) {
1982 if (x
>= inst
->term
->cols
)
1984 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1985 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1989 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1990 x
*inst
->font_width
+inst
->cfg
.window_border
,
1991 y
*inst
->font_height
+inst
->cfg
.window_border
,
1992 x
*inst
->font_width
+inst
->cfg
.window_border
,
1993 y
*inst
->font_height
+inst
->cfg
.window_border
,
1994 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1997 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1998 unsigned long attr
, int lattr
)
2000 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
2001 struct gui_data
*inst
= dctx
->inst
;
2002 GdkGC
*gc
= dctx
->gc
;
2004 int passive
, widefactor
;
2006 if (attr
& TATTR_PASCURS
) {
2007 attr
&= ~TATTR_PASCURS
;
2011 if ((attr
& TATTR_ACTCURS
) && inst
->cfg
.cursor_type
!= 0) {
2012 attr
&= ~TATTR_ACTCURS
;
2014 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
2016 if (attr
& ATTR_WIDE
) {
2022 if (lattr
!= LATTR_NORM
) {
2024 if (x
>= inst
->term
->cols
)
2026 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
2027 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
2031 if (inst
->cfg
.cursor_type
== 0) {
2033 * An active block cursor will already have been done by
2034 * the above do_text call, so we only need to do anything
2038 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
2039 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
2040 x
*inst
->font_width
+inst
->cfg
.window_border
,
2041 y
*inst
->font_height
+inst
->cfg
.window_border
,
2042 len
*inst
->font_width
-1, inst
->font_height
-1);
2046 int startx
, starty
, dx
, dy
, length
, i
;
2050 if ((attr
& ATTR_WIDE
) || lattr
!= LATTR_NORM
)
2051 char_width
= 2*inst
->font_width
;
2053 char_width
= inst
->font_width
;
2055 if (inst
->cfg
.cursor_type
== 1) {
2056 uheight
= inst
->fonts
[0]->ascent
+ 1;
2057 if (uheight
>= inst
->font_height
)
2058 uheight
= inst
->font_height
- 1;
2060 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
;
2061 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
+ uheight
;
2064 length
= len
* char_width
;
2067 if (attr
& TATTR_RIGHTCURS
)
2068 xadjust
= char_width
- 1;
2069 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
+ xadjust
;
2070 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
;
2073 length
= inst
->font_height
;
2076 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
2078 for (i
= 0; i
< length
; i
++) {
2080 gdk_draw_point(inst
->pixmap
, gc
, startx
, starty
);
2086 gdk_draw_line(inst
->pixmap
, gc
, startx
, starty
,
2087 startx
+ (length
-1) * dx
, starty
+ (length
-1) * dy
);
2091 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
2092 x
*inst
->font_width
+inst
->cfg
.window_border
,
2093 y
*inst
->font_height
+inst
->cfg
.window_border
,
2094 x
*inst
->font_width
+inst
->cfg
.window_border
,
2095 y
*inst
->font_height
+inst
->cfg
.window_border
,
2096 len
*widefactor
*inst
->font_width
, inst
->font_height
);
2099 GdkCursor
*make_mouse_ptr(struct gui_data
*inst
, int cursor_val
)
2102 * Truly hideous hack: GTK doesn't allow us to set the mouse
2103 * cursor foreground and background colours unless we've _also_
2104 * created our own cursor from bitmaps. Therefore, I need to
2105 * load the `cursor' font and draw glyphs from it on to
2106 * pixmaps, in order to construct my cursors with the fg and bg
2107 * I want. This is a gross hack, but it's more self-contained
2108 * than linking in Xlib to find the X window handle to
2109 * inst->area and calling XRecolorCursor, and it's more
2110 * futureproof than hard-coding the shapes as bitmap arrays.
2112 static GdkFont
*cursor_font
= NULL
;
2113 GdkPixmap
*source
, *mask
;
2115 GdkColor cfg
= { 0, 65535, 65535, 65535 };
2116 GdkColor cbg
= { 0, 0, 0, 0 };
2117 GdkColor dfg
= { 1, 65535, 65535, 65535 };
2118 GdkColor dbg
= { 0, 0, 0, 0 };
2121 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
2123 if (cursor_val
== -2) {
2124 gdk_font_unref(cursor_font
);
2128 if (cursor_val
>= 0 && !cursor_font
)
2129 cursor_font
= gdk_font_load("cursor");
2132 * Get the text extent of the cursor in question. We use the
2133 * mask character for this, because it's typically slightly
2134 * bigger than the main character.
2136 if (cursor_val
>= 0) {
2138 text
[0] = (char)cursor_val
+ 1;
2139 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
2140 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
2146 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
2147 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
2150 * Draw the mask character on the mask pixmap.
2152 gc
= gdk_gc_new(mask
);
2153 gdk_gc_set_foreground(gc
, &dbg
);
2154 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
2155 if (cursor_val
>= 0) {
2157 text
[0] = (char)cursor_val
+ 1;
2158 gdk_gc_set_foreground(gc
, &dfg
);
2159 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
2164 * Draw the main character on the source pixmap.
2166 gc
= gdk_gc_new(source
);
2167 gdk_gc_set_foreground(gc
, &dbg
);
2168 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
2169 if (cursor_val
>= 0) {
2171 text
[0] = (char)cursor_val
;
2172 gdk_gc_set_foreground(gc
, &dfg
);
2173 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
2178 * Create the cursor.
2180 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
2185 gdk_pixmap_unref(source
);
2186 gdk_pixmap_unref(mask
);
2191 void modalfatalbox(char *p
, ...)
2194 fprintf(stderr
, "FATAL ERROR: ");
2196 vfprintf(stderr
, p
, ap
);
2198 fputc('\n', stderr
);
2202 void cmdline_error(char *p
, ...)
2205 fprintf(stderr
, "%s: ", appname
);
2207 vfprintf(stderr
, p
, ap
);
2209 fputc('\n', stderr
);
2213 char *get_x_display(void *frontend
)
2215 return gdk_get_display();
2218 long get_windowid(void *frontend
)
2220 struct gui_data
*inst
= (struct gui_data
*)frontend
;
2221 return (long)GDK_WINDOW_XWINDOW(inst
->area
->window
);
2224 static void help(FILE *fp
) {
2226 "pterm option summary:\n"
2228 " --display DISPLAY Specify X display to use (note '--')\n"
2229 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2230 " -fn FONT Normal text font\n"
2231 " -fb FONT Bold text font\n"
2232 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2233 " -sl LINES Number of lines of scrollback\n"
2234 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2235 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2236 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2237 " -T TITLE Window title\n"
2238 " -ut, +ut Do(default) or do not update utmp\n"
2239 " -ls, +ls Do(default) or do not make shell a login shell\n"
2240 " -sb, +sb Do(default) or do not display a scrollbar\n"
2241 " -log PATH Log all output to a file\n"
2242 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2243 " -xrm RESOURCE-STRING Set an X resource\n"
2244 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2245 ) < 0 || fflush(fp
) < 0) {
2246 perror("output error");
2251 int do_cmdline(int argc
, char **argv
, int do_everything
,
2252 struct gui_data
*inst
, Config
*cfg
)
2258 * Macros to make argument handling easier. Note that because
2259 * they need to call `continue', they cannot be contained in
2260 * the usual do {...} while (0) wrapper to make them
2261 * syntactically single statements; hence it is not legal to
2262 * use one of these macros as an unbraced statement between
2265 #define EXPECTS_ARG { \
2266 if (--argc <= 0) { \
2268 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2273 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2275 while (--argc
> 0) {
2280 * Shameless cheating. Debian requires all X terminal
2281 * emulators to support `-T title'; but
2282 * cmdline_process_param will eat -T (it means no-pty) and
2283 * complain that pterm doesn't support it. So, in pterm
2284 * only, we convert -T into -title.
2286 if ((cmdline_tooltype
& TOOLTYPE_NONNETWORK
) &&
2290 ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
2291 do_everything ?
1 : -1, cfg
);
2294 cmdline_error("option \"%s\" requires an argument", p
);
2295 } else if (ret
== 2) {
2296 --argc
, ++argv
; /* skip next argument */
2298 } else if (ret
== 1) {
2302 if (!strcmp(p
, "-fn") || !strcmp(p
, "-font")) {
2305 strncpy(cfg
->font
.name
, val
, sizeof(cfg
->font
.name
));
2306 cfg
->font
.name
[sizeof(cfg
->font
.name
)-1] = '\0';
2308 } else if (!strcmp(p
, "-fb")) {
2311 strncpy(cfg
->boldfont
.name
, val
, sizeof(cfg
->boldfont
.name
));
2312 cfg
->boldfont
.name
[sizeof(cfg
->boldfont
.name
)-1] = '\0';
2314 } else if (!strcmp(p
, "-fw")) {
2317 strncpy(cfg
->widefont
.name
, val
, sizeof(cfg
->widefont
.name
));
2318 cfg
->widefont
.name
[sizeof(cfg
->widefont
.name
)-1] = '\0';
2320 } else if (!strcmp(p
, "-fwb")) {
2323 strncpy(cfg
->wideboldfont
.name
, val
, sizeof(cfg
->wideboldfont
.name
));
2324 cfg
->wideboldfont
.name
[sizeof(cfg
->wideboldfont
.name
)-1] = '\0';
2326 } else if (!strcmp(p
, "-cs")) {
2329 strncpy(cfg
->line_codepage
, val
, sizeof(cfg
->line_codepage
));
2330 cfg
->line_codepage
[sizeof(cfg
->line_codepage
)-1] = '\0';
2332 } else if (!strcmp(p
, "-geometry")) {
2333 int flags
, x
, y
, w
, h
;
2337 flags
= XParseGeometry(val
, &x
, &y
, &w
, &h
);
2338 if (flags
& WidthValue
)
2340 if (flags
& HeightValue
)
2343 if (flags
& (XValue
| YValue
)) {
2346 inst
->gotpos
= TRUE
;
2347 inst
->gravity
= ((flags
& XNegative ?
1 : 0) |
2348 (flags
& YNegative ?
2 : 0));
2351 } else if (!strcmp(p
, "-sl")) {
2354 cfg
->savelines
= atoi(val
);
2356 } else if (!strcmp(p
, "-fg") || !strcmp(p
, "-bg") ||
2357 !strcmp(p
, "-bfg") || !strcmp(p
, "-bbg") ||
2358 !strcmp(p
, "-cfg") || !strcmp(p
, "-cbg")) {
2363 if (!gdk_color_parse(val
, &col
)) {
2365 fprintf(stderr
, "%s: unable to parse colour \"%s\"\n",
2369 index
= (!strcmp(p
, "-fg") ?
0 :
2370 !strcmp(p
, "-bg") ?
2 :
2371 !strcmp(p
, "-bfg") ?
1 :
2372 !strcmp(p
, "-bbg") ?
3 :
2373 !strcmp(p
, "-cfg") ?
4 :
2374 !strcmp(p
, "-cbg") ?
5 : -1);
2375 assert(index
!= -1);
2376 cfg
->colours
[index
][0] = col
.red
/ 256;
2377 cfg
->colours
[index
][1] = col
.green
/ 256;
2378 cfg
->colours
[index
][2] = col
.blue
/ 256;
2381 } else if (use_pty_argv
&& !strcmp(p
, "-e")) {
2382 /* This option swallows all further arguments. */
2388 pty_argv
= snewn(argc
+1, char *);
2390 for (i
= 0; i
< argc
; i
++)
2391 pty_argv
[i
] = argv
[i
];
2392 pty_argv
[argc
] = NULL
;
2393 break; /* finished command-line processing */
2395 err
= 1, fprintf(stderr
, "%s: -e expects an argument\n",
2398 } else if (!strcmp(p
, "-title")) {
2401 strncpy(cfg
->wintitle
, val
, sizeof(cfg
->wintitle
));
2402 cfg
->wintitle
[sizeof(cfg
->wintitle
)-1] = '\0';
2404 } else if (!strcmp(p
, "-log")) {
2407 strncpy(cfg
->logfilename
.path
, val
, sizeof(cfg
->logfilename
.path
));
2408 cfg
->logfilename
.path
[sizeof(cfg
->logfilename
.path
)-1] = '\0';
2409 cfg
->logtype
= LGTYP_DEBUG
;
2411 } else if (!strcmp(p
, "-ut-") || !strcmp(p
, "+ut")) {
2413 cfg
->stamp_utmp
= 0;
2415 } else if (!strcmp(p
, "-ut")) {
2417 cfg
->stamp_utmp
= 1;
2419 } else if (!strcmp(p
, "-ls-") || !strcmp(p
, "+ls")) {
2421 cfg
->login_shell
= 0;
2423 } else if (!strcmp(p
, "-ls")) {
2425 cfg
->login_shell
= 1;
2427 } else if (!strcmp(p
, "-nethack")) {
2429 cfg
->nethack_keypad
= 1;
2431 } else if (!strcmp(p
, "-sb-") || !strcmp(p
, "+sb")) {
2435 } else if (!strcmp(p
, "-sb")) {
2439 } else if (!strcmp(p
, "-name")) {
2443 } else if (!strcmp(p
, "-xrm")) {
2445 provide_xrm_string(val
);
2447 } else if(!strcmp(p
, "-help") || !strcmp(p
, "--help")) {
2451 } else if(p
[0] != '-' && (!do_everything
||
2452 process_nonoption_arg(p
, cfg
))) {
2457 fprintf(stderr
, "%s: unrecognized option '%s'\n", appname
, p
);
2464 static void block_signal(int sig
, int block_it
) {
2468 sigaddset(&ss
, sig
);
2469 if(sigprocmask(block_it ? SIG_BLOCK
: SIG_UNBLOCK
, &ss
, 0) < 0) {
2470 perror("sigprocmask");
2476 * This function retrieves the character set encoding of a font. It
2477 * returns the character set without the X11 hack (in case the user
2478 * asks to use the font's own encoding).
2480 static int set_font_info(struct gui_data
*inst
, int fontid
)
2482 GdkFont
*font
= inst
->fonts
[fontid
];
2483 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2484 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2485 Atom charset_registry
, charset_encoding
;
2486 unsigned long registry_ret
, encoding_ret
;
2487 int retval
= CS_NONE
;
2489 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
2490 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
2491 inst
->fontinfo
[fontid
].charset
= CS_NONE
;
2492 inst
->fontinfo
[fontid
].is_wide
= 0;
2493 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
2494 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
2496 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
2497 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
2499 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
2500 retval
= inst
->fontinfo
[fontid
].charset
=
2501 charset_from_xenc(encoding
);
2502 /* FIXME: when libcharset supports wide encodings fix this. */
2503 if (!strcasecmp(encoding
, "iso10646-1")) {
2504 inst
->fontinfo
[fontid
].is_wide
= 1;
2509 * Hack for X line-drawing characters: if the primary
2510 * font is encoded as ISO-8859-anything, and has valid
2511 * glyphs in the first 32 char positions, it is assumed
2512 * that those glyphs are the VT100 line-drawing
2515 * Actually, we'll hack even harder by only checking
2516 * position 0x19 (vertical line, VT100 linedrawing
2517 * `x'). Then we can check it easily by seeing if the
2518 * ascent and descent differ.
2520 if (inst
->fontinfo
[fontid
].charset
== CS_ISO8859_1
) {
2521 int lb
, rb
, wid
, asc
, desc
;
2526 gdk_string_extents(inst
->fonts
[fontid
], text
,
2527 &lb
, &rb
, &wid
, &asc
, &desc
);
2529 inst
->fontinfo
[fontid
].charset
= CS_ISO8859_1_X11
;
2539 int uxsel_input_add(int fd
, int rwx
) {
2541 if (rwx
& 1) flags
|= GDK_INPUT_READ
;
2542 if (rwx
& 2) flags
|= GDK_INPUT_WRITE
;
2543 if (rwx
& 4) flags
|= GDK_INPUT_EXCEPTION
;
2544 return gdk_input_add(fd
, flags
, fd_input_func
, NULL
);
2547 void uxsel_input_remove(int id
) {
2548 gdk_input_remove(id
);
2551 char *guess_derived_font_name(GdkFont
*font
, int bold
, int wide
)
2553 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2554 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2555 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
2557 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
2558 char *name
= XGetAtomName(disp
, (Atom
)ret
);
2559 if (name
&& name
[0] == '-') {
2561 char *dupname
, *extrafree
= NULL
, *ret
;
2565 p
= q
= dupname
= dupstr(name
); /* skip initial minus */
2568 while (*p
&& nstr
< lenof(strings
)) {
2571 strings
[nstr
++] = p
+1;
2576 if (nstr
< lenof(strings
))
2577 return NULL
; /* XLFD was malformed */
2580 strings
[2] = "bold";
2583 /* 4 is `wideness', which obviously may have changed. */
2584 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2585 strings
[4] = strings
[5] = "*";
2586 strings
[11] = extrafree
= dupprintf("%d", 2*atoi(strings
[11]));
2589 ret
= dupcat("-", strings
[ 0], "-", strings
[ 1], "-", strings
[ 2],
2590 "-", strings
[ 3], "-", strings
[ 4], "-", strings
[ 5],
2591 "-", strings
[ 6], "-", strings
[ 7], "-", strings
[ 8],
2592 "-", strings
[ 9], "-", strings
[10], "-", strings
[11],
2593 "-", strings
[12], NULL
);
2603 void setup_fonts_ucs(struct gui_data
*inst
)
2610 gdk_font_unref(inst
->fonts
[0]);
2612 gdk_font_unref(inst
->fonts
[1]);
2614 gdk_font_unref(inst
->fonts
[2]);
2616 gdk_font_unref(inst
->fonts
[3]);
2618 inst
->fonts
[0] = gdk_font_load(inst
->cfg
.font
.name
);
2619 if (!inst
->fonts
[0]) {
2620 fprintf(stderr
, "%s: unable to load font \"%s\"\n", appname
,
2621 inst
->cfg
.font
.name
);
2624 font_charset
= set_font_info(inst
, 0);
2626 if (inst
->cfg
.shadowbold
) {
2627 inst
->fonts
[1] = NULL
;
2629 if (inst
->cfg
.boldfont
.name
[0]) {
2630 name
= inst
->cfg
.boldfont
.name
;
2633 name
= guess_derived_font_name(inst
->fonts
[0], TRUE
, FALSE
);
2636 inst
->fonts
[1] = name ?
gdk_font_load(name
) : NULL
;
2637 if (inst
->fonts
[1]) {
2638 set_font_info(inst
, 1);
2639 } else if (!guessed
) {
2640 fprintf(stderr
, "%s: unable to load bold font \"%s\"\n", appname
,
2641 inst
->cfg
.boldfont
.name
);
2648 if (inst
->cfg
.widefont
.name
[0]) {
2649 name
= inst
->cfg
.widefont
.name
;
2652 name
= guess_derived_font_name(inst
->fonts
[0], FALSE
, TRUE
);
2655 inst
->fonts
[2] = name ?
gdk_font_load(name
) : NULL
;
2656 if (inst
->fonts
[2]) {
2657 set_font_info(inst
, 2);
2658 } else if (!guessed
) {
2659 fprintf(stderr
, "%s: unable to load wide font \"%s\"\n", appname
,
2660 inst
->cfg
.widefont
.name
);
2666 if (inst
->cfg
.shadowbold
) {
2667 inst
->fonts
[3] = NULL
;
2669 if (inst
->cfg
.wideboldfont
.name
[0]) {
2670 name
= inst
->cfg
.wideboldfont
.name
;
2674 * Here we have some choices. We can widen the bold font,
2675 * bolden the wide font, or widen and bolden the standard
2676 * font. Try them all, in that order!
2678 if (inst
->cfg
.widefont
.name
[0])
2679 name
= guess_derived_font_name(inst
->fonts
[2], TRUE
, FALSE
);
2680 else if (inst
->cfg
.boldfont
.name
[0])
2681 name
= guess_derived_font_name(inst
->fonts
[1], FALSE
, TRUE
);
2683 name
= guess_derived_font_name(inst
->fonts
[0], TRUE
, TRUE
);
2686 inst
->fonts
[3] = name ?
gdk_font_load(name
) : NULL
;
2687 if (inst
->fonts
[3]) {
2688 set_font_info(inst
, 3);
2689 } else if (!guessed
) {
2690 fprintf(stderr
, "%s: unable to load wide/bold font \"%s\"\n", appname
,
2691 inst
->cfg
.wideboldfont
.name
);
2698 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
2699 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
2701 inst
->direct_to_font
= init_ucs(&inst
->ucsdata
,
2702 inst
->cfg
.line_codepage
, font_charset
,
2706 void set_geom_hints(struct gui_data
*inst
)
2709 geom
.min_width
= inst
->font_width
+ 2*inst
->cfg
.window_border
;
2710 geom
.min_height
= inst
->font_height
+ 2*inst
->cfg
.window_border
;
2711 geom
.max_width
= geom
.max_height
= -1;
2712 geom
.base_width
= 2*inst
->cfg
.window_border
;
2713 geom
.base_height
= 2*inst
->cfg
.window_border
;
2714 geom
.width_inc
= inst
->font_width
;
2715 geom
.height_inc
= inst
->font_height
;
2716 geom
.min_aspect
= geom
.max_aspect
= 0;
2717 gtk_window_set_geometry_hints(GTK_WINDOW(inst
->window
), inst
->area
, &geom
,
2718 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
2719 GDK_HINT_RESIZE_INC
);
2722 void clear_scrollback_menuitem(GtkMenuItem
*item
, gpointer data
)
2724 struct gui_data
*inst
= (struct gui_data
*)data
;
2725 term_clrsb(inst
->term
);
2728 void reset_terminal_menuitem(GtkMenuItem
*item
, gpointer data
)
2730 struct gui_data
*inst
= (struct gui_data
*)data
;
2731 term_pwron(inst
->term
);
2732 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2735 void copy_all_menuitem(GtkMenuItem
*item
, gpointer data
)
2737 struct gui_data
*inst
= (struct gui_data
*)data
;
2738 term_copyall(inst
->term
);
2741 void special_menuitem(GtkMenuItem
*item
, gpointer data
)
2743 struct gui_data
*inst
= (struct gui_data
*)data
;
2744 int code
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
2747 inst
->back
->special(inst
->backhandle
, code
);
2750 void about_menuitem(GtkMenuItem
*item
, gpointer data
)
2752 struct gui_data
*inst
= (struct gui_data
*)data
;
2753 about_box(inst
->window
);
2756 void event_log_menuitem(GtkMenuItem
*item
, gpointer data
)
2758 struct gui_data
*inst
= (struct gui_data
*)data
;
2759 showeventlog(inst
->eventlogstuff
, inst
->window
);
2762 void change_settings_menuitem(GtkMenuItem
*item
, gpointer data
)
2764 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2765 static const int ww
[] = {
2766 6, 7, 8, 9, 10, 11, 12, 13,
2767 14, 15, 16, 17, 18, 19, 20, 21,
2770 struct gui_data
*inst
= (struct gui_data
*)data
;
2771 char *title
= dupcat(appname
, " Reconfiguration", NULL
);
2772 Config cfg2
, oldcfg
;
2775 cfg2
= inst
->cfg
; /* structure copy */
2777 if (do_config_box(title
, &cfg2
, 1)) {
2779 oldcfg
= inst
->cfg
; /* structure copy */
2780 inst
->cfg
= cfg2
; /* structure copy */
2782 /* Pass new config data to the logging module */
2783 log_reconfig(inst
->logctx
, &cfg2
);
2785 * Flush the line discipline's edit buffer in the case
2786 * where local editing has just been disabled.
2788 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2789 /* Pass new config data to the terminal */
2790 term_reconfig(inst
->term
, &cfg2
);
2791 /* Pass new config data to the back end */
2792 inst
->back
->reconfig(inst
->backhandle
, &cfg2
);
2795 * Just setting inst->cfg is sufficient to cause colour
2796 * setting changes to appear on the next ESC]R palette
2797 * reset. But we should also check whether any colour
2798 * settings have been changed, and revert the ones that
2799 * have to the new default, on the assumption that the user
2800 * is most likely to want an immediate update.
2802 for (i
= 0; i
< NCOLOURS
; i
++) {
2803 if (oldcfg
.colours
[ww
[i
]][0] != cfg2
.colours
[ww
[i
]][0] ||
2804 oldcfg
.colours
[ww
[i
]][1] != cfg2
.colours
[ww
[i
]][1] ||
2805 oldcfg
.colours
[ww
[i
]][2] != cfg2
.colours
[ww
[i
]][2]) {
2806 real_palette_set(inst
, i
, cfg2
.colours
[ww
[i
]][0],
2807 cfg2
.colours
[ww
[i
]][1],
2808 cfg2
.colours
[ww
[i
]][2]);
2811 * If the default background has changed, we must
2812 * repaint the space in between the window border
2813 * and the text area.
2816 set_window_background(inst
);
2817 draw_backing_rect(inst
);
2823 * If the scrollbar needs to be shown, hidden, or moved
2824 * from one end to the other of the window, do so now.
2826 if (oldcfg
.scrollbar
!= cfg2
.scrollbar
) {
2828 gtk_widget_show(inst
->sbar
);
2830 gtk_widget_hide(inst
->sbar
);
2832 if (oldcfg
.scrollbar_on_left
!= cfg2
.scrollbar_on_left
) {
2833 gtk_box_reorder_child(inst
->hbox
, inst
->sbar
,
2834 cfg2
.scrollbar_on_left ?
0 : 1);
2838 * Change the window title, if required.
2840 if (strcmp(oldcfg
.wintitle
, cfg2
.wintitle
))
2841 set_title(inst
, cfg2
.wintitle
);
2842 set_window_titles(inst
);
2845 * Redo the whole tangled fonts and Unicode mess if
2848 if (strcmp(oldcfg
.font
.name
, cfg2
.font
.name
) ||
2849 strcmp(oldcfg
.boldfont
.name
, cfg2
.boldfont
.name
) ||
2850 strcmp(oldcfg
.widefont
.name
, cfg2
.widefont
.name
) ||
2851 strcmp(oldcfg
.wideboldfont
.name
, cfg2
.wideboldfont
.name
) ||
2852 strcmp(oldcfg
.line_codepage
, cfg2
.line_codepage
) ||
2853 oldcfg
.vtmode
!= cfg2
.vtmode
||
2854 oldcfg
.shadowbold
!= cfg2
.shadowbold
) {
2855 setup_fonts_ucs(inst
);
2861 * Resize the window.
2863 if (oldcfg
.width
!= cfg2
.width
|| oldcfg
.height
!= cfg2
.height
||
2864 oldcfg
.window_border
!= cfg2
.window_border
|| need_size
) {
2865 set_geom_hints(inst
);
2866 request_resize(inst
, cfg2
.width
, cfg2
.height
);
2869 * The above will have caused a call to term_size() for
2870 * us if it happened. If the user has fiddled with only
2871 * the scrollback size, the above will not have
2872 * happened and we will need an explicit term_size()
2875 if (oldcfg
.savelines
!= cfg2
.savelines
)
2876 term_size(inst
->term
, inst
->term
->rows
, inst
->term
->cols
,
2880 term_invalidate(inst
->term
);
2883 * We do an explicit full redraw here to ensure the window
2884 * border has been redrawn as well as the text area.
2886 gtk_widget_queue_draw(inst
->area
);
2891 void fork_and_exec_self(struct gui_data
*inst
, int fd_to_close
, ...)
2894 * Re-execing ourself is not an exact science under Unix. I do
2895 * the best I can by using /proc/self/exe if available and by
2896 * assuming argv[0] can be found on $PATH if not.
2898 * Note that we also have to reconstruct the elements of the
2899 * original argv which gtk swallowed, since the user wants the
2900 * new session to appear on the same X display as the old one.
2908 * Collect the arguments with which to re-exec ourself.
2910 va_start(ap
, fd_to_close
);
2911 n
= 2; /* progname and terminating NULL */
2912 n
+= inst
->ngtkargs
;
2913 while (va_arg(ap
, char *) != NULL
)
2917 args
= snewn(n
, char *);
2918 args
[0] = inst
->progname
;
2920 for (i
= 0; i
< inst
->ngtkargs
; i
++)
2921 args
[i
+1] = inst
->gtkargvstart
[i
];
2924 va_start(ap
, fd_to_close
);
2925 while ((args
[i
++] = va_arg(ap
, char *)) != NULL
);
2931 * Do the double fork.
2944 } else if (pid2
> 0) {
2946 * First child has successfully forked second child. My
2947 * Work Here Is Done. Note the use of _exit rather than
2948 * exit: the latter appears to cause destroy messages
2949 * to be sent to the X server. I suspect gtk uses
2956 * If we reach here, we are the second child, so we now
2957 * actually perform the exec.
2959 if (fd_to_close
>= 0)
2962 execv("/proc/self/exe", args
);
2963 execvp(inst
->progname
, args
);
2969 waitpid(pid
, &status
, 0);
2974 void dup_session_menuitem(GtkMenuItem
*item
, gpointer gdata
)
2976 struct gui_data
*inst
= (struct gui_data
*)gdata
;
2978 * For this feature we must marshal cfg and (possibly) pty_argv
2979 * into a byte stream, create a pipe, and send this byte stream
2980 * to the child through the pipe.
2987 if (pipe(pipefd
) < 0) {
2992 size
= sizeof(inst
->cfg
);
2993 if (use_pty_argv
&& pty_argv
) {
2994 for (i
= 0; pty_argv
[i
]; i
++)
2995 size
+= strlen(pty_argv
[i
]) + 1;
2998 data
= snewn(size
, char);
2999 memcpy(data
, &inst
->cfg
, sizeof(inst
->cfg
));
3000 if (use_pty_argv
&& pty_argv
) {
3001 int p
= sizeof(inst
->cfg
);
3002 for (i
= 0; pty_argv
[i
]; i
++) {
3003 strcpy(data
+ p
, pty_argv
[i
]);
3004 p
+= strlen(pty_argv
[i
]) + 1;
3009 sprintf(option
, "---[%d,%d]", pipefd
[0], size
);
3010 fcntl(pipefd
[0], F_SETFD
, 0);
3011 fork_and_exec_self(inst
, pipefd
[1], option
, NULL
);
3015 while (i
< size
&& (ret
= write(pipefd
[1], data
+ i
, size
- i
)) > 0)
3018 perror("write to pipe");
3023 int read_dupsession_data(struct gui_data
*inst
, Config
*cfg
, char *arg
)
3025 int fd
, i
, ret
, size
;
3028 if (sscanf(arg
, "---[%d,%d]", &fd
, &size
) != 2) {
3029 fprintf(stderr
, "%s: malformed magic argument `%s'\n", appname
, arg
);
3033 data
= snewn(size
, char);
3035 while (i
< size
&& (ret
= read(fd
, data
+ i
, size
- i
)) > 0)
3038 perror("read from pipe");
3040 } else if (i
< size
) {
3041 fprintf(stderr
, "%s: unexpected EOF in Duplicate Session data\n",
3046 memcpy(cfg
, data
, sizeof(Config
));
3047 if (use_pty_argv
&& size
> sizeof(Config
)) {
3051 while (i
< size
&& data
[i
]) i
++;
3053 fprintf(stderr
, "%s: malformed Duplicate Session data\n",
3060 pty_argv
= snewn(n
+1, char *);
3066 while (i
< size
&& data
[i
]) i
++;
3069 pty_argv
[n
++] = dupstr(p
);
3076 void new_session_menuitem(GtkMenuItem
*item
, gpointer data
)
3078 struct gui_data
*inst
= (struct gui_data
*)data
;
3080 fork_and_exec_self(inst
, -1, NULL
);
3083 void saved_session_menuitem(GtkMenuItem
*item
, gpointer data
)
3085 struct gui_data
*inst
= (struct gui_data
*)data
;
3086 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
3088 fork_and_exec_self(inst
, -1, "-load", str
, NULL
);
3091 void saved_session_freedata(GtkMenuItem
*item
, gpointer data
)
3093 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
3098 void update_specials_menu(void *frontend
)
3100 struct gui_data
*inst
= (struct gui_data
*)frontend
;
3102 const struct telnet_special
*specials
;
3104 specials
= inst
->back
->get_specials(inst
->backhandle
);
3105 gtk_container_foreach(GTK_CONTAINER(inst
->specialsmenu
),
3106 (GtkCallback
)gtk_widget_destroy
, NULL
);
3109 GtkWidget
*menuitem
;
3110 for (i
= 0; specials
[i
].name
; i
++) {
3111 if (*specials
[i
].name
) {
3112 menuitem
= gtk_menu_item_new_with_label(specials
[i
].name
);
3113 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
3114 GINT_TO_POINTER(specials
[i
].code
));
3115 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
3116 GTK_SIGNAL_FUNC(special_menuitem
), inst
);
3118 menuitem
= gtk_menu_item_new();
3119 gtk_container_add(GTK_CONTAINER(inst
->specialsmenu
), menuitem
);
3120 gtk_widget_show(menuitem
);
3122 gtk_widget_show(inst
->specialsitem1
);
3123 gtk_widget_show(inst
->specialsitem2
);
3125 gtk_widget_hide(inst
->specialsitem1
);
3126 gtk_widget_hide(inst
->specialsitem2
);
3130 int pt_main(int argc
, char **argv
)
3132 extern Backend
*select_backend(Config
*cfg
);
3133 extern int cfgbox(Config
*cfg
);
3134 struct gui_data
*inst
;
3137 * Create an instance structure and initialise to zeroes
3139 inst
= snew(struct gui_data
);
3140 memset(inst
, 0, sizeof(*inst
));
3141 inst
->alt_keycode
= -1; /* this one needs _not_ to be zero */
3143 /* defer any child exit handling until we're ready to deal with
3145 block_signal(SIGCHLD
, 1);
3147 inst
->progname
= argv
[0];
3149 * Copy the original argv before letting gtk_init fiddle with
3150 * it. It will be required later.
3154 inst
->gtkargvstart
= snewn(argc
-1, char *);
3155 for (i
= 1; i
< argc
; i
++)
3156 inst
->gtkargvstart
[i
-1] = dupstr(argv
[i
]);
3158 gtk_init(&argc
, &argv
);
3159 inst
->ngtkargs
= oldargc
- argc
;
3162 if (argc
> 1 && !strncmp(argv
[1], "---", 3)) {
3163 read_dupsession_data(inst
, &inst
->cfg
, argv
[1]);
3164 /* Splatter this argument so it doesn't clutter a ps listing */
3165 memset(argv
[1], 0, strlen(argv
[1]));
3167 if (do_cmdline(argc
, argv
, 0, inst
, &inst
->cfg
))
3168 exit(1); /* pre-defaults pass to get -class */
3169 do_defaults(NULL
, &inst
->cfg
);
3170 if (do_cmdline(argc
, argv
, 1, inst
, &inst
->cfg
))
3171 exit(1); /* post-defaults, do everything */
3173 cmdline_run_saved(&inst
->cfg
);
3175 if (!*inst
->cfg
.host
&& !cfgbox(&inst
->cfg
))
3176 exit(0); /* config box hit Cancel */
3179 if (!compound_text_atom
)
3180 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
3181 if (!utf8_string_atom
)
3182 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
3184 setup_fonts_ucs(inst
);
3187 inst
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
3190 * Set up the colour map.
3192 palette_reset(inst
);
3194 inst
->width
= inst
->cfg
.width
;
3195 inst
->height
= inst
->cfg
.height
;
3197 inst
->area
= gtk_drawing_area_new();
3198 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
3199 inst
->font_width
* inst
->cfg
.width
+ 2*inst
->cfg
.window_border
,
3200 inst
->font_height
* inst
->cfg
.height
+ 2*inst
->cfg
.window_border
);
3201 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3202 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
3203 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
3205 * We always create the scrollbar; it remains invisible if
3206 * unwanted, so we can pop it up quickly if it suddenly becomes
3209 if (inst
->cfg
.scrollbar_on_left
)
3210 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
3211 gtk_box_pack_start(inst
->hbox
, inst
->area
, TRUE
, TRUE
, 0);
3212 if (!inst
->cfg
.scrollbar_on_left
)
3213 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
3215 gtk_container_add(GTK_CONTAINER(inst
->window
), GTK_WIDGET(inst
->hbox
));
3217 set_geom_hints(inst
);
3219 gtk_widget_show(inst
->area
);
3220 if (inst
->cfg
.scrollbar
)
3221 gtk_widget_show(inst
->sbar
);
3223 gtk_widget_hide(inst
->sbar
);
3224 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
3227 int x
= inst
->xpos
, y
= inst
->ypos
;
3229 gtk_widget_size_request(GTK_WIDGET(inst
->window
), &req
);
3230 if (inst
->gravity
& 1) x
+= gdk_screen_width() - req
.width
;
3231 if (inst
->gravity
& 2) y
+= gdk_screen_height() - req
.height
;
3232 gtk_window_set_position(GTK_WINDOW(inst
->window
), GTK_WIN_POS_NONE
);
3233 gtk_widget_set_uposition(GTK_WIDGET(inst
->window
), x
, y
);
3236 gtk_signal_connect(GTK_OBJECT(inst
->window
), "destroy",
3237 GTK_SIGNAL_FUNC(destroy
), inst
);
3238 gtk_signal_connect(GTK_OBJECT(inst
->window
), "delete_event",
3239 GTK_SIGNAL_FUNC(delete_window
), inst
);
3240 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_press_event",
3241 GTK_SIGNAL_FUNC(key_event
), inst
);
3242 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_release_event",
3243 GTK_SIGNAL_FUNC(key_event
), inst
);
3244 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_in_event",
3245 GTK_SIGNAL_FUNC(focus_event
), inst
);
3246 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_out_event",
3247 GTK_SIGNAL_FUNC(focus_event
), inst
);
3248 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
3249 GTK_SIGNAL_FUNC(configure_area
), inst
);
3250 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
3251 GTK_SIGNAL_FUNC(expose_area
), inst
);
3252 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
3253 GTK_SIGNAL_FUNC(button_event
), inst
);
3254 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
3255 GTK_SIGNAL_FUNC(button_event
), inst
);
3256 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
3257 GTK_SIGNAL_FUNC(motion_event
), inst
);
3258 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
3259 GTK_SIGNAL_FUNC(selection_received
), inst
);
3260 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
3261 GTK_SIGNAL_FUNC(selection_get
), inst
);
3262 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
3263 GTK_SIGNAL_FUNC(selection_clear
), inst
);
3264 if (inst
->cfg
.scrollbar
)
3265 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
3266 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
3267 gtk_timeout_add(20, timer_func
, inst
);
3268 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
3269 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
3270 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
3271 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_MOTION_MASK
);
3273 gtk_widget_show(inst
->window
);
3275 set_window_background(inst
);
3278 * Set up the Ctrl+rightclick context menu.
3281 GtkWidget
*menuitem
;
3283 extern const int use_event_log
, new_session
, saved_sessions
;
3285 inst
->menu
= gtk_menu_new();
3287 #define MKMENUITEM(title, func) do { \
3288 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3289 gtk_menu_item_new(); \
3290 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3291 gtk_widget_show(menuitem); \
3293 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3294 GTK_SIGNAL_FUNC(func), inst); \
3297 MKMENUITEM("New Session", new_session_menuitem
);
3298 MKMENUITEM("Duplicate Session", dup_session_menuitem
);
3299 if (saved_sessions
) {
3300 struct sesslist sesslist
;
3303 inst
->sessionsmenu
= gtk_menu_new();
3305 get_sesslist(&sesslist
, TRUE
);
3306 for (i
= 1; i
< sesslist
.nsessions
; i
++) {
3307 menuitem
= gtk_menu_item_new_with_label(sesslist
.sessions
[i
]);
3308 gtk_container_add(GTK_CONTAINER(inst
->sessionsmenu
), menuitem
);
3309 gtk_widget_show(menuitem
);
3310 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
3311 dupstr(sesslist
.sessions
[i
]));
3312 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
3313 GTK_SIGNAL_FUNC(saved_session_menuitem
),
3315 gtk_signal_connect(GTK_OBJECT(menuitem
), "destroy",
3316 GTK_SIGNAL_FUNC(saved_session_freedata
),
3319 get_sesslist(&sesslist
, FALSE
);
3321 MKMENUITEM("Saved Sessions", NULL
);
3322 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
),
3323 inst
->sessionsmenu
);
3325 MKMENUITEM(NULL
, NULL
);
3326 MKMENUITEM("Change Settings", change_settings_menuitem
);
3327 MKMENUITEM(NULL
, NULL
);
3329 MKMENUITEM("Event Log", event_log_menuitem
);
3330 MKMENUITEM("Special Commands", NULL
);
3331 inst
->specialsmenu
= gtk_menu_new();
3332 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), inst
->specialsmenu
);
3333 inst
->specialsitem1
= menuitem
;
3334 MKMENUITEM(NULL
, NULL
);
3335 inst
->specialsitem2
= menuitem
;
3336 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem
);
3337 MKMENUITEM("Reset Terminal", reset_terminal_menuitem
);
3338 MKMENUITEM("Copy All", copy_all_menuitem
);
3339 MKMENUITEM(NULL
, NULL
);
3340 s
= dupcat("About ", appname
, NULL
);
3341 MKMENUITEM(s
, about_menuitem
);
3346 inst
->textcursor
= make_mouse_ptr(inst
, GDK_XTERM
);
3347 inst
->rawcursor
= make_mouse_ptr(inst
, GDK_LEFT_PTR
);
3348 inst
->blankcursor
= make_mouse_ptr(inst
, -1);
3349 make_mouse_ptr(inst
, -2); /* clean up cursor font */
3350 inst
->currcursor
= inst
->textcursor
;
3351 show_mouseptr(inst
, 1);
3353 inst
->eventlogstuff
= eventlogstuff_new();
3355 inst
->term
= term_init(&inst
->cfg
, &inst
->ucsdata
, inst
);
3356 inst
->logctx
= log_init(inst
, &inst
->cfg
);
3357 term_provide_logctx(inst
->term
, inst
->logctx
);
3361 term_size(inst
->term
, inst
->cfg
.height
, inst
->cfg
.width
, inst
->cfg
.savelines
);
3363 inst
->back
= select_backend(&inst
->cfg
);
3368 error
= inst
->back
->init((void *)inst
, &inst
->backhandle
,
3369 &inst
->cfg
, inst
->cfg
.host
, inst
->cfg
.port
,
3370 &realhost
, inst
->cfg
.tcp_nodelay
);
3373 char *msg
= dupprintf("Unable to open connection to %s:\n%s",
3374 inst
->cfg
.host
, error
);
3375 inst
->exited
= TRUE
;
3376 fatal_message_box(inst
->window
, msg
);
3381 if (inst
->cfg
.wintitle
[0]) {
3382 set_title(inst
, inst
->cfg
.wintitle
);
3383 set_icon(inst
, inst
->cfg
.wintitle
);
3385 char *title
= make_default_wintitle(realhost
);
3386 set_title(inst
, title
);
3387 set_icon(inst
, title
);
3391 inst
->back
->provide_logctx(inst
->backhandle
, inst
->logctx
);
3392 update_specials_menu(inst
);
3394 term_provide_resize_fn(inst
->term
, inst
->back
->size
, inst
->backhandle
);
3397 ldisc_create(&inst
->cfg
, inst
->term
, inst
->back
, inst
->backhandle
, inst
);
3398 ldisc_send(inst
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
3400 /* now we're reday to deal with the child exit handler being
3402 block_signal(SIGCHLD
, 0);
3405 * Block SIGPIPE: if we attempt Duplicate Session or similar
3406 * and it falls over in some way, we certainly don't want
3407 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3408 * this _after_ (at least pterm) forks off its child process,
3409 * since the child wants SIGPIPE handled in the usual way.
3411 block_signal(SIGPIPE
, 1);
3413 inst
->exited
= FALSE
;