blob: 2fc82c3d132d6d406ca1150ee93fb17a9b4a9ada [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
Prabir Pradhan84395e62022-10-26 19:52:00 +000019#include "HidUsageAccumulator.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070020#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 Pradhan2197cb42022-10-11 02:56:14 +000027 ~KeyboardInputMapper() override = default;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070028
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070029 uint32_t getSources() const override;
Harry Cuttsd02ea102023-03-17 18:21:30 +000030 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070031 void dump(std::string& dump) override;
32 [[nodiscard]] std::list<NotifyArgs> configure(nsecs_t when,
33 const InputReaderConfiguration* config,
34 uint32_t changes) override;
35 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
36 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070037
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070038 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
39 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
40 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
41 uint8_t* outFlags) override;
42 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070043
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070044 int32_t getMetaState() override;
45 bool updateMetaState(int32_t keyCode) override;
46 std::optional<int32_t> getAssociatedDisplayId() override;
47 void updateLedState(bool reset) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070048
49private:
50 // The current viewport.
Prabir Pradhan2197cb42022-10-11 02:56:14 +000051 std::optional<DisplayViewport> mViewport{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070052
53 struct KeyDown {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000054 nsecs_t downTime{};
55 int32_t keyCode{};
56 int32_t scanCode{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070057 };
58
Prabir Pradhan2197cb42022-10-11 02:56:14 +000059 uint32_t mSource{};
60 int32_t mKeyboardType{};
Zixuan Qufecb6062022-11-12 04:44:31 +000061 std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070062
Prabir Pradhan2197cb42022-10-11 02:56:14 +000063 std::vector<KeyDown> mKeyDowns{}; // keys that are down
64 int32_t mMetaState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065
Prabir Pradhan84395e62022-10-26 19:52:00 +000066 HidUsageAccumulator mHidUsageAccumulator;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070067
68 struct LedState {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000069 bool avail{}; // led is available
70 bool on{}; // we think the led is currently on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070071 };
Prabir Pradhan2197cb42022-10-11 02:56:14 +000072 LedState mCapsLockLedState{};
73 LedState mNumLockLedState{};
74 LedState mScrollLockLedState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070075
76 // Immutable configuration parameters.
77 struct Parameters {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000078 bool orientationAware{};
79 bool handlesKeyRepeat{};
80 bool doNotWakeByDefault{};
81 } mParameters{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070082
83 void configureParameters();
Prabir Pradhan2197cb42022-10-11 02:56:14 +000084 void dumpParameters(std::string& dump) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085
Michael Wrighta9cf4192022-12-01 23:46:39 +000086 ui::Rotation getOrientation();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070087 int32_t getDisplayId();
88
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070089 [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
90 int32_t scanCode, int32_t usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091
92 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
93
Prabir Pradhan2197cb42022-10-11 02:56:14 +000094 std::optional<size_t> findKeyDownIndex(int32_t scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070095
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);
Prabir Pradhan2197cb42022-10-11 02:56:14 +000099 std::optional<DisplayViewport> findViewport(const InputReaderConfiguration* config);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700100 [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101};
102
103} // namespace android