FS#143 - Increase the number of supported joysticks to 4

FS#142 - Fix joystick axes being sometimes ignored


git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1318 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-01-05 16:48:32 +00:00
parent 053042bf5a
commit 8bbf7dfc46
7 changed files with 125 additions and 51 deletions

View File

@ -412,7 +412,7 @@
>
</File>
<File
RelativePath="..\..\src\SFML\Window\Event.hpp"
RelativePath="..\..\include\SFML\Window\Event.hpp"
>
</File>
<File

View File

@ -48,12 +48,18 @@ namespace priv
void Joystick::Initialize(unsigned int Index)
{
// Initial state
myNbAxes = 0;
myNbButtons = 0;
for (int i = 0; i < Joy::Count; ++i)
myState.Axis[i] = 0.f;
myPovX = 0;
myPovY = 0;
for (int i = 0; i < JoystickState::MaxButtons; ++i)
{
myState.Buttons[i] = false;
}
for (int i = 0; i < Joy::Count; ++i)
{
myState.Axis[i] = 0.f;
myAxes[i] = false;
}
// Open the joystick handle
std::ostringstream oss;
@ -64,12 +70,29 @@ void Joystick::Initialize(unsigned int Index)
// Use non-blocking mode
fcntl(myDescriptor, F_SETFL, O_NONBLOCK);
// Get number of axes and buttons
char NbAxes, NbButtons;
ioctl(myDescriptor, JSIOCGAXES, &NbAxes);
// Get number of buttons
char NbButtons;
ioctl(myDescriptor, JSIOCGBUTTONS, &NbButtons);
myNbAxes = NbAxes;
myNbButtons = NbButtons;
// Get the supported axes
char NbAxes, Axes[ABS_MAX + 1];
ioctl(myDescriptor, JSIOCGAXES, &NbAxes);
ioctl(myDescriptor, JSIOCGAXMAP, Axes);
for (int i = 0; i < NbAxes; ++i)
{
switch (Axes[i])
{
case ABS_X : myAxes[Joy::AxisX] = true; break;
case ABS_Y : myAxes[Joy::AxisY] = true; break;
case ABS_Z : case ABS_THROTTLE : myAxes[Joy::AxisZ] = true; break;
case ABS_RZ: case ABS_RUDDER: myAxes[Joy::AxisR] = true; break;
case ABS_RX : myAxes[Joy::AxisU] = true; break;
case ABS_RY : myAxes[Joy::AxisV] = true; break;
case ABS_HAT0X : case ABS_HAT0Y : myAxes[Joy::AxisPOV] = true; break;
default : break;
}
}
}
}
@ -89,9 +112,39 @@ JoystickState Joystick::UpdateState()
// An axis has been moved
case JS_EVENT_AXIS :
{
if (JoyState.number < Joy::Count)
myState.Axis[JoyState.number] = JoyState.value * 100.f / 32767.f;
switch (JoyState.number)
{
case ABS_X : myState.Axis[Joy::AxisX] = JoyState.value * 100.f / 32767.f; break;
case ABS_Y : myState.Axis[Joy::AxisY] = JoyState.value * 100.f / 32767.f; break;
case ABS_Z : case ABS_THROTTLE : myState.Axis[Joy::AxisZ] = JoyState.value * 100.f / 32767.f; break;
case ABS_RZ: case ABS_RUDDER: myState.Axis[Joy::AxisR] = JoyState.value * 100.f / 32767.f; break;
case ABS_RX : myState.Axis[Joy::AxisU] = JoyState.value * 100.f / 32767.f; break;
case ABS_RY : myState.Axis[Joy::AxisV] = JoyState.value * 100.f / 32767.f; break;
case ABS_HAT0X : myPovX = JoyState.value; break;
case ABS_HAT0Y : myPovY = JoyState.value; break;
default : break;
}
break;
// Compute the new POV angle
if (myPovX == 1)
{
if (myPovY == 1) myState.Axis[Joy::AxisPOV] = 315.f;
else if (myPovY == -1) myState.Axis[Joy::AxisPOV] = 45.f;
else myState.Axis[Joy::AxisPOV] = 0.f;
}
else if (myPovX == -1)
{
if (myPovY == 1) myState.Axis[Joy::AxisPOV] = 225.f;
else if (myPovY == -1) myState.Axis[Joy::AxisPOV] = 135.f;
else myState.Axis[Joy::AxisPOV] = 180.f;
}
else
{
if (myPovY == 1) myState.Axis[Joy::AxisPOV] = 270.f;
else if (myPovY == -1) myState.Axis[Joy::AxisPOV] = 90.f;
else myState.Axis[Joy::AxisPOV] = 0.f; // what is it supposed to be??
}
}
// A button has been pressed
@ -110,11 +163,11 @@ JoystickState Joystick::UpdateState()
////////////////////////////////////////////////////////////
/// Get the number of axes supported by the joystick
/// Check if the joystick supports the given axis
////////////////////////////////////////////////////////////
unsigned int Joystick::GetAxesCount() const
bool Joystick::HasAxis(Joy::Axis Axis) const
{
return myNbAxes;
return myAxes[Axis];
}
@ -148,11 +201,11 @@ JoystickState Joystick::UpdateState()
////////////////////////////////////////////////////////////
/// Get the number of axes supported by the joystick
/// Check if the joystick supports the given axis
////////////////////////////////////////////////////////////
unsigned int Joystick::GetAxesCount() const
bool Joystick::HasAxis(Joy::Axis Axis) const
{
return 0;
return false;
}

