blob: 31251f44330e98e96cd1820abdc5c29581a5aa56 [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
17#ifndef _UI_INPUTREADER_KEYBOARD_INPUT_MAPPER_H
18#define _UI_INPUTREADER_KEYBOARD_INPUT_MAPPER_H
19
20#include "InputMapper.h"
21
22namespace android {
23
24class KeyboardInputMapper : public InputMapper {
25public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080026 KeyboardInputMapper(InputDeviceContext& deviceContext, uint32_t source, int32_t keyboardType);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070027 virtual ~KeyboardInputMapper();
28
Philip Junker4af3b3d2021-12-14 10:36:55 +010029 virtual uint32_t getSources() const override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070030 virtual void populateDeviceInfo(InputDeviceInfo* deviceInfo) override;
31 virtual void dump(std::string& dump) override;
32 virtual void configure(nsecs_t when, const InputReaderConfiguration* config,
33 uint32_t changes) override;
34 virtual void reset(nsecs_t when) override;
35 virtual void process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070036
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070037 virtual int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
38 virtual int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
Siarhei Vishniakou74007942022-06-13 13:57:47 -070039 virtual bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
40 uint8_t* outFlags) override;
Philip Junker4af3b3d2021-12-14 10:36:55 +010041 virtual int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070042
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070043 virtual int32_t getMetaState() override;
Arthur Hungcb40a002021-08-03 14:31:01 +000044 virtual bool updateMetaState(int32_t keyCode) override;
Prabir Pradhanf1fbf9e2019-09-04 16:29:40 -070045 virtual std::optional<int32_t> getAssociatedDisplayId() override;
arthurhungc903df12020-08-11 15:08:42 +080046 virtual void updateLedState(bool reset);
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 Vishniakou58ba3d12021-02-11 01:31:07 +000090 void processKey(nsecs_t when, nsecs_t readTime, bool down, int32_t scanCode, int32_t usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091
92 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
93
94 ssize_t findKeyDown(int32_t scanCode);
95
96 void resetLedState();
97 void initializeLedState(LedState& ledState, int32_t led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070098 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
99 std::optional<DisplayViewport> findViewport(nsecs_t when,
100 const InputReaderConfiguration* config);
Arthur Hung2141d542022-08-23 07:45:21 +0000101 void cancelAllDownKeys(nsecs_t when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700102};
103
104} // namespace android
105
106#endif // _UI_INPUTREADER_KEYBOARD_INPUT_MAPPER_H