• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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:
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     input_host_t* mHost;
47     input_host_callbacks_t mCallbacks;
48 };
49 
50 class InputReport : private InputHostBase {
51 public:
52     virtual ~InputReport() = default;
53 
54     InputReport(const InputReport& rhs) = default;
55     InputReport& operator=(const InputReport& rhs) = default;
56     operator input_report_t*() const { return mReport; }
57 
58     void reportEvent(InputDeviceHandle d);
59 
60 private:
61     friend class InputReportDefinition;
62 
InputReport(input_host_t * host,input_host_callbacks_t cb,input_report_t * r)63     InputReport(input_host_t* host, input_host_callbacks_t cb, input_report_t* r) :
64         InputHostBase(host, cb), mReport(r) {}
65 
66     input_report_t* mReport;
67 };
68 
69 class InputReportDefinition : private InputHostBase {
70 public:
71     virtual ~InputReportDefinition() = default;
72 
73     InputReportDefinition(const InputReportDefinition& rhs) = default;
74     InputReportDefinition& operator=(const InputReportDefinition& rhs) = default;
75     operator input_report_definition_t*() { return mReportDefinition; }
76 
77     void addCollection(InputCollectionId id, int32_t arity);
78     void declareUsage(InputCollectionId id, InputUsage usage, int32_t min, int32_t max,
79             float resolution);
80     void declareUsage(InputCollectionId id, InputUsage* usage, size_t usageCount);
81 
82     InputReport allocateReport();
83 
84 private:
85     friend class InputHost;
86 
InputReportDefinition(input_host_t * host,input_host_callbacks_t cb,input_report_definition_t * r)87     InputReportDefinition(
88             input_host_t* host, input_host_callbacks_t cb, input_report_definition_t* r) :
89         InputHostBase(host, cb), mReportDefinition(r) {}
90 
91     input_report_definition_t* mReportDefinition;
92 };
93 
94 class InputDeviceDefinition : private InputHostBase {
95 public:
96     virtual ~InputDeviceDefinition() = default;
97 
98     InputDeviceDefinition(const InputDeviceDefinition& rhs) = default;
99     InputDeviceDefinition& operator=(const InputDeviceDefinition& rhs) = default;
100     operator input_device_definition_t*() { return mDeviceDefinition; }
101 
102     void addReport(InputReportDefinition r);
103 
104 private:
105     friend class InputHost;
106 
InputDeviceDefinition(input_host_t * host,input_host_callbacks_t cb,input_device_definition_t * d)107     InputDeviceDefinition(
108             input_host_t* host, input_host_callbacks_t cb, input_device_definition_t* d) :
109         InputHostBase(host, cb), mDeviceDefinition(d) {}
110 
111     input_device_definition_t* mDeviceDefinition;
112 };
113 
114 class InputProperty : private InputHostBase {
115 public:
116     virtual ~InputProperty();
117 
118     operator input_property_t*() { return mProperty; }
119 
120     const char* getKey();
121     const char* getValue();
122 
123     // Default move constructor transfers ownership of the input_property_t
124     // pointer.
125     InputProperty(InputProperty&& rhs) = default;
126 
127     // Prevent copy/assign because of the ownership of the underlying
128     // input_property_t pointer.
129     InputProperty(const InputProperty& rhs) = delete;
130     InputProperty& operator=(const InputProperty& rhs) = delete;
131 
132 private:
133     friend class InputPropertyMap;
134 
InputProperty(input_host_t * host,input_host_callbacks_t cb,input_property_t * p)135     InputProperty(
136             input_host_t* host, input_host_callbacks_t cb, input_property_t* p) :
137         InputHostBase(host, cb), mProperty(p) {}
138 
139     input_property_t* mProperty;
140 };
141 
142 class InputPropertyMap : private InputHostBase {
143 public:
144     virtual ~InputPropertyMap();
145 
146     operator input_property_map_t*() { return mMap; }
147 
148     InputProperty getDeviceProperty(const char* key);
149 
150     // Default move constructor transfers ownership of the input_property_map_t
151     // pointer.
152     InputPropertyMap(InputPropertyMap&& rhs) = default;
153 
154     // Prevent copy/assign because of the ownership of the underlying
155     // input_property_map_t pointer.
156     InputPropertyMap(const InputPropertyMap& rhs) = delete;
157     InputPropertyMap& operator=(const InputPropertyMap& rhs) = delete;
158 
159 private:
160     friend class InputHost;
161 
InputPropertyMap(input_host_t * host,input_host_callbacks_t cb,input_property_map_t * m)162     InputPropertyMap(
163             input_host_t* host, input_host_callbacks_t cb, input_property_map_t* m) :
164         InputHostBase(host, cb), mMap(m) {}
165 
166     input_property_map_t* mMap;
167 };
168 
169 class InputHost : private InputHostBase {
170 public:
InputHost(input_host_t * host,input_host_callbacks_t cb)171     InputHost(input_host_t* host, input_host_callbacks_t cb) : InputHostBase(host, cb) {}
172     virtual ~InputHost() = default;
173 
174     InputHost(const InputHost& rhs) = default;
175     InputHost& operator=(const InputHost& rhs) = default;
176 
177     InputDeviceIdentifier createDeviceIdentifier(const char* name, int32_t productId,
178             int32_t vendorId, InputBus bus, const char* uniqueId);
179 
180     InputDeviceDefinition createDeviceDefinition();
181     InputReportDefinition createInputReportDefinition();
182     InputReportDefinition createOutputReportDefinition();
183 
184     InputDeviceHandle registerDevice(InputDeviceIdentifier id, InputDeviceDefinition d);
185     void unregisterDevice(InputDeviceHandle handle);
186 
187     InputPropertyMap getDevicePropertyMap(InputDeviceIdentifier id);
188 };
189 
190 }  // namespace android
191 
192 #endif  // ANDROID_INPUT_HOST_H_
193