2015-08-18 22:43:30 +00:00
|
|
|
#include "boo/inputdev/IHIDListener.hpp"
|
|
|
|
#include "boo/inputdev/DeviceFinder.hpp"
|
2015-04-29 07:56:16 +00:00
|
|
|
#include <libudev.h>
|
|
|
|
#include <string.h>
|
2015-08-18 18:00:24 +00:00
|
|
|
#include <stdio.h>
|
2015-05-12 09:38:37 +00:00
|
|
|
#include <signal.h>
|
2015-04-29 07:56:16 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2015-04-29 10:24:39 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
|
|
|
|
2015-11-13 02:11:32 +00:00
|
|
|
static udev* UDEV_INST = nullptr;
|
2015-04-30 07:01:55 +00:00
|
|
|
udev* GetUdev()
|
2015-04-29 07:56:16 +00:00
|
|
|
{
|
|
|
|
if (!UDEV_INST)
|
|
|
|
UDEV_INST = udev_new();
|
|
|
|
return UDEV_INST;
|
|
|
|
}
|
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
class HIDListenerUdev final : public IHIDListener
|
2015-04-29 07:56:16 +00:00
|
|
|
{
|
2015-08-18 19:40:26 +00:00
|
|
|
DeviceFinder& m_finder;
|
2015-04-29 07:56:16 +00:00
|
|
|
|
|
|
|
udev_monitor* m_udevMon;
|
2015-11-13 02:11:32 +00:00
|
|
|
std::thread m_udevThread;
|
2015-04-29 07:56:16 +00:00
|
|
|
bool m_udevRunning;
|
|
|
|
bool m_scanningEnabled;
|
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
static void deviceConnected(HIDListenerUdev* listener,
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_device* device)
|
|
|
|
{
|
|
|
|
if (!listener->m_scanningEnabled)
|
|
|
|
return;
|
|
|
|
|
2015-04-30 23:17:46 +00:00
|
|
|
/* Filter to USB/BT */
|
|
|
|
const char* dt = udev_device_get_devtype(device);
|
2015-11-21 01:12:22 +00:00
|
|
|
DeviceToken::DeviceType type;
|
2015-04-30 23:17:46 +00:00
|
|
|
if (!strcmp(dt, "usb_device"))
|
2015-11-21 01:12:22 +00:00
|
|
|
type = DeviceToken::DeviceType::USB;
|
2015-04-30 23:17:46 +00:00
|
|
|
else if (!strcmp(dt, "bluetooth_device"))
|
2015-11-21 01:12:22 +00:00
|
|
|
type = DeviceToken::DeviceType::Bluetooth;
|
2015-04-30 23:17:46 +00:00
|
|
|
else
|
2015-04-29 07:56:16 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-30 23:17:46 +00:00
|
|
|
/* Prevent redundant registration */
|
|
|
|
const char* devPath = udev_device_get_syspath(device);
|
|
|
|
if (listener->m_finder._hasToken(devPath))
|
2015-04-29 07:56:16 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int vid = 0, pid = 0;
|
2015-04-30 23:17:46 +00:00
|
|
|
udev_list_entry* attrs = udev_device_get_properties_list_entry(device);
|
|
|
|
#if 0
|
2015-11-13 02:11:32 +00:00
|
|
|
udev_list_entry* att = nullptr;
|
2015-04-30 23:17:46 +00:00
|
|
|
udev_list_entry_foreach(att, attrs)
|
|
|
|
{
|
|
|
|
const char* name = udev_list_entry_get_name(att);
|
|
|
|
const char* val = udev_list_entry_get_value(att);
|
|
|
|
fprintf(stderr, "%s %s\n", name, val);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n\n");
|
|
|
|
#endif
|
2015-04-29 07:56:16 +00:00
|
|
|
|
|
|
|
udev_list_entry* vide = udev_list_entry_get_by_name(attrs, "ID_VENDOR_ID");
|
|
|
|
if (vide)
|
2015-11-13 02:11:32 +00:00
|
|
|
vid = strtol(udev_list_entry_get_value(vide), nullptr, 16);
|
2015-04-29 07:56:16 +00:00
|
|
|
|
|
|
|
udev_list_entry* pide = udev_list_entry_get_by_name(attrs, "ID_MODEL_ID");
|
|
|
|
if (pide)
|
2015-11-13 02:11:32 +00:00
|
|
|
pid = strtol(udev_list_entry_get_value(pide), nullptr, 16);
|
2015-04-29 07:56:16 +00:00
|
|
|
|
2015-11-13 02:11:32 +00:00
|
|
|
const char* manuf = nullptr;
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_list_entry* manufe = udev_list_entry_get_by_name(attrs, "ID_VENDOR");
|
|
|
|
if (manufe)
|
|
|
|
manuf = udev_list_entry_get_value(manufe);
|
|
|
|
|
2015-11-13 02:11:32 +00:00
|
|
|
const char* product = nullptr;
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_list_entry* producte = udev_list_entry_get_by_name(attrs, "ID_MODEL");
|
|
|
|
if (producte)
|
|
|
|
product = udev_list_entry_get_value(producte);
|
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
if (!listener->m_finder._insertToken(DeviceToken(type, vid, pid, manuf, product, devPath)))
|
2015-04-30 23:17:46 +00:00
|
|
|
{
|
|
|
|
/* Matched-insertion failed; see if generic HID interface is available */
|
2015-11-13 02:11:32 +00:00
|
|
|
udev_list_entry* devInterfaces = nullptr;
|
2015-11-21 01:12:22 +00:00
|
|
|
if (type == DeviceToken::DeviceType::USB)
|
2015-04-30 23:17:46 +00:00
|
|
|
devInterfaces = udev_list_entry_get_by_name(attrs, "ID_USB_INTERFACES");
|
2015-11-21 01:12:22 +00:00
|
|
|
else if (type == DeviceToken::DeviceType::Bluetooth)
|
2015-04-30 23:17:46 +00:00
|
|
|
devInterfaces = udev_list_entry_get_by_name(attrs, "ID_BLUETOOTH_INTERFACES");
|
|
|
|
if (devInterfaces)
|
|
|
|
{
|
|
|
|
const char* interfacesStr = udev_list_entry_get_value(devInterfaces);
|
|
|
|
if (strstr(interfacesStr, ":030104") || /* HID / GenericDesktop / Joystick */
|
2015-10-31 04:28:21 +00:00
|
|
|
strstr(interfacesStr, ":030105")) /* HID / GenericDesktop / Gamepad */
|
2015-04-30 23:17:46 +00:00
|
|
|
{
|
|
|
|
udev_enumerate* hidEnum = udev_enumerate_new(UDEV_INST);
|
|
|
|
udev_enumerate_add_match_parent(hidEnum, device);
|
|
|
|
udev_enumerate_add_match_subsystem(hidEnum, "hid");
|
|
|
|
udev_enumerate_scan_devices(hidEnum);
|
|
|
|
udev_list_entry* hidEnt = udev_enumerate_get_list_entry(hidEnum);
|
|
|
|
if (hidEnt)
|
|
|
|
{
|
|
|
|
const char* hidPath = udev_list_entry_get_name(hidEnt);
|
|
|
|
if (!listener->m_finder._hasToken(hidPath))
|
2015-11-21 01:12:22 +00:00
|
|
|
listener->m_finder._insertToken(DeviceToken(DeviceToken::DeviceType::GenericHID,
|
2015-04-30 23:17:46 +00:00
|
|
|
vid, pid, manuf, product, hidPath));
|
|
|
|
}
|
|
|
|
udev_enumerate_unref(hidEnum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-29 07:56:16 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
static void deviceDisconnected(HIDListenerUdev* listener,
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_device* device)
|
|
|
|
{
|
|
|
|
const char* devPath = udev_device_get_syspath(device);
|
|
|
|
listener->m_finder._removeToken(devPath);
|
|
|
|
}
|
|
|
|
|
2015-11-13 02:11:32 +00:00
|
|
|
/* Empty handler for SIGTERM */
|
2015-11-16 20:43:27 +00:00
|
|
|
static void _sigterm(int) {}
|
2015-11-13 02:11:32 +00:00
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
static void _udevProc(HIDListenerUdev* listener)
|
2015-04-29 07:56:16 +00:00
|
|
|
{
|
2015-11-13 02:11:32 +00:00
|
|
|
/* SIGTERM will be used to terminate thread */
|
|
|
|
struct sigaction s;
|
|
|
|
s.sa_handler = _sigterm;
|
|
|
|
sigemptyset(&s.sa_mask);
|
|
|
|
s.sa_flags = 0;
|
|
|
|
sigaction(SIGTERM, &s, nullptr);
|
|
|
|
|
2015-11-17 20:25:17 +00:00
|
|
|
sigset_t waitmask, origmask;
|
|
|
|
sigemptyset(&waitmask);
|
|
|
|
sigaddset(&waitmask, SIGTERM);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &waitmask, &origmask);
|
|
|
|
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_monitor_enable_receiving(listener->m_udevMon);
|
|
|
|
int fd = udev_monitor_get_fd(listener->m_udevMon);
|
|
|
|
while (listener->m_udevRunning)
|
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
2015-11-17 20:25:17 +00:00
|
|
|
if (pselect(fd+1, &fds, nullptr, nullptr, nullptr, &origmask) < 0)
|
2015-11-13 02:11:32 +00:00
|
|
|
{
|
|
|
|
/* SIGTERM handled here */
|
|
|
|
if (errno == EINTR)
|
|
|
|
break;
|
|
|
|
}
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_device* dev = udev_monitor_receive_device(listener->m_udevMon);
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
const char* action = udev_device_get_action(dev);
|
|
|
|
if (!strcmp(action, "add"))
|
|
|
|
deviceConnected(listener, dev);
|
|
|
|
else if (!strcmp(action, "remove"))
|
|
|
|
deviceDisconnected(listener, dev);
|
|
|
|
udev_device_unref(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-08-18 19:40:26 +00:00
|
|
|
HIDListenerUdev(DeviceFinder& finder)
|
2015-04-29 07:56:16 +00:00
|
|
|
: m_finder(finder)
|
|
|
|
{
|
2015-04-29 10:24:39 +00:00
|
|
|
/* Setup hotplug events */
|
2015-04-30 07:01:55 +00:00
|
|
|
m_udevMon = udev_monitor_new_from_netlink(GetUdev(), "udev");
|
2015-04-29 07:56:16 +00:00
|
|
|
if (!m_udevMon)
|
2015-08-18 18:00:24 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "unable to init udev_monitor");
|
|
|
|
abort();
|
|
|
|
}
|
2015-04-30 23:17:46 +00:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(m_udevMon, "usb", "usb_device");
|
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(m_udevMon, "bluetooth", "bluetooth_device");
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_monitor_filter_update(m_udevMon);
|
|
|
|
|
|
|
|
/* Initial HID Device Add */
|
|
|
|
m_scanningEnabled = true;
|
|
|
|
scanNow();
|
|
|
|
m_scanningEnabled = false;
|
|
|
|
|
|
|
|
/* Start hotplug thread */
|
|
|
|
m_udevRunning = true;
|
2015-11-13 02:11:32 +00:00
|
|
|
m_udevThread = std::thread(_udevProc, this);
|
2015-04-29 07:56:16 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
~HIDListenerUdev()
|
2015-04-29 07:56:16 +00:00
|
|
|
{
|
|
|
|
m_udevRunning = false;
|
2015-11-13 02:11:32 +00:00
|
|
|
pthread_kill(m_udevThread.native_handle(), SIGTERM);
|
|
|
|
m_udevThread.join();
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_monitor_unref(m_udevMon);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Automatic device scanning */
|
|
|
|
bool startScanning()
|
|
|
|
{
|
|
|
|
m_scanningEnabled = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool stopScanning()
|
|
|
|
{
|
|
|
|
m_scanningEnabled = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Manual device scanning */
|
|
|
|
bool scanNow()
|
|
|
|
{
|
2015-04-30 07:01:55 +00:00
|
|
|
udev_enumerate* uenum = udev_enumerate_new(GetUdev());
|
2015-04-30 23:17:46 +00:00
|
|
|
udev_enumerate_add_match_subsystem(uenum, "usb");
|
|
|
|
udev_enumerate_add_match_property(uenum, "DEVTYPE", "usb_device");
|
|
|
|
udev_enumerate_add_match_subsystem(uenum, "bluetooth");
|
|
|
|
udev_enumerate_add_match_property(uenum, "DEVTYPE", "bluetooth_device");
|
2015-04-29 07:56:16 +00:00
|
|
|
udev_enumerate_scan_devices(uenum);
|
|
|
|
udev_list_entry* uenumList = udev_enumerate_get_list_entry(uenum);
|
|
|
|
udev_list_entry* uenumItem;
|
|
|
|
udev_list_entry_foreach(uenumItem, uenumList)
|
|
|
|
{
|
|
|
|
const char* devPath = udev_list_entry_get_name(uenumItem);
|
|
|
|
udev_device* dev = udev_device_new_from_syspath(UDEV_INST, devPath);
|
|
|
|
if (dev)
|
|
|
|
deviceConnected(this, dev);
|
|
|
|
udev_device_unref(dev);
|
|
|
|
}
|
|
|
|
udev_enumerate_unref(uenum);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-08-18 19:40:26 +00:00
|
|
|
IHIDListener* IHIDListenerNew(DeviceFinder& finder)
|
2015-04-29 07:56:16 +00:00
|
|
|
{
|
2015-08-18 19:40:26 +00:00
|
|
|
return new HIDListenerUdev(finder);
|
2015-04-29 07:56:16 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 10:24:39 +00:00
|
|
|
}
|