View File

@ -58,12 +58,14 @@ public :
JoystickState UpdateState();
////////////////////////////////////////////////////////////
/// Get the number of axes supported by the joystick
/// Check if the joystick supports the given axis
///
/// \return Number of axis
/// \param Axis : Axis to check
///
/// \return True of the axis is supported, false otherwise
///
////////////////////////////////////////////////////////////
unsigned int GetAxesCount() const;
bool HasAxis(Joy::Axis Axis) const;
////////////////////////////////////////////////////////////
/// Get the number of buttons supported by the joystick
@ -78,10 +80,12 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
int myDescriptor; ///< Linux descriptor of the joystick
unsigned int myNbAxes; ///< Number of axis supported by the joystick
unsigned int myNbButtons; ///< Number of buttons supported by the joystick
JoystickState myState; ///< Current state of the joystick
int myDescriptor; ///< Linux descriptor of the joystick
unsigned int myNbButtons; ///< Number of buttons supported by the joystick
bool myAxes[Joy::Count]; ///< Supported axes
JoystickState myState; ///< Current state of the joystick
int myPovX; ///< Last X position of the POV
int myPovY; ///< Last Y position of the POV
};
} // namespace priv

View File

@ -42,10 +42,12 @@ namespace priv
void Joystick::Initialize(unsigned int Index)
{
// Reset state
myIndex = JOYSTICKID1;
myNbAxes = 0;
myNbButtons = 0;
myIsConnected = false;
myIndex = JOYSTICKID1;
myNbButtons = 0;
myIsConnected = false;
myHasContinuousPOV = false;
for (int i = 0; i < Joy::Count; ++i)
myAxes[i] = false;
// Get the Index-th connected joystick
MMRESULT Error;
@ -64,11 +66,19 @@ void Joystick::Initialize(unsigned int Index)
myIsConnected = true;
JOYCAPS Caps;
joyGetDevCaps(myIndex, &Caps, sizeof(Caps));
myNbAxes = Caps.wNumAxes;
myNbButtons = Caps.wNumButtons;
if (myNbButtons > JoystickState::MaxButtons)
myNbButtons = JoystickState::MaxButtons;
myAxes[Joy::AxisX] = true;
myAxes[Joy::AxisY] = true;
myAxes[Joy::AxisZ] = (Caps.wCaps & JOYCAPS_HASZ) != 0;
myAxes[Joy::AxisR] = (Caps.wCaps & JOYCAPS_HASR) != 0;
myAxes[Joy::AxisU] = (Caps.wCaps & JOYCAPS_HASU) != 0;
myAxes[Joy::AxisV] = (Caps.wCaps & JOYCAPS_HASV) != 0;
myAxes[Joy::AxisPOV] = (Caps.wCaps & JOYCAPS_HASPOV) != 0;
myHasContinuousPOV = (Caps.wCaps & JOYCAPS_POVCTS) != 0;
return;
}
@ -94,8 +104,9 @@ JoystickState Joystick::UpdateState()
{
// Get the current joystick state
JOYINFOEX Pos;
Pos.dwFlags = JOY_RETURNALL;
Pos.dwSize = sizeof(JOYINFOEX);
Pos.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNBUTTONS;
Pos.dwFlags |= myHasContinuousPOV ? JOY_RETURNPOVCTS : JOY_RETURNPOV;
Pos.dwSize = sizeof(JOYINFOEX);
if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
{
// Axes
@ -121,11 +132,11 @@ JoystickState Joystick::UpdateState()
////////////////////////////////////////////////////////////
/// Get the number of axes supported by the joystick
/// Check if the joystick supports the given axis
////////////////////////////////////////////////////////////
unsigned int Joystick::GetAxesCount() const
bool Joystick::HasAxis(Joy::Axis Axis) const
{
return myNbAxes;
return myAxes[Axis];
}

View File

@ -58,12 +58,14 @@ public :
JoystickState UpdateState();
////////////////////////////////////////////////////////////
/// Get the number of axes supported by the joystick
/// Check if the joystick supports the given axis
///
/// \return Number of axis
/// \param Axis : Axis to check
///
/// \return True of the axis is supported, false otherwise
///
////////////////////////////////////////////////////////////
unsigned int GetAxesCount() const;
bool HasAxis(Joy::Axis Axis) const;
////////////////////////////////////////////////////////////
/// Get the number of buttons supported by the joystick
@ -78,10 +80,11 @@ private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
bool myIsConnected; ///< Is there a joystick connected?
unsigned int myIndex; ///< Windows ID of the joystick
unsigned int myNbAxes; ///< Number of axis supported by the joystick
unsigned int myNbButtons; ///< Number of buttons supported by the joystick
bool myIsConnected; ///< Is there a joystick connected?
unsigned int myIndex; ///< Windows ID of the joystick
unsigned int myNbButtons; ///< Number of buttons supported by the joystick
bool myAxes[Joy::Count]; ///< Supported axes
bool myHasContinuousPOV; ///< True if the driver supports continuous values for the POV
};
} // namespace priv

