2015-06-29 00:17:35 +00:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2015-06-29 00:17:35 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "InputCommon/ControllerInterface/evdev/evdev.h"
|
|
|
|
|
2017-12-25 23:38:44 +00:00
|
|
|
#include <algorithm>
|
2017-09-09 19:52:35 +00:00
|
|
|
#include <cstring>
|
2015-08-04 17:59:34 +00:00
|
|
|
#include <map>
|
2016-07-14 15:50:35 +00:00
|
|
|
#include <memory>
|
2017-09-09 19:52:35 +00:00
|
|
|
#include <string>
|
2015-06-29 00:17:35 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libudev.h>
|
2016-07-14 15:50:35 +00:00
|
|
|
#include <sys/eventfd.h>
|
2019-11-16 21:40:43 +00:00
|
|
|
#include <unistd.h>
|
2016-07-14 15:50:35 +00:00
|
|
|
|
2015-09-26 20:39:47 +00:00
|
|
|
#include "Common/Assert.h"
|
2016-07-14 15:50:35 +00:00
|
|
|
#include "Common/Flag.h"
|
2015-06-29 00:17:35 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-07-11 12:00:09 +00:00
|
|
|
#include "Common/MathUtil.h"
|
2019-03-17 17:51:50 +00:00
|
|
|
#include "Common/ScopeGuard.h"
|
2016-07-01 03:05:24 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2016-07-14 15:50:35 +00:00
|
|
|
#include "Common/Thread.h"
|
2016-06-12 15:08:04 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2015-06-29 00:17:35 +00:00
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
namespace ciface::evdev
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-11-09 00:23:22 +00:00
|
|
|
class Input : public Core::Device::Input
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Input(u16 code, libevdev* dev) : m_code(code), m_dev(dev) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const u16 m_code;
|
|
|
|
libevdev* const m_dev;
|
|
|
|
};
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
class Button : public Input
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Button(u8 index, u16 code, libevdev* dev) : Input(code, dev), m_index(index) {}
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
ControlState GetState() const final override
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
int value = 0;
|
|
|
|
libevdev_fetch_event_value(m_dev, EV_KEY, m_code, &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::optional<std::string> GetEventCodeName() const
|
|
|
|
{
|
|
|
|
if (const char* code_name = libevdev_event_code_get_name(EV_KEY, m_code))
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
2022-07-19 22:13:26 +00:00
|
|
|
const auto name = StripWhitespace(code_name);
|
2019-11-16 21:40:43 +00:00
|
|
|
|
|
|
|
for (auto remove_prefix : {"BTN_", "KEY_"})
|
|
|
|
{
|
|
|
|
if (name.find(remove_prefix) == 0)
|
|
|
|
return std::string(name.substr(std::strlen(remove_prefix)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::string(name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
2019-11-09 00:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
std::string GetIndexedName() const { return "Button " + std::to_string(m_index); }
|
|
|
|
|
|
|
|
const u8 m_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NumberedButton final : public Button
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Button::Button;
|
|
|
|
|
|
|
|
std::string GetName() const override { return GetIndexedName(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class NamedButton final : public Button
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Button::Button;
|
|
|
|
|
|
|
|
bool IsMatchingName(std::string_view name) const final override
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
// Match either the "START" naming provided by evdev or the "Button 0"-like naming.
|
|
|
|
return name == GetEventCodeName() || name == GetIndexedName();
|
2019-11-09 00:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
std::string GetName() const override { return GetEventCodeName().value_or(GetIndexedName()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class NamedButtonWithNoBackwardsCompat final : public Button
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Button::Button;
|
|
|
|
|
|
|
|
std::string GetName() const override { return GetEventCodeName().value_or(GetIndexedName()); }
|
2019-11-09 00:23:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AnalogInput : public Input
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Input::Input;
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
ControlState GetState() const final override
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
|
|
|
int value = 0;
|
|
|
|
libevdev_fetch_event_value(m_dev, EV_ABS, m_code, &value);
|
|
|
|
|
2019-11-10 16:04:13 +00:00
|
|
|
return (value - m_base) / m_range;
|
2019-11-09 00:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2019-11-10 16:04:13 +00:00
|
|
|
ControlState m_range;
|
2019-11-09 00:23:22 +00:00
|
|
|
int m_base;
|
|
|
|
};
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
class Axis : public AnalogInput
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Axis(u8 index, u16 code, bool upper, libevdev* dev) : AnalogInput(code, dev), m_index(index)
|
|
|
|
{
|
|
|
|
const int min = libevdev_get_abs_minimum(m_dev, m_code);
|
|
|
|
const int max = libevdev_get_abs_maximum(m_dev, m_code);
|
|
|
|
|
|
|
|
m_base = (max + min) / 2;
|
|
|
|
m_range = (upper ? max : min) - m_base;
|
|
|
|
}
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
std::string GetName() const override { return GetIndexedName(); }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::string GetIndexedName() const
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
|
|
|
return "Axis " + std::to_string(m_index) + (m_range < 0 ? '-' : '+');
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const u8 m_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MotionDataInput final : public AnalogInput
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MotionDataInput(u16 code, ControlState resolution_scale, libevdev* dev) : AnalogInput(code, dev)
|
|
|
|
{
|
|
|
|
auto* const info = libevdev_get_abs_info(m_dev, m_code);
|
|
|
|
|
|
|
|
// The average of the minimum and maximum value. (neutral value)
|
|
|
|
m_base = (info->maximum + info->minimum) / 2;
|
|
|
|
|
|
|
|
m_range = info->resolution / resolution_scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetName() const override
|
|
|
|
{
|
|
|
|
// Unfortunately there doesn't seem to be a "standard" orientation
|
|
|
|
// so we can't use "Accel Up"-like names.
|
|
|
|
constexpr std::array<const char*, 6> motion_data_names = {{
|
|
|
|
"Accel X",
|
|
|
|
"Accel Y",
|
|
|
|
"Accel Z",
|
|
|
|
"Gyro X",
|
|
|
|
"Gyro Y",
|
|
|
|
"Gyro Z",
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Our name array relies on sane axis codes from 0 to 5.
|
|
|
|
static_assert(ABS_X == 0, "evdev axis value sanity check");
|
|
|
|
static_assert(ABS_RX == 3, "evdev axis value sanity check");
|
|
|
|
|
|
|
|
return std::string(motion_data_names[m_code]) + (m_range < 0 ? '-' : '+');
|
|
|
|
}
|
|
|
|
|
2020-02-22 16:27:43 +00:00
|
|
|
bool IsDetectable() const override { return false; }
|
2019-11-09 00:23:22 +00:00
|
|
|
};
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
class CursorInput final : public Axis
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Axis::Axis;
|
|
|
|
|
|
|
|
std::string GetName() const final override
|
|
|
|
{
|
|
|
|
// "Cursor X-" naming.
|
|
|
|
return std::string("Cursor ") + char('X' + m_code) + (m_range < 0 ? '-' : '+');
|
|
|
|
}
|
|
|
|
|
2020-02-22 16:27:43 +00:00
|
|
|
bool IsDetectable() const override { return false; }
|
2019-11-16 21:40:43 +00:00
|
|
|
};
|
|
|
|
|
2016-07-14 15:50:35 +00:00
|
|
|
static std::thread s_hotplug_thread;
|
|
|
|
static Common::Flag s_hotplug_thread_running;
|
|
|
|
static int s_wakeup_eventfd;
|
|
|
|
|
|
|
|
// There is no easy way to get the device name from only a dev node
|
|
|
|
// during a device removed event, since libevdev can't work on removed devices;
|
|
|
|
// sysfs is not stable, so this is probably the easiest way to get a name for a node.
|
2021-05-15 09:11:27 +00:00
|
|
|
// This can, and will be modified by different thread, possibly concurrently,
|
|
|
|
// as devices can be destroyed by any thread at any time. As of now it's protected
|
|
|
|
// by ControllerInterface::m_devices_population_mutex.
|
2019-11-16 21:40:43 +00:00
|
|
|
static std::map<std::string, std::weak_ptr<evdevDevice>> s_devnode_objects;
|
|
|
|
|
2020-06-01 20:59:45 +00:00
|
|
|
static std::shared_ptr<evdevDevice>
|
|
|
|
FindDeviceWithUniqueIDAndPhysicalLocation(const char* unique_id, const char* physical_location)
|
2019-11-16 21:40:43 +00:00
|
|
|
{
|
2020-06-01 20:59:45 +00:00
|
|
|
if (!unique_id || !physical_location)
|
2019-11-16 21:40:43 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
for (auto& [node, dev] : s_devnode_objects)
|
|
|
|
{
|
|
|
|
if (const auto device = dev.lock())
|
|
|
|
{
|
|
|
|
const auto* dev_uniq = device->GetUniqueID();
|
2020-06-01 20:59:45 +00:00
|
|
|
const auto* dev_phys = device->GetPhysicalLocation();
|
2019-11-16 21:40:43 +00:00
|
|
|
|
2020-06-01 20:59:45 +00:00
|
|
|
if (dev_uniq && dev_phys && std::strcmp(dev_uniq, unique_id) == 0 &&
|
|
|
|
std::strcmp(dev_phys, physical_location) == 0)
|
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
return device;
|
2020-06-01 20:59:45 +00:00
|
|
|
}
|
2019-11-16 21:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:59:19 +00:00
|
|
|
static void AddDeviceNode(const char* devnode)
|
2019-11-16 21:40:43 +00:00
|
|
|
{
|
|
|
|
// Unfortunately udev gives us no way to filter out the non event device interfaces.
|
|
|
|
// So we open it and see if it works with evdev ioctls or not.
|
|
|
|
|
|
|
|
// The device file will be read on one of the main threads, so we open in non-blocking mode.
|
|
|
|
const int fd = open(devnode, O_RDWR | O_NONBLOCK);
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
libevdev* dev = nullptr;
|
|
|
|
if (libevdev_new_from_fd(fd, &dev) != 0)
|
|
|
|
{
|
|
|
|
// This usually fails because the device node isn't an evdev device, such as /dev/input/js0
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-17 15:09:47 +00:00
|
|
|
const auto uniq = libevdev_get_uniq(dev);
|
2020-06-01 20:59:45 +00:00
|
|
|
const auto phys = libevdev_get_phys(dev);
|
|
|
|
auto evdev_device = FindDeviceWithUniqueIDAndPhysicalLocation(uniq, phys);
|
2019-11-16 21:40:43 +00:00
|
|
|
if (evdev_device)
|
|
|
|
{
|
2021-05-04 20:37:06 +00:00
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE,
|
2020-10-22 21:49:29 +00:00
|
|
|
"evdev combining devices with unique id: {}, physical location: {}", uniq, phys);
|
2019-11-17 15:09:47 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
evdev_device->AddNode(devnode, fd, dev);
|
|
|
|
|
2019-11-17 15:33:26 +00:00
|
|
|
// Remove and re-add device as naming and inputs may have changed.
|
|
|
|
// This will also give it the correct index and invoke device change callbacks.
|
2021-05-15 09:11:27 +00:00
|
|
|
// Make sure to force the device removal immediately (as they are shared ptrs and
|
|
|
|
// they could be kept alive, preventing us from re-creating the device)
|
|
|
|
g_controller_interface.RemoveDevice(
|
|
|
|
[&evdev_device](const auto* device) {
|
|
|
|
return static_cast<const evdevDevice*>(device) == evdev_device.get();
|
|
|
|
},
|
|
|
|
true);
|
2019-11-17 15:33:26 +00:00
|
|
|
|
|
|
|
g_controller_interface.AddDevice(evdev_device);
|
2019-11-16 21:40:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
evdev_device = std::make_shared<evdevDevice>();
|
|
|
|
|
|
|
|
const bool was_interesting = evdev_device->AddNode(devnode, fd, dev);
|
|
|
|
|
|
|
|
if (was_interesting)
|
|
|
|
g_controller_interface.AddDevice(evdev_device);
|
|
|
|
}
|
|
|
|
|
2021-05-15 09:11:27 +00:00
|
|
|
// If the devices failed to be added to g_controller_interface, it will be added here but then
|
|
|
|
// immediately removed in its destructor due to the shared ptr not having any references left
|
2019-11-16 21:40:43 +00:00
|
|
|
s_devnode_objects.emplace(devnode, std::move(evdev_device));
|
|
|
|
}
|
2016-07-14 15:50:35 +00:00
|
|
|
|
|
|
|
static void HotplugThreadFunc()
|
|
|
|
{
|
|
|
|
Common::SetCurrentThreadName("evdev Hotplug Thread");
|
2021-05-04 20:37:06 +00:00
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "evdev hotplug thread started");
|
2016-07-14 15:50:35 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
udev* const udev = udev_new();
|
2019-03-17 17:51:50 +00:00
|
|
|
Common::ScopeGuard udev_guard([udev] { udev_unref(udev); });
|
|
|
|
|
2021-11-11 02:34:28 +00:00
|
|
|
ASSERT_MSG(CONTROLLERINTERFACE, udev != nullptr, "Couldn't initialize libudev.");
|
2016-07-14 15:50:35 +00:00
|
|
|
|
|
|
|
// Set up monitoring
|
2019-01-08 15:28:24 +00:00
|
|
|
udev_monitor* const monitor = udev_monitor_new_from_netlink(udev, "udev");
|
2019-03-17 17:51:50 +00:00
|
|
|
Common::ScopeGuard monitor_guard([monitor] { udev_monitor_unref(monitor); });
|
|
|
|
|
2016-07-14 15:50:35 +00:00
|
|
|
udev_monitor_filter_add_match_subsystem_devtype(monitor, "input", nullptr);
|
|
|
|
udev_monitor_enable_receiving(monitor);
|
|
|
|
const int monitor_fd = udev_monitor_get_fd(monitor);
|
|
|
|
|
|
|
|
while (s_hotplug_thread_running.IsSet())
|
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(monitor_fd, &fds);
|
|
|
|
FD_SET(s_wakeup_eventfd, &fds);
|
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
const int ret =
|
|
|
|
select(std::max(monitor_fd, s_wakeup_eventfd) + 1, &fds, nullptr, nullptr, nullptr);
|
2016-07-14 15:50:35 +00:00
|
|
|
if (ret < 1 || !FD_ISSET(monitor_fd, &fds))
|
|
|
|
continue;
|
|
|
|
|
2019-03-17 17:51:50 +00:00
|
|
|
udev_device* const dev = udev_monitor_receive_device(monitor);
|
|
|
|
Common::ScopeGuard dev_guard([dev] { udev_device_unref(dev); });
|
2016-07-14 15:50:35 +00:00
|
|
|
|
2019-03-17 17:51:50 +00:00
|
|
|
const char* const action = udev_device_get_action(dev);
|
|
|
|
const char* const devnode = udev_device_get_devnode(dev);
|
2016-07-14 15:50:35 +00:00
|
|
|
if (!devnode)
|
|
|
|
continue;
|
|
|
|
|
2021-05-15 09:11:27 +00:00
|
|
|
// Use g_controller_interface.PlatformPopulateDevices() to protect access around
|
|
|
|
// s_devnode_objects. Note that even if we get these events at the same time as a
|
|
|
|
// a PopulateDevices() request (e.g. on start up, we might get all the add events
|
|
|
|
// for connected devices), this won't ever cause duplicate devices as AddDeviceNode()
|
|
|
|
// automatically removes the old one if it already existed
|
2016-07-14 15:50:35 +00:00
|
|
|
if (strcmp(action, "remove") == 0)
|
|
|
|
{
|
2021-05-15 09:11:27 +00:00
|
|
|
g_controller_interface.PlatformPopulateDevices([&devnode] {
|
|
|
|
std::shared_ptr<evdevDevice> ptr;
|
2019-01-08 15:28:24 +00:00
|
|
|
|
2021-05-15 09:11:27 +00:00
|
|
|
const auto it = s_devnode_objects.find(devnode);
|
|
|
|
if (it != s_devnode_objects.end())
|
|
|
|
ptr = it->second.lock();
|
2019-11-16 21:40:43 +00:00
|
|
|
|
2021-05-15 09:11:27 +00:00
|
|
|
// If we don't recognize this device, ptr will be null and no device will be removed.
|
2019-01-08 15:28:24 +00:00
|
|
|
|
2021-05-15 09:11:27 +00:00
|
|
|
g_controller_interface.RemoveDevice([&ptr](const auto* device) {
|
|
|
|
return static_cast<const evdevDevice*>(device) == ptr.get();
|
|
|
|
});
|
2019-11-16 21:40:43 +00:00
|
|
|
});
|
2016-07-14 15:50:35 +00:00
|
|
|
}
|
2018-10-06 20:32:12 +00:00
|
|
|
else if (strcmp(action, "add") == 0)
|
2016-07-14 15:50:35 +00:00
|
|
|
{
|
2021-05-15 09:11:27 +00:00
|
|
|
g_controller_interface.PlatformPopulateDevices([&devnode] { AddDeviceNode(devnode); });
|
2016-07-14 15:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-04 20:37:06 +00:00
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "evdev hotplug thread stopped");
|
2016-07-14 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void StartHotplugThread()
|
|
|
|
{
|
2016-12-05 00:37:36 +00:00
|
|
|
// Mark the thread as running.
|
|
|
|
if (!s_hotplug_thread_running.TestAndSet())
|
2019-01-08 15:28:24 +00:00
|
|
|
{
|
2016-12-05 00:37:36 +00:00
|
|
|
// It was already running.
|
2016-07-14 15:50:35 +00:00
|
|
|
return;
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
2016-07-14 15:50:35 +00:00
|
|
|
|
|
|
|
s_wakeup_eventfd = eventfd(0, 0);
|
2021-11-11 02:34:28 +00:00
|
|
|
ASSERT_MSG(CONTROLLERINTERFACE, s_wakeup_eventfd != -1, "Couldn't create eventfd.");
|
2016-07-14 15:50:35 +00:00
|
|
|
s_hotplug_thread = std::thread(HotplugThreadFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void StopHotplugThread()
|
|
|
|
{
|
2016-12-05 00:37:36 +00:00
|
|
|
// Tell the hotplug thread to stop.
|
|
|
|
if (!s_hotplug_thread_running.TestAndClear())
|
2019-01-08 15:28:24 +00:00
|
|
|
{
|
2016-12-05 00:37:36 +00:00
|
|
|
// It wasn't running, we're done.
|
|
|
|
return;
|
2016-07-14 15:50:35 +00:00
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
|
|
|
|
// Write something to efd so that select() stops blocking.
|
|
|
|
const uint64_t value = 1;
|
2021-03-04 16:09:49 +00:00
|
|
|
static_cast<void>(!write(s_wakeup_eventfd, &value, sizeof(uint64_t)));
|
2019-01-08 15:28:24 +00:00
|
|
|
|
2016-12-05 00:37:36 +00:00
|
|
|
s_hotplug_thread.join();
|
2018-02-04 21:48:38 +00:00
|
|
|
close(s_wakeup_eventfd);
|
2016-07-14 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 15:08:04 +00:00
|
|
|
void Init()
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2016-10-16 20:39:05 +00:00
|
|
|
StartHotplugThread();
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-05-15 09:11:27 +00:00
|
|
|
// Only call this when ControllerInterface::m_devices_population_mutex is locked
|
2016-10-16 20:39:05 +00:00
|
|
|
void PopulateDevices()
|
|
|
|
{
|
2021-05-15 09:11:27 +00:00
|
|
|
// Don't run if we are not initialized
|
|
|
|
if (!s_hotplug_thread_running.IsSet())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
// We use udev to iterate over all /dev/input/event* devices.
|
|
|
|
// Note: the Linux kernel is currently limited to just 32 event devices. If
|
|
|
|
// this ever changes, hopefully udev will take care of this.
|
2016-07-14 15:50:35 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
udev* const udev = udev_new();
|
2021-11-11 02:34:28 +00:00
|
|
|
ASSERT_MSG(CONTROLLERINTERFACE, udev != nullptr, "Couldn't initialize libudev.");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
// List all input devices
|
2019-01-08 15:28:24 +00:00
|
|
|
udev_enumerate* const enumerate = udev_enumerate_new(udev);
|
2015-06-29 00:17:35 +00:00
|
|
|
udev_enumerate_add_match_subsystem(enumerate, "input");
|
|
|
|
udev_enumerate_scan_devices(enumerate);
|
2019-01-08 15:28:24 +00:00
|
|
|
udev_list_entry* const devices = udev_enumerate_get_list_entry(enumerate);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
// Iterate over all input devices
|
|
|
|
udev_list_entry* dev_list_entry;
|
|
|
|
udev_list_entry_foreach(dev_list_entry, devices)
|
|
|
|
{
|
|
|
|
const char* path = udev_list_entry_get_name(dev_list_entry);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
udev_device* dev = udev_device_new_from_syspath(udev, path);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
if (const char* devnode = udev_device_get_devnode(dev))
|
|
|
|
AddDeviceNode(devnode);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
udev_device_unref(dev);
|
|
|
|
}
|
|
|
|
udev_enumerate_unref(enumerate);
|
|
|
|
udev_unref(udev);
|
2016-07-14 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
|
|
|
StopHotplugThread();
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
bool evdevDevice::AddNode(std::string devnode, int fd, libevdev* dev)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
m_nodes.emplace_back(Node{std::move(devnode), fd, dev});
|
2018-10-06 20:32:12 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
// Take on the alphabetically first name.
|
2022-07-19 22:13:26 +00:00
|
|
|
const auto potential_new_name = StripWhitespace(libevdev_get_name(dev));
|
2019-11-16 21:40:43 +00:00
|
|
|
if (m_name.empty() || potential_new_name < m_name)
|
|
|
|
m_name = potential_new_name;
|
|
|
|
|
|
|
|
const bool is_motion_device = libevdev_has_property(dev, INPUT_PROP_ACCELEROMETER);
|
|
|
|
const bool is_pointing_device = libevdev_has_property(dev, INPUT_PROP_BUTTONPAD);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
// If a device has BTN_JOYSTICK it probably uses event codes counting up from 0x120
|
|
|
|
// which have very useless and wrong names.
|
|
|
|
const bool has_btn_joystick = libevdev_has_event_code(dev, EV_KEY, BTN_JOYSTICK);
|
|
|
|
const bool has_sensible_button_names = !has_btn_joystick;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
// Buttons (and keyboard keys)
|
2015-06-29 00:17:35 +00:00
|
|
|
int num_buttons = 0;
|
2019-11-16 21:40:43 +00:00
|
|
|
for (int key = 0; key != KEY_CNT; ++key)
|
2019-01-17 15:54:07 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_KEY, key))
|
|
|
|
{
|
|
|
|
if (is_pointing_device || is_motion_device)
|
|
|
|
{
|
|
|
|
// This node will probably be combined with another with regular buttons.
|
|
|
|
// We don't want to match "Button 0" names here as it will name clash.
|
|
|
|
AddInput(new NamedButtonWithNoBackwardsCompat(num_buttons, key, dev));
|
|
|
|
}
|
|
|
|
else if (has_sensible_button_names)
|
|
|
|
{
|
|
|
|
AddInput(new NamedButton(num_buttons, key, dev));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AddInput(new NumberedButton(num_buttons, key, dev));
|
|
|
|
}
|
|
|
|
|
|
|
|
++num_buttons;
|
|
|
|
}
|
2019-01-17 15:54:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
int num_axis = 0;
|
2019-11-09 00:23:22 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
if (is_motion_device)
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
|
|
|
// If INPUT_PROP_ACCELEROMETER is set then X,Y,Z,RX,RY,RZ contain motion data.
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
auto add_motion_inputs = [&num_axis, dev, this](int first_code, double scale) {
|
2019-11-09 00:23:22 +00:00
|
|
|
for (int i = 0; i != 3; ++i)
|
|
|
|
{
|
|
|
|
const int code = first_code + i;
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_ABS, code))
|
2019-11-09 00:23:22 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
AddInput(new MotionDataInput(code, scale * -1, dev));
|
|
|
|
AddInput(new MotionDataInput(code, scale, dev));
|
2019-11-09 00:23:22 +00:00
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
++num_axis;
|
2019-11-09 00:23:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// evdev resolution is specified in "g"s and deg/s.
|
|
|
|
// Convert these to m/s/s and rad/s.
|
|
|
|
constexpr ControlState accel_scale = MathUtil::GRAVITY_ACCELERATION;
|
|
|
|
constexpr ControlState gyro_scale = MathUtil::TAU / 360;
|
|
|
|
|
|
|
|
add_motion_inputs(ABS_X, accel_scale);
|
|
|
|
add_motion_inputs(ABS_RX, gyro_scale);
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_pointing_device)
|
|
|
|
{
|
|
|
|
auto add_cursor_input = [&num_axis, dev, this](int code) {
|
|
|
|
if (libevdev_has_event_code(dev, EV_ABS, code))
|
|
|
|
{
|
|
|
|
AddInput(new CursorInput(num_axis, code, false, dev));
|
|
|
|
AddInput(new CursorInput(num_axis, code, true, dev));
|
|
|
|
|
|
|
|
++num_axis;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
add_cursor_input(ABS_X);
|
|
|
|
add_cursor_input(ABS_Y);
|
|
|
|
|
|
|
|
return true;
|
2019-11-09 00:23:22 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
// Axes beyond ABS_MISC have strange behavior (for multi-touch) which we do not handle.
|
|
|
|
const int abs_axis_code_count = ABS_MISC;
|
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
// Absolute axis (thumbsticks)
|
2019-11-16 21:40:43 +00:00
|
|
|
for (int axis = 0; axis != abs_axis_code_count; ++axis)
|
2019-01-17 15:54:07 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_ABS, axis))
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
AddAnalogInputs(new Axis(num_axis, axis, false, dev), new Axis(num_axis, axis, true, dev));
|
2019-01-08 15:28:24 +00:00
|
|
|
++num_axis;
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
2019-01-17 15:54:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
// Disable autocenter
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_FF, FF_AUTOCENTER))
|
2019-01-08 15:28:24 +00:00
|
|
|
{
|
|
|
|
input_event ie = {};
|
|
|
|
ie.type = EV_FF;
|
|
|
|
ie.code = FF_AUTOCENTER;
|
|
|
|
ie.value = 0;
|
|
|
|
|
2021-03-04 16:09:49 +00:00
|
|
|
static_cast<void>(!write(fd, &ie, sizeof(ie)));
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Constant FF effect
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_FF, FF_CONSTANT))
|
2019-01-08 15:28:24 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
AddOutput(new ConstantEffect(fd));
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Periodic FF effects
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_FF, FF_PERIODIC))
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
for (auto wave : {FF_SINE, FF_SQUARE, FF_TRIANGLE, FF_SAW_UP, FF_SAW_DOWN})
|
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_FF, wave))
|
|
|
|
AddOutput(new PeriodicEffect(fd, wave));
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
|
|
|
|
// Rumble (i.e. Left/Right) (i.e. Strong/Weak) effect
|
2019-11-16 21:40:43 +00:00
|
|
|
if (libevdev_has_event_code(dev, EV_FF, FF_RUMBLE))
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
AddOutput(new RumbleEffect(fd, RumbleEffect::Motor::Strong));
|
|
|
|
AddOutput(new RumbleEffect(fd, RumbleEffect::Motor::Weak));
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
// TODO: Add leds as output devices
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-01-06 23:46:24 +00:00
|
|
|
// Filter out interesting devices (see description below)
|
2019-11-16 21:40:43 +00:00
|
|
|
return num_axis >= 2 || num_buttons >= 8;
|
2020-01-06 23:52:05 +00:00
|
|
|
|
2020-01-06 23:46:24 +00:00
|
|
|
// On modern linux systems, there are a lot of event devices that aren't controllers.
|
|
|
|
// For example, the PC Speaker is an event device. Webcams sometimes show up as
|
|
|
|
// event devices. The power button is an event device.
|
2020-01-06 23:52:05 +00:00
|
|
|
//
|
2020-01-06 23:46:24 +00:00
|
|
|
// We don't want these showing up in the list of controllers, so we use this
|
|
|
|
// heuristic to filter out anything that doesn't smell like a controller:
|
|
|
|
//
|
2020-01-06 23:52:05 +00:00
|
|
|
// More than two analog axis:
|
2020-01-06 23:46:24 +00:00
|
|
|
// Most controllers have at least one stick. This rule will catch all such
|
2020-01-06 23:52:05 +00:00
|
|
|
// controllers, while ignoring anything with a single axis (like the mouse
|
2020-01-06 23:46:24 +00:00
|
|
|
// scroll-wheel)
|
|
|
|
//
|
|
|
|
// --- OR ---
|
|
|
|
//
|
|
|
|
// More than 8 buttons:
|
|
|
|
// The user might be using a digital only pad such as a NES controller.
|
|
|
|
// This rule caches such controllers, while eliminating any device with
|
|
|
|
// only a few buttons, like the power button. Sometimes laptops have devices
|
|
|
|
// with 5 or 6 special buttons, which is why the threshold is set to 8 to
|
|
|
|
// match a NES controller.
|
2020-01-06 23:52:05 +00:00
|
|
|
//
|
2020-01-06 23:46:24 +00:00
|
|
|
// This heuristic is quite loose. The user may still see weird devices showing up
|
|
|
|
// as controllers, but it hopefully shouldn't filter out anything they actually
|
2020-01-06 23:52:05 +00:00
|
|
|
// want to use.
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 21:40:43 +00:00
|
|
|
const char* evdevDevice::GetUniqueID() const
|
|
|
|
{
|
|
|
|
if (m_nodes.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
2019-11-17 15:09:47 +00:00
|
|
|
const auto uniq = libevdev_get_uniq(m_nodes.front().device);
|
|
|
|
|
|
|
|
// Some devices (e.g. Mayflash adapter) return an empty string which is not very unique.
|
|
|
|
if (uniq && std::strlen(uniq) == 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return uniq;
|
2019-11-16 21:40:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-01 20:59:45 +00:00
|
|
|
const char* evdevDevice::GetPhysicalLocation() const
|
|
|
|
{
|
|
|
|
if (m_nodes.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return libevdev_get_phys(m_nodes.front().device);
|
|
|
|
}
|
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
evdevDevice::~evdevDevice()
|
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
for (auto& node : m_nodes)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
s_devnode_objects.erase(node.devnode);
|
|
|
|
libevdev_free(node.device);
|
|
|
|
close(node.fd);
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void evdevDevice::UpdateInput()
|
|
|
|
{
|
|
|
|
// Run through all evdev events
|
|
|
|
// libevdev will keep track of the actual controller state internally which can be queried
|
|
|
|
// later with libevdev_fetch_event_value()
|
2019-11-16 21:40:43 +00:00
|
|
|
for (auto& node : m_nodes)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
int rc = LIBEVDEV_READ_STATUS_SUCCESS;
|
|
|
|
while (rc >= 0)
|
|
|
|
{
|
|
|
|
input_event ev;
|
|
|
|
if (LIBEVDEV_READ_STATUS_SYNC == rc)
|
|
|
|
rc = libevdev_next_event(node.device, LIBEVDEV_READ_FLAG_SYNC, &ev);
|
|
|
|
else
|
|
|
|
rc = libevdev_next_event(node.device, LIBEVDEV_READ_FLAG_NORMAL, &ev);
|
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 15:50:35 +00:00
|
|
|
bool evdevDevice::IsValid() const
|
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
for (auto& node : m_nodes)
|
2016-07-14 15:50:35 +00:00
|
|
|
{
|
2019-11-16 21:40:43 +00:00
|
|
|
const int current_fd = libevdev_get_fd(node.device);
|
|
|
|
|
|
|
|
if (current_fd == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
libevdev* device = nullptr;
|
|
|
|
if (libevdev_new_from_fd(current_fd, &device) != 0)
|
|
|
|
{
|
|
|
|
close(current_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
libevdev_free(device);
|
2016-07-14 15:50:35 +00:00
|
|
|
}
|
2019-11-16 21:40:43 +00:00
|
|
|
|
2016-07-14 15:50:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
evdevDevice::Effect::Effect(int fd) : m_fd(fd)
|
|
|
|
{
|
|
|
|
m_effect.id = -1;
|
|
|
|
// Left (for wheels):
|
|
|
|
m_effect.direction = 0x4000;
|
|
|
|
m_effect.replay.length = RUMBLE_LENGTH_MS;
|
|
|
|
|
|
|
|
// FYI: type is set within UpdateParameters.
|
|
|
|
m_effect.type = DISABLED_EFFECT_TYPE;
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
std::string evdevDevice::ConstantEffect::GetName() const
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
return "Constant";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string evdevDevice::PeriodicEffect::GetName() const
|
|
|
|
{
|
|
|
|
switch (m_effect.u.periodic.waveform)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
|
|
|
case FF_SQUARE:
|
|
|
|
return "Square";
|
2019-01-08 15:28:24 +00:00
|
|
|
case FF_TRIANGLE:
|
|
|
|
return "Triangle";
|
|
|
|
case FF_SINE:
|
|
|
|
return "Sine";
|
|
|
|
case FF_SAW_UP:
|
|
|
|
return "Sawtooth Up";
|
|
|
|
case FF_SAW_DOWN:
|
|
|
|
return "Sawtooth Down";
|
2015-06-29 00:17:35 +00:00
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
std::string evdevDevice::RumbleEffect::GetName() const
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-01-17 16:13:32 +00:00
|
|
|
return (Motor::Strong == m_motor) ? "Strong" : "Weak";
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
void evdevDevice::Effect::SetState(ControlState state)
|
|
|
|
{
|
|
|
|
if (UpdateParameters(state))
|
2015-07-05 19:57:31 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
// Update effect if parameters changed.
|
|
|
|
UpdateEffect();
|
2015-07-05 19:57:31 +00:00
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
void evdevDevice::Effect::UpdateEffect()
|
|
|
|
{
|
|
|
|
// libevdev doesn't have nice helpers for forcefeedback
|
|
|
|
// we will use the file descriptors directly.
|
|
|
|
|
|
|
|
// Note: m_effect.type is set within UpdateParameters
|
|
|
|
// to determine if effect should be playing or not.
|
|
|
|
if (m_effect.type != DISABLED_EFFECT_TYPE)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
if (-1 == m_effect.id)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
// If effect was not uploaded (previously stopped)
|
|
|
|
// we upload it and start playback
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
ioctl(m_fd, EVIOCSFF, &m_effect);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
input_event play = {};
|
|
|
|
play.type = EV_FF;
|
|
|
|
play.code = m_effect.id;
|
|
|
|
play.value = 1;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-03-04 16:09:49 +00:00
|
|
|
static_cast<void>(!write(m_fd, &play, sizeof(play)));
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
|
|
|
else
|
2016-10-22 17:08:17 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
// Effect is already playing. Just update parameters.
|
|
|
|
ioctl(m_fd, EVIOCSFF, &m_effect);
|
2016-10-22 17:08:17 +00:00
|
|
|
}
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stop and remove effect.
|
|
|
|
ioctl(m_fd, EVIOCRMFF, m_effect.id);
|
|
|
|
m_effect.id = -1;
|
|
|
|
}
|
2015-07-05 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
evdevDevice::ConstantEffect::ConstantEffect(int fd) : Effect(fd)
|
2015-07-05 19:57:31 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
m_effect.u.constant = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
evdevDevice::PeriodicEffect::PeriodicEffect(int fd, u16 waveform) : Effect(fd)
|
|
|
|
{
|
|
|
|
m_effect.u.periodic = {};
|
|
|
|
m_effect.u.periodic.waveform = waveform;
|
|
|
|
m_effect.u.periodic.period = RUMBLE_PERIOD_MS;
|
|
|
|
m_effect.u.periodic.offset = 0;
|
|
|
|
m_effect.u.periodic.phase = 0;
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
|
2019-01-17 15:54:07 +00:00
|
|
|
evdevDevice::RumbleEffect::RumbleEffect(int fd, Motor motor) : Effect(fd), m_motor(motor)
|
2019-01-08 15:28:24 +00:00
|
|
|
{
|
|
|
|
m_effect.u.rumble = {};
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
|
|
|
|
bool evdevDevice::ConstantEffect::UpdateParameters(ControlState state)
|
|
|
|
{
|
|
|
|
s16& value = m_effect.u.constant.level;
|
|
|
|
const s16 old_value = value;
|
|
|
|
|
|
|
|
constexpr s16 MAX_VALUE = 0x7fff;
|
|
|
|
value = s16(state * MAX_VALUE);
|
|
|
|
|
|
|
|
m_effect.type = value ? FF_CONSTANT : DISABLED_EFFECT_TYPE;
|
|
|
|
return value != old_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool evdevDevice::PeriodicEffect::UpdateParameters(ControlState state)
|
|
|
|
{
|
|
|
|
s16& value = m_effect.u.periodic.magnitude;
|
|
|
|
const s16 old_value = value;
|
|
|
|
|
|
|
|
constexpr s16 MAX_VALUE = 0x7fff;
|
|
|
|
value = s16(state * MAX_VALUE);
|
|
|
|
|
|
|
|
m_effect.type = value ? FF_PERIODIC : DISABLED_EFFECT_TYPE;
|
|
|
|
return value != old_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool evdevDevice::RumbleEffect::UpdateParameters(ControlState state)
|
|
|
|
{
|
2019-01-17 16:13:32 +00:00
|
|
|
u16& value = (Motor::Strong == m_motor) ? m_effect.u.rumble.strong_magnitude :
|
2019-01-17 15:54:07 +00:00
|
|
|
m_effect.u.rumble.weak_magnitude;
|
2019-01-08 15:28:24 +00:00
|
|
|
const u16 old_value = value;
|
|
|
|
|
|
|
|
constexpr u16 MAX_VALUE = 0xffff;
|
|
|
|
value = u16(state * MAX_VALUE);
|
|
|
|
|
|
|
|
m_effect.type = value ? FF_RUMBLE : DISABLED_EFFECT_TYPE;
|
|
|
|
return value != old_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
evdevDevice::Effect::~Effect()
|
|
|
|
{
|
|
|
|
m_effect.type = DISABLED_EFFECT_TYPE;
|
|
|
|
UpdateEffect();
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::evdev
|