Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 355b9db

Browse files
committed
scene: add wlr_scene_xdg_popup_create
This allows compositors to easily add an xdg_popup to the scene-graph while retaining the ability to unconstraint the popup and decide its final position. Compositors can handle new popups with the wlr_xdg_shell.new_surface event, get the parent scene-graph node via wlr_xdg_popup.parent.data, create a new scene-graph node via wlr_scene_xdg_popup_tree_create, and unconstraint the popup if they want to.
1 parent cbedbd0 commit 355b9db

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

include/wlr/types/wlr_scene.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
struct wlr_output;
2727
struct wlr_output_layout;
28+
struct wlr_xdg_popup;
2829

2930
enum wlr_scene_node_type {
3031
WLR_SCENE_NODE_ROOT,
@@ -298,4 +299,11 @@ bool wlr_scene_attach_output_layout(struct wlr_scene *scene,
298299
struct wlr_scene_node *wlr_scene_subsurface_tree_create(
299300
struct wlr_scene_node *parent, struct wlr_surface *surface);
300301

302+
/**
303+
* Add a node displaying an xdg_popup and all of its sub-surfaces to the
304+
* scene-graph.
305+
*/
306+
struct wlr_scene_node *wlr_scene_xdg_popup_create(
307+
struct wlr_scene_node *parent, struct wlr_xdg_popup *popup);
308+
301309
#endif

types/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ wlr_files += files(
1010
'scene/subsurface_tree.c',
1111
'scene/wlr_scene.c',
1212
'scene/output_layout.c',
13+
'scene/xdg_shell.c',
1314
'seat/wlr_seat_keyboard.c',
1415
'seat/wlr_seat_pointer.c',
1516
'seat/wlr_seat_touch.c',

types/scene/xdg_shell.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <stdlib.h>
2+
#include <wlr/types/wlr_scene.h>
3+
#include <wlr/types/wlr_xdg_shell.h>
4+
5+
struct wlr_scene_xdg_popup {
6+
struct wlr_scene_tree *tree;
7+
struct wlr_xdg_popup *popup;
8+
struct wlr_scene_node *surface_node;
9+
10+
struct wl_listener tree_destroy;
11+
struct wl_listener popup_destroy;
12+
struct wl_listener popup_map;
13+
struct wl_listener popup_unmap;
14+
struct wl_listener popup_configure;
15+
};
16+
17+
static void scene_popup_handle_tree_destroy(struct wl_listener *listener,
18+
void *data) {
19+
struct wlr_scene_xdg_popup *scene_popup =
20+
wl_container_of(listener, scene_popup, tree_destroy);
21+
// tree and surface_node will be cleaned up by scene_node_finish
22+
wl_list_remove(&scene_popup->tree_destroy.link);
23+
wl_list_remove(&scene_popup->popup_destroy.link);
24+
free(scene_popup);
25+
}
26+
27+
static void scene_popup_handle_popup_destroy(struct wl_listener *listener,
28+
void *data) {
29+
struct wlr_scene_xdg_popup *scene_popup =
30+
wl_container_of(listener, scene_popup, popup_destroy);
31+
wlr_scene_node_destroy(&scene_popup->tree->node);
32+
}
33+
34+
static void scene_popup_handle_popup_map(struct wl_listener *listener,
35+
void *data) {
36+
struct wlr_scene_xdg_popup *scene_popup =
37+
wl_container_of(listener, scene_popup, popup_map);
38+
wlr_scene_node_set_enabled(&scene_popup->tree->node, true);
39+
}
40+
41+
static void scene_popup_handle_popup_unmap(struct wl_listener *listener,
42+
void *data) {
43+
struct wlr_scene_xdg_popup *scene_popup =
44+
wl_container_of(listener, scene_popup, popup_unmap);
45+
wlr_scene_node_set_enabled(&scene_popup->tree->node, false);
46+
}
47+
48+
static void scene_popup_update_position(
49+
struct wlr_scene_xdg_popup *scene_popup) {
50+
double sx, sy;
51+
wlr_xdg_popup_get_position(scene_popup->popup, &sx, &sy);
52+
wlr_scene_node_set_position(&scene_popup->tree->node, sx, sy);
53+
}
54+
55+
static void scene_popup_handle_popup_configure(struct wl_listener *listener,
56+
void *data) {
57+
struct wlr_scene_xdg_popup *scene_popup =
58+
wl_container_of(listener, scene_popup, popup_configure);
59+
scene_popup_update_position(scene_popup);
60+
}
61+
62+
struct wlr_scene_node *wlr_scene_xdg_popup_create(
63+
struct wlr_scene_node *parent, struct wlr_xdg_popup *popup) {
64+
struct wlr_scene_xdg_popup *scene_popup = calloc(1, sizeof(*scene_popup));
65+
if (scene_popup == NULL) {
66+
return NULL;
67+
}
68+
69+
scene_popup->popup = popup;
70+
71+
scene_popup->tree = wlr_scene_tree_create(parent);
72+
if (scene_popup->tree == NULL) {
73+
free(scene_popup);
74+
return NULL;
75+
}
76+
77+
scene_popup->surface_node = wlr_scene_subsurface_tree_create(
78+
&scene_popup->tree->node, popup->base->surface);
79+
if (scene_popup->surface_node == NULL) {
80+
wlr_scene_node_destroy(&scene_popup->tree->node);
81+
free(scene_popup);
82+
return NULL;
83+
}
84+
85+
scene_popup->tree_destroy.notify = scene_popup_handle_tree_destroy;
86+
wl_signal_add(&scene_popup->tree->node.events.destroy,
87+
&scene_popup->tree_destroy);
88+
89+
scene_popup->popup_destroy.notify = scene_popup_handle_popup_destroy;
90+
wl_signal_add(&popup->base->events.destroy, &scene_popup->popup_destroy);
91+
92+
scene_popup->popup_map.notify = scene_popup_handle_popup_map;
93+
wl_signal_add(&popup->base->events.map, &scene_popup->popup_map);
94+
95+
scene_popup->popup_unmap.notify = scene_popup_handle_popup_unmap;
96+
wl_signal_add(&popup->base->events.unmap, &scene_popup->popup_unmap);
97+
98+
scene_popup->popup_configure.notify = scene_popup_handle_popup_configure;
99+
wl_signal_add(&popup->base->events.configure, &scene_popup->popup_configure);
100+
101+
wlr_scene_node_set_enabled(&scene_popup->tree->node, popup->base->mapped);
102+
scene_popup_update_position(scene_popup);
103+
104+
return &scene_popup->tree->node;
105+
}

0 commit comments

Comments
 (0)