View File

@ -221,18 +221,21 @@ void WindowImpl::ProcessJoystickEvents()
myJoyStates[i] = myJoysticks[i].UpdateState();
// Axis
for (unsigned int j = 0; j < myJoysticks[i].GetAxesCount(); ++j)
for (unsigned int j = 0; j < Joy::Count; ++j)
{
float PrevPos = PreviousState.Axis[j];
float CurrPos = myJoyStates[i].Axis[j];
if (fabs(CurrPos - PrevPos) >= myJoyThreshold)
if (myJoysticks[i].HasAxis(static_cast<Joy::Axis>(i)))
{
Event Event;
Event.Type = Event::JoyMoved;
Event.JoyMove.JoystickId = i;
Event.JoyMove.Axis = static_cast<Joy::Axis>(j);
Event.JoyMove.Position = CurrPos;
SendEvent(Event);
float PrevPos = PreviousState.Axis[j];
float CurrPos = myJoyStates[i].Axis[j];
if (fabs(CurrPos - PrevPos) >= myJoyThreshold)
{
Event Event;
Event.Type = Event::JoyMoved;
Event.JoyMove.JoystickId = i;
Event.JoyMove.Axis = static_cast<Joy::Axis>(j);
Event.JoyMove.Position = CurrPos;
SendEvent(Event);
}
}
}

View File

@ -293,7 +293,7 @@ private :
////////////////////////////////////////////////////////////
// Total number of joysticks supported
////////////////////////////////////////////////////////////
enum {JoysticksCount = 2};
enum {JoysticksCount = 4};
////////////////////////////////////////////////////////////
// Member data