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