This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
output.c
239 lines (195 loc) · 6.32 KB
/
output.c
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
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wayland-client.h>
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-output-unstable-v1-client-protocol.h"
#include "oguri.h"
#include "animation.h"
#include "output.h"
#include "buffers.h"
static void noop() {} // For unused listener members.
//
// Wayland outputs
//
static void oguri_recreate_buffers(struct oguri_output * output) {
if (!output->width || !output->height) {
// Probably in the middle of reconfiguring, will try again later.
return;
}
output->cached_frames = 0;
struct oguri_buffer * buffer, * tmp;
wl_list_for_each_safe(buffer, tmp, &output->buffer_ring, link) {
oguri_buffer_destroy(buffer);
}
wl_list_init(&output->buffer_ring);
// If this is the first time we're being configured, we will have zero
// buffers. We always want to have at least two. If it isn't the first
// time, we need to reallocate as many as we already had because the
// animation loop won't do it for us if it has already finished the first
// cycle.
unsigned int count = (output->buffer_count) ? output->buffer_count : 2;
output->buffer_count = 0;
if (!oguri_allocate_buffers(output, count)) {
fprintf(stderr, "Could not allocate buffers!\n");
output->oguri->run = false;
}
}
static void handle_output_scale(
void * data,
struct wl_output * wl_output __attribute__((unused)),
int32_t factor) {
struct oguri_output * output = data;
output->scale = factor;
}
static void handle_output_done(
void * data,
struct wl_output * wl_output __attribute__((unused))) {
struct oguri_output * output = data;
oguri_recreate_buffers(output);
}
struct wl_output_listener output_listener = {
.scale = handle_output_scale,
.geometry = noop,
.mode = noop,
.done = handle_output_done,
};
//
// wlroots layer surface
//
static void layer_surface_configure(
void * data,
struct zwlr_layer_surface_v1 * layer_surface,
uint32_t serial,
uint32_t width,
uint32_t height) {
struct oguri_output * output = data;
output->width = width;
output->height = height;
// The entire surface can be marked opaque, as it should be the lowest
// z-index on the display.
struct wl_region * opaque = wl_compositor_create_region(
output->oguri->compositor);
wl_region_add(opaque, 0, 0, output->width, output->height);
wl_surface_set_opaque_region(output->surface, opaque);
wl_region_destroy(opaque);
zwlr_layer_surface_v1_ack_configure(layer_surface, serial);
oguri_recreate_buffers(output);
}
static void layer_surface_closed(
void * data,
struct zwlr_layer_surface_v1 * layer_surface __attribute__((unused))) {
struct oguri_output * output = data;
oguri_output_destroy(output);
}
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layer_surface_configure,
.closed = layer_surface_closed,
};
//
// XDG Output Manager
//
static void handle_xdg_output_name(
void * data,
struct zxdg_output_v1 * xdg_output __attribute__((unused)),
const char *name) {
struct oguri_output * output = (struct oguri_output *)data;
output->name = strdup(name);
}
static void handle_xdg_output_done(
void * data __attribute__((unused)),
struct zxdg_output_v1 * xdg_output) {
// We have no further use for this object.
zxdg_output_v1_destroy(xdg_output);
}
struct zxdg_output_v1_listener xdg_output_listener = {
.name = handle_xdg_output_name,
.done = handle_xdg_output_done,
.logical_position = noop,
.logical_size = noop,
.description = noop,
};
//
// Outputs
//
struct oguri_output * oguri_output_create(
struct oguri_state * oguri, struct wl_output * wl_output) {
struct oguri_output * output = calloc(1, sizeof(struct oguri_output));
output->oguri = oguri;
wl_list_init(&output->link);
wl_list_init(&output->buffer_ring);
output->output = wl_output;
wl_output_add_listener(wl_output, &output_listener, output);
output->surface = wl_compositor_create_surface(oguri->compositor);
if (!output->surface) {
fprintf(stderr, "Couldn't create surface for output!");
oguri_output_destroy(output);
return NULL;
}
struct wl_region * input_region = wl_compositor_create_region(
oguri->compositor);
if (!input_region) {
fprintf(stderr, "Couldn't create input region for output!");
oguri_output_destroy(output);
return NULL;
}
wl_surface_set_input_region(output->surface, input_region);
wl_region_destroy(input_region);
output->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
oguri->layer_shell,
output->surface,
output->output,
ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND,
"wallpaper");
if (!output->layer_surface) {
fprintf(stderr, "Couldn't create layer surface for output!");
oguri_output_destroy(output);
return NULL;
}
// xdg-output support is optional, so we need to check for it.
if (oguri->output_manager) {
struct zxdg_output_v1 * xdg_output =
zxdg_output_manager_v1_get_xdg_output(
oguri->output_manager, output->output);
zxdg_output_v1_add_listener(xdg_output, &xdg_output_listener, output);
}
else {
// TODO: Need to assign name as str of index and manually associate.
}
zwlr_layer_surface_v1_set_size(output->layer_surface, 0, 0);
zwlr_layer_surface_v1_set_anchor(output->layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, -1);
zwlr_layer_surface_v1_add_listener(output->layer_surface,
&layer_surface_listener, output);
wl_surface_commit(output->surface);
// Get the rest of the output properties. We need the name to associate
// this output with an oguri_output_config, and oguri_reconfigure assumes
// it is already present.
wl_display_roundtrip(oguri->display);
wl_list_insert(oguri->idle_outputs.prev, &output->link);
oguri_reconfigure(oguri);
return output;
}
void oguri_output_destroy(struct oguri_output * output) {
wl_list_remove(&output->link);
free(output->name);
if (output->surface) {
wl_surface_destroy(output->surface);
}
if (output->layer_surface) {
zwlr_layer_surface_v1_destroy(output->layer_surface);
}
struct oguri_buffer * buffer, * tmp;
wl_list_for_each_safe(buffer, tmp, &output->buffer_ring, link) {
oguri_buffer_destroy(buffer);
}
wl_output_destroy(output->output);
free(output);
}