evdev: Some random cleanup

This commit is contained in:
Megamouse 2020-03-12 10:32:22 +01:00
parent d802b3d8b2
commit 9c5da55dca

View file

@ -18,7 +18,8 @@
LOG_CHANNEL(evdev_log, "evdev"); LOG_CHANNEL(evdev_log, "evdev");
evdev_joystick_handler::evdev_joystick_handler() : PadHandlerBase(pad_handler::evdev) evdev_joystick_handler::evdev_joystick_handler()
: PadHandlerBase(pad_handler::evdev)
{ {
init_configs(); init_configs();
@ -103,8 +104,8 @@ bool evdev_joystick_handler::Init()
{ {
if (*static_cast<cfg::_bool*>(node.second)) if (*static_cast<cfg::_bool*>(node.second))
{ {
std::string name = node.first; const auto name = node.first;
int code = libevdev_event_code_from_name(EV_ABS, name.c_str()); const int code = libevdev_event_code_from_name(EV_ABS, name.c_str());
if (code < 0) if (code < 0)
evdev_log.error("Failed to read axis name from %s. [code = %d] [name = %s]", m_pos_axis_config.cfg_name, code, name); evdev_log.error("Failed to read axis name from %s. [code = %d] [name = %s]", m_pos_axis_config.cfg_name, code, name);
else else
@ -139,13 +140,13 @@ bool evdev_joystick_handler::update_device(const std::shared_ptr<PadDevice>& dev
const auto& path = evdev_device->path; const auto& path = evdev_device->path;
libevdev*& dev = evdev_device->device; libevdev*& dev = evdev_device->device;
bool was_connected = dev != nullptr; const bool was_connected = dev != nullptr;
if (access(path.c_str(), R_OK) == -1) if (access(path.c_str(), R_OK) == -1)
{ {
if (was_connected) if (was_connected)
{ {
int fd = libevdev_get_fd(dev); const int fd = libevdev_get_fd(dev);
libevdev_free(dev); libevdev_free(dev);
close(fd); close(fd);
dev = nullptr; dev = nullptr;
@ -158,15 +159,15 @@ bool evdev_joystick_handler::update_device(const std::shared_ptr<PadDevice>& dev
if (was_connected) if (was_connected)
return true; // It's already been connected, and the js is still present. return true; // It's already been connected, and the js is still present.
int fd = open(path.c_str(), O_RDWR | O_NONBLOCK); const int fd = open(path.c_str(), O_RDWR | O_NONBLOCK);
if (fd == -1) if (fd == -1)
{ {
int err = errno; const int err = errno;
evdev_log.error("Failed to open joystick: %s [errno %d]", strerror(err), err); evdev_log.error("Failed to open joystick: %s [errno %d]", strerror(err), err);
return false; return false;
} }
int ret = libevdev_new_from_fd(fd, &dev); const int ret = libevdev_new_from_fd(fd, &dev);
if (ret < 0) if (ret < 0)
{ {
evdev_log.error("Failed to initialize libevdev for joystick: %s [errno %d]", strerror(-ret), -ret); evdev_log.error("Failed to initialize libevdev for joystick: %s [errno %d]", strerror(-ret), -ret);
@ -195,7 +196,7 @@ void evdev_joystick_handler::Close()
auto& dev = evdev_device->device; auto& dev = evdev_device->device;
if (dev != nullptr) if (dev != nullptr)
{ {
int fd = libevdev_get_fd(dev); const int fd = libevdev_get_fd(dev);
if (evdev_device->effect_id != -1) if (evdev_device->effect_id != -1)
ioctl(fd, EVIOCRMFF, evdev_device->effect_id); ioctl(fd, EVIOCRMFF, evdev_device->effect_id);
libevdev_free(dev); libevdev_free(dev);
@ -216,35 +217,37 @@ std::unordered_map<u64, std::pair<u16, bool>> evdev_joystick_handler::GetButtonV
if (!Init()) if (!Init())
return button_values; return button_values;
for (auto entry : button_list) for (const auto entry : button_list)
{ {
auto code = entry.first; const auto code = entry.first;
int val = 0; int val = 0;
if (libevdev_fetch_event_value(dev, EV_KEY, code, &val) == 0) if (libevdev_fetch_event_value(dev, EV_KEY, code, &val) == 0)
continue; continue;
button_values.emplace(code, std::make_pair<u16, bool>(static_cast<u16>(val > 0 ? 255 : 0), false)); button_values.emplace(code, std::make_pair<u16, bool>(static_cast<u16>(val > 0 ? 255 : 0), false));
} }
for (auto entry : axis_list) for (const auto entry : axis_list)
{ {
auto code = entry.first; const auto code = entry.first;
int val = 0; int val = 0;
if (libevdev_fetch_event_value(dev, EV_ABS, code, &val) == 0) if (libevdev_fetch_event_value(dev, EV_ABS, code, &val) == 0)
continue; continue;
int min = libevdev_get_abs_minimum(dev, code); const int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code); const int max = libevdev_get_abs_maximum(dev, code);
// Triggers do not need handling of negative values // Triggers do not need handling of negative values
if (min >= 0 && std::find(m_positive_axis.begin(), m_positive_axis.end(), code) == m_positive_axis.end()) if (min >= 0 && std::find(m_positive_axis.begin(), m_positive_axis.end(), code) == m_positive_axis.end())
{ {
float fvalue = ScaleStickInput(val, min, max); const float fvalue = ScaleStickInput(val, min, max);
button_values.emplace(code, std::make_pair<u16, bool>(static_cast<u16>(fvalue), false)); button_values.emplace(code, std::make_pair<u16, bool>(static_cast<u16>(fvalue), false));
continue; continue;
} }
float fvalue = ScaleStickInput2(val, min, max); const float fvalue = ScaleStickInput2(val, min, max);
if (fvalue < 0) if (fvalue < 0)
button_values.emplace(code, std::make_pair<u16, bool>(static_cast<u16>(std::abs(fvalue)), true)); button_values.emplace(code, std::make_pair<u16, bool>(static_cast<u16>(std::abs(fvalue)), true));
else else
@ -257,7 +260,7 @@ std::unordered_map<u64, std::pair<u16, bool>> evdev_joystick_handler::GetButtonV
std::shared_ptr<evdev_joystick_handler::EvdevDevice> evdev_joystick_handler::get_evdev_device(const std::string& device) std::shared_ptr<evdev_joystick_handler::EvdevDevice> evdev_joystick_handler::get_evdev_device(const std::string& device)
{ {
// Add device if not yet present // Add device if not yet present
int pad_index = add_device(device, nullptr, true); const int pad_index = add_device(device, nullptr, true);
if (pad_index < 0) if (pad_index < 0)
return nullptr; return nullptr;
@ -294,7 +297,7 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
auto find_value = [=, this](const std::string& name) auto find_value = [=, this](const std::string& name)
{ {
int key = FindKeyCodeByString(rev_axis_list, name, false); int key = FindKeyCodeByString(rev_axis_list, name, false);
bool dir = key >= 0; const bool dir = key >= 0;
if (key < 0) if (key < 0)
key = FindKeyCodeByString(axis_list, name, false); key = FindKeyCodeByString(axis_list, name, false);
if (key < 0) if (key < 0)
@ -321,11 +324,8 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
std::pair<u16, std::string> pressed_button = { 0, "" }; std::pair<u16, std::string> pressed_button = { 0, "" };
for (const auto& button : button_list) for (const auto& [code, name] : button_list)
{ {
int code = button.first;
std::string name = button.second;
// Handle annoying useless buttons // Handle annoying useless buttons
if (padId.find("Xbox 360") != umax && code >= BTN_TRIGGER_HAPPY) if (padId.find("Xbox 360") != umax && code >= BTN_TRIGGER_HAPPY)
continue; continue;
@ -335,7 +335,7 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end()) if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end())
continue; continue;
u16 value = data[code].first; const u16 value = data[code].first;
if (value > 0) if (value > 0)
{ {
if (get_blacklist) if (get_blacklist)
@ -348,24 +348,21 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
} }
} }
for (const auto& button : axis_list) for (const auto& [code, name] : axis_list)
{ {
int code = button.first;
std::string name = button.second;
if (data[code].second) if (data[code].second)
continue; continue;
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end()) if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end())
continue; continue;
u16 value = data[code].first; const u16 value = data[code].first;
if (value > 0 && value >= m_thumb_threshold) if (value > 0 && value >= m_thumb_threshold)
{ {
if (get_blacklist) if (get_blacklist)
{ {
int min = libevdev_get_abs_minimum(dev, code); const int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code); const int max = libevdev_get_abs_maximum(dev, code);
blacklist.emplace_back(name); blacklist.emplace_back(name);
evdev_log.error("Evdev Calibration: Added axis [ %d = %s = %s ] to blacklist. [ Value = %d ] [ Min = %d ] [ Max = %d ]", code, libevdev_event_code_get_name(EV_ABS, code), name, value, min, max); evdev_log.error("Evdev Calibration: Added axis [ %d = %s = %s ] to blacklist. [ Value = %d ] [ Min = %d ] [ Max = %d ]", code, libevdev_event_code_get_name(EV_ABS, code), name, value, min, max);
} }
@ -374,24 +371,21 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
} }
} }
for (const auto& button : rev_axis_list) for (const auto& [code, name] : rev_axis_list)
{ {
int code = button.first;
std::string name = button.second;
if (!data[code].second) if (!data[code].second)
continue; continue;
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end()) if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end())
continue; continue;
u16 value = data[code].first; const u16 value = data[code].first;
if (value > 0 && value >= m_thumb_threshold) if (value > 0 && value >= m_thumb_threshold)
{ {
if (get_blacklist) if (get_blacklist)
{ {
int min = libevdev_get_abs_minimum(dev, code); const int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code); const int max = libevdev_get_abs_maximum(dev, code);
blacklist.emplace_back(name); blacklist.emplace_back(name);
evdev_log.error("Evdev Calibration: Added rev axis [ %d = %s = %s ] to blacklist. [ Value = %d ] [ Min = %d ] [ Max = %d ]", code, libevdev_event_code_get_name(EV_ABS, code), name, value, min, max); evdev_log.error("Evdev Calibration: Added rev axis [ %d = %s = %s ] to blacklist. [ Value = %d ] [ Min = %d ] [ Max = %d ]", code, libevdev_event_code_get_name(EV_ABS, code), name, value, min, max);
} }
@ -416,12 +410,12 @@ void evdev_joystick_handler::get_next_button_press(const std::string& padId, con
// https://github.com/dolphin-emu/dolphin/blob/master/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp // https://github.com/dolphin-emu/dolphin/blob/master/Source/Core/InputCommon/ControllerInterface/evdev/evdev.cpp
// https://github.com/reicast/reicast-emulator/blob/master/core/linux-dist/evdev.cpp // https://github.com/reicast/reicast-emulator/blob/master/core/linux-dist/evdev.cpp
// http://www.infradead.org/~mchehab/kernel_docs_pdf/linux-input.pdf // http://www.infradead.org/~mchehab/kernel_docs_pdf/linux-input.pdf
void evdev_joystick_handler::SetRumble(std::shared_ptr<EvdevDevice>device, u16 large, u16 small) void evdev_joystick_handler::SetRumble(std::shared_ptr<EvdevDevice> device, u16 large, u16 small)
{ {
if (!device || !device->has_rumble || device->effect_id == -2) if (!device || !device->has_rumble || device->effect_id == -2)
return; return;
int fd = libevdev_get_fd(device->device); const int fd = libevdev_get_fd(device->device);
if (fd < 0) if (fd < 0)
return; return;
@ -514,8 +508,8 @@ void evdev_joystick_handler::SetPadData(const std::string& padId, u32 largeMotor
int evdev_joystick_handler::GetButtonInfo(const input_event& evt, const std::shared_ptr<EvdevDevice>& device, int& value) int evdev_joystick_handler::GetButtonInfo(const input_event& evt, const std::shared_ptr<EvdevDevice>& device, int& value)
{ {
int code = evt.code; const int code = evt.code;
int val = evt.value; const int val = evt.value;
m_is_button_or_trigger = false; m_is_button_or_trigger = false;
switch (evt.type) switch (evt.type)
@ -541,8 +535,8 @@ int evdev_joystick_handler::GetButtonInfo(const input_event& evt, const std::sha
return -1; return -1;
} }
auto& dev = device->device; auto& dev = device->device;
int min = libevdev_get_abs_minimum(dev, code); const int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code); const int max = libevdev_get_abs_maximum(dev, code);
// Triggers do not need handling of negative values // Triggers do not need handling of negative values
if (min >= 0 && std::find(m_positive_axis.begin(), m_positive_axis.end(), code) == m_positive_axis.end()) if (min >= 0 && std::find(m_positive_axis.begin(), m_positive_axis.end(), code) == m_positive_axis.end())
@ -553,13 +547,12 @@ int evdev_joystick_handler::GetButtonInfo(const input_event& evt, const std::sha
return code; return code;
} }
float fvalue = ScaleStickInput2(val, min, max); const float fvalue = ScaleStickInput2(val, min, max);
m_is_negative = fvalue < 0; m_is_negative = fvalue < 0;
value = static_cast<u16>(std::abs(fvalue)); value = static_cast<u16>(std::abs(fvalue));
return code; return code;
} }
default: default: return -1;
return -1;
} }
} }
@ -575,11 +568,11 @@ std::vector<std::string> evdev_joystick_handler::ListDevices()
while (devdir.read(et)) while (devdir.read(et))
{ {
// Check if the entry starts with event (a 5-letter word) // Check if the entry starts with event (a 5-letter word)
if (et.name.size() > 5 && et.name.compare(0, 5,"event") == 0) if (et.name.size() > 5 && et.name.compare(0, 5, "event") == 0)
{ {
int fd = open(("/dev/input/" + et.name).c_str(), O_RDWR | O_NONBLOCK); const int fd = open(("/dev/input/" + et.name).c_str(), O_RDWR | O_NONBLOCK);
struct libevdev *dev = NULL; struct libevdev* dev = NULL;
int rc = libevdev_new_from_fd(fd, &dev); const int rc = libevdev_new_from_fd(fd, &dev);
if (rc < 0) if (rc < 0)
{ {
// If it's just a bad file descriptor, don't bother logging, but otherwise, log it. // If it's just a bad file descriptor, don't bother logging, but otherwise, log it.
@ -624,10 +617,10 @@ int evdev_joystick_handler::add_device(const std::string& device, const std::sha
// Check if the entry starts with event (a 5-letter word) // Check if the entry starts with event (a 5-letter word)
if (et.name.size() > 5 && et.name.compare(0, 5, "event") == 0) if (et.name.size() > 5 && et.name.compare(0, 5, "event") == 0)
{ {
std::string path = "/dev/input/" + et.name; const std::string path = "/dev/input/" + et.name;
int fd = open(path.c_str(), O_RDWR | O_NONBLOCK); const int fd = open(path.c_str(), O_RDWR | O_NONBLOCK);
struct libevdev *dev = NULL; struct libevdev* dev = NULL;
int rc = libevdev_new_from_fd(fd, &dev); const int rc = libevdev_new_from_fd(fd, &dev);
if (rc < 0) if (rc < 0)
{ {
// If it's just a bad file descriptor, don't bother logging, but otherwise, log it. // If it's just a bad file descriptor, don't bother logging, but otherwise, log it.
@ -728,7 +721,7 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr<PadDevice>& devic
m_dev->cur_type = evt.type; m_dev->cur_type = evt.type;
int value; int value;
int button_code = GetButtonInfo(evt, m_dev, value); const int button_code = GetButtonInfo(evt, m_dev, value);
if (button_code < 0 || value < 0) if (button_code < 0 || value < 0)
return; return;
@ -745,7 +738,7 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr<PadDevice>& devic
{ {
// get axis direction and skip on error or set to 0 if the stick/hat is actually pointing to the other direction. // get axis direction and skip on error or set to 0 if the stick/hat is actually pointing to the other direction.
// maybe mimic on error, needs investigation. FindAxisDirection should ideally never return -1 anyway // maybe mimic on error, needs investigation. FindAxisDirection should ideally never return -1 anyway
int direction = FindAxisDirection(axis_orientations, i); const int direction = FindAxisDirection(axis_orientations, i);
m_dev->cur_dir = direction; m_dev->cur_dir = direction;
if (direction < 0) if (direction < 0)
@ -778,8 +771,8 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr<PadDevice>& devic
if (!m_is_button_or_trigger && evt.type == EV_ABS) if (!m_is_button_or_trigger && evt.type == EV_ABS)
{ {
int index = BUTTON_COUNT + (idx * 2) + 1; const int index = BUTTON_COUNT + (idx * 2) + 1;
int min_direction = FindAxisDirection(axis_orientations, index); const int min_direction = FindAxisDirection(axis_orientations, index);
m_dev->cur_dir = min_direction; m_dev->cur_dir = min_direction;
if (min_direction < 0) if (min_direction < 0)
@ -804,8 +797,8 @@ void evdev_joystick_handler::get_mapping(const std::shared_ptr<PadDevice>& devic
if (!m_is_button_or_trigger && evt.type == EV_ABS) if (!m_is_button_or_trigger && evt.type == EV_ABS)
{ {
int index = BUTTON_COUNT + (idx * 2); const int index = BUTTON_COUNT + (idx * 2);
int max_direction = FindAxisDirection(axis_orientations, index); const int max_direction = FindAxisDirection(axis_orientations, index);
m_dev->cur_dir = max_direction; m_dev->cur_dir = max_direction;
if (max_direction < 0) if (max_direction < 0)
@ -859,21 +852,21 @@ void evdev_joystick_handler::apply_pad_data(const std::shared_ptr<PadDevice>& de
auto profile = device->config; auto profile = device->config;
// Handle vibration // Handle vibration
int idx_l = profile->switch_vibration_motors ? 1 : 0; const int idx_l = profile->switch_vibration_motors ? 1 : 0;
int idx_s = profile->switch_vibration_motors ? 0 : 1; const int idx_s = profile->switch_vibration_motors ? 0 : 1;
u16 force_large = profile->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value * 257 : vibration_min; const u16 force_large = profile->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].m_value * 257 : vibration_min;
u16 force_small = profile->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value * 257 : vibration_min; const u16 force_small = profile->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value * 257 : vibration_min;
SetRumble(evdev_device, force_large, force_small); SetRumble(evdev_device, force_large, force_small);
} }
// Search axis_orientations map for the direction by index, returns -1 if not found, 0 for positive and 1 for negative // Search axis_orientations map for the direction by index, returns -1 if not found, 0 for positive and 1 for negative
int evdev_joystick_handler::FindAxisDirection(const std::unordered_map<int, bool>& map, int index) int evdev_joystick_handler::FindAxisDirection(const std::unordered_map<int, bool>& map, int index)
{ {
auto it = map.find(index); if (const auto it = map.find(index); it != map.end())
if (it == map.end()) {
return -1;
else
return it->second; return it->second;
}
return -1;
} }
bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device) bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std::string& device)
@ -889,7 +882,7 @@ bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std
m_dev = std::make_shared<EvdevDevice>(); m_dev = std::make_shared<EvdevDevice>();
int index = static_cast<int>(bindings.size()); const int index = static_cast<int>(bindings.size());
m_pad_configs[index].load(); m_pad_configs[index].load();
m_dev->config = &m_pad_configs[index]; m_dev->config = &m_pad_configs[index];
pad_config* p_profile = m_dev->config; pad_config* p_profile = m_dev->config;
@ -921,13 +914,12 @@ bool evdev_joystick_handler::bindPadToDevice(std::shared_ptr<Pad> pad, const std
return static_cast<u32>(key); return static_cast<u32>(key);
}; };
auto evdevbutton = [&](const cfg::string& name) auto evdevbutton = [&, idx = i](const cfg::string& name)
{ {
int index = i;
EvdevButton button; EvdevButton button;
button.code = find_key(name); button.code = find_key(name);
button.type = last_type; button.type = last_type;
button.dir = axis_orientations[index]; button.dir = axis_orientations[idx];
return button; return button;
}; };