blob: 98ce26f54e1db70ff9fe839dd11c7c440e56ab3d [file] [log] [blame]
Tim Kilbourn73475a42015-02-13 10:35:20 -08001/*
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 Kilbourn6fa82482015-04-06 13:49:40 -070020#include <memory>
21
Tim Kilbourn73475a42015-02-13 10:35:20 -080022#include <hardware/input.h>
23
24namespace 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
35using InputBus = input_bus_t;
36using InputCollectionId = input_collection_id_t;
37using InputDeviceHandle = input_device_handle_t*;
38using InputDeviceIdentifier = input_device_identifier_t*;
39using InputUsage = input_usage_t;
40
41class InputHostBase {
42protected:
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
50class InputReport : private InputHostBase {
51public:
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
60private:
61 friend class InputReportDefinition;
62
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
69class InputReportDefinition : private InputHostBase {
70public:
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
84private:
85 friend class InputHost;
86
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
94class InputDeviceDefinition : private InputHostBase {
95public:
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
104private:
105 friend class InputHost;
106
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
Tim Kilbourn6fa82482015-04-06 13:49:40 -0700114class InputProperty : private InputHostBase {
115public:
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
132private:
133 friend class InputPropertyMap;
134
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
142class InputPropertyMap : private InputHostBase {
143public:
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
159private:
160 friend class InputHost;
161
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
Tim Kilbourn73475a42015-02-13 10:35:20 -0800169class InputHost : private InputHostBase {
170public:
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);
Tim Kilbourn6fa82482015-04-06 13:49:40 -0700186
187 InputPropertyMap getDevicePropertyMap(InputDeviceIdentifier id);
Tim Kilbourn73475a42015-02-13 10:35:20 -0800188};
189
190} // namespace android
191
192#endif // ANDROID_INPUT_HOST_H_