]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/pterm.c
04e26a492f925bdac298854877f2c7f916de1b3a
[PuTTY.git] / unix / pterm.c
1 /*
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.
4  */
5
6 #include <string.h>
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <time.h>
11 #include <gtk/gtk.h>
12
13 #define PUTTY_DO_GLOBALS               /* actually _define_ globals */
14 #include "putty.h"
15
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)}
19
20 #define NCOLOURS (lenof(((Config *)0)->colours))
21
22 struct gui_data {
23     GtkWidget *area;
24     GdkPixmap *pixmap;
25     GdkFont *fonts[2];                 /* normal and bold (for now!) */
26     GdkCursor *rawcursor, *textcursor;
27     GdkColor cols[NCOLOURS];
28     GdkColormap *colmap;
29     GdkGC *black_gc, *white_gc;
30 };
31
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;
35
36 void ldisc_update(int echo, int edit)
37 {
38     /*
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.
43      */
44 }
45
46 int askappend(char *filename)
47 {
48     /*
49      * FIXME: for the moment we just wipe the log file. Since I
50      * haven't yet enabled logging, this shouldn't matter yet!
51      */
52     return 2;
53 }
54
55 void logevent(char *string)
56 {
57     /*
58      * FIXME: event log entries are currently ignored.
59      */
60 }
61
62 /*
63  * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
64  * into a cooked one (SELECT, EXTEND, PASTE).
65  * 
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.
70  */
71 Mouse_Button translate_button(Mouse_Button button)
72 {
73     if (button == MBT_LEFT)
74         return MBT_SELECT;
75     if (button == MBT_MIDDLE)
76         return MBT_PASTE;
77     if (button == MBT_RIGHT)
78         return MBT_EXTEND;
79     return 0;                          /* shouldn't happen */
80 }
81
82 /*
83  * Minimise or restore the window in response to a server-side
84  * request.
85  */
86 void set_iconic(int iconic)
87 {
88     /* FIXME: currently ignored */
89 }
90
91 /*
92  * Move the window in response to a server-side request.
93  */
94 void move_window(int x, int y)
95 {
96     /* FIXME: currently ignored */
97 }
98
99 /*
100  * Move the window to the top or bottom of the z-order in response
101  * to a server-side request.
102  */
103 void set_zorder(int top)
104 {
105     /* FIXME: currently ignored */
106 }
107
108 /*
109  * Refresh the window in response to a server-side request.
110  */
111 void refresh_window(void)
112 {
113     /* FIXME: currently ignored */
114 }
115
116 /*
117  * Maximise or restore the window in response to a server-side
118  * request.
119  */
120 void set_zoomed(int zoomed)
121 {
122     /* FIXME: currently ignored */
123 }
124
125 /*
126  * Report whether the window is iconic, for terminal reports.
127  */
128 int is_iconic(void)
129 {
130     return 0;                          /* FIXME */
131 }
132
133 /*
134  * Report the window's position, for terminal reports.
135  */
136 void get_window_pos(int *x, int *y)
137 {
138     *x = 3; *y = 4;                    /* FIXME */
139 }
140
141 /*
142  * Report the window's pixel size, for terminal reports.
143  */
144 void get_window_pixels(int *x, int *y)
145 {
146     *x = 1; *y = 2;                    /* FIXME */
147 }
148
149 /*
150  * Return the window or icon title.
151  */
152 char *get_window_title(int icon)
153 {
154     return "FIXME: window title retrieval not yet implemented";
155 }
156
157 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
158 {
159     /*
160      * FIXME: warn on close?
161      */
162     return FALSE;
163 }
164
165 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
166 {
167     struct gui_data *inst = (struct gui_data *)data;
168
169     if (inst->pixmap)
170         gdk_pixmap_unref(inst->pixmap);
171
172     inst->pixmap = gdk_pixmap_new(widget->window, 9*80, 15*24, -1);
173
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;
178
179     /*
180      * Set up the colour map.
181      */
182     inst->colmap = gdk_colormap_get_system();
183     {
184         static const int ww[] = {
185             6, 7, 8, 9, 10, 11, 12, 13,
186             14, 15, 16, 17, 18, 19, 20, 21,
187             0, 1, 2, 3, 4, 5
188         };
189         gboolean success[NCOLOURS];
190         int i;
191
192         assert(lenof(ww) == NCOLOURS);
193
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;
198         }
199
200         gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
201                                   FALSE, FALSE, success);
202         for (i = 0; i < NCOLOURS; i++) {
203             if (!success[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]);
206         }
207     }
208
209     return TRUE;
210 }
211
212 gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
213 {
214     /* struct gui_data *inst = (struct gui_data *)data; */
215
216     /*
217      * Pass the exposed rectangle to terminal.c, which will call us
218      * back to do the actual painting.
219      */
220     term_paint(NULL, 
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);
224     return TRUE;
225 }
226
227 #define KEY_PRESSED(k) \
228     (inst->keystate[(k) / 32] & (1 << ((k) % 32)))
229
230 gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
231 {
232     /* struct gui_data *inst = (struct gui_data *)data; */
233
234     if (event->type == GDK_KEY_PRESS) {
235         ldisc_send(event->string, strlen(event->string), 1);
236         term_out();
237     }
238
239     return TRUE;
240 }
241
242 gint timer_func(gpointer data)
243 {
244     /* struct gui_data *inst = (struct gui_data *)data; */
245
246     term_update();
247     return TRUE;
248 }
249
250 void destroy(GtkWidget *widget, gpointer data)
251 {
252     gtk_main_quit();
253 }
254
255 gint focus_event(GtkWidget *widget, GdkEventFocus *event, gpointer data)
256 {
257     has_focus = event->in;
258     term_out();
259     term_update();
260     return FALSE;
261 }
262
263 /*
264  * set or clear the "raw mouse message" mode
265  */
266 void set_raw_mouse_mode(int activate)
267 {
268     activate = activate && !cfg.no_mouse_rep;
269     send_raw_mouse = activate;
270     if (send_raw_mouse)
271         gdk_window_set_cursor(inst->area->window, inst->rawcursor);
272     else
273         gdk_window_set_cursor(inst->area->window, inst->textcursor);
274 }
275
276 void request_resize(int w, int h)
277 {
278     /* FIXME: currently ignored */
279 }
280
281 void palette_set(int n, int r, int g, int b)
282 {
283     /* FIXME: currently ignored */
284 }
285 void palette_reset(void)
286 {
287     /* FIXME: currently ignored */
288 }
289
290 void write_clip(wchar_t * data, int len, int must_deselect)
291 {
292     /* FIXME: currently ignored */
293 }
294
295 void get_clip(wchar_t ** p, int *len)
296 {
297     if (p) {
298         /* FIXME: currently nonfunctional */
299         *p = NULL;
300         *len = 0;
301     }
302 }
303
304 void set_title(char *title)
305 {
306     /* FIXME: currently ignored */
307 }
308
309 void set_icon(char *title)
310 {
311     /* FIXME: currently ignored */
312 }
313
314 void set_sbar(int total, int start, int page)
315 {
316     /* FIXME: currently ignored */
317 }
318
319 void sys_cursor(int x, int y)
320 {
321     /*
322      * This is meaningless under X.
323      */
324 }
325
326 void beep(int mode)
327 {
328     gdk_beep();
329 }
330
331 int CharWidth(Context ctx, int uc)
332 {
333     /*
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.
337      */
338     return 1;
339 }
340
341 Context get_ctx(void)
342 {
343     GdkGC *gc;
344     if (!inst->area->window)
345         return NULL;
346     gc = gdk_gc_new(inst->area->window);
347     return gc;
348 }
349
350 void free_ctx(Context ctx)
351 {
352     GdkGC *gc = (GdkGC *)ctx;
353     gdk_gc_unref(gc);
354 }
355
356 /*
357  * Draw a line of text in the window, at given character
358  * coordinates, in given attributes.
359  *
360  * We are allowed to fiddle with the contents of `text'.
361  */
362 void do_text(Context ctx, int x, int y, char *text, int len,
363              unsigned long attr, int lattr)
364 {
365     int nfg, nbg, t;
366     GdkGC *gc = (GdkGC *)ctx;
367
368     /*
369      * NYI:
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!
374      *  - shadow bolding
375      *  - underline
376      */
377
378     nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
379     nbg = 2 * ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
380     if (attr & ATTR_REVERSE) {
381         t = nfg;
382         nfg = nbg;
383         nbg = t;
384     }
385     if (cfg.bold_colour && (attr & ATTR_BOLD))
386         nfg++;
387     if (cfg.bold_colour && (attr & ATTR_BLINK))
388         nbg++;
389     if (attr & TATTR_ACTCURS) {
390         nfg = NCOLOURS-2;
391         nbg = NCOLOURS-1;
392     }
393
394     gdk_gc_set_foreground(gc, &inst->cols[nbg]);
395     gdk_draw_rectangle(inst->pixmap, gc, 1, x*9, y*15, len*9, 15);
396     
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);
400
401     if (attr & ATTR_UNDER) {
402         int uheight = inst->fonts[0]->ascent + 1;
403         if (uheight >= 15)
404             uheight = 14;
405         gdk_draw_line(inst->pixmap, gc, x*9, y*15 + uheight,
406                       (x+len)*9-1, y*15+uheight);
407     }
408
409     gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
410                     x*9, y*15, x*9, y*15, len*9, 15);
411 }
412
413 void do_cursor(Context ctx, int x, int y, char *text, int len,
414                unsigned long attr, int lattr)
415 {
416     int passive;
417     GdkGC *gc = (GdkGC *)ctx;
418
419     /*
420      * NYI: cursor shapes other than block
421      */
422     if (attr & TATTR_PASCURS) {
423         attr &= ~TATTR_PASCURS;
424         passive = 1;
425     } else
426         passive = 0;
427     do_text(ctx, x, y, text, len, attr, lattr);
428     if (passive) {
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);
433     }
434 }
435
436 void modalfatalbox(char *p, ...)
437 {
438     va_list ap;
439     fprintf(stderr, "FATAL ERROR: ");
440     va_start(ap, p);
441     vfprintf(stderr, p, ap);
442     va_end(ap);
443     fputc('\n', stderr);
444     exit(1);
445 }
446
447 int main(int argc, char **argv)
448 {
449     GtkWidget *window;
450
451     gtk_init(&argc, &argv);
452
453     do_defaults(NULL, &cfg);
454
455     init_ucs();
456
457     back = &pty_backend;
458     back->init(NULL, 0, NULL, 0);
459
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 */
464
465     gtk_container_add(GTK_CONTAINER(window), inst->area);
466
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);
484
485     gtk_widget_show(inst->area);
486     gtk_widget_show(window);
487
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);
491
492     term_init();
493     term_size(24, 80, 2000);
494
495     gtk_main();
496
497     return 0;
498 }