InputDriver.cpp revision 3e38aad0227134a804ba79e9690194ec4c13502e
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#include <stdint.h>
18#include <sys/types.h>
19
20#define LOG_TAG "InputDriver"
21
22#define LOG_NDEBUG 0
23
24#include "InputDriver.h"
25#include "InputHost.h"
26
27#include <hardware/input.h>
28#include <input/InputDevice.h>
29#include <utils/Log.h>
30#include <utils/PropertyMap.h>
31#include <utils/String8.h>
32
33#define INDENT2 "    "
34
35namespace android {
36
37static input_host_callbacks_t kCallbacks = {
38    .create_device_identifier = create_device_identifier,
39    .create_device_definition = create_device_definition,
40    .create_input_report_definition = create_input_report_definition,
41    .create_output_report_definition = create_output_report_definition,
42    .input_device_definition_add_report = input_device_definition_add_report,
43    .input_report_definition_add_collection = input_report_definition_add_collection,
44    .input_report_definition_declare_usage_int = input_report_definition_declare_usage_int,
45    .input_report_definition_declare_usages_bool = input_report_definition_declare_usages_bool,
46    .register_device = register_device,
47    .input_allocate_report = input_allocate_report,
48    .input_report_set_usage_int = input_report_set_usage_int,
49    .input_report_set_usage_bool = input_report_set_usage_bool,
50    .report_event = report_event,
51    .input_get_device_property_map = input_get_device_property_map,
52    .input_get_device_property = input_get_device_property,
53    .input_get_property_key = input_get_property_key,
54    .input_get_property_value = input_get_property_value,
55    .input_free_device_property = input_free_device_property,
56    .input_free_device_property_map = input_free_device_property_map,
57};
58
59InputDriver::InputDriver(const char* name) : mName(String8(name)) {
60    const hw_module_t* module;
61    int err = input_open(&module, name);
62    LOG_ALWAYS_FATAL_IF(err != 0, "Input module %s not found", name);
63    mHal = reinterpret_cast<const input_module_t*>(module);
64}
65
66void InputDriver::init(InputHostInterface* host) {
67    mHal->init(mHal, static_cast<input_host_t*>(host), kCallbacks);
68}
69
70void InputDriver::dump(String8& result) {
71    result.appendFormat(INDENT2 "HAL Input Driver (%s)\n", mName.string());
72}
73
74} // namespace android
75
76struct input_property_map {
77    android::PropertyMap* propertyMap;
78};
79
80struct input_property {
81    android::String8 key;
82    android::String8 value;
83};
84
85struct input_device_identifier {
86    const char* name;
87    const char* uniqueId;
88    input_bus_t bus;
89    int32_t     vendorId;
90    int32_t     productId;
91    int32_t     version;
92};
93
94// HAL wrapper functions
95
96namespace android {
97
98::input_device_identifier_t* create_device_identifier(input_host_t* host,
99        const char* name, int32_t product_id, int32_t vendor_id,
100        input_bus_t bus, const char* unique_id) {
101    auto identifier = new ::input_device_identifier {
102        .name = name,
103        .productId = product_id,
104        .vendorId = vendor_id,
105        //.bus = bus,
106        .uniqueId = unique_id,
107    };
108    // store this identifier somewhere? in the host?
109    return identifier;
110}
111
112input_device_definition_t* create_device_definition(input_host_t* host) {
113    return nullptr;
114}
115
116input_report_definition_t* create_input_report_definition(input_host_t* host) {
117    return nullptr;
118}
119
120input_report_definition_t* create_output_report_definition(input_host_t* host) {
121    return nullptr;
122}
123
124void input_device_definition_add_report(input_host_t* host,
125        input_device_definition_t* d, input_report_definition_t* r) { }
126
127void input_report_definition_add_collection(input_host_t* host,
128        input_report_definition_t* report, input_collection_id_t id, int32_t arity) { }
129
130void input_report_definition_declare_usage_int(input_host_t* host,
131        input_report_definition_t* report, input_collection_id_t id,
132        input_usage_t usage, int32_t min, int32_t max, float resolution) { }
133
134void input_report_definition_declare_usages_bool(input_host_t* host,
135        input_report_definition_t* report, input_collection_id_t id,
136        input_usage_t* usage, size_t usage_count) { }
137
138
139input_device_handle_t* register_device(input_host_t* host,
140        input_device_identifier_t* id, input_device_definition_t* d) {
141    return nullptr;
142}
143
144input_report_t* input_allocate_report(input_host_t* host, input_report_definition_t* r) {
145    return nullptr;
146}
147void input_report_set_usage_int(input_host_t* host, input_report_t* r,
148        input_collection_id_t id, input_usage_t usage, int32_t value, int32_t arity_index) { }
149
150void input_report_set_usage_bool(input_host_t* host, input_report_t* r,
151        input_collection_id_t id, input_usage_t usage, bool value, int32_t arity_index) { }
152
153void report_event(input_host_t* host, input_device_handle_t* d, input_report_t* report) { }
154
155input_property_map_t* input_get_device_property_map(input_host_t* host,
156        input_device_identifier_t* id) {
157    InputDeviceIdentifier idi;
158    idi.name = id->name;
159    idi.uniqueId = id->uniqueId;
160    idi.bus = id->bus;
161    idi.vendor = id->vendorId;
162    idi.product = id->productId;
163    idi.version = id->version;
164
165    String8 configFile = getInputDeviceConfigurationFilePathByDeviceIdentifier(
166            idi, INPUT_DEVICE_CONFIGURATION_FILE_TYPE_CONFIGURATION);
167    if (configFile.isEmpty()) {
168        ALOGD("No input device configuration file found for device '%s'.",
169                idi.name.string());
170    } else {
171        auto propMap = new input_property_map_t();
172        status_t status = PropertyMap::load(configFile, &propMap->propertyMap);
173        if (status) {
174            ALOGE("Error loading input device configuration file for device '%s'. "
175                    "Using default configuration.",
176                    idi.name.string());
177            delete propMap;
178            return nullptr;
179        }
180        return propMap;
181    }
182    return nullptr;
183}
184
185input_property_t* input_get_device_property(input_host_t* host, input_property_map_t* map,
186        const char* key) {
187    String8 keyString(key);
188    if (map != nullptr) {
189        if (map->propertyMap->hasProperty(keyString)) {
190            auto prop = new input_property_t();
191            if (!map->propertyMap->tryGetProperty(keyString, prop->value)) {
192                delete prop;
193                return nullptr;
194            }
195            prop->key = keyString;
196            return prop;
197        }
198    }
199    return nullptr;
200}
201
202const char* input_get_property_key(input_host_t* host, input_property_t* property) {
203    if (property != nullptr) {
204        return property->key.string();
205    }
206    return nullptr;
207}
208
209const char* input_get_property_value(input_host_t* host, input_property_t* property) {
210    if (property != nullptr) {
211        return property->value.string();
212    }
213    return nullptr;
214}
215
216void input_free_device_property(input_host_t* host, input_property_t* property) {
217    if (property != nullptr) {
218        delete property;
219    }
220}
221
222void input_free_device_property_map(input_host_t* host, input_property_map_t* map) {
223    if (map != nullptr) {
224        delete map->propertyMap;
225        delete map;
226    }
227}
228
229} // namespace android
230