]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_mouse.c
ncurses 5.4
[ncurses.git] / ncurses / base / lib_mouse.c
index 0555f1fd2ce74a7cc3f88adfdc9c0530634d9f61..b2fc9d547acff9a1a0777599c7708f45f8ffb175 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2000,2002 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -29,6 +29,7 @@
 /****************************************************************************
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
+ *     and: Thomas E. Dickey 1996-2003                                      *
  ****************************************************************************/
 
 /*
 #endif
 
 #include <curses.priv.h>
+
+MODULE_ID("$Id: lib_mouse.c,v 1.68 2003/11/08 21:50:50 tom Exp $")
+
 #include <term.h>
+#include <tic.h>
 
 #if USE_GPM_SUPPORT
 #ifndef LINT                   /* don't need this for llib-lncurses */
 #endif
 #endif
 
-MODULE_ID("$Id: lib_mouse.c,v 1.61 2002/09/28 16:08:58 tom Exp $")
+#if USE_SYSMOUSE
+#undef buttons                 /* symbol conflict in consio.h */
+#undef mouse_info              /* symbol conflict in consio.h */
+#include <osreldate.h>
+#if (__FreeBSD_version >= 400017)
+#include <sys/consio.h>
+#include <sys/fbio.h>
+#else
+#include <machine/console.h>
+#endif
+#endif /* use_SYSMOUSE */
 
 #define MY_TRACE TRACE_ICALLS|TRACE_IEVENT
 
-#define INVALID_EVENT  -1
+#define        MASK_RELEASE(x)         ((001 << (6 * ((x) - 1))))
+#define        MASK_PRESS(x)           ((002 << (6 * ((x) - 1))))
+#define        MASK_CLICK(x)           ((004 << (6 * ((x) - 1))))
+#define        MASK_DOUBLE_CLICK(x)    ((010 << (6 * ((x) - 1))))
+#define        MASK_TRIPLE_CLICK(x)    ((020 << (6 * ((x) - 1))))
+#define        MASK_RESERVED_EVENT(x)  ((040 << (6 * ((x) - 1))))
+
+#define BUTTON_CLICKED  (BUTTON1_CLICKED  | BUTTON2_CLICKED  | BUTTON3_CLICKED)
+#define BUTTON_PRESSED  (BUTTON1_PRESSED  | BUTTON2_PRESSED  | BUTTON3_PRESSED)
+#define BUTTON_RELEASED (BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON3_RELEASED)
 
-static int mousetype;
-#define M_XTERM                -1      /* use xterm's mouse tracking? */
-#define M_NONE         0       /* no mouse device */
-#define M_GPM          1       /* use GPM */
-#define M_QNX          2       /* QNX mouse on console */
-#define M_QNX_TERM     3       /* QNX mouse on pterm/xterm (using qansi-m) */
+#define INVALID_EVENT  -1
+#define NORMAL_EVENT   0
 
 #if USE_GPM_SUPPORT
 #ifndef LINT
@@ -117,7 +137,11 @@ static void _nc_mouse_wrap(SCREEN *);
  */
 static MEVENT events[EV_MAX];  /* hold the last mouse event seen */
 static MEVENT *eventp = events;        /* next free slot in event queue */
+
+#undef  NEXT
 #define NEXT(ep)       ((ep == events + EV_MAX - 1) ? events : ep + 1)
+
+#undef  PREV
 #define PREV(ep)       ((ep == events) ? events + EV_MAX - 1 : ep - 1)
 
 #ifdef TRACE
@@ -135,7 +159,7 @@ _trace_slot(const char *tag)
 }
 #endif
 
-#ifdef USE_EMX_MOUSE
+#if USE_EMX_MOUSE
 
 #  define TOP_ROW          0
 #  define LEFT_COL         0
@@ -229,10 +253,86 @@ server_state(const int state)
     mouse_activated = state;
 }
 
