Skip to content
This repository has been archived by the owner on Nov 4, 2023. It is now read-only.

Commit

Permalink
Tweaked glfw joystick mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
blitz-research committed Aug 26, 2014
1 parent d35c4e1 commit 2213430
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 38 deletions.
131 changes: 112 additions & 19 deletions targets/glfw/modules/native/glfwgame.cpp
Expand Up @@ -102,6 +102,24 @@ enum{
VKEY_COMMA=188,VKEY_PERIOD=190,VKEY_SLASH=191
};

enum{
JOY_A=0x00,
JOY_B=0x01,
JOY_X=0x02,
JOY_Y=0x03,
JOY_LB=0x04,
JOY_RB=0x05,
JOY_BACK=0x06,
JOY_START=0x07,
JOY_LEFT=0x08,
JOY_UP=0x09,
JOY_RIGHT=0x0a,
JOY_DOWN=0x0b,
JOY_LSB=0x0c,
JOY_RSB=0x0d,
JOY_MENU=0x0e
};

BBGlfwGame *BBGlfwGame::_glfwGame;

BBGlfwGame::BBGlfwGame():_updatePeriod(0),_nextUpdate(0),_swapInterval( CFG_GLFW_SWAP_INTERVAL ){
Expand All @@ -126,39 +144,112 @@ int BBGlfwGame::Millisecs(){

bool BBGlfwGame::PollJoystick( int port,Array<Float> joyx,Array<Float> joyy,Array<Float> joyz,Array<bool> buttons ){

int joy=GLFW_JOYSTICK_1+port;
if( !glfwGetJoystickParam( joy,GLFW_PRESENT ) ) return false;
//Just in case...my PC has either started doing weird things with joystick ordering, or I assumed too much in the past!
int joy=0;
for( joy=GLFW_JOYSTICK_1;joy<=GLFW_JOYSTICK_16;++joy ){
if( !glfwGetJoystickParam( joy,GLFW_PRESENT ) ) continue;
if( !port ) break;
--port;
}
if( joy>GLFW_JOYSTICK_16 ) return false;

//Stopped working at some point...
// int joy=GLFW_JOYSTICK_1+port;
// if( !glfwGetJoystickParam( joy,GLFW_PRESENT ) ) return false;

//read axes
float axes[6];
memset( axes,0,sizeof(axes) );
int n_axes=glfwGetJoystickPos( joy,axes,6 );
joyx[0]=axes[0];joyy[0]=axes[1];joyz[0]=axes[2];
joyx[1]=axes[3];joyy[1]=axes[4];joyz[1]=axes[5];

//read buttons
unsigned char buts[32];
memset( buts,0,sizeof(buts) );
int n_buts=glfwGetJoystickButtons( joy,buts,32 );
if( n_buts>12 ){
for( int i=0;i<8;++i ) buttons[i]=(buts[i]==GLFW_PRESS);
for( int i=0;i<4;++i ) buttons[i+8]=(buts[n_buts-4+i]==GLFW_PRESS);
for( int i=0;i<n_buts-12;++i ) buttons[i+12]=(buts[i+8]==GLFW_PRESS);

// static int done;
// if( !done++ ) printf( "n_axes=%i, n_buts=%i\n",n_axes,n_buts );fflush( stdout );

const int *dev_axes;
const int *dev_buttons;

#if _WIN32

//xbox 360 controller
const int xbox360_axes[]={0,1,2,4,0x43,0x42,999};
const int xbox360_buttons[]={0,1,2,3,4,5,6,7,-4,-3,-2,-1,8,9,999};

//logitech dual action
const int logitech_axes[]={0,1,0x86,2,0x43,0x87,999};
const int logitech_buttons[]={1,2,0,3,4,5,8,9,-4,-3,-2,-1,10,11,999};

if( n_axes==5 && n_buts==14 ){
dev_axes=xbox360_axes;
dev_buttons=xbox360_buttons;
}else{
for( int i=0;i<n_buts;++i ) buttons[i]=(buts[i]==GLFW_PRESS);
dev_axes=logitech_axes;
dev_buttons=logitech_buttons;
}

//kludges for device type!
if( n_axes==5 && n_buts==14 ){
//XBOX_360?
joyx[1]=axes[4];
joyy[1]=-axes[3];
}else if( n_axes==4 && n_buts==18 ){
//My Saitek?
joyy[1]=-joyz[0];
#else

//xbox 360 controller
const int xbox360_axes[]={0,1,0x14,2,3,0x25,999};
const int xbox360_buttons[]={11,12,13,14,8,9,5,4,2,0,3,1,6,7,10,999};

//ps3 controller
const int ps3_axes[]={0,1,0x88,2,3,0x89,999};
const int ps3_buttons[]={14,13,15,12,10,11,0,3,7,4,5,6,1,2,16,999};

//logitech dual action
const int logitech_axes[]={0,1,0x86,2,3,0x87,999};
const int logitech_buttons[]={1,2,0,3,4,5,8,9,15,12,13,14,10,11,999};

if( n_axes==6 && n_buts==15 ){
dev_axes=xbox360_axes;
dev_buttons=xbox360_buttons;
}else if( n_axes==4 && n_buts==19 ){
dev_axes=ps3_axes;
dev_buttons=ps3_buttons;
}else{
dev_axes=logitech_axes;
dev_buttons=logitech_buttons;
}

#endif

const int *p=dev_axes;

float joys[6]={0,0,0,0,0,0};

for( int i=0;i<6 && p[i]!=999;++i ){
int j=p[i]&0xf,k=p[i]&~0xf;
if( k==0x10 ){
joys[i]=(axes[j]+1)/2;
}else if( k==0x20 ){
joys[i]=(1-axes[j])/2;
}else if( k==0x40 ){
joys[i]=-axes[j];
}else if( k==0x80 ){
joys[i]=(buts[j]==GLFW_PRESS);
}else{
joys[i]=axes[j];
}
}

//enough!
joyx[0]=joys[0];joyy[0]=joys[1];joyz[0]=joys[2];
joyx[1]=joys[3];joyy[1]=joys[4];joyz[1]=joys[5];

p=dev_buttons;

for( int i=0;i<32;++i ) buttons[i]=false;

for( int i=0;i<32 && p[i]!=999;++i ){
int j=p[i];
if( j<0 ) j+=n_buts;
buttons[i]=(buts[j]==GLFW_PRESS);
}

return true;
}

Expand Down Expand Up @@ -250,7 +341,7 @@ String BBGlfwGame::PathToFilePath( String path ){
return _baseDir+"/data/"+path.Slice( 14 );
}else if( path.StartsWith( "monkey://internal/" ) ){
if( !_internalDir.Length() ){
#ifdef CFG_GFLW_APP_LABEL
#ifdef CFG_GLFW_APP_LABEL

#if _WIN32
_internalDir=String( getenv( "APPDATA" ) );
Expand Down Expand Up @@ -574,6 +665,7 @@ void BBGlfwGame::SetSwapInterval( int interval ){
}

void BBGlfwGame::UpdateEvents(){

if( _suspended ){
glfwWaitEvents();
}else{
Expand Down Expand Up @@ -605,6 +697,7 @@ void BBGlfwGame::Run(){
while( glfwGetWindowParam( GLFW_OPENED ) ){

RenderGame();

glfwSwapBuffers();

if( _nextUpdate ){
Expand Down
35 changes: 16 additions & 19 deletions targets/glfw/template/glfw/lib/win32/win32_joystick.c
Expand Up @@ -55,7 +55,8 @@ static int _glfwJoystickPresent( int joy )
{
return GL_FALSE;
}



// Is the joystick present?
if( _glfw_joyGetPos( joy - GLFW_JOYSTICK_1, &ji ) != JOYERR_NOERROR )
{
Expand Down Expand Up @@ -203,20 +204,18 @@ int _glfwPlatformGetJoystickButtons( int joy, unsigned char *buttons,
JOYINFOEX ji;
int button;

// return 0;

// Is joystick present?
if( !_glfwJoystickPresent( joy ) )
{
return 0;
}

// Get joystick capabilities
_glfw_joyGetDevCaps( joy - GLFW_JOYSTICK_1, &jc, sizeof(JOYCAPS) );

// Get joystick state
ji.dwSize = sizeof( JOYINFOEX );
ji.dwFlags = JOY_RETURNBUTTONS;
ji.dwFlags = JOY_RETURNBUTTONS|JOY_RETURNPOV;
_glfw_joyGetPosEx( joy - GLFW_JOYSTICK_1, &ji );

// Get states of all requested buttons
Expand All @@ -226,20 +225,18 @@ int _glfwPlatformGetJoystickButtons( int joy, unsigned char *buttons,
buttons[ button ] = (unsigned char)(ji.dwButtons & (1UL << button) ? GLFW_PRESS : GLFW_RELEASE);
button++;
}

//also POV thingy
if (jc.wCaps & JOYCAPS_HASPOV)
{
const int masks[]={ 2,6,4,12, 8,9,1,3 };

int pov=ji.dwPOV/100/45;

int mask=( pov>=0 && pov<8 ) ? masks[ pov ] : 0;

int i;
for( i=0;button < numbuttons && i<4;++i )
{
buttons[ button ] = (mask & (1<<i)) ? GLFW_PRESS : GLFW_RELEASE;

if( (jc.wCaps & JOYCAPS_HASPOV) && button+3 < numbuttons ){
int i=-1;
switch( ji.dwPOV ){
case JOY_POVLEFT:i=0;break;
case JOY_POVFORWARD:i=1;break;
case JOY_POVRIGHT:i=2;break;
case JOY_POVBACKWARD:i=3;break;
}
int j;
for( j=0;j<4;++j ){
buttons[button] = (j==i) ? GLFW_PRESS : GLFW_RELEASE;
button++;
}
}
Expand Down

0 comments on commit 2213430

Please sign in to comment.