2005-01-23 01:36:29 +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,
|
|
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
#include <SDL.h>
|
2005-03-25 10:33:39 +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_axis10(0),
|
|
|
|
m_axis11(0),
|
|
|
|
m_axis20(0),
|
|
|
|
m_axis21(0),
|
|
|
|
m_prec(3200),
|
|
|
|
m_buttonnum(-2),
|
|
|
|
m_hatdir(-2),
|
|
|
|
m_isinit(0),
|
|
|
|
m_istrig(0)
|
|
|
|
{
|
|
|
|
m_private = new PrivateData();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
delete m_private;
|
|
|
|
}
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
|
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
|
|
|
{
|
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
|
|
|
|
if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
|
|
|
|
echo("Error-Initializing-SDL: " << SDL_GetError());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
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];
|
2008-04-04 20:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SCA_Joystick::ReleaseInstance()
|
|
|
|
{
|
|
|
|
if (--m_refCount == 0)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
|
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
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
bool SCA_Joystick::aRightAxisIsPositive(int axis)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
int res = pGetAxis(axis,1);
|
|
|
|
res > m_prec? result = true: result = false;
|
|
|
|
m_istrig = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCA_Joystick::aUpAxisIsPositive(int axis)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
int res = pGetAxis(axis,0);
|
|
|
|
res < -m_prec? result = true : result = false;
|
|
|
|
m_istrig = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCA_Joystick::aLeftAxisIsPositive(int axis)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
int res = pGetAxis(axis,1);
|
|
|
|
res < -m_prec ? result = true : result = false;
|
|
|
|
m_istrig = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCA_Joystick::aDownAxisIsPositive(int axis)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
int res = pGetAxis(axis,0);
|
|
|
|
res > m_prec ? result = true:result = false;
|
|
|
|
m_istrig = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCA_Joystick::aButtonPressIsPositive(int button)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
|
|
|
|
m_istrig = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCA_Joystick::aButtonReleaseIsPositive(int button)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
|
|
|
|
m_istrig = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SCA_Joystick::aHatIsPositive(int dir)
|
|
|
|
{
|
|
|
|
bool result;
|
|
|
|
int res = pGetHat(dir);
|
|
|
|
res == dir? result = true : result = false;
|
|
|
|
m_istrig = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SCA_Joystick::pGetButtonPress(int button)
|
|
|
|
{
|
|
|
|
if(button == m_buttonnum)
|
|
|
|
return m_buttonnum;
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SCA_Joystick::pGetButtonRelease(int button)
|
|
|
|
{
|
|
|
|
if(button == m_buttonnum)
|
|
|
|
return m_buttonnum;
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SCA_Joystick::pGetHat(int direction)
|
|
|
|
{
|
|
|
|
if(direction == m_hatdir){
|
|
|
|
return m_hatdir;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SCA_Joystick::GetNumberOfAxes()
|
|
|
|
{
|
|
|
|
int number;
|
|
|
|
if(m_isinit){
|
|
|
|
if(m_private->m_joystick){
|
|
|
|
number = SDL_JoystickNumAxes(m_private->m_joystick);
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int SCA_Joystick::GetNumberOfButtons()
|
|
|
|
{
|
|
|
|
int number;
|
|
|
|
if(m_isinit){
|
|
|
|
if(m_private->m_joystick){
|
|
|
|
number = SDL_JoystickNumButtons(m_private->m_joystick);
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
int SCA_Joystick::GetNumberOfHats()
|
|
|
|
{
|
|
|
|
int number;
|
|
|
|
if(m_isinit){
|
|
|
|
if(m_private->m_joystick){
|
|
|
|
number = SDL_JoystickNumHats(m_private->m_joystick);
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-08-31 18:42:58 +00:00
|
|
|
bool SCA_Joystick::CreateJoystickDevice(void)
|
2005-01-23 01:36:29 +00:00
|
|
|
{
|
|
|
|
if(m_isinit == false){
|
2008-08-31 18:42:58 +00:00
|
|
|
if (m_joyindex>=SDL_NumJoysticks()) {
|
|
|
|
// don't print a message, because this is done anyway
|
|
|
|
//echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
|
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);
|
|
|
|
|
|
|
|
echo("Joystick " << m_joyindex << " initialized");
|
|
|
|
m_isinit = true;
|
2005-01-23 01:36:29 +00:00
|
|
|
}
|
2008-08-31 18:42:58 +00:00
|
|
|
return true;
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 06:25:13 +00:00
|
|
|
int SCA_Joystick::Connected(void)
|
|
|
|
{
|
|
|
|
if (m_isinit){
|
|
|
|
if(SDL_JoystickOpened(m_joyindex)){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-03-25 10:33:39 +00:00
|
|
|
|
2005-01-23 01:36:29 +00:00
|
|
|
void SCA_Joystick::pFillAxes()
|
|
|
|
{
|
|
|
|
if(GetNumberOfAxes() == 1){
|
|
|
|
m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
|
|
|
|
m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
|
|
|
|
}else if(GetNumberOfAxes() > 1){
|
|
|
|
m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
|
|
|
|
m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
|
|
|
|
m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
|
|
|
|
m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3);
|
|
|
|
}else{
|
|
|
|
m_axis10 = 0;m_axis11 = 0;
|
|
|
|
m_axis20 = 0;m_axis21 = 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)
|
|
|
|
{
|
|
|
|
if(axisnum == 1 && udlr == 1)return m_axis10; //u/d
|
|
|
|
if(axisnum == 1 && udlr == 0)return m_axis11; //l/r
|
|
|
|
if(axisnum == 2 && udlr == 0)return m_axis20; //...
|
|
|
|
if(axisnum == 2 && udlr == 1)return m_axis21;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|