1 //========================================================================
2 // GLFW 3.3 macOS - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2009-2019 Camilla Löwy <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 <stdint.h>
28 #include <dlfcn.h>
29 
30 #include <Carbon/Carbon.h>
31 
32 // NOTE: All of NSGL was deprecated in the 10.14 SDK
33 //       This disables the pointless warnings for every symbol we use
34 #define GL_SILENCE_DEPRECATION
35 
36 #if defined(__OBJC__)
37 #import <Cocoa/Cocoa.h>
38 #else
39 typedef void* id;
40 #endif
41 
42 // NOTE: Many Cocoa enum values have been renamed and we need to build across
43 //       SDK versions where one is unavailable or the other deprecated
44 //       We use the newer names in code and these macros to handle compatibility
45 #if MAC_OS_X_VERSION_MAX_ALLOWED < 101200
46  #define NSBitmapFormatAlphaNonpremultiplied NSAlphaNonpremultipliedBitmapFormat
47  #define NSEventMaskAny NSAnyEventMask
48  #define NSEventMaskKeyUp NSKeyUpMask
49  #define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
50  #define NSEventModifierFlagCommand NSCommandKeyMask
51  #define NSEventModifierFlagControl NSControlKeyMask
52  #define NSEventModifierFlagDeviceIndependentFlagsMask NSDeviceIndependentModifierFlagsMask
53  #define NSEventModifierFlagOption NSAlternateKeyMask
54  #define NSEventModifierFlagShift NSShiftKeyMask
55  #define NSEventTypeApplicationDefined NSApplicationDefined
56  #define NSWindowStyleMaskBorderless NSBorderlessWindowMask
57  #define NSWindowStyleMaskClosable NSClosableWindowMask
58  #define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
59  #define NSWindowStyleMaskResizable NSResizableWindowMask
60  #define NSWindowStyleMaskTitled NSTitledWindowMask
61 #endif
62 
63 typedef VkFlags VkMacOSSurfaceCreateFlagsMVK;
64 typedef VkFlags VkMetalSurfaceCreateFlagsEXT;
65 
66 typedef struct VkMacOSSurfaceCreateInfoMVK
67 {
68     VkStructureType                 sType;
69     const void*                     pNext;
70     VkMacOSSurfaceCreateFlagsMVK    flags;
71     const void*                     pView;
72 } VkMacOSSurfaceCreateInfoMVK;
73 
74 typedef struct VkMetalSurfaceCreateInfoEXT
75 {
76     VkStructureType                 sType;
77     const void*                     pNext;
78     VkMetalSurfaceCreateFlagsEXT    flags;
79     const void*                     pLayer;
80 } VkMetalSurfaceCreateInfoEXT;
81 
82 typedef VkResult (APIENTRY *PFN_vkCreateMacOSSurfaceMVK)(VkInstance,const VkMacOSSurfaceCreateInfoMVK*,const VkAllocationCallbacks*,VkSurfaceKHR*);
83 typedef VkResult (APIENTRY *PFN_vkCreateMetalSurfaceEXT)(VkInstance,const VkMetalSurfaceCreateInfoEXT*,const VkAllocationCallbacks*,VkSurfaceKHR*);
84 
85 #include "posix_thread.h"
86 #include "cocoa_joystick.h"
87 #include "nsgl_context.h"
88 #include "egl_context.h"
89 #include "osmesa_context.h"
90 
91 #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
92 #define _glfw_dlclose(handle) dlclose(handle)
93 #define _glfw_dlsym(handle, name) dlsym(handle, name)
94 
95 #define _GLFW_EGL_NATIVE_WINDOW  ((EGLNativeWindowType) window->ns.layer)
96 #define _GLFW_EGL_NATIVE_DISPLAY EGL_DEFAULT_DISPLAY
97 
98 #define _GLFW_PLATFORM_WINDOW_STATE         _GLFWwindowNS  ns
99 #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryNS ns
100 #define _GLFW_PLATFORM_LIBRARY_TIMER_STATE  _GLFWtimerNS   ns
101 #define _GLFW_PLATFORM_MONITOR_STATE        _GLFWmonitorNS ns
102 #define _GLFW_PLATFORM_CURSOR_STATE         _GLFWcursorNS  ns
103 
104 // HIToolbox.framework pointer typedefs
105 #define kTISPropertyUnicodeKeyLayoutData _glfw.ns.tis.kPropertyUnicodeKeyLayoutData
106 typedef TISInputSourceRef (*PFN_TISCopyCurrentKeyboardLayoutInputSource)(void);
107 #define TISCopyCurrentKeyboardLayoutInputSource _glfw.ns.tis.CopyCurrentKeyboardLayoutInputSource
108 typedef void* (*PFN_TISGetInputSourceProperty)(TISInputSourceRef,CFStringRef);
109 #define TISGetInputSourceProperty _glfw.ns.tis.GetInputSourceProperty
110 typedef UInt8 (*PFN_LMGetKbdType)(void);
111 #define LMGetKbdType _glfw.ns.tis.GetKbdType
112 
113 
114 // Cocoa-specific per-window data
115 //
116 typedef struct _GLFWwindowNS
117 {
118     id              object;
119     id              delegate;
120     id              view;
121     id              layer;
122 
123     GLFWbool        maximized;
124     GLFWbool        occluded;
125     GLFWbool        retina;
126 
127     // Cached window properties to filter out duplicate events
128     int             width, height;
129     int             fbWidth, fbHeight;
130     float           xscale, yscale;
131 
132     // The total sum of the distances the cursor has been warped
133     // since the last cursor motion event was processed
134     // This is kept to counteract Cocoa doing the same internally
135     double          cursorWarpDeltaX, cursorWarpDeltaY;
136 
137 } _GLFWwindowNS;
138 
139 // Cocoa-specific global data
140 //
141 typedef struct _GLFWlibraryNS
142 {
143     CGEventSourceRef    eventSource;
144     id                  delegate;
145     GLFWbool            finishedLaunching;
146     GLFWbool            cursorHidden;
147     TISInputSourceRef   inputSource;
148     IOHIDManagerRef     hidManager;
149     id                  unicodeData;
150     id                  helper;
151     id                  keyUpMonitor;
152     id                  nibObjects;
153 
154     char                keynames[GLFW_KEY_LAST + 1][17];
155     short int           keycodes[256];
156     short int           scancodes[GLFW_KEY_LAST + 1];
157     char*               clipboardString;
158     CGPoint             cascadePoint;
159     // Where to place the cursor when re-enabled
160     double              restoreCursorPosX, restoreCursorPosY;
161     // The window whose disabled cursor mode is active
162     _GLFWwindow*        disabledCursorWindow;
163 
164     struct {
165         CFBundleRef     bundle;
166         PFN_TISCopyCurrentKeyboardLayoutInputSource CopyCurrentKeyboardLayoutInputSource;
167         PFN_TISGetInputSourceProperty GetInputSourceProperty;
168         PFN_LMGetKbdType GetKbdType;
169         CFStringRef     kPropertyUnicodeKeyLayoutData;
170     } tis;
171 
172 } _GLFWlibraryNS;
173 
174 // Cocoa-specific per-monitor data
175 //
176 typedef struct _GLFWmonitorNS
177 {
178     CGDirectDisplayID   displayID;
179     CGDisplayModeRef    previousMode;
180     uint32_t            unitNumber;
181     id                  screen;
182     double              fallbackRefreshRate;
183 
184 } _GLFWmonitorNS;
185 
186 // Cocoa-specific per-cursor data
187 //
188 typedef struct _GLFWcursorNS
189 {
190     id              object;
191 
192 } _GLFWcursorNS;
193 
194 // Cocoa-specific global timer data
195 //
196 typedef struct _GLFWtimerNS
197 {
198     uint64_t        frequency;
199 
200 } _GLFWtimerNS;
201 
202 
203 void _glfwInitTimerNS(void);
204 
205 void _glfwPollMonitorsNS(void);
206 void _glfwSetVideoModeNS(_GLFWmonitor* monitor, const GLFWvidmode* desired);
207 void _glfwRestoreVideoModeNS(_GLFWmonitor* monitor);
208 
209 float _glfwTransformYNS(float y);
210 
211 void* _glfwLoadLocalVulkanLoaderNS(void);
212 
213