Added joystick implementation for FreeBSD (#477)

This commit is contained in:
David Demelier 2013-10-08 22:40:11 +02:00 committed by Laurent Gomila
parent 713aed7079
commit cc3dc29ef4
19 changed files with 1043 additions and 527 deletions

View File

@ -15,9 +15,10 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
endif() endif()
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(LINUX 1) set(LINUX 1)
set(UNIX 1)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
# FreeBSD compile path is the same as Linux set(FreeBSD 1)
set(LINUX 1) set(UNIX 1)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(MACOSX 1) set(MACOSX 1)

View File

@ -52,22 +52,33 @@ if(WINDOWS)
# make sure that we use the Unicode version of the Win API functions # make sure that we use the Unicode version of the Win API functions
add_definitions(-DUNICODE) add_definitions(-DUNICODE)
elseif(LINUX) elseif(UNIX)
set(PLATFORM_SRC set(PLATFORM_SRC
${SRCROOT}/Linux/Display.cpp ${SRCROOT}/Unix/Display.cpp
${SRCROOT}/Linux/Display.hpp ${SRCROOT}/Unix/Display.hpp
${SRCROOT}/Linux/GlxContext.cpp ${SRCROOT}/Unix/GlxContext.cpp
${SRCROOT}/Linux/GlxContext.hpp ${SRCROOT}/Unix/GlxContext.hpp
${SRCROOT}/Linux/InputImpl.cpp ${SRCROOT}/Unix/InputImpl.cpp
${SRCROOT}/Linux/InputImpl.hpp ${SRCROOT}/Unix/InputImpl.hpp
${SRCROOT}/Linux/JoystickImpl.cpp ${SRCROOT}/Unix/VideoModeImpl.cpp
${SRCROOT}/Linux/JoystickImpl.hpp ${SRCROOT}/Unix/WindowImplX11.cpp
${SRCROOT}/Linux/VideoModeImpl.cpp ${SRCROOT}/Unix/WindowImplX11.hpp
${SRCROOT}/Linux/WindowImplX11.cpp
${SRCROOT}/Linux/WindowImplX11.hpp
) )
source_group("linux" FILES ${PLATFORM_SRC}) if(LINUX)
else() # MACOSX set(PLATFORM_SRC
${PLATFORM_SRC}
${SRCROOT}/Unix/JoystickImpl.cpp
${SRCROOT}/Unix/JoystickImpl.hpp
)
elseif(FREEBSD)
set(PLATFORM_SRC
${PLATFORM_SRC}
${SRCROOT}/FreeBSD/JoystickImpl.cpp
${SRCROOT}/FreeBSD/JoystickImpl.hpp
)
endif()
source_group("unix" FILES ${PLATFORM_SRC})
elseif(MACOSX)
set(PLATFORM_SRC set(PLATFORM_SRC
${SRCROOT}/OSX/cpp_objc_conversion.h ${SRCROOT}/OSX/cpp_objc_conversion.h
${SRCROOT}/OSX/cpp_objc_conversion.mm ${SRCROOT}/OSX/cpp_objc_conversion.mm
@ -126,6 +137,9 @@ if(WINDOWS)
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} winmm gdi32) set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} winmm gdi32)
elseif(LINUX) elseif(LINUX)
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} ${X11_X11_LIB} ${X11_Xrandr_LIB}) set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} ${X11_X11_LIB} ${X11_Xrandr_LIB})
if(FREEBSD)
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} usbhid)
endif()
elseif(MACOSX) elseif(MACOSX)
set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} "-framework Foundation -framework AppKit -framework IOKit -framework Carbon") set(WINDOW_EXT_LIBS ${WINDOW_EXT_LIBS} "-framework Foundation -framework AppKit -framework IOKit -framework Carbon")
endif() endif()

View File

