Stop hard-coding a nonstandard font. We now default to `fixed', and
[u/mdw/putty] / unix / pterm.c
index 97fe6c4..bc9813a 100644 (file)
@@ -8,6 +8,9 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkkeysyms.h>
 
@@ -28,6 +31,7 @@ struct gui_data {
     GdkColor cols[NCOLOURS];
     GdkColormap *colmap;
     GdkGC *black_gc, *white_gc;
+    int font_width, font_height;
 };
 
 static struct gui_data the_inst;
@@ -170,10 +174,10 @@ gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
     if (inst->pixmap)
        gdk_pixmap_unref(inst->pixmap);
 
-    inst->pixmap = gdk_pixmap_new(widget->window, 9*80, 15*24, -1);
+    inst->pixmap = gdk_pixmap_new(widget->window,
+                                 cfg.width * inst->font_width,
+                                 cfg.height * inst->font_height, -1);
 
-    inst->fonts[0] = gdk_font_load("9x15t");   /* XXCONFIG */
-    inst->fonts[1] = NULL;             /* XXCONFIG */
     inst->black_gc = widget->style->black_gc;
     inst->white_gc = widget->style->white_gc;
 
@@ -219,9 +223,10 @@ gint expose_area(GtkWidget *widget, GdkEventExpose *event, gpointer data)
      * back to do the actual painting.
      */
     term_paint(NULL, 
-              event->area.x / 9, event->area.y / 15,
-              (event->area.x + event->area.width - 1) / 9,
-              (event->area.y + event->area.height - 1) / 15);
+              event->area.x / inst->font_width,
+              event->area.y / inst->font_height,
+              (event->area.x + event->area.width - 1) / inst->font_width,
+              (event->area.y + event->area.height - 1) / inst->font_height);
     return TRUE;
 }
 
@@ -593,6 +598,32 @@ gint timer_func(gpointer data)
     return TRUE;
 }
 
+void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
+{
+    /* struct gui_data *inst = (struct gui_data *)data; */
+    char buf[4096];
+    int ret;
+
+    ret = read(sourcefd, buf, sizeof(buf));
+
+    /*
+     * Clean termination condition is that either ret == 0, or ret
+     * < 0 and errno == EIO. Not sure why the latter, but it seems
+     * to happen. Boo.
+     */
+    if (ret == 0 || (ret < 0 && errno == EIO)) {
+       exit(0);
+    }
+
+    if (ret < 0) {
+       perror("read pty master");
+       exit(1);
+    }
+    if (ret > 0)
+       from_backend(0, buf, ret);
+    term_out();
+}
+
 void destroy(GtkWidget *widget, gpointer data)
 {
     gtk_main_quit();
@@ -738,22 +769,27 @@ void do_text(Context ctx, int x, int y, char *text, int len,
     }
 
     gdk_gc_set_foreground(gc, &inst->cols[nbg]);
-    gdk_draw_rectangle(inst->pixmap, gc, 1, x*9, y*15, len*9, 15);
+    gdk_draw_rectangle(inst->pixmap, gc, 1,
+                      x*inst->font_width, y*inst->font_height,
+                      len*inst->font_width, inst->font_height);
     
     gdk_gc_set_foreground(gc, &inst->cols[nfg]);
-    gdk_draw_text(inst->pixmap, inst->fonts[0], gc,
-                 x*9, y*15 + inst->fonts[0]->ascent, text, len);
+    gdk_draw_text(inst->pixmap, inst->fonts[0], gc, x*inst->font_width,
+                 y*inst->font_height + inst->fonts[0]->ascent, text, len);
 
     if (attr & ATTR_UNDER) {
        int uheight = inst->fonts[0]->ascent + 1;
-       if (uheight >= 15)
-           uheight = 14;
-       gdk_draw_line(inst->pixmap, gc, x*9, y*15 + uheight,
-                     (x+len)*9-1, y*15+uheight);
+       if (uheight >= inst->font_height)
+           uheight = inst->font_height - 1;
+       gdk_draw_line(inst->pixmap, gc, x*inst->font_width,
+                     y*inst->font_height + uheight,
+                     (x+len)*inst->font_width-1, y*inst->font_height+uheight);
     }
 
     gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
-                   x*9, y*15, x*9, y*15, len*9, 15);
+                   x*inst->font_width, y*inst->font_height,
+                   x*inst->font_width, y*inst->font_height,
+                   len*inst->font_width, inst->font_height);
 }
 
 void do_cursor(Context ctx, int x, int y, char *text, int len,
@@ -773,9 +809,13 @@ void do_cursor(Context ctx, int x, int y, char *text, int len,
     do_text(ctx, x, y, text, len, attr, lattr);
     if (passive) {
        gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
-       gdk_draw_rectangle(inst->pixmap, gc, 0, x*9, y*15, len*9-1, 15-1);
+       gdk_draw_rectangle(inst->pixmap, gc, 0,
+                          x*inst->font_width, y*inst->font_height,
+                          len*inst->font_width-1, inst->font_height-1);
        gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
-                       x*9, y*15, x*9, y*15, len*9, 15);
+                       x*inst->font_width, y*inst->font_height,
+                       x*inst->font_width, y*inst->font_height,
+                       len*inst->font_width, inst->font_height);
     }
 }
 
@@ -793,11 +833,17 @@ void modalfatalbox(char *p, ...)
 int main(int argc, char **argv)
 {
     GtkWidget *window;
+    extern int pty_master_fd;         /* declared in pty.c */
 
     gtk_init(&argc, &argv);
 
     do_defaults(NULL, &cfg);
 
+    inst->fonts[0] = gdk_font_load(cfg.font);
+    inst->fonts[1] = NULL;             /* FIXME: what about bold font? */
+    inst->font_width = gdk_char_width(inst->fonts[0], ' ');
+    inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
+
     init_ucs();
 
     back = &pty_backend;
@@ -806,7 +852,8 @@ int main(int argc, char **argv)
     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     inst->area = gtk_drawing_area_new();
     gtk_drawing_area_size(GTK_DRAWING_AREA(inst->area),
-                         9*80, 15*24);/* FIXME: proper resizing stuff */
+                         inst->font_width * cfg.width,
+                         inst->font_height * cfg.height);
 
     gtk_container_add(GTK_CONTAINER(window), inst->area);
 
@@ -825,6 +872,7 @@ int main(int argc, char **argv)
     gtk_signal_connect(GTK_OBJECT(inst->area), "expose_event",
                       GTK_SIGNAL_FUNC(expose_area), inst);
     gtk_timeout_add(20, timer_func, inst);
+    gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
     gtk_widget_add_events(GTK_WIDGET(inst->area),
                          GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);