cocoa_platform.h 8.9 KB

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