2 * pterm - a fusion of the PuTTY terminal emulator with a Unix pty
3 * back end, all running as a GTK application. Wish me luck.
18 #include <sys/types.h>
21 #include <gdk/gdkkeysyms.h>
24 #include <X11/Xutil.h>
26 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
31 #define CAT2(x,y) x ## y
32 #define CAT(x,y) CAT2(x,y)
33 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
35 #define NCOLOURS (lenof(((Config *)0)->colours))
38 GtkWidget
*window
, *area
, *sbar
;
40 GtkAdjustment
*sbar_adjust
;
42 GdkFont
*fonts
[4]; /* normal, bold, wide, widebold */
47 GdkCursor
*rawcursor
, *textcursor
, *blankcursor
, *currcursor
;
48 GdkColor cols
[NCOLOURS
];
50 wchar_t *pastein_data
;
53 char *pasteout_data
, *pasteout_data_utf8
;
54 int pasteout_data_len
, pasteout_data_utf8_len
;
55 int font_width
, font_height
;
58 guint term_paste_idle_id
;
59 GdkAtom compound_text_atom
, utf8_string_atom
;
62 char wintitle
[sizeof(((Config
*)0)->wintitle
)];
63 char icontitle
[sizeof(((Config
*)0)->wintitle
)];
64 int master_fd
, master_func_id
, exited
;
75 struct gui_data
*inst
;
78 static int send_raw_mouse
;
80 static char *app_name
= "pterm";
82 char *x_get_default(char *key
)
84 return XGetDefault(GDK_DISPLAY(), app_name
, key
);
88 * Default settings that are specific to pterm.
90 char *platform_default_s(char *name
)
92 if (!strcmp(name
, "Font"))
93 return "fixed"; /* COE_NORMAL works badly in an xterm */
97 int platform_default_i(char *name
, int def
)
99 if (!strcmp(name
, "CloseOnExit"))
100 return COE_ALWAYS
; /* COE_NORMAL works badly in an xterm */
104 void ldisc_update(void *frontend
, int echo
, int edit
)
107 * This is a stub in pterm. If I ever produce a Unix
108 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
109 * then it will require some termios manoeuvring analogous to
110 * that in the Windows plink.c, but here it's meaningless.
114 int askappend(void *frontend
, char *filename
)
117 * Logging in an xterm-alike is liable to be something you only
118 * do at serious diagnostic need. Hence, I'm going to take the
119 * easy option for now and assume we always want to overwrite
120 * log files. I can always make it properly configurable later.
125 void logevent(void *frontend
, char *string
)
128 * This is not a very helpful function: events are logged
129 * pretty much exclusively by the back end, and our pty back
130 * end is self-contained. So we need do nothing.
134 int font_dimension(void *frontend
, int which
)/* 0 for width, 1 for height */
136 struct gui_data
*inst
= (struct gui_data
*)frontend
;
139 return inst
->font_height
;
141 return inst
->font_width
;
145 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
146 * into a cooked one (SELECT, EXTEND, PASTE).
148 * In Unix, this is not configurable; the X button arrangement is
149 * rock-solid across all applications, everyone has a three-button
150 * mouse or a means of faking it, and there is no need to switch
151 * buttons around at all.
153 Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
155 /* struct gui_data *inst = (struct gui_data *)frontend; */
157 if (button
== MBT_LEFT
)
159 if (button
== MBT_MIDDLE
)
161 if (button
== MBT_RIGHT
)
163 return 0; /* shouldn't happen */
167 * Minimise or restore the window in response to a server-side
170 void set_iconic(void *frontend
, int iconic
)
173 * GTK 1.2 doesn't know how to do this.
175 #if GTK_CHECK_VERSION(2,0,0)
176 struct gui_data
*inst
= (struct gui_data
*)frontend
;
178 gtk_window_iconify(GTK_WINDOW(inst
->window
));
180 gtk_window_deiconify(GTK_WINDOW(inst
->window
));
185 * Move the window in response to a server-side request.
187 void move_window(void *frontend
, int x
, int y
)
189 struct gui_data
*inst
= (struct gui_data
*)frontend
;
191 * I assume that when the GTK version of this call is available
192 * we should use it. Not sure how it differs from the GDK one,
195 #if GTK_CHECK_VERSION(2,0,0)
196 gtk_window_move(GTK_WINDOW(inst
->window
), x
, y
);
198 gdk_window_move(inst
->window
->window
, x
, y
);
203 * Move the window to the top or bottom of the z-order in response
204 * to a server-side request.
206 void set_zorder(void *frontend
, int top
)
208 struct gui_data
*inst
= (struct gui_data
*)frontend
;
210 gdk_window_raise(inst
->window
->window
);
212 gdk_window_lower(inst
->window
->window
);
216 * Refresh the window in response to a server-side request.
218 void refresh_window(void *frontend
)
220 struct gui_data
*inst
= (struct gui_data
*)frontend
;
221 term_invalidate(inst
->term
);
225 * Maximise or restore the window in response to a server-side
228 void set_zoomed(void *frontend
, int zoomed
)
231 * GTK 1.2 doesn't know how to do this.
233 #if GTK_CHECK_VERSION(2,0,0)
234 struct gui_data
*inst
= (struct gui_data
*)frontend
;
236 gtk_window_maximize(GTK_WINDOW(inst
->window
));
238 gtk_window_unmaximize(GTK_WINDOW(inst
->window
));
243 * Report whether the window is iconic, for terminal reports.
245 int is_iconic(void *frontend
)
247 struct gui_data
*inst
= (struct gui_data
*)frontend
;
248 return !gdk_window_is_viewable(inst
->window
->window
);
252 * Report the window's position, for terminal reports.
254 void get_window_pos(void *frontend
, int *x
, int *y
)
256 struct gui_data
*inst
= (struct gui_data
*)frontend
;
258 * I assume that when the GTK version of this call is available
259 * we should use it. Not sure how it differs from the GDK one,
262 #if GTK_CHECK_VERSION(2,0,0)
263 gtk_window_get_position(GTK_WINDOW(inst
->window
), x
, y
);
265 gdk_window_get_position(inst
->window
->window
, x
, y
);
270 * Report the window's pixel size, for terminal reports.
272 void get_window_pixels(void *frontend
, int *x
, int *y
)
274 struct gui_data
*inst
= (struct gui_data
*)frontend
;
276 * I assume that when the GTK version of this call is available
277 * we should use it. Not sure how it differs from the GDK one,
280 #if GTK_CHECK_VERSION(2,0,0)
281 gtk_window_get_size(GTK_WINDOW(inst
->window
), x
, y
);
283 gdk_window_get_size(inst
->window
->window
, x
, y
);
288 * Return the window or icon title.
290 char *get_window_title(void *frontend
, int icon
)
292 struct gui_data
*inst
= (struct gui_data
*)frontend
;
293 return icon ? inst
->wintitle
: inst
->icontitle
;
296 gint
delete_window(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
299 * We could implement warn-on-close here if we really wanted
305 static void show_mouseptr(struct gui_data
*inst
, int show
)
307 if (!inst
->cfg
.hide_mouseptr
)
310 gdk_window_set_cursor(inst
->area
->window
, inst
->currcursor
);
312 gdk_window_set_cursor(inst
->area
->window
, inst
->blankcursor
);
313 inst
->mouseptr_visible
= show
;
316 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
318 struct gui_data
*inst
= (struct gui_data
*)data
;
319 int w
, h
, need_size
= 0;
321 w
= (event
->width
- 2*inst
->cfg
.window_border
) / inst
->font_width
;
322 h
= (event
->height
- 2*inst
->cfg
.window_border
) / inst
->font_height
;
324 if (w
!= inst
->cfg
.width
|| h
!= inst
->cfg
.height
) {
326 gdk_pixmap_unref(inst
->pixmap
);
330 inst
->cfg
.height
= h
;
336 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
337 (inst
->cfg
.width
* inst
->font_width
+
338 2*inst
->cfg
.window_border
),
339 (inst
->cfg
.height
* inst
->font_height
+
340 2*inst
->cfg
.window_border
), -1);
342 gc
= gdk_gc_new(inst
->area
->window
);
343 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
344 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
345 inst
->cfg
.width
* inst
->font_width
+ 2*inst
->cfg
.window_border
,
346 inst
->cfg
.height
* inst
->font_height
+ 2*inst
->cfg
.window_border
);
351 term_size(inst
->term
, h
, w
, inst
->cfg
.savelines
);
357 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
359 struct gui_data
*inst
= (struct gui_data
*)data
;
362 * Pass the exposed rectangle to terminal.c, which will call us
363 * back to do the actual painting.
366 gdk_draw_pixmap(widget
->window
,
367 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
369 event
->area
.x
, event
->area
.y
,
370 event
->area
.x
, event
->area
.y
,
371 event
->area
.width
, event
->area
.height
);
376 #define KEY_PRESSED(k) \
377 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
379 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
381 struct gui_data
*inst
= (struct gui_data
*)data
;
385 /* By default, nothing is generated. */
389 * If Alt is being released after typing an Alt+numberpad
390 * sequence, we should generate the code that was typed.
392 * Note that we only do this if more than one key was actually
393 * pressed - I don't think Alt+NumPad4 should be ^D or that
394 * Alt+NumPad3 should be ^C, for example. There's no serious
395 * inconvenience in having to type a zero before a single-digit
398 if (event
->type
== GDK_KEY_RELEASE
&&
399 (event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
400 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
) &&
401 inst
->alt_keycode
>= 0 && inst
->alt_digits
> 1) {
403 printf("Alt key up, keycode = %d\n", inst
->alt_keycode
);
405 output
[0] = inst
->alt_keycode
;
410 if (event
->type
== GDK_KEY_PRESS
) {
414 printf("keypress: keyval = %04x, state = %08x; string =",
415 event
->keyval
, event
->state
);
416 for (i
= 0; event
->string
[i
]; i
++)
417 printf(" %02x", (unsigned char) event
->string
[i
]);
423 * NYI: Compose key (!!! requires Unicode faff before even trying)
427 * If Alt has just been pressed, we start potentially
428 * accumulating an Alt+numberpad code. We do this by
429 * setting alt_keycode to -1 (nothing yet but plausible).
431 if ((event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
432 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
)) {
433 inst
->alt_keycode
= -1;
434 inst
->alt_digits
= 0;
435 goto done
; /* this generates nothing else */
439 * If we're seeing a numberpad key press with Mod1 down,
440 * consider adding it to alt_keycode if that's sensible.
441 * Anything _else_ with Mod1 down cancels any possibility
442 * of an ALT keycode: we set alt_keycode to -2.
444 if ((event
->state
& GDK_MOD1_MASK
) && inst
->alt_keycode
!= -2) {
446 switch (event
->keyval
) {
447 case GDK_KP_0
: case GDK_KP_Insert
: digit
= 0; break;
448 case GDK_KP_1
: case GDK_KP_End
: digit
= 1; break;
449 case GDK_KP_2
: case GDK_KP_Down
: digit
= 2; break;
450 case GDK_KP_3
: case GDK_KP_Page_Down
: digit
= 3; break;
451 case GDK_KP_4
: case GDK_KP_Left
: digit
= 4; break;
452 case GDK_KP_5
: case GDK_KP_Begin
: digit
= 5; break;
453 case GDK_KP_6
: case GDK_KP_Right
: digit
= 6; break;
454 case GDK_KP_7
: case GDK_KP_Home
: digit
= 7; break;
455 case GDK_KP_8
: case GDK_KP_Up
: digit
= 8; break;
456 case GDK_KP_9
: case GDK_KP_Page_Up
: digit
= 9; break;
459 inst
->alt_keycode
= -2; /* it's invalid */
462 printf("Adding digit %d to keycode %d", digit
,
465 if (inst
->alt_keycode
== -1)
466 inst
->alt_keycode
= digit
; /* one-digit code */
468 inst
->alt_keycode
= inst
->alt_keycode
* 10 + digit
;
471 printf(" gives new code %d\n", inst
->alt_keycode
);
473 /* Having used this digit, we now do nothing more with it. */
479 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
482 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
483 term_scroll(inst
->term
, 0, -inst
->cfg
.height
/2);
486 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_CONTROL_MASK
)) {
487 term_scroll(inst
->term
, 0, -1);
490 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
491 term_scroll(inst
->term
, 0, +inst
->cfg
.height
/2);
494 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_CONTROL_MASK
)) {
495 term_scroll(inst
->term
, 0, +1);
500 * Neither does Shift-Ins.
502 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
507 /* ALT+things gives leading Escape. */
509 strncpy(output
+1, event
->string
, 31);
511 end
= strlen(output
);
512 if (event
->state
& GDK_MOD1_MASK
) {
514 if (end
== 1) end
= 0;
518 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
519 if (!event
->string
[0] && event
->keyval
== '`' &&
520 (event
->state
& GDK_CONTROL_MASK
)) {
525 /* Control-Break is the same as Control-C */
526 if (event
->keyval
== GDK_Break
&&
527 (event
->state
& GDK_CONTROL_MASK
)) {
532 /* Control-2, Control-Space and Control-@ are NUL */
533 if (!event
->string
[0] &&
534 (event
->keyval
== ' ' || event
->keyval
== '2' ||
535 event
->keyval
== '@') &&
536 (event
->state
& (GDK_SHIFT_MASK
|
537 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
542 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
543 if (!event
->string
[0] && event
->keyval
== ' ' &&
544 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
545 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
550 /* We don't let GTK tell us what Backspace is! We know better. */
551 if (event
->keyval
== GDK_BackSpace
&&
552 !(event
->state
& GDK_SHIFT_MASK
)) {
553 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x7F' : '\x08';
556 /* For Shift Backspace, do opposite of what is configured. */
557 if (event
->keyval
== GDK_BackSpace
&&
558 (event
->state
& GDK_SHIFT_MASK
)) {
559 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x08' : '\x7F';
563 /* Shift-Tab is ESC [ Z */
564 if (event
->keyval
== GDK_ISO_Left_Tab
||
565 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
566 end
= 1 + sprintf(output
+1, "\033[Z");
570 * NetHack keypad mode.
572 if (inst
->cfg
.nethack_keypad
) {
574 switch (event
->keyval
) {
575 case GDK_KP_1
: case GDK_KP_End
: keys
= "bB"; break;
576 case GDK_KP_2
: case GDK_KP_Down
: keys
= "jJ"; break;
577 case GDK_KP_3
: case GDK_KP_Page_Down
: keys
= "nN"; break;
578 case GDK_KP_4
: case GDK_KP_Left
: keys
= "hH"; break;
579 case GDK_KP_5
: case GDK_KP_Begin
: keys
= ".."; break;
580 case GDK_KP_6
: case GDK_KP_Right
: keys
= "lL"; break;
581 case GDK_KP_7
: case GDK_KP_Home
: keys
= "yY"; break;
582 case GDK_KP_8
: case GDK_KP_Up
: keys
= "kK"; break;
583 case GDK_KP_9
: case GDK_KP_Page_Up
: keys
= "uU"; break;
587 if (event
->state
& GDK_SHIFT_MASK
)
596 * Application keypad mode.
598 if (inst
->term
->app_keypad_keys
&& !inst
->cfg
.no_applic_k
) {
600 switch (event
->keyval
) {
601 case GDK_Num_Lock
: xkey
= 'P'; break;
602 case GDK_KP_Divide
: xkey
= 'Q'; break;
603 case GDK_KP_Multiply
: xkey
= 'R'; break;
604 case GDK_KP_Subtract
: xkey
= 'S'; break;
606 * Keypad + is tricky. It covers a space that would
607 * be taken up on the VT100 by _two_ keys; so we
608 * let Shift select between the two. Worse still,
609 * in xterm function key mode we change which two...
612 if (inst
->cfg
.funky_type
== 2) {
613 if (event
->state
& GDK_SHIFT_MASK
)
617 } else if (event
->state
& GDK_SHIFT_MASK
)
622 case GDK_KP_Enter
: xkey
= 'M'; break;
623 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
624 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
625 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
626 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
627 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
628 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
629 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
630 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
631 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
632 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
633 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
636 if (inst
->term
->vt52_mode
) {
637 if (xkey
>= 'P' && xkey
<= 'S')
638 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
640 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
642 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
648 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
649 * for integer decimal nn.)
651 * We also deal with the weird ones here. Linux VCs replace F1
652 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
653 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
658 switch (event
->keyval
) {
660 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
663 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
666 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
669 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
672 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
675 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
678 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
681 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
684 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
687 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
720 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
721 case GDK_Home
: case GDK_KP_Home
:
724 case GDK_Insert
: case GDK_KP_Insert
:
727 case GDK_Delete
: case GDK_KP_Delete
:
730 case GDK_End
: case GDK_KP_End
:
733 case GDK_Page_Up
: case GDK_KP_Page_Up
:
736 case GDK_Page_Down
: case GDK_KP_Page_Down
:
740 /* Reorder edit keys to physical order */
741 if (inst
->cfg
.funky_type
== 3 && code
<= 6)
742 code
= "\0\2\1\4\5\3\6"[code
];
744 if (inst
->term
->vt52_mode
&& code
> 0 && code
<= 6) {
745 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
749 if (inst
->cfg
.funky_type
== 5 && /* SCO function keys */
750 code
>= 11 && code
<= 34) {
751 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
753 switch (event
->keyval
) {
754 case GDK_F1
: index
= 0; break;
755 case GDK_F2
: index
= 1; break;
756 case GDK_F3
: index
= 2; break;
757 case GDK_F4
: index
= 3; break;
758 case GDK_F5
: index
= 4; break;
759 case GDK_F6
: index
= 5; break;
760 case GDK_F7
: index
= 6; break;
761 case GDK_F8
: index
= 7; break;
762 case GDK_F9
: index
= 8; break;
763 case GDK_F10
: index
= 9; break;
764 case GDK_F11
: index
= 10; break;
765 case GDK_F12
: index
= 11; break;
767 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
768 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
769 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
772 if (inst
->cfg
.funky_type
== 5 && /* SCO small keypad */
773 code
>= 1 && code
<= 6) {
774 char codes
[] = "HL.FIG";
779 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
783 if ((inst
->term
->vt52_mode
|| inst
->cfg
.funky_type
== 4) &&
784 code
>= 11 && code
<= 24) {
790 if (inst
->term
->vt52_mode
)
791 end
= 1 + sprintf(output
+1,
792 "\x1B%c", code
+ 'P' - 11 - offt
);
794 end
= 1 + sprintf(output
+1,
795 "\x1BO%c", code
+ 'P' - 11 - offt
);
798 if (inst
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
799 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
802 if (inst
->cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
803 if (inst
->term
->vt52_mode
)
804 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
806 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
809 if (inst
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
810 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
814 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
820 * Cursor keys. (This includes the numberpad cursor keys,
821 * if we haven't already done them due to app keypad mode.)
823 * Here we also process un-numlocked un-appkeypadded KP5,
824 * which sends ESC [ G.
828 switch (event
->keyval
) {
829 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
830 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
831 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
832 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
833 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
837 * The arrow keys normally do ESC [ A and so on. In
838 * app cursor keys mode they do ESC O A instead.
839 * Ctrl toggles the two modes.
841 if (inst
->term
->vt52_mode
) {
842 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
843 } else if (!inst
->term
->app_cursor_keys
^
844 !(event
->state
& GDK_CONTROL_MASK
)) {
845 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
847 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
860 printf("generating sequence:");
861 for (i
= start
; i
< end
; i
++)
862 printf(" %02x", (unsigned char) output
[i
]);
866 if (!inst
->direct_to_font
) {
868 * The stuff we've just generated is assumed to be
869 * ISO-8859-1! This sounds insane, but `man
870 * XLookupString' agrees: strings of this type returned
871 * from the X server are hardcoded to 8859-1. Strictly
872 * speaking we should be doing this using some sort of
873 * GtkIMContext, which (if we're lucky) would give us
874 * our data directly in Unicode; but that's not
875 * supported in GTK 1.2 as far as I can tell, and it's
876 * poorly documented even in 2.0, so it'll have to
879 lpage_send(inst
->ldisc
, CS_ISO8859_1
, output
+start
, end
-start
, 1);
882 * In direct-to-font mode, we just send the string
883 * exactly as we received it.
885 ldisc_send(inst
->ldisc
, output
+start
, end
-start
, 1);
888 show_mouseptr(inst
, 0);
889 term_seen_key_event(inst
->term
);
890 term_out(inst
->term
);
896 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
898 struct gui_data
*inst
= (struct gui_data
*)data
;
899 int shift
, ctrl
, alt
, x
, y
, button
, act
;
901 show_mouseptr(inst
, 1);
903 if (event
->button
== 4 && event
->type
== GDK_BUTTON_PRESS
) {
904 term_scroll(inst
->term
, 0, -5);
907 if (event
->button
== 5 && event
->type
== GDK_BUTTON_PRESS
) {
908 term_scroll(inst
->term
, 0, +5);
912 shift
= event
->state
& GDK_SHIFT_MASK
;
913 ctrl
= event
->state
& GDK_CONTROL_MASK
;
914 alt
= event
->state
& GDK_MOD1_MASK
;
915 if (event
->button
== 1)
917 else if (event
->button
== 2)
919 else if (event
->button
== 3)
922 return FALSE
; /* don't even know what button! */
924 switch (event
->type
) {
925 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
926 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
927 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
928 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
929 default: return FALSE
; /* don't know this event type */
932 if (send_raw_mouse
&& !(inst
->cfg
.mouse_override
&& shift
) &&
933 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
934 return TRUE
; /* we ignore these in raw mouse mode */
936 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
937 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
939 term_mouse(inst
->term
, button
, act
, x
, y
, shift
, ctrl
, alt
);
944 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
946 struct gui_data
*inst
= (struct gui_data
*)data
;
947 int shift
, ctrl
, alt
, x
, y
, button
;
949 show_mouseptr(inst
, 1);
951 shift
= event
->state
& GDK_SHIFT_MASK
;
952 ctrl
= event
->state
& GDK_CONTROL_MASK
;
953 alt
= event
->state
& GDK_MOD1_MASK
;
954 if (event
->state
& GDK_BUTTON1_MASK
)
956 else if (event
->state
& GDK_BUTTON2_MASK
)
958 else if (event
->state
& GDK_BUTTON3_MASK
)
961 return FALSE
; /* don't even know what button! */
963 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
964 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
966 term_mouse(inst
->term
, button
, MA_DRAG
, x
, y
, shift
, ctrl
, alt
);
971 void done_with_pty(struct gui_data
*inst
)
973 extern void pty_close(void);
975 if (inst
->master_fd
>= 0) {
977 inst
->master_fd
= -1;
978 gtk_input_remove(inst
->master_func_id
);
981 if (!inst
->exited
&& inst
->back
->exitcode(inst
->backhandle
) >= 0) {
982 int exitcode
= inst
->back
->exitcode(inst
->backhandle
);
985 clean
= WIFEXITED(exitcode
) && (WEXITSTATUS(exitcode
) == 0);
988 * Terminate now, if the Close On Exit setting is
991 if (inst
->cfg
.close_on_exit
== COE_ALWAYS
||
992 (inst
->cfg
.close_on_exit
== COE_NORMAL
&& clean
))
996 * Otherwise, output an indication that the session has
1001 if (WIFEXITED(exitcode
))
1002 sprintf(message
, "\r\n[pterm: process terminated with exit"
1003 " code %d]\r\n", WEXITSTATUS(exitcode
));
1004 else if (WIFSIGNALED(exitcode
))
1005 #ifdef HAVE_NO_STRSIGNAL
1006 sprintf(message
, "\r\n[pterm: process terminated on signal"
1007 " %d]\r\n", WTERMSIG(exitcode
));
1009 sprintf(message
, "\r\n[pterm: process terminated on signal"
1010 " %d (%.400s)]\r\n", WTERMSIG(exitcode
),
1011 strsignal(WTERMSIG(exitcode
)));
1013 from_backend((void *)inst
->term
, 0, message
, strlen(message
));
1019 void frontend_keypress(void *handle
)
1021 struct gui_data
*inst
= (struct gui_data
*)handle
;
1024 * If our child process has exited but not closed, terminate on
1031 gint
timer_func(gpointer data
)
1033 struct gui_data
*inst
= (struct gui_data
*)data
;
1035 if (inst
->back
->exitcode(inst
->backhandle
) >= 0) {
1037 * The primary child process died. We could keep the
1038 * terminal open for remaining subprocesses to output to,
1039 * but conventional wisdom seems to feel that that's the
1040 * Wrong Thing for an xterm-alike, so we bail out now
1041 * (though we don't necessarily _close_ the window,
1042 * depending on the state of Close On Exit). This would be
1043 * easy enough to change or make configurable if necessary.
1045 done_with_pty(inst
);
1048 term_update(inst
->term
);
1049 term_blink(inst
->term
, 0);
1053 void pty_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
1055 struct gui_data
*inst
= (struct gui_data
*)data
;
1059 ret
= read(sourcefd
, buf
, sizeof(buf
));
1062 * Clean termination condition is that either ret == 0, or ret
1063 * < 0 and errno == EIO. Not sure why the latter, but it seems
1066 if (ret
== 0 || (ret
< 0 && errno
== EIO
)) {
1067 done_with_pty(inst
);
1068 } else if (ret
< 0) {
1069 perror("read pty master");
1072 from_backend(inst
->term
, 0, buf
, ret
);
1073 term_blink(inst
->term
, 1);
1074 term_out(inst
->term
);
1077 void destroy(GtkWidget
*widget
, gpointer data
)
1082 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
1084 struct gui_data
*inst
= (struct gui_data
*)data
;
1085 inst
->term
->has_focus
= event
->in
;
1086 term_out(inst
->term
);
1087 term_update(inst
->term
);
1088 show_mouseptr(inst
, 1);
1093 * set or clear the "raw mouse message" mode
1095 void set_raw_mouse_mode(void *frontend
, int activate
)
1097 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1098 activate
= activate
&& !inst
->cfg
.no_mouse_rep
;
1099 send_raw_mouse
= activate
;
1101 inst
->currcursor
= inst
->rawcursor
;
1103 inst
->currcursor
= inst
->textcursor
;
1104 show_mouseptr(inst
, inst
->mouseptr_visible
);
1107 void request_resize(void *frontend
, int w
, int h
)
1109 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1110 int large_x
, large_y
;
1111 int offset_x
, offset_y
;
1113 GtkRequisition inner
, outer
;
1116 * This is a heinous hack dreamed up by the gnome-terminal
1117 * people to get around a limitation in gtk. The problem is
1118 * that in order to set the size correctly we really need to be
1119 * calling gtk_window_resize - but that needs to know the size
1120 * of the _whole window_, not the drawing area. So what we do
1121 * is to set an artificially huge size request on the drawing
1122 * area, recompute the resulting size request on the window,
1123 * and look at the difference between the two. That gives us
1124 * the x and y offsets we need to translate drawing area size
1125 * into window size for real, and then we call
1126 * gtk_window_resize.
1130 * We start by retrieving the current size of the whole window.
1131 * Adding a bit to _that_ will give us a value we can use as a
1132 * bogus size request which guarantees to be bigger than the
1133 * current size of the drawing area.
1135 get_window_pixels(inst
, &large_x
, &large_y
);
1139 #if GTK_CHECK_VERSION(2,0,0)
1140 gtk_widget_set_size_request(inst
->area
, large_x
, large_y
);
1142 gtk_widget_set_usize(inst
->area
, large_x
, large_y
);
1144 gtk_widget_size_request(inst
->area
, &inner
);
1145 gtk_widget_size_request(inst
->window
, &outer
);
1147 offset_x
= outer
.width
- inner
.width
;
1148 offset_y
= outer
.height
- inner
.height
;
1150 area_x
= inst
->font_width
* w
+ 2*inst
->cfg
.window_border
;
1151 area_y
= inst
->font_height
* h
+ 2*inst
->cfg
.window_border
;
1154 * Now we must set the size request on the drawing area back to
1155 * something sensible before we commit the real resize. Best
1156 * way to do this, I think, is to set it to what the size is
1157 * really going to end up being.
1159 #if GTK_CHECK_VERSION(2,0,0)
1160 gtk_widget_set_size_request(inst
->area
, area_x
, area_y
);
1162 gtk_widget_set_usize(inst
->area
, area_x
, area_y
);
1165 #if GTK_CHECK_VERSION(2,0,0)
1166 gtk_window_resize(GTK_WINDOW(inst
->window
),
1167 area_x
+ offset_x
, area_y
+ offset_y
);
1169 gdk_window_resize(inst
->window
->window
,
1170 area_x
+ offset_x
, area_y
+ offset_y
);
1174 static void real_palette_set(struct gui_data
*inst
, int n
, int r
, int g
, int b
)
1176 gboolean success
[1];
1178 inst
->cols
[n
].red
= r
* 0x0101;
1179 inst
->cols
[n
].green
= g
* 0x0101;
1180 inst
->cols
[n
].blue
= b
* 0x0101;
1182 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
+ n
, 1,
1183 FALSE
, FALSE
, success
);
1185 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1189 void set_window_background(struct gui_data
*inst
)
1191 if (inst
->area
&& inst
->area
->window
)
1192 gdk_window_set_background(inst
->area
->window
, &inst
->cols
[18]);
1193 if (inst
->window
&& inst
->window
->window
)
1194 gdk_window_set_background(inst
->window
->window
, &inst
->cols
[18]);
1197 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1199 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1200 static const int first
[21] = {
1201 0, 2, 4, 6, 8, 10, 12, 14,
1202 1, 3, 5, 7, 9, 11, 13, 15,
1205 real_palette_set(inst
, first
[n
], r
, g
, b
);
1207 real_palette_set(inst
, first
[n
] + 1, r
, g
, b
);
1209 set_window_background(inst
);
1212 void palette_reset(void *frontend
)
1214 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1215 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1216 static const int ww
[] = {
1217 6, 7, 8, 9, 10, 11, 12, 13,
1218 14, 15, 16, 17, 18, 19, 20, 21,
1221 gboolean success
[NCOLOURS
];
1224 assert(lenof(ww
) == NCOLOURS
);
1226 if (!inst
->colmap
) {
1227 inst
->colmap
= gdk_colormap_get_system();
1229 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
, NCOLOURS
);
1232 for (i
= 0; i
< NCOLOURS
; i
++) {
1233 inst
->cols
[i
].red
= inst
->cfg
.colours
[ww
[i
]][0] * 0x0101;
1234 inst
->cols
[i
].green
= inst
->cfg
.colours
[ww
[i
]][1] * 0x0101;
1235 inst
->cols
[i
].blue
= inst
->cfg
.colours
[ww
[i
]][2] * 0x0101;
1238 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
1239 FALSE
, FALSE
, success
);
1240 for (i
= 0; i
< NCOLOURS
; i
++) {
1242 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1243 i
, inst
->cfg
.colours
[i
][0], inst
->cfg
.colours
[i
][1], inst
->cfg
.colours
[i
][2]);
1246 set_window_background(inst
);
1249 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
1251 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1252 if (inst
->pasteout_data
)
1253 sfree(inst
->pasteout_data
);
1254 if (inst
->pasteout_data_utf8
)
1255 sfree(inst
->pasteout_data_utf8
);
1258 * Set up UTF-8 paste data. This only happens if we aren't in
1259 * direct-to-font mode using the D800 hack.
1261 if (!inst
->direct_to_font
) {
1262 wchar_t *tmp
= data
;
1265 inst
->pasteout_data_utf8
= smalloc(len
*6);
1266 inst
->pasteout_data_utf8_len
= len
*6;
1267 inst
->pasteout_data_utf8_len
=
1268 charset_from_unicode(&tmp
, &tmplen
, inst
->pasteout_data_utf8
,
1269 inst
->pasteout_data_utf8_len
,
1270 CS_UTF8
, NULL
, NULL
, 0);
1271 if (inst
->pasteout_data_utf8_len
== 0) {
1272 sfree(inst
->pasteout_data_utf8
);
1273 inst
->pasteout_data_utf8
= NULL
;
1275 inst
->pasteout_data_utf8
=
1276 srealloc(inst
->pasteout_data_utf8
,
1277 inst
->pasteout_data_utf8_len
);
1280 inst
->pasteout_data_utf8
= NULL
;
1281 inst
->pasteout_data_utf8_len
= 0;
1284 inst
->pasteout_data
= smalloc(len
*6);
1285 inst
->pasteout_data_len
= len
*6;
1286 inst
->pasteout_data_len
= wc_to_mb(line_codepage
, 0, data
, len
,
1287 inst
->pasteout_data
,
1288 inst
->pasteout_data_len
, NULL
, NULL
);
1289 if (inst
->pasteout_data_len
== 0) {
1290 sfree(inst
->pasteout_data
);
1291 inst
->pasteout_data
= NULL
;
1293 inst
->pasteout_data
=
1294 srealloc(inst
->pasteout_data
, inst
->pasteout_data_len
);
1297 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
1298 GDK_CURRENT_TIME
)) {
1299 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1300 GDK_SELECTION_TYPE_STRING
, 1);
1301 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1302 inst
->compound_text_atom
, 1);
1303 if (inst
->pasteout_data_utf8
)
1304 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1305 inst
->utf8_string_atom
, 1);
1309 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1310 guint info
, guint time_stamp
, gpointer data
)
1312 struct gui_data
*inst
= (struct gui_data
*)data
;
1313 if (seldata
->target
== inst
->utf8_string_atom
)
1314 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1315 inst
->pasteout_data_utf8
,
1316 inst
->pasteout_data_utf8_len
);
1318 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1319 inst
->pasteout_data
, inst
->pasteout_data_len
);
1322 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1325 struct gui_data
*inst
= (struct gui_data
*)data
;
1326 term_deselect(inst
->term
);
1327 if (inst
->pasteout_data
)
1328 sfree(inst
->pasteout_data
);
1329 if (inst
->pasteout_data_utf8
)
1330 sfree(inst
->pasteout_data_utf8
);
1331 inst
->pasteout_data
= NULL
;
1332 inst
->pasteout_data_len
= 0;
1333 inst
->pasteout_data_utf8
= NULL
;
1334 inst
->pasteout_data_utf8_len
= 0;
1338 void request_paste(void *frontend
)
1340 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1342 * In Unix, pasting is asynchronous: all we can do at the
1343 * moment is to call gtk_selection_convert(), and when the data
1344 * comes back _then_ we can call term_do_paste().
1347 if (!inst
->direct_to_font
) {
1349 * First we attempt to retrieve the selection as a UTF-8
1350 * string (which we will convert to the correct code page
1351 * before sending to the session, of course). If that
1352 * fails, selection_received() will be informed and will
1353 * fall back to an ordinary string.
1355 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1356 inst
->utf8_string_atom
, GDK_CURRENT_TIME
);
1359 * If we're in direct-to-font mode, we disable UTF-8
1360 * pasting, and go straight to ordinary string data.
1362 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1363 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1367 gint
idle_paste_func(gpointer data
); /* forward ref */
1369 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1370 guint time
, gpointer data
)
1372 struct gui_data
*inst
= (struct gui_data
*)data
;
1374 if (seldata
->target
== inst
->utf8_string_atom
&& seldata
->length
<= 0) {
1376 * Failed to get a UTF-8 selection string. Try an ordinary
1379 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1380 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1385 * Any other failure should just go foom.
1387 if (seldata
->length
<= 0 ||
1388 (seldata
->type
!= GDK_SELECTION_TYPE_STRING
&&
1389 seldata
->type
!= inst
->utf8_string_atom
))
1390 return; /* Nothing happens. */
1392 if (inst
->pastein_data
)
1393 sfree(inst
->pastein_data
);
1395 inst
->pastein_data
= smalloc(seldata
->length
* sizeof(wchar_t));
1396 inst
->pastein_data_len
= seldata
->length
;
1397 inst
->pastein_data_len
=
1398 mb_to_wc((seldata
->type
== inst
->utf8_string_atom ?
1399 CS_UTF8
: line_codepage
),
1400 0, seldata
->data
, seldata
->length
,
1401 inst
->pastein_data
, inst
->pastein_data_len
);
1403 term_do_paste(inst
->term
);
1405 if (term_paste_pending(inst
->term
))
1406 inst
->term_paste_idle_id
= gtk_idle_add(idle_paste_func
, inst
);
1409 gint
idle_paste_func(gpointer data
)
1411 struct gui_data
*inst
= (struct gui_data
*)data
;
1413 if (term_paste_pending(inst
->term
))
1414 term_paste(inst
->term
);
1416 gtk_idle_remove(inst
->term_paste_idle_id
);
1422 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
1424 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1427 *p
= inst
->pastein_data
;
1428 *len
= inst
->pastein_data_len
;
1432 void set_title(void *frontend
, char *title
)
1434 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1435 strncpy(inst
->wintitle
, title
, lenof(inst
->wintitle
));
1436 inst
->wintitle
[lenof(inst
->wintitle
)-1] = '\0';
1437 gtk_window_set_title(GTK_WINDOW(inst
->window
), inst
->wintitle
);
1440 void set_icon(void *frontend
, char *title
)
1442 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1443 strncpy(inst
->icontitle
, title
, lenof(inst
->icontitle
));
1444 inst
->icontitle
[lenof(inst
->icontitle
)-1] = '\0';
1445 gdk_window_set_icon_name(inst
->window
->window
, inst
->icontitle
);
1448 void set_sbar(void *frontend
, int total
, int start
, int page
)
1450 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1451 if (!inst
->cfg
.scrollbar
)
1453 inst
->sbar_adjust
->lower
= 0;
1454 inst
->sbar_adjust
->upper
= total
;
1455 inst
->sbar_adjust
->value
= start
;
1456 inst
->sbar_adjust
->page_size
= page
;
1457 inst
->sbar_adjust
->step_increment
= 1;
1458 inst
->sbar_adjust
->page_increment
= page
/2;
1459 inst
->ignore_sbar
= TRUE
;
1460 gtk_adjustment_changed(inst
->sbar_adjust
);
1461 inst
->ignore_sbar
= FALSE
;
1464 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
1466 struct gui_data
*inst
= (struct gui_data
*)data
;
1468 if (!inst
->cfg
.scrollbar
)
1470 if (!inst
->ignore_sbar
)
1471 term_scroll(inst
->term
, 1, (int)adj
->value
);
1474 void sys_cursor(void *frontend
, int x
, int y
)
1477 * This is meaningless under X.
1482 * This is still called when mode==BELL_VISUAL, even though the
1483 * visual bell is handled entirely within terminal.c, because we
1484 * may want to perform additional actions on any kind of bell (for
1485 * example, taskbar flashing in Windows).
1487 void beep(void *frontend
, int mode
)
1489 if (mode
!= BELL_VISUAL
)
1493 int char_width(Context ctx
, int uc
)
1496 * Under X, any fixed-width font really _is_ fixed-width.
1497 * Double-width characters will be dealt with using a separate
1498 * font. For the moment we can simply return 1.
1503 Context
get_ctx(void *frontend
)
1505 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1506 struct draw_ctx
*dctx
;
1508 if (!inst
->area
->window
)
1511 dctx
= smalloc(sizeof(*dctx
));
1513 dctx
->gc
= gdk_gc_new(inst
->area
->window
);
1517 void free_ctx(Context ctx
)
1519 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1520 /* struct gui_data *inst = dctx->inst; */
1521 GdkGC
*gc
= dctx
->gc
;
1527 * Draw a line of text in the window, at given character
1528 * coordinates, in given attributes.
1530 * We are allowed to fiddle with the contents of `text'.
1532 void do_text_internal(Context ctx
, int x
, int y
, char *text
, int len
,
1533 unsigned long attr
, int lattr
)
1535 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1536 struct gui_data
*inst
= dctx
->inst
;
1537 GdkGC
*gc
= dctx
->gc
;
1539 int nfg
, nbg
, t
, fontid
, shadow
, rlen
, widefactor
;
1541 nfg
= 2 * ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1542 nbg
= 2 * ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1543 if (attr
& ATTR_REVERSE
) {
1548 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
1550 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
1552 if (attr
& TATTR_ACTCURS
) {
1557 fontid
= shadow
= 0;
1559 if (attr
& ATTR_WIDE
) {
1566 if ((attr
& ATTR_BOLD
) && !inst
->cfg
.bold_colour
) {
1567 if (inst
->fonts
[fontid
| 1])
1573 if (lattr
!= LATTR_NORM
) {
1575 if (x
>= inst
->term
->cols
)
1577 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1578 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1586 r
.x
= x
*inst
->font_width
+inst
->cfg
.window_border
;
1587 r
.y
= y
*inst
->font_height
+inst
->cfg
.window_border
;
1588 r
.width
= rlen
*widefactor
*inst
->font_width
;
1589 r
.height
= inst
->font_height
;
1590 gdk_gc_set_clip_rectangle(gc
, &r
);
1593 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
1594 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
1595 x
*inst
->font_width
+inst
->cfg
.window_border
,
1596 y
*inst
->font_height
+inst
->cfg
.window_border
,
1597 rlen
*widefactor
*inst
->font_width
, inst
->font_height
);
1599 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
1606 wcs
= smalloc(sizeof(wchar_t) * (len
+1));
1607 for (i
= 0; i
< len
; i
++) {
1608 wcs
[i
] = (wchar_t) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
1611 if (inst
->fonts
[fontid
] == NULL
) {
1613 * The font for this contingency does not exist.
1614 * Typically this means we've been given ATTR_WIDE
1615 * character and have no wide font. So we display
1616 * nothing at all; such is life.
1618 } else if (inst
->fontinfo
[fontid
].is_wide
) {
1620 * At least one version of gdk_draw_text_wc() has a
1621 * weird bug whereby it reads `len' elements of the
1622 * input string, but only draws `len/2'. Hence I'm
1623 * going to make its input array twice as long as it
1624 * theoretically needs to be, and pass in twice the
1625 * actual number of characters. If a fixed gdk actually
1626 * takes the doubled length seriously, then (a) the
1627 * array will stand scrutiny up to the full length, (b)
1628 * the spare elements of the array are full of zeroes
1629 * which will probably be an empty glyph in the font,
1630 * and (c) the clip rectangle should prevent it causing
1633 gwcs
= smalloc(sizeof(GdkWChar
) * (len
*2+1));
1634 memset(gwcs
, 0, sizeof(GdkWChar
) * (len
*2+1));
1636 * FIXME: when we have a wide-char equivalent of
1637 * from_unicode, use it instead of this.
1639 for (i
= 0; i
<= len
; i
++)
1641 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1642 x
*inst
->font_width
+inst
->cfg
.window_border
,
1643 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1647 gcs
= smalloc(sizeof(GdkWChar
) * (len
+1));
1648 wc_to_mb(inst
->fontinfo
[fontid
].charset
, 0,
1649 wcs
, len
, gcs
, len
, ".", NULL
);
1650 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1651 x
*inst
->font_width
+inst
->cfg
.window_border
,
1652 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1660 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1661 x
*inst
->font_width
+inst
->cfg
.window_border
+ inst
->cfg
.shadowboldoffset
,
1662 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1666 if (attr
& ATTR_UNDER
) {
1667 int uheight
= inst
->fonts
[0]->ascent
+ 1;
1668 if (uheight
>= inst
->font_height
)
1669 uheight
= inst
->font_height
- 1;
1670 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+inst
->cfg
.window_border
,
1671 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
,
1672 (x
+len
)*widefactor
*inst
->font_width
-1+inst
->cfg
.window_border
,
1673 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
);
1676 if (lattr
!= LATTR_NORM
) {
1678 * I can't find any plausible StretchBlt equivalent in the
1679 * X server, so I'm going to do this the slow and painful
1680 * way. This will involve repeated calls to
1681 * gdk_draw_pixmap() to stretch the text horizontally. It's
1682 * O(N^2) in time and O(N) in network bandwidth, but you
1683 * try thinking of a better way. :-(
1686 for (i
= 0; i
< len
* widefactor
* inst
->font_width
; i
++) {
1687 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1688 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
,
1689 y
*inst
->font_height
+inst
->cfg
.window_border
,
1690 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
+1,
1691 y
*inst
->font_height
+inst
->cfg
.window_border
,
1692 len
* inst
->font_width
- i
, inst
->font_height
);
1695 if (lattr
!= LATTR_WIDE
) {
1697 /* Now stretch vertically, in the same way. */
1698 if (lattr
== LATTR_BOT
)
1702 for (i
= 0; i
< inst
->font_height
; i
+=2) {
1703 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1704 x
*inst
->font_width
+inst
->cfg
.window_border
,
1705 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*i
+db
,
1706 x
*widefactor
*inst
->font_width
+inst
->cfg
.window_border
,
1707 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*(i
+1),
1708 len
* inst
->font_width
, inst
->font_height
-i
-1);
1714 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
1715 unsigned long attr
, int lattr
)
1717 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1718 struct gui_data
*inst
= dctx
->inst
;
1719 GdkGC
*gc
= dctx
->gc
;
1722 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1724 if (attr
& ATTR_WIDE
) {
1730 if (lattr
!= LATTR_NORM
) {
1732 if (x
>= inst
->term
->cols
)
1734 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1735 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1739 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1740 x
*inst
->font_width
+inst
->cfg
.window_border
,
1741 y
*inst
->font_height
+inst
->cfg
.window_border
,
1742 x
*inst
->font_width
+inst
->cfg
.window_border
,
1743 y
*inst
->font_height
+inst
->cfg
.window_border
,
1744 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1747 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1748 unsigned long attr
, int lattr
)
1750 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1751 struct gui_data
*inst
= dctx
->inst
;
1752 GdkGC
*gc
= dctx
->gc
;
1754 int passive
, widefactor
;
1756 if (attr
& TATTR_PASCURS
) {
1757 attr
&= ~TATTR_PASCURS
;
1761 if ((attr
& TATTR_ACTCURS
) && inst
->cfg
.cursor_type
!= 0) {
1762 attr
&= ~TATTR_ACTCURS
;
1764 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1766 if (attr
& ATTR_WIDE
) {
1772 if (lattr
!= LATTR_NORM
) {
1774 if (x
>= inst
->term
->cols
)
1776 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1777 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1781 if (inst
->cfg
.cursor_type
== 0) {
1783 * An active block cursor will already have been done by
1784 * the above do_text call, so we only need to do anything
1788 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1789 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
1790 x
*inst
->font_width
+inst
->cfg
.window_border
,
1791 y
*inst
->font_height
+inst
->cfg
.window_border
,
1792 len
*inst
->font_width
-1, inst
->font_height
-1);
1796 int startx
, starty
, dx
, dy
, length
, i
;
1800 if ((attr
& ATTR_WIDE
) || lattr
!= LATTR_NORM
)
1801 char_width
= 2*inst
->font_width
;
1803 char_width
= inst
->font_width
;
1805 if (inst
->cfg
.cursor_type
== 1) {
1806 uheight
= inst
->fonts
[0]->ascent
+ 1;
1807 if (uheight
>= inst
->font_height
)
1808 uheight
= inst
->font_height
- 1;
1810 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
;
1811 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
+ uheight
;
1814 length
= len
* char_width
;
1817 if (attr
& TATTR_RIGHTCURS
)
1818 xadjust
= char_width
- 1;
1819 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
+ xadjust
;
1820 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
;
1823 length
= inst
->font_height
;
1826 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1828 for (i
= 0; i
< length
; i
++) {
1830 gdk_draw_point(inst
->pixmap
, gc
, startx
, starty
);
1836 gdk_draw_line(inst
->pixmap
, gc
, startx
, starty
,
1837 startx
+ (length
-1) * dx
, starty
+ (length
-1) * dy
);
1841 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1842 x
*inst
->font_width
+inst
->cfg
.window_border
,
1843 y
*inst
->font_height
+inst
->cfg
.window_border
,
1844 x
*inst
->font_width
+inst
->cfg
.window_border
,
1845 y
*inst
->font_height
+inst
->cfg
.window_border
,
1846 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1849 GdkCursor
*make_mouse_ptr(struct gui_data
*inst
, int cursor_val
)
1852 * Truly hideous hack: GTK doesn't allow us to set the mouse
1853 * cursor foreground and background colours unless we've _also_
1854 * created our own cursor from bitmaps. Therefore, I need to
1855 * load the `cursor' font and draw glyphs from it on to
1856 * pixmaps, in order to construct my cursors with the fg and bg
1857 * I want. This is a gross hack, but it's more self-contained
1858 * than linking in Xlib to find the X window handle to
1859 * inst->area and calling XRecolorCursor, and it's more
1860 * futureproof than hard-coding the shapes as bitmap arrays.
1862 static GdkFont
*cursor_font
= NULL
;
1863 GdkPixmap
*source
, *mask
;
1865 GdkColor cfg
= { 0, 65535, 65535, 65535 };
1866 GdkColor cbg
= { 0, 0, 0, 0 };
1867 GdkColor dfg
= { 1, 65535, 65535, 65535 };
1868 GdkColor dbg
= { 0, 0, 0, 0 };
1871 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
1873 if (cursor_val
== -2) {
1874 gdk_font_unref(cursor_font
);
1878 if (cursor_val
>= 0 && !cursor_font
)
1879 cursor_font
= gdk_font_load("cursor");
1882 * Get the text extent of the cursor in question. We use the
1883 * mask character for this, because it's typically slightly
1884 * bigger than the main character.
1886 if (cursor_val
>= 0) {
1888 text
[0] = (char)cursor_val
+ 1;
1889 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
1890 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
1896 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
1897 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
1900 * Draw the mask character on the mask pixmap.
1902 gc
= gdk_gc_new(mask
);
1903 gdk_gc_set_foreground(gc
, &dbg
);
1904 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
1905 if (cursor_val
>= 0) {
1907 text
[0] = (char)cursor_val
+ 1;
1908 gdk_gc_set_foreground(gc
, &dfg
);
1909 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
1914 * Draw the main character on the source pixmap.
1916 gc
= gdk_gc_new(source
);
1917 gdk_gc_set_foreground(gc
, &dbg
);
1918 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
1919 if (cursor_val
>= 0) {
1921 text
[0] = (char)cursor_val
;
1922 gdk_gc_set_foreground(gc
, &dfg
);
1923 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
1928 * Create the cursor.
1930 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
1935 gdk_pixmap_unref(source
);
1936 gdk_pixmap_unref(mask
);
1941 void modalfatalbox(char *p
, ...)
1944 fprintf(stderr
, "FATAL ERROR: ");
1946 vfprintf(stderr
, p
, ap
);
1948 fputc('\n', stderr
);
1952 char *get_x_display(void *frontend
)
1954 return gdk_get_display();
1957 static void help(FILE *fp
) {
1959 "pterm option summary:\n"
1961 " --display DISPLAY Specify X display to use (note '--')\n"
1962 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1963 " -fn FONT Normal text font\n"
1964 " -fb FONT Bold text font\n"
1965 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1966 " -sl LINES Number of lines of scrollback\n"
1967 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1968 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1969 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1970 " -T TITLE Window title\n"
1971 " -ut, +ut Do(default) or do not update utmp\n"
1972 " -ls, +ls Do(default) or do not make shell a login shell\n"
1973 " -sb, +sb Do(default) or do not display a scrollbar\n"
1974 " -log PATH Log all output to a file\n"
1975 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1976 " -xrm RESOURCE-STRING Set an X resource\n"
1977 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1978 ) < 0 || fflush(fp
) < 0) {
1979 perror("output error");
1984 int do_cmdline(int argc
, char **argv
, int do_everything
, Config
*cfg
)
1987 extern char **pty_argv
; /* declared in pty.c */
1990 * Macros to make argument handling easier. Note that because
1991 * they need to call `continue', they cannot be contained in
1992 * the usual do {...} while (0) wrapper to make them
1993 * syntactically single statements; hence it is not legal to
1994 * use one of these macros as an unbraced statement between
1997 #define EXPECTS_ARG { \
1998 if (--argc <= 0) { \
2000 fprintf(stderr, "pterm: %s expects an argument\n", p); \
2005 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2014 while (--argc
> 0) {
2016 if (!strcmp(p
, "-fn") || !strcmp(p
, "-font")) {
2019 strncpy(cfg
->font
, val
, sizeof(cfg
->font
));
2020 cfg
->font
[sizeof(cfg
->font
)-1] = '\0';
2022 } else if (!strcmp(p
, "-fb")) {
2025 strncpy(cfg
->boldfont
, val
, sizeof(cfg
->boldfont
));
2026 cfg
->boldfont
[sizeof(cfg
->boldfont
)-1] = '\0';
2028 } else if (!strcmp(p
, "-fw")) {
2031 strncpy(cfg
->widefont
, val
, sizeof(cfg
->widefont
));
2032 cfg
->widefont
[sizeof(cfg
->widefont
)-1] = '\0';
2034 } else if (!strcmp(p
, "-fwb")) {
2037 strncpy(cfg
->wideboldfont
, val
, sizeof(cfg
->wideboldfont
));
2038 cfg
->wideboldfont
[sizeof(cfg
->wideboldfont
)-1] = '\0';
2040 } else if (!strcmp(p
, "-cs")) {
2043 strncpy(cfg
->line_codepage
, val
, sizeof(cfg
->line_codepage
));
2044 cfg
->line_codepage
[sizeof(cfg
->line_codepage
)-1] = '\0';
2046 } else if (!strcmp(p
, "-geometry")) {
2047 int flags
, x
, y
, w
, h
;
2051 flags
= XParseGeometry(val
, &x
, &y
, &w
, &h
);
2052 if (flags
& WidthValue
)
2054 if (flags
& HeightValue
)
2058 * Apparently setting the initial window position is
2059 * difficult in GTK 1.2. Not entirely sure why this
2060 * should be. 2.0 has gtk_window_parse_geometry(),
2061 * which would help... For the moment, though, I can't
2062 * be bothered with this.
2065 } else if (!strcmp(p
, "-sl")) {
2068 cfg
->savelines
= atoi(val
);
2070 } else if (!strcmp(p
, "-fg") || !strcmp(p
, "-bg") ||
2071 !strcmp(p
, "-bfg") || !strcmp(p
, "-bbg") ||
2072 !strcmp(p
, "-cfg") || !strcmp(p
, "-cbg")) {
2077 if (!gdk_color_parse(val
, &col
)) {
2079 fprintf(stderr
, "pterm: unable to parse colour \"%s\"\n", val
);
2082 index
= (!strcmp(p
, "-fg") ?
0 :
2083 !strcmp(p
, "-bg") ?
2 :
2084 !strcmp(p
, "-bfg") ?
1 :
2085 !strcmp(p
, "-bbg") ?
3 :
2086 !strcmp(p
, "-cfg") ?
4 :
2087 !strcmp(p
, "-cbg") ?
5 : -1);
2088 assert(index
!= -1);
2089 cfg
->colours
[index
][0] = col
.red
/ 256;
2090 cfg
->colours
[index
][1] = col
.green
/ 256;
2091 cfg
->colours
[index
][2] = col
.blue
/ 256;
2094 } else if (!strcmp(p
, "-e")) {
2095 /* This option swallows all further arguments. */
2101 pty_argv
= smalloc((argc
+1) * sizeof(char *));
2103 for (i
= 0; i
< argc
; i
++)
2104 pty_argv
[i
] = argv
[i
];
2105 pty_argv
[argc
] = NULL
;
2106 break; /* finished command-line processing */
2108 err
= 1, fprintf(stderr
, "pterm: -e expects an argument\n");
2110 } else if (!strcmp(p
, "-T")) {
2113 strncpy(cfg
->wintitle
, val
, sizeof(cfg
->wintitle
));
2114 cfg
->wintitle
[sizeof(cfg
->wintitle
)-1] = '\0';
2116 } else if (!strcmp(p
, "-log")) {
2119 strncpy(cfg
->logfilename
, val
, sizeof(cfg
->logfilename
));
2120 cfg
->logfilename
[sizeof(cfg
->logfilename
)-1] = '\0';
2121 cfg
->logtype
= LGTYP_DEBUG
;
2123 } else if (!strcmp(p
, "-ut-") || !strcmp(p
, "+ut")) {
2125 cfg
->stamp_utmp
= 0;
2127 } else if (!strcmp(p
, "-ut")) {
2129 cfg
->stamp_utmp
= 1;
2131 } else if (!strcmp(p
, "-ls-") || !strcmp(p
, "+ls")) {
2133 cfg
->login_shell
= 0;
2135 } else if (!strcmp(p
, "-ls")) {
2137 cfg
->login_shell
= 1;
2139 } else if (!strcmp(p
, "-nethack")) {
2141 cfg
->nethack_keypad
= 1;
2143 } else if (!strcmp(p
, "-sb-") || !strcmp(p
, "+sb")) {
2147 } else if (!strcmp(p
, "-sb")) {
2151 } else if (!strcmp(p
, "-name")) {
2155 } else if (!strcmp(p
, "-xrm")) {
2157 provide_xrm_string(val
);
2159 } else if(!strcmp(p
, "-help") || !strcmp(p
, "--help")) {
2165 fprintf(stderr
, "pterm: unrecognized option '%s'\n", p
);
2172 static void block_signal(int sig
, int block_it
) {
2176 sigaddset(&ss
, sig
);
2177 if(sigprocmask(block_it ? SIG_BLOCK
: SIG_UNBLOCK
, &ss
, 0) < 0) {
2178 perror("sigprocmask");
2184 * This function retrieves the character set encoding of a font. It
2185 * returns the character set without the X11 hack (in case the user
2186 * asks to use the font's own encoding).
2188 static int set_font_info(struct gui_data
*inst
, int fontid
)
2190 GdkFont
*font
= inst
->fonts
[fontid
];
2191 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2192 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2193 Atom charset_registry
, charset_encoding
;
2194 unsigned long registry_ret
, encoding_ret
;
2195 int retval
= CS_NONE
;
2197 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
2198 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
2199 inst
->fontinfo
[fontid
].charset
= CS_NONE
;
2200 inst
->fontinfo
[fontid
].is_wide
= 0;
2201 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
2202 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
2204 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
2205 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
2207 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
2208 retval
= inst
->fontinfo
[fontid
].charset
=
2209 charset_from_xenc(encoding
);
2210 /* FIXME: when libcharset supports wide encodings fix this. */
2211 if (!strcasecmp(encoding
, "iso10646-1")) {
2212 inst
->fontinfo
[fontid
].is_wide
= 1;
2217 * Hack for X line-drawing characters: if the primary
2218 * font is encoded as ISO-8859-anything, and has valid
2219 * glyphs in the first 32 char positions, it is assumed
2220 * that those glyphs are the VT100 line-drawing
2223 * Actually, we'll hack even harder by only checking
2224 * position 0x19 (vertical line, VT100 linedrawing
2225 * `x'). Then we can check it easily by seeing if the
2226 * ascent and descent differ.
2228 if (inst
->fontinfo
[fontid
].charset
== CS_ISO8859_1
) {
2229 int lb
, rb
, wid
, asc
, desc
;
2234 gdk_string_extents(inst
->fonts
[fontid
], text
,
2235 &lb
, &rb
, &wid
, &asc
, &desc
);
2237 inst
->fontinfo
[fontid
].charset
= CS_ISO8859_1_X11
;
2247 int main(int argc
, char **argv
)
2249 extern int pty_master_fd
; /* declared in pty.c */
2250 extern void pty_pre_init(void); /* declared in pty.c */
2251 struct gui_data
*inst
;
2254 /* defer any child exit handling until we're ready to deal with
2256 block_signal(SIGCHLD
, 1);
2260 gtk_init(&argc
, &argv
);
2263 * Create an instance structure and initialise to zeroes
2265 inst
= smalloc(sizeof(*inst
));
2266 memset(inst
, 0, sizeof(*inst
));
2267 inst
->alt_keycode
= -1; /* this one needs _not_ to be zero */
2269 if (do_cmdline(argc
, argv
, 0, &inst
->cfg
))
2270 exit(1); /* pre-defaults pass to get -class */
2271 do_defaults(NULL
, &inst
->cfg
);
2272 if (do_cmdline(argc
, argv
, 1, &inst
->cfg
))
2273 exit(1); /* post-defaults, do everything */
2275 inst
->fonts
[0] = gdk_font_load(inst
->cfg
.font
);
2276 if (!inst
->fonts
[0]) {
2277 fprintf(stderr
, "pterm: unable to load font \"%s\"\n", inst
->cfg
.font
);
2280 font_charset
= set_font_info(inst
, 0);
2281 if (inst
->cfg
.boldfont
[0]) {
2282 inst
->fonts
[1] = gdk_font_load(inst
->cfg
.boldfont
);
2283 if (!inst
->fonts
[1]) {
2284 fprintf(stderr
, "pterm: unable to load bold font \"%s\"\n",
2285 inst
->cfg
.boldfont
);
2288 set_font_info(inst
, 1);
2290 inst
->fonts
[1] = NULL
;
2291 if (inst
->cfg
.widefont
[0]) {
2292 inst
->fonts
[2] = gdk_font_load(inst
->cfg
.widefont
);
2293 if (!inst
->fonts
[2]) {
2294 fprintf(stderr
, "pterm: unable to load wide font \"%s\"\n",
2295 inst
->cfg
.boldfont
);
2298 set_font_info(inst
, 2);
2300 inst
->fonts
[2] = NULL
;
2301 if (inst
->cfg
.wideboldfont
[0]) {
2302 inst
->fonts
[3] = gdk_font_load(inst
->cfg
.wideboldfont
);
2303 if (!inst
->fonts
[3]) {
2304 fprintf(stderr
, "pterm: unable to load wide/bold font \"%s\"\n",
2305 inst
->cfg
.boldfont
);
2308 set_font_info(inst
, 3);
2310 inst
->fonts
[3] = NULL
;
2312 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
2313 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
2315 inst
->compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
2316 inst
->utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
2318 inst
->direct_to_font
= init_ucs(inst
->cfg
.line_codepage
, font_charset
);
2320 inst
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2322 if (inst
->cfg
.wintitle
[0])
2323 set_title(inst
, inst
->cfg
.wintitle
);
2325 set_title(inst
, "pterm");
2328 * Set up the colour map.
2330 palette_reset(inst
);
2332 inst
->area
= gtk_drawing_area_new();
2333 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
2334 inst
->font_width
* inst
->cfg
.width
+ 2*inst
->cfg
.window_border
,
2335 inst
->font_height
* inst
->cfg
.height
+ 2*inst
->cfg
.window_border
);
2336 if (inst
->cfg
.scrollbar
) {
2337 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2338 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
2340 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
2341 if (inst
->cfg
.scrollbar
) {
2342 if (inst
->cfg
.scrollbar_on_left
)
2343 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2345 gtk_box_pack_end(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2347 gtk_box_pack_start(inst
->hbox
, inst
->area
, TRUE
, TRUE
, 0);
2349 gtk_container_add(GTK_CONTAINER(inst
->window
), GTK_WIDGET(inst
->hbox
));
2353 geom
.min_width
= inst
->font_width
+ 2*inst
->cfg
.window_border
;
2354 geom
.min_height
= inst
->font_height
+ 2*inst
->cfg
.window_border
;
2355 geom
.max_width
= geom
.max_height
= -1;
2356 geom
.base_width
= 2*inst
->cfg
.window_border
;
2357 geom
.base_height
= 2*inst
->cfg
.window_border
;
2358 geom
.width_inc
= inst
->font_width
;
2359 geom
.height_inc
= inst
->font_height
;
2360 geom
.min_aspect
= geom
.max_aspect
= 0;
2361 gtk_window_set_geometry_hints(GTK_WINDOW(inst
->window
), inst
->area
, &geom
,
2362 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
2363 GDK_HINT_RESIZE_INC
);
2366 gtk_signal_connect(GTK_OBJECT(inst
->window
), "destroy",
2367 GTK_SIGNAL_FUNC(destroy
), inst
);
2368 gtk_signal_connect(GTK_OBJECT(inst
->window
), "delete_event",
2369 GTK_SIGNAL_FUNC(delete_window
), inst
);
2370 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_press_event",
2371 GTK_SIGNAL_FUNC(key_event
), inst
);
2372 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_release_event",
2373 GTK_SIGNAL_FUNC(key_event
), inst
);
2374 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_in_event",
2375 GTK_SIGNAL_FUNC(focus_event
), inst
);
2376 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_out_event",
2377 GTK_SIGNAL_FUNC(focus_event
), inst
);
2378 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
2379 GTK_SIGNAL_FUNC(configure_area
), inst
);
2380 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
2381 GTK_SIGNAL_FUNC(expose_area
), inst
);
2382 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
2383 GTK_SIGNAL_FUNC(button_event
), inst
);
2384 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
2385 GTK_SIGNAL_FUNC(button_event
), inst
);
2386 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
2387 GTK_SIGNAL_FUNC(motion_event
), inst
);
2388 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
2389 GTK_SIGNAL_FUNC(selection_received
), inst
);
2390 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
2391 GTK_SIGNAL_FUNC(selection_get
), inst
);
2392 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
2393 GTK_SIGNAL_FUNC(selection_clear
), inst
);
2394 if (inst
->cfg
.scrollbar
)
2395 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
2396 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
2397 gtk_timeout_add(20, timer_func
, inst
);
2398 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
2399 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
2400 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
2401 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_MOTION_MASK
);
2403 gtk_widget_show(inst
->area
);
2404 if (inst
->cfg
.scrollbar
)
2405 gtk_widget_show(inst
->sbar
);
2406 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
2407 gtk_widget_show(inst
->window
);
2409 set_window_background(inst
);
2411 inst
->textcursor
= make_mouse_ptr(inst
, GDK_XTERM
);
2412 inst
->rawcursor
= make_mouse_ptr(inst
, GDK_LEFT_PTR
);
2413 inst
->blankcursor
= make_mouse_ptr(inst
, -1);
2414 make_mouse_ptr(inst
, -2); /* clean up cursor font */
2415 inst
->currcursor
= inst
->textcursor
;
2416 show_mouseptr(inst
, 1);
2418 inst
->term
= term_init(&inst
->cfg
, inst
);
2419 inst
->logctx
= log_init(inst
, &inst
->cfg
);
2420 term_provide_logctx(inst
->term
, inst
->logctx
);
2422 inst
->back
= &pty_backend
;
2423 inst
->back
->init((void *)inst
->term
, &inst
->backhandle
, &inst
->cfg
,
2425 inst
->back
->provide_logctx(inst
->backhandle
, inst
->logctx
);
2427 term_provide_resize_fn(inst
->term
, inst
->back
->size
, inst
->backhandle
);
2429 term_size(inst
->term
, inst
->cfg
.height
, inst
->cfg
.width
, inst
->cfg
.savelines
);
2432 ldisc_create(&inst
->cfg
, inst
->term
, inst
->back
, inst
->backhandle
, inst
);
2433 ldisc_send(inst
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
2435 inst
->master_fd
= pty_master_fd
;
2436 inst
->exited
= FALSE
;
2437 inst
->master_func_id
= gdk_input_add(pty_master_fd
, GDK_INPUT_READ
,
2438 pty_input_func
, inst
);
2440 /* now we're reday to deal with the child exit handler being
2442 block_signal(SIGCHLD
, 0);