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-2019
   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         define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
  94         define_sequence (MCKEY_EXTENDED_MOUSE, xmouse_extended_seq, MCKEY_NOACTION);
  95         break;
  96 
  97     default:
  98         break;
  99     }
 100 
 101     enable_mouse ();
 102 }
 103 
 104 /* --------------------------------------------------------------------------------------------- */
 105 
 106 void
 107 enable_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 108 {
 109     if (mouse_enabled)
 110         return;
 111 
 112     switch (use_mouse_p)
 113     {
 114 #ifdef HAVE_LIBGPM
 115     case MOUSE_GPM:
 116         {
 117             Gpm_Connect conn;
 118 
 119             conn.eventMask = ~GPM_MOVE;
 120             conn.defaultMask = GPM_MOVE;
 121             conn.minMod = 0;
 122             conn.maxMod = 0;
 123 
 124             mouse_fd = Gpm_Open (&conn, 0);
 125             if (mouse_fd == -1)
 126             {
 127                 use_mouse_p = MOUSE_NONE;
 128                 return;
 129             }
 130             mouse_enabled = TRUE;
 131         }
 132         break;
 133 #endif /* HAVE_LIBGPM */
 134 
 135     case MOUSE_XTERM_NORMAL_TRACKING:
 136         /* save old highlight mouse tracking */
 137         printf (ESC_STR "[?1001s");
 138 
 139         /* enable mouse tracking */
 140         printf (ESC_STR "[?1000h");
 141 
 142         /* enable SGR extended mouse reporting */
 143         printf (ESC_STR "[?1006h");
 144 
 145         fflush (stdout);
 146         mouse_enabled = TRUE;
 147         break;
 148 
 149     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
 150         /* save old highlight mouse tracking */
 151         printf (ESC_STR "[?1001s");
 152 
 153         /* enable mouse tracking */
 154         printf (ESC_STR "[?1002h");
 155 
 156         /* enable SGR extended mouse reporting */
 157         printf (ESC_STR "[?1006h");
 158 
 159         fflush (stdout);
 160         mouse_enabled = TRUE;
 161         break;
 162 
 163     default:
 164         break;
 165     }
 166 }
 167 
 168 /* --------------------------------------------------------------------------------------------- */
 169 
 170 void
 171 disable_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173     if (!mouse_enabled)
 174         return;
 175 
 176     mouse_enabled = FALSE;
 177 
 178     switch (use_mouse_p)
 179     {
 180 #ifdef HAVE_LIBGPM
 181     case MOUSE_GPM:
 182         Gpm_Close ();
 183         break;
 184 #endif
 185     case MOUSE_XTERM_NORMAL_TRACKING:
 186         /* disable SGR extended mouse reporting */
 187         printf (ESC_STR "[?1006l");
 188 
 189         /* disable mouse tracking */
 190         printf (ESC_STR "[?1000l");
 191 
 192         /* restore old highlight mouse tracking */
 193         printf (ESC_STR "[?1001r");
 194 
 195         fflush (stdout);
 196         break;
 197     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
 198         /* disable SGR extended mouse reporting */
 199         printf (ESC_STR "[?1006l");
 200 
 201         /* disable mouse tracking */
 202         printf (ESC_STR "[?1002l");
 203 
 204         /* restore old highlight mouse tracking */
 205         printf (ESC_STR "[?1001r");
 206 
 207         fflush (stdout);
 208         break;
 209     default:
 210         break;
 211     }
 212 }
 213 
 214 /* --------------------------------------------------------------------------------------------- */

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