1 /* hhanemaa@cs.ruu.nl */
2 /* Mouse library functions */
3
4 #include <signal.h>
5 #include <sys/time.h>
6 #include <sys/types.h>
7 #include <vga.h>
8 #include "vgamouse.h"
9 #include "../libvga.h"
10
11 extern int mouse_open;
12
13 void (*__svgalib_mouse_eventhandler) (int, int, int, int, int, int, int) = NULL;
14
15 static struct MouseCaps mouse_caps = {0, 0, 0, 0, 0, 0};
16
17 #include "ms.c" /* include low-level mouse driver */
18
19
20 static struct sigaction oldsiga;
21 static void (*currentinthandler) (int);
22
inthandler(int signal)23 static void inthandler(int signal)
24 {
25 mouse_close();
26 /* restore old interrupt */
27 sigaction(SIGINT, &oldsiga, NULL);
28 raise(SIGINT);
29 }
30
31 static void default_handler(int, int, int, int, int, int, int);
32
mouse_init_return_fd(char * dev,int type,int samplerate)33 int mouse_init_return_fd(char *dev, int type, int samplerate)
34 {
35 struct sigaction siga;
36
37 if (!mouse_open) {
38 if (strcmp(dev, "") == 0)
39 m_dev = "/dev/mouse";
40 else
41 m_dev = dev;
42 m_type = type & MOUSE_TYPE_MASK;
43 m_modem_ctl = type & ~MOUSE_TYPE_MASK;
44
45 m_sample = samplerate;
46
47 currentinthandler = NULL;
48
49 /* Initialize mouse device. */
50 if (m_type == MOUSE_NONE || m_type < MOUSE_MICROSOFT || m_type > MOUSE_LAST)
51 return -1;
52 if (ms_init())
53 return -1;
54
55 /* Install default mouse handler. Old coordinates are preserved. */
56 __svgalib_mouse_eventhandler = default_handler;
57
58 /* Install interrupt handler. */
59 currentinthandler = inthandler;
60 siga.sa_handler = inthandler;
61 siga.sa_flags = 0;
62 zero_sa_mask(&(siga.sa_mask));
63 sigaction(SIGINT, &siga, &oldsiga);
64 mouse_open = 1;
65 }
66 return __svgalib_mouse_fd; /* Return mouse fd. */
67 }
68
69 /* Old compatible mouse_init.
70 Returns 0 if succesful, -1 on error. */
71
mouse_init(char * dev,int type,int samplerate)72 int mouse_init(char *dev, int type, int samplerate)
73 {
74 if (mouse_open)
75 return 0;
76 if (mouse_init_return_fd(dev, type, samplerate) == -1)
77 return -1;
78 else {
79 mouse_open = 1;
80 return 0;
81 }
82 }
83
84 /* Reads an event from the mouse buffer.
85 Warning: For now, we assume there's no console switching. */
86
mouse_update()87 int mouse_update()
88 {
89 int result;
90 result = get_ms_event(0);
91 return result;
92 }
93
mouse_waitforupdate()94 void mouse_waitforupdate()
95 {
96 #if 0
97 fd_set *readfds, writefds, exceptfds;
98 struct timeval timeout;
99 FD_ZERO(readfds);
100 FD_ZERO(writefds);
101 FD_ZERO(exceptfds);
102 FD_SET(__svgalib_mouse_fd, readfds);
103 /* need to setup timeout. */
104 select(readfds, writefds, exceptfds, &timeout);
105 #else
106 get_ms_event(1);
107 #endif
108 return;
109 }
110
mouse_seteventhandler(__mouse_handler handler)111 void mouse_seteventhandler(__mouse_handler handler)
112 {
113 __svgalib_mouse_eventhandler = handler;
114 }
115
mouse_close()116 void mouse_close()
117 {
118 if(mouse_open) {
119 ms_close();
120 if (currentinthandler != NULL)
121 /* Restore old interrupt. */
122 sigaction(SIGINT, &oldsiga, NULL);
123 }
124 mouse_open=0;
125 }
126
127
128 /* Default event handler. */
129
mouse_setdefaulteventhandler()130 void mouse_setdefaulteventhandler()
131 {
132 __svgalib_mouse_eventhandler = default_handler;
133 }
134
135 static int mouse_x = 0;
136 static int mouse_y = 0;
137 static int mouse_z = 0;
138 static int mouse_rx = 0;
139 static int mouse_ry = 0;
140 static int mouse_rz = 0;
141 static int mouse_button = 0;
142 static int scale = 1;
143 static int minx = 0;
144 static int maxx = 32767;
145 static int miny = 0;
146 static int maxy = 32767;
147 static int minz = 0;
148 static int maxz = 32767;
149 static int minrx = 0;
150 static int maxrx = 32767;
151 static int minry = 0;
152 static int maxry = 32767;
153 static int minrz = 0;
154 static int maxrz = 32767;
155 static int wrap = 0;
156 static int wantcaps = 0;
157
158 /* Sets mouse wrap state*/
mouse_setwrap(int state)159 void mouse_setwrap(int state)
160 {
161 wrap = state;
162 }
163
default_handler(int button,int dx,int dy,int dz,int drx,int dry,int drz)164 static void default_handler(int button, int dx, int dy, int dz, int drx,
165 int dry, int drz)
166 {
167 mouse_button = button;
168 mouse_x += dx;
169 mouse_y += dy;
170 mouse_z += dz;
171 if (mouse_x / scale > maxx) {
172 if (wrap & MOUSE_WRAPX)
173 mouse_x -= (maxx - minx) * scale;
174 else
175 mouse_x = maxx * scale;
176 }
177 /* - 1; ??? */
178 if (mouse_x / scale < minx) {
179 if (wrap & MOUSE_WRAPX)
180 mouse_x += (maxx - minx) * scale;
181 else
182 mouse_x = minx * scale;
183 }
184 if (mouse_y / scale > maxy) {
185 if (wrap & MOUSE_WRAPY)
186 mouse_y -= (maxy - miny) * scale;
187 else
188 mouse_y = maxy * scale;
189 }
190 /* - 1; ??? */
191 if (mouse_y / scale < miny) {
192 if (wrap & MOUSE_WRAPY)
193 mouse_y += (maxy - miny) * scale;
194 else
195 mouse_y = miny * scale;
196 }
197 if (mouse_z / scale > maxz) {
198 if (wrap & MOUSE_WRAPZ)
199 mouse_z -= (maxz - minz) * scale;
200 else
201 mouse_z = maxz * scale;
202 }
203 /* - 1; ??? */
204 if (mouse_z / scale < minz) {
205 if (wrap & MOUSE_WRAPZ)
206 mouse_z += (maxz - minz) * scale;
207 else
208 mouse_z = minz * scale;
209 }
210 switch (wrap & MOUSE_ROT_COORDS) {
211 case MOUSE_ROT_INFINITESIMAL:
212 mouse_rx = drx;
213 mouse_ry = dry;
214 mouse_rz = drz;
215 break;
216 case MOUSE_ROT_RX_RY_RZ:
217 mouse_rx += drx;
218 mouse_ry += dry;
219 mouse_rz += drz;
220 break;
221 }
222 if (mouse_rx / scale > maxrx) {
223 if (wrap & MOUSE_WRAPRX)
224 mouse_rx -= (maxrx - minrx) * scale;
225 else
226 mouse_rx = maxrx * scale;
227 }
228 /* - 1; ??? */
229 if (mouse_rx / scale < minrx) {
230 if (wrap & MOUSE_WRAPRX)
231 mouse_rx += (maxrx - minrx) * scale;
232 else
233 mouse_rx = minrx * scale;
234 }
235 if (mouse_ry / scale > maxry) {
236 if (wrap & MOUSE_WRAPRY)
237 mouse_ry -= (maxry - minry) * scale;
238 else
239 mouse_ry = maxry * scale;
240 }
241 /* - 1; ??? */
242 if (mouse_ry / scale < minry) {
243 if (wrap & MOUSE_WRAPRY)
244 mouse_ry += (maxry - minry) * scale;
245 else
246 mouse_ry = minry * scale;
247 }
248 if (mouse_rz / scale > maxrz) {
249 if (wrap & MOUSE_WRAPRZ)
250 mouse_rz -= (maxrz - minrz) * scale;
251 else
252 mouse_rz = maxrz * scale;
253 }
254 /* - 1; ??? */
255 if (mouse_rz / scale < minrz) {
256 if (wrap & MOUSE_WRAPRZ)
257 mouse_rz += (maxrz - minrz) * scale;
258 else
259 mouse_rz = minrz * scale;
260 }
261 }
262
263 /* Sets the mouse position */
mouse_setposition(int x,int y)264 void mouse_setposition(int x, int y)
265 {
266 mouse_x = x * scale;
267 mouse_y = y * scale;
268 }
269
270 /* Set the mouse position for up to six dimensions. */
mouse_setposition_6d(int x,int y,int z,int rx,int ry,int rz,int dim_mask)271 void mouse_setposition_6d(int x, int y, int z, int rx, int ry, int rz,
272 int dim_mask)
273 {
274 if (dim_mask & MOUSE_XDIM)
275 mouse_x = x * scale;
276 if (dim_mask & MOUSE_YDIM)
277 mouse_y = y * scale;
278 if (dim_mask & MOUSE_ZDIM)
279 mouse_z = z * scale;
280 if (dim_mask & MOUSE_RXDIM)
281 mouse_rx = rx * scale;
282 if (dim_mask & MOUSE_RYDIM)
283 mouse_ry = ry * scale;
284 if (dim_mask & MOUSE_RZDIM)
285 mouse_rz = rz * scale;
286 if ((dim_mask & MOUSE_CAPS) && (x == MOUSE_WANTCAPS))
287 wantcaps = 1;
288 }
289
290 /* Set the mouse range along the x axis. */
mouse_setxrange(int x1,int x2)291 void mouse_setxrange(int x1, int x2)
292 {
293 minx = x1;
294 maxx = x2;
295 }
296
297 /* Set the mouse range along the y axis. */
mouse_setyrange(int y1,int y2)298 void mouse_setyrange(int y1, int y2)
299 {
300 miny = y1;
301 maxy = y2;
302 }
303
304 /* Set the mouse range for up to six dimensions. */
mouse_setrange_6d(int x1,int x2,int y1,int y2,int z1,int z2,int rx1,int rx2,int ry1,int ry2,int rz1,int rz2,int dim_mask)305 void mouse_setrange_6d(int x1, int x2, int y1, int y2, int z1, int z2,
306 int rx1, int rx2, int ry1, int ry2, int rz1, int rz2, int dim_mask)
307 {
308
309 if (dim_mask & MOUSE_XDIM) {
310 minx = x1;
311 maxx = x2;
312 }
313 if (dim_mask & MOUSE_YDIM) {
314 miny = y1;
315 maxy = y2;
316 }
317 if (dim_mask & MOUSE_ZDIM) {
318 minz = z1;
319 maxz = z2;
320 }
321 if (dim_mask & MOUSE_RXDIM) {
322 minrx = rx1;
323 maxrx = rx2;
324 }
325 if (dim_mask & MOUSE_RYDIM) {
326 minry = ry1;
327 maxry = ry2;
328 }
329 if (dim_mask & MOUSE_RZDIM) {
330 minrz = rz1;
331 maxrz = rz2;
332 }
333 }
334
335 /* set the sensitivity of the mouse
336 This routine sets the scale factor between the motion reported by the
337 mouse and the size of one pixel. The larger scale is, the slower the
338 mouse cursor apears to move.
339 Bugs: Scale cannot be set less than one, since it is an integer. This
340 means that there is no war to make the mouse faster than it inherently
341 is.*/
mouse_setscale(int s)342 void mouse_setscale(int s)
343 {
344 if (scale==0)
345 printf("Mouse scale must be non-zero!\n");
346 else {
347 mouse_x = (mouse_x*s)/scale;
348 mouse_y = (mouse_y*s)/scale;
349 mouse_z = (mouse_z*s)/scale;
350 mouse_rx = (mouse_rx*s)/scale;
351 mouse_ry = (mouse_ry*s)/scale;
352 mouse_rz = (mouse_rz*s)/scale;
353 scale = s;
354 }
355 }
356
357
mouse_getx()358 int mouse_getx()
359 {
360 return mouse_x / scale;
361 }
362
mouse_gety()363 int mouse_gety()
364 {
365 return mouse_y / scale;
366 }
367
mouse_getposition_6d(int * x,int * y,int * z,int * rx,int * ry,int * rz)368 void mouse_getposition_6d(int *x, int *y, int *z, int *rx, int *ry, int *rz)
369 {
370 if (wantcaps) {
371 /* Return the mouse capabilities */
372 *x = mouse_caps.key;
373 *y = mouse_caps.buttons;
374 *z = mouse_caps.axes;
375 *rx = mouse_caps.info;
376 *ry = mouse_caps.reserved0;
377 *rz = mouse_caps.reserved1;
378 wantcaps = 0;
379 return;
380 }
381 if (x)
382 *x = mouse_x / scale;
383 if (y)
384 *y = mouse_y / scale;
385 if (z)
386 *z = mouse_z / scale;
387 if (rx)
388 *rx = mouse_rx / scale;
389 if (ry)
390 *ry = mouse_ry / scale;
391 if (rz)
392 *rz = mouse_rz / scale;
393 }
394
mouse_getbutton()395 int mouse_getbutton()
396 {
397 return mouse_button;
398 }
399