summaryrefslogtreecommitdiffstats
path: root/libs/ui/KeyLayoutMap.cpp
blob: 15ae54c6f6b5565c012f112681380d4d61a2523e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#define LOG_TAG "KeyLayoutMap"

#include "KeyLayoutMap.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <utils/String8.h>
#include <stdlib.h>
#include <ui/KeycodeLabels.h>
#include <utils/Log.h>

namespace android {

KeyLayoutMap::KeyLayoutMap()
    :m_status(NO_INIT),
     m_keys()
{
}

KeyLayoutMap::~KeyLayoutMap()
{
}

static String8
next_token(char const** p, int *line)
{
    bool begun = false;
    const char* begin = *p;
    const char* end = *p;
    while (true) {
        if (*end == '\n') {
            (*line)++;
        }
        switch (*end)
        {
            case '#':
                if (begun) {
                    *p = end;
                    return String8(begin, end-begin);
                } else {
                    do {
                        begin++;
                        end++;
                    } while (*begin != '\0' && *begin != '\n');
                }
            case '\0':
            case ' ':
            case '\n':
            case '\r':
            case '\t':
                if (begun || (*end == '\0')) {
                    *p = end;
                    return String8(begin, end-begin);
                } else {
                    begin++;
                    end++;
                    break;
                }
            default:
                end++;
                begun = true;
        }
    }
}

static int32_t
token_to_value(const char *literal, const KeycodeLabel *list)
{
    while (list->literal) {
        if (0 == strcmp(literal, list->literal)) {
            return list->value;
        }
        list++;
    }
    return list->value;
}

status_t
KeyLayoutMap::load(const char* filename)
{
    int fd = open(filename, O_RDONLY);
    if (fd < 0) {
        LOGE("error opening file=%s err=%s\n", filename, strerror(errno));
        m_status = errno;
        return errno;
    }

    off_t len = lseek(fd, 0, SEEK_END);
    off_t errlen = lseek(fd, 0, SEEK_SET);
    if (len < 0 || errlen < 0) {
        close(fd);
        LOGE("error seeking file=%s err=%s\n", filename, strerror(errno));
        m_status = errno;
        return errno;
    }

    char* buf = (char*)malloc(len+1);
    if (read(fd, buf, len) != len) {
        LOGE("error reading file=%s err=%s\n", filename, strerror(errno));
        m_status = errno != 0 ? errno : ((int)NOT_ENOUGH_DATA);
        return errno != 0 ? errno : ((int)NOT_ENOUGH_DATA);
    }
    errno = 0;
    buf[len] = '\0';

    int32_t scancode = -1;
    int32_t keycode = -1;
    uint32_t flags = 0;
    uint32_t tmp;
    char* end;
    status_t err = NO_ERROR;
    int line = 1;
    char const* p = buf;
    enum { BEGIN, SCANCODE, KEYCODE, FLAG } state = BEGIN;
    while (true) {
        String8 token = next_token(&p, &line);
        if (*p == '\0') {
            break;
        }
        switch (state)
        {
            case BEGIN:
                if (token == "key") {
                    state = SCANCODE;
                } else {
                    LOGE("%s:%d: expected key, got '%s'\n", filename, line,
                            token.string());
                    err = BAD_VALUE;
                    goto done;
                }
                break;
            case SCANCODE:
                scancode = strtol(token.string(), &end, 0);
                if (*end != '\0') {
                    LOGE("%s:%d: expected scancode (a number), got '%s'\n",
                            filename, line, token.string());
                    goto done;
                }
                //LOGI("%s:%d: got scancode %d\n", filename, line, scancode );
                state = KEYCODE;
                break;
            case KEYCODE:
                keycode = token_to_value(token.string(), KEYCODES);
                //LOGI("%s:%d: got keycode %d for %s\n", filename, line, keycode, token.string() );
                if (keycode == 0) {
                    LOGE("%s:%d: expected keycode, got '%s'\n",
                            filename, line, token.string());
                    goto done;
                }
                state = FLAG;
                break;
            case FLAG:
                if (token == "key") {
                    if (scancode != -1) {
                        //LOGI("got key decl scancode=%d keycode=%d"
                        //       " flags=0x%08x\n", scancode, keycode, flags);
                        Key k = { keycode, flags };
                        m_keys.add(scancode, k);
                        state = SCANCODE;
                        scancode = -1;
                        keycode = -1;
                        flags = 0;
                        break;
                    }
                }
                tmp = token_to_value(token.string(), FLAGS);
                //LOGI("%s:%d: got flags %x for %s\n", filename, line, tmp, token.string() );
                if (tmp == 0) {
                    LOGE("%s:%d: expected flag, got '%s'\n",
                            filename, line, token.string());
                    goto done;
                }
                flags |= tmp;
                break;
        }
    }
    if (state == FLAG && scancode != -1 ) {
        //LOGI("got key decl scancode=%d keycode=%d"
        //       " flags=0x%08x\n", scancode, keycode, flags);
        Key k = { keycode, flags };
        m_keys.add(scancode, k);
    }

done:
    free(buf);
    close(fd);

    m_status = err;
    return err;
}

status_t
KeyLayoutMap::map(int32_t scancode, int32_t *keycode, uint32_t *flags) const
{
    if (m_status != NO_ERROR) {
        return m_status;
    }

    ssize_t index = m_keys.indexOfKey(scancode);
    if (index < 0) {
        //LOGW("couldn't map scancode=%d\n", scancode);
        return NAME_NOT_FOUND;
    }

    const Key& k = m_keys.valueAt(index);

    *keycode = k.keycode;
    *flags = k.flags;

    //LOGD("mapped scancode=%d to keycode=%d flags=0x%08x\n", scancode,
    //        keycode, flags);

    return NO_ERROR;
}

status_t
KeyLayoutMap::findScancodes(int32_t keycode, Vector<int32_t>* outScancodes) const
{
    if (m_status != NO_ERROR) {
        return m_status;
    }
    
    const size_t N = m_keys.size();
    for (size_t i=0; i<N; i++) {
        if (m_keys.valueAt(i).keycode == keycode) {
            outScancodes->add(m_keys.keyAt(i));
        }
    }
    
    return NO_ERROR;
}

};