root/lib/tty/mouse.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. show_mouse_pointer
  2. init_mouse
  3. enable_mouse
  4. disable_mouse

   1 /*
   2    Mouse managing
   3 
   4    Copyright (C) 1994-2020
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Andrew Borodin <aborodin@vmail.ru>, 2009.
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 /** \file mouse.c
  27  *  \brief Source: mouse managing
  28  *
  29  *  Events received by clients of this library have their coordinates 0 based
  30  */
  31 
  32 #include <config.h>
  33 
  34 #include <stdio.h>
  35 #include <sys/types.h>
  36 #include <unistd.h>
  37 
  38 #include "lib/global.h"
  39 
  40 #include "tty.h"
  41 #include "tty-internal.h"       /* mouse_enabled */
  42 #include "mouse.h"
  43 #include "key.h"                /* define sequence */
  44 
  45 /*** global variables ****************************************************************************/
  46 
  47 Mouse_Type use_mouse_p = MOUSE_NONE;
  48 gboolean mouse_enabled = FALSE;
  49 int mouse_fd = -1;              /* for when gpm_fd changes to < 0 and the old one must be cleared from select_set */
  50 const char *xmouse_seq;
  51 const char *xmouse_extended_seq;
  52 
  53 /*** file scope macro definitions ****************************************************************/
  54 
  55 /*** file scope type declarations ****************************************************************/
  56 
  57 /*** file scope variables ************************************************************************/
  58 
  59 /*** file scope functions ************************************************************************/
  60 /* --------------------------------------------------------------------------------------------- */
  61 
  62 /* --------------------------------------------------------------------------------------------- */
  63 /*** public functions ****************************************************************************/
  64 /* --------------------------------------------------------------------------------------------- */
  65 
  66 void
  67 show_mouse_pointer (int x, int y)
     /* [previous][next][first][last][top][bottom][index][help]  */
  68 {
  69 #ifdef HAVE_LIBGPM
  70     if (use_mouse_p == MOUSE_GPM)
  71         Gpm_DrawPointer (x, y, gpm_consolefd);
  72 #else
  73     (void) x;
  74     (void) y;
  75 #endif /* HAVE_LIBGPM */
  76 }
  77 
  78 /* --------------------------------------------------------------------------------------------- */
  79 
  80 void
  81 init_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  82 {
  83     switch (use_mouse_p)
  84     {
  85 #ifdef HAVE_LIBGPM
  86     case MOUSE_NONE:
  87         use_mouse_p = MOUSE_GPM;
  88         break;
  89 #endif /* HAVE_LIBGPM */
  90 
  91     case MOUSE_XTERM_NORMAL_TRACKING:
  92     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  93         if (xmouse_seq != NULL)
  94             define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
  95         if (xmouse_extended_seq != NULL)
  96             define_sequence (MCKEY_EXTENDED_MOUSE, xmouse_extended_seq, MCKEY_NOACTION);
  97         break;
  98 
  99     default:
 100         break;
 101     }
 102 
 103     enable_mouse ();
 104 }
 105 
 106 /* --------------------------------------------------------------------------------------------- */
 107 
 108 void
 109 enable_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 110 {
 111     if (mouse_enabled)
 112         return;
 113 
 114     switch (use_mouse_p)
 115     {
 116 #ifdef HAVE_LIBGPM
 117     case MOUSE_GPM:
 118         {
 119             Gpm_Connect conn;
 120 
 121             conn.eventMask = ~GPM_MOVE;
 122             conn.defaultMask = GPM_MOVE;
 123             conn.minMod = 0;
 124             conn.maxMod = 0;
 125 
 126             mouse_fd = Gpm_Open (&conn, 0);
 127             if (mouse_fd == -1)
 128             {
 129                 use_mouse_p = MOUSE_NONE;
 130                 return;
 131             }
 132             mouse_enabled = TRUE;
 133         }
 134         break;
 135 #endif /* HAVE_LIBGPM */
 136 
 137     case MOUSE_XTERM_NORMAL_TRACKING:
 138         /* save old highlight mouse tracking */
 139         printf (ESC_STR "[?1001s");
 140 
 141         /* enable mouse tracking */
 142         printf (ESC_STR "[?1000h");
 143 
 144         /* enable SGR extended mouse reporting */
 145         printf (ESC_STR "[?1006h");
 146 
 147         fflush (stdout);
 148         mouse_enabled = TRUE;
 149         break;
 150 
 151     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
 152         /* save old highlight mouse tracking */
 153         printf (ESC_STR "[?1001s");
 154 
 155         /* enable mouse tracking */
 156         printf (ESC_STR "[?1002h");
 157 
 158         /* enable SGR extended mouse reporting */
 159         printf (ESC_STR "[?1006h");
 160 
 161         fflush (stdout);
 162         mouse_enabled = TRUE;
 163         break;
 164 
 165     default:
 166         break;
 167     }
 168 }
 169 
 170 /* --------------------------------------------------------------------------------------------- */
 171 
 172 void
 173 disable_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 174 {
 175     if (!mouse_enabled)
 176         return;
 177 
 178     mouse_enabled = FALSE;
 179 
 180     switch (use_mouse_p)
 181     {
 182 #ifdef HAVE_LIBGPM
 183     case MOUSE_GPM:
 184         Gpm_Close ();
 185         break;
 186 #endif
 187     case MOUSE_XTERM_NORMAL_TRACKING:
 188         /* disable SGR extended mouse reporting */
 189         printf (ESC_STR "[?1006l");
 190 
 191         /* disable mouse tracking */
 192         printf (ESC_STR "[?1000l");
 193 
 194         /* restore old highlight mouse tracking */
 195         printf (ESC_STR "[?1001r");
 196 
 197         fflush (stdout);
 198         break;
 199     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
 200         /* disable SGR extended mouse reporting */
 201         printf (ESC_STR "[?1006l");
 202 
 203         /* disable mouse tracking */
 204         printf (ESC_STR "[?1002l");
 205 
 206         /* restore old highlight mouse tracking */
 207         printf (ESC_STR "[?1001r");
 208 
 209         fflush (stdout);
 210         break;
 211     default:
 212         break;
 213     }
 214 }
 215 
 216 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */