Implement reading of X resources, and -name to change the name under
[sgt/putty] / unix / pterm.c
index b59c27c..1262782 100644 (file)
@@ -472,6 +472,12 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
            output[1] = cfg.bksp_is_delete ? '\x7F' : '\x08';
            end = 2;
        }
+       /* For Shift Backspace, do opposite of what is configured. */
+       if (event->keyval == GDK_BackSpace &&
+           (event->state & GDK_SHIFT_MASK)) {
+           output[1] = cfg.bksp_is_delete ? '\x08' : '\x7F';
+           end = 2;
+       }
 
        /* Shift-Tab is ESC [ Z */
        if (event->keyval == GDK_ISO_Left_Tab ||
@@ -1233,17 +1239,15 @@ void free_ctx(Context ctx)
  *
  * We are allowed to fiddle with the contents of `text'.
  */
-void do_text(Context ctx, int x, int y, char *text, int len,
-            unsigned long attr, int lattr)
+void do_text_internal(Context ctx, int x, int y, char *text, int len,
+                     unsigned long attr, int lattr)
 {
-    int nfg, nbg, t;
+    int nfg, nbg, t, fontid, shadow;
     GdkGC *gc = (GdkGC *)ctx;
 
     /*
      * NYI:
      *  - Unicode, code pages, and ATTR_WIDE for CJK support.
-     *  - cursor shapes other than block
-     *  - shadow bolding
      */
 
     nfg = 2 * ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
@@ -1262,6 +1266,14 @@ void do_text(Context ctx, int x, int y, char *text, int len,
        nbg = NCOLOURS-1;
     }
 
+    fontid = shadow = 0;
+    if ((attr & ATTR_BOLD) && !cfg.bold_colour) {
+       if (inst->fonts[1])
+           fontid = 1;
+       else
+           shadow = 1;
+    }
+
     if (lattr != LATTR_NORM) {
        x *= 2;
        if (x >= cols)
@@ -1277,11 +1289,25 @@ void do_text(Context ctx, int x, int y, char *text, int len,
                       len*inst->font_width, inst->font_height);
 
     gdk_gc_set_foreground(gc, &inst->cols[nfg]);
-    gdk_draw_text(inst->pixmap, inst->fonts[0], gc,
+    gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
                  x*inst->font_width+cfg.window_border,
                  y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
                  text, len);
 
+    /*
+     * X fonts seem to be pretty consistent about leaving the
+     * _left_ pixel of the cell blank rather than the right. Hence
+     * I'm going to hard-code shadow bolding as displaying one
+     * pixel to the left rather than try to work out whether it
+     * should be left or right.
+     */
+    if (shadow) {
+       gdk_draw_text(inst->pixmap, inst->fonts[fontid], gc,
+                     x*inst->font_width+cfg.window_border - 1,
+                     y*inst->font_height+cfg.window_border+inst->fonts[0]->ascent,
+                     text, len);
+    }
+
     if (attr & ATTR_UNDER) {
        int uheight = inst->fonts[0]->ascent + 1;
        if (uheight >= inst->font_height)
@@ -1327,6 +1353,22 @@ void do_text(Context ctx, int x, int y, char *text, int len,
                                len * inst->font_width, inst->font_height-i-1);
            }
        }
+    }
+}
+
+void do_text(Context ctx, int x, int y, char *text, int len,
+            unsigned long attr, int lattr)
+{
+    GdkGC *gc = (GdkGC *)ctx;
+
+    do_text_internal(ctx, x, y, text, len, attr, lattr);
+
+    if (lattr != LATTR_NORM) {
+       x *= 2;
+       if (x >= cols)
+           return;
+       if (x + len*2 > cols)
+           len = (cols-x)/2;          /* trim to LH half */
        len *= 2;
     }
 
@@ -1344,28 +1386,91 @@ void do_cursor(Context ctx, int x, int y, char *text, int len,
     int passive;
     GdkGC *gc = (GdkGC *)ctx;
 
-    /*
-     * NYI: cursor shapes other than block
-     */
     if (attr & TATTR_PASCURS) {
        attr &= ~TATTR_PASCURS;
        passive = 1;
     } else
        passive = 0;
-    do_text(ctx, x, y, text, len, attr, lattr);
-    if (passive) {
+    if ((attr & TATTR_ACTCURS) && cfg.cursor_type != 0) {
+       attr &= ~TATTR_ACTCURS;
+    }
+    do_text_internal(ctx, x, y, text, len, attr, lattr);
+
+    if (lattr != LATTR_NORM) {
+       x *= 2;
+       if (x >= cols)
+           return;
+       if (x + len*2 > cols)
+           len = (cols-x)/2;          /* trim to LH half */
+       len *= 2;
+    }
+
+    if (cfg.cursor_type == 0) {
+       /*
+        * An active block cursor will already have been done by
+        * the above do_text call, so we only need to do anything
+        * if it's passive.
+        */
+       if (passive) {
+           gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
+           gdk_draw_rectangle(inst->pixmap, gc, 0,
+                              x*inst->font_width+cfg.window_border,
+                              y*inst->font_height+cfg.window_border,
+                              len*inst->font_width-1, inst->font_height-1);
+       }
+    } else {
+       int uheight;
+       int startx, starty, dx, dy, length, i;
+
+       int char_width;
+
+       if ((attr & ATTR_WIDE) || lattr != LATTR_NORM)
+           char_width = 2*inst->font_width;
+       else
+           char_width = inst->font_width;
+
+       if (cfg.cursor_type == 1) {
+           uheight = inst->fonts[0]->ascent + 1;
+           if (uheight >= inst->font_height)
+               uheight = inst->font_height - 1;
+
+           startx = x * inst->font_width + cfg.window_border;
+           starty = y * inst->font_height + cfg.window_border + uheight;
+           dx = 1;
+           dy = 0;
+           length = len * char_width;
+       } else {
+           int xadjust = 0;
+           if (attr & TATTR_RIGHTCURS)
+               xadjust = char_width - 1;
+           startx = x * inst->font_width + cfg.window_border + xadjust;
+           starty = y * inst->font_height + cfg.window_border;
+           dx = 0;
+           dy = 1;
+           length = inst->font_height;
+       }
+
        gdk_gc_set_foreground(gc, &inst->cols[NCOLOURS-1]);
-       gdk_draw_rectangle(inst->pixmap, gc, 0,
-                          x*inst->font_width+cfg.window_border,
-                          y*inst->font_height+cfg.window_border,
-                          len*inst->font_width-1, inst->font_height-1);
-       gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
-                       x*inst->font_width+cfg.window_border,
-                       y*inst->font_height+cfg.window_border,
-                       x*inst->font_width+cfg.window_border,
-                       y*inst->font_height+cfg.window_border,
-                       len*inst->font_width, inst->font_height);
+       if (passive) {
+           for (i = 0; i < length; i++) {
+               if (i % 2 == 0) {
+                   gdk_draw_point(inst->pixmap, gc, startx, starty);
+               }
+               startx += dx;
+               starty += dy;
+           }
+       } else {
+           gdk_draw_line(inst->pixmap, gc, startx, starty,
+                         startx + (length-1) * dx, starty + (length-1) * dy);
+       }
     }
+
+    gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
+                   x*inst->font_width+cfg.window_border,
+                   y*inst->font_height+cfg.window_border,
+                   x*inst->font_width+cfg.window_border,
+                   y*inst->font_height+cfg.window_border,
+                   len*inst->font_width, inst->font_height);
 }
 
 GdkCursor *make_mouse_ptr(int cursor_val)
@@ -1476,26 +1581,47 @@ char *get_x_display(void)
     return gdk_get_display();
 }
 
-int main(int argc, char **argv)
+char *app_name = "pterm";
+
+int do_cmdline(int argc, char **argv, int do_everything)
 {
-    extern int pty_master_fd;         /* declared in pty.c */
-    extern char **pty_argv;           /* declared in pty.c */
     int err = 0;
+    extern char **pty_argv;           /* declared in pty.c */
 
-    gtk_init(&argc, &argv);
-
-    do_defaults(NULL, &cfg);
-
+    /*
+     * Macros to make argument handling easier.
+     */
+#define EXPECTS_ARG do { \
+    if (--argc <= 0) { \
+       err = 1; \
+       fprintf(stderr, "pterm: %s expects an argument\n", p); \
+    } else \
+       val = *++argv; \
+} while (0)
+#define SECOND_PASS_ONLY do { \
+    if (!do_everything) continue; \
+} while (0)
+
+    char *val;
     while (--argc > 0) {
        char *p = *++argv;
        if (!strcmp(p, "-fn")) {
-           if (--argc > 0) {
-               strncpy(cfg.font, *++argv, sizeof(cfg.font));
-               cfg.font[sizeof(cfg.font)-1] = '\0';
-           } else
-               err = 1, fprintf(stderr, "pterm: -fn expects an argument\n");
-       }
-       if (!strcmp(p, "-e")) {
+           EXPECTS_ARG;
+           SECOND_PASS_ONLY;
+           strncpy(cfg.font, val, sizeof(cfg.font));
+           cfg.font[sizeof(cfg.font)-1] = '\0';
+
+       } else if (!strcmp(p, "-fb")) {
+           EXPECTS_ARG;
+           SECOND_PASS_ONLY;
+           strncpy(cfg.boldfont, val, sizeof(cfg.boldfont));
+           cfg.boldfont[sizeof(cfg.boldfont)-1] = '\0';
+
+       } else if (!strcmp(p, "-e")) {
+           /* This option swallows all further arguments. */
+           if (!do_everything)
+               break;
+
            if (--argc > 0) {
                int i;
                pty_argv = smalloc((argc+1) * sizeof(char *));
@@ -1506,45 +1632,79 @@ int main(int argc, char **argv)
                break;                 /* finished command-line processing */
            } else
                err = 1, fprintf(stderr, "pterm: -e expects an argument\n");
-       }
-       if (!strcmp(p, "-T")) {
-           if (--argc > 0) {
-               strncpy(cfg.wintitle, *++argv, sizeof(cfg.wintitle));
-               cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
-           } else
-               err = 1, fprintf(stderr, "pterm: -T expects an argument\n");
-       }
-       if (!strcmp(p, "-log")) {
-           if (--argc > 0) {
-               strncpy(cfg.logfilename, *++argv, sizeof(cfg.logfilename));
-               cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
-               cfg.logtype = LGTYP_DEBUG;
-           } else
-               err = 1, fprintf(stderr, "pterm: -log expects an argument\n");
-       }
-       if (!strcmp(p, "-hide")) {
+
+       } else if (!strcmp(p, "-T")) {
+           EXPECTS_ARG;
+           SECOND_PASS_ONLY;
+           strncpy(cfg.wintitle, val, sizeof(cfg.wintitle));
+           cfg.wintitle[sizeof(cfg.wintitle)-1] = '\0';
+
+       } else if (!strcmp(p, "-log")) {
+           EXPECTS_ARG;
+           SECOND_PASS_ONLY;
+           strncpy(cfg.logfilename, val, sizeof(cfg.logfilename));
+           cfg.logfilename[sizeof(cfg.logfilename)-1] = '\0';
+           cfg.logtype = LGTYP_DEBUG;
+
+       } else if (!strcmp(p, "-hide")) {
+           SECOND_PASS_ONLY;
            cfg.hide_mouseptr = 1;
-       }
-       if (!strcmp(p, "-ut-")) {
+
+       } else if (!strcmp(p, "-ut-")) {
+           SECOND_PASS_ONLY;
            cfg.stamp_utmp = 0;
-       }
-       if (!strcmp(p, "-ls-")) {
+
+       } else if (!strcmp(p, "-ls-")) {
+           SECOND_PASS_ONLY;
            cfg.login_shell = 0;
-       }
-       if (!strcmp(p, "-nethack")) {
+
+       } else if (!strcmp(p, "-nethack")) {
+           SECOND_PASS_ONLY;
            cfg.nethack_keypad = 1;
-       }
-       if (!strcmp(p, "-sb-")) {
+
+       } else if (!strcmp(p, "-sb-")) {
+           SECOND_PASS_ONLY;
            cfg.scrollbar = 0;
+
+       } else if (!strcmp(p, "-name")) {
+           EXPECTS_ARG;
+           app_name = val;
        }
     }
 
+    return err;
+}
+
+int main(int argc, char **argv)
+{
+    extern int pty_master_fd;         /* declared in pty.c */
+    extern void pty_pre_init(void);    /* declared in pty.c */
+
+    pty_pre_init();
+
+    gtk_init(&argc, &argv);
+
+    if (do_cmdline(argc, argv, 0))     /* pre-defaults pass to get -class */
+       exit(1);
+    do_defaults(NULL, &cfg);
+    if (do_cmdline(argc, argv, 1))     /* post-defaults, do everything */
+       exit(1);
+
     inst->fonts[0] = gdk_font_load(cfg.font);
     if (!inst->fonts[0]) {
        fprintf(stderr, "pterm: unable to load font \"%s\"\n", cfg.font);
        exit(1);
     }
-    inst->fonts[1] = NULL;             /* FIXME: what about bold font? */
+    if (cfg.boldfont[0]) {
+       inst->fonts[1] = gdk_font_load(cfg.boldfont);
+       if (!inst->fonts[1]) {
+           fprintf(stderr, "pterm: unable to load bold font \"%s\"\n",
+                   cfg.boldfont);
+           exit(1);
+       }
+    } else
+       inst->fonts[1] = NULL;
+
     inst->font_width = gdk_char_width(inst->fonts[0], ' ');
     inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;