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
,
49 GtkWidget
*sessionsmenu
;
51 GdkFont
*fonts
[4]; /* normal, bold, wide, widebold */
56 int xpos
, ypos
, gotpos
, gravity
;
57 GdkCursor
*rawcursor
, *textcursor
, *blankcursor
, *currcursor
;
58 GdkColor cols
[NCOLOURS
];
60 wchar_t *pastein_data
;
63 char *pasteout_data
, *pasteout_data_ctext
, *pasteout_data_utf8
;
64 int pasteout_data_len
, pasteout_data_ctext_len
, pasteout_data_utf8_len
;
65 int font_width
, font_height
;
69 guint term_paste_idle_id
;
72 char wintitle
[sizeof(((Config
*)0)->wintitle
)];
73 char icontitle
[sizeof(((Config
*)0)->wintitle
)];
74 int master_fd
, master_func_id
;
81 struct unicode_data ucsdata
;
84 char *progname
, **gtkargvstart
;
86 guint32 input_event_time
; /* Timestamp of the most recent input event. */
91 struct gui_data
*inst
;
94 static int send_raw_mouse
;
96 static char *app_name
= "pterm";
98 static void start_backend(struct gui_data
*inst
);
100 char *x_get_default(const char *key
)
102 return XGetDefault(GDK_DISPLAY(), app_name
, key
);
105 void connection_fatal(void *frontend
, char *p
, ...)
107 struct gui_data
*inst
= (struct gui_data
*)frontend
;
112 msg
= dupvprintf(p
, ap
);
115 fatal_message_box(inst
->window
, msg
);
117 if (inst
->cfg
.close_on_exit
== FORCE_ON
)
122 * Default settings that are specific to pterm.
124 FontSpec
platform_default_fontspec(const char *name
)
127 if (!strcmp(name
, "Font"))
128 strcpy(ret
.name
, "fixed");
134 Filename
platform_default_filename(const char *name
)
137 if (!strcmp(name
, "LogFileName"))
138 strcpy(ret
.path
, "putty.log");
144 char *platform_default_s(const char *name
)
149 int platform_default_i(const char *name
, int def
)
151 if (!strcmp(name
, "CloseOnExit"))
152 return 2; /* maps to FORCE_ON after painful rearrangement :-( */
153 if (!strcmp(name
, "WinNameAlways"))
154 return 0; /* X natively supports icon titles, so use 'em by default */
158 void ldisc_update(void *frontend
, int echo
, int edit
)
161 * This is a stub in pterm. If I ever produce a Unix
162 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
163 * then it will require some termios manoeuvring analogous to
164 * that in the Windows plink.c, but here it's meaningless.
168 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
170 struct gui_data
*inst
= (struct gui_data
*)frontend
;
171 return term_data(inst
->term
, is_stderr
, data
, len
);
174 void logevent(void *frontend
, const char *string
)
176 struct gui_data
*inst
= (struct gui_data
*)frontend
;
178 log_eventlog(inst
->logctx
, string
);
180 logevent_dlg(inst
->eventlogstuff
, string
);
183 int font_dimension(void *frontend
, int which
)/* 0 for width, 1 for height */
185 struct gui_data
*inst
= (struct gui_data
*)frontend
;
188 return inst
->font_height
;
190 return inst
->font_width
;
194 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
195 * into a cooked one (SELECT, EXTEND, PASTE).
197 * In Unix, this is not configurable; the X button arrangement is
198 * rock-solid across all applications, everyone has a three-button
199 * mouse or a means of faking it, and there is no need to switch
200 * buttons around at all.
202 static Mouse_Button
translate_button(Mouse_Button button
)
204 /* struct gui_data *inst = (struct gui_data *)frontend; */
206 if (button
== MBT_LEFT
)
208 if (button
== MBT_MIDDLE
)
210 if (button
== MBT_RIGHT
)
212 return 0; /* shouldn't happen */
216 * Return the top-level GtkWindow associated with a particular
217 * front end instance.
219 void *get_window(void *frontend
)
221 struct gui_data
*inst
= (struct gui_data
*)frontend
;
226 * Minimise or restore the window in response to a server-side
229 void set_iconic(void *frontend
, int iconic
)
232 * GTK 1.2 doesn't know how to do this.
234 #if GTK_CHECK_VERSION(2,0,0)
235 struct gui_data
*inst
= (struct gui_data
*)frontend
;
237 gtk_window_iconify(GTK_WINDOW(inst
->window
));
239 gtk_window_deiconify(GTK_WINDOW(inst
->window
));
244 * Move the window in response to a server-side request.
246 void move_window(void *frontend
, int x
, int y
)
248 struct gui_data
*inst
= (struct gui_data
*)frontend
;
250 * I assume that when the GTK version of this call is available
251 * we should use it. Not sure how it differs from the GDK one,
254 #if GTK_CHECK_VERSION(2,0,0)
255 gtk_window_move(GTK_WINDOW(inst
->window
), x
, y
);
257 gdk_window_move(inst
->window
->window
, x
, y
);
262 * Move the window to the top or bottom of the z-order in response
263 * to a server-side request.
265 void set_zorder(void *frontend
, int top
)
267 struct gui_data
*inst
= (struct gui_data
*)frontend
;
269 gdk_window_raise(inst
->window
->window
);
271 gdk_window_lower(inst
->window
->window
);
275 * Refresh the window in response to a server-side request.
277 void refresh_window(void *frontend
)
279 struct gui_data
*inst
= (struct gui_data
*)frontend
;
280 term_invalidate(inst
->term
);
284 * Maximise or restore the window in response to a server-side
287 void set_zoomed(void *frontend
, int zoomed
)
290 * GTK 1.2 doesn't know how to do this.
292 #if GTK_CHECK_VERSION(2,0,0)
293 struct gui_data
*inst
= (struct gui_data
*)frontend
;
295 gtk_window_maximize(GTK_WINDOW(inst
->window
));
297 gtk_window_unmaximize(GTK_WINDOW(inst
->window
));
302 * Report whether the window is iconic, for terminal reports.
304 int is_iconic(void *frontend
)
306 struct gui_data
*inst
= (struct gui_data
*)frontend
;
307 return !gdk_window_is_viewable(inst
->window
->window
);
311 * Report the window's position, for terminal reports.
313 void get_window_pos(void *frontend
, int *x
, int *y
)
315 struct gui_data
*inst
= (struct gui_data
*)frontend
;
317 * I assume that when the GTK version of this call is available
318 * we should use it. Not sure how it differs from the GDK one,
321 #if GTK_CHECK_VERSION(2,0,0)
322 gtk_window_get_position(GTK_WINDOW(inst
->window
), x
, y
);
324 gdk_window_get_position(inst
->window
->window
, x
, y
);
329 * Report the window's pixel size, for terminal reports.
331 void get_window_pixels(void *frontend
, int *x
, int *y
)
333 struct gui_data
*inst
= (struct gui_data
*)frontend
;
335 * I assume that when the GTK version of this call is available
336 * we should use it. Not sure how it differs from the GDK one,
339 #if GTK_CHECK_VERSION(2,0,0)
340 gtk_window_get_size(GTK_WINDOW(inst
->window
), x
, y
);
342 gdk_window_get_size(inst
->window
->window
, x
, y
);
347 * Return the window or icon title.
349 char *get_window_title(void *frontend
, int icon
)
351 struct gui_data
*inst
= (struct gui_data
*)frontend
;
352 return icon ? inst
->icontitle
: inst
->wintitle
;
355 gint
delete_window(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
357 struct gui_data
*inst
= (struct gui_data
*)data
;
358 if (!inst
->exited
&& inst
->cfg
.warn_on_close
) {
359 if (!reallyclose(inst
))
365 static void show_mouseptr(struct gui_data
*inst
, int show
)
367 if (!inst
->cfg
.hide_mouseptr
)
370 gdk_window_set_cursor(inst
->area
->window
, inst
->currcursor
);
372 gdk_window_set_cursor(inst
->area
->window
, inst
->blankcursor
);
373 inst
->mouseptr_visible
= show
;
376 void draw_backing_rect(struct gui_data
*inst
)
378 GdkGC
*gc
= gdk_gc_new(inst
->area
->window
);
379 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
380 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
381 inst
->cfg
.width
* inst
->font_width
+ 2*inst
->cfg
.window_border
,
382 inst
->cfg
.height
* inst
->font_height
+ 2*inst
->cfg
.window_border
);
386 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
388 struct gui_data
*inst
= (struct gui_data
*)data
;
389 int w
, h
, need_size
= 0;
392 * See if the terminal size has changed, in which case we must
393 * let the terminal know.
395 w
= (event
->width
- 2*inst
->cfg
.window_border
) / inst
->font_width
;
396 h
= (event
->height
- 2*inst
->cfg
.window_border
) / inst
->font_height
;
397 if (w
!= inst
->width
|| h
!= inst
->height
) {
398 inst
->cfg
.width
= inst
->width
= w
;
399 inst
->cfg
.height
= inst
->height
= h
;
404 gdk_pixmap_unref(inst
->pixmap
);
408 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
409 (inst
->cfg
.width
* inst
->font_width
+
410 2*inst
->cfg
.window_border
),
411 (inst
->cfg
.height
* inst
->font_height
+
412 2*inst
->cfg
.window_border
), -1);
414 draw_backing_rect(inst
);
416 if (need_size
&& inst
->term
) {
417 term_size(inst
->term
, h
, w
, inst
->cfg
.savelines
);
421 term_invalidate(inst
->term
);
426 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
428 struct gui_data
*inst
= (struct gui_data
*)data
;
431 * Pass the exposed rectangle to terminal.c, which will call us
432 * back to do the actual painting.
435 gdk_draw_pixmap(widget
->window
,
436 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
438 event
->area
.x
, event
->area
.y
,
439 event
->area
.x
, event
->area
.y
,
440 event
->area
.width
, event
->area
.height
);
445 #define KEY_PRESSED(k) \
446 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
448 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
450 struct gui_data
*inst
= (struct gui_data
*)data
;
452 wchar_t ucsoutput
[2];
453 int ucsval
, start
, end
, special
, use_ucsoutput
;
455 /* Remember the timestamp. */
456 inst
->input_event_time
= event
->time
;
458 /* By default, nothing is generated. */
460 special
= use_ucsoutput
= FALSE
;
463 * If Alt is being released after typing an Alt+numberpad
464 * sequence, we should generate the code that was typed.
466 * Note that we only do this if more than one key was actually
467 * pressed - I don't think Alt+NumPad4 should be ^D or that
468 * Alt+NumPad3 should be ^C, for example. There's no serious
469 * inconvenience in having to type a zero before a single-digit
472 if (event
->type
== GDK_KEY_RELEASE
&&
473 (event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
474 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
) &&
475 inst
->alt_keycode
>= 0 && inst
->alt_digits
> 1) {
477 printf("Alt key up, keycode = %d\n", inst
->alt_keycode
);
479 output
[0] = inst
->alt_keycode
;
484 if (event
->type
== GDK_KEY_PRESS
) {
488 printf("keypress: keyval = %04x, state = %08x; string =",
489 event
->keyval
, event
->state
);
490 for (i
= 0; event
->string
[i
]; i
++)
491 printf(" %02x", (unsigned char) event
->string
[i
]);
497 * NYI: Compose key (!!! requires Unicode faff before even trying)
501 * If Alt has just been pressed, we start potentially
502 * accumulating an Alt+numberpad code. We do this by
503 * setting alt_keycode to -1 (nothing yet but plausible).
505 if ((event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
506 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
)) {
507 inst
->alt_keycode
= -1;
508 inst
->alt_digits
= 0;
509 goto done
; /* this generates nothing else */
513 * If we're seeing a numberpad key press with Mod1 down,
514 * consider adding it to alt_keycode if that's sensible.
515 * Anything _else_ with Mod1 down cancels any possibility
516 * of an ALT keycode: we set alt_keycode to -2.
518 if ((event
->state
& GDK_MOD1_MASK
) && inst
->alt_keycode
!= -2) {
520 switch (event
->keyval
) {
521 case GDK_KP_0
: case GDK_KP_Insert
: digit
= 0; break;
522 case GDK_KP_1
: case GDK_KP_End
: digit
= 1; break;
523 case GDK_KP_2
: case GDK_KP_Down
: digit
= 2; break;
524 case GDK_KP_3
: case GDK_KP_Page_Down
: digit
= 3; break;
525 case GDK_KP_4
: case GDK_KP_Left
: digit
= 4; break;
526 case GDK_KP_5
: case GDK_KP_Begin
: digit
= 5; break;
527 case GDK_KP_6
: case GDK_KP_Right
: digit
= 6; break;
528 case GDK_KP_7
: case GDK_KP_Home
: digit
= 7; break;
529 case GDK_KP_8
: case GDK_KP_Up
: digit
= 8; break;
530 case GDK_KP_9
: case GDK_KP_Page_Up
: digit
= 9; break;
533 inst
->alt_keycode
= -2; /* it's invalid */
536 printf("Adding digit %d to keycode %d", digit
,
539 if (inst
->alt_keycode
== -1)
540 inst
->alt_keycode
= digit
; /* one-digit code */
542 inst
->alt_keycode
= inst
->alt_keycode
* 10 + digit
;
545 printf(" gives new code %d\n", inst
->alt_keycode
);
547 /* Having used this digit, we now do nothing more with it. */
553 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
556 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
557 term_scroll(inst
->term
, 0, -inst
->cfg
.height
/2);
560 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_CONTROL_MASK
)) {
561 term_scroll(inst
->term
, 0, -1);
564 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
565 term_scroll(inst
->term
, 0, +inst
->cfg
.height
/2);
568 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_CONTROL_MASK
)) {
569 term_scroll(inst
->term
, 0, +1);
574 * Neither does Shift-Ins.
576 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
582 use_ucsoutput
= FALSE
;
584 /* ALT+things gives leading Escape. */
586 strncpy(output
+1, event
->string
, 31);
587 if (!*event
->string
&&
588 (ucsval
= keysym_to_unicode(event
->keyval
)) >= 0) {
589 ucsoutput
[0] = '\033';
590 ucsoutput
[1] = ucsval
;
591 use_ucsoutput
= TRUE
;
595 end
= strlen(output
);
597 if (event
->state
& GDK_MOD1_MASK
) {
599 if (end
== 1) end
= 0;
603 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
604 if (!event
->string
[0] && event
->keyval
== '`' &&
605 (event
->state
& GDK_CONTROL_MASK
)) {
607 use_ucsoutput
= FALSE
;
611 /* Control-Break is the same as Control-C */
612 if (event
->keyval
== GDK_Break
&&
613 (event
->state
& GDK_CONTROL_MASK
)) {
615 use_ucsoutput
= FALSE
;
620 /* We handle Return ourselves, because it needs to be flagged as
621 * special to ldisc. */
622 if (event
->keyval
== GDK_Return
) {
624 use_ucsoutput
= FALSE
;
629 /* Control-2, Control-Space and Control-@ are NUL */
630 if (!event
->string
[0] &&
631 (event
->keyval
== ' ' || event
->keyval
== '2' ||
632 event
->keyval
== '@') &&
633 (event
->state
& (GDK_SHIFT_MASK
|
634 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
636 use_ucsoutput
= FALSE
;
640 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
641 if (!event
->string
[0] && event
->keyval
== ' ' &&
642 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
643 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
645 use_ucsoutput
= FALSE
;
649 /* We don't let GTK tell us what Backspace is! We know better. */
650 if (event
->keyval
== GDK_BackSpace
&&
651 !(event
->state
& GDK_SHIFT_MASK
)) {
652 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x7F' : '\x08';
653 use_ucsoutput
= FALSE
;
657 /* For Shift Backspace, do opposite of what is configured. */
658 if (event
->keyval
== GDK_BackSpace
&&
659 (event
->state
& GDK_SHIFT_MASK
)) {
660 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x08' : '\x7F';
661 use_ucsoutput
= FALSE
;
666 /* Shift-Tab is ESC [ Z */
667 if (event
->keyval
== GDK_ISO_Left_Tab
||
668 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
669 end
= 1 + sprintf(output
+1, "\033[Z");
670 use_ucsoutput
= FALSE
;
674 * NetHack keypad mode.
676 if (inst
->cfg
.nethack_keypad
) {
678 switch (event
->keyval
) {
679 case GDK_KP_1
: case GDK_KP_End
: keys
= "bB"; break;
680 case GDK_KP_2
: case GDK_KP_Down
: keys
= "jJ"; break;
681 case GDK_KP_3
: case GDK_KP_Page_Down
: keys
= "nN"; break;
682 case GDK_KP_4
: case GDK_KP_Left
: keys
= "hH"; break;
683 case GDK_KP_5
: case GDK_KP_Begin
: keys
= ".."; break;
684 case GDK_KP_6
: case GDK_KP_Right
: keys
= "lL"; break;
685 case GDK_KP_7
: case GDK_KP_Home
: keys
= "yY"; break;
686 case GDK_KP_8
: case GDK_KP_Up
: keys
= "kK"; break;
687 case GDK_KP_9
: case GDK_KP_Page_Up
: keys
= "uU"; break;
691 if (event
->state
& GDK_SHIFT_MASK
)
695 use_ucsoutput
= FALSE
;
701 * Application keypad mode.
703 if (inst
->term
->app_keypad_keys
&& !inst
->cfg
.no_applic_k
) {
705 switch (event
->keyval
) {
706 case GDK_Num_Lock
: xkey
= 'P'; break;
707 case GDK_KP_Divide
: xkey
= 'Q'; break;
708 case GDK_KP_Multiply
: xkey
= 'R'; break;
709 case GDK_KP_Subtract
: xkey
= 'S'; break;
711 * Keypad + is tricky. It covers a space that would
712 * be taken up on the VT100 by _two_ keys; so we
713 * let Shift select between the two. Worse still,
714 * in xterm function key mode we change which two...
717 if (inst
->cfg
.funky_type
== FUNKY_XTERM
) {
718 if (event
->state
& GDK_SHIFT_MASK
)
722 } else if (event
->state
& GDK_SHIFT_MASK
)
727 case GDK_KP_Enter
: xkey
= 'M'; break;
728 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
729 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
730 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
731 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
732 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
733 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
734 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
735 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
736 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
737 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
738 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
741 if (inst
->term
->vt52_mode
) {
742 if (xkey
>= 'P' && xkey
<= 'S')
743 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
745 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
747 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
748 use_ucsoutput
= FALSE
;
754 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
755 * for integer decimal nn.)
757 * We also deal with the weird ones here. Linux VCs replace F1
758 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
759 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
764 switch (event
->keyval
) {
766 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
769 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
772 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
775 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
778 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
781 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
784 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
787 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
790 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
793 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
826 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
827 case GDK_Home
: case GDK_KP_Home
:
830 case GDK_Insert
: case GDK_KP_Insert
:
833 case GDK_Delete
: case GDK_KP_Delete
:
836 case GDK_End
: case GDK_KP_End
:
839 case GDK_Page_Up
: case GDK_KP_Page_Up
:
842 case GDK_Page_Down
: case GDK_KP_Page_Down
:
846 /* Reorder edit keys to physical order */
847 if (inst
->cfg
.funky_type
== FUNKY_VT400
&& code
<= 6)
848 code
= "\0\2\1\4\5\3\6"[code
];
850 if (inst
->term
->vt52_mode
&& code
> 0 && code
<= 6) {
851 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
852 use_ucsoutput
= FALSE
;
856 if (inst
->cfg
.funky_type
== FUNKY_SCO
&& /* SCO function keys */
857 code
>= 11 && code
<= 34) {
858 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
860 switch (event
->keyval
) {
861 case GDK_F1
: index
= 0; break;
862 case GDK_F2
: index
= 1; break;
863 case GDK_F3
: index
= 2; break;
864 case GDK_F4
: index
= 3; break;
865 case GDK_F5
: index
= 4; break;
866 case GDK_F6
: index
= 5; break;
867 case GDK_F7
: index
= 6; break;
868 case GDK_F8
: index
= 7; break;
869 case GDK_F9
: index
= 8; break;
870 case GDK_F10
: index
= 9; break;
871 case GDK_F11
: index
= 10; break;
872 case GDK_F12
: index
= 11; break;
874 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
875 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
876 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
877 use_ucsoutput
= FALSE
;
880 if (inst
->cfg
.funky_type
== FUNKY_SCO
&& /* SCO small keypad */
881 code
>= 1 && code
<= 6) {
882 char codes
[] = "HL.FIG";
887 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
889 use_ucsoutput
= FALSE
;
892 if ((inst
->term
->vt52_mode
|| inst
->cfg
.funky_type
== FUNKY_VT100P
) &&
893 code
>= 11 && code
<= 24) {
899 if (inst
->term
->vt52_mode
)
900 end
= 1 + sprintf(output
+1,
901 "\x1B%c", code
+ 'P' - 11 - offt
);
903 end
= 1 + sprintf(output
+1,
904 "\x1BO%c", code
+ 'P' - 11 - offt
);
905 use_ucsoutput
= FALSE
;
908 if (inst
->cfg
.funky_type
== FUNKY_LINUX
&& code
>= 11 && code
<= 15) {
909 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
910 use_ucsoutput
= FALSE
;
913 if (inst
->cfg
.funky_type
== FUNKY_XTERM
&& code
>= 11 && code
<= 14) {
914 if (inst
->term
->vt52_mode
)
915 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
917 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
918 use_ucsoutput
= FALSE
;
921 if (inst
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
922 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
923 use_ucsoutput
= FALSE
;
927 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
928 use_ucsoutput
= FALSE
;
934 * Cursor keys. (This includes the numberpad cursor keys,
935 * if we haven't already done them due to app keypad mode.)
937 * Here we also process un-numlocked un-appkeypadded KP5,
938 * which sends ESC [ G.
942 switch (event
->keyval
) {
943 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
944 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
945 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
946 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
947 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
951 * The arrow keys normally do ESC [ A and so on. In
952 * app cursor keys mode they do ESC O A instead.
953 * Ctrl toggles the two modes.
955 if (inst
->term
->vt52_mode
) {
956 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
957 } else if (!inst
->term
->app_cursor_keys
^
958 !(event
->state
& GDK_CONTROL_MASK
)) {
959 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
961 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
963 use_ucsoutput
= FALSE
;
975 printf("generating sequence:");
976 for (i
= start
; i
< end
; i
++)
977 printf(" %02x", (unsigned char) output
[i
]);
983 * For special control characters, the character set
984 * should never matter.
986 output
[end
] = '\0'; /* NUL-terminate */
988 ldisc_send(inst
->ldisc
, output
+start
, -2, 1);
989 } else if (!inst
->direct_to_font
) {
990 if (!use_ucsoutput
) {
992 * The stuff we've just generated is assumed to be
993 * ISO-8859-1! This sounds insane, but `man
994 * XLookupString' agrees: strings of this type
995 * returned from the X server are hardcoded to
996 * 8859-1. Strictly speaking we should be doing
997 * this using some sort of GtkIMContext, which (if
998 * we're lucky) would give us our data directly in
999 * Unicode; but that's not supported in GTK 1.2 as
1000 * far as I can tell, and it's poorly documented
1001 * even in 2.0, so it'll have to wait.
1004 lpage_send(inst
->ldisc
, CS_ISO8859_1
, output
+start
,
1008 * We generated our own Unicode key data from the
1009 * keysym, so use that instead.
1012 luni_send(inst
->ldisc
, ucsoutput
+start
, end
-start
, 1);
1016 * In direct-to-font mode, we just send the string
1017 * exactly as we received it.
1020 ldisc_send(inst
->ldisc
, output
+start
, end
-start
, 1);
1023 show_mouseptr(inst
, 0);
1024 term_seen_key_event(inst
->term
);
1025 term_out(inst
->term
);
1031 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
1033 struct gui_data
*inst
= (struct gui_data
*)data
;
1034 int shift
, ctrl
, alt
, x
, y
, button
, act
;
1036 /* Remember the timestamp. */
1037 inst
->input_event_time
= event
->time
;
1039 show_mouseptr(inst
, 1);
1041 if (event
->button
== 4 && event
->type
== GDK_BUTTON_PRESS
) {
1042 term_scroll(inst
->term
, 0, -5);
1045 if (event
->button
== 5 && event
->type
== GDK_BUTTON_PRESS
) {
1046 term_scroll(inst
->term
, 0, +5);
1050 shift
= event
->state
& GDK_SHIFT_MASK
;
1051 ctrl
= event
->state
& GDK_CONTROL_MASK
;
1052 alt
= event
->state
& GDK_MOD1_MASK
;
1054 if (event
->button
== 3 && ctrl
) {
1055 gtk_menu_popup(GTK_MENU(inst
->menu
), NULL
, NULL
, NULL
, NULL
,
1056 event
->button
, event
->time
);
1060 if (event
->button
== 1)
1062 else if (event
->button
== 2)
1063 button
= MBT_MIDDLE
;
1064 else if (event
->button
== 3)
1067 return FALSE
; /* don't even know what button! */
1069 switch (event
->type
) {
1070 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
1071 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
1072 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
1073 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
1074 default: return FALSE
; /* don't know this event type */
1077 if (send_raw_mouse
&& !(inst
->cfg
.mouse_override
&& shift
) &&
1078 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
1079 return TRUE
; /* we ignore these in raw mouse mode */
1081 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1082 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1084 term_mouse(inst
->term
, button
, translate_button(button
), act
,
1085 x
, y
, shift
, ctrl
, alt
);
1090 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
1092 struct gui_data
*inst
= (struct gui_data
*)data
;
1093 int shift
, ctrl
, alt
, x
, y
, button
;
1095 /* Remember the timestamp. */
1096 inst
->input_event_time
= event
->time
;
1098 show_mouseptr(inst
, 1);
1100 shift
= event
->state
& GDK_SHIFT_MASK
;
1101 ctrl
= event
->state
& GDK_CONTROL_MASK
;
1102 alt
= event
->state
& GDK_MOD1_MASK
;
1103 if (event
->state
& GDK_BUTTON1_MASK
)
1105 else if (event
->state
& GDK_BUTTON2_MASK
)
1106 button
= MBT_MIDDLE
;
1107 else if (event
->state
& GDK_BUTTON3_MASK
)
1110 return FALSE
; /* don't even know what button! */
1112 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
1113 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
1115 term_mouse(inst
->term
, button
, translate_button(button
), MA_DRAG
,
1116 x
, y
, shift
, ctrl
, alt
);
1121 void frontend_keypress(void *handle
)
1123 struct gui_data
*inst
= (struct gui_data
*)handle
;
1126 * If our child process has exited but not closed, terminate on
1133 gint
timer_func(gpointer data
)
1135 struct gui_data
*inst
= (struct gui_data
*)data
;
1138 if (!inst
->exited
&&
1139 (exitcode
= inst
->back
->exitcode(inst
->backhandle
)) >= 0) {
1140 inst
->exited
= TRUE
;
1141 if (inst
->cfg
.close_on_exit
== FORCE_ON
||
1142 (inst
->cfg
.close_on_exit
== AUTO
&& exitcode
== 0))
1143 exit(0); /* just go. */
1145 ldisc_free(inst
->ldisc
);
1149 inst
->back
->free(inst
->backhandle
);
1150 inst
->backhandle
= NULL
;
1152 update_specials_menu(inst
);
1154 gtk_widget_show(inst
->restartitem
);
1157 term_update(inst
->term
);
1158 term_blink(inst
->term
, 0);
1162 void fd_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
1165 * We must process exceptional notifications before ordinary
1166 * readability ones, or we may go straight past the urgent
1169 if (condition
& GDK_INPUT_EXCEPTION
)
1170 select_result(sourcefd
, 4);
1171 if (condition
& GDK_INPUT_READ
)
1172 select_result(sourcefd
, 1);
1173 if (condition
& GDK_INPUT_WRITE
)
1174 select_result(sourcefd
, 2);
1177 void destroy(GtkWidget
*widget
, gpointer data
)
1182 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
1184 struct gui_data
*inst
= (struct gui_data
*)data
;
1185 inst
->term
->has_focus
= event
->in
;
1186 term_out(inst
->term
);
1187 term_update(inst
->term
);
1188 show_mouseptr(inst
, 1);
1193 * set or clear the "raw mouse message" mode
1195 void set_raw_mouse_mode(void *frontend
, int activate
)
1197 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1198 activate
= activate
&& !inst
->cfg
.no_mouse_rep
;
1199 send_raw_mouse
= activate
;
1201 inst
->currcursor
= inst
->rawcursor
;
1203 inst
->currcursor
= inst
->textcursor
;
1204 show_mouseptr(inst
, inst
->mouseptr_visible
);
1207 void request_resize(void *frontend
, int w
, int h
)
1209 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1210 int large_x
, large_y
;
1211 int offset_x
, offset_y
;
1213 GtkRequisition inner
, outer
;
1216 * This is a heinous hack dreamed up by the gnome-terminal
1217 * people to get around a limitation in gtk. The problem is
1218 * that in order to set the size correctly we really need to be
1219 * calling gtk_window_resize - but that needs to know the size
1220 * of the _whole window_, not the drawing area. So what we do
1221 * is to set an artificially huge size request on the drawing
1222 * area, recompute the resulting size request on the window,
1223 * and look at the difference between the two. That gives us
1224 * the x and y offsets we need to translate drawing area size
1225 * into window size for real, and then we call
1226 * gtk_window_resize.
1230 * We start by retrieving the current size of the whole window.
1231 * Adding a bit to _that_ will give us a value we can use as a
1232 * bogus size request which guarantees to be bigger than the
1233 * current size of the drawing area.
1235 get_window_pixels(inst
, &large_x
, &large_y
);
1239 #if GTK_CHECK_VERSION(2,0,0)
1240 gtk_widget_set_size_request(inst
->area
, large_x
, large_y
);
1242 gtk_widget_set_usize(inst
->area
, large_x
, large_y
);
1244 gtk_widget_size_request(inst
->area
, &inner
);
1245 gtk_widget_size_request(inst
->window
, &outer
);
1247 offset_x
= outer
.width
- inner
.width
;
1248 offset_y
= outer
.height
- inner
.height
;
1250 area_x
= inst
->font_width
* w
+ 2*inst
->cfg
.window_border
;
1251 area_y
= inst
->font_height
* h
+ 2*inst
->cfg
.window_border
;
1254 * Now we must set the size request on the drawing area back to
1255 * something sensible before we commit the real resize. Best
1256 * way to do this, I think, is to set it to what the size is
1257 * really going to end up being.
1259 #if GTK_CHECK_VERSION(2,0,0)
1260 gtk_widget_set_size_request(inst
->area
, area_x
, area_y
);
1262 gtk_widget_set_usize(inst
->area
, area_x
, area_y
);
1263 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
), area_x
, area_y
);
1266 gtk_container_dequeue_resize_handler(GTK_CONTAINER(inst
->window
));
1268 #if GTK_CHECK_VERSION(2,0,0)
1269 gtk_window_resize(GTK_WINDOW(inst
->window
),
1270 area_x
+ offset_x
, area_y
+ offset_y
);
1272 gdk_window_resize(inst
->window
->window
,
1273 area_x
+ offset_x
, area_y
+ offset_y
);
1277 static void real_palette_set(struct gui_data
*inst
, int n
, int r
, int g
, int b
)
1279 gboolean success
[1];
1281 inst
->cols
[n
].red
= r
* 0x0101;
1282 inst
->cols
[n
].green
= g
* 0x0101;
1283 inst
->cols
[n
].blue
= b
* 0x0101;
1285 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
+ n
, 1);
1286 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
+ n
, 1,
1287 FALSE
, FALSE
, success
);
1289 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n", appname
,
1293 void set_window_background(struct gui_data
*inst
)
1295 if (inst
->area
&& inst
->area
->window
)
1296 gdk_window_set_background(inst
->area
->window
, &inst
->cols
[18]);
1297 if (inst
->window
&& inst
->window
->window
)
1298 gdk_window_set_background(inst
->window
->window
, &inst
->cols
[18]);
1301 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1303 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1304 static const int first
[21] = {
1305 0, 2, 4, 6, 8, 10, 12, 14,
1306 1, 3, 5, 7, 9, 11, 13, 15,
1309 real_palette_set(inst
, first
[n
], r
, g
, b
);
1311 real_palette_set(inst
, first
[n
] + 1, r
, g
, b
);
1313 set_window_background(inst
);
1316 void palette_reset(void *frontend
)
1318 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1319 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1320 static const int ww
[] = {
1321 6, 7, 8, 9, 10, 11, 12, 13,
1322 14, 15, 16, 17, 18, 19, 20, 21,
1325 gboolean success
[NCOLOURS
];
1328 assert(lenof(ww
) == NCOLOURS
);
1330 if (!inst
->colmap
) {
1331 inst
->colmap
= gdk_colormap_get_system();
1333 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
, NCOLOURS
);
1336 for (i
= 0; i
< NCOLOURS
; i
++) {
1337 inst
->cols
[i
].red
= inst
->cfg
.colours
[ww
[i
]][0] * 0x0101;
1338 inst
->cols
[i
].green
= inst
->cfg
.colours
[ww
[i
]][1] * 0x0101;
1339 inst
->cols
[i
].blue
= inst
->cfg
.colours
[ww
[i
]][2] * 0x0101;
1342 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
1343 FALSE
, FALSE
, success
);
1344 for (i
= 0; i
< NCOLOURS
; i
++) {
1346 g_error("%s: couldn't allocate colour %d (#%02x%02x%02x)\n",
1347 appname
, i
, inst
->cfg
.colours
[i
][0],
1348 inst
->cfg
.colours
[i
][1], inst
->cfg
.colours
[i
][2]);
1351 set_window_background(inst
);
1354 /* Ensure that all the cut buffers exist - according to the ICCCM, we must
1355 * do this before we start using cut buffers.
1357 void init_cutbuffers()
1359 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1360 XA_CUT_BUFFER0
, XA_STRING
, 8, PropModeAppend
, "", 0);
1361 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1362 XA_CUT_BUFFER1
, XA_STRING
, 8, PropModeAppend
, "", 0);
1363 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1364 XA_CUT_BUFFER2
, XA_STRING
, 8, PropModeAppend
, "", 0);
1365 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1366 XA_CUT_BUFFER3
, XA_STRING
, 8, PropModeAppend
, "", 0);
1367 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1368 XA_CUT_BUFFER4
, XA_STRING
, 8, PropModeAppend
, "", 0);
1369 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1370 XA_CUT_BUFFER5
, XA_STRING
, 8, PropModeAppend
, "", 0);
1371 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1372 XA_CUT_BUFFER6
, XA_STRING
, 8, PropModeAppend
, "", 0);
1373 XChangeProperty(GDK_DISPLAY(), GDK_ROOT_WINDOW(),
1374 XA_CUT_BUFFER7
, XA_STRING
, 8, PropModeAppend
, "", 0);
1377 /* Store the data in a cut-buffer. */
1378 void store_cutbuffer(char * ptr
, int len
)
1380 /* ICCCM says we must rotate the buffers before storing to buffer 0. */
1381 XRotateBuffers(GDK_DISPLAY(), 1);
1382 XStoreBytes(GDK_DISPLAY(), ptr
, len
);
1385 /* Retrieve data from a cut-buffer.
1386 * Returned data needs to be freed with XFree().
1388 char * retrieve_cutbuffer(int * nbytes
)
1391 ptr
= XFetchBytes(GDK_DISPLAY(), nbytes
);
1392 if (*nbytes
<= 0 && ptr
!= 0) {
1399 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
1401 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1402 if (inst
->pasteout_data
)
1403 sfree(inst
->pasteout_data
);
1404 if (inst
->pasteout_data_ctext
)
1405 sfree(inst
->pasteout_data_ctext
);
1406 if (inst
->pasteout_data_utf8
)
1407 sfree(inst
->pasteout_data_utf8
);
1410 * Set up UTF-8 and compound text paste data. This only happens
1411 * if we aren't in direct-to-font mode using the D800 hack.
1413 if (!inst
->direct_to_font
) {
1414 wchar_t *tmp
= data
;
1419 inst
->pasteout_data_utf8
= snewn(len
*6, char);
1420 inst
->pasteout_data_utf8_len
= len
*6;
1421 inst
->pasteout_data_utf8_len
=
1422 charset_from_unicode(&tmp
, &tmplen
, inst
->pasteout_data_utf8
,
1423 inst
->pasteout_data_utf8_len
,
1424 CS_UTF8
, NULL
, NULL
, 0);
1425 if (inst
->pasteout_data_utf8_len
== 0) {
1426 sfree(inst
->pasteout_data_utf8
);
1427 inst
->pasteout_data_utf8
= NULL
;
1429 inst
->pasteout_data_utf8
=
1430 sresize(inst
->pasteout_data_utf8
,
1431 inst
->pasteout_data_utf8_len
+ 1, char);
1432 inst
->pasteout_data_utf8
[inst
->pasteout_data_utf8_len
] = '\0';
1436 * Now let Xlib convert our UTF-8 data into compound text.
1438 list
[0] = inst
->pasteout_data_utf8
;
1439 if (Xutf8TextListToTextProperty(GDK_DISPLAY(), list
, 1,
1440 XCompoundTextStyle
, &tp
) == 0) {
1441 inst
->pasteout_data_ctext
= snewn(tp
.nitems
+1, char);
1442 memcpy(inst
->pasteout_data_ctext
, tp
.value
, tp
.nitems
);
1443 inst
->pasteout_data_ctext_len
= tp
.nitems
;
1446 inst
->pasteout_data_ctext
= NULL
;
1447 inst
->pasteout_data_ctext_len
= 0;
1450 inst
->pasteout_data_utf8
= NULL
;
1451 inst
->pasteout_data_utf8_len
= 0;
1452 inst
->pasteout_data_ctext
= NULL
;
1453 inst
->pasteout_data_ctext_len
= 0;
1456 inst
->pasteout_data
= snewn(len
*6, char);
1457 inst
->pasteout_data_len
= len
*6;
1458 inst
->pasteout_data_len
= wc_to_mb(inst
->ucsdata
.line_codepage
, 0,
1459 data
, len
, inst
->pasteout_data
,
1460 inst
->pasteout_data_len
,
1462 if (inst
->pasteout_data_len
== 0) {
1463 sfree(inst
->pasteout_data
);
1464 inst
->pasteout_data
= NULL
;
1466 inst
->pasteout_data
=
1467 sresize(inst
->pasteout_data
, inst
->pasteout_data_len
, char);
1470 store_cutbuffer(inst
->pasteout_data
, inst
->pasteout_data_len
);
1472 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
1473 inst
->input_event_time
)) {
1474 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1475 GDK_SELECTION_TYPE_STRING
, 1);
1476 if (inst
->pasteout_data_ctext
)
1477 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1478 compound_text_atom
, 1);
1479 if (inst
->pasteout_data_utf8
)
1480 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1481 utf8_string_atom
, 1);
1485 term_deselect(inst
->term
);
1488 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1489 guint info
, guint time_stamp
, gpointer data
)
1491 struct gui_data
*inst
= (struct gui_data
*)data
;
1492 if (seldata
->target
== utf8_string_atom
)
1493 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1494 inst
->pasteout_data_utf8
,
1495 inst
->pasteout_data_utf8_len
);
1496 else if (seldata
->target
== compound_text_atom
)
1497 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1498 inst
->pasteout_data_ctext
,
1499 inst
->pasteout_data_ctext_len
);
1501 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1502 inst
->pasteout_data
, inst
->pasteout_data_len
);
1505 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1508 struct gui_data
*inst
= (struct gui_data
*)data
;
1510 term_deselect(inst
->term
);
1511 if (inst
->pasteout_data
)
1512 sfree(inst
->pasteout_data
);
1513 if (inst
->pasteout_data_ctext
)
1514 sfree(inst
->pasteout_data_ctext
);
1515 if (inst
->pasteout_data_utf8
)
1516 sfree(inst
->pasteout_data_utf8
);
1517 inst
->pasteout_data
= NULL
;
1518 inst
->pasteout_data_len
= 0;
1519 inst
->pasteout_data_ctext
= NULL
;
1520 inst
->pasteout_data_ctext_len
= 0;
1521 inst
->pasteout_data_utf8
= NULL
;
1522 inst
->pasteout_data_utf8_len
= 0;
1526 void request_paste(void *frontend
)
1528 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1530 * In Unix, pasting is asynchronous: all we can do at the
1531 * moment is to call gtk_selection_convert(), and when the data
1532 * comes back _then_ we can call term_do_paste().
1535 if (!inst
->direct_to_font
) {
1537 * First we attempt to retrieve the selection as a UTF-8
1538 * string (which we will convert to the correct code page
1539 * before sending to the session, of course). If that
1540 * fails, selection_received() will be informed and will
1541 * fall back to an ordinary string.
1543 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1545 inst
->input_event_time
);
1548 * If we're in direct-to-font mode, we disable UTF-8
1549 * pasting, and go straight to ordinary string data.
1551 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1552 GDK_SELECTION_TYPE_STRING
,
1553 inst
->input_event_time
);
1557 gint
idle_paste_func(gpointer data
); /* forward ref */
1559 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1560 guint time
, gpointer data
)
1562 struct gui_data
*inst
= (struct gui_data
*)data
;
1566 int length
, count
, ret
;
1567 int free_list_required
= 0;
1568 int free_required
= 0;
1571 if (seldata
->target
== utf8_string_atom
&& seldata
->length
<= 0) {
1573 * Failed to get a UTF-8 selection string. Try compound
1576 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1578 inst
->input_event_time
);
1582 if (seldata
->target
== compound_text_atom
&& seldata
->length
<= 0) {
1584 * Failed to get UTF-8 or compound text. Try an ordinary
1587 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1588 GDK_SELECTION_TYPE_STRING
,
1589 inst
->input_event_time
);
1594 * If we have data, but it's not of a type we can deal with,
1595 * we have to ignore the data.
1597 if (seldata
->length
> 0 &&
1598 seldata
->type
!= GDK_SELECTION_TYPE_STRING
&&
1599 seldata
->type
!= compound_text_atom
&&
1600 seldata
->type
!= utf8_string_atom
)
1604 * If we have no data, try looking in a cut buffer.
1606 if (seldata
->length
<= 0) {
1607 text
= retrieve_cutbuffer(&length
);
1610 /* Xterm is rumoured to expect Latin-1, though I havn't checked the
1611 * source, so use that as a de-facto standard. */
1612 charset
= CS_ISO8859_1
;
1616 * Convert COMPOUND_TEXT into UTF-8.
1618 if (seldata
->type
== compound_text_atom
) {
1619 tp
.value
= seldata
->data
;
1620 tp
.encoding
= (Atom
) seldata
->type
;
1621 tp
.format
= seldata
->format
;
1622 tp
.nitems
= seldata
->length
;
1623 ret
= Xutf8TextPropertyToTextList(GDK_DISPLAY(), &tp
,
1625 if (ret
!= 0 || count
!= 1) {
1627 * Compound text failed; fall back to STRING.
1629 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1630 GDK_SELECTION_TYPE_STRING
,
1631 inst
->input_event_time
);
1635 length
= strlen(list
[0]);
1637 free_list_required
= 1;
1639 text
= (char *)seldata
->data
;
1640 length
= seldata
->length
;
1641 charset
= (seldata
->type
== utf8_string_atom ?
1642 CS_UTF8
: inst
->ucsdata
.line_codepage
);
1646 if (inst
->pastein_data
)
1647 sfree(inst
->pastein_data
);
1649 inst
->pastein_data
= snewn(length
, wchar_t);
1650 inst
->pastein_data_len
= length
;
1651 inst
->pastein_data_len
=
1652 mb_to_wc(charset
, 0, text
, length
,
1653 inst
->pastein_data
, inst
->pastein_data_len
);
1655 term_do_paste(inst
->term
);
1657 if (term_paste_pending(inst
->term
))
1658 inst
->term_paste_idle_id
= gtk_idle_add(idle_paste_func
, inst
);
1660 if (free_list_required
)
1661 XFreeStringList(list
);
1666 gint
idle_paste_func(gpointer data
)
1668 struct gui_data
*inst
= (struct gui_data
*)data
;
1670 if (term_paste_pending(inst
->term
))
1671 term_paste(inst
->term
);
1673 gtk_idle_remove(inst
->term_paste_idle_id
);
1679 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
1681 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1684 *p
= inst
->pastein_data
;
1685 *len
= inst
->pastein_data_len
;
1689 static void set_window_titles(struct gui_data
*inst
)
1692 * We must always call set_icon_name after calling set_title,
1693 * since set_title will write both names. Irritating, but such
1696 gtk_window_set_title(GTK_WINDOW(inst
->window
), inst
->wintitle
);
1697 if (!inst
->cfg
.win_name_always
)
1698 gdk_window_set_icon_name(inst
->window
->window
, inst
->icontitle
);
1701 void set_title(void *frontend
, char *title
)
1703 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1704 strncpy(inst
->wintitle
, title
, lenof(inst
->wintitle
));
1705 inst
->wintitle
[lenof(inst
->wintitle
)-1] = '\0';
1706 set_window_titles(inst
);
1709 void set_icon(void *frontend
, char *title
)
1711 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1712 strncpy(inst
->icontitle
, title
, lenof(inst
->icontitle
));
1713 inst
->icontitle
[lenof(inst
->icontitle
)-1] = '\0';
1714 set_window_titles(inst
);
1717 void set_sbar(void *frontend
, int total
, int start
, int page
)
1719 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1720 if (!inst
->cfg
.scrollbar
)
1722 inst
->sbar_adjust
->lower
= 0;
1723 inst
->sbar_adjust
->upper
= total
;
1724 inst
->sbar_adjust
->value
= start
;
1725 inst
->sbar_adjust
->page_size
= page
;
1726 inst
->sbar_adjust
->step_increment
= 1;
1727 inst
->sbar_adjust
->page_increment
= page
/2;
1728 inst
->ignore_sbar
= TRUE
;
1729 gtk_adjustment_changed(inst
->sbar_adjust
);
1730 inst
->ignore_sbar
= FALSE
;
1733 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
1735 struct gui_data
*inst
= (struct gui_data
*)data
;
1737 if (!inst
->cfg
.scrollbar
)
1739 if (!inst
->ignore_sbar
)
1740 term_scroll(inst
->term
, 1, (int)adj
->value
);
1743 void sys_cursor(void *frontend
, int x
, int y
)
1746 * This is meaningless under X.
1751 * This is still called when mode==BELL_VISUAL, even though the
1752 * visual bell is handled entirely within terminal.c, because we
1753 * may want to perform additional actions on any kind of bell (for
1754 * example, taskbar flashing in Windows).
1756 void beep(void *frontend
, int mode
)
1758 if (mode
!= BELL_VISUAL
)
1762 int char_width(Context ctx
, int uc
)
1765 * Under X, any fixed-width font really _is_ fixed-width.
1766 * Double-width characters will be dealt with using a separate
1767 * font. For the moment we can simply return 1.
1772 Context
get_ctx(void *frontend
)
1774 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1775 struct draw_ctx
*dctx
;
1777 if (!inst
->area
->window
)
1780 dctx
= snew(struct draw_ctx
);
1782 dctx
->gc
= gdk_gc_new(inst
->area
->window
);
1786 void free_ctx(Context ctx
)
1788 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1789 /* struct gui_data *inst = dctx->inst; */
1790 GdkGC
*gc
= dctx
->gc
;
1796 * Draw a line of text in the window, at given character
1797 * coordinates, in given attributes.
1799 * We are allowed to fiddle with the contents of `text'.
1801 void do_text_internal(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1802 unsigned long attr
, int lattr
)
1804 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1805 struct gui_data
*inst
= dctx
->inst
;
1806 GdkGC
*gc
= dctx
->gc
;
1808 int nfg
, nbg
, t
, fontid
, shadow
, rlen
, widefactor
;
1810 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1811 nfg
= 2 * (nfg
& 0xF) + (nfg
& 0x10 ?
1 : 0);
1812 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1813 nbg
= 2 * (nbg
& 0xF) + (nbg
& 0x10 ?
1 : 0);
1814 if (attr
& ATTR_REVERSE
) {
1819 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
1821 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
1823 if (attr
& TATTR_ACTCURS
) {
1828 fontid
= shadow
= 0;
1830 if (attr
& ATTR_WIDE
) {
1837 if ((attr
& ATTR_BOLD
) && !inst
->cfg
.bold_colour
) {
1838 if (inst
->fonts
[fontid
| 1])
1844 if ((lattr
& LATTR_MODE
) != LATTR_NORM
) {
1846 if (x
>= inst
->term
->cols
)
1848 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1849 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1857 r
.x
= x
*inst
->font_width
+inst
->cfg
.window_border
;
1858 r
.y
= y
*inst
->font_height
+inst
->cfg
.window_border
;
1859 r
.width
= rlen
*widefactor
*inst
->font_width
;
1860 r
.height
= inst
->font_height
;
1861 gdk_gc_set_clip_rectangle(gc
, &r
);
1864 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
1865 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
1866 x
*inst
->font_width
+inst
->cfg
.window_border
,
1867 y
*inst
->font_height
+inst
->cfg
.window_border
,
1868 rlen
*widefactor
*inst
->font_width
, inst
->font_height
);
1870 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
1877 wcs
= snewn(len
+1, wchar_t);
1878 for (i
= 0; i
< len
; i
++) {
1882 if (inst
->fonts
[fontid
] == NULL
) {
1884 * The font for this contingency does not exist.
1885 * Typically this means we've been given ATTR_WIDE
1886 * character and have no wide font. So we display
1887 * nothing at all; such is life.
1889 } else if (inst
->fontinfo
[fontid
].is_wide
) {
1891 * At least one version of gdk_draw_text_wc() has a
1892 * weird bug whereby it reads `len' elements of the
1893 * input string, but only draws `len/2'. Hence I'm
1894 * going to make its input array twice as long as it
1895 * theoretically needs to be, and pass in twice the
1896 * actual number of characters. If a fixed gdk actually
1897 * takes the doubled length seriously, then (a) the
1898 * array will stand scrutiny up to the full length, (b)
1899 * the spare elements of the array are full of zeroes
1900 * which will probably be an empty glyph in the font,
1901 * and (c) the clip rectangle should prevent it causing
1904 gwcs
= snewn(len
*2+1, GdkWChar
);
1905 memset(gwcs
, 0, sizeof(GdkWChar
) * (len
*2+1));
1907 * FIXME: when we have a wide-char equivalent of
1908 * from_unicode, use it instead of this.
1910 for (i
= 0; i
<= len
; i
++)
1912 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1913 x
*inst
->font_width
+inst
->cfg
.window_border
,
1914 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1917 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1918 x
*inst
->font_width
+inst
->cfg
.window_border
+inst
->cfg
.shadowboldoffset
,
1919 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1923 gcs
= snewn(len
+1, gchar
);
1924 wc_to_mb(inst
->fontinfo
[fontid
].charset
, 0,
1925 wcs
, len
, gcs
, len
, ".", NULL
, NULL
);
1926 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1927 x
*inst
->font_width
+inst
->cfg
.window_border
,
1928 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1931 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1932 x
*inst
->font_width
+inst
->cfg
.window_border
+inst
->cfg
.shadowboldoffset
,
1933 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1940 if (attr
& ATTR_UNDER
) {
1941 int uheight
= inst
->fonts
[0]->ascent
+ 1;
1942 if (uheight
>= inst
->font_height
)
1943 uheight
= inst
->font_height
- 1;
1944 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+inst
->cfg
.window_border
,
1945 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
,
1946 (x
+len
)*widefactor
*inst
->font_width
-1+inst
->cfg
.window_border
,
1947 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
);
1950 if ((lattr
& LATTR_MODE
) != LATTR_NORM
) {
1952 * I can't find any plausible StretchBlt equivalent in the
1953 * X server, so I'm going to do this the slow and painful
1954 * way. This will involve repeated calls to
1955 * gdk_draw_pixmap() to stretch the text horizontally. It's
1956 * O(N^2) in time and O(N) in network bandwidth, but you
1957 * try thinking of a better way. :-(
1960 for (i
= 0; i
< len
* widefactor
* inst
->font_width
; i
++) {
1961 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1962 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
,
1963 y
*inst
->font_height
+inst
->cfg
.window_border
,
1964 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
+1,
1965 y
*inst
->font_height
+inst
->cfg
.window_border
,
1966 len
* inst
->font_width
- i
, inst
->font_height
);
1969 if ((lattr
& LATTR_MODE
) != LATTR_WIDE
) {
1971 /* Now stretch vertically, in the same way. */
1972 if ((lattr
& LATTR_MODE
) == LATTR_BOT
)
1976 for (i
= 0; i
< inst
->font_height
; i
+=2) {
1977 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1978 x
*inst
->font_width
+inst
->cfg
.window_border
,
1979 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*i
+db
,
1980 x
*widefactor
*inst
->font_width
+inst
->cfg
.window_border
,
1981 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*(i
+1),
1982 len
* inst
->font_width
, inst
->font_height
-i
-1);
1988 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
1989 unsigned long attr
, int lattr
)
1991 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1992 struct gui_data
*inst
= dctx
->inst
;
1993 GdkGC
*gc
= dctx
->gc
;
1996 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1998 if (attr
& ATTR_WIDE
) {
2004 if ((lattr
& LATTR_MODE
) != LATTR_NORM
) {
2006 if (x
>= inst
->term
->cols
)
2008 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
2009 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
2013 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
2014 x
*inst
->font_width
+inst
->cfg
.window_border
,
2015 y
*inst
->font_height
+inst
->cfg
.window_border
,
2016 x
*inst
->font_width
+inst
->cfg
.window_border
,
2017 y
*inst
->font_height
+inst
->cfg
.window_border
,
2018 len
*widefactor
*inst
->font_width
, inst
->font_height
);
2021 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
2022 unsigned long attr
, int lattr
)
2024 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
2025 struct gui_data
*inst
= dctx
->inst
;
2026 GdkGC
*gc
= dctx
->gc
;
2028 int active
, passive
, widefactor
;
2030 if (attr
& TATTR_PASCURS
) {
2031 attr
&= ~TATTR_PASCURS
;
2035 if ((attr
& TATTR_ACTCURS
) && inst
->cfg
.cursor_type
!= 0) {
2036 attr
&= ~TATTR_ACTCURS
;
2040 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
2042 if (attr
& ATTR_WIDE
) {
2048 if ((lattr
& LATTR_MODE
) != LATTR_NORM
) {
2050 if (x
>= inst
->term
->cols
)
2052 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
2053 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
2057 if (inst
->cfg
.cursor_type
== 0) {
2059 * An active block cursor will already have been done by
2060 * the above do_text call, so we only need to do anything
2064 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
2065 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
2066 x
*inst
->font_width
+inst
->cfg
.window_border
,
2067 y
*inst
->font_height
+inst
->cfg
.window_border
,
2068 len
*inst
->font_width
-1, inst
->font_height
-1);
2072 int startx
, starty
, dx
, dy
, length
, i
;
2076 if ((attr
& ATTR_WIDE
) || (lattr
& LATTR_MODE
) != LATTR_NORM
)
2077 char_width
= 2*inst
->font_width
;
2079 char_width
= inst
->font_width
;
2081 if (inst
->cfg
.cursor_type
== 1) {
2082 uheight
= inst
->fonts
[0]->ascent
+ 1;
2083 if (uheight
>= inst
->font_height
)
2084 uheight
= inst
->font_height
- 1;
2086 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
;
2087 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
+ uheight
;
2090 length
= len
* char_width
;
2093 if (attr
& TATTR_RIGHTCURS
)
2094 xadjust
= char_width
- 1;
2095 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
+ xadjust
;
2096 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
;
2099 length
= inst
->font_height
;
2102 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
2104 for (i
= 0; i
< length
; i
++) {
2106 gdk_draw_point(inst
->pixmap
, gc
, startx
, starty
);
2111 } else if (active
) {
2112 gdk_draw_line(inst
->pixmap
, gc
, startx
, starty
,
2113 startx
+ (length
-1) * dx
, starty
+ (length
-1) * dy
);
2114 } /* else no cursor (e.g., blinked off) */
2117 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
2118 x
*inst
->font_width
+inst
->cfg
.window_border
,
2119 y
*inst
->font_height
+inst
->cfg
.window_border
,
2120 x
*inst
->font_width
+inst
->cfg
.window_border
,
2121 y
*inst
->font_height
+inst
->cfg
.window_border
,
2122 len
*widefactor
*inst
->font_width
, inst
->font_height
);
2125 GdkCursor
*make_mouse_ptr(struct gui_data
*inst
, int cursor_val
)
2128 * Truly hideous hack: GTK doesn't allow us to set the mouse
2129 * cursor foreground and background colours unless we've _also_
2130 * created our own cursor from bitmaps. Therefore, I need to
2131 * load the `cursor' font and draw glyphs from it on to
2132 * pixmaps, in order to construct my cursors with the fg and bg
2133 * I want. This is a gross hack, but it's more self-contained
2134 * than linking in Xlib to find the X window handle to
2135 * inst->area and calling XRecolorCursor, and it's more
2136 * futureproof than hard-coding the shapes as bitmap arrays.
2138 static GdkFont
*cursor_font
= NULL
;
2139 GdkPixmap
*source
, *mask
;
2141 GdkColor cfg
= { 0, 65535, 65535, 65535 };
2142 GdkColor cbg
= { 0, 0, 0, 0 };
2143 GdkColor dfg
= { 1, 65535, 65535, 65535 };
2144 GdkColor dbg
= { 0, 0, 0, 0 };
2147 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
2149 if (cursor_val
== -2) {
2150 gdk_font_unref(cursor_font
);
2154 if (cursor_val
>= 0 && !cursor_font
)
2155 cursor_font
= gdk_font_load("cursor");
2158 * Get the text extent of the cursor in question. We use the
2159 * mask character for this, because it's typically slightly
2160 * bigger than the main character.
2162 if (cursor_val
>= 0) {
2164 text
[0] = (char)cursor_val
+ 1;
2165 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
2166 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
2172 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
2173 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
2176 * Draw the mask character on the mask pixmap.
2178 gc
= gdk_gc_new(mask
);
2179 gdk_gc_set_foreground(gc
, &dbg
);
2180 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
2181 if (cursor_val
>= 0) {
2183 text
[0] = (char)cursor_val
+ 1;
2184 gdk_gc_set_foreground(gc
, &dfg
);
2185 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
2190 * Draw the main character on the source pixmap.
2192 gc
= gdk_gc_new(source
);
2193 gdk_gc_set_foreground(gc
, &dbg
);
2194 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
2195 if (cursor_val
>= 0) {
2197 text
[0] = (char)cursor_val
;
2198 gdk_gc_set_foreground(gc
, &dfg
);
2199 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
2204 * Create the cursor.
2206 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
2211 gdk_pixmap_unref(source
);
2212 gdk_pixmap_unref(mask
);
2217 void modalfatalbox(char *p
, ...)
2220 fprintf(stderr
, "FATAL ERROR: ");
2222 vfprintf(stderr
, p
, ap
);
2224 fputc('\n', stderr
);
2228 void cmdline_error(char *p
, ...)
2231 fprintf(stderr
, "%s: ", appname
);
2233 vfprintf(stderr
, p
, ap
);
2235 fputc('\n', stderr
);
2239 char *get_x_display(void *frontend
)
2241 return gdk_get_display();
2244 long get_windowid(void *frontend
)
2246 struct gui_data
*inst
= (struct gui_data
*)frontend
;
2247 return (long)GDK_WINDOW_XWINDOW(inst
->area
->window
);
2250 static void help(FILE *fp
) {
2252 "pterm option summary:\n"
2254 " --display DISPLAY Specify X display to use (note '--')\n"
2255 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
2256 " -fn FONT Normal text font\n"
2257 " -fb FONT Bold text font\n"
2258 " -geometry GEOMETRY Position and size of window (size in characters)\n"
2259 " -sl LINES Number of lines of scrollback\n"
2260 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
2261 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
2262 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
2263 " -T TITLE Window title\n"
2264 " -ut, +ut Do(default) or do not update utmp\n"
2265 " -ls, +ls Do(default) or do not make shell a login shell\n"
2266 " -sb, +sb Do(default) or do not display a scrollbar\n"
2267 " -log PATH Log all output to a file\n"
2268 " -nethack Map numeric keypad to hjklyubn direction keys\n"
2269 " -xrm RESOURCE-STRING Set an X resource\n"
2270 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
2271 ) < 0 || fflush(fp
) < 0) {
2272 perror("output error");
2277 int do_cmdline(int argc
, char **argv
, int do_everything
,
2278 struct gui_data
*inst
, Config
*cfg
)
2284 * Macros to make argument handling easier. Note that because
2285 * they need to call `continue', they cannot be contained in
2286 * the usual do {...} while (0) wrapper to make them
2287 * syntactically single statements; hence it is not legal to
2288 * use one of these macros as an unbraced statement between
2291 #define EXPECTS_ARG { \
2292 if (--argc <= 0) { \
2294 fprintf(stderr, "%s: %s expects an argument\n", appname, p); \
2299 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2301 while (--argc
> 0) {
2306 * Shameless cheating. Debian requires all X terminal
2307 * emulators to support `-T title'; but
2308 * cmdline_process_param will eat -T (it means no-pty) and
2309 * complain that pterm doesn't support it. So, in pterm
2310 * only, we convert -T into -title.
2312 if ((cmdline_tooltype
& TOOLTYPE_NONNETWORK
) &&
2316 ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
2317 do_everything ?
1 : -1, cfg
);
2320 cmdline_error("option \"%s\" requires an argument", p
);
2321 } else if (ret
== 2) {
2322 --argc
, ++argv
; /* skip next argument */
2324 } else if (ret
== 1) {
2328 if (!strcmp(p
, "-fn") || !strcmp(p
, "-font")) {
2331 strncpy(cfg
->font
.name
, val
, sizeof(cfg
->font
.name
));
2332 cfg
->font
.name
[sizeof(cfg
->font
.name
)-1] = '\0';
2334 } else if (!strcmp(p
, "-fb")) {
2337 strncpy(cfg
->boldfont
.name
, val
, sizeof(cfg
->boldfont
.name
));
2338 cfg
->boldfont
.name
[sizeof(cfg
->boldfont
.name
)-1] = '\0';
2340 } else if (!strcmp(p
, "-fw")) {
2343 strncpy(cfg
->widefont
.name
, val
, sizeof(cfg
->widefont
.name
));
2344 cfg
->widefont
.name
[sizeof(cfg
->widefont
.name
)-1] = '\0';
2346 } else if (!strcmp(p
, "-fwb")) {
2349 strncpy(cfg
->wideboldfont
.name
, val
, sizeof(cfg
->wideboldfont
.name
));
2350 cfg
->wideboldfont
.name
[sizeof(cfg
->wideboldfont
.name
)-1] = '\0';
2352 } else if (!strcmp(p
, "-cs")) {
2355 strncpy(cfg
->line_codepage
, val
, sizeof(cfg
->line_codepage
));
2356 cfg
->line_codepage
[sizeof(cfg
->line_codepage
)-1] = '\0';
2358 } else if (!strcmp(p
, "-geometry")) {
2359 int flags
, x
, y
, w
, h
;
2363 flags
= XParseGeometry(val
, &x
, &y
, &w
, &h
);
2364 if (flags
& WidthValue
)
2366 if (flags
& HeightValue
)
2369 if (flags
& (XValue
| YValue
)) {
2372 inst
->gotpos
= TRUE
;
2373 inst
->gravity
= ((flags
& XNegative ?
1 : 0) |
2374 (flags
& YNegative ?
2 : 0));
2377 } else if (!strcmp(p
, "-sl")) {
2380 cfg
->savelines
= atoi(val
);
2382 } else if (!strcmp(p
, "-fg") || !strcmp(p
, "-bg") ||
2383 !strcmp(p
, "-bfg") || !strcmp(p
, "-bbg") ||
2384 !strcmp(p
, "-cfg") || !strcmp(p
, "-cbg")) {
2389 if (!gdk_color_parse(val
, &col
)) {
2391 fprintf(stderr
, "%s: unable to parse colour \"%s\"\n",
2395 index
= (!strcmp(p
, "-fg") ?
0 :
2396 !strcmp(p
, "-bg") ?
2 :
2397 !strcmp(p
, "-bfg") ?
1 :
2398 !strcmp(p
, "-bbg") ?
3 :
2399 !strcmp(p
, "-cfg") ?
4 :
2400 !strcmp(p
, "-cbg") ?
5 : -1);
2401 assert(index
!= -1);
2402 cfg
->colours
[index
][0] = col
.red
/ 256;
2403 cfg
->colours
[index
][1] = col
.green
/ 256;
2404 cfg
->colours
[index
][2] = col
.blue
/ 256;
2407 } else if (use_pty_argv
&& !strcmp(p
, "-e")) {
2408 /* This option swallows all further arguments. */
2414 pty_argv
= snewn(argc
+1, char *);
2416 for (i
= 0; i
< argc
; i
++)
2417 pty_argv
[i
] = argv
[i
];
2418 pty_argv
[argc
] = NULL
;
2419 break; /* finished command-line processing */
2421 err
= 1, fprintf(stderr
, "%s: -e expects an argument\n",
2424 } else if (!strcmp(p
, "-title")) {
2427 strncpy(cfg
->wintitle
, val
, sizeof(cfg
->wintitle
));
2428 cfg
->wintitle
[sizeof(cfg
->wintitle
)-1] = '\0';
2430 } else if (!strcmp(p
, "-log")) {
2433 strncpy(cfg
->logfilename
.path
, val
, sizeof(cfg
->logfilename
.path
));
2434 cfg
->logfilename
.path
[sizeof(cfg
->logfilename
.path
)-1] = '\0';
2435 cfg
->logtype
= LGTYP_DEBUG
;
2437 } else if (!strcmp(p
, "-ut-") || !strcmp(p
, "+ut")) {
2439 cfg
->stamp_utmp
= 0;
2441 } else if (!strcmp(p
, "-ut")) {
2443 cfg
->stamp_utmp
= 1;
2445 } else if (!strcmp(p
, "-ls-") || !strcmp(p
, "+ls")) {
2447 cfg
->login_shell
= 0;
2449 } else if (!strcmp(p
, "-ls")) {
2451 cfg
->login_shell
= 1;
2453 } else if (!strcmp(p
, "-nethack")) {
2455 cfg
->nethack_keypad
= 1;
2457 } else if (!strcmp(p
, "-sb-") || !strcmp(p
, "+sb")) {
2461 } else if (!strcmp(p
, "-sb")) {
2465 } else if (!strcmp(p
, "-name")) {
2469 } else if (!strcmp(p
, "-xrm")) {
2471 provide_xrm_string(val
);
2473 } else if(!strcmp(p
, "-help") || !strcmp(p
, "--help")) {
2477 } else if(p
[0] != '-' && (!do_everything
||
2478 process_nonoption_arg(p
, cfg
))) {
2483 fprintf(stderr
, "%s: unrecognized option '%s'\n", appname
, p
);
2490 static void block_signal(int sig
, int block_it
) {
2494 sigaddset(&ss
, sig
);
2495 if(sigprocmask(block_it ? SIG_BLOCK
: SIG_UNBLOCK
, &ss
, 0) < 0) {
2496 perror("sigprocmask");
2502 * This function retrieves the character set encoding of a font. It
2503 * returns the character set without the X11 hack (in case the user
2504 * asks to use the font's own encoding).
2506 static int set_font_info(struct gui_data
*inst
, int fontid
)
2508 GdkFont
*font
= inst
->fonts
[fontid
];
2509 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2510 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2511 Atom charset_registry
, charset_encoding
;
2512 unsigned long registry_ret
, encoding_ret
;
2513 int retval
= CS_NONE
;
2515 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
2516 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
2517 inst
->fontinfo
[fontid
].charset
= CS_NONE
;
2518 inst
->fontinfo
[fontid
].is_wide
= 0;
2519 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
2520 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
2522 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
2523 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
2525 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
2526 retval
= inst
->fontinfo
[fontid
].charset
=
2527 charset_from_xenc(encoding
);
2528 /* FIXME: when libcharset supports wide encodings fix this. */
2529 if (!strcasecmp(encoding
, "iso10646-1")) {
2530 inst
->fontinfo
[fontid
].is_wide
= 1;
2535 * Hack for X line-drawing characters: if the primary
2536 * font is encoded as ISO-8859-anything, and has valid
2537 * glyphs in the first 32 char positions, it is assumed
2538 * that those glyphs are the VT100 line-drawing
2541 * Actually, we'll hack even harder by only checking
2542 * position 0x19 (vertical line, VT100 linedrawing
2543 * `x'). Then we can check it easily by seeing if the
2544 * ascent and descent differ.
2546 if (inst
->fontinfo
[fontid
].charset
== CS_ISO8859_1
) {
2547 int lb
, rb
, wid
, asc
, desc
;
2552 gdk_string_extents(inst
->fonts
[fontid
], text
,
2553 &lb
, &rb
, &wid
, &asc
, &desc
);
2555 inst
->fontinfo
[fontid
].charset
= CS_ISO8859_1_X11
;
2565 int uxsel_input_add(int fd
, int rwx
) {
2567 if (rwx
& 1) flags
|= GDK_INPUT_READ
;
2568 if (rwx
& 2) flags
|= GDK_INPUT_WRITE
;
2569 if (rwx
& 4) flags
|= GDK_INPUT_EXCEPTION
;
2570 return gdk_input_add(fd
, flags
, fd_input_func
, NULL
);
2573 void uxsel_input_remove(int id
) {
2574 gdk_input_remove(id
);
2577 char *guess_derived_font_name(GdkFont
*font
, int bold
, int wide
)
2579 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2580 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2581 Atom fontprop
= XInternAtom(disp
, "FONT", False
);
2583 if (XGetFontProperty(xfs
, fontprop
, &ret
)) {
2584 char *name
= XGetAtomName(disp
, (Atom
)ret
);
2585 if (name
&& name
[0] == '-') {
2587 char *dupname
, *extrafree
= NULL
, *ret
;
2591 p
= q
= dupname
= dupstr(name
); /* skip initial minus */
2594 while (*p
&& nstr
< lenof(strings
)) {
2597 strings
[nstr
++] = p
+1;
2602 if (nstr
< lenof(strings
))
2603 return NULL
; /* XLFD was malformed */
2606 strings
[2] = "bold";
2609 /* 4 is `wideness', which obviously may have changed. */
2610 /* 5 is additional style, which may be e.g. `ja' or `ko'. */
2611 strings
[4] = strings
[5] = "*";
2612 strings
[11] = extrafree
= dupprintf("%d", 2*atoi(strings
[11]));
2615 ret
= dupcat("-", strings
[ 0], "-", strings
[ 1], "-", strings
[ 2],
2616 "-", strings
[ 3], "-", strings
[ 4], "-", strings
[ 5],
2617 "-", strings
[ 6], "-", strings
[ 7], "-", strings
[ 8],
2618 "-", strings
[ 9], "-", strings
[10], "-", strings
[11],
2619 "-", strings
[12], NULL
);
2629 void setup_fonts_ucs(struct gui_data
*inst
)
2636 gdk_font_unref(inst
->fonts
[0]);
2638 gdk_font_unref(inst
->fonts
[1]);
2640 gdk_font_unref(inst
->fonts
[2]);
2642 gdk_font_unref(inst
->fonts
[3]);
2644 inst
->fonts
[0] = gdk_font_load(inst
->cfg
.font
.name
);
2645 if (!inst
->fonts
[0]) {
2646 fprintf(stderr
, "%s: unable to load font \"%s\"\n", appname
,
2647 inst
->cfg
.font
.name
);
2650 font_charset
= set_font_info(inst
, 0);
2652 if (inst
->cfg
.shadowbold
) {
2653 inst
->fonts
[1] = NULL
;
2655 if (inst
->cfg
.boldfont
.name
[0]) {
2656 name
= inst
->cfg
.boldfont
.name
;
2659 name
= guess_derived_font_name(inst
->fonts
[0], TRUE
, FALSE
);
2662 inst
->fonts
[1] = name ?
gdk_font_load(name
) : NULL
;
2663 if (inst
->fonts
[1]) {
2664 set_font_info(inst
, 1);
2665 } else if (!guessed
) {
2666 fprintf(stderr
, "%s: unable to load bold font \"%s\"\n", appname
,
2667 inst
->cfg
.boldfont
.name
);
2674 if (inst
->cfg
.widefont
.name
[0]) {
2675 name
= inst
->cfg
.widefont
.name
;
2678 name
= guess_derived_font_name(inst
->fonts
[0], FALSE
, TRUE
);
2681 inst
->fonts
[2] = name ?
gdk_font_load(name
) : NULL
;
2682 if (inst
->fonts
[2]) {
2683 set_font_info(inst
, 2);
2684 } else if (!guessed
) {
2685 fprintf(stderr
, "%s: unable to load wide font \"%s\"\n", appname
,
2686 inst
->cfg
.widefont
.name
);
2692 if (inst
->cfg
.shadowbold
) {
2693 inst
->fonts
[3] = NULL
;
2695 if (inst
->cfg
.wideboldfont
.name
[0]) {
2696 name
= inst
->cfg
.wideboldfont
.name
;
2700 * Here we have some choices. We can widen the bold font,
2701 * bolden the wide font, or widen and bolden the standard
2702 * font. Try them all, in that order!
2704 if (inst
->cfg
.widefont
.name
[0])
2705 name
= guess_derived_font_name(inst
->fonts
[2], TRUE
, FALSE
);
2706 else if (inst
->cfg
.boldfont
.name
[0])
2707 name
= guess_derived_font_name(inst
->fonts
[1], FALSE
, TRUE
);
2709 name
= guess_derived_font_name(inst
->fonts
[0], TRUE
, TRUE
);
2712 inst
->fonts
[3] = name ?
gdk_font_load(name
) : NULL
;
2713 if (inst
->fonts
[3]) {
2714 set_font_info(inst
, 3);
2715 } else if (!guessed
) {
2716 fprintf(stderr
, "%s: unable to load wide/bold font \"%s\"\n", appname
,
2717 inst
->cfg
.wideboldfont
.name
);
2724 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
2725 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
2727 inst
->direct_to_font
= init_ucs(&inst
->ucsdata
,
2728 inst
->cfg
.line_codepage
, font_charset
,
2732 void set_geom_hints(struct gui_data
*inst
)
2735 geom
.min_width
= inst
->font_width
+ 2*inst
->cfg
.window_border
;
2736 geom
.min_height
= inst
->font_height
+ 2*inst
->cfg
.window_border
;
2737 geom
.max_width
= geom
.max_height
= -1;
2738 geom
.base_width
= 2*inst
->cfg
.window_border
;
2739 geom
.base_height
= 2*inst
->cfg
.window_border
;
2740 geom
.width_inc
= inst
->font_width
;
2741 geom
.height_inc
= inst
->font_height
;
2742 geom
.min_aspect
= geom
.max_aspect
= 0;
2743 gtk_window_set_geometry_hints(GTK_WINDOW(inst
->window
), inst
->area
, &geom
,
2744 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
2745 GDK_HINT_RESIZE_INC
);
2748 void clear_scrollback_menuitem(GtkMenuItem
*item
, gpointer data
)
2750 struct gui_data
*inst
= (struct gui_data
*)data
;
2751 term_clrsb(inst
->term
);
2754 void reset_terminal_menuitem(GtkMenuItem
*item
, gpointer data
)
2756 struct gui_data
*inst
= (struct gui_data
*)data
;
2757 term_pwron(inst
->term
);
2759 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2762 void copy_all_menuitem(GtkMenuItem
*item
, gpointer data
)
2764 struct gui_data
*inst
= (struct gui_data
*)data
;
2765 term_copyall(inst
->term
);
2768 void special_menuitem(GtkMenuItem
*item
, gpointer data
)
2770 struct gui_data
*inst
= (struct gui_data
*)data
;
2771 int code
= GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item
),
2775 inst
->back
->special(inst
->backhandle
, code
);
2778 void about_menuitem(GtkMenuItem
*item
, gpointer data
)
2780 struct gui_data
*inst
= (struct gui_data
*)data
;
2781 about_box(inst
->window
);
2784 void event_log_menuitem(GtkMenuItem
*item
, gpointer data
)
2786 struct gui_data
*inst
= (struct gui_data
*)data
;
2787 showeventlog(inst
->eventlogstuff
, inst
->window
);
2790 void change_settings_menuitem(GtkMenuItem
*item
, gpointer data
)
2792 /* This maps colour indices in inst->cfg to those used in inst->cols. */
2793 static const int ww
[] = {
2794 6, 7, 8, 9, 10, 11, 12, 13,
2795 14, 15, 16, 17, 18, 19, 20, 21,
2798 struct gui_data
*inst
= (struct gui_data
*)data
;
2799 char *title
= dupcat(appname
, " Reconfiguration", NULL
);
2800 Config cfg2
, oldcfg
;
2803 cfg2
= inst
->cfg
; /* structure copy */
2805 if (do_config_box(title
, &cfg2
, 1)) {
2807 oldcfg
= inst
->cfg
; /* structure copy */
2808 inst
->cfg
= cfg2
; /* structure copy */
2810 /* Pass new config data to the logging module */
2811 log_reconfig(inst
->logctx
, &cfg2
);
2813 * Flush the line discipline's edit buffer in the case
2814 * where local editing has just been disabled.
2817 ldisc_send(inst
->ldisc
, NULL
, 0, 0);
2818 /* Pass new config data to the terminal */
2819 term_reconfig(inst
->term
, &cfg2
);
2820 /* Pass new config data to the back end */
2822 inst
->back
->reconfig(inst
->backhandle
, &cfg2
);
2825 * Just setting inst->cfg is sufficient to cause colour
2826 * setting changes to appear on the next ESC]R palette
2827 * reset. But we should also check whether any colour
2828 * settings have been changed, and revert the ones that
2829 * have to the new default, on the assumption that the user
2830 * is most likely to want an immediate update.
2832 for (i
= 0; i
< NCOLOURS
; i
++) {
2833 if (oldcfg
.colours
[ww
[i
]][0] != cfg2
.colours
[ww
[i
]][0] ||
2834 oldcfg
.colours
[ww
[i
]][1] != cfg2
.colours
[ww
[i
]][1] ||
2835 oldcfg
.colours
[ww
[i
]][2] != cfg2
.colours
[ww
[i
]][2]) {
2836 real_palette_set(inst
, i
, cfg2
.colours
[ww
[i
]][0],
2837 cfg2
.colours
[ww
[i
]][1],
2838 cfg2
.colours
[ww
[i
]][2]);
2841 * If the default background has changed, we must
2842 * repaint the space in between the window border
2843 * and the text area.
2846 set_window_background(inst
);
2847 draw_backing_rect(inst
);
2853 * If the scrollbar needs to be shown, hidden, or moved
2854 * from one end to the other of the window, do so now.
2856 if (oldcfg
.scrollbar
!= cfg2
.scrollbar
) {
2858 gtk_widget_show(inst
->sbar
);
2860 gtk_widget_hide(inst
->sbar
);
2862 if (oldcfg
.scrollbar_on_left
!= cfg2
.scrollbar_on_left
) {
2863 gtk_box_reorder_child(inst
->hbox
, inst
->sbar
,
2864 cfg2
.scrollbar_on_left ?
0 : 1);
2868 * Change the window title, if required.
2870 if (strcmp(oldcfg
.wintitle
, cfg2
.wintitle
))
2871 set_title(inst
, cfg2
.wintitle
);
2872 set_window_titles(inst
);
2875 * Redo the whole tangled fonts and Unicode mess if
2878 if (strcmp(oldcfg
.font
.name
, cfg2
.font
.name
) ||
2879 strcmp(oldcfg
.boldfont
.name
, cfg2
.boldfont
.name
) ||
2880 strcmp(oldcfg
.widefont
.name
, cfg2
.widefont
.name
) ||
2881 strcmp(oldcfg
.wideboldfont
.name
, cfg2
.wideboldfont
.name
) ||
2882 strcmp(oldcfg
.line_codepage
, cfg2
.line_codepage
) ||
2883 oldcfg
.vtmode
!= cfg2
.vtmode
||
2884 oldcfg
.shadowbold
!= cfg2
.shadowbold
) {
2885 setup_fonts_ucs(inst
);
2891 * Resize the window.
2893 if (oldcfg
.width
!= cfg2
.width
|| oldcfg
.height
!= cfg2
.height
||
2894 oldcfg
.window_border
!= cfg2
.window_border
|| need_size
) {
2895 set_geom_hints(inst
);
2896 request_resize(inst
, cfg2
.width
, cfg2
.height
);
2899 * The above will have caused a call to term_size() for
2900 * us if it happened. If the user has fiddled with only
2901 * the scrollback size, the above will not have
2902 * happened and we will need an explicit term_size()
2905 if (oldcfg
.savelines
!= cfg2
.savelines
)
2906 term_size(inst
->term
, inst
->term
->rows
, inst
->term
->cols
,
2910 term_invalidate(inst
->term
);
2913 * We do an explicit full redraw here to ensure the window
2914 * border has been redrawn as well as the text area.
2916 gtk_widget_queue_draw(inst
->area
);
2921 void fork_and_exec_self(struct gui_data
*inst
, int fd_to_close
, ...)
2924 * Re-execing ourself is not an exact science under Unix. I do
2925 * the best I can by using /proc/self/exe if available and by
2926 * assuming argv[0] can be found on $PATH if not.
2928 * Note that we also have to reconstruct the elements of the
2929 * original argv which gtk swallowed, since the user wants the
2930 * new session to appear on the same X display as the old one.
2938 * Collect the arguments with which to re-exec ourself.
2940 va_start(ap
, fd_to_close
);
2941 n
= 2; /* progname and terminating NULL */
2942 n
+= inst
->ngtkargs
;
2943 while (va_arg(ap
, char *) != NULL
)
2947 args
= snewn(n
, char *);
2948 args
[0] = inst
->progname
;
2950 for (i
= 0; i
< inst
->ngtkargs
; i
++)
2951 args
[i
+1] = inst
->gtkargvstart
[i
];
2954 va_start(ap
, fd_to_close
);
2955 while ((args
[i
++] = va_arg(ap
, char *)) != NULL
);
2961 * Do the double fork.
2974 } else if (pid2
> 0) {
2976 * First child has successfully forked second child. My
2977 * Work Here Is Done. Note the use of _exit rather than
2978 * exit: the latter appears to cause destroy messages
2979 * to be sent to the X server. I suspect gtk uses
2986 * If we reach here, we are the second child, so we now
2987 * actually perform the exec.
2989 if (fd_to_close
>= 0)
2992 execv("/proc/self/exe", args
);
2993 execvp(inst
->progname
, args
);
2999 waitpid(pid
, &status
, 0);
3004 void dup_session_menuitem(GtkMenuItem
*item
, gpointer gdata
)
3006 struct gui_data
*inst
= (struct gui_data
*)gdata
;
3008 * For this feature we must marshal cfg and (possibly) pty_argv
3009 * into a byte stream, create a pipe, and send this byte stream
3010 * to the child through the pipe.
3017 if (pipe(pipefd
) < 0) {
3022 size
= sizeof(inst
->cfg
);
3023 if (use_pty_argv
&& pty_argv
) {
3024 for (i
= 0; pty_argv
[i
]; i
++)
3025 size
+= strlen(pty_argv
[i
]) + 1;
3028 data
= snewn(size
, char);
3029 memcpy(data
, &inst
->cfg
, sizeof(inst
->cfg
));
3030 if (use_pty_argv
&& pty_argv
) {
3031 int p
= sizeof(inst
->cfg
);
3032 for (i
= 0; pty_argv
[i
]; i
++) {
3033 strcpy(data
+ p
, pty_argv
[i
]);
3034 p
+= strlen(pty_argv
[i
]) + 1;
3039 sprintf(option
, "---[%d,%d]", pipefd
[0], size
);
3040 fcntl(pipefd
[0], F_SETFD
, 0);
3041 fork_and_exec_self(inst
, pipefd
[1], option
, NULL
);
3045 while (i
< size
&& (ret
= write(pipefd
[1], data
+ i
, size
- i
)) > 0)
3048 perror("write to pipe");
3053 int read_dupsession_data(struct gui_data
*inst
, Config
*cfg
, char *arg
)
3055 int fd
, i
, ret
, size
;
3058 if (sscanf(arg
, "---[%d,%d]", &fd
, &size
) != 2) {
3059 fprintf(stderr
, "%s: malformed magic argument `%s'\n", appname
, arg
);
3063 data
= snewn(size
, char);
3065 while (i
< size
&& (ret
= read(fd
, data
+ i
, size
- i
)) > 0)
3068 perror("read from pipe");
3070 } else if (i
< size
) {
3071 fprintf(stderr
, "%s: unexpected EOF in Duplicate Session data\n",
3076 memcpy(cfg
, data
, sizeof(Config
));
3077 if (use_pty_argv
&& size
> sizeof(Config
)) {
3081 while (i
< size
&& data
[i
]) i
++;
3083 fprintf(stderr
, "%s: malformed Duplicate Session data\n",
3090 pty_argv
= snewn(n
+1, char *);
3096 while (i
< size
&& data
[i
]) i
++;
3099 pty_argv
[n
++] = dupstr(p
);
3106 void new_session_menuitem(GtkMenuItem
*item
, gpointer data
)
3108 struct gui_data
*inst
= (struct gui_data
*)data
;
3110 fork_and_exec_self(inst
, -1, NULL
);
3113 void restart_session_menuitem(GtkMenuItem
*item
, gpointer data
)
3115 struct gui_data
*inst
= (struct gui_data
*)data
;
3118 logevent(inst
, "----- Session restarted -----");
3119 start_backend(inst
);
3120 inst
->exited
= FALSE
;
3124 void saved_session_menuitem(GtkMenuItem
*item
, gpointer data
)
3126 struct gui_data
*inst
= (struct gui_data
*)data
;
3127 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
3129 fork_and_exec_self(inst
, -1, "-load", str
, NULL
);
3132 void saved_session_freedata(GtkMenuItem
*item
, gpointer data
)
3134 char *str
= (char *)gtk_object_get_data(GTK_OBJECT(item
), "user-data");
3139 void update_specials_menu(void *frontend
)
3141 struct gui_data
*inst
= (struct gui_data
*)frontend
;
3143 const struct telnet_special
*specials
;
3146 specials
= inst
->back
->get_specials(inst
->backhandle
);
3150 gtk_container_foreach(GTK_CONTAINER(inst
->specialsmenu
),
3151 (GtkCallback
)gtk_widget_destroy
, NULL
);
3154 GtkWidget
*menuitem
;
3155 for (i
= 0; specials
[i
].name
; i
++) {
3156 if (*specials
[i
].name
) {
3157 menuitem
= gtk_menu_item_new_with_label(specials
[i
].name
);
3158 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
3159 GINT_TO_POINTER(specials
[i
].code
));
3160 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
3161 GTK_SIGNAL_FUNC(special_menuitem
), inst
);
3163 menuitem
= gtk_menu_item_new();
3164 gtk_container_add(GTK_CONTAINER(inst
->specialsmenu
), menuitem
);
3165 gtk_widget_show(menuitem
);
3167 gtk_widget_show(inst
->specialsitem1
);
3168 gtk_widget_show(inst
->specialsitem2
);
3170 gtk_widget_hide(inst
->specialsitem1
);
3171 gtk_widget_hide(inst
->specialsitem2
);
3175 static void start_backend(struct gui_data
*inst
)
3177 extern Backend
*select_backend(Config
*cfg
);
3181 inst
->back
= select_backend(&inst
->cfg
);
3183 error
= inst
->back
->init((void *)inst
, &inst
->backhandle
,
3184 &inst
->cfg
, inst
->cfg
.host
, inst
->cfg
.port
,
3185 &realhost
, inst
->cfg
.tcp_nodelay
,
3186 inst
->cfg
.tcp_keepalives
);
3189 char *msg
= dupprintf("Unable to open connection to %s:\n%s",
3190 inst
->cfg
.host
, error
);
3191 inst
->exited
= TRUE
;
3192 fatal_message_box(inst
->window
, msg
);
3197 if (inst
->cfg
.wintitle
[0]) {
3198 set_title(inst
, inst
->cfg
.wintitle
);
3199 set_icon(inst
, inst
->cfg
.wintitle
);
3201 char *title
= make_default_wintitle(realhost
);
3202 set_title(inst
, title
);
3203 set_icon(inst
, title
);
3206 inst
->back
->provide_logctx(inst
->backhandle
, inst
->logctx
);
3207 update_specials_menu(inst
);
3209 term_provide_resize_fn(inst
->term
, inst
->back
->size
, inst
->backhandle
);
3212 ldisc_create(&inst
->cfg
, inst
->term
, inst
->back
, inst
->backhandle
,
3215 gtk_widget_hide(inst
->restartitem
);
3218 int pt_main(int argc
, char **argv
)
3220 extern int cfgbox(Config
*cfg
);
3221 struct gui_data
*inst
;
3224 * Create an instance structure and initialise to zeroes
3226 inst
= snew(struct gui_data
);
3227 memset(inst
, 0, sizeof(*inst
));
3228 inst
->alt_keycode
= -1; /* this one needs _not_ to be zero */
3230 /* defer any child exit handling until we're ready to deal with
3232 block_signal(SIGCHLD
, 1);
3234 inst
->progname
= argv
[0];
3236 * Copy the original argv before letting gtk_init fiddle with
3237 * it. It will be required later.
3241 inst
->gtkargvstart
= snewn(argc
-1, char *);
3242 for (i
= 1; i
< argc
; i
++)
3243 inst
->gtkargvstart
[i
-1] = dupstr(argv
[i
]);
3245 gtk_init(&argc
, &argv
);
3246 inst
->ngtkargs
= oldargc
- argc
;
3249 if (argc
> 1 && !strncmp(argv
[1], "---", 3)) {
3250 read_dupsession_data(inst
, &inst
->cfg
, argv
[1]);
3251 /* Splatter this argument so it doesn't clutter a ps listing */
3252 memset(argv
[1], 0, strlen(argv
[1]));
3254 if (do_cmdline(argc
, argv
, 0, inst
, &inst
->cfg
))
3255 exit(1); /* pre-defaults pass to get -class */
3256 do_defaults(NULL
, &inst
->cfg
);
3257 if (do_cmdline(argc
, argv
, 1, inst
, &inst
->cfg
))
3258 exit(1); /* post-defaults, do everything */
3260 cmdline_run_saved(&inst
->cfg
);
3262 if (!*inst
->cfg
.host
&& !cfgbox(&inst
->cfg
))
3263 exit(0); /* config box hit Cancel */
3266 if (!compound_text_atom
)
3267 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
3268 if (!utf8_string_atom
)
3269 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
3271 setup_fonts_ucs(inst
);
3274 inst
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
3277 * Set up the colour map.
3279 palette_reset(inst
);
3281 inst
->width
= inst
->cfg
.width
;
3282 inst
->height
= inst
->cfg
.height
;
3284 inst
->area
= gtk_drawing_area_new();
3285 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
3286 inst
->font_width
* inst
->cfg
.width
+ 2*inst
->cfg
.window_border
,
3287 inst
->font_height
* inst
->cfg
.height
+ 2*inst
->cfg
.window_border
);
3288 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
3289 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
3290 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
3292 * We always create the scrollbar; it remains invisible if
3293 * unwanted, so we can pop it up quickly if it suddenly becomes
3296 if (inst
->cfg
.scrollbar_on_left
)
3297 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
3298 gtk_box_pack_start(inst
->hbox
, inst
->area
, TRUE
, TRUE
, 0);
3299 if (!inst
->cfg
.scrollbar_on_left
)
3300 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
3302 gtk_container_add(GTK_CONTAINER(inst
->window
), GTK_WIDGET(inst
->hbox
));
3304 set_geom_hints(inst
);
3306 gtk_widget_show(inst
->area
);
3307 if (inst
->cfg
.scrollbar
)
3308 gtk_widget_show(inst
->sbar
);
3310 gtk_widget_hide(inst
->sbar
);
3311 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
3314 int x
= inst
->xpos
, y
= inst
->ypos
;
3316 gtk_widget_size_request(GTK_WIDGET(inst
->window
), &req
);
3317 if (inst
->gravity
& 1) x
+= gdk_screen_width() - req
.width
;
3318 if (inst
->gravity
& 2) y
+= gdk_screen_height() - req
.height
;
3319 gtk_window_set_position(GTK_WINDOW(inst
->window
), GTK_WIN_POS_NONE
);
3320 gtk_widget_set_uposition(GTK_WIDGET(inst
->window
), x
, y
);
3323 gtk_signal_connect(GTK_OBJECT(inst
->window
), "destroy",
3324 GTK_SIGNAL_FUNC(destroy
), inst
);
3325 gtk_signal_connect(GTK_OBJECT(inst
->window
), "delete_event",
3326 GTK_SIGNAL_FUNC(delete_window
), inst
);
3327 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_press_event",
3328 GTK_SIGNAL_FUNC(key_event
), inst
);
3329 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_release_event",
3330 GTK_SIGNAL_FUNC(key_event
), inst
);
3331 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_in_event",
3332 GTK_SIGNAL_FUNC(focus_event
), inst
);
3333 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_out_event",
3334 GTK_SIGNAL_FUNC(focus_event
), inst
);
3335 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
3336 GTK_SIGNAL_FUNC(configure_area
), inst
);
3337 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
3338 GTK_SIGNAL_FUNC(expose_area
), inst
);
3339 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
3340 GTK_SIGNAL_FUNC(button_event
), inst
);
3341 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
3342 GTK_SIGNAL_FUNC(button_event
), inst
);
3343 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
3344 GTK_SIGNAL_FUNC(motion_event
), inst
);
3345 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
3346 GTK_SIGNAL_FUNC(selection_received
), inst
);
3347 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
3348 GTK_SIGNAL_FUNC(selection_get
), inst
);
3349 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
3350 GTK_SIGNAL_FUNC(selection_clear
), inst
);
3351 if (inst
->cfg
.scrollbar
)
3352 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
3353 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
3354 gtk_timeout_add(20, timer_func
, inst
);
3355 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
3356 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
3357 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
3358 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_MOTION_MASK
);
3360 gtk_widget_show(inst
->window
);
3362 set_window_background(inst
);
3365 * Set up the Ctrl+rightclick context menu.
3368 GtkWidget
*menuitem
;
3370 extern const int use_event_log
, new_session
, saved_sessions
;
3372 inst
->menu
= gtk_menu_new();
3374 #define MKMENUITEM(title, func) do { \
3375 menuitem = title ? gtk_menu_item_new_with_label(title) : \
3376 gtk_menu_item_new(); \
3377 gtk_container_add(GTK_CONTAINER(inst->menu), menuitem); \
3378 gtk_widget_show(menuitem); \
3380 gtk_signal_connect(GTK_OBJECT(menuitem), "activate", \
3381 GTK_SIGNAL_FUNC(func), inst); \
3384 MKMENUITEM("New Session", new_session_menuitem
);
3385 MKMENUITEM("Restart Session", restart_session_menuitem
);
3386 inst
->restartitem
= menuitem
;
3387 gtk_widget_hide(inst
->restartitem
);
3388 MKMENUITEM("Duplicate Session", dup_session_menuitem
);
3389 if (saved_sessions
) {
3390 struct sesslist sesslist
;
3393 inst
->sessionsmenu
= gtk_menu_new();
3395 get_sesslist(&sesslist
, TRUE
);
3396 for (i
= 1; i
< sesslist
.nsessions
; i
++) {
3397 menuitem
= gtk_menu_item_new_with_label(sesslist
.sessions
[i
]);
3398 gtk_container_add(GTK_CONTAINER(inst
->sessionsmenu
), menuitem
);
3399 gtk_widget_show(menuitem
);
3400 gtk_object_set_data(GTK_OBJECT(menuitem
), "user-data",
3401 dupstr(sesslist
.sessions
[i
]));
3402 gtk_signal_connect(GTK_OBJECT(menuitem
), "activate",
3403 GTK_SIGNAL_FUNC(saved_session_menuitem
),
3405 gtk_signal_connect(GTK_OBJECT(menuitem
), "destroy",
3406 GTK_SIGNAL_FUNC(saved_session_freedata
),
3409 get_sesslist(&sesslist
, FALSE
);
3411 MKMENUITEM("Saved Sessions", NULL
);
3412 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
),
3413 inst
->sessionsmenu
);
3415 MKMENUITEM(NULL
, NULL
);
3416 MKMENUITEM("Change Settings", change_settings_menuitem
);
3417 MKMENUITEM(NULL
, NULL
);
3419 MKMENUITEM("Event Log", event_log_menuitem
);
3420 MKMENUITEM("Special Commands", NULL
);
3421 inst
->specialsmenu
= gtk_menu_new();
3422 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem
), inst
->specialsmenu
);
3423 inst
->specialsitem1
= menuitem
;
3424 MKMENUITEM(NULL
, NULL
);
3425 inst
->specialsitem2
= menuitem
;
3426 MKMENUITEM("Clear Scrollback", clear_scrollback_menuitem
);
3427 MKMENUITEM("Reset Terminal", reset_terminal_menuitem
);
3428 MKMENUITEM("Copy All", copy_all_menuitem
);
3429 MKMENUITEM(NULL
, NULL
);
3430 s
= dupcat("About ", appname
, NULL
);
3431 MKMENUITEM(s
, about_menuitem
);
3436 inst
->textcursor
= make_mouse_ptr(inst
, GDK_XTERM
);
3437 inst
->rawcursor
= make_mouse_ptr(inst
, GDK_LEFT_PTR
);
3438 inst
->blankcursor
= make_mouse_ptr(inst
, -1);
3439 make_mouse_ptr(inst
, -2); /* clean up cursor font */
3440 inst
->currcursor
= inst
->textcursor
;
3441 show_mouseptr(inst
, 1);
3443 inst
->eventlogstuff
= eventlogstuff_new();
3445 inst
->term
= term_init(&inst
->cfg
, &inst
->ucsdata
, inst
);
3446 inst
->logctx
= log_init(inst
, &inst
->cfg
);
3447 term_provide_logctx(inst
->term
, inst
->logctx
);
3451 term_size(inst
->term
, inst
->cfg
.height
, inst
->cfg
.width
, inst
->cfg
.savelines
);
3453 start_backend(inst
);
3455 ldisc_send(inst
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
3457 /* now we're reday to deal with the child exit handler being
3459 block_signal(SIGCHLD
, 0);
3462 * Block SIGPIPE: if we attempt Duplicate Session or similar
3463 * and it falls over in some way, we certainly don't want
3464 * SIGPIPE terminating the main pterm/PuTTY. Note that we do
3465 * this _after_ (at least pterm) forks off its child process,
3466 * since the child wants SIGPIPE handled in the usual way.
3468 block_signal(SIGPIPE
, 1);
3470 inst
->exited
= FALSE
;