cocoa_platform.h 7.25 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
typedef VkFlags VkMacOSSurfaceCreateFlagsMVK;
64
typedef VkFlags VkMetalSurfaceCreateFlagsEXT;
65
66
67
68
69
70
71
72
73

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

74
75
typedef struct VkMetalSurfaceCreateInfoEXT
{
76
77
78
    VkStructureType                 sType;
    const void*                     pNext;
    VkMetalSurfaceCreateFlagsEXT    flags;
79
    const void*                     pLayer;
80
81
} VkMetalSurfaceCreateInfoEXT;

82
83
typedef VkResult (APIENTRY *PFN_vkCreateMacOSSurfaceMVK)(VkInstance,const VkMacOSSurfaceCreateInfoMVK*,const VkAllocationCallbacks*,VkSurfaceKHR*);
typedef VkResult (APIENTRY *PFN_vkCreateMetalSurfaceEXT)(VkInstance,const VkMetalSurfaceCreateInfoEXT*,const VkAllocationCallbacks*,VkSurfaceKHR*);
84

Camilla Löwy's avatar
Camilla Löwy committed
85
#include "posix_thread.h"
86
#include "cocoa_joystick.h"
87
#include "nsgl_context.h"
Camilla Löwy's avatar
Camilla Löwy committed
88
#include "egl_context.h"
Camilla Löwy's avatar
Camilla Löwy committed
89
#include "osmesa_context.h"
Camilla Berglund's avatar
Camilla Berglund committed
90

Camilla Berglund's avatar
Camilla Berglund committed
91
92
93
94
#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
95
96
97
#define _GLFW_EGL_NATIVE_WINDOW  ((EGLNativeWindowType) window->ns.view)
#define _GLFW_EGL_NATIVE_DISPLAY EGL_DEFAULT_DISPLAY

98
99
#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
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
105
106
107
108
109
110
111
112
// 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

113

Camilla Berglund's avatar
Camilla Berglund committed
114
115
// Cocoa-specific per-window data
//
116
117
typedef struct _GLFWwindowNS
{
Camilla Berglund's avatar
Camilla Berglund committed
118
    id              object;
Camilla Berglund's avatar
Camilla Berglund committed
119
    id              delegate;
Camilla Berglund's avatar
Camilla Berglund committed
120
    id              view;
121
    id              layer;
Camilla Berglund's avatar
Camilla Berglund committed
122

123
    GLFWbool        maximized;
124
    GLFWbool        retina;
125

126
    // Cached window properties to filter out duplicate events
127
128
    int             width, height;
    int             fbWidth, fbHeight;
129
    float           xscale, yscale;
130

131
132
133
    // 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
134
    double          cursorWarpDeltaX, cursorWarpDeltaY;
135

136
} _GLFWwindowNS;
Camilla Berglund's avatar
Camilla Berglund committed
137

Camilla Berglund's avatar
Camilla Berglund committed
138
139
// Cocoa-specific global data
//
140
141
typedef struct _GLFWlibraryNS
{
Camilla Berglund's avatar
Camilla Berglund committed
142
143
    CGEventSourceRef    eventSource;
    id                  delegate;
144
    GLFWbool            finishedLaunching;
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
145
    GLFWbool            cursorHidden;
Camilla Berglund's avatar
Camilla Berglund committed
146
    TISInputSourceRef   inputSource;
147
    IOHIDManagerRef     hidManager;
Camilla Berglund's avatar
Camilla Berglund committed
148
    id                  unicodeData;
149
150
151
    id                  helper;
    id                  keyUpMonitor;
    id                  nibObjects;
Camilla Berglund's avatar
Camilla Berglund committed
152

Camilla Löwy's avatar
Camilla Löwy committed
153
    char                keynames[GLFW_KEY_LAST + 1][17];
Camilla Berglund's avatar
Cleanup    
Camilla Berglund committed
154
155
    short int           keycodes[256];
    short int           scancodes[GLFW_KEY_LAST + 1];
Camilla Berglund's avatar
Camilla Berglund committed
156
    char*               clipboardString;
157
    CGPoint             cascadePoint;
158
159
    // Where to place the cursor when re-enabled
    double              restoreCursorPosX, restoreCursorPosY;
160
161
    // The window whose disabled cursor mode is active
    _GLFWwindow*        disabledCursorWindow;
162

163
164
165
166
167
168
169
170
    struct {
        CFBundleRef     bundle;
        PFN_TISCopyCurrentKeyboardLayoutInputSource CopyCurrentKeyboardLayoutInputSource;
        PFN_TISGetInputSourceProperty GetInputSourceProperty;
        PFN_LMGetKbdType GetKbdType;
        CFStringRef     kPropertyUnicodeKeyLayoutData;
    } tis;

171
} _GLFWlibraryNS;
Camilla Berglund's avatar
Camilla Berglund committed
172

Camilla Berglund's avatar
Camilla Berglund committed
173
174
// Cocoa-specific per-monitor data
//
175
176
typedef struct _GLFWmonitorNS
{
177
178
    CGDirectDisplayID   displayID;
    CGDisplayModeRef    previousMode;
179
    uint32_t            unitNumber;
Camilla Löwy's avatar
Camilla Löwy committed
180
    id                  screen;
181
    double              fallbackRefreshRate;
182
183
184

} _GLFWmonitorNS;

Camilla Berglund's avatar
Camilla Berglund committed
185
186
// Cocoa-specific per-cursor data
//
187
188
typedef struct _GLFWcursorNS
{
Camilla Berglund's avatar
Camilla Berglund committed
189
190
    id              object;

191
192
} _GLFWcursorNS;

Camilla Berglund's avatar
Camilla Berglund committed
193
194
// Cocoa-specific global timer data
//
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
195
typedef struct _GLFWtimerNS
196
{
197
    uint64_t        frequency;
198

Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
199
} _GLFWtimerNS;
200

201

202
void _glfwInitTimerNS(void);
203

204
void _glfwPollMonitorsNS(void);
Camilla Löwy's avatar
Cleanup    
Camilla Löwy committed
205
void _glfwSetVideoModeNS(_GLFWmonitor* monitor, const GLFWvidmode* desired);
206
void _glfwRestoreVideoModeNS(_GLFWmonitor* monitor);
207

208
209
float _glfwTransformYNS(float y);