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