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-2025
   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 <https://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 =
  50     -1;  // for when gpm_fd changes to < 0 and the old one must be cleared from select_set
  51 const char *xmouse_seq;
  52 const char *xmouse_extended_seq;
  53 
  54 /*** file scope macro definitions ****************************************************************/
  55 
  56 /*** file scope type declarations ****************************************************************/
  57 
  58 /*** file scope variables ************************************************************************/
  59 
  60 /*** file scope functions ************************************************************************/
  61 /* --------------------------------------------------------------------------------------------- */
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 /*** public functions ****************************************************************************/
  65 /* --------------------------------------------------------------------------------------------- */
  66 
  67 void
  68 show_mouse_pointer (int x, int y)
     /* [previous][next][first][last][top][bottom][index][help]  */
  69 {
  70 #ifdef HAVE_LIBGPM
  71     if (use_mouse_p == MOUSE_GPM)
  72         Gpm_DrawPointer (x, y, gpm_consolefd);
  73 #else
  74     (void) x;
  75     (void) y;
  76 #endif
  77 }
  78 
  79 /* --------------------------------------------------------------------------------------------- */
  80 
  81 void
  82 init_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  83 {
  84     switch (use_mouse_p)
  85     {
  86 #ifdef HAVE_LIBGPM
  87     case MOUSE_NONE:
  88         use_mouse_p = MOUSE_GPM;
  89         break;
  90 #endif
  91 
  92     case MOUSE_XTERM_NORMAL_TRACKING:
  93     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
  94         if (xmouse_seq != NULL)
  95             define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
  96         if (xmouse_extended_seq != NULL)
  97             define_sequence (MCKEY_EXTENDED_MOUSE, xmouse_extended_seq, MCKEY_NOACTION);
  98         break;
  99 
 100     default:
 101         break;
 102     }
 103 
 104     enable_mouse ();
 105 }
 106 
 107 /* --------------------------------------------------------------------------------------------- */
 108 
 109 void
 110 enable_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 111 {
 112     if (mouse_enabled)
 113         return;
 114 
 115     switch (use_mouse_p)
 116     {
 117 #ifdef HAVE_LIBGPM
 118     case MOUSE_GPM:
 119     {
 120         Gpm_Connect conn;
 121 
 122         conn.eventMask = ~GPM_MOVE;
 123         conn.defaultMask = GPM_MOVE;
 124         conn.minMod = 0;
 125         conn.maxMod = 0;
 126 
 127         mouse_fd = Gpm_Open (&conn, 0);
 128         if (mouse_fd == -1)
 129         {
 130             use_mouse_p = MOUSE_NONE;
 131             return;
 132         }
 133         mouse_enabled = TRUE;
 134     }
 135     break;
 136 #endif
 137 
 138     case MOUSE_XTERM_NORMAL_TRACKING:
 139         // save old highlight mouse tracking
 140         printf (ESC_STR "[?1001s");
 141 
 142         // enable mouse tracking
 143         printf (ESC_STR "[?1000h");
 144 
 145         // enable SGR extended mouse reporting
 146         printf (ESC_STR "[?1006h");
 147 
 148         fflush (stdout);
 149         mouse_enabled = TRUE;
 150         break;
 151 
 152     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
 153         // save old highlight mouse tracking
 154         printf (ESC_STR "[?1001s");
 155 
 156         // enable mouse tracking
 157         printf (ESC_STR "[?1002h");
 158 
 159         // enable SGR extended mouse reporting
 160         printf (ESC_STR "[?1006h");
 161 
 162         fflush (stdout);
 163         mouse_enabled = TRUE;
 164         break;
 165 
 166     default:
 167         break;
 168     }
 169 }
 170 
 171 /* --------------------------------------------------------------------------------------------- */
 172 
 173 void
 174 disable_mouse (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 175 {
 176     if (!mouse_enabled)
 177         return;
 178 
 179     mouse_enabled = FALSE;
 180 
 181     switch (use_mouse_p)
 182     {
 183 #ifdef HAVE_LIBGPM
 184     case MOUSE_GPM:
 185         Gpm_Close ();
 186         break;
 187 #endif
 188     case MOUSE_XTERM_NORMAL_TRACKING:
 189         // disable SGR extended mouse reporting
 190         printf (ESC_STR "[?1006l");
 191 
 192         // disable mouse tracking
 193         printf (ESC_STR "[?1000l");
 194 
 195         // restore old highlight mouse tracking
 196         printf (ESC_STR "[?1001r");
 197 
 198         fflush (stdout);
 199         break;
 200     case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
 201         // disable SGR extended mouse reporting
 202         printf (ESC_STR "[?1006l");
 203 
 204         // disable mouse tracking
 205         printf (ESC_STR "[?1002l");
 206 
 207         // restore old highlight mouse tracking
 208         printf (ESC_STR "[?1001r");
 209 
 210         fflush (stdout);
 211         break;
 212     default:
 213         break;
 214     }
 215 }
 216 
 217 /* --------------------------------------------------------------------------------------------- */

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