xref: /aosp_15_r20/frameworks/native/services/inputflinger/reader/mapper/KeyboardInputMapper.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2019 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 #pragma once
18 
19 #include <com_android_input_flags.h>
20 
21 #include "HidUsageAccumulator.h"
22 #include "InputMapper.h"
23 
24 namespace android {
25 
26 class KeyboardInputMapper : public InputMapper {
27 public:
28     template <class T, class... Args>
29     friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext,
30                                                 const InputReaderConfiguration& readerConfig,
31                                                 Args... args);
32     ~KeyboardInputMapper() override = default;
33 
34     uint32_t getSources() const override;
35     void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
36     void dump(std::string& dump) override;
37     [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when,
38                                                     const InputReaderConfiguration& config,
39                                                     ConfigurationChanges changes) override;
40     [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
41     [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent) override;
42 
43     int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
44     int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
45     bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
46                                uint8_t* outFlags) override;
47     int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override;
48 
49     int32_t getMetaState() override;
50     std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override;
51     void updateLedState(bool reset) override;
52 
53 private:
54     // The current viewport.
55     std::optional<DisplayViewport> mViewport{};
56 
57     struct KeyDown {
58         nsecs_t downTime{};
59         int32_t keyCode{};
60         int32_t scanCode{};
61         int32_t flags{};
62     };
63 
64     // The keyboard source for this mapper. Events generated should use the source shared
65     // by all KeyboardInputMappers for this input device.
66     uint32_t mMapperSource{};
67 
68     std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo;
69 
70     std::vector<KeyDown> mKeyDowns{}; // keys that are down
71     int32_t mMetaState{};
72 
73     HidUsageAccumulator mHidUsageAccumulator;
74 
75     struct LedState {
76         bool avail{}; // led is available
77         bool on{};    // we think the led is currently on
78     };
79     LedState mCapsLockLedState{};
80     LedState mNumLockLedState{};
81     LedState mScrollLockLedState{};
82 
83     // Immutable configuration parameters.
84     struct Parameters {
85         bool orientationAware{};
86         bool handlesKeyRepeat{};
87         bool doNotWakeByDefault{};
88     } mParameters{};
89 
90     // Store the value of enable wake for alphanumeric keyboard flag.
91     const bool mEnableAlphabeticKeyboardWakeFlag =
92             com::android::input::flags::enable_alphabetic_keyboard_wake();
93 
94     KeyboardInputMapper(InputDeviceContext& deviceContext,
95                         const InputReaderConfiguration& readerConfig, uint32_t source);
96     void configureParameters();
97     void dumpParameters(std::string& dump) const;
98 
99     ui::Rotation getOrientation();
100     ui::LogicalDisplayId getDisplayId();
101 
102     [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
103                                                    int32_t scanCode, int32_t usageCode);
104 
105     bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
106 
107     std::optional<size_t> findKeyDownIndex(int32_t scanCode);
108     std::optional<KeyboardLayoutInfo> getKeyboardLayoutInfo() const;
109     bool updateKeyboardLayoutOverlay();
110 
111     void resetLedState();
112     void initializeLedState(LedState& ledState, int32_t led);
113     void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
114     std::optional<DisplayViewport> findViewport(const InputReaderConfiguration& readerConfig);
115     [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when);
116     void onKeyDownProcessed(nsecs_t downTime);
117     uint32_t getEventSource() const;
118 
119     bool wakeOnAlphabeticKeyboard(const KeyboardType keyboardType) const;
120 };
121 
122 } // namespace android
123