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