2020-06-24 02:49:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019-2020 PixlOne
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-07-17 05:53:51 +00:00
|
|
|
#include <system_error>
|
2020-07-05 06:55:46 +00:00
|
|
|
|
|
|
|
#include "InputDevice.h"
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <libevdev/libevdev.h>
|
|
|
|
#include <libevdev/libevdev-uinput.h>
|
2020-07-14 04:12:40 +00:00
|
|
|
}
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2019-10-05 01:56:17 +00:00
|
|
|
using namespace logid;
|
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
InputDevice::InvalidEventCode::InvalidEventCode(const std::string& name) :
|
|
|
|
_what ("Invalid event code " + name)
|
2020-07-05 06:55:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* InputDevice::InvalidEventCode::what() const noexcept
|
|
|
|
{
|
|
|
|
return _what.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputDevice::InputDevice(const char* name)
|
2019-07-17 05:53:51 +00:00
|
|
|
{
|
|
|
|
device = libevdev_new();
|
|
|
|
libevdev_set_name(device, name);
|
|
|
|
|
|
|
|
libevdev_enable_event_type(device, EV_KEY);
|
|
|
|
libevdev_enable_event_type(device, EV_REL);
|
2020-07-14 04:12:40 +00:00
|
|
|
for(unsigned int i = 0; i < REL_CNT; i++)
|
2019-07-17 05:53:51 +00:00
|
|
|
libevdev_enable_event_code(device, EV_REL, i, nullptr);
|
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
int err = libevdev_uinput_create_from_device(device,
|
|
|
|
LIBEVDEV_UINPUT_OPEN_MANAGED, &ui_device);
|
2019-07-17 05:53:51 +00:00
|
|
|
|
2020-12-14 18:49:28 +00:00
|
|
|
if(err != 0) {
|
|
|
|
libevdev_free(device);
|
2019-07-17 05:53:51 +00:00
|
|
|
throw std::system_error(-err, std::generic_category());
|
2020-12-14 18:49:28 +00:00
|
|
|
}
|
2019-07-17 05:53:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
InputDevice::~InputDevice()
|
2019-08-08 22:22:54 +00:00
|
|
|
{
|
2020-07-05 06:55:46 +00:00
|
|
|
libevdev_uinput_destroy(ui_device);
|
|
|
|
libevdev_free(device);
|
2019-08-08 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:02:44 +00:00
|
|
|
void InputDevice::registerKey(uint code)
|
|
|
|
{
|
|
|
|
libevdev_enable_event_code(device, EV_KEY, code, nullptr);
|
|
|
|
int err = libevdev_uinput_create_from_device(device,
|
|
|
|
LIBEVDEV_UINPUT_OPEN_MANAGED, &ui_device);
|
|
|
|
|
|
|
|
if(err != 0) {
|
|
|
|
libevdev_free(device);
|
|
|
|
throw std::system_error(-err, std::generic_category());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
void InputDevice::moveAxis(uint axis, int movement)
|
2019-07-17 05:53:51 +00:00
|
|
|
{
|
2020-07-05 06:55:46 +00:00
|
|
|
_sendEvent(EV_REL, axis, movement);
|
2019-07-17 05:53:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
void InputDevice::pressKey(uint code)
|
2019-07-17 05:53:51 +00:00
|
|
|
{
|
2020-07-05 06:55:46 +00:00
|
|
|
_sendEvent(EV_KEY, code, 1);
|
2019-07-17 05:53:51 +00:00
|
|
|
}
|
2020-07-05 06:55:46 +00:00
|
|
|
|
|
|
|
void InputDevice::releaseKey(uint code)
|
|
|
|
{
|
|
|
|
_sendEvent(EV_KEY, code, 0);
|
|
|
|
}
|
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
uint InputDevice::toKeyCode(const std::string& name)
|
2020-07-05 06:55:46 +00:00
|
|
|
{
|
2020-07-14 04:12:40 +00:00
|
|
|
return _toEventCode(EV_KEY, name);
|
2020-07-05 06:55:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 04:12:40 +00:00
|
|
|
uint InputDevice::toAxisCode(const std::string& name)
|
2020-07-05 06:55:46 +00:00
|
|
|
{
|
2020-07-14 04:12:40 +00:00
|
|
|
return _toEventCode(EV_REL, name);
|
2020-07-05 06:55:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 20:05:20 +00:00
|
|
|
/* Returns -1 if axis_code is not hi-res */
|
|
|
|
int InputDevice::getLowResAxis(const uint axis_code)
|
|
|
|
{
|
2020-08-22 01:02:24 +00:00
|
|
|
/* Some systems don't have these hi-res axes */
|
|
|
|
#ifdef REL_WHEEL_HI_RES
|
2020-08-21 20:05:20 +00:00
|
|
|
if(axis_code == REL_WHEEL_HI_RES)
|
|
|
|
return REL_WHEEL;
|
2020-08-22 01:02:24 +00:00
|
|
|
#endif
|
|
|
|
#ifdef REL_HWHEEL_HI_RES
|
2020-08-21 20:05:20 +00:00
|
|
|
if(axis_code == REL_HWHEEL_HI_RES)
|
|
|
|
return REL_HWHEEL;
|
2020-08-22 01:02:24 +00:00
|
|
|
#endif
|
2020-08-21 20:05:20 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-07-05 06:55:46 +00:00
|
|
|
uint InputDevice::_toEventCode(uint type, const std::string& name)
|
|
|
|
{
|
|
|
|
int code = libevdev_event_code_from_name(type, name.c_str());
|
|
|
|
|
|
|
|
if(code == -1)
|
|
|
|
throw InvalidEventCode(name);
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputDevice::_sendEvent(uint type, uint code, int value)
|
|
|
|
{
|
|
|
|
libevdev_uinput_write_event(ui_device, type, code, value);
|
|
|
|
libevdev_uinput_write_event(ui_device, EV_SYN, SYN_REPORT, 0);
|
2020-12-14 19:02:44 +00:00
|
|
|
}
|