pterm's manpage now documents the NoRemoteQTitle resource. Should
[u/mdw/putty] / unix / pterm.c
index 3bb9f6b..aba3ecb 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;
 }
 
@@ -164,7 +166,7 @@ int from_backend(void *frontend, int is_stderr, const char *data, int len)
     return term_data(inst->term, is_stderr, data, len);
 }
 
-void logevent(void *frontend, char *string)
+void logevent(void *frontend, const char *string)
 {
     struct gui_data *inst = (struct gui_data *)frontend;
 
@@ -342,7 +344,7 @@ void get_window_pixels(void *frontend, int *x, int *y)
 char *get_window_title(void *frontend, int icon)
 {
     struct gui_data *inst = (struct gui_data *)frontend;
-    return icon ? inst->wintitle : inst->icontitle;
+    return icon ? inst->icontitle : inst->wintitle;
 }
 
 gint delete_window(GtkWidget *widget, GdkEvent *event, gpointer data)
@@ -438,11 +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 = FALSE;
+    special = use_ucsoutput = FALSE;
 
     /*
      * If Alt is being released after typing an Alt+numberpad
@@ -564,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;
@@ -580,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;
        }
 
@@ -587,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;
        }
@@ -595,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;
        }
@@ -606,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;
        }
 
@@ -614,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;
        }
 
@@ -621,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;
        }
@@ -628,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;
        }
@@ -636,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;
        }
 
        /*
@@ -660,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;
            }
        }
@@ -712,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;
            }
        }
@@ -815,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;
            }
 
@@ -839,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 */
@@ -850,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) &&
@@ -865,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) {
@@ -876,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;
            }
        }
@@ -918,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;
            }
        }
@@ -943,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
@@ -1465,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)
@@ -1478,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)
@@ -2064,6 +2116,17 @@ int do_cmdline(int argc, char **argv, int do_everything,
        char *p = *++argv;
         int ret;
 
+       /*
+        * Shameless cheating. Debian requires all X terminal
+        * emulators to support `-T title'; but
+        * cmdline_process_param will eat -T (it means no-pty) and
+        * complain that pterm doesn't support it. So, in pterm
+        * only, we convert -T into -title.
+        */
+       if ((cmdline_tooltype & TOOLTYPE_NONNETWORK) &&
+           !strcmp(p, "-T"))
+           p = "-title";
+
         ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
                                     do_everything ? 1 : -1, cfg);
 
@@ -2380,7 +2443,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)
@@ -2421,7 +2485,8 @@ void copy_all_menuitem(GtkMenuItem *item, gpointer data)
 void special_menuitem(GtkMenuItem *item, gpointer data)
 {
     struct gui_data *inst = (struct gui_data *)data;
-    int code = (int)gtk_object_get_data(GTK_OBJECT(item), "user-data");
+    int code = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
+                                                  "user-data"));
 
     inst->back->special(inst->backhandle, code);
 }
@@ -2507,6 +2572,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
@@ -2516,7 +2582,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
@@ -2770,7 +2837,7 @@ void update_specials_menu(void *frontend)
            if (*specials[i].name) {
                menuitem = gtk_menu_item_new_with_label(specials[i].name);
                gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
-                                   (gpointer)specials[i].code);
+                                   GINT_TO_POINTER(specials[i].code));
                gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
                                   GTK_SIGNAL_FUNC(special_menuitem), inst);
            } else
@@ -2803,12 +2870,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
@@ -3026,7 +3087,8 @@ int pt_main(int argc, char **argv)
 
     inst->back = select_backend(&inst->cfg);
     {
-       char *realhost, *error;
+       char *realhost;
+       const char *error;
 
        error = inst->back->init((void *)inst, &inst->backhandle,
                                  &inst->cfg, inst->cfg.host, inst->cfg.port,
@@ -3041,11 +3103,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);
         }
     }
@@ -3062,6 +3126,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();