+#endif /* USE_EMX_MOUSE */
+
+#if USE_SYSMOUSE
+static void
+handle_sysmouse(int sig GCC_UNUSED)
+{
+    struct mouse_info the_mouse;
+    MEVENT *work;
+
+    the_mouse.operation = MOUSE_GETINFO;
+    if (SP != 0
+       && SP->_mouse_fd >= 0
+       && SP->_sysmouse_tail < FIFO_SIZE
+       && ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
+
+       if (SP->_sysmouse_head > SP->_sysmouse_tail) {
+           SP->_sysmouse_tail = 0;
+           SP->_sysmouse_head = 0;
+       }
+       work = &(SP->_sysmouse_fifo[SP->_sysmouse_tail]);
+       memset(work, 0, sizeof(*work));
+       work->id = NORMAL_EVENT;        /* there's only one mouse... */
+
+       SP->_sysmouse_old_buttons = SP->_sysmouse_new_buttons;
+       SP->_sysmouse_new_buttons = the_mouse.u.data.buttons & 0x7;
+
+       if (SP->_sysmouse_new_buttons) {
+           if (SP->_sysmouse_new_buttons & 1)
+               work->bstate |= BUTTON1_PRESSED;
+           if (SP->_sysmouse_new_buttons & 2)
+               work->bstate |= BUTTON2_PRESSED;
+           if (SP->_sysmouse_new_buttons & 4)
+               work->bstate |= BUTTON3_PRESSED;
+       } else {
+           if (SP->_sysmouse_old_buttons & 1)
+               work->bstate |= BUTTON1_RELEASED;
+           if (SP->_sysmouse_old_buttons & 2)
+               work->bstate |= BUTTON2_RELEASED;
+           if (SP->_sysmouse_old_buttons & 4)
+               work->bstate |= BUTTON3_RELEASED;
+       }
+
+       /* for cosmetic bug in syscons.c on FreeBSD 3.[34] */
+       the_mouse.operation = MOUSE_HIDE;
+       ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
+       the_mouse.operation = MOUSE_SHOW;
+       ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
+
+       /*
+        * We're only interested if the button is pressed or released.
+        * FIXME: implement continuous event-tracking.
+        */
+       if (SP->_sysmouse_new_buttons != SP->_sysmouse_old_buttons) {
+           SP->_sysmouse_tail += 1;
+       }
+       work->x = the_mouse.u.data.x / SP->_sysmouse_char_width;
+       work->y = the_mouse.u.data.y / SP->_sysmouse_char_height;
+    }
+}
 #endif
 
 static int initialized;
 
+static void
+init_xterm_mouse(void)
+{
+    SP->_mouse_type = M_XTERM;
+    SP->_mouse_xtermcap = tigetstr("XM");
+    if (!VALID_STRING(SP->_mouse_xtermcap))
+       SP->_mouse_xtermcap = "\033[?1000%?%p1%{1}%=%th%el%;";
+}
+
+#if !USE_EMX_MOUSE
+static void
+enable_xterm_mouse(int enable)
+{
+    putp(tparm(SP->_mouse_xtermcap, enable));
+}
+#endif /* !USE_EMX_MOUSE */
+
 static void
 initialize_mousetype(void)
 {
@@ -240,20 +340,23 @@ initialize_mousetype(void)
 
     /* Try gpm first, because gpm may be configured to run in xterm */
 #if USE_GPM_SUPPORT
-    /* GPM: initialize connection to gpm server */
-    gpm_connect.eventMask = GPM_DOWN | GPM_UP;
-    gpm_connect.defaultMask = ~(gpm_connect.eventMask | GPM_HARD);
-    gpm_connect.minMod = 0;
-    gpm_connect.maxMod = ~((1 << KG_SHIFT) | (1 << KG_SHIFTL) | (1 << KG_SHIFTR));
-    if (Gpm_Open(&gpm_connect, 0) >= 0) {      /* returns the file-descriptor */
-       mousetype = M_GPM;
-       SP->_mouse_fd = gpm_fd;
-       return;
+    /* GPM does printf's without checking if stdout is a terminal */
+    if (isatty(fileno(stdout))) {
+       /* GPM: initialize connection to gpm server */
+       gpm_connect.eventMask = GPM_DOWN | GPM_UP;
+       gpm_connect.defaultMask = ~(gpm_connect.eventMask | GPM_HARD);
+       gpm_connect.minMod = 0;
+       gpm_connect.maxMod = ~((1 << KG_SHIFT) | (1 << KG_SHIFTL) | (1 << KG_SHIFTR));
+       if (Gpm_Open(&gpm_connect, 0) >= 0) {   /* returns the file-descriptor */
+           SP->_mouse_type = M_GPM;
+           SP->_mouse_fd = gpm_fd;
+           return;
+       }
     }
 #endif
 
     /* OS/2 VIO */
-#ifdef USE_EMX_MOUSE
+#if USE_EMX_MOUSE
     if (!mouse_thread
        && strstr(cur_term->type.term_names, "xterm") == 0
        && key_mouse) {
@@ -287,22 +390,87 @@ initialize_mousetype(void)
                printf("mouse thread error %d=%#x", rc, rc);
                return;
            } else {
-               mousetype = M_XTERM;
+               SP->_mouse_type = M_XTERM;
                return;
            }
        }
     }
 #endif
 
