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
;
70 struct unicode_data ucsdata
;
76 struct gui_data
*inst
;
79 static int send_raw_mouse
;
81 static char *app_name
= "pterm";
83 char *x_get_default(const char *key
)
85 return XGetDefault(GDK_DISPLAY(), app_name
, key
);
89 * Default settings that are specific to pterm.
91 char *platform_default_s(const char *name
)
93 if (!strcmp(name
, "Font"))
94 return "fixed"; /* COE_NORMAL works badly in an xterm */
98 int platform_default_i(const char *name
, int def
)
100 if (!strcmp(name
, "CloseOnExit"))
101 return COE_ALWAYS
; /* COE_NORMAL works badly in an xterm */
105 void ldisc_update(void *frontend
, int echo
, int edit
)
108 * This is a stub in pterm. If I ever produce a Unix
109 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
110 * then it will require some termios manoeuvring analogous to
111 * that in the Windows plink.c, but here it's meaningless.
115 int askappend(void *frontend
, char *filename
)
118 * Logging in an xterm-alike is liable to be something you only
119 * do at serious diagnostic need. Hence, I'm going to take the
120 * easy option for now and assume we always want to overwrite
121 * log files. I can always make it properly configurable later.
126 void logevent(void *frontend
, char *string
)
129 * This is not a very helpful function: events are logged
130 * pretty much exclusively by the back end, and our pty back
131 * end is self-contained. So we need do nothing.
135 int font_dimension(void *frontend
, int which
)/* 0 for width, 1 for height */
137 struct gui_data
*inst
= (struct gui_data
*)frontend
;
140 return inst
->font_height
;
142 return inst
->font_width
;
146 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
147 * into a cooked one (SELECT, EXTEND, PASTE).
149 * In Unix, this is not configurable; the X button arrangement is
150 * rock-solid across all applications, everyone has a three-button
151 * mouse or a means of faking it, and there is no need to switch
152 * buttons around at all.
154 static Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
156 /* struct gui_data *inst = (struct gui_data *)frontend; */
158 if (button
== MBT_LEFT
)
160 if (button
== MBT_MIDDLE
)
162 if (button
== MBT_RIGHT
)
164 return 0; /* shouldn't happen */
168 * Minimise or restore the window in response to a server-side
171 void set_iconic(void *frontend
, int iconic
)
174 * GTK 1.2 doesn't know how to do this.
176 #if GTK_CHECK_VERSION(2,0,0)
177 struct gui_data
*inst
= (struct gui_data
*)frontend
;
179 gtk_window_iconify(GTK_WINDOW(inst
->window
));
181 gtk_window_deiconify(GTK_WINDOW(inst
->window
));
186 * Move the window in response to a server-side request.
188 void move_window(void *frontend
, int x
, int y
)
190 struct gui_data
*inst
= (struct gui_data
*)frontend
;
192 * I assume that when the GTK version of this call is available
193 * we should use it. Not sure how it differs from the GDK one,
196 #if GTK_CHECK_VERSION(2,0,0)
197 gtk_window_move(GTK_WINDOW(inst
->window
), x
, y
);
199 gdk_window_move(inst
->window
->window
, x
, y
);
204 * Move the window to the top or bottom of the z-order in response
205 * to a server-side request.
207 void set_zorder(void *frontend
, int top
)
209 struct gui_data
*inst
= (struct gui_data
*)frontend
;
211 gdk_window_raise(inst
->window
->window
);
213 gdk_window_lower(inst
->window
->window
);
217 * Refresh the window in response to a server-side request.
219 void refresh_window(void *frontend
)
221 struct gui_data
*inst
= (struct gui_data
*)frontend
;
222 term_invalidate(inst
->term
);
226 * Maximise or restore the window in response to a server-side
229 void set_zoomed(void *frontend
, int zoomed
)
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_maximize(GTK_WINDOW(inst
->window
));
239 gtk_window_unmaximize(GTK_WINDOW(inst
->window
));
244 * Report whether the window is iconic, for terminal reports.
246 int is_iconic(void *frontend
)
248 struct gui_data
*inst
= (struct gui_data
*)frontend
;
249 return !gdk_window_is_viewable(inst
->window
->window
);
253 * Report the window's position, for terminal reports.
255 void get_window_pos(void *frontend
, int *x
, int *y
)
257 struct gui_data
*inst
= (struct gui_data
*)frontend
;
259 * I assume that when the GTK version of this call is available
260 * we should use it. Not sure how it differs from the GDK one,
263 #if GTK_CHECK_VERSION(2,0,0)
264 gtk_window_get_position(GTK_WINDOW(inst
->window
), x
, y
);
266 gdk_window_get_position(inst
->window
->window
, x
, y
);
271 * Report the window's pixel size, for terminal reports.
273 void get_window_pixels(void *frontend
, int *x
, int *y
)
275 struct gui_data
*inst
= (struct gui_data
*)frontend
;
277 * I assume that when the GTK version of this call is available
278 * we should use it. Not sure how it differs from the GDK one,
281 #if GTK_CHECK_VERSION(2,0,0)
282 gtk_window_get_size(GTK_WINDOW(inst
->window
), x
, y
);
284 gdk_window_get_size(inst
->window
->window
, x
, y
);
289 * Return the window or icon title.
291 char *get_window_title(void *frontend
, int icon
)
293 struct gui_data
*inst
= (struct gui_data
*)frontend
;
294 return icon ? inst
->wintitle
: inst
->icontitle
;
297 gint
delete_window(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
300 * We could implement warn-on-close here if we really wanted
306 static void show_mouseptr(struct gui_data
*inst
, int show
)
308 if (!inst
->cfg
.hide_mouseptr
)
311 gdk_window_set_cursor(inst
->area
->window
, inst
->currcursor
);
313 gdk_window_set_cursor(inst
->area
->window
, inst
->blankcursor
);
314 inst
->mouseptr_visible
= show
;
317 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
319 struct gui_data
*inst
= (struct gui_data
*)data
;
320 int w
, h
, need_size
= 0;
322 w
= (event
->width
- 2*inst
->cfg
.window_border
) / inst
->font_width
;
323 h
= (event
->height
- 2*inst
->cfg
.window_border
) / inst
->font_height
;
325 if (w
!= inst
->cfg
.width
|| h
!= inst
->cfg
.height
) {
327 gdk_pixmap_unref(inst
->pixmap
);
331 inst
->cfg
.height
= h
;
337 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
338 (inst
->cfg
.width
* inst
->font_width
+
339 2*inst
->cfg
.window_border
),
340 (inst
->cfg
.height
* inst
->font_height
+
341 2*inst
->cfg
.window_border
), -1);
343 gc
= gdk_gc_new(inst
->area
->window
);
344 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
345 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
346 inst
->cfg
.width
* inst
->font_width
+ 2*inst
->cfg
.window_border
,
347 inst
->cfg
.height
* inst
->font_height
+ 2*inst
->cfg
.window_border
);
352 term_size(inst
->term
, h
, w
, inst
->cfg
.savelines
);
358 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
360 struct gui_data
*inst
= (struct gui_data
*)data
;
363 * Pass the exposed rectangle to terminal.c, which will call us
364 * back to do the actual painting.
367 gdk_draw_pixmap(widget
->window
,
368 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
370 event
->area
.x
, event
->area
.y
,
371 event
->area
.x
, event
->area
.y
,
372 event
->area
.width
, event
->area
.height
);
377 #define KEY_PRESSED(k) \
378 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
380 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
382 struct gui_data
*inst
= (struct gui_data
*)data
;
386 /* By default, nothing is generated. */
390 * If Alt is being released after typing an Alt+numberpad
391 * sequence, we should generate the code that was typed.
393 * Note that we only do this if more than one key was actually
394 * pressed - I don't think Alt+NumPad4 should be ^D or that
395 * Alt+NumPad3 should be ^C, for example. There's no serious
396 * inconvenience in having to type a zero before a single-digit
399 if (event
->type
== GDK_KEY_RELEASE
&&
400 (event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
401 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
) &&
402 inst
->alt_keycode
>= 0 && inst
->alt_digits
> 1) {
404 printf("Alt key up, keycode = %d\n", inst
->alt_keycode
);
406 output
[0] = inst
->alt_keycode
;
411 if (event
->type
== GDK_KEY_PRESS
) {
415 printf("keypress: keyval = %04x, state = %08x; string =",
416 event
->keyval
, event
->state
);
417 for (i
= 0; event
->string
[i
]; i
++)
418 printf(" %02x", (unsigned char) event
->string
[i
]);
424 * NYI: Compose key (!!! requires Unicode faff before even trying)
428 * If Alt has just been pressed, we start potentially
429 * accumulating an Alt+numberpad code. We do this by
430 * setting alt_keycode to -1 (nothing yet but plausible).
432 if ((event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
433 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
)) {
434 inst
->alt_keycode
= -1;
435 inst
->alt_digits
= 0;
436 goto done
; /* this generates nothing else */
440 * If we're seeing a numberpad key press with Mod1 down,
441 * consider adding it to alt_keycode if that's sensible.
442 * Anything _else_ with Mod1 down cancels any possibility
443 * of an ALT keycode: we set alt_keycode to -2.
445 if ((event
->state
& GDK_MOD1_MASK
) && inst
->alt_keycode
!= -2) {
447 switch (event
->keyval
) {
448 case GDK_KP_0
: case GDK_KP_Insert
: digit
= 0; break;
449 case GDK_KP_1
: case GDK_KP_End
: digit
= 1; break;
450 case GDK_KP_2
: case GDK_KP_Down
: digit
= 2; break;
451 case GDK_KP_3
: case GDK_KP_Page_Down
: digit
= 3; break;
452 case GDK_KP_4
: case GDK_KP_Left
: digit
= 4; break;
453 case GDK_KP_5
: case GDK_KP_Begin
: digit
= 5; break;
454 case GDK_KP_6
: case GDK_KP_Right
: digit
= 6; break;
455 case GDK_KP_7
: case GDK_KP_Home
: digit
= 7; break;
456 case GDK_KP_8
: case GDK_KP_Up
: digit
= 8; break;
457 case GDK_KP_9
: case GDK_KP_Page_Up
: digit
= 9; break;
460 inst
->alt_keycode
= -2; /* it's invalid */
463 printf("Adding digit %d to keycode %d", digit
,
466 if (inst
->alt_keycode
== -1)
467 inst
->alt_keycode
= digit
; /* one-digit code */
469 inst
->alt_keycode
= inst
->alt_keycode
* 10 + digit
;
472 printf(" gives new code %d\n", inst
->alt_keycode
);
474 /* Having used this digit, we now do nothing more with it. */
480 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
483 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
484 term_scroll(inst
->term
, 0, -inst
->cfg
.height
/2);
487 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_CONTROL_MASK
)) {
488 term_scroll(inst
->term
, 0, -1);
491 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
492 term_scroll(inst
->term
, 0, +inst
->cfg
.height
/2);
495 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_CONTROL_MASK
)) {
496 term_scroll(inst
->term
, 0, +1);
501 * Neither does Shift-Ins.
503 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
508 /* ALT+things gives leading Escape. */
510 strncpy(output
+1, event
->string
, 31);
512 end
= strlen(output
);
513 if (event
->state
& GDK_MOD1_MASK
) {
515 if (end
== 1) end
= 0;
519 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
520 if (!event
->string
[0] && event
->keyval
== '`' &&
521 (event
->state
& GDK_CONTROL_MASK
)) {
526 /* Control-Break is the same as Control-C */
527 if (event
->keyval
== GDK_Break
&&
528 (event
->state
& GDK_CONTROL_MASK
)) {
533 /* Control-2, Control-Space and Control-@ are NUL */
534 if (!event
->string
[0] &&
535 (event
->keyval
== ' ' || event
->keyval
== '2' ||
536 event
->keyval
== '@') &&
537 (event
->state
& (GDK_SHIFT_MASK
|
538 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
543 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
544 if (!event
->string
[0] && event
->keyval
== ' ' &&
545 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
546 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
551 /* We don't let GTK tell us what Backspace is! We know better. */
552 if (event
->keyval
== GDK_BackSpace
&&
553 !(event
->state
& GDK_SHIFT_MASK
)) {
554 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x7F' : '\x08';
557 /* For Shift Backspace, do opposite of what is configured. */
558 if (event
->keyval
== GDK_BackSpace
&&
559 (event
->state
& GDK_SHIFT_MASK
)) {
560 output
[1] = inst
->cfg
.bksp_is_delete ?
'\x08' : '\x7F';
564 /* Shift-Tab is ESC [ Z */
565 if (event
->keyval
== GDK_ISO_Left_Tab
||
566 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
567 end
= 1 + sprintf(output
+1, "\033[Z");
571 * NetHack keypad mode.
573 if (inst
->cfg
.nethack_keypad
) {
575 switch (event
->keyval
) {
576 case GDK_KP_1
: case GDK_KP_End
: keys
= "bB"; break;
577 case GDK_KP_2
: case GDK_KP_Down
: keys
= "jJ"; break;
578 case GDK_KP_3
: case GDK_KP_Page_Down
: keys
= "nN"; break;
579 case GDK_KP_4
: case GDK_KP_Left
: keys
= "hH"; break;
580 case GDK_KP_5
: case GDK_KP_Begin
: keys
= ".."; break;
581 case GDK_KP_6
: case GDK_KP_Right
: keys
= "lL"; break;
582 case GDK_KP_7
: case GDK_KP_Home
: keys
= "yY"; break;
583 case GDK_KP_8
: case GDK_KP_Up
: keys
= "kK"; break;
584 case GDK_KP_9
: case GDK_KP_Page_Up
: keys
= "uU"; break;
588 if (event
->state
& GDK_SHIFT_MASK
)
597 * Application keypad mode.
599 if (inst
->term
->app_keypad_keys
&& !inst
->cfg
.no_applic_k
) {
601 switch (event
->keyval
) {
602 case GDK_Num_Lock
: xkey
= 'P'; break;
603 case GDK_KP_Divide
: xkey
= 'Q'; break;
604 case GDK_KP_Multiply
: xkey
= 'R'; break;
605 case GDK_KP_Subtract
: xkey
= 'S'; break;
607 * Keypad + is tricky. It covers a space that would
608 * be taken up on the VT100 by _two_ keys; so we
609 * let Shift select between the two. Worse still,
610 * in xterm function key mode we change which two...
613 if (inst
->cfg
.funky_type
== 2) {
614 if (event
->state
& GDK_SHIFT_MASK
)
618 } else if (event
->state
& GDK_SHIFT_MASK
)
623 case GDK_KP_Enter
: xkey
= 'M'; break;
624 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
625 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
626 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
627 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
628 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
629 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
630 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
631 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
632 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
633 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
634 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
637 if (inst
->term
->vt52_mode
) {
638 if (xkey
>= 'P' && xkey
<= 'S')
639 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
641 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
643 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
649 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
650 * for integer decimal nn.)
652 * We also deal with the weird ones here. Linux VCs replace F1
653 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
654 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
659 switch (event
->keyval
) {
661 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
664 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
667 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
670 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
673 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
676 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
679 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
682 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
685 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
688 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
721 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
722 case GDK_Home
: case GDK_KP_Home
:
725 case GDK_Insert
: case GDK_KP_Insert
:
728 case GDK_Delete
: case GDK_KP_Delete
:
731 case GDK_End
: case GDK_KP_End
:
734 case GDK_Page_Up
: case GDK_KP_Page_Up
:
737 case GDK_Page_Down
: case GDK_KP_Page_Down
:
741 /* Reorder edit keys to physical order */
742 if (inst
->cfg
.funky_type
== 3 && code
<= 6)
743 code
= "\0\2\1\4\5\3\6"[code
];
745 if (inst
->term
->vt52_mode
&& code
> 0 && code
<= 6) {
746 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
750 if (inst
->cfg
.funky_type
== 5 && /* SCO function keys */
751 code
>= 11 && code
<= 34) {
752 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
754 switch (event
->keyval
) {
755 case GDK_F1
: index
= 0; break;
756 case GDK_F2
: index
= 1; break;
757 case GDK_F3
: index
= 2; break;
758 case GDK_F4
: index
= 3; break;
759 case GDK_F5
: index
= 4; break;
760 case GDK_F6
: index
= 5; break;
761 case GDK_F7
: index
= 6; break;
762 case GDK_F8
: index
= 7; break;
763 case GDK_F9
: index
= 8; break;
764 case GDK_F10
: index
= 9; break;
765 case GDK_F11
: index
= 10; break;
766 case GDK_F12
: index
= 11; break;
768 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
769 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
770 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
773 if (inst
->cfg
.funky_type
== 5 && /* SCO small keypad */
774 code
>= 1 && code
<= 6) {
775 char codes
[] = "HL.FIG";
780 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
784 if ((inst
->term
->vt52_mode
|| inst
->cfg
.funky_type
== 4) &&
785 code
>= 11 && code
<= 24) {
791 if (inst
->term
->vt52_mode
)
792 end
= 1 + sprintf(output
+1,
793 "\x1B%c", code
+ 'P' - 11 - offt
);
795 end
= 1 + sprintf(output
+1,
796 "\x1BO%c", code
+ 'P' - 11 - offt
);
799 if (inst
->cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
800 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
803 if (inst
->cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
804 if (inst
->term
->vt52_mode
)
805 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
807 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
810 if (inst
->cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
811 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
815 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
821 * Cursor keys. (This includes the numberpad cursor keys,
822 * if we haven't already done them due to app keypad mode.)
824 * Here we also process un-numlocked un-appkeypadded KP5,
825 * which sends ESC [ G.
829 switch (event
->keyval
) {
830 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
831 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
832 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
833 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
834 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
838 * The arrow keys normally do ESC [ A and so on. In
839 * app cursor keys mode they do ESC O A instead.
840 * Ctrl toggles the two modes.
842 if (inst
->term
->vt52_mode
) {
843 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
844 } else if (!inst
->term
->app_cursor_keys
^
845 !(event
->state
& GDK_CONTROL_MASK
)) {
846 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
848 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
861 printf("generating sequence:");
862 for (i
= start
; i
< end
; i
++)
863 printf(" %02x", (unsigned char) output
[i
]);
867 if (!inst
->direct_to_font
) {
869 * The stuff we've just generated is assumed to be
870 * ISO-8859-1! This sounds insane, but `man
871 * XLookupString' agrees: strings of this type returned
872 * from the X server are hardcoded to 8859-1. Strictly
873 * speaking we should be doing this using some sort of
874 * GtkIMContext, which (if we're lucky) would give us
875 * our data directly in Unicode; but that's not
876 * supported in GTK 1.2 as far as I can tell, and it's
877 * poorly documented even in 2.0, so it'll have to
880 lpage_send(inst
->ldisc
, CS_ISO8859_1
, output
+start
, end
-start
, 1);
883 * In direct-to-font mode, we just send the string
884 * exactly as we received it.
886 ldisc_send(inst
->ldisc
, output
+start
, end
-start
, 1);
889 show_mouseptr(inst
, 0);
890 term_seen_key_event(inst
->term
);
891 term_out(inst
->term
);
897 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
899 struct gui_data
*inst
= (struct gui_data
*)data
;
900 int shift
, ctrl
, alt
, x
, y
, button
, act
;
902 show_mouseptr(inst
, 1);
904 if (event
->button
== 4 && event
->type
== GDK_BUTTON_PRESS
) {
905 term_scroll(inst
->term
, 0, -5);
908 if (event
->button
== 5 && event
->type
== GDK_BUTTON_PRESS
) {
909 term_scroll(inst
->term
, 0, +5);
913 shift
= event
->state
& GDK_SHIFT_MASK
;
914 ctrl
= event
->state
& GDK_CONTROL_MASK
;
915 alt
= event
->state
& GDK_MOD1_MASK
;
916 if (event
->button
== 1)
918 else if (event
->button
== 2)
920 else if (event
->button
== 3)
923 return FALSE
; /* don't even know what button! */
925 switch (event
->type
) {
926 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
927 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
928 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
929 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
930 default: return FALSE
; /* don't know this event type */
933 if (send_raw_mouse
&& !(inst
->cfg
.mouse_override
&& shift
) &&
934 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
935 return TRUE
; /* we ignore these in raw mouse mode */
937 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
938 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
940 term_mouse(inst
->term
, button
, translate_button(button
), act
,
941 x
, y
, shift
, ctrl
, alt
);
946 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
948 struct gui_data
*inst
= (struct gui_data
*)data
;
949 int shift
, ctrl
, alt
, x
, y
, button
;
951 show_mouseptr(inst
, 1);
953 shift
= event
->state
& GDK_SHIFT_MASK
;
954 ctrl
= event
->state
& GDK_CONTROL_MASK
;
955 alt
= event
->state
& GDK_MOD1_MASK
;
956 if (event
->state
& GDK_BUTTON1_MASK
)
958 else if (event
->state
& GDK_BUTTON2_MASK
)
960 else if (event
->state
& GDK_BUTTON3_MASK
)
963 return FALSE
; /* don't even know what button! */
965 x
= (event
->x
- inst
->cfg
.window_border
) / inst
->font_width
;
966 y
= (event
->y
- inst
->cfg
.window_border
) / inst
->font_height
;
968 term_mouse(inst
->term
, button
, translate_button(button
), MA_DRAG
,
969 x
, y
, shift
, ctrl
, alt
);
974 void done_with_pty(struct gui_data
*inst
)
976 extern void pty_close(void);
978 if (inst
->master_fd
>= 0) {
980 inst
->master_fd
= -1;
981 gtk_input_remove(inst
->master_func_id
);
984 if (!inst
->exited
&& inst
->back
->exitcode(inst
->backhandle
) >= 0) {
985 int exitcode
= inst
->back
->exitcode(inst
->backhandle
);
988 clean
= WIFEXITED(exitcode
) && (WEXITSTATUS(exitcode
) == 0);
991 * Terminate now, if the Close On Exit setting is
994 if (inst
->cfg
.close_on_exit
== COE_ALWAYS
||
995 (inst
->cfg
.close_on_exit
== COE_NORMAL
&& clean
))
999 * Otherwise, output an indication that the session has
1004 if (WIFEXITED(exitcode
))
1005 sprintf(message
, "\r\n[pterm: process terminated with exit"
1006 " code %d]\r\n", WEXITSTATUS(exitcode
));
1007 else if (WIFSIGNALED(exitcode
))
1008 #ifdef HAVE_NO_STRSIGNAL
1009 sprintf(message
, "\r\n[pterm: process terminated on signal"
1010 " %d]\r\n", WTERMSIG(exitcode
));
1012 sprintf(message
, "\r\n[pterm: process terminated on signal"
1013 " %d (%.400s)]\r\n", WTERMSIG(exitcode
),
1014 strsignal(WTERMSIG(exitcode
)));
1016 from_backend((void *)inst
->term
, 0, message
, strlen(message
));
1022 void frontend_keypress(void *handle
)
1024 struct gui_data
*inst
= (struct gui_data
*)handle
;
1027 * If our child process has exited but not closed, terminate on
1034 gint
timer_func(gpointer data
)
1036 struct gui_data
*inst
= (struct gui_data
*)data
;
1038 if (inst
->back
->exitcode(inst
->backhandle
) >= 0) {
1040 * The primary child process died. We could keep the
1041 * terminal open for remaining subprocesses to output to,
1042 * but conventional wisdom seems to feel that that's the
1043 * Wrong Thing for an xterm-alike, so we bail out now
1044 * (though we don't necessarily _close_ the window,
1045 * depending on the state of Close On Exit). This would be
1046 * easy enough to change or make configurable if necessary.
1048 done_with_pty(inst
);
1051 term_update(inst
->term
);
1052 term_blink(inst
->term
, 0);
1056 void pty_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
1058 struct gui_data
*inst
= (struct gui_data
*)data
;
1062 ret
= read(sourcefd
, buf
, sizeof(buf
));
1065 * Clean termination condition is that either ret == 0, or ret
1066 * < 0 and errno == EIO. Not sure why the latter, but it seems
1069 if (ret
== 0 || (ret
< 0 && errno
== EIO
)) {
1070 done_with_pty(inst
);
1071 } else if (ret
< 0) {
1072 perror("read pty master");
1075 from_backend(inst
->term
, 0, buf
, ret
);
1076 term_blink(inst
->term
, 1);
1077 term_out(inst
->term
);
1080 void destroy(GtkWidget
*widget
, gpointer data
)
1085 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
1087 struct gui_data
*inst
= (struct gui_data
*)data
;
1088 inst
->term
->has_focus
= event
->in
;
1089 term_out(inst
->term
);
1090 term_update(inst
->term
);
1091 show_mouseptr(inst
, 1);
1096 * set or clear the "raw mouse message" mode
1098 void set_raw_mouse_mode(void *frontend
, int activate
)
1100 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1101 activate
= activate
&& !inst
->cfg
.no_mouse_rep
;
1102 send_raw_mouse
= activate
;
1104 inst
->currcursor
= inst
->rawcursor
;
1106 inst
->currcursor
= inst
->textcursor
;
1107 show_mouseptr(inst
, inst
->mouseptr_visible
);
1110 void request_resize(void *frontend
, int w
, int h
)
1112 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1113 int large_x
, large_y
;
1114 int offset_x
, offset_y
;
1116 GtkRequisition inner
, outer
;
1119 * This is a heinous hack dreamed up by the gnome-terminal
1120 * people to get around a limitation in gtk. The problem is
1121 * that in order to set the size correctly we really need to be
1122 * calling gtk_window_resize - but that needs to know the size
1123 * of the _whole window_, not the drawing area. So what we do
1124 * is to set an artificially huge size request on the drawing
1125 * area, recompute the resulting size request on the window,
1126 * and look at the difference between the two. That gives us
1127 * the x and y offsets we need to translate drawing area size
1128 * into window size for real, and then we call
1129 * gtk_window_resize.
1133 * We start by retrieving the current size of the whole window.
1134 * Adding a bit to _that_ will give us a value we can use as a
1135 * bogus size request which guarantees to be bigger than the
1136 * current size of the drawing area.
1138 get_window_pixels(inst
, &large_x
, &large_y
);
1142 #if GTK_CHECK_VERSION(2,0,0)
1143 gtk_widget_set_size_request(inst
->area
, large_x
, large_y
);
1145 gtk_widget_set_usize(inst
->area
, large_x
, large_y
);
1147 gtk_widget_size_request(inst
->area
, &inner
);
1148 gtk_widget_size_request(inst
->window
, &outer
);
1150 offset_x
= outer
.width
- inner
.width
;
1151 offset_y
= outer
.height
- inner
.height
;
1153 area_x
= inst
->font_width
* w
+ 2*inst
->cfg
.window_border
;
1154 area_y
= inst
->font_height
* h
+ 2*inst
->cfg
.window_border
;
1157 * Now we must set the size request on the drawing area back to
1158 * something sensible before we commit the real resize. Best
1159 * way to do this, I think, is to set it to what the size is
1160 * really going to end up being.
1162 #if GTK_CHECK_VERSION(2,0,0)
1163 gtk_widget_set_size_request(inst
->area
, area_x
, area_y
);
1165 gtk_widget_set_usize(inst
->area
, area_x
, area_y
);
1168 #if GTK_CHECK_VERSION(2,0,0)
1169 gtk_window_resize(GTK_WINDOW(inst
->window
),
1170 area_x
+ offset_x
, area_y
+ offset_y
);
1172 gdk_window_resize(inst
->window
->window
,
1173 area_x
+ offset_x
, area_y
+ offset_y
);
1177 static void real_palette_set(struct gui_data
*inst
, int n
, int r
, int g
, int b
)
1179 gboolean success
[1];
1181 inst
->cols
[n
].red
= r
* 0x0101;
1182 inst
->cols
[n
].green
= g
* 0x0101;
1183 inst
->cols
[n
].blue
= b
* 0x0101;
1185 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
+ n
, 1,
1186 FALSE
, FALSE
, success
);
1188 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1192 void set_window_background(struct gui_data
*inst
)
1194 if (inst
->area
&& inst
->area
->window
)
1195 gdk_window_set_background(inst
->area
->window
, &inst
->cols
[18]);
1196 if (inst
->window
&& inst
->window
->window
)
1197 gdk_window_set_background(inst
->window
->window
, &inst
->cols
[18]);
1200 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1202 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1203 static const int first
[21] = {
1204 0, 2, 4, 6, 8, 10, 12, 14,
1205 1, 3, 5, 7, 9, 11, 13, 15,
1208 real_palette_set(inst
, first
[n
], r
, g
, b
);
1210 real_palette_set(inst
, first
[n
] + 1, r
, g
, b
);
1212 set_window_background(inst
);
1215 void palette_reset(void *frontend
)
1217 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1218 /* This maps colour indices in inst->cfg to those used in inst->cols. */
1219 static const int ww
[] = {
1220 6, 7, 8, 9, 10, 11, 12, 13,
1221 14, 15, 16, 17, 18, 19, 20, 21,
1224 gboolean success
[NCOLOURS
];
1227 assert(lenof(ww
) == NCOLOURS
);
1229 if (!inst
->colmap
) {
1230 inst
->colmap
= gdk_colormap_get_system();
1232 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
, NCOLOURS
);
1235 for (i
= 0; i
< NCOLOURS
; i
++) {
1236 inst
->cols
[i
].red
= inst
->cfg
.colours
[ww
[i
]][0] * 0x0101;
1237 inst
->cols
[i
].green
= inst
->cfg
.colours
[ww
[i
]][1] * 0x0101;
1238 inst
->cols
[i
].blue
= inst
->cfg
.colours
[ww
[i
]][2] * 0x0101;
1241 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
1242 FALSE
, FALSE
, success
);
1243 for (i
= 0; i
< NCOLOURS
; i
++) {
1245 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1246 i
, inst
->cfg
.colours
[i
][0], inst
->cfg
.colours
[i
][1], inst
->cfg
.colours
[i
][2]);
1249 set_window_background(inst
);
1252 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
1254 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1255 if (inst
->pasteout_data
)
1256 sfree(inst
->pasteout_data
);
1257 if (inst
->pasteout_data_utf8
)
1258 sfree(inst
->pasteout_data_utf8
);
1261 * Set up UTF-8 paste data. This only happens if we aren't in
1262 * direct-to-font mode using the D800 hack.
1264 if (!inst
->direct_to_font
) {
1265 wchar_t *tmp
= data
;
1268 inst
->pasteout_data_utf8
= smalloc(len
*6);
1269 inst
->pasteout_data_utf8_len
= len
*6;
1270 inst
->pasteout_data_utf8_len
=
1271 charset_from_unicode(&tmp
, &tmplen
, inst
->pasteout_data_utf8
,
1272 inst
->pasteout_data_utf8_len
,
1273 CS_UTF8
, NULL
, NULL
, 0);
1274 if (inst
->pasteout_data_utf8_len
== 0) {
1275 sfree(inst
->pasteout_data_utf8
);
1276 inst
->pasteout_data_utf8
= NULL
;
1278 inst
->pasteout_data_utf8
=
1279 srealloc(inst
->pasteout_data_utf8
,
1280 inst
->pasteout_data_utf8_len
);
1283 inst
->pasteout_data_utf8
= NULL
;
1284 inst
->pasteout_data_utf8_len
= 0;
1287 inst
->pasteout_data
= smalloc(len
*6);
1288 inst
->pasteout_data_len
= len
*6;
1289 inst
->pasteout_data_len
= wc_to_mb(inst
->ucsdata
.line_codepage
, 0,
1290 data
, len
, inst
->pasteout_data
,
1291 inst
->pasteout_data_len
,
1293 if (inst
->pasteout_data_len
== 0) {
1294 sfree(inst
->pasteout_data
);
1295 inst
->pasteout_data
= NULL
;
1297 inst
->pasteout_data
=
1298 srealloc(inst
->pasteout_data
, inst
->pasteout_data_len
);
1301 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
1302 GDK_CURRENT_TIME
)) {
1303 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1304 GDK_SELECTION_TYPE_STRING
, 1);
1305 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1306 inst
->compound_text_atom
, 1);
1307 if (inst
->pasteout_data_utf8
)
1308 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1309 inst
->utf8_string_atom
, 1);
1313 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1314 guint info
, guint time_stamp
, gpointer data
)
1316 struct gui_data
*inst
= (struct gui_data
*)data
;
1317 if (seldata
->target
== inst
->utf8_string_atom
)
1318 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1319 inst
->pasteout_data_utf8
,
1320 inst
->pasteout_data_utf8_len
);
1322 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1323 inst
->pasteout_data
, inst
->pasteout_data_len
);
1326 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1329 struct gui_data
*inst
= (struct gui_data
*)data
;
1330 term_deselect(inst
->term
);
1331 if (inst
->pasteout_data
)
1332 sfree(inst
->pasteout_data
);
1333 if (inst
->pasteout_data_utf8
)
1334 sfree(inst
->pasteout_data_utf8
);
1335 inst
->pasteout_data
= NULL
;
1336 inst
->pasteout_data_len
= 0;
1337 inst
->pasteout_data_utf8
= NULL
;
1338 inst
->pasteout_data_utf8_len
= 0;
1342 void request_paste(void *frontend
)
1344 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1346 * In Unix, pasting is asynchronous: all we can do at the
1347 * moment is to call gtk_selection_convert(), and when the data
1348 * comes back _then_ we can call term_do_paste().
1351 if (!inst
->direct_to_font
) {
1353 * First we attempt to retrieve the selection as a UTF-8
1354 * string (which we will convert to the correct code page
1355 * before sending to the session, of course). If that
1356 * fails, selection_received() will be informed and will
1357 * fall back to an ordinary string.
1359 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1360 inst
->utf8_string_atom
, GDK_CURRENT_TIME
);
1363 * If we're in direct-to-font mode, we disable UTF-8
1364 * pasting, and go straight to ordinary string data.
1366 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1367 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1371 gint
idle_paste_func(gpointer data
); /* forward ref */
1373 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1374 guint time
, gpointer data
)
1376 struct gui_data
*inst
= (struct gui_data
*)data
;
1378 if (seldata
->target
== inst
->utf8_string_atom
&& seldata
->length
<= 0) {
1380 * Failed to get a UTF-8 selection string. Try an ordinary
1383 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1384 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1389 * Any other failure should just go foom.
1391 if (seldata
->length
<= 0 ||
1392 (seldata
->type
!= GDK_SELECTION_TYPE_STRING
&&
1393 seldata
->type
!= inst
->utf8_string_atom
))
1394 return; /* Nothing happens. */
1396 if (inst
->pastein_data
)
1397 sfree(inst
->pastein_data
);
1399 inst
->pastein_data
= smalloc(seldata
->length
* sizeof(wchar_t));
1400 inst
->pastein_data_len
= seldata
->length
;
1401 inst
->pastein_data_len
=
1402 mb_to_wc((seldata
->type
== inst
->utf8_string_atom ?
1403 CS_UTF8
: inst
->ucsdata
.line_codepage
),
1404 0, seldata
->data
, seldata
->length
,
1405 inst
->pastein_data
, inst
->pastein_data_len
);
1407 term_do_paste(inst
->term
);
1409 if (term_paste_pending(inst
->term
))
1410 inst
->term_paste_idle_id
= gtk_idle_add(idle_paste_func
, inst
);
1413 gint
idle_paste_func(gpointer data
)
1415 struct gui_data
*inst
= (struct gui_data
*)data
;
1417 if (term_paste_pending(inst
->term
))
1418 term_paste(inst
->term
);
1420 gtk_idle_remove(inst
->term_paste_idle_id
);
1426 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
1428 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1431 *p
= inst
->pastein_data
;
1432 *len
= inst
->pastein_data_len
;
1436 void set_title(void *frontend
, char *title
)
1438 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1439 strncpy(inst
->wintitle
, title
, lenof(inst
->wintitle
));
1440 inst
->wintitle
[lenof(inst
->wintitle
)-1] = '\0';
1441 gtk_window_set_title(GTK_WINDOW(inst
->window
), inst
->wintitle
);
1444 void set_icon(void *frontend
, char *title
)
1446 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1447 strncpy(inst
->icontitle
, title
, lenof(inst
->icontitle
));
1448 inst
->icontitle
[lenof(inst
->icontitle
)-1] = '\0';
1449 gdk_window_set_icon_name(inst
->window
->window
, inst
->icontitle
);
1452 void set_sbar(void *frontend
, int total
, int start
, int page
)
1454 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1455 if (!inst
->cfg
.scrollbar
)
1457 inst
->sbar_adjust
->lower
= 0;
1458 inst
->sbar_adjust
->upper
= total
;
1459 inst
->sbar_adjust
->value
= start
;
1460 inst
->sbar_adjust
->page_size
= page
;
1461 inst
->sbar_adjust
->step_increment
= 1;
1462 inst
->sbar_adjust
->page_increment
= page
/2;
1463 inst
->ignore_sbar
= TRUE
;
1464 gtk_adjustment_changed(inst
->sbar_adjust
);
1465 inst
->ignore_sbar
= FALSE
;
1468 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
1470 struct gui_data
*inst
= (struct gui_data
*)data
;
1472 if (!inst
->cfg
.scrollbar
)
1474 if (!inst
->ignore_sbar
)
1475 term_scroll(inst
->term
, 1, (int)adj
->value
);
1478 void sys_cursor(void *frontend
, int x
, int y
)
1481 * This is meaningless under X.
1486 * This is still called when mode==BELL_VISUAL, even though the
1487 * visual bell is handled entirely within terminal.c, because we
1488 * may want to perform additional actions on any kind of bell (for
1489 * example, taskbar flashing in Windows).
1491 void beep(void *frontend
, int mode
)
1493 if (mode
!= BELL_VISUAL
)
1497 int char_width(Context ctx
, int uc
)
1500 * Under X, any fixed-width font really _is_ fixed-width.
1501 * Double-width characters will be dealt with using a separate
1502 * font. For the moment we can simply return 1.
1507 Context
get_ctx(void *frontend
)
1509 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1510 struct draw_ctx
*dctx
;
1512 if (!inst
->area
->window
)
1515 dctx
= smalloc(sizeof(*dctx
));
1517 dctx
->gc
= gdk_gc_new(inst
->area
->window
);
1521 void free_ctx(Context ctx
)
1523 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1524 /* struct gui_data *inst = dctx->inst; */
1525 GdkGC
*gc
= dctx
->gc
;
1531 * Draw a line of text in the window, at given character
1532 * coordinates, in given attributes.
1534 * We are allowed to fiddle with the contents of `text'.
1536 void do_text_internal(Context ctx
, int x
, int y
, char *text
, int len
,
1537 unsigned long attr
, int lattr
)
1539 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1540 struct gui_data
*inst
= dctx
->inst
;
1541 GdkGC
*gc
= dctx
->gc
;
1543 int nfg
, nbg
, t
, fontid
, shadow
, rlen
, widefactor
;
1545 nfg
= 2 * ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1546 nbg
= 2 * ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1547 if (attr
& ATTR_REVERSE
) {
1552 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
1554 if (inst
->cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
1556 if (attr
& TATTR_ACTCURS
) {
1561 fontid
= shadow
= 0;
1563 if (attr
& ATTR_WIDE
) {
1570 if ((attr
& ATTR_BOLD
) && !inst
->cfg
.bold_colour
) {
1571 if (inst
->fonts
[fontid
| 1])
1577 if (lattr
!= LATTR_NORM
) {
1579 if (x
>= inst
->term
->cols
)
1581 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1582 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1590 r
.x
= x
*inst
->font_width
+inst
->cfg
.window_border
;
1591 r
.y
= y
*inst
->font_height
+inst
->cfg
.window_border
;
1592 r
.width
= rlen
*widefactor
*inst
->font_width
;
1593 r
.height
= inst
->font_height
;
1594 gdk_gc_set_clip_rectangle(gc
, &r
);
1597 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
1598 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
1599 x
*inst
->font_width
+inst
->cfg
.window_border
,
1600 y
*inst
->font_height
+inst
->cfg
.window_border
,
1601 rlen
*widefactor
*inst
->font_width
, inst
->font_height
);
1603 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
1610 wcs
= smalloc(sizeof(wchar_t) * (len
+1));
1611 for (i
= 0; i
< len
; i
++) {
1612 wcs
[i
] = (wchar_t) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
1615 if (inst
->fonts
[fontid
] == NULL
) {
1617 * The font for this contingency does not exist.
1618 * Typically this means we've been given ATTR_WIDE
1619 * character and have no wide font. So we display
1620 * nothing at all; such is life.
1622 } else if (inst
->fontinfo
[fontid
].is_wide
) {
1624 * At least one version of gdk_draw_text_wc() has a
1625 * weird bug whereby it reads `len' elements of the
1626 * input string, but only draws `len/2'. Hence I'm
1627 * going to make its input array twice as long as it
1628 * theoretically needs to be, and pass in twice the
1629 * actual number of characters. If a fixed gdk actually
1630 * takes the doubled length seriously, then (a) the
1631 * array will stand scrutiny up to the full length, (b)
1632 * the spare elements of the array are full of zeroes
1633 * which will probably be an empty glyph in the font,
1634 * and (c) the clip rectangle should prevent it causing
1637 gwcs
= smalloc(sizeof(GdkWChar
) * (len
*2+1));
1638 memset(gwcs
, 0, sizeof(GdkWChar
) * (len
*2+1));
1640 * FIXME: when we have a wide-char equivalent of
1641 * from_unicode, use it instead of this.
1643 for (i
= 0; i
<= len
; i
++)
1645 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1646 x
*inst
->font_width
+inst
->cfg
.window_border
,
1647 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1651 gcs
= smalloc(sizeof(GdkWChar
) * (len
+1));
1652 wc_to_mb(inst
->fontinfo
[fontid
].charset
, 0,
1653 wcs
, len
, gcs
, len
, ".", NULL
, NULL
);
1654 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1655 x
*inst
->font_width
+inst
->cfg
.window_border
,
1656 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1664 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1665 x
*inst
->font_width
+inst
->cfg
.window_border
+ inst
->cfg
.shadowboldoffset
,
1666 y
*inst
->font_height
+inst
->cfg
.window_border
+inst
->fonts
[0]->ascent
,
1670 if (attr
& ATTR_UNDER
) {
1671 int uheight
= inst
->fonts
[0]->ascent
+ 1;
1672 if (uheight
>= inst
->font_height
)
1673 uheight
= inst
->font_height
- 1;
1674 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+inst
->cfg
.window_border
,
1675 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
,
1676 (x
+len
)*widefactor
*inst
->font_width
-1+inst
->cfg
.window_border
,
1677 y
*inst
->font_height
+ uheight
+ inst
->cfg
.window_border
);
1680 if (lattr
!= LATTR_NORM
) {
1682 * I can't find any plausible StretchBlt equivalent in the
1683 * X server, so I'm going to do this the slow and painful
1684 * way. This will involve repeated calls to
1685 * gdk_draw_pixmap() to stretch the text horizontally. It's
1686 * O(N^2) in time and O(N) in network bandwidth, but you
1687 * try thinking of a better way. :-(
1690 for (i
= 0; i
< len
* widefactor
* inst
->font_width
; i
++) {
1691 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1692 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
,
1693 y
*inst
->font_height
+inst
->cfg
.window_border
,
1694 x
*inst
->font_width
+inst
->cfg
.window_border
+ 2*i
+1,
1695 y
*inst
->font_height
+inst
->cfg
.window_border
,
1696 len
* inst
->font_width
- i
, inst
->font_height
);
1699 if (lattr
!= LATTR_WIDE
) {
1701 /* Now stretch vertically, in the same way. */
1702 if (lattr
== LATTR_BOT
)
1706 for (i
= 0; i
< inst
->font_height
; i
+=2) {
1707 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1708 x
*inst
->font_width
+inst
->cfg
.window_border
,
1709 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*i
+db
,
1710 x
*widefactor
*inst
->font_width
+inst
->cfg
.window_border
,
1711 y
*inst
->font_height
+inst
->cfg
.window_border
+dt
*(i
+1),
1712 len
* inst
->font_width
, inst
->font_height
-i
-1);
1718 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
1719 unsigned long attr
, int lattr
)
1721 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1722 struct gui_data
*inst
= dctx
->inst
;
1723 GdkGC
*gc
= dctx
->gc
;
1726 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1728 if (attr
& ATTR_WIDE
) {
1734 if (lattr
!= LATTR_NORM
) {
1736 if (x
>= inst
->term
->cols
)
1738 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1739 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1743 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1744 x
*inst
->font_width
+inst
->cfg
.window_border
,
1745 y
*inst
->font_height
+inst
->cfg
.window_border
,
1746 x
*inst
->font_width
+inst
->cfg
.window_border
,
1747 y
*inst
->font_height
+inst
->cfg
.window_border
,
1748 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1751 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1752 unsigned long attr
, int lattr
)
1754 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1755 struct gui_data
*inst
= dctx
->inst
;
1756 GdkGC
*gc
= dctx
->gc
;
1758 int passive
, widefactor
;
1760 if (attr
& TATTR_PASCURS
) {
1761 attr
&= ~TATTR_PASCURS
;
1765 if ((attr
& TATTR_ACTCURS
) && inst
->cfg
.cursor_type
!= 0) {
1766 attr
&= ~TATTR_ACTCURS
;
1768 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1770 if (attr
& ATTR_WIDE
) {
1776 if (lattr
!= LATTR_NORM
) {
1778 if (x
>= inst
->term
->cols
)
1780 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1781 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1785 if (inst
->cfg
.cursor_type
== 0) {
1787 * An active block cursor will already have been done by
1788 * the above do_text call, so we only need to do anything
1792 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1793 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
1794 x
*inst
->font_width
+inst
->cfg
.window_border
,
1795 y
*inst
->font_height
+inst
->cfg
.window_border
,
1796 len
*inst
->font_width
-1, inst
->font_height
-1);
1800 int startx
, starty
, dx
, dy
, length
, i
;
1804 if ((attr
& ATTR_WIDE
) || lattr
!= LATTR_NORM
)
1805 char_width
= 2*inst
->font_width
;
1807 char_width
= inst
->font_width
;
1809 if (inst
->cfg
.cursor_type
== 1) {
1810 uheight
= inst
->fonts
[0]->ascent
+ 1;
1811 if (uheight
>= inst
->font_height
)
1812 uheight
= inst
->font_height
- 1;
1814 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
;
1815 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
+ uheight
;
1818 length
= len
* char_width
;
1821 if (attr
& TATTR_RIGHTCURS
)
1822 xadjust
= char_width
- 1;
1823 startx
= x
* inst
->font_width
+ inst
->cfg
.window_border
+ xadjust
;
1824 starty
= y
* inst
->font_height
+ inst
->cfg
.window_border
;
1827 length
= inst
->font_height
;
1830 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1832 for (i
= 0; i
< length
; i
++) {
1834 gdk_draw_point(inst
->pixmap
, gc
, startx
, starty
);
1840 gdk_draw_line(inst
->pixmap
, gc
, startx
, starty
,
1841 startx
+ (length
-1) * dx
, starty
+ (length
-1) * dy
);
1845 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1846 x
*inst
->font_width
+inst
->cfg
.window_border
,
1847 y
*inst
->font_height
+inst
->cfg
.window_border
,
1848 x
*inst
->font_width
+inst
->cfg
.window_border
,
1849 y
*inst
->font_height
+inst
->cfg
.window_border
,
1850 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1853 GdkCursor
*make_mouse_ptr(struct gui_data
*inst
, int cursor_val
)
1856 * Truly hideous hack: GTK doesn't allow us to set the mouse
1857 * cursor foreground and background colours unless we've _also_
1858 * created our own cursor from bitmaps. Therefore, I need to
1859 * load the `cursor' font and draw glyphs from it on to
1860 * pixmaps, in order to construct my cursors with the fg and bg
1861 * I want. This is a gross hack, but it's more self-contained
1862 * than linking in Xlib to find the X window handle to
1863 * inst->area and calling XRecolorCursor, and it's more
1864 * futureproof than hard-coding the shapes as bitmap arrays.
1866 static GdkFont
*cursor_font
= NULL
;
1867 GdkPixmap
*source
, *mask
;
1869 GdkColor cfg
= { 0, 65535, 65535, 65535 };
1870 GdkColor cbg
= { 0, 0, 0, 0 };
1871 GdkColor dfg
= { 1, 65535, 65535, 65535 };
1872 GdkColor dbg
= { 0, 0, 0, 0 };
1875 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
1877 if (cursor_val
== -2) {
1878 gdk_font_unref(cursor_font
);
1882 if (cursor_val
>= 0 && !cursor_font
)
1883 cursor_font
= gdk_font_load("cursor");
1886 * Get the text extent of the cursor in question. We use the
1887 * mask character for this, because it's typically slightly
1888 * bigger than the main character.
1890 if (cursor_val
>= 0) {
1892 text
[0] = (char)cursor_val
+ 1;
1893 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
1894 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
1900 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
1901 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
1904 * Draw the mask character on the mask pixmap.
1906 gc
= gdk_gc_new(mask
);
1907 gdk_gc_set_foreground(gc
, &dbg
);
1908 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
1909 if (cursor_val
>= 0) {
1911 text
[0] = (char)cursor_val
+ 1;
1912 gdk_gc_set_foreground(gc
, &dfg
);
1913 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
1918 * Draw the main character on the source pixmap.
1920 gc
= gdk_gc_new(source
);
1921 gdk_gc_set_foreground(gc
, &dbg
);
1922 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
1923 if (cursor_val
>= 0) {
1925 text
[0] = (char)cursor_val
;
1926 gdk_gc_set_foreground(gc
, &dfg
);
1927 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
1932 * Create the cursor.
1934 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
1939 gdk_pixmap_unref(source
);
1940 gdk_pixmap_unref(mask
);
1945 void modalfatalbox(char *p
, ...)
1948 fprintf(stderr
, "FATAL ERROR: ");
1950 vfprintf(stderr
, p
, ap
);
1952 fputc('\n', stderr
);
1956 char *get_x_display(void *frontend
)
1958 return gdk_get_display();
1961 static void help(FILE *fp
) {
1963 "pterm option summary:\n"
1965 " --display DISPLAY Specify X display to use (note '--')\n"
1966 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1967 " -fn FONT Normal text font\n"
1968 " -fb FONT Bold text font\n"
1969 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1970 " -sl LINES Number of lines of scrollback\n"
1971 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1972 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1973 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1974 " -T TITLE Window title\n"
1975 " -ut, +ut Do(default) or do not update utmp\n"
1976 " -ls, +ls Do(default) or do not make shell a login shell\n"
1977 " -sb, +sb Do(default) or do not display a scrollbar\n"
1978 " -log PATH Log all output to a file\n"
1979 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1980 " -xrm RESOURCE-STRING Set an X resource\n"
1981 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1982 ) < 0 || fflush(fp
) < 0) {
1983 perror("output error");
1988 int do_cmdline(int argc
, char **argv
, int do_everything
, Config
*cfg
)
1991 extern char **pty_argv
; /* declared in pty.c */
1994 * Macros to make argument handling easier. Note that because
1995 * they need to call `continue', they cannot be contained in
1996 * the usual do {...} while (0) wrapper to make them
1997 * syntactically single statements; hence it is not legal to
1998 * use one of these macros as an unbraced statement between
2001 #define EXPECTS_ARG { \
2002 if (--argc <= 0) { \
2004 fprintf(stderr, "pterm: %s expects an argument\n", p); \
2009 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
2018 while (--argc
> 0) {
2020 if (!strcmp(p
, "-fn") || !strcmp(p
, "-font")) {
2023 strncpy(cfg
->font
, val
, sizeof(cfg
->font
));
2024 cfg
->font
[sizeof(cfg
->font
)-1] = '\0';
2026 } else if (!strcmp(p
, "-fb")) {
2029 strncpy(cfg
->boldfont
, val
, sizeof(cfg
->boldfont
));
2030 cfg
->boldfont
[sizeof(cfg
->boldfont
)-1] = '\0';
2032 } else if (!strcmp(p
, "-fw")) {
2035 strncpy(cfg
->widefont
, val
, sizeof(cfg
->widefont
));
2036 cfg
->widefont
[sizeof(cfg
->widefont
)-1] = '\0';
2038 } else if (!strcmp(p
, "-fwb")) {
2041 strncpy(cfg
->wideboldfont
, val
, sizeof(cfg
->wideboldfont
));
2042 cfg
->wideboldfont
[sizeof(cfg
->wideboldfont
)-1] = '\0';
2044 } else if (!strcmp(p
, "-cs")) {
2047 strncpy(cfg
->line_codepage
, val
, sizeof(cfg
->line_codepage
));
2048 cfg
->line_codepage
[sizeof(cfg
->line_codepage
)-1] = '\0';
2050 } else if (!strcmp(p
, "-geometry")) {
2051 int flags
, x
, y
, w
, h
;
2055 flags
= XParseGeometry(val
, &x
, &y
, &w
, &h
);
2056 if (flags
& WidthValue
)
2058 if (flags
& HeightValue
)
2062 * Apparently setting the initial window position is
2063 * difficult in GTK 1.2. Not entirely sure why this
2064 * should be. 2.0 has gtk_window_parse_geometry(),
2065 * which would help... For the moment, though, I can't
2066 * be bothered with this.
2069 } else if (!strcmp(p
, "-sl")) {
2072 cfg
->savelines
= atoi(val
);
2074 } else if (!strcmp(p
, "-fg") || !strcmp(p
, "-bg") ||
2075 !strcmp(p
, "-bfg") || !strcmp(p
, "-bbg") ||
2076 !strcmp(p
, "-cfg") || !strcmp(p
, "-cbg")) {
2081 if (!gdk_color_parse(val
, &col
)) {
2083 fprintf(stderr
, "pterm: unable to parse colour \"%s\"\n", val
);
2086 index
= (!strcmp(p
, "-fg") ?
0 :
2087 !strcmp(p
, "-bg") ?
2 :
2088 !strcmp(p
, "-bfg") ?
1 :
2089 !strcmp(p
, "-bbg") ?
3 :
2090 !strcmp(p
, "-cfg") ?
4 :
2091 !strcmp(p
, "-cbg") ?
5 : -1);
2092 assert(index
!= -1);
2093 cfg
->colours
[index
][0] = col
.red
/ 256;
2094 cfg
->colours
[index
][1] = col
.green
/ 256;
2095 cfg
->colours
[index
][2] = col
.blue
/ 256;
2098 } else if (!strcmp(p
, "-e")) {
2099 /* This option swallows all further arguments. */
2105 pty_argv
= smalloc((argc
+1) * sizeof(char *));
2107 for (i
= 0; i
< argc
; i
++)
2108 pty_argv
[i
] = argv
[i
];
2109 pty_argv
[argc
] = NULL
;
2110 break; /* finished command-line processing */
2112 err
= 1, fprintf(stderr
, "pterm: -e expects an argument\n");
2114 } else if (!strcmp(p
, "-T")) {
2117 strncpy(cfg
->wintitle
, val
, sizeof(cfg
->wintitle
));
2118 cfg
->wintitle
[sizeof(cfg
->wintitle
)-1] = '\0';
2120 } else if (!strcmp(p
, "-log")) {
2123 strncpy(cfg
->logfilename
, val
, sizeof(cfg
->logfilename
));
2124 cfg
->logfilename
[sizeof(cfg
->logfilename
)-1] = '\0';
2125 cfg
->logtype
= LGTYP_DEBUG
;
2127 } else if (!strcmp(p
, "-ut-") || !strcmp(p
, "+ut")) {
2129 cfg
->stamp_utmp
= 0;
2131 } else if (!strcmp(p
, "-ut")) {
2133 cfg
->stamp_utmp
= 1;
2135 } else if (!strcmp(p
, "-ls-") || !strcmp(p
, "+ls")) {
2137 cfg
->login_shell
= 0;
2139 } else if (!strcmp(p
, "-ls")) {
2141 cfg
->login_shell
= 1;
2143 } else if (!strcmp(p
, "-nethack")) {
2145 cfg
->nethack_keypad
= 1;
2147 } else if (!strcmp(p
, "-sb-") || !strcmp(p
, "+sb")) {
2151 } else if (!strcmp(p
, "-sb")) {
2155 } else if (!strcmp(p
, "-name")) {
2159 } else if (!strcmp(p
, "-xrm")) {
2161 provide_xrm_string(val
);
2163 } else if(!strcmp(p
, "-help") || !strcmp(p
, "--help")) {
2169 fprintf(stderr
, "pterm: unrecognized option '%s'\n", p
);
2176 static void block_signal(int sig
, int block_it
) {
2180 sigaddset(&ss
, sig
);
2181 if(sigprocmask(block_it ? SIG_BLOCK
: SIG_UNBLOCK
, &ss
, 0) < 0) {
2182 perror("sigprocmask");
2188 * This function retrieves the character set encoding of a font. It
2189 * returns the character set without the X11 hack (in case the user
2190 * asks to use the font's own encoding).
2192 static int set_font_info(struct gui_data
*inst
, int fontid
)
2194 GdkFont
*font
= inst
->fonts
[fontid
];
2195 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2196 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2197 Atom charset_registry
, charset_encoding
;
2198 unsigned long registry_ret
, encoding_ret
;
2199 int retval
= CS_NONE
;
2201 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
2202 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
2203 inst
->fontinfo
[fontid
].charset
= CS_NONE
;
2204 inst
->fontinfo
[fontid
].is_wide
= 0;
2205 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
2206 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
2208 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
2209 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
2211 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
2212 retval
= inst
->fontinfo
[fontid
].charset
=
2213 charset_from_xenc(encoding
);
2214 /* FIXME: when libcharset supports wide encodings fix this. */
2215 if (!strcasecmp(encoding
, "iso10646-1")) {
2216 inst
->fontinfo
[fontid
].is_wide
= 1;
2221 * Hack for X line-drawing characters: if the primary
2222 * font is encoded as ISO-8859-anything, and has valid
2223 * glyphs in the first 32 char positions, it is assumed
2224 * that those glyphs are the VT100 line-drawing
2227 * Actually, we'll hack even harder by only checking
2228 * position 0x19 (vertical line, VT100 linedrawing
2229 * `x'). Then we can check it easily by seeing if the
2230 * ascent and descent differ.
2232 if (inst
->fontinfo
[fontid
].charset
== CS_ISO8859_1
) {
2233 int lb
, rb
, wid
, asc
, desc
;
2238 gdk_string_extents(inst
->fonts
[fontid
], text
,
2239 &lb
, &rb
, &wid
, &asc
, &desc
);
2241 inst
->fontinfo
[fontid
].charset
= CS_ISO8859_1_X11
;
2251 int main(int argc
, char **argv
)
2253 extern int pty_master_fd
; /* declared in pty.c */
2254 extern void pty_pre_init(void); /* declared in pty.c */
2255 struct gui_data
*inst
;
2258 /* defer any child exit handling until we're ready to deal with
2260 block_signal(SIGCHLD
, 1);
2264 gtk_init(&argc
, &argv
);
2267 * Create an instance structure and initialise to zeroes
2269 inst
= smalloc(sizeof(*inst
));
2270 memset(inst
, 0, sizeof(*inst
));
2271 inst
->alt_keycode
= -1; /* this one needs _not_ to be zero */
2273 if (do_cmdline(argc
, argv
, 0, &inst
->cfg
))
2274 exit(1); /* pre-defaults pass to get -class */
2275 do_defaults(NULL
, &inst
->cfg
);
2276 if (do_cmdline(argc
, argv
, 1, &inst
->cfg
))
2277 exit(1); /* post-defaults, do everything */
2279 inst
->fonts
[0] = gdk_font_load(inst
->cfg
.font
);
2280 if (!inst
->fonts
[0]) {
2281 fprintf(stderr
, "pterm: unable to load font \"%s\"\n", inst
->cfg
.font
);
2284 font_charset
= set_font_info(inst
, 0);
2285 if (inst
->cfg
.boldfont
[0]) {
2286 inst
->fonts
[1] = gdk_font_load(inst
->cfg
.boldfont
);
2287 if (!inst
->fonts
[1]) {
2288 fprintf(stderr
, "pterm: unable to load bold font \"%s\"\n",
2289 inst
->cfg
.boldfont
);
2292 set_font_info(inst
, 1);
2294 inst
->fonts
[1] = NULL
;
2295 if (inst
->cfg
.widefont
[0]) {
2296 inst
->fonts
[2] = gdk_font_load(inst
->cfg
.widefont
);
2297 if (!inst
->fonts
[2]) {
2298 fprintf(stderr
, "pterm: unable to load wide font \"%s\"\n",
2299 inst
->cfg
.boldfont
);
2302 set_font_info(inst
, 2);
2304 inst
->fonts
[2] = NULL
;
2305 if (inst
->cfg
.wideboldfont
[0]) {
2306 inst
->fonts
[3] = gdk_font_load(inst
->cfg
.wideboldfont
);
2307 if (!inst
->fonts
[3]) {
2308 fprintf(stderr
, "pterm: unable to load wide/bold font \"%s\"\n",
2309 inst
->cfg
.boldfont
);
2312 set_font_info(inst
, 3);
2314 inst
->fonts
[3] = NULL
;
2316 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
2317 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
2319 inst
->compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
2320 inst
->utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
2322 inst
->direct_to_font
= init_ucs(&inst
->ucsdata
,
2323 inst
->cfg
.line_codepage
, font_charset
);
2325 inst
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2327 if (inst
->cfg
.wintitle
[0])
2328 set_title(inst
, inst
->cfg
.wintitle
);
2330 set_title(inst
, "pterm");
2333 * Set up the colour map.
2335 palette_reset(inst
);
2337 inst
->area
= gtk_drawing_area_new();
2338 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
2339 inst
->font_width
* inst
->cfg
.width
+ 2*inst
->cfg
.window_border
,
2340 inst
->font_height
* inst
->cfg
.height
+ 2*inst
->cfg
.window_border
);
2341 if (inst
->cfg
.scrollbar
) {
2342 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2343 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
2345 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
2346 if (inst
->cfg
.scrollbar
) {
2347 if (inst
->cfg
.scrollbar_on_left
)
2348 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2350 gtk_box_pack_end(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2352 gtk_box_pack_start(inst
->hbox
, inst
->area
, TRUE
, TRUE
, 0);
2354 gtk_container_add(GTK_CONTAINER(inst
->window
), GTK_WIDGET(inst
->hbox
));
2358 geom
.min_width
= inst
->font_width
+ 2*inst
->cfg
.window_border
;
2359 geom
.min_height
= inst
->font_height
+ 2*inst
->cfg
.window_border
;
2360 geom
.max_width
= geom
.max_height
= -1;
2361 geom
.base_width
= 2*inst
->cfg
.window_border
;
2362 geom
.base_height
= 2*inst
->cfg
.window_border
;
2363 geom
.width_inc
= inst
->font_width
;
2364 geom
.height_inc
= inst
->font_height
;
2365 geom
.min_aspect
= geom
.max_aspect
= 0;
2366 gtk_window_set_geometry_hints(GTK_WINDOW(inst
->window
), inst
->area
, &geom
,
2367 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
2368 GDK_HINT_RESIZE_INC
);
2371 gtk_signal_connect(GTK_OBJECT(inst
->window
), "destroy",
2372 GTK_SIGNAL_FUNC(destroy
), inst
);
2373 gtk_signal_connect(GTK_OBJECT(inst
->window
), "delete_event",
2374 GTK_SIGNAL_FUNC(delete_window
), inst
);
2375 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_press_event",
2376 GTK_SIGNAL_FUNC(key_event
), inst
);
2377 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_release_event",
2378 GTK_SIGNAL_FUNC(key_event
), inst
);
2379 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_in_event",
2380 GTK_SIGNAL_FUNC(focus_event
), inst
);
2381 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_out_event",
2382 GTK_SIGNAL_FUNC(focus_event
), inst
);
2383 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
2384 GTK_SIGNAL_FUNC(configure_area
), inst
);
2385 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
2386 GTK_SIGNAL_FUNC(expose_area
), inst
);
2387 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
2388 GTK_SIGNAL_FUNC(button_event
), inst
);
2389 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
2390 GTK_SIGNAL_FUNC(button_event
), inst
);
2391 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
2392 GTK_SIGNAL_FUNC(motion_event
), inst
);
2393 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
2394 GTK_SIGNAL_FUNC(selection_received
), inst
);
2395 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
2396 GTK_SIGNAL_FUNC(selection_get
), inst
);
2397 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
2398 GTK_SIGNAL_FUNC(selection_clear
), inst
);
2399 if (inst
->cfg
.scrollbar
)
2400 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
2401 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
2402 gtk_timeout_add(20, timer_func
, inst
);
2403 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
2404 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
2405 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
2406 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_MOTION_MASK
);
2408 gtk_widget_show(inst
->area
);
2409 if (inst
->cfg
.scrollbar
)
2410 gtk_widget_show(inst
->sbar
);
2411 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
2412 gtk_widget_show(inst
->window
);
2414 set_window_background(inst
);
2416 inst
->textcursor
= make_mouse_ptr(inst
, GDK_XTERM
);
2417 inst
->rawcursor
= make_mouse_ptr(inst
, GDK_LEFT_PTR
);
2418 inst
->blankcursor
= make_mouse_ptr(inst
, -1);
2419 make_mouse_ptr(inst
, -2); /* clean up cursor font */
2420 inst
->currcursor
= inst
->textcursor
;
2421 show_mouseptr(inst
, 1);
2423 inst
->term
= term_init(&inst
->cfg
, &inst
->ucsdata
, inst
);
2424 inst
->logctx
= log_init(inst
, &inst
->cfg
);
2425 term_provide_logctx(inst
->term
, inst
->logctx
);
2427 inst
->back
= &pty_backend
;
2428 inst
->back
->init((void *)inst
->term
, &inst
->backhandle
, &inst
->cfg
,
2430 inst
->back
->provide_logctx(inst
->backhandle
, inst
->logctx
);
2432 term_provide_resize_fn(inst
->term
, inst
->back
->size
, inst
->backhandle
);
2434 term_size(inst
->term
, inst
->cfg
.height
, inst
->cfg
.width
, inst
->cfg
.savelines
);
2437 ldisc_create(&inst
->cfg
, inst
->term
, inst
->back
, inst
->backhandle
, inst
);
2438 ldisc_send(inst
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
2440 inst
->master_fd
= pty_master_fd
;
2441 inst
->exited
= FALSE
;
2442 inst
->master_func_id
= gdk_input_add(pty_master_fd
, GDK_INPUT_READ
,
2443 pty_input_func
, inst
);
2445 /* now we're reday to deal with the child exit handler being
2447 block_signal(SIGCHLD
, 0);