detection of xterm mouse support

Oskar Liljeblad oskar at osk.mine.nu
Thu Dec 27 10:39:44 UTC 2001


On Friday, December 21, 2001 at 19:44, Pavel Roskin wrote:

Hi again,

> > The correct way to determine _xterm_ (not generic) mouse support
> > is to check the "kmous" terminfo ("Km" termcap) capability.
> 
> The current code is significantly different with respect to the mouse 
> compared to mc-4.5.55.  You can find the latest snapshot at 
> http://www.ibiblio.org/pub/Linux/utils/file/managers/mc/snapshots/
> 
> MC checks those capabilities now.  However, it still enables mouse support
> only for known terminals.  The problem is that the Linux console also has
> mouse capabilities, but we are using the gpm library instead.
> 
> I see the following solutions how to make it right:
> 
> 1) Get rid of gpm library and enable xterms-tyle events on the Linux
> console.  This would simplify things greatly and it's probably the best
> solution, but it would require some time cleaning gpm from all
> configuration files and may not work for everybody.
> 
> 2) Change the order of initialization and enable xterm-style mouse if the
> capability is found and gpm mouse has not been found.
> 
> 3) Enable xterm-style mouse if the capability is found and the terminal
> name doesn't beging with "linux".  That's the cheapest hack.

The patch below fixes so that gpm and xterm mouse support is
detected correctly. This is done in a way similar to ncurses
(solution 2 above). I have rewritten init_mouse in src/mouse.c
so that we first check for Gpm mouse support the right way
(call Gpm_Open, and keep it open if successful). If it fails,
xterm mouse support is detected by comparing the "kmous"
capability with "\033[M" as I described in my original mail.
If too fails, mouse support is disabled.

I have tried the patch in the following conditions:

  1. rxvt with TERM=xterm - got mouse
  2. rxvt with TERM=xterm-color - got mouse
  3. rxvt with TERM=Eterm (has kmous) - got mouse
  4. linux console with TERM=linux - got mouse
  5. linux console with TERM=ansi - got mouse
  6. eterm with TERM=Eterm - got mouse

Please let me know what you think about the patch.

Regards,

Oskar Liljeblad (oskar at osk.mine.nu)
-------------- next part --------------
diff -ru mc-4.5.99a/src/main.c mc-4.5.99a-oskar/src/main.c
--- mc-4.5.99a/src/main.c	Fri Nov 30 16:05:23 2001
+++ mc-4.5.99a-oskar/src/main.c	Thu Dec 27 11:11:35 2001
@@ -1720,9 +1720,6 @@
 init_xterm_support (void)
 {
     char   *termvalue;
-#ifdef HAVE_SLANG
-    char   *term_entry;
-#endif
 	
     termvalue = getenv ("TERM");
     if (!termvalue || !(*termvalue)){
@@ -1730,39 +1727,19 @@
 	exit (1);
     }
 
-    /* Check mouse capabilities */
-#ifdef HAVE_SLANG
-    term_entry = SLtt_tigetent (termvalue);
-    xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
-#else
-    xmouse_seq = tigetstr ("kmous");
-#endif
-
-     /* -1 means invalid capability, shouldn't happen, but let's make it 0 */
-    if ((int) xmouse_seq == -1) {
-	xmouse_seq = NULL;
-    }
-
     if (force_xterm
 	    || strncmp (termvalue, "xterm", 5) == 0
 	    || strncmp (termvalue, "rxvt", 4) == 0
 	    || strcmp (termvalue, "dtterm") == 0) {
+	/* Eventually this variable should be removed */
 	xterm_flag = 1;
 
-	/* Default to the standard xterm sequence */
-	if (!xmouse_seq) {
-	    xmouse_seq = ESC_STR "[M";
-	}
-
-	/* Enable mouse unless explicitly disabled by --nomouse */
-	if (use_mouse_p != MOUSE_DISABLED) {
-	    use_mouse_p = MOUSE_XTERM;
-	}
-
 #if 0 /* It works on xterm, but not on rxvt */
 	printf (ESC_STR "]0;GNU Midnight Commander\7");
 #endif
     }
+   
+    /* We will initialize mouse in setup_mc */
 }
 
 static void setup_mc (void)