+#if USE_SYSMOUSE
+    {
+       struct mouse_info the_mouse;
+       char *the_device = 0;
+
+       if (isatty(SP->_ifd))
+           the_device = ttyname(SP->_ifd);
+       if (the_device == 0)
+           the_device = "/dev/tty";
+
+       SP->_mouse_fd = open(the_device, O_RDWR);
+
+       if (SP->_mouse_fd >= 0) {
+           /*
+            * sysmouse does not have a usable user interface for obtaining
+            * mouse events.  The logical way to proceed (reading data on a
+            * stream) only works if one opens the device as root.  Even in
+            * that mode, careful examination shows we lose events
+            * occasionally.  The interface provided for user programs is to
+            * establish a signal handler.  really.
+            *
+            * Take over SIGUSR2 for this purpose since SIGUSR1 is more
+            * likely to be used by an application.  getch() will have to
+            * handle the misleading EINTR's.
+            */
+           signal(SIGUSR2, SIG_IGN);
+           the_mouse.operation = MOUSE_MODE;
+           the_mouse.u.mode.mode = 0;
+           the_mouse.u.mode.signal = SIGUSR2;
+           if (ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse) != -1) {
+               signal(SIGUSR2, handle_sysmouse);
+               the_mouse.operation = MOUSE_SHOW;
+               ioctl(SP->_mouse_fd, CONS_MOUSECTL, &the_mouse);
+
+#if defined(FBIO_MODEINFO) || defined(CONS_MODEINFO)   /* FreeBSD > 2.x */
+               {
+#ifndef FBIO_GETMODE           /* FreeBSD 3.x */
+#define FBIO_GETMODE    CONS_GET
+#define FBIO_MODEINFO   CONS_MODEINFO
+#endif /* FBIO_GETMODE */
+                   video_info_t the_video;
+
+                   if (ioctl(SP->_mouse_fd,
+                             FBIO_GETMODE,
+                             &the_video.vi_mode) != -1
+                       && ioctl(SP->_mouse_fd,
+                                FBIO_MODEINFO,
+                                &the_video) != -1) {
+                       SP->_sysmouse_char_width = the_video.vi_cwidth;
+                       SP->_sysmouse_char_height = the_video.vi_cheight;
+                   }
+               }
+#endif /* defined(FBIO_MODEINFO) || defined(CONS_MODEINFO) */
+
+               if (SP->_sysmouse_char_width <= 0)
+                   SP->_sysmouse_char_width = 8;
+               if (SP->_sysmouse_char_height <= 0)
+                   SP->_sysmouse_char_height = 16;
+               SP->_mouse_type = M_SYSMOUSE;
+               return;
+           }
+       }
+    }
+#endif /* USE_SYSMOUSE */
+
     /* we know how to recognize mouse events under "xterm" */
     if (key_mouse != 0) {
        if (!strcmp(key_mouse, xterm_kmous)) {
-           mousetype = M_XTERM;
+           init_xterm_mouse();
            return;
        }
     } else if (strstr(cur_term->type.term_names, "xterm") != 0) {
        (void) _nc_add_to_try(&(SP->_keytry), xterm_kmous, KEY_MOUSE);
-       mousetype = M_XTERM;
+       init_xterm_mouse();
        return;
     }
 }
