From 03255fd20209bc26ca54ae6c1562feb0a157c5e3 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 30 Mar 2018 15:33:16 -0400 Subject: [PATCH 1/2] Fix pointer events for hidden workspaces --- sway/input/cursor.c | 83 +++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/sway/input/cursor.c b/sway/input/cursor.c index d57ac3e3..4abc61ea 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -6,10 +6,11 @@ #endif #include #include -#include "sway/input/cursor.h" -#include "sway/tree/view.h" #include "list.h" #include "log.h" +#include "sway/input/cursor.h" +#include "sway/output.h" +#include "sway/tree/view.h" static void cursor_update_position(struct sway_cursor *cursor) { double x = cursor->cursor->x; @@ -19,16 +20,12 @@ static void cursor_update_position(struct sway_cursor *cursor) { cursor->y = y; } -static void cursor_send_pointer_motion(struct sway_cursor *cursor, - uint32_t time) { - struct wlr_seat *seat = cursor->seat->wlr_seat; - struct wlr_surface *surface = NULL; - double sx, sy; - +static struct sway_container *cursor_at(struct sway_cursor *cursor, + struct wlr_surface **surface, double *sx, double *sy) { // check for unmanaged views first + struct wl_list *unmanaged = &root_container.sway_root->unmanaged_views; struct sway_view *view; - wl_list_for_each_reverse(view, &root_container.sway_root->unmanaged_views, - unmanaged_view_link) { + wl_list_for_each_reverse(view, unmanaged, unmanaged_view_link) { if (view->type == SWAY_XWAYLAND_VIEW) { struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; struct wlr_box box = { @@ -39,19 +36,42 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, }; if (wlr_box_contains_point(&box, cursor->x, cursor->y)) { - surface = xsurface->surface; - sx = cursor->x - box.x; - sy = cursor->y - box.y; - wlr_seat_pointer_notify_enter(seat, surface, sx, sy); - wlr_seat_pointer_notify_motion(seat, time, sx, sy); - return; + *surface = xsurface->surface; + *sx = cursor->x - box.x; + *sy = cursor->y - box.y; + return view->swayc; } } } - struct sway_container *swayc = - container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); - if (swayc) { + // find the output the cursor is on + struct wlr_output_layout *output_layout = + root_container.sway_root->output_layout; + struct wlr_output *wlr_output = + wlr_output_layout_output_at(output_layout, cursor->x, cursor->y); + if (wlr_output == NULL) { + return NULL; + } + struct sway_output *output = wlr_output->data; + + // find the focused workspace on the output for this seat + struct sway_container *workspace = + sway_seat_get_focus_inactive(cursor->seat, output->swayc); + if (workspace->type != C_WORKSPACE) { + workspace = container_parent(workspace, C_WORKSPACE); + } + + return container_at(workspace, cursor->x, cursor->y, surface, sx, sy); +} + +static void cursor_send_pointer_motion(struct sway_cursor *cursor, + uint32_t time) { + struct wlr_seat *seat = cursor->seat->wlr_seat; + struct wlr_surface *surface = NULL; + double sx, sy; + struct sway_container *cont = cursor_at(cursor, &surface, &sx, &sy); + + if (cont) { wlr_seat_pointer_notify_enter(seat, surface, sx, sy); wlr_seat_pointer_notify_motion(seat, time, sx, sy); } else { @@ -60,8 +80,7 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, } static void handle_cursor_motion(struct wl_listener *listener, void *data) { - struct sway_cursor *cursor = - wl_container_of(listener, cursor, motion); + struct sway_cursor *cursor = wl_container_of(listener, cursor, motion); struct wlr_event_pointer_motion *event = data; wlr_cursor_move(cursor->cursor, event->device, event->delta_x, event->delta_y); @@ -80,16 +99,13 @@ static void handle_cursor_motion_absolute(struct wl_listener *listener, } static void handle_cursor_button(struct wl_listener *listener, void *data) { - struct sway_cursor *cursor = - wl_container_of(listener, cursor, button); + struct sway_cursor *cursor = wl_container_of(listener, cursor, button); struct wlr_event_pointer_button *event = data; if (event->button == BTN_LEFT) { struct wlr_surface *surface = NULL; double sx, sy; - struct sway_container *swayc = - container_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); - + struct sway_container *swayc = cursor_at(cursor, &surface, &sx, &sy); sway_seat_set_focus(cursor->seat, swayc); } @@ -98,23 +114,20 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { } static void handle_cursor_axis(struct wl_listener *listener, void *data) { - struct sway_cursor *cursor = - wl_container_of(listener, cursor, axis); + struct sway_cursor *cursor = wl_container_of(listener, cursor, axis); struct wlr_event_pointer_axis *event = data; wlr_seat_pointer_notify_axis(cursor->seat->wlr_seat, event->time_msec, event->orientation, event->delta); } static void handle_touch_down(struct wl_listener *listener, void *data) { - struct sway_cursor *cursor = - wl_container_of(listener, cursor, touch_down); + struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_down); struct wlr_event_touch_down *event = data; wlr_log(L_DEBUG, "TODO: handle touch down event: %p", event); } static void handle_touch_up(struct wl_listener *listener, void *data) { - struct sway_cursor *cursor = - wl_container_of(listener, cursor, touch_up); + struct sway_cursor *cursor = wl_container_of(listener, cursor, touch_up); struct wlr_event_touch_up *event = data; wlr_log(L_DEBUG, "TODO: handle touch up event: %p", event); } @@ -127,15 +140,13 @@ static void handle_touch_motion(struct wl_listener *listener, void *data) { } static void handle_tool_axis(struct wl_listener *listener, void *data) { - struct sway_cursor *cursor = - wl_container_of(listener, cursor, tool_axis); + struct sway_cursor *cursor = wl_container_of(listener, cursor, tool_axis); struct wlr_event_tablet_tool_axis *event = data; wlr_log(L_DEBUG, "TODO: handle tool axis event: %p", event); } static void handle_tool_tip(struct wl_listener *listener, void *data) { - struct sway_cursor *cursor = - wl_container_of(listener, cursor, tool_tip); + struct sway_cursor *cursor = wl_container_of(listener, cursor, tool_tip); struct wlr_event_tablet_tool_tip *event = data; wlr_log(L_DEBUG, "TODO: handle tool tip event: %p", event); } From eb716c6c43ab3c9f265c2629538e232c4b3de625 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 30 Mar 2018 16:12:02 -0400 Subject: [PATCH 2/2] Fix segfaults when focusing a workspace --- sway/input/cursor.c | 29 ++++++++++++++++++++--------- sway/ipc-server.c | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 4abc61ea..717d864b 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -20,7 +20,11 @@ static void cursor_update_position(struct sway_cursor *cursor) { cursor->y = y; } -static struct sway_container *cursor_at(struct sway_cursor *cursor, +/** + * Returns the container at the cursor's position. If the container is a view, + * stores the surface at the cursor's position in `*surface`. + */ +static struct sway_container *container_at_cursor(struct sway_cursor *cursor, struct wlr_surface **surface, double *sx, double *sy) { // check for unmanaged views first struct wl_list *unmanaged = &root_container.sway_root->unmanaged_views; @@ -55,13 +59,18 @@ static struct sway_container *cursor_at(struct sway_cursor *cursor, struct sway_output *output = wlr_output->data; // find the focused workspace on the output for this seat - struct sway_container *workspace = + struct sway_container *workspace_cont = sway_seat_get_focus_inactive(cursor->seat, output->swayc); - if (workspace->type != C_WORKSPACE) { - workspace = container_parent(workspace, C_WORKSPACE); + if (workspace_cont != NULL && workspace_cont->type != C_WORKSPACE) { + workspace_cont = container_parent(workspace_cont, C_WORKSPACE); + } + if (workspace_cont == NULL) { + return output->swayc; } - return container_at(workspace, cursor->x, cursor->y, surface, sx, sy); + struct sway_container *view_cont = container_at(workspace_cont, + cursor->x, cursor->y, surface, sx, sy); + return view_cont != NULL ? view_cont : workspace_cont; } static void cursor_send_pointer_motion(struct sway_cursor *cursor, @@ -69,9 +78,10 @@ static void cursor_send_pointer_motion(struct sway_cursor *cursor, struct wlr_seat *seat = cursor->seat->wlr_seat; struct wlr_surface *surface = NULL; double sx, sy; - struct sway_container *cont = cursor_at(cursor, &surface, &sx, &sy); + struct sway_container *cont = + container_at_cursor(cursor, &surface, &sx, &sy); - if (cont) { + if (cont != NULL && surface != NULL) { wlr_seat_pointer_notify_enter(seat, surface, sx, sy); wlr_seat_pointer_notify_motion(seat, time, sx, sy); } else { @@ -105,8 +115,9 @@ static void handle_cursor_button(struct wl_listener *listener, void *data) { if (event->button == BTN_LEFT) { struct wlr_surface *surface = NULL; double sx, sy; - struct sway_container *swayc = cursor_at(cursor, &surface, &sx, &sy); - sway_seat_set_focus(cursor->seat, swayc); + struct sway_container *cont = + container_at_cursor(cursor, &surface, &sx, &sy); + sway_seat_set_focus(cursor->seat, cont); } wlr_seat_pointer_notify_button(cursor->seat->wlr_seat, event->time_msec, diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 394161af..6f7a0670 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -390,7 +390,7 @@ static void ipc_get_workspaces_callback(struct sway_container *workspace, struct sway_seat *seat = sway_input_manager_get_default_seat(input_manager); struct sway_container *focused_ws = sway_seat_get_focus(seat); - if (focused_ws->type != C_WORKSPACE) { + if (focused_ws != NULL && focused_ws->type != C_WORKSPACE) { focused_ws = container_parent(focused_ws, C_WORKSPACE); } bool focused = workspace == focused_ws;