blob: e7f22a260c3b0c9c51f592b58742252a6ce15865 [file] [log] [blame]
Jeff Brown6b53e8d2010-11-10 16:03:06 -08001/*
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
Mathias Agopianb93a03f82012-02-17 15:34:57 -080017#ifndef _ANDROIDFW_KEY_LAYOUT_MAP_H
18#define _ANDROIDFW_KEY_LAYOUT_MAP_H
Jeff Brown6b53e8d2010-11-10 16:03:06 -080019
20#include <stdint.h>
21#include <utils/Errors.h>
22#include <utils/KeyedVector.h>
23#include <utils/Tokenizer.h>
Jeff Brown9f25b7f2012-04-10 14:30:49 -070024#include <utils/RefBase.h>
Jeff Brown6b53e8d2010-11-10 16:03:06 -080025
26namespace android {
27
Jeff Brown3a22fa02011-03-04 13:07:49 -080028struct 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
54 AxisInfo() : mode(MODE_NORMAL), axis(-1), highAxis(-1), splitValue(0), flatOverride(-1) {
55 }
56};
57
Jeff Brown6b53e8d2010-11-10 16:03:06 -080058/**
Jeff Brown6f2fba42011-02-19 01:08:02 -080059 * Describes a mapping from keyboard scan codes and joystick axes to Android key codes and axes.
Jeff Brown9f25b7f2012-04-10 14:30:49 -070060 *
61 * This object is immutable after it has been loaded.
Jeff Brown6b53e8d2010-11-10 16:03:06 -080062 */
Jeff Brown9f25b7f2012-04-10 14:30:49 -070063class KeyLayoutMap : public RefBase {
Jeff Brown6b53e8d2010-11-10 16:03:06 -080064public:
Jeff Brown9f25b7f2012-04-10 14:30:49 -070065 static status_t load(const String8& filename, sp<KeyLayoutMap>* outMap);
Jeff Brown6b53e8d2010-11-10 16:03:06 -080066
Jeff Brown49ccac52012-04-11 18:27:33 -070067 status_t mapKey(int32_t scanCode, int32_t usageCode,
68 int32_t* outKeyCode, uint32_t* outFlags) const;
Jeff Brown6f2fba42011-02-19 01:08:02 -080069 status_t findScanCodesForKey(int32_t keyCode, Vector<int32_t>* outScanCodes) const;
70
Jeff Brown3a22fa02011-03-04 13:07:49 -080071 status_t mapAxis(int32_t scanCode, AxisInfo* outAxisInfo) const;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080072
Jeff Brown9f25b7f2012-04-10 14:30:49 -070073protected:
74 virtual ~KeyLayoutMap();
75
Jeff Brown6b53e8d2010-11-10 16:03:06 -080076private:
77 struct Key {
78 int32_t keyCode;
79 uint32_t flags;
80 };
81
Jeff Brown49ccac52012-04-11 18:27:33 -070082 KeyedVector<int32_t, Key> mKeysByScanCode;
83 KeyedVector<int32_t, Key> mKeysByUsageCode;
Jeff Brown3a22fa02011-03-04 13:07:49 -080084 KeyedVector<int32_t, AxisInfo> mAxes;
Jeff Brown6b53e8d2010-11-10 16:03:06 -080085
86 KeyLayoutMap();
87
Jeff Brown49ccac52012-04-11 18:27:33 -070088 const Key* getKey(int32_t scanCode, int32_t usageCode) const;
89
Jeff Brown6b53e8d2010-11-10 16:03:06 -080090 class Parser {
91 KeyLayoutMap* mMap;
92 Tokenizer* mTokenizer;
93
94 public:
95 Parser(KeyLayoutMap* map, Tokenizer* tokenizer);
96 ~Parser();
97 status_t parse();
98
99 private:
100 status_t parseKey();
Jeff Brown6f2fba42011-02-19 01:08:02 -0800101 status_t parseAxis();
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800102 };
103};
104
105} // namespace android
106
Mathias Agopianb93a03f82012-02-17 15:34:57 -0800107#endif // _ANDROIDFW_KEY_LAYOUT_MAP_H