#ifndef PLATFORM_MACOS_H #define PLATFORM_MACOS_H // NOTE: cocoa window + input, same contract as the wayland / win32 layers. the objective-c lives // in platform_macos.mm, its own translation unit, so app.cpp stays plain c++ (an objective-c++ // unity build would collide with Nil, id, YES/NO). this header is the whole interface between // them: plain structs and macos_* functions with c linkage. // // vulkan goes through MoltenVK (linked statically in the release build) onto a CAMetalLayer. // macos specifics: // - logical pixels are cocoa points, the integer scale is the backing scale (2 on retina) // - cmd and ctrl both count as PLATFORM_MOD_CTRL (cmd+z = ctrl+z), option is PLATFORM_MOD_ALT // but still types text (it's how many layouts type @, [, ~ ...) // - files dropped on the window, on the dock icon, or opened from finder ("open with", double // click with the .app) all arrive as PlatformInput.drop_paths // - live resize: cocoa's resize loop calls the refresh callback from windowDidResize typedef struct MacWindowState MacWindowState; struct MacWindowState { PlatformInput input; u32 cursor; u32 width; // logical (points) u32 height; u32 scale; // backing scale factor b32 resized; b32 is_layer; b32 maximized; b32 suspended; // minimized or fully occluded b32 should_close; u64 last_frame_done_ns; u64 frame_requested_ns; PlatformRefreshProc *refresh; void *refresh_user; void *metal_layer; // CAMetalLayer, for vk_init }; struct PlatformWindow { MacWindowState mac; }; extern "C" { b32 macos_window_open(PlatformWindow *window, WindowParams params); b32 macos_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask); void macos_window_close(PlatformWindow *window); void macos_window_begin_move(PlatformWindow *window); void macos_window_toggle_maximized(PlatformWindow *window); void macos_window_set_cursor(PlatformWindow *window, u32 cursor); void macos_show_error(const char *title, const char *message); } #ifndef PLATFORM_MACOS_IMPLEMENTATION internal PlatformWindow *macos_window_open_arena(Arena *arena, WindowParams params) { PlatformWindow *window = PushStructZero(arena, PlatformWindow); return macos_window_open(window, params) ? window : NULL; } internal void platform_show_error(const char *title, const char *message) { macos_show_error(title, message); } internal void macos_window_size(PlatformWindow *window, u32 *width, u32 *height, u32 *scale) { *width = window->mac.width; *height = window->mac.height; *scale = window->mac.scale; } internal b32 macos_window_take_resized(PlatformWindow *window) { b32 resized = window->mac.resized; window->mac.resized = false; return resized; } internal void macos_window_request_frame(PlatformWindow *window) { u64 now = time_now_ns(); window->mac.frame_requested_ns = now; window->mac.last_frame_done_ns = now; } #define PlatformWindowOpen(arena, params) macos_window_open_arena(arena, params) #define PlatformWindowPump(window, input, timeout_ns, fds, count, ready) macos_window_pump(window, input, timeout_ns, fds, count, ready) #define PlatformWindowLastFrameDone(window) ((window)->mac.last_frame_done_ns) #define PlatformWindowFrameRequested(window) ((window)->mac.frame_requested_ns) #define PlatformWindowSize(window, w, h, s) macos_window_size(window, w, h, s) #define PlatformWindowTakeResized(window) macos_window_take_resized(window) #define PlatformWindowClose(window) macos_window_close(window) #define PlatformWindowBeginMove(window) macos_window_begin_move(window) #define PlatformWindowToggleMaximized(window) macos_window_toggle_maximized(window) #define PlatformWindowRequestFrame(window) macos_window_request_frame(window) #define PlatformWindowCancelFrame(window) ((void)(window)) #define PlatformWindowFrameReady(window) ((void)(window), true) #define PlatformWindowIsLayer(window) ((window)->mac.is_layer) #define PlatformWindowSetCursor(window, cursor) macos_window_set_cursor(window, (u32)(cursor)) #define PlatformWindowSuspended(window) ((window)->mac.suspended) #define PlatformWindowUiScale(window) ((void)(window), 1.0f) #define PlatformWindowSurfaceSource(window) (VulkanSurfaceSource{VK_SURFACE_SOURCE_METAL, (window)->mac.metal_layer, NULL}) #define PlatformWindowSetRefresh(window, proc, user) ((window)->mac.refresh = (proc), (window)->mac.refresh_user = (user)) #endif #endif // PLATFORM_MACOS_H