2021-12-31 15:14:15 +00:00
|
|
|
// Copyright 2021 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-04-15 04:02:53 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Android/Android.h"
|
2021-12-10 02:22:16 +00:00
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
#include <algorithm>
|
2022-01-01 17:31:50 +00:00
|
|
|
#include <atomic>
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
#include <chrono>
|
2021-12-31 15:14:15 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
#include <android/input.h>
|
|
|
|
#include <android/keycodes.h>
|
|
|
|
#include <jni.h>
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
#include "Common/Assert.h"
|
2021-12-31 15:14:15 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2022-01-01 17:31:50 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2021-12-31 15:14:15 +00:00
|
|
|
|
2016-06-12 15:08:04 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2013-04-15 04:02:53 +00:00
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
#include "jni/AndroidCommon/AndroidCommon.h"
|
|
|
|
#include "jni/AndroidCommon/IDCache.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
jclass s_list_class;
|
|
|
|
jmethodID s_list_get;
|
|
|
|
jmethodID s_list_size;
|
|
|
|
|
|
|
|
jclass s_input_device_class;
|
|
|
|
jmethodID s_input_device_get_device_ids;
|
|
|
|
jmethodID s_input_device_get_device;
|
|
|
|
jmethodID s_input_device_get_controller_number;
|
2022-01-01 17:31:50 +00:00
|
|
|
jmethodID s_input_device_get_id;
|
2021-12-31 15:14:15 +00:00
|
|
|
jmethodID s_input_device_get_motion_ranges;
|
|
|
|
jmethodID s_input_device_get_name;
|
|
|
|
jmethodID s_input_device_get_sources;
|
|
|
|
jmethodID s_input_device_has_keys;
|
|
|
|
|
|
|
|
jclass s_motion_range_class;
|
|
|
|
jmethodID s_motion_range_get_axis;
|
|
|
|
jmethodID s_motion_range_get_max;
|
|
|
|
jmethodID s_motion_range_get_min;
|
|
|
|
jmethodID s_motion_range_get_source;
|
|
|
|
|
2022-01-01 17:31:50 +00:00
|
|
|
jclass s_input_event_class;
|
|
|
|
jmethodID s_input_event_get_device;
|
|
|
|
|
|
|
|
jclass s_key_event_class;
|
|
|
|
jmethodID s_key_event_get_action;
|
|
|
|
jmethodID s_key_event_get_keycode;
|
|
|
|
|
|
|
|
jclass s_motion_event_class;
|
|
|
|
jmethodID s_motion_event_get_axis_value;
|
|
|
|
jmethodID s_motion_event_get_source;
|
|
|
|
|
2022-03-06 19:27:25 +00:00
|
|
|
jclass s_controller_interface_class;
|
|
|
|
jmethodID s_controller_interface_register_input_device_listener;
|
|
|
|
jmethodID s_controller_interface_unregister_input_device_listener;
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
jclass s_sensor_event_listener_class;
|
|
|
|
jmethodID s_sensor_event_listener_constructor;
|
2022-09-16 21:02:30 +00:00
|
|
|
jmethodID s_sensor_event_listener_constructor_input_device;
|
|
|
|
jmethodID s_sensor_event_listener_set_device_qualifier;
|
2022-09-16 20:03:03 +00:00
|
|
|
jmethodID s_sensor_event_listener_enable_sensor_events;
|
|
|
|
jmethodID s_sensor_event_listener_disable_sensor_events;
|
|
|
|
jmethodID s_sensor_event_listener_get_axis_names;
|
|
|
|
jmethodID s_sensor_event_listener_get_negative_axes;
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
jintArray s_keycodes_array;
|
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
constexpr Clock::duration ACTIVE_INPUT_TIMEOUT = std::chrono::milliseconds(1000);
|
|
|
|
|
2022-01-01 17:31:50 +00:00
|
|
|
std::unordered_map<jint, ciface::Core::DeviceQualifier> s_device_id_to_device_qualifier;
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
constexpr int MAX_KEYCODE = AKEYCODE_PROFILE_SWITCH; // Up to date as of SDK 31
|
|
|
|
|
|
|
|
const std::array<std::string_view, MAX_KEYCODE + 1> KEYCODE_NAMES = {
|
|
|
|
"Unknown",
|
|
|
|
"Soft Left",
|
|
|
|
"Soft Right",
|
|
|
|
"Home",
|
|
|
|
"Back",
|
|
|
|
"Call",
|
|
|
|
"End Call",
|
|
|
|
"0",
|
|
|
|
"1",
|
|
|
|
"2",
|
|
|
|
"3",
|
|
|
|
"4",
|
|
|
|
"5",
|
|
|
|
"6",
|
|
|
|
"7",
|
|
|
|
"8",
|
|
|
|
"9",
|
|
|
|
"Star",
|
|
|
|
"Pound",
|
|
|
|
"Up",
|
|
|
|
"Down",
|
|
|
|
"Left",
|
|
|
|
"Right",
|
|
|
|
"Center",
|
|
|
|
"Volume Up",
|
|
|
|
"Volume Down",
|
|
|
|
"Power",
|
|
|
|
"Camera",
|
|
|
|
"Clear",
|
|
|
|
"A",
|
|
|
|
"B",
|
|
|
|
"C",
|
|
|
|
"D",
|
|
|
|
"E",
|
|
|
|
"F",
|
|
|
|
"G",
|
|
|
|
"H",
|
|
|
|
"I",
|
|
|
|
"J",
|
|
|
|
"K",
|
|
|
|
"L",
|
|
|
|
"M",
|
|
|
|
"N",
|
|
|
|
"O",
|
|
|
|
"P",
|
|
|
|
"Q",
|
|
|
|
"R",
|
|
|
|
"S",
|
|
|
|
"T",
|
|
|
|
"U",
|
|
|
|
"V",
|
|
|
|
"W",
|
|
|
|
"X",
|
|
|
|
"Y",
|
|
|
|
"Z",
|
|
|
|
"Comma",
|
|
|
|
"Period",
|
|
|
|
"Left Alt",
|
|
|
|
"Right Alt",
|
|
|
|
"Left Shift",
|
|
|
|
"Right Shift",
|
|
|
|
"Tab",
|
|
|
|
"Space",
|
|
|
|
"Sym",
|
|
|
|
"Explorer",
|
|
|
|
"Envelope",
|
|
|
|
"Enter",
|
|
|
|
"Backspace",
|
|
|
|
"Grave",
|
|
|
|
"Minus",
|
|
|
|
"Equals",
|
|
|
|
"Left Bracket",
|
|
|
|
"Right Bracket",
|
|
|
|
"Backslash",
|
|
|
|
"Semicolon",
|
|
|
|
"Apostrophe",
|
|
|
|
"Slash",
|
|
|
|
"At",
|
|
|
|
"Num",
|
|
|
|
"Headset Hook",
|
|
|
|
"Focus",
|
|
|
|
"Plus",
|
|
|
|
"Menu",
|
|
|
|
"Notification",
|
|
|
|
"Search",
|
|
|
|
"Play Pause",
|
|
|
|
"Stop",
|
|
|
|
"Next",
|
|
|
|
"Previous",
|
|
|
|
"Rewind",
|
|
|
|
"Fast Forward",
|
|
|
|
"Mute",
|
|
|
|
"Page Up",
|
|
|
|
"Page Down",
|
|
|
|
"Emoji",
|
|
|
|
"Switch Charset",
|
|
|
|
"Button A",
|
|
|
|
"Button B",
|
|
|
|
"Button C",
|
|
|
|
"Button X",
|
|
|
|
"Button Y",
|
|
|
|
"Button Z",
|
|
|
|
"Button L1",
|
|
|
|
"Button R1",
|
|
|
|
"Button L2",
|
|
|
|
"Button R2",
|
|
|
|
"Button L3",
|
|
|
|
"Button R3",
|
|
|
|
"Start",
|
|
|
|
"Select",
|
|
|
|
"Mode",
|
|
|
|
"Escape",
|
|
|
|
"Delete",
|
|
|
|
"Left Ctrl",
|
|
|
|
"Right Ctrl",
|
|
|
|
"Caps Lock",
|
|
|
|
"Scroll Lock",
|
|
|
|
"Left Meta",
|
|
|
|
"Right Meta",
|
|
|
|
"Fn",
|
|
|
|
"PrtSc SysRq",
|
|
|
|
"Pause Break",
|
|
|
|
"Move Home",
|
|
|
|
"Move End",
|
|
|
|
"Insert",
|
|
|
|
"Forward",
|
|
|
|
"Play",
|
|
|
|
"Pause",
|
|
|
|
"Close",
|
|
|
|
"Eject",
|
|
|
|
"Record",
|
|
|
|
"F1",
|
|
|
|
"F2",
|
|
|
|
"F3",
|
|
|
|
"F4",
|
|
|
|
"F5",
|
|
|
|
"F6",
|
|
|
|
"F7",
|
|
|
|
"F8",
|
|
|
|
"F9",
|
|
|
|
"F10",
|
|
|
|
"F11",
|
|
|
|
"F12",
|
|
|
|
"Num Lock",
|
|
|
|
"Numpad 0",
|
|
|
|
"Numpad 1",
|
|
|
|
"Numpad 2",
|
|
|
|
"Numpad 3",
|
|
|
|
"Numpad 4",
|
|
|
|
"Numpad 5",
|
|
|
|
"Numpad 6",
|
|
|
|
"Numpad 7",
|
|
|
|
"Numpad 8",
|
|
|
|
"Numpad 9",
|
|
|
|
"Numpad Divide",
|
|
|
|
"Numpad Multiply",
|
|
|
|
"Numpad Subtract",
|
|
|
|
"Numpad Add",
|
|
|
|
"Numpad Dot",
|
|
|
|
"Numpad Comma",
|
|
|
|
"Numpad Enter",
|
|
|
|
"Numpad Equals",
|
|
|
|
"Numpad Left Paren",
|
|
|
|
"Numpad Right Paren",
|
|
|
|
"Volume Mute",
|
|
|
|
"Info",
|
|
|
|
"Channel Up",
|
|
|
|
"Channel Down",
|
|
|
|
"Zoom In",
|
|
|
|
"Zoom Out",
|
|
|
|
"TV",
|
|
|
|
"Window",
|
|
|
|
"Guide",
|
|
|
|
"DVR",
|
|
|
|
"Bookmark",
|
|
|
|
"Captions",
|
|
|
|
"Settings",
|
|
|
|
"TV Power",
|
|
|
|
"TV Input",
|
|
|
|
"STB Power",
|
|
|
|
"STB Input",
|
|
|
|
"AVR Power",
|
|
|
|
"AVR Input",
|
|
|
|
"Prog Red",
|
|
|
|
"Prog Green",
|
|
|
|
"Prog Yellow",
|
|
|
|
"Prog Blue",
|
|
|
|
"App Switch",
|
|
|
|
"Button 1",
|
|
|
|
"Button 2",
|
|
|
|
"Button 3",
|
|
|
|
"Button 4",
|
|
|
|
"Button 5",
|
|
|
|
"Button 6",
|
|
|
|
"Button 7",
|
|
|
|
"Button 8",
|
|
|
|
"Button 9",
|
|
|
|
"Button 10",
|
|
|
|
"Button 11",
|
|
|
|
"Button 12",
|
|
|
|
"Button 13",
|
|
|
|
"Button 14",
|
|
|
|
"Button 15",
|
|
|
|
"Button 16",
|
|
|
|
"Language Switch",
|
|
|
|
"Manner Mode",
|
|
|
|
"3D Mode",
|
|
|
|
"Contacts",
|
|
|
|
"Calendar",
|
|
|
|
"Music",
|
|
|
|
"Calculator",
|
|
|
|
"Zenkaku Hankaku",
|
|
|
|
"Eisu",
|
|
|
|
"Henkan",
|
|
|
|
"Muhenkan",
|
|
|
|
"Katakana Hiragana",
|
|
|
|
"Yen",
|
|
|
|
"Ro",
|
|
|
|
"Kana",
|
|
|
|
"Assist",
|
|
|
|
"Brightness Down",
|
|
|
|
"Brightness Up",
|
|
|
|
"Audio Track",
|
|
|
|
"Sleep",
|
|
|
|
"Wakeup",
|
|
|
|
"Pairing",
|
|
|
|
"Top Menu",
|
|
|
|
"11",
|
|
|
|
"12",
|
|
|
|
"Last Channel",
|
|
|
|
"Data Service",
|
|
|
|
"Voice Assist",
|
|
|
|
"Radio Service",
|
|
|
|
"Teletext",
|
|
|
|
"Number Entry",
|
|
|
|
"Terrestrial Analog",
|
|
|
|
"Terrestrial Digital",
|
|
|
|
"Satellite",
|
|
|
|
"Satellite BS",
|
|
|
|
"Satellite CS",
|
|
|
|
"Satellite Service",
|
|
|
|
"Network",
|
|
|
|
"Antenna Cable",
|
|
|
|
"Input HDMI 1",
|
|
|
|
"Input HDMI 2",
|
|
|
|
"Input HDMI 3",
|
|
|
|
"Input HDMI 4",
|
|
|
|
"Input Composite 1",
|
|
|
|
"Input Composite 2",
|
|
|
|
"Input Component 1",
|
|
|
|
"Input Component 2",
|
|
|
|
"Input VGA 1",
|
|
|
|
"Audio Description",
|
|
|
|
"Audio Description Mix Up",
|
|
|
|
"Audio Description Mix Down",
|
|
|
|
"Zoom Mode",
|
|
|
|
"Contents Menu",
|
|
|
|
"Media Context Menu",
|
|
|
|
"Timer Programming",
|
|
|
|
"Help",
|
|
|
|
"Navigate Previous",
|
|
|
|
"Navigate Next",
|
|
|
|
"Navigate In",
|
|
|
|
"Navigate Out",
|
|
|
|
"Stem Primary",
|
|
|
|
"Stem 1",
|
|
|
|
"Stem 2",
|
|
|
|
"Stem 3",
|
|
|
|
"Up Left",
|
|
|
|
"Down Left",
|
|
|
|
"Up Right",
|
|
|
|
"Down Right",
|
|
|
|
"Skip Forward",
|
|
|
|
"Skip Backward",
|
|
|
|
"Step Forward",
|
|
|
|
"Step Backward",
|
|
|
|
"Soft Sleep",
|
|
|
|
"Cut",
|
|
|
|
"Copy",
|
|
|
|
"Paste",
|
|
|
|
"System Navigation Up",
|
|
|
|
"System Navigation Down",
|
|
|
|
"System Navigation Left",
|
|
|
|
"System Navigation Right",
|
|
|
|
"All Apps",
|
|
|
|
"Refresh",
|
|
|
|
"Thumbs Up",
|
|
|
|
"Thumbs Down",
|
|
|
|
"Profile Switch",
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string ConstructKeyName(int keycode)
|
|
|
|
{
|
|
|
|
return std::string(KEYCODE_NAMES[keycode]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ConstructAxisNamePrefix(int source)
|
|
|
|
{
|
|
|
|
// A device is allowed to have two axes with the same axis ID but different source IDs,
|
|
|
|
// so we have to make sure to include the source in the axis name.
|
|
|
|
|
|
|
|
static const std::unordered_map<int, std::string> source_names{
|
|
|
|
{AINPUT_SOURCE_KEYBOARD, "Keyboard"},
|
|
|
|
{AINPUT_SOURCE_DPAD, "Dpad"},
|
|
|
|
{AINPUT_SOURCE_GAMEPAD, "Gamepad"},
|
|
|
|
{AINPUT_SOURCE_TOUCHSCREEN, "Touch"},
|
|
|
|
{AINPUT_SOURCE_MOUSE, "Cursor"},
|
|
|
|
{AINPUT_SOURCE_STYLUS, "Stylus"},
|
|
|
|
{AINPUT_SOURCE_BLUETOOTH_STYLUS, "BTStylus"},
|
|
|
|
{AINPUT_SOURCE_TRACKBALL, "Trackball"},
|
|
|
|
{AINPUT_SOURCE_MOUSE_RELATIVE, "Mouse"},
|
|
|
|
{AINPUT_SOURCE_TOUCHPAD, "Touchpad"},
|
|
|
|
{AINPUT_SOURCE_TOUCH_NAVIGATION, "Touchnav"},
|
|
|
|
{AINPUT_SOURCE_JOYSTICK, "Axis"}, // The typical source for all axes on a gamepad
|
|
|
|
{AINPUT_SOURCE_HDMI, "HDMI"},
|
|
|
|
{AINPUT_SOURCE_SENSOR, "Sensor"},
|
|
|
|
{AINPUT_SOURCE_ROTARY_ENCODER, "Rotary"},
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto it = source_names.find(source);
|
|
|
|
if (it != source_names.end())
|
|
|
|
return fmt::format("{} ", it->second);
|
|
|
|
else
|
|
|
|
return fmt::format("Axis {:08x}/", source);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ConstructAxisName(int source, int axis, bool negative)
|
|
|
|
{
|
|
|
|
const char sign = negative ? '-' : '+';
|
|
|
|
return fmt::format("{}{}{}", ConstructAxisNamePrefix(source), axis, sign);
|
|
|
|
}
|
|
|
|
|
2022-01-01 17:31:50 +00:00
|
|
|
std::shared_ptr<ciface::Core::Device> FindDevice(jint device_id)
|
|
|
|
{
|
|
|
|
const auto it = s_device_id_to_device_qualifier.find(device_id);
|
|
|
|
if (it == s_device_id_to_device_qualifier.end())
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Could not find device ID {}", device_id);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciface::Core::DeviceQualifier& qualifier = it->second;
|
|
|
|
std::shared_ptr<ciface::Core::Device> device = g_controller_interface.FindDevice(qualifier);
|
|
|
|
if (!device)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Could not find device {}", qualifier.ToString());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return device;
|
|
|
|
}
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
namespace ciface::Android
|
2013-04-15 04:02:53 +00:00
|
|
|
{
|
2022-01-01 17:31:50 +00:00
|
|
|
class AndroidInput : public Core::Device::Input
|
2021-12-31 15:14:15 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-01-01 17:31:50 +00:00
|
|
|
explicit AndroidInput(std::string name) : m_name(std::move(name))
|
2021-12-31 15:14:15 +00:00
|
|
|
{
|
|
|
|
DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Created {}", m_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetName() const override { return m_name; }
|
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
ControlState GetState() const override
|
|
|
|
{
|
|
|
|
m_last_polled.store(Clock::now(), std::memory_order_relaxed);
|
|
|
|
return m_state.load(std::memory_order_relaxed);
|
|
|
|
}
|
2022-01-01 17:31:50 +00:00
|
|
|
|
|
|
|
void SetState(ControlState state) { m_state.store(state, std::memory_order_relaxed); }
|
2021-12-31 15:14:15 +00:00
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
Clock::time_point GetLastPolled() const { return m_last_polled.load(std::memory_order_relaxed); }
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
private:
|
|
|
|
std::string m_name;
|
2022-01-01 17:31:50 +00:00
|
|
|
std::atomic<ControlState> m_state = 0;
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
mutable std::atomic<Clock::time_point> m_last_polled{};
|
2022-01-01 17:31:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AndroidKey final : public AndroidInput
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit AndroidKey(int keycode) : AndroidInput(ConstructKeyName(keycode)) {}
|
2021-12-31 15:14:15 +00:00
|
|
|
};
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
class AndroidAxis : public AndroidInput
|
2021-12-31 15:14:15 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
AndroidAxis(int source, int axis, bool negative)
|
2022-01-01 17:31:50 +00:00
|
|
|
: AndroidInput(ConstructAxisName(source, axis, negative)), m_negative(negative)
|
2021-12-31 15:14:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
AndroidAxis(std::string name, bool negative) : AndroidInput(std::move(name)), m_negative(negative)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
ControlState GetState() const override
|
|
|
|
{
|
2022-01-01 17:31:50 +00:00
|
|
|
return m_negative ? -AndroidInput::GetState() : AndroidInput::GetState();
|
|
|
|
}
|
2021-12-31 15:14:15 +00:00
|
|
|
|
|
|
|
private:
|
2022-01-01 17:31:50 +00:00
|
|
|
bool m_negative;
|
2021-12-31 15:14:15 +00:00
|
|
|
};
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
class AndroidSensorAxis final : public AndroidAxis
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AndroidSensorAxis(std::string name, bool negative) : AndroidAxis(std::move(name), negative) {}
|
|
|
|
|
|
|
|
bool IsDetectable() const override { return false; }
|
|
|
|
};
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
class AndroidDevice final : public Core::Device
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AndroidDevice(JNIEnv* env, jobject input_device)
|
2022-09-16 21:02:30 +00:00
|
|
|
: m_sensor_event_listener(AddSensors(env, input_device)),
|
2022-09-16 20:03:03 +00:00
|
|
|
m_source(env->CallIntMethod(input_device, s_input_device_get_sources)),
|
|
|
|
m_controller_number(env->CallIntMethod(input_device, s_input_device_get_controller_number))
|
2021-12-31 15:14:15 +00:00
|
|
|
{
|
|
|
|
jstring j_name =
|
|
|
|
reinterpret_cast<jstring>(env->CallObjectMethod(input_device, s_input_device_get_name));
|
|
|
|
m_name = GetJString(env, j_name);
|
|
|
|
env->DeleteLocalRef(j_name);
|
|
|
|
|
|
|
|
DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Sources for {}: {:08x}", GetQualifiedName(), m_source);
|
|
|
|
|
|
|
|
AddKeys(env, input_device);
|
|
|
|
AddAxes(env, input_device);
|
|
|
|
}
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
// Constructor for the device added by Dolphin to contain sensor inputs
|
|
|
|
AndroidDevice(JNIEnv* env, std::string name)
|
2022-09-16 21:02:30 +00:00
|
|
|
: m_sensor_event_listener(AddSensors(env, nullptr)), m_source(AINPUT_SOURCE_SENSOR),
|
2022-09-16 20:03:03 +00:00
|
|
|
m_controller_number(0), m_name(std::move(name))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~AndroidDevice()
|
|
|
|
{
|
|
|
|
if (m_sensor_event_listener)
|
|
|
|
IDCache::GetEnvForThread()->DeleteGlobalRef(m_sensor_event_listener);
|
|
|
|
}
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
std::string GetName() const override { return m_name; }
|
|
|
|
|
|
|
|
std::string GetSource() const override { return "Android"; }
|
|
|
|
|
|
|
|
std::optional<int> GetPreferredId() const override
|
|
|
|
{
|
|
|
|
return m_controller_number != 0 ? std::make_optional(m_controller_number) : std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetSortPriority() const override
|
|
|
|
{
|
|
|
|
// If m_controller_number is non-zero, Android considers this to be a gamepad
|
|
|
|
if (m_controller_number != 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((m_source & AINPUT_SOURCE_KEYBOARD) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((m_source & (AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_MOUSE_RELATIVE)) != 0)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
jobject GetSensorEventListener() { return m_sensor_event_listener; }
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
private:
|
|
|
|
void AddKeys(JNIEnv* env, jobject input_device)
|
|
|
|
{
|
|
|
|
jbooleanArray keys_array = reinterpret_cast<jbooleanArray>(
|
|
|
|
env->CallObjectMethod(input_device, s_input_device_has_keys, s_keycodes_array));
|
|
|
|
jboolean* keys = env->GetBooleanArrayElements(keys_array, nullptr);
|
|
|
|
jsize keys_count = env->GetArrayLength(keys_array);
|
|
|
|
for (jsize i = 0; i < keys_count; ++i)
|
|
|
|
{
|
|
|
|
// These specific keys never get delivered to applications,
|
|
|
|
// so there's no point in letting users try to map them
|
|
|
|
if (i == AKEYCODE_HOME || i == AKEYCODE_ASSIST || i == AKEYCODE_VOICE_ASSIST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (keys[i])
|
|
|
|
AddInput(new AndroidKey(i));
|
|
|
|
}
|
|
|
|
env->ReleaseBooleanArrayElements(keys_array, keys, JNI_ABORT);
|
|
|
|
env->DeleteLocalRef(keys_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddAxes(JNIEnv* env, jobject input_device)
|
|
|
|
{
|
|
|
|
jobject motion_ranges_list =
|
|
|
|
env->CallObjectMethod(input_device, s_input_device_get_motion_ranges);
|
|
|
|
jint motion_ranges_count = env->CallIntMethod(motion_ranges_list, s_list_size);
|
|
|
|
for (jint i = 0; i < motion_ranges_count; ++i)
|
|
|
|
{
|
|
|
|
jobject motion_range = env->CallObjectMethod(motion_ranges_list, s_list_get, i);
|
|
|
|
|
|
|
|
jint source = env->CallIntMethod(motion_range, s_motion_range_get_source);
|
|
|
|
jint axis = env->CallIntMethod(motion_range, s_motion_range_get_axis);
|
|
|
|
jfloat min = env->CallFloatMethod(motion_range, s_motion_range_get_min);
|
|
|
|
jfloat max = env->CallFloatMethod(motion_range, s_motion_range_get_max);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(motion_range);
|
|
|
|
|
|
|
|
AndroidAxis* positive = nullptr;
|
|
|
|
AndroidAxis* negative = nullptr;
|
|
|
|
if (max > 0)
|
|
|
|
positive = new AndroidAxis(source, axis, false);
|
|
|
|
if (min < 0)
|
|
|
|
negative = new AndroidAxis(source, axis, true);
|
|
|
|
|
|
|
|
if (positive && negative)
|
|
|
|
AddAnalogInputs(positive, negative);
|
|
|
|
else if (positive || negative)
|
|
|
|
AddInput(positive ? positive : negative);
|
|
|
|
}
|
|
|
|
env->DeleteLocalRef(motion_ranges_list);
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:02:30 +00:00
|
|
|
jobject AddSensors(JNIEnv* env, jobject input_device)
|
2022-09-16 20:03:03 +00:00
|
|
|
{
|
2022-09-16 21:02:30 +00:00
|
|
|
jobject sensor_event_listener;
|
|
|
|
if (input_device)
|
|
|
|
{
|
|
|
|
sensor_event_listener =
|
|
|
|
env->NewObject(s_sensor_event_listener_class,
|
|
|
|
s_sensor_event_listener_constructor_input_device, input_device);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sensor_event_listener =
|
|
|
|
env->NewObject(s_sensor_event_listener_class, s_sensor_event_listener_constructor);
|
|
|
|
}
|
2022-09-16 20:03:03 +00:00
|
|
|
|
|
|
|
jobjectArray j_axis_names = reinterpret_cast<jobjectArray>(
|
|
|
|
env->CallObjectMethod(sensor_event_listener, s_sensor_event_listener_get_axis_names));
|
|
|
|
std::vector<std::string> axis_names = JStringArrayToVector(env, j_axis_names);
|
|
|
|
env->DeleteLocalRef(j_axis_names);
|
|
|
|
|
|
|
|
jbooleanArray j_negative_axes = reinterpret_cast<jbooleanArray>(
|
|
|
|
env->CallObjectMethod(sensor_event_listener, s_sensor_event_listener_get_negative_axes));
|
|
|
|
jboolean* negative_axes = env->GetBooleanArrayElements(j_negative_axes, nullptr);
|
|
|
|
|
|
|
|
ASSERT(axis_names.size() == env->GetArrayLength(j_negative_axes));
|
|
|
|
for (size_t i = 0; i < axis_names.size(); ++i)
|
|
|
|
AddInput(new AndroidSensorAxis(axis_names[i], negative_axes[i]));
|
|
|
|
|
|
|
|
env->ReleaseBooleanArrayElements(j_negative_axes, negative_axes, 0);
|
|
|
|
env->DeleteLocalRef(j_negative_axes);
|
|
|
|
|
|
|
|
jobject global_sensor_event_listener = env->NewGlobalRef(sensor_event_listener);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(sensor_event_listener);
|
|
|
|
|
|
|
|
return global_sensor_event_listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobject m_sensor_event_listener;
|
2021-12-31 15:14:15 +00:00
|
|
|
const int m_source;
|
|
|
|
const int m_controller_number;
|
|
|
|
std::string m_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creates an array that contains every possible keycode
|
|
|
|
static jintArray CreateKeyCodesArray(JNIEnv* env)
|
|
|
|
{
|
|
|
|
jintArray keycodes_array = env->NewIntArray(MAX_KEYCODE + 1);
|
|
|
|
|
|
|
|
int* keycodes = env->GetIntArrayElements(keycodes_array, nullptr);
|
|
|
|
for (int i = 0; i <= MAX_KEYCODE; ++i)
|
|
|
|
keycodes[i] = i;
|
|
|
|
env->ReleaseIntArrayElements(keycodes_array, keycodes, 0);
|
|
|
|
|
|
|
|
return keycodes_array;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
|
|
|
|
const jclass list_class = env->FindClass("java/util/List");
|
|
|
|
s_list_class = reinterpret_cast<jclass>(env->NewGlobalRef(list_class));
|
|
|
|
s_list_get = env->GetMethodID(s_list_class, "get", "(I)Ljava/lang/Object;");
|
|
|
|
s_list_size = env->GetMethodID(s_list_class, "size", "()I");
|
|
|
|
env->DeleteLocalRef(list_class);
|
|
|
|
|
|
|
|
const jclass input_device_class = env->FindClass("android/view/InputDevice");
|
|
|
|
s_input_device_class = reinterpret_cast<jclass>(env->NewGlobalRef(input_device_class));
|
|
|
|
s_input_device_get_device_ids =
|
|
|
|
env->GetStaticMethodID(s_input_device_class, "getDeviceIds", "()[I");
|
|
|
|
s_input_device_get_device =
|
|
|
|
env->GetStaticMethodID(s_input_device_class, "getDevice", "(I)Landroid/view/InputDevice;");
|
|
|
|
s_input_device_get_controller_number =
|
|
|
|
env->GetMethodID(s_input_device_class, "getControllerNumber", "()I");
|
2022-01-01 17:31:50 +00:00
|
|
|
s_input_device_get_id = env->GetMethodID(s_input_device_class, "getId", "()I");
|
2021-12-31 15:14:15 +00:00
|
|
|
s_input_device_get_motion_ranges =
|
|
|
|
env->GetMethodID(s_input_device_class, "getMotionRanges", "()Ljava/util/List;");
|
|
|
|
s_input_device_get_name =
|
|
|
|
env->GetMethodID(s_input_device_class, "getName", "()Ljava/lang/String;");
|
|
|
|
s_input_device_get_sources = env->GetMethodID(s_input_device_class, "getSources", "()I");
|
|
|
|
s_input_device_has_keys = env->GetMethodID(s_input_device_class, "hasKeys", "([I)[Z");
|
|
|
|
env->DeleteLocalRef(input_device_class);
|
|
|
|
|
|
|
|
const jclass motion_range_class = env->FindClass("android/view/InputDevice$MotionRange");
|
|
|
|
s_motion_range_class = reinterpret_cast<jclass>(env->NewGlobalRef(motion_range_class));
|
|
|
|
s_motion_range_get_axis = env->GetMethodID(s_motion_range_class, "getAxis", "()I");
|
|
|
|
s_motion_range_get_max = env->GetMethodID(s_motion_range_class, "getMax", "()F");
|
|
|
|
s_motion_range_get_min = env->GetMethodID(s_motion_range_class, "getMin", "()F");
|
|
|
|
s_motion_range_get_source = env->GetMethodID(s_motion_range_class, "getSource", "()I");
|
|
|
|
env->DeleteLocalRef(motion_range_class);
|
|
|
|
|
2022-01-01 17:31:50 +00:00
|
|
|
const jclass input_event_class = env->FindClass("android/view/InputEvent");
|
|
|
|
s_input_event_class = reinterpret_cast<jclass>(env->NewGlobalRef(input_event_class));
|
|
|
|
s_input_event_get_device =
|
|
|
|
env->GetMethodID(s_input_event_class, "getDevice", "()Landroid/view/InputDevice;");
|
|
|
|
|
|
|
|
const jclass key_event_class = env->FindClass("android/view/KeyEvent");
|
|
|
|
s_key_event_class = reinterpret_cast<jclass>(env->NewGlobalRef(key_event_class));
|
|
|
|
s_key_event_get_action = env->GetMethodID(s_key_event_class, "getAction", "()I");
|
|
|
|
s_key_event_get_keycode = env->GetMethodID(s_key_event_class, "getKeyCode", "()I");
|
|
|
|
|
|
|
|
const jclass motion_event_class = env->FindClass("android/view/MotionEvent");
|
|
|
|
s_motion_event_class = reinterpret_cast<jclass>(env->NewGlobalRef(motion_event_class));
|
|
|
|
s_motion_event_get_axis_value = env->GetMethodID(s_motion_event_class, "getAxisValue", "(I)F");
|
|
|
|
s_motion_event_get_source = env->GetMethodID(s_motion_event_class, "getSource", "()I");
|
|
|
|
|
2022-03-06 19:27:25 +00:00
|
|
|
const jclass controller_interface_class =
|
|
|
|
env->FindClass("org/dolphinemu/dolphinemu/features/input/model/ControllerInterface");
|
|
|
|
s_controller_interface_class =
|
|
|
|
reinterpret_cast<jclass>(env->NewGlobalRef(controller_interface_class));
|
|
|
|
s_controller_interface_register_input_device_listener =
|
|
|
|
env->GetStaticMethodID(s_controller_interface_class, "registerInputDeviceListener", "()V");
|
|
|
|
s_controller_interface_unregister_input_device_listener =
|
|
|
|
env->GetStaticMethodID(s_controller_interface_class, "unregisterInputDeviceListener", "()V");
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
const jclass sensor_event_listener_class =
|
|
|
|
env->FindClass("org/dolphinemu/dolphinemu/features/input/model/DolphinSensorEventListener");
|
|
|
|
s_sensor_event_listener_class =
|
|
|
|
reinterpret_cast<jclass>(env->NewGlobalRef(sensor_event_listener_class));
|
|
|
|
s_sensor_event_listener_constructor =
|
|
|
|
env->GetMethodID(s_sensor_event_listener_class, "<init>", "()V");
|
2022-09-16 21:02:30 +00:00
|
|
|
s_sensor_event_listener_constructor_input_device =
|
|
|
|
env->GetMethodID(s_sensor_event_listener_class, "<init>", "(Landroid/view/InputDevice;)V");
|
|
|
|
s_sensor_event_listener_set_device_qualifier = env->GetMethodID(
|
|
|
|
s_sensor_event_listener_class, "setDeviceQualifier", "(Ljava/lang/String;)V");
|
2022-09-16 20:03:03 +00:00
|
|
|
s_sensor_event_listener_enable_sensor_events =
|
|
|
|
env->GetMethodID(s_sensor_event_listener_class, "enableSensorEvents",
|
|
|
|
"(Lorg/dolphinemu/dolphinemu/features/input/model/SensorEventRequester;)V");
|
|
|
|
s_sensor_event_listener_disable_sensor_events =
|
|
|
|
env->GetMethodID(s_sensor_event_listener_class, "disableSensorEvents", "()V");
|
|
|
|
s_sensor_event_listener_get_axis_names =
|
|
|
|
env->GetMethodID(s_sensor_event_listener_class, "getAxisNames", "()[Ljava/lang/String;");
|
|
|
|
s_sensor_event_listener_get_negative_axes =
|
|
|
|
env->GetMethodID(s_sensor_event_listener_class, "getNegativeAxes", "()[Z");
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
jintArray keycodes_array = CreateKeyCodesArray(env);
|
|
|
|
s_keycodes_array = reinterpret_cast<jintArray>(env->NewGlobalRef(keycodes_array));
|
|
|
|
env->DeleteLocalRef(keycodes_array);
|
2022-03-06 19:27:25 +00:00
|
|
|
|
|
|
|
env->CallStaticVoidMethod(s_controller_interface_class,
|
|
|
|
s_controller_interface_register_input_device_listener);
|
2021-12-31 15:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
|
2022-03-06 19:27:25 +00:00
|
|
|
env->CallStaticVoidMethod(s_controller_interface_class,
|
|
|
|
s_controller_interface_unregister_input_device_listener);
|
|
|
|
|
2021-12-31 15:14:15 +00:00
|
|
|
env->DeleteGlobalRef(s_input_device_class);
|
|
|
|
env->DeleteGlobalRef(s_motion_range_class);
|
2022-01-01 17:31:50 +00:00
|
|
|
env->DeleteGlobalRef(s_input_event_class);
|
|
|
|
env->DeleteGlobalRef(s_key_event_class);
|
|
|
|
env->DeleteGlobalRef(s_motion_event_class);
|
2022-03-06 19:27:25 +00:00
|
|
|
env->DeleteGlobalRef(s_controller_interface_class);
|
2022-09-16 20:03:03 +00:00
|
|
|
env->DeleteGlobalRef(s_sensor_event_listener_class);
|
2021-12-31 15:14:15 +00:00
|
|
|
env->DeleteGlobalRef(s_keycodes_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddDevice(JNIEnv* env, int device_id)
|
|
|
|
{
|
|
|
|
jobject input_device =
|
|
|
|
env->CallStaticObjectMethod(s_input_device_class, s_input_device_get_device, device_id);
|
|
|
|
|
|
|
|
auto device = std::make_shared<AndroidDevice>(env, input_device);
|
|
|
|
|
|
|
|
env->DeleteLocalRef(input_device);
|
|
|
|
|
2022-01-01 17:31:50 +00:00
|
|
|
if (device->Inputs().empty() && device->Outputs().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_controller_interface.AddDevice(device);
|
|
|
|
|
|
|
|
Core::DeviceQualifier qualifier;
|
|
|
|
qualifier.FromDevice(device.get());
|
|
|
|
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Added device ID {} as {}", device_id,
|
|
|
|
device->GetQualifiedName());
|
|
|
|
s_device_id_to_device_qualifier.emplace(device_id, qualifier);
|
2022-09-16 21:02:30 +00:00
|
|
|
|
|
|
|
jstring j_qualifier = ToJString(env, qualifier.ToString());
|
|
|
|
env->CallVoidMethod(device->GetSensorEventListener(),
|
|
|
|
s_sensor_event_listener_set_device_qualifier, j_qualifier);
|
|
|
|
env->DeleteLocalRef(j_qualifier);
|
2021-12-31 15:14:15 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
static void AddSensorDevice(JNIEnv* env)
|
|
|
|
{
|
|
|
|
// Device sensors (accelerometer, etc.) aren't associated with any Android InputDevice.
|
|
|
|
// Create an otherwise empty Dolphin input device so that they have somewhere to live.
|
|
|
|
|
|
|
|
auto device = std::make_shared<AndroidDevice>(env, "Device Sensors");
|
|
|
|
|
|
|
|
if (device->Inputs().empty() && device->Outputs().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_controller_interface.AddDevice(device);
|
|
|
|
|
|
|
|
Core::DeviceQualifier qualifier;
|
|
|
|
qualifier.FromDevice(device.get());
|
|
|
|
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Added sensor device as {}", device->GetQualifiedName());
|
2022-09-16 21:02:30 +00:00
|
|
|
|
|
|
|
jstring j_qualifier = ToJString(env, qualifier.ToString());
|
|
|
|
env->CallVoidMethod(device->GetSensorEventListener(),
|
|
|
|
s_sensor_event_listener_set_device_qualifier, j_qualifier);
|
|
|
|
env->DeleteLocalRef(j_qualifier);
|
2022-09-16 20:03:03 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
void PopulateDevices()
|
2013-04-15 04:02:53 +00:00
|
|
|
{
|
2021-12-31 15:14:15 +00:00
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Android populating devices");
|
|
|
|
|
|
|
|
JNIEnv* env = IDCache::GetEnvForThread();
|
|
|
|
|
|
|
|
jintArray device_ids_array = reinterpret_cast<jintArray>(
|
|
|
|
env->CallStaticObjectMethod(s_input_device_class, s_input_device_get_device_ids));
|
|
|
|
int* device_ids = env->GetIntArrayElements(device_ids_array, nullptr);
|
|
|
|
jsize device_ids_count = env->GetArrayLength(device_ids_array);
|
|
|
|
for (jsize i = 0; i < device_ids_count; ++i)
|
|
|
|
AddDevice(env, device_ids[i]);
|
|
|
|
env->ReleaseIntArrayElements(device_ids_array, device_ids, JNI_ABORT);
|
|
|
|
env->DeleteLocalRef(device_ids_array);
|
2022-09-16 20:03:03 +00:00
|
|
|
|
|
|
|
AddSensorDevice(env);
|
2018-09-02 20:55:22 +00:00
|
|
|
}
|
2021-12-31 15:14:15 +00:00
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::Android
|
2022-01-01 17:31:50 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchKeyEvent(
|
|
|
|
JNIEnv* env, jclass, jobject key_event)
|
|
|
|
{
|
|
|
|
const jint action = env->CallIntMethod(key_event, s_key_event_get_action);
|
|
|
|
ControlState state;
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case AKEY_EVENT_ACTION_DOWN:
|
|
|
|
state = 1;
|
|
|
|
break;
|
|
|
|
case AKEY_EVENT_ACTION_UP:
|
|
|
|
state = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return JNI_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const jobject input_device = env->CallObjectMethod(key_event, s_input_event_get_device);
|
|
|
|
const jint device_id = env->CallIntMethod(input_device, s_input_device_get_id);
|
|
|
|
env->DeleteLocalRef(input_device);
|
|
|
|
const std::shared_ptr<ciface::Core::Device> device = FindDevice(device_id);
|
|
|
|
if (!device)
|
|
|
|
return JNI_FALSE;
|
|
|
|
|
|
|
|
const jint keycode = env->CallIntMethod(key_event, s_key_event_get_keycode);
|
|
|
|
const std::string input_name = ConstructKeyName(keycode);
|
|
|
|
|
|
|
|
ciface::Core::Device::Input* input = device->FindInput(input_name);
|
|
|
|
if (!input)
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Could not find input {} in device {}", input_name,
|
|
|
|
device->GetQualifiedName());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
auto casted_input = static_cast<ciface::Android::AndroidInput*>(input);
|
|
|
|
casted_input->SetState(state);
|
|
|
|
const Clock::time_point last_polled = casted_input->GetLastPolled();
|
2022-01-01 17:31:50 +00:00
|
|
|
|
|
|
|
DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Set {} of {} to {}", input_name, device->GetQualifiedName(),
|
|
|
|
state);
|
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
return last_polled >= Clock::now() - ACTIVE_INPUT_TIMEOUT;
|
2022-01-01 17:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchGenericMotionEvent(
|
|
|
|
JNIEnv* env, jclass, jobject motion_event)
|
|
|
|
{
|
|
|
|
const jobject input_device = env->CallObjectMethod(motion_event, s_input_event_get_device);
|
|
|
|
const jint device_id = env->CallIntMethod(input_device, s_input_device_get_id);
|
|
|
|
env->DeleteLocalRef(input_device);
|
|
|
|
const std::shared_ptr<ciface::Core::Device> device = FindDevice(device_id);
|
|
|
|
if (!device)
|
|
|
|
return JNI_FALSE;
|
|
|
|
|
|
|
|
const jint source = env->CallIntMethod(motion_event, s_motion_event_get_source);
|
|
|
|
const std::string axis_name_prefix = ConstructAxisNamePrefix(source);
|
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
Clock::time_point last_polled{};
|
|
|
|
|
2022-01-01 17:31:50 +00:00
|
|
|
for (ciface::Core::Device::Input* input : device->Inputs())
|
|
|
|
{
|
|
|
|
const std::string input_name = input->GetName();
|
|
|
|
if (input_name.starts_with(axis_name_prefix))
|
|
|
|
{
|
|
|
|
const std::string axis_id_str = input_name.substr(
|
|
|
|
axis_name_prefix.size(), input_name.size() - axis_name_prefix.size() - sizeof('+'));
|
|
|
|
|
|
|
|
int axis_id;
|
|
|
|
if (!TryParse(axis_id_str, &axis_id))
|
|
|
|
{
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Failed to parse \"{}\" from \"{}\" as axis ID",
|
|
|
|
axis_id_str, input_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
float value = env->CallFloatMethod(motion_event, s_motion_event_get_axis_value, axis_id);
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
|
|
|
|
auto casted_input = static_cast<ciface::Android::AndroidInput*>(input);
|
|
|
|
casted_input->SetState(value);
|
|
|
|
last_polled = std::max(last_polled, casted_input->GetLastPolled());
|
2022-01-01 17:31:50 +00:00
|
|
|
|
|
|
|
DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Set {} of {} to {}", input_name,
|
|
|
|
device->GetQualifiedName(), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
ControllerInterface/Android: Return whether input was handled
When Android presents an input event to an app, it wants the app to
return true or false depending on whether the app handled the event or
not. If the event wasn't handled by the app, it will be passed on to
the system, which may decide to take an action depending on what kind
of input event it is. For instance, if a B button press is passed on to
the system, it will be turned into a Back press. But if an R1 press is
passed on to the system, nothing in particular happens.
It's important that we get this return value right in Dolphin. For
instance, the user generally wouldn't want a B button press to open
the EmulationActivity menu, so B button presses usually shouldn't be
passed on to the system - but volume button presses usually should be
passed on to the system, since it would be hard to adjust the volume
otherwise. What ButtonManager did was to pass on input events that are
for a button which the user has not mapped, which I think makes sense.
But exactly how to implement that is more complicated in the new input
backend than in ButtonManager, because now we have a separation between
the input backend and the code that keeps track of the user's mappings.
What I'm going with in this commit is to treat an input as mapped if
it has been polled recently. In part I chose this because it seemed
like a simple way of implementing it that wouldn't cause too many
layering violations, but it also has two useful side effects:
1. If a controller is not being polled (e.g. GameCube controllers in
Wii games that don't use them), its mappings will not be considered.
2. Once sensor input is implemented in the Android input backend,
we will be able to use this "polled recently" tracking to power down
the sensors at times when the game is using a Wii Remote reporting
mode that doesn't include motion data. (Assuming that the sensor
inputs only are mapped to Wii Remote motion controls, that is.)
2022-03-06 10:51:59 +00:00
|
|
|
return last_polled >= Clock::now() - ACTIVE_INPUT_TIMEOUT;
|
2022-01-01 17:31:50 +00:00
|
|
|
}
|
2022-03-06 19:27:25 +00:00
|
|
|
|
2022-09-16 20:03:03 +00:00
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_dispatchSensorEvent(
|
2022-09-16 21:02:30 +00:00
|
|
|
JNIEnv* env, jclass, jstring j_device_qualifier, jstring j_axis_name, jfloat value)
|
2022-09-16 20:03:03 +00:00
|
|
|
{
|
2022-09-16 21:02:30 +00:00
|
|
|
ciface::Core::DeviceQualifier device_qualifier;
|
|
|
|
device_qualifier.FromString(GetJString(env, j_device_qualifier));
|
2022-09-16 20:03:03 +00:00
|
|
|
const std::shared_ptr<ciface::Core::Device> device =
|
2022-09-16 21:02:30 +00:00
|
|
|
g_controller_interface.FindDevice(device_qualifier);
|
2022-09-16 20:03:03 +00:00
|
|
|
if (!device)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const std::string axis_name = GetJString(env, j_axis_name);
|
|
|
|
|
|
|
|
for (ciface::Core::Device::Input* input : device->Inputs())
|
|
|
|
{
|
|
|
|
const std::string input_name = input->GetName();
|
|
|
|
if (input_name == axis_name)
|
|
|
|
{
|
|
|
|
auto casted_input = static_cast<ciface::Android::AndroidInput*>(input);
|
|
|
|
casted_input->SetState(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_enableSensorEvents(
|
|
|
|
JNIEnv* env, jclass, jobject j_sensor_event_requester)
|
|
|
|
{
|
2022-09-16 21:02:30 +00:00
|
|
|
for (std::shared_ptr<ciface::Core::Device>& device : g_controller_interface.GetAllDevices())
|
|
|
|
{
|
|
|
|
env->CallVoidMethod(
|
|
|
|
static_cast<ciface::Android::AndroidDevice*>(device.get())->GetSensorEventListener(),
|
|
|
|
s_sensor_event_listener_enable_sensor_events, j_sensor_event_requester);
|
|
|
|
}
|
2022-09-16 20:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_disableSensorEvents(
|
|
|
|
JNIEnv* env, jclass)
|
|
|
|
{
|
2022-09-16 21:02:30 +00:00
|
|
|
for (std::shared_ptr<ciface::Core::Device>& device : g_controller_interface.GetAllDevices())
|
|
|
|
{
|
|
|
|
env->CallVoidMethod(
|
|
|
|
static_cast<ciface::Android::AndroidDevice*>(device.get())->GetSensorEventListener(),
|
|
|
|
s_sensor_event_listener_disable_sensor_events);
|
|
|
|
}
|
2022-09-16 20:03:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 19:27:25 +00:00
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_dolphinemu_dolphinemu_features_input_model_ControllerInterface_refreshDevices(JNIEnv* env,
|
|
|
|
jclass)
|
|
|
|
{
|
|
|
|
g_controller_interface.RefreshDevices();
|
|
|
|
}
|
2022-01-01 17:31:50 +00:00
|
|
|
}
|