~sircmpwn/wio

ref: ec6567e899a5bbb8e6fe59afd9d62c1892ad894b wio/output.c -rw-r--r-- 13.5 KiB
ec6567e8Leon Plickat Bug-Fix: Can no longer select borders hidden under views 1 year, 2 months ago
                                                                                
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <wayland-server.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_output.h>
#include <wlr/render/wlr_renderer.h>
#include "colors.h"
#include "layers.h"
#include "server.h"
#include "view.h"

struct render_data {
	struct wlr_output *output;
	struct wlr_renderer *renderer;
	struct wio_view *view;
	struct timespec *when;
};

static void render_surface(struct wlr_surface *surface,
		int sx, int sy, void *data) {
	struct render_data *rdata = data;
	struct wio_view *view = rdata->view;
	struct wlr_output *output = rdata->output;
	struct wlr_texture *texture = wlr_surface_get_texture(surface);
	if (texture == NULL) {
		return;
	}
	double ox = 0, oy = 0;
	wlr_output_layout_output_coords(
			view->server->output_layout, output, &ox, &oy);
	ox += view->x + sx, oy += view->y + sy;
	struct wlr_box box = {
		.x = ox * output->scale,
		.y = oy * output->scale,
		.width = surface->current.width * output->scale,
		.height = surface->current.height * output->scale,
	};
	float matrix[9];
	enum wl_output_transform transform =
		wlr_output_transform_invert(surface->current.transform);
	wlr_matrix_project_box(matrix, &box, transform, 0,
		output->transform_matrix);
	wlr_render_texture_with_matrix(rdata->renderer, texture, matrix, 1);
	wlr_surface_send_frame_done(surface, rdata->when);
}

static int scale_length(int length, int offset, float scale) {
	return round((offset + length) * scale) - round(offset * scale);
}

void scale_box(struct wlr_box *box, float scale) {
	box->width = scale_length(box->width, box->x, scale);
	box->height = scale_length(box->height, box->y, scale);
	box->x = round(box->x * scale);
	box->y = round(box->y * scale);
}

static void render_menu(struct wio_output *output) {
	struct wio_server *server = output->server;
	struct wlr_renderer *renderer = server->renderer;

	size_t ntextures =
		sizeof(server->menu.inactive_textures) /
		sizeof(server->menu.inactive_textures[0]);
	int scale = output->wlr_output->scale;
	int border = 3 * scale, margin = 4 * scale;
	int text_height = 0, text_width = 0;
	for (size_t i = 0; i < ntextures; ++i) {
		int width, height;
		// Assumes inactive/active textures are the same size
		// (they probably are)
		wlr_texture_get_size(
				server->menu.inactive_textures[i], &width, &height);
		width /= scale, height /= scale;
		text_height += height + margin;
		if (width >= text_width) {
			text_width = width;
		}
	}
	text_width += border * 2 + margin;
	text_height += border * 2 - margin;

	double ox = server->menu.x, oy = server->menu.y;
	wlr_output_layout_output_coords(
			server->output_layout, output->wlr_output, &ox, &oy);

	struct wlr_box bg_box = { 0 };
	// Background
	bg_box.x = ox;
	bg_box.y = oy;
	bg_box.width = text_width;
	bg_box.height = text_height;
	scale_box(&bg_box, scale);
	wlr_render_rect(renderer, &bg_box, menu_unselected,
			output->wlr_output->transform_matrix);
	// Top
	bg_box.x = ox;
	bg_box.y = oy;
	bg_box.width = text_width;
	bg_box.height = border;
	scale_box(&bg_box, scale);
	wlr_render_rect(renderer, &bg_box, menu_border,
			output->wlr_output->transform_matrix);
	// Bottom
	bg_box.x = ox;
	bg_box.y = oy + text_height;
	bg_box.width = text_width + border;
	bg_box.height = border;
	scale_box(&bg_box, scale);
	wlr_render_rect(renderer, &bg_box, menu_border,
			output->wlr_output->transform_matrix);
	// Left
	bg_box.x = ox;
	bg_box.y = oy;
	bg_box.width = border;
	bg_box.height = text_height;
	scale_box(&bg_box, scale);
	wlr_render_rect(renderer, &bg_box, menu_border,
			output->wlr_output->transform_matrix);
	// Right
	bg_box.x = ox + text_width;
	bg_box.y = oy;
	bg_box.width = border;
	bg_box.height = text_height;
	scale_box(&bg_box, scale);
	wlr_render_rect(renderer, &bg_box, menu_border,
			output->wlr_output->transform_matrix);

	double cur_x = server->cursor->x, cur_y = server->cursor->y;
	wlr_output_layout_output_coords(server->output_layout,
			output->wlr_output, &cur_x, &cur_y);
	server->menu.selected = -1;
	ox += margin;
	oy += margin;
	for (size_t i = 0; i < ntextures; ++i) {
		int width, height;
		struct wlr_texture *texture = server->menu.inactive_textures[i];
		wlr_texture_get_size(texture, &width, &height);
		width /= scale, height /= scale;
		struct wlr_box box = { 0 };
		box.x = ox - scale /* fudge */;
		box.y = oy - scale /* fudge */;
		box.width = text_width - border;
		box.height = height + margin;
		if (wlr_box_contains_point(&box, cur_x, cur_y)) {
			server->menu.selected = i;
			texture = server->menu.active_textures[i];
			scale_box(&box, scale);
			wlr_render_rect(renderer, &box, menu_selected,
					output->wlr_output->transform_matrix);
		} else {
			wlr_texture_get_size(texture, &width, &height);
			width /= scale, height /= scale;
		}
		box.x = (ox + (text_width / 2 - width / 2)) * scale;
		box.y = oy * scale;
		box.width = width * scale;
		box.height = height * scale;
		float matrix[9];
		wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
			output->wlr_output->transform_matrix);
		wlr_render_texture_with_matrix(renderer, texture, matrix, 1);
		oy += height + margin;
	}

	server->menu.width = text_width;
	server->menu.height = text_height;
}

