blob: 96044eb0a85f6a8398cb256a6ee2d4a1d3478a61 [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:
Arpit Singh033e3ec2023-04-26 14:43:16 +000026 template <class T, class... Args>
27 friend std::unique_ptr<T> createInputMapper(InputDeviceContext& deviceContext,
28 const InputReaderConfiguration& readerConfig,
29 Args... args);
Prabir Pradhan2197cb42022-10-11 02:56:14 +000030 ~KeyboardInputMapper() override = default;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070031
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070032 uint32_t getSources() const override;
Harry Cuttsd02ea102023-03-17 18:21:30 +000033 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070034 void dump(std::string& dump) override;
Arpit Singh4be4eef2023-03-28 14:26:01 +000035 [[nodiscard]] std::list<NotifyArgs> reconfigure(nsecs_t when,
Arpit Singhed6c3de2023-04-05 19:24:37 +000036 const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000037 ConfigurationChanges changes) override;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070038 [[nodiscard]] std::list<NotifyArgs> reset(nsecs_t when) override;
39 [[nodiscard]] std::list<NotifyArgs> process(const RawEvent* rawEvent) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070040
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070041 int32_t getKeyCodeState(uint32_t sourceMask, int32_t keyCode) override;
42 int32_t getScanCodeState(uint32_t sourceMask, int32_t scanCode) override;
43 bool markSupportedKeyCodes(uint32_t sourceMask, const std::vector<int32_t>& keyCodes,
44 uint8_t* outFlags) override;
45 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070046
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070047 int32_t getMetaState() override;
48 bool updateMetaState(int32_t keyCode) override;
49 std::optional<int32_t> getAssociatedDisplayId() override;
50 void updateLedState(bool reset) override;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070051
52private:
53 // The current viewport.
Prabir Pradhan2197cb42022-10-11 02:56:14 +000054 std::optional<DisplayViewport> mViewport{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070055
56 struct KeyDown {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000057 nsecs_t downTime{};
58 int32_t keyCode{};
59 int32_t scanCode{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070060 };
61
Prabir Pradhan2197cb42022-10-11 02:56:14 +000062 uint32_t mSource{};
63 int32_t mKeyboardType{};
Zixuan Qufecb6062022-11-12 04:44:31 +000064 std::optional<KeyboardLayoutInfo> mKeyboardLayoutInfo;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070065
Prabir Pradhan2197cb42022-10-11 02:56:14 +000066 std::vector<KeyDown> mKeyDowns{}; // keys that are down
67 int32_t mMetaState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070068
Prabir Pradhan84395e62022-10-26 19:52:00 +000069 HidUsageAccumulator mHidUsageAccumulator;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070070
71 struct LedState {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000072 bool avail{}; // led is available
73 bool on{}; // we think the led is currently on
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070074 };
Prabir Pradhan2197cb42022-10-11 02:56:14 +000075 LedState mCapsLockLedState{};
76 LedState mNumLockLedState{};
77 LedState mScrollLockLedState{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070078
79 // Immutable configuration parameters.
80 struct Parameters {
Prabir Pradhan2197cb42022-10-11 02:56:14 +000081 bool orientationAware{};
82 bool handlesKeyRepeat{};
83 bool doNotWakeByDefault{};
84 } mParameters{};
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070085
Arpit Singh033e3ec2023-04-26 14:43:16 +000086 KeyboardInputMapper(InputDeviceContext& deviceContext,
87 const InputReaderConfiguration& readerConfig, uint32_t source,
88 int32_t keyboardType);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070089 void configureParameters();
Prabir Pradhan2197cb42022-10-11 02:56:14 +000090 void dumpParameters(std::string& dump) const;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070091
Michael Wrighta9cf4192022-12-01 23:46:39 +000092 ui::Rotation getOrientation();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070093 int32_t getDisplayId();
94
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070095 [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
96 int32_t scanCode, int32_t usageCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070097
98 bool updateMetaStateIfNeeded(int32_t keyCode, bool down);
99
Prabir Pradhan2197cb42022-10-11 02:56:14 +0000100 std::optional<size_t> findKeyDownIndex(int32_t scanCode);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101
102 void resetLedState();
103 void initializeLedState(LedState& ledState, int32_t led);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700104 void updateLedStateForModifier(LedState& ledState, int32_t led, int32_t modifier, bool reset);
Arpit Singhed6c3de2023-04-05 19:24:37 +0000105 std::optional<DisplayViewport> findViewport(const InputReaderConfiguration& readerConfig);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700106 [[nodiscard]] std::list<NotifyArgs> cancelAllDownKeys(nsecs_t when);
Arpit Singha5ea7c12023-07-05 15:39:25 +0000107 void onKeyDownProcessed();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700108};
109
110} // namespace android