@@ -323,7 +491,7 @@ _nc_mouse_init(void)
 
        initialize_mousetype();
 
-       T(("_nc_mouse_init() set mousetype to %d", mousetype));
+       T(("_nc_mouse_init() set mousetype to %d", SP->_mouse_type));
     }
 }
 
@@ -334,74 +502,113 @@ _nc_mouse_init(void)
 static bool
 _nc_mouse_event(SCREEN * sp GCC_UNUSED)
 {
+    bool result = FALSE;
+
+    switch (SP->_mouse_type) {
+    case M_XTERM:
+       /* xterm: never have to query, mouse events are in the keyboard stream */
+#if USE_EMX_MOUSE
+       {
+           char kbuf[3];
+
+           int i, res = read(M_FD(sp), &kbuf, 3);      /* Eat the prefix */
+           if (res != 3)
+               printf("Got %d chars instead of 3 for prefix.\n", res);
+           for (i = 0; i < res; i++) {
+               if (kbuf[i] != key_mouse[i])
+                   printf("Got char %d instead of %d for prefix.\n",
+                          (int) kbuf[i], (int) key_mouse[i]);
+           }
+           result = TRUE;
+       }
+#endif /* USE_EMX_MOUSE */
+       break;
+
 #if USE_GPM_SUPPORT
-    /* GPM: query server for event, return TRUE if we find one */
-    Gpm_Event ev;
+    case M_GPM:
+       {
+           /* query server for event, return TRUE if we find one */
+           Gpm_Event ev;
+
+           if (Gpm_GetEvent(&ev) == 1) {
+               /* there's only one mouse... */
+               eventp->id = NORMAL_EVENT;
+
+               eventp->bstate = 0;
+               switch (ev.type & 0x0f) {
+               case (GPM_DOWN):
+                   if (ev.buttons & GPM_B_LEFT)
+                       eventp->bstate |= BUTTON1_PRESSED;
+                   if (ev.buttons & GPM_B_MIDDLE)
+                       eventp->bstate |= BUTTON2_PRESSED;
+                   if (ev.buttons & GPM_B_RIGHT)
+                       eventp->bstate |= BUTTON3_PRESSED;
+                   break;
+               case (GPM_UP):
+                   if (ev.buttons & GPM_B_LEFT)
+                       eventp->bstate |= BUTTON1_RELEASED;
+                   if (ev.buttons & GPM_B_MIDDLE)
+                       eventp->bstate |= BUTTON2_RELEASED;
+                   if (ev.buttons & GPM_B_RIGHT)
+                       eventp->bstate |= BUTTON3_RELEASED;
+                   break;
+               default:
+                   break;
+               }
 
-    if (Gpm_GetEvent(&ev) == 1) {
-       eventp->id = 0;         /* there's only one mouse... */
+               eventp->x = ev.x - 1;
+               eventp->y = ev.y - 1;
+               eventp->z = 0;
 
-       eventp->bstate = 0;
-       switch (ev.type & 0x0f) {
-       case (GPM_DOWN):
-           if (ev.buttons & GPM_B_LEFT)
-               eventp->bstate |= BUTTON1_PRESSED;
-           if (ev.buttons & GPM_B_MIDDLE)
-               eventp->bstate |= BUTTON2_PRESSED;
-           if (ev.buttons & GPM_B_RIGHT)
-               eventp->bstate |= BUTTON3_PRESSED;
-           break;
-       case (GPM_UP):
-           if (ev.buttons & GPM_B_LEFT)
-               eventp->bstate |= BUTTON1_RELEASED;
-           if (ev.buttons & GPM_B_MIDDLE)
-               eventp->bstate |= BUTTON2_RELEASED;
-           if (ev.buttons & GPM_B_RIGHT)
-               eventp->bstate |= BUTTON3_RELEASED;
-           break;
-       default:
-           break;
+               /* bump the next-free pointer into the circular list */
+               eventp = NEXT(eventp);
+               result = TRUE;
+           }
        }
+       break;
+#endif
 
-       eventp->x = ev.x - 1;
-       eventp->y = ev.y - 1;
-       eventp->z = 0;
+#if USE_SYSMOUSE
+    case M_SYSMOUSE:
+       if (SP->_sysmouse_head < SP->_sysmouse_tail) {
+           *eventp = SP->_sysmouse_fifo[SP->_sysmouse_head];
 
-       /* bump the next-free pointer into the circular list */
-       eventp = NEXT(eventp);
-       return (TRUE);
-    }
-#endif
+           /*
+            * Point the fifo-head to the next possible location.  If there
+            * are none, reset the indices.  This may be interrupted by the
+            * signal handler, doing essentially the same reset.
+            */
+           SP->_sysmouse_head += 1;
+           if (SP->_sysmouse_head == SP->_sysmouse_tail) {
+               SP->_sysmouse_tail = 0;
+               SP->_sysmouse_head = 0;
+           }
 
-#ifdef USE_EMX_MOUSE
-    {
-       char kbuf[3];
-
-       int i, res = read(M_FD(sp), &kbuf, 3);  /* Eat the prefix */
-       if (res != 3)
-           printf("Got %d chars instead of 3 for prefix.\n", res);
-       for (i = 0; i < res; i++) {
-           if (kbuf[i] != key_mouse[i])
-               printf("Got char %d instead of %d for prefix.\n",
-                      (int) kbuf[i], (int) key_mouse[i]);
+           /* bump the next-free pointer into the circular list */
+           eventp = NEXT(eventp);
+           result = TRUE;
        }
-       return TRUE;
+       break;
+#endif /* USE_SYSMOUSE */
+
+    case M_NONE:
+       break;
     }
-#endif /* USE_EMX_MOUSE */
 
-    /* xterm: never have to query, mouse events are in the keyboard stream */
-    return (FALSE);            /* no event waiting */
+    return result;             /* true if we found an event */
 }
 
 static bool
 _nc_mouse_inline(SCREEN * sp)
 /* mouse report received in the keyboard stream -- parse its info */
 {
+    bool result = FALSE;
+
     TR(MY_TRACE, ("_nc_mouse_inline() called"));
 
-    if (mousetype == M_XTERM) {
+    if (SP->_mouse_type == M_XTERM) {
        unsigned char kbuf[4];
-       MEVENT *prev;
+       mmask_t prev;
        size_t grabbed;
        int res;
 
@@ -438,7 +645,7 @@ _nc_mouse_inline(SCREEN * sp)
        for (grabbed = 0; grabbed < 3; grabbed += res) {
 
            /* For VIO mouse we add extra bit 64 to disambiguate button-up. */
-#ifdef USE_EMX_MOUSE
+#if USE_EMX_MOUSE
            res = read(M_FD(sp) >= 0 ? M_FD(sp) : sp->_ifd, &kbuf, 3);
 #else
            res = read(sp->_ifd, kbuf + grabbed, 3 - grabbed);
@@ -451,59 +658,65 @@ _nc_mouse_inline(SCREEN * sp)
        TR(TRACE_IEVENT,
           ("_nc_mouse_inline sees the following xterm data: '%s'", kbuf));
 
-       eventp->id = 0;         /* there's only one mouse... */
+       /* there's only one mouse... */
+       eventp->id = NORMAL_EVENT;
 
        /* processing code goes here */
        eventp->bstate = 0;
+       prev = PREV(eventp)->bstate;
+
+#if USE_EMX_MOUSE
+#define PRESS_POSITION(n) \
+       eventp->bstate = MASK_PRESS(n); \
+       if (kbuf[0] & 0x40) \
+           eventp->bstate = MASK_RELEASE(n)
+#else
+#define PRESS_POSITION(n) \
+       eventp->bstate = (prev & MASK_PRESS(n) \
+                       ? REPORT_MOUSE_POSITION \
+                       : MASK_PRESS(n))
+#endif
+
        switch (kbuf[0] & 0x3) {
        case 0x0:
-           eventp->bstate = BUTTON1_PRESSED;
-#ifdef USE_EMX_MOUSE
-           if (kbuf[0] & 0x40)
-               eventp->bstate = BUTTON1_RELEASED;
-#endif
+           PRESS_POSITION(1);
            break;
 
        case 0x1:
-           eventp->bstate = BUTTON2_PRESSED;
-#ifdef USE_EMX_MOUSE
-           if (kbuf[0] & 0x40)
-               eventp->bstate = BUTTON2_RELEASED;
-#endif
+           PRESS_POSITION(2);
            break;
 
        case 0x2:
-           eventp->bstate = BUTTON3_PRESSED;
-#ifdef USE_EMX_MOUSE
-           if (kbuf[0] & 0x40)
-               eventp->bstate = BUTTON3_RELEASED;
-#endif
+           PRESS_POSITION(3);
            break;
 
        case 0x3:
            /*
-            * Release events aren't reported for individual buttons,
-            * just for the button set as a whole...
+            * Release events aren't reported for individual buttons, just for
+            * the button set as a whole.  However, because there are normally
+            * no mouse events under xterm that intervene between press and
+            * release, we can infer the button actually released by looking at
+            * the previous event.
             */
-           eventp->bstate =
-               (BUTTON1_RELEASED |
-                BUTTON2_RELEASED |
-                BUTTON3_RELEASED);
-           /*
-            * ...however, because there are no kinds of mouse events under
-            * xterm that can intervene between press and release, we can
-            * deduce which buttons were actually released by looking at the
-            * previous event.
-            */
-           prev = PREV(eventp);
-           if (!(prev->bstate & BUTTON1_PRESSED))
-               eventp->bstate &= ~BUTTON1_RELEASED;
-           if (!(prev->bstate & BUTTON2_PRESSED))
-               eventp->bstate &= ~BUTTON2_RELEASED;
-           if (!(prev->bstate & BUTTON3_PRESSED))
-               eventp->bstate &= ~BUTTON3_RELEASED;
+           if (prev & (BUTTON_PRESSED | BUTTON_RELEASED)) {
+               eventp->bstate = BUTTON_RELEASED;
+               if (!(prev & BUTTON1_PRESSED))
+                   eventp->bstate &= ~BUTTON1_RELEASED;
+               if (!(prev & BUTTON2_PRESSED))
+                   eventp->bstate &= ~BUTTON2_RELEASED;
+               if (!(prev & BUTTON3_PRESSED))
+                   eventp->bstate &= ~BUTTON3_RELEASED;
+           } else {
+               /*
+                * XFree86 xterm will return a stream of release-events to
+                * let the application know where the mouse is going, if the
+                * private mode 1002 or 1003 is enabled.
+                */
+               eventp->bstate = REPORT_MOUSE_POSITION;
+           }
            break;
        }
+       result = (eventp->bstate & REPORT_MOUSE_POSITION) ? TRUE : FALSE;
 
        if (kbuf[0] & 4) {
            eventp->bstate |= BUTTON_SHIFT;
@@ -529,7 +742,7 @@ _nc_mouse_inline(SCREEN * sp)
 #endif
     }
 
-    return (FALSE);
+    return (result);
 }
 
 static void
@@ -542,16 +755,16 @@ mouse_activate(bool on)
 
     if (on) {
 
-       switch (mousetype) {
+       switch (SP->_mouse_type) {
        case M_XTERM:
 #if NCURSES_EXT_FUNCS
            keyok(KEY_MOUSE, on);
 #endif
            TPUTS_TRACE("xterm mouse initialization");
-#ifdef USE_EMX_MOUSE
+#if USE_EMX_MOUSE
            server_state(1);
 #else
-           putp("\033[?1000h");
+           enable_xterm_mouse(1);
 #endif
            break;
 #if USE_GPM_SUPPORT
@@ -559,6 +772,13 @@ mouse_activate(bool on)
            SP->_mouse_fd = gpm_fd;
            break;
 #endif
+#if USE_SYSMOUSE
+       case M_SYSMOUSE:
+           signal(SIGUSR2, handle_sysmouse);
+           break;
+#endif
+       case M_NONE:
+           return;
        }
        /* Make runtime binding to cut down on object size of applications that
         * do not use the mouse (e.g., 'clear').
@@ -571,19 +791,26 @@ mouse_activate(bool on)
 
     } else {
 
-       switch (mousetype) {
+       switch (SP->_mouse_type) {
        case M_XTERM:
            TPUTS_TRACE("xterm mouse deinitialization");
-#ifdef USE_EMX_MOUSE
+#if USE_EMX_MOUSE
            server_state(0);
 #else
-           putp("\033[?1000l");
+           enable_xterm_mouse(0);
 #endif
            break;
 #if USE_GPM_SUPPORT
        case M_GPM:
            break;
 #endif
+#if USE_SYSMOUSE
+       case M_SYSMOUSE:
+           signal(SIGUSR2, SIG_IGN);
+           break;
+#endif
+       case M_NONE:
+           return;
        }
     }
     _nc_flush();
@@ -609,7 +836,7 @@ _nc_mouse_parse(int runcount)
      * When we enter this routine, the event list next-free pointer
      * points just past a run of mouse events that we know were separated
      * in time by less than the critical click interval. The job of this
-     * routine is to collaps this run into a single higher-level event
+     * routine is to collapse this run into a single higher-level event
      * or gesture.
      *
      * We accomplish this in two passes.  The first pass merges press/release
@@ -631,7 +858,7 @@ _nc_mouse_parse(int runcount)
           ("_nc_mouse_parse: returning simple mouse event %s at slot %ld",
            _tracemouse(prev),
            (long) (prev - events)));
-       return (prev->id >= 0)
+       return (prev->id >= NORMAL_EVENT)
            ? ((prev->bstate & eventmask) ? TRUE : FALSE)
            : FALSE;
     }
@@ -657,7 +884,7 @@ _nc_mouse_parse(int runcount)
        merge = FALSE;
        for (ep = runp; (next = NEXT(ep)) != eventp; ep = next) {
            if (ep->x == next->x && ep->y == next->y
-               && (ep->bstate & (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED))
+               && (ep->bstate & BUTTON_PRESSED)
                && (!(ep->bstate & BUTTON1_PRESSED)
                    == !(next->bstate & BUTTON1_RELEASED))
                && (!(ep->bstate & BUTTON2_PRESSED)
@@ -729,10 +956,8 @@ _nc_mouse_parse(int runcount)
                    continue;
 
                /* merge click events forward */
-               if ((ep->bstate &
-                    (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED))
-                   && (follower->bstate &
-                       (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED))) {
+               if ((ep->bstate & BUTTON_CLICKED)
+                   && (follower->bstate & BUTTON_CLICKED)) {
                    if ((eventmask & BUTTON1_DOUBLE_CLICKED)
                        && (follower->bstate & BUTTON1_CLICKED)) {
                        follower->bstate &= ~BUTTON1_CLICKED;
@@ -760,8 +985,7 @@ _nc_mouse_parse(int runcount)
                     (BUTTON1_DOUBLE_CLICKED
                      | BUTTON2_DOUBLE_CLICKED
                      | BUTTON3_DOUBLE_CLICKED))
-                   && (follower->bstate &
-                       (BUTTON1_CLICKED | BUTTON2_CLICKED | BUTTON3_CLICKED))) {
+                   && (follower->bstate & BUTTON_CLICKED)) {
                    if ((eventmask & BUTTON1_TRIPLE_CLICKED)
                        && (follower->bstate & BUTTON1_CLICKED)) {
                        follower->bstate &= ~BUTTON1_CLICKED;
@@ -831,7 +1055,7 @@ _nc_mouse_wrap(SCREEN * sp GCC_UNUSED)
 {
     TR(MY_TRACE, ("_nc_mouse_wrap() called"));
 
-    switch (mousetype) {
+    switch (SP->_mouse_type) {
     case M_XTERM:
        if (eventmask)
            mouse_activate(FALSE);
@@ -841,6 +1065,13 @@ _nc_mouse_wrap(SCREEN * sp GCC_UNUSED)
     case M_GPM:
        break;
 #endif
+#if USE_SYSMOUSE
+    case M_SYSMOUSE:
+       mouse_activate(FALSE);
+       break;
+#endif
+    case M_NONE:
+       break;
     }
 }
 
@@ -850,11 +1081,27 @@ _nc_mouse_resume(SCREEN * sp GCC_UNUSED)
 {
     TR(MY_TRACE, ("_nc_mouse_resume() called"));
 
-    /* xterm: re-enable reporting */
-    if (mousetype == M_XTERM && eventmask)
-       mouse_activate(TRUE);
+    switch (SP->_mouse_type) {
+    case M_XTERM:
+       /* xterm: re-enable reporting */
+       if (eventmask)
+           mouse_activate(TRUE);
+       break;
 
-    /* GPM: reclaim our event set */
+#if USE_GPM_SUPPORT
+    case M_GPM:
+       /* GPM: reclaim our event set */
+       break;
+#endif
+
+#if USE_SYSMOUSE
+    case M_SYSMOUSE:
+       mouse_activate(TRUE);
+       break;
+#endif
+    case M_NONE:
+       break;
+    }
 }
 
 /**************************************************************************
@@ -869,7 +1116,7 @@ getmouse(MEVENT * aevent)
 {
     T((T_CALLED("getmouse(%p)"), aevent));
 
-    if (aevent && (mousetype != M_NONE)) {
+    if (aevent && (SP->_mouse_type != M_NONE)) {
        /* compute the current-event pointer */
        MEVENT *prev = PREV(eventp);
 
@@ -917,14 +1164,14 @@ mousemask(mmask_t newmask, mmask_t * oldmask)
        returnBits(0);
 
     _nc_mouse_init();
-    if (mousetype != M_NONE) {
+    if (SP->_mouse_type != M_NONE) {
        eventmask = newmask &
-           (BUTTON_ALT | BUTTON_CTRL | BUTTON_SHIFT
-            | BUTTON1_PRESSED | BUTTON1_RELEASED | BUTTON1_CLICKED
+           (REPORT_MOUSE_POSITION | BUTTON_ALT | BUTTON_CTRL | BUTTON_SHIFT
+            | BUTTON_PRESSED
+            | BUTTON_RELEASED
+            | BUTTON_CLICKED
             | BUTTON1_DOUBLE_CLICKED | BUTTON1_TRIPLE_CLICKED
-            | BUTTON2_PRESSED | BUTTON2_RELEASED | BUTTON2_CLICKED
             | BUTTON2_DOUBLE_CLICKED | BUTTON2_TRIPLE_CLICKED
-            | BUTTON3_PRESSED | BUTTON3_RELEASED | BUTTON3_CLICKED
             | BUTTON3_DOUBLE_CLICKED | BUTTON3_TRIPLE_CLICKED);
 
        mouse_activate(eventmask != 0);
@@ -977,7 +1224,7 @@ mouseinterval(int maxclick)
 NCURSES_EXPORT(int)
 _nc_has_mouse(void)
 {
-    return (mousetype == M_NONE ? 0 : 1);
+    return (SP->_mouse_type == M_NONE ? 0 : 1);
 }
 
 NCURSES_EXPORT(bool)
@@ -1010,5 +1257,3 @@ wmouse_trafo(const WINDOW *win, int *pY, int *pX, bool to_screen)
     }
     returnBool(result);
 }
-
-/* lib_mouse.c ends here */