Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qgroundcontrol
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Valentin Platzgummer
qgroundcontrol
Commits
41fc1196
Commit
41fc1196
authored
Apr 23, 2016
by
Gregory Dymarek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implementing handlers
parent
51814a3d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
41 deletions
+53
-41
JoystickAndroid.cc
src/Joystick/JoystickAndroid.cc
+50
-39
JoystickAndroid.h
src/Joystick/JoystickAndroid.h
+3
-2
No files found.
src/Joystick/JoystickAndroid.cc
View file @
41fc1196
...
...
@@ -6,19 +6,18 @@
int
JoystickAndroid
::
_androidBtnListCount
;
int
*
JoystickAndroid
::
_androidBtnList
;
int
JoystickAndroid
::
ACTION_DOWN
;
int
JoystickAndroid
::
ACTION_UP
;
QMutex
JoystickAndroid
::
m_mutex
;
JoystickAndroid
::
JoystickAndroid
(
const
Q
AndroidJniObject
&
inputDevice
,
const
Q
String
&
name
,
int
axisCount
,
int
buttonCount
,
int
id
,
MultiVehicleManager
*
multiVehicleManager
)
JoystickAndroid
::
JoystickAndroid
(
const
QString
&
name
,
int
axisCount
,
int
buttonCount
,
int
id
,
MultiVehicleManager
*
multiVehicleManager
)
:
Joystick
(
name
,
axisCount
,
buttonCount
,
multiVehicleManager
)
,
deviceId
(
id
)
{
int
i
;
QAndroidJniEnvironment
env
;
//if we define inputDevice in here this will fail for some reason, hence passing through argument
//QAndroidJniObject inputDevice = QAndroidJniObject::callStaticObjectMethod("android/view/InputDevice", "getDevice", "(I)Landroid/view/InputDevice;", id);
QAndroidJniObject
inputDevice
=
QAndroidJniObject
::
callStaticObjectMethod
(
"android/view/InputDevice"
,
"getDevice"
,
"(I)Landroid/view/InputDevice;"
,
id
);
//set button mapping (number->code)
jintArray
b
=
env
->
NewIntArray
(
_androidBtnListCount
);
...
...
@@ -44,15 +43,14 @@ JoystickAndroid::JoystickAndroid(const QAndroidJniObject &inputDevice, const QSt
axisValue
=
new
int
[
_axisCount
];
axisCode
=
new
int
[
_axisCount
];
QAndroidJniObject
rangeListNative
=
inputDevice
.
callObjectMethod
(
"getMotionRanges"
,
"()Ljava/util/List;"
);
for
(
i
=
0
;
i
<
_axisCount
;
i
++
)
{
QAndroidJniObject
range
=
rangeListNative
.
callObjectMethod
(
"get"
,
"()Landroid/view/InputDevice/MotionRange;"
);
if
(
range
.
isValid
())
axisCode
[
i
]
=
range
.
callMethod
<
jint
>
(
"getAxis"
);
for
(
i
=
0
;
i
<
_axisCount
;
i
++
)
{
QAndroidJniObject
range
=
rangeListNative
.
callObjectMethod
(
"get"
,
"(I)Ljava/lang/Object;"
,
i
);
axisCode
[
i
]
=
range
.
callMethod
<
jint
>
(
"getAxis"
);
axisValue
[
i
]
=
0
;
}
q
Debug
(
)
<<
"axis:"
<<
_axisCount
<<
"buttons:"
<<
_buttonCount
;
q
CDebug
(
JoystickLog
)
<<
"axis:"
<<
_axisCount
<<
"buttons:"
<<
_buttonCount
;
QtAndroidPrivate
::
registerGenericMotionEventListener
(
this
);
QtAndroidPrivate
::
registerKeyEventListener
(
this
);
}
...
...
@@ -67,31 +65,11 @@ JoystickAndroid::~JoystickAndroid() {
QtAndroidPrivate
::
unregisterKeyEventListener
(
this
);
}
bool
JoystickAndroid
::
handleKeyEvent
(
jobject
event
)
{
QJNIObjectPrivate
ev
(
event
);
QMutexLocker
lock
(
&
m_mutex
);
const
int
_deviceId
=
ev
.
callMethod
<
jint
>
(
"getDeviceId"
,
"()I"
);
if
(
_deviceId
!=
deviceId
)
return
false
;
//qDebug() << "handleKeyEvent!" << deviceId;
return
true
;
}
bool
JoystickAndroid
::
handleGenericMotionEvent
(
jobject
event
)
{
QJNIObjectPrivate
ev
(
event
);
QMutexLocker
lock
(
&
m_mutex
);
const
int
_deviceId
=
ev
.
callMethod
<
jint
>
(
"getDeviceId"
,
"()I"
);
if
(
_deviceId
!=
deviceId
)
return
false
;
//qDebug() << "handleMotionEvent!" << deviceId;
return
true
;
}
QMap
<
QString
,
Joystick
*>
JoystickAndroid
::
discover
(
MultiVehicleManager
*
_multiVehicleManager
)
{
bool
joystickFound
=
false
;
static
QMap
<
QString
,
Joystick
*>
ret
;
_
buttonList
();
//it's enough to run it once, should be in a static constructor
_
initStatic
();
//it's enough to run it once, should be in a static constructor
QMutexLocker
lock
(
&
m_mutex
);
...
...
@@ -131,13 +109,13 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe
jbooleanArray
jSupportedButtons
=
btns
.
object
<
jbooleanArray
>
();
jboolean
*
supportedButtons
=
env
->
GetBooleanArrayElements
(
jSupportedButtons
,
nullptr
);
int
buttonCount
=
0
;
for
(
i
=
0
;
i
<
_androidBtnListCount
;
i
++
)
if
(
supportedButtons
[
i
])
buttonCount
++
;
for
(
i
nt
j
=
0
;
j
<
_androidBtnListCount
;
j
++
)
if
(
supportedButtons
[
j
])
buttonCount
++
;
env
->
ReleaseBooleanArrayElements
(
jSupportedButtons
,
supportedButtons
,
0
);
q
Debug
(
)
<<
"
\t
"
<<
name
<<
"id:"
<<
buff
[
i
]
<<
"axes:"
<<
axisCount
<<
"buttons:"
<<
buttonCount
;
q
CDebug
(
JoystickLog
)
<<
"
\t
"
<<
name
<<
"id:"
<<
buff
[
i
]
<<
"axes:"
<<
axisCount
<<
"buttons:"
<<
buttonCount
;
ret
[
name
]
=
new
JoystickAndroid
(
inputDevice
,
name
,
axisCount
,
buttonCount
,
buff
[
i
],
_multiVehicleManager
);
ret
[
name
]
=
new
JoystickAndroid
(
name
,
axisCount
,
buttonCount
,
buff
[
i
],
_multiVehicleManager
);
joystickFound
=
true
;
}
...
...
@@ -147,6 +125,40 @@ QMap<QString, Joystick*> JoystickAndroid::discover(MultiVehicleManager* _multiVe
return
ret
;
}
bool
JoystickAndroid
::
handleKeyEvent
(
jobject
event
)
{
QJNIObjectPrivate
ev
(
event
);
QMutexLocker
lock
(
&
m_mutex
);
const
int
_deviceId
=
ev
.
callMethod
<
jint
>
(
"getDeviceId"
,
"()I"
);
if
(
_deviceId
!=
deviceId
)
return
false
;
const
int
action
=
ev
.
callMethod
<
jint
>
(
"getAction"
,
"()I"
);
const
int
keyCode
=
ev
.
callMethod
<
jint
>
(
"getKeyCode"
,
"()I"
);
for
(
int
i
=
0
;
i
<
_buttonCount
;
i
++
)
{
if
(
btnCode
[
i
]
==
keyCode
)
{
if
(
action
==
ACTION_DOWN
)
btnValue
[
i
]
=
true
;
if
(
action
==
ACTION_UP
)
btnValue
[
i
]
=
false
;
return
true
;
}
}
return
false
;
}
bool
JoystickAndroid
::
handleGenericMotionEvent
(
jobject
event
)
{
QJNIObjectPrivate
ev
(
event
);
QMutexLocker
lock
(
&
m_mutex
);
const
int
_deviceId
=
ev
.
callMethod
<
jint
>
(
"getDeviceId"
,
"()I"
);
if
(
_deviceId
!=
deviceId
)
return
false
;
for
(
int
i
=
0
;
i
<
_axisCount
;
i
++
)
{
const
float
v
=
ev
.
callMethod
<
jfloat
>
(
"getAxisValue"
,
"(I)F"
,
axisCode
[
i
]);
axisValue
[
i
]
=
(
int
)(
v
*
32767.
f
);
}
return
true
;
}
bool
JoystickAndroid
::
open
(
void
)
{
return
true
;
}
...
...
@@ -169,7 +181,7 @@ int JoystickAndroid::getAxis(int i) {
//helper method
void
JoystickAndroid
::
_
buttonList
()
{
void
JoystickAndroid
::
_
initStatic
()
{
//this gets list of all possible buttons - this is needed to check how many buttons our gamepad supports
//instead of the whole logic below we could have just a simple array of hardcoded int values as these 'should' not change
...
...
@@ -202,8 +214,7 @@ void JoystickAndroid::_buttonList() {
ret
[
i
++
]
=
QAndroidJniObject
::
getStaticField
<
jint
>
(
"android/view/KeyEvent"
,
"KEYCODE_BUTTON_Y"
);
ret
[
i
++
]
=
QAndroidJniObject
::
getStaticField
<
jint
>
(
"android/view/KeyEvent"
,
"KEYCODE_BUTTON_Z"
);
for
(
int
j
=
0
;
j
<
_androidBtnListCount
;
j
++
)
qDebug
()
<<
"
\t
possible button: "
+
QString
::
number
(
_androidBtnList
[
j
]);
ACTION_DOWN
=
QAndroidJniObject
::
getStaticField
<
jint
>
(
"android/view/KeyEvent"
,
"ACTION_DOWN"
);
ACTION_UP
=
QAndroidJniObject
::
getStaticField
<
jint
>
(
"android/view/KeyEvent"
,
"ACTION_UP"
);
}
src/Joystick/JoystickAndroid.h
View file @
41fc1196
...
...
@@ -15,7 +15,7 @@
class
JoystickAndroid
:
public
Joystick
,
public
QtAndroidPrivate
::
GenericMotionEventListener
,
public
QtAndroidPrivate
::
KeyEventListener
{
public:
JoystickAndroid
(
const
Q
AndroidJniObject
&
inputDevice
,
const
Q
String
&
name
,
int
axisCount
,
int
buttonCount
,
int
id
,
MultiVehicleManager
*
multiVehicleManager
);
JoystickAndroid
(
const
QString
&
name
,
int
axisCount
,
int
buttonCount
,
int
id
,
MultiVehicleManager
*
multiVehicleManager
);
~
JoystickAndroid
();
static
QMap
<
QString
,
Joystick
*>
discover
(
MultiVehicleManager
*
_multiVehicleManager
);
...
...
@@ -36,10 +36,11 @@ private:
bool
*
btnValue
;
int
*
axisValue
;
static
void
_
buttonList
();
static
void
_
initStatic
();
static
int
*
_androidBtnList
;
//list of all possible android buttons
static
int
_androidBtnListCount
;
static
int
ACTION_DOWN
,
ACTION_UP
;
static
QMutex
m_mutex
;
int
deviceId
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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