Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -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 | #ifndef ANDROID_INPUT_HOST_H_ |
| 18 | #define ANDROID_INPUT_HOST_H_ |
| 19 | |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 22 | #include <hardware/input.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | /** |
| 27 | * Classes in this file wrap the corresponding interfaces in the Input HAL. They |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 28 | * are intended to be lightweight, as they primarily wrap pointers to callbacks. |
| 29 | * It is still important not to use an object after a HAL-specific method has |
| 30 | * freed the underlying representation. |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 31 | * |
| 32 | * See hardware/input.h for details about each of these methods. |
| 33 | */ |
| 34 | |
| 35 | using InputBus = input_bus_t; |
| 36 | using InputCollectionId = input_collection_id_t; |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 37 | using InputDeviceHandle = input_device_handle_t; |
| 38 | using InputDeviceIdentifier = input_device_identifier_t; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 39 | using InputUsage = input_usage_t; |
| 40 | |
| 41 | class InputHostBase { |
| 42 | protected: |
| 43 | InputHostBase(input_host_t* host, input_host_callbacks_t cb) : mHost(host), mCallbacks(cb) {} |
| 44 | virtual ~InputHostBase() = default; |
| 45 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 46 | InputHostBase(const InputHostBase& rhs) = delete; |
| 47 | InputHostBase(InputHostBase&& rhs) = delete; |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame] | 48 | |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 49 | input_host_t* mHost; |
| 50 | input_host_callbacks_t mCallbacks; |
| 51 | }; |
| 52 | |
| 53 | class InputReport : private InputHostBase { |
| 54 | public: |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 55 | InputReport(input_host_t* host, input_host_callbacks_t cb, input_report_t* r) : |
| 56 | InputHostBase(host, cb), mReport(r) {} |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 57 | virtual ~InputReport() = default; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 58 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 59 | virtual void setIntUsage(InputCollectionId id, InputUsage usage, int32_t value, |
| 60 | int32_t arityIndex); |
| 61 | virtual void setBoolUsage(InputCollectionId id, InputUsage usage, bool value, |
| 62 | int32_t arityIndex); |
| 63 | virtual void reportEvent(InputDeviceHandle* d); |
| 64 | |
| 65 | operator input_report_t*() const { return mReport; } |
| 66 | |
| 67 | InputReport(const InputReport& rhs) = delete; |
| 68 | InputReport& operator=(const InputReport& rhs) = delete; |
| 69 | private: |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 70 | input_report_t* mReport; |
| 71 | }; |
| 72 | |
| 73 | class InputReportDefinition : private InputHostBase { |
| 74 | public: |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 75 | InputReportDefinition(input_host_t* host, input_host_callbacks_t cb, |
| 76 | input_report_definition_t* r) : InputHostBase(host, cb), mReportDefinition(r) {} |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 77 | virtual ~InputReportDefinition() = default; |
| 78 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 79 | virtual void addCollection(InputCollectionId id, int32_t arity); |
| 80 | virtual void declareUsage(InputCollectionId id, InputUsage usage, int32_t min, int32_t max, |
| 81 | float resolution); |
| 82 | virtual void declareUsages(InputCollectionId id, InputUsage* usage, size_t usageCount); |
| 83 | |
| 84 | virtual InputReport* allocateReport(); |
| 85 | |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 86 | operator input_report_definition_t*() { return mReportDefinition; } |
| 87 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 88 | InputReportDefinition(const InputReportDefinition& rhs) = delete; |
| 89 | InputReportDefinition& operator=(const InputReportDefinition& rhs) = delete; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 90 | private: |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 91 | input_report_definition_t* mReportDefinition; |
| 92 | }; |
| 93 | |
| 94 | class InputDeviceDefinition : private InputHostBase { |
| 95 | public: |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 96 | InputDeviceDefinition(input_host_t* host, input_host_callbacks_t cb, |
| 97 | input_device_definition_t* d) : |
| 98 | InputHostBase(host, cb), mDeviceDefinition(d) {} |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 99 | virtual ~InputDeviceDefinition() = default; |
| 100 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 101 | virtual void addReport(InputReportDefinition* r); |
| 102 | |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 103 | operator input_device_definition_t*() { return mDeviceDefinition; } |
| 104 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 105 | InputDeviceDefinition(const InputDeviceDefinition& rhs) = delete; |
| 106 | InputDeviceDefinition& operator=(const InputDeviceDefinition& rhs) = delete; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 107 | private: |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 108 | input_device_definition_t* mDeviceDefinition; |
| 109 | }; |
| 110 | |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 111 | class InputProperty : private InputHostBase { |
| 112 | public: |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 113 | virtual ~InputProperty() = default; |
| 114 | |
| 115 | InputProperty(input_host_t* host, input_host_callbacks_t cb, input_property_t* p) : |
| 116 | InputHostBase(host, cb), mProperty(p) {} |
| 117 | |
| 118 | virtual const char* getKey() const; |
| 119 | virtual const char* getValue() const; |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 120 | |
| 121 | operator input_property_t*() { return mProperty; } |
| 122 | |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 123 | InputProperty(const InputProperty& rhs) = delete; |
| 124 | InputProperty& operator=(const InputProperty& rhs) = delete; |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 125 | private: |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 126 | input_property_t* mProperty; |
| 127 | }; |
| 128 | |
| 129 | class InputPropertyMap : private InputHostBase { |
| 130 | public: |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 131 | virtual ~InputPropertyMap() = default; |
| 132 | |
| 133 | InputPropertyMap(input_host_t* host, input_host_callbacks_t cb, input_property_map_t* m) : |
| 134 | InputHostBase(host, cb), mMap(m) {} |
| 135 | |
| 136 | virtual InputProperty* getDeviceProperty(const char* key) const; |
| 137 | virtual void freeDeviceProperty(InputProperty* property) const; |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 138 | |
| 139 | operator input_property_map_t*() { return mMap; } |
| 140 | |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 141 | InputPropertyMap(const InputPropertyMap& rhs) = delete; |
| 142 | InputPropertyMap& operator=(const InputPropertyMap& rhs) = delete; |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 143 | private: |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 144 | input_property_map_t* mMap; |
| 145 | }; |
| 146 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 147 | class InputHostInterface { |
| 148 | public: |
| 149 | virtual ~InputHostInterface() = default; |
| 150 | |
| 151 | virtual InputDeviceIdentifier* createDeviceIdentifier(const char* name, int32_t productId, |
| 152 | int32_t vendorId, InputBus bus, const char* uniqueId) = 0; |
| 153 | |
| 154 | virtual InputDeviceDefinition* createDeviceDefinition() = 0; |
| 155 | virtual InputReportDefinition* createInputReportDefinition() = 0; |
| 156 | virtual InputReportDefinition* createOutputReportDefinition() = 0; |
| 157 | virtual void freeReportDefinition(InputReportDefinition* reportDef) = 0; |
| 158 | |
| 159 | virtual InputDeviceHandle* registerDevice(InputDeviceIdentifier* id, |
| 160 | InputDeviceDefinition* d) = 0; |
| 161 | virtual void unregisterDevice(InputDeviceHandle* handle) = 0; |
| 162 | |
| 163 | virtual InputPropertyMap* getDevicePropertyMap(InputDeviceIdentifier* id) = 0; |
| 164 | virtual void freeDevicePropertyMap(InputPropertyMap* propertyMap) = 0; |
| 165 | }; |
| 166 | |
| 167 | class InputHost : public InputHostInterface, private InputHostBase { |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 168 | public: |
| 169 | InputHost(input_host_t* host, input_host_callbacks_t cb) : InputHostBase(host, cb) {} |
| 170 | virtual ~InputHost() = default; |
| 171 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 172 | InputDeviceIdentifier* createDeviceIdentifier(const char* name, int32_t productId, |
| 173 | int32_t vendorId, InputBus bus, const char* uniqueId) override; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 174 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 175 | InputDeviceDefinition* createDeviceDefinition() override; |
| 176 | InputReportDefinition* createInputReportDefinition() override; |
| 177 | InputReportDefinition* createOutputReportDefinition() override; |
| 178 | virtual void freeReportDefinition(InputReportDefinition* reportDef) override; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 179 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 180 | InputDeviceHandle* registerDevice(InputDeviceIdentifier* id, InputDeviceDefinition* d) override; |
| 181 | void unregisterDevice(InputDeviceHandle* handle) override; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 182 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 183 | InputPropertyMap* getDevicePropertyMap(InputDeviceIdentifier* id) override; |
| 184 | void freeDevicePropertyMap(InputPropertyMap* propertyMap) override; |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 185 | |
Tim Kilbourn | 4f3145d | 2015-05-04 17:26:30 -0700 | [diff] [blame] | 186 | InputHost(const InputHost& rhs) = delete; |
| 187 | InputHost& operator=(const InputHost& rhs) = delete; |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace android |
| 191 | |
| 192 | #endif // ANDROID_INPUT_HOST_H_ |