Re: xterm_title patch 898



* Tribhuvan <loka rcn com> [Sat, 04 Jan 2003]:
> Adam Byrtek 'alpha' wrote:
> >It was recently discussed on this list, and I've made a patch to store
> >and restore old title:
> I inserted the code as per above patch on 4.6.pre2 and tested:
> rxvt      - ok on localhost: no-restore on remote host
> gnome-terminal - same as rxvt
> dtterm - title changes but old_xterm_title goes to stdout (comand line 
> in mc) and causes unpredictable bhvr
>                this is from the store_xterm_title function (this is on 
> localhost, so didn't bother testing on remote)
> xterm  - ok on localhost but same problem as dtterm when xterm on remote 
> host
> 
> so I see two difficulties:
> 1) get same behavior on remote host as localhost
> 2) stop the old_xterm_title from printing to the command line in dtterm 
> (localhost) & xterm (remote)

In my humble opinion the restore does not work over network
because the implementation is fundamentally broken.

See:

printf ("\e[21t");
fflush (stdout);

nodelay (stdscr, TRUE);
usleep(1000);
while (i < 255 && (c=getch()) != -1) {
    title[i++] = c;
}
nodelay (stdscr, FALSE);

If the response of the terminal, which must travel over
network, is not available in mere 1 milisecond, then the code 
fails. getch() in nonblocking mode immediately returns -1.

I don't like this approach at all.

Please try the patch attached to this message and report whether
the remote host problem went away.

But please note that the patch is an experimental version and may
not compile on your platform at all.

Thank you.

-- 
Tomas Styblo <tripie cpan org>
PGP: http://pgp.mit.edu:11371/pks/lookup?op=get&search=0xC97EA4B6
diff -purN mc-4.6.0-pre2/src/main.c mc-4.6.0-pre2.new/src/main.c
--- mc-4.6.0-pre2/src/main.c	Thu Dec 26 17:38:37 2002
+++ mc-4.6.0-pre2.new/src/main.c	Sun Jan  5 05:23:15 2003
@@ -2406,11 +2406,86 @@ compatibility_move_mc_files (void)
 }
 #endif				/* NATIVE_WIN32 */
 
+static void 
+xterm_sig_alrm_h (int sig) {
+    /* Do nothing.
+     * The only purpose of this signal is to interrupt the 
+     * read system call in xterm_read_title(). */
+}
+
+static char *
+store_xterm_title (void)
+{
+    static char title[256];
+    struct sigaction sa;
+    int cnt;
+    char *cmd = "\033[21t";
+    int e;
+    int tty;
+    
+    if ((tty = open("/dev/tty", O_RDWR | O_NOCTTY)) == -1) {
+        return (NULL);
+    }
+    /* Make sure we will not be blocked infinitely. */
+    sa.sa_handler = xterm_sig_alrm_h;
+    sa.sa_flags = SA_INTERRUPT | SA_RESETHAND;
+    sigaction(SIGALRM, &sa, NULL);
+    alarm(5);
+
+    /* Write the request for window title. */
+    e = strlen(cmd); 
+    while ((cnt = write(tty, cmd, e)) > 0) {
+        e -= cnt;
+        cmd += cnt;
+    }
+    
+    if (cnt == -1) {
+        close(tty);
+        return (NULL);
+    }
+    else {
+        /* Read the response. */
+        int i = 0;
+        char ch;
+        int seen_esc = 0;
+        while (i < sizeof(title) && read(tty, &ch, 1) > 0) {
+            if (seen_esc == 0) {
+                if (ch == '\033')
+                    seen_esc = 1;
+                else
+                    continue;
+            }
+            if (ch == '\x9c')  /* ST */
+                break;
+            title[i++] = ch;
+        }
+        close(tty);
+        if (i < 4) {
+            return (NULL);
+        }
+        else {
+            title[i] = '\0';
+            return (title + 3); /* OSC l */
+        }
+    }
+}
+
+
+static void
+restore_xterm_title (const unsigned char *title)
+{
+    if (title != NULL) {
+	    printf("\e]0;%s\a", title);
+	    fflush(stdout);
+    }
+}	
+
 int
 main (int argc, char *argv[])
 {
     /* if on, it displays the information that files have been moved to ~/.mc */
     int show_change_notice = 0;
+    unsigned char *old_xterm_title = NULL;
 
     /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
     setlocale (LC_ALL, "");
@@ -2513,9 +2588,17 @@ main (int argc, char *argv[])
 #endif				/* HAVE_SUBSHELL_SUPPORT */
 	prompt = (geteuid () == 0) ? "# " : "$ ";
 
+    /* Must be done after slang_init, init_curses, init_xterm_support */
+    if (xterm_flag)
+        old_xterm_title = store_xterm_title();
+
     /* Program main loop */
     do_nc ();
 
+    if (xterm_flag && xterm_title && old_xterm_title)
+        restore_xterm_title(old_xterm_title);
+   
+
     /* Save the tree store */
     tree_store_save ();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]