summaryrefslogtreecommitdiffstats
path: root/source/platform/platform_win32.h
blob: 8c2b4ea5ea11eac7d3466774a545d7a8af305bb2 (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
#ifndef PLATFORM_WIN32_H
#define PLATFORM_WIN32_H

// NOTE: win32 window + input, same contract as platform_linux_wayland.h. vulkan presents
// through vkCreateWin32SurfaceKHR, which only needs the HINSTANCE and HWND.
//
// differences from wayland worth knowing:
//   - native title bar and borders (wayland asks for server side decorations, same idea)
//   - windows dpi is fractional (125%, 150%), the ui wants an integer buffer scale, so scale is
//     dpi / 96 rounded to nearest, the logical size is physical / scale, and the leftover factor
//     (PlatformWindowUiScale, e.g. 1.5 / 2 = 0.75) is applied by the ui as extra zoom
//   - no frame callbacks: the swapchain uses fifo, vkQueuePresentKHR paces us to vblank
//   - key repeat comes from the os (WM_KEYDOWN with the repeat bit)
//   - --layer becomes a borderless topmost tool window anchored to the work area

typedef struct Win32WindowState Win32WindowState;
struct Win32WindowState {
	HINSTANCE instance;
	HWND      hwnd;
	HANDLE    timer; // high resolution waitable timer for sub millisecond pump timeouts, may be NULL

	PlatformInput input;
	u32           cursor;        // PlatformCursor currently shown
	HCURSOR       cursor_handle;
	b32           tracking_mouse; // TrackMouseEvent armed, we get a WM_MOUSELEAVE
	WCHAR         high_surrogate; // first half of a WM_CHAR pair

	//- size
	u32 width;  // logical
	u32 height;
	u32 physical_width;
	u32 physical_height;
	u32 scale;  // integer buffer scale
	u32 dpi;
	u32 min_width; // logical
	u32 min_height;
	b32 resized;

	b32 is_layer;
	b32 maximized;
	b32 suspended; // minimized: nothing to present to
	b32 should_close;

	//- frame pacing (fifo does the real work, these keep the main loop's bookkeeping happy)
	u64 last_frame_done_ns;
	u64 frame_requested_ns;

	//- live resize: DefWindowProc runs a modal loop while the edge is dragged, we draw from inside it
	PlatformRefreshProc *refresh;
	void                *refresh_user;
	b32                  in_size_move;
};

struct PlatformWindow {
	Win32WindowState win32;
};

internal PlatformWindow *win32_window_open(Arena *arena, WindowParams params);
// NOTE: blocks up to timeout_ns (-1 forever) for window messages or one of extra_fds (win32
// HANDLEs carried in an int, -1 = none) becoming signaled. fills input. false once closing.
internal b32  win32_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask);
internal void win32_window_size(PlatformWindow *window, u32 *width, u32 *height, u32 *scale);
internal b32  win32_window_take_resized(PlatformWindow *window);
internal void win32_window_close(PlatformWindow *window);
internal void win32_window_begin_move(PlatformWindow *window);
internal void win32_window_toggle_maximized(PlatformWindow *window);
internal void win32_window_request_frame(PlatformWindow *window);
internal void win32_window_cancel_frame(PlatformWindow *window);
internal b32  win32_window_frame_ready(PlatformWindow *window);
internal void win32_window_set_cursor(PlatformWindow *window, PlatformCursor cursor);
internal void    win32_clipboard_set(PlatformWindow *window, const char *data, u64 size);
internal String8 win32_clipboard_get(PlatformWindow *window, Arena *arena);
#define PlatformClipboardSet(window, data, size) win32_clipboard_set(window, data, size)
#define PlatformClipboardGet(window, arena)      win32_clipboard_get(window, arena)

#define PlatformWindowOpen(arena, params)                                win32_window_open(arena, params)
#define PlatformWindowPump(window, input, timeout_ns, fds, count, ready) win32_window_pump(window, input, timeout_ns, fds, count, ready)
#define PlatformWindowLastFrameDone(window)                              ((window)->win32.last_frame_done_ns)
#define PlatformWindowFrameRequested(window)                             ((window)->win32.frame_requested_ns)
#define PlatformWindowSize(window, w, h, s)                              win32_window_size(window, w, h, s)
#define PlatformWindowTakeResized(window)                                win32_window_take_resized(window)
#define PlatformWindowClose(window)                                      win32_window_close(window)
#define PlatformWindowBeginMove(window)                                  win32_window_begin_move(window)
#define PlatformWindowToggleMaximized(window)                            win32_window_toggle_maximized(window)
#define PlatformWindowRequestFrame(window)                               win32_window_request_frame(window)
#define PlatformWindowCancelFrame(window)                                win32_window_cancel_frame(window)
#define PlatformWindowFrameReady(window)                                 win32_window_frame_ready(window)
#define PlatformWindowIsLayer(window)                                    ((window)->win32.is_layer)
#define PlatformWindowSetCursor(window, cursor)                          win32_window_set_cursor(window, cursor)
#define PlatformWindowSuspended(window)                                  ((window)->win32.suspended)
#define PlatformWindowSurfaceSource(window)                              (VulkanSurfaceSource{VK_SURFACE_SOURCE_WIN32, (void *)(window)->win32.instance, (void *)(window)->win32.hwnd})
#define PlatformWindowUiScale(window)                                    ((f32)(window)->win32.dpi / (96.0f * (f32)(window)->win32.scale))
#define PlatformWindowSetRefresh(window, proc, user)                     ((window)->win32.refresh = (proc), (window)->win32.refresh_user = (user))

#endif // PLATFORM_WIN32_H