0033c137eb6b415ec45b2e1ba8fa4d7e2c0b5902
[blender.git] / source / gameengine / GameLogic / Joystick / SCA_JoystickEvents.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (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 Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): snailrose.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
29  *  \ingroup gamelogic
30  */
31
32 #ifdef WITH_SDL
33 #  include <SDL.h>
34 #endif
35
36 #include "SCA_Joystick.h"
37 #include "SCA_JoystickPrivate.h"
38
39 #ifdef _MSC_VER
40 #  include <cstdio> /* printf */
41 #endif
42
43 #ifdef WITH_SDL
44 void SCA_Joystick::OnAxisMotion(SDL_Event* sdl_event)
45 {
46         if (sdl_event->jaxis.axis >= JOYAXIS_MAX)
47                 return;
48         
49         m_axis_array[sdl_event->jaxis.axis] = sdl_event->jaxis.value;
50         m_istrig_axis = 1;
51 }
52
53 /* See notes below in the event loop */
54 void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
55 {
56         if (sdl_event->jhat.hat >= JOYHAT_MAX)
57                 return;
58
59         m_hat_array[sdl_event->jhat.hat] = sdl_event->jhat.value;
60         m_istrig_hat = 1;
61 }
62
63 /* See notes below in the event loop */
64 void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event)
65 {
66         m_istrig_button = 1;
67 }
68
69
70 void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event)
71 {
72         //if (sdl_event->jbutton.button > m_buttonmax) /* unsigned int so always above 0 */
73         //      return;
74         // sdl_event->jbutton.button;
75         
76         m_istrig_button = 1;
77 }
78
79
80 void SCA_Joystick::OnNothing(SDL_Event* sdl_event)
81 {
82         m_istrig_axis = m_istrig_button = m_istrig_hat = 0;
83 }
84
85 void SCA_Joystick::HandleEvents(void)
86 {
87         SDL_Event               sdl_event;
88
89         if (SDL_PollEvent == (void*)0) {
90                 return;
91         }
92
93         int i;
94         for (i=0; i<m_joynum; i++) { /* could use JOYINDEX_MAX but no reason to */
95                 if (SCA_Joystick::m_instance[i])
96                         SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
97         }
98         
99         while (SDL_PollEvent(&sdl_event)) {
100                 /* Note! m_instance[sdl_event.jaxis.which]
101                  * will segfault if over JOYINDEX_MAX, not too nice but what are the chances? */
102                 
103                 /* Note!, with buttons, this wont care which button is pressed,
104                  * only to set 'm_istrig_button', actual pressed buttons are detected by SDL_JoystickGetButton */
105                 
106                 /* Note!, if you manage to press and release a button within 1 logic tick
107                  * it wont work as it should */
108                 
109                 switch (sdl_event.type) {
110                         case SDL_JOYAXISMOTION:
111                                 SCA_Joystick::m_instance[sdl_event.jaxis.which]->OnAxisMotion(&sdl_event);
112                                 break;
113                         case SDL_JOYHATMOTION:
114                                 SCA_Joystick::m_instance[sdl_event.jhat.which]->OnHatMotion(&sdl_event);
115                                 break;
116                         case SDL_JOYBUTTONUP:
117                                 SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonUp(&sdl_event);
118                                 break;
119                         case SDL_JOYBUTTONDOWN:
120                                 SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonDown(&sdl_event);
121                                 break;
122 #if 0   /* Not used yet */
123                         case SDL_JOYBALLMOTION:
124                                 SCA_Joystick::m_instance[sdl_event.jball.which]->OnBallMotion(&sdl_event);
125                                 break;
126 #endif
127 #if SDL_VERSION_ATLEAST(2, 0, 0)
128                         case SDL_JOYDEVICEADDED:
129                         case SDL_JOYDEVICEREMOVED:
130                                 /* pass */
131                                 break;
132 #endif
133                         default:
134                                 printf("SCA_Joystick::HandleEvents, Unknown SDL event (%d), this should not happen\n", sdl_event.type);
135                                 break;
136                 }
137         }
138 }
139 #endif /* WITH_SDL */