mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 21:43:03 +02:00
Update to SDL2
This commit is contained in:
parent
f5ee55bef4
commit
762cc71d31
6 changed files with 436 additions and 114 deletions
249
cmake/modules/FindSDL2.cmake
Normal file
249
cmake/modules/FindSDL2.cmake
Normal file
|
@ -0,0 +1,249 @@
|
||||||
|
# Locate SDL2 library
|
||||||
|
# This module defines
|
||||||
|
# SDL2_LIBRARY, the name of the library to link against
|
||||||
|
# SDL2_FOUND, if false, do not try to link to SDL2
|
||||||
|
# SDL2_INCLUDE_DIR, where to find SDL.h
|
||||||
|
#
|
||||||
|
# This module responds to the the flag:
|
||||||
|
# SDL2_BUILDING_LIBRARY
|
||||||
|
# If this is defined, then no SDL2_main will be linked in because
|
||||||
|
# only applications need main().
|
||||||
|
# Otherwise, it is assumed you are building an application and this
|
||||||
|
# module will attempt to locate and set the the proper link flags
|
||||||
|
# as part of the returned SDL2_LIBRARY variable.
|
||||||
|
#
|
||||||
|
# Don't forget to include SDL2main.h and SDL2main.m your project for the
|
||||||
|
# OS X framework based version. (Other versions link to -lSDL2main which
|
||||||
|
# this module will try to find on your behalf.) Also for OS X, this
|
||||||
|
# module will automatically add the -framework Cocoa on your behalf.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
|
||||||
|
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
|
||||||
|
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
|
||||||
|
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
|
||||||
|
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
|
||||||
|
# as appropriate. These values are used to generate the final SDL2_LIBRARY
|
||||||
|
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# $SDL2 is an environment variable that would
|
||||||
|
# correspond to the ./configure --prefix=$SDL2
|
||||||
|
# used in building SDL2.
|
||||||
|
# l.e.galup 9-20-02
|
||||||
|
#
|
||||||
|
# Modified by Eric Wing.
|
||||||
|
# Added code to assist with automated building by using environmental variables
|
||||||
|
# and providing a more controlled/consistent search behavior.
|
||||||
|
# Added new modifications to recognize OS X frameworks and
|
||||||
|
# additional Unix paths (FreeBSD, etc).
|
||||||
|
# Also corrected the header search path to follow "proper" SDL2 guidelines.
|
||||||
|
# Added a search for SDL2main which is needed by some platforms.
|
||||||
|
# Added a search for threads which is needed by some platforms.
|
||||||
|
# Added needed compile switches for MinGW.
|
||||||
|
#
|
||||||
|
# On OSX, this will prefer the Framework version (if found) over others.
|
||||||
|
# People will have to manually change the cache values of
|
||||||
|
# SDL2_LIBRARY to override this selection or set the CMake environment
|
||||||
|
# CMAKE_INCLUDE_PATH to modify the search paths.
|
||||||
|
#
|
||||||
|
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
|
||||||
|
# This needed to change because "proper" SDL2 convention
|
||||||
|
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
|
||||||
|
# reasons because not all systems place things in SDL2/ (see FreeBSD).
|
||||||
|
#
|
||||||
|
# Ported by Johnny Patterson. This is a literal port for SDL2 of the FindSDL.cmake
|
||||||
|
# module with the minor edit of changing "SDL" to "SDL2" where necessary. This
|
||||||
|
# was not created for redistribution, and exists temporarily pending official
|
||||||
|
# SDL2 CMake modules.
|
||||||
|
#
|
||||||
|
# Note that on windows this will only search for the 32bit libraries, to search
|
||||||
|
# for 64bit change x86/i686-w64 to x64/x86_64-w64
|
||||||
|
|
||||||
|
#=============================================================================
|
||||||
|
# Copyright 2003-2009 Kitware, Inc.
|
||||||
|
#
|
||||||
|
# CMake - Cross Platform Makefile Generator
|
||||||
|
# Copyright 2000-2014 Kitware, Inc.
|
||||||
|
# Copyright 2000-2011 Insight Software Consortium
|
||||||
|
# All rights reserved.
|
||||||
|
#
|
||||||
|
# Redistribution and use in source and binary forms, with or without
|
||||||
|
# modification, are permitted provided that the following conditions
|
||||||
|
# are met:
|
||||||
|
#
|
||||||
|
# * Redistributions of source code must retain the above copyright
|
||||||
|
# notice, this list of conditions and the following disclaimer.
|
||||||
|
#
|
||||||
|
# * Redistributions in binary form must reproduce the above copyright
|
||||||
|
# notice, this list of conditions and the following disclaimer in the
|
||||||
|
# documentation and/or other materials provided with the distribution.
|
||||||
|
#
|
||||||
|
# * Neither the names of Kitware, Inc., the Insight Software Consortium,
|
||||||
|
# nor the names of their contributors may be used to endorse or promote
|
||||||
|
# products derived from this software without specific prior written
|
||||||
|
# permission.
|
||||||
|
#
|
||||||
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
#
|
||||||
|
# This software is distributed WITHOUT ANY WARRANTY; without even the
|
||||||
|
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the License for more information.
|
||||||
|
#=============================================================================
|
||||||
|
# (To distribute this file outside of CMake, substitute the full
|
||||||
|
# License text for the above reference.)
|
||||||
|
|
||||||
|
FIND_PATH(SDL2_INCLUDE_DIR SDL.h
|
||||||
|
HINTS
|
||||||
|
$ENV{SDL2}
|
||||||
|
PATH_SUFFIXES include/SDL2 include SDL2
|
||||||
|
i686-w64-mingw32/include/SDL2
|
||||||
|
x86_64-w64-mingw32/include/SDL2
|
||||||
|
PATHS
|
||||||
|
~/Library/Frameworks
|
||||||
|
/Library/Frameworks
|
||||||
|
/usr/local/include/SDL2
|
||||||
|
/usr/include/SDL2
|
||||||
|
/sw # Fink
|
||||||
|
/opt/local # DarwinPorts
|
||||||
|
/opt/csw # Blastwave
|
||||||
|
/opt
|
||||||
|
)
|
||||||
|
|
||||||
|
# Lookup the 64 bit libs on x64
|
||||||
|
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||||
|
FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2
|
||||||
|
HINTS
|
||||||
|
$ENV{SDL2}
|
||||||
|
PATH_SUFFIXES lib64 lib
|
||||||
|
lib/x64
|
||||||
|
x86_64-w64-mingw32/lib
|
||||||
|
PATHS
|
||||||
|
/sw
|
||||||
|
/opt/local
|
||||||
|
/opt/csw
|
||||||
|
/opt
|
||||||
|
)
|
||||||
|
# On 32bit build find the 32bit libs
|
||||||
|
ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||||
|
FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2
|
||||||
|
HINTS
|
||||||
|
$ENV{SDL2}
|
||||||
|
PATH_SUFFIXES lib
|
||||||
|
lib/x86
|
||||||
|
i686-w64-mingw32/lib
|
||||||
|
PATHS
|
||||||
|
/sw
|
||||||
|
/opt/local
|
||||||
|
/opt/csw
|
||||||
|
/opt
|
||||||
|
)
|
||||||
|
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||||
|
|
||||||
|
IF(NOT SDL2_BUILDING_LIBRARY)
|
||||||
|
IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
|
||||||
|
# Non-OS X framework versions expect you to also dynamically link to
|
||||||
|
# SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
|
||||||
|
# seem to provide SDL2main for compatibility even though they don't
|
||||||
|
# necessarily need it.
|
||||||
|
# Lookup the 64 bit libs on x64
|
||||||
|
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||||
|
FIND_LIBRARY(SDL2MAIN_LIBRARY
|
||||||
|
NAMES SDL2main
|
||||||
|
HINTS
|
||||||
|
$ENV{SDL2}
|
||||||
|
PATH_SUFFIXES lib64 lib
|
||||||
|
lib/x64
|
||||||
|
x86_64-w64-mingw32/lib
|
||||||
|
PATHS
|
||||||
|
/sw
|
||||||
|
/opt/local
|
||||||
|
/opt/csw
|
||||||
|
/opt
|
||||||
|
)
|
||||||
|
# On 32bit build find the 32bit libs
|
||||||
|
ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||||
|
FIND_LIBRARY(SDL2MAIN_LIBRARY
|
||||||
|
NAMES SDL2main
|
||||||
|
HINTS
|
||||||
|
$ENV{SDL2}
|
||||||
|
PATH_SUFFIXES lib
|
||||||
|
lib/x86
|
||||||
|
i686-w64-mingw32/lib
|
||||||
|
PATHS
|
||||||
|
/sw
|
||||||
|
/opt/local
|
||||||
|
/opt/csw
|
||||||
|
/opt
|
||||||
|
)
|
||||||
|
ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||||
|
ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
|
||||||
|
ENDIF(NOT SDL2_BUILDING_LIBRARY)
|
||||||
|
|
||||||
|
# SDL2 may require threads on your system.
|
||||||
|
# The Apple build may not need an explicit flag because one of the
|
||||||
|
# frameworks may already provide it.
|
||||||
|
# But for non-OSX systems, I will use the CMake Threads package.
|
||||||
|
IF(NOT APPLE)
|
||||||
|
FIND_PACKAGE(Threads)
|
||||||
|
ENDIF(NOT APPLE)
|
||||||
|
|
||||||
|
# MinGW needs an additional library, mwindows
|
||||||
|
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
|
||||||
|
# (Actually on second look, I think it only needs one of the m* libraries.)
|
||||||
|
IF(MINGW)
|
||||||
|
SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
|
||||||
|
ENDIF(MINGW)
|
||||||
|
|
||||||
|
SET(SDL2_FOUND "NO")
|
||||||
|
IF(SDL2_LIBRARY_TEMP)
|
||||||
|
# For SDL2main
|
||||||
|
IF(NOT SDL2_BUILDING_LIBRARY)
|
||||||
|
IF(SDL2MAIN_LIBRARY)
|
||||||
|
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
|
||||||
|
ENDIF(SDL2MAIN_LIBRARY)
|
||||||
|
ENDIF(NOT SDL2_BUILDING_LIBRARY)
|
||||||
|
|
||||||
|
# For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
|
||||||
|
# CMake doesn't display the -framework Cocoa string in the UI even
|
||||||
|
# though it actually is there if I modify a pre-used variable.
|
||||||
|
# I think it has something to do with the CACHE STRING.
|
||||||
|
# So I use a temporary variable until the end so I can set the
|
||||||
|
# "real" variable in one-shot.
|
||||||
|
IF(APPLE)
|
||||||
|
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
|
||||||
|
ENDIF(APPLE)
|
||||||
|
|
||||||
|
# For threads, as mentioned Apple doesn't need this.
|
||||||
|
# In fact, there seems to be a problem if I used the Threads package
|
||||||
|
# and try using this line, so I'm just skipping it entirely for OS X.
|
||||||
|
IF(NOT APPLE)
|
||||||
|
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
|
||||||
|
ENDIF(NOT APPLE)
|
||||||
|
|
||||||
|
# For MinGW library
|
||||||
|
IF(MINGW)
|
||||||
|
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
|
||||||
|
ENDIF(MINGW)
|
||||||
|
|
||||||
|
# Set the final string here so the GUI reflects the final state.
|
||||||
|
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
|
||||||
|
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
|
||||||
|
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
|
||||||
|
|
||||||
|
SET(SDL2_FOUND "YES")
|
||||||
|
ENDIF(SDL2_LIBRARY_TEMP)
|
||||||
|
|
||||||
|
INCLUDE(FindPackageHandleStandardArgs)
|
||||||
|
|
||||||
|
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
|
|
@ -2,7 +2,7 @@ set(TARGET_NAME interface)
|
||||||
project(${TARGET_NAME})
|
project(${TARGET_NAME})
|
||||||
|
|
||||||
# set a default root dir for each of our optional externals if it was not passed
|
# set a default root dir for each of our optional externals if it was not passed
|
||||||
set(OPTIONAL_EXTERNALS "Faceplus" "Faceshift" "LibOVR" "PrioVR" "Sixense" "Visage" "LeapMotion" "RtMidi" "Qxmpp" "SDL")
|
set(OPTIONAL_EXTERNALS "Faceplus" "Faceshift" "LibOVR" "PrioVR" "Sixense" "Visage" "LeapMotion" "RtMidi" "Qxmpp" "SDL2")
|
||||||
foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
|
foreach(EXTERNAL ${OPTIONAL_EXTERNALS})
|
||||||
string(TOUPPER ${EXTERNAL} ${EXTERNAL}_UPPERCASE)
|
string(TOUPPER ${EXTERNAL} ${EXTERNAL}_UPPERCASE)
|
||||||
if (NOT ${${EXTERNAL}_UPPERCASE}_ROOT_DIR)
|
if (NOT ${${EXTERNAL}_UPPERCASE}_ROOT_DIR)
|
||||||
|
|
|
@ -15,13 +15,17 @@
|
||||||
|
|
||||||
#include "Joystick.h"
|
#include "Joystick.h"
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
const float MAX_AXIS = 32767.0f;
|
||||||
|
|
||||||
Joystick::Joystick(const QString& name, SDL_Joystick* sdlJoystick) :
|
#ifdef HAVE_SDL2
|
||||||
|
|
||||||
|
Joystick::Joystick(SDL_JoystickID instanceId, const QString& name, SDL_GameController* sdlGameController) :
|
||||||
|
_instanceId(instanceId),
|
||||||
_name(name),
|
_name(name),
|
||||||
_axes(QVector<float>(SDL_JoystickNumAxes(sdlJoystick))),
|
_sdlGameController(sdlGameController),
|
||||||
_buttons(QVector<bool>(SDL_JoystickNumButtons(sdlJoystick))),
|
_sdlJoystick(SDL_GameControllerGetJoystick(_sdlGameController)),
|
||||||
_sdlJoystick(sdlJoystick)
|
_axes(QVector<float>(SDL_JoystickNumAxes(_sdlJoystick))),
|
||||||
|
_buttons(QVector<bool>(SDL_JoystickNumButtons(_sdlJoystick)))
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,30 +37,24 @@ Joystick::~Joystick() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::closeJoystick() {
|
void Joystick::closeJoystick() {
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL2
|
||||||
SDL_JoystickClose(_sdlJoystick);
|
SDL_GameControllerClose(_sdlGameController);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Joystick::update() {
|
|
||||||
#ifdef HAVE_SDL
|
|
||||||
// update our current values, emit a signal when there is a change
|
|
||||||
for (int j = 0; j < getNumAxes(); j++) {
|
|
||||||
float newValue = glm::round(SDL_JoystickGetAxis(_sdlJoystick, j) + 0.5f) / std::numeric_limits<short>::max();
|
|
||||||
|
|
||||||
if (_axes[j] != newValue) {
|
#ifdef HAVE_SDL2
|
||||||
float oldValue = _axes[j];
|
void Joystick::handleAxisEvent(const SDL_ControllerAxisEvent& event) {
|
||||||
_axes[j] = newValue;
|
float oldValue = _axes[event.axis];
|
||||||
emit axisValueChanged(j, newValue, oldValue);
|
float newValue = event.value / MAX_AXIS;
|
||||||
}
|
_axes[event.axis] = newValue;
|
||||||
}
|
emit axisValueChanged(event.axis, newValue, oldValue);
|
||||||
for (int j = 0; j < getNumButtons(); j++) {
|
}
|
||||||
bool newValue = SDL_JoystickGetButton(_sdlJoystick, j);
|
|
||||||
if (_buttons[j] != newValue) {
|
void Joystick::handleButtonEvent(const SDL_ControllerButtonEvent& event) {
|
||||||
bool oldValue = _buttons[j];
|
bool oldValue = _buttons[event.button];
|
||||||
_buttons[j] = newValue;
|
bool newValue = event.state == SDL_PRESSED;
|
||||||
emit buttonStateChanged(j, newValue, oldValue);
|
_buttons[event.button] = newValue;
|
||||||
}
|
emit buttonStateChanged(event.button, newValue, oldValue);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
|
|
@ -15,7 +15,7 @@
|
||||||
#include <qobject.h>
|
#include <qobject.h>
|
||||||
#include <qvector.h>
|
#include <qvector.h>
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL2
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#undef main
|
#undef main
|
||||||
#endif
|
#endif
|
||||||
|
@ -24,6 +24,10 @@ class Joystick : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(QString name READ getName)
|
Q_PROPERTY(QString name READ getName)
|
||||||
|
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
|
Q_PROPERTY(int instanceId READ getInstanceId)
|
||||||
|
#endif
|
||||||
|
|
||||||
Q_PROPERTY(int numAxes READ getNumAxes)
|
Q_PROPERTY(int numAxes READ getNumAxes)
|
||||||
Q_PROPERTY(int numButtons READ getNumButtons)
|
Q_PROPERTY(int numButtons READ getNumButtons)
|
||||||
|
@ -31,19 +35,21 @@ public:
|
||||||
Joystick();
|
Joystick();
|
||||||
~Joystick();
|
~Joystick();
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL2
|
||||||
Joystick(const QString& name, SDL_Joystick* sdlJoystick);
|
Joystick(SDL_JoystickID instanceId, const QString& name, SDL_GameController* sdlGameController);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void update();
|
|
||||||
|
|
||||||
void closeJoystick();
|
void closeJoystick();
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL2
|
||||||
void setSDLJoystick(SDL_Joystick* sdlJoystick) { _sdlJoystick = sdlJoystick; }
|
void handleAxisEvent(const SDL_ControllerAxisEvent& event);
|
||||||
|
void handleButtonEvent(const SDL_ControllerButtonEvent& event);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const QString& getName() const { return _name; }
|
const QString& getName() const { return _name; }
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
|
int getInstanceId() const { return _instanceId; }
|
||||||
|
#endif
|
||||||
|
|
||||||
const QVector<float>& getAxes() const { return _axes; }
|
const QVector<float>& getAxes() const { return _axes; }
|
||||||
const QVector<bool>& getButtons() const { return _buttons; }
|
const QVector<bool>& getButtons() const { return _buttons; }
|
||||||
|
@ -55,13 +61,15 @@ signals:
|
||||||
void axisValueChanged(int axis, float newValue, float oldValue);
|
void axisValueChanged(int axis, float newValue, float oldValue);
|
||||||
void buttonStateChanged(int button, float newValue, float oldValue);
|
void buttonStateChanged(int button, float newValue, float oldValue);
|
||||||
private:
|
private:
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
|
SDL_GameController* _sdlGameController;
|
||||||
|
SDL_Joystick* _sdlJoystick;
|
||||||
|
SDL_JoystickID _instanceId;
|
||||||
|
#endif
|
||||||
|
|
||||||
QString _name;
|
QString _name;
|
||||||
QVector<float> _axes;
|
QVector<float> _axes;
|
||||||
QVector<bool> _buttons;
|
QVector<bool> _buttons;
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
|
||||||
SDL_Joystick* _sdlJoystick;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_JoystickTracker_h
|
#endif // hifi_JoystickTracker_h
|
||||||
|
|
|
@ -10,8 +10,9 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
|
#include <QScriptValue>
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL2
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#undef main
|
#undef main
|
||||||
#endif
|
#endif
|
||||||
|
@ -20,104 +21,103 @@
|
||||||
|
|
||||||
#include "JoystickScriptingInterface.h"
|
#include "JoystickScriptingInterface.h"
|
||||||
|
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
|
SDL_JoystickID getInstanceId(SDL_GameController* controller) {
|
||||||
|
SDL_Joystick* joystick = SDL_GameControllerGetJoystick(controller);
|
||||||
|
return SDL_JoystickInstanceID(joystick);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
JoystickScriptingInterface& JoystickScriptingInterface::getInstance() {
|
JoystickScriptingInterface& JoystickScriptingInterface::getInstance() {
|
||||||
static JoystickScriptingInterface sharedInstance;
|
static JoystickScriptingInterface sharedInstance;
|
||||||
return sharedInstance;
|
return sharedInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
JoystickScriptingInterface::JoystickScriptingInterface() :
|
JoystickScriptingInterface::JoystickScriptingInterface() :
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
_openJoysticks(),
|
_openJoysticks(),
|
||||||
_availableDeviceNames(),
|
#endif
|
||||||
_isInitialized(false)
|
_isInitialized(false)
|
||||||
{
|
{
|
||||||
reset();
|
#ifdef HAVE_SDL2
|
||||||
|
bool initSuccess = (SDL_Init(SDL_INIT_GAMECONTROLLER) == 0);
|
||||||
|
|
||||||
|
if (initSuccess) {
|
||||||
|
int joystickCount = SDL_NumJoysticks();
|
||||||
|
|
||||||
|
for (int i = 0; i < joystickCount; i++) {
|
||||||
|
SDL_GameController* controller = SDL_GameControllerOpen(i);
|
||||||
|
SDL_JoystickID id = getInstanceId(controller);
|
||||||
|
Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
|
||||||
|
_openJoysticks[id] = joystick;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isInitialized = true;
|
||||||
|
} else {
|
||||||
|
qDebug() << "Error initializing SDL";
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
JoystickScriptingInterface::~JoystickScriptingInterface() {
|
JoystickScriptingInterface::~JoystickScriptingInterface() {
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
qDeleteAll(_openJoysticks);
|
qDeleteAll(_openJoysticks);
|
||||||
|
|
||||||
#ifdef HAVE_SDL
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
_isInitialized = false;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void JoystickScriptingInterface::reset() {
|
const QObjectList JoystickScriptingInterface::getAllJoysticks() const {
|
||||||
#ifdef HAVE_SDL
|
QObjectList objectList;
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
if (_isInitialized) {
|
const QList<Joystick*> joystickList = _openJoysticks.values();
|
||||||
_isInitialized = false;
|
for (int i = 0; i < joystickList.length(); i++) {
|
||||||
|
objectList << joystickList[i];
|
||||||
// close all the open joysticks before we quit
|
|
||||||
foreach(Joystick* openJoystick, _openJoysticks) {
|
|
||||||
openJoystick->closeJoystick();
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool initSuccess = (SDL_Init(SDL_INIT_JOYSTICK) == 0);
|
|
||||||
|
|
||||||
if (initSuccess) {
|
|
||||||
|
|
||||||
int joystickCount = SDL_NumJoysticks();
|
|
||||||
|
|
||||||
for (int i = 0; i < joystickCount; i++) {
|
|
||||||
_availableDeviceNames << SDL_JoystickName(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach(const QString& joystickName, _openJoysticks.keys()) {
|
|
||||||
_openJoysticks[joystickName]->setSDLJoystick(openSDLJoystickWithName(joystickName));
|
|
||||||
}
|
|
||||||
|
|
||||||
_isInitialized = true;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
return objectList;
|
||||||
}
|
}
|
||||||
|
|
||||||
void JoystickScriptingInterface::update() {
|
void JoystickScriptingInterface::update() {
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL2
|
||||||
if (_isInitialized) {
|
if (_isInitialized) {
|
||||||
PerformanceTimer perfTimer("JoystickScriptingInterface::update");
|
PerformanceTimer perfTimer("JoystickScriptingInterface::update");
|
||||||
SDL_JoystickUpdate();
|
SDL_GameControllerUpdate();
|
||||||
|
SDL_Event event;
|
||||||
foreach(Joystick* joystick, _openJoysticks) {
|
while (SDL_PollEvent(&event)) {
|
||||||
joystick->update();
|
if (event.type == SDL_CONTROLLERAXISMOTION) {
|
||||||
|
Joystick* joystick = _openJoysticks[event.caxis.which];
|
||||||
|
if (joystick) {
|
||||||
|
joystick->handleAxisEvent(event.caxis);
|
||||||
|
}
|
||||||
|
} else if (event.type == SDL_CONTROLLERBUTTONDOWN || event.type == SDL_CONTROLLERBUTTONUP) {
|
||||||
|
Joystick* joystick = _openJoysticks[event.cbutton.which];
|
||||||
|
if (joystick) {
|
||||||
|
joystick->handleButtonEvent(event.cbutton);
|
||||||
|
}
|
||||||
|
} else if (event.type == SDL_CONTROLLERDEVICEADDED) {
|
||||||
|
SDL_GameController* controller = SDL_GameControllerOpen(event.cdevice.which);
|
||||||
|
|
||||||
|
SDL_JoystickID id = getInstanceId(controller);
|
||||||
|
Joystick* joystick = new Joystick(id, SDL_GameControllerName(controller), controller);
|
||||||
|
_openJoysticks[id] = joystick;
|
||||||
|
emit joystickAdded(joystick);
|
||||||
|
} else if (event.type == SDL_CONTROLLERDEVICEREMOVED) {
|
||||||
|
Joystick* joystick = _openJoysticks[event.cdevice.which];
|
||||||
|
_openJoysticks.remove(event.cdevice.which);
|
||||||
|
emit joystickRemoved(joystick);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) {
|
#ifdef HAVE_SDL2
|
||||||
Joystick* matchingJoystick = _openJoysticks.value(name);
|
|
||||||
#ifdef HAVE_SDL
|
|
||||||
if (!matchingJoystick) {
|
|
||||||
SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name);
|
|
||||||
|
|
||||||
if (openSDLJoystick) {
|
|
||||||
matchingJoystick = _openJoysticks.insert(name, new Joystick(name, openSDLJoystick)).value();
|
|
||||||
} else {
|
|
||||||
qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer.";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return matchingJoystick;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString& name) {
|
||||||
#ifdef HAVE_SDL
|
|
||||||
|
|
||||||
SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString &name) {
|
|
||||||
// we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists
|
// we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists
|
||||||
int joystickCount = SDL_NumJoysticks();
|
int joystickCount = SDL_NumJoysticks();
|
||||||
|
|
||||||
for (int i = 0; i < joystickCount; i++) {
|
|
||||||
if (SDL_JoystickName(i) == name) {
|
|
||||||
return SDL_JoystickOpen(i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,34 +15,101 @@
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
|
#ifdef HAVE_SDL2
|
||||||
|
#include <SDL.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "devices/Joystick.h"
|
#include "devices/Joystick.h"
|
||||||
|
|
||||||
/// Handles joystick input through SDL.
|
/// Handles joystick input through SDL.
|
||||||
class JoystickScriptingInterface : public QObject {
|
class JoystickScriptingInterface : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(QStringList availableJoystickNames READ getAvailableJoystickNames)
|
#ifdef HAVE_SDL2
|
||||||
|
Q_PROPERTY(int AXIS_INVALID READ axisInvalid)
|
||||||
|
Q_PROPERTY(int AXIS_LEFT_X READ axisLeftX)
|
||||||
|
Q_PROPERTY(int AXIS_LEFT_Y READ axisLeftY)
|
||||||
|
Q_PROPERTY(int AXIS_RIGHT_X READ axisRightX)
|
||||||
|
Q_PROPERTY(int AXIS_RIGHT_Y READ axisRightY)
|
||||||
|
Q_PROPERTY(int AXIS_TRIGGER_LEFT READ axisTriggerLeft)
|
||||||
|
Q_PROPERTY(int AXIS_TRIGGER_RIGHT READ axisTriggerRight)
|
||||||
|
Q_PROPERTY(int AXIS_MAX READ axisMax)
|
||||||
|
|
||||||
|
Q_PROPERTY(int BUTTON_INVALID READ buttonInvalid)
|
||||||
|
Q_PROPERTY(int BUTTON_FACE_BOTTOM READ buttonFaceBottom)
|
||||||
|
Q_PROPERTY(int BUTTON_FACE_RIGHT READ buttonFaceRight)
|
||||||
|
Q_PROPERTY(int BUTTON_FACE_LEFT READ buttonFaceLeft)
|
||||||
|
Q_PROPERTY(int BUTTON_FACE_TOP READ buttonFaceTop)
|
||||||
|
Q_PROPERTY(int BUTTON_BACK READ buttonBack)
|
||||||
|
Q_PROPERTY(int BUTTON_GUIDE READ buttonGuide)
|
||||||
|
Q_PROPERTY(int BUTTON_START READ buttonStart)
|
||||||
|
Q_PROPERTY(int BUTTON_LEFT_STICK READ buttonLeftStick)
|
||||||
|
Q_PROPERTY(int BUTTON_RIGHT_STICK READ buttonRightStick)
|
||||||
|
Q_PROPERTY(int BUTTON_LEFT_SHOULDER READ buttonLeftShoulder)
|
||||||
|
Q_PROPERTY(int BUTTON_RIGHT_SHOULDER READ buttonRightShoulder)
|
||||||
|
Q_PROPERTY(int BUTTON_DPAD_UP READ buttonDpadUp)
|
||||||
|
Q_PROPERTY(int BUTTON_DPAD_DOWN READ buttonDpadDown)
|
||||||
|
Q_PROPERTY(int BUTTON_DPAD_LEFT READ buttonDpadLeft)
|
||||||
|
Q_PROPERTY(int BUTTON_DPAD_RIGHT READ buttonDpadRight)
|
||||||
|
Q_PROPERTY(int BUTTON_MAX READ buttonMax)
|
||||||
|
|
||||||
|
Q_PROPERTY(int BUTTON_PRESSED READ buttonPressed)
|
||||||
|
Q_PROPERTY(int BUTTON_RELEASED READ buttonRelease)
|
||||||
|
#endif
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static JoystickScriptingInterface& getInstance();
|
static JoystickScriptingInterface& getInstance();
|
||||||
|
|
||||||
const QStringList& getAvailableJoystickNames() const { return _availableDeviceNames; }
|
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
Joystick* joystickWithName(const QString& name);
|
const QObjectList getAllJoysticks() const;
|
||||||
void reset();
|
|
||||||
|
signals:
|
||||||
|
void joystickAdded(Joystick* joystick);
|
||||||
|
void joystickRemoved(Joystick* joystick);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#ifdef HAVE_SDL
|
#ifdef HAVE_SDL2
|
||||||
|
int axisInvalid() const { return SDL_CONTROLLER_AXIS_INVALID; }
|
||||||
|
int axisLeftX() const { return SDL_CONTROLLER_AXIS_LEFTX; }
|
||||||
|
int axisLeftY() const { return SDL_CONTROLLER_AXIS_LEFTY; }
|
||||||
|
int axisRightX() const { return SDL_CONTROLLER_AXIS_RIGHTX; }
|
||||||
|
int axisRightY() const { return SDL_CONTROLLER_AXIS_RIGHTY; }
|
||||||
|
int axisTriggerLeft() const { return SDL_CONTROLLER_AXIS_TRIGGERLEFT; }
|
||||||
|
int axisTriggerRight() const { return SDL_CONTROLLER_AXIS_TRIGGERRIGHT; }
|
||||||
|
int axisMax() const { return SDL_CONTROLLER_AXIS_MAX; }
|
||||||
|
|
||||||
|
int buttonInvalid() const { return SDL_CONTROLLER_BUTTON_INVALID; }
|
||||||
|
int buttonFaceBottom() const { return SDL_CONTROLLER_BUTTON_A; }
|
||||||
|
int buttonFaceRight() const { return SDL_CONTROLLER_BUTTON_B; }
|
||||||
|
int buttonFaceLeft() const { return SDL_CONTROLLER_BUTTON_X; }
|
||||||
|
int buttonFaceTop() const { return SDL_CONTROLLER_BUTTON_Y; }
|
||||||
|
int buttonBack() const { return SDL_CONTROLLER_BUTTON_BACK; }
|
||||||
|
int buttonGuide() const { return SDL_CONTROLLER_BUTTON_GUIDE; }
|
||||||
|
int buttonStart() const { return SDL_CONTROLLER_BUTTON_START; }
|
||||||
|
int buttonLeftStick() const { return SDL_CONTROLLER_BUTTON_LEFTSTICK; }
|
||||||
|
int buttonRightStick() const { return SDL_CONTROLLER_BUTTON_RIGHTSTICK; }
|
||||||
|
int buttonLeftShoulder() const { return SDL_CONTROLLER_BUTTON_LEFTSHOULDER; }
|
||||||
|
int buttonRightShoulder() const { return SDL_CONTROLLER_BUTTON_RIGHTSHOULDER; }
|
||||||
|
int buttonDpadUp() const { return SDL_CONTROLLER_BUTTON_DPAD_UP; }
|
||||||
|
int buttonDpadDown() const { return SDL_CONTROLLER_BUTTON_DPAD_DOWN; }
|
||||||
|
int buttonDpadLeft() const { return SDL_CONTROLLER_BUTTON_DPAD_LEFT; }
|
||||||
|
int buttonDpadRight() const { return SDL_CONTROLLER_BUTTON_DPAD_RIGHT; }
|
||||||
|
int buttonMax() const { return SDL_CONTROLLER_BUTTON_MAX; }
|
||||||
|
|
||||||
|
int buttonPressed() const { return SDL_PRESSED; }
|
||||||
|
int buttonRelease() const { return SDL_RELEASED; }
|
||||||
|
|
||||||
SDL_Joystick* openSDLJoystickWithName(const QString& name);
|
SDL_Joystick* openSDLJoystickWithName(const QString& name);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
JoystickScriptingInterface();
|
JoystickScriptingInterface();
|
||||||
~JoystickScriptingInterface();
|
~JoystickScriptingInterface();
|
||||||
|
|
||||||
QMap<QString, Joystick*> _openJoysticks;
|
#ifdef HAVE_SDL2
|
||||||
QStringList _availableDeviceNames;
|
QMap<SDL_JoystickID, Joystick*> _openJoysticks;
|
||||||
|
#endif
|
||||||
bool _isInitialized;
|
bool _isInitialized;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue