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 |
| 28 | * are intended to be lightweight and passed by value. It is still important not |
| 29 | * to use an object after a HAL-specific method has freed the underlying |
| 30 | * representation. |
| 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; |
| 37 | using InputDeviceHandle = input_device_handle_t*; |
| 38 | using InputDeviceIdentifier = input_device_identifier_t*; |
| 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 | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame^] | 46 | InputHostBase(const InputHostBase& rhs) = default; |
| 47 | InputHostBase(InputHostBase&& rhs) = default; |
| 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: |
| 55 | virtual ~InputReport() = default; |
| 56 | |
| 57 | InputReport(const InputReport& rhs) = default; |
| 58 | InputReport& operator=(const InputReport& rhs) = default; |
| 59 | operator input_report_t*() const { return mReport; } |
| 60 | |
| 61 | void reportEvent(InputDeviceHandle d); |
| 62 | |
| 63 | private: |
| 64 | friend class InputReportDefinition; |
| 65 | |
| 66 | InputReport(input_host_t* host, input_host_callbacks_t cb, input_report_t* r) : |
| 67 | InputHostBase(host, cb), mReport(r) {} |
| 68 | |
| 69 | input_report_t* mReport; |
| 70 | }; |
| 71 | |
| 72 | class InputReportDefinition : private InputHostBase { |
| 73 | public: |
| 74 | virtual ~InputReportDefinition() = default; |
| 75 | |
| 76 | InputReportDefinition(const InputReportDefinition& rhs) = default; |
| 77 | InputReportDefinition& operator=(const InputReportDefinition& rhs) = default; |
| 78 | operator input_report_definition_t*() { return mReportDefinition; } |
| 79 | |
| 80 | void addCollection(InputCollectionId id, int32_t arity); |
| 81 | void declareUsage(InputCollectionId id, InputUsage usage, int32_t min, int32_t max, |
| 82 | float resolution); |
| 83 | void declareUsage(InputCollectionId id, InputUsage* usage, size_t usageCount); |
| 84 | |
| 85 | InputReport allocateReport(); |
| 86 | |
| 87 | private: |
| 88 | friend class InputHost; |
| 89 | |
| 90 | InputReportDefinition( |
| 91 | input_host_t* host, input_host_callbacks_t cb, input_report_definition_t* r) : |
| 92 | InputHostBase(host, cb), mReportDefinition(r) {} |
| 93 | |
| 94 | input_report_definition_t* mReportDefinition; |
| 95 | }; |
| 96 | |
| 97 | class InputDeviceDefinition : private InputHostBase { |
| 98 | public: |
| 99 | virtual ~InputDeviceDefinition() = default; |
| 100 | |
| 101 | InputDeviceDefinition(const InputDeviceDefinition& rhs) = default; |
| 102 | InputDeviceDefinition& operator=(const InputDeviceDefinition& rhs) = default; |
| 103 | operator input_device_definition_t*() { return mDeviceDefinition; } |
| 104 | |
| 105 | void addReport(InputReportDefinition r); |
| 106 | |
| 107 | private: |
| 108 | friend class InputHost; |
| 109 | |
| 110 | InputDeviceDefinition( |
| 111 | input_host_t* host, input_host_callbacks_t cb, input_device_definition_t* d) : |
| 112 | InputHostBase(host, cb), mDeviceDefinition(d) {} |
| 113 | |
| 114 | input_device_definition_t* mDeviceDefinition; |
| 115 | }; |
| 116 | |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 117 | class InputProperty : private InputHostBase { |
| 118 | public: |
| 119 | virtual ~InputProperty(); |
| 120 | |
| 121 | operator input_property_t*() { return mProperty; } |
| 122 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame^] | 123 | const char* getKey() const; |
| 124 | const char* getValue() const; |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 125 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame^] | 126 | // Transfers ownership of the input_property_t pointer. |
| 127 | InputProperty(InputProperty&& rhs); |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 128 | |
| 129 | // Prevent copy/assign because of the ownership of the underlying |
| 130 | // input_property_t pointer. |
| 131 | InputProperty(const InputProperty& rhs) = delete; |
| 132 | InputProperty& operator=(const InputProperty& rhs) = delete; |
| 133 | |
| 134 | private: |
| 135 | friend class InputPropertyMap; |
| 136 | |
| 137 | InputProperty( |
| 138 | input_host_t* host, input_host_callbacks_t cb, input_property_t* p) : |
| 139 | InputHostBase(host, cb), mProperty(p) {} |
| 140 | |
| 141 | input_property_t* mProperty; |
| 142 | }; |
| 143 | |
| 144 | class InputPropertyMap : private InputHostBase { |
| 145 | public: |
| 146 | virtual ~InputPropertyMap(); |
| 147 | |
| 148 | operator input_property_map_t*() { return mMap; } |
| 149 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame^] | 150 | InputProperty getDeviceProperty(const char* key) const; |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 151 | |
Tim Kilbourn | 3186e7b | 2015-04-16 15:32:08 -0700 | [diff] [blame^] | 152 | // Transfers ownership of the input_property_map_t pointer. |
| 153 | InputPropertyMap(InputPropertyMap&& rhs); |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 154 | |
| 155 | // Prevent copy/assign because of the ownership of the underlying |
| 156 | // input_property_map_t pointer. |
| 157 | InputPropertyMap(const InputPropertyMap& rhs) = delete; |
| 158 | InputPropertyMap& operator=(const InputPropertyMap& rhs) = delete; |
| 159 | |
| 160 | private: |
| 161 | friend class InputHost; |
| 162 | |
| 163 | InputPropertyMap( |
| 164 | input_host_t* host, input_host_callbacks_t cb, input_property_map_t* m) : |
| 165 | InputHostBase(host, cb), mMap(m) {} |
| 166 | |
| 167 | input_property_map_t* mMap; |
| 168 | }; |
| 169 | |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 170 | class InputHost : private InputHostBase { |
| 171 | public: |
| 172 | InputHost(input_host_t* host, input_host_callbacks_t cb) : InputHostBase(host, cb) {} |
| 173 | virtual ~InputHost() = default; |
| 174 | |
| 175 | InputHost(const InputHost& rhs) = default; |
| 176 | InputHost& operator=(const InputHost& rhs) = default; |
| 177 | |
| 178 | InputDeviceIdentifier createDeviceIdentifier(const char* name, int32_t productId, |
| 179 | int32_t vendorId, InputBus bus, const char* uniqueId); |
| 180 | |
| 181 | InputDeviceDefinition createDeviceDefinition(); |
| 182 | InputReportDefinition createInputReportDefinition(); |
| 183 | InputReportDefinition createOutputReportDefinition(); |
| 184 | |
| 185 | InputDeviceHandle registerDevice(InputDeviceIdentifier id, InputDeviceDefinition d); |
| 186 | void unregisterDevice(InputDeviceHandle handle); |
Tim Kilbourn | 6fa8248 | 2015-04-06 13:49:40 -0700 | [diff] [blame] | 187 | |
| 188 | InputPropertyMap getDevicePropertyMap(InputDeviceIdentifier id); |
Tim Kilbourn | 73475a4 | 2015-02-13 10:35:20 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | } // namespace android |
| 192 | |
| 193 | #endif // ANDROID_INPUT_HOST_H_ |