static void render_view_border(struct wlr_renderer *renderer,
		struct wio_output *output, struct wio_view *view,
		int x, int y, int width, int height,
		int selection) {
	float color[4];
	if (selection) {
		memcpy(color, selection_box, sizeof(color));
	} else if (!view || view->xdg_surface->toplevel->current.activated) {
		memcpy(color, active_border, sizeof(color));
	} else {
		memcpy(color, inactive_border, sizeof(color));
	}
	struct wlr_output *wlr_output = output->wlr_output;
	int scale = wlr_output->scale;
	double ox = 0, oy = 0;
	wlr_output_layout_output_coords(
			output->server->output_layout, wlr_output, &ox, &oy);
	ox *= scale, oy *= scale;
	struct wlr_box borders;
	// Top
	borders.x = (x - window_border) * scale;
	borders.x += ox;
	borders.y = (y - window_border) * scale;
	borders.y += oy;
	borders.width = (width + window_border * 2) * scale;
	borders.height = window_border * scale;
	wlr_render_rect(renderer, &borders, color, wlr_output->transform_matrix);
	// Right
	borders.x = (x + width) * scale;
	borders.x += ox;
	borders.y = (y - window_border) * scale;
	borders.y += oy;
	borders.width = window_border * scale;
	borders.height = (height + window_border * 2) * scale;
	wlr_render_rect(renderer, &borders, color, wlr_output->transform_matrix);
	// Bottom
	borders.x = (x - window_border) * scale;
	borders.x += ox;
	borders.y = (y + height) * scale;
	borders.y += oy;
	borders.width = (width + window_border * 2) * scale;
	borders.height = window_border * scale;
	wlr_render_rect(renderer, &borders, color, wlr_output->transform_matrix);
	// Left
	borders.x = (x - window_border) * scale;
	borders.x += ox;
	borders.y = (y - window_border) * scale;
	borders.y += oy;
	borders.width = window_border * scale;
	borders.height = (height + window_border * 2) * scale;
	wlr_render_rect(renderer, &borders, color, wlr_output->transform_matrix);
}

struct render_data_layer {
	struct wlr_output *output;
	struct wlr_renderer *renderer;
	struct wio_view *view;
	struct timespec *when;
};

static void render_layer_surface(struct wlr_surface *surface,
		int sx, int sy, void *data) {
	struct wio_layer_surface *layer_surface = data;
	struct wlr_texture *texture = wlr_surface_get_texture(surface);
	if (texture == NULL) {
		return;
	}
	struct wlr_output *output = layer_surface->layer_surface->output;
	double ox = 0, oy = 0;
	wlr_output_layout_output_coords(
			layer_surface->server->output_layout, output, &ox, &oy);
	ox += layer_surface->geo.x + sx, oy += layer_surface->geo.y + sy;
	float matrix[9];
	enum wl_output_transform transform =
		wlr_output_transform_invert(surface->current.transform);
	struct wlr_box box;
	memcpy(&box, &layer_surface->geo, sizeof(struct wlr_box));
	wlr_matrix_project_box(matrix, &box, transform, 0,
		output->transform_matrix);
	wlr_render_texture_with_matrix(layer_surface->server->renderer,
			texture, matrix, 1);
	// Hack because I'm too lazy to fish through a new rdata struct
	struct timespec now;
	clock_gettime(CLOCK_MONOTONIC, &now);
	wlr_surface_send_frame_done(surface, &now);
}

static void render_layer(
		struct wio_output *output, struct wl_list *layer_surfaces) {
	struct wio_layer_surface *layer_surface;
	wl_list_for_each(layer_surface, layer_surfaces, link) {
		struct wlr_layer_surface_v1 *wlr_layer_surface_v1 =
			layer_surface->layer_surface;
		wlr_surface_for_each_surface(wlr_layer_surface_v1->surface,
			render_layer_surface, layer_surface);
	}
}

