diff --git a/include/sway/container.h b/include/sway/container.h index 08a98ed9..0e1cc8a3 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "list.h" typedef struct sway_container swayc_t; @@ -136,4 +137,7 @@ swayc_t *destroy_view(swayc_t *view); swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type); +swayc_t *swayc_at(swayc_t *parent, double lx, double ly, + struct wlr_surface **surface, double *sx, double *sy); + #endif diff --git a/include/sway/input/cursor.h b/include/sway/input/cursor.h index aa873f46..cc529de6 100644 --- a/include/sway/input/cursor.h +++ b/include/sway/input/cursor.h @@ -4,9 +4,12 @@ #include "sway/input/seat.h" struct sway_cursor { + struct sway_seat *seat; struct wlr_cursor *cursor; struct wlr_xcursor_manager *xcursor_manager; + double x, y; + struct wl_listener motion; struct wl_listener motion_absolute; struct wl_listener button; diff --git a/sway/desktop/output.c b/sway/desktop/output.c index d2003834..0e7f7060 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -25,8 +25,8 @@ static void output_frame_view(swayc_t *view, void *data) { } // TODO // - Deal with wlr_output_layout - int width = sway_view->width; - int height = sway_view->height; + int width = sway_view->surface->current->width; + int height = sway_view->surface->current->height; int render_width = width * wlr_output->scale; int render_height = height * wlr_output->scale; double ox = view->x, oy = view->y; @@ -40,19 +40,33 @@ static void output_frame_view(swayc_t *view, void *data) { // return; //} + // if the shell specifies window geometry, make the top left corner of the + // window in the top left corner of the container to avoid arbitrarily + // sized gaps based on the attached buffer size + int window_offset_x = 0; + int window_offset_y = 0; + + if (view->sway_view->type == SWAY_XDG_SHELL_V6_VIEW) { + window_offset_x = view->sway_view->wlr_xdg_surface_v6->geometry->x; + window_offset_y = view->sway_view->wlr_xdg_surface_v6->geometry->y; + } + // TODO double rotation = 0; float matrix[16]; float translate_origin[16]; wlr_matrix_translate(&translate_origin, - (int)ox + render_width / 2, (int)oy + render_height / 2, 0); + (int)ox + render_width / 2 - window_offset_x, + (int)oy + render_height / 2 - window_offset_y, + 0); float rotate[16]; wlr_matrix_rotate(&rotate, rotation); float translate_center[16]; - wlr_matrix_translate(&translate_center, -render_width / 2, + wlr_matrix_translate(&translate_center, + -render_width / 2, -render_height / 2, 0); float scale[16]; @@ -122,10 +136,10 @@ void output_add_notify(struct wl_listener *listener, void *data) { output->resolution.notify = output_resolution_notify; wl_signal_add(&wlr_output->events.resolution, &output->resolution); - for (int i = 0; i < server->input->seats->length; ++i) { - struct sway_seat *seat = server->input->seats->items[i]; - sway_seat_configure_xcursor(seat); - } + for (int i = 0; i < server->input->seats->length; ++i) { + struct sway_seat *seat = server->input->seats->items[i]; + sway_seat_configure_xcursor(seat); + } arrange_windows(output->swayc, -1, -1); } diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 4f0344be..059f907d 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -2,16 +2,44 @@ #include #include #include "sway/input/cursor.h" +#include "sway/view.h" +#include "list.h" #include "log.h" +static void cursor_update_position(struct sway_cursor *cursor) { + double x = cursor->cursor->x; + double y = cursor->cursor->y; + + wlr_xcursor_manager_set_cursor_image(cursor->xcursor_manager, + "left_ptr", cursor->cursor); + + cursor->x = x; + cursor->y = y; +} + +static void cursor_send_pointer_motion(struct sway_cursor *cursor, + uint32_t time) { + struct wlr_seat *seat = cursor->seat->seat; + struct wlr_surface *surface = NULL; + double sx, sy; + swayc_t *swayc = + swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); + if (swayc) { + wlr_seat_pointer_enter(seat, surface, sx, sy); + wlr_seat_pointer_notify_motion(seat, time, sx, sy); + } else { + wlr_seat_pointer_clear_focus(seat); + } +} + static void handle_cursor_motion(struct wl_listener *listener, void *data) { struct sway_cursor *cursor = wl_container_of(listener, cursor, motion); struct wlr_event_pointer_motion *event = data; - sway_log(L_DEBUG, "TODO: handle cursor motion event: dx=%f, dy=%f", event->delta_x, event->delta_y); - wlr_cursor_move(cursor->cursor, event->device, event->delta_x, event->delta_y); - sway_log(L_DEBUG, "TODO: new x=%f, y=%f", cursor->cursor->x, cursor->cursor->y); - wlr_xcursor_manager_set_cursor_image(cursor->xcursor_manager, "left_ptr", cursor->cursor); + wlr_cursor_move(cursor->cursor, event->device, + event->delta_x, event->delta_y); + cursor_update_position(cursor); + cursor_send_pointer_motion(cursor, event->time_msec); } static void handle_cursor_motion_absolute(struct wl_listener *listener, @@ -19,7 +47,10 @@ static void handle_cursor_motion_absolute(struct wl_listener *listener, struct sway_cursor *cursor = wl_container_of(listener, cursor, motion_absolute); struct wlr_event_pointer_motion_absolute *event = data; - sway_log(L_DEBUG, "TODO: handle event: %p", event); + wlr_cursor_warp_absolute(cursor->cursor, event->device, + event->x_mm / event->width_mm, event->y_mm / event->height_mm); + cursor_update_position(cursor); + cursor_send_pointer_motion(cursor, event->time_msec); } static void handle_cursor_button(struct wl_listener *listener, void *data) { @@ -91,6 +122,7 @@ struct sway_cursor *sway_cursor_create(struct sway_seat *seat) { return NULL; } + cursor->seat = seat; wlr_cursor_attach_output_layout(wlr_cursor, root_container.output_layout); // input events diff --git a/sway/tree/container.c b/sway/tree/container.c index e205fbcf..321ef8b1 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "sway/container.h" #include "sway/layout.h" #include "sway/output.h" @@ -168,3 +169,62 @@ swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type) { } while (container && container->type != type); return container; } + +swayc_t *swayc_at(swayc_t *parent, double lx, double ly, + struct wlr_surface **surface, double *sx, double *sy) { + list_t *queue = create_list(); + list_add(queue, parent); + + swayc_t *swayc = NULL; + while (queue->length) { + swayc = queue->items[0]; + list_del(queue, 0); + if (swayc->type == C_VIEW) { + struct sway_view *sview = swayc->sway_view; + swayc_t *soutput = swayc_parent_by_type(swayc, C_OUTPUT); + struct wlr_box *output_box = + wlr_output_layout_get_box(root_container.output_layout, + soutput->sway_output->wlr_output); + double ox = lx - output_box->x; + double oy = ly - output_box->y; + double view_sx = ox - swayc->x; + double view_sy = oy - swayc->y; + int width = swayc->sway_view->surface->current->width; + int height = swayc->sway_view->surface->current->height; + + // TODO popups and subsurfaces + switch (sview->type) { + case SWAY_WL_SHELL_VIEW: + break; + case SWAY_XDG_SHELL_V6_VIEW: + // the top left corner of the sway container is the + // coordinate of the top left corner of the window geometry + view_sx += sview->wlr_xdg_surface_v6->geometry->x; + view_sy += sview->wlr_xdg_surface_v6->geometry->y; + break; + case SWAY_XWAYLAND_VIEW: + break; + default: + break; + } + + if (view_sx > 0 && view_sx < width && + view_sy > 0 && view_sy < height && + pixman_region32_contains_point( + &sview->surface->current->input, + view_sx, view_sy, NULL)) { + *sx = view_sx; + *sy = view_sy; + *surface = swayc->sway_view->surface; + list_free(queue); + return swayc; + } + } else { + list_cat(queue, swayc->children); + } + } + + list_free(queue); + + return NULL; +}