cocoa_platform.h 7.51 KB
Newer Older
Camilla Berglund's avatar
Camilla Berglund committed
1
//========================================================================
2
// GLFW 3.3 macOS - www.glfw.org
Camilla Berglund's avatar
Camilla Berglund committed
3
//------------------------------------------------------------------------
Camilla Löwy's avatar
Camilla Löwy committed
4
// Copyright (c) 2009-2019 Camilla Löwy <elmindreda@glfw.org>
Camilla Berglund's avatar
Camilla Berglund committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would
//    be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
//    be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
//    distribution.
//
//========================================================================

27
#include <stdint.h>
Camilla Berglund's avatar
Camilla Berglund committed
28
#include <dlfcn.h>
29

Camilla Löwy's avatar
Camilla Löwy committed
30
#include <Carbon/Carbon.h>
31

32
33
34
35
// NOTE: All of NSGL was deprecated in the 10.14 SDK
//       This disables the pointless warnings for every symbol we use
#define GL_SILENCE_DEPRECATION

Camilla Berglund's avatar
Camilla Berglund committed
36
37
38
#if defined(__OBJC__)
#import <Cocoa/Cocoa.h>
#else
39
typedef void* id;
Camilla Berglund's avatar
Camilla Berglund committed
40
41
#endif

42
43
44
// NOTE: Many Cocoa enum values have been renamed and we need to build across
//       SDK versions where one is unavailable or the other deprecated
//       We use the newer names in code and these macros to handle compatibility
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101200
 #define NSBitmapFormatAlphaNonpremultiplied NSAlphaNonpremultipliedBitmapFormat
 #define NSEventMaskAny NSAnyEventMask
 #define NSEventMaskKeyUp NSKeyUpMask
 #define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
 #define NSEventModifierFlagCommand NSCommandKeyMask
 #define NSEventModifierFlagControl NSControlKeyMask
 #define NSEventModifierFlagDeviceIndependentFlagsMask NSDeviceIndependentModifierFlagsMask
 #define NSEventModifierFlagOption NSAlternateKeyMask
 #define NSEventModifierFlagShift NSShiftKeyMask
 #define NSEventTypeApplicationDefined NSApplicationDefined
 #define NSWindowStyleMaskBorderless NSBorderlessWindowMask
 #define NSWindowStyleMaskClosable NSClosableWindowMask
 #define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
 #define NSWindowStyleMaskResizable NSResizableWindowMask
 #define NSWindowStyleMaskTitled NSTitledWindowMask
#endif

63
#if defined(VK_USE_PLATFORM_MACOS_MVK)
64
65
66
67
68
69
70
71
72
73
74
75
typedef VkFlags VkMacOSSurfaceCreateFlagsMVK;

typedef struct VkMacOSSurfaceCreateInfoMVK
{
    VkStructureType                 sType;
    const void*                     pNext;
    VkMacOSSurfaceCreateFlagsMVK    flags;
    const void*                     pView;
} VkMacOSSurfaceCreateInfoMVK;

typedef VkResult (APIENTRY *PFN_vkCreateMacOSSurfaceMVK)(VkInstance,const VkMacOSSurfaceCreateInfoMVK*,const VkAllocationCallbacks*,VkSurfaceKHR*);

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#elif defined(VK_USE_PLATFORM_METAL_EXT)
#define VK_EXT_metal_surface 1
typedef void CAMetalLayer;

#define VK_EXT_METAL_SURFACE_SPEC_VERSION 1
#define VK_EXT_METAL_SURFACE_EXTENSION_NAME "VK_EXT_metal_surface"
typedef VkFlags VkMetalSurfaceCreateFlagsEXT;
typedef struct VkMetalSurfaceCreateInfoEXT {
    VkStructureType                 sType;
    const void*                     pNext;
    VkMetalSurfaceCreateFlagsEXT    flags;
    const CAMetalLayer*             pLayer;
} VkMetalSurfaceCreateInfoEXT;

typedef VkResult (APIENTRY *PFN_vkCreateMetalSurfaceEXT)(VkInstance, const VkMetalSurfaceCreateInfoEXT*, const VkAllocationCallbacks*, VkSurfaceKHR*);
#endif

Camilla Löwy's avatar
Camilla Löwy committed
93
#include "posix_thread.h"
94
#include "cocoa_joystick.h"
95
#include "nsgl_context.h"
Camilla Löwy's avatar
Camilla Löwy committed
96
#include "egl_context.h"
Camilla Löwy's avatar
Camilla Löwy committed
97
#include "osmesa_context.h"
Camilla Berglund's avatar
Camilla Berglund committed
98

Camilla Berglund's avatar
Camilla Berglund committed
99
100
101
102
#define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
#define _glfw_dlclose(handle) dlclose(handle)
#define _glfw_dlsym(handle, name) dlsym(handle, name)

Camilla Löwy's avatar
Camilla Löwy committed
103
104
105
#define _GLFW_EGL_NATIVE_WINDOW  ((EGLNativeWindowType) window->ns.view)
#define _GLFW_EGL_NATIVE_DISPLAY EGL_DEFAULT_DISPLAY

106
107
#define _GLFW_PLATFORM_WINDOW_STATE         _GLFWwindowNS  ns
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryNS ns
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
108
#define _GLFW_PLATFORM_LIBRARY_TIMER_STATE  _GLFWtimerNS   ns
109
#define _GLFW_PLATFORM_MONITOR_STATE        _GLFWmonitorNS ns
110
#define _GLFW_PLATFORM_CURSOR_STATE         _GLFWcursorNS  ns
111

