Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 17 | #include <functional> |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 20 | #include <unordered_map> |
| 21 | #include <vector> |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 22 | |
| 23 | #define LOG_TAG "InputDriver" |
| 24 | |
| 25 | #define LOG_NDEBUG 0 |
| 26 | |
| 27 | #include "InputDriver.h" |
| 28 | #include "InputHost.h" |
| 29 | |
| 30 | #include <hardware/input.h> |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 31 | #include <input/InputDevice.h> |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 33 | #include <utils/PropertyMap.h> |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 34 | #include <utils/String8.h> |
| 35 | |
| 36 | #define INDENT2 " " |
| 37 | |
| 38 | namespace android { |
| 39 | |
| 40 | static input_host_callbacks_t kCallbacks = { |
| 41 | .create_device_identifier = create_device_identifier, |
| 42 | .create_device_definition = create_device_definition, |
| 43 | .create_input_report_definition = create_input_report_definition, |
| 44 | .create_output_report_definition = create_output_report_definition, |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 45 | .free_report_definition = free_report_definition, |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 46 | .input_device_definition_add_report = input_device_definition_add_report, |
| 47 | .input_report_definition_add_collection = input_report_definition_add_collection, |
| 48 | .input_report_definition_declare_usage_int = input_report_definition_declare_usage_int, |
| 49 | .input_report_definition_declare_usages_bool = input_report_definition_declare_usages_bool, |
| 50 | .register_device = register_device, |
| 51 | .input_allocate_report = input_allocate_report, |
Tim Kilbourn | 8943ce3 | 2015-02-27 15:09:34 -0800 | [diff] [blame] | 52 | .input_report_set_usage_int = input_report_set_usage_int, |
| 53 | .input_report_set_usage_bool = input_report_set_usage_bool, |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 54 | .report_event = report_event, |
Tim Kilbourn | e5364c8 | 2015-04-06 13:48:50 -0700 | [diff] [blame] | 55 | .input_get_device_property_map = input_get_device_property_map, |
| 56 | .input_get_device_property = input_get_device_property, |
| 57 | .input_get_property_key = input_get_property_key, |
| 58 | .input_get_property_value = input_get_property_value, |
| 59 | .input_free_device_property = input_free_device_property, |
| 60 | .input_free_device_property_map = input_free_device_property_map, |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | InputDriver::InputDriver(const char* name) : mName(String8(name)) { |
| 64 | const hw_module_t* module; |
| 65 | int err = input_open(&module, name); |
| 66 | LOG_ALWAYS_FATAL_IF(err != 0, "Input module %s not found", name); |
| 67 | mHal = reinterpret_cast<const input_module_t*>(module); |
| 68 | } |
| 69 | |
| 70 | void InputDriver::init(InputHostInterface* host) { |
| 71 | mHal->init(mHal, static_cast<input_host_t*>(host), kCallbacks); |
| 72 | } |
| 73 | |
| 74 | void InputDriver::dump(String8& result) { |
| 75 | result.appendFormat(INDENT2 "HAL Input Driver (%s)\n", mName.string()); |
| 76 | } |
| 77 | |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 78 | } // namespace android |
| 79 | |
| 80 | struct input_property_map { |
| 81 | android::PropertyMap* propertyMap; |
| 82 | }; |
| 83 | |
| 84 | struct input_property { |
| 85 | android::String8 key; |
| 86 | android::String8 value; |
| 87 | }; |
| 88 | |
| 89 | struct input_device_identifier { |
| 90 | const char* name; |
| 91 | const char* uniqueId; |
| 92 | input_bus_t bus; |
| 93 | int32_t vendorId; |
| 94 | int32_t productId; |
| 95 | int32_t version; |
| 96 | }; |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 97 | |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 98 | struct input_device_definition { |
| 99 | std::vector<input_report_definition*> reportDefs; |
| 100 | }; |
| 101 | |
| 102 | struct input_device_handle { |
| 103 | input_device_identifier_t* id; |
| 104 | input_device_definition_t* def; |
| 105 | }; |
| 106 | |
| 107 | struct input_int_usage { |
| 108 | input_usage_t usage; |
| 109 | int32_t min; |
| 110 | int32_t max; |
| 111 | float resolution; |
| 112 | }; |
| 113 | |
| 114 | struct input_collection { |
| 115 | int32_t arity; |
| 116 | std::vector<input_int_usage> intUsages; |
| 117 | std::vector<input_usage_t> boolUsages; |
| 118 | }; |
| 119 | |
| 120 | struct InputCollectionIdHasher { |
| 121 | std::size_t operator()(const input_collection_id& id) const { |
| 122 | return std::hash<int>()(static_cast<int>(id)); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | struct input_report_definition { |
| 127 | std::unordered_map<input_collection_id_t, input_collection, InputCollectionIdHasher> collections; |
| 128 | }; |
| 129 | |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 130 | // HAL wrapper functions |
| 131 | |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 132 | namespace android { |
| 133 | |
| 134 | ::input_device_identifier_t* create_device_identifier(input_host_t* host, |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 135 | const char* name, int32_t product_id, int32_t vendor_id, |
| 136 | input_bus_t bus, const char* unique_id) { |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 137 | auto identifier = new ::input_device_identifier { |
| 138 | .name = name, |
| 139 | .productId = product_id, |
| 140 | .vendorId = vendor_id, |
| 141 | //.bus = bus, |
| 142 | .uniqueId = unique_id, |
| 143 | }; |
| 144 | // store this identifier somewhere? in the host? |
| 145 | return identifier; |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | input_device_definition_t* create_device_definition(input_host_t* host) { |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 149 | return new ::input_device_definition; |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | input_report_definition_t* create_input_report_definition(input_host_t* host) { |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 153 | return new ::input_report_definition; |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | input_report_definition_t* create_output_report_definition(input_host_t* host) { |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 157 | return new ::input_report_definition; |
| 158 | } |
| 159 | |
| 160 | void free_report_definition(input_host_t* host, input_report_definition_t* report_def) { |
| 161 | delete report_def; |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void input_device_definition_add_report(input_host_t* host, |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 165 | input_device_definition_t* d, input_report_definition_t* r) { |
| 166 | d->reportDefs.push_back(r); |
| 167 | } |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 168 | |
| 169 | void input_report_definition_add_collection(input_host_t* host, |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 170 | input_report_definition_t* report, input_collection_id_t id, int32_t arity) { |
| 171 | report->collections[id] = {.arity = arity}; |
| 172 | } |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 173 | |
| 174 | void input_report_definition_declare_usage_int(input_host_t* host, |
| 175 | input_report_definition_t* report, input_collection_id_t id, |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 176 | input_usage_t usage, int32_t min, int32_t max, float resolution) { |
| 177 | if (report->collections.find(id) != report->collections.end()) { |
| 178 | report->collections[id].intUsages.push_back({ |
| 179 | .usage = usage, .min = min, .max = max, .resolution = resolution}); |
| 180 | } |
| 181 | } |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 182 | |
| 183 | void input_report_definition_declare_usages_bool(input_host_t* host, |
| 184 | input_report_definition_t* report, input_collection_id_t id, |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 185 | input_usage_t* usage, size_t usage_count) { |
| 186 | if (report->collections.find(id) != report->collections.end()) { |
| 187 | for (size_t i = 0; i < usage_count; ++i) { |
| 188 | report->collections[id].boolUsages.push_back(usage[i]); |
| 189 | } |
| 190 | } |
| 191 | } |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 192 | |
| 193 | input_device_handle_t* register_device(input_host_t* host, |
| 194 | input_device_identifier_t* id, input_device_definition_t* d) { |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 195 | ALOGD("Registering device %s with %d input reports", id->name, d->reportDefs.size()); |
| 196 | return new input_device_handle{ .id = id, .def = d }; |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | input_report_t* input_allocate_report(input_host_t* host, input_report_definition_t* r) { |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 200 | ALOGD("Allocating input report for definition %p", r); |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 201 | return nullptr; |
| 202 | } |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 203 | |
Tim Kilbourn | 8943ce3 | 2015-02-27 15:09:34 -0800 | [diff] [blame] | 204 | void input_report_set_usage_int(input_host_t* host, input_report_t* r, |
| 205 | input_collection_id_t id, input_usage_t usage, int32_t value, int32_t arity_index) { } |
| 206 | |
| 207 | void input_report_set_usage_bool(input_host_t* host, input_report_t* r, |
| 208 | input_collection_id_t id, input_usage_t usage, bool value, int32_t arity_index) { } |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 209 | |
Tim Kilbourn | c472986 | 2015-05-08 17:13:43 -0700 | [diff] [blame^] | 210 | void report_event(input_host_t* host, input_device_handle_t* d, input_report_t* report) { |
| 211 | ALOGD("report_event %p for handle %p", report, d); |
| 212 | } |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 213 | |
Tim Kilbourn | e5364c8 | 2015-04-06 13:48:50 -0700 | [diff] [blame] | 214 | input_property_map_t* input_get_device_property_map(input_host_t* host, |
| 215 | input_device_identifier_t* id) { |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 216 | InputDeviceIdentifier idi; |
| 217 | idi.name = id->name; |
| 218 | idi.uniqueId = id->uniqueId; |
| 219 | idi.bus = id->bus; |
| 220 | idi.vendor = id->vendorId; |
| 221 | idi.product = id->productId; |
| 222 | idi.version = id->version; |
| 223 | |
| 224 | String8 configFile = getInputDeviceConfigurationFilePathByDeviceIdentifier( |
| 225 | idi, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION); |
| 226 | if (configFile.isEmpty()) { |
| 227 | ALOGD("No input device configuration file found for device '%s'.", |
| 228 | idi.name.string()); |
| 229 | } else { |
| 230 | auto propMap = new input_property_map_t(); |
| 231 | status_t status = PropertyMap::load(configFile, &propMap->propertyMap); |
| 232 | if (status) { |
| 233 | ALOGE("Error loading input device configuration file for device '%s'. " |
| 234 | "Using default configuration.", |
| 235 | idi.name.string()); |
| 236 | delete propMap; |
| 237 | return nullptr; |
| 238 | } |
| 239 | return propMap; |
| 240 | } |
Tim Kilbourn | e5364c8 | 2015-04-06 13:48:50 -0700 | [diff] [blame] | 241 | return nullptr; |
| 242 | } |
| 243 | |
| 244 | input_property_t* input_get_device_property(input_host_t* host, input_property_map_t* map, |
| 245 | const char* key) { |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 246 | String8 keyString(key); |
| 247 | if (map != nullptr) { |
| 248 | if (map->propertyMap->hasProperty(keyString)) { |
| 249 | auto prop = new input_property_t(); |
| 250 | if (!map->propertyMap->tryGetProperty(keyString, prop->value)) { |
| 251 | delete prop; |
| 252 | return nullptr; |
| 253 | } |
| 254 | prop->key = keyString; |
| 255 | return prop; |
| 256 | } |
| 257 | } |
Tim Kilbourn | e5364c8 | 2015-04-06 13:48:50 -0700 | [diff] [blame] | 258 | return nullptr; |
| 259 | } |
| 260 | |
| 261 | const char* input_get_property_key(input_host_t* host, input_property_t* property) { |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 262 | if (property != nullptr) { |
| 263 | return property->key.string(); |
| 264 | } |
Tim Kilbourn | e5364c8 | 2015-04-06 13:48:50 -0700 | [diff] [blame] | 265 | return nullptr; |
| 266 | } |
| 267 | |
| 268 | const char* input_get_property_value(input_host_t* host, input_property_t* property) { |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 269 | if (property != nullptr) { |
| 270 | return property->value.string(); |
| 271 | } |
Tim Kilbourn | e5364c8 | 2015-04-06 13:48:50 -0700 | [diff] [blame] | 272 | return nullptr; |
| 273 | } |
| 274 | |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 275 | void input_free_device_property(input_host_t* host, input_property_t* property) { |
| 276 | if (property != nullptr) { |
| 277 | delete property; |
| 278 | } |
| 279 | } |
Tim Kilbourn | e5364c8 | 2015-04-06 13:48:50 -0700 | [diff] [blame] | 280 | |
Tim Kilbourn | 3e38aad | 2015-04-07 13:02:27 -0700 | [diff] [blame] | 281 | void input_free_device_property_map(input_host_t* host, input_property_map_t* map) { |
| 282 | if (map != nullptr) { |
| 283 | delete map->propertyMap; |
| 284 | delete map; |
| 285 | } |
| 286 | } |
Michael Wright | 6f78360 | 2015-02-13 17:35:16 -0800 | [diff] [blame] | 287 | |
| 288 | } // namespace android |