Introduce the ability to control whether the shell run in pterm is a
[u/mdw/putty] / unix / pterm.c
index b18fc1f..769f728 100644 (file)
@@ -38,6 +38,8 @@ struct gui_data {
     int pasteout_data_len;
     int font_width, font_height;
     int ignore_sbar;
+    int mouseptr_visible;
+    guint term_paste_idle_id;
     GdkAtom compound_text_atom;
     char wintitle[sizeof(((Config *)0)->wintitle)];
 };
@@ -59,8 +61,10 @@ void ldisc_update(int echo, int edit)
 int askappend(char *filename)
 {
     /*
-     * FIXME: for the moment we just wipe the log file. Since I
-     * haven't yet enabled logging, this shouldn't matter yet!
+     * Logging in an xterm-alike is liable to be something you only
+     * do at serious diagnostic need. Hence, I'm going to take the
+     * easy option for now and assume we always want to overwrite
+     * log files. I can always make it properly configurable later.
      */
     return 2;
 }
@@ -186,6 +190,7 @@ void show_mouseptr(int show)
        gdk_window_set_cursor(inst->area->window, inst->currcursor);
     else
        gdk_window_set_cursor(inst->area->window, inst->blankcursor);
+    inst->mouseptr_visible = show;
 }
 
 gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
@@ -193,6 +198,11 @@ gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
     struct gui_data *inst = (struct gui_data *)data;
     int w, h, need_size = 0;
 
+    /*
+     * Set up the colour map.
+     */
+    palette_reset();
+
     w = (event->width - 2*cfg.window_border) / inst->font_width;
     h = (event->height - 2*cfg.window_border) / inst->font_height;
 
@@ -226,37 +236,6 @@ gint configure_area(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
        term_size(h, w, cfg.savelines);
     }
 
-    /*
-     * Set up the colour map.
-     */
-    if (!inst->colmap) {
-       static const int ww[] = {
-           6, 7, 8, 9, 10, 11, 12, 13,
-           14, 15, 16, 17, 18, 19, 20, 21,
-           0, 1, 2, 3, 4, 5
-       };
-       gboolean success[NCOLOURS];
-       int i;
-
-       inst->colmap = gdk_colormap_get_system();
-
-       assert(lenof(ww) == NCOLOURS);
-
-       for (i = 0; i < NCOLOURS; i++) {
-           inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
-           inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
-           inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
-       }
-
-       gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
-                                 FALSE, FALSE, success);
-       for (i = 0; i < NCOLOURS; i++) {
-           if (!success[i])
-               g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
-                       i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
-       }
-    }
-
     return TRUE;
 }
 
@@ -302,7 +281,6 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
 
        /*
         * NYI:
-        *  - nethack mode
         *  - alt+numpad
         *  - Compose key (!!! requires Unicode faff before even trying)
         */
@@ -774,13 +752,6 @@ gint timer_func(gpointer data)
     return TRUE;
 }
 
-gint idle_func(gpointer data)
-{
-    /* struct gui_data *inst = (struct gui_data *)data; */
-    term_paste();
-    return TRUE;
-}
-
 void pty_input_func(gpointer data, gint sourcefd, GdkInputCondition condition)
 {
     /* struct gui_data *inst = (struct gui_data *)data; */
@@ -832,7 +803,7 @@ void set_raw_mouse_mode(int activate)
        inst->currcursor = inst->rawcursor;
     else
        inst->currcursor = inst->textcursor;
-    show_mouseptr(1);
+    show_mouseptr(inst->mouseptr_visible);
 }
 
 void request_resize(int w, int h)
@@ -840,13 +811,65 @@ void request_resize(int w, int h)
     /* FIXME: currently ignored */
 }
 
