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