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 | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 26 | virtual ~KeyboardInputMapper(); |
| 27 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 28 | virtual uint32_t getSources() const override; |
Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 29 | virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) override; |
| 30 | virtual void dump(std::string& dump) override; |
| 31 | virtual void configure(nsecs_t when, const InputReaderConfiguration* config, |
| 32 | uint32_t changes) override; |
| 33 | virtual void reset(nsecs_t when) override; |
| 34 | virtual void process(const RawEvent* rawEvent) override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 35 | |
Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 36 | virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override; |
| 37 | virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override; |
Siarhei Vishniakou | 7400794 | 2022-06-13 13:57:47 -0700 | [diff] [blame] | 38 | virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes, |
| 39 | uint8_t* outFlags) override; |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 40 | virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 41 | |
Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 42 | virtual int32_t getMetaState() override; |
Arthur Hung | cb40a00 | 2021-08-03 14:31:01 +0000 | [diff] [blame] | 43 | virtual bool updateMetaState(int32_t keyCode) override; |
Prabir Pradhan | f1fbf9e | 2019-09-04 16:29:40 -0700 | [diff] [blame] | 44 | virtual std::optional<int32_t> getAssociatedDisplayId() override; |
arthurhung | c903df1 | 2020-08-11 15:08:42 +0800 | [diff] [blame] | 45 | virtual void updateLedState(bool reset); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | // The current viewport. |
| 49 | std::optional<DisplayViewport> mViewport; |
| 50 | |
| 51 | struct KeyDown { |
Arthur Hung | 2141d54 | 2022-08-23 07:45:21 +0000 | [diff] [blame] | 52 | nsecs_t downTime; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 53 | int32_t keyCode; |
| 54 | int32_t scanCode; |
| 55 | }; |
| 56 | |
| 57 | uint32_t mSource; |
| 58 | int32_t mKeyboardType; |
| 59 | |
| 60 | std::vector<KeyDown> mKeyDowns; // keys that are down |
| 61 | int32_t mMetaState; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 62 | |
| 63 | int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none |
| 64 | |
| 65 | struct LedState { |
| 66 | bool avail; // led is available |
| 67 | bool on; // we think the led is currently on |
| 68 | }; |
| 69 | LedState mCapsLockLedState; |
| 70 | LedState mNumLockLedState; |
| 71 | LedState mScrollLockLedState; |
| 72 | |
| 73 | // Immutable configuration parameters. |
| 74 | struct Parameters { |
| 75 | bool orientationAware; |
| 76 | bool handlesKeyRepeat; |
Powei Feng | d041c5d | 2019-05-03 17:11:33 -0700 | [diff] [blame] | 77 | bool doNotWakeByDefault; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 78 | } mParameters; |
| 79 | |
| 80 | void configureParameters(); |
| 81 | void dumpParameters(std::string& dump); |
| 82 | |
| 83 | int32_t getOrientation(); |
| 84 | int32_t getDisplayId(); |
| 85 | |
| 86 | bool isKeyboardOrGamepadKey(int32_t scanCode); |
| 87 | bool isMediaKey(int32_t keyCode); |
| 88 | |
Siarhei Vishniakou | 58ba3d1 | 2021-02-11 01:31:07 +0000 | [diff] [blame] | 89 | void processKey(nsecs_t when, nsecs_t readTime, bool down, int32_t scanCode, int32_t usageCode); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 90 | |
| 91 | bool updateMetaStateIfNeeded(int32_t keyCode, bool down); |
| 92 | |
| 93 | ssize_t findKeyDown(int32_t scanCode); |
| 94 | |
| 95 | void resetLedState(); |
| 96 | void initializeLedState(LedState& ledState, int32_t led); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 97 | void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset); |
| 98 | std::optional<DisplayViewport> findViewport(nsecs_t when, |
| 99 | const InputReaderConfiguration* config); |
Arthur Hung | 2141d54 | 2022-08-23 07:45:21 +0000 | [diff] [blame] | 100 | void cancelAllDownKeys(nsecs_t when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace android |