summaryrefslogtreecommitdiffstats
path: root/source/platform/platform.h
blob: d0e1bb549c6c18412f60cec4d6da052dcbf46d6f (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
#ifndef PLATFORM_H
#define PLATFORM_H

#include "render_surface.h"

//- window

typedef enum PlatformAnchor {
	PLATFORM_ANCHOR_TOP    = 1,
	PLATFORM_ANCHOR_BOTTOM = 2,
	PLATFORM_ANCHOR_LEFT   = 4,
	PLATFORM_ANCHOR_RIGHT  = 8,
} PlatformAnchor;

typedef struct WindowParams WindowParams;
struct WindowParams {
	cstring name;     // app id (matches the .desktop file) and layer namespace
	cstring title;
	u32     min_width;
	u32     min_height;
	u32     width;    // logical pixels
	u32     height;
	b32     layer;    // wlr layer shell surface instead of an xdg toplevel
	u32     anchor;   // PlatformAnchor flags, 0 = centered
	s32     margin;
};

typedef struct PlatformWindow PlatformWindow;

//- input
// NOTE: everything is in logical pixels, the renderer multiplies by the buffer scale

typedef struct PlatformButton PlatformButton;
struct PlatformButton {
	s32 half_transition_count;
	b32 ended_down;
};

typedef enum PlatformModifier {
	PLATFORM_MOD_SHIFT = 1,
	PLATFORM_MOD_CTRL  = 2,
	PLATFORM_MOD_ALT   = 4,
} PlatformModifier;

typedef struct PlatformKeyEvent PlatformKeyEvent;
struct PlatformKeyEvent {
	u32 keysym; // xkb keysym, layout aware
	u32 mods;
	b32 repeat;
};

#define PLATFORM_MAX_KEY_EVENTS 64
#define PLATFORM_MAX_DROPS      8
#define PLATFORM_DROP_PATH_SIZE 1024

typedef struct PlatformInput PlatformInput;
struct PlatformInput {
	PlatformButton mouse[3]; // left, right, middle
	f32 mouse_x;
	f32 mouse_y;
	b32 mouse_in_window;
	f32 scroll_x; // horizontal wheel / touchpad scroll
	f32 scroll_y; // wheel notches this frame, positive = down

	PlatformKeyEvent keys[PLATFORM_MAX_KEY_EVENTS];
	u32 key_count;
	u8  text[128]; // utf8 typed this frame
	u32 text_size;
	u32 mods;
	b32 focused;
	b32 changed; // any input or window event since the last pump, i.e. worth a redraw

	// NOTE: files dropped onto the window this frame (wayland: text/uri-list, win32: WM_DROPFILES),
	// utf-8 absolute paths. drop_x / drop_y is where they landed, logical pixels.
	char drop_paths[PLATFORM_MAX_DROPS][PLATFORM_DROP_PATH_SIZE];
	u32  drop_count;
	f32  drop_x;
	f32  drop_y;
};

typedef enum PlatformCursor {
	PLATFORM_CURSOR_DEFAULT,
	PLATFORM_CURSOR_POINTER,
	PLATFORM_CURSOR_GRABBING,
	PLATFORM_CURSOR_RESIZE_X,
	PLATFORM_CURSOR_RESIZE_Y,
	PLATFORM_CURSOR_CROSSHAIR,
	PLATFORM_CURSOR_MOVE,
	PLATFORM_CURSOR_RESIZE_NWSE,
} PlatformCursor;

// NOTE: live resize. win32 and cocoa run a modal loop while the user drags the window edge, our
// main loop is blocked inside the pump for the whole drag. the platform calls this from inside
// that loop so the app can draw a frame at the new size. NULL = no live redraw.
typedef void PlatformRefreshProc(void *user);

// NOTE: every backend implements these with the same names:
//   platform_show_error(title, message)   blocking message box (stderr where there is none),
//                                         usable before a window exists
//   PlatformWindowSurfaceSource(window)   VulkanSurfaceSource for vk_init
//   PlatformWindowUiScale(window)         fractional part of the display scale the integer buffer
//                                         scale can't express (150% = scale 1 * 1.5 or 2 * 0.75)
//   PlatformWindowSetRefresh(window, proc, user)
internal void platform_show_error(const char *title, const char *message);

#define ButtonPressed(button)  ((button).ended_down && (button).half_transition_count > 0)
#define ButtonReleased(button) (!(button).ended_down && (button).half_transition_count > 0)

#endif // PLATFORM_H