Support for non-ISO-8859-1 X keysyms. So in particular, pterm in a
[u/mdw/putty] / unix / pterm.c
index 00c33ef..cf52ec3 100644 (file)
@@ -145,6 +145,8 @@ int platform_default_i(const char *name, int def)
 {
     if (!strcmp(name, "CloseOnExit"))
        return 2;  /* maps to FORCE_ON after painful rearrangement :-( */
+    if (!strcmp(name, "WinNameAlways"))
+       return 0;  /* X natively supports icon titles, so use 'em by default */
     return def;
 }
 
@@ -438,10 +440,12 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
 {
     struct gui_data *inst = (struct gui_data *)data;
     char output[32];
-    int start, end, special;
+    wchar_t ucsoutput[2];
+    int ucsval, start, end, special, use_ucsoutput;
 
     /* By default, nothing is generated. */
     end = start = 0;
+    special = use_ucsoutput = FALSE;
 
     /*
      * If Alt is being released after typing an Alt+numberpad
@@ -563,12 +567,21 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
        }
 
        special = FALSE;
+       use_ucsoutput = FALSE;
 
        /* ALT+things gives leading Escape. */
        output[0] = '\033';
        strncpy(output+1, event->string, 31);
-       output[31] = '\0';
-       end = strlen(output);
+       if (!*event->string &&
+           (ucsval = keysym_to_unicode(event->keyval)) >= 0) {
+           ucsoutput[0] = '\033';
+           ucsoutput[1] = ucsval;
+           use_ucsoutput = TRUE;
+           end = 2;
+       } else {
+           output[31] = '\0';
+           end = strlen(output);
+       }
        if (event->state & GDK_MOD1_MASK) {
            start = 0;
            if (end == 1) end = 0;
@@ -579,6 +592,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
        if (!event->string[0] && event->keyval == '`' &&
            (event->state & GDK_CONTROL_MASK)) {
            output[1] = '\x1C';
+           use_ucsoutput = FALSE;
            end = 2;
        }
 
@@ -586,6 +600,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
        if (event->keyval == GDK_Break &&
            (event->state & GDK_CONTROL_MASK)) {
            output[1] = '\003';
+           use_ucsoutput = FALSE;
            end = 2;
            special = TRUE;
        }
@@ -594,6 +609,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
         * special to ldisc. */
        if (event->keyval == GDK_Return) {
            output[1] = '\015';
+           use_ucsoutput = FALSE;
            end = 2;
            special = TRUE;
        }
@@ -605,6 +621,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
            (event->state & (GDK_SHIFT_MASK |
                             GDK_CONTROL_MASK)) == GDK_CONTROL_MASK) {
            output[1] = '\0';
+           use_ucsoutput = FALSE;
            end = 2;
        }
 
@@ -613,6 +630,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
            (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) ==
            (GDK_SHIFT_MASK | GDK_CONTROL_MASK)) {
            output[1] = '\240';
+           use_ucsoutput = FALSE;
            end = 2;
        }
 
@@ -620,6 +638,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
        if (event->keyval == GDK_BackSpace &&
            !(event->state & GDK_SHIFT_MASK)) {
            output[1] = inst->cfg.bksp_is_delete ? '\x7F' : '\x08';
+           use_ucsoutput = FALSE;
            end = 2;
            special = TRUE;
        }
@@ -627,6 +646,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
        if (event->keyval == GDK_BackSpace &&
            (event->state & GDK_SHIFT_MASK)) {
            output[1] = inst->cfg.bksp_is_delete ? '\x08' : '\x7F';
+           use_ucsoutput = FALSE;
            end = 2;
            special = TRUE;
        }
@@ -635,6 +655,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
        if (event->keyval == GDK_ISO_Left_Tab ||
            (event->keyval == GDK_Tab && (event->state & GDK_SHIFT_MASK))) {
            end = 1 + sprintf(output+1, "\033[Z");
+           use_ucsoutput = FALSE;
        }
 
        /*
@@ -659,6 +680,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
                    output[1] = keys[1];
                else
                    output[1] = keys[0];
+               use_ucsoutput = FALSE;
                goto done;
            }
        }
@@ -711,6 +733,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
                        end = 1 + sprintf(output+1, "\033?%c", xkey);
                } else
                    end = 1 + sprintf(output+1, "\033O%c", xkey);
+               use_ucsoutput = FALSE;
                goto done;
            }
        }
@@ -814,6 +837,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
 
            if (inst->term->vt52_mode && code > 0 && code <= 6) {
                end = 1 + sprintf(output+1, "\x1B%c", " HLMEIG"[code]);
+               use_ucsoutput = FALSE;
                goto done;
            }
 
@@ -838,6 +862,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
                if (event->state & GDK_SHIFT_MASK) index += 12;
                if (event->state & GDK_CONTROL_MASK) index += 24;
                end = 1 + sprintf(output+1, "\x1B[%c", codes[index]);
+               use_ucsoutput = FALSE;
                goto done;
            }
            if (inst->cfg.funky_type == 5 &&     /* SCO small keypad */
