-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudevconnection.h
203 lines (163 loc) · 5.85 KB
/
udevconnection.h
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#pragma once
#include <string>
#include <algorithm>
#include <filesystem>
extern "C" {
#include <libudev.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
}
#include "onreturn.h"
#include "utils.h"
struct UdevConnection {
UdevConnection()
{
context = udev_new();
if (!context) {
fprintf(stderr, "Failed to connect to udev\n");
return;
}
udevMonitor = udev_monitor_new_from_netlink(context, "udev");
if (!udevMonitor) {
fprintf(stderr, "Failed to create udev monitor\n");
return;
}
udev_monitor_filter_add_match_subsystem_devtype(udevMonitor, "input", 0);
udev_monitor_enable_receiving(udevMonitor);
udevSocketFd = udev_monitor_get_fd(udevMonitor);
udevAvailable = true;
init();
}
static std::string devicePath(udev_device *dev)
{
std::string linkPath;
const std::string sysName = std_sux::string(udev_device_get_property_value(dev, "DEVNAME"));
if (!sysName.empty()) {
linkPath = sysName;
} else {
if (s_verbose) puts("Falling back");
udev_list_entry *devLink= udev_device_get_devlinks_list_entry(dev);
if (!devLink) {
return {};
}
linkPath = std_sux::string(udev_list_entry_get_name(devLink));
}
return linkPath;
}
std::string addKeyboard(udev_device *dev)
{
const std::string id = udev_device_get_devpath(dev);
const std::string isKeyboard = std_sux::string(udev_device_get_property_value(dev, "ID_INPUT_KEYBOARD"));
const std::string isKey = std_sux::string(udev_device_get_property_value(dev, "ID_INPUT_KEY"));
if (isKeyboard != "1" && isKey != "1") {
if (s_verbose) fprintf(stderr, "!!!!!!!! Skipping non-keyboard %s\n", id.c_str());
if (s_veryVerbose) printProperties(dev);
if (s_verbose) fprintf(stderr, " -------------\n");
return "";
}
// It's a list entry, but we only need one
std::string linkPath = devicePath(dev);
if (linkPath.empty() || !std::filesystem::exists(linkPath)) {
if (s_verbose) fprintf(stderr, "Skipping device not in /dev: %s (%s)\n", id.c_str(), linkPath.c_str());
if (s_veryVerbose) printProperties(dev);
return "";
}
// Not initialized yet
if (!udev_device_get_is_initialized(dev)) {
if (s_verbose) printf("%s not initialized yet\n", linkPath.c_str());
return "";
}
if (keyboardPaths.count(id) != 0) {
if (s_verbose) printf("%s already added\n", linkPath.c_str());
return "";
}
if (s_verbose) fprintf(stdout, "Found keyboard: %s: %s\n", id.c_str(), linkPath.c_str());
if (s_veryVerbose) printProperties(dev);
keyboardPaths[id] = linkPath;
return linkPath;
}
void init()
{
udev_enumerate *enumerate = udev_enumerate_new(context);
udev_enumerate_add_match_subsystem(enumerate, "input");
udev_enumerate_scan_devices(enumerate);
udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry *entry = nullptr;
udev_list_entry_foreach(entry, devices) {
const char *path = udev_list_entry_get_name(entry);
if (!path) {
fprintf(stderr, "Invalid device when listing\n");
continue;
}
udev_device *dev = udev_device_new_from_syspath(context, path);
if (!dev) {
fprintf(stderr, "failed getting %s\n", path);
continue;
}
addKeyboard(dev);
udev_device_unref(dev);
}
udev_enumerate_unref(enumerate);
if (s_verbose) printf("Got %ld keyboards\n", keyboardPaths.size());
}
~UdevConnection()
{
if (udevMonitor) {
udev_monitor_unref(udevMonitor);
}
if (context) {
udev_unref(context);
}
}
void printProperties(udev_device *dev)
{
fprintf(stderr, "sysname: %s\n", udev_device_get_sysname(dev));
udev_list_entry *entry = udev_device_get_properties_list_entry(dev);
while (entry) {
const char *name = udev_list_entry_get_name(entry);
const char *value = udev_list_entry_get_value(entry);
fprintf(stderr, "property name: %s value %s\n", name, value);
entry = udev_list_entry_get_next(entry);
}
}
enum UpdateResult {
NoUpdate,
KeyboardAdded,
KeyboardRemoved
};
UpdateResult update(std::string *keyboardPath)
{
if (!udevAvailable) {
fprintf(stderr, "udev unavailable\n");
return NoUpdate;
}
udev_device *dev = udev_monitor_receive_device(udevMonitor);
OnReturn releaseDev([&]() {
udev_device_unref(dev);
});
const std::string id = udev_device_get_devpath(dev);
const std::string action = udev_device_get_action(dev);
if (s_verbose) printf("udev action: %s for id %s\n", action.c_str(), id.c_str());
if (action == "remove" || action == "offline") {
if (!keyboardPaths.contains(id)) {
return NoUpdate;
}
*keyboardPath = keyboardPaths[id];
keyboardPaths.erase(id);
return KeyboardRemoved;
}
std::string path = addKeyboard(dev);
if (!path.empty()) {
*keyboardPath = path;
return KeyboardAdded;
}
return NoUpdate;
}
udev *context = nullptr;
udev_monitor *udevMonitor = nullptr;
bool udevAvailable = false;
int udevSocketFd = -1;
std::unordered_map<std::string, std::string> keyboardPaths;
};