[PATCH] Clean xterm title restore (was: xterm_title Test Fix (delay removed))



On Sun, Jan 19, 2003 at 09:42:52AM -0500, Pavel Roskin wrote:
> > I could implement this if you think it's good.
> 
> Yes.

I looked into vim code as Thomas suggested, but the whole xterm title
thing was really huge and messy, about few hundreds of code. I've
decided to do it the simple way. Used XFetchName instead of XGetWMName
(the later returns property, requires additional decoding) and dropped
some other hacks - they could be necessary in (imho) 0.01% of systems,
and xterm title in fact is not so critical, so it is not worth
bloating the code (imo).

Patch attached and sent to the BTS (update for #898). Feel free to
send any comments, I don't have any experience with xlib programming.

Regards

PS. Pavel, could you also apply #954, it is simple yet important.

-- 

  _.|._ |_  _.    : Adam Byrtek, alpha@(irc.pl|debian.org)
 (_|||_)| |(_|    : gg 1802819, pgp 0xB25952C0
     |            : jid alpha.pl(at)jabber.org
Index: main.c
===================================================================
RCS file: /cvs/gnome/mc/src/main.c,v
retrieving revision 1.272
diff -u -p -r1.272 main.c
--- main.c	27 Dec 2002 23:23:34 -0000	1.272
+++ main.c	19 Jan 2003 21:50:28 -0000
@@ -41,6 +41,10 @@
 #include <ctype.h>
 #include <signal.h>
 
+#ifdef HAVE_TEXTMODE_X11_SUPPORT
+#   include <X11/Xlib.h>
+#endif
+
 /* Program include files */
 #include "global.h"
 #include "tty.h"
@@ -1544,7 +1549,72 @@ setup_pre (void)
 #endif
 }
 
+/* Connect to X11 display, fetch and return current window title */
+static char *
+fetch_xterm_title()
+{
+#ifndef HAVE_TEXTMODE_X11_SUPPORT
+    return NULL;
+#else
+    Window x11_window = 0;
+    Display *x11_display = NULL;
+    static char title[256];
+    char *buf, *winid;
+    int status;
+    
+    if ((winid = getenv("WINDOWID")) == NULL)
+	return NULL;
+    if ((x11_window = (Window)atol(winid)) == 0)
+	return NULL;
+    /* TODO: should we set an alarm for tcp timeouts? */
+    if ((x11_display = XOpenDisplay(NULL)) == NULL)
+	return NULL;
+    
+    status = XFetchName(x11_display, x11_window, &buf);
+    XCloseDisplay(x11_display);
+
+    if (status && buf != NULL) {
+	strncpy(title, buf, 256);
+	title[255] = '\0';
+	XFree((void *) buf);
+    } else {
+	return NULL;
+    }
+    
+    return title;
+#endif
+}
+
+/* Use xterm escape sequence to set xterm window title */
 static void
+set_xterm_title (char *title)
+{
+    if (title != NULL) {
+	printf("\e]0;%s\a", title);
+	fflush(stdout);
+    }
+}
+
+/* Update current directory in the xterm title
+ * (invoked on cd, execute, panel refresh...) */
+void
+update_xterm_title_path (void)
+{
+    char *p, *s;
+
+    if (xterm_flag && xterm_title) {
+	p = s = g_strdup_printf ("mc - %s", strip_home_and_password (cpanel->cwd));
+	do {
+	    if (*s < 32)
+		*s = '?';
+	} while (*++s);
+
+	set_xterm_title (p);
+	g_free (p);
+    }
+}
+
+static char *
 init_xterm_support (void)
 {
     char *termvalue;
@@ -1591,6 +1661,18 @@ init_xterm_support (void)
 	    use_mouse_p = MOUSE_XTERM;
 	}
     }
+    
+    /* remember current window title */
+    if (xterm_flag)
+        return fetch_xterm_title();
+    else 
+	return NULL;
+}
+
+static void
+shutdown_xterm_support (char *old_title)
+{
+    set_xterm_title(old_title);
 }
 
 static void
@@ -1832,24 +1914,6 @@ midnight_callback (struct Dlg_head *h, i
 
 #define xtoolkit_panel_setup()
 
-/* Show current directory in the xterm title */
-void
-update_xterm_title_path (void)
-{
-    unsigned char *p, *s;
-
-    if (xterm_flag && xterm_title) {
-	p = s = g_strdup (strip_home_and_password (cpanel->cwd));
-	do {
-	    if (*s <= 32)
-		*s = '?';
-	} while (*++s);
-	fprintf (stdout, "\33]0;mc - %s\7", p);
-	fflush (stdout);
-	g_free (p);
-    }
-}
-
 /* Load new hint and display it. */
 void
 load_hint (void)
@@ -2408,6 +2472,7 @@ main (int argc, char *argv[])
 {
     /* if on, it displays the information that files have been moved to ~/.mc */
     int show_change_notice = 0;
+    char *old_xterm_title = NULL;
 
     /* We had LC_CTYPE before, LC_ALL includs LC_TYPE as well */
     setlocale (LC_ALL, "");
@@ -2474,7 +2539,7 @@ main (int argc, char *argv[])
 
     init_curses ();
 
-    init_xterm_support ();
+    old_xterm_title = init_xterm_support ();
 
 #ifdef HAVE_SUBSHELL_SUPPORT
 
@@ -2534,6 +2599,9 @@ main (int argc, char *argv[])
     if (alternate_plus_minus)
 	numeric_keypad_mode ();
 
+    if (xterm_flag)
+	shutdown_xterm_support (old_xterm_title);
+    
 #ifndef NATIVE_WIN32
     signal (SIGCHLD, SIG_DFL);	/* Disable the SIGCHLD handler */
 #endif
--- src/ChangeLog	27 Dec 2002 17:52:24 -0000	1.1088
+++ src/ChangeLog	29 Dec 2002 13:03:09 -0000
@@ -0,0 +1,7 @@
+2003-01-19  Adam Byrtek  <alpha debian org>
+
+	* main.c (set_xterm_title, fetch_xterm_title): New
+	functions. (init_xterm_support): Old xterm window title stored.
+	(shutdown_xterm_support):  Window title restored at clean exit. 
+	Some cleanups.
+


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