cocoa_platform.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #include <stdint.h>
  27. #include <dlfcn.h>
  28. #include <Carbon/Carbon.h>
  29. #if defined(__OBJC__)
  30. #import <Cocoa/Cocoa.h>
  31. #import <CoreVideo/CoreVideo.h>
  32. #else
  33. typedef void* id;
  34. typedef void* CVDisplayLinkRef;
  35. #endif
  36. // NOTE: Many Cocoa enum values have been renamed and we need to build across
  37. // SDK versions where one is unavailable or the other deprecated
  38. // We use the newer names in code and these macros to handle compatibility
  39. #if MAC_OS_X_VERSION_MAX_ALLOWED < 101200
  40. #define NSBitmapFormatAlphaNonpremultiplied NSAlphaNonpremultipliedBitmapFormat
  41. #define NSEventMaskAny NSAnyEventMask
  42. #define NSEventMaskKeyUp NSKeyUpMask
  43. #define NSEventModifierFlagCapsLock NSAlphaShiftKeyMask
  44. #define NSEventModifierFlagCommand NSCommandKeyMask
  45. #define NSEventModifierFlagControl NSControlKeyMask
  46. #define NSEventModifierFlagDeviceIndependentFlagsMask NSDeviceIndependentModifierFlagsMask
  47. #define NSEventModifierFlagOption NSAlternateKeyMask
  48. #define NSEventModifierFlagShift NSShiftKeyMask
  49. #define NSEventTypeApplicationDefined NSApplicationDefined
  50. #define NSWindowStyleMaskBorderless NSBorderlessWindowMask
  51. #define NSWindowStyleMaskClosable NSClosableWindowMask
  52. #define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
  53. #define NSWindowStyleMaskResizable NSResizableWindowMask
  54. #define NSWindowStyleMaskTitled NSTitledWindowMask
  55. #endif
  56. #if (MAC_OS_X_VERSION_MAX_ALLOWED < 101400)
  57. #define NSPasteboardTypeFileURL NSFilenamesPboardType
  58. #define NSBitmapFormatAlphaNonpremultiplied NSAlphaNonpremultipliedBitmapFormat
  59. #define NSPasteboardTypeString NSStringPboardType
  60. #define NSOpenGLContextParameterSurfaceOpacity NSOpenGLCPSurfaceOpacity
  61. #endif
  62. #define debug_key(...) if (_glfw.hints.init.debugKeyboard) { fprintf(stderr, __VA_ARGS__); fflush(stderr); }
  63. typedef int (* GLFWcocoatextinputfilterfun)(int,int,unsigned int, unsigned long);
  64. typedef bool (* GLFWapplicationshouldhandlereopenfun)(int);
  65. typedef bool (* GLFWhandleurlopen)(const char*);
  66. typedef void (* GLFWapplicationwillfinishlaunchingfun)(void);
  67. typedef bool (* GLFWcocoatogglefullscreenfun)(GLFWwindow*);
  68. typedef void (* GLFWcocoarenderframefun)(GLFWwindow*);
  69. typedef VkFlags VkMacOSSurfaceCreateFlagsMVK;
  70. typedef VkFlags VkMetalSurfaceCreateFlagsEXT;
  71. typedef struct VkMacOSSurfaceCreateInfoMVK
  72. {
  73. VkStructureType sType;
  74. const void* pNext;
  75. VkMacOSSurfaceCreateFlagsMVK flags;
  76. const void* pView;
  77. } VkMacOSSurfaceCreateInfoMVK;
  78. typedef struct VkMetalSurfaceCreateInfoEXT
  79. {
  80. VkStructureType sType;
  81. const void* pNext;
  82. VkMetalSurfaceCreateFlagsEXT flags;
  83. const void* pLayer;
  84. } VkMetalSurfaceCreateInfoEXT;
  85. typedef VkResult (APIENTRY *PFN_vkCreateMacOSSurfaceMVK)(VkInstance,const VkMacOSSurfaceCreateInfoMVK*,const VkAllocationCallbacks*,VkSurfaceKHR*);
  86. typedef VkResult (APIENTRY *PFN_vkCreateMetalSurfaceEXT)(VkInstance,const VkMetalSurfaceCreateInfoEXT*,const VkAllocationCallbacks*,VkSurfaceKHR*);
  87. #include "posix_thread.h"
  88. #include "cocoa_joystick.h"
  89. #include "nsgl_context.h"
  90. #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
  91. #define _glfw_dlclose(handle) dlclose(handle)
  92. #define _glfw_dlsym(handle, name) dlsym(handle, name)
  93. #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowNS ns
  94. #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryNS ns
  95. #define _GLFW_PLATFORM_LIBRARY_TIMER_STATE _GLFWtimerNS ns
  96. #define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorNS ns
  97. #define _GLFW_PLATFORM_CURSOR_STATE _GLFWcursorNS ns
  98. // HIToolbox.framework pointer typedefs
  99. #define kTISPropertyUnicodeKeyLayoutData _glfw.ns.tis.kPropertyUnicodeKeyLayoutData
  100. typedef TISInputSourceRef (*PFN_TISCopyCurrentKeyboardLayoutInputSource)(void);
  101. #define TISCopyCurrentKeyboardLayoutInputSource _glfw.ns.tis.CopyCurrentKeyboardLayoutInputSource
  102. typedef void* (*PFN_TISGetInputSourceProperty)(TISInputSourceRef,CFStringRef);
  103. #define TISGetInputSourceProperty _glfw.ns.tis.GetInputSourceProperty
  104. typedef UInt8 (*PFN_LMGetKbdType)(void);
  105. #define LMGetKbdType _glfw.ns.tis.GetKbdType
  106. // Cocoa-specific per-window data
  107. //
  108. typedef struct _GLFWwindowNS
  109. {
  110. id object;
  111. id delegate;
  112. id view;
  113. id layer;
  114. bool maximized;
  115. bool retina;
  116. bool in_traditional_fullscreen;
  117. bool in_fullscreen_transition;
  118. bool titlebar_hidden;
  119. unsigned long pre_full_screen_style_mask;
  120. // Cached window properties to filter out duplicate events
  121. int width, height;
  122. int fbWidth, fbHeight;
  123. float xscale, yscale;
  124. int blur_radius;
  125. // The total sum of the distances the cursor has been warped
  126. // since the last cursor motion event was processed
  127. // This is kept to counteract Cocoa doing the same internally
  128. double cursorWarpDeltaX, cursorWarpDeltaY;
  129. // The text input filter callback
  130. GLFWcocoatextinputfilterfun textInputFilterCallback;
  131. // The toggle fullscreen intercept callback
  132. GLFWcocoatogglefullscreenfun toggleFullscreenCallback;
  133. // Dead key state
  134. UInt32 deadKeyState;
  135. // Whether a render frame has been requested for this window
  136. bool renderFrameRequested;
  137. GLFWcocoarenderframefun renderFrameCallback;
  138. // update cursor after switching desktops with Mission Control
  139. bool delayed_cursor_update_requested;
  140. GLFWcocoarenderframefun resizeCallback;
  141. } _GLFWwindowNS;
  142. typedef struct _GLFWDisplayLinkNS
  143. {
  144. CVDisplayLinkRef displayLink;
  145. CGDirectDisplayID displayID;
  146. monotonic_t lastRenderFrameRequestedAt, first_unserviced_render_frame_request_at;
  147. } _GLFWDisplayLinkNS;
  148. // Cocoa-specific global data
  149. //
  150. typedef struct _GLFWlibraryNS
  151. {
  152. CGEventSourceRef eventSource;
  153. id delegate;
  154. bool finishedLaunching;
  155. bool cursorHidden;
  156. TISInputSourceRef inputSource;
  157. IOHIDManagerRef hidManager;
  158. id unicodeData;
  159. id helper;
  160. id keyUpMonitor, keyDownMonitor, flagsChangedMonitor;
  161. id appleSettings;
  162. id nibObjects;
  163. char keyName[64];
  164. char text[512];
  165. CGPoint cascadePoint;
  166. // Where to place the cursor when re-enabled
  167. double restoreCursorPosX, restoreCursorPosY;
  168. // The window whose disabled cursor mode is active
  169. _GLFWwindow* disabledCursorWindow;
  170. struct {
  171. CFBundleRef bundle;
  172. PFN_TISCopyCurrentKeyboardLayoutInputSource CopyCurrentKeyboardLayoutInputSource;
  173. PFN_TISGetInputSourceProperty GetInputSourceProperty;
  174. PFN_LMGetKbdType GetKbdType;
  175. CFStringRef kPropertyUnicodeKeyLayoutData;
  176. } tis;
  177. struct {
  178. _GLFWDisplayLinkNS entries[256];
  179. size_t count;
  180. } displayLinks;
  181. // the callback to handle url open events
  182. GLFWhandleurlopen url_open_callback;
  183. } _GLFWlibraryNS;
  184. // Cocoa-specific per-monitor data
  185. //
  186. typedef struct _GLFWmonitorNS
  187. {
  188. CGDirectDisplayID displayID;
  189. CGDisplayModeRef previousMode;
  190. uint32_t unitNumber;
  191. id screen;
  192. double fallbackRefreshRate;
  193. } _GLFWmonitorNS;
  194. // Cocoa-specific per-cursor data
  195. //
  196. typedef struct _GLFWcursorNS
  197. {
  198. id object;
  199. } _GLFWcursorNS;
  200. // Cocoa-specific global timer data
  201. //
  202. typedef struct _GLFWtimerNS
  203. {
  204. uint64_t frequency;
  205. } _GLFWtimerNS;
  206. void _glfwPollMonitorsNS(void);
  207. void _glfwSetVideoModeNS(_GLFWmonitor* monitor, const GLFWvidmode* desired);
  208. void _glfwRestoreVideoModeNS(_GLFWmonitor* monitor);
  209. float _glfwTransformYNS(float y);
  210. void* _glfwLoadLocalVulkanLoaderNS(void);
  211. void _glfwClearDisplayLinks(void);
  212. void _glfwRestartDisplayLinks(void);
  213. void _glfwDispatchTickCallback(void);
  214. void _glfwDispatchRenderFrame(CGDirectDisplayID);
  215. void _glfwShutdownCVDisplayLink(unsigned long long, void*);
  216. void _glfwCocoaPostEmptyEvent(void);
  217. void _glfw_create_cv_display_link(_GLFWDisplayLinkNS *entry);
  218. _GLFWDisplayLinkNS* _glfw_create_display_link(CGDirectDisplayID);
  219. uint32_t vk_to_unicode_key_with_current_layout(uint16_t keycode);