+void real_palette_set(int n, int r, int g, int b)
+{
+    gboolean success[1];
+
+    inst->cols[n].red = r * 0x0101;
+    inst->cols[n].green = g * 0x0101;
+    inst->cols[n].blue = b * 0x0101;
+
+    gdk_colormap_alloc_colors(inst->colmap, inst->cols + n, 1,
+                             FALSE, FALSE, success);
+    if (!success[0])
+       g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
+               n, r, g, b);
+}
+
 void palette_set(int n, int r, int g, int b)
 {
-    /* FIXME: currently ignored */
+    static const int first[21] = {
+       0, 2, 4, 6, 8, 10, 12, 14,
+       1, 3, 5, 7, 9, 11, 13, 15,
+       16, 17, 18, 20, 22
+    };
+    real_palette_set(first[n], r, g, b);
+    if (first[n] >= 18)
+       real_palette_set(first[n] + 1, r, g, b);
 }
+
 void palette_reset(void)
 {
-    /* FIXME: currently ignored */
+    /* This maps colour indices in cfg to those used in inst->cols. */
+    static const int ww[] = {
+       6, 7, 8, 9, 10, 11, 12, 13,
+        14, 15, 16, 17, 18, 19, 20, 21,
+       0, 1, 2, 3, 4, 5
+    };
+    gboolean success[NCOLOURS];
+    int i;
+
+    assert(lenof(ww) == NCOLOURS);
+
+    if (!inst->colmap) {
+       inst->colmap = gdk_colormap_get_system();
+    } else {
+       gdk_colormap_free_colors(inst->colmap, inst->cols, NCOLOURS);
+    }
+
+    for (i = 0; i < NCOLOURS; i++) {
+       inst->cols[i].red = cfg.colours[ww[i]][0] * 0x0101;
+       inst->cols[i].green = cfg.colours[ww[i]][1] * 0x0101;
+       inst->cols[i].blue = cfg.colours[ww[i]][2] * 0x0101;
+    }
+
+    gdk_colormap_alloc_colors(inst->colmap, inst->cols, NCOLOURS,
+                             FALSE, FALSE, success);
+    for (i = 0; i < NCOLOURS; i++) {
+       if (!success[i])
+           g_error("pterm: couldn't allocate colour %d (#%02x%02x%02x)\n",
+                   i, cfg.colours[i][0], cfg.colours[i][1], cfg.colours[i][2]);
+    }
 }
 
 void write_clip(wchar_t * data, int len, int must_deselect)
@@ -896,6 +919,8 @@ void request_paste(void)
                          GDK_SELECTION_TYPE_STRING, GDK_CURRENT_TIME);
 }
 
+gint idle_paste_func(gpointer data);   /* forward ref */
+
 void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
                        gpointer data)
 {
@@ -912,8 +937,24 @@ void selection_received(GtkWidget *widget, GtkSelectionData *seldata,
             inst->pastein_data, inst->pastein_data_len);
 
     term_do_paste();
+
+    if (term_paste_pending())
+       inst->term_paste_idle_id = gtk_idle_add(idle_paste_func, inst);
+}
+
+gint idle_paste_func(gpointer data)
+{
+    struct gui_data *inst = (struct gui_data *)data;
+
+    if (term_paste_pending())
+       term_paste();
+    else
+       gtk_idle_remove(inst->term_paste_idle_id);
+
+    return TRUE;
 }
 
