]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/mouse.cpp
start of SDL2 port
[taylor/freespace2.git] / src / io / mouse.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Io/Mouse.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Routines to read the mouse.
16  *
17  * $Log$
18  * Revision 1.7  2003/05/25 02:30:42  taylor
19  * Freespace 1 support
20  *
21  * Revision 1.6  2002/07/13 06:46:48  theoddone33
22  * Warning cleanups
23  *
24  * Revision 1.5  2002/06/09 04:41:22  relnev
25  * added copyright header
26  *
27  * Revision 1.4  2002/06/02 04:26:34  relnev
28  * warning cleanup
29  *
30  * Revision 1.3  2002/05/29 06:25:13  theoddone33
31  * Keyboard input, mouse tracking now work
32  *
33  * Revision 1.2  2002/05/07 03:16:46  theoddone33
34  * The Great Newline Fix
35  *
36  * Revision 1.1.1.1  2002/05/03 03:28:09  root
37  * Initial import.
38  *
39  * 
40  * 4     7/15/99 9:20a Andsager
41  * FS2_DEMO initial checkin
42  * 
43  * 3     6/02/99 6:18p Dave
44  * Fixed TNT lockup problems! Wheeeee!
45  * 
46  * 2     10/07/98 10:53a Dave
47  * Initial checkin.
48  * 
49  * 1     10/07/98 10:49a Dave
50  * 
51  * 29    6/10/98 2:52p Hoffoss
52  * Made mouse code use DI by default, but fall back on normal code if that
53  * fails.
54  * 
55  * 28    5/24/98 1:35p Hoffoss
56  * Fixed bug where  mouse cursor is always recentering with a
57  * mouse_flush() call in debug version.
58  * 
59  * 27    5/22/98 4:50p Hoffoss
60  * Trying to fix mouse acceleration problem..
61  * 
62  * 26    5/21/98 12:26p Lawrance
63  * Fixed mouse jerk at mission start while in debug build only.
64  * 
65  * 25    5/15/98 2:41p Hoffoss
66  * Made mouse default to off (for flying ship) and fixed some new pilot
67  * init bugs.
68  * 
69  * 24    5/08/98 4:13p Hoffoss
70  * Fixed problem with mouse pointer centering causing lost keypresses.
71  * 
72  * 23    5/07/98 6:58p Hoffoss
73  * Made changes to mouse code to fix a number of problems.
74  * 
75  * 22    5/05/98 8:38p Hoffoss
76  * Added sensitivity adjustment to options menu and made it save to pilot
77  * file.
78  * 
79  * 21    5/05/98 1:03p Hoffoss
80  * Fixed initialization bug.
81  * 
82  * 20    5/01/98 5:45p Hoffoss
83  * Made further improvements to the mouse code.
84  * 
85  * 19    5/01/98 3:35p Hoffoss
86  * Made changes to release version of mouse code.
87  * 
88  * 18    5/01/98 1:14p Hoffoss
89  * Changed mouse usage so directInput is only used for release version.
90  * 
91  * 17    4/30/98 5:40p Hoffoss
92  * Added mouse as a supported control to fly the ship.
93  * 
94  * 16    4/29/98 12:13a Lawrance
95  * Add function to check down count of mouse button without reseting the
96  * internal count.  Added hook to reset demo trailer timer when a button
97  * is pressed.
98  * 
99  * 15    4/02/98 5:26p John
100  * 
101  * 14    1/19/98 6:15p John
102  * Fixed all my Optimized Build compiler warnings
103  * 
104  * 13    12/04/97 3:47p John
105  * Made joystick move mouse cursor
106  * 
107  * 12    11/20/97 5:36p Dave
108  * Hooked in a bunch of main hall changes (including sound). Made it
109  * possible to reposition (rewind/ffwd) 
110  * sound buffer pointers. Fixed animation direction change framerate
111  * problem.
112  * 
113  * 11    5/12/97 11:41a John
114  * Added range checking to mouse position
115  * 
116  * 10    4/22/97 5:55p Lawrance
117  * let mouse.cpp decide if mouse has moved
118  * 
119  * 9     4/22/97 12:29p John
120  * Changed mouse code so that you have to call mouse_init for the mouse
121  * stuff to work.
122  * 
123  * 8     4/22/97 10:56a John
124  * fixed some resource leaks.
125  * 
126  * 7     3/26/97 10:52a Lawrance
127  * mouse always on in menus, disappears in gameplay after 1 second
128  * 
129  * 6     3/11/97 1:37p Lawrance
130  * added mouse_up_count(), changed mouse_mark() to mouse_mark_button() &
131  * mouse_mark_move()
132  * 
133  * 5     12/09/96 1:29p Lawrance
134  * adding 3 button support
135  * 
136  * 4     12/03/96 4:19p John
137  * Added some code so that holding down the mouse buttons between menus
138  * doesn't select the next menu.
139  *
140  * $NoKeywords: $
141  */
142
143
144 #include "mouse.h"
145 #include "2d.h"
146 #include "osapi.h"
147
148
149 static int mouse_inited = 0;
150
151 static int Mouse_x;
152 static int Mouse_y;
153
154 int mouse_flags;
155 int mouse_left_pressed = 0;
156 int mouse_right_pressed = 0;
157 int mouse_middle_pressed = 0;
158 int mouse_left_up = 0;
159 int mouse_right_up = 0;
160 int mouse_middle_up = 0;
161 int Mouse_dx = 0;
162 int Mouse_dy = 0;
163 int Mouse_dz = 0;
164
165 int Mouse_sensitivity = 4;
166 int Use_mouse_to_fly = 0;
167 int Mouse_hidden = 0;
168 int Keep_mouse_centered = 0;;
169
170 void mouse_force_pos(int x, int y);
171
172
173 int mouse_is_visible()
174 {
175         return !Mouse_hidden;
176 }
177
178 void mouse_close()
179 {
180         if (!mouse_inited)
181                 return;
182
183         mouse_inited = 0;
184 }
185
186 void mouse_init()
187 {
188         // Initialize queue
189         if (mouse_inited)
190                 return;
191
192         mouse_inited = 1;
193
194         mouse_flags = 0;
195         Mouse_x = gr_screen.max_w / 2;
196         Mouse_y = gr_screen.max_h / 2;
197
198         atexit( mouse_close );
199 }
200
201
202 // ----------------------------------------------------------------------------
203 // mouse_mark_button() is called asynchronously by the OS when a mouse button
204 // goes up or down.  The mouse button that is affected is passed via the 
205 // flags parameter.  
206 //
207 // parameters:   flags ==> mouse button pressed/released
208 //               set   ==> 1 - button is pressed
209 //                         0 - button is released
210
211 void mouse_mark_button( uint flags, int set)
212 {
213         if ( !mouse_inited )
214                 return;
215
216         if ( !(mouse_flags & MOUSE_LEFT_BUTTON) )       {
217
218                 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
219                         mouse_left_pressed++;
220                 }
221         }
222         else {
223                 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
224                         mouse_left_up++;
225                 }
226         }
227
228         if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) )      {
229
230                 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
231                         mouse_right_pressed++;
232                 }
233         }
234         else {
235                 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
236                         mouse_right_up++;
237                 }
238         }
239
240         if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) )     {
241
242                 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
243                         mouse_middle_pressed++;
244                 }
245         }
246         else {
247                 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
248                         mouse_middle_up++;
249                 }
250         }
251
252         if ( set ){
253                 mouse_flags |= flags;
254         } else {
255                 mouse_flags &= ~flags;
256         }
257 }
258
259 void mouse_flush()
260 {
261         if (!mouse_inited)
262                 return;
263
264         mouse_eval_deltas();
265         Mouse_dx = Mouse_dy = Mouse_dz = 0;
266         mouse_left_pressed = 0;
267         mouse_right_pressed = 0;
268         mouse_middle_pressed = 0;
269         mouse_flags = 0;
270 }
271
272 int mouse_down_count(int n, int reset_count)
273 {
274         int tmp = 0;
275
276         if ( !mouse_inited )
277                 return 0;
278
279         if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON) )
280                 return 0;
281
282         switch (n) {
283                 case MOUSE_LEFT_BUTTON:
284                         tmp = mouse_left_pressed;
285                         if ( reset_count ) {
286                                 mouse_left_pressed = 0;
287                         }
288                         break;
289
290                 case MOUSE_RIGHT_BUTTON:
291                         tmp = mouse_right_pressed;
292                         if ( reset_count ) {
293                                 mouse_right_pressed = 0;
294                         }
295                         break;
296
297                 case MOUSE_MIDDLE_BUTTON:
298                         tmp = mouse_middle_pressed;
299                         if ( reset_count ) {
300                                 mouse_middle_pressed = 0;
301                         }
302                         break;
303         } // end switch
304
305         return tmp;
306 }
307
308 // mouse_up_count() returns the number of times button n has gone from down to up
309 // since the last call
310 //
311 // parameters:  n - button of mouse (see #define's in mouse.h)
312 //
313 int mouse_up_count(int n)
314 {
315         int tmp = 0;
316
317         if ( !mouse_inited )
318                 return 0;
319
320         if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON) )
321                 return 0;
322
323         switch (n) {
324                 case MOUSE_LEFT_BUTTON:
325                         tmp = mouse_left_up;
326                         mouse_left_up = 0;
327                         break;
328
329                 case MOUSE_RIGHT_BUTTON:
330                         tmp = mouse_right_up;
331                         mouse_right_up = 0;
332                         break;
333
334                 case MOUSE_MIDDLE_BUTTON:
335                         tmp = mouse_middle_up;
336                         mouse_middle_up = 0;
337                         break;
338
339                 default:
340                         Assert(0);      // can't happen
341                         break;
342         } // end switch
343
344         return tmp;
345 }
346
347 // returns 1 if mouse button btn is down, 0 otherwise
348
349 int mouse_down(int btn)
350 {
351         int tmp;
352
353         if ( !mouse_inited )
354                 return 0;
355
356         if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON) )
357                 return 0;
358
359         if ( mouse_flags & btn )
360                 tmp = 1;
361         else
362                 tmp = 0;
363
364         return tmp;
365 }
366
367 // returns the fraction of time btn has been down since last call 
368 // (currently returns 1 if buttons is down, 0 otherwise)
369 //
370 float mouse_down_time(int btn)
371 {
372         float tmp;
373
374         if ( !mouse_inited )
375                 return 0.0f;
376
377         if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON) )
378                 return 0.0f;
379
380         if ( mouse_flags & btn )
381                 tmp = 1.0f;
382         else
383                 tmp = 0.0f;
384
385         return tmp;
386 }
387
388 void mouse_get_delta(int *dx, int *dy, int *dz)
389 {
390         if (dx)
391                 *dx = Mouse_dx;
392         if (dy)
393                 *dy = Mouse_dy;
394         if (dz)
395                 *dz = Mouse_dz;
396 }
397
398 // Forces the actual windows cursor to be at (x,y).  This may be independent of our tracked (x,y) mouse pos.
399 void mouse_force_pos(int x, int y)
400 {
401         if (os_foreground()) {  // only mess with windows's mouse if we are in control of it
402                 extern SDL_Window *GL_window;
403                 SDL_WarpMouseInWindow(GL_window, x, y);
404         }
405 }
406
407 #include "gamesequence.h"
408
409 // change in mouse position since last call
410 void mouse_eval_deltas()
411 {
412         static int old_x = 0;
413         static int old_y = 0;
414         int tmp_x, tmp_y, cx, cy;
415
416         Mouse_dx = Mouse_dy = Mouse_dz = 0;
417
418         if (!mouse_inited)
419                 return;
420
421         cx = gr_screen.max_w / 2;
422         cy = gr_screen.max_h / 2;
423
424         SDL_GetMouseState(&tmp_x, &tmp_y);
425
426         Mouse_dx = tmp_x - old_x;
427         Mouse_dy = tmp_y - old_y;
428         Mouse_dz = 0;
429
430         if (Keep_mouse_centered && Mouse_hidden) {
431                 if (Mouse_dx || Mouse_dy)
432                         mouse_force_pos(cx, cy);
433
434                 old_x = cx;
435                 old_y = cy;
436
437         } else {
438                 old_x = tmp_x;
439                 old_y = tmp_y;
440         }
441 }
442
443 int mouse_get_pos(int *xpos, int *ypos)
444 {
445         int flags;
446
447         if (!mouse_inited) {
448                 *xpos = *ypos = 0;
449                 return 0;
450         }
451
452         flags = SDL_GetMouseState(&Mouse_x, &Mouse_y);
453
454         if (Mouse_x < 0){
455                 Mouse_x = 0;
456         }
457
458         if (Mouse_y < 0){
459                 Mouse_y = 0;
460         }
461
462         if (Mouse_x >= gr_screen.max_w){
463                 Mouse_x = gr_screen.max_w - 1;
464         }
465
466         if (Mouse_y >= gr_screen.max_h){
467                 Mouse_y = gr_screen.max_h - 1;
468         }
469         
470         if (xpos){
471                 *xpos = Mouse_x;
472         }
473
474         if (ypos){
475                 *ypos = Mouse_y;
476         }
477
478         return flags;
479 }
480
481 void mouse_get_real_pos(int *mx, int *my)
482 {
483         SDL_GetMouseState (mx, my);
484 }
485
486 void mouse_set_pos(int xpos, int ypos)
487 {
488         if ((xpos != Mouse_x) || (ypos != Mouse_y)){
489                 mouse_force_pos(xpos, ypos);
490         }
491 }