blob: e9c822276cdf697a1c550bb3273f2574512f9723 [file] [log] [blame]
Michael Wright5113aff2015-02-13 17:31:41 -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#define LOG_NDEBUG 0
18#define LOG_TAG "EvdevModule"
19
Tim Kilbourn73475a42015-02-13 10:35:20 -080020#include <memory>
21#include <string>
22#include <thread>
23
Michael Wright5113aff2015-02-13 17:31:41 -080024#include <assert.h>
25#include <hardware/hardware.h>
26#include <hardware/input.h>
27
Tim Kilbourn73475a42015-02-13 10:35:20 -080028#include <utils/Log.h>
29
30#include "InputHub.h"
31#include "InputDeviceManager.h"
32#include "InputHost.h"
33
34namespace android {
35
36static const char kDevInput[] = "/dev/input";
37
38class EvdevModule {
39public:
40 explicit EvdevModule(InputHost inputHost);
41
42 void init();
43 void notifyReport(input_report_t* r);
44
45private:
46 void loop();
47
48 InputHost mInputHost;
49 std::shared_ptr<InputDeviceManager> mDeviceManager;
50 std::shared_ptr<InputHub> mInputHub;
51 std::thread mPollThread;
52};
53
54static std::shared_ptr<EvdevModule> gEvdevModule;
55
56EvdevModule::EvdevModule(InputHost inputHost) :
57 mInputHost(inputHost),
58 mDeviceManager(std::make_shared<InputDeviceManager>()),
59 mInputHub(std::make_shared<InputHub>(mDeviceManager)) {}
60
61void EvdevModule::init() {
62 ALOGV("%s", __func__);
63
64 mInputHub->registerDevicePath(kDevInput);
65 mPollThread = std::thread(&EvdevModule::loop, this);
66}
67
68void EvdevModule::notifyReport(input_report_t* r) {
69 ALOGV("%s", __func__);
70
71 // notifyReport() will be called from an arbitrary thread within the input
72 // host. Since InputHub is not threadsafe, this is how I expect this to
73 // work:
74 // * notifyReport() will queue up the output report in the EvdevModule and
75 // call wake() on the InputHub.
76 // * In the main loop thread, after returning from poll(), the queue will
77 // be processed with any pending work.
78}
79
80void EvdevModule::loop() {
81 ALOGV("%s", __func__);
82 for (;;) {
83 mInputHub->poll();
84
85 // TODO: process any pending work, like notify reports
86 }
87}
Michael Wright5113aff2015-02-13 17:31:41 -080088
89extern "C" {
90
91static int dummy_open(const hw_module_t __unused *module, const char __unused *id,
Tim Kilbourn73475a42015-02-13 10:35:20 -080092 hw_device_t __unused **device) {
93 ALOGW("open not implemented in the input HAL!");
Michael Wright5113aff2015-02-13 17:31:41 -080094 return 0;
95}
96
97static void input_init(const input_module_t* module,
98 input_host_t* host, input_host_callbacks_t cb) {
Tim Kilbourn73475a42015-02-13 10:35:20 -080099 LOG_ALWAYS_FATAL_IF(strcmp(module->common.id, INPUT_HARDWARE_MODULE_ID) != 0);
100 InputHost inputHost = {host, cb};
101 gEvdevModule = std::make_shared<EvdevModule>(inputHost);
102 gEvdevModule->init();
Michael Wright5113aff2015-02-13 17:31:41 -0800103}
104
Tim Kilbourn73475a42015-02-13 10:35:20 -0800105static void input_notify_report(const input_module_t* module, input_report_t* r) {
106 LOG_ALWAYS_FATAL_IF(strcmp(module->common.id, INPUT_HARDWARE_MODULE_ID) != 0);
107 LOG_ALWAYS_FATAL_IF(gEvdevModule == nullptr);
108 gEvdevModule->notifyReport(r);
Michael Wright5113aff2015-02-13 17:31:41 -0800109}
110
111static struct hw_module_methods_t input_module_methods = {
112 .open = dummy_open,
113};
114
115input_module_t HAL_MODULE_INFO_SYM = {
116 .common = {
117 .tag = HARDWARE_MODULE_TAG,
118 .module_api_version = INPUT_MODULE_API_VERSION_1_0,
119 .hal_api_version = HARDWARE_HAL_API_VERSION,
120 .id = INPUT_HARDWARE_MODULE_ID,
121 .name = "Input evdev HAL",
122 .author = "The Android Open Source Project",
123 .methods = &input_module_methods,
124 .dso = NULL,
125 .reserved = {0},
126 },
127
128 .init = input_init,
129 .notify_report = input_notify_report,
130};
131
132} // extern "C"
133
134} // namespace input