+
 void get_clip(wchar_t ** p, int *len)
 {
     if (p) {
@@ -1005,7 +1046,6 @@ void do_text(Context ctx, int x, int y, char *text, int len,
     /*
      * NYI:
      *  - Unicode, code pages, and ATTR_WIDE for CJK support.
-     *  - LATTR_* (ESC # 4 double-width and double-height stuff)
      *  - cursor shapes other than block
      *  - shadow bolding
      */
@@ -1026,6 +1066,14 @@ void do_text(Context ctx, int x, int y, char *text, int len,
        nbg = NCOLOURS-1;
     }
 
+    if (lattr != LATTR_NORM) {
+       x *= 2;
+       if (x >= cols)
+           return;
+       if (x + len*2 > cols)
+           len = (cols-x)/2;          /* trim to LH half */
+    }
+
     gdk_gc_set_foreground(gc, &inst->cols[nbg]);
     gdk_draw_rectangle(inst->pixmap, gc, 1,
                       x*inst->font_width+cfg.window_border,
@@ -1048,6 +1096,44 @@ void do_text(Context ctx, int x, int y, char *text, int len,
                      y*inst->font_height + uheight + cfg.window_border);
     }
 
+    if (lattr != LATTR_NORM) {
+       /*
+        * I can't find any plausible StretchBlt equivalent in the
+        * X server, so I'm going to do this the slow and painful
+        * way. This will involve repeated calls to
+        * gdk_draw_pixmap() to stretch the text horizontally. It's
+        * O(N^2) in time and O(N) in network bandwidth, but you
+        * try thinking of a better way. :-(
+        */
+       int i;
+       for (i = 0; i < len * inst->font_width; i++) {
+           gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
+                           x*inst->font_width+cfg.window_border + 2*i,
+                           y*inst->font_height+cfg.window_border,
+                           x*inst->font_width+cfg.window_border + 2*i+1,
+                           y*inst->font_height+cfg.window_border,
+                           len * inst->font_width - i, inst->font_height);
+       }
+       len *= 2;
+       if (lattr != LATTR_WIDE) {
+           int dt, db;
+           /* Now stretch vertically, in the same way. */
+           if (lattr == LATTR_BOT)
+               dt = 0, db = 1;
+           else
+               dt = 1, db = 0;
+           for (i = 0; i < inst->font_height; i+=2) {
+               gdk_draw_pixmap(inst->pixmap, gc, inst->pixmap,
+                               x*inst->font_width+cfg.window_border,
+                               y*inst->font_height+cfg.window_border+dt*i+db,
+                               x*inst->font_width+cfg.window_border,
+                               y*inst->font_height+cfg.window_border+dt*(i+1),
+                               len * inst->font_width, inst->font_height-i-1);
+           }
+       }
+       len *= 2;
+    }
+
     gdk_draw_pixmap(inst->area->window, gc, inst->pixmap,
                    x*inst->font_width+cfg.window_border,
                    y*inst->font_height+cfg.window_border,
@@ -1189,6 +1275,11 @@ void modalfatalbox(char *p, ...)
     exit(1);
 }
 
+char *get_x_display(void)
+{
+    return gdk_get_display();
+}
+
 int main(int argc, char **argv)
 {
     extern int pty_master_fd;         /* declared in pty.c */
@@ -1227,9 +1318,23 @@ int main(int argc, char **argv)
            } 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")) {
            cfg.hide_mouseptr = 1;
        }
+       if (!strcmp(p, "-ut-")) {
+           cfg.stamp_utmp = 0;
+       }
+       if (!strcmp(p, "-ls-")) {
+           cfg.login_shell = 0;
+       }
        if (!strcmp(p, "-nethack")) {
            cfg.nethack_keypad = 1;
        }
@@ -1244,9 +1349,6 @@ int main(int argc, char **argv)
 
     init_ucs();
 
-    back = &pty_backend;
-    back->init(NULL, 0, NULL, 0);
-
     inst->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
     if (cfg.wintitle[0])
@@ -1309,9 +1411,7 @@ int main(int argc, char **argv)
                       GTK_SIGNAL_FUNC(selection_clear), inst);
     gtk_signal_connect(GTK_OBJECT(inst->sbar_adjust), "value_changed",
                       GTK_SIGNAL_FUNC(scrollbar_moved), inst);
-    gtk_idle_add(idle_func, 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 |
                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
@@ -1329,6 +1429,11 @@ int main(int argc, char **argv)
     inst->currcursor = inst->textcursor;
     show_mouseptr(1);
 
+    back = &pty_backend;
+    back->init(NULL, 0, NULL, 0);
+
+    gdk_input_add(pty_master_fd, GDK_INPUT_READ, pty_input_func, inst);
+
     term_init();
     term_size(24, 80, 2000);