]> icculus.org git repositories - taylor/freespace2.git/blob - src/io/mouse.cpp
remove broken DCF() functions
[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 btn, int set)
212 {
213         uint flags = 0;
214
215         if ( !mouse_inited )
216                 return;
217
218         switch (btn) {
219                 case SDL_BUTTON_LEFT:
220                         flags |= MOUSE_LEFT_BUTTON;
221                         break;
222
223                 case SDL_BUTTON_RIGHT:
224                         flags |= MOUSE_RIGHT_BUTTON;
225                         break;
226
227                 case SDL_BUTTON_MIDDLE:
228                         flags |= MOUSE_MIDDLE_BUTTON;
229                         break;
230
231                 default:
232                         return;
233         }
234
235         if ( !(mouse_flags & MOUSE_LEFT_BUTTON) )       {
236
237                 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 1) ) {
238                         mouse_left_pressed++;
239                 }
240         }
241         else {
242                 if ( (flags & MOUSE_LEFT_BUTTON) && (set == 0) ){
243                         mouse_left_up++;
244                 }
245         }
246
247         if ( !(mouse_flags & MOUSE_RIGHT_BUTTON) )      {
248
249                 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 1) ){
250                         mouse_right_pressed++;
251                 }
252         }
253         else {
254                 if ( (flags & MOUSE_RIGHT_BUTTON) && (set == 0) ){
255                         mouse_right_up++;
256                 }
257         }
258
259         if ( !(mouse_flags & MOUSE_MIDDLE_BUTTON) )     {
260
261                 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 1) ){
262                         mouse_middle_pressed++;
263                 }
264         }
265         else {
266                 if ( (flags & MOUSE_MIDDLE_BUTTON) && (set == 0) ){
267                         mouse_middle_up++;
268                 }
269         }
270
271         if ( set ){
272                 mouse_flags |= flags;
273         } else {
274                 mouse_flags &= ~flags;
275         }
276 }
277
278 void mouse_flush()
279 {
280         if (!mouse_inited)
281                 return;
282
283         mouse_eval_deltas();
284         Mouse_dx = Mouse_dy = Mouse_dz = 0;
285         mouse_left_pressed = 0;
286         mouse_right_pressed = 0;
287         mouse_middle_pressed = 0;
288         mouse_flags = 0;
289 }
290
291 int mouse_down_count(int n, int reset_count)
292 {
293         int tmp = 0;
294
295         if ( !mouse_inited )
296                 return 0;
297
298         if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON) )
299                 return 0;
300
301         switch (n) {
302                 case MOUSE_LEFT_BUTTON:
303                         tmp = mouse_left_pressed;
304                         if ( reset_count ) {
305                                 mouse_left_pressed = 0;
306                         }
307                         break;
308
309                 case MOUSE_RIGHT_BUTTON:
310                         tmp = mouse_right_pressed;
311                         if ( reset_count ) {
312                                 mouse_right_pressed = 0;
313                         }
314                         break;
315
316                 case MOUSE_MIDDLE_BUTTON:
317                         tmp = mouse_middle_pressed;
318                         if ( reset_count ) {
319                                 mouse_middle_pressed = 0;
320                         }
321                         break;
322         } // end switch
323
324         return tmp;
325 }
326
327 // mouse_up_count() returns the number of times button n has gone from down to up
328 // since the last call
329 //
330 // parameters:  n - button of mouse (see #define's in mouse.h)
331 //
332 int mouse_up_count(int n)
333 {
334         int tmp = 0;
335
336         if ( !mouse_inited )
337                 return 0;
338
339         if ( (n < LOWEST_MOUSE_BUTTON) || (n > HIGHEST_MOUSE_BUTTON) )
340                 return 0;
341
342         switch (n) {
343                 case MOUSE_LEFT_BUTTON:
344                         tmp = mouse_left_up;
345                         mouse_left_up = 0;
346                         break;
347
348                 case MOUSE_RIGHT_BUTTON:
349                         tmp = mouse_right_up;
350                         mouse_right_up = 0;
351                         break;
352
353                 case MOUSE_MIDDLE_BUTTON:
354                         tmp = mouse_middle_up;
355                         mouse_middle_up = 0;
356                         break;
357
358                 default:
359                         Assert(0);      // can't happen
360                         break;
361         } // end switch
362
363         return tmp;
364 }
365
366 // returns 1 if mouse button btn is down, 0 otherwise
367
368 int mouse_down(int btn)
369 {
370         int tmp;
371
372         if ( !mouse_inited )
373                 return 0;
374
375         if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON) )
376                 return 0;
377
378         if ( mouse_flags & btn )
379                 tmp = 1;
380         else
381                 tmp = 0;
382
383         return tmp;
384 }
385
386 // returns the fraction of time btn has been down since last call 
387 // (currently returns 1 if buttons is down, 0 otherwise)
388 //
389 float mouse_down_time(int btn)
390 {
391         float tmp;
392
393         if ( !mouse_inited )
394                 return 0.0f;
395
396         if ( (btn < LOWEST_MOUSE_BUTTON) || (btn > HIGHEST_MOUSE_BUTTON) )
397                 return 0.0f;
398
399         if ( mouse_flags & btn )
400                 tmp = 1.0f;
401         else
402                 tmp = 0.0f;
403
404         return tmp;
405 }
406
407 void mouse_get_delta(int *dx, int *dy, int *dz)
408 {
409         if (dx)
410                 *dx = Mouse_dx;
411         if (dy)
412                 *dy = Mouse_dy;
413         if (dz)
414                 *dz = Mouse_dz;
415 }
416
417 extern SDL_Window *GL_window;
418
419 // Forces the actual windows cursor to be at (x,y).  This may be independent of our tracked (x,y) mouse pos.
420 void mouse_force_pos(int x, int y)
421 {
422         if (os_foreground()) {  // only mess with windows's mouse if we are in control of it
423                 SDL_WarpMouseInWindow(GL_window, x, y);
424         }
425 }
426
427 static bool Mouse_grabbed = false;
428 // reusing obsolete function for special position handling
429 void mouse_eval_deltas()
430 {
431         // make sure mouse is bound to window if we're flying with it
432         if (Keep_mouse_centered && Mouse_hidden) {
433                 if ( !Mouse_grabbed ) {
434                         SDL_SetRelativeMouseMode(SDL_TRUE);
435                         SDL_SetWindowGrab(GL_window, SDL_TRUE);
436                         Mouse_grabbed = true;
437                 }
438         } else {
439                 if (Mouse_grabbed) {
440                         SDL_SetRelativeMouseMode(SDL_FALSE);
441                         SDL_SetWindowGrab(GL_window, SDL_FALSE);
442                         Mouse_grabbed = false;
443                 }
444         }
445 }
446
447 int mouse_get_pos(int *xpos, int *ypos)
448 {
449         if ( !mouse_inited ) {
450                 if (xpos) {
451                         *xpos = 0;
452                 }
453
454                 if (ypos) {
455                         *ypos = 0;
456                 }
457
458                 return 0;
459         }
460
461         if (xpos){
462                 *xpos = Mouse_x;
463         }
464
465         if (ypos){
466                 *ypos = Mouse_y;
467         }
468
469         return mouse_flags;
470 }
471
472 void mouse_get_real_pos(int *mx, int *my)
473 {
474         if (mx) {
475                 *mx = Mouse_x;
476         }
477
478         if (my) {
479                 *my = Mouse_y;
480         }
481 }
482
483 void mouse_set_pos(int xpos, int ypos)
484 {
485         if ((xpos != Mouse_x) || (ypos != Mouse_y)){
486                 mouse_force_pos(xpos, ypos);
487         }
488 }
489
490 void mouse_update_pos(int x, int y, int dx, int dy)
491 {
492         Mouse_x = x;
493         Mouse_y = y;
494         Mouse_dx = dx;
495         Mouse_dy = dy;
496 }