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#ifndef ANDROID_INPUT_MAPPER_H_
18#define ANDROID_INPUT_MAPPER_H_
19
20struct input_device_handle;
21
22namespace android {
23
24class InputDeviceNode;
25class InputReport;
26class InputReportDefinition;
27struct InputEvent;
28using InputDeviceHandle = struct input_device_handle;
29
30/**
31 * An InputMapper processes raw evdev input events and combines them into
32 * Android input HAL reports. A given InputMapper will focus on a particular
33 * type of input, like key presses or touch events. A single InputDevice may
34 * have multiple InputMappers, corresponding to the different types of inputs it
35 * supports.
36 */
37class InputMapper {
38public:
39    InputMapper() = default;
40    virtual ~InputMapper() {}
41
42    /**
43     * If the mapper supports input events from the InputDevice,
44     * configureInputReport will populate the InputReportDefinition and return
45     * true. If input is not supported, false is returned, and the InputDevice
46     * may free or re-use the InputReportDefinition.
47     */
48    virtual bool configureInputReport(InputDeviceNode* devNode, InputReportDefinition* report) {
49        return false;
50    }
51
52    /**
53     * If the mapper supports output events from the InputDevice,
54     * configureOutputReport will populate the InputReportDefinition and return
55     * true. If output is not supported, false is returned, and the InputDevice
56     * may free or re-use the InputReportDefinition.
57     */
58    virtual bool configureOutputReport(InputDeviceNode* devNode, InputReportDefinition* report) {
59        return false;
60    }
61
62    // Set the InputDeviceHandle after registering the device with the host.
63    virtual void setDeviceHandle(InputDeviceHandle* handle) { mDeviceHandle = handle; }
64    // Process the InputEvent.
65    virtual void process(const InputEvent& event) = 0;
66
67protected:
68    virtual void setInputReportDefinition(InputReportDefinition* reportDef) final {
69        mInputReportDef = reportDef;
70    }
71    virtual void setOutputReportDefinition(InputReportDefinition* reportDef) final {
72        mOutputReportDef = reportDef;
73    }
74    virtual InputReportDefinition* getInputReportDefinition() final { return mInputReportDef; }
75    virtual InputReportDefinition* getOutputReportDefinition() final { return mOutputReportDef; }
76    virtual InputDeviceHandle* getDeviceHandle() final { return mDeviceHandle; }
77    virtual InputReport* getInputReport() final;
78
79private:
80    InputReportDefinition* mInputReportDef = nullptr;
81    InputReportDefinition* mOutputReportDef = nullptr;
82    InputDeviceHandle* mDeviceHandle = nullptr;
83    InputReport* mReport = nullptr;
84};
85
86}  // namespace android
87
88#endif  // ANDROID_INPUT_MAPPER_H_
89