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");
evdev_joystick_handler::evdev_joystick_handler() : PadHandlerBase(pad_handler::evdev)
evdev_joystick_handler::evdev_joystick_handler()
: PadHandlerBase(pad_handler::evdev)
{
init_configs();
@ -103,8 +104,8 @@ bool evdev_joystick_handler::Init()
{
if (*static_cast<cfg::_bool*>(node.second))
{
std::string name = node.first;
int code = libevdev_event_code_from_name(EV_ABS, name.c_str());
const auto name = node.first;
const int code = libevdev_event_code_from_name(EV_ABS, name.c_str());
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);
else
@ -139,13 +140,13 @@ bool evdev_joystick_handler::update_device(const std::shared_ptr<PadDevice>& dev
const auto& path = evdev_device->path;
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 (was_connected)
{
int fd = libevdev_get_fd(dev);
const int fd = libevdev_get_fd(dev);
libevdev_free(dev);
close(fd);
dev = nullptr;
@ -158,15 +159,15 @@ bool evdev_joystick_handler::update_device(const std::shared_ptr<PadDevice>& dev
if (was_connected)
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)
{
int err = errno;
const int err = errno;
evdev_log.error("Failed to open joystick: %s [errno %d]", strerror(err), err);
return false;
}
int ret = libevdev_new_from_fd(fd, &dev);
const int ret = libevdev_new_from_fd(fd, &dev);
if (ret < 0)
{
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;
if (dev != nullptr)
{
int fd = libevdev_get_fd(dev);
const int fd = libevdev_get_fd(dev);
if (evdev_device->effect_id != -1)
ioctl(fd, EVIOCRMFF, evdev_device->effect_id);
libevdev_free(dev);
@ -216,35 +217,37 @@ std::unordered_map<u64, std::pair<u16, bool>> evdev_joystick_handler::GetButtonV
if (!Init())
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;
if (libevdev_fetch_event_value(dev, EV_KEY, code, &val) == 0)
continue;
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;
if (libevdev_fetch_event_value(dev, EV_ABS, code, &val) == 0)
continue;
int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code);
const int min = libevdev_get_abs_minimum(dev, code);
const int max = libevdev_get_abs_maximum(dev, code);
// 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())
{
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));
continue;
}
float fvalue = ScaleStickInput2(val, min, max);
const float fvalue = ScaleStickInput2(val, min, max);
if (fvalue < 0)
button_values.emplace(code, std::make_pair<u16, bool>(static_cast<u16>(std::abs(fvalue)), true));
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)
{
// 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)
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)
{
int key = FindKeyCodeByString(rev_axis_list, name, false);
bool dir = key >= 0;
const bool dir = key >= 0;
if (key < 0)
key = FindKeyCodeByString(axis_list, name, false);
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, "" };
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
if (padId.find("Xbox 360") != umax && code >= BTN_TRIGGER_HAPPY)
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())
continue;
u16 value = data[code].first;
const u16 value = data[code].first;
if (value > 0)
{
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)
continue;
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end())
continue;
u16 value = data[code].first;
const u16 value = data[code].first;
if (value > 0 && value >= m_thumb_threshold)
{
if (get_blacklist)
{
int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code);
const int min = libevdev_get_abs_minimum(dev, code);
const int max = libevdev_get_abs_maximum(dev, code);
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);
}
@ -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)
continue;
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), name) != blacklist.end())
continue;
u16 value = data[code].first;
const u16 value = data[code].first;
if (value > 0 && value >= m_thumb_threshold)
{
if (get_blacklist)
{
int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code);
const int min = libevdev_get_abs_minimum(dev, code);
const int max = libevdev_get_abs_maximum(dev, code);
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);
}
@ -421,7 +415,7 @@ void evdev_joystick_handler::SetRumble(std::shared_ptr<EvdevDevice>device, u16 l
if (!device || !device->has_rumble || device->effect_id == -2)
return;
int fd = libevdev_get_fd(device->device);
const int fd = libevdev_get_fd(device->device);
if (fd < 0)
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 code = evt.code;
int val = evt.value;
const int code = evt.code;
const int val = evt.value;
m_is_button_or_trigger = false;
switch (evt.type)
@ -541,8 +535,8 @@ int evdev_joystick_handler::GetButtonInfo(const input_event& evt, const std::sha
return -1;
}
auto& dev = device->device;
int min = libevdev_get_abs_minimum(dev, code);
int max = libevdev_get_abs_maximum(dev, code);
const int min = libevdev_get_abs_minimum(dev, code);
const int max = libevdev_get_abs_maximum(dev, code);
// 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())
@ -553,13 +547,12 @@ int evdev_joystick_handler::GetButtonInfo(const input_event& evt, const std::sha
return code;
}
float fvalue = ScaleStickInput2(val, min, max);
const float fvalue = ScaleStickInput2(val, min, max);
m_is_negative = fvalue < 0;
value = static_cast<u16>(std::abs(fvalue));
return code;
}
default:
return -1;
default: return -1;
}
}
@ -577,9 +570,9 @@ std::vector<std::string> evdev_joystick_handler::ListDevices()
// Check if the entry starts with event (a 5-letter word)
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;
int rc = libevdev_new_from_fd(fd, &dev);
const int rc = libevdev_new_from_fd(fd, &dev);
if (rc < 0)
{
// 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)
if (et.name.size() > 5 && et.name.compare(0, 5, "event") == 0)
{
std::string path = "/dev/input/" + et.name;
int fd = open(path.c_str(), O_RDWR | O_NONBLOCK);
const std::string path = "/dev/input/" + et.name;
const int fd = open(path.c_str(), O_RDWR | O_NONBLOCK);
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 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;
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)
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.
// 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;
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)
{
int index = BUTTON_COUNT + (idx * 2) + 1;
int min_direction = FindAxisDirection(axis_orientations, index);
const int index = BUTTON_COUNT + (idx * 2) + 1;
const int min_direction = FindAxisDirection(axis_orientations, index);
m_dev->cur_dir = min_direction;
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)
{
int index = BUTTON_COUNT + (idx * 2);
int max_direction = FindAxisDirection(axis_orientations, index);
const int index = BUTTON_COUNT + (idx * 2);
const int max_direction = FindAxisDirection(axis_orientations, index);
m_dev->cur_dir = max_direction;
if (max_direction < 0)
@ -859,22 +852,22 @@ void evdev_joystick_handler::apply_pad_data(const std::shared_ptr<PadDevice>& de
auto profile = device->config;
// Handle vibration
int idx_l = profile->switch_vibration_motors ? 1 : 0;
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;
u16 force_small = profile->enable_vibration_motor_small ? pad->m_vibrateMotors[idx_s].m_value * 257 : vibration_min;
const int idx_l = profile->switch_vibration_motors ? 1 : 0;
const int idx_s = profile->switch_vibration_motors ? 0 : 1;
const u16 force_large = profile->enable_vibration_motor_large ? pad->m_vibrateMotors[idx_l].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);
}
// 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)
{
auto it = map.find(index);
if (it == map.end())
return -1;
else
if (const auto it = map.find(index); it != map.end())
{
return it->second;
}
return -1;
}
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>();
int index = static_cast<int>(bindings.size());
const int index = static_cast<int>(bindings.size());
m_pad_configs[index].load();
m_dev->config = &m_pad_configs[index];
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);
};
auto evdevbutton = [&](const cfg::string& name)
auto evdevbutton = [&, idx = i](const cfg::string& name)
{
int index = i;
EvdevButton button;
button.code = find_key(name);
button.type = last_type;
button.dir = axis_orientations[index];
button.dir = axis_orientations[idx];
return button;
};