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:
parent
053042bf5a
commit
8bbf7dfc46
@ -412,7 +412,7 @@
|
|||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath="..\..\src\SFML\Window\Event.hpp"
|
RelativePath="..\..\include\SFML\Window\Event.hpp"
|
||||||
>
|
>
|
||||||
</File>
|
</File>
|
||||||
<File
|
<File
|
||||||
|
@ -48,12 +48,18 @@ namespace priv
|
|||||||
void Joystick::Initialize(unsigned int Index)
|
void Joystick::Initialize(unsigned int Index)
|
||||||
{
|
{
|
||||||
// Initial state
|
// Initial state
|
||||||
myNbAxes = 0;
|
|
||||||
myNbButtons = 0;
|
myNbButtons = 0;
|
||||||
for (int i = 0; i < Joy::Count; ++i)
|
myPovX = 0;
|
||||||
myState.Axis[i] = 0.f;
|
myPovY = 0;
|
||||||
for (int i = 0; i < JoystickState::MaxButtons; ++i)
|
for (int i = 0; i < JoystickState::MaxButtons; ++i)
|
||||||
|
{
|
||||||
myState.Buttons[i] = false;
|
myState.Buttons[i] = false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < Joy::Count; ++i)
|
||||||
|
{
|
||||||
|
myState.Axis[i] = 0.f;
|
||||||
|
myAxes[i] = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Open the joystick handle
|
// Open the joystick handle
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
@ -64,12 +70,29 @@ void Joystick::Initialize(unsigned int Index)
|
|||||||
// Use non-blocking mode
|
// Use non-blocking mode
|
||||||
fcntl(myDescriptor, F_SETFL, O_NONBLOCK);
|
fcntl(myDescriptor, F_SETFL, O_NONBLOCK);
|
||||||
|
|
||||||
// Get number of axes and buttons
|
// Get number of buttons
|
||||||
char NbAxes, NbButtons;
|
char NbButtons;
|
||||||
ioctl(myDescriptor, JSIOCGAXES, &NbAxes);
|
|
||||||
ioctl(myDescriptor, JSIOCGBUTTONS, &NbButtons);
|
ioctl(myDescriptor, JSIOCGBUTTONS, &NbButtons);
|
||||||
myNbAxes = NbAxes;
|
|
||||||
myNbButtons = NbButtons;
|
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
|
// An axis has been moved
|
||||||
case JS_EVENT_AXIS :
|
case JS_EVENT_AXIS :
|
||||||
{
|
{
|
||||||
if (JoyState.number < Joy::Count)
|
switch (JoyState.number)
|
||||||
myState.Axis[JoyState.number] = JoyState.value * 100.f / 32767.f;
|
{
|
||||||
|
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;
|
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
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,12 +58,14 @@ public :
|
|||||||
JoystickState UpdateState();
|
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
|
/// Get the number of buttons supported by the joystick
|
||||||
@ -78,10 +80,12 @@ private :
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
int myDescriptor; ///< Linux descriptor of the joystick
|
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
|
||||||
unsigned int myNbButtons; ///< Number of buttons supported by the joystick
|
bool myAxes[Joy::Count]; ///< Supported axes
|
||||||
JoystickState myState; ///< Current state of the joystick
|
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
|
} // namespace priv
|
||||||
|
@ -42,10 +42,12 @@ namespace priv
|
|||||||
void Joystick::Initialize(unsigned int Index)
|
void Joystick::Initialize(unsigned int Index)
|
||||||
{
|
{
|
||||||
// Reset state
|
// Reset state
|
||||||
myIndex = JOYSTICKID1;
|
myIndex = JOYSTICKID1;
|
||||||
myNbAxes = 0;
|
myNbButtons = 0;
|
||||||
myNbButtons = 0;
|
myIsConnected = false;
|
||||||
myIsConnected = false;
|
myHasContinuousPOV = false;
|
||||||
|
for (int i = 0; i < Joy::Count; ++i)
|
||||||
|
myAxes[i] = false;
|
||||||
|
|
||||||
// Get the Index-th connected joystick
|
// Get the Index-th connected joystick
|
||||||
MMRESULT Error;
|
MMRESULT Error;
|
||||||
@ -64,11 +66,19 @@ void Joystick::Initialize(unsigned int Index)
|
|||||||
myIsConnected = true;
|
myIsConnected = true;
|
||||||
JOYCAPS Caps;
|
JOYCAPS Caps;
|
||||||
joyGetDevCaps(myIndex, &Caps, sizeof(Caps));
|
joyGetDevCaps(myIndex, &Caps, sizeof(Caps));
|
||||||
myNbAxes = Caps.wNumAxes;
|
|
||||||
myNbButtons = Caps.wNumButtons;
|
myNbButtons = Caps.wNumButtons;
|
||||||
if (myNbButtons > JoystickState::MaxButtons)
|
if (myNbButtons > JoystickState::MaxButtons)
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,8 +104,9 @@ JoystickState Joystick::UpdateState()
|
|||||||
{
|
{
|
||||||
// Get the current joystick state
|
// Get the current joystick state
|
||||||
JOYINFOEX Pos;
|
JOYINFOEX Pos;
|
||||||
Pos.dwFlags = JOY_RETURNALL;
|
Pos.dwFlags = JOY_RETURNX | JOY_RETURNY | JOY_RETURNZ | JOY_RETURNR | JOY_RETURNU | JOY_RETURNV | JOY_RETURNBUTTONS;
|
||||||
Pos.dwSize = sizeof(JOYINFOEX);
|
Pos.dwFlags |= myHasContinuousPOV ? JOY_RETURNPOVCTS : JOY_RETURNPOV;
|
||||||
|
Pos.dwSize = sizeof(JOYINFOEX);
|
||||||
if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
|
if (joyGetPosEx(myIndex, &Pos) == JOYERR_NOERROR)
|
||||||
{
|
{
|
||||||
// Axes
|
// 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];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,12 +58,14 @@ public :
|
|||||||
JoystickState UpdateState();
|
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
|
/// Get the number of buttons supported by the joystick
|
||||||
@ -78,10 +80,11 @@ private :
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
bool myIsConnected; ///< Is there a joystick connected?
|
bool myIsConnected; ///< Is there a joystick connected?
|
||||||
unsigned int myIndex; ///< Windows ID of the joystick
|
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
|
||||||
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
|
} // namespace priv
|
||||||
|
@ -221,18 +221,21 @@ void WindowImpl::ProcessJoystickEvents()
|
|||||||
myJoyStates[i] = myJoysticks[i].UpdateState();
|
myJoyStates[i] = myJoysticks[i].UpdateState();
|
||||||
|
|
||||||
// Axis
|
// 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];
|
if (myJoysticks[i].HasAxis(static_cast<Joy::Axis>(i)))
|
||||||
float CurrPos = myJoyStates[i].Axis[j];
|
|
||||||
if (fabs(CurrPos - PrevPos) >= myJoyThreshold)
|
|
||||||
{
|
{
|
||||||
Event Event;
|
float PrevPos = PreviousState.Axis[j];
|
||||||
Event.Type = Event::JoyMoved;
|
float CurrPos = myJoyStates[i].Axis[j];
|
||||||
Event.JoyMove.JoystickId = i;
|
if (fabs(CurrPos - PrevPos) >= myJoyThreshold)
|
||||||
Event.JoyMove.Axis = static_cast<Joy::Axis>(j);
|
{
|
||||||
Event.JoyMove.Position = CurrPos;
|
Event Event;
|
||||||
SendEvent(Event);
|
Event.Type = Event::JoyMoved;
|
||||||
|
Event.JoyMove.JoystickId = i;
|
||||||
|
Event.JoyMove.Axis = static_cast<Joy::Axis>(j);
|
||||||
|
Event.JoyMove.Position = CurrPos;
|
||||||
|
SendEvent(Event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,7 +293,7 @@ private :
|
|||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Total number of joysticks supported
|
// Total number of joysticks supported
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
enum {JoysticksCount = 2};
|
enum {JoysticksCount = 4};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
|
Loading…
Reference in New Issue
Block a user