@ -0,0 +1,380 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
// 2013-2013 David Demelier (demelier.david@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/JoystickImpl.hpp>
#include <SFML/System/Err.hpp>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <utility>
////////////////////////////////////////////////////////////
/// \brief This file implements FreeBSD driver joystick
///
/// It has been tested on a Saitek gamepad with 12 buttons,
/// 2 analog axis and one hat.
///
/// Note: old joy(4) drivers are not supported and no one use that
/// anymore.
////////////////////////////////////////////////////////////
namespace
{
std::map<unsigned int, std::string> plugged;
std::map<int, std::pair<int, int> > hatmap;
bool isJoystick(const char *name)
{
int fd, id;
bool ret;
report_desc_t desc = NULL;
hid_data_t data = NULL;
hid_item_t item;
// Assume it isn't
ret = false;
if ((fd = ::open(name, O_RDONLY | O_NONBLOCK)) < 0)
return false;
if ((desc = hid_get_report_desc(fd)) == NULL)
goto end;
id = hid_get_report_id(fd);
if ((data = hid_start_parse(desc, 1 << hid_input, id)) == NULL)
goto end;
while (hid_get_item(data, &item) > 0) {
switch (item.kind) {
case hid_collection:
switch (HID_PAGE(item.usage)) {
case HUP_GENERIC_DESKTOP:
switch (HID_USAGE(item.usage)) {
case HUG_JOYSTICK:
case HUG_GAME_PAD:
ret = true;
break;
default:
break;
}
default:
break;
}
default:
break;
}
}
end:
if (desc != NULL)
hid_dispose_report_desc(desc);
if (data != NULL)
hid_end_parse(data);
close(fd);
return ret;
}
void updatePluggedList()
{
DIR *dp;
struct dirent *entry;
/*
* Devices /dev/uhid<x> are shared between joystick and any other
* human interface device. We need to iterate over all found devices
* and check if they are joysticks. The index of JoystickImpl::open
* does not match the /dev/uhid<index> device!
*/
if ((dp = opendir("/dev")) != NULL) {
char name[FILENAME_MAX];
int jc = 0;
while ((entry = readdir(dp)) != NULL && jc < sf::Joystick::Count) {
if (strncmp(entry->d_name, "uhid", 4) == 0) {
std::sprintf(name, "/dev/%s", entry->d_name);
if (isJoystick(name))
plugged[jc++] = std::string(name);
}
}
closedir(dp);
}
}
int usageToAxis(int usage)
{
int axis;
switch (usage) {
case HUG_X:
axis = sf::Joystick::X;
break;
case HUG_Y:
axis = sf::Joystick::Y;
break;
case HUG_Z:
axis = sf::Joystick::Z;
break;
case HUG_RZ:
axis = sf::Joystick::R;
break;
case HUG_RX:
axis = sf::Joystick::U;
break;
case HUG_RY:
axis = sf::Joystick::V;
break;
default:
axis = -1;
break;
}
return axis;
}
void hatvalToSFML(int value, sf::priv::JoystickState &state)
{
state.axes[sf::Joystick::PovX] = hatmap[value].first;
state.axes[sf::Joystick::PovY] = hatmap[value].second;
}
}
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
void JoystickImpl::initialize()
{
hid_init(NULL);
// Do an initial scan
updatePluggedList();
// Map of hat values
hatmap[0] = std::make_pair<int, int>(0, 0); // center
hatmap[1] = std::make_pair<int, int>(0, -100); // top
hatmap[3] = std::make_pair<int, int>(100, 0); // right
hatmap[5] = std::make_pair<int, int>(0, 100); // bottom
hatmap[7] = std::make_pair<int, int>(-100, 0); // left
hatmap[2] = std::make_pair<int, int>(100, -100); // top-right
hatmap[4] = std::make_pair<int, int>(100, 100); // bottom-right
hatmap[6] = std::make_pair<int, int>(-100, 100); // bottom-left
hatmap[8] = std::make_pair<int, int>(-100, -100); // top-left
}
////////////////////////////////////////////////////////////
void JoystickImpl::cleanup()
{
}
////////////////////////////////////////////////////////////
bool JoystickImpl::isConnected(unsigned int index)
{
return plugged.find(index) != plugged.end();
}
////////////////////////////////////////////////////////////
bool JoystickImpl::open(unsigned int index)
{
if (isConnected(index))
{
// Open the joystick's file descriptor (read-only and non-blocking)
m_file = ::open(plugged[index].c_str(), O_RDONLY | O_NONBLOCK);
if (m_file >= 0)
{
// Reset the joystick state
m_state = JoystickState();
// Get the report descriptor
m_desc = hid_get_report_desc(m_file);
if (m_desc == NULL) {
::close(m_file);
return false;
}
// And the id
m_id = hid_get_report_id(m_file);
// Then allocate a buffer for data retrievement
m_length = hid_report_size(m_desc, hid_input, m_id);
m_buffer = std::calloc(1, m_length);
if (m_buffer == NULL) {
::close(m_file);
::hid_dispose_report_desc(m_desc);
return false;
}
return m_state.connected = true;
}
else
{
return false;
}
}
else
{
return false;
}
}
////////////////////////////////////////////////////////////
void JoystickImpl::close()
{
::close(m_file);
::hid_dispose_report_desc(m_desc);
::free(m_buffer);
}
////////////////////////////////////////////////////////////
JoystickCaps JoystickImpl::getCapabilities() const
{
JoystickCaps caps;
hid_data_t data;
hid_item_t item;
data = hid_start_parse(m_desc, 1 << hid_input, m_id);
while (hid_get_item(data, &item)) {
switch (item.kind) {
case hid_input:
switch (HID_PAGE(item.usage)) {
case HUP_BUTTON:
caps.buttonCount ++;
break;
case HUP_GENERIC_DESKTOP:
{
int usage = HID_USAGE(item.usage);
int axis;
if (usage == HUG_HAT_SWITCH) {
caps.axes[Joystick::PovX] = true;
caps.axes[Joystick::PovY] = true;
}
else if ((axis = usageToAxis(usage)) != -1)
{
caps.axes[axis] = true;
}
break;
}
default:
break;
}
default:
break;
}
}
hid_end_parse(data);
return caps;
}
////////////////////////////////////////////////////////////
JoystickState JoystickImpl::JoystickImpl::update()
{
while (read(m_file, m_buffer, m_length) == m_length) {
hid_data_t data;
hid_item_t item;
data = hid_start_parse(m_desc, 1 << hid_input, m_id);
// No memory?
if (data == NULL)
continue;
int but = 0;
while (hid_get_item(data, &item)) {
switch (item.kind) {
case hid_input:
switch (HID_PAGE(item.usage)) {
case HUP_BUTTON:
m_state.buttons[but++] = hid_get_data(m_buffer, &item);
break;
case HUP_GENERIC_DESKTOP:
{
int usage = HID_USAGE(item.usage);
int v = hid_get_data(m_buffer, &item);
int axis;
if (usage == HUG_HAT_SWITCH)
hatvalToSFML(v, m_state);
else if ((axis = usageToAxis(usage)) != -1)
{
int &min = item.logical_minimum;
int &max = item.logical_maximum;
v = (v - min) * (200) / (max - min) -100;
m_state.axes[axis] = v;
}
break;
}
default:
break;
}
default:
break;
}
}
hid_end_parse(data);
}
return m_state;
}
} // namespace priv
} // namespace sf
// vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:

