File: EvdevInjector.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (339 lines) | stat: -rw-r--r-- 9,801 bytes parent folder | download | duplicates (5)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "EvdevInjector.h"

#include <errno.h>
#include <inttypes.h>
#include <linux/input.h>
#include <log/log.h>
#include <string.h>
#include <sys/fcntl.h>
#include <unistd.h>

namespace android {
namespace dvr {

int EvdevInjector::UInput::Open() {
  errno = 0;
  fd_.reset(open("/dev/uinput", O_WRONLY | O_NONBLOCK));
  if (fd_.get() < 0) {
    ALOGE("couldn't open uinput (r=%d errno=%d)", fd_.get(), errno);
  }
  return errno;
}

int EvdevInjector::UInput::Close() {
  errno = 0;
  fd_.reset();
  return errno;
}

int EvdevInjector::UInput::Write(const void* buf, size_t count) {
  ALOGV("UInput::Write(%zu, %02X...)", count, *static_cast<const char*>(buf));
  errno = 0;
  ssize_t r = write(fd_.get(), buf, count);
  if (r != static_cast<ssize_t>(count)) {
    ALOGE("write(%zu) failed (r=%zd errno=%d)", count, r, errno);
  }
  return errno;
}

int EvdevInjector::UInput::IoctlSetInt(int request, int value) {
  ALOGV("UInput::IoctlSetInt(0x%X, 0x%X)", request, value);
  errno = 0;
  if (const int status = ioctl(fd_.get(), request, value)) {
    ALOGE("ioctl(%d, 0x%X, 0x%X) failed (r=%d errno=%d)", fd_.get(), request,
          value, status, errno);
  }
  return errno;
}

int EvdevInjector::UInput::IoctlVoid(int request) {
  ALOGV("UInput::IoctlVoid(0x%X)", request);
  errno = 0;
  if (const int status = ioctl(fd_.get(), request)) {
    ALOGE("ioctl(%d, 0x%X) failed (r=%d errno=%d)", fd_.get(), request, status,
          errno);
  }
  return errno;
}

void EvdevInjector::Close() {
  uinput_->Close();
  state_ = State::CLOSED;
}

int EvdevInjector::ConfigureBegin(const char* device_name, int16_t bustype,
                                  int16_t vendor, int16_t product,
                                  int16_t version) {
  ALOGV("ConfigureBegin %s 0x%04" PRIX16 " 0x%04" PRIX16 " 0x%04" PRIX16
        " 0x%04" PRIX16 "",
        device_name, bustype, vendor, product, version);
  if (!device_name || strlen(device_name) >= UINPUT_MAX_NAME_SIZE) {
    return Error(ERROR_DEVICE_NAME);
  }
  if (const int status = RequireState(State::NEW)) {
    return status;
  }
  if (!uinput_) {
    owned_uinput_.reset(new EvdevInjector::UInput());
    uinput_ = owned_uinput_.get();
  }
  if (const int status = uinput_->Open()) {
    // Without uinput we're dead in the water.
    state_ = State::CLOSED;
    return Error(status);
  }
  state_ = State::CONFIGURING;
  // Initialize device setting structure.
  memset(&uidev_, 0, sizeof(uidev_));
  strncpy(uidev_.name, device_name, UINPUT_MAX_NAME_SIZE);
  uidev_.id.bustype = bustype;
  uidev_.id.vendor = vendor;
  uidev_.id.product = product;
  uidev_.id.version = version;
  return 0;
}

int EvdevInjector::ConfigureInputProperty(int property) {
  ALOGV("ConfigureInputProperty %d", property);
  if (property < 0 || property >= INPUT_PROP_CNT) {
    ALOGE("property 0x%X out of range [0,0x%X)", property, INPUT_PROP_CNT);
    return Error(ERROR_PROPERTY_RANGE);
  }
  if (const int status = RequireState(State::CONFIGURING)) {
    return status;
  }
  if (const int status = uinput_->IoctlSetInt(UI_SET_PROPBIT, property)) {
    ALOGE("failed to set property %d", property);
    return Error(status);
  }
  return 0;
}

int EvdevInjector::ConfigureKey(uint16_t key) {
  ALOGV("ConfigureKey 0x%02" PRIX16 "", key);
  if (key < 0 || key >= KEY_CNT) {
    ALOGE("key 0x%X out of range [0,0x%X)", key, KEY_CNT);
    return Error(ERROR_KEY_RANGE);
  }
  if (const int status = RequireState(State::CONFIGURING)) {
    return status;
  }
  if (const int status = EnableEventType(EV_KEY)) {
    return status;
  }
  if (const int status = uinput_->IoctlSetInt(UI_SET_KEYBIT, key)) {
    ALOGE("failed to enable EV_KEY 0x%02" PRIX16 "", key);
    return Error(status);
  }
  return 0;
}

int EvdevInjector::ConfigureAbs(uint16_t abs_type, int32_t min, int32_t max,
                                int32_t fuzz, int32_t flat) {
  ALOGV("ConfigureAbs 0x%" PRIX16 " %" PRId32 " %" PRId32 " %" PRId32
        " %" PRId32 "",
        abs_type, min, max, fuzz, flat);
  if (abs_type < 0 || abs_type >= ABS_CNT) {
    ALOGE("EV_ABS type 0x%" PRIX16 " out of range [0,0x%X)", abs_type, ABS_CNT);
    return Error(ERROR_ABS_RANGE);
  }
  if (const int status = RequireState(State::CONFIGURING)) {
    return status;
  }
  if (const int status = EnableEventType(EV_ABS)) {
    return status;
  }
  if (const int status = uinput_->IoctlSetInt(UI_SET_ABSBIT, abs_type)) {
    ALOGE("failed to enable EV_ABS 0x%" PRIX16 "", abs_type);
    return Error(status);
  }
  uidev_.absmin[abs_type] = min;
  uidev_.absmax[abs_type] = max;
  uidev_.absfuzz[abs_type] = fuzz;
  uidev_.absflat[abs_type] = flat;
  return 0;
}

int EvdevInjector::ConfigureMultiTouchXY(int x0, int y0, int x1, int y1) {
  if (const int status = ConfigureAbs(ABS_MT_POSITION_X, x0, x1, 0, 0)) {
    return status;
  }
  if (const int status = ConfigureAbs(ABS_MT_POSITION_Y, y0, y1, 0, 0)) {
    return status;
  }
  return 0;
}

int EvdevInjector::ConfigureAbsSlots(int slots) {
  return ConfigureAbs(ABS_MT_SLOT, 0, slots, 0, 0);
}

int EvdevInjector::ConfigureRel(uint16_t rel_type) {
  ALOGV("ConfigureRel 0x%" PRIX16 "", rel_type);
  if (rel_type < 0 || rel_type >= REL_CNT) {
    ALOGE("EV_REL type 0x%" PRIX16 " out of range [0,0x%X)", rel_type, REL_CNT);
    return Error(ERROR_REL_RANGE);
  }
  if (const int status = RequireState(State::CONFIGURING)) {
    return status;
  }
  if (const int status = EnableEventType(EV_REL)) {
    return status;
  }
  if (const int status = uinput_->IoctlSetInt(UI_SET_RELBIT, rel_type)) {
    ALOGE("failed to enable EV_REL 0x%" PRIX16 "", rel_type);
    return Error(status);
  }
  return 0;
}

int EvdevInjector::ConfigureEnd() {
  ALOGV("ConfigureEnd:");
  ALOGV("  name=\"%s\"", uidev_.name);
  ALOGV("  id.bustype=0x%04" PRIX16, uidev_.id.bustype);
  ALOGV("  id.vendor=0x%04" PRIX16, uidev_.id.vendor);
  ALOGV("  id.product=0x%04" PRIX16, uidev_.id.product);
  ALOGV("  id.version=0x%04" PRIX16, uidev_.id.version);
  ALOGV("  ff_effects_max=%" PRIu32, uidev_.ff_effects_max);
  for (int i = 0; i < ABS_CNT; ++i) {
    if (uidev_.absmin[i]) {
      ALOGV("  absmin[%d]=%" PRId32, i, uidev_.absmin[i]);
    }
    if (uidev_.absmax[i]) {
      ALOGV("  absmax[%d]=%" PRId32, i, uidev_.absmax[i]);
    }
    if (uidev_.absfuzz[i]) {
      ALOGV("  absfuzz[%d]=%" PRId32, i, uidev_.absfuzz[i]);
    }
    if (uidev_.absflat[i]) {
      ALOGV("  absflat[%d]=%" PRId32, i, uidev_.absflat[i]);
    }
  }

  if (const int status = RequireState(State::CONFIGURING)) {
    return status;
  }
  // Write out device settings.
  if (const int status = uinput_->Write(&uidev_, sizeof uidev_)) {
    ALOGE("failed to write device settings");
    return Error(status);
  }
  // Create device node.
  if (const int status = uinput_->IoctlVoid(UI_DEV_CREATE)) {
    ALOGE("failed to create device node");
    return Error(status);
  }
  state_ = State::READY;
  return 0;
}

int EvdevInjector::Send(uint16_t type, uint16_t code, int32_t value) {
  ALOGV("Send(0x%" PRIX16 ", 0x%" PRIX16 ", 0x%" PRIX32 ")", type, code, value);
  if (const int status = RequireState(State::READY)) {
    return status;
  }
  struct input_event event;
  memset(&event, 0, sizeof(event));
  event.type = type;
  event.code = code;
  event.value = value;
  if (const int status = uinput_->Write(&event, sizeof(event))) {
    ALOGE("failed to write event 0x%" PRIX16 ", 0x%" PRIX16 ", 0x%" PRIX32,
          type, code, value);
    return Error(status);
  }
  return 0;
}

int EvdevInjector::SendSynReport() { return Send(EV_SYN, SYN_REPORT, 0); }

int EvdevInjector::SendKey(uint16_t code, int32_t value) {
  return Send(EV_KEY, code, value);
}

int EvdevInjector::SendAbs(uint16_t code, int32_t value) {
  return Send(EV_ABS, code, value);
}

int EvdevInjector::SendRel(uint16_t code, int32_t value) {
  return Send(EV_REL, code, value);
}

int EvdevInjector::SendMultiTouchSlot(int32_t slot) {
  if (latest_slot_ != slot) {
    if (const int status = SendAbs(ABS_MT_SLOT, slot)) {
      return status;
    }
    latest_slot_ = slot;
  }
  return 0;
}

int EvdevInjector::SendMultiTouchXY(int32_t slot, int32_t id, int32_t x,
                                    int32_t y) {
  if (const int status = SendMultiTouchSlot(slot)) {
    return status;
  }
  if (const int status = SendAbs(ABS_MT_TRACKING_ID, id)) {
    return status;
  }
  if (const int status = SendAbs(ABS_MT_POSITION_X, x)) {
    return status;
  }
  if (const int status = SendAbs(ABS_MT_POSITION_Y, y)) {
    return status;
  }
  return 0;
}

int EvdevInjector::SendMultiTouchLift(int32_t slot) {
  if (const int status = SendMultiTouchSlot(slot)) {
    return status;
  }
  if (const int status = SendAbs(ABS_MT_TRACKING_ID, -1)) {
    return status;
  }
  return 0;
}

int EvdevInjector::Error(int code) {
  if (!error_) {
    error_ = code;
  }
  return code;
}

int EvdevInjector::RequireState(State required_state) {
  if (error_) {
    return error_;
  }
  if (state_ != required_state) {
    ALOGE("in state %d but require state %d", static_cast<int>(state_),
          static_cast<int>(required_state));
    return Error(ERROR_SEQUENCING);
  }
  return 0;
}

int EvdevInjector::EnableEventType(uint16_t type) {
  if (const int status = RequireState(State::CONFIGURING)) {
    return status;
  }
  if (enabled_event_types_.count(type) > 0) {
    return 0;
  }
  if (const int status = uinput_->IoctlSetInt(UI_SET_EVBIT, type)) {
    ALOGE("failed to enable event type 0x%X", type);
    return Error(status);
  }
  enabled_event_types_.insert(type);
  return 0;
}

void EvdevInjector::dumpInternal(String8& result) {
  result.appendFormat("injector_state = %d\n", static_cast<int>(state_));
  result.appendFormat("injector_error = %d\n", error_);
}

}  // namespace dvr
}  // namespace android