2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #ifndef DISABLE_SDL
28 #include <SDL.h>
29 #endif
30
31 #include "SCA_Joystick.h"
32 #include "SCA_JoystickPrivate.h"
33
34
35 #ifndef DISABLE_SDL
36 void SCA_Joystick::OnAxisMotion(SDL_Event* sdl_event)
37 {
38         pFillAxes();
39         m_axisnum       = sdl_event->jaxis.axis;
40         m_axisvalue = sdl_event->jaxis.value;
41         m_istrig_axis = 1;
42 }
43
44
45 void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
46 {
47         m_hatdir = sdl_event->jhat.value;
48         m_hatnum = sdl_event->jhat.hat;
49         m_istrig_hat = 1;
50 }
51
52 void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event)
53 {
54         m_istrig_button = 1;
55         
56         /* this is needed for the "all events" option
57          * so we know if there are no buttons pressed */
58         int i;
59         for (i=0; i<m_buttonmax; i++) {
60                 if (SDL_JoystickGetButton(m_private->m_joystick, i)) {
61                         m_buttonnum = i;
62                         return;
63                 }
64         }
65         m_buttonnum = -2;
66 }
67
68
69 void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event)
70 {
71         if(sdl_event->jbutton.button <= m_buttonmax) /* unsigned int so always above 0 */
72         {
73                 m_istrig_button = 1;
74                 m_buttonnum = sdl_event->jbutton.button;
75         }
76 }
77
78
79 void SCA_Joystick::OnNothing(SDL_Event* sdl_event)
80 {
81         m_istrig_axis = m_istrig_button = m_istrig_hat = 0;
82 }
83
84 /* only handle events for 1 joystick */
85
86 void SCA_Joystick::HandleEvents(void)
87 {
88         SDL_Event               sdl_event;
89         
90         int i;
91         for (i=0; i<JOYINDEX_MAX; i++) {
92                 if(SCA_Joystick::m_instance[i])
93                         SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
94         }
95         
96         if(SDL_PollEvent(&sdl_event))
97         {
98                 /* Note! m_instance[sdl_event.jaxis.which]
99                  * will segfault if over JOYINDEX_MAX, not too nice but what are the chances? */
100                 switch(sdl_event.type)
101                 {
102                 case SDL_JOYAXISMOTION:
103                         SCA_Joystick::m_instance[sdl_event.jaxis.which]->OnAxisMotion(&sdl_event);
104                         break;
105                 case SDL_JOYHATMOTION:
106                         SCA_Joystick::m_instance[sdl_event.jhat.which]->OnHatMotion(&sdl_event);
107                         break;
108                 case SDL_JOYBUTTONUP:
109                         SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonUp(&sdl_event);
110                         break;
111                 case SDL_JOYBUTTONDOWN:
112                         SCA_Joystick::m_instance[sdl_event.jbutton.which]->OnButtonDown(&sdl_event);
113                         break;
114 #if 0   /* Not used yet */
115                 case SDL_JOYBALLMOTION:
116                         SCA_Joystick::m_instance[sdl_event.jball.which]->OnBallMotion(&sdl_event);
117                         break;
118 #endif
119                 default:
120                         printf("SCA_Joystick::HandleEvents, Unknown SDL event, this should not happen\n");
121                         break;
122                 }
123         }
124 }
125 #endif