View File

@ -0,0 +1,123 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_JOYSTICKIMPLFREEBSD_HPP
#define SFML_JOYSTICKIMPLFREEBSD_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <dev/usb/usbhid.h>
#include <usbhid.h>
namespace sf
{
namespace priv
{
////////////////////////////////////////////////////////////
/// \brief FreeBSD implementation of joysticks
///
/// This code has been tested on FreeBSD 9.1 only.
////////////////////////////////////////////////////////////
class JoystickImpl
{
public :
////////////////////////////////////////////////////////////
/// \brief Perform the global initialization of the joystick module
///
////////////////////////////////////////////////////////////
static void initialize();
////////////////////////////////////////////////////////////
/// \brief Perform the global cleanup of the joystick module
///
////////////////////////////////////////////////////////////
static void cleanup();
////////////////////////////////////////////////////////////
/// \brief Check if a joystick is currently connected
///
/// \param index Index of the joystick to check
///
/// \return True if the joystick is connected, false otherwise
///
////////////////////////////////////////////////////////////
static bool isConnected(unsigned int index);
////////////////////////////////////////////////////////////
/// \brief Open the joystick
///
/// \param index Index assigned to the joystick
///
/// \return True on success, false on failure
///
////////////////////////////////////////////////////////////
bool open(unsigned int index);
////////////////////////////////////////////////////////////
/// \brief Close the joystick
///
////////////////////////////////////////////////////////////
void close();
////////////////////////////////////////////////////////////
/// \brief Get the joystick capabilities
///
/// \return Joystick capabilities
///
////////////////////////////////////////////////////////////
JoystickCaps getCapabilities() const;
////////////////////////////////////////////////////////////
/// \brief Update the joystick and get its new state
///
/// \return Joystick state
///
////////////////////////////////////////////////////////////
JoystickState update();
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
int m_file; ///< File descriptor of the joystick
report_desc_t m_desc; ///< USB report descriptor
int m_id; ///< USB id
void *m_buffer; ///< USB HID buffer
int m_length; ///< Buffer length
JoystickState m_state; ///< Current state of the joystick
};
} // namespace priv
} // namespace sf
#endif // SFML_JOYSTICKIMPLFREEBSD_HPP

View File

@ -43,7 +43,7 @@
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD) #elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
#include <SFML/Window/Linux/GlxContext.hpp> #include <SFML/Window/Unix/GlxContext.hpp>
typedef sf::priv::GlxContext ContextType; typedef sf::priv::GlxContext ContextType;
#elif defined(SFML_SYSTEM_MACOS) #elif defined(SFML_SYSTEM_MACOS)

