typedef struct bJoystickSensor {
char name[32];
short type;
- short pad;
+ short joyindex;
int axis;
int axisf;
int button;
#define SENS_JOY_HAT_DIR 0
#define SENS_DELAY_REPEAT 1
-
+// should match JOYINDEX_MAX in SCA_JoystickDefines.h */
+#define SENS_JOY_MAXINDEX 8
#endif
draw_default_sensor_header(sens, block, xco, yco, width);
joy= sens->data;
-
+
+ uiDefButS(block, NUM, 1, "Index:", xco+10, yco-44, 0.6 * (width-120), 19,
+ &joy->joyindex, 0, SENS_JOY_MAXINDEX-1, 100, 0,
+ "Spesify which joystick to use");
str= "Type %t|Button %x0|Axis %x1|Hat%x2";
- uiDefButS(block, MENU, B_REDR, str, xco+10, yco-44, 0.6 * (width-20), 19,
+ uiDefButS(block, MENU, B_REDR, str, xco+87, yco-44, 0.6 * (width-150), 19,
&joy->type, 0, 31, 0, 0,
"The type of event this joystick sensor is triggered on.");
gamesensor = new SCA_JoystickSensor(
eventmgr,
gameobj,
+ bjoy->joyindex,
joysticktype,
axis,axisf,
prec,
#include "SCA_Joystick.h"
#include "SCA_JoystickPrivate.h"
-
-SCA_Joystick::SCA_Joystick()
+SCA_Joystick::SCA_Joystick(short int index)
:
+ m_joyindex(index),
m_axis10(0),
m_axis11(0),
m_axis20(0),
delete m_private;
}
-SCA_Joystick *SCA_Joystick::m_instance = NULL;
+SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
int SCA_Joystick::m_refCount = 0;
-SCA_Joystick *SCA_Joystick::GetInstance()
+SCA_Joystick *SCA_Joystick::GetInstance( short int joyindex )
{
- if (m_instance == 0)
+ if (joyindex < 0 || joyindex >= JOYINDEX_MAX) {
+ echo("Error-invalid joystick index: " << joyindex);
+ return NULL;
+ }
+
+ if (m_refCount == 0)
{
- m_instance = new SCA_Joystick();
- m_instance->CreateJoystickDevice();
+ 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();
+ }
m_refCount = 1;
}
else
{
m_refCount++;
}
- return m_instance;
+ return m_instance[joyindex];
}
void SCA_Joystick::ReleaseInstance()
{
if (--m_refCount == 0)
{
- DestroyJoystickDevice();
- delete m_instance;
- m_instance = NULL;
+ 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 );
}
}
-
-bool SCA_Joystick::CreateJoystickDevice()
-{
- bool init = false;
- init = pCreateJoystickDevice();
- return init;
-}
-
-
-void SCA_Joystick::DestroyJoystickDevice()
-{
- if(m_isinit)
- pDestroyJoystickDevice();
-}
-
-
void SCA_Joystick::HandleEvents()
{
if(m_isinit)
return -1;
}
-bool SCA_Joystick::pCreateJoystickDevice()
+bool SCA_Joystick::CreateJoystickDevice(void)
{
if(m_isinit == false){
- if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
- echo("Error-Initializing-SDL: " << SDL_GetError());
- return false;
- }
- if(SDL_NumJoysticks() > 0){
- for(int i=0; i<SDL_NumJoysticks();i++){
- m_private->m_joystick = SDL_JoystickOpen(i);
- SDL_JoystickEventState(SDL_ENABLE);
- m_numjoys = i;
- }
- echo("Joystick-initialized");
- m_isinit = true;
- return true;
- }else{
- echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
+ if (m_joyindex>=SDL_NumJoysticks()) {
+ // don't print a message, because this is done anyway
+ //echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
return false;
}
+
+ m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
+ SDL_JoystickEventState(SDL_ENABLE);
+
+ echo("Joystick " << m_joyindex << " initialized");
+ m_isinit = true;
}
- return false;
+ return true;
}
-void SCA_Joystick::pDestroyJoystickDevice()
+void SCA_Joystick::DestroyJoystickDevice(void)
{
- echo("Closing-");
- for(int i=0; i<SDL_NumJoysticks(); i++){
- if(SDL_JoystickOpened(i)){
+ if (m_isinit){
+ if(SDL_JoystickOpened(m_joyindex)){
+ echo("Closing-joystick " << m_joyindex);
SDL_JoystickClose(m_private->m_joystick);
}
+ m_isinit = false;
}
- SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
}
class SCA_Joystick
{
- static SCA_Joystick *m_instance;
+ static SCA_Joystick *m_instance[JOYINDEX_MAX];
static int m_refCount;
class PrivateData;
int m_joyindex;
- /*!
-
- * the number of avail joysticks
-
- */
-
- int m_numjoys;
-
/*
*support for 2 axes
*/
- bool pCreateJoystickDevice(void);
+ bool CreateJoystickDevice(void);
/*
*/
- void pDestroyJoystickDevice(void);
+ void DestroyJoystickDevice(void);
int pGetHat(int direction);
- SCA_Joystick();
+ SCA_Joystick(short int index);
~SCA_Joystick();
- bool CreateJoystickDevice(void);
-
- void DestroyJoystickDevice(void);
-
-
public:
- static SCA_Joystick *GetInstance();
+ static SCA_Joystick *GetInstance( short int joyindex );
void ReleaseInstance();
#define echo(x) std::cout << x << std::endl;
#endif
+#define JOYINDEX_MAX 8
+
/* function callbacks */
#define HANDLE_AXISMOTION(fn) ((fn)(), 0L)
#define HANDLE_HATMOTION(fn) ((fn)(), 0L)
: SCA_EventManager(JOY_EVENTMGR),
m_logicmgr(logicmgr)
{
- m_joystick = SCA_Joystick::GetInstance();
+ int i;
+ for (i=0; i<JOYINDEX_MAX; i++) {
+ m_joystick[i] = SCA_Joystick::GetInstance( i );
+ }
+ //m_joystick = NULL;
}
SCA_JoystickManager::~SCA_JoystickManager()
{
- m_joystick->ReleaseInstance();
+ int i;
+ for (i=0; i<JOYINDEX_MAX; i++) {
+ m_joystick[i]->ReleaseInstance();
+ }
}
SCA_JoystickSensor* joysensor = (SCA_JoystickSensor*)(*it);
if(!joysensor->IsSuspended())
{
- m_joystick->HandleEvents();
+ m_joystick[joysensor->GetJoyIndex()]->HandleEvents();
joysensor->Activate(m_logicmgr, NULL);
}
}
}
-SCA_Joystick *SCA_JoystickManager::GetJoystickDevice()
+SCA_Joystick *SCA_JoystickManager::GetJoystickDevice( short int joyindex)
{
/*
*Return the instance of SCA_Joystick for use
*/
- return m_joystick;
+ return m_joystick[joyindex];
}
/**
* SDL Joystick Class Instance
*/
- SCA_Joystick *m_joystick;
+ SCA_Joystick *m_joystick[JOYINDEX_MAX];
public:
SCA_JoystickManager(class SCA_LogicManager* logicmgr);
virtual ~SCA_JoystickManager();
virtual void NextFrame(double curtime,double deltatime);
- SCA_Joystick* GetJoystickDevice(void);
+ SCA_Joystick* GetJoystickDevice(short int joyindex);
};
SCA_JoystickSensor::SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
SCA_IObject* gameobj,
+ short int joyindex,
short int joymode,
int axis, int axisf,int prec,
int button, int buttonf,
m_hat(hat),
m_hatf(hatf),
m_precision(prec),
- m_joymode(joymode)
+ m_joymode(joymode),
+ m_joyindex(joyindex)
{
/*
std::cout << " axis " << m_axis << std::endl;
bool SCA_JoystickSensor::Evaluate(CValue* event)
{
- SCA_Joystick *js = m_pJoystickMgr->GetJoystickDevice();
+ SCA_Joystick *js = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
bool result = false;
bool reset = m_reset && m_level;
PyObject* args,
PyObject* kwds) {
int a,b,c,d;
- SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
+ SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
a = joy->GetAxis10();
b = joy->GetAxis11();
c = joy->GetAxis20();
PyObject* args,
PyObject* kwds) {
int num;
- SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
+ SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
num = joy->GetNumberOfAxes();
return Py_BuildValue("i",num);
}
PyObject* args,
PyObject* kwds) {
int num;
- SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
+ SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
num = joy->GetNumberOfButtons();
return Py_BuildValue("i",num);
}
PyObject* args,
PyObject* kwds) {
int num;
- SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice();
+ SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
num = joy->GetNumberOfHats();
return Py_BuildValue("i",num);
}
* The mode to determine axis,button or hat
*/
short int m_joymode;
+ /**
+ * Select which joystick to use
+ */
+ short int m_joyindex;
enum KX_JOYSENSORMODE {
KX_JOYSENSORMODE_NODEF = 0,
public:
SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
SCA_IObject* gameobj,
+ short int joyindex,
short int joymode,
int axis, int axisf,int prec,
int button, int buttonf,
virtual bool IsPositiveTrigger();
virtual void Init();
+ short int GetJoyIndex(void){
+ return m_joyindex;
+ }
+
/* --------------------------------------------------------------------- */
/* Python interface ---------------------------------------------------- */
/* --------------------------------------------------------------------- */