aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/platform/IOSXKeyResource.cpp
blob: 1866968ec82b9b91059a079c2a2e2335be24e02f (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
/*
 * 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/IOSXKeyResource.h"

#include <Carbon/Carbon.h>

KeyID
IOSXKeyResource::getKeyID(UInt8 c)
{
    if (c == 0) {
        return kKeyNone;
    }
    else if (c >= 32 && c < 127) {
        // ASCII
        return static_cast<KeyID>(c);
    }
    else {
        // handle special keys
        switch (c) {
        case 0x01:
            return kKeyHome;

        case 0x02:
            return kKeyKP_Enter;

        case 0x03:
            return kKeyKP_Enter;

        case 0x04:
            return kKeyEnd;

        case 0x05:
            return kKeyHelp;

        case 0x08:
            return kKeyBackSpace;

        case 0x09:
            return kKeyTab;

        case 0x0b:
            return kKeyPageUp;

        case 0x0c:
            return kKeyPageDown;

        case 0x0d:
            return kKeyReturn;

        case 0x10:
            // OS X maps all the function keys (F1, etc) to this one key.
            // we can't determine the right key here so we have to do it
            // some other way.
            return kKeyNone;

        case 0x1b:
            return kKeyEscape;

        case 0x1c:
            return kKeyLeft;

        case 0x1d:
            return kKeyRight;

        case 0x1e:
            return kKeyUp;

        case 0x1f:
            return kKeyDown;

        case 0x7f:
            return kKeyDelete;

        case 0x06:
        case 0x07:
        case 0x0a:
        case 0x0e:
        case 0x0f:
        case 0x11:
        case 0x12:
        case 0x13:
        case 0x14:
        case 0x15:
        case 0x16:
        case 0x17:
        case 0x18:
        case 0x19:
        case 0x1a:
            // discard other control characters
            return kKeyNone;

        default:
            // not special or unknown
            break;
        }

        // create string with character
        char str[2];
        str[0] = static_cast<char>(c);
        str[1] = 0;

        // get current keyboard script
        TISInputSourceRef isref = TISCopyCurrentKeyboardInputSource();
        CFArrayRef langs = (CFArrayRef) TISGetInputSourceProperty(isref, kTISPropertyInputSourceLanguages);
        CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding(
                                        (CFStringRef)CFArrayGetValueAtIndex(langs, 0));
        // convert to unicode
        CFStringRef cfString =
            CFStringCreateWithCStringNoCopy(
                kCFAllocatorDefault, str, encoding, kCFAllocatorNull);

        // sometimes CFStringCreate...() returns NULL (e.g. Apple Korean
        // encoding with char value 214).  if it did then make no key,
        // otherwise CFStringCreateMutableCopy() will crash.
        if (cfString == NULL) {
            return kKeyNone;
        }

        // convert to precomposed
        CFMutableStringRef mcfString =
            CFStringCreateMutableCopy(kCFAllocatorDefault, 0, cfString);
        CFRelease(cfString);
        CFStringNormalize(mcfString, kCFStringNormalizationFormC);

        // check result
        int unicodeLength = CFStringGetLength(mcfString);
        if (unicodeLength == 0) {
            CFRelease(mcfString);
            return kKeyNone;
        }
        if (unicodeLength > 1) {
            // FIXME -- more than one character, we should handle this
            CFRelease(mcfString);
            return kKeyNone;
        }

        // get unicode character
        UniChar uc = CFStringGetCharacterAtIndex(mcfString, 0);
        CFRelease(mcfString);

        // convert to KeyID
        return static_cast<KeyID>(uc);
    }
}

KeyID
IOSXKeyResource::unicharToKeyID(UniChar c)
{
    switch (c) {
    case 3:
        return kKeyKP_Enter;

    case 8:
        return kKeyBackSpace;

    case 9:
        return kKeyTab;

    case 13:
        return kKeyReturn;

    case 27:
        return kKeyEscape;

    case 127:
        return kKeyDelete;

    default:
        if (c < 32) {
            return kKeyNone;
        }
        return static_cast<KeyID>(c);
    }
}