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.
13 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
16 #define CAT2(x,y) x ## y
17 #define CAT(x,y) CAT2(x,y)
18 #define ASSERT(x) enum {CAT(assertion_,__LINE__) = 1 / (x)}
20 #define NCOLOURS (lenof(((Config *)0)->colours))
25 GdkFont *fonts[2]; /* normal and bold (for now!) */
26 GdkCursor *rawcursor, *textcursor;
27 GdkColor cols[NCOLOURS];
29 GdkGC *black_gc, *white_gc;
32 static struct gui_data the_inst;
33 static struct gui_data *inst = &the_inst; /* so we always write `inst->' */
34 static int send_raw_mouse;
36 void ldisc_update(int echo, int edit)
39 * This is a stub in pterm. If I ever produce a Unix
40 * command-line ssh/telnet/rlogin client (i.e. a port of plink)
41 * then it will require some termios manoeuvring analogous to
42 * that in the Windows plink.c, but here it's meaningless.
46 int askappend(char *filename)
49 * FIXME: for the moment we just wipe the log file. Since I
50 * haven't yet enabled logging, this shouldn't matter yet!
55 void logevent(char *string)
58 * FIXME: event log entries are currently ignored.
63 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
64 * into a cooked one (SELECT, EXTEND, PASTE).
66 * In Unix, this is not configurable; the X button arrangement is
67 * rock-solid across all applications, everyone has a three-button
68 * mouse or a means of faking it, and there is no need to switch
69 * buttons around at all.
71 Mouse_Button translate_button(Mouse_Button button)
73 if (button == MBT_LEFT)
75 if (button == MBT_MIDDLE)
77 if (button == MBT_RIGHT)
79 return 0; /* shouldn't happen */
83 * Minimise or restore the window in response to a server-side
86 void set_iconic(int iconic)
88 /* FIXME: currently ignored */
92 * Move the window in response to a server-side request.
94 void move_window(int x, int y)
96 /* FIXME: currently ignored */
100 * Move the window to the top or bottom of the z-order in response
101 * to a server-side request.
103 void set_zorder(int top)
105 /* FIXME: currently ignored */
109 * Refresh the window in response to a server-side request.
111 void refresh_window(void)
113 /* FIXME: currently ignored */
117 * Maximise or restore the window in response to a server-side
120 void set_zoomed(int zoomed)
122 /* FIXME: currently ignored */
126 * Report whether the window is iconic, for terminal reports.
130 return 0; /* FIXME */
134 * Report the window's position, for terminal reports.
136 void get_window_pos(int *x, int *y)
138 *x = 3; *y = 4; /* FIXME */
142 * Report the window's pixel size, for terminal reports.
144 void get_window_pixels(int *x, int *y)
146 *x = 1; *y = 2; /* FIXME */
150 * Return the window or icon title.
152 char *get_window_title(int icon)
154 return "FIXME: window title retrieval not yet implemented";
157 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
160 * FIXME: warn on close?
165 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
167 struct gui_data *inst = (struct gui_data *)data;
170 gdk_pixmap_unref(inst->pixmap);
172 inst->pixmap = gdk_pixmap_new(widget->window, 9*80, 15*24, -1);
174 inst->fonts[0] = gdk_font_load("9x15t"); /* XXCONFIG */
175 inst->fonts[1] = NULL; /* XXCONFIG */
176 inst->black_gc = widget->style->black_gc;
177 inst->white_gc = widget->style->white_gc;
180 * Set up the colour map.
182 inst->colmap = gdk_colormap_get_system();
184 static const int ww[] = {
185 6, 7, 8, 9, 10, 11, 12, 13,
186 14, 15, 16, 17, 18, 19, 20, 21,
189 gboolean success[NCOLOURS];
192 assert(lenof(ww) == NCOLOURS);
194 for (i = 0; i < NCOLOURS; i++) {
195 inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
196 inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
197 inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
200 gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
201 FALSE, FALSE, success);
202 for (i = 0; i < NCOLOURS; i++) {
204 g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
205 i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
212 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
214 /* struct gui_data *inst = (struct gui_data *)data; */
217 * Pass the exposed rectangle to terminal.c, which will call us
218 * back to do the actual painting.
221 event->area.x / 9, event->area.y / 15,
222 (event->area.x + event->area.width - 1) / 9,
223 (event->area.y + event->area.height - 1) / 15);
227 #define KEY_PRESSED(k) \
228 (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
230 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
232 /* struct gui_data *inst = (struct gui_data *)data; */
234 if (event->type == GDK_KEY_PRESS) {
235 ldisc_send(event->string, strlen(event->string), 1);
242 gint timer_func(gpointer data)
244 /* struct gui_data *inst = (struct gui_data *)data; */
250 void destroy(GtkWidget *widget, gpointer data)
255 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
257 has_focus = event->in;
264 * set or clear the "raw mouse message" mode
266 void set_raw_mouse_mode(int activate)
268 activate = activate && !cfg.no_mouse_rep;
269 send_raw_mouse = activate;
271 gdk_window_set_cursor(inst->area->window, inst->rawcursor);
273 gdk_window_set_cursor(inst->area->window, inst->textcursor);
276 void request_resize(int w, int h)
278 /* FIXME: currently ignored */
281 void palette_set(int n, int r, int g, int b)
283 /* FIXME: currently ignored */
285 void palette_reset(void)
287 /* FIXME: currently ignored */
290 void write_clip(wchar_t * data, int len, int must_deselect)
292 /* FIXME: currently ignored */
295 void get_clip(wchar_t ** p, int *len)
298 /* FIXME: currently nonfunctional */
304 void set_title(char *title)
306 /* FIXME: currently ignored */
309 void set_icon(char *title)
311 /* FIXME: currently ignored */
314 void set_sbar(int total, int start, int page)
316 /* FIXME: currently ignored */
319 void sys_cursor(int x, int y)
322 * This is meaningless under X.
331 int CharWidth(Context ctx, int uc)
334 * Under X, any fixed-width font really _is_ fixed-width.
335 * Double-width characters will be dealt with using a separate
336 * font. For the moment we can simply return 1.
341 Context get_ctx(void)
344 if (!inst->area->window)
346 gc = gdk_gc_new(inst->area->window);
350 void free_ctx(Context ctx)
352 GdkGC *gc = (GdkGC *)ctx;
357 * Draw a line of text in the window, at given character
358 * coordinates, in given attributes.
360 * We are allowed to fiddle with the contents of `text'.
362 void do_text(Context ctx, int x, int y, char *text, int len,
363 unsigned long attr, int lattr)
366 GdkGC *gc = (GdkGC *)ctx;
370 * - ATTR_WIDE (is this for Unicode CJK? I hope so)
371 * - LATTR_* (ESC # 4 double-width and double-height stuff)
372 * - cursor shapes other than block
373 * - VT100 line drawing stuff; code pages in general!
378 nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
379 nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
380 if (attr & ATTR_REVERSE) {
385 if (cfg.bold_colour && (attr & ATTR_BOLD))
387 if (cfg.bold_colour && (attr & ATTR_BLINK))
389 if (attr & TATTR_ACTCURS) {
394 gdk_gc_set_foreground(gc, &inst->cols[nbg]);
395 gdk_draw_rectangle(inst->pixmap, gc, 1, x*9, y*15, len*9, 15);
397 gdk_gc_set_foreground(gc, &inst->cols[nfg]);
398 gdk_draw_text(inst->pixmap, inst->fonts[0], gc,
399 x*9, y*15 + inst->fonts[0]->ascent, text, len);
401 if (attr & ATTR_UNDER) {
402 int uheight = inst->fonts[0]->ascent + 1;
405 gdk_draw_line(inst->pixmap, gc, x*9, y*15 + uheight,
406 (x+len)*9-1, y*15+uheight);
409 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
410 x*9, y*15, x*9, y*15, len*9, 15);
413 void do_cursor(Context ctx, int x, int y, char *text, int len,
414 unsigned long attr, int lattr)
417 GdkGC *gc = (GdkGC *)ctx;
420 * NYI: cursor shapes other than block
422 if (attr & TATTR_PASCURS) {
423 attr &= ~TATTR_PASCURS;
427 do_text(ctx, x, y, text, len, attr, lattr);
429 gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
430 gdk_draw_rectangle(inst->pixmap, gc, 0, x*9, y*15, len*9-1, 15-1);
431 gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
432 x*9, y*15, x*9, y*15, len*9, 15);
436 void modalfatalbox(char *p, ...)
439 fprintf(stderr, "FATAL ERROR: ");
441 vfprintf(stderr, p, ap);
447 int main(int argc, char **argv)
451 gtk_init(&argc, &argv);
453 do_defaults(NULL, &cfg);
458 back->init(NULL, 0, NULL, 0);
460 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
461 inst->area = gtk_drawing_area_new();
462 gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
463 9*80, 15*24);/* FIXME: proper resizing stuff */
465 gtk_container_add(GTK_CONTAINER(window), inst->area);
467 gtk_signal_connect(GTK_OBJECT(window), "destroy",
468 GTK_SIGNAL_FUNC(destroy), inst);
469 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
470 GTK_SIGNAL_FUNC(delete_window), inst);
471 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
472 GTK_SIGNAL_FUNC(key_event), inst);
473 gtk_signal_connect(GTK_OBJECT(window), "focus_in_event",
474 GTK_SIGNAL_FUNC(focus_event), inst);
475 gtk_signal_connect(GTK_OBJECT(window), "focus_out_event",
476 GTK_SIGNAL_FUNC(focus_event), inst);
477 gtk_signal_connect(GTK_OBJECT(inst->area), "configure_event",
478 GTK_SIGNAL_FUNC(configure_area), inst);
479 gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
480 GTK_SIGNAL_FUNC(expose_area), inst);
481 gtk_timeout_add(20, timer_func, inst);
482 gtk_widget_add_events(GTK_WIDGET(inst->area),
483 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
485 gtk_widget_show(inst->area);
486 gtk_widget_show(window);
488 inst->textcursor = gdk_cursor_new(GDK_XTERM);
489 inst->rawcursor = gdk_cursor_new(GDK_ARROW);
490 gdk_window_set_cursor(inst->area->window, inst->textcursor);
493 term_size(24, 80, 2000);