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
;
74 struct gui_data
*inst
;
77 static int send_raw_mouse
;
79 static char *app_name
= "pterm";
81 char *x_get_default(char *key
)
83 return XGetDefault(GDK_DISPLAY(), app_name
, key
);
86 void ldisc_update(void *frontend
, int echo
, int edit
)
89 * This is a stub in pterm. If I ever produce a Unix
90 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
91 * then it will require some termios manoeuvring analogous to
92 * that in the Windows plink.c, but here it's meaningless.
96 int askappend(void *frontend
, char *filename
)
99 * Logging in an xterm-alike is liable to be something you only
100 * do at serious diagnostic need. Hence, I'm going to take the
101 * easy option for now and assume we always want to overwrite
102 * log files. I can always make it properly configurable later.
107 void logevent(void *frontend
, char *string
)
110 * This is not a very helpful function: events are logged
111 * pretty much exclusively by the back end, and our pty back
112 * end is self-contained. So we need do nothing.
116 int font_dimension(void *frontend
, int which
)/* 0 for width, 1 for height */
118 struct gui_data
*inst
= (struct gui_data
*)frontend
;
121 return inst
->font_height
;
123 return inst
->font_width
;
127 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
128 * into a cooked one (SELECT, EXTEND, PASTE).
130 * In Unix, this is not configurable; the X button arrangement is
131 * rock-solid across all applications, everyone has a three-button
132 * mouse or a means of faking it, and there is no need to switch
133 * buttons around at all.
135 Mouse_Button
translate_button(void *frontend
, Mouse_Button button
)
137 /* struct gui_data *inst = (struct gui_data *)frontend; */
139 if (button
== MBT_LEFT
)
141 if (button
== MBT_MIDDLE
)
143 if (button
== MBT_RIGHT
)
145 return 0; /* shouldn't happen */
149 * Minimise or restore the window in response to a server-side
152 void set_iconic(void *frontend
, int iconic
)
155 * GTK 1.2 doesn't know how to do this.
157 #if GTK_CHECK_VERSION(2,0,0)
158 struct gui_data
*inst
= (struct gui_data
*)frontend
;
160 gtk_window_iconify(GTK_WINDOW(inst
->window
));
162 gtk_window_deiconify(GTK_WINDOW(inst
->window
));
167 * Move the window in response to a server-side request.
169 void move_window(void *frontend
, int x
, int y
)
171 struct gui_data
*inst
= (struct gui_data
*)frontend
;
173 * I assume that when the GTK version of this call is available
174 * we should use it. Not sure how it differs from the GDK one,
177 #if GTK_CHECK_VERSION(2,0,0)
178 gtk_window_move(GTK_WINDOW(inst
->window
), x
, y
);
180 gdk_window_move(inst
->window
->window
, x
, y
);
185 * Move the window to the top or bottom of the z-order in response
186 * to a server-side request.
188 void set_zorder(void *frontend
, int top
)
190 struct gui_data
*inst
= (struct gui_data
*)frontend
;
192 gdk_window_raise(inst
->window
->window
);
194 gdk_window_lower(inst
->window
->window
);
198 * Refresh the window in response to a server-side request.
200 void refresh_window(void *frontend
)
202 struct gui_data
*inst
= (struct gui_data
*)frontend
;
203 term_invalidate(inst
->term
);
207 * Maximise or restore the window in response to a server-side
210 void set_zoomed(void *frontend
, int zoomed
)
213 * GTK 1.2 doesn't know how to do this.
215 #if GTK_CHECK_VERSION(2,0,0)
216 struct gui_data
*inst
= (struct gui_data
*)frontend
;
218 gtk_window_maximize(GTK_WINDOW(inst
->window
));
220 gtk_window_unmaximize(GTK_WINDOW(inst
->window
));
225 * Report whether the window is iconic, for terminal reports.
227 int is_iconic(void *frontend
)
229 struct gui_data
*inst
= (struct gui_data
*)frontend
;
230 return !gdk_window_is_viewable(inst
->window
->window
);
234 * Report the window's position, for terminal reports.
236 void get_window_pos(void *frontend
, int *x
, int *y
)
238 struct gui_data
*inst
= (struct gui_data
*)frontend
;
240 * I assume that when the GTK version of this call is available
241 * we should use it. Not sure how it differs from the GDK one,
244 #if GTK_CHECK_VERSION(2,0,0)
245 gtk_window_get_position(GTK_WINDOW(inst
->window
), x
, y
);
247 gdk_window_get_position(inst
->window
->window
, x
, y
);
252 * Report the window's pixel size, for terminal reports.
254 void get_window_pixels(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_size(GTK_WINDOW(inst
->window
), x
, y
);
265 gdk_window_get_size(inst
->window
->window
, x
, y
);
270 * Return the window or icon title.
272 char *get_window_title(void *frontend
, int icon
)
274 struct gui_data
*inst
= (struct gui_data
*)frontend
;
275 return icon ? inst
->wintitle
: inst
->icontitle
;
278 gint
delete_window(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
281 * We could implement warn-on-close here if we really wanted
287 static void show_mouseptr(struct gui_data
*inst
, int show
)
289 if (!cfg
.hide_mouseptr
)
292 gdk_window_set_cursor(inst
->area
->window
, inst
->currcursor
);
294 gdk_window_set_cursor(inst
->area
->window
, inst
->blankcursor
);
295 inst
->mouseptr_visible
= show
;
298 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
300 struct gui_data
*inst
= (struct gui_data
*)data
;
301 int w
, h
, need_size
= 0;
303 w
= (event
->width
- 2*cfg
.window_border
) / inst
->font_width
;
304 h
= (event
->height
- 2*cfg
.window_border
) / inst
->font_height
;
306 if (w
!= cfg
.width
|| h
!= cfg
.height
) {
308 gdk_pixmap_unref(inst
->pixmap
);
318 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
319 (cfg
.width
* inst
->font_width
+
320 2*cfg
.window_border
),
321 (cfg
.height
* inst
->font_height
+
322 2*cfg
.window_border
), -1);
324 gc
= gdk_gc_new(inst
->area
->window
);
325 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
326 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
327 cfg
.width
* inst
->font_width
+ 2*cfg
.window_border
,
328 cfg
.height
* inst
->font_height
+ 2*cfg
.window_border
);
333 term_size(inst
->term
, h
, w
, cfg
.savelines
);
339 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
341 struct gui_data
*inst
= (struct gui_data
*)data
;
344 * Pass the exposed rectangle to terminal.c, which will call us
345 * back to do the actual painting.
348 gdk_draw_pixmap(widget
->window
,
349 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
351 event
->area
.x
, event
->area
.y
,
352 event
->area
.x
, event
->area
.y
,
353 event
->area
.width
, event
->area
.height
);
358 #define KEY_PRESSED(k) \
359 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
361 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
363 struct gui_data
*inst
= (struct gui_data
*)data
;
367 /* By default, nothing is generated. */
371 * If Alt is being released after typing an Alt+numberpad
372 * sequence, we should generate the code that was typed.
374 * Note that we only do this if more than one key was actually
375 * pressed - I don't think Alt+NumPad4 should be ^D or that
376 * Alt+NumPad3 should be ^C, for example. There's no serious
377 * inconvenience in having to type a zero before a single-digit
380 if (event
->type
== GDK_KEY_RELEASE
&&
381 (event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
382 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
) &&
383 inst
->alt_keycode
>= 0 && inst
->alt_digits
> 1) {
385 printf("Alt key up, keycode = %d\n", inst
->alt_keycode
);
387 output
[0] = inst
->alt_keycode
;
392 if (event
->type
== GDK_KEY_PRESS
) {
396 printf("keypress: keyval = %04x, state = %08x; string =",
397 event
->keyval
, event
->state
);
398 for (i
= 0; event
->string
[i
]; i
++)
399 printf(" %02x", (unsigned char) event
->string
[i
]);
405 * NYI: Compose key (!!! requires Unicode faff before even trying)
409 * If Alt has just been pressed, we start potentially
410 * accumulating an Alt+numberpad code. We do this by
411 * setting alt_keycode to -1 (nothing yet but plausible).
413 if ((event
->keyval
== GDK_Meta_L
|| event
->keyval
== GDK_Alt_L
||
414 event
->keyval
== GDK_Meta_R
|| event
->keyval
== GDK_Alt_R
)) {
415 inst
->alt_keycode
= -1;
416 inst
->alt_digits
= 0;
417 goto done
; /* this generates nothing else */
421 * If we're seeing a numberpad key press with Mod1 down,
422 * consider adding it to alt_keycode if that's sensible.
423 * Anything _else_ with Mod1 down cancels any possibility
424 * of an ALT keycode: we set alt_keycode to -2.
426 if ((event
->state
& GDK_MOD1_MASK
) && inst
->alt_keycode
!= -2) {
428 switch (event
->keyval
) {
429 case GDK_KP_0
: case GDK_KP_Insert
: digit
= 0; break;
430 case GDK_KP_1
: case GDK_KP_End
: digit
= 1; break;
431 case GDK_KP_2
: case GDK_KP_Down
: digit
= 2; break;
432 case GDK_KP_3
: case GDK_KP_Page_Down
: digit
= 3; break;
433 case GDK_KP_4
: case GDK_KP_Left
: digit
= 4; break;
434 case GDK_KP_5
: case GDK_KP_Begin
: digit
= 5; break;
435 case GDK_KP_6
: case GDK_KP_Right
: digit
= 6; break;
436 case GDK_KP_7
: case GDK_KP_Home
: digit
= 7; break;
437 case GDK_KP_8
: case GDK_KP_Up
: digit
= 8; break;
438 case GDK_KP_9
: case GDK_KP_Page_Up
: digit
= 9; break;
441 inst
->alt_keycode
= -2; /* it's invalid */
444 printf("Adding digit %d to keycode %d", digit
,
447 if (inst
->alt_keycode
== -1)
448 inst
->alt_keycode
= digit
; /* one-digit code */
450 inst
->alt_keycode
= inst
->alt_keycode
* 10 + digit
;
453 printf(" gives new code %d\n", inst
->alt_keycode
);
455 /* Having used this digit, we now do nothing more with it. */
461 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
464 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
465 term_scroll(inst
->term
, 0, -cfg
.height
/2);
468 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
469 term_scroll(inst
->term
, 0, +cfg
.height
/2);
474 * Neither does Shift-Ins.
476 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
481 /* ALT+things gives leading Escape. */
483 strncpy(output
+1, event
->string
, 31);
485 end
= strlen(output
);
486 if (event
->state
& GDK_MOD1_MASK
) {
488 if (end
== 1) end
= 0;
492 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
493 if (!event
->string
[0] && event
->keyval
== '`' &&
494 (event
->state
& GDK_CONTROL_MASK
)) {
499 /* Control-Break is the same as Control-C */
500 if (event
->keyval
== GDK_Break
&&
501 (event
->state
& GDK_CONTROL_MASK
)) {
506 /* Control-2, Control-Space and Control-@ are NUL */
507 if (!event
->string
[0] &&
508 (event
->keyval
== ' ' || event
->keyval
== '2' ||
509 event
->keyval
== '@') &&
510 (event
->state
& (GDK_SHIFT_MASK
|
511 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
516 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
517 if (!event
->string
[0] && event
->keyval
== ' ' &&
518 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
519 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
524 /* We don't let GTK tell us what Backspace is! We know better. */
525 if (event
->keyval
== GDK_BackSpace
&&
526 !(event
->state
& GDK_SHIFT_MASK
)) {
527 output
[1] = cfg
.bksp_is_delete ?
'\x7F' : '\x08';
530 /* For Shift Backspace, do opposite of what is configured. */
531 if (event
->keyval
== GDK_BackSpace
&&
532 (event
->state
& GDK_SHIFT_MASK
)) {
533 output
[1] = cfg
.bksp_is_delete ?
'\x08' : '\x7F';
537 /* Shift-Tab is ESC [ Z */
538 if (event
->keyval
== GDK_ISO_Left_Tab
||
539 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
540 end
= 1 + sprintf(output
+1, "\033[Z");
544 * NetHack keypad mode.
546 if (cfg
.nethack_keypad
) {
548 switch (event
->keyval
) {
549 case GDK_KP_1
: case GDK_KP_End
: keys
= "bB"; break;
550 case GDK_KP_2
: case GDK_KP_Down
: keys
= "jJ"; break;
551 case GDK_KP_3
: case GDK_KP_Page_Down
: keys
= "nN"; break;
552 case GDK_KP_4
: case GDK_KP_Left
: keys
= "hH"; break;
553 case GDK_KP_5
: case GDK_KP_Begin
: keys
= ".."; break;
554 case GDK_KP_6
: case GDK_KP_Right
: keys
= "lL"; break;
555 case GDK_KP_7
: case GDK_KP_Home
: keys
= "yY"; break;
556 case GDK_KP_8
: case GDK_KP_Up
: keys
= "kK"; break;
557 case GDK_KP_9
: case GDK_KP_Page_Up
: keys
= "uU"; break;
561 if (event
->state
& GDK_SHIFT_MASK
)
570 * Application keypad mode.
572 if (inst
->term
->app_keypad_keys
&& !cfg
.no_applic_k
) {
574 switch (event
->keyval
) {
575 case GDK_Num_Lock
: xkey
= 'P'; break;
576 case GDK_KP_Divide
: xkey
= 'Q'; break;
577 case GDK_KP_Multiply
: xkey
= 'R'; break;
578 case GDK_KP_Subtract
: xkey
= 'S'; break;
580 * Keypad + is tricky. It covers a space that would
581 * be taken up on the VT100 by _two_ keys; so we
582 * let Shift select between the two. Worse still,
583 * in xterm function key mode we change which two...
586 if (cfg
.funky_type
== 2) {
587 if (event
->state
& GDK_SHIFT_MASK
)
591 } else if (event
->state
& GDK_SHIFT_MASK
)
596 case GDK_KP_Enter
: xkey
= 'M'; break;
597 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
598 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
599 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
600 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
601 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
602 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
603 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
604 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
605 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
606 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
607 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
610 if (inst
->term
->vt52_mode
) {
611 if (xkey
>= 'P' && xkey
<= 'S')
612 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
614 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
616 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
622 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
623 * for integer decimal nn.)
625 * We also deal with the weird ones here. Linux VCs replace F1
626 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
627 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
632 switch (event
->keyval
) {
634 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
637 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
640 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
643 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
646 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
649 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
652 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
655 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
658 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
661 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
694 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
695 case GDK_Home
: case GDK_KP_Home
:
698 case GDK_Insert
: case GDK_KP_Insert
:
701 case GDK_Delete
: case GDK_KP_Delete
:
704 case GDK_End
: case GDK_KP_End
:
707 case GDK_Page_Up
: case GDK_KP_Page_Up
:
710 case GDK_Page_Down
: case GDK_KP_Page_Down
:
714 /* Reorder edit keys to physical order */
715 if (cfg
.funky_type
== 3 && code
<= 6)
716 code
= "\0\2\1\4\5\3\6"[code
];
718 if (inst
->term
->vt52_mode
&& code
> 0 && code
<= 6) {
719 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
723 if (cfg
.funky_type
== 5 && /* SCO function keys */
724 code
>= 11 && code
<= 34) {
725 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
727 switch (event
->keyval
) {
728 case GDK_F1
: index
= 0; break;
729 case GDK_F2
: index
= 1; break;
730 case GDK_F3
: index
= 2; break;
731 case GDK_F4
: index
= 3; break;
732 case GDK_F5
: index
= 4; break;
733 case GDK_F6
: index
= 5; break;
734 case GDK_F7
: index
= 6; break;
735 case GDK_F8
: index
= 7; break;
736 case GDK_F9
: index
= 8; break;
737 case GDK_F10
: index
= 9; break;
738 case GDK_F11
: index
= 10; break;
739 case GDK_F12
: index
= 11; break;
741 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
742 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
743 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
746 if (cfg
.funky_type
== 5 && /* SCO small keypad */
747 code
>= 1 && code
<= 6) {
748 char codes
[] = "HL.FIG";
753 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
757 if ((inst
->term
->vt52_mode
|| cfg
.funky_type
== 4) &&
758 code
>= 11 && code
<= 24) {
764 if (inst
->term
->vt52_mode
)
765 end
= 1 + sprintf(output
+1,
766 "\x1B%c", code
+ 'P' - 11 - offt
);
768 end
= 1 + sprintf(output
+1,
769 "\x1BO%c", code
+ 'P' - 11 - offt
);
772 if (cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
773 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
776 if (cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
777 if (inst
->term
->vt52_mode
)
778 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
780 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
783 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
784 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
788 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
794 * Cursor keys. (This includes the numberpad cursor keys,
795 * if we haven't already done them due to app keypad mode.)
797 * Here we also process un-numlocked un-appkeypadded KP5,
798 * which sends ESC [ G.
802 switch (event
->keyval
) {
803 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
804 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
805 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
806 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
807 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
811 * The arrow keys normally do ESC [ A and so on. In
812 * app cursor keys mode they do ESC O A instead.
813 * Ctrl toggles the two modes.
815 if (inst
->term
->vt52_mode
) {
816 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
817 } else if (!inst
->term
->app_cursor_keys
^
818 !(event
->state
& GDK_CONTROL_MASK
)) {
819 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
821 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
834 printf("generating sequence:");
835 for (i
= start
; i
< end
; i
++)
836 printf(" %02x", (unsigned char) output
[i
]);
840 if (!inst
->direct_to_font
) {
842 * The stuff we've just generated is assumed to be
843 * ISO-8859-1! This sounds insane, but `man
844 * XLookupString' agrees: strings of this type returned
845 * from the X server are hardcoded to 8859-1. Strictly
846 * speaking we should be doing this using some sort of
847 * GtkIMContext, which (if we're lucky) would give us
848 * our data directly in Unicode; but that's not
849 * supported in GTK 1.2 as far as I can tell, and it's
850 * poorly documented even in 2.0, so it'll have to
853 lpage_send(inst
->ldisc
, CS_ISO8859_1
, output
+start
, end
-start
, 1);
856 * In direct-to-font mode, we just send the string
857 * exactly as we received it.
859 ldisc_send(inst
->ldisc
, output
+start
, end
-start
, 1);
862 show_mouseptr(inst
, 0);
863 term_seen_key_event(inst
->term
);
864 term_out(inst
->term
);
870 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
872 struct gui_data
*inst
= (struct gui_data
*)data
;
873 int shift
, ctrl
, alt
, x
, y
, button
, act
;
875 show_mouseptr(inst
, 1);
877 if (event
->button
== 4 && event
->type
== GDK_BUTTON_PRESS
) {
878 term_scroll(inst
->term
, 0, -5);
881 if (event
->button
== 5 && event
->type
== GDK_BUTTON_PRESS
) {
882 term_scroll(inst
->term
, 0, +5);
886 shift
= event
->state
& GDK_SHIFT_MASK
;
887 ctrl
= event
->state
& GDK_CONTROL_MASK
;
888 alt
= event
->state
& GDK_MOD1_MASK
;
889 if (event
->button
== 1)
891 else if (event
->button
== 2)
893 else if (event
->button
== 3)
896 return FALSE
; /* don't even know what button! */
898 switch (event
->type
) {
899 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
900 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
901 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
902 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
903 default: return FALSE
; /* don't know this event type */
906 if (send_raw_mouse
&& !(cfg
.mouse_override
&& shift
) &&
907 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
908 return TRUE
; /* we ignore these in raw mouse mode */
910 x
= (event
->x
- cfg
.window_border
) / inst
->font_width
;
911 y
= (event
->y
- cfg
.window_border
) / inst
->font_height
;
913 term_mouse(inst
->term
, button
, act
, x
, y
, shift
, ctrl
, alt
);
918 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
920 struct gui_data
*inst
= (struct gui_data
*)data
;
921 int shift
, ctrl
, alt
, x
, y
, button
;
923 show_mouseptr(inst
, 1);
925 shift
= event
->state
& GDK_SHIFT_MASK
;
926 ctrl
= event
->state
& GDK_CONTROL_MASK
;
927 alt
= event
->state
& GDK_MOD1_MASK
;
928 if (event
->state
& GDK_BUTTON1_MASK
)
930 else if (event
->state
& GDK_BUTTON2_MASK
)
932 else if (event
->state
& GDK_BUTTON3_MASK
)
935 return FALSE
; /* don't even know what button! */
937 x
= (event
->x
- cfg
.window_border
) / inst
->font_width
;
938 y
= (event
->y
- cfg
.window_border
) / inst
->font_height
;
940 term_mouse(inst
->term
, button
, MA_DRAG
, x
, y
, shift
, ctrl
, alt
);
945 void done_with_pty(struct gui_data
*inst
)
947 extern void pty_close(void);
949 if (inst
->master_fd
>= 0) {
951 inst
->master_fd
= -1;
952 gtk_input_remove(inst
->master_func_id
);
955 if (!inst
->exited
&& inst
->back
->exitcode(inst
->backhandle
) >= 0) {
956 int exitcode
= inst
->back
->exitcode(inst
->backhandle
);
959 clean
= WIFEXITED(exitcode
) && (WEXITSTATUS(exitcode
) == 0);
962 * Terminate now, if the Close On Exit setting is
965 if (cfg
.close_on_exit
== COE_ALWAYS
||
966 (cfg
.close_on_exit
== COE_NORMAL
&& clean
))
970 * Otherwise, output an indication that the session has
975 if (WIFEXITED(exitcode
))
976 sprintf(message
, "\r\n[pterm: process terminated with exit"
977 " code %d]\r\n", WEXITSTATUS(exitcode
));
978 else if (WIFSIGNALED(exitcode
))
979 #ifdef HAVE_NO_STRSIGNAL
980 sprintf(message
, "\r\n[pterm: process terminated on signal"
981 " %d]\r\n", WTERMSIG(exitcode
));
983 sprintf(message
, "\r\n[pterm: process terminated on signal"
984 " %d (%.400s)]\r\n", WTERMSIG(exitcode
),
985 strsignal(WTERMSIG(exitcode
)));
987 from_backend((void *)inst
->term
, 0, message
, strlen(message
));
993 void frontend_keypress(void *handle
)
995 struct gui_data
*inst
= (struct gui_data
*)handle
;
998 * If our child process has exited but not closed, terminate on
1005 gint
timer_func(gpointer data
)
1007 struct gui_data
*inst
= (struct gui_data
*)data
;
1009 if (inst
->back
->exitcode(inst
->backhandle
) >= 0) {
1011 * The primary child process died. We could keep the
1012 * terminal open for remaining subprocesses to output to,
1013 * but conventional wisdom seems to feel that that's the
1014 * Wrong Thing for an xterm-alike, so we bail out now
1015 * (though we don't necessarily _close_ the window,
1016 * depending on the state of Close On Exit). This would be
1017 * easy enough to change or make configurable if necessary.
1019 done_with_pty(inst
);
1022 term_update(inst
->term
);
1023 term_blink(inst
->term
, 0);
1027 void pty_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
1029 struct gui_data
*inst
= (struct gui_data
*)data
;
1033 ret
= read(sourcefd
, buf
, sizeof(buf
));
1036 * Clean termination condition is that either ret == 0, or ret
1037 * < 0 and errno == EIO. Not sure why the latter, but it seems
1040 if (ret
== 0 || (ret
< 0 && errno
== EIO
)) {
1041 done_with_pty(inst
);
1042 } else if (ret
< 0) {
1043 perror("read pty master");
1046 from_backend(inst
->term
, 0, buf
, ret
);
1047 term_blink(inst
->term
, 1);
1048 term_out(inst
->term
);
1051 void destroy(GtkWidget
*widget
, gpointer data
)
1056 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
1058 struct gui_data
*inst
= (struct gui_data
*)data
;
1059 inst
->term
->has_focus
= event
->in
;
1060 term_out(inst
->term
);
1061 term_update(inst
->term
);
1062 show_mouseptr(inst
, 1);
1067 * set or clear the "raw mouse message" mode
1069 void set_raw_mouse_mode(void *frontend
, int activate
)
1071 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1072 activate
= activate
&& !cfg
.no_mouse_rep
;
1073 send_raw_mouse
= activate
;
1075 inst
->currcursor
= inst
->rawcursor
;
1077 inst
->currcursor
= inst
->textcursor
;
1078 show_mouseptr(inst
, inst
->mouseptr_visible
);
1081 void request_resize(void *frontend
, int w
, int h
)
1083 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1084 int large_x
, large_y
;
1085 int offset_x
, offset_y
;
1087 GtkRequisition inner
, outer
;
1090 * This is a heinous hack dreamed up by the gnome-terminal
1091 * people to get around a limitation in gtk. The problem is
1092 * that in order to set the size correctly we really need to be
1093 * calling gtk_window_resize - but that needs to know the size
1094 * of the _whole window_, not the drawing area. So what we do
1095 * is to set an artificially huge size request on the drawing
1096 * area, recompute the resulting size request on the window,
1097 * and look at the difference between the two. That gives us
1098 * the x and y offsets we need to translate drawing area size
1099 * into window size for real, and then we call
1100 * gtk_window_resize.
1104 * We start by retrieving the current size of the whole window.
1105 * Adding a bit to _that_ will give us a value we can use as a
1106 * bogus size request which guarantees to be bigger than the
1107 * current size of the drawing area.
1109 get_window_pixels(inst
, &large_x
, &large_y
);
1113 #if GTK_CHECK_VERSION(2,0,0)
1114 gtk_widget_set_size_request(inst
->area
, large_x
, large_y
);
1116 gtk_widget_set_usize(inst
->area
, large_x
, large_y
);
1118 gtk_widget_size_request(inst
->area
, &inner
);
1119 gtk_widget_size_request(inst
->window
, &outer
);
1121 offset_x
= outer
.width
- inner
.width
;
1122 offset_y
= outer
.height
- inner
.height
;
1124 area_x
= inst
->font_width
* w
+ 2*cfg
.window_border
;
1125 area_y
= inst
->font_height
* h
+ 2*cfg
.window_border
;
1128 * Now we must set the size request on the drawing area back to
1129 * something sensible before we commit the real resize. Best
1130 * way to do this, I think, is to set it to what the size is
1131 * really going to end up being.
1133 #if GTK_CHECK_VERSION(2,0,0)
1134 gtk_widget_set_size_request(inst
->area
, area_x
, area_y
);
1136 gtk_widget_set_usize(inst
->area
, area_x
, area_y
);
1139 #if GTK_CHECK_VERSION(2,0,0)
1140 gtk_window_resize(GTK_WINDOW(inst
->window
),
1141 area_x
+ offset_x
, area_y
+ offset_y
);
1143 gdk_window_resize(inst
->window
->window
,
1144 area_x
+ offset_x
, area_y
+ offset_y
);
1148 static void real_palette_set(struct gui_data
*inst
, int n
, int r
, int g
, int b
)
1150 gboolean success
[1];
1152 inst
->cols
[n
].red
= r
* 0x0101;
1153 inst
->cols
[n
].green
= g
* 0x0101;
1154 inst
->cols
[n
].blue
= b
* 0x0101;
1156 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
+ n
, 1,
1157 FALSE
, FALSE
, success
);
1159 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1163 void set_window_background(struct gui_data
*inst
)
1165 if (inst
->area
&& inst
->area
->window
)
1166 gdk_window_set_background(inst
->area
->window
, &inst
->cols
[18]);
1167 if (inst
->window
&& inst
->window
->window
)
1168 gdk_window_set_background(inst
->window
->window
, &inst
->cols
[18]);
1171 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
1173 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1174 static const int first
[21] = {
1175 0, 2, 4, 6, 8, 10, 12, 14,
1176 1, 3, 5, 7, 9, 11, 13, 15,
1179 real_palette_set(inst
, first
[n
], r
, g
, b
);
1181 real_palette_set(inst
, first
[n
] + 1, r
, g
, b
);
1183 set_window_background(inst
);
1186 void palette_reset(void *frontend
)
1188 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1189 /* This maps colour indices in cfg to those used in inst->cols. */
1190 static const int ww
[] = {
1191 6, 7, 8, 9, 10, 11, 12, 13,
1192 14, 15, 16, 17, 18, 19, 20, 21,
1195 gboolean success
[NCOLOURS
];
1198 assert(lenof(ww
) == NCOLOURS
);
1200 if (!inst
->colmap
) {
1201 inst
->colmap
= gdk_colormap_get_system();
1203 gdk_colormap_free_colors(inst
->colmap
, inst
->cols
, NCOLOURS
);
1206 for (i
= 0; i
< NCOLOURS
; i
++) {
1207 inst
->cols
[i
].red
= cfg
.colours
[ww
[i
]][0] * 0x0101;
1208 inst
->cols
[i
].green
= cfg
.colours
[ww
[i
]][1] * 0x0101;
1209 inst
->cols
[i
].blue
= cfg
.colours
[ww
[i
]][2] * 0x0101;
1212 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
1213 FALSE
, FALSE
, success
);
1214 for (i
= 0; i
< NCOLOURS
; i
++) {
1216 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
1217 i
, cfg
.colours
[i
][0], cfg
.colours
[i
][1], cfg
.colours
[i
][2]);
1220 set_window_background(inst
);
1223 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
1225 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1226 if (inst
->pasteout_data
)
1227 sfree(inst
->pasteout_data
);
1228 if (inst
->pasteout_data_utf8
)
1229 sfree(inst
->pasteout_data_utf8
);
1232 * Set up UTF-8 paste data. This only happens if we aren't in
1233 * direct-to-font mode using the D800 hack.
1235 if (!inst
->direct_to_font
) {
1236 wchar_t *tmp
= data
;
1239 inst
->pasteout_data_utf8
= smalloc(len
*6);
1240 inst
->pasteout_data_utf8_len
= len
*6;
1241 inst
->pasteout_data_utf8_len
=
1242 charset_from_unicode(&tmp
, &tmplen
, inst
->pasteout_data_utf8
,
1243 inst
->pasteout_data_utf8_len
,
1244 CS_UTF8
, NULL
, NULL
, 0);
1245 if (inst
->pasteout_data_utf8_len
== 0) {
1246 sfree(inst
->pasteout_data_utf8
);
1247 inst
->pasteout_data_utf8
= NULL
;
1249 inst
->pasteout_data_utf8
=
1250 srealloc(inst
->pasteout_data_utf8
,
1251 inst
->pasteout_data_utf8_len
);
1254 inst
->pasteout_data_utf8
= NULL
;
1255 inst
->pasteout_data_utf8_len
= 0;
1258 inst
->pasteout_data
= smalloc(len
*6);
1259 inst
->pasteout_data_len
= len
*6;
1260 inst
->pasteout_data_len
= wc_to_mb(line_codepage
, 0, data
, len
,
1261 inst
->pasteout_data
,
1262 inst
->pasteout_data_len
, NULL
, NULL
);
1263 if (inst
->pasteout_data_len
== 0) {
1264 sfree(inst
->pasteout_data
);
1265 inst
->pasteout_data
= NULL
;
1267 inst
->pasteout_data
=
1268 srealloc(inst
->pasteout_data
, inst
->pasteout_data_len
);
1271 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
1272 GDK_CURRENT_TIME
)) {
1273 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1274 GDK_SELECTION_TYPE_STRING
, 1);
1275 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1276 inst
->compound_text_atom
, 1);
1277 if (inst
->pasteout_data_utf8
)
1278 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
1279 inst
->utf8_string_atom
, 1);
1283 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1284 guint info
, guint time_stamp
, gpointer data
)
1286 struct gui_data
*inst
= (struct gui_data
*)data
;
1287 if (seldata
->target
== inst
->utf8_string_atom
)
1288 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1289 inst
->pasteout_data_utf8
,
1290 inst
->pasteout_data_utf8_len
);
1292 gtk_selection_data_set(seldata
, seldata
->target
, 8,
1293 inst
->pasteout_data
, inst
->pasteout_data_len
);
1296 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
1299 struct gui_data
*inst
= (struct gui_data
*)data
;
1300 term_deselect(inst
->term
);
1301 if (inst
->pasteout_data
)
1302 sfree(inst
->pasteout_data
);
1303 if (inst
->pasteout_data_utf8
)
1304 sfree(inst
->pasteout_data_utf8
);
1305 inst
->pasteout_data
= NULL
;
1306 inst
->pasteout_data_len
= 0;
1307 inst
->pasteout_data_utf8
= NULL
;
1308 inst
->pasteout_data_utf8_len
= 0;
1312 void request_paste(void *frontend
)
1314 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1316 * In Unix, pasting is asynchronous: all we can do at the
1317 * moment is to call gtk_selection_convert(), and when the data
1318 * comes back _then_ we can call term_do_paste().
1321 if (!inst
->direct_to_font
) {
1323 * First we attempt to retrieve the selection as a UTF-8
1324 * string (which we will convert to the correct code page
1325 * before sending to the session, of course). If that
1326 * fails, selection_received() will be informed and will
1327 * fall back to an ordinary string.
1329 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1330 inst
->utf8_string_atom
, GDK_CURRENT_TIME
);
1333 * If we're in direct-to-font mode, we disable UTF-8
1334 * pasting, and go straight to ordinary string data.
1336 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1337 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1341 gint
idle_paste_func(gpointer data
); /* forward ref */
1343 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
1344 guint time
, gpointer data
)
1346 struct gui_data
*inst
= (struct gui_data
*)data
;
1348 if (seldata
->target
== inst
->utf8_string_atom
&& seldata
->length
<= 0) {
1350 * Failed to get a UTF-8 selection string. Try an ordinary
1353 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
1354 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
1359 * Any other failure should just go foom.
1361 if (seldata
->length
<= 0 ||
1362 (seldata
->type
!= GDK_SELECTION_TYPE_STRING
&&
1363 seldata
->type
!= inst
->utf8_string_atom
))
1364 return; /* Nothing happens. */
1366 if (inst
->pastein_data
)
1367 sfree(inst
->pastein_data
);
1369 inst
->pastein_data
= smalloc(seldata
->length
* sizeof(wchar_t));
1370 inst
->pastein_data_len
= seldata
->length
;
1371 inst
->pastein_data_len
=
1372 mb_to_wc((seldata
->type
== inst
->utf8_string_atom ?
1373 CS_UTF8
: line_codepage
),
1374 0, seldata
->data
, seldata
->length
,
1375 inst
->pastein_data
, inst
->pastein_data_len
);
1377 term_do_paste(inst
->term
);
1379 if (term_paste_pending(inst
->term
))
1380 inst
->term_paste_idle_id
= gtk_idle_add(idle_paste_func
, inst
);
1383 gint
idle_paste_func(gpointer data
)
1385 struct gui_data
*inst
= (struct gui_data
*)data
;
1387 if (term_paste_pending(inst
->term
))
1388 term_paste(inst
->term
);
1390 gtk_idle_remove(inst
->term_paste_idle_id
);
1396 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
1398 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1401 *p
= inst
->pastein_data
;
1402 *len
= inst
->pastein_data_len
;
1406 void set_title(void *frontend
, char *title
)
1408 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1409 strncpy(inst
->wintitle
, title
, lenof(inst
->wintitle
));
1410 inst
->wintitle
[lenof(inst
->wintitle
)-1] = '\0';
1411 gtk_window_set_title(GTK_WINDOW(inst
->window
), inst
->wintitle
);
1414 void set_icon(void *frontend
, char *title
)
1416 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1417 strncpy(inst
->icontitle
, title
, lenof(inst
->icontitle
));
1418 inst
->icontitle
[lenof(inst
->icontitle
)-1] = '\0';
1419 gdk_window_set_icon_name(inst
->window
->window
, inst
->icontitle
);
1422 void set_sbar(void *frontend
, int total
, int start
, int page
)
1424 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1427 inst
->sbar_adjust
->lower
= 0;
1428 inst
->sbar_adjust
->upper
= total
;
1429 inst
->sbar_adjust
->value
= start
;
1430 inst
->sbar_adjust
->page_size
= page
;
1431 inst
->sbar_adjust
->step_increment
= 1;
1432 inst
->sbar_adjust
->page_increment
= page
/2;
1433 inst
->ignore_sbar
= TRUE
;
1434 gtk_adjustment_changed(inst
->sbar_adjust
);
1435 inst
->ignore_sbar
= FALSE
;
1438 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
1440 struct gui_data
*inst
= (struct gui_data
*)data
;
1444 if (!inst
->ignore_sbar
)
1445 term_scroll(inst
->term
, 1, (int)adj
->value
);
1448 void sys_cursor(void *frontend
, int x
, int y
)
1451 * This is meaningless under X.
1456 * This is still called when mode==BELL_VISUAL, even though the
1457 * visual bell is handled entirely within terminal.c, because we
1458 * may want to perform additional actions on any kind of bell (for
1459 * example, taskbar flashing in Windows).
1461 void beep(void *frontend
, int mode
)
1463 if (mode
!= BELL_VISUAL
)
1467 int char_width(Context ctx
, int uc
)
1470 * Under X, any fixed-width font really _is_ fixed-width.
1471 * Double-width characters will be dealt with using a separate
1472 * font. For the moment we can simply return 1.
1477 Context
get_ctx(void *frontend
)
1479 struct gui_data
*inst
= (struct gui_data
*)frontend
;
1480 struct draw_ctx
*dctx
;
1482 if (!inst
->area
->window
)
1485 dctx
= smalloc(sizeof(*dctx
));
1487 dctx
->gc
= gdk_gc_new(inst
->area
->window
);
1491 void free_ctx(Context ctx
)
1493 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1494 /* struct gui_data *inst = dctx->inst; */
1495 GdkGC
*gc
= dctx
->gc
;
1501 * Draw a line of text in the window, at given character
1502 * coordinates, in given attributes.
1504 * We are allowed to fiddle with the contents of `text'.
1506 void do_text_internal(Context ctx
, int x
, int y
, char *text
, int len
,
1507 unsigned long attr
, int lattr
)
1509 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1510 struct gui_data
*inst
= dctx
->inst
;
1511 GdkGC
*gc
= dctx
->gc
;
1513 int nfg
, nbg
, t
, fontid
, shadow
, rlen
, widefactor
;
1515 nfg
= 2 * ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
1516 nbg
= 2 * ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
1517 if (attr
& ATTR_REVERSE
) {
1522 if (cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
1524 if (cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
1526 if (attr
& TATTR_ACTCURS
) {
1531 fontid
= shadow
= 0;
1533 if (attr
& ATTR_WIDE
) {
1540 if ((attr
& ATTR_BOLD
) && !cfg
.bold_colour
) {
1541 if (inst
->fonts
[fontid
| 1])
1547 if (lattr
!= LATTR_NORM
) {
1549 if (x
>= inst
->term
->cols
)
1551 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1552 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1560 r
.x
= x
*inst
->font_width
+cfg
.window_border
;
1561 r
.y
= y
*inst
->font_height
+cfg
.window_border
;
1562 r
.width
= rlen
*widefactor
*inst
->font_width
;
1563 r
.height
= inst
->font_height
;
1564 gdk_gc_set_clip_rectangle(gc
, &r
);
1567 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
1568 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
1569 x
*inst
->font_width
+cfg
.window_border
,
1570 y
*inst
->font_height
+cfg
.window_border
,
1571 rlen
*widefactor
*inst
->font_width
, inst
->font_height
);
1573 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
1580 wcs
= smalloc(sizeof(wchar_t) * (len
+1));
1581 for (i
= 0; i
< len
; i
++) {
1582 wcs
[i
] = (wchar_t) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
1585 if (inst
->fonts
[fontid
] == NULL
) {
1587 * The font for this contingency does not exist.
1588 * Typically this means we've been given ATTR_WIDE
1589 * character and have no wide font. So we display
1590 * nothing at all; such is life.
1592 } else if (inst
->fontinfo
[fontid
].is_wide
) {
1593 gwcs
= smalloc(sizeof(GdkWChar
) * (len
+1));
1595 * FIXME: when we have a wide-char equivalent of
1596 * from_unicode, use it instead of this.
1598 for (i
= 0; i
<= len
; i
++)
1600 gdk_draw_text_wc(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1601 x
*inst
->font_width
+cfg
.window_border
,
1602 y
*inst
->font_height
+cfg
.window_border
+inst
->fonts
[0]->ascent
,
1606 gcs
= smalloc(sizeof(GdkWChar
) * (len
+1));
1607 wc_to_mb(inst
->fontinfo
[fontid
].charset
, 0,
1608 wcs
, len
, gcs
, len
, ".", NULL
);
1609 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1610 x
*inst
->font_width
+cfg
.window_border
,
1611 y
*inst
->font_height
+cfg
.window_border
+inst
->fonts
[0]->ascent
,
1619 gdk_draw_text(inst
->pixmap
, inst
->fonts
[fontid
], gc
,
1620 x
*inst
->font_width
+cfg
.window_border
+ cfg
.shadowboldoffset
,
1621 y
*inst
->font_height
+cfg
.window_border
+inst
->fonts
[0]->ascent
,
1625 if (attr
& ATTR_UNDER
) {
1626 int uheight
= inst
->fonts
[0]->ascent
+ 1;
1627 if (uheight
>= inst
->font_height
)
1628 uheight
= inst
->font_height
- 1;
1629 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+cfg
.window_border
,
1630 y
*inst
->font_height
+ uheight
+ cfg
.window_border
,
1631 (x
+len
)*widefactor
*inst
->font_width
-1+cfg
.window_border
,
1632 y
*inst
->font_height
+ uheight
+ cfg
.window_border
);
1635 if (lattr
!= LATTR_NORM
) {
1637 * I can't find any plausible StretchBlt equivalent in the
1638 * X server, so I'm going to do this the slow and painful
1639 * way. This will involve repeated calls to
1640 * gdk_draw_pixmap() to stretch the text horizontally. It's
1641 * O(N^2) in time and O(N) in network bandwidth, but you
1642 * try thinking of a better way. :-(
1645 for (i
= 0; i
< len
* widefactor
* inst
->font_width
; i
++) {
1646 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1647 x
*inst
->font_width
+cfg
.window_border
+ 2*i
,
1648 y
*inst
->font_height
+cfg
.window_border
,
1649 x
*inst
->font_width
+cfg
.window_border
+ 2*i
+1,
1650 y
*inst
->font_height
+cfg
.window_border
,
1651 len
* inst
->font_width
- i
, inst
->font_height
);
1654 if (lattr
!= LATTR_WIDE
) {
1656 /* Now stretch vertically, in the same way. */
1657 if (lattr
== LATTR_BOT
)
1661 for (i
= 0; i
< inst
->font_height
; i
+=2) {
1662 gdk_draw_pixmap(inst
->pixmap
, gc
, inst
->pixmap
,
1663 x
*inst
->font_width
+cfg
.window_border
,
1664 y
*inst
->font_height
+cfg
.window_border
+dt
*i
+db
,
1665 x
*widefactor
*inst
->font_width
+cfg
.window_border
,
1666 y
*inst
->font_height
+cfg
.window_border
+dt
*(i
+1),
1667 len
* inst
->font_width
, inst
->font_height
-i
-1);
1673 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
1674 unsigned long attr
, int lattr
)
1676 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1677 struct gui_data
*inst
= dctx
->inst
;
1678 GdkGC
*gc
= dctx
->gc
;
1681 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1683 if (attr
& ATTR_WIDE
) {
1689 if (lattr
!= LATTR_NORM
) {
1691 if (x
>= inst
->term
->cols
)
1693 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1694 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1698 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1699 x
*inst
->font_width
+cfg
.window_border
,
1700 y
*inst
->font_height
+cfg
.window_border
,
1701 x
*inst
->font_width
+cfg
.window_border
,
1702 y
*inst
->font_height
+cfg
.window_border
,
1703 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1706 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
1707 unsigned long attr
, int lattr
)
1709 struct draw_ctx
*dctx
= (struct draw_ctx
*)ctx
;
1710 struct gui_data
*inst
= dctx
->inst
;
1711 GdkGC
*gc
= dctx
->gc
;
1713 int passive
, widefactor
;
1715 if (attr
& TATTR_PASCURS
) {
1716 attr
&= ~TATTR_PASCURS
;
1720 if ((attr
& TATTR_ACTCURS
) && cfg
.cursor_type
!= 0) {
1721 attr
&= ~TATTR_ACTCURS
;
1723 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
1725 if (attr
& ATTR_WIDE
) {
1731 if (lattr
!= LATTR_NORM
) {
1733 if (x
>= inst
->term
->cols
)
1735 if (x
+ len
*2*widefactor
> inst
->term
->cols
)
1736 len
= (inst
->term
->cols
-x
)/2/widefactor
;/* trim to LH half */
1740 if (cfg
.cursor_type
== 0) {
1742 * An active block cursor will already have been done by
1743 * the above do_text call, so we only need to do anything
1747 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1748 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
1749 x
*inst
->font_width
+cfg
.window_border
,
1750 y
*inst
->font_height
+cfg
.window_border
,
1751 len
*inst
->font_width
-1, inst
->font_height
-1);
1755 int startx
, starty
, dx
, dy
, length
, i
;
1759 if ((attr
& ATTR_WIDE
) || lattr
!= LATTR_NORM
)
1760 char_width
= 2*inst
->font_width
;
1762 char_width
= inst
->font_width
;
1764 if (cfg
.cursor_type
== 1) {
1765 uheight
= inst
->fonts
[0]->ascent
+ 1;
1766 if (uheight
>= inst
->font_height
)
1767 uheight
= inst
->font_height
- 1;
1769 startx
= x
* inst
->font_width
+ cfg
.window_border
;
1770 starty
= y
* inst
->font_height
+ cfg
.window_border
+ uheight
;
1773 length
= len
* char_width
;
1776 if (attr
& TATTR_RIGHTCURS
)
1777 xadjust
= char_width
- 1;
1778 startx
= x
* inst
->font_width
+ cfg
.window_border
+ xadjust
;
1779 starty
= y
* inst
->font_height
+ cfg
.window_border
;
1782 length
= inst
->font_height
;
1785 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
1787 for (i
= 0; i
< length
; i
++) {
1789 gdk_draw_point(inst
->pixmap
, gc
, startx
, starty
);
1795 gdk_draw_line(inst
->pixmap
, gc
, startx
, starty
,
1796 startx
+ (length
-1) * dx
, starty
+ (length
-1) * dy
);
1800 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
1801 x
*inst
->font_width
+cfg
.window_border
,
1802 y
*inst
->font_height
+cfg
.window_border
,
1803 x
*inst
->font_width
+cfg
.window_border
,
1804 y
*inst
->font_height
+cfg
.window_border
,
1805 len
*widefactor
*inst
->font_width
, inst
->font_height
);
1808 GdkCursor
*make_mouse_ptr(struct gui_data
*inst
, int cursor_val
)
1811 * Truly hideous hack: GTK doesn't allow us to set the mouse
1812 * cursor foreground and background colours unless we've _also_
1813 * created our own cursor from bitmaps. Therefore, I need to
1814 * load the `cursor' font and draw glyphs from it on to
1815 * pixmaps, in order to construct my cursors with the fg and bg
1816 * I want. This is a gross hack, but it's more self-contained
1817 * than linking in Xlib to find the X window handle to
1818 * inst->area and calling XRecolorCursor, and it's more
1819 * futureproof than hard-coding the shapes as bitmap arrays.
1821 static GdkFont
*cursor_font
= NULL
;
1822 GdkPixmap
*source
, *mask
;
1824 GdkColor cfg
= { 0, 65535, 65535, 65535 };
1825 GdkColor cbg
= { 0, 0, 0, 0 };
1826 GdkColor dfg
= { 1, 65535, 65535, 65535 };
1827 GdkColor dbg
= { 0, 0, 0, 0 };
1830 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
1832 if (cursor_val
== -2) {
1833 gdk_font_unref(cursor_font
);
1837 if (cursor_val
>= 0 && !cursor_font
)
1838 cursor_font
= gdk_font_load("cursor");
1841 * Get the text extent of the cursor in question. We use the
1842 * mask character for this, because it's typically slightly
1843 * bigger than the main character.
1845 if (cursor_val
>= 0) {
1847 text
[0] = (char)cursor_val
+ 1;
1848 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
1849 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
1855 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
1856 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
1859 * Draw the mask character on the mask pixmap.
1861 gc
= gdk_gc_new(mask
);
1862 gdk_gc_set_foreground(gc
, &dbg
);
1863 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
1864 if (cursor_val
>= 0) {
1866 text
[0] = (char)cursor_val
+ 1;
1867 gdk_gc_set_foreground(gc
, &dfg
);
1868 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
1873 * Draw the main character on the source pixmap.
1875 gc
= gdk_gc_new(source
);
1876 gdk_gc_set_foreground(gc
, &dbg
);
1877 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
1878 if (cursor_val
>= 0) {
1880 text
[0] = (char)cursor_val
;
1881 gdk_gc_set_foreground(gc
, &dfg
);
1882 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
1887 * Create the cursor.
1889 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
1894 gdk_pixmap_unref(source
);
1895 gdk_pixmap_unref(mask
);
1900 void modalfatalbox(char *p
, ...)
1903 fprintf(stderr
, "FATAL ERROR: ");
1905 vfprintf(stderr
, p
, ap
);
1907 fputc('\n', stderr
);
1911 char *get_x_display(void *frontend
)
1913 return gdk_get_display();
1916 static void help(FILE *fp
) {
1918 "pterm option summary:\n"
1920 " --display DISPLAY Specify X display to use (note '--')\n"
1921 " -name PREFIX Prefix when looking up resources (default: pterm)\n"
1922 " -fn FONT Normal text font\n"
1923 " -fb FONT Bold text font\n"
1924 " -geometry WIDTHxHEIGHT Size of terminal in characters\n"
1925 " -sl LINES Number of lines of scrollback\n"
1926 " -fg COLOUR, -bg COLOUR Foreground/background colour\n"
1927 " -bfg COLOUR, -bbg COLOUR Foreground/background bold colour\n"
1928 " -cfg COLOUR, -bfg COLOUR Foreground/background cursor colour\n"
1929 " -T TITLE Window title\n"
1930 " -ut, +ut Do(default) or do not update utmp\n"
1931 " -ls, +ls Do(default) or do not make shell a login shell\n"
1932 " -sb, +sb Do(default) or do not display a scrollbar\n"
1933 " -log PATH Log all output to a file\n"
1934 " -nethack Map numeric keypad to hjklyubn direction keys\n"
1935 " -xrm RESOURCE-STRING Set an X resource\n"
1936 " -e COMMAND [ARGS...] Execute command (consumes all remaining args)\n"
1937 ) < 0 || fflush(fp
) < 0) {
1938 perror("output error");
1943 int do_cmdline(int argc
, char **argv
, int do_everything
)
1946 extern char **pty_argv
; /* declared in pty.c */
1949 * Macros to make argument handling easier. Note that because
1950 * they need to call `continue', they cannot be contained in
1951 * the usual do {...} while (0) wrapper to make them
1952 * syntactically single statements; hence it is not legal to
1953 * use one of these macros as an unbraced statement between
1956 #define EXPECTS_ARG { \
1957 if (--argc <= 0) { \
1959 fprintf(stderr, "pterm: %s expects an argument\n", p); \
1964 #define SECOND_PASS_ONLY { if (!do_everything) continue; }
1973 while (--argc
> 0) {
1975 if (!strcmp(p
, "-fn") || !strcmp(p
, "-font")) {
1978 strncpy(cfg
.font
, val
, sizeof(cfg
.font
));
1979 cfg
.font
[sizeof(cfg
.font
)-1] = '\0';
1981 } else if (!strcmp(p
, "-fb")) {
1984 strncpy(cfg
.boldfont
, val
, sizeof(cfg
.boldfont
));
1985 cfg
.boldfont
[sizeof(cfg
.boldfont
)-1] = '\0';
1987 } else if (!strcmp(p
, "-fw")) {
1990 strncpy(cfg
.widefont
, val
, sizeof(cfg
.widefont
));
1991 cfg
.widefont
[sizeof(cfg
.widefont
)-1] = '\0';
1993 } else if (!strcmp(p
, "-fwb")) {
1996 strncpy(cfg
.wideboldfont
, val
, sizeof(cfg
.wideboldfont
));
1997 cfg
.wideboldfont
[sizeof(cfg
.wideboldfont
)-1] = '\0';
1999 } else if (!strcmp(p
, "-cs")) {
2002 strncpy(cfg
.line_codepage
, val
, sizeof(cfg
.line_codepage
));
2003 cfg
.line_codepage
[sizeof(cfg
.line_codepage
)-1] = '\0';
2005 } else if (!strcmp(p
, "-geometry")) {
2006 int flags
, x
, y
, w
, h
;
2010 flags
= XParseGeometry(val
, &x
, &y
, &w
, &h
);
2011 if (flags
& WidthValue
)
2013 if (flags
& HeightValue
)
2017 * Apparently setting the initial window position is
2018 * difficult in GTK 1.2. Not entirely sure why this
2019 * should be. 2.0 has gtk_window_parse_geometry(),
2020 * which would help... For the moment, though, I can't
2021 * be bothered with this.
2024 } else if (!strcmp(p
, "-sl")) {
2027 cfg
.savelines
= atoi(val
);
2029 } else if (!strcmp(p
, "-fg") || !strcmp(p
, "-bg") ||
2030 !strcmp(p
, "-bfg") || !strcmp(p
, "-bbg") ||
2031 !strcmp(p
, "-cfg") || !strcmp(p
, "-cbg")) {
2036 if (!gdk_color_parse(val
, &col
)) {
2038 fprintf(stderr
, "pterm: unable to parse colour \"%s\"\n", val
);
2041 index
= (!strcmp(p
, "-fg") ?
0 :
2042 !strcmp(p
, "-bg") ?
2 :
2043 !strcmp(p
, "-bfg") ?
1 :
2044 !strcmp(p
, "-bbg") ?
3 :
2045 !strcmp(p
, "-cfg") ?
4 :
2046 !strcmp(p
, "-cbg") ?
5 : -1);
2047 assert(index
!= -1);
2048 cfg
.colours
[index
][0] = col
.red
/ 256;
2049 cfg
.colours
[index
][1] = col
.green
/ 256;
2050 cfg
.colours
[index
][2] = col
.blue
/ 256;
2053 } else if (!strcmp(p
, "-e")) {
2054 /* This option swallows all further arguments. */
2060 pty_argv
= smalloc((argc
+1) * sizeof(char *));
2062 for (i
= 0; i
< argc
; i
++)
2063 pty_argv
[i
] = argv
[i
];
2064 pty_argv
[argc
] = NULL
;
2065 break; /* finished command-line processing */
2067 err
= 1, fprintf(stderr
, "pterm: -e expects an argument\n");
2069 } else if (!strcmp(p
, "-T")) {
2072 strncpy(cfg
.wintitle
, val
, sizeof(cfg
.wintitle
));
2073 cfg
.wintitle
[sizeof(cfg
.wintitle
)-1] = '\0';
2075 } else if (!strcmp(p
, "-log")) {
2078 strncpy(cfg
.logfilename
, val
, sizeof(cfg
.logfilename
));
2079 cfg
.logfilename
[sizeof(cfg
.logfilename
)-1] = '\0';
2080 cfg
.logtype
= LGTYP_DEBUG
;
2082 } else if (!strcmp(p
, "-ut-") || !strcmp(p
, "+ut")) {
2086 } else if (!strcmp(p
, "-ut")) {
2090 } else if (!strcmp(p
, "-ls-") || !strcmp(p
, "+ls")) {
2092 cfg
.login_shell
= 0;
2094 } else if (!strcmp(p
, "-ls")) {
2096 cfg
.login_shell
= 1;
2098 } else if (!strcmp(p
, "-nethack")) {
2100 cfg
.nethack_keypad
= 1;
2102 } else if (!strcmp(p
, "-sb-") || !strcmp(p
, "+sb")) {
2106 } else if (!strcmp(p
, "-sb")) {
2110 } else if (!strcmp(p
, "-name")) {
2114 } else if (!strcmp(p
, "-xrm")) {
2116 provide_xrm_string(val
);
2118 } else if(!strcmp(p
, "-help") || !strcmp(p
, "--help")) {
2124 fprintf(stderr
, "pterm: unrecognized option '%s'\n", p
);
2131 static void block_signal(int sig
, int block_it
) {
2135 sigaddset(&ss
, sig
);
2136 if(sigprocmask(block_it ? SIG_BLOCK
: SIG_UNBLOCK
, &ss
, 0) < 0) {
2137 perror("sigprocmask");
2143 * This function retrieves the character set encoding of a font. It
2144 * returns the character set without the X11 hack (in case the user
2145 * asks to use the font's own encoding).
2147 static int set_font_info(struct gui_data
*inst
, int fontid
)
2149 GdkFont
*font
= inst
->fonts
[fontid
];
2150 XFontStruct
*xfs
= GDK_FONT_XFONT(font
);
2151 Display
*disp
= GDK_FONT_XDISPLAY(font
);
2152 Atom charset_registry
, charset_encoding
;
2153 unsigned long registry_ret
, encoding_ret
;
2154 int retval
= CS_NONE
;
2156 charset_registry
= XInternAtom(disp
, "CHARSET_REGISTRY", False
);
2157 charset_encoding
= XInternAtom(disp
, "CHARSET_ENCODING", False
);
2158 inst
->fontinfo
[fontid
].charset
= CS_NONE
;
2159 inst
->fontinfo
[fontid
].is_wide
= 0;
2160 if (XGetFontProperty(xfs
, charset_registry
, ®istry_ret
) &&
2161 XGetFontProperty(xfs
, charset_encoding
, &encoding_ret
)) {
2163 reg
= XGetAtomName(disp
, (Atom
)registry_ret
);
2164 enc
= XGetAtomName(disp
, (Atom
)encoding_ret
);
2166 char *encoding
= dupcat(reg
, "-", enc
, NULL
);
2167 retval
= inst
->fontinfo
[fontid
].charset
=
2168 charset_from_xenc(encoding
);
2169 /* FIXME: when libcharset supports wide encodings fix this. */
2170 if (!strcasecmp(encoding
, "iso10646-1")) {
2171 inst
->fontinfo
[fontid
].is_wide
= 1;
2176 * Hack for X line-drawing characters: if the primary
2177 * font is encoded as ISO-8859-anything, and has valid
2178 * glyphs in the first 32 char positions, it is assumed
2179 * that those glyphs are the VT100 line-drawing
2182 * Actually, we'll hack even harder by only checking
2183 * position 0x19 (vertical line, VT100 linedrawing
2184 * `x'). Then we can check it easily by seeing if the
2185 * ascent and descent differ.
2187 if (inst
->fontinfo
[fontid
].charset
== CS_ISO8859_1
) {
2188 int lb
, rb
, wid
, asc
, desc
;
2193 gdk_string_extents(inst
->fonts
[fontid
], text
,
2194 &lb
, &rb
, &wid
, &asc
, &desc
);
2196 inst
->fontinfo
[fontid
].charset
= CS_ISO8859_1_X11
;
2206 int main(int argc
, char **argv
)
2208 extern int pty_master_fd
; /* declared in pty.c */
2209 extern void pty_pre_init(void); /* declared in pty.c */
2210 struct gui_data
*inst
;
2213 /* defer any child exit handling until we're ready to deal with
2215 block_signal(SIGCHLD
, 1);
2219 gtk_init(&argc
, &argv
);
2221 if (do_cmdline(argc
, argv
, 0)) /* pre-defaults pass to get -class */
2223 do_defaults(NULL
, &cfg
);
2224 if (do_cmdline(argc
, argv
, 1)) /* post-defaults, do everything */
2228 * Create an instance structure and initialise to zeroes
2230 inst
= smalloc(sizeof(*inst
));
2231 memset(inst
, 0, sizeof(*inst
));
2232 inst
->alt_keycode
= -1; /* this one needs _not_ to be zero */
2234 inst
->fonts
[0] = gdk_font_load(cfg
.font
);
2235 if (!inst
->fonts
[0]) {
2236 fprintf(stderr
, "pterm: unable to load font \"%s\"\n", cfg
.font
);
2239 font_charset
= set_font_info(inst
, 0);
2240 if (cfg
.boldfont
[0]) {
2241 inst
->fonts
[1] = gdk_font_load(cfg
.boldfont
);
2242 if (!inst
->fonts
[1]) {
2243 fprintf(stderr
, "pterm: unable to load bold font \"%s\"\n",
2247 set_font_info(inst
, 1);
2249 inst
->fonts
[1] = NULL
;
2250 if (cfg
.widefont
[0]) {
2251 inst
->fonts
[2] = gdk_font_load(cfg
.widefont
);
2252 if (!inst
->fonts
[2]) {
2253 fprintf(stderr
, "pterm: unable to load wide font \"%s\"\n",
2257 set_font_info(inst
, 2);
2259 inst
->fonts
[2] = NULL
;
2260 if (cfg
.wideboldfont
[0]) {
2261 inst
->fonts
[3] = gdk_font_load(cfg
.wideboldfont
);
2262 if (!inst
->fonts
[3]) {
2263 fprintf(stderr
, "pterm: unable to load wide/bold font \"%s\"\n",
2267 set_font_info(inst
, 3);
2269 inst
->fonts
[3] = NULL
;
2271 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
2272 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
2274 inst
->compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
2275 inst
->utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
2277 inst
->direct_to_font
= init_ucs(font_charset
);
2279 inst
->window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
2281 if (cfg
.wintitle
[0])
2282 set_title(inst
, cfg
.wintitle
);
2284 set_title(inst
, "pterm");
2287 * Set up the colour map.
2289 palette_reset(inst
);
2291 inst
->area
= gtk_drawing_area_new();
2292 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
2293 inst
->font_width
* cfg
.width
+ 2*cfg
.window_border
,
2294 inst
->font_height
* cfg
.height
+ 2*cfg
.window_border
);
2295 if (cfg
.scrollbar
) {
2296 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0,0,0,0,0,0));
2297 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
2299 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
2300 if (cfg
.scrollbar
) {
2301 if (cfg
.scrollbar_on_left
)
2302 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2304 gtk_box_pack_end(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
2306 gtk_box_pack_start(inst
->hbox
, inst
->area
, TRUE
, TRUE
, 0);
2308 gtk_container_add(GTK_CONTAINER(inst
->window
), GTK_WIDGET(inst
->hbox
));
2312 geom
.min_width
= inst
->font_width
+ 2*cfg
.window_border
;
2313 geom
.min_height
= inst
->font_height
+ 2*cfg
.window_border
;
2314 geom
.max_width
= geom
.max_height
= -1;
2315 geom
.base_width
= 2*cfg
.window_border
;
2316 geom
.base_height
= 2*cfg
.window_border
;
2317 geom
.width_inc
= inst
->font_width
;
2318 geom
.height_inc
= inst
->font_height
;
2319 geom
.min_aspect
= geom
.max_aspect
= 0;
2320 gtk_window_set_geometry_hints(GTK_WINDOW(inst
->window
), inst
->area
, &geom
,
2321 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
2322 GDK_HINT_RESIZE_INC
);
2325 gtk_signal_connect(GTK_OBJECT(inst
->window
), "destroy",
2326 GTK_SIGNAL_FUNC(destroy
), inst
);
2327 gtk_signal_connect(GTK_OBJECT(inst
->window
), "delete_event",
2328 GTK_SIGNAL_FUNC(delete_window
), inst
);
2329 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_press_event",
2330 GTK_SIGNAL_FUNC(key_event
), inst
);
2331 gtk_signal_connect(GTK_OBJECT(inst
->window
), "key_release_event",
2332 GTK_SIGNAL_FUNC(key_event
), inst
);
2333 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_in_event",
2334 GTK_SIGNAL_FUNC(focus_event
), inst
);
2335 gtk_signal_connect(GTK_OBJECT(inst
->window
), "focus_out_event",
2336 GTK_SIGNAL_FUNC(focus_event
), inst
);
2337 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
2338 GTK_SIGNAL_FUNC(configure_area
), inst
);
2339 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
2340 GTK_SIGNAL_FUNC(expose_area
), inst
);
2341 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
2342 GTK_SIGNAL_FUNC(button_event
), inst
);
2343 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
2344 GTK_SIGNAL_FUNC(button_event
), inst
);
2345 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
2346 GTK_SIGNAL_FUNC(motion_event
), inst
);
2347 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
2348 GTK_SIGNAL_FUNC(selection_received
), inst
);
2349 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
2350 GTK_SIGNAL_FUNC(selection_get
), inst
);
2351 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
2352 GTK_SIGNAL_FUNC(selection_clear
), inst
);
2354 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
2355 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
2356 gtk_timeout_add(20, timer_func
, inst
);
2357 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
2358 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
2359 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
2360 GDK_POINTER_MOTION_MASK
| GDK_BUTTON_MOTION_MASK
);
2362 gtk_widget_show(inst
->area
);
2364 gtk_widget_show(inst
->sbar
);
2365 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
2366 gtk_widget_show(inst
->window
);
2368 set_window_background(inst
);
2370 inst
->textcursor
= make_mouse_ptr(inst
, GDK_XTERM
);
2371 inst
->rawcursor
= make_mouse_ptr(inst
, GDK_LEFT_PTR
);
2372 inst
->blankcursor
= make_mouse_ptr(inst
, -1);
2373 make_mouse_ptr(inst
, -2); /* clean up cursor font */
2374 inst
->currcursor
= inst
->textcursor
;
2375 show_mouseptr(inst
, 1);
2377 inst
->term
= term_init(&cfg
, inst
);
2378 inst
->logctx
= log_init(inst
);
2379 term_provide_logctx(inst
->term
, inst
->logctx
);
2381 inst
->back
= &pty_backend
;
2382 inst
->back
->init((void *)inst
->term
, &inst
->backhandle
, NULL
, 0, NULL
, 0);
2383 inst
->back
->provide_logctx(inst
->backhandle
, inst
->logctx
);
2385 term_provide_resize_fn(inst
->term
, inst
->back
->size
, inst
->backhandle
);
2387 term_size(inst
->term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2390 ldisc_create(&cfg
, inst
->term
, inst
->back
, inst
->backhandle
, inst
);
2391 ldisc_send(inst
->ldisc
, NULL
, 0, 0);/* cause ldisc to notice changes */
2393 inst
->master_fd
= pty_master_fd
;
2394 inst
->exited
= FALSE
;
2395 inst
->master_func_id
= gdk_input_add(pty_master_fd
, GDK_INPUT_READ
,
2396 pty_input_func
, inst
);
2398 /* now we're reday to deal with the child exit handler being
2400 block_signal(SIGCHLD
, 0);