Repository navigation
Expand file tree
/
Copy pathUInputDevice.cpp
More file actions
102 lines (81 loc) · 2.44 KB
/
Copy pathUInputDevice.cpp
File metadata and controls
102 lines (81 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
libevdev-cpp - Minimal C++ libevdev wrapper for InputActions
Copyright (C) 2006 Marcin Woźniak
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 <https://www.gnu.org/licenses/>.
*/
#include "UInputDevice.h"
#include "Device.h"
#include "exceptions.h"
#include "logging.h"
#include <fcntl.h>
#include <libevdev/libevdev-uinput.h>
#include <libevdev/libevdev.h>
namespace InputActions::libevdev
{
UInputDevice::UInputDevice(libevdev_uinput *device)
: m_device(device)
{
}
UInputDevice::UInputDevice(UInputDevice &&other)
{
*this = std::move(other);
}
UInputDevice::~UInputDevice()
{
if (!m_device) {
return;
}
libevdev_uinput_destroy(m_device);
}
UInputDevice UInputDevice::createManaged(Device *device, const QString &name)
{
const auto oldName = device->name();
if (!name.isEmpty()) {
device->setName(name);
}
libevdev_uinput *uinput;
const auto error = libevdev_uinput_create_from_device(device->raw(), LIBEVDEV_UINPUT_OPEN_MANAGED, &uinput);
if (!name.isEmpty()) {
device->setName(oldName);
}
if (error) {
qWarning(LIBEVDEV_CPP, "libevdev_uinput_create_from_device failed: %d", -error);
throw UInputDeviceCreationException(-error);
}
return UInputDevice(uinput);
}
int UInputDevice::writeEvent(int type, int code, int value)
{
return libevdev_uinput_write_event(m_device, type, code, value);
}
int UInputDevice::writeSynReportEvent()
{
return writeEvent(EV_SYN, SYN_REPORT, 0);
}
int UInputDevice::fd() const
{
return libevdev_uinput_get_fd(m_device);
}
QString UInputDevice::devNode() const
{
return libevdev_uinput_get_devnode(m_device);
}
void UInputDevice::removeNonBlockFlag()
{
fcntl(fd(), F_SETFL, fcntl(fd(), F_GETFL, 0) & ~O_NONBLOCK);
}
UInputDevice &UInputDevice::operator=(UInputDevice &&other)
{
std::swap(m_device, other.m_device);
return *this;
}
}