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