2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
2005-01-23 01:36:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2005-01-23 01:36:29 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-01-23 01:36:29 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): snailrose.
|
|
|
|
*
|
2008-04-16 22:40:48 +00:00
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
2005-01-23 01:36:29 +00:00
|
|
|
*/
|
2011-02-25 13:32:11 +00:00
|
|
|
|
|
|
|
/** \file gameengine/GameLogic/Joystick/SCA_Joystick.cpp
|
|
|
|
* \ingroup gamelogic
|
|
|
|
*/
|
|
|
|
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
|
|
|
# include <SDL.h>
|
2008-10-07 05:09:24 +00:00
|
|
|
#endif
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2008-10-10 14:22:54 +00:00
|
|
|
#include <stdio.h>
|
2009-04-17 10:24:44 +00:00
|
|
|
#include <stdlib.h>
|
2008-10-10 14:22:54 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
#include "SCA_Joystick.h"
|
|
|
|
#include "SCA_JoystickPrivate.h"
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
SCA_Joystick::SCA_Joystick(short int index)
|
2005-01-23 01:36:29 +00:00
|
|
|
:
|
2008-08-31 18:42:58 +00:00
|
|
|
m_joyindex(index),
|
2005-01-23 01:36:29 +00:00
|
|
|
m_prec(3200),
|
2009-02-25 03:26:02 +00:00
|
|
|
m_axismax(-1),
|
|
|
|
m_buttonmax(-1),
|
|
|
|
m_hatmax(-1),
|
2005-01-23 01:36:29 +00:00
|
|
|
m_isinit(0),
|
2008-10-08 03:16:19 +00:00
|
|
|
m_istrig_axis(0),
|
|
|
|
m_istrig_button(0),
|
2009-02-25 03:26:02 +00:00
|
|
|
m_istrig_hat(0)
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
2009-04-07 06:23:45 +00:00
|
|
|
for(int i=0; i<JOYAXIS_MAX; i++)
|
|
|
|
m_axis_array[i]= 0;
|
2009-05-25 06:24:23 +00:00
|
|
|
|
|
|
|
for(int i=0; i<JOYHAT_MAX; i++)
|
|
|
|
m_hat_array[i]= 0;
|
|
|
|
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2005-01-23 01:36:29 +00:00
|
|
|
m_private = new PrivateData();
|
2008-09-21 05:38:28 +00:00
|
|
|
#endif
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
SCA_Joystick::~SCA_Joystick()
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2005-01-23 01:36:29 +00:00
|
|
|
delete m_private;
|
2008-09-21 05:38:28 +00:00
|
|
|
#endif
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
|
2009-05-28 13:44:32 +00:00
|
|
|
int SCA_Joystick::m_joynum = 0;
|
2008-04-04 20:39:31 +00:00
|
|
|
int SCA_Joystick::m_refCount = 0;
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
SCA_Joystick *SCA_Joystick::GetInstance( short int joyindex )
|
2008-04-04 20:39:31 +00:00
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifndef WITH_SDL
|
2008-09-21 05:38:28 +00:00
|
|
|
return NULL;
|
2011-10-20 00:19:21 +00:00
|
|
|
#else /* WITH_SDL */
|
2008-08-31 18:42:58 +00:00
|
|
|
if (joyindex < 0 || joyindex >= JOYINDEX_MAX) {
|
|
|
|
echo("Error-invalid joystick index: " << joyindex);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_refCount == 0)
|
2008-04-04 20:39:31 +00:00
|
|
|
{
|
2008-08-31 18:42:58 +00:00
|
|
|
int i;
|
|
|
|
// do this once only
|
2011-07-25 15:44:41 +00:00
|
|
|
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO) == -1 ){
|
2008-08-31 18:42:58 +00:00
|
|
|
echo("Error-Initializing-SDL: " << SDL_GetError());
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-05-28 13:44:32 +00:00
|
|
|
|
|
|
|
m_joynum = SDL_NumJoysticks();
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
for (i=0; i<JOYINDEX_MAX; i++) {
|
|
|
|
m_instance[i] = new SCA_Joystick(i);
|
|
|
|
m_instance[i]->CreateJoystickDevice();
|
|
|
|
}
|
2008-04-04 20:39:31 +00:00
|
|
|
m_refCount = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_refCount++;
|
|
|
|
}
|
2008-08-31 18:42:58 +00:00
|
|
|
return m_instance[joyindex];
|
2011-10-20 00:19:21 +00:00
|
|
|
#endif /* WITH_SDL */
|
2008-04-04 20:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SCA_Joystick::ReleaseInstance()
|
|
|
|
{
|
|
|
|
if (--m_refCount == 0)
|
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2008-08-31 18:42:58 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i<JOYINDEX_MAX; i++) {
|
|
|
|
if (m_instance[i]) {
|
|
|
|
m_instance[i]->DestroyJoystickDevice();
|
|
|
|
delete m_instance[i];
|
|
|
|
}
|
|
|
|
m_instance[i]= NULL;
|
|
|
|
}
|
2008-09-21 05:38:28 +00:00
|
|
|
|
2011-07-25 15:44:41 +00:00
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO);
|
2011-10-20 00:19:21 +00:00
|
|
|
#endif /* WITH_SDL */
|
2008-04-04 20:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
void SCA_Joystick::cSetPrecision(int val)
|
|
|
|
{
|
|
|
|
m_prec = val;
|
|
|
|
}
|
|
|
|
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2009-04-07 06:23:45 +00:00
|
|
|
bool SCA_Joystick::aAxisPairIsPositive(int axis)
|
2008-10-07 05:09:24 +00:00
|
|
|
{
|
2009-04-07 06:23:45 +00:00
|
|
|
return (pAxisTest(axis) > m_prec) ? true:false;
|
2008-10-07 05:09:24 +00:00
|
|
|
}
|
|
|
|
|
2009-04-07 06:23:45 +00:00
|
|
|
bool SCA_Joystick::aAxisPairDirectionIsPositive(int axis, int dir)
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
|
|
|
|
2009-04-07 06:23:45 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (dir==JOYAXIS_UP || dir==JOYAXIS_DOWN)
|
|
|
|
res = pGetAxis(axis, 1);
|
|
|
|
else /* JOYAXIS_LEFT || JOYAXIS_RIGHT */
|
|
|
|
res = pGetAxis(axis, 0);
|
|
|
|
|
|
|
|
if (dir==JOYAXIS_DOWN || dir==JOYAXIS_RIGHT)
|
|
|
|
return (res > m_prec) ? true : false;
|
|
|
|
else /* JOYAXIS_UP || JOYAXIS_LEFT */
|
|
|
|
return (res < -m_prec) ? true : false;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
2009-04-07 06:23:45 +00:00
|
|
|
bool SCA_Joystick::aAxisIsPositive(int axis_single)
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
2009-04-07 06:23:45 +00:00
|
|
|
return abs(m_axis_array[axis_single]) > m_prec ? true:false;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
2008-10-07 05:09:24 +00:00
|
|
|
bool SCA_Joystick::aAnyButtonPressIsPositive(void)
|
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2009-05-28 13:44:32 +00:00
|
|
|
/* this is needed for the "all events" option
|
|
|
|
* so we know if there are no buttons pressed */
|
|
|
|
for (int i=0; i<m_buttonmax; i++)
|
|
|
|
if (SDL_JoystickGetButton(m_private->m_joystick, i))
|
|
|
|
return true;
|
2009-06-13 11:28:29 +00:00
|
|
|
#endif
|
2009-05-28 13:44:32 +00:00
|
|
|
return false;
|
2008-10-07 05:09:24 +00:00
|
|
|
}
|
2005-01-23 01:36:29 +00:00
|
|
|
|
|
|
|
bool SCA_Joystick::aButtonPressIsPositive(int button)
|
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifndef WITH_SDL
|
2008-09-21 05:38:28 +00:00
|
|
|
return false;
|
|
|
|
#else
|
2005-01-23 01:36:29 +00:00
|
|
|
bool result;
|
|
|
|
SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
|
|
|
|
return result;
|
2008-09-21 05:38:28 +00:00
|
|
|
#endif
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCA_Joystick::aButtonReleaseIsPositive(int button)
|
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifndef WITH_SDL
|
2008-09-21 05:38:28 +00:00
|
|
|
return false;
|
|
|
|
#else
|
2005-01-23 01:36:29 +00:00
|
|
|
bool result;
|
|
|
|
SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
|
|
|
|
return result;
|
2008-09-21 05:38:28 +00:00
|
|
|
#endif
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-25 06:24:23 +00:00
|
|
|
bool SCA_Joystick::aHatIsPositive(int hatnum, int dir)
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
2009-05-25 06:24:23 +00:00
|
|
|
return (GetHat(hatnum)==dir) ? true : false;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SCA_Joystick::GetNumberOfAxes()
|
|
|
|
{
|
2008-10-07 05:09:24 +00:00
|
|
|
return m_axismax;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SCA_Joystick::GetNumberOfButtons()
|
|
|
|
{
|
2008-10-07 05:09:24 +00:00
|
|
|
return m_buttonmax;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
int SCA_Joystick::GetNumberOfHats()
|
|
|
|
{
|
2008-10-07 05:09:24 +00:00
|
|
|
return m_hatmax;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
bool SCA_Joystick::CreateJoystickDevice(void)
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifndef WITH_SDL
|
2009-05-14 21:06:48 +00:00
|
|
|
m_isinit = true;
|
|
|
|
m_axismax = m_buttonmax = m_hatmax = 0;
|
2008-09-21 05:38:28 +00:00
|
|
|
return false;
|
2011-10-20 00:19:21 +00:00
|
|
|
#else /* WITH_SDL */
|
2005-01-23 01:36:29 +00:00
|
|
|
if(m_isinit == false){
|
2009-05-28 13:44:32 +00:00
|
|
|
if (m_joyindex>=m_joynum) {
|
2008-08-31 18:42:58 +00:00
|
|
|
// don't print a message, because this is done anyway
|
|
|
|
//echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
|
2009-05-14 21:06:48 +00:00
|
|
|
|
|
|
|
// Need this so python args can return empty lists
|
|
|
|
m_axismax = m_buttonmax = m_hatmax = 0;
|
2005-01-23 01:36:29 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-08-31 18:42:58 +00:00
|
|
|
|
|
|
|
m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
|
|
|
|
SDL_JoystickEventState(SDL_ENABLE);
|
|
|
|
m_isinit = true;
|
2008-10-07 05:09:24 +00:00
|
|
|
|
|
|
|
echo("Joystick " << m_joyindex << " initialized");
|
|
|
|
|
|
|
|
/* must run after being initialized */
|
|
|
|
m_axismax = SDL_JoystickNumAxes(m_private->m_joystick);
|
|
|
|
m_buttonmax = SDL_JoystickNumButtons(m_private->m_joystick);
|
|
|
|
m_hatmax = SDL_JoystickNumHats(m_private->m_joystick);
|
2009-04-07 06:23:45 +00:00
|
|
|
|
2009-05-14 21:06:48 +00:00
|
|
|
if (m_axismax > JOYAXIS_MAX) m_axismax= JOYAXIS_MAX; /* very unlikely */
|
|
|
|
else if (m_axismax < 0) m_axismax = 0;
|
2009-04-07 06:23:45 +00:00
|
|
|
|
2009-05-25 06:24:23 +00:00
|
|
|
if (m_hatmax > JOYHAT_MAX) m_hatmax= JOYHAT_MAX; /* very unlikely */
|
|
|
|
else if(m_hatmax<0) m_hatmax= 0;
|
|
|
|
|
2009-05-14 21:06:48 +00:00
|
|
|
if(m_buttonmax<0) m_buttonmax= 0;
|
2009-05-25 06:24:23 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
2008-08-31 18:42:58 +00:00
|
|
|
return true;
|
2011-10-20 00:19:21 +00:00
|
|
|
#endif /* WITH_SDL */
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
void SCA_Joystick::DestroyJoystickDevice(void)
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2008-08-31 18:42:58 +00:00
|
|
|
if (m_isinit){
|
|
|
|
if(SDL_JoystickOpened(m_joyindex)){
|
|
|
|
echo("Closing-joystick " << m_joyindex);
|
2005-01-23 01:36:29 +00:00
|
|
|
SDL_JoystickClose(m_private->m_joystick);
|
|
|
|
}
|
2008-08-31 18:42:58 +00:00
|
|
|
m_isinit = false;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
2011-10-20 00:19:21 +00:00
|
|
|
#endif /* WITH_SDL */
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
|
|
|
|
2008-09-01 06:25:13 +00:00
|
|
|
int SCA_Joystick::Connected(void)
|
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2008-09-21 05:38:28 +00:00
|
|
|
if (m_isinit && SDL_JoystickOpened(m_joyindex))
|
|
|
|
return 1;
|
|
|
|
#endif
|
2008-09-01 06:25:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
int SCA_Joystick::pGetAxis(int axisnum, int udlr)
|
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2009-04-07 06:23:45 +00:00
|
|
|
return m_axis_array[(axisnum*2)+udlr];
|
2008-09-21 05:38:28 +00:00
|
|
|
#endif
|
2005-01-23 01:36:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-07 05:09:24 +00:00
|
|
|
int SCA_Joystick::pAxisTest(int axisnum)
|
|
|
|
{
|
2011-10-20 00:19:21 +00:00
|
|
|
#ifdef WITH_SDL
|
2009-04-07 06:23:45 +00:00
|
|
|
short i1= m_axis_array[(axisnum*2)];
|
|
|
|
short i2= m_axis_array[(axisnum*2)+1];
|
|
|
|
|
2008-10-08 03:16:19 +00:00
|
|
|
/* long winded way to do
|
|
|
|
* return MAX2(abs(i1), abs(i2))
|
|
|
|
* avoid abs from math.h */
|
|
|
|
if (i1 < 0) i1 = -i1;
|
|
|
|
if (i2 < 0) i2 = -i2;
|
|
|
|
if (i1 <i2) return i2;
|
|
|
|
else return i1;
|
2011-10-20 00:19:21 +00:00
|
|
|
#else /* WITH_SDL */
|
2008-10-07 05:09:24 +00:00
|
|
|
return 0;
|
2011-10-20 00:19:21 +00:00
|
|
|
#endif /* WITH_SDL */
|
2008-10-07 05:09:24 +00:00
|
|
|
}
|