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