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