Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / lib / tty / mouse.c
blob299eca9e7e53eff23f677009a9f658fa73a891ab
1 /*
2 Mouse managing
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2006,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Andrew Borodin <aborodin@vmail.ru>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file mouse.c
28 * \brief Source: mouse managing
30 * Events received by clients of this library have their coordinates 0 based
33 #include <config.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <unistd.h>
39 #include "lib/global.h"
41 #include "tty.h"
42 #include "tty-internal.h" /* mouse_enabled */
43 #include "mouse.h"
44 #include "key.h" /* define sequence */
46 /*** global variables ****************************************************************************/
48 Mouse_Type use_mouse_p = MOUSE_NONE;
49 gboolean mouse_enabled = FALSE;
50 const char *xmouse_seq;
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 /* --------------------------------------------------------------------------------------------- */
62 /*** public functions ****************************************************************************/
63 /* --------------------------------------------------------------------------------------------- */
65 void
66 show_mouse_pointer (int x, int y)
68 #ifdef HAVE_LIBGPM
69 if (use_mouse_p == MOUSE_GPM)
70 Gpm_DrawPointer (x, y, gpm_consolefd);
71 #else
72 (void) x;
73 (void) y;
74 #endif /* HAVE_LIBGPM */
77 /* --------------------------------------------------------------------------------------------- */
79 void
80 init_mouse (void)
82 switch (use_mouse_p)
84 #ifdef HAVE_LIBGPM
85 case MOUSE_NONE:
86 use_mouse_p = MOUSE_GPM;
87 break;
88 #endif /* HAVE_LIBGPM */
90 case MOUSE_XTERM_NORMAL_TRACKING:
91 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
92 define_sequence (MCKEY_MOUSE, xmouse_seq, MCKEY_NOACTION);
93 break;
95 default:
96 break;
99 enable_mouse ();
102 /* --------------------------------------------------------------------------------------------- */
104 void
105 enable_mouse (void)
107 if (mouse_enabled)
108 return;
110 switch (use_mouse_p)
112 #ifdef HAVE_LIBGPM
113 case MOUSE_GPM:
115 int mouse_d;
116 Gpm_Connect conn;
118 conn.eventMask = ~GPM_MOVE;
119 conn.defaultMask = GPM_MOVE;
120 conn.minMod = 0;
121 conn.maxMod = 0;
123 mouse_d = Gpm_Open (&conn, 0);
124 if (mouse_d == -1)
126 use_mouse_p = MOUSE_NONE;
127 return;
129 mouse_enabled = TRUE;
131 break;
132 #endif /* HAVE_LIBGPM */
134 case MOUSE_XTERM_NORMAL_TRACKING:
135 /* save old highlight mouse tracking */
136 printf (ESC_STR "[?1001s");
138 /* enable mouse tracking */
139 printf (ESC_STR "[?1000h");
141 fflush (stdout);
142 mouse_enabled = TRUE;
143 break;
145 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
146 /* save old highlight mouse tracking */
147 printf (ESC_STR "[?1001s");
149 /* enable mouse tracking */
150 printf (ESC_STR "[?1002h");
152 fflush (stdout);
153 mouse_enabled = TRUE;
154 break;
156 default:
157 break;
161 /* --------------------------------------------------------------------------------------------- */
163 void
164 disable_mouse (void)
166 if (!mouse_enabled)
167 return;
169 mouse_enabled = FALSE;
171 switch (use_mouse_p)
173 #ifdef HAVE_LIBGPM
174 case MOUSE_GPM:
175 Gpm_Close ();
176 break;
177 #endif
178 case MOUSE_XTERM_NORMAL_TRACKING:
179 /* disable mouse tracking */
180 printf (ESC_STR "[?1000l");
182 /* restore old highlight mouse tracking */
183 printf (ESC_STR "[?1001r");
185 fflush (stdout);
186 break;
187 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
188 /* disable mouse tracking */
189 printf (ESC_STR "[?1002l");
191 /* restore old highlight mouse tracking */
192 printf (ESC_STR "[?1001r");
194 fflush (stdout);
195 break;
196 default:
197 break;
201 /* --------------------------------------------------------------------------------------------- */