From 874c844ce0c2ac41ffec84306a3445d1cdac2912 Mon Sep 17 00:00:00 2001 From: Keith Bowes Date: Wed, 19 Feb 2020 18:43:39 -0500 Subject: [PATCH] Replaced the old mcwayface rendering code with tinywl's xdg_shell code, allowing it to compile with wlroots 0.9.0+ --- README.md | 5 -- include/waybox/output.h | 14 ++++ include/waybox/server.h | 16 ++++ include/waybox/xdg_shell.h | 3 + waybox/meson.build | 1 + waybox/output.c | 112 ++++++++++++++++++++++++--- waybox/server.c | 6 +- waybox/xdg_shell.c | 150 +++++++++++++++++++++++++++++++++++++ 8 files changed, 291 insertions(+), 16 deletions(-) create mode 100644 include/waybox/xdg_shell.h create mode 100644 waybox/xdg_shell.c diff --git a/README.md b/README.md index 6686135..614f1d7 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,6 @@ # waybox An openbox clone on Wayland (WIP) -This is the basic-apps branch, in which I'm working to get simple apps to work. -Currently, I'm targeting wlroots 0.6.0, but after I get the basic functionality -worked out, I want to get it to compile with wlroots 0.10.0. Right now, a -weston-terminal window will show, but it won't receive keyboard input. - ### Goals The main goal of this project is to provide a similar feel to openbox but on wayland diff --git a/include/waybox/output.h b/include/waybox/output.h index b68e240..0a14df8 100644 --- a/include/waybox/output.h +++ b/include/waybox/output.h @@ -28,6 +28,20 @@ struct wb_output { struct wl_list link; }; +struct wb_view { + struct wl_list link; + struct wb_server *server; + struct wlr_xdg_surface *xdg_surface; + + struct wl_listener map; + struct wl_listener unmap; + struct wl_listener destroy; + struct wl_listener request_move; + struct wl_listener request_resize; + bool mapped; + int x, y; +}; + void output_frame_notify(struct wl_listener* listener, void *data); void output_destroy_notify(struct wl_listener* listener, void *data); void new_output_notify(struct wl_listener* listener, void *data); diff --git a/include/waybox/server.h b/include/waybox/server.h index 6636b74..72bd238 100644 --- a/include/waybox/server.h +++ b/include/waybox/server.h @@ -27,6 +27,12 @@ #include "waybox/cursor.h" #include "waybox/seat.h" +enum wb_cursor_mode { + WB_CURSOR_PASSTHROUGH, + WB_CURSOR_MOVE, + WB_CURSOR_RESIZE, +}; + struct wb_server { struct wl_display *wl_display; struct wl_event_loop *wl_event_loop; @@ -38,6 +44,16 @@ struct wb_server { struct wb_cursor *cursor; struct wb_seat * seat; + enum wb_cursor_mode cursor_mode; + struct wb_view *grabbed_view; + double grab_x, grab_y; + int grab_width, grab_height; + uint32_t resize_edges; + + struct wlr_xdg_shell *xdg_shell; + struct wl_listener new_xdg_surface; + struct wl_list views; + struct wl_listener new_output; struct wl_listener new_input; struct wl_list outputs; // wb_output::link diff --git a/include/waybox/xdg_shell.h b/include/waybox/xdg_shell.h new file mode 100644 index 0000000..a53c413 --- /dev/null +++ b/include/waybox/xdg_shell.h @@ -0,0 +1,3 @@ +#include "waybox/server.h" + +void init_xdg_shell(struct wb_server *server); diff --git a/waybox/meson.build b/waybox/meson.build index b341eb0..9a05460 100644 --- a/waybox/meson.build +++ b/waybox/meson.build @@ -4,6 +4,7 @@ wb_src = files( 'output.c', 'seat.c', 'server.c', + 'xdg_shell.c', ) wb_dep = [ diff --git a/waybox/output.c b/waybox/output.c index b8a4332..b728471 100644 --- a/waybox/output.c +++ b/waybox/output.c @@ -1,5 +1,74 @@ #include +struct render_data { + struct wlr_output *output; + struct wlr_renderer *renderer; + struct wb_view *view; + struct timespec *when; +}; + +static void render_surface(struct wlr_surface *surface, int sx, int sy, void *data) { + /* This function is called for every surface that needs to be rendered. */ + struct render_data *rdata = data; + struct wb_view *view = rdata->view; + struct wlr_output *output = rdata->output; + + /* We first obtain a wlr_texture, which is a GPU resource. wlroots + * automatically handles negotiating these with the client. The underlying + * resource could be an opaque handle passed from the client, or the client + * could have sent a pixel buffer which we copied to the GPU, or a few other + * means. You don't have to worry about this, wlroots takes care of it. */ + struct wlr_texture *texture = wlr_surface_get_texture(surface); + if (texture == NULL) { + return; + } + + /* The view has a position in layout coordinates. If you have two displays, + * one next to the other, both 1080p, a view on the rightmost display might + * have layout coordinates of 2000,100. We need to translate that to + * output-local coordinates, or (2000 - 1920). */ + double ox = 0, oy = 0; + wlr_output_layout_output_coords( + view->server->layout, output, &ox, &oy); + ox += view->x + sx, oy += view->y + sy; + + /* We also have to apply the scale factor for HiDPI outputs. This is only + * part of the puzzle, TinyWL does not fully support HiDPI. */ + struct wlr_box box = { + .x = ox * output->scale, + .y = oy * output->scale, + .width = surface->current.width * output->scale, + .height = surface->current.height * output->scale, + }; + + /* + * Those familiar with OpenGL are also familiar with the role of matricies + * in graphics programming. We need to prepare a matrix to render the view + * with. wlr_matrix_project_box is a helper which takes a box with a desired + * x, y coordinates, width and height, and an output geometry, then + * prepares an orthographic projection and multiplies the necessary + * transforms to produce a model-view-projection matrix. + * + * Naturally you can do this any way you like, for example to make a 3D + * compositor. + */ + float matrix[9]; + enum wl_output_transform transform = + wlr_output_transform_invert(surface->current.transform); + wlr_matrix_project_box(matrix, &box, transform, 0, + output->transform_matrix); + + /* This takes our matrix, the texture, and an alpha, and performs the actual + * rendering on the GPU. */ + wlr_render_texture_with_matrix(rdata->renderer, texture, matrix, 1); + + /* This lets the client know that we've displayed that frame and it can + * prepare another one now if it likes. */ + wlr_surface_send_frame_done(surface, rdata->when); +} + + + void output_frame_notify(struct wl_listener *listener, void *data) { struct wb_output *output = wl_container_of(listener, output, frame); // struct wlr_backend *backend = output->server->backend; @@ -10,13 +79,34 @@ void output_frame_notify(struct wl_listener *listener, void *data) { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - wlr_output_attach_render(wlr_output, NULL); - wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height); + if (!wlr_output_attach_render(wlr_output, NULL)) { + return; + } + int width, height; + wlr_output_effective_resolution(output->wlr_output, &width, &height); + wlr_renderer_begin(renderer, width, height); float color[4] = {0.4f, 0.4f, 0.4f, 1.0f}; wlr_renderer_clear(renderer, color); - struct wl_resource *_surface; + struct wb_view *view; + wl_list_for_each_reverse(view, &output->server->views, link) + { + if (!view->mapped) + /* An unmapped view should not be rendered */ + continue; + + struct render_data rdata = { + .output = output->wlr_output, + .renderer = renderer, + .view = view, + .when = &now, + }; + + wlr_xdg_surface_for_each_surface(view->xdg_surface, render_surface, &rdata); + } + + /*struct wl_resource *_surface; wl_resource_for_each(_surface, &output->server->compositor->surface_resources) { struct wlr_surface *surface = wlr_surface_from_resource(_surface); if (!wlr_surface_has_buffer(surface)) { @@ -33,10 +123,10 @@ void output_frame_notify(struct wl_listener *listener, void *data) { struct wlr_texture *texture = wlr_surface_get_texture(surface); wlr_render_texture_with_matrix(renderer, texture, matrix, 1.0f); wlr_surface_send_frame_done(surface, &now); - } + }*/ - wlr_output_commit(wlr_output); wlr_renderer_end(renderer); + wlr_output_commit(wlr_output); output->last_frame = now; } @@ -56,11 +146,14 @@ void new_output_notify(struct wl_listener *listener, void *data) { struct wlr_output *wlr_output = data; if (!wl_list_empty(&wlr_output->modes)) { - struct wlr_output_mode *mode = - wl_container_of(wlr_output->modes.prev, mode, link); + struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output); wlr_output_set_mode(wlr_output, mode); + wlr_output_enable(wlr_output, true); - wlr_output_create_global(wlr_output); + if (!wlr_output_commit(wlr_output)) { + return; + } + //wlr_output_create_global(wlr_output); } struct wb_output *output = calloc(1, sizeof(struct wb_output)); @@ -74,7 +167,8 @@ void new_output_notify(struct wl_listener *listener, void *data) { output->frame.notify = output_frame_notify; wl_signal_add(&wlr_output->events.frame, &output->frame); - wlr_output_layout_add_auto(server->layout, output->wlr_output); + wlr_output_layout_add_auto(server->layout, wlr_output); + wlr_output_create_global(wlr_output); wlr_xcursor_manager_load(server->cursor->xcursor_manager, output->wlr_output->scale); wlr_xcursor_manager_set_cursor_image(server->cursor->xcursor_manager, "left_ptr", server->cursor->cursor); } diff --git a/waybox/server.c b/waybox/server.c index 2d8a91a..5834d5b 100644 --- a/waybox/server.c +++ b/waybox/server.c @@ -1,5 +1,6 @@ -#include "waybox/server.h" #include "waybox/seat.h" +#include "waybox/xdg_shell.h" + bool init_wb(struct wb_server* server) { @@ -59,7 +60,8 @@ bool start_wb(struct wb_server* server) { server->compositor = wlr_compositor_create(server->wl_display, wlr_backend_get_renderer(server->backend)); wlr_data_device_manager_create(server->wl_display); - wlr_xdg_shell_create(server->wl_display); + wl_list_init(&server->views); + init_xdg_shell(server); //wlr_idle_create(server->wl_display); return true; diff --git a/waybox/xdg_shell.c b/waybox/xdg_shell.c new file mode 100644 index 0000000..c9a89fb --- /dev/null +++ b/waybox/xdg_shell.c @@ -0,0 +1,150 @@ +#include "waybox/xdg_shell.h" + +static void focus_view(struct wb_view *view, struct wlr_surface *surface) { + /* Note: this function only deals with keyboard focus. */ + if (view == NULL) { + return; + } + struct wb_server *server = view->server; + struct wlr_seat *seat = server->seat->seat; + struct wlr_surface *prev_surface = seat->keyboard_state.focused_surface; + if (prev_surface == surface) { + /* Don't re-focus an already focused surface. */ + return; + } + if (prev_surface) { + /* + * Deactivate the previously focused surface. This lets the client know + * it no longer has focus and the client will repaint accordingly, e.g. + * stop displaying a caret. + */ + struct wlr_xdg_surface *previous = wlr_xdg_surface_from_wlr_surface( + seat->keyboard_state.focused_surface); + wlr_xdg_toplevel_set_activated(previous, false); + } + struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat); + /* Move the view to the front */ + wl_list_remove(&view->link); + wl_list_insert(&server->views, &view->link); + /* Activate the new surface */ + wlr_xdg_toplevel_set_activated(view->xdg_surface, true); + /* + * Tell the seat to have the keyboard enter this surface. wlroots will keep + * track of this and automatically send key events to the appropriate + * clients without additional work on your part. + */ + wlr_seat_keyboard_notify_enter(seat, view->xdg_surface->surface, + keyboard->keycodes, keyboard->num_keycodes, &keyboard->modifiers); +} + +static void xdg_surface_map(struct wl_listener *listener, void *data) { + /* Called when the surface is mapped, or ready to display on-screen. */ + struct wb_view *view = wl_container_of(listener, view, map); + view->mapped = true; + focus_view(view, view->xdg_surface->surface); +} + +static void xdg_surface_unmap(struct wl_listener *listener, void *data) { + /* Called when the surface is unmapped, and should no longer be shown. */ + struct wb_view *view = wl_container_of(listener, view, unmap); + view->mapped = false; +} + +static void xdg_surface_destroy(struct wl_listener *listener, void *data) { + /* Called when the surface is destroyed and should never be shown again. */ + struct wb_view *view = wl_container_of(listener, view, destroy); + wl_list_remove(&view->link); + free(view); +} + +static void begin_interactive(struct wb_view *view, + enum wb_cursor_mode mode, uint32_t edges) { + /* This function sets up an interactive move or resize operation, where the + * compositor stops propegating pointer events to clients and instead + * consumes them itself, to move or resize windows. */ + struct wb_server *server = view->server; + struct wlr_surface *focused_surface = + server->seat->seat->pointer_state.focused_surface; + if (view->xdg_surface->surface != focused_surface) { + /* Deny move/resize requests from unfocused clients. */ + return; + } + server->grabbed_view = view; + server->cursor_mode = mode; + struct wlr_box geo_box; + wlr_xdg_surface_get_geometry(view->xdg_surface, &geo_box); + if (mode == WB_CURSOR_MOVE) { + server->grab_x = server->cursor->cursor->x - view->x; + server->grab_y = server->cursor->cursor->y - view->y; + } else { + server->grab_x = server->cursor->cursor->x + geo_box.x; + server->grab_y = server->cursor->cursor->y + geo_box.y; + } + server->grab_width = geo_box.width; + server->grab_height = geo_box.height; + server->resize_edges = edges; +} + +static void xdg_toplevel_request_move( + struct wl_listener *listener, void *data) { + /* This event is raised when a client would like to begin an interactive + * move, typically because the user clicked on their client-side + * decorations. Note that a more sophisticated compositor should check the + * provied serial against a list of button press serials sent to this + * client, to prevent the client from requesting this whenever they want. */ + struct wb_view *view = wl_container_of(listener, view, request_move); + begin_interactive(view, WB_CURSOR_MOVE, 0); +} + +static void xdg_toplevel_request_resize( + struct wl_listener *listener, void *data) { + /* This event is raised when a client would like to begin an interactive + * resize, typically because the user clicked on their client-side + * decorations. Note that a more sophisticated compositor should check the + * provied serial against a list of button press serials sent to this + * client, to prevent the client from requesting this whenever they want. */ + struct wlr_xdg_toplevel_resize_event *event = data; + struct wb_view *view = wl_container_of(listener, view, request_resize); + begin_interactive(view, WB_CURSOR_RESIZE, event->edges); +} + +static void server_new_xdg_surface(struct wl_listener *listener, void *data) { + /* This event is raised when wlr_xdg_shell receives a new xdg surface from a + * client, either a toplevel (application window) or popup. */ + struct wb_server *server = + wl_container_of(listener, server, new_xdg_surface); + struct wlr_xdg_surface *xdg_surface = data; + if (xdg_surface->role != WLR_XDG_SURFACE_ROLE_TOPLEVEL) { + return; + } + + /* Allocate a tinywl_view for this surface */ + struct wb_view *view = + calloc(1, sizeof(struct wb_view)); + view->server = server; + view->xdg_surface = xdg_surface; + + /* Listen to the various events it can emit */ + view->map.notify = xdg_surface_map; + wl_signal_add(&xdg_surface->events.map, &view->map); + view->unmap.notify = xdg_surface_unmap; + wl_signal_add(&xdg_surface->events.unmap, &view->unmap); + view->destroy.notify = xdg_surface_destroy; + wl_signal_add(&xdg_surface->events.destroy, &view->destroy); + + /* cotd */ + struct wlr_xdg_toplevel *toplevel = xdg_surface->toplevel; + view->request_move.notify = xdg_toplevel_request_move; + wl_signal_add(&toplevel->events.request_move, &view->request_move); + view->request_resize.notify = xdg_toplevel_request_resize; + wl_signal_add(&toplevel->events.request_resize, &view->request_resize); + + /* Add it to the list of views. */ + wl_list_insert(&server->views, &view->link); +} + +void init_xdg_shell(struct wb_server *server) { + server->xdg_shell = wlr_xdg_shell_create(server->wl_display); + server->new_xdg_surface.notify = server_new_xdg_surface; + wl_signal_add(&server->xdg_shell->events.new_surface, &server->new_xdg_surface); +}