static void output_frame(struct wl_listener *listener, void *data) {
	struct wio_output *output = wl_container_of(listener, output, frame);
	struct wio_server *server = output->server;
	struct wlr_renderer *renderer = server->renderer;

	struct timespec now;
	clock_gettime(CLOCK_MONOTONIC, &now);

	if (!wlr_output_attach_render(output->wlr_output, NULL)) {
		return;
	}

	struct wlr_output *wlr_output = output->wlr_output;
	wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height);
	wlr_renderer_clear(renderer, background);

	render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
	render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]);

	struct wio_view *view;
	wl_list_for_each_reverse(view, &server->views, link) {
		if (!view->xdg_surface->mapped) {
			continue;
		}
		struct render_data rdata = {
			.output = wlr_output,
			.view = view,
			.renderer = renderer,
			.when = &now,
		};

		render_view_border(renderer, output, view, view->x, view->y,
				view->xdg_surface->surface->current.width,
				view->xdg_surface->surface->current.height,
				0);
		wlr_xdg_surface_for_each_surface(view->xdg_surface,
				render_surface, &rdata);
	}
	view = server->interactive.view;
	switch (server->input_state) {
	case INPUT_STATE_BORDER_DRAG_TOP:
		render_view_border(renderer, output, view,
			view->x,
			server->cursor->y,
			view->xdg_surface->surface->current.width,
			view->xdg_surface->surface->current.height - (server->cursor->y - server->interactive.sy),
			1);
		break;
	case INPUT_STATE_BORDER_DRAG_LEFT:
		render_view_border(renderer, output, view,
			server->cursor->x,
			view->y,
			view->xdg_surface->surface->current.width - (server->cursor->x - server->interactive.sx),
			view->xdg_surface->surface->current.height,
			1);
		break;
	case INPUT_STATE_BORDER_DRAG_BOTTOM:
		render_view_border(renderer, output, view,
			view->x,
			view->y,
			view->xdg_surface->surface->current.width,
			server->cursor->y - server->interactive.sy,
			1);
		break;
	case INPUT_STATE_BORDER_DRAG_RIGHT:
		render_view_border(renderer, output, view,
			view->x,
			view->y,
			server->cursor->x - server->interactive.sx,
			view->xdg_surface->surface->current.height,
			1);
		break;
	case INPUT_STATE_MOVE:
		render_view_border(renderer, output, view,
			server->cursor->x - server->interactive.sx,
			server->cursor->y - server->interactive.sy,
			view->xdg_surface->surface->current.width,
			view->xdg_surface->surface->current.height,
			1);
		break;
	case INPUT_STATE_NEW_END:
	case INPUT_STATE_RESIZE_END:
		render_view_border(renderer, output, NULL,
			server->interactive.sx, server->interactive.sy,
			server->cursor->x - server->interactive.sx,
			server->cursor->y - server->interactive.sy,
			1);
		break;
	default:
		break;
	}

	render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);

	if (server->menu.x != -1 && server->menu.y != -1) {
		render_menu(output);
	}

	render_layer(output, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);

	wlr_output_render_software_cursors(wlr_output, NULL);
	wlr_renderer_end(renderer);
	wlr_output_commit(wlr_output);
}

void server_new_output(struct wl_listener *listener, void *data) {
	struct wio_server *server =
		wl_container_of(listener, server, new_output);
	struct wlr_output *wlr_output = data;

	struct wio_output *output = calloc(1, sizeof(struct wio_output));
	output->wlr_output = wlr_output;
	output->server = server;
	output->frame.notify = output_frame;
	wl_signal_add(&wlr_output->events.frame, &output->frame);
	wl_list_insert(&server->outputs, &output->link);
	wlr_output->data = output;

	wl_list_init(&output->layers[0]);
	wl_list_init(&output->layers[1]);
	wl_list_init(&output->layers[2]);
	wl_list_init(&output->layers[3]);

	struct wio_output_config *_config, *config = NULL;
	wl_list_for_each(_config, &server->output_configs, link) {
		if (strcmp(_config->name, wlr_output->name) == 0) {
			config = _config;
			break;
		}
	}

	if (config) {
		if (config->x == -1 && config->y == -1) {
			wlr_output_layout_add_auto(server->output_layout, wlr_output);
		} else {
			wlr_output_layout_add(server->output_layout, wlr_output,
					config->x, config->y);
		}
		bool modeset = false;
		if (config->width && config->height
				&& !wl_list_empty(&wlr_output->modes)) {
			struct wlr_output_mode *mode;
			wl_list_for_each(mode, &wlr_output->modes, link) {
				if (mode->width == config->width
						&& mode->height == config->height) {
					wlr_output_set_mode(wlr_output, mode);
					modeset = true;
				}
			}
		}
		if (!modeset) {
			struct wlr_output_mode *mode =
				wlr_output_preferred_mode(wlr_output);
			if (mode) {
				wlr_output_set_mode(wlr_output, mode);
			}
		}
		if (config->scale) {
			wlr_output_set_scale(wlr_output, config->scale);
		}
		if (config->transform) {
			wlr_output_set_transform(wlr_output, config->transform);
		}
	} else {
		struct wlr_output_mode *mode =
			wlr_output_preferred_mode(wlr_output);
		if (mode) {
			wlr_output_set_mode(wlr_output, mode);
		}
		wlr_output_layout_add_auto(server->output_layout, wlr_output);
	}

	wlr_output_create_global(wlr_output);
}