1999-09-09 Federico Mena Quintero <federico@redhat.com>
[midnight-commander.git] / src / mouse.c
blob1c4344d6d877ebfc50f6ff575d035c7570adb382
1 /* Mouse managing
2 Copyright (C) 1994 Miguel de Icaza.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Events received by clients of this library have their coordinates 0 */
19 /* based */
21 /* "$Id$" */
23 #include <config.h>
24 #include <sys/types.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <signal.h> /* For kill() and SIGQUIT */
29 #include <fcntl.h>
30 #if (!defined(__IBMC__) && !defined(__IBMCPP__)) && !defined(HAS_NO_TERMIOS_H)
31 # include <termios.h>
32 #endif
33 #include <stdio.h>
34 #include "global.h"
35 #include "mouse.h"
36 #include "key.h" /* define sequence */
37 #include "tty.h" /* get ncurses header */
39 int xmouse_flag = 0;
42 * This chunk of NCURSES internals is used to support the keyok() function for
43 * NCURSES versions between 1.9.6 (when the mouse code was introduced in 1995),
44 * and 4.2 (when the keyok function will be release).
46 #ifndef HAVE_KEYOK
47 #ifdef NCURSES_MOUSE_VERSION /* first defined for ncurses 1.9.6 */
48 #ifdef NCURSES_970530 /* defined by configure script */
50 * ncurses 1.9.8a ported to QNX doesn't provide the SP pointer as a global
51 * symbol in the library...
53 #ifndef __QNX__
54 struct tries {
55 struct tries *child;
56 struct tries *sibling;
57 unsigned char ch;
58 unsigned short value;
61 struct screen {
62 int _ifd;
63 FILE *_ofp;
64 #if NCURSES_970530 >= 2
65 char *_setbuf; /*4.0*/
66 #endif
67 int _checkfd;
68 struct term *_term;
69 short _lines;
70 short _columns;
71 #if NCURSES_970530 >= 1
72 short _lines_avail; /*1.9.9g*/
73 short _topstolen; /*1.9.9g*/
74 #endif
75 struct _win_st *_curscr;
76 struct _win_st *_newscr;
77 struct _win_st *_stdscr;
78 struct tries *_keytry; /* "Try" for use with keypad mode */
79 /* there's more, but this is just for alignment */
81 extern struct screen *SP;
84 * Remove a code from the specified tree, freeing the unused nodes. Returns
85 * true if the code was found/removed.
87 static
88 int _nc_remove_key(struct tries **tree, unsigned short code)
90 struct tries *ptr = (*tree);
92 if (code > 0) {
93 while (ptr != 0) {
94 if (_nc_remove_key(&(ptr->child), code)) {
95 return TRUE;
97 if (ptr->value == code) {
98 *tree = 0;
99 g_free (ptr);
100 return TRUE;
102 ptr = ptr->sibling;
105 return FALSE;
109 keyok(int code, bool flag)
111 _nc_remove_key(&(SP->_keytry), code);
112 return OK;
114 #endif /* __QNX__ */
115 #endif /* NCURSES_970530 */
116 #endif /* NCURSES_MOUSE_VERSION */
117 #endif /* HAVE_KEYOK */
119 #ifdef HAVE_LIBGPM
120 static int mouse_d; /* Handle to the mouse server */
121 #endif
123 #ifdef DEBUGMOUSE
124 /* Only used for debugging */
125 static int top_event = 0;
126 FILE *log;
127 #endif
129 #ifdef HAVE_LIBGPM
131 void show_mouse_pointer (int x, int y)
133 #ifdef HAVE_LIBGPM
134 if (use_mouse_p == GPM_MOUSE){
135 Gpm_DrawPointer (x, y, gpm_consolefd);
137 #endif
140 #endif /* HAVE_LIBGPM */
141 #if 0
142 int mouse_handler (Gpm_Event *gpm_event)
144 MouseEvent *event = mouse_events;
145 int x = last_x = gpm_event->x;
146 int y = last_y = gpm_event->y;
147 int redo = 0;
149 /* DEBUGM ((log, "Mouse [%d, %d]\n", x, y)); */
151 /* Call any registered event handlers */
152 for (; event; event = (MouseEvent *) event->next){
153 if ((event->x1 <= x) && (x <= event->x2)
154 && (event->y1 <= y) && (y <= event->y2)){
155 gpm_event->x -= event->x1;
156 gpm_event->y -= event->y1;
157 last_mouse_event = event;
158 redo = (*(event->mouse_callback))(gpm_event, event->data);
159 gpm_event->x += event->x1;
160 gpm_event->y += event->y1;
161 break;
164 return redo;
167 int redo_mouse (Gpm_Event *event)
169 if (last_mouse_event){
170 int result;
171 event->x -= last_mouse_event->x1;
172 event->y -= last_mouse_event->y1;
173 result = (*(last_mouse_event->mouse_callback))
174 (event,last_mouse_event->data);
175 event->x += last_mouse_event->x1;
176 event->y += last_mouse_event->y1;
177 return result;
179 return MOU_NORMAL;
181 #endif
183 void init_mouse (void)
186 * MC's use of xterm mouse is incompatible with NCURSES's support. The
187 * simplest solution is to disable NCURSE's mouse.
189 #ifdef NCURSES_MOUSE_VERSION
190 /* See the comment above about QNX/ncurses 1.9.8a ... */
191 #ifndef __QNX__
192 keyok(KEY_MOUSE, FALSE);
193 #endif /* __QNX__ */
194 #endif /* NCURSES_MOUSE_VERSION */
196 #if defined(NCURSES_970530)
197 #endif
198 switch (use_mouse_p)
200 #ifdef HAVE_LIBGPM
201 case GPM_MOUSE:
203 Gpm_Connect conn;
205 conn.eventMask = ~GPM_MOVE;
206 conn.defaultMask = GPM_MOVE;
207 conn.minMod = 0;
208 conn.maxMod = 0;
210 if ((mouse_d = Gpm_Open (&conn, 0)) == -1)
211 return;
213 #ifdef DEBUGMOUSE
214 log = fopen ("mouse.log", "w");
215 #endif
217 break;
218 #endif /* HAVE_LIBGPM */
219 case XTERM_MOUSE:
220 if (!xmouse_flag) {
222 /* save old highlight mouse tracking */
223 printf("%c[?1001s",27);
225 /* enable mouse tracking */
226 printf("%c[?1000h",27);
228 fflush (stdout);
229 /* turn on */
230 xmouse_flag = 1;
231 define_sequence (MCKEY_MOUSE, ESC_STR "[M", MCKEY_NOACTION);
233 break;
234 default:
235 /* nothing */
236 break;
237 } /* switch (use_mouse_p) */
240 void shut_mouse (void)
242 switch (use_mouse_p){
243 #ifdef HAVE_LIBGPM
244 case GPM_MOUSE:
245 Gpm_Close ();
246 break;
247 #endif
248 case XTERM_MOUSE:
249 if (xmouse_flag) {
251 /* disable mouse tracking */
252 /* Changed the 1 for an 'l' below: */
253 printf("%c[?1000l",27);
255 /* restore old highlight mouse tracking */
256 printf("%c[?1001r",27);
258 fflush (stdout);
259 /* off */
260 xmouse_flag = 0;
262 break;
263 default:
264 /* nothing */
265 break;
269 #ifdef DEBUGMOUSE
270 void mouse_log (char *function, char *file, int line)
272 fprintf (log, "%s called from %s:%d\n", function, file, line);
274 #endif