Home | History | Annotate | Download | only in evdev
      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 #define LOG_NDEBUG 0
     18 #define LOG_TAG "EvdevModule"
     19 
     20 #include <memory>
     21 #include <string>
     22 #include <thread>
     23 
     24 #include <assert.h>
     25 #include <hardware/hardware.h>
     26 #include <hardware/input.h>
     27 
     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:
     40     explicit EvdevModule(InputHost inputHost);
     41 
     42     void init();
     43     void notifyReport(input_report_t* r);
     44 
     45 private:
     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 
     54 static std::shared_ptr<EvdevModule> gEvdevModule;
     55 
     56 EvdevModule::EvdevModule(InputHost inputHost) :
     57     mInputHost(inputHost),
     58     mDeviceManager(std::make_shared<InputDeviceManager>()),
     59     mInputHub(std::make_shared<InputHub>(mDeviceManager)) {}
     60 
     61 void EvdevModule::init() {
     62     ALOGV("%s", __func__);
     63 
     64     mInputHub->registerDevicePath(kDevInput);
     65     mPollThread = std::thread(&EvdevModule::loop, this);
     66 }
     67 
     68 void 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 
     80 void EvdevModule::loop() {
     81     ALOGV("%s", __func__);
     82     for (;;) {
     83         mInputHub->poll();
     84 
     85         // TODO: process any pending work, like notify reports
     86     }
     87 }
     88 
     89 extern "C" {
     90 
     91 static int dummy_open(const hw_module_t __unused *module, const char __unused *id,
     92         hw_device_t __unused **device) {
     93     ALOGW("open not implemented in the input HAL!");
     94     return 0;
     95 }
     96 
     97 static void input_init(const input_module_t* module,
     98         input_host_t* host, input_host_callbacks_t cb) {
     99     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();
    103 }
    104 
    105 static 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);
    109 }
    110 
    111 static struct hw_module_methods_t input_module_methods = {
    112     .open = dummy_open,
    113 };
    114 
    115 input_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
    135