Value accuracy of mouse_enabled global variable.
[midnight-commander.git] / lib / tty / mouse.c
blobd2f917f3272451f00ddf1f637a6eb766f16569f7
1 /* Mouse managing
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
3 2007, 2009 Free Software Foundation, Inc.
5 Written by:
6 Andrew Borodin <aborodin@vmail.ru>, 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file mouse.c
23 * \brief Source: mouse managing
25 * Events received by clients of this library have their coordinates 0 based
28 #include <config.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <unistd.h>
34 #include "lib/global.h"
36 #include "tty.h"
37 #include "tty-internal.h" /* mouse_enabled */
38 #include "mouse.h"
39 #include "key.h" /* define sequence */
41 /*** global variables ****************************************************************************/
43 Mouse_Type use_mouse_p = MOUSE_NONE;
44 gboolean mouse_enabled = FALSE;
45 const char *xmouse_seq;
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 /*** file scope functions ************************************************************************/
54 /* --------------------------------------------------------------------------------------------- */
56 /* --------------------------------------------------------------------------------------------- */
57 /*** public functions ****************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 void
61 show_mouse_pointer (int x, int y)
63 #ifdef HAVE_LIBGPM
64 if (use_mouse_p == MOUSE_GPM)
65 Gpm_DrawPointer (x, y, gpm_consolefd);
66 #else
67 (void) x;
68 (void) y;
69 #endif /* HAVE_LIBGPM */
72 /* --------------------------------------------------------------------------------------------- */
74 void
75 init_mouse (void)
77 switch (use_mouse_p)
79 #ifdef HAVE_LIBGPM
80 case MOUSE_NONE:
81 use_mouse_p = MOUSE_GPM;
82 break;
83 #endif /* HAVE_LIBGPM */
85 case MOUSE_XTERM_NORMAL_TRACKING:
86 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
87 define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
88 break;
90 default:
91 break;
94 enable_mouse ();
97 /* --------------------------------------------------------------------------------------------- */
99 void
100 enable_mouse (void)
102 if (mouse_enabled)
103 return;
105 switch (use_mouse_p)
107 #ifdef HAVE_LIBGPM
108 case MOUSE_GPM:
110 int mouse_d;
111 Gpm_Connect conn;
113 conn.eventMask = ~GPM_MOVE;
114 conn.defaultMask = GPM_MOVE;
115 conn.minMod = 0;
116 conn.maxMod = 0;
118 mouse_d = Gpm_Open (&conn, 0);
119 if (mouse_d == -1)
121 use_mouse_p = MOUSE_NONE;
122 return;
124 mouse_enabled = TRUE;
126 break;
127 #endif /* HAVE_LIBGPM */
129 case MOUSE_XTERM_NORMAL_TRACKING:
130 /* save old highlight mouse tracking */
131 printf (ESC_STR "[?1001s");
133 /* enable mouse tracking */
134 printf (ESC_STR "[?1000h");
136 fflush (stdout);
137 mouse_enabled = TRUE;
138 break;
140 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
141 /* save old highlight mouse tracking */
142 printf (ESC_STR "[?1001s");
144 /* enable mouse tracking */
145 printf (ESC_STR "[?1002h");
147 fflush (stdout);
148 mouse_enabled = TRUE;
149 break;
151 default:
152 break;
156 /* --------------------------------------------------------------------------------------------- */
158 void
159 disable_mouse (void)
161 if (!mouse_enabled)
162 return;
164 mouse_enabled = FALSE;
166 switch (use_mouse_p)
168 #ifdef HAVE_LIBGPM
169 case MOUSE_GPM:
170 Gpm_Close ();
171 break;
172 #endif
173 case MOUSE_XTERM_NORMAL_TRACKING:
174 /* disable mouse tracking */
175 printf (ESC_STR "[?1000l");
177 /* restore old highlight mouse tracking */
178 printf (ESC_STR "[?1001r");
180 fflush (stdout);
181 break;
182 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
183 /* disable mouse tracking */
184 printf (ESC_STR "[?1002l");
186 /* restore old highlight mouse tracking */
187 printf (ESC_STR "[?1001r");
189 fflush (stdout);
190 break;
191 default:
192 break;
196 /* --------------------------------------------------------------------------------------------- */