summaryrefslogtreecommitdiffstats
path: root/source/platform/platform_macos.h
blob: 19e685fc3a5459edf6b503ff424797d68290d5df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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);
void macos_clipboard_set(const char *data, u64 size);
u64  macos_clipboard_get(char *out, u64 cap); // bytes written (cap - 1 at most, zero terminated)
}

#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;
}

internal String8 macos_clipboard_get_arena(Arena *arena)
{
	u64  cap = MiB(1);
	char *out = PushArray(arena, char, cap);
	u64  n   = macos_clipboard_get(out, cap);
	return ToString8Size((u8 *)out, n);
}
#define PlatformClipboardSet(window, data, size)                         ((void)(window), macos_clipboard_set(data, size))
#define PlatformClipboardGet(window, arena)                              ((void)(window), macos_clipboard_get_arena(arena))
#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