@@ -1809,6 +1786,8 @@
 
 static void done_mc (void)
 {
+    free_mouse ();
+
     done_menu ();
     
     /* Setup shutdown
diff -ru mc-4.5.99a/src/mouse.c mc-4.5.99a-oskar/src/mouse.c
--- mc-4.5.99a/src/mouse.c	Wed Nov 14 19:27:45 2001
+++ mc-4.5.99a-oskar/src/mouse.c	Thu Dec 27 11:37:28 2001
@@ -34,7 +34,6 @@
 #include "key.h"		/* define sequence */
 
 int mouse_enabled = 0;
-char *xmouse_seq;
 
 #ifdef HAVE_LIBGPM
 void show_mouse_pointer (int x, int y)
@@ -47,19 +46,70 @@
 
 void init_mouse (void)
 {
-    switch (use_mouse_p) {
 #ifdef HAVE_LIBGPM
-    case MOUSE_NONE:
+    static Gpm_Connect conn;
+    int mouse_d;
+#endif
+    char   *termvalue;
+#ifdef HAVE_SLANG
+    char   *term_entry;
+#endif
+    char *xmouse_seq;
+
+    if (use_mouse_p != MOUSE_NONE)
+    	return;
+
+#ifdef HAVE_LIBGPM
+    conn.eventMask   = ~GPM_MOVE;
+    conn.defaultMask = GPM_MOVE;
+    conn.minMod      = 0;
+    conn.maxMod      = 0;
+
+    mouse_d = Gpm_Open (&conn, 0);
+    if (mouse_d >= 0) {
 	use_mouse_p = MOUSE_GPM;
-	break;
-#endif /* HAVE_LIBGPM */
-    case MOUSE_XTERM:
+	enable_mouse ();
+	return;
+    }
+#endif
+
+    termvalue = getenv ("TERM");
+    if (!termvalue || !(*termvalue)){
+	fprintf (stderr, _("The TERM environment variable is unset!\n"));
+	exit (1);
+    }
+
+#ifdef HAVE_SLANG
+    term_entry = SLtt_tigetent (termvalue);
+    xmouse_seq = SLtt_tigetstr ("Km", &term_entry);
+#else
+    xmouse_seq = tigetstr ("kmous");
+#endif
+    if (xmouse_seq != NULL
+    	    && (int) xmouse_seq != -1
+    	    && strcmp(xmouse_seq, "\033[M") == 0) {
+    	use_mouse_p = MOUSE_XTERM;
 	define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
+	enable_mouse ();
+	return;
+    }
+
+    use_mouse_p = MOUSE_NONE;
+}
+
+void free_mouse (void)
+{
+    disable_mouse ();
+
+    switch (use_mouse_p) {
+#ifdef HAVE_LIBGPM
+    case MOUSE_GPM:
+    	Gpm_Close ();
 	break;
+#endif /* HAVE_LIBGPM */
     default:
 	break;
     }
-    enable_mouse ();
 }
 
 void enable_mouse (void)
@@ -68,25 +118,12 @@
 	return;
     }
 
+    mouse_enabled = 1; 
+
     switch (use_mouse_p) {
 #ifdef HAVE_LIBGPM
     case MOUSE_GPM:
-	{
-	    int mouse_d;
-	    Gpm_Connect conn;
-
-	    conn.eventMask   = ~GPM_MOVE;
-	    conn.defaultMask = GPM_MOVE;
-	    conn.minMod      = 0;
-	    conn.maxMod      = 0;
-
-	    mouse_d = Gpm_Open (&conn, 0);
-	    if (mouse_d == -1) {
-		use_mouse_p = MOUSE_NONE;
-	        return;
-	    }
-	    mouse_enabled = 1;
-	}
+    	/* already enabled by setting mouse_enabled */
 	break;
 #endif /* HAVE_LIBGPM */
     case MOUSE_XTERM:
@@ -97,7 +134,6 @@
 	printf(ESC_STR "[?1000h");
 
 	fflush (stdout);
-	mouse_enabled = 1; 
 	break;
     default:
 	break;
@@ -115,7 +151,7 @@
     switch (use_mouse_p) {
 #ifdef HAVE_LIBGPM
     case MOUSE_GPM:
-	Gpm_Close ();
+    	/* already disabled by setting mouse_enabled */
 	break;
 #endif
     case MOUSE_XTERM:
diff -ru mc-4.5.99a/src/mouse.h mc-4.5.99a-oskar/src/mouse.h
--- mc-4.5.99a/src/mouse.h	Mon Sep 17 06:43:59 2001
+++ mc-4.5.99a-oskar/src/mouse.h	Thu Dec 27 10:46:54 2001
@@ -59,10 +59,8 @@
 /* The mouse is currently: 1 - enabled, 0 - disabled */
 extern int mouse_enabled;
 
-/* String indicating that a mouse event has occured, usually "\E[M" */
-extern char *xmouse_seq;
-
 void init_mouse (void);
+void free_mouse (void);
 void enable_mouse (void);
 void disable_mouse (void);
 


More information about the mc-devel mailing list