blob: d6a04d9375bd3826a182802873c9fb9211543f96 [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
Tim Kilbourn3186e7b2015-04-16 15:32:08 -070046 InputHostBase(const InputHostBase& rhs) = default;
47 InputHostBase(InputHostBase&& rhs) = default;
48
Tim Kilbourn73475a42015-02-13 10:35:20 -080049 input_host_t* mHost;
50 input_host_callbacks_t mCallbacks;
51};
52
53class InputReport : private InputHostBase {
54public:
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
63private:
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
72class InputReportDefinition : private InputHostBase {
73public:
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
87private:
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
97class InputDeviceDefinition : private InputHostBase {
98public:
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
107private:
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 Kilbourn6fa82482015-04-06 13:49:40 -0700117class InputProperty : private InputHostBase {
118public:
119 virtual ~InputProperty();
120
121 operator input_property_t*() { return mProperty; }
122
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700123 const char* getKey() const;
124 const char* getValue() const;
Tim Kilbourn6fa82482015-04-06 13:49:40 -0700125
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700126 // Transfers ownership of the input_property_t pointer.
127 InputProperty(InputProperty&& rhs);
Tim Kilbourn6fa82482015-04-06 13:49:40 -0700128
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
134private:
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
144class InputPropertyMap : private InputHostBase {
145public:
146 virtual ~InputPropertyMap();
147
148 operator input_property_map_t*() { return mMap; }
149
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700150 InputProperty getDeviceProperty(const char* key) const;
Tim Kilbourn6fa82482015-04-06 13:49:40 -0700151
Tim Kilbourn3186e7b2015-04-16 15:32:08 -0700152 // Transfers ownership of the input_property_map_t pointer.
153 InputPropertyMap(InputPropertyMap&& rhs);
Tim Kilbourn6fa82482015-04-06 13:49:40 -0700154
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
160private:
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 Kilbourn73475a42015-02-13 10:35:20 -0800170class InputHost : private InputHostBase {
171public:
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 Kilbourn6fa82482015-04-06 13:49:40 -0700187
188 InputPropertyMap getDevicePropertyMap(InputDeviceIdentifier id);
Tim Kilbourn73475a42015-02-13 10:35:20 -0800189};
190
191} // namespace android
192
193#endif // ANDROID_INPUT_HOST_H_