• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//========================================================================
2// GLFW 3.2 OS X - www.glfw.org
3//------------------------------------------------------------------------
4// Copyright (c) 2009-2016 Camilla Berglund <elmindreda@glfw.org>
5//
6// This software is provided 'as-is', without any express or implied
7// warranty. In no event will the authors be held liable for any damages
8// arising from the use of this software.
9//
10// Permission is granted to anyone to use this software for any purpose,
11// including commercial applications, and to alter it and redistribute it
12// freely, subject to the following restrictions:
13//
14// 1. The origin of this software must not be misrepresented; you must not
15//    claim that you wrote the original software. If you use this software
16//    in a product, an acknowledgment in the product documentation would
17//    be appreciated but is not required.
18//
19// 2. Altered source versions must be plainly marked as such, and must not
20//    be misrepresented as being the original software.
21//
22// 3. This notice may not be removed or altered from any source
23//    distribution.
24//
25//========================================================================
26
27#include "internal.h"
28#include <sys/param.h> // For MAXPATHLEN
29
30
31#if defined(_GLFW_USE_CHDIR)
32
33// Change to our application bundle's resources directory, if present
34//
35static void changeToResourcesDirectory(void)
36{
37    char resourcesPath[MAXPATHLEN];
38
39    CFBundleRef bundle = CFBundleGetMainBundle();
40    if (!bundle)
41        return;
42
43    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(bundle);
44
45    CFStringRef last = CFURLCopyLastPathComponent(resourcesURL);
46    if (CFStringCompare(CFSTR("Resources"), last, 0) != kCFCompareEqualTo)
47    {
48        CFRelease(last);
49        CFRelease(resourcesURL);
50        return;
51    }
52
53    CFRelease(last);
54
55    if (!CFURLGetFileSystemRepresentation(resourcesURL,
56                                          true,
57                                          (UInt8*) resourcesPath,
58                                          MAXPATHLEN))
59    {
60        CFRelease(resourcesURL);
61        return;
62    }
63
64    CFRelease(resourcesURL);
65
66    chdir(resourcesPath);
67}
68
69#endif /* _GLFW_USE_CHDIR */
70
71// Create key code translation tables
72//
73static void createKeyTables(void)
74{
75    int scancode;
76
77    memset(_glfw.ns.publicKeys, -1, sizeof(_glfw.ns.publicKeys));
78    memset(_glfw.ns.nativeKeys, -1, sizeof(_glfw.ns.nativeKeys));
79
80    _glfw.ns.publicKeys[0x1D] = GLFW_KEY_0;
81    _glfw.ns.publicKeys[0x12] = GLFW_KEY_1;
82    _glfw.ns.publicKeys[0x13] = GLFW_KEY_2;
83    _glfw.ns.publicKeys[0x14] = GLFW_KEY_3;
84    _glfw.ns.publicKeys[0x15] = GLFW_KEY_4;
85    _glfw.ns.publicKeys[0x17] = GLFW_KEY_5;
86    _glfw.ns.publicKeys[0x16] = GLFW_KEY_6;
87    _glfw.ns.publicKeys[0x1A] = GLFW_KEY_7;
88    _glfw.ns.publicKeys[0x1C] = GLFW_KEY_8;
89    _glfw.ns.publicKeys[0x19] = GLFW_KEY_9;
90    _glfw.ns.publicKeys[0x00] = GLFW_KEY_A;
91    _glfw.ns.publicKeys[0x0B] = GLFW_KEY_B;
92    _glfw.ns.publicKeys[0x08] = GLFW_KEY_C;
93    _glfw.ns.publicKeys[0x02] = GLFW_KEY_D;
94    _glfw.ns.publicKeys[0x0E] = GLFW_KEY_E;
95    _glfw.ns.publicKeys[0x03] = GLFW_KEY_F;
96    _glfw.ns.publicKeys[0x05] = GLFW_KEY_G;
97    _glfw.ns.publicKeys[0x04] = GLFW_KEY_H;
98    _glfw.ns.publicKeys[0x22] = GLFW_KEY_I;
99    _glfw.ns.publicKeys[0x26] = GLFW_KEY_J;
100    _glfw.ns.publicKeys[0x28] = GLFW_KEY_K;
101    _glfw.ns.publicKeys[0x25] = GLFW_KEY_L;
102    _glfw.ns.publicKeys[0x2E] = GLFW_KEY_M;
103    _glfw.ns.publicKeys[0x2D] = GLFW_KEY_N;
104    _glfw.ns.publicKeys[0x1F] = GLFW_KEY_O;
105    _glfw.ns.publicKeys[0x23] = GLFW_KEY_P;
106    _glfw.ns.publicKeys[0x0C] = GLFW_KEY_Q;
107    _glfw.ns.publicKeys[0x0F] = GLFW_KEY_R;
108    _glfw.ns.publicKeys[0x01] = GLFW_KEY_S;
109    _glfw.ns.publicKeys[0x11] = GLFW_KEY_T;
110    _glfw.ns.publicKeys[0x20] = GLFW_KEY_U;
111    _glfw.ns.publicKeys[0x09] = GLFW_KEY_V;
112    _glfw.ns.publicKeys[0x0D] = GLFW_KEY_W;
113    _glfw.ns.publicKeys[0x07] = GLFW_KEY_X;
114    _glfw.ns.publicKeys[0x10] = GLFW_KEY_Y;
115    _glfw.ns.publicKeys[0x06] = GLFW_KEY_Z;
116
117    _glfw.ns.publicKeys[0x27] = GLFW_KEY_APOSTROPHE;
118    _glfw.ns.publicKeys[0x2A] = GLFW_KEY_BACKSLASH;
119    _glfw.ns.publicKeys[0x2B] = GLFW_KEY_COMMA;
120    _glfw.ns.publicKeys[0x18] = GLFW_KEY_EQUAL;
121    _glfw.ns.publicKeys[0x32] = GLFW_KEY_GRAVE_ACCENT;
122    _glfw.ns.publicKeys[0x21] = GLFW_KEY_LEFT_BRACKET;
123    _glfw.ns.publicKeys[0x1B] = GLFW_KEY_MINUS;
124    _glfw.ns.publicKeys[0x2F] = GLFW_KEY_PERIOD;
125    _glfw.ns.publicKeys[0x1E] = GLFW_KEY_RIGHT_BRACKET;
126    _glfw.ns.publicKeys[0x29] = GLFW_KEY_SEMICOLON;
127    _glfw.ns.publicKeys[0x2C] = GLFW_KEY_SLASH;
128    _glfw.ns.publicKeys[0x0A] = GLFW_KEY_WORLD_1;
129
130    _glfw.ns.publicKeys[0x33] = GLFW_KEY_BACKSPACE;
131    _glfw.ns.publicKeys[0x39] = GLFW_KEY_CAPS_LOCK;
132    _glfw.ns.publicKeys[0x75] = GLFW_KEY_DELETE;
133    _glfw.ns.publicKeys[0x7D] = GLFW_KEY_DOWN;
134    _glfw.ns.publicKeys[0x77] = GLFW_KEY_END;
135    _glfw.ns.publicKeys[0x24] = GLFW_KEY_ENTER;
136    _glfw.ns.publicKeys[0x35] = GLFW_KEY_ESCAPE;
137    _glfw.ns.publicKeys[0x7A] = GLFW_KEY_F1;
138    _glfw.ns.publicKeys[0x78] = GLFW_KEY_F2;
139    _glfw.ns.publicKeys[0x63] = GLFW_KEY_F3;
140    _glfw.ns.publicKeys[0x76] = GLFW_KEY_F4;
141    _glfw.ns.publicKeys[0x60] = GLFW_KEY_F5;
142    _glfw.ns.publicKeys[0x61] = GLFW_KEY_F6;
143    _glfw.ns.publicKeys[0x62] = GLFW_KEY_F7;
144    _glfw.ns.publicKeys[0x64] = GLFW_KEY_F8;
145    _glfw.ns.publicKeys[0x65] = GLFW_KEY_F9;
146    _glfw.ns.publicKeys[0x6D] = GLFW_KEY_F10;
147    _glfw.ns.publicKeys[0x67] = GLFW_KEY_F11;
148    _glfw.ns.publicKeys[0x6F] = GLFW_KEY_F12;
149    _glfw.ns.publicKeys[0x69] = GLFW_KEY_F13;
150    _glfw.ns.publicKeys[0x6B] = GLFW_KEY_F14;
151    _glfw.ns.publicKeys[0x71] = GLFW_KEY_F15;
152    _glfw.ns.publicKeys[0x6A] = GLFW_KEY_F16;
153    _glfw.ns.publicKeys[0x40] = GLFW_KEY_F17;
154    _glfw.ns.publicKeys[0x4F] = GLFW_KEY_F18;
155    _glfw.ns.publicKeys[0x50] = GLFW_KEY_F19;
156    _glfw.ns.publicKeys[0x5A] = GLFW_KEY_F20;
157    _glfw.ns.publicKeys[0x73] = GLFW_KEY_HOME;
158    _glfw.ns.publicKeys[0x72] = GLFW_KEY_INSERT;
159    _glfw.ns.publicKeys[0x7B] = GLFW_KEY_LEFT;
160    _glfw.ns.publicKeys[0x3A] = GLFW_KEY_LEFT_ALT;
161    _glfw.ns.publicKeys[0x3B] = GLFW_KEY_LEFT_CONTROL;
162    _glfw.ns.publicKeys[0x38] = GLFW_KEY_LEFT_SHIFT;
163    _glfw.ns.publicKeys[0x37] = GLFW_KEY_LEFT_SUPER;
164    _glfw.ns.publicKeys[0x6E] = GLFW_KEY_MENU;
165    _glfw.ns.publicKeys[0x47] = GLFW_KEY_NUM_LOCK;
166    _glfw.ns.publicKeys[0x79] = GLFW_KEY_PAGE_DOWN;
167    _glfw.ns.publicKeys[0x74] = GLFW_KEY_PAGE_UP;
168    _glfw.ns.publicKeys[0x7C] = GLFW_KEY_RIGHT;
169    _glfw.ns.publicKeys[0x3D] = GLFW_KEY_RIGHT_ALT;
170    _glfw.ns.publicKeys[0x3E] = GLFW_KEY_RIGHT_CONTROL;
171    _glfw.ns.publicKeys[0x3C] = GLFW_KEY_RIGHT_SHIFT;
172    _glfw.ns.publicKeys[0x36] = GLFW_KEY_RIGHT_SUPER;
173    _glfw.ns.publicKeys[0x31] = GLFW_KEY_SPACE;
174    _glfw.ns.publicKeys[0x30] = GLFW_KEY_TAB;
175    _glfw.ns.publicKeys[0x7E] = GLFW_KEY_UP;
176
177    _glfw.ns.publicKeys[0x52] = GLFW_KEY_KP_0;
178    _glfw.ns.publicKeys[0x53] = GLFW_KEY_KP_1;
179    _glfw.ns.publicKeys[0x54] = GLFW_KEY_KP_2;
180    _glfw.ns.publicKeys[0x55] = GLFW_KEY_KP_3;
181    _glfw.ns.publicKeys[0x56] = GLFW_KEY_KP_4;
182    _glfw.ns.publicKeys[0x57] = GLFW_KEY_KP_5;
183    _glfw.ns.publicKeys[0x58] = GLFW_KEY_KP_6;
184    _glfw.ns.publicKeys[0x59] = GLFW_KEY_KP_7;
185    _glfw.ns.publicKeys[0x5B] = GLFW_KEY_KP_8;
186    _glfw.ns.publicKeys[0x5C] = GLFW_KEY_KP_9;
187    _glfw.ns.publicKeys[0x45] = GLFW_KEY_KP_ADD;
188    _glfw.ns.publicKeys[0x41] = GLFW_KEY_KP_DECIMAL;
189    _glfw.ns.publicKeys[0x4B] = GLFW_KEY_KP_DIVIDE;
190    _glfw.ns.publicKeys[0x4C] = GLFW_KEY_KP_ENTER;
191    _glfw.ns.publicKeys[0x51] = GLFW_KEY_KP_EQUAL;
192    _glfw.ns.publicKeys[0x43] = GLFW_KEY_KP_MULTIPLY;
193    _glfw.ns.publicKeys[0x4E] = GLFW_KEY_KP_SUBTRACT;
194
195    for (scancode = 0;  scancode < 256;  scancode++)
196    {
197        // Store the reverse translation for faster key name lookup
198        if (_glfw.ns.publicKeys[scancode] >= 0)
199            _glfw.ns.nativeKeys[_glfw.ns.publicKeys[scancode]] = scancode;
200    }
201}
202
203// Retrieve Unicode data for the current keyboard layout
204//
205static GLFWbool updateUnicodeDataNS(void)
206{
207    if (_glfw.ns.inputSource)
208    {
209        CFRelease(_glfw.ns.inputSource);
210        _glfw.ns.inputSource = NULL;
211        _glfw.ns.unicodeData = nil;
212    }
213
214    _glfw.ns.inputSource = TISCopyCurrentKeyboardLayoutInputSource();
215    if (!_glfw.ns.inputSource)
216    {
217        _glfwInputError(GLFW_PLATFORM_ERROR,
218                        "Cocoa: Failed to retrieve keyboard layout input source");
219        return GLFW_FALSE;
220    }
221
222    _glfw.ns.unicodeData = TISGetInputSourceProperty(_glfw.ns.inputSource,
223                                                     kTISPropertyUnicodeKeyLayoutData);
224    if (!_glfw.ns.unicodeData)
225    {
226        _glfwInputError(GLFW_PLATFORM_ERROR,
227                        "Cocoa: Failed to retrieve keyboard layout Unicode data");
228        return GLFW_FALSE;
229    }
230
231    return GLFW_TRUE;
232}
233
234// Load HIToolbox.framework and the TIS symbols we need from it
235//
236static GLFWbool initializeTIS(void)
237{
238    // This works only because Cocoa has already loaded it properly
239    _glfw.ns.tis.bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.HIToolbox"));
240    if (!_glfw.ns.tis.bundle)
241    {
242        _glfwInputError(GLFW_PLATFORM_ERROR,
243                        "Cocoa: Failed to load HIToolbox.framework");
244        return GLFW_FALSE;
245    }
246
247    CFStringRef* kPropertyUnicodeKeyLayoutData =
248        CFBundleGetDataPointerForName(_glfw.ns.tis.bundle,
249                                      CFSTR("kTISPropertyUnicodeKeyLayoutData"));
250    CFStringRef* kNotifySelectedKeyboardInputSourceChanged =
251        CFBundleGetDataPointerForName(_glfw.ns.tis.bundle,
252                                      CFSTR("kTISNotifySelectedKeyboardInputSourceChanged"));
253    _glfw.ns.tis.CopyCurrentKeyboardLayoutInputSource =
254        CFBundleGetFunctionPointerForName(_glfw.ns.tis.bundle,
255                                          CFSTR("TISCopyCurrentKeyboardLayoutInputSource"));
256    _glfw.ns.tis.GetInputSourceProperty =
257        CFBundleGetFunctionPointerForName(_glfw.ns.tis.bundle,
258                                          CFSTR("TISGetInputSourceProperty"));
259    _glfw.ns.tis.GetKbdType =
260        CFBundleGetFunctionPointerForName(_glfw.ns.tis.bundle,
261                                          CFSTR("LMGetKbdType"));
262
263    if (!kPropertyUnicodeKeyLayoutData ||
264        !kNotifySelectedKeyboardInputSourceChanged ||
265        !TISCopyCurrentKeyboardLayoutInputSource ||
266        !TISGetInputSourceProperty ||
267        !LMGetKbdType)
268    {
269        _glfwInputError(GLFW_PLATFORM_ERROR,
270                        "Cocoa: Failed to load TIS API symbols");
271        return GLFW_FALSE;
272    }
273
274    _glfw.ns.tis.kPropertyUnicodeKeyLayoutData =
275        *kPropertyUnicodeKeyLayoutData;
276    _glfw.ns.tis.kNotifySelectedKeyboardInputSourceChanged =
277        *kNotifySelectedKeyboardInputSourceChanged;
278
279    return updateUnicodeDataNS();
280}
281
282@interface GLFWLayoutListener : NSObject
283@end
284
285@implementation GLFWLayoutListener
286
287- (void)selectedKeyboardInputSourceChanged:(NSObject* )object
288{
289    updateUnicodeDataNS();
290}
291
292@end
293
294
295//////////////////////////////////////////////////////////////////////////
296//////                       GLFW platform API                      //////
297//////////////////////////////////////////////////////////////////////////
298
299int _glfwPlatformInit(void)
300{
301    _glfw.ns.autoreleasePool = [[NSAutoreleasePool alloc] init];
302
303    _glfw.ns.listener = [[GLFWLayoutListener alloc] init];
304    [[NSDistributedNotificationCenter defaultCenter]
305        addObserver:_glfw.ns.listener
306           selector:@selector(selectedKeyboardInputSourceChanged:)
307               name:(__bridge NSString*)kTISNotifySelectedKeyboardInputSourceChanged
308             object:nil];
309
310#if defined(_GLFW_USE_CHDIR)
311    changeToResourcesDirectory();
312#endif
313
314    createKeyTables();
315
316    _glfw.ns.eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
317    if (!_glfw.ns.eventSource)
318        return GLFW_FALSE;
319
320    CGEventSourceSetLocalEventsSuppressionInterval(_glfw.ns.eventSource, 0.0);
321
322    if (!initializeTIS())
323        return GLFW_FALSE;
324
325    if (!_glfwInitThreadLocalStoragePOSIX())
326        return GLFW_FALSE;
327
328    _glfwInitTimerNS();
329    _glfwInitJoysticksNS();
330
331    return GLFW_TRUE;
332}
333
334void _glfwPlatformTerminate(void)
335{
336    if (_glfw.ns.inputSource)
337    {
338        CFRelease(_glfw.ns.inputSource);
339        _glfw.ns.inputSource = NULL;
340        _glfw.ns.unicodeData = nil;
341    }
342
343    if (_glfw.ns.eventSource)
344    {
345        CFRelease(_glfw.ns.eventSource);
346        _glfw.ns.eventSource = NULL;
347    }
348
349    if (_glfw.ns.delegate)
350    {
351        [NSApp setDelegate:nil];
352        [_glfw.ns.delegate release];
353        _glfw.ns.delegate = nil;
354    }
355
356    if (_glfw.ns.listener)
357    {
358        [[NSDistributedNotificationCenter defaultCenter]
359            removeObserver:_glfw.ns.listener
360                      name:(__bridge NSString*)kTISNotifySelectedKeyboardInputSourceChanged
361                    object:nil];
362        [[NSDistributedNotificationCenter defaultCenter]
363            removeObserver:_glfw.ns.listener];
364        [_glfw.ns.listener release];
365        _glfw.ns.listener = nil;
366    }
367
368    [_glfw.ns.cursor release];
369    _glfw.ns.cursor = nil;
370
371    free(_glfw.ns.clipboardString);
372
373    _glfwTerminateNSGL();
374    _glfwTerminateJoysticksNS();
375    _glfwTerminateThreadLocalStoragePOSIX();
376
377    [_glfw.ns.autoreleasePool release];
378    _glfw.ns.autoreleasePool = nil;
379}
380
381const char* _glfwPlatformGetVersionString(void)
382{
383    return _GLFW_VERSION_NUMBER " Cocoa NSGL"
384#if defined(_GLFW_USE_CHDIR)
385        " chdir"
386#endif
387#if defined(_GLFW_USE_MENUBAR)
388        " menubar"
389#endif
390#if defined(_GLFW_USE_RETINA)
391        " retina"
392#endif
393#if defined(_GLFW_BUILD_DLL)
394        " dynamic"
395#endif
396        ;
397}
398
399