blob: 8d72ee9629c71c43f56f650b8ff2d88300ff5d73 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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 Pradhan48108662022-09-09 21:22:04 +000017#pragma once
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
19#include "InputMapper.h"
20
21namespace android {
22
23class KeyboardInputMapper : public InputMapper {
24public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080025 KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source, int32_t keyboardType);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070026 virtual ~KeyboardInputMapper();
27
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070028 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 Pradhanbaa5c822019-08-30 15:27:05 -070036
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070037 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 Pradhanbaa5c822019-08-30 15:27:05 -070042
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070043 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 Pradhanbaa5c822019-08-30 15:27:05 -070047
48private:
49 // The current viewport.
50 std::optional<DisplayViewport> mViewport;
51
52 struct KeyDown {
Arthur Hung2141d542022-08-23 07:45:21 +000053 nsecs_t downTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070054 int32_t keyCode;
55 int32_t scanCode;
56 };
57
58 uint32_t mSource;
59 int32_t mKeyboardType;
60
61 std::vector<KeyDown> mKeyDowns; // keys that are down
62 int32_t mMetaState;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070063
64 int32_t mCurrentHidUsage; // most recent HID usage seen this packet, or 0 if none
65
66 struct LedState {
67 bool avail; // led is available
68 bool on; // we think the led is currently on
69 };
70 LedState mCapsLockLedState;
71 LedState mNumLockLedState;
72 LedState mScrollLockLedState;
73
74 // Immutable configuration parameters.
75 struct Parameters {
76 bool orientationAware;
77 bool handlesKeyRepeat;
Powei Fengd041c5d2019-05-03 17:11:33 -070078 bool doNotWakeByDefault;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070079 } mParameters;
80
81 void configureParameters();
82 void dumpParameters(std::string& dump);
83
84 int32_t getOrientation();
85 int32_t getDisplayId();
86
87 bool isKeyboardOrGamepadKey(int32_t scanCode);
88 bool isMediaKey(int32_t keyCode);
89
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070090 [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
91 int32_t scanCode, int32_t usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070092
93 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
94
95 ssize_t findKeyDown(int32_t scanCode);
96
97 void resetLedState();
98 void initializeLedState(LedState& ledState, int32_t led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
100 std::optional<DisplayViewport> findViewport(nsecs_t when,
101 const InputReaderConfiguration* config);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700102 [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700103};
104
105} // namespace android