2 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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. The Blender
8 * Foundation also sells licenses for use in proprietary software under
9 * the Blender License. See http://www.blender.org/BL/ for information
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
22 * All rights reserved.
24 * The Original Code is: all of this file.
26 * Contributor(s): snailrose.
28 * ***** END GPL/BL DUAL LICENSE BLOCK *****
32 #include "SCA_Joystick.h"
33 #include "SCA_JoystickPrivate.h"
35 SCA_Joystick::SCA_Joystick()
47 m_private = new PrivateData();
50 SCA_Joystick::~SCA_Joystick()
55 bool SCA_Joystick::CreateJoystickDevice()
58 init = pCreateJoystickDevice();
63 void SCA_Joystick::DestroyJoystickDevice()
66 pDestroyJoystickDevice();
69 void SCA_Joystick::HandleEvents()
73 if(SDL_PollEvent(&m_private->m_event))
75 switch(m_private->m_event.type)
77 case SDL_JOYAXISMOTION: {HANDLE_AXISMOTION(OnAxisMotion);break;}
78 case SDL_JOYHATMOTION: {HANDLE_HATMOTION(OnHatMotion); break;}
79 case SDL_JOYBUTTONUP: {HANDLE_BUTTONUP(OnButtonUp); break;}
80 case SDL_JOYBUTTONDOWN: {HANDLE_BUTTONDOWN(OnButtonDown);break;}
81 case SDL_JOYBALLMOTION: {HANDLE_BALLMOTION(OnBallMotion);break;}
82 default: {HANDLE_NOEVENT(OnNothing); break;}
89 void SCA_Joystick::cSetPrecision(int val)
94 bool SCA_Joystick::aRightAxisIsPositive(int axis)
97 int res = pGetAxis(axis,1);
98 res > m_prec? result = true: result = false;
104 bool SCA_Joystick::aUpAxisIsPositive(int axis)
107 int res = pGetAxis(axis,0);
108 res < -m_prec? result = true : result = false;
114 bool SCA_Joystick::aLeftAxisIsPositive(int axis)
117 int res = pGetAxis(axis,1);
118 res < -m_prec ? result = true : result = false;
124 bool SCA_Joystick::aDownAxisIsPositive(int axis)
127 int res = pGetAxis(axis,0);
128 res > m_prec ? result = true:result = false;
134 bool SCA_Joystick::aButtonPressIsPositive(int button)
137 SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
143 bool SCA_Joystick::aButtonReleaseIsPositive(int button)
146 SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
152 bool SCA_Joystick::aHatIsPositive(int dir)
155 int res = pGetHat(dir);
156 res == dir? result = true : result = false;
162 int SCA_Joystick::pGetButtonPress(int button)
164 if(button == m_buttonnum)
171 int SCA_Joystick::pGetButtonRelease(int button)
173 if(button == m_buttonnum)
179 int SCA_Joystick::pGetHat(int direction)
181 if(direction == m_hatdir){
188 bool SCA_Joystick::GetJoyAxisMotion()
192 if(SDL_PollEvent(&m_private->m_event)){
193 switch(m_private->m_event.type)
195 case SDL_JOYAXISMOTION:
205 bool SCA_Joystick::GetJoyButtonPress()
209 if(SDL_PollEvent(&m_private->m_event)){
210 switch(m_private->m_event.type)
212 case SDL_JOYBUTTONDOWN:
222 bool SCA_Joystick::GetJoyButtonRelease()
227 if(SDL_PollEvent(&m_private->m_event)){
228 switch(m_private->m_event.type)
230 case SDL_JOYBUTTONUP:
240 bool SCA_Joystick::GetJoyHatMotion()
244 if(SDL_PollEvent(&m_private->m_event)){
245 switch(m_private->m_event.type)
247 case SDL_JOYHATMOTION:
257 int SCA_Joystick::GetNumberOfAxes()
261 if(m_private->m_joystick){
262 number = SDL_JoystickNumAxes(m_private->m_joystick);
270 int SCA_Joystick::GetNumberOfButtons()
274 if(m_private->m_joystick){
275 number = SDL_JoystickNumButtons(m_private->m_joystick);
282 int SCA_Joystick::GetNumberOfHats()
286 if(m_private->m_joystick){
287 number = SDL_JoystickNumHats(m_private->m_joystick);
295 bool SCA_Joystick::pCreateJoystickDevice()
297 if(m_isinit == false){
298 if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
299 echo("Error-Initializing-SDL: " << SDL_GetError());
302 if(SDL_NumJoysticks() > 0){
303 for(int i=0; i<SDL_NumJoysticks();i++){
304 m_private->m_joystick = SDL_JoystickOpen(i);
305 SDL_JoystickEventState(SDL_ENABLE);
308 echo("Joystick-initialized");
312 echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
320 void SCA_Joystick::pDestroyJoystickDevice()
323 for(int i=0; i<SDL_NumJoysticks(); i++){
324 if(SDL_JoystickOpened(i)){
325 SDL_JoystickClose(m_private->m_joystick);
328 SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
331 void SCA_Joystick::pFillAxes()
333 if(GetNumberOfAxes() == 1){
334 m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
335 m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
336 }else if(GetNumberOfAxes() > 1){
337 m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
338 m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
339 m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
340 m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3);
342 m_axis10 = 0;m_axis11 = 0;
343 m_axis20 = 0;m_axis21 = 0;
347 int SCA_Joystick::pGetAxis(int axisnum, int udlr)
349 if(axisnum == 1 && udlr == 1)return m_axis10; //u/d
350 if(axisnum == 1 && udlr == 0)return m_axis11; //l/r
351 if(axisnum == 2 && udlr == 0)return m_axis20; //...
352 if(axisnum == 2 && udlr == 1)return m_axis21;