remove trailing whitespace at end of lines
[reactos.git] / rosapps / mc / src / mouse.c
1 /* Mouse managing
2 Copyright (C) 1994 Miguel de Icaza.
3
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.
8
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.
13
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. */
17
18 /* Events received by clients of this library have their coordinates 0 */
19 /* based */
20
21 /* "$Id$" */
22
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(OS2_NT)
31 # include <termios.h>
32 #endif
33 #include <malloc.h>
34 #include <stdio.h>
35
36 #include "mad.h"
37 #include "mouse.h"
38 #include "global.h" /* ESC_STR */
39 #include "util.h" /* xmalloc */
40 #include "key.h" /* define sequence */
41 #include "tty.h" /* get ncurses header */
42
43 int xmouse_flag = 0;
44
45 /*
46 * This chunk of NCURSES internals is used to support the keyok() function for
47 * NCURSES versions between 1.9.6 (when the mouse code was introduced in 1995),
48 * and 4.2 (when the keyok function will be release).
49 */
50 #ifndef HAVE_KEYOK
51 #ifdef NCURSES_MOUSE_VERSION /* first defined for ncurses 1.9.6 */
52 #ifdef NCURSES_970530 /* defined by configure script */
53 /*
54 * ncurses 1.9.8a ported to QNX doesn't provide the SP pointer as a global
55 * symbol in the library...
56 */
57 #ifndef __QNX__
58 struct tries {
59 struct tries *child;
60 struct tries *sibling;
61 unsigned char ch;
62 unsigned short value;
63 };
64
65 struct screen {
66 int _ifd;
67 FILE *_ofp;
68 #if NCURSES_970530 >= 2
69 char *_setbuf; /*4.0*/
70 #endif
71 int _checkfd;
72 struct term *_term;
73 short _lines;
74 short _columns;
75 #if NCURSES_970530 >= 1
76 short _lines_avail; /*1.9.9g*/
77 short _topstolen; /*1.9.9g*/
78 #endif
79 struct _win_st *_curscr;
80 struct _win_st *_newscr;
81 struct _win_st *_stdscr;
82 struct tries *_keytry; /* "Try" for use with keypad mode */
83 /* there's more, but this is just for alignment */
84 };
85 extern struct screen *SP;
86
87 /*
88 * Remove a code from the specified tree, freeing the unused nodes. Returns
89 * true if the code was found/removed.
90 */
91 static
92 int _nc_remove_key(struct tries **tree, unsigned short code)
93 {
94 struct tries *ptr = (*tree);
95
96 if (code > 0) {
97 while (ptr != 0) {
98 if (_nc_remove_key(&(ptr->child), code)) {
99 return TRUE;
100 }
101 if (ptr->value == code) {
102 *tree = 0;
103 free(ptr);
104 return TRUE;
105 }
106 ptr = ptr->sibling;
107 }
108 }
109 return FALSE;
110 }
111
112 int
113 keyok(int code, bool flag)
114 {
115 _nc_remove_key(&(SP->_keytry), code);
116 return OK;
117 }
118 #endif /* __QNX__ */
119 #endif /* NCURSES_970530 */
120 #endif /* NCURSES_MOUSE_VERSION */
121 #endif /* HAVE_KEYOK */
122
123 #ifdef HAVE_LIBGPM
124 static int mouse_d; /* Handle to the mouse server */
125 #endif
126
127 #ifdef DEBUGMOUSE
128 /* Only used for debugging */
129 static int top_event = 0;
130 FILE *log;
131 #endif
132
133 #ifdef HAVE_LIBGPM
134
135 void show_mouse_pointer (int x, int y)
136 {
137 #ifdef HAVE_LIBGPM
138 if (use_mouse_p == GPM_MOUSE){
139 Gpm_DrawPointer (x, y, gpm_consolefd);
140 }
141 #endif
142 }
143
144 #endif /* HAVE_LIBGPM */
145 #if 0
146 int mouse_handler (Gpm_Event *gpm_event)
147 {
148 MouseEvent *event = mouse_events;
149 int x = last_x = gpm_event->x;
150 int y = last_y = gpm_event->y;
151 int redo = 0;
152
153 /* DEBUGM ((log, "Mouse [%d, %d]\n", x, y)); */
154
155 /* Call any registered event handlers */
156 for (; event; event = (MouseEvent *) event->next){
157 if ((event->x1 <= x) && (x <= event->x2)
158 && (event->y1 <= y) && (y <= event->y2)){
159 gpm_event->x -= event->x1;
160 gpm_event->y -= event->y1;
161 last_mouse_event = event;
162 redo = (*(event->mouse_callback))(gpm_event, event->data);
163 gpm_event->x += event->x1;
164 gpm_event->y += event->y1;
165 break;
166 }
167 }
168 return redo;
169 }
170
171 int redo_mouse (Gpm_Event *event)
172 {
173 if (last_mouse_event){
174 int result;
175 event->x -= last_mouse_event->x1;
176 event->y -= last_mouse_event->y1;
177 result = (*(last_mouse_event->mouse_callback))
178 (event,last_mouse_event->data);
179 event->x += last_mouse_event->x1;
180 event->y += last_mouse_event->y1;
181 return result;
182 }
183 return MOU_NORMAL;
184 }
185 #endif
186
187 void init_mouse (void)
188 {
189 /*
190 * MC's use of xterm mouse is incompatible with NCURSES's support. The
191 * simplest solution is to disable NCURSE's mouse.
192 */
193 #ifdef NCURSES_MOUSE_VERSION
194 /* See the comment above about QNX/ncurses 1.9.8a ... */
195 #ifndef __QNX__
196 keyok(KEY_MOUSE, FALSE);
197 #endif /* __QNX__ */
198 #endif /* NCURSES_MOUSE_VERSION */
199
200 #if defined(NCURSES_970530)
201 #endif
202 switch (use_mouse_p)
203 {
204 #ifdef HAVE_LIBGPM
205 case GPM_MOUSE:
206 {
207 Gpm_Connect conn;
208
209 conn.eventMask = ~GPM_MOVE;
210 conn.defaultMask = GPM_MOVE;
211 conn.minMod = 0;
212 conn.maxMod = 0;
213
214 if ((mouse_d = Gpm_Open (&conn, 0)) == -1)
215 return;
216
217 #ifdef DEBUGMOUSE
218 log = fopen ("mouse.log", "w");
219 #endif
220 }
221 break;
222 #endif /* HAVE_LIBGPM */
223 case XTERM_MOUSE:
224 if (!xmouse_flag) {
225
226 /* save old highlight mouse tracking */
227 printf("%c[?1001s",27);
228
229 /* enable mouse tracking */
230 printf("%c[?1000h",27);
231
232 fflush (stdout);
233 /* turn on */
234 xmouse_flag = 1;
235 define_sequence (MCKEY_MOUSE, ESC_STR "[M", MCKEY_NOACTION);
236 }
237 break;
238 default:
239 /* nothing */
240 break;
241 } /* switch (use_mouse_p) */
242 }
243
244 void shut_mouse (void)
245 {
246 switch (use_mouse_p){
247 #ifdef HAVE_LIBGPM
248 case GPM_MOUSE:
249 Gpm_Close ();
250 break;
251 #endif
252 case XTERM_MOUSE:
253 if (xmouse_flag) {
254
255 /* disable mouse tracking */
256 /* Changed the 1 for an 'l' below: */
257 printf("%c[?1000l",27);
258
259 /* restore old highlight mouse tracking */
260 printf("%c[?1001r",27);
261
262 fflush (stdout);
263 /* off */
264 xmouse_flag = 0;
265 }
266 break;
267 default:
268 /* nothing */
269 break;
270 }
271 }
272
273 #ifdef DEBUGMOUSE
274 void mouse_log (char *function, char *file, int line)
275 {
276 fprintf (log, "%s called from %s:%d\n", function, file, line);
277 }
278 #endif
279