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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include "../include/GlobalHotkeysJoystick.hpp"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/eventfd.h>
namespace gsr {
static constexpr double double_click_timeout_seconds = 0.33;
// Returns -1 on error
static int get_js_dev_input_id_from_filepath(const char *dev_input_filepath) {
if(strncmp(dev_input_filepath, "/dev/input/js", 13) != 0)
return -1;
int dev_input_id = -1;
if(sscanf(dev_input_filepath + 13, "%d", &dev_input_id) == 1)
return dev_input_id;
return -1;
}
GlobalHotkeysJoystick::~GlobalHotkeysJoystick() {
if(event_fd > 0) {
const uint64_t exit = 1;
write(event_fd, &exit, sizeof(exit));
}
if(read_thread.joinable())
read_thread.join();
if(event_fd > 0)
close(event_fd);
for(int i = 0; i < num_poll_fd; ++i) {
close(poll_fd[i].fd);
}
}
bool GlobalHotkeysJoystick::start() {
if(num_poll_fd > 0)
return false;
event_fd = eventfd(0, 0);
if(event_fd <= 0)
return false;
event_index = num_poll_fd;
poll_fd[num_poll_fd] = {
event_fd,
POLLIN,
0
};
extra_data[num_poll_fd] = {
-1
};
++num_poll_fd;
if(!hotplug.start()) {
fprintf(stderr, "Warning: failed to setup hotplugging\n");
} else {
hotplug_poll_index = num_poll_fd;
poll_fd[num_poll_fd] = {
hotplug.steal_fd(),
POLLIN,
0
};
extra_data[num_poll_fd] = {
-1
};
++num_poll_fd;
}
char dev_input_path[128];
for(int i = 0; i < 8; ++i) {
snprintf(dev_input_path, sizeof(dev_input_path), "/dev/input/js%d", i);
add_device(dev_input_path, false);
}
if(num_poll_fd == 0)
fprintf(stderr, "Info: no joysticks found, assuming they might be connected later\n");
read_thread = std::thread(&GlobalHotkeysJoystick::read_events, this);
return true;
}
bool GlobalHotkeysJoystick::bind_action(const std::string &id, GlobalHotkeyCallback callback) {
return bound_actions_by_id.insert(std::make_pair(id, std::move(callback))).second;
}
void GlobalHotkeysJoystick::poll_events() {
if(num_poll_fd == 0)
return;
if(save_replay) {
save_replay = false;
auto it = bound_actions_by_id.find("save_replay");
if(it != bound_actions_by_id.end())
it->second("save_replay");
}
}
void GlobalHotkeysJoystick::read_events() {
js_event event;
while(poll(poll_fd, num_poll_fd, -1) > 0) {
for(int i = 0; i < num_poll_fd; ++i) {
if(poll_fd[i].revents & (POLLHUP|POLLERR|POLLNVAL)) {
if(i == event_index)
goto done;
if(remove_poll_fd(i))
--i; // This item was removed so we want to repeat the same index to continue to the next item
continue;
}
if(!(poll_fd[i].revents & POLLIN))
continue;
if(i == event_index) {
goto done;
} else if(i == hotplug_poll_index) {
hotplug.process_event_data(poll_fd[i].fd, [&](HotplugAction hotplug_action, const char *devname) {
char dev_input_filepath[1024];
snprintf(dev_input_filepath, sizeof(dev_input_filepath), "/dev/%s", devname);
switch(hotplug_action) {
case HotplugAction::ADD: {
// Cant open the /dev/input device immediately or it fails.
// TODO: Remove this hack when a better solution is found.
usleep(50 * 1000);
add_device(dev_input_filepath);
break;
}
case HotplugAction::REMOVE: {
if(remove_device(dev_input_filepath))
--i; // This item was removed so we want to repeat the same index to continue to the next item
break;
}
}
});
} else {
process_js_event(poll_fd[i].fd, event);
}
}
}
done:
;
}
void GlobalHotkeysJoystick::process_js_event(int fd, js_event &event) {
if(read(fd, &event, sizeof(event)) != sizeof(event))
return;
if((event.type & JS_EVENT_BUTTON) == 0)
return;
if(event.number == 8 && event.value == 1) {
++num_times_clicked;
if(num_times_clicked == 1)
double_click_clock.restart();
else if(num_times_clicked == 2 && double_click_clock.restart() >= double_click_timeout_seconds)
num_times_clicked = 0;
if(num_times_clicked == 2) {
save_replay = true;
num_times_clicked = 0;
}
}
}
bool GlobalHotkeysJoystick::add_device(const char *dev_input_filepath, bool print_error) {
if(num_poll_fd >= max_js_poll_fd) {
fprintf(stderr, "Warning: failed to add joystick device %s, too many joysticks have been added\n", dev_input_filepath);
return false;
}
const int dev_input_id = get_js_dev_input_id_from_filepath(dev_input_filepath);
if(dev_input_id == -1)
return false;
const int fd = open(dev_input_filepath, O_RDONLY);
if(fd <= 0) {
if(print_error)
fprintf(stderr, "Error: failed to add joystick %s, error: %s\n", dev_input_filepath, strerror(errno));
return false;
}
poll_fd[num_poll_fd] = {
fd,
POLLIN,
0
};
extra_data[num_poll_fd] = {
dev_input_id
};
++num_poll_fd;
fprintf(stderr, "Info: added joystick: %s\n", dev_input_filepath);
return true;
}
bool GlobalHotkeysJoystick::remove_device(const char *dev_input_filepath) {
const int dev_input_id = get_js_dev_input_id_from_filepath(dev_input_filepath);
if(dev_input_id == -1)
return false;
const int poll_fd_index = get_poll_fd_index_by_dev_input_id(dev_input_id);
if(poll_fd_index == -1)
return false;
fprintf(stderr, "Info: removed joystick: %s\n", dev_input_filepath);
return remove_poll_fd(poll_fd_index);
}
bool GlobalHotkeysJoystick::remove_poll_fd(int index) {
if(index < 0 || index >= num_poll_fd)
return false;
close(poll_fd[index].fd);
for(int i = index + 1; i < num_poll_fd; ++i) {
poll_fd[i - 1] = poll_fd[i];
extra_data[i - 1] = extra_data[i];
}
--num_poll_fd;
return true;
}
int GlobalHotkeysJoystick::get_poll_fd_index_by_dev_input_id(int dev_input_id) const {
for(int i = 0; i < num_poll_fd; ++i) {
if(dev_input_id == extra_data[i].dev_input_id)
return i;
}
return -1;
}
}
|