@@ -849,6 +874,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
                } else {
                    end = 1 + sprintf(output+1, "\x1B[%c", codes[code-1]);
                }
+               use_ucsoutput = FALSE;
                goto done;
            }
            if ((inst->term->vt52_mode || inst->cfg.funky_type == 4) &&
@@ -864,10 +890,12 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
                else
                    end = 1 + sprintf(output+1,
                                      "\x1BO%c", code + 'P' - 11 - offt);
+               use_ucsoutput = FALSE;
                goto done;
            }
            if (inst->cfg.funky_type == 1 && code >= 11 && code <= 15) {
                end = 1 + sprintf(output+1, "\x1B[[%c", code + 'A' - 11);
+               use_ucsoutput = FALSE;
                goto done;
            }
            if (inst->cfg.funky_type == 2 && code >= 11 && code <= 14) {
@@ -875,14 +903,17 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
                    end = 1 + sprintf(output+1, "\x1B%c", code + 'P' - 11);
                else
                    end = 1 + sprintf(output+1, "\x1BO%c", code + 'P' - 11);
+               use_ucsoutput = FALSE;
                goto done;
            }
            if (inst->cfg.rxvt_homeend && (code == 1 || code == 4)) {
                end = 1 + sprintf(output+1, code == 1 ? "\x1B[H" : "\x1BOw");
+               use_ucsoutput = FALSE;
                goto done;
            }
            if (code) {
                end = 1 + sprintf(output+1, "\x1B[%d~", code);
+               use_ucsoutput = FALSE;
                goto done;
            }
        }
@@ -917,6 +948,7 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
                } else {                    
                    end = 1 + sprintf(output+1, "\033[%c", xkey);
                }
+               use_ucsoutput = FALSE;
                goto done;
            }
        }
@@ -942,19 +974,28 @@ gint key_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
            output[end] = '\0';        /* NUL-terminate */
            ldisc_send(inst->ldisc, output+start, -2, 1);
        } else if (!inst->direct_to_font) {
-           /*
-            * The stuff we've just generated is assumed to be
-            * ISO-8859-1! This sounds insane, but `man
-            * XLookupString' agrees: strings of this type returned
-            * from the X server are hardcoded to 8859-1. Strictly
-            * speaking we should be doing this using some sort of
-            * GtkIMContext, which (if we're lucky) would give us
-            * our data directly in Unicode; but that's not
-            * supported in GTK 1.2 as far as I can tell, and it's
-            * poorly documented even in 2.0, so it'll have to
-            * wait.
-            */
-           lpage_send(inst->ldisc, CS_ISO8859_1, output+start, end-start, 1);
+           if (!use_ucsoutput) {
+               /*
+                * The stuff we've just generated is assumed to be
+                * ISO-8859-1! This sounds insane, but `man
+                * XLookupString' agrees: strings of this type
+                * returned from the X server are hardcoded to
+                * 8859-1. Strictly speaking we should be doing
+                * this using some sort of GtkIMContext, which (if
+                * we're lucky) would give us our data directly in
+                * Unicode; but that's not supported in GTK 1.2 as
+                * far as I can tell, and it's poorly documented
+                * even in 2.0, so it'll have to wait.
+                */
+               lpage_send(inst->ldisc, CS_ISO8859_1, output+start,
+                          end-start, 1);
+           } else {
+               /*
+                * We generated our own Unicode key data from the
+                * keysym, so use that instead.
+                */
+               luni_send(inst->ldisc, ucsoutput+start, end-start, 1);
+           }
        } else {
            /*
             * In direct-to-font mode, we just send the string
@@ -1464,12 +1505,24 @@ void get_clip(void *frontend, wchar_t ** p, int *len)
     }
 }
 
+static void set_window_titles(struct gui_data *inst)
+{
+    /*
+     * We must always call set_icon_name after calling set_title,
+     * since set_title will write both names. Irritating, but such
+     * is life.
+     */
+    gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
+    if (!inst->cfg.win_name_always)
+       gdk_window_set_icon_name(inst->window->window, inst->icontitle);
+}
+
 void set_title(void *frontend, char *title)
 {
     struct gui_data *inst = (struct gui_data *)frontend;
     strncpy(inst->wintitle, title, lenof(inst->wintitle));
     inst->wintitle[lenof(inst->wintitle)-1] = '\0';
-    gtk_window_set_title(GTK_WINDOW(inst->window), inst->wintitle);
+    set_window_titles(inst);
 }
 
 void set_icon(void *frontend, char *title)
