Repository navigation
Expand file tree
/
Copy pathPathContext.cpp
More file actions
113 lines (94 loc) · 2.78 KB
/
Copy pathPathContext.cpp
File metadata and controls
113 lines (94 loc) · 2.78 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
103
104
105
106
107
108
109
110
111
112
113
/*
libinput-cpp - C++ libinput wrapper for InputActions
Copyright (C) 2026 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 "PathContext.h"
#include "Device.h"
#include "Event.h"
#include <fcntl.h>
#include <libinput.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <unistd.h>
namespace InputActions::libinput
{
PathContext::PathContext()
{
static libinput_interface interface{
.open_restricted = &PathContext::openRestricted,
.close_restricted = &PathContext::closeRestricted,
};
m_libinput = libinput_path_create_context(&interface, this);
m_notifier = std::make_unique<QSocketNotifier>(fd(), QSocketNotifier::Read);
connect(m_notifier.get(), &QSocketNotifier::activated, this, [this]() {
Q_EMIT eventsAvailable();
});
}
PathContext::~PathContext()
{
m_devices.clear();
libinput_unref(m_libinput);
}
int PathContext::fd() const
{
return libinput_get_fd(m_libinput);
}
Device *PathContext::addDevice(const QString &path, bool grab)
{
m_grab = grab;
auto *libinputDevice = libinput_path_add_device(m_libinput, path.toStdString().c_str());
if (!libinputDevice) {
return {};
}
libinput_device_ref(libinputDevice);
auto device = std::make_unique<Device>(libinputDevice);
auto *devicePtr = device.get();
m_devices.push_back(std::move(device));
return devicePtr;
}
void PathContext::removeDevice(Device *device)
{
libinput_path_remove_device(device->raw());
std::erase_if(m_devices, [device](auto &existingDevice) {
return existingDevice.get() == device;
});
}
void PathContext::dispatch()
{
libinput_dispatch(m_libinput);
}
std::optional<Event> PathContext::getEvent()
{
auto *event = libinput_get_event(m_libinput);
if (!event) {
return {};
}
return {event};
}
int PathContext::openRestricted(const char *path, int flags, void *data)
{
const auto *self = static_cast<PathContext *>(data);
const auto fd = open(path, flags);
if (fd < 0) {
return -errno;
}
if (self->m_grab) {
ioctl(fd, EVIOCGRAB, 1);
}
return fd;
}
void PathContext::closeRestricted(int fd, void *data)
{
close(fd);
}
}