-
Notifications
You must be signed in to change notification settings - Fork 20
/
xdg_shell.c
407 lines (348 loc) · 13.9 KB
/
xdg_shell.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
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
// Copyright 2020 - 2024, project-repo and the cagebreak contributors
// SPDX-License-Identifier: MIT
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdbool.h>
#include <wayland-server-core.h>
#include <wlr/types/wlr_output_layout.h>
#include <wlr/types/wlr_scene.h>
#include <wlr/types/wlr_xdg_decoration_v1.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/box.h>
#include <wlr/util/edges.h>
#include <wlr/util/log.h>
#include "output.h"
#include "server.h"
#include "view.h"
#include "workspace.h"
#include "xdg_shell.h"
static void
xdg_decoration_handle_destroy(struct wl_listener *listener,
__attribute__((unused)) void *data) {
struct cg_xdg_decoration *xdg_decoration =
wl_container_of(listener, xdg_decoration, destroy);
wl_list_remove(&xdg_decoration->destroy.link);
wl_list_remove(&xdg_decoration->request_mode.link);
wl_list_remove(&xdg_decoration->link);
free(xdg_decoration);
}
static void
xdg_decoration_handle_request_mode(struct wl_listener *listener,
__attribute__((unused)) void *_data) {
struct cg_xdg_decoration *xdg_decoration =
wl_container_of(listener, xdg_decoration, request_mode);
if(xdg_decoration->wlr_decoration->toplevel->base->initialized) {
wlr_xdg_toplevel_decoration_v1_set_mode(
xdg_decoration->wlr_decoration,
WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
}
}
static void
popup_unconstrain(struct cg_xdg_shell_popup *popup) {
struct cg_view *view = popup->view;
struct wlr_box *popup_box = &popup->wlr_popup->current.geometry;
struct wlr_output_layout *output_layout = view->server->output_layout;
struct wlr_output *wlr_output = wlr_output_layout_output_at(
output_layout,
output_get_layout_box(view->workspace->output).x + view->ox +
popup_box->x,
output_get_layout_box(view->workspace->output).y + view->oy +
popup_box->y);
struct wlr_box output_box;
wlr_output_layout_get_box(output_layout, wlr_output, &output_box);
struct wlr_box output_toplevel_box = {.x = -view->ox,
.y = -view->oy,
.width = output_box.width,
.height = output_box.height};
wlr_xdg_popup_unconstrain_from_box(popup->wlr_popup, &output_toplevel_box);
}
static struct cg_xdg_shell_view *
xdg_shell_view_from_view(struct cg_view *view) {
return (struct cg_xdg_shell_view *)view;
}
static const struct cg_xdg_shell_view *
xdg_shell_view_from_const_view(const struct cg_view *view) {
return (const struct cg_xdg_shell_view *)view;
}
static pid_t
get_pid(const struct cg_view *view) {
pid_t pid;
struct wl_client *client =
wl_resource_get_client(view->wlr_surface->resource);
wl_client_get_credentials(client, &pid, NULL, NULL);
return pid;
}
static char *
get_title(const struct cg_view *view) {
const struct cg_xdg_shell_view *xdg_shell_view =
xdg_shell_view_from_const_view(view);
return xdg_shell_view->toplevel->title;
}
static bool
is_primary(const struct cg_view *view) {
const struct cg_xdg_shell_view *xdg_shell_view =
xdg_shell_view_from_const_view(view);
struct wlr_xdg_toplevel *parent = xdg_shell_view->toplevel->parent;
return parent == NULL;
}
static void
activate(struct cg_view *view, bool activate) {
struct cg_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view);
wlr_xdg_toplevel_set_activated(xdg_shell_view->toplevel, activate);
}
static void
close(struct cg_view *view) {
struct cg_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view);
if(view->type == CG_XDG_SHELL_VIEW) {
wlr_xdg_toplevel_send_close(xdg_shell_view->toplevel);
}
}
static void
maximize(struct cg_view *view, int width, int height) {
struct cg_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view);
wlr_xdg_toplevel_set_size(xdg_shell_view->toplevel, width, height);
enum wlr_edges edges =
WLR_EDGE_LEFT | WLR_EDGE_RIGHT | WLR_EDGE_TOP | WLR_EDGE_BOTTOM;
wlr_xdg_toplevel_set_tiled(xdg_shell_view->toplevel, edges);
}
static void
destroy(struct cg_view *view) {
struct cg_xdg_shell_view *xdg_shell_view = xdg_shell_view_from_view(view);
free(xdg_shell_view);
}
static void
handle_xdg_shell_surface_request_fullscreen(
struct wl_listener *listener, __attribute__((unused)) void *data) {
struct cg_xdg_shell_view *xdg_shell_view =
wl_container_of(listener, xdg_shell_view, request_fullscreen);
/**
* Certain clients do not like figuring out their own window geometry if
* they display in fullscreen mode, so we set it here (if the view was
* already mapped).
*/
if(xdg_shell_view->view.workspace != NULL) {
struct wlr_box layout_box;
wlr_output_layout_get_box(
xdg_shell_view->view.server->output_layout,
xdg_shell_view->view.workspace->output->wlr_output, &layout_box);
wlr_xdg_toplevel_set_size(xdg_shell_view->toplevel, layout_box.width,
layout_box.height);
}
wlr_xdg_toplevel_set_fullscreen(
xdg_shell_view->toplevel,
xdg_shell_view->toplevel->requested.fullscreen);
}
static void
handle_xdg_shell_surface_unmap(struct wl_listener *listener,
__attribute__((unused)) void *_data) {
struct cg_xdg_shell_view *xdg_shell_view =
wl_container_of(listener, xdg_shell_view, unmap);
struct cg_view *view = &xdg_shell_view->view;
view_unmap(view);
}
struct cg_xdg_decoration *
xdg_decoration_from_surface(struct wlr_surface *surface,
struct cg_server *server) {
struct cg_xdg_decoration *deco;
wl_list_for_each(deco, &server->xdg_decorations, link) {
if(deco->wlr_decoration->toplevel->base->surface == surface) {
return deco;
}
}
return NULL;
}
static void
handle_xdg_shell_surface_map(struct wl_listener *listener,
__attribute__((unused)) void *_data) {
struct cg_xdg_shell_view *xdg_shell_view =
wl_container_of(listener, xdg_shell_view, map);
struct cg_view *view = &xdg_shell_view->view;
view_map(view, xdg_shell_view->toplevel->base->surface,
view->server->curr_output
->workspaces[view->server->curr_output->curr_workspace]);
}
static void
handle_xdg_shell_surface_destroy(struct wl_listener *listener,
__attribute__((unused)) void *_data) {
struct cg_xdg_shell_view *xdg_shell_view =
wl_container_of(listener, xdg_shell_view, destroy);
struct cg_view *view = &xdg_shell_view->view;
wl_list_remove(&xdg_shell_view->map.link);
wl_list_remove(&xdg_shell_view->unmap.link);
wl_list_remove(&xdg_shell_view->destroy.link);
wl_list_remove(&xdg_shell_view->new_popup.link);
wl_list_remove(&xdg_shell_view->request_fullscreen.link);
xdg_shell_view->toplevel = NULL;
view_destroy(view);
}
static const struct cg_view_impl xdg_shell_view_impl = {.get_pid = get_pid,
.get_title = get_title,
.is_primary =
is_primary,
.activate = activate,
.close = close,
.maximize = maximize,
.destroy = destroy};
void
handle_xdg_shell_toplevel_commit(struct wl_listener *listener,
__attribute__((unused)) void *data) {
struct cg_xdg_shell_view *xdg_shell_view =
wl_container_of(listener, xdg_shell_view, commit);
struct cg_view *view = &xdg_shell_view->view;
struct wlr_xdg_surface *xdg_surface = xdg_shell_view->toplevel->base;
if(xdg_surface->initial_commit) {
struct cg_xdg_decoration *decoration =
xdg_decoration_from_surface(xdg_surface->surface, view->server);
if(decoration != NULL) {
wlr_xdg_toplevel_decoration_v1_set_mode(
decoration->wlr_decoration,
WLR_XDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
}
wlr_xdg_surface_schedule_configure(xdg_surface);
wlr_xdg_toplevel_set_wm_capabilities(
xdg_shell_view->toplevel, XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN);
}
}
void
handle_xdg_shell_toplevel_new(struct wl_listener *listener, void *data) {
struct cg_server *server =
wl_container_of(listener, server, new_xdg_shell_toplevel);
struct wlr_xdg_toplevel *xdg_toplevel = data;
wlr_xdg_surface_ping(xdg_toplevel->base);
struct cg_xdg_shell_view *xdg_shell_view =
calloc(1, sizeof(struct cg_xdg_shell_view));
if(!xdg_shell_view) {
wlr_log(WLR_ERROR, "Failed to allocate XDG Shell view");
return;
}
view_init(&xdg_shell_view->view, CG_XDG_SHELL_VIEW, &xdg_shell_view_impl,
server);
xdg_shell_view->toplevel = xdg_toplevel;
xdg_shell_view->new_popup.notify = handle_xdg_shell_popup_new;
wl_signal_add(&xdg_toplevel->base->events.new_popup,
&xdg_shell_view->new_popup);
xdg_shell_view->commit.notify = handle_xdg_shell_toplevel_commit;
wl_signal_add(&xdg_toplevel->base->surface->events.commit,
&xdg_shell_view->commit);
xdg_shell_view->map.notify = handle_xdg_shell_surface_map;
wl_signal_add(&xdg_toplevel->base->surface->events.map,
&xdg_shell_view->map);
xdg_shell_view->unmap.notify = handle_xdg_shell_surface_unmap;
wl_signal_add(&xdg_toplevel->base->surface->events.unmap,
&xdg_shell_view->unmap);
xdg_shell_view->destroy.notify = handle_xdg_shell_surface_destroy;
wl_signal_add(&xdg_toplevel->base->events.destroy,
&xdg_shell_view->destroy);
xdg_shell_view->request_fullscreen.notify =
handle_xdg_shell_surface_request_fullscreen;
wl_signal_add(&xdg_toplevel->events.request_fullscreen,
&xdg_shell_view->request_fullscreen);
wlr_scene_xdg_surface_create(xdg_shell_view->view.scene_tree,
xdg_toplevel->base);
xdg_toplevel->base->data = xdg_shell_view;
return;
}
static void
handle_xdg_shell_popup_destroy(struct wl_listener *listener,
__attribute__((unused)) void *data) {
struct cg_xdg_shell_popup *popup =
wl_container_of(listener, popup, destroy);
wl_list_remove(&popup->new_popup.link);
wl_list_remove(&popup->destroy.link);
wl_list_remove(&popup->commit.link);
wl_list_remove(&popup->reposition.link);
wlr_scene_node_destroy(&popup->scene_tree->node);
free(popup);
}
static void
handle_xdg_shell_popup_commit(struct wl_listener *listener,
__attribute__((unused)) void *data) {
struct cg_xdg_shell_popup *popup = wl_container_of(listener, popup, commit);
if(popup->wlr_popup->base->initial_commit) {
popup_unconstrain(popup);
}
}
static void
handle_xdg_shell_popup_reposition(struct wl_listener *listener,
__attribute__((unused)) void *data) {
struct cg_xdg_shell_popup *popup = wl_container_of(listener, popup, commit);
popup_unconstrain(popup);
}
static struct cg_xdg_shell_popup *
create_xdg_popup(struct wlr_xdg_popup *wlr_popup, struct cg_view *view,
struct wlr_scene_tree *parent_tree);
void
handle_xdg_shell_popup_new_popup(struct wl_listener *listener, void *data) {
struct cg_xdg_shell_popup *popup =
wl_container_of(listener, popup, new_popup);
struct wlr_xdg_popup *wlr_popup = data;
create_xdg_popup(wlr_popup, popup->view, popup->xdg_surface_tree);
}
static struct cg_xdg_shell_popup *
create_xdg_popup(struct wlr_xdg_popup *wlr_popup, struct cg_view *view,
struct wlr_scene_tree *parent_tree) {
struct wlr_xdg_surface *xdg_surface = wlr_popup->base;
struct cg_xdg_shell_popup *popup = calloc(1, sizeof(*popup));
if(!popup) {
return NULL;
}
popup->wlr_popup = wlr_popup;
popup->view = view;
struct cg_xdg_shell_view *xdg_view = wl_container_of(view, xdg_view, view);
xdg_surface->data = xdg_view;
popup->scene_tree = wlr_scene_tree_create(parent_tree);
if(!popup->scene_tree) {
free(popup);
return NULL;
}
popup->xdg_surface_tree =
wlr_scene_xdg_surface_create(popup->scene_tree, xdg_surface);
if(!popup->xdg_surface_tree) {
wlr_scene_node_destroy(&popup->scene_tree->node);
free(popup);
return NULL;
}
wl_signal_add(&xdg_surface->surface->events.commit, &popup->commit);
popup->commit.notify = handle_xdg_shell_popup_commit;
wl_signal_add(&xdg_surface->events.new_popup, &popup->new_popup);
popup->new_popup.notify = handle_xdg_shell_popup_new_popup;
wl_signal_add(&wlr_popup->events.reposition, &popup->reposition);
popup->reposition.notify = handle_xdg_shell_popup_reposition;
wl_signal_add(&wlr_popup->events.destroy, &popup->destroy);
popup->destroy.notify = handle_xdg_shell_popup_destroy;
return popup;
}
void
handle_xdg_shell_popup_new(struct wl_listener *listener, void *data) {
struct cg_xdg_shell_view *xdg_shell_view =
wl_container_of(listener, xdg_shell_view, new_popup);
struct wlr_xdg_popup *xdg_popup = data;
struct cg_xdg_shell_popup *popup = create_xdg_popup(
xdg_popup, &xdg_shell_view->view, xdg_shell_view->view.scene_tree);
if(!popup) {
return;
}
int lx, ly;
wlr_scene_node_coords(&popup->view->scene_tree->node, &lx, &ly);
wlr_scene_node_set_position(&popup->scene_tree->node, lx, ly);
return;
}
void
handle_xdg_toplevel_decoration(struct wl_listener *listener, void *data) {
struct cg_server *server =
wl_container_of(listener, server, xdg_toplevel_decoration);
struct wlr_xdg_toplevel_decoration_v1 *wlr_decoration = data;
struct cg_xdg_decoration *xdg_decoration =
calloc(1, sizeof(struct cg_xdg_decoration));
if(!xdg_decoration) {
return;
}
wl_list_insert(&server->xdg_decorations, &xdg_decoration->link);
xdg_decoration->wlr_decoration = wlr_decoration;
xdg_decoration->server = server;
xdg_decoration->destroy.notify = xdg_decoration_handle_destroy;
wl_signal_add(&wlr_decoration->events.destroy, &xdg_decoration->destroy);
xdg_decoration->request_mode.notify = xdg_decoration_handle_request_mode;
wl_signal_add(&wlr_decoration->events.request_mode,
&xdg_decoration->request_mode);
}