summaryrefslogtreecommitdiffstats
path: root/source/platform/platform_linux.cpp
blob: 68047848a79451b03b632b4280295f6f44b91f65 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//- shared

internal s32 linux_hex_value(char c)
{
	if(c >= '0' && c <= '9') return c - '0';
	if(c >= 'a' && c <= 'f') return c - 'a' + 10;
	if(c >= 'A' && c <= 'F') return c - 'A' + 10;
	return -1;
}

// NOTE: text/uri-list: one uri per line (crlf), '#' lines are comments. file://host/path, the
// host is empty or localhost for local files. percent escapes decoded.
internal void linux_parse_uri_list(PlatformInput *input, const char *text, u64 size)
{
	const char *at  = text;
	const char *end = text + size;
	while(at < end && input->drop_count < PLATFORM_MAX_DROPS) {
		const char *line_end = at;
		while(line_end < end && *line_end != '\r' && *line_end != '\n' && *line_end != 0) ++line_end;
		u64 length = (u64)(line_end - at);
		if(length > 7 && at[0] != '#' && strncmp(at, "file://", 7) == 0) {
			const char *path     = at + 7;
			while(path < line_end && *path != '/') ++path; // skip the host
			char       *out      = input->drop_paths[input->drop_count];
			u32         size_out = 0;
			for(const char *c = path; c < line_end && size_out + 1 < PLATFORM_DROP_PATH_SIZE; ++c) {
				if(*c == '%' && c + 2 < line_end && linux_hex_value(c[1]) >= 0 && linux_hex_value(c[2]) >= 0) {
					out[size_out++] = (char)(linux_hex_value(c[1]) * 16 + linux_hex_value(c[2]));
					c += 2;
				} else {
					out[size_out++] = *c;
				}
			}
			out[size_out] = 0;
			if(size_out > 0) ++input->drop_count;
		}
		at = line_end;
		while(at < end && (*at == '\r' || *at == '\n' || *at == 0)) ++at;
	}
}

internal void platform_show_error(const char *title, const char *message)
{
	// NOTE: no toolkit to borrow a dialog from. stderr always, plus a desktop notification /
	// dialog through whatever helper exists, so a double clicked binary still says something.
	fprintf(stderr, "%s: %s\n", title, message);
	if(!isatty(STDERR_FILENO)) {
		const char *helpers[] = {"zenity", "kdialog", "notify-send"};
		for(u32 index = 0; index < ArrayCount(helpers); ++index) {
			char check[128];
			snprintf(check, sizeof(check), "command -v %s >/dev/null 2>&1", helpers[index]);
			if(system(check) != 0) continue;
			pid_t pid = fork();
			if(pid == 0) {
				if(index == 0)      execlp("zenity", "zenity", "--error", "--title", title, "--text", message, (char *)NULL);
				else if(index == 1) execlp("kdialog", "kdialog", "--title", title, "--error", message, (char *)NULL);
				else                execlp("notify-send", "notify-send", "-u", "critical", title, message, (char *)NULL);
				_exit(127);
			}
			if(pid > 0) waitpid(pid, NULL, 0);
			break;
		}
	}
}

//- dispatch

internal PlatformWindow *linux_window_open(Arena *arena, WindowParams params)
{
	PlatformWindow *window = PushStructZero(arena, PlatformWindow);
	const char     *forced = getenv("DESIGN_IT_BACKEND");
	b32             want_x11 = forced && !strcmp(forced, "x11");
	b32             want_wl  = forced && !strcmp(forced, "wayland");

	if(!want_x11) {
		window->backend = LINUX_BACKEND_WAYLAND;
		if(wayland_window_open(window, params)) return window;
		if(want_wl) return NULL;
		MemSetZero(&window->wayland, sizeof(window->wayland));
	}
	window->backend = LINUX_BACKEND_X11;
	if(x11_window_open(window, params)) return window;
	return NULL;
}

#define LinuxIsWayland(window) ((window)->backend == LINUX_BACKEND_WAYLAND)

internal b32 linux_window_pump(PlatformWindow *window, PlatformInput *input, s64 timeout_ns, int *extra_fds, u32 extra_count, u32 *ready_mask)
{
	return LinuxIsWayland(window) ? wayland_window_pump(window, input, timeout_ns, extra_fds, extra_count, ready_mask)
	                              : x11_window_pump(window, input, timeout_ns, extra_fds, extra_count, ready_mask);
}

