diff --git a/include/sway/layout.h b/include/sway/layout.h index bfd96a026..af5614531 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -9,6 +9,8 @@ struct sway_root { struct wlr_output_layout *output_layout; struct wl_listener output_layout_change; + + struct wl_list unmanaged_views; // sway_view::unmanaged_view_link }; void init_layout(void); diff --git a/include/sway/view.h b/include/sway/view.h index 900bd296f..08c5480b6 100644 --- a/include/sway/view.h +++ b/include/sway/view.h @@ -28,6 +28,8 @@ struct sway_xwayland_surface { struct wl_listener request_resize; struct wl_listener request_maximize; struct wl_listener request_configure; + struct wl_listener unmap_notify; + struct wl_listener map_notify; struct wl_listener destroy; int pending_width, pending_height; @@ -91,6 +93,9 @@ struct sway_view { double ox, double oy); void (*set_activated)(struct sway_view *view, bool activated); } iface; + + // only used for unmanaged views (shell specific) + struct wl_list unmanaged_view_link; // sway_root::unmanaged views }; #endif diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 2b428c30b..0f00222b3 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -1,9 +1,11 @@ #define _POSIX_C_SOURCE 200809L +#include #include #include #include #include #include +#include #include #include #include "log.h" @@ -15,76 +17,198 @@ #include "sway/input/input-manager.h" #include "sway/input/seat.h" +/** + * Rotate a child's position relative to a parent. The parent size is (pw, ph), + * the child position is (*sx, *sy) and its size is (sw, sh). + */ +static void rotate_child_position(double *sx, double *sy, double sw, double sh, + double pw, double ph, float rotation) { + if (rotation != 0.0) { + // Coordinates relative to the center of the subsurface + double ox = *sx - pw/2 + sw/2, + oy = *sy - ph/2 + sh/2; + // Rotated coordinates + double rx = cos(-rotation)*ox - sin(-rotation)*oy, + ry = cos(-rotation)*oy + sin(-rotation)*ox; + *sx = rx + pw/2 - sw/2; + *sy = ry + ph/2 - sh/2; + } +} + +static void render_surface(struct wlr_surface *surface, + struct wlr_output *wlr_output, struct timespec *when, + double lx, double ly, float rotation) { + if (!wlr_surface_has_buffer(surface)) { + return; + } + struct wlr_output_layout *layout = root_container.sway_root->output_layout; + int width = surface->current->width; + int height = surface->current->height; + int render_width = width * wlr_output->scale; + int render_height = height * wlr_output->scale; + double ox = lx, oy = ly; + wlr_output_layout_output_coords(layout, wlr_output, &ox, &oy); + ox *= wlr_output->scale; + oy *= wlr_output->scale; + + struct wlr_box render_box = { + .x = lx, .y = ly, + .width = render_width, .height = render_height, + }; + if (wlr_output_layout_intersects(layout, wlr_output, &render_box)) { + float matrix[16]; + + float translate_center[16]; + wlr_matrix_translate(&translate_center, + (int)ox + render_width / 2, (int)oy + render_height / 2, 0); + + float rotate[16]; + wlr_matrix_rotate(&rotate, rotation); + + float translate_origin[16]; + wlr_matrix_translate(&translate_origin, -render_width / 2, + -render_height / 2, 0); + + float scale[16]; + wlr_matrix_scale(&scale, render_width, render_height, 1); + + float transform[16]; + wlr_matrix_mul(&translate_center, &rotate, &transform); + wlr_matrix_mul(&transform, &translate_origin, &transform); + wlr_matrix_mul(&transform, &scale, &transform); + + if (surface->current->transform != WL_OUTPUT_TRANSFORM_NORMAL) { + float surface_translate_center[16]; + wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0); + + float surface_transform[16]; + wlr_matrix_transform(surface_transform, + wlr_output_transform_invert(surface->current->transform)); + + float surface_translate_origin[16]; + wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); + + wlr_matrix_mul(&transform, &surface_translate_center, + &transform); + wlr_matrix_mul(&transform, &surface_transform, &transform); + wlr_matrix_mul(&transform, &surface_translate_origin, + &transform); + } + + wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); + + wlr_render_with_matrix(server.renderer, surface->texture, + &matrix); + + wlr_surface_send_frame_done(surface, when); + } + + struct wlr_subsurface *subsurface; + wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) { + struct wlr_surface_state *state = subsurface->surface->current; + double sx = state->subsurface_position.x; + double sy = state->subsurface_position.y; + double sw = state->buffer_width / state->scale; + double sh = state->buffer_height / state->scale; + rotate_child_position(&sx, &sy, sw, sh, width, height, rotation); + + render_surface(subsurface->surface, wlr_output, when, + lx + sx, + ly + sy, + rotation); + } +} + +static void render_xdg_v6_popups(struct wlr_xdg_surface_v6 *surface, + struct wlr_output *wlr_output, struct timespec *when, double base_x, + double base_y, float rotation) { + double width = surface->surface->current->width; + double height = surface->surface->current->height; + + struct wlr_xdg_surface_v6 *popup; + wl_list_for_each(popup, &surface->popups, popup_link) { + if (!popup->configured) { + continue; + } + + double popup_width = popup->surface->current->width; + double popup_height = popup->surface->current->height; + + double popup_sx, popup_sy; + wlr_xdg_surface_v6_popup_get_position(popup, &popup_sx, &popup_sy); + rotate_child_position(&popup_sx, &popup_sy, popup_width, popup_height, + width, height, rotation); + + render_surface(popup->surface, wlr_output, when, + base_x + popup_sx, base_y + popup_sy, rotation); + render_xdg_v6_popups(popup, wlr_output, when, + base_x + popup_sx, base_y + popup_sy, rotation); + } +} + +static void render_wl_shell_surface(struct wlr_wl_shell_surface *surface, + struct wlr_output *wlr_output, struct timespec *when, + double lx, double ly, float rotation, + bool is_child) { + if (is_child || surface->state != WLR_WL_SHELL_SURFACE_STATE_POPUP) { + render_surface(surface->surface, wlr_output, when, + lx, ly, rotation); + + double width = surface->surface->current->width; + double height = surface->surface->current->height; + + struct wlr_wl_shell_surface *popup; + wl_list_for_each(popup, &surface->popups, popup_link) { + double popup_width = popup->surface->current->width; + double popup_height = popup->surface->current->height; + + double popup_x = popup->transient_state->x; + double popup_y = popup->transient_state->y; + rotate_child_position(&popup_x, &popup_y, popup_width, popup_height, + width, height, rotation); + + render_wl_shell_surface(popup, wlr_output, when, + lx + popup_x, ly + popup_y, rotation, true); + } + } +} + + static void output_frame_view(swayc_t *view, void *data) { struct sway_output *output = data; struct wlr_output *wlr_output = output->wlr_output; struct sway_view *sway_view = view->sway_view; struct wlr_surface *surface = sway_view->surface; - if (!wlr_surface_has_buffer(surface)) { + + if (!surface) { return; } - // TODO - // - Deal with wlr_output_layout - 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; - // TODO - //wlr_output_layout_output_coords(desktop->layout, wlr_output, &ox, &oy); - ox *= wlr_output->scale; - oy *= wlr_output->scale; - // TODO - //if (wlr_output_layout_intersects(desktop->layout, wlr_output, - // lx, ly, lx + render_width, ly + render_height)) { - // 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; + switch (sway_view->type) { + case SWAY_XDG_SHELL_V6_VIEW: { + int window_offset_x = view->sway_view->wlr_xdg_surface_v6->geometry->x; + int window_offset_y = view->sway_view->wlr_xdg_surface_v6->geometry->y; + render_surface(surface, wlr_output, &output->last_frame, + view->x - window_offset_x, + view->y - window_offset_y, + 0); + render_xdg_v6_popups(sway_view->wlr_xdg_surface_v6, wlr_output, + &output->last_frame, + view->x - window_offset_x, view->y - window_offset_y, + 0); + break; + } + case SWAY_WL_SHELL_VIEW: + render_wl_shell_surface(sway_view->wlr_wl_shell_surface, wlr_output, + &output->last_frame, view->x, view->y, 0, false); + break; + case SWAY_XWAYLAND_VIEW: + render_surface(surface, wlr_output, &output->last_frame, view->x, + view->y, 0); + break; + default: + break; } - - // TODO - double rotation = 0; - float matrix[16]; - - float translate_origin[16]; - wlr_matrix_translate(&translate_origin, - (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, - -render_height / 2, 0); - - float scale[16]; - wlr_matrix_scale(&scale, render_width, render_height, 1); - - float transform[16]; - wlr_matrix_mul(&translate_origin, &rotate, &transform); - wlr_matrix_mul(&transform, &translate_center, &transform); - wlr_matrix_mul(&transform, &scale, &transform); - wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); - - wlr_render_with_matrix(output->server->renderer, surface->texture, &matrix); - - // TODO: move into wlroots - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - - wlr_surface_send_frame_done(surface, &now); } static void output_frame_notify(struct wl_listener *listener, void *data) { @@ -92,18 +216,30 @@ static void output_frame_notify(struct wl_listener *listener, void *data) { struct wlr_output *wlr_output = data; struct sway_server *server = soutput->server; - struct timespec now; - clock_gettime(CLOCK_MONOTONIC, &now); - wlr_output_make_current(wlr_output); wlr_renderer_begin(server->renderer, wlr_output); swayc_descendants_of_type( &root_container, C_VIEW, output_frame_view, soutput); + // render unmanaged views on top + struct sway_view *view; + wl_list_for_each(view, &root_container.sway_root->unmanaged_views, + unmanaged_view_link) { + if (view->type == SWAY_XWAYLAND_VIEW) { + // the only kind of unamanged view right now is xwayland override redirect + int view_x = view->wlr_xwayland_surface->x; + int view_y = view->wlr_xwayland_surface->y; + render_surface(view->surface, wlr_output, &soutput->last_frame, + view_x, view_y, 0); + } + } + wlr_renderer_end(server->renderer); wlr_output_swap_buffers(wlr_output); + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); soutput->last_frame = now; } diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c index 345a1398c..e34f51608 100644 --- a/sway/desktop/wl_shell.c +++ b/sway/desktop/wl_shell.c @@ -77,11 +77,13 @@ void handle_wl_shell_surface(struct wl_listener *listener, void *data) { listener, server, wl_shell_surface); struct wlr_wl_shell_surface *shell_surface = data; - if (shell_surface->state != WLR_WL_SHELL_SURFACE_STATE_TOPLEVEL) { - // TODO: transient and popups should be floating + if (shell_surface->state == WLR_WL_SHELL_SURFACE_STATE_POPUP) { + // popups don't get views return; } + // TODO make transient windows floating + wlr_log(L_DEBUG, "New wl_shell toplevel title='%s' app_id='%s'", shell_surface->title, shell_surface->class); wlr_wl_shell_surface_ping(shell_surface); diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c index 43bb2e006..a4d9687d4 100644 --- a/sway/desktop/xwayland.c +++ b/sway/desktop/xwayland.c @@ -93,13 +93,68 @@ static void handle_commit(struct wl_listener *listener, void *data) { static void handle_destroy(struct wl_listener *listener, void *data) { struct sway_xwayland_surface *sway_surface = wl_container_of(listener, sway_surface, destroy); + struct wlr_xwayland_surface *xsurface = data; wl_list_remove(&sway_surface->commit.link); wl_list_remove(&sway_surface->destroy.link); wl_list_remove(&sway_surface->request_configure.link); + if (xsurface->override_redirect) { + if (xsurface->mapped) { + wl_list_remove(&sway_surface->view->unmanaged_view_link); + } + } + swayc_t *parent = destroy_view(sway_surface->view->swayc); + if (parent) { + arrange_windows(parent, -1, -1); + } + free(sway_surface->view); free(sway_surface); - arrange_windows(parent, -1, -1); +} + +static void handle_unmap_notify(struct wl_listener *listener, void *data) { + struct sway_xwayland_surface *sway_surface = + wl_container_of(listener, sway_surface, unmap_notify); + struct wlr_xwayland_surface *xsurface = data; + if (xsurface->override_redirect) { + wl_list_remove(&sway_surface->view->unmanaged_view_link); + } + + // take it out of the tree + swayc_t *parent = destroy_view(sway_surface->view->swayc); + if (parent) { + arrange_windows(parent, -1, -1); + } + + sway_surface->view->swayc = NULL; + sway_surface->view->surface = NULL; +} + +static void handle_map_notify(struct wl_listener *listener, void *data) { + // TODO put the view back into the tree + struct sway_xwayland_surface *sway_surface = + wl_container_of(listener, sway_surface, map_notify); + struct wlr_xwayland_surface *xsurface = data; + + sway_surface->view->surface = xsurface->surface; + + // put it back into the tree + if (xsurface->override_redirect) { + wl_list_insert(&root_container.sway_root->unmanaged_views, + &sway_surface->view->unmanaged_view_link); + } else { + struct sway_view *view = sway_surface->view; + destroy_view(view->swayc); + + swayc_t *parent = root_container.children->items[0]; + parent = parent->children->items[0]; // workspace + + swayc_t *cont = new_view(parent, view); + view->swayc = cont; + + arrange_windows(cont->parent, -1, -1); + sway_input_manager_set_focus(input_manager, cont); + } } static void handle_configure_request(struct wl_listener *listener, void *data) { @@ -119,11 +174,6 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { listener, server, xwayland_surface); struct wlr_xwayland_surface *xsurface = data; - if (xsurface->override_redirect) { - // TODO: floating popups - return; - } - wlr_log(L_DEBUG, "New xwayland surface title='%s' class='%s'", xsurface->title, xsurface->class); @@ -144,26 +194,37 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) { sway_view->iface.set_activated = set_activated; sway_view->wlr_xwayland_surface = xsurface; sway_view->sway_xwayland_surface = sway_surface; - // TODO remove from the tree when the surface goes away (unmapped) sway_view->surface = xsurface->surface; sway_surface->view = sway_view; // TODO: - // - Wire up listeners - // - Handle popups // - Look up pid and open on appropriate workspace // - Set new view to maximized so it behaves nicely // - Criteria - sway_surface->commit.notify = handle_commit; wl_signal_add(&xsurface->surface->events.commit, &sway_surface->commit); - sway_surface->destroy.notify = handle_destroy; + sway_surface->commit.notify = handle_commit; + wl_signal_add(&xsurface->events.destroy, &sway_surface->destroy); - sway_surface->request_configure.notify = handle_configure_request; + sway_surface->destroy.notify = handle_destroy; + wl_signal_add(&xsurface->events.request_configure, &sway_surface->request_configure); + sway_surface->request_configure.notify = handle_configure_request; + + wl_signal_add(&xsurface->events.unmap_notify, &sway_surface->unmap_notify); + sway_surface->unmap_notify.notify = handle_unmap_notify; + + wl_signal_add(&xsurface->events.map_notify, &sway_surface->map_notify); + sway_surface->map_notify.notify = handle_map_notify; + + if (xsurface->override_redirect) { + // these don't get a container in the tree + wl_list_insert(&root_container.sway_root->unmanaged_views, + &sway_view->unmanaged_view_link); + return; + } - // TODO: actual focus semantics swayc_t *parent = root_container.children->items[0]; parent = parent->children->items[0]; // workspace diff --git a/sway/input/cursor.c b/sway/input/cursor.c index c51b59f99..e6a4eca8a 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -24,6 +24,31 @@ 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; + + // check for unmanaged views first + struct sway_view *view; + wl_list_for_each_reverse(view, &root_container.sway_root->unmanaged_views, + unmanaged_view_link) { + if (view->type == SWAY_XWAYLAND_VIEW) { + struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface; + struct wlr_box box = { + .x = xsurface->x, + .y = xsurface->y, + .width = xsurface->width, + .height = xsurface->height, + }; + + 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; + } + } + } + swayc_t *swayc = swayc_at(&root_container, cursor->x, cursor->y, &surface, &sx, &sy); if (swayc) { diff --git a/sway/tree/container.c b/sway/tree/container.c index 31ec2ce50..d241f69a4 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -207,7 +207,7 @@ swayc_t *destroy_output(swayc_t *output) { } swayc_t *destroy_view(swayc_t *view) { - if (!sway_assert(view, "null view passed to destroy_view")) { + if (!view) { return NULL; } wlr_log(L_DEBUG, "Destroying view '%s'", view->name); @@ -259,7 +259,6 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, 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; @@ -268,6 +267,20 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, // 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; + + // check for popups + double popup_sx, popup_sy; + struct wlr_xdg_surface_v6 *popup = + wlr_xdg_surface_v6_popup_at(sview->wlr_xdg_surface_v6, + view_sx, view_sy, &popup_sx, &popup_sy); + + if (popup) { + *sx = view_sx - popup_sx; + *sy = view_sy - popup_sy; + *surface = popup->surface; + list_free(queue); + return swayc; + } break; case SWAY_XWAYLAND_VIEW: break; @@ -275,6 +288,19 @@ swayc_t *swayc_at(swayc_t *parent, double lx, double ly, break; } + // check for subsurfaces + double sub_x, sub_y; + struct wlr_subsurface *subsurface = + wlr_surface_subsurface_at(sview->surface, + view_sx, view_sy, &sub_x, &sub_y); + if (subsurface) { + *sx = view_sx - sub_x; + *sy = view_sy - sub_y; + *surface = subsurface->surface; + list_free(queue); + return swayc; + } + if (view_sx > 0 && view_sx < width && view_sy > 0 && view_sy < height && pixman_region32_contains_point( diff --git a/sway/tree/layout.c b/sway/tree/layout.c index 13b8a3951..01535f2de 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -51,6 +51,7 @@ void init_layout(void) { root_container.sway_root = calloc(1, sizeof(*root_container.sway_root)); root_container.sway_root->output_layout = wlr_output_layout_create(); + wl_list_init(&root_container.sway_root->unmanaged_views); root_container.sway_root->output_layout_change.notify = output_layout_change_notify;