Skip to content

Commit 8100779

Browse files
DHrpcs3Megamouse
authored andcommitted
hid: add support of android devices
1 parent d766bae commit 8100779

12 files changed

+81
-26
lines changed

rpcs3/Input/ds3_pad_handler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void ds3_pad_handler::init_config(cfg_pad* cfg)
218218
cfg->from_default();
219219
}
220220

221-
void ds3_pad_handler::check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view wide_serial)
221+
void ds3_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view wide_serial)
222222
{
223223
if (!hidDevice)
224224
{
@@ -508,12 +508,16 @@ bool ds3_pad_handler::get_is_right_stick(const std::shared_ptr<PadDevice>& /*dev
508508
PadHandlerBase::connection ds3_pad_handler::update_connection(const std::shared_ptr<PadDevice>& device)
509509
{
510510
ds3_device* dev = static_cast<ds3_device*>(device.get());
511-
if (!dev || dev->path.empty())
511+
if (!dev || dev->path == hid_enumerated_device_default)
512512
return connection::disconnected;
513513

514514
if (dev->hidDevice == nullptr)
515515
{
516+
#ifdef ANDROID
517+
if (hid_device* hid_dev = hid_libusb_wrap_sys_device(dev->path, -1))
518+
#else
516519
if (hid_device* hid_dev = hid_open_path(dev->path.c_str()))
520+
#endif
517521
{
518522
if (hid_set_nonblocking(hid_dev, 1) == -1)
519523
{

rpcs3/Input/ds3_pad_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class ds3_pad_handler final : public hid_pad_handler<ds3_device>
145145
private:
146146
ds3_pad_handler::DataStatus get_data(ds3_device* ds3dev) override;
147147
int send_output_report(ds3_device* ds3dev) override;
148-
void check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view serial) override;
148+
void check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view serial) override;
149149

150150
bool get_is_left_trigger(const std::shared_ptr<PadDevice>& device, u64 keyCode) override;
151151
bool get_is_right_trigger(const std::shared_ptr<PadDevice>& device, u64 keyCode) override;

rpcs3/Input/ds4_pad_handler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ bool ds4_pad_handler::GetCalibrationData(DS4Device* ds4Dev) const
556556
return true;
557557
}
558558

559-
void ds4_pad_handler::check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view wide_serial)
559+
void ds4_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view wide_serial)
560560
{
561561
if (!hidDevice)
562562
{
@@ -831,13 +831,17 @@ bool ds4_pad_handler::get_is_touch_pad_motion(const std::shared_ptr<PadDevice>&
831831
PadHandlerBase::connection ds4_pad_handler::update_connection(const std::shared_ptr<PadDevice>& device)
832832
{
833833
DS4Device* dev = static_cast<DS4Device*>(device.get());
834-
if (!dev || dev->path.empty())
834+
if (!dev || dev->path == hid_enumerated_device_default)
835835
return connection::disconnected;
836836

837837
if (dev->hidDevice == nullptr)
838838
{
839839
// try to reconnect
840+
#ifdef ANDROID
841+
if (hid_device* hid_dev = hid_libusb_wrap_sys_device(dev->path, -1))
842+
#else
840843
if (hid_device* hid_dev = hid_open_path(dev->path.c_str()))
844+
#endif
841845
{
842846
if (hid_set_nonblocking(hid_dev, 1) == -1)
843847
{

rpcs3/Input/ds4_pad_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class ds4_pad_handler final : public hid_pad_handler<DS4Device>
184184
// Copies data into padData if status is NewData, otherwise buffer is untouched
185185
DataStatus get_data(DS4Device* ds4Device) override;
186186
int send_output_report(DS4Device* device) override;
187-
void check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view serial) override;
187+
void check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view serial) override;
188188

189189
bool get_is_left_trigger(const std::shared_ptr<PadDevice>& device, u64 keyCode) override;
190190
bool get_is_right_trigger(const std::shared_ptr<PadDevice>& device, u64 keyCode) override;

rpcs3/Input/dualsense_pad_handler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ dualsense_pad_handler::dualsense_pad_handler()
9999
m_thumb_threshold = thumb_max / 2;
100100
}
101101

102-
void dualsense_pad_handler::check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view wide_serial)
102+
void dualsense_pad_handler::check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view wide_serial)
103103
{
104104
if (!hidDevice)
105105
{
@@ -562,13 +562,17 @@ bool dualsense_pad_handler::get_is_touch_pad_motion(const std::shared_ptr<PadDev
562562
PadHandlerBase::connection dualsense_pad_handler::update_connection(const std::shared_ptr<PadDevice>& device)
563563
{
564564
DualSenseDevice* dev = static_cast<DualSenseDevice*>(device.get());
565-
if (!dev || dev->path.empty())
565+
if (!dev || dev->path == hid_enumerated_device_default)
566566
return connection::disconnected;
567567

568568
if (dev->hidDevice == nullptr)
569569
{
570570
// try to reconnect
571+
#ifdef ANDROID
572+
if (hid_device* hid_dev = hid_libusb_wrap_sys_device(dev->path, -1))
573+
#else
571574
if (hid_device* hid_dev = hid_open_path(dev->path.c_str()))
575+
#endif
572576
{
573577
if (hid_set_nonblocking(hid_dev, 1) == -1)
574578
{

rpcs3/Input/dualsense_pad_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class dualsense_pad_handler final : public hid_pad_handler<DualSenseDevice>
245245
bool get_calibration_data(DualSenseDevice* dev) const;
246246

247247
DataStatus get_data(DualSenseDevice* device) override;
248-
void check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view wide_serial) override;
248+
void check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view wide_serial) override;
249249
int send_output_report(DualSenseDevice* device) override;
250250

251251
bool get_is_left_trigger(const std::shared_ptr<PadDevice>& device, u64 keyCode) override;

rpcs3/Input/hid_pad_handler.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
LOG_CHANNEL(hid_log, "HID");
2020

21+
#ifdef ANDROID
22+
std::vector<int> g_android_usb_devices;
23+
std::mutex g_android_usb_devices_mutex;
24+
#endif
25+
2126
struct hid_instance
2227
{
2328
public:
@@ -56,9 +61,9 @@ struct hid_instance
5661

5762
hid_log.notice("Initializing HIDAPI ...");
5863

59-
if (hid_init() != 0)
64+
if (int errorCode = hid_init(); errorCode != 0)
6065
{
61-
hid_log.fatal("hid_init error");
66+
hid_log.fatal("hid_init error %d", errorCode);
6267
return false;
6368
}
6469

@@ -179,9 +184,18 @@ template <class Device>
179184
void hid_pad_handler<Device>::enumerate_devices()
180185
{
181186
Timer timer;
182-
std::set<std::string> device_paths;
183-
std::map<std::string, std::wstring> serials;
187+
std::set<hid_enumerated_device_type> device_paths;
188+
std::map<hid_enumerated_device_type, std::wstring> serials;
184189

190+
#ifdef ANDROID
191+
{
192+
std::lock_guard lock(g_android_usb_devices_mutex);
193+
for (auto device : g_android_usb_devices)
194+
{
195+
device_paths.insert(device);
196+
}
197+
}
198+
#else
185199
for (const auto& [vid, pid] : m_ids)
186200
{
187201
hid_device_info* dev_info = hid_enumerate(vid, pid);
@@ -209,6 +223,7 @@ void hid_pad_handler<Device>::enumerate_devices()
209223
}
210224
hid_free_enumeration(head);
211225
}
226+
#endif
212227
hid_log.notice("%s enumeration found %d devices (%f ms)", m_type, device_paths.size(), timer.GetElapsedTimeInMilliSec());
213228

214229
std::lock_guard lock(m_enumeration_mutex);
@@ -254,7 +269,7 @@ void hid_pad_handler<Device>::update_devices()
254269
// Scrap devices that are not in the new list
255270
for (auto& controller : m_controllers)
256271
{
257-
if (controller.second && !controller.second->path.empty() && !m_new_enumerated_devices.contains(controller.second->path))
272+
if (controller.second && controller.second->path != hid_enumerated_device_default && !m_new_enumerated_devices.contains(controller.second->path))
258273
{
259274
controller.second->close();
260275
cfg_pad* config = controller.second->config;
@@ -284,7 +299,11 @@ void hid_pad_handler<Device>::update_devices()
284299
}
285300
#endif
286301

287-
if (hid_device* dev = hid_open_path(path.c_str()))
302+
#ifdef ANDROID
303+
if (hid_device* dev = hid_libusb_wrap_sys_device(path, -1))
304+
#else
305+
if (hid_device* dev = hid_open_path(path))
306+
#endif
288307
{
289308
if (const hid_device_info* info = hid_get_device_info(dev))
290309
{

rpcs3/Input/hid_pad_handler.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@
66

77
#include "hidapi.h"
88

9+
#ifdef ANDROID
10+
#include "hidapi_libusb.h"
11+
#endif
12+
913
#include <mutex>
1014

15+
#ifdef ANDROID
16+
using hid_enumerated_device_type = int;
17+
using hid_enumerated_device_view = int;
18+
inline constexpr auto hid_enumerated_device_default = -1;
19+
extern std::vector<int> g_android_usb_devices;
20+
extern std::mutex g_android_usb_devices_mutex;
21+
#else
22+
using hid_enumerated_device_type = std::string;
23+
using hid_enumerated_device_view = std::string_view;
24+
inline constexpr auto hid_enumerated_device_default = std::string();
25+
#endif
26+
1127
struct CalibData
1228
{
1329
s16 bias = 0;
@@ -38,7 +54,7 @@ class HidDevice : public PadDevice
3854
#ifdef _WIN32
3955
hid_device* bt_device{nullptr}; // Used in ps move handler
4056
#endif
41-
std::string path;
57+
hid_enumerated_device_type path = hid_enumerated_device_default;
4258
u8 led_delay_on{0};
4359
u8 led_delay_off{0};
4460
u8 battery_level{0};
@@ -78,17 +94,17 @@ class hid_pad_handler : public PadHandlerBase
7894
// pseudo 'controller id' to keep track of unique controllers
7995
std::map<std::string, std::shared_ptr<Device>> m_controllers;
8096

81-
std::set<std::string> m_last_enumerated_devices;
82-
std::set<std::string> m_new_enumerated_devices;
83-
std::map<std::string, std::wstring> m_enumerated_serials;
97+
std::set<hid_enumerated_device_type> m_last_enumerated_devices;
98+
std::set<hid_enumerated_device_type> m_new_enumerated_devices;
99+
std::map<hid_enumerated_device_type, std::wstring> m_enumerated_serials;
84100
std::mutex m_enumeration_mutex;
85101
std::unique_ptr<named_thread<std::function<void()>>> m_enumeration_thread;
86102

87103
void enumerate_devices();
88104
void update_devices();
89105
std::shared_ptr<Device> get_hid_device(const std::string& padId);
90106

91-
virtual void check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view serial) = 0;
107+
virtual void check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view serial) = 0;
92108
virtual int send_output_report(Device* device) = 0;
93109
virtual DataStatus get_data(Device* device) = 0;
94110

rpcs3/Input/ps_move_handler.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ hid_device* ps_move_handler::connect_move_device(ps_move_device* device, std::st
265265
}
266266
#endif
267267

268-
void ps_move_handler::check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view wide_serial)
268+
void ps_move_handler::check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view wide_serial)
269269
{
270270
#ifndef _WIN32
271271
if (!hidDevice)
@@ -422,7 +422,7 @@ ps_move_handler::DataStatus ps_move_handler::get_data(ps_move_device* device)
422422
PadHandlerBase::connection ps_move_handler::update_connection(const std::shared_ptr<PadDevice>& device)
423423
{
424424
ps_move_device* move_device = static_cast<ps_move_device*>(device.get());
425-
if (!move_device || move_device->path.empty())
425+
if (!move_device || move_device->path == hid_enumerated_device_default)
426426
return connection::disconnected;
427427

428428
if (move_device->hidDevice == nullptr)
@@ -433,8 +433,12 @@ PadHandlerBase::connection ps_move_handler::update_connection(const std::shared_
433433
{
434434
move_device->hidDevice = dev;
435435
}
436+
#else
437+
#ifdef ANDROID
438+
if (hid_device* dev = hid_libusb_wrap_sys_device(move_device->path, -1))
436439
#else
437440
if (hid_device* dev = hid_open_path(move_device->path.c_str()))
441+
#endif
438442
{
439443
if (hid_set_nonblocking(dev, 1) == -1)
440444
{

rpcs3/Input/ps_move_handler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class ps_move_handler final : public hid_pad_handler<ps_move_device>
189189
#endif
190190

191191
DataStatus get_data(ps_move_device* device) override;
192-
void check_add_device(hid_device* hidDevice, std::string_view path, std::wstring_view wide_serial) override;
192+
void check_add_device(hid_device* hidDevice, hid_enumerated_device_view path, std::wstring_view wide_serial) override;
193193
int send_output_report(ps_move_device* device) override;
194194

195195
bool get_is_left_trigger(const std::shared_ptr<PadDevice>& device, u64 keyCode) override;

0 commit comments

Comments
 (0)