@@ -1477,7 +1530,7 @@ void set_icon(void *frontend, char *title)
     struct gui_data *inst = (struct gui_data *)frontend;
     strncpy(inst->icontitle, title, lenof(inst->icontitle));
     inst->icontitle[lenof(inst->icontitle)-1] = '\0';
-    gdk_window_set_icon_name(inst->window->window, inst->icontitle);
+    set_window_titles(inst);
 }
 
 void set_sbar(void *frontend, int total, int start, int page)
@@ -2379,7 +2432,8 @@ void setup_fonts_ucs(struct gui_data *inst)
     inst->font_height = inst->fonts[0]->ascent + inst->fonts[0]->descent;
 
     inst->direct_to_font = init_ucs(&inst->ucsdata,
-                                   inst->cfg.line_codepage, font_charset);
+                                   inst->cfg.line_codepage, font_charset,
+                                   inst->cfg.vtmode);
 }
 
 void set_geom_hints(struct gui_data *inst)
@@ -2506,6 +2560,7 @@ void change_settings_menuitem(GtkMenuItem *item, gpointer data)
          */
         if (strcmp(oldcfg.wintitle, cfg2.wintitle))
             set_title(inst, cfg2.wintitle);
+       set_window_titles(inst);
 
         /*
          * Redo the whole tangled fonts and Unicode mess if
@@ -2515,7 +2570,8 @@ void change_settings_menuitem(GtkMenuItem *item, gpointer data)
             strcmp(oldcfg.boldfont.name, cfg2.boldfont.name) ||
             strcmp(oldcfg.widefont.name, cfg2.widefont.name) ||
             strcmp(oldcfg.wideboldfont.name, cfg2.wideboldfont.name) ||
-            strcmp(oldcfg.line_codepage, cfg2.line_codepage)) {
+            strcmp(oldcfg.line_codepage, cfg2.line_codepage) ||
+           oldcfg.vtmode != cfg2.vtmode) {
             setup_fonts_ucs(inst);
             need_size = 1;
         } else
@@ -2669,7 +2725,7 @@ void dup_session_menuitem(GtkMenuItem *item, gpointer gdata)
     fork_and_exec_self(inst, pipefd[1], option, NULL);
     close(pipefd[0]);
 
-    i = 0;
+    i = ret = 0;
     while (i < size && (ret = write(pipefd[1], data + i, size - i)) > 0)
        i += ret;
     if (ret < 0)
@@ -2689,7 +2745,7 @@ int read_dupsession_data(struct gui_data *inst, Config *cfg, char *arg)
     }
 
     data = snewn(size, char);
-    i = 0;
+    i = ret = 0;
     while (i < size && (ret = read(fd, data + i, size - i)) > 0)
        i += ret;
     if (ret < 0) {
@@ -2802,12 +2858,6 @@ int pt_main(int argc, char **argv)
      * it */
     block_signal(SIGCHLD, 1);
 
-    /*
-     * SIGPIPE is not something we want to see terminating the
-     * process.
-     */
-    block_signal(SIGPIPE, 1);
-
     inst->progname = argv[0];
     /*
      * Copy the original argv before letting gtk_init fiddle with
@@ -3040,11 +3090,13 @@ int pt_main(int argc, char **argv)
            return 0;
        }
 
-        if (inst->cfg.wintitle[0])
+        if (inst->cfg.wintitle[0]) {
             set_title(inst, inst->cfg.wintitle);
-        else {
+            set_icon(inst, inst->cfg.wintitle);
+       } else {
             char *title = make_default_wintitle(realhost);
             set_title(inst, title);
+            set_icon(inst, title);
             sfree(title);
         }
     }
@@ -3061,6 +3113,15 @@ int pt_main(int argc, char **argv)
      * called */
     block_signal(SIGCHLD, 0);
 
+    /*
+     * Block SIGPIPE: if we attempt Duplicate Session or similar
+     * and it falls over in some way, we certainly don't want
+     * SIGPIPE terminating the main pterm/PuTTY. Note that we do
+     * this _after_ (at least pterm) forks off its child process,
+     * since the child wants SIGPIPE handled in the usual way.
+     */
+    block_signal(SIGPIPE, 1);
+
     inst->exited = FALSE;
 
     gtk_main();