View File

@ -33,7 +33,7 @@
#if defined(SFML_SYSTEM_WINDOWS) #if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/Window/Win32/InputImpl.hpp> #include <SFML/Window/Win32/InputImpl.hpp>
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD) #elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
#include <SFML/Window/Linux/InputImpl.hpp> #include <SFML/Window/Unix/InputImpl.hpp>
#elif defined(SFML_SYSTEM_MACOS) #elif defined(SFML_SYSTEM_MACOS)
#include <SFML/Window/OSX/InputImpl.hpp> #include <SFML/Window/OSX/InputImpl.hpp>
#endif #endif

View File

@ -79,8 +79,10 @@ struct JoystickState
#if defined(SFML_SYSTEM_WINDOWS) #if defined(SFML_SYSTEM_WINDOWS)
#include <SFML/Window/Win32/JoystickImpl.hpp> #include <SFML/Window/Win32/JoystickImpl.hpp>
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD) #elif defined(SFML_SYSTEM_LINUX)
#include <SFML/Window/Linux/JoystickImpl.hpp> #include <SFML/Window/Unix/JoystickImpl.hpp>
#elif defined(SFML_SYSTEM_FREEBSD)
#include <SFML/Window/FreeBSD/JoystickImpl.hpp>
#elif defined(SFML_SYSTEM_MACOS) #elif defined(SFML_SYSTEM_MACOS)
#include <SFML/Window/OSX/JoystickImpl.hpp> #include <SFML/Window/OSX/JoystickImpl.hpp>
#endif #endif

View File

@ -25,7 +25,7 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/Linux/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <cassert> #include <cassert>

View File

@ -26,9 +26,9 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#define GLX_GLXEXT_LEGACY // so that our local glxext.h is used instead of the system one #define GLX_GLXEXT_LEGACY // so that our local glxext.h is used instead of the system one
#include <SFML/Window/Linux/GlxContext.hpp> #include <SFML/Window/Unix/GlxContext.hpp>
#include <SFML/Window/Linux/WindowImplX11.hpp> #include <SFML/Window/Unix/WindowImplX11.hpp>
#include <SFML/Window/Linux/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <SFML/OpenGL.hpp> #include <SFML/OpenGL.hpp>
#include <SFML/Window/glext/glxext.h> #include <SFML/Window/glext/glxext.h>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>

View File

@ -25,9 +25,9 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/Linux/InputImpl.hpp> #include <SFML/Window/Unix/InputImpl.hpp>
#include <SFML/Window/Window.hpp> #include <SFML/Window/Window.hpp>
#include <SFML/Window/Linux/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>

View File

@ -28,13 +28,8 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#if defined(SFML_SYSTEM_LINUX)
#include <linux/joystick.h> #include <linux/joystick.h>
#include <fcntl.h> #include <fcntl.h>
#elif defined(SFML_SYSTEM_FREEBSD)
// #include <sys/joystick.h> ?
#define ABS_MAX 1
#endif
namespace sf namespace sf

View File

@ -26,7 +26,7 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/VideoModeImpl.hpp> #include <SFML/Window/VideoModeImpl.hpp>
#include <SFML/Window/Linux/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h> #include <X11/extensions/Xrandr.h>

View File

@ -26,14 +26,15 @@
// Headers // Headers
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
#include <SFML/Window/WindowStyle.hpp> // important to be included first (conflict with None) #include <SFML/Window/WindowStyle.hpp> // important to be included first (conflict with None)
#include <SFML/Window/Linux/WindowImplX11.hpp> #include <SFML/Window/Unix/WindowImplX11.hpp>
#include <SFML/Window/Linux/GlxContext.hpp> #include <SFML/Window/Unix/GlxContext.hpp>
#include <SFML/Window/Linux/Display.hpp> #include <SFML/Window/Unix/Display.hpp>
#include <SFML/System/Utf.hpp> #include <SFML/System/Utf.hpp>
#include <SFML/System/Err.hpp> #include <SFML/System/Err.hpp>
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/extensions/Xrandr.h> #include <X11/extensions/Xrandr.h>
#include <libgen.h>
#include <unistd.h> #include <unistd.h>
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>

View File

@ -39,7 +39,7 @@
#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD) #elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD)
#include <SFML/Window/Linux/WindowImplX11.hpp> #include <SFML/Window/Unix/WindowImplX11.hpp>
typedef sf::priv::WindowImplX11 WindowImplType; typedef sf::priv::WindowImplX11 WindowImplType;
#elif defined(SFML_SYSTEM_MACOS) #elif defined(SFML_SYSTEM_MACOS)