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 | |
| 19 | #include "InputMapper.h" |
| 20 | |
| 21 | namespace android { |
| 22 | |
| 23 | class KeyboardInputMapper : public InputMapper { |
| 24 | public: |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 25 | KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source, int32_t keyboardType); |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 26 | ~KeyboardInputMapper() override = default; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 27 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 28 | uint32_t getSources() const override; |
| 29 | void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; |
| 30 | void dump(std::string& dump) override; |
| 31 | [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when, |
| 32 | const InputReaderConfiguration* config, |
| 33 | uint32_t changes) override; |
| 34 | [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override; |
| 35 | [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 36 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 37 | int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override; |
| 38 | int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override; |
| 39 | bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes, |
| 40 | uint8_t* outFlags) override; |
| 41 | int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const 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 getMetaState() override; |
| 44 | bool updateMetaState(int32_t keyCode) override; |
| 45 | std::optional<int32_t> getAssociatedDisplayId() override; |
| 46 | void updateLedState(bool reset) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | // The current viewport. |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 50 | std::optional<DisplayViewport> mViewport{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 51 | |
| 52 | struct KeyDown { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 53 | nsecs_t downTime{}; |
| 54 | int32_t keyCode{}; |
| 55 | int32_t scanCode{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 58 | uint32_t mSource{}; |
| 59 | int32_t mKeyboardType{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 60 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 61 | std::vector<KeyDown> mKeyDowns{}; // keys that are down |
| 62 | int32_t mMetaState{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 63 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 64 | int32_t mCurrentHidUsage{}; // most recent HID usage seen this packet, or 0 if none |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 65 | |
| 66 | struct LedState { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 67 | bool avail{}; // led is available |
| 68 | bool on{}; // we think the led is currently on |
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 | LedState mCapsLockLedState{}; |
| 71 | LedState mNumLockLedState{}; |
| 72 | LedState mScrollLockLedState{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 73 | |
| 74 | // Immutable configuration parameters. |
| 75 | struct Parameters { |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 76 | bool orientationAware{}; |
| 77 | bool handlesKeyRepeat{}; |
| 78 | bool doNotWakeByDefault{}; |
| 79 | } mParameters{}; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 80 | |
| 81 | void configureParameters(); |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 82 | void dumpParameters(std::string& dump) const; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 83 | |
| 84 | int32_t getOrientation(); |
| 85 | int32_t getDisplayId(); |
| 86 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 87 | [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down, |
| 88 | int32_t scanCode, int32_t usageCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 89 | |
| 90 | bool updateMetaStateIfNeeded(int32_t keyCode, bool down); |
| 91 | |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 92 | std::optional<size_t> findKeyDownIndex(int32_t scanCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 93 | |
| 94 | void resetLedState(); |
| 95 | void initializeLedState(LedState& ledState, int32_t led); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 96 | void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset); |
Prabir Pradhan | 2197cb4 | 2022-10-11 02:56:14 +0000 | [diff] [blame^] | 97 | std::optional<DisplayViewport> findViewport(const InputReaderConfiguration* config); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 98 | [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace android |