1 //========================================================================
2 // GLFW 3.5 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 
29 #include <Carbon/Carbon.h>
30 #include <IOKit/hid/IOHIDLib.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 #ifndef GL_SILENCE_DEPRECATION
35 #define GL_SILENCE_DEPRECATION
36 #endif
37 
38 #if defined(__OBJC__)
39 #import <Cocoa/Cocoa.h>
40 #else
41 typedef void* id;
42 #endif
43 
44 // NOTE: Many Cocoa enum values have been renamed and we need to build across
45 //       SDK versions where one is unavailable or deprecated.
46 //       We use the newer names in code and replace them with the older names if
47 //       the base SDK does not provide the newer names.
48 
49 #if MAC_OS_X_VERSION_MAX_ALLOWED < 101400
50  #define NSOpenGLContextParameterSwapInterval NSOpenGLCPSwapInterval
51  #define NSOpenGLContextParameterSurfaceOpacity NSOpenGLCPSurfaceOpacity
52 #endif
53 
54 #if MAC_OS_X_VERSION_MAX_ALLOWED < 101200
55  #define NSBitmapFormatAlphaNonpremultiplied NSAlphaNonpremultipliedBitmapFormat
56  #define NSEventMaskAny NSAnyEventMask
57  #define NSEventMaskKeyUp NSKeyUpMask
58  #define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
59  #define NSEventModifierFlagCommand NSCommandKeyMask
60  #define NSEventModifierFlagControl NSControlKeyMask
61  #define NSEventModifierFlagDeviceIndependentFlagsMask NSDeviceIndependentModifierFlagsMask
62  #define NSEventModifierFlagOption NSAlternateKeyMask
63  #define NSEventModifierFlagShift NSShiftKeyMask
64  #define NSEventTypeApplicationDefined NSApplicationDefined
65  #define NSWindowStyleMaskBorderless NSBorderlessWindowMask
66  #define NSWindowStyleMaskClosable NSClosableWindowMask
67  #define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
68  #define NSWindowStyleMaskResizable NSResizableWindowMask
69  #define NSWindowStyleMaskTitled NSTitledWindowMask
70 #endif
71 
72 // NOTE: Many Cocoa dynamically linked constants have been renamed and we need
73 //       to build across SDK versions where one is unavailable or deprecated.
74 //       We use the newer names in code and replace them with the older names if
75 //       the deployment target is older than the newer names.
76 
77 #if MAC_OS_X_VERSION_MIN_REQUIRED < 101300
78  #define NSPasteboardTypeURL NSURLPboardType
79 #endif
80 
81 typedef VkFlags VkMacOSSurfaceCreateFlagsMVK;
82 typedef VkFlags VkMetalSurfaceCreateFlagsEXT;
83 
84 typedef struct VkMacOSSurfaceCreateInfoMVK
85 {
86     VkStructureType                 sType;
87     const void*                     pNext;
88     VkMacOSSurfaceCreateFlagsMVK    flags;
89     const void*                     pView;
90 } VkMacOSSurfaceCreateInfoMVK;
91 
92 typedef struct VkMetalSurfaceCreateInfoEXT
93 {
94     VkStructureType                 sType;
95     const void*                     pNext;
96     VkMetalSurfaceCreateFlagsEXT    flags;
97     const void*                     pLayer;
98 } VkMetalSurfaceCreateInfoEXT;
99 
100 typedef VkResult (APIENTRY *PFN_vkCreateMacOSSurfaceMVK)(VkInstance,const VkMacOSSurfaceCreateInfoMVK*,const VkAllocationCallbacks*,VkSurfaceKHR*);
101 typedef VkResult (APIENTRY *PFN_vkCreateMetalSurfaceEXT)(VkInstance,const VkMetalSurfaceCreateInfoEXT*,const VkAllocationCallbacks*,VkSurfaceKHR*);
102 
103 #define GLFW_COCOA_WINDOW_STATE         _GLFWwindowNS  ns;
104 #define GLFW_COCOA_LIBRARY_WINDOW_STATE _GLFWlibraryNS ns;
105 #define GLFW_COCOA_MONITOR_STATE        _GLFWmonitorNS ns;
106 #define GLFW_COCOA_CURSOR_STATE         _GLFWcursorNS  ns;
107 
108 #define GLFW_NSGL_CONTEXT_STATE         _GLFWcontextNSGL nsgl;
109 #define GLFW_NSGL_LIBRARY_CONTEXT_STATE _GLFWlibraryNSGL nsgl;
110 
111 // HIToolbox.framework pointer typedefs
112 #define kTISPropertyUnicodeKeyLayoutData _glfw.ns.tis.kPropertyUnicodeKeyLayoutData
113 typedef TISInputSourceRef (*PFN_TISCopyCurrentKeyboardLayoutInputSource)(void);
114 #define TISCopyCurrentKeyboardLayoutInputSource _glfw.ns.tis.CopyCurrentKeyboardLayoutInputSource
115 typedef void* (*PFN_TISGetInputSourceProperty)(TISInputSourceRef,CFStringRef);
116 #define TISGetInputSourceProperty _glfw.ns.tis.GetInputSourceProperty
117 typedef UInt8 (*PFN_LMGetKbdType)(void);
118 #define LMGetKbdType _glfw.ns.tis.GetKbdType
119 
120 
121 // NSGL-specific per-context data
122 //
123 typedef struct _GLFWcontextNSGL
124 {
125     id                pixelFormat;
126     id                object;
127 } _GLFWcontextNSGL;
128 
129 // NSGL-specific global data
130 //
131 typedef struct _GLFWlibraryNSGL
132 {
133     // dlopen handle for OpenGL.framework (for glfwGetProcAddress)
134     CFBundleRef     framework;
135 } _GLFWlibraryNSGL;
136 
137 // Cocoa-specific per-window data
138 //
139 typedef struct _GLFWwindowNS
140 {
141     id              object;
142     id              delegate;
143     id              view;
144     id              layer;
145 
146     GLFWbool        maximized;
147     GLFWbool        occluded;
148     GLFWbool        scaleFramebuffer;
149 
150     // Cached window properties to filter out duplicate events
151     int             width, height;
152     int             fbWidth, fbHeight;
153     float           xscale, yscale;
154 
155     // The total sum of the distances the cursor has been warped
156     // since the last cursor motion event was processed
157     // This is kept to counteract Cocoa doing the same internally
158     double          cursorWarpDeltaX, cursorWarpDeltaY;
159 } _GLFWwindowNS;
160 
161 // Cocoa-specific global data
162 //
163 typedef struct _GLFWlibraryNS
164 {
165     CGEventSourceRef    eventSource;
166     id                  delegate;
167     GLFWbool            cursorHidden;
168     TISInputSourceRef   inputSource;
169     IOHIDManagerRef     hidManager;
170     id                  unicodeData;
171     id                  helper;
172     id                  keyUpMonitor;
173     id                  nibObjects;
174 
175     char                keynames[GLFW_KEY_LAST + 1][17];
176     short int           keycodes[256];
177     short int           scancodes[GLFW_KEY_LAST + 1];
178     char*               clipboardString;
179     CGPoint             cascadePoint;
180     // Where to place the cursor when re-enabled
181     double              restoreCursorPosX, restoreCursorPosY;
182     // The window whose disabled cursor mode is active
183     _GLFWwindow*        disabledCursorWindow;
184 
185     struct {
186         CFBundleRef     bundle;
187         PFN_TISCopyCurrentKeyboardLayoutInputSource CopyCurrentKeyboardLayoutInputSource;
188         PFN_TISGetInputSourceProperty GetInputSourceProperty;
189         PFN_LMGetKbdType GetKbdType;
190         CFStringRef     kPropertyUnicodeKeyLayoutData;
191     } tis;
192 } _GLFWlibraryNS;
193 
194 // Cocoa-specific per-monitor data
195 //
196 typedef struct _GLFWmonitorNS
197 {
198     CGDirectDisplayID   displayID;
199     CGDisplayModeRef    previousMode;
200     uint32_t            unitNumber;
201     id                  screen;
202     double              fallbackRefreshRate;
203 } _GLFWmonitorNS;
204 
205 // Cocoa-specific per-cursor data
206 //
207 typedef struct _GLFWcursorNS
208 {
209     id              object;
210 } _GLFWcursorNS;
211 
212 
213 GLFWbool _glfwConnectCocoa(int platformID, _GLFWplatform* platform);
214 int _glfwInitCocoa(void);
215 void _glfwTerminateCocoa(void);
216 
217 GLFWbool _glfwCreateWindowCocoa(_GLFWwindow* window, const _GLFWwndconfig* wndconfig, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig);
218 void _glfwDestroyWindowCocoa(_GLFWwindow* window);
219 void _glfwSetWindowTitleCocoa(_GLFWwindow* window, const char* title);
220 void _glfwSetWindowIconCocoa(_GLFWwindow* window, int count, const GLFWimage* images);
221 void _glfwGetWindowPosCocoa(_GLFWwindow* window, int* xpos, int* ypos);
222 void _glfwSetWindowPosCocoa(_GLFWwindow* window, int xpos, int ypos);
223 void _glfwGetWindowSizeCocoa(_GLFWwindow* window, int* width, int* height);
224 void _glfwSetWindowSizeCocoa(_GLFWwindow* window, int width, int height);
225 void _glfwSetWindowSizeLimitsCocoa(_GLFWwindow* window, int minwidth, int minheight, int maxwidth, int maxheight);
226 void _glfwSetWindowAspectRatioCocoa(_GLFWwindow* window, int numer, int denom);
227 void _glfwGetFramebufferSizeCocoa(_GLFWwindow* window, int* width, int* height);
228 void _glfwGetWindowFrameSizeCocoa(_GLFWwindow* window, int* left, int* top, int* right, int* bottom);
229 void _glfwGetWindowContentScaleCocoa(_GLFWwindow* window, float* xscale, float* yscale);
230 void _glfwIconifyWindowCocoa(_GLFWwindow* window);
231 void _glfwRestoreWindowCocoa(_GLFWwindow* window);
232 void _glfwMaximizeWindowCocoa(_GLFWwindow* window);
233 void _glfwShowWindowCocoa(_GLFWwindow* window);
234 void _glfwHideWindowCocoa(_GLFWwindow* window);
235 void _glfwRequestWindowAttentionCocoa(_GLFWwindow* window);
236 void _glfwFocusWindowCocoa(_GLFWwindow* window);
237 void _glfwSetWindowMonitorCocoa(_GLFWwindow* window, _GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
238 GLFWbool _glfwWindowFocusedCocoa(_GLFWwindow* window);
239 GLFWbool _glfwWindowIconifiedCocoa(_GLFWwindow* window);
240 GLFWbool _glfwWindowVisibleCocoa(_GLFWwindow* window);
241 GLFWbool _glfwWindowMaximizedCocoa(_GLFWwindow* window);
242 GLFWbool _glfwWindowHoveredCocoa(_GLFWwindow* window);
243 GLFWbool _glfwFramebufferTransparentCocoa(_GLFWwindow* window);
244 void _glfwSetWindowResizableCocoa(_GLFWwindow* window, GLFWbool enabled);
245 void _glfwSetWindowDecoratedCocoa(_GLFWwindow* window, GLFWbool enabled);
246 void _glfwSetWindowFloatingCocoa(_GLFWwindow* window, GLFWbool enabled);
247 float _glfwGetWindowOpacityCocoa(_GLFWwindow* window);
248 void _glfwSetWindowOpacityCocoa(_GLFWwindow* window, float opacity);
249 void _glfwSetWindowMousePassthroughCocoa(_GLFWwindow* window, GLFWbool enabled);
250 
251 void _glfwSetRawMouseMotionCocoa(_GLFWwindow *window, GLFWbool enabled);
252 GLFWbool _glfwRawMouseMotionSupportedCocoa(void);
253 
254 void _glfwPollEventsCocoa(void);
255 void _glfwWaitEventsCocoa(void);
256 void _glfwWaitEventsTimeoutCocoa(double timeout);
257 void _glfwPostEmptyEventCocoa(void);
258 
259 void _glfwGetCursorPosCocoa(_GLFWwindow* window, double* xpos, double* ypos);
260 void _glfwSetCursorPosCocoa(_GLFWwindow* window, double xpos, double ypos);
261 void _glfwSetCursorModeCocoa(_GLFWwindow* window, int mode);
262 const char* _glfwGetScancodeNameCocoa(int scancode);
263 int _glfwGetKeyScancodeCocoa(int key);
264 GLFWbool _glfwCreateCursorCocoa(_GLFWcursor* cursor, const GLFWimage* image, int xhot, int yhot);
265 GLFWbool _glfwCreateStandardCursorCocoa(_GLFWcursor* cursor, int shape);
266 void _glfwDestroyCursorCocoa(_GLFWcursor* cursor);
267 void _glfwSetCursorCocoa(_GLFWwindow* window, _GLFWcursor* cursor);
268 void _glfwSetClipboardStringCocoa(const char* string);
269 const char* _glfwGetClipboardStringCocoa(void);
270 
271 EGLenum _glfwGetEGLPlatformCocoa(EGLint** attribs);
272 EGLNativeDisplayType _glfwGetEGLNativeDisplayCocoa(void);
273 EGLNativeWindowType _glfwGetEGLNativeWindowCocoa(_GLFWwindow* window);
274 
275 void _glfwGetRequiredInstanceExtensionsCocoa(char** extensions);
276 GLFWbool _glfwGetPhysicalDevicePresentationSupportCocoa(VkInstance instance, VkPhysicalDevice device, uint32_t queuefamily);
277 VkResult _glfwCreateWindowSurfaceCocoa(VkInstance instance, _GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface);
278 
279 void _glfwFreeMonitorCocoa(_GLFWmonitor* monitor);
280 void _glfwGetMonitorPosCocoa(_GLFWmonitor* monitor, int* xpos, int* ypos);
281 void _glfwGetMonitorContentScaleCocoa(_GLFWmonitor* monitor, float* xscale, float* yscale);
282 void _glfwGetMonitorWorkareaCocoa(_GLFWmonitor* monitor, int* xpos, int* ypos, int* width, int* height);
283 GLFWvidmode* _glfwGetVideoModesCocoa(_GLFWmonitor* monitor, int* count);
284 GLFWbool _glfwGetVideoModeCocoa(_GLFWmonitor* monitor, GLFWvidmode* mode);
285 GLFWbool _glfwGetGammaRampCocoa(_GLFWmonitor* monitor, GLFWgammaramp* ramp);
286 void _glfwSetGammaRampCocoa(_GLFWmonitor* monitor, const GLFWgammaramp* ramp);
287 
288 void _glfwPollMonitorsCocoa(void);
289 void _glfwSetVideoModeCocoa(_GLFWmonitor* monitor, const GLFWvidmode* desired);
290 void _glfwRestoreVideoModeCocoa(_GLFWmonitor* monitor);
291 
292 float _glfwTransformYCocoa(float y);
293 
294 void* _glfwLoadLocalVulkanLoaderCocoa(void);
295 
296 GLFWbool _glfwInitNSGL(void);
297 void _glfwTerminateNSGL(void);
298 GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window,
299                                 const _GLFWctxconfig* ctxconfig,
300                                 const _GLFWfbconfig* fbconfig);
301 void _glfwDestroyContextNSGL(_GLFWwindow* window);
302 
303