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

Commit bb79d90

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 0817c52 commit bb79d90

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

include/wlr/types/wlr_scene.h

+8
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

+1
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

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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_ack_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+
wl_list_remove(&scene_popup->popup_map.link);
25+
wl_list_remove(&scene_popup->popup_unmap.link);
26+
wl_list_remove(&scene_popup->popup_ack_configure.link);
27+
free(scene_popup);
28+
}
29+
30+
static void scene_popup_handle_popup_destroy(struct wl_listener *listener,
31+
void *data) {
32+
struct wlr_scene_xdg_popup *scene_popup =
33+
wl_container_of(listener, scene_popup, popup_destroy);
34+
wlr_scene_node_destroy(&scene_popup->tree->node);
35+
}
36+
37+
static void scene_popup_handle_popup_map(struct wl_listener *listener,
38+
void *data) {
39+
struct wlr_scene_xdg_popup *scene_popup =
40+
wl_container_of(listener, scene_popup, popup_map);
41+
wlr_scene_node_set_enabled(&scene_popup->tree->node, true);
42+
}
43+
44+
static void scene_popup_handle_popup_unmap(struct wl_listener *listener,
45+
void *data) {
46+
struct wlr_scene_xdg_popup *scene_popup =
47+
wl_container_of(listener, scene_popup, popup_unmap);
48+
wlr_scene_node_set_enabled(&scene_popup->tree->node, false);
49+
}
50+
51+
static void scene_popup_update_position(
52+
struct wlr_scene_xdg_popup *scene_popup) {
53+
struct wlr_xdg_popup *popup = scene_popup->popup;
54+
55+
struct wlr_box geo = {0};
56+
wlr_xdg_surface_get_geometry(popup->base, &geo);
57+
58+
wlr_scene_node_set_position(&scene_popup->tree->node,
59+
popup->geometry.x - geo.x, popup->geometry.y - geo.y);
60+
}
61+
62+
static void scene_popup_handle_popup_ack_configure(struct wl_listener *listener,
63+
void *data) {
64+
struct wlr_scene_xdg_popup *scene_popup =
65+
wl_container_of(listener, scene_popup, popup_ack_configure);
66+
scene_popup_update_position(scene_popup);
67+
}
68+
69+
struct wlr_scene_node *wlr_scene_xdg_popup_create(
70+
struct wlr_scene_node *parent, struct wlr_xdg_popup *popup) {
71+
struct wlr_scene_xdg_popup *scene_popup = calloc(1, sizeof(*scene_popup));
72+
if (scene_popup == NULL) {
73+
return NULL;
74+
}
75+
76+
scene_popup->popup = popup;
77+
78+
scene_popup->tree = wlr_scene_tree_create(parent);
79+
if (scene_popup->tree == NULL) {
80+
free(scene_popup);
81+
return NULL;
82+
}
83+
84+
scene_popup->surface_node = wlr_scene_subsurface_tree_create(
85+
&scene_popup->tree->node, popup->base->surface);
86+
if (scene_popup->surface_node == NULL) {
87+
wlr_scene_node_destroy(&scene_popup->tree->node);
88+
free(scene_popup);
89+
return NULL;
90+
}
91+
92+
scene_popup->tree_destroy.notify = scene_popup_handle_tree_destroy;
93+
wl_signal_add(&scene_popup->tree->node.events.destroy,
94+
&scene_popup->tree_destroy);
95+
96+
scene_popup->popup_destroy.notify = scene_popup_handle_popup_destroy;
97+
wl_signal_add(&popup->base->events.destroy, &scene_popup->popup_destroy);
98+
99+
scene_popup->popup_map.notify = scene_popup_handle_popup_map;
100+
wl_signal_add(&popup->base->events.map, &scene_popup->popup_map);
101+
102+
scene_popup->popup_unmap.notify = scene_popup_handle_popup_unmap;
103+
wl_signal_add(&popup->base->events.unmap, &scene_popup->popup_unmap);
104+
105+
scene_popup->popup_ack_configure.notify =
106+
scene_popup_handle_popup_ack_configure;
107+
wl_signal_add(&popup->base->events.ack_configure,
108+
&scene_popup->popup_ack_configure);
109+
110+
wlr_scene_node_set_enabled(&scene_popup->tree->node, popup->base->mapped);
111+
scene_popup_update_position(scene_popup);
112+
113+
return &scene_popup->tree->node;
114+
}

0 commit comments

Comments
 (0)