Debian Codegraph @

ddio/lnxmouse.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ddio_lnx/lnxmouse.cpp $
21 * $Revision: 1.10 $
22 * $Date: 2000/07/07 03:47:19 $
23 * $Author: hercules $
24 *
25 * Linux mouse routines
26 *
27 * $Log: lnxmouse.cpp,v $
28 * Revision 1.10 2000/07/07 03:47:19 hercules
29 * Scale the ball values to mouse range (64000 -> 640)
30 *
31 * Revision 1.9 2000/07/04 00:30:04 icculus
32 * Again.
33 *
34 * Revision 1.8 2000/07/04 00:29:00 icculus
35 * Cleanups. Removed chunks of commented-out, abandoned attempts, and some
36 * no-longer used global variables.
37 *
38 * Revision 1.7 2000/06/29 10:38:45 hercules
39 * Don't warp the mouse during mouse reset
40 *
41 * Revision 1.6 2000/06/29 09:51:53 hercules
42 * Added support for wheel mouse
43 *
44 * Revision 1.5 2000/06/29 06:41:23 icculus
45 * mad commits.
46 *
47 * Revision 1.4 2000/06/24 01:15:15 icculus
48 * patched to compile.
49 *
50 * Revision 1.3 2000/04/28 20:13:42 icculus
51 * Took out some mprintfs
52 *
53 * Revision 1.2 2000/04/27 11:36:29 icculus
54 * Rewritten to use SDL.
55 *
56 * Revision 1.1.1.1 2000/04/18 00:00:33 icculus
57 * initial checkin
58 *
59 *
60 * 17 9/09/99 4:36p Jeff
61 * use DGA extension for much better mouse control
62 *
63 * 16 9/07/99 4:35p Jeff
64 * added a call to XSync() to attempt to better the mouse performance...
65 *
66 * 15 8/22/99 10:00p Jeff
67 * attempt to better the mouse motion...but it's not working...XWindows
68 * seems to buffer up motion motion events when the mouse moves very
69 * small. Added support for up to 5 mouse buttons
70 *
71 * 14 8/19/99 2:24p Jeff
72 * mouse resets to center of the screen
73 *
74 * 13 7/14/99 9:06p Jeff
75 * added comment header
76 *
77 * $NoKeywords: $
78 */
79
80 // ----------------------------------------------------------------------------
81 // Mouse Interface
82 // ----------------------------------------------------------------------------
83
84 #include <cstring>
85 #include <SDL.h>
86
87 #include "pserror.h"
88 #include "psclass.h"
89 #include "ddio.h"
90 #include "ddio_lnx.h"
91
92 bool ddio_mouseGrabbed = false;
93 static bool DDIO_mouse_init = false;
94
95 static struct mses_state {
96 int fd; // file descriptor of mouse
97 int t, l, b, r; // limit rectangle of absolute mouse coords
98 int x, y, dx, dy, cx, cy; // current x,y,z in absolute mouse coords
99 int btn_mask;
100 } DDIO_mouse_state;
101
102 struct t_mse_button_info {
103 bool is_down[N_MSEBTNS];
104 uint8_t down_count[N_MSEBTNS];
105 uint8_t up_count[N_MSEBTNS];
106 float time_down[N_MSEBTNS];
107 float time_up[N_MSEBTNS];
108 };
109
110 struct t_mse_event {
111 int16_t btn;
112 int16_t state;
113 };
114
115 static t_mse_button_info DIM_buttons;
116 static tQueue<t_mse_event, 16> MB_queue;
117 static int Mouse_mode = MOUSE_STANDARD_MODE;
118
119 // ---------------------------------------------------------------------------
120 // Initialization Functions
121
122 bool ddio_MouseInit(void) {
123 DDIO_mouse_init = true;
124 ddio_MouseReset();
125 return true;
126 }
127
128 int ddio_MouseGetCaps(int *buttons, int *axes) {
129 *buttons = 6;
130 *axes = 2;
131 return MOUSE_LB | MOUSE_CB | MOUSE_RB;
132 }
133
134 void ddio_MouseClose() { DDIO_mouse_init = false; }
135
136 void ddio_MouseSetLimits(int left, int top, int right, int bottom, int zmin, int zmax) {
137 DDIO_mouse_state.l = left;
138 DDIO_mouse_state.t = top;
139 DDIO_mouse_state.r = right;
140 DDIO_mouse_state.b = bottom;
141 }
142
143 void ddio_MouseGetLimits(int *left, int *top, int *right, int *bottom, int *zmin, int *zmax) {
144 *left = DDIO_mouse_state.l;
145 *top = DDIO_mouse_state.t;
146 *right = DDIO_mouse_state.r;
147 *bottom = DDIO_mouse_state.b;
148
149 if (zmin)
150 *zmin = 0;
151 if (zmax)
152 *zmax = 10;
153 }
154
155 void ddio_MouseReset() {
156 ddio_MouseSetLimits(0, 0, Lnx_app_obj->m_W, Lnx_app_obj->m_H);
157 DDIO_mouse_state.btn_mask = 0;
158
159 // reset button states
160 ddio_MouseQueueFlush();
161
162 /*
163 if(Mouse_mode==MOUSE_STANDARD_MODE)
164 return;
165 bool use_dga = false;
166
167 if(Lnx_app_obj && Lnx_app_obj->m_Flags&APPFLAG_DGAMOUSE)
168 use_dga = true;
169
170 // warp the mouse.
171 if(!use_dga)
172 {
173 XWarpPointer(Lnx_app_obj->m_Display, None, Lnx_app_obj->m_Window,
174 0,0,0,0, Lnx_app_obj->m_W/2, Lnx_app_obj->m_H/2);
175 }
176 */
177
178 DDIO_mouse_state.x = DDIO_mouse_state.cx = Lnx_app_obj->m_W / 2;
179 DDIO_mouse_state.y = DDIO_mouse_state.cy = Lnx_app_obj->m_H / 2;
180 DDIO_mouse_state.dy = DDIO_mouse_state.dx = 0;
181 }
182
183 // displays the mouse pointer. Each Hide call = Show call.
184
185 static int Mouse_counter = 0;
186
187 void ddio_MouseShow() { Mouse_counter++; }
188
189 void ddio_MouseHide() { Mouse_counter--; }
190
191 void ddio_InternalMouseSuspend(void) {}
192
193 void ddio_InternalMouseResume(void) {}
194
195 void ddio_MouseMode(int mode) { Mouse_mode = mode; }
196
197 // virtual coordinate system for mouse (match to video resolution set for optimal mouse usage.
198 void ddio_MouseSetVCoords(int width, int height) { ddio_MouseSetLimits(0, 0, width, height); }
199
200 int sdlMouseButtonDownFilter(SDL_Event const *event) {
201 ASSERT(event->type == SDL_MOUSEBUTTONDOWN);
202
203 const SDL_MouseButtonEvent *ev = &event->button;
204 t_mse_event mevt;
205
206 if (ev->button == 1) {
207 DDIO_mouse_state.btn_mask |= MOUSE_LB;
208 DIM_buttons.down_count[0]++;
209 DIM_buttons.time_down[0] = timer_GetTime();
210 DIM_buttons.is_down[0] = true;
211 mevt.btn = 0;
212 mevt.state = true;
213 MB_queue.send(mevt);
214 // mprintf(0, "MOUSE Button 0: Down\n");
215 } else if (ev->button == 2) {
216 DDIO_mouse_state.btn_mask |= MOUSE_RB;
217 DIM_buttons.down_count[1]++;
218 DIM_buttons.time_down[1] = timer_GetTime();
219 DIM_buttons.is_down[1] = true;
220 mevt.btn = 1;
221 mevt.state = true;
222 MB_queue.send(mevt);
223 // mprintf(0, "MOUSE Button 1: Down\n");
224 }
225 else if (ev->button == 3) {
226 DDIO_mouse_state.btn_mask |= MOUSE_CB;
227 DIM_buttons.down_count[2]++;
228 DIM_buttons.time_down[2] = timer_GetTime();
229 DIM_buttons.is_down[2] = true;
230 mevt.btn = 2;
231 mevt.state = true;
232 MB_queue.send(mevt);
233 // mprintf(0, "MOUSE Button 2: Down\n");
234 }
235
236 // buttons 4 and 5 are reserved for the mouse wheel...that's how the engine works...adjust in here.
237
238 else if (ev->button == 4) {
239 DDIO_mouse_state.btn_mask |= MOUSE_B6;
240 DIM_buttons.down_count[5]++;
241 DIM_buttons.time_down[5] = timer_GetTime();
242 DIM_buttons.is_down[5] = true;
243 mevt.btn = 5;
244 mevt.state = true;
245 MB_queue.send(mevt);
246 // mprintf(0, "MOUSE Button 5: Down\n");
247 }
248 else if (ev->button == 5) {
249 DDIO_mouse_state.btn_mask |= MOUSE_B7;
250 DIM_buttons.down_count[6]++;
251 DIM_buttons.time_down[6] = timer_GetTime();
252 DIM_buttons.is_down[6] = true;
253 mevt.btn = 6;
254 mevt.state = true;
255 MB_queue.send(mevt);
256 // mprintf(0, "MOUSE Button 6: Down\n");
257 }
258 else if (ev->button == 6) {
259 DDIO_mouse_state.btn_mask |= MOUSE_B8;
260 DIM_buttons.down_count[7]++;
261 DIM_buttons.time_down[7] = timer_GetTime();
262 DIM_buttons.is_down[7] = true;
263 mevt.btn = 7;
264 mevt.state = true;
265 MB_queue.send(mevt);
266 // mprintf(0, "MOUSE Button 7: Down\n");
267 }
268
269 return (0);
270 }
271
272 int sdlMouseButtonUpFilter(SDL_Event const *event) {
273 ASSERT(event->type == SDL_MOUSEBUTTONUP);
274
275 const SDL_MouseButtonEvent *ev = &event->button;
276 t_mse_event mevt;
277
278 if (ev->button == 1) {
279 DDIO_mouse_state.btn_mask &= (~MOUSE_LB);
280 DIM_buttons.up_count[0]++;
281 DIM_buttons.is_down[0] = false;
282 DIM_buttons.time_up[0] = timer_GetTime();
283 mevt.btn = 0;
284 mevt.state = false;
285 MB_queue.send(mevt);
286 // mprintf(0, "MOUSE Button 0: Up\n");
287 }
288 else if (ev->button == 2) {
289 DDIO_mouse_state.btn_mask &= (~MOUSE_RB);
290 DIM_buttons.up_count[1]++;
291 DIM_buttons.is_down[1] = false;
292 DIM_buttons.time_up[1] = timer_GetTime();
293 mevt.btn = 1;
294 mevt.state = false;
295 MB_queue.send(mevt);
296 // mprintf(0, "MOUSE Button 1: Up\n");
297 }
298 else if (ev->button == 3) {
299 DDIO_mouse_state.btn_mask &= (~MOUSE_CB);
300 DIM_buttons.up_count[2]++;
301 DIM_buttons.is_down[2] = false;
302 DIM_buttons.time_up[2] = timer_GetTime();
303 mevt.btn = 2;
304 mevt.state = false;
305 MB_queue.send(mevt);
306 // mprintf(0, "MOUSE Button 2: Up\n");
307
308 }
309
310 // buttons 4 and 5 are reserved for the mouse wheel...that's how the engine works...adjust in here.
311
312 else if (ev->button == 4) {
313 DDIO_mouse_state.btn_mask &= (~MOUSE_B6);
314 DIM_buttons.up_count[5]++;
315 DIM_buttons.is_down[5] = false;
316 DIM_buttons.time_up[5] = timer_GetTime();
317 mevt.btn = 5;
318 mevt.state = false;
319 MB_queue.send(mevt);
320 // mprintf(0, "MOUSE Button 5: Up\n");
321 }
322 else if (ev->button == 5) {
323 DDIO_mouse_state.btn_mask &= (~MOUSE_B7);
324 DIM_buttons.up_count[6]++;
325 DIM_buttons.is_down[6] = false;
326 DIM_buttons.time_up[6] = timer_GetTime();
327 mevt.btn = 6;
328 mevt.state = false;
329 MB_queue.send(mevt);
330 // mprintf(0, "MOUSE Button 6: Up\n");
331 }
332 else if (ev->button == 6) {
333 DDIO_mouse_state.btn_mask &= (~MOUSE_B8);
334 DIM_buttons.up_count[7]++;
335 DIM_buttons.is_down[7] = false;
336 DIM_buttons.time_up[7] = timer_GetTime();
337 mevt.btn = 7;
338 mevt.state = false;
339 MB_queue.send(mevt);
340 // mprintf(0, "MOUSE Button 7: Up\n");
341 }
342
343 return (0);
344 }
345
346 int sdlMouseWheelFilter(SDL_Event const *event) {
347 ASSERT(event->type == SDL_MOUSEWHEEL);
348
349 const SDL_MouseWheelEvent *ev = &event->wheel;
350 t_mse_event mevt;
351
352 // !!! FIXME: this ignores horizontal wheels for now, since Descent3 doesn't currently have a concept of them
353 // !!! FIXME: (vertical mouse wheels are represented as mouse buttons 4 and 5, incorrectly, on all platforms).
354 // !!! FIXME: this will require improvements to the engine before this changes here, though.
355
356 if (ev->y > 0) { /* Mouse scroll up */
357 DDIO_mouse_state.btn_mask |= MOUSE_B5;
358 DIM_buttons.down_count[4]++;
359 DIM_buttons.time_down[4] = timer_GetTime();
360 DIM_buttons.is_down[4] = true;
361 mevt.btn = 4;
362 mevt.state = true;
363 MB_queue.send(mevt);
364
365 // send an immediate release event, as if the "button" was clicked. !!! FIXME: this also needs improvements in the engine.
366 // don't remove from btn_mask
367 DIM_buttons.up_count[4]++;
368 DIM_buttons.is_down[4] = false;
369 DIM_buttons.time_up[4] = timer_GetTime();
370 mevt.btn = 4;
371 mevt.state = false;
372 MB_queue.send(mevt);
373 // mprintf(0, "MOUSE Scrollwheel: Rolled Up\n");
374 } else if (ev->y < 0) { /* Mouse scroll down */
375 DDIO_mouse_state.btn_mask |= MOUSE_B6;
376 DIM_buttons.down_count[5]++;
377 DIM_buttons.time_down[5] = timer_GetTime();
378 DIM_buttons.is_down[5] = true;
379 mevt.btn = 5;
380 mevt.state = true;
381 MB_queue.send(mevt);
382
383 // send an immediate release event, as if the "button" was clicked. !!! FIXME: this also needs improvements in the engine.
384 // don't remove from btn_mask
385 DIM_buttons.up_count[5]++;
386 DIM_buttons.is_down[5] = false;
387 DIM_buttons.time_up[5] = timer_GetTime();
388 mevt.btn = 5;
389 mevt.state = false;
390 MB_queue.send(mevt);
391 // mprintf(0, "MOUSE Scrollwheel: Rolled Down\n");
392 }
393
394 return 0;
395 }
396
397 int sdlMouseMotionFilter(SDL_Event const *event) {
398 /*
399 int oldmx, oldmy;
400 int deltamx,deltamy;
401 oldmx = DDIO_mouse_state.cx;
402 oldmy = DDIO_mouse_state.cy;
403 deltamx = deltamy = 0;
404
405 bool use_dga = false;
406
407 if(Lnx_app_obj->m_Flags&APPFLAG_DGAMOUSE)
408 use_dga = true;
409
410 if(Mouse_mode==MOUSE_STANDARD_MODE)
411 return;
412
413 while (XCheckMaskEvent(Lnx_app_obj->m_Display, PointerMotionMask | ButtonPressMask | ButtonReleaseMask, &evt))
414 {
415 switch (evt.type)
416 {
417 case MotionNotify:
418 if(use_dga)
419 {
420
421 deltamx += event->motion.x; //evt.xmotion.x_root;
422 deltamy += event->motion.y; //evt.xmotion.y_root;
423
424 }else
425 {
426 if ((evt.xmotion.x != DDIO_mouse_state.cx) && (evt.xmotion.y != DDIO_mouse_state.cy)) {
427 deltamx += (evt.xmotion.x - oldmx);
428 deltamy += (evt.xmotion.y - oldmy);
429 oldmx = evt.xmotion.x;
430 oldmy = evt.xmotion.y;
431 }
432 }
433 break;
434 case ButtonPress:
435 case ButtonRelease:
436 break;
437 }
438 }
439
440 if( (deltamx) || (deltamy) )
441 {
442 DDIO_mouse_state.dx = deltamx;
443 DDIO_mouse_state.dy = deltamy;
444 DDIO_mouse_state.x += deltamx;
445 DDIO_mouse_state.y += deltamy;
446 if (DDIO_mouse_state.x < DDIO_mouse_state.l) DDIO_mouse_state.x = DDIO_mouse_state.l;
447 if (DDIO_mouse_state.x >= DDIO_mouse_state.r) DDIO_mouse_state.x = DDIO_mouse_state.r-1;
448 if (DDIO_mouse_state.y < DDIO_mouse_state.t) DDIO_mouse_state.y = DDIO_mouse_state.t;
449 if (DDIO_mouse_state.y >= DDIO_mouse_state.b) DDIO_mouse_state.y = DDIO_mouse_state.b-1;
450
451 // warp the mouse.
452 if(!use_dga)
453 {
454 XWarpPointer(Lnx_app_obj->m_Display, None, Lnx_app_obj->m_Window,
455 0,0,0,0, Lnx_app_obj->m_W/2, Lnx_app_obj->m_H/2);
456 }
457 }
458 */
459
460 if (event->type == SDL_JOYBALLMOTION) {
461 DDIO_mouse_state.dx = event->jball.xrel / 100;
462 DDIO_mouse_state.dy = event->jball.yrel / 100;
463 DDIO_mouse_state.x += DDIO_mouse_state.dx;
464 DDIO_mouse_state.y += DDIO_mouse_state.dy;
465 } // if
466 else {
467 if (ddio_mouseGrabbed) {
468 DDIO_mouse_state.dx = event->motion.xrel;
469 DDIO_mouse_state.dy = event->motion.yrel;
470 DDIO_mouse_state.x += DDIO_mouse_state.dx;
471 DDIO_mouse_state.y += DDIO_mouse_state.dy;
472 } // if
473 else {
474 DDIO_mouse_state.dx = event->motion.x - DDIO_mouse_state.x;
475 DDIO_mouse_state.dy = event->motion.y - DDIO_mouse_state.y;
476 DDIO_mouse_state.x = event->motion.x;
477 DDIO_mouse_state.y = event->motion.y;
478 } // else
479 } // else
480
481 if (DDIO_mouse_state.x < DDIO_mouse_state.l)
482 DDIO_mouse_state.x = DDIO_mouse_state.l;
483 if (DDIO_mouse_state.x >= DDIO_mouse_state.r)
484 DDIO_mouse_state.x = DDIO_mouse_state.r - 1;
485 if (DDIO_mouse_state.y < DDIO_mouse_state.t)
486 DDIO_mouse_state.y = DDIO_mouse_state.t;
487 if (DDIO_mouse_state.y >= DDIO_mouse_state.b)
488 DDIO_mouse_state.y = DDIO_mouse_state.b - 1;
489
490 return (0);
491 }
492
493 // This function will handle all mouse events.
494 void ddio_InternalMouseFrame(void) {
495 static unsigned frame_count = 0;
496 SDL_PumpEvents();
497 frame_count++;
498 }
499
500 /* x, y = absolute mouse position
501 dx, dy = mouse deltas since last call
502 return value is mouse button mask
503 */
504 int ddio_MouseGetState(int *x, int *y, int *dx, int *dy, int *z, int *dz) {
505 // update mouse timer.
506 int btn_mask = DDIO_mouse_state.btn_mask;
507
508 // get return values.
509 if (x)
510 *x = DDIO_mouse_state.x;
511 if (y)
512 *y = DDIO_mouse_state.y;
513 if (z)
514 *z = 0;
515 if (dx)
516 *dx = DDIO_mouse_state.dx;
517 if (dy)
518 *dy = DDIO_mouse_state.dy;
519 if (dz)
520 *dz = 0;
521
522 DDIO_mouse_state.dx = 0;
523 DDIO_mouse_state.dy = 0;
524
525 // unset the mouse wheel "button" once it's been retrieved.
526 DDIO_mouse_state.btn_mask &= ~(MOUSE_B5|MOUSE_B6);
527
528 return btn_mask;
529 }
530
531 // resets mouse queue and button info only.
532 void ddio_MouseQueueFlush() {
533 memset(&DIM_buttons, 0, sizeof(DIM_buttons));
534 MB_queue.flush();
535 }
536
537 // gets a mouse button event, returns false if none.
538 bool ddio_MouseGetEvent(int *btn, bool *state) {
539 t_mse_event evt;
540
541 if (MB_queue.recv(&evt)) {
542 *btn = (int)evt.btn;
543 *state = evt.state ? true : false;
544 return true;
545 }
546
547 return false;
548 }
549
550 // return mouse button down time.
551 float ddio_MouseBtnDownTime(int btn) {
552 float dtime, curtime = timer_GetTime();
553
554 ASSERT(btn >= 0 && btn < N_MSEBTNS);
555
556 if (DIM_buttons.is_down[btn]) {
557 dtime = curtime - DIM_buttons.time_down[btn];
558 DIM_buttons.time_down[btn] = curtime;
559 } else {
560 dtime = DIM_buttons.time_up[btn] - DIM_buttons.time_down[btn];
561 DIM_buttons.time_down[btn] = 0;
562 DIM_buttons.time_up[btn] = 0;
563 }
564
565 return dtime;
566 }
567
568 // return mouse button down time
569 int ddio_MouseBtnDownCount(int btn) {
570 if (btn < 0 || btn >= N_MSEBTNS)
571 return 0;
572 int n_downs = DIM_buttons.down_count[btn];
573
574 if (n_downs) {
575 DIM_buttons.down_count[btn] = 0;
576 }
577
578 return n_downs;
579 }
580
581 // return mouse button up count
582 int ddio_MouseBtnUpCount(int btn) {
583 if (btn < 0 || btn >= N_MSEBTNS)
584 return 0;
585 int n_ups = DIM_buttons.up_count[btn];
586 DIM_buttons.up_count[btn] = 0;
587 return n_ups;
588 }
589
590 char Ctltext_MseBtnBindings[N_MSEBTNS][32] = {"mse-1\0\0\0\0\0\0\0\0\0\0\0\0",
591 "mse-2\0\0\0\0\0\0\0\0\0\0\0\0",
592 "mse-3\0\0\0\0\0\0\0\0\0\0\0\0",
593 "mse-4\0\0\0\0\0\0\0\0\0\0\0",
594 "msew-u\0\0\0\0\0\0\0\0\0\0\0",
595 "msew-d\0\0\0\0\0\0\0\0\0\0\0",
596 "",
597 ""};
598
599 char Ctltext_MseAxisBindings[][32] = {"mse-X\0\0\0\0\0\0\0\0\0\0\0\0", "mse-Y\0\0\0\0\0\0\0\0\0\0\0\0",
600 "msewheel\0\0\0\0\0\0\0\0\0\0"};
601
602 // returns string to binding.
603 const char *ddio_MouseGetBtnText(int btn) {
604 if (btn >= N_MSEBTNS || btn < 0)
605 return ("");
606 return Ctltext_MseBtnBindings[btn];
607 }
608
609 const char *ddio_MouseGetAxisText(int axis) {
610 if (axis >= (sizeof(Ctltext_MseAxisBindings) / sizeof(char *)) || axis < 0)
611 return ("");
612 return Ctltext_MseAxisBindings[axis];
613 }