aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/platform/OSXUchrKeyResource.cpp
blob: e0230e95fdf30e8c17c05a3b57577e4906b144dc (plain) (blame)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * barrier -- mouse and keyboard sharing utility
 * Copyright (C) 2016 Symless Ltd.
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file LICENSE that should have accompanied this file.
 * 
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "platform/OSXUchrKeyResource.h"

#include <Carbon/Carbon.h>

//
// OSXUchrKeyResource
//

OSXUchrKeyResource::OSXUchrKeyResource(const void* resource,
                UInt32 keyboardType) :
    m_m(NULL),
    m_cti(NULL),
    m_sdi(NULL),
    m_sri(NULL),
    m_st(NULL)
{
    m_resource = static_cast<const UCKeyboardLayout*>(resource);
    if (m_resource == NULL) {
        return;
    }

    // find the keyboard info for the current keyboard type
    const UCKeyboardTypeHeader* th = NULL;
    const UCKeyboardLayout* r = m_resource;
    for (ItemCount i = 0; i < r->keyboardTypeCount; ++i) {
        if (keyboardType >= r->keyboardTypeList[i].keyboardTypeFirst &&
            keyboardType <= r->keyboardTypeList[i].keyboardTypeLast) {
            th = r->keyboardTypeList + i;
            break;
        }
        if (r->keyboardTypeList[i].keyboardTypeFirst == 0) {
            // found the default.  use it unless we find a match.
            th = r->keyboardTypeList + i;
        }
    }
    if (th == NULL) {
        // cannot find a suitable keyboard type
        return;
    }

    // get tables for keyboard type
    const UInt8* const base = reinterpret_cast<const UInt8*>(m_resource);
    m_m   = reinterpret_cast<const UCKeyModifiersToTableNum*>(base +
                                th->keyModifiersToTableNumOffset);
    m_cti = reinterpret_cast<const UCKeyToCharTableIndex*>(base +
                                th->keyToCharTableIndexOffset);
    m_sdi = reinterpret_cast<const UCKeySequenceDataIndex*>(base +
                                th->keySequenceDataIndexOffset);
    if (th->keyStateRecordsIndexOffset != 0) {
        m_sri = reinterpret_cast<const UCKeyStateRecordsIndex*>(base +
                                th->keyStateRecordsIndexOffset);
    }
    if (th->keyStateTerminatorsOffset != 0) {
        m_st = reinterpret_cast<const UCKeyStateTerminators*>(base +
                                th->keyStateTerminatorsOffset);
    }

    // find the space key, but only if it can combine with dead keys.
    // a dead key followed by a space yields the non-dead version of
    // the dead key.
    m_spaceOutput = 0xffffu;
    UInt32 table  = getTableForModifier(0);
    for (UInt32 button = 0, n = getNumButtons(); button < n; ++button) {
        KeyID id = getKey(table, button);
        if (id == 0x20) {
            UCKeyOutput c =
                reinterpret_cast<const UCKeyOutput*>(base +
                                m_cti->keyToCharTableOffsets[table])[button];
            if ((c & kUCKeyOutputTestForIndexMask) ==
                                kUCKeyOutputStateIndexMask) {
                m_spaceOutput = (c & kUCKeyOutputGetIndexMask);
                break;
            }
        }
    }
}

bool
OSXUchrKeyResource::isValid() const
{
    return (m_m != NULL);
}

UInt32
OSXUchrKeyResource::getNumModifierCombinations() const
{
    // only 32 (not 256) because the righthanded modifier bits are ignored
    return 32;
}

UInt32
OSXUchrKeyResource::getNumTables() const
{
    return m_cti->keyToCharTableCount;
}

UInt32
OSXUchrKeyResource::getNumButtons() const
{
    return m_cti->keyToCharTableSize;
}

UInt32
OSXUchrKeyResource::getTableForModifier(UInt32 mask) const
{
    if (mask >= m_m->modifiersCount) {
        return m_m->defaultTableNum;
    }
    else {
        return m_m->tableNum[mask];
    }
}

