blob: 3dd570d79b3e9e00519048045fbe60f52e1c0de7 [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 Pradhan2197cb42022-10-11 02:56:14 +000026 ~KeyboardInputMapper() override = default;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070027
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.
Prabir Pradhan2197cb42022-10-11 02:56:14 +000050 std::optional<DisplayViewport> mViewport{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051
52 struct KeyDown {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000053 nsecs_t downTime{};
54 int32_t keyCode{};
55 int32_t scanCode{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070056 };
57
Prabir Pradhan2197cb42022-10-11 02:56:14 +000058 uint32_t mSource{};
59 int32_t mKeyboardType{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060
Prabir Pradhan2197cb42022-10-11 02:56:14 +000061 std::vector<KeyDown> mKeyDowns{}; // keys that are down
62 int32_t mMetaState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070063
Prabir Pradhan2197cb42022-10-11 02:56:14 +000064 int32_t mCurrentHidUsage{}; // most recent HID usage seen this packet, or 0 if none
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065
66 struct LedState {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000067 bool avail{}; // led is available
68 bool on{}; // we think the led is currently on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070069 };
Prabir Pradhan2197cb42022-10-11 02:56:14 +000070 LedState mCapsLockLedState{};
71 LedState mNumLockLedState{};
72 LedState mScrollLockLedState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070073
74 // Immutable configuration parameters.
75 struct Parameters {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000076 bool orientationAware{};
77 bool handlesKeyRepeat{};
78 bool doNotWakeByDefault{};
79 } mParameters{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070080
81 void configureParameters();
Prabir Pradhan2197cb42022-10-11 02:56:14 +000082 void dumpParameters(std::string& dump) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083
84 int32_t getOrientation();
85 int32_t getDisplayId();
86
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070087 [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
88 int32_t scanCode, int32_t usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089
90 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
91
Prabir Pradhan2197cb42022-10-11 02:56:14 +000092 std::optional<size_t> findKeyDownIndex(int32_t scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093
94 void resetLedState();
95 void initializeLedState(LedState& ledState, int32_t led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070096 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
Prabir Pradhan2197cb42022-10-11 02:56:14 +000097 std::optional<DisplayViewport> findViewport(const InputReaderConfiguration* config);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070098 [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070099};
100
101} // namespace android