Merge branch '2833_url_port_in_history'
[midnight-commander.git] / lib / tty / mouse.c
blob58eaca26b85dfc6c441b9244310f7bb61a4a7820
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 /* enable urxvt extended mouse coordinate reporting */
142 printf (ESC_STR "[?1015h");
144 fflush (stdout);
145 mouse_enabled = TRUE;
146 break;
148 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
149 /* save old highlight mouse tracking */
150 printf (ESC_STR "[?1001s");
152 /* enable mouse tracking */
153 printf (ESC_STR "[?1002h");
155 /* enable urxvt extended mouse coordinate reporting */
156 printf (ESC_STR "[?1015h");
158 fflush (stdout);
159 mouse_enabled = TRUE;
160 break;
162 default:
163 break;
167 /* --------------------------------------------------------------------------------------------- */
169 void
170 disable_mouse (void)
172 if (!mouse_enabled)
173 return;
175 mouse_enabled = FALSE;
177 switch (use_mouse_p)
179 #ifdef HAVE_LIBGPM
180 case MOUSE_GPM:
181 Gpm_Close ();
182 break;
183 #endif
184 case MOUSE_XTERM_NORMAL_TRACKING:
185 /* disable urxvt extended mouse coordinate reporting */
186 printf (ESC_STR "[?1015l");
188 /* disable mouse tracking */
189 printf (ESC_STR "[?1000l");
191 /* restore old highlight mouse tracking */
192 printf (ESC_STR "[?1001r");
194 fflush (stdout);
195 break;
196 case MOUSE_XTERM_BUTTON_EVENT_TRACKING:
197 /* disable urxvt extended mouse coordinate reporting */
198 printf (ESC_STR "[?1015l");
200 /* disable mouse tracking */
201 printf (ESC_STR "[?1002l");
203 /* restore old highlight mouse tracking */
204 printf (ESC_STR "[?1001r");
206 fflush (stdout);
207 break;
208 default:
209 break;
213 /* --------------------------------------------------------------------------------------------- */