112
113
114
115
116
117
118
119
120
// HIToolbox.framework pointer typedefs
#define kTISPropertyUnicodeKeyLayoutData _glfw.ns.tis.kPropertyUnicodeKeyLayoutData
typedef TISInputSourceRef (*PFN_TISCopyCurrentKeyboardLayoutInputSource)(void);
#define TISCopyCurrentKeyboardLayoutInputSource _glfw.ns.tis.CopyCurrentKeyboardLayoutInputSource
typedef void* (*PFN_TISGetInputSourceProperty)(TISInputSourceRef,CFStringRef);
#define TISGetInputSourceProperty _glfw.ns.tis.GetInputSourceProperty
typedef UInt8 (*PFN_LMGetKbdType)(void);
#define LMGetKbdType _glfw.ns.tis.GetKbdType

121

Camilla Berglund's avatar
Camilla Berglund committed
122
123
// Cocoa-specific per-window data
//
124
125
typedef struct _GLFWwindowNS
{
Camilla Berglund's avatar
Camilla Berglund committed
126
    id              object;
Camilla Berglund's avatar
Camilla Berglund committed
127
    id              delegate;
Camilla Berglund's avatar
Camilla Berglund committed
128
    id              view;
129
    id              layer;
Camilla Berglund's avatar
Camilla Berglund committed
130

131
    GLFWbool        maximized;
132
    GLFWbool        retina;
133

134
    // Cached window properties to filter out duplicate events
135
136
    int             width, height;
    int             fbWidth, fbHeight;
137
    float           xscale, yscale;
138

139
140
141
    // The total sum of the distances the cursor has been warped
    // since the last cursor motion event was processed
    // This is kept to counteract Cocoa doing the same internally
142
    double          cursorWarpDeltaX, cursorWarpDeltaY;
143

144
} _GLFWwindowNS;
Camilla Berglund's avatar
Camilla Berglund committed
145

Camilla Berglund's avatar
Camilla Berglund committed
146
147
// Cocoa-specific global data
//
148
149
typedef struct _GLFWlibraryNS
{
Camilla Berglund's avatar
Camilla Berglund committed
150
151
    CGEventSourceRef    eventSource;
    id                  delegate;
152
    GLFWbool            finishedLaunching;
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
153
    GLFWbool            cursorHidden;
Camilla Berglund's avatar
Camilla Berglund committed
154
    TISInputSourceRef   inputSource;
155
    IOHIDManagerRef     hidManager;
Camilla Berglund's avatar
Camilla Berglund committed
156
    id                  unicodeData;
157
158
159
    id                  helper;
    id                  keyUpMonitor;
    id                  nibObjects;
Camilla Berglund's avatar
Camilla Berglund committed
160

Camilla Löwy's avatar
Camilla Löwy committed
161
    char                keynames[GLFW_KEY_LAST + 1][17];
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
162
163
    short int           keycodes[256];
    short int           scancodes[GLFW_KEY_LAST + 1];
Camilla Berglund's avatar
Camilla Berglund committed
164
    char*               clipboardString;
165
    CGPoint             cascadePoint;
166
167
    // Where to place the cursor when re-enabled
    double              restoreCursorPosX, restoreCursorPosY;
168
169
    // The window whose disabled cursor mode is active
    _GLFWwindow*        disabledCursorWindow;
170

171
172
173
174
175
176
177
178
    struct {
        CFBundleRef     bundle;
        PFN_TISCopyCurrentKeyboardLayoutInputSource CopyCurrentKeyboardLayoutInputSource;
        PFN_TISGetInputSourceProperty GetInputSourceProperty;
        PFN_LMGetKbdType GetKbdType;
        CFStringRef     kPropertyUnicodeKeyLayoutData;
    } tis;

179
} _GLFWlibraryNS;
Camilla Berglund's avatar
Camilla Berglund committed
180

Camilla Berglund's avatar
Camilla Berglund committed
181
182
// Cocoa-specific per-monitor data
//
183
184
typedef struct _GLFWmonitorNS
{
185
186
    CGDirectDisplayID   displayID;
    CGDisplayModeRef    previousMode;
187
    uint32_t            unitNumber;
Camilla Löwy's avatar
Camilla Löwy committed
188
    id                  screen;
189
    double              fallbackRefreshRate;
190
191
192

} _GLFWmonitorNS;

Camilla Berglund's avatar
Camilla Berglund committed
193
194
// Cocoa-specific per-cursor data
//
195
196
typedef struct _GLFWcursorNS
{
Camilla Berglund's avatar
Camilla Berglund committed
197
198
    id              object;

199
200
} _GLFWcursorNS;

Camilla Berglund's avatar
Camilla Berglund committed
201
202
// Cocoa-specific global timer data
//
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
203
typedef struct _GLFWtimerNS
204
{
205
    uint64_t        frequency;
206

Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
207
} _GLFWtimerNS;
208

209

210
void _glfwInitTimerNS(void);
211

212
void _glfwPollMonitorsNS(void);
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
213
void _glfwSetVideoModeNS(_GLFWmonitor* monitor, const GLFWvidmode* desired);
214
void _glfwRestoreVideoModeNS(_GLFWmonitor* monitor);
215

216
217
float _glfwTransformYNS(float y);