summaryrefslogtreecommitdiff
path: root/src/SFML/Window/Linux/Joystick.cpp
diff options
context:
space:
mode:
authorChristoph Egger <Christoph.Egger@gmx.de>2010-04-07 22:00:13 +0200
committerChristoph Egger <Christoph.Egger@gmx.de>2010-04-07 22:00:13 +0200
commit46bf1cf82d855660c212ee3421dff3050ec8ffea (patch)
treef0fadfc004a86242ad096dee015ed7c20cc7f554 /src/SFML/Window/Linux/Joystick.cpp
parent25177f23f35afa0e6ebdd63d581003c298d3a7df (diff)
Imported Upstream version 1.6+repack1
Diffstat (limited to 'src/SFML/Window/Linux/Joystick.cpp')
-rwxr-xr-xsrc/SFML/Window/Linux/Joystick.cpp98
1 files changed, 73 insertions, 25 deletions
diff --git a/src/SFML/Window/Linux/Joystick.cpp b/src/SFML/Window/Linux/Joystick.cpp
index 509bc53..829a741 100755
--- a/src/SFML/Window/Linux/Joystick.cpp
+++ b/src/SFML/Window/Linux/Joystick.cpp
@@ -28,13 +28,6 @@
#include <SFML/Window/Joystick.hpp>
#include <sstream>
-#if defined(SFML_SYSTEM_LINUX)
- #include <linux/joystick.h>
- #include <fcntl.h>
-#elif defined(SFML_SYSTEM_FREEBSD)
- // #include <sys/joystick.h> ?
-#endif
-
namespace sf
{
@@ -48,12 +41,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;
- for (int i = 0; i < JoystickState::MaxButtons; ++i)
+ myPovX = 0;
+ myPovY = 0;
+ for (int i = 0; i < Joy::ButtonCount; ++i)
+ {
myState.Buttons[i] = false;
+ }
+ for (int i = 0; i < Joy::AxisCount; ++i)
+ {
+ myState.Axis[i] = 0.f;
+ myAxes[i] = false;
+ }
// Open the joystick handle
std::ostringstream oss;
@@ -63,13 +62,32 @@ 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;
+ if (myNbButtons > Joy::ButtonCount)
+ myNbButtons = Joy::ButtonCount;
+
+ // Get the supported axes
+ char NbAxes;
+ ioctl(myDescriptor, JSIOCGAXES, &NbAxes);
+ ioctl(myDescriptor, JSIOCGAXMAP, myAxesMapping);
+ for (int i = 0; i < NbAxes; ++i)
+ {
+ switch (myAxesMapping[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,11 +107,41 @@ 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 (myAxesMapping[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;
+ }
+
+ // Compute the new POV angle
+ if (myPovX > 0)
+ {
+ if (myPovY > 0) myState.Axis[Joy::AxisPOV] = 135.f;
+ else if (myPovY < 0) myState.Axis[Joy::AxisPOV] = 45.f;
+ else myState.Axis[Joy::AxisPOV] = 90.f;
+ }
+ else if (myPovX < 0)
+ {
+ if (myPovY > 0) myState.Axis[Joy::AxisPOV] = 225.f;
+ else if (myPovY < 0) myState.Axis[Joy::AxisPOV] = 315.f;
+ else myState.Axis[Joy::AxisPOV] = 270.f;
+ }
+ else
+ {
+ if (myPovY > 0) myState.Axis[Joy::AxisPOV] = 180.f;
+ else if (myPovY < 0) myState.Axis[Joy::AxisPOV] = 0.f;
+ else myState.Axis[Joy::AxisPOV] = -1.f;
+ }
break;
}
-
+
// A button has been pressed
case JS_EVENT_BUTTON :
{
@@ -110,11 +158,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 +196,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;
}