Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 _LIBINPUT_KEY_LAYOUT_MAP_H |
| 18 | #define _LIBINPUT_KEY_LAYOUT_MAP_H |
| 19 | |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame^] | 20 | #include <android-base/result.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/KeyedVector.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame^] | 25 | #include <utils/Tokenizer.h> |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | struct AxisInfo { |
| 30 | enum Mode { |
| 31 | // Axis value is reported directly. |
| 32 | MODE_NORMAL = 0, |
| 33 | // Axis value should be inverted before reporting. |
| 34 | MODE_INVERT = 1, |
| 35 | // Axis value should be split into two axes |
| 36 | MODE_SPLIT = 2, |
| 37 | }; |
| 38 | |
| 39 | // Axis mode. |
| 40 | Mode mode; |
| 41 | |
| 42 | // Axis id. |
| 43 | // When split, this is the axis used for values smaller than the split position. |
| 44 | int32_t axis; |
| 45 | |
| 46 | // When split, this is the axis used for values after higher than the split position. |
| 47 | int32_t highAxis; |
| 48 | |
| 49 | // The split value, or 0 if not split. |
| 50 | int32_t splitValue; |
| 51 | |
| 52 | // The flat value, or -1 if none. |
| 53 | int32_t flatOverride; |
| 54 | |
| 55 | AxisInfo() : mode(MODE_NORMAL), axis(-1), highAxis(-1), splitValue(0), flatOverride(-1) { |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * Describes a mapping from keyboard scan codes and joystick axes to Android key codes and axes. |
| 61 | * |
| 62 | * This object is immutable after it has been loaded. |
| 63 | */ |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame^] | 64 | class KeyLayoutMap { |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 65 | public: |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame^] | 66 | static base::Result<std::shared_ptr<KeyLayoutMap>> load(const std::string& filename); |
| 67 | static base::Result<std::shared_ptr<KeyLayoutMap>> load(Tokenizer* tokenizer); |
| 68 | static base::Result<std::shared_ptr<KeyLayoutMap>> loadContents(const std::string& filename, |
| 69 | const char* contents); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 70 | |
| 71 | status_t mapKey(int32_t scanCode, int32_t usageCode, |
| 72 | int32_t* outKeyCode, uint32_t* outFlags) const; |
Arthur Hung | 7c3ae9c | 2019-03-11 11:23:03 +0800 | [diff] [blame] | 73 | status_t findScanCodesForKey(int32_t keyCode, std::vector<int32_t>* outScanCodes) const; |
Michael Wright | 74bdd2e | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 74 | status_t findScanCodeForLed(int32_t ledCode, int32_t* outScanCode) const; |
| 75 | status_t findUsageCodeForLed(int32_t ledCode, int32_t* outUsageCode) const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 76 | |
| 77 | status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const; |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame^] | 78 | const std::string getLoadFileName() const; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 79 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 80 | virtual ~KeyLayoutMap(); |
| 81 | |
| 82 | private: |
| 83 | struct Key { |
| 84 | int32_t keyCode; |
| 85 | uint32_t flags; |
| 86 | }; |
| 87 | |
Michael Wright | 74bdd2e | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 88 | struct Led { |
| 89 | int32_t ledCode; |
| 90 | }; |
| 91 | |
| 92 | |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 93 | KeyedVector<int32_t, Key> mKeysByScanCode; |
| 94 | KeyedVector<int32_t, Key> mKeysByUsageCode; |
| 95 | KeyedVector<int32_t, AxisInfo> mAxes; |
Michael Wright | 74bdd2e | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 96 | KeyedVector<int32_t, Led> mLedsByScanCode; |
| 97 | KeyedVector<int32_t, Led> mLedsByUsageCode; |
Chris Ye | 1abffbd | 2020-08-18 12:50:12 -0700 | [diff] [blame^] | 98 | std::string mLoadFileName; |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 99 | |
| 100 | KeyLayoutMap(); |
| 101 | |
| 102 | const Key* getKey(int32_t scanCode, int32_t usageCode) const; |
| 103 | |
| 104 | class Parser { |
| 105 | KeyLayoutMap* mMap; |
| 106 | Tokenizer* mTokenizer; |
| 107 | |
| 108 | public: |
| 109 | Parser(KeyLayoutMap* map, Tokenizer* tokenizer); |
| 110 | ~Parser(); |
| 111 | status_t parse(); |
| 112 | |
| 113 | private: |
| 114 | status_t parseKey(); |
| 115 | status_t parseAxis(); |
Michael Wright | 74bdd2e | 2013-10-17 17:35:53 -0700 | [diff] [blame] | 116 | status_t parseLed(); |
Jeff Brown | 5912f95 | 2013-07-01 19:10:31 -0700 | [diff] [blame] | 117 | }; |
| 118 | }; |
| 119 | |
| 120 | } // namespace android |
| 121 | |
| 122 | #endif // _LIBINPUT_KEY_LAYOUT_MAP_H |