KeyID
OSXUchrKeyResource::getKey(UInt32 table, UInt32 button) const
{
    assert(table < getNumTables());
    assert(button < getNumButtons());

    const UInt8* const base   = reinterpret_cast<const UInt8*>(m_resource);
    const UCKeyOutput* cPtr = reinterpret_cast<const UCKeyOutput*>(base +
                                m_cti->keyToCharTableOffsets[table]);

  const UCKeyOutput c = cPtr[button];

    KeySequence keys;
    switch (c & kUCKeyOutputTestForIndexMask) {
    case kUCKeyOutputStateIndexMask:
        if (!getDeadKey(keys, c & kUCKeyOutputGetIndexMask)) {
            return kKeyNone;
        }
        break;

    case kUCKeyOutputSequenceIndexMask:
    default:
        if (!addSequence(keys, c)) {
            return kKeyNone;
        }
        break;
    }

    // XXX -- no support for multiple characters
    if (keys.size() != 1) {
        return kKeyNone;
    }

    return keys.front();
}

bool
OSXUchrKeyResource::getDeadKey(
    KeySequence& keys, UInt16 index) const
{
    if (m_sri == NULL || index >= m_sri->keyStateRecordCount) {
        // XXX -- should we be using some other fallback?
        return false;
    }

    UInt16 state = 0;
    if (!getKeyRecord(keys, index, state)) {
        return false;
    }
    if (state == 0) {
        // not a dead key
        return true;
    }

    // no dead keys if we couldn't find the space key
    if (m_spaceOutput == 0xffffu) {
        return false;
    }

    // the dead key should not have put anything in the key list
    if (!keys.empty()) {
        return false;
    }

    // get the character generated by pressing the space key after the
    // dead key.  if we're still in a compose state afterwards then we're
    // confused so we bail.
    if (!getKeyRecord(keys, m_spaceOutput, state) || state != 0) {
        return false;
    }

    // convert keys to their dead counterparts
    for (KeySequence::iterator i = keys.begin(); i != keys.end(); ++i) {
        *i = barrier::KeyMap::getDeadKey(*i);
    }

    return true;
}

bool
OSXUchrKeyResource::getKeyRecord(
    KeySequence& keys, UInt16 index, UInt16& state) const
{
    const UInt8* const base = reinterpret_cast<const UInt8*>(m_resource);
    const UCKeyStateRecord* sr =
        reinterpret_cast<const UCKeyStateRecord*>(base +
                                m_sri->keyStateRecordOffsets[index]);
    const UCKeyStateEntryTerminal* kset =
        reinterpret_cast<const UCKeyStateEntryTerminal*>(sr->stateEntryData);

    UInt16 nextState = 0;
    bool found       = false;
    if (state == 0) {
        found     = true;
        nextState = sr->stateZeroNextState;
        if (!addSequence(keys, sr->stateZeroCharData)) {
            return false;
        }
    }
    else {
        // we have a next entry
        switch (sr->stateEntryFormat) {
        case kUCKeyStateEntryTerminalFormat:
            for (UInt16 j = 0; j < sr->stateEntryCount; ++j) {
                if (kset[j].curState == state) {
                    if (!addSequence(keys, kset[j].charData)) {
                        return false;
                    }
                    nextState = 0;
                    found     = true;
                    break;
                }
            }
            break;

        case kUCKeyStateEntryRangeFormat:
            // XXX -- not supported yet
            break;

        default:
            // XXX -- unknown format
            return false;
        }
    }
    if (!found) {
        // use a terminator
        if (m_st != NULL && state < m_st->keyStateTerminatorCount) {
            if (!addSequence(keys, m_st->keyStateTerminators[state - 1])) {
                return false;
            }
        }
        nextState = sr->stateZeroNextState;
        if (!addSequence(keys, sr->stateZeroCharData)) {
            return false;
        }
    }

    // next
    state = nextState;

    return true;
}

bool
OSXUchrKeyResource::addSequence(
    KeySequence& keys, UCKeyCharSeq c) const
{
    if ((c & kUCKeyOutputTestForIndexMask) == kUCKeyOutputSequenceIndexMask) {
        UInt16 index = (c & kUCKeyOutputGetIndexMask);
        if (index < m_sdi->charSequenceCount &&
            m_sdi->charSequenceOffsets[index] !=
                m_sdi->charSequenceOffsets[index + 1]) {
            // XXX -- sequences not supported yet
            return false;
        }
    }

    if (c != 0xfffe && c != 0xffff) {
        KeyID id = unicharToKeyID(c);
        if (id != kKeyNone) {
            keys.push_back(id);
        }
    }

    return true;
}