2 * ***** BEGIN GPL 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.
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.
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.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): snailrose.
25 * ***** END GPL LICENSE BLOCK *****
33 #include "SCA_Joystick.h"
34 #include "SCA_JoystickPrivate.h"
36 SCA_Joystick::SCA_Joystick(short int index)
50 for(int i=0; i<JOYAXIS_MAX; i++)
53 m_private = new PrivateData();
58 SCA_Joystick::~SCA_Joystick()
66 SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
67 int SCA_Joystick::m_refCount = 0;
69 SCA_Joystick *SCA_Joystick::GetInstance( short int joyindex )
74 if (joyindex < 0 || joyindex >= JOYINDEX_MAX) {
75 echo("Error-invalid joystick index: " << joyindex);
83 if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
84 echo("Error-Initializing-SDL: " << SDL_GetError());
87 for (i=0; i<JOYINDEX_MAX; i++) {
88 m_instance[i] = new SCA_Joystick(i);
89 m_instance[i]->CreateJoystickDevice();
97 return m_instance[joyindex];
101 void SCA_Joystick::ReleaseInstance()
103 if (--m_refCount == 0)
107 for (i=0; i<JOYINDEX_MAX; i++) {
109 m_instance[i]->DestroyJoystickDevice();
110 delete m_instance[i];
115 SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
120 void SCA_Joystick::cSetPrecision(int val)
126 bool SCA_Joystick::aAxisPairIsPositive(int axis)
128 return (pAxisTest(axis) > m_prec) ? true:false;
131 bool SCA_Joystick::aAxisPairDirectionIsPositive(int axis, int dir)
136 if (dir==JOYAXIS_UP || dir==JOYAXIS_DOWN)
137 res = pGetAxis(axis, 1);
138 else /* JOYAXIS_LEFT || JOYAXIS_RIGHT */
139 res = pGetAxis(axis, 0);
141 if (dir==JOYAXIS_DOWN || dir==JOYAXIS_RIGHT)
142 return (res > m_prec) ? true : false;
143 else /* JOYAXIS_UP || JOYAXIS_LEFT */
144 return (res < -m_prec) ? true : false;
147 bool SCA_Joystick::aAxisIsPositive(int axis_single)
149 return abs(m_axis_array[axis_single]) > m_prec ? true:false;
152 bool SCA_Joystick::aAnyButtonPressIsPositive(void)
154 return (m_buttonnum==-2) ? false : true;
157 bool SCA_Joystick::aAnyButtonReleaseIsPositive(void)
159 return (m_buttonnum==-2) ? true : false;
162 bool SCA_Joystick::aButtonPressIsPositive(int button)
168 SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
174 bool SCA_Joystick::aButtonReleaseIsPositive(int button)
180 SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
186 bool SCA_Joystick::aHatIsPositive(int dir)
189 int res = pGetHat(dir);
190 res == dir? result = true : result = false;
194 int SCA_Joystick::pGetHat(int direction)
196 if(direction == m_hatdir){
202 int SCA_Joystick::GetNumberOfAxes()
208 int SCA_Joystick::GetNumberOfButtons()
214 int SCA_Joystick::GetNumberOfHats()
219 bool SCA_Joystick::CreateJoystickDevice(void)
224 if(m_isinit == false){
225 if (m_joyindex>=SDL_NumJoysticks()) {
226 // don't print a message, because this is done anyway
227 //echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
231 m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
232 SDL_JoystickEventState(SDL_ENABLE);
235 echo("Joystick " << m_joyindex << " initialized");
237 /* must run after being initialized */
238 m_axismax = SDL_JoystickNumAxes(m_private->m_joystick);
239 if (m_axismax > JOYAXIS_MAX) m_axismax= JOYAXIS_MAX; /* very unlikely */
241 m_buttonmax = SDL_JoystickNumButtons(m_private->m_joystick);
242 m_hatmax = SDL_JoystickNumHats(m_private->m_joystick);
251 void SCA_Joystick::DestroyJoystickDevice(void)
255 if(SDL_JoystickOpened(m_joyindex)){
256 echo("Closing-joystick " << m_joyindex);
257 SDL_JoystickClose(m_private->m_joystick);
264 int SCA_Joystick::Connected(void)
267 if (m_isinit && SDL_JoystickOpened(m_joyindex))
273 void SCA_Joystick::pFillAxes()
276 for(int i=0; i<m_axismax; i++)
277 m_axis_array[i]= SDL_JoystickGetAxis(m_private->m_joystick, i);
282 int SCA_Joystick::pGetAxis(int axisnum, int udlr)
285 return m_axis_array[(axisnum*2)+udlr];
290 int SCA_Joystick::pAxisTest(int axisnum)
293 short i1= m_axis_array[(axisnum*2)];
294 short i2= m_axis_array[(axisnum*2)+1];
296 /* long winded way to do
297 * return MAX2(abs(i1), abs(i2))
298 * avoid abs from math.h */
299 if (i1 < 0) i1 = -i1;
300 if (i2 < 0) i2 = -i2;
301 if (i1 <i2) return i2;