blob: 2136d253c9411bf1eddb672d337c25bbf01ee0ca [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
Philip Junker4af3b3d2021-12-14 10:36:55 +010028 virtual uint32_t getSources() const override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070029 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 Pradhanbaa5c822019-08-30 15:27:05 -070035
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070036 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
37 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -070038 virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
39 uint8_t* outFlags) override;
Philip Junker4af3b3d2021-12-14 10:36:55 +010040 virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070041
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070042 virtual int32_t getMetaState() override;
Arthur Hungcb40a002021-08-03 14:31:01 +000043 virtual bool updateMetaState(int32_t keyCode) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070044 virtual std::optional<int32_t> getAssociatedDisplayId() override;
arthurhungc903df12020-08-11 15:08:42 +080045 virtual void updateLedState(bool reset);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046
47private:
48 // The current viewport.
49 std::optional<DisplayViewport> mViewport;
50
51 struct KeyDown {
Arthur Hung2141d542022-08-23 07:45:21 +000052 nsecs_t downTime;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070053 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 Pradhanbaa5c822019-08-30 15:27:05 -070062
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 Fengd041c5d2019-05-03 17:11:33 -070077 bool doNotWakeByDefault;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078 } 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 Vishniakou58ba3d12021-02-11 01:31:07 +000089 void processKey(nsecs_t when, nsecs_t readTime, bool down, int32_t scanCode, int32_t usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070090
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 Pradhanbaa5c822019-08-30 15:27:05 -070097 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 Hung2141d542022-08-23 07:45:21 +0000100 void cancelAllDownKeys(nsecs_t when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101};
102
103} // namespace android