internal void linux_window_size(PlatformWindow *window, u32 *width, u32 *height, u32 *scale)
{
	if(LinuxIsWayland(window)) {
		wayland_window_size(window, width, height, scale);
	} else {
		*width  = window->x11.width;
		*height = window->x11.height;
		*scale  = window->x11.scale;
	}
}

internal b32 linux_window_take_resized(PlatformWindow *window)
{
	if(LinuxIsWayland(window)) return wayland_window_take_resized(window);
	b32 resized         = window->x11.resized;
	window->x11.resized = false;
	return resized;
}

internal void linux_window_close(PlatformWindow *window)
{
	if(LinuxIsWayland(window)) wayland_window_close(window);
	else x11_window_close(window);
}

internal void linux_window_begin_move(PlatformWindow *window)
{
	if(LinuxIsWayland(window)) wayland_window_begin_move(window);
	else x11_window_begin_move(window);
}

internal void linux_window_toggle_maximized(PlatformWindow *window)
{
	if(LinuxIsWayland(window)) wayland_window_toggle_maximized(window);
	else x11_window_toggle_maximized(window);
}

internal void linux_window_request_frame(PlatformWindow *window)
{
	if(LinuxIsWayland(window)) {
		wayland_window_request_frame(window);
	} else {
		u64 now                         = time_now_ns();
		window->x11.frame_requested_ns  = now;
		window->x11.last_frame_done_ns  = now;
	}
}

internal void linux_window_cancel_frame(PlatformWindow *window)
{
	if(LinuxIsWayland(window)) wayland_window_cancel_frame(window);
}

internal b32 linux_window_frame_ready(PlatformWindow *window)
{
	return LinuxIsWayland(window) ? wayland_window_frame_ready(window) : true;
}

internal void linux_window_set_cursor(PlatformWindow *window, PlatformCursor cursor)
{
	if(LinuxIsWayland(window)) wayland_window_set_cursor(window, cursor);
	else x11_window_set_cursor(window, cursor);
}

internal u64 linux_window_last_frame_done(PlatformWindow *window)
{
	return LinuxIsWayland(window) ? window->wayland.last_frame_done_ns : window->x11.last_frame_done_ns;
}

internal u64 linux_window_frame_requested(PlatformWindow *window)
{
	return LinuxIsWayland(window) ? window->wayland.frame_requested_ns : window->x11.frame_requested_ns;
}

internal b32 linux_window_is_layer(PlatformWindow *window)
{
	return LinuxIsWayland(window) ? window->wayland.is_layer : window->x11.is_layer;
}

internal b32 linux_window_suspended(PlatformWindow *window)
{
	return LinuxIsWayland(window) ? window->wayland.suspended : window->x11.suspended;
}

internal f32 linux_window_ui_scale(PlatformWindow *window)
{
	// NOTE: wayland hands us an integer buffer scale (no fractional-scale-v1 yet)
	if(LinuxIsWayland(window)) return 1.0f;
	return window->x11.dpi / (96.0f * (f32)window->x11.scale);
}

internal VulkanSurfaceSource linux_window_surface_source(PlatformWindow *window)
{
	VulkanSurfaceSource source = {};
	if(LinuxIsWayland(window)) {
		source.kind = VK_SURFACE_SOURCE_WAYLAND;
		source.a    = (void *)window->wayland.display;
		source.b    = (void *)window->wayland.surface;
	} else {
		source.kind = VK_SURFACE_SOURCE_XLIB;
		source.a    = (void *)window->x11.display;
		source.b    = (void *)(umm)window->x11.window;
	}
	return source;
}

internal void linux_clipboard_set(PlatformWindow *window, const char *data, u64 size)
{
	if(LinuxIsWayland(window)) wayland_clipboard_set(window, data, size);
	else x11_clipboard_set(window, data, size);
}

internal String8 linux_clipboard_get(PlatformWindow *window, Arena *arena)
{
	return LinuxIsWayland(window) ? wayland_clipboard_get(window, arena) : x11_clipboard_get(window, arena);
}