-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoutputdev.cpp
More file actions
279 lines (250 loc) · 8.61 KB
/
Copy pathoutputdev.cpp
File metadata and controls
279 lines (250 loc) · 8.61 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright 2017, Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>
This file is part of inputmap.
inputmap 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.
inputmap 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 inputmap. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/uinput.h>
#include <sys/epoll.h>
#include <algorithm>
#include "outputdev.h"
#include "inputdev.h"
#include "event-codes.h"
#include "devinput-parser.h"
OutputDevice::OutputDevice(const IniSection &ini, IInputByName &inputFinder)
{
std::string name = ini.find_single_value("name");
std::string phys = ini.find_single_value("phys");
std::string bus = ini.find_single_value("bus");
std::string vendor = ini.find_single_value("vendor");
std::string product = ini.find_single_value("product");
std::string version = ini.find_single_value("version");
if (name.empty())
name = "InputMap";
uinput_setup us = {};
if (!bus.empty())
us.id.bustype = bus_id(bus.c_str());
else
us.id.bustype = BUS_VIRTUAL;
us.id.version = parse_int(version, 1);
us.id.vendor = parse_hex_int(vendor, 0);
us.id.product = parse_hex_int(product, 0);
strcpy(us.name, name.c_str());
m_fd = FD_open("/dev/uinput", O_RDWR);
test(ioctl(m_fd.get(), UI_SET_PHYS, phys.c_str()), "UI_SET_PHYS");
bool has_rel = false;
for (const auto &kv : g_rel_names)
{
if (!kv.name)
continue;
std::string ref = ini.find_single_value(kv.name);
if (ref.empty())
continue;
m_rel.emplace_back(kv.id, parse_ref(ref, inputFinder));
if (!has_rel)
{
test(ioctl(m_fd.get(), UI_SET_EVBIT, EV_REL), "EV_REL");
has_rel = true;
}
test(ioctl(m_fd.get(), UI_SET_RELBIT, kv.id), "UI_SET_RELBIT");
}
bool has_key = false;
for (const auto &kv : g_key_names)
{
if (!kv.name)
continue;
std::string ref = ini.find_single_value(kv.name);
if (ref.empty())
continue;
m_key.emplace_back(kv.id, parse_ref(ref, inputFinder));
if (!has_key)
{
test(ioctl(m_fd.get(), UI_SET_EVBIT, EV_KEY), "EV_KEY");
has_key = true;
}
test(ioctl(m_fd.get(), UI_SET_KEYBIT, kv.id), "UI_SET_KEYBIT");
}
bool has_abs = false;
for (const auto &kv : g_abs_names)
{
if (!kv.name)
continue;
std::string ref = ini.find_single_value(kv.name);
if (ref.empty())
continue;
m_abs.emplace_back(kv.id, parse_ref(ref, inputFinder));
if (!has_abs)
{
test(ioctl(m_fd.get(), UI_SET_EVBIT, EV_ABS), "EV_ABS");
has_abs = true;
}
uinput_abs_setup abs = {};
abs.code = kv.id;
abs.absinfo.minimum = -32767;
abs.absinfo.maximum = 32767; //TODO: configure ABS range
test(ioctl(m_fd.get(), UI_ABS_SETUP, &abs), "abs");
}
bool has_ff = false;
for (const auto &kv : g_ff_names)
{
if (!kv.name)
continue;
std::string ref = ini.find_single_value(kv.name);
if (ref.empty())
continue;
auto pref = parse_ref(ref, inputFinder);
auto xref = dynamic_cast<ValueRef*>(pref.get());
if (!xref || xref->get_value_id().type != EV_FF || xref->get_value_id().code != kv.id)
{
throw std::runtime_error("FF ref must be a simple reference to the same FF value");
}
pref.release();
m_ff.emplace_back(kv.id, std::unique_ptr<ValueRef>(xref));
if (!has_ff)
{
us.ff_effects_max = 16;
test(ioctl(m_fd.get(), UI_SET_EVBIT, EV_FF), "EV_FF");
has_ff = true;
}
test(ioctl(m_fd.get(), UI_SET_FFBIT, kv.id), "UI_SET_FFBIT");
}
test(ioctl(m_fd.get(), UI_DEV_SETUP, &us), "UI_DEV_SETUP");
test(ioctl(m_fd.get(), UI_DEV_CREATE, 0), "UI_DEV_CREATE");
}
inline input_event create_event(int type, int code, int value)
{
input_event ev;
ev.type = type;
ev.code = code;
ev.value = value;
return ev;
}
inline void do_event(std::vector<input_event> &evs, int type, int code, ValueExpr *ref)
{
if (!ref)
return;
value_t value = ref->get_value();
if (type == EV_ABS)
value *= 32767;
evs.push_back(create_event(type, code, static_cast<int>(value)));
}
void OutputDevice::sync()
{
std::vector<input_event> evs;
for (auto &v: m_rel)
do_event(evs, EV_REL, v.first, v.second.get());
for (auto &v: m_key)
do_event(evs, EV_KEY, v.first, v.second.get());
for (auto &v: m_abs)
do_event(evs, EV_ABS, v.first, v.second.get());
if (!evs.empty())
{
evs.push_back(create_event(EV_SYN, SYN_REPORT, 0));
test(write(m_fd.get(), evs.data(), evs.size() * sizeof(input_event)), "write");
}
}
ValueRef *OutputDevice::get_ff(int id)
{
for (auto &v: m_ff)
if (v.first == id)
return v.second.get();
return nullptr;
}
PollResult OutputDevice::on_poll(int event)
{
if ((event & EPOLLIN) == 0)
return PollResult::None;
input_event ev;
int res = read(fd(), &ev, sizeof(input_event));
if (res == -1)
{
if (errno == EINTR)
return PollResult::None;
perror("output read");
return PollResult::Error;
}
//printf("EV %d %d %d\n", ev.type, ev.code, ev.value);
switch (ev.type)
{
case EV_UINPUT:
switch (ev.code)
{
case UI_FF_UPLOAD:
{
uinput_ff_upload ff{};
ff.request_id = ev.value;
test(ioctl(m_fd.get(), UI_BEGIN_FF_UPLOAD, &ff), "UI_BEGIN_FF_UPLOAD");
//printf("UPLOAD 0x%X, id=%d (%d, %d)\n", ff.effect.type, ff.effect.id, ff.effect.u.rumble.weak_magnitude, ff.effect.u.rumble.strong_magnitude);
ValueRef *ffout = get_ff(ff.effect.type);
auto device = ffout ? ffout->get_device() : nullptr;
int out_id = ff.effect.id;
int in_id = out_id < 0 || !device ? -EINVAL : device->ff_upload(ff.effect);
ff.retval = in_id < 0 ? in_id : 0;
test(ioctl(m_fd.get(), UI_END_FF_UPLOAD, &ff), "UI_END_FF_UPLOAD");
if (in_id >= 0)
{
if (static_cast<unsigned>(out_id) >= m_effects.size())
m_effects.resize(out_id + 1);
auto &effect = m_effects[out_id];
effect.device = device;
effect.input_id = in_id;
}
}
break;
case UI_FF_ERASE:
{
uinput_ff_erase ff{};
ff.request_id = ev.value;
test(ioctl(m_fd.get(), UI_BEGIN_FF_ERASE, &ff), "UI_BEGIN_FF_ERASE");
//printf("ERASE %d\n", ff.effect_id);
if (ff.effect_id < 0 || ff.effect_id >= m_effects.size())
{
ff.retval = -EINVAL;
}
else
{
auto &effect = m_effects[ff.effect_id];
auto device = effect.device.lock();
int in_id = effect.input_id;
effect = FFEffect{};
if (device)
{
int err = device->ff_erase(in_id);
if (err < 0)
ff.retval = err;
else
ff.retval = 0;
}
else
{
ff.retval = -EINVAL;
}
}
test(ioctl(m_fd.get(), UI_END_FF_ERASE, &ff), "UI_END_FF_ERASE");
}
break;
}
break;
case EV_FF:
{
//printf("FF %s id=%d\n", ev.value? "start" : "stop", ev.code);
if (ev.code >= 0 && ev.code < m_effects.size())
{
auto &effect = m_effects[ev.code];
auto device = effect.device.lock();
if (device)
device->ff_run(effect.input_id, ev.value != 0);
}
}
break;
}
return PollResult::None;
}