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 
20 #include <memory>
21 
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, 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.
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:
InputHostBase(input_host_t * host,input_host_callbacks_t cb)43     InputHostBase(input_host_t* host, input_host_callbacks_t cb) : mHost(host), mCallbacks(cb) {}
44     virtual ~InputHostBase() = default;
45 
46     InputHostBase(const InputHostBase& rhs) = delete;
47     InputHostBase(InputHostBase&& rhs) = delete;
48 
49     input_host_t* mHost;
50     input_host_callbacks_t mCallbacks;
51 };
52 
53 class InputReport : private InputHostBase {
54 public:
InputReport(input_host_t * host,input_host_callbacks_t cb,input_report_t * r)55     InputReport(input_host_t* host, input_host_callbacks_t cb, input_report_t* r) :
56         InputHostBase(host, cb), mReport(r) {}
57     virtual ~InputReport() = default;
58 
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:
70     input_report_t* mReport;
71 };
72 
73 class InputReportDefinition : private InputHostBase {
74 public:
InputReportDefinition(input_host_t * host,input_host_callbacks_t cb,input_report_definition_t * r)75     InputReportDefinition(input_host_t* host, input_host_callbacks_t cb,
76             input_report_definition_t* r) : InputHostBase(host, cb), mReportDefinition(r) {}
77     virtual ~InputReportDefinition() = default;
78 
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 
86     operator input_report_definition_t*() { return mReportDefinition; }
87 
88     InputReportDefinition(const InputReportDefinition& rhs) = delete;
89     InputReportDefinition& operator=(const InputReportDefinition& rhs) = delete;
90 private:
91     input_report_definition_t* mReportDefinition;
92 };
93 
94 class InputDeviceDefinition : private InputHostBase {
95 public:
InputDeviceDefinition(input_host_t * host,input_host_callbacks_t cb,input_device_definition_t * d)96     InputDeviceDefinition(input_host_t* host, input_host_callbacks_t cb,
97             input_device_definition_t* d) :
98         InputHostBase(host, cb), mDeviceDefinition(d) {}
99     virtual ~InputDeviceDefinition() = default;
100 
101     virtual void addReport(InputReportDefinition* r);
102 
103     operator input_device_definition_t*() { return mDeviceDefinition; }
104 
105     InputDeviceDefinition(const InputDeviceDefinition& rhs) = delete;
106     InputDeviceDefinition& operator=(const InputDeviceDefinition& rhs) = delete;
107 private:
108     input_device_definition_t* mDeviceDefinition;
109 };
110 
111 class InputProperty : private InputHostBase {
112 public:
113     virtual ~InputProperty() = default;
114 
InputProperty(input_host_t * host,input_host_callbacks_t cb,input_property_t * p)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;
120 
121     operator input_property_t*() { return mProperty; }
122 
123     InputProperty(const InputProperty& rhs) = delete;
124     InputProperty& operator=(const InputProperty& rhs) = delete;
125 private:
126     input_property_t* mProperty;
127 };
128 
129 class InputPropertyMap : private InputHostBase {
130 public:
131     virtual ~InputPropertyMap() = default;
132 
InputPropertyMap(input_host_t * host,input_host_callbacks_t cb,input_property_map_t * m)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;
138 
139     operator input_property_map_t*() { return mMap; }
140 
141     InputPropertyMap(const InputPropertyMap& rhs) = delete;
142     InputPropertyMap& operator=(const InputPropertyMap& rhs) = delete;
143 private:
144     input_property_map_t* mMap;
145 };
146 
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 {
168 public:
InputHost(input_host_t * host,input_host_callbacks_t cb)169     InputHost(input_host_t* host, input_host_callbacks_t cb) : InputHostBase(host, cb) {}
170     virtual ~InputHost() = default;
171 
172     InputDeviceIdentifier* createDeviceIdentifier(const char* name, int32_t productId,
173             int32_t vendorId, InputBus bus, const char* uniqueId) override;
174 
175     InputDeviceDefinition* createDeviceDefinition() override;
176     InputReportDefinition* createInputReportDefinition() override;
177     InputReportDefinition* createOutputReportDefinition() override;
178     virtual void freeReportDefinition(InputReportDefinition* reportDef) override;
179 
180     InputDeviceHandle* registerDevice(InputDeviceIdentifier* id, InputDeviceDefinition* d) override;
181     void unregisterDevice(InputDeviceHandle* handle) override;
182 
183     InputPropertyMap* getDevicePropertyMap(InputDeviceIdentifier* id) override;
184     void freeDevicePropertyMap(InputPropertyMap* propertyMap) override;
185 
186     InputHost(const InputHost& rhs) = delete;
187     InputHost& operator=(const InputHost& rhs) = delete;
188 };
189 
190 }  // namespace android
191 
192 #endif  // ANDROID_INPUT_HOST_H_
193