From 1ee209dc56446fd53e82d6854ad1bd0634eae022 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Mon, 26 Aug 2024 17:13:47 -0400 Subject: [PATCH] cursor: Split node_at_coords into smaller functions --- include/sway/input/cursor.h | 8 +++++ sway/input/cursor.c | 67 +++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/include/sway/input/cursor.h b/include/sway/input/cursor.h index 8b3ce941..527218dc 100644 --- a/include/sway/input/cursor.h +++ b/include/sway/input/cursor.h @@ -84,6 +84,14 @@ struct sway_cursor { struct sway_node; +struct wlr_scene_node *scene_node_at_coords( + double lx, double ly, double *sx, double *sy); + +struct wlr_surface *surface_try_from_scene_node(struct wlr_scene_node *node); + +struct sway_node *sway_node_try_from_scene_node(struct wlr_scene_node *node, + double lx, double ly); + struct sway_node *node_at_coords( double lx, double ly, struct wlr_surface **surface, double *sx, double *sy); diff --git a/sway/input/cursor.c b/sway/input/cursor.c index fff6067e..068390f9 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -38,14 +38,8 @@ static uint32_t get_current_time_msec(void) { return now.tv_sec * 1000 + now.tv_nsec / 1000000; } -/** - * Returns the node at the cursor's position. If there is a surface at that - * location, it is stored in **surface (it may not be a view). - */ -struct sway_node *node_at_coords(double lx, double ly, - struct wlr_surface **surface, double *sx, double *sy) { - struct wlr_scene_node *scene_node = NULL; - +struct wlr_scene_node *scene_node_at_coords( + double lx, double ly, double *sx, double *sy) { struct wlr_scene_node *node; wl_list_for_each_reverse(node, &root->layer_tree->children, link) { struct wlr_scene_tree *layer = wlr_scene_tree_from_node(node); @@ -56,28 +50,37 @@ struct sway_node *node_at_coords(double lx, double ly, continue; } - scene_node = wlr_scene_node_at(&layer->node, lx, ly, sx, sy); - if (scene_node) { - break; + struct wlr_scene_node *node = wlr_scene_node_at(&layer->node, lx, ly, sx, sy); + if (node) { + return node; } } - if (scene_node) { - // determine what wlr_surface we clicked on - if (scene_node->type == WLR_SCENE_NODE_BUFFER) { - struct wlr_scene_buffer *scene_buffer = - wlr_scene_buffer_from_node(scene_node); - struct wlr_scene_surface *scene_surface = - wlr_scene_surface_try_from_buffer(scene_buffer); + return NULL; +} - if (scene_surface) { - *surface = scene_surface->surface; - } - } +struct wlr_surface *surface_try_from_scene_node(struct wlr_scene_node *node) { + if (!node || node->type != WLR_SCENE_NODE_BUFFER) { + return NULL; + } + struct wlr_scene_buffer *scene_buffer = + wlr_scene_buffer_from_node(node); + struct wlr_scene_surface *scene_surface = + wlr_scene_surface_try_from_buffer(scene_buffer); + + if (scene_surface) { + return scene_surface->surface; + } + + return NULL; +} + +struct sway_node *sway_node_try_from_scene_node(struct wlr_scene_node *node, + double lx, double ly) { + if (node) { struct sway_container *con = - scene_descriptor_find(scene_node, SWAY_SCENE_DESC_CONTAINER); - + scene_descriptor_find(node, SWAY_SCENE_DESC_CONTAINER); if (con) { // If this condition succeeds, the container is currently in the // process of being destroyed. In this case, ignore the container @@ -90,12 +93,12 @@ struct sway_node *node_at_coords(double lx, double ly, // if we clicked on a layer shell or unmanaged xwayland we don't // want to return the workspace node. - if (scene_descriptor_find(scene_node, SWAY_SCENE_DESC_LAYER_SHELL)) { + if (scene_descriptor_find(node, SWAY_SCENE_DESC_LAYER_SHELL)) { return NULL; } #if WLR_HAS_XWAYLAND - if (scene_descriptor_find(scene_node, SWAY_SCENE_DESC_XWAYLAND_UNMANAGED)) { + if (scene_descriptor_find(node, SWAY_SCENE_DESC_XWAYLAND_UNMANAGED)) { return NULL; } #endif @@ -122,6 +125,18 @@ struct sway_node *node_at_coords(double lx, double ly, return &ws->node; } +/** + * Returns the node at the cursor's position. If there is a surface at that + * location, it is stored in **surface (it may not be a view). + */ +struct sway_node *node_at_coords(double lx, double ly, + struct wlr_surface **surface, double *sx, double *sy) { + struct wlr_scene_node *scene_node = scene_node_at_coords(lx, ly, sx, sy); + *surface = surface_try_from_scene_node(scene_node); + + return sway_node_try_from_scene_node(scene_node, lx, ly); +} + void cursor_rebase(struct sway_cursor *cursor) { uint32_t time_msec = get_current_time_msec(); seatop_rebase(cursor->seat, time_msec);