Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Valentin Platzgummer
qgroundcontrol
Commits
e67514bc
Commit
e67514bc
authored
Aug 20, 2016
by
Rustom Jehangir
Browse files
SDL2: Load game controller mappings and utilize game controller if available
parent
50a0e516
Changes
3
Hide whitespace changes
Inline
Side-by-side
qgcresources.qrc
View file @
e67514bc
...
...
@@ -247,6 +247,9 @@
<file alias="joystickThrottleUp.png">resources/calibration/joystick/joystickThrottleUp.png</file>
<file alias="joystickThrottleDown.png">resources/calibration/joystick/joystickThrottleDown.png</file>
</qresource>
<qresource prefix="/db/mapping/joystick">
<file alias="gamecontrollerdb.txt">resources/SDL_GameControllerDB/gamecontrollerdb.txt</file>
</qresource>
<qresource prefix="/res/styles">
<file alias="style-dark.css">resources/styles/style-dark.css</file>
<file alias="style-light.css">resources/styles/style-light.css</file>
...
...
src/Joystick/JoystickSDL.cc
View file @
e67514bc
...
...
@@ -3,9 +3,11 @@
#include
"QGCApplication.h"
#include
<QQmlEngine>
#include
<QTextStream>
JoystickSDL
::
JoystickSDL
(
const
QString
&
name
,
int
axisCount
,
int
buttonCount
,
int
hatCount
,
int
index
,
MultiVehicleManager
*
multiVehicleManager
)
JoystickSDL
::
JoystickSDL
(
const
QString
&
name
,
int
axisCount
,
int
buttonCount
,
int
hatCount
,
int
index
,
bool
isGameController
,
MultiVehicleManager
*
multiVehicleManager
)
:
Joystick
(
name
,
axisCount
,
buttonCount
,
hatCount
,
multiVehicleManager
)
,
_isGameController
(
isGameController
)
,
_index
(
index
)
{
}
...
...
@@ -13,11 +15,13 @@ JoystickSDL::JoystickSDL(const QString& name, int axisCount, int buttonCount, in
QMap
<
QString
,
Joystick
*>
JoystickSDL
::
discover
(
MultiVehicleManager
*
_multiVehicleManager
)
{
static
QMap
<
QString
,
Joystick
*>
ret
;
if
(
SDL_InitSubSystem
(
SDL_INIT_
JOYSTICK
|
SDL_INIT_NOPARACHUTE
)
<
0
)
{
if
(
SDL_InitSubSystem
(
SDL_INIT_
GAMECONTROLLER
|
SDL_INIT_NOPARACHUTE
)
<
0
)
{
qWarning
()
<<
"Couldn't initialize SimpleDirectMediaLayer:"
<<
SDL_GetError
();
return
ret
;
}
_loadGameControllerMappings
();
// Load available joysticks
qCDebug
(
JoystickLog
)
<<
"Available joysticks"
;
...
...
@@ -25,26 +29,27 @@ QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicl
for
(
int
i
=
0
;
i
<
SDL_NumJoysticks
();
i
++
)
{
QString
name
=
SDL_JoystickNameForIndex
(
i
);
if
(
SDL_IsGameController
(
i
))
{
qDebug
()
<<
name
<<
"supports SDL GameController!"
;
}
else
{
qDebug
()
<<
name
<<
"DOES NOT support SDL GameController!"
;
}
SDL_GameController
*
sdlController
=
SDL_GameControllerOpen
(
i
);
qDebug
()
<<
SDL_GetError
();
if
(
!
ret
.
contains
(
name
))
{
int
axisCount
,
buttonCount
,
hatCount
;
bool
isGameController
;
SDL_Joystick
*
sdlJoystick
=
SDL_JoystickOpen
(
i
);
axisCount
=
SDL_JoystickNumAxes
(
sdlJoystick
);
buttonCount
=
SDL_JoystickNumButtons
(
sdlJoystick
);
hatCount
=
SDL_JoystickNumHats
(
sdlJoystick
);
if
(
SDL_IsGameController
(
i
))
{
isGameController
=
true
;
axisCount
=
SDL_CONTROLLER_AXIS_MAX
;
buttonCount
=
SDL_CONTROLLER_BUTTON_MAX
;
hatCount
=
0
;
}
else
{
isGameController
=
false
;
axisCount
=
SDL_JoystickNumAxes
(
sdlJoystick
);
buttonCount
=
SDL_JoystickNumButtons
(
sdlJoystick
);
hatCount
=
SDL_JoystickNumHats
(
sdlJoystick
);
}
SDL_JoystickClose
(
sdlJoystick
);
qCDebug
(
JoystickLog
)
<<
"
\t
"
<<
name
<<
"axes:"
<<
axisCount
<<
"buttons:"
<<
buttonCount
<<
"hats:"
<<
hatCount
;
ret
[
name
]
=
new
JoystickSDL
(
name
,
axisCount
,
buttonCount
,
hatCount
,
i
,
_multiVehicleManager
);
ret
[
name
]
=
new
JoystickSDL
(
name
,
axisCount
,
buttonCount
,
hatCount
,
i
,
isGameController
,
_multiVehicleManager
);
}
else
{
qCDebug
(
JoystickLog
)
<<
"
\t
Skipping duplicate"
<<
name
;
}
...
...
@@ -52,8 +57,28 @@ QMap<QString, Joystick*> JoystickSDL::discover(MultiVehicleManager* _multiVehicl
return
ret
;
}
void
JoystickSDL
::
_loadGameControllerMappings
(
void
)
{
QFile
file
(
":/db/mapping/joystick/gamecontrollerdb.txt"
);
if
(
!
file
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
{
qWarning
()
<<
"Couldn't load GameController mapping database."
;
return
;
}
QTextStream
s
(
&
file
);
while
(
!
s
.
atEnd
())
{
SDL_GameControllerAddMapping
(
s
.
readLine
().
toStdString
().
c_str
());
}
}
bool
JoystickSDL
::
_open
(
void
)
{
sdlJoystick
=
SDL_JoystickOpen
(
_index
);
if
(
_isGameController
)
{
sdlController
=
SDL_GameControllerOpen
(
_index
);
sdlJoystick
=
SDL_GameControllerGetJoystick
(
sdlController
);
}
else
{
sdlJoystick
=
SDL_JoystickOpen
(
_index
);
}
if
(
!
sdlJoystick
)
{
qCWarning
(
JoystickLog
)
<<
"SDL_JoystickOpen failed:"
<<
SDL_GetError
();
...
...
@@ -70,15 +95,24 @@ void JoystickSDL::_close(void) {
bool
JoystickSDL
::
_update
(
void
)
{
SDL_JoystickUpdate
();
SDL_GameControllerUpdate
();
return
true
;
}
bool
JoystickSDL
::
_getButton
(
int
i
)
{
return
!!
SDL_JoystickGetButton
(
sdlJoystick
,
i
);
if
(
_isGameController
)
{
return
!!
SDL_GameControllerGetButton
(
sdlController
,
SDL_GameControllerButton
(
i
));
}
else
{
return
!!
SDL_JoystickGetButton
(
sdlJoystick
,
i
);
}
}
int
JoystickSDL
::
_getAxis
(
int
i
)
{
return
SDL_JoystickGetAxis
(
sdlJoystick
,
i
);
if
(
_isGameController
)
{
return
SDL_GameControllerGetAxis
(
sdlController
,
SDL_GameControllerAxis
(
i
));
}
else
{
return
SDL_JoystickGetAxis
(
sdlJoystick
,
i
);
}
}
uint8_t
JoystickSDL
::
_getHat
(
int
hat
,
int
i
)
{
...
...
src/Joystick/JoystickSDL.h
View file @
e67514bc
...
...
@@ -15,11 +15,13 @@
class
JoystickSDL
:
public
Joystick
{
public:
JoystickSDL
(
const
QString
&
name
,
int
axisCount
,
int
buttonCount
,
int
hatCount
,
int
index
,
MultiVehicleManager
*
multiVehicleManager
);
JoystickSDL
(
const
QString
&
name
,
int
axisCount
,
int
buttonCount
,
int
hatCount
,
int
index
,
bool
isGameController
,
MultiVehicleManager
*
multiVehicleManager
);
static
QMap
<
QString
,
Joystick
*>
discover
(
MultiVehicleManager
*
_multiVehicleManager
);
private:
static
void
_loadGameControllerMappings
();
bool
_open
()
final
;
void
_close
()
final
;
bool
_update
()
final
;
...
...
@@ -29,6 +31,8 @@ private:
uint8_t
_getHat
(
int
hat
,
int
i
)
final
;
SDL_Joystick
*
sdlJoystick
;
SDL_GameController
*
sdlController
;
bool
_isGameController
;
int
_index
;
///< Index for SDL_JoystickOpen
};
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment