Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 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 | |
Prabir Pradhan | 4810866 | 2022-09-09 21:22:04 +0000 | [diff] [blame] | 17 | #pragma once |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
Henry Barnor | a7ed637 | 2024-10-21 14:33:43 -0700 | [diff] [blame] | 19 | #include <com_android_input_flags.h> |
| 20 | |
Prabir Pradhan | 84395e6 | 2022-10-26 19:52:00 +0000 | [diff] [blame] | 21 | #include "HidUsageAccumulator.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 22 | #include "InputMapper.h" |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | class KeyboardInputMapper : public InputMapper { |
| 27 | public: |
Arpit Singh | 67ca684 | 2023-04-26 14:43:16 +0000 | [diff] [blame] | 28 | template <class T, class... Args> |
| 29 | friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext, |
| 30 | const InputReaderConfiguration& readerConfig, |
| 31 | Args... args); |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 32 | ~KeyboardInputMapper() override = default; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 33 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 34 | uint32_t getSources() const override; |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 35 | void populateDeviceInfo(InputDeviceInfo& deviceInfo) override; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 36 | void dump(std::string& dump) override; |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 37 | [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 38 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame] | 39 | ConfigurationChanges changes) override; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 40 | [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override; |
Harry Cutts | a32a119 | 2024-06-04 15:10:31 +0000 | [diff] [blame] | 41 | [[nodiscard]] std::list<NotifyArgs> process(const RawEvent& rawEvent) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 42 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 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; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 48 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 49 | int32_t getMetaState() override; |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 50 | std::optional<ui::LogicalDisplayId> getAssociatedDisplayId() override; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 51 | void updateLedState(bool reset) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | // The current viewport. |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 55 | std::optional<DisplayViewport> mViewport{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 56 | |
| 57 | struct KeyDown { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 58 | nsecs_t downTime{}; |
| 59 | int32_t keyCode{}; |
| 60 | int32_t scanCode{}; |
Justin Chung | 71ddb43 | 2023-03-27 04:29:07 +0000 | [diff] [blame] | 61 | int32_t flags{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Prabir Pradhan | 3863665 | 2024-07-23 21:59:36 +0000 | [diff] [blame] | 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 | |
Zixuan Qu | fecb606 | 2022-11-12 04:44:31 +0000 | [diff] [blame] | 68 | std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 69 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 70 | std::vector<KeyDown> mKeyDowns{}; // keys that are down |
| 71 | int32_t mMetaState{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 72 | |
Prabir Pradhan | 84395e6 | 2022-10-26 19:52:00 +0000 | [diff] [blame] | 73 | HidUsageAccumulator mHidUsageAccumulator; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 74 | |
| 75 | struct LedState { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 76 | bool avail{}; // led is available |
| 77 | bool on{}; // we think the led is currently on |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 78 | }; |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 79 | LedState mCapsLockLedState{}; |
| 80 | LedState mNumLockLedState{}; |
| 81 | LedState mScrollLockLedState{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 82 | |
| 83 | // Immutable configuration parameters. |
| 84 | struct Parameters { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 85 | bool orientationAware{}; |
| 86 | bool handlesKeyRepeat{}; |
| 87 | bool doNotWakeByDefault{}; |
| 88 | } mParameters{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 89 | |
Henry Barnor | a7ed637 | 2024-10-21 14:33:43 -0700 | [diff] [blame] | 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 | |
Arpit Singh | 67ca684 | 2023-04-26 14:43:16 +0000 | [diff] [blame] | 94 | KeyboardInputMapper(InputDeviceContext& deviceContext, |
Vaibhav Devmurari | e58ffb9 | 2024-05-22 17:38:25 +0000 | [diff] [blame] | 95 | const InputReaderConfiguration& readerConfig, uint32_t source); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 96 | void configureParameters(); |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 97 | void dumpParameters(std::string& dump) const; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 98 | |
Michael Wright | a9cf419 | 2022-12-01 23:46:39 +0000 | [diff] [blame] | 99 | ui::Rotation getOrientation(); |
Linnan Li | 13bf76a | 2024-05-05 19:18:02 +0800 | [diff] [blame] | 100 | ui::LogicalDisplayId getDisplayId(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 101 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 102 | [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down, |
| 103 | int32_t scanCode, int32_t usageCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 104 | |
| 105 | bool updateMetaStateIfNeeded(int32_t keyCode, bool down); |
| 106 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame] | 107 | std::optional<size_t> findKeyDownIndex(int32_t scanCode); |
Vaibhav Devmurari | dec3080 | 2023-07-11 15:02:03 +0000 | [diff] [blame] | 108 | std::optional<KeyboardLayoutInfo> getKeyboardLayoutInfo() const; |
| 109 | bool updateKeyboardLayoutOverlay(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 110 | |
| 111 | void resetLedState(); |
| 112 | void initializeLedState(LedState& ledState, int32_t led); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 113 | void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset); |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 114 | std::optional<DisplayViewport> findViewport(const InputReaderConfiguration& readerConfig); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 115 | [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when); |
Arpit Singh | 82e413e | 2023-10-10 19:30:58 +0000 | [diff] [blame] | 116 | void onKeyDownProcessed(nsecs_t downTime); |
Prabir Pradhan | 3863665 | 2024-07-23 21:59:36 +0000 | [diff] [blame] | 117 | uint32_t getEventSource() const; |
Henry Barnor | a7ed637 | 2024-10-21 14:33:43 -0700 | [diff] [blame] | 118 | |
| 119 | bool wakeOnAlphabeticKeyboard(const KeyboardType keyboardType) const; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace android |