1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "virtual_device_base.h"
18
19 #include <glog/logging.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "log/log.h"
26
27 using vsoc_input_service::VirtualDeviceBase;
28
29 namespace {
30
DoIoctl(int fd,int request,const uint32_t value)31 bool DoIoctl(int fd, int request, const uint32_t value) {
32 int rc = ioctl(fd, request, value);
33 if (rc < 0) {
34 SLOGE("ioctl failed (%s)", strerror(errno));
35 return false;
36 }
37 return true;
38 }
39
40 } // namespace
41
VirtualDeviceBase(const char * device_name,uint16_t product_id)42 VirtualDeviceBase::VirtualDeviceBase(const char* device_name,
43 uint16_t product_id)
44 : device_name_(device_name),
45 bus_type_(BUS_USB),
46 vendor_id_(0x6006),
47 product_id_(product_id),
48 version_(1) {}
49
~VirtualDeviceBase()50 VirtualDeviceBase::~VirtualDeviceBase() {
51 if (fd_ >= 0) {
52 close(fd_);
53 fd_ = -1;
54 }
55 }
56
SetUp()57 bool VirtualDeviceBase::SetUp() {
58 fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
59 if (fd_ < 0) {
60 SLOGE("Failed to open /dev/uinput (%s)", strerror(errno));
61 return false;
62 }
63
64 strncpy(dev_.name, device_name_, sizeof(dev_.name));
65 dev_.id.bustype = bus_type_;
66 dev_.id.vendor = vendor_id_;
67 dev_.id.product = product_id_;
68 dev_.id.version = version_;
69
70 for (uint32_t evt_type : GetEventTypes()) {
71 if (!DoIoctl(fd_, UI_SET_EVBIT, evt_type)) {
72 SLOGE("Error setting event type: %" PRIu32, evt_type);
73 return false;
74 }
75 }
76
77 for (uint32_t key : GetKeys()) {
78 if (!DoIoctl(fd_, UI_SET_KEYBIT, key)) {
79 SLOGE("Error setting key: %" PRIu32, key);
80 return false;
81 }
82 }
83
84 for (uint32_t property : GetProperties()) {
85 if (!DoIoctl(fd_, UI_SET_PROPBIT, property)) {
86 SLOGE("Error setting property: %" PRIu32, property);
87 return false;
88 }
89 }
90
91 for (uint32_t abs : GetAbs()) {
92 if (!DoIoctl(fd_, UI_SET_ABSBIT, abs)) {
93 SLOGE("Error setting abs: %" PRIu32, abs);
94 return false;
95 }
96 }
97
98 if (write(fd_, &dev_, sizeof(dev_)) < 0) {
99 SLOGE("Unable to set input device info (%s)", strerror(errno));
100 return false;
101 }
102 if (ioctl(fd_, UI_DEV_CREATE) < 0) {
103 SLOGE("Unable to create input device (%s)", strerror(errno));
104 return false;
105 }
106
107 LOG(INFO) << "set up virtual device";
108
109 return true;
110 }
111
EmitEvent(uint16_t type,uint16_t code,uint32_t value)112 bool VirtualDeviceBase::EmitEvent(uint16_t type,
113 uint16_t code,
114 uint32_t value) {
115 struct input_event event {};
116 event.type = type;
117 event.code = code;
118 event.value = value;
119
120 if (write(fd_, &event, sizeof(event)) < static_cast<ssize_t>(sizeof(event))) {
121 SLOGE("Event write failed (%s): aborting", strerror(errno));
122 return false;
123 }
124 return true;
125 }
126
127 // By default devices have no event types, keys, properties or absolutes,
128 // subclasses can override this behavior if necessary.
GetEventTypes() const129 const std::vector<const uint32_t>& VirtualDeviceBase::GetEventTypes() const {
130 static const std::vector<const uint32_t> evt_types{};
131 return evt_types;
132 }
GetKeys() const133 const std::vector<const uint32_t>& VirtualDeviceBase::GetKeys() const {
134 static const std::vector<const uint32_t> keys{};
135 return keys;
136 }
GetProperties() const137 const std::vector<const uint32_t>& VirtualDeviceBase::GetProperties() const {
138 static const std::vector<const uint32_t> properties{};
139 return properties;
140 }
GetAbs() const141 const std::vector<const uint32_t>& VirtualDeviceBase::GetAbs() const {
142 static const std::vector<const uint32_t> abs{};
143 return abs;
144 }
145