cocoa_platform.h (7427B)
1 //======================================================================== 2 // GLFW 3.4 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.view) 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 retina; 125 126 // Cached window properties to filter out duplicate events 127 int width, height; 128 int fbWidth, fbHeight; 129 float xscale, yscale; 130 131 // The total sum of the distances the cursor has been warped 132 // since the last cursor motion event was processed 133 // This is kept to counteract Cocoa doing the same internally 134 double cursorWarpDeltaX, cursorWarpDeltaY; 135 136 } _GLFWwindowNS; 137 138 // Cocoa-specific global data 139 // 140 typedef struct _GLFWlibraryNS 141 { 142 CGEventSourceRef eventSource; 143 id delegate; 144 GLFWbool cursorHidden; 145 TISInputSourceRef inputSource; 146 IOHIDManagerRef hidManager; 147 id unicodeData; 148 id helper; 149 id keyUpMonitor; 150 id nibObjects; 151 152 char keynames[GLFW_KEY_LAST + 1][17]; 153 short int keycodes[256]; 154 short int scancodes[GLFW_KEY_LAST + 1]; 155 char* clipboardString; 156 CGPoint cascadePoint; 157 // Where to place the cursor when re-enabled 158 double restoreCursorPosX, restoreCursorPosY; 159 // The window whose disabled cursor mode is active 160 _GLFWwindow* disabledCursorWindow; 161 162 struct { 163 CFBundleRef bundle; 164 PFN_TISCopyCurrentKeyboardLayoutInputSource CopyCurrentKeyboardLayoutInputSource; 165 PFN_TISGetInputSourceProperty GetInputSourceProperty; 166 PFN_LMGetKbdType GetKbdType; 167 CFStringRef kPropertyUnicodeKeyLayoutData; 168 } tis; 169 170 } _GLFWlibraryNS; 171 172 // Cocoa-specific per-monitor data 173 // 174 typedef struct _GLFWmonitorNS 175 { 176 CGDirectDisplayID displayID; 177 CGDisplayModeRef previousMode; 178 uint32_t unitNumber; 179 id screen; 180 double fallbackRefreshRate; 181 182 } _GLFWmonitorNS; 183 184 // Cocoa-specific per-cursor data 185 // 186 typedef struct _GLFWcursorNS 187 { 188 id object; 189 190 } _GLFWcursorNS; 191 192 // Cocoa-specific global timer data 193 // 194 typedef struct _GLFWtimerNS 195 { 196 uint64_t frequency; 197 198 } _GLFWtimerNS; 199 200 201 void _glfwInitTimerNS(void); 202 203 void _glfwPollMonitorsNS(void); 204 void _glfwSetVideoModeNS(_GLFWmonitor* monitor, const GLFWvidmode* desired); 205 void _glfwRestoreVideoModeNS(_GLFWmonitor* monitor); 206 207 float _glfwTransformYNS(float y); 208 209 void* _glfwLoadLocalVulkanLoaderNS(void); 210