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.
15 #include <gdk/gdkkeysyms.h>
17 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
20 #define CAT2(x,y) x ## y
21 #define CAT(x,y) CAT2(x,y)
22 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
24 #define NCOLOURS (lenof(((Config *)0)->colours))
27 GtkWidget
*area
, *sbar
;
29 GtkAdjustment
*sbar_adjust
;
31 GdkFont
*fonts
[2]; /* normal and bold (for now!) */
32 GdkCursor
*rawcursor
, *textcursor
;
33 GdkColor cols
[NCOLOURS
];
35 wchar_t *pastein_data
;
38 int pasteout_data_len
;
39 int font_width
, font_height
;
42 static struct gui_data the_inst
;
43 static struct gui_data
*inst
= &the_inst
; /* so we always write `inst->' */
44 static int send_raw_mouse
;
46 void ldisc_update(int echo
, int edit
)
49 * This is a stub in pterm. If I ever produce a Unix
50 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
51 * then it will require some termios manoeuvring analogous to
52 * that in the Windows plink.c, but here it's meaningless.
56 int askappend(char *filename
)
59 * FIXME: for the moment we just wipe the log file. Since I
60 * haven't yet enabled logging, this shouldn't matter yet!
65 void logevent(char *string
)
68 * FIXME: event log entries are currently ignored.
73 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
74 * into a cooked one (SELECT, EXTEND, PASTE).
76 * In Unix, this is not configurable; the X button arrangement is
77 * rock-solid across all applications, everyone has a three-button
78 * mouse or a means of faking it, and there is no need to switch
79 * buttons around at all.
81 Mouse_Button
translate_button(Mouse_Button button
)
83 if (button
== MBT_LEFT
)
85 if (button
== MBT_MIDDLE
)
87 if (button
== MBT_RIGHT
)
89 return 0; /* shouldn't happen */
93 * Minimise or restore the window in response to a server-side
96 void set_iconic(int iconic
)
98 /* FIXME: currently ignored */
102 * Move the window in response to a server-side request.
104 void move_window(int x
, int y
)
106 /* FIXME: currently ignored */
110 * Move the window to the top or bottom of the z-order in response
111 * to a server-side request.
113 void set_zorder(int top
)
115 /* FIXME: currently ignored */
119 * Refresh the window in response to a server-side request.
121 void refresh_window(void)
123 /* FIXME: currently ignored */
127 * Maximise or restore the window in response to a server-side
130 void set_zoomed(int zoomed
)
132 /* FIXME: currently ignored */
136 * Report whether the window is iconic, for terminal reports.
140 return 0; /* FIXME */
144 * Report the window's position, for terminal reports.
146 void get_window_pos(int *x
, int *y
)
148 *x
= 3; *y
= 4; /* FIXME */
152 * Report the window's pixel size, for terminal reports.
154 void get_window_pixels(int *x
, int *y
)
156 *x
= 1; *y
= 2; /* FIXME */
160 * Return the window or icon title.
162 char *get_window_title(int icon
)
164 return "FIXME: window title retrieval not yet implemented";
167 gint
delete_window(GtkWidget
*widget
, GdkEvent
*event
, gpointer data
)
170 * FIXME: warn on close?
175 gint
configure_area(GtkWidget
*widget
, GdkEventConfigure
*event
, gpointer data
)
177 struct gui_data
*inst
= (struct gui_data
*)data
;
180 gdk_pixmap_unref(inst
->pixmap
);
182 inst
->pixmap
= gdk_pixmap_new(widget
->window
,
183 (cfg
.width
* inst
->font_width
+
184 2*cfg
.window_border
),
185 (cfg
.height
* inst
->font_height
+
186 2*cfg
.window_border
), -1);
190 gc
= gdk_gc_new(inst
->area
->window
);
191 gdk_gc_set_foreground(gc
, &inst
->cols
[18]); /* default background */
192 gdk_draw_rectangle(inst
->pixmap
, gc
, 1, 0, 0,
193 cfg
.width
* inst
->font_width
+ 2*cfg
.window_border
,
194 cfg
.height
* inst
->font_height
+ 2*cfg
.window_border
);
199 * Set up the colour map.
201 inst
->colmap
= gdk_colormap_get_system();
203 static const int ww
[] = {
204 6, 7, 8, 9, 10, 11, 12, 13,
205 14, 15, 16, 17, 18, 19, 20, 21,
208 gboolean success
[NCOLOURS
];
211 assert(lenof(ww
) == NCOLOURS
);
213 for (i
= 0; i
< NCOLOURS
; i
++) {
214 inst
->cols
[i
].red
= cfg
.colours
[ww
[i
]][0] * 0x0101;
215 inst
->cols
[i
].green
= cfg
.colours
[ww
[i
]][1] * 0x0101;
216 inst
->cols
[i
].blue
= cfg
.colours
[ww
[i
]][2] * 0x0101;
219 gdk_colormap_alloc_colors(inst
->colmap
, inst
->cols
, NCOLOURS
,
220 FALSE
, FALSE
, success
);
221 for (i
= 0; i
< NCOLOURS
; i
++) {
223 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
224 i
, cfg
.colours
[i
][0], cfg
.colours
[i
][1], cfg
.colours
[i
][2]);
231 gint
expose_area(GtkWidget
*widget
, GdkEventExpose
*event
, gpointer data
)
233 /* struct gui_data *inst = (struct gui_data *)data; */
236 * Pass the exposed rectangle to terminal.c, which will call us
237 * back to do the actual painting.
240 gdk_draw_pixmap(widget
->window
,
241 widget
->style
->fg_gc
[GTK_WIDGET_STATE(widget
)],
243 event
->area
.x
, event
->area
.y
,
244 event
->area
.x
, event
->area
.y
,
245 event
->area
.width
, event
->area
.height
);
250 #define KEY_PRESSED(k) \
251 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
253 gint
key_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
255 /* struct gui_data *inst = (struct gui_data *)data; */
259 if (event
->type
== GDK_KEY_PRESS
) {
263 printf("keypress: keyval = %04x, state = %08x; string =",
264 event
->keyval
, event
->state
);
265 for (i
= 0; event
->string
[i
]; i
++)
266 printf(" %02x", (unsigned char) event
->string
[i
]);
275 * - Compose key (!!! requires Unicode faff before even trying)
279 * Shift-PgUp and Shift-PgDn don't even generate keystrokes
282 if (event
->keyval
== GDK_Page_Up
&& (event
->state
& GDK_SHIFT_MASK
)) {
283 term_scroll(0, -cfg
.height
/2);
286 if (event
->keyval
== GDK_Page_Down
&& (event
->state
& GDK_SHIFT_MASK
)) {
287 term_scroll(0, +cfg
.height
/2);
292 * Neither does Shift-Ins.
294 if (event
->keyval
== GDK_Insert
&& (event
->state
& GDK_SHIFT_MASK
)) {
299 /* ALT+things gives leading Escape. */
301 strncpy(output
+1, event
->string
, 31);
303 end
= strlen(output
);
304 if (event
->state
& GDK_MOD1_MASK
)
309 /* Control-` is the same as Control-\ (unless gtk has a better idea) */
310 if (!event
->string
[0] && event
->keyval
== '`' &&
311 (event
->state
& GDK_CONTROL_MASK
)) {
316 /* Control-Break is the same as Control-C */
317 if (event
->keyval
== GDK_Break
&&
318 (event
->state
& GDK_CONTROL_MASK
)) {
323 /* Control-2, Control-Space and Control-@ are NUL */
324 if (!event
->string
[0] &&
325 (event
->keyval
== ' ' || event
->keyval
== '2' ||
326 event
->keyval
== '@') &&
327 (event
->state
& (GDK_SHIFT_MASK
|
328 GDK_CONTROL_MASK
)) == GDK_CONTROL_MASK
) {
333 /* Control-Shift-Space is 160 (ISO8859 nonbreaking space) */
334 if (!event
->string
[0] && event
->keyval
== ' ' &&
335 (event
->state
& (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) ==
336 (GDK_SHIFT_MASK
| GDK_CONTROL_MASK
)) {
341 /* We don't let GTK tell us what Backspace is! We know better. */
342 if (event
->keyval
== GDK_BackSpace
&&
343 !(event
->state
& GDK_SHIFT_MASK
)) {
344 output
[1] = cfg
.bksp_is_delete ?
'\x7F' : '\x08';
348 /* Shift-Tab is ESC [ Z */
349 if (event
->keyval
== GDK_ISO_Left_Tab
||
350 (event
->keyval
== GDK_Tab
&& (event
->state
& GDK_SHIFT_MASK
))) {
351 end
= 1 + sprintf(output
+1, "\033[Z");
355 * Application keypad mode.
357 if (app_keypad_keys
&& !cfg
.no_applic_k
) {
359 switch (event
->keyval
) {
360 case GDK_Num_Lock
: xkey
= 'P'; break;
361 case GDK_KP_Divide
: xkey
= 'Q'; break;
362 case GDK_KP_Multiply
: xkey
= 'R'; break;
363 case GDK_KP_Subtract
: xkey
= 'S'; break;
365 * Keypad + is tricky. It covers a space that would
366 * be taken up on the VT100 by _two_ keys; so we
367 * let Shift select between the two. Worse still,
368 * in xterm function key mode we change which two...
371 if (cfg
.funky_type
== 2) {
372 if (event
->state
& GDK_SHIFT_MASK
)
376 } else if (event
->state
& GDK_SHIFT_MASK
)
381 case GDK_KP_Enter
: xkey
= 'M'; break;
382 case GDK_KP_0
: case GDK_KP_Insert
: xkey
= 'p'; break;
383 case GDK_KP_1
: case GDK_KP_End
: xkey
= 'q'; break;
384 case GDK_KP_2
: case GDK_KP_Down
: xkey
= 'r'; break;
385 case GDK_KP_3
: case GDK_KP_Page_Down
: xkey
= 's'; break;
386 case GDK_KP_4
: case GDK_KP_Left
: xkey
= 't'; break;
387 case GDK_KP_5
: case GDK_KP_Begin
: xkey
= 'u'; break;
388 case GDK_KP_6
: case GDK_KP_Right
: xkey
= 'v'; break;
389 case GDK_KP_7
: case GDK_KP_Home
: xkey
= 'w'; break;
390 case GDK_KP_8
: case GDK_KP_Up
: xkey
= 'x'; break;
391 case GDK_KP_9
: case GDK_KP_Page_Up
: xkey
= 'y'; break;
392 case GDK_KP_Decimal
: case GDK_KP_Delete
: xkey
= 'n'; break;
396 if (xkey
>= 'P' && xkey
<= 'S')
397 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
399 end
= 1 + sprintf(output
+1, "\033?%c", xkey
);
401 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
407 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
408 * for integer decimal nn.)
410 * We also deal with the weird ones here. Linux VCs replace F1
411 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
412 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
417 switch (event
->keyval
) {
419 code
= (event
->state
& GDK_SHIFT_MASK ?
23 : 11);
422 code
= (event
->state
& GDK_SHIFT_MASK ?
24 : 12);
425 code
= (event
->state
& GDK_SHIFT_MASK ?
25 : 13);
428 code
= (event
->state
& GDK_SHIFT_MASK ?
26 : 14);
431 code
= (event
->state
& GDK_SHIFT_MASK ?
28 : 15);
434 code
= (event
->state
& GDK_SHIFT_MASK ?
29 : 17);
437 code
= (event
->state
& GDK_SHIFT_MASK ?
31 : 18);
440 code
= (event
->state
& GDK_SHIFT_MASK ?
32 : 19);
443 code
= (event
->state
& GDK_SHIFT_MASK ?
33 : 20);
446 code
= (event
->state
& GDK_SHIFT_MASK ?
34 : 21);
479 if (!(event
->state
& GDK_CONTROL_MASK
)) switch (event
->keyval
) {
480 case GDK_Home
: case GDK_KP_Home
:
483 case GDK_Insert
: case GDK_KP_Insert
:
486 case GDK_Delete
: case GDK_KP_Delete
:
489 case GDK_End
: case GDK_KP_End
:
492 case GDK_Page_Up
: case GDK_KP_Page_Up
:
495 case GDK_Page_Down
: case GDK_KP_Page_Down
:
499 /* Reorder edit keys to physical order */
500 if (cfg
.funky_type
== 3 && code
<= 6)
501 code
= "\0\2\1\4\5\3\6"[code
];
503 if (vt52_mode
&& code
> 0 && code
<= 6) {
504 end
= 1 + sprintf(output
+1, "\x1B%c", " HLMEIG"[code
]);
508 if (cfg
.funky_type
== 5 && /* SCO function keys */
509 code
>= 11 && code
<= 34) {
510 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
512 switch (event
->keyval
) {
513 case GDK_F1
: index
= 0; break;
514 case GDK_F2
: index
= 1; break;
515 case GDK_F3
: index
= 2; break;
516 case GDK_F4
: index
= 3; break;
517 case GDK_F5
: index
= 4; break;
518 case GDK_F6
: index
= 5; break;
519 case GDK_F7
: index
= 6; break;
520 case GDK_F8
: index
= 7; break;
521 case GDK_F9
: index
= 8; break;
522 case GDK_F10
: index
= 9; break;
523 case GDK_F11
: index
= 10; break;
524 case GDK_F12
: index
= 11; break;
526 if (event
->state
& GDK_SHIFT_MASK
) index
+= 12;
527 if (event
->state
& GDK_CONTROL_MASK
) index
+= 24;
528 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[index
]);
531 if (cfg
.funky_type
== 5 && /* SCO small keypad */
532 code
>= 1 && code
<= 6) {
533 char codes
[] = "HL.FIG";
538 end
= 1 + sprintf(output
+1, "\x1B[%c", codes
[code
-1]);
542 if ((vt52_mode
|| cfg
.funky_type
== 4) && code
>= 11 && code
<= 24) {
549 end
= 1 + sprintf(output
+1,
550 "\x1B%c", code
+ 'P' - 11 - offt
);
552 end
= 1 + sprintf(output
+1,
553 "\x1BO%c", code
+ 'P' - 11 - offt
);
556 if (cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
557 end
= 1 + sprintf(output
+1, "\x1B[[%c", code
+ 'A' - 11);
560 if (cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
562 end
= 1 + sprintf(output
+1, "\x1B%c", code
+ 'P' - 11);
564 end
= 1 + sprintf(output
+1, "\x1BO%c", code
+ 'P' - 11);
567 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
568 end
= 1 + sprintf(output
+1, code
== 1 ?
"\x1B[H" : "\x1BOw");
572 end
= 1 + sprintf(output
+1, "\x1B[%d~", code
);
578 * Cursor keys. (This includes the numberpad cursor keys,
579 * if we haven't already done them due to app keypad mode.)
581 * Here we also process un-numlocked un-appkeypadded KP5,
582 * which sends ESC [ G.
586 switch (event
->keyval
) {
587 case GDK_Up
: case GDK_KP_Up
: xkey
= 'A'; break;
588 case GDK_Down
: case GDK_KP_Down
: xkey
= 'B'; break;
589 case GDK_Right
: case GDK_KP_Right
: xkey
= 'C'; break;
590 case GDK_Left
: case GDK_KP_Left
: xkey
= 'D'; break;
591 case GDK_Begin
: case GDK_KP_Begin
: xkey
= 'G'; break;
595 * The arrow keys normally do ESC [ A and so on. In
596 * app cursor keys mode they do ESC O A instead.
597 * Ctrl toggles the two modes.
600 end
= 1 + sprintf(output
+1, "\033%c", xkey
);
601 } else if (!app_cursor_keys
^
602 !(event
->state
& GDK_CONTROL_MASK
)) {
603 end
= 1 + sprintf(output
+1, "\033O%c", xkey
);
605 end
= 1 + sprintf(output
+1, "\033[%c", xkey
);
616 printf("generating sequence:");
617 for (i
= start
; i
< end
; i
++)
618 printf(" %02x", (unsigned char) output
[i
]);
622 ldisc_send(output
+start
, end
-start
, 1);
629 gint
button_event(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
631 struct gui_data
*inst
= (struct gui_data
*)data
;
632 int shift
, ctrl
, alt
, x
, y
, button
, act
;
634 shift
= event
->state
& GDK_SHIFT_MASK
;
635 ctrl
= event
->state
& GDK_CONTROL_MASK
;
636 alt
= event
->state
& GDK_MOD1_MASK
;
637 if (event
->button
== 1)
639 else if (event
->button
== 2)
641 else if (event
->button
== 3)
644 return FALSE
; /* don't even know what button! */
646 switch (event
->type
) {
647 case GDK_BUTTON_PRESS
: act
= MA_CLICK
; break;
648 case GDK_BUTTON_RELEASE
: act
= MA_RELEASE
; break;
649 case GDK_2BUTTON_PRESS
: act
= MA_2CLK
; break;
650 case GDK_3BUTTON_PRESS
: act
= MA_3CLK
; break;
651 default: return FALSE
; /* don't know this event type */
654 if (send_raw_mouse
&& !(cfg
.mouse_override
&& shift
) &&
655 act
!= MA_CLICK
&& act
!= MA_RELEASE
)
656 return TRUE
; /* we ignore these in raw mouse mode */
658 x
= (event
->x
- cfg
.window_border
) / inst
->font_width
;
659 y
= (event
->y
- cfg
.window_border
) / inst
->font_height
;
661 term_mouse(button
, act
, x
, y
, shift
, ctrl
, alt
);
666 gint
motion_event(GtkWidget
*widget
, GdkEventMotion
*event
, gpointer data
)
668 struct gui_data
*inst
= (struct gui_data
*)data
;
669 int shift
, ctrl
, alt
, x
, y
, button
;
671 shift
= event
->state
& GDK_SHIFT_MASK
;
672 ctrl
= event
->state
& GDK_CONTROL_MASK
;
673 alt
= event
->state
& GDK_MOD1_MASK
;
674 if (event
->state
& GDK_BUTTON1_MASK
)
676 else if (event
->state
& GDK_BUTTON2_MASK
)
678 else if (event
->state
& GDK_BUTTON3_MASK
)
681 return FALSE
; /* don't even know what button! */
683 x
= (event
->x
- cfg
.window_border
) / inst
->font_width
;
684 y
= (event
->y
- cfg
.window_border
) / inst
->font_height
;
686 term_mouse(button
, MA_DRAG
, x
, y
, shift
, ctrl
, alt
);
691 gint
timer_func(gpointer data
)
693 /* struct gui_data *inst = (struct gui_data *)data; */
699 void pty_input_func(gpointer data
, gint sourcefd
, GdkInputCondition condition
)
701 /* struct gui_data *inst = (struct gui_data *)data; */
705 ret
= read(sourcefd
, buf
, sizeof(buf
));
708 * Clean termination condition is that either ret == 0, or ret
709 * < 0 and errno == EIO. Not sure why the latter, but it seems
712 if (ret
== 0 || (ret
< 0 && errno
== EIO
)) {
717 perror("read pty master");
721 from_backend(0, buf
, ret
);
725 void destroy(GtkWidget
*widget
, gpointer data
)
730 gint
focus_event(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
)
732 has_focus
= event
->in
;
739 * set or clear the "raw mouse message" mode
741 void set_raw_mouse_mode(int activate
)
743 activate
= activate
&& !cfg
.no_mouse_rep
;
744 send_raw_mouse
= activate
;
746 gdk_window_set_cursor(inst
->area
->window
, inst
->rawcursor
);
748 gdk_window_set_cursor(inst
->area
->window
, inst
->textcursor
);
751 void request_resize(int w
, int h
)
753 /* FIXME: currently ignored */
756 void palette_set(int n
, int r
, int g
, int b
)
758 /* FIXME: currently ignored */
760 void palette_reset(void)
762 /* FIXME: currently ignored */
765 void write_clip(wchar_t * data
, int len
, int must_deselect
)
767 if (inst
->pasteout_data
)
768 sfree(inst
->pasteout_data
);
769 inst
->pasteout_data
= smalloc(len
);
770 inst
->pasteout_data_len
= len
;
771 wc_to_mb(0, 0, data
, len
, inst
->pasteout_data
, inst
->pasteout_data_len
,
774 if (gtk_selection_owner_set(inst
->area
, GDK_SELECTION_PRIMARY
,
776 gtk_selection_add_target(inst
->area
, GDK_SELECTION_PRIMARY
,
777 GDK_SELECTION_TYPE_STRING
, 1);
781 void selection_get(GtkWidget
*widget
, GtkSelectionData
*seldata
,
782 guint info
, guint time_stamp
, gpointer data
)
784 gtk_selection_data_set(seldata
, GDK_SELECTION_TYPE_STRING
, 8,
785 inst
->pasteout_data
, inst
->pasteout_data_len
);
788 gint
selection_clear(GtkWidget
*widget
, GdkEventSelection
*seldata
,
792 if (inst
->pasteout_data
)
793 sfree(inst
->pasteout_data
);
794 inst
->pasteout_data
= NULL
;
795 inst
->pasteout_data_len
= 0;
799 void request_paste(void)
802 * In Unix, pasting is asynchronous: all we can do at the
803 * moment is to call gtk_selection_convert(), and when the data
804 * comes back _then_ we can call term_do_paste().
806 gtk_selection_convert(inst
->area
, GDK_SELECTION_PRIMARY
,
807 GDK_SELECTION_TYPE_STRING
, GDK_CURRENT_TIME
);
810 void selection_received(GtkWidget
*widget
, GtkSelectionData
*seldata
,
813 if (seldata
->length
<= 0 ||
814 seldata
->type
!= GDK_SELECTION_TYPE_STRING
)
815 return; /* Nothing happens. */
817 if (inst
->pastein_data
)
818 sfree(inst
->pastein_data
);
820 inst
->pastein_data
= smalloc(seldata
->length
* sizeof(wchar_t));
821 inst
->pastein_data_len
= seldata
->length
;
822 mb_to_wc(0, 0, seldata
->data
, seldata
->length
,
823 inst
->pastein_data
, inst
->pastein_data_len
);
828 void get_clip(wchar_t ** p
, int *len
)
831 *p
= inst
->pastein_data
;
832 *len
= inst
->pastein_data_len
;
836 void set_title(char *title
)
838 /* FIXME: currently ignored */
841 void set_icon(char *title
)
843 /* FIXME: currently ignored */
846 void set_sbar(int total
, int start
, int page
)
848 inst
->sbar_adjust
->lower
= 0;
849 inst
->sbar_adjust
->upper
= total
;
850 inst
->sbar_adjust
->value
= start
;
851 inst
->sbar_adjust
->page_size
= page
;
852 inst
->sbar_adjust
->step_increment
= 1;
853 inst
->sbar_adjust
->page_increment
= page
/2;
854 gtk_adjustment_changed(inst
->sbar_adjust
);
857 void scrollbar_moved(GtkAdjustment
*adj
, gpointer data
)
859 term_scroll(1, (int)adj
->value
);
862 void sys_cursor(int x
, int y
)
865 * This is meaningless under X.
874 int CharWidth(Context ctx
, int uc
)
877 * Under X, any fixed-width font really _is_ fixed-width.
878 * Double-width characters will be dealt with using a separate
879 * font. For the moment we can simply return 1.
884 Context
get_ctx(void)
887 if (!inst
->area
->window
)
889 gc
= gdk_gc_new(inst
->area
->window
);
893 void free_ctx(Context ctx
)
895 GdkGC
*gc
= (GdkGC
*)ctx
;
900 * Draw a line of text in the window, at given character
901 * coordinates, in given attributes.
903 * We are allowed to fiddle with the contents of `text'.
905 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
906 unsigned long attr
, int lattr
)
909 GdkGC
*gc
= (GdkGC
*)ctx
;
913 * - ATTR_WIDE (is this for Unicode CJK? I hope so)
914 * - LATTR_* (ESC # 4 double-width and double-height stuff)
915 * - cursor shapes other than block
916 * - VT100 line drawing stuff; code pages in general!
920 nfg
= 2 * ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
921 nbg
= 2 * ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
922 if (attr
& ATTR_REVERSE
) {
927 if (cfg
.bold_colour
&& (attr
& ATTR_BOLD
))
929 if (cfg
.bold_colour
&& (attr
& ATTR_BLINK
))
931 if (attr
& TATTR_ACTCURS
) {
936 gdk_gc_set_foreground(gc
, &inst
->cols
[nbg
]);
937 gdk_draw_rectangle(inst
->pixmap
, gc
, 1,
938 x
*inst
->font_width
+cfg
.window_border
,
939 y
*inst
->font_height
+cfg
.window_border
,
940 len
*inst
->font_width
, inst
->font_height
);
942 gdk_gc_set_foreground(gc
, &inst
->cols
[nfg
]);
943 gdk_draw_text(inst
->pixmap
, inst
->fonts
[0], gc
,
944 x
*inst
->font_width
+cfg
.window_border
,
945 y
*inst
->font_height
+cfg
.window_border
+inst
->fonts
[0]->ascent
,
948 if (attr
& ATTR_UNDER
) {
949 int uheight
= inst
->fonts
[0]->ascent
+ 1;
950 if (uheight
>= inst
->font_height
)
951 uheight
= inst
->font_height
- 1;
952 gdk_draw_line(inst
->pixmap
, gc
, x
*inst
->font_width
+cfg
.window_border
,
953 y
*inst
->font_height
+ uheight
+ cfg
.window_border
,
954 (x
+len
)*inst
->font_width
-1+cfg
.window_border
,
955 y
*inst
->font_height
+ uheight
+ cfg
.window_border
);
958 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
959 x
*inst
->font_width
+cfg
.window_border
,
960 y
*inst
->font_height
+cfg
.window_border
,
961 x
*inst
->font_width
+cfg
.window_border
,
962 y
*inst
->font_height
+cfg
.window_border
,
963 len
*inst
->font_width
, inst
->font_height
);
966 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
967 unsigned long attr
, int lattr
)
970 GdkGC
*gc
= (GdkGC
*)ctx
;
973 * NYI: cursor shapes other than block
975 if (attr
& TATTR_PASCURS
) {
976 attr
&= ~TATTR_PASCURS
;
980 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
982 gdk_gc_set_foreground(gc
, &inst
->cols
[NCOLOURS
-1]);
983 gdk_draw_rectangle(inst
->pixmap
, gc
, 0,
984 x
*inst
->font_width
+cfg
.window_border
,
985 y
*inst
->font_height
+cfg
.window_border
,
986 len
*inst
->font_width
-1, inst
->font_height
-1);
987 gdk_draw_pixmap(inst
->area
->window
, gc
, inst
->pixmap
,
988 x
*inst
->font_width
+cfg
.window_border
,
989 y
*inst
->font_height
+cfg
.window_border
,
990 x
*inst
->font_width
+cfg
.window_border
,
991 y
*inst
->font_height
+cfg
.window_border
,
992 len
*inst
->font_width
, inst
->font_height
);
996 GdkCursor
*make_mouse_ptr(int cursor_val
)
999 * Truly hideous hack: GTK doesn't allow us to set the mouse
1000 * cursor foreground and background colours unless we've _also_
1001 * created our own cursor from bitmaps. Therefore, I need to
1002 * load the `cursor' font and draw glyphs from it on to
1003 * pixmaps, in order to construct my cursors with the fg and bg
1004 * I want. This is a gross hack, but it's more self-contained
1005 * than linking in Xlib to find the X window handle to
1006 * inst->area and calling XRecolorCursor, and it's more
1007 * futureproof than hard-coding the shapes as bitmap arrays.
1009 static GdkFont
*cursor_font
= NULL
;
1010 GdkPixmap
*source
, *mask
;
1012 GdkColor cfg
= { 0, 65535, 65535, 65535 };
1013 GdkColor cbg
= { 0, 0, 0, 0 };
1014 GdkColor dfg
= { 1, 65535, 65535, 65535 };
1015 GdkColor dbg
= { 0, 0, 0, 0 };
1018 gint lb
, rb
, wid
, asc
, desc
, w
, h
, x
, y
;
1020 if (cursor_val
< 0) {
1021 gdk_font_unref(cursor_font
);
1026 cursor_font
= gdk_font_load("cursor");
1029 * Get the text extent of the cursor in question. We use the
1030 * mask character for this, because it's typically slightly
1031 * bigger than the main character.
1034 text
[0] = (char)cursor_val
+ 1;
1035 gdk_string_extents(cursor_font
, text
, &lb
, &rb
, &wid
, &asc
, &desc
);
1036 w
= rb
-lb
; h
= asc
+desc
; x
= -lb
; y
= asc
;
1038 source
= gdk_pixmap_new(NULL
, w
, h
, 1);
1039 mask
= gdk_pixmap_new(NULL
, w
, h
, 1);
1042 * Draw the mask character on the mask pixmap.
1045 text
[0] = (char)cursor_val
+ 1;
1046 gc
= gdk_gc_new(mask
);
1047 gdk_gc_set_foreground(gc
, &dbg
);
1048 gdk_draw_rectangle(mask
, gc
, 1, 0, 0, w
, h
);
1049 gdk_gc_set_foreground(gc
, &dfg
);
1050 gdk_draw_text(mask
, cursor_font
, gc
, x
, y
, text
, 1);
1054 * Draw the main character on the source pixmap.
1057 text
[0] = (char)cursor_val
;
1058 gc
= gdk_gc_new(source
);
1059 gdk_gc_set_foreground(gc
, &dbg
);
1060 gdk_draw_rectangle(source
, gc
, 1, 0, 0, w
, h
);
1061 gdk_gc_set_foreground(gc
, &dfg
);
1062 gdk_draw_text(source
, cursor_font
, gc
, x
, y
, text
, 1);
1066 * Create the cursor.
1068 ret
= gdk_cursor_new_from_pixmap(source
, mask
, &cfg
, &cbg
, x
, y
);
1073 gdk_pixmap_unref(source
);
1074 gdk_pixmap_unref(mask
);
1079 void modalfatalbox(char *p
, ...)
1082 fprintf(stderr
, "FATAL ERROR: ");
1084 vfprintf(stderr
, p
, ap
);
1086 fputc('\n', stderr
);
1090 int main(int argc
, char **argv
)
1093 extern int pty_master_fd
; /* declared in pty.c */
1095 gtk_init(&argc
, &argv
);
1097 do_defaults(NULL
, &cfg
);
1099 inst
->fonts
[0] = gdk_font_load(cfg
.font
);
1100 inst
->fonts
[1] = NULL
; /* FIXME: what about bold font? */
1101 inst
->font_width
= gdk_char_width(inst
->fonts
[0], ' ');
1102 inst
->font_height
= inst
->fonts
[0]->ascent
+ inst
->fonts
[0]->descent
;
1106 back
= &pty_backend
;
1107 back
->init(NULL
, 0, NULL
, 0);
1109 window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
1110 inst
->area
= gtk_drawing_area_new();
1111 gtk_drawing_area_size(GTK_DRAWING_AREA(inst
->area
),
1112 inst
->font_width
* cfg
.width
+ 2*cfg
.window_border
,
1113 inst
->font_height
* cfg
.height
+ 2*cfg
.window_border
);
1114 inst
->sbar_adjust
= GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, 0, 0, 0, 0));
1115 inst
->sbar
= gtk_vscrollbar_new(inst
->sbar_adjust
);
1116 inst
->hbox
= GTK_BOX(gtk_hbox_new(FALSE
, 0));
1117 gtk_box_pack_start(inst
->hbox
, inst
->area
, FALSE
, FALSE
, 0);
1118 gtk_box_pack_start(inst
->hbox
, inst
->sbar
, FALSE
, FALSE
, 0);
1120 gtk_container_add(GTK_CONTAINER(window
), GTK_WIDGET(inst
->hbox
));
1124 geom
.min_width
= inst
->font_width
+ 2*cfg
.window_border
;
1125 geom
.min_height
= inst
->font_height
+ 2*cfg
.window_border
;
1126 geom
.max_width
= geom
.max_height
= -1;
1127 geom
.base_width
= 2*cfg
.window_border
;
1128 geom
.base_height
= 2*cfg
.window_border
;
1129 geom
.width_inc
= inst
->font_width
;
1130 geom
.height_inc
= inst
->font_height
;
1131 geom
.min_aspect
= geom
.max_aspect
= 0;
1132 gtk_window_set_geometry_hints(GTK_WINDOW(window
), inst
->area
, &geom
,
1133 GDK_HINT_MIN_SIZE
| GDK_HINT_BASE_SIZE
|
1134 GDK_HINT_RESIZE_INC
);
1137 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
1138 GTK_SIGNAL_FUNC(destroy
), inst
);
1139 gtk_signal_connect(GTK_OBJECT(window
), "delete_event",
1140 GTK_SIGNAL_FUNC(delete_window
), inst
);
1141 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
1142 GTK_SIGNAL_FUNC(key_event
), inst
);
1143 gtk_signal_connect(GTK_OBJECT(window
), "focus_in_event",
1144 GTK_SIGNAL_FUNC(focus_event
), inst
);
1145 gtk_signal_connect(GTK_OBJECT(window
), "focus_out_event",
1146 GTK_SIGNAL_FUNC(focus_event
), inst
);
1147 gtk_signal_connect(GTK_OBJECT(inst
->area
), "configure_event",
1148 GTK_SIGNAL_FUNC(configure_area
), inst
);
1149 gtk_signal_connect(GTK_OBJECT(inst
->area
), "expose_event",
1150 GTK_SIGNAL_FUNC(expose_area
), inst
);
1151 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_press_event",
1152 GTK_SIGNAL_FUNC(button_event
), inst
);
1153 gtk_signal_connect(GTK_OBJECT(inst
->area
), "button_release_event",
1154 GTK_SIGNAL_FUNC(button_event
), inst
);
1155 gtk_signal_connect(GTK_OBJECT(inst
->area
), "motion_notify_event",
1156 GTK_SIGNAL_FUNC(motion_event
), inst
);
1157 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_received",
1158 GTK_SIGNAL_FUNC(selection_received
), inst
);
1159 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_get",
1160 GTK_SIGNAL_FUNC(selection_get
), inst
);
1161 gtk_signal_connect(GTK_OBJECT(inst
->area
), "selection_clear_event",
1162 GTK_SIGNAL_FUNC(selection_clear
), inst
);
1163 gtk_signal_connect(GTK_OBJECT(inst
->sbar_adjust
), "value_changed",
1164 GTK_SIGNAL_FUNC(scrollbar_moved
), inst
);
1165 gtk_timeout_add(20, timer_func
, inst
);
1166 gdk_input_add(pty_master_fd
, GDK_INPUT_READ
, pty_input_func
, inst
);
1167 gtk_widget_add_events(GTK_WIDGET(inst
->area
),
1168 GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
|
1169 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
1170 GDK_BUTTON_MOTION_MASK
);
1172 gtk_widget_show(inst
->area
);
1173 gtk_widget_show(inst
->sbar
);
1174 gtk_widget_show(GTK_WIDGET(inst
->hbox
));
1175 gtk_widget_show(window
);
1177 inst
->textcursor
= make_mouse_ptr(GDK_XTERM
);
1178 inst
->rawcursor
= make_mouse_ptr(GDK_LEFT_PTR
);
1179 make_mouse_ptr(-1); /* clean up cursor font */
1180 gdk_window_set_cursor(inst
->area
->window
, inst
->textcursor
);
1183 term_size(24, 80, 2000);