2015-06-29 00:17:35 +00:00
|
|
|
// Copyright 2015 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-12-25 23:38:44 +00:00
|
|
|
#include <algorithm>
|
2017-09-09 19:52:35 +00:00
|
|
|
#include <cstring>
|
2015-07-08 18:09:08 +00:00
|
|
|
#include <fcntl.h>
|
2015-06-29 00:17:35 +00:00
|
|
|
#include <libudev.h>
|
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
|
|
|
#include <unistd.h>
|
|
|
|
|
2016-07-14 15:50:35 +00:00
|
|
|
#include <sys/eventfd.h>
|
|
|
|
|
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
|
|
|
#include "InputCommon/ControllerInterface/evdev/evdev.h"
|
|
|
|
|
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace evdev
|
|
|
|
{
|
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.
|
|
|
|
static std::map<std::string, std::string> s_devnode_name_map;
|
|
|
|
|
|
|
|
static void HotplugThreadFunc()
|
|
|
|
{
|
|
|
|
Common::SetCurrentThreadName("evdev Hotplug Thread");
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "evdev hotplug thread started");
|
|
|
|
|
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); });
|
|
|
|
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT_MSG(PAD, 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;
|
|
|
|
|
|
|
|
if (strcmp(action, "remove") == 0)
|
|
|
|
{
|
|
|
|
const auto it = s_devnode_name_map.find(devnode);
|
|
|
|
if (it == s_devnode_name_map.end())
|
2019-01-08 15:28:24 +00:00
|
|
|
{
|
|
|
|
// We don't know the name for this device, so it is probably not an evdev device.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-07-14 15:50:35 +00:00
|
|
|
const std::string& name = it->second;
|
|
|
|
g_controller_interface.RemoveDevice([&name](const auto& device) {
|
|
|
|
return device->GetSource() == "evdev" && device->GetName() == name && !device->IsValid();
|
|
|
|
});
|
2019-01-08 15:28:24 +00:00
|
|
|
|
2016-07-14 15:50:35 +00:00
|
|
|
s_devnode_name_map.erase(devnode);
|
|
|
|
}
|
2018-10-06 20:32:12 +00:00
|
|
|
else if (strcmp(action, "add") == 0)
|
2016-07-14 15:50:35 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
const auto device = std::make_shared<evdevDevice>(devnode);
|
2016-07-14 15:50:35 +00:00
|
|
|
if (device->IsInteresting())
|
|
|
|
{
|
2018-10-06 20:32:12 +00:00
|
|
|
s_devnode_name_map.emplace(devnode, device->GetName());
|
2016-07-14 15:50:35 +00:00
|
|
|
g_controller_interface.AddDevice(std::move(device));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "evdev hotplug thread stopped");
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT_MSG(PAD, 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;
|
|
|
|
static_cast<void>(write(s_wakeup_eventfd, &value, sizeof(uint64_t)));
|
|
|
|
|
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-07-14 15:50:35 +00:00
|
|
|
s_devnode_name_map.clear();
|
2016-10-16 20:39:05 +00:00
|
|
|
StartHotplugThread();
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-10-16 20:39:05 +00:00
|
|
|
void PopulateDevices()
|
|
|
|
{
|
|
|
|
// 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();
|
2018-03-15 00:34:35 +00:00
|
|
|
ASSERT_MSG(PAD, udev != nullptr, "Couldn't initialize libudev.");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// List all input devices
|
2019-01-08 15:28:24 +00:00
|
|
|
udev_enumerate* const enumerate = udev_enumerate_new(udev);
|
2016-06-24 08:43:46 +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
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
udev_device* dev = udev_device_new_from_syspath(udev, path);
|
|
|
|
|
|
|
|
const char* devnode = udev_device_get_devnode(dev);
|
2018-10-06 20:32:12 +00:00
|
|
|
if (devnode)
|
2016-06-24 08:43:46 +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.
|
2019-01-08 15:28:24 +00:00
|
|
|
const auto input = std::make_shared<evdevDevice>(devnode);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
if (input->IsInteresting())
|
|
|
|
{
|
2018-10-06 20:32:12 +00:00
|
|
|
s_devnode_name_map.emplace(devnode, input->GetName());
|
2016-06-25 19:46:39 +00:00
|
|
|
g_controller_interface.AddDevice(std::move(input));
|
2016-06-24 08:43:46 +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
|
|
|
}
|
|
|
|
|
2016-07-14 08:25:52 +00:00
|
|
|
evdevDevice::evdevDevice(const std::string& devnode) : m_devfile(devnode)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// The device file will be read on one of the main threads, so we open in non-blocking mode.
|
|
|
|
m_fd = open(devnode.c_str(), O_RDWR | O_NONBLOCK);
|
2018-10-06 20:32:12 +00:00
|
|
|
if (m_fd == -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
if (libevdev_new_from_fd(m_fd, &m_dev) != 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
// This usually fails because the device node isn't an evdev device, such as /dev/input/js0
|
2016-06-24 08:43:46 +00:00
|
|
|
close(m_fd);
|
2019-01-08 15:28:24 +00:00
|
|
|
m_fd = -1;
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-01 03:05:24 +00:00
|
|
|
m_name = StripSpaces(libevdev_get_name(m_dev));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
// Buttons (and keyboard keys)
|
2016-06-24 08:43:46 +00:00
|
|
|
int num_buttons = 0;
|
|
|
|
for (int key = 0; key < KEY_MAX; key++)
|
2019-01-17 15:54:07 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (libevdev_has_event_code(m_dev, EV_KEY, key))
|
|
|
|
AddInput(new Button(num_buttons++, key, m_dev));
|
2019-01-17 15:54:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Absolute axis (thumbsticks)
|
|
|
|
int num_axis = 0;
|
|
|
|
for (int axis = 0; axis < 0x100; axis++)
|
2019-01-17 15:54:07 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (libevdev_has_event_code(m_dev, EV_ABS, axis))
|
|
|
|
{
|
|
|
|
AddAnalogInputs(new Axis(num_axis, axis, false, m_dev),
|
|
|
|
new Axis(num_axis, axis, true, m_dev));
|
2019-01-08 15:28:24 +00:00
|
|
|
++num_axis;
|
2016-06-24 08:43:46 +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
|
|
|
|
if (libevdev_has_event_code(m_dev, EV_FF, FF_AUTOCENTER))
|
|
|
|
{
|
|
|
|
input_event ie = {};
|
|
|
|
ie.type = EV_FF;
|
|
|
|
ie.code = FF_AUTOCENTER;
|
|
|
|
ie.value = 0;
|
|
|
|
|
|
|
|
static_cast<void>(write(m_fd, &ie, sizeof(ie)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constant FF effect
|
|
|
|
if (libevdev_has_event_code(m_dev, EV_FF, FF_CONSTANT))
|
|
|
|
{
|
|
|
|
AddOutput(new ConstantEffect(m_fd));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Periodic FF effects
|
2016-06-24 08:43:46 +00:00
|
|
|
if (libevdev_has_event_code(m_dev, EV_FF, FF_PERIODIC))
|
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
for (auto wave : {FF_SINE, FF_SQUARE, FF_TRIANGLE, FF_SAW_UP, FF_SAW_DOWN})
|
|
|
|
{
|
|
|
|
if (libevdev_has_event_code(m_dev, EV_FF, wave))
|
|
|
|
AddOutput(new PeriodicEffect(m_fd, wave));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2019-01-08 15:28:24 +00:00
|
|
|
|
|
|
|
// Rumble (i.e. Left/Right) (i.e. Strong/Weak) effect
|
2016-06-24 08:43:46 +00:00
|
|
|
if (libevdev_has_event_code(m_dev, EV_FF, FF_RUMBLE))
|
|
|
|
{
|
2019-01-17 16:13:32 +00:00
|
|
|
AddOutput(new RumbleEffect(m_fd, RumbleEffect::Motor::Strong));
|
|
|
|
AddOutput(new RumbleEffect(m_fd, RumbleEffect::Motor::Weak));
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add leds as output devices
|
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
// Was there some reasoning behind these numbers?
|
2016-06-24 08:43:46 +00:00
|
|
|
m_interesting = num_axis >= 2 || num_buttons >= 8;
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
evdevDevice::~evdevDevice()
|
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
if (m_fd != -1)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
libevdev_free(m_dev);
|
|
|
|
close(m_fd);
|
|
|
|
}
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void evdevDevice::UpdateInput()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// 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()
|
|
|
|
int rc = LIBEVDEV_READ_STATUS_SUCCESS;
|
2019-01-08 15:28:24 +00:00
|
|
|
while (rc >= 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
input_event ev;
|
|
|
|
if (LIBEVDEV_READ_STATUS_SYNC == rc)
|
2016-06-24 08:43:46 +00:00
|
|
|
rc = libevdev_next_event(m_dev, LIBEVDEV_READ_FLAG_SYNC, &ev);
|
|
|
|
else
|
|
|
|
rc = libevdev_next_event(m_dev, 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
|
|
|
|
{
|
|
|
|
int current_fd = libevdev_get_fd(m_dev);
|
|
|
|
if (current_fd == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
libevdev* device;
|
|
|
|
if (libevdev_new_from_fd(current_fd, &device) != 0)
|
|
|
|
{
|
|
|
|
close(current_fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
libevdev_free(device);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-29 00:17:35 +00:00
|
|
|
std::string evdevDevice::Button::GetName() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Buttons below 0x100 are mostly keyboard keys, and the names make sense
|
|
|
|
if (m_code < 0x100)
|
|
|
|
{
|
|
|
|
const char* name = libevdev_event_code_get_name(EV_KEY, m_code);
|
|
|
|
if (name)
|
2016-07-01 03:05:24 +00:00
|
|
|
return StripSpaces(name);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
// But controllers use codes above 0x100, and the standard label often doesn't match.
|
|
|
|
// We are better off with Button 0 and so on.
|
|
|
|
return "Button " + std::to_string(m_index);
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ControlState evdevDevice::Button::GetState() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
int value = 0;
|
|
|
|
libevdev_fetch_event_value(m_dev, EV_KEY, m_code, &value);
|
|
|
|
return value;
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
evdevDevice::Axis::Axis(u8 index, u16 code, bool upper, libevdev* dev)
|
2019-01-08 15:28:24 +00:00
|
|
|
: m_code(code), m_index(index), m_dev(dev)
|
2015-06-29 00:17:35 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
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;
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string evdevDevice::Axis::GetName() const
|
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
return "Axis " + std::to_string(m_index) + (m_range < 0 ? '-' : '+');
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ControlState evdevDevice::Axis::GetState() const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
int value = 0;
|
|
|
|
libevdev_fetch_event_value(m_dev, EV_ABS, m_code, &value);
|
2015-06-30 12:37:26 +00:00
|
|
|
|
2019-02-27 01:46:21 +00:00
|
|
|
return ControlState(value - m_base) / m_range;
|
2019-01-08 15:28:24 +00:00
|
|
|
}
|
2015-06-30 12:37:26 +00:00
|
|
|
|
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)
|
2016-06-24 08:43:46 +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";
|
2016-06-24 08:43:46 +00:00
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
2015-06-29 00:17:35 +00:00
|
|
|
}
|
|
|
|
|
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))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
// Update effect if parameters changed.
|
|
|
|
UpdateEffect();
|
2016-06-24 08:43:46 +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)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-01-08 15:28:24 +00:00
|
|
|
if (-1 == m_effect.id)
|
2016-06-24 08:43:46 +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
|
|
|
|
2019-01-08 15:28:24 +00:00
|
|
|
static_cast<void>(write(m_fd, &play, sizeof(play)));
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2016-06-24 08:43:46 +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-01-08 15:28:24 +00:00
|
|
|
} // namespace evdev
|
|
|
|
} // namespace ciface
|