2019-12-26 21:37:31 +00:00
|
|
|
#include "labwc.h"
|
2020-06-11 21:20:43 +01:00
|
|
|
#include "theme.h"
|
2019-12-26 21:37:31 +00:00
|
|
|
|
2020-05-30 21:28:17 +01:00
|
|
|
struct draw_data {
|
2019-12-26 21:37:31 +00:00
|
|
|
struct wlr_renderer *renderer;
|
2020-05-30 21:28:17 +01:00
|
|
|
float *transform_matrix;
|
|
|
|
|
float *rgba;
|
2019-12-26 21:37:31 +00:00
|
|
|
};
|
|
|
|
|
|
2020-05-30 21:28:17 +01:00
|
|
|
static void draw_rect(struct draw_data *d, struct wlr_box box)
|
|
|
|
|
{
|
|
|
|
|
wlr_render_rect(d->renderer, &box, d->rgba, d->transform_matrix);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 13:42:40 +01:00
|
|
|
static void render_cycle_box(struct output *output)
|
|
|
|
|
{
|
|
|
|
|
if (!output->server->cycle_view)
|
|
|
|
|
return;
|
|
|
|
|
struct view *view;
|
|
|
|
|
wl_list_for_each_reverse (view, &output->server->views, link) {
|
|
|
|
|
if (view != output->server->cycle_view)
|
|
|
|
|
continue;
|
2020-05-26 12:56:33 +01:00
|
|
|
struct wlr_box box;
|
2020-06-05 23:04:54 +01:00
|
|
|
if ((view->type == LAB_XWAYLAND_VIEW) ||
|
|
|
|
|
!rc.client_side_decorations) {
|
2020-05-26 12:56:33 +01:00
|
|
|
box = deco_max_extents(view);
|
|
|
|
|
} else {
|
|
|
|
|
box = view_get_surface_geometry(view);
|
|
|
|
|
box.x += view->x;
|
|
|
|
|
box.y += view->y;
|
|
|
|
|
}
|
2020-05-25 13:42:40 +01:00
|
|
|
float cycle_color[] = { 0.0, 0.0, 0.0, 0.2 };
|
|
|
|
|
wlr_render_rect(output->server->renderer, &box, cycle_color,
|
|
|
|
|
output->wlr_output->transform_matrix);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 19:27:59 +01:00
|
|
|
static void render_icon(struct draw_data *d, struct wlr_box box,
|
|
|
|
|
struct wlr_texture *texture)
|
|
|
|
|
{
|
|
|
|
|
float matrix[9];
|
|
|
|
|
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
|
|
|
|
|
d->transform_matrix);
|
|
|
|
|
wlr_render_texture_with_matrix(d->renderer, texture, matrix, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
static void render_decorations(struct wlr_output *output, struct view *view)
|
|
|
|
|
{
|
2020-05-04 22:21:30 +01:00
|
|
|
if (!view_want_deco(view))
|
2019-12-28 20:33:01 +00:00
|
|
|
return;
|
2020-05-30 21:28:17 +01:00
|
|
|
struct draw_data ddata = {
|
|
|
|
|
.renderer = view->server->renderer,
|
|
|
|
|
.transform_matrix = output->transform_matrix,
|
|
|
|
|
};
|
2019-12-26 21:37:31 +00:00
|
|
|
|
2020-06-12 21:41:30 +01:00
|
|
|
ddata.rgba = theme.window_active_handle_bg_color;
|
2020-05-30 21:28:17 +01:00
|
|
|
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_TOP));
|
|
|
|
|
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_RIGHT));
|
|
|
|
|
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_BOTTOM));
|
|
|
|
|
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_LEFT));
|
2020-05-04 22:21:30 +01:00
|
|
|
|
2020-06-15 21:44:57 +01:00
|
|
|
if (view_hasfocus(view))
|
|
|
|
|
ddata.rgba = theme.window_active_title_bg_color;
|
|
|
|
|
else
|
|
|
|
|
ddata.rgba = theme.window_inactive_title_bg_color;
|
2020-05-30 21:28:17 +01:00
|
|
|
draw_rect(&ddata, deco_box(view, LAB_DECO_PART_TITLE));
|
2020-06-29 19:27:59 +01:00
|
|
|
|
|
|
|
|
render_icon(&ddata, deco_box(view, LAB_DECO_ICON_CLOSE),
|
|
|
|
|
theme.xbm_close);
|
2019-12-26 21:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-30 21:28:17 +01:00
|
|
|
struct render_data {
|
|
|
|
|
struct wlr_output *output;
|
|
|
|
|
struct wlr_renderer *renderer;
|
|
|
|
|
struct view *view;
|
|
|
|
|
struct timespec *when;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
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.
|
|
|
|
|
*/
|
2019-12-26 21:37:31 +00:00
|
|
|
struct render_data *rdata = data;
|
2019-12-27 20:48:58 +00:00
|
|
|
struct view *view = rdata->view;
|
2019-12-26 21:37:31 +00:00
|
|
|
struct wlr_output *output = rdata->output;
|
|
|
|
|
|
|
|
|
|
/* We first obtain a wlr_texture, which is a GPU resource. wlroots
|
2019-12-27 21:22:45 +00:00
|
|
|
* 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. */
|
2019-12-26 21:37:31 +00:00
|
|
|
struct wlr_texture *texture = wlr_surface_get_texture(surface);
|
|
|
|
|
if (texture == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
/* 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). */
|
2019-12-26 21:37:31 +00:00
|
|
|
double ox = 0, oy = 0;
|
2019-12-27 21:22:45 +00:00
|
|
|
wlr_output_layout_output_coords(view->server->output_layout, output,
|
|
|
|
|
&ox, &oy);
|
2019-12-26 21:37:31 +00:00
|
|
|
ox += view->x + sx;
|
|
|
|
|
oy += view->y + sy;
|
|
|
|
|
|
2020-05-29 22:26:16 +01:00
|
|
|
/* TODO: Support HiDPI */
|
2019-12-26 21:37:31 +00:00
|
|
|
struct wlr_box box = {
|
|
|
|
|
.x = ox * output->scale,
|
|
|
|
|
.y = oy * output->scale,
|
|
|
|
|
.width = surface->current.width * output->scale,
|
|
|
|
|
.height = surface->current.height * output->scale,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*
|
2019-12-27 21:22:45 +00:00
|
|
|
* 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.
|
2019-12-26 21:37:31 +00:00
|
|
|
*
|
|
|
|
|
* 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,
|
2019-12-27 21:22:45 +00:00
|
|
|
output->transform_matrix);
|
2019-12-26 21:37:31 +00:00
|
|
|
|
2020-05-29 22:26:16 +01:00
|
|
|
/*
|
|
|
|
|
* This takes our matrix, the texture, and an alpha, and performs the
|
|
|
|
|
* actual rendering on the GPU.
|
|
|
|
|
*/
|
2019-12-26 21:37:31 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
void output_frame(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
/* This function is called every time an output is ready to display a
|
|
|
|
|
* frame, generally at the output's refresh rate (e.g. 60Hz). */
|
|
|
|
|
struct output *output = wl_container_of(listener, output, frame);
|
2019-12-26 21:37:31 +00:00
|
|
|
struct wlr_renderer *renderer = output->server->renderer;
|
|
|
|
|
|
|
|
|
|
struct timespec now;
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
|
|
|
|
|
|
/* wlr_output_attach_render makes the OpenGL context current. */
|
|
|
|
|
if (!wlr_output_attach_render(output->wlr_output, NULL)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
/* The "effective" resolution can change if you rotate your outputs. */
|
|
|
|
|
int width, height;
|
|
|
|
|
wlr_output_effective_resolution(output->wlr_output, &width, &height);
|
2019-12-27 21:22:45 +00:00
|
|
|
/* Begin the renderer (calls glViewport and some other GL sanity checks)
|
|
|
|
|
*/
|
2019-12-26 21:37:31 +00:00
|
|
|
wlr_renderer_begin(renderer, width, height);
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
float color[4] = { 0.3, 0.3, 0.3, 1.0 };
|
2019-12-26 21:37:31 +00:00
|
|
|
wlr_renderer_clear(renderer, color);
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
/* Each subsequent window we render is rendered on top of the last.
|
|
|
|
|
* Because our view list is ordered front-to-back, we iterate over it
|
|
|
|
|
* backwards. */
|
2019-12-27 20:48:58 +00:00
|
|
|
struct view *view;
|
2019-12-27 21:22:45 +00:00
|
|
|
wl_list_for_each_reverse (view, &output->server->views, link) {
|
2019-12-26 21:37:31 +00:00
|
|
|
if (!view->mapped) {
|
|
|
|
|
/* An unmapped view should not be rendered. */
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
struct render_data rdata = {
|
|
|
|
|
.output = output->wlr_output,
|
|
|
|
|
.view = view,
|
|
|
|
|
.renderer = renderer,
|
|
|
|
|
.when = &now,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render_decorations(output->wlr_output, view);
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
/* This calls our render_surface function for each surface among
|
|
|
|
|
* the xdg_surface's toplevel and popups. */
|
2019-12-26 21:37:31 +00:00
|
|
|
if (view->type == LAB_XDG_SHELL_VIEW) {
|
2019-12-27 21:22:45 +00:00
|
|
|
wlr_xdg_surface_for_each_surface(
|
|
|
|
|
view->xdg_surface, render_surface, &rdata);
|
2019-12-26 21:37:31 +00:00
|
|
|
} else if (view->type == LAB_XWAYLAND_VIEW) {
|
|
|
|
|
render_surface(view->xwayland_surface->surface, 0, 0,
|
2019-12-27 21:22:45 +00:00
|
|
|
&rdata);
|
2019-12-26 21:37:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 13:42:40 +01:00
|
|
|
/* If in cycle (alt-tab) mode, highlight selected view */
|
|
|
|
|
render_cycle_box(output);
|
|
|
|
|
|
2019-12-27 21:22:45 +00:00
|
|
|
/* Hardware cursors are rendered by the GPU on a separate plane, and can
|
|
|
|
|
* be moved around without re-rendering what's beneath them - which is
|
|
|
|
|
* more efficient. However, not all hardware supports hardware cursors.
|
|
|
|
|
* For this reason, wlroots provides a software fallback, which we ask
|
|
|
|
|
* it to render here. wlr_cursor handles configuring hardware vs
|
|
|
|
|
* software cursors for you,
|
2019-12-26 21:37:31 +00:00
|
|
|
* and this function is a no-op when hardware cursors are in use. */
|
|
|
|
|
wlr_output_render_software_cursors(output->wlr_output, NULL);
|
|
|
|
|
|
|
|
|
|
/* Conclude rendering and swap the buffers, showing the final frame
|
|
|
|
|
* on-screen. */
|
|
|
|
|
wlr_renderer_end(renderer);
|
|
|
|
|
wlr_output_commit(output->wlr_output);
|
|
|
|
|
}
|
2020-05-29 22:18:03 +01:00
|
|
|
|
|
|
|
|
void output_new(struct wl_listener *listener, void *data)
|
|
|
|
|
{
|
|
|
|
|
/* This event is rasied by the backend when a new output (aka a display
|
|
|
|
|
* or monitor) becomes available. */
|
|
|
|
|
struct server *server = wl_container_of(listener, server, new_output);
|
|
|
|
|
struct wlr_output *wlr_output = data;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Some backends don't have modes. DRM+KMS does, and we need to set a
|
|
|
|
|
* mode before we can use the output. The mode is a tuple of (width,
|
|
|
|
|
* height, refresh rate), and each monitor supports only a specific set
|
|
|
|
|
* of modes. We just pick the monitor's preferred mode.
|
|
|
|
|
* TODO: support user configuration
|
|
|
|
|
*/
|
|
|
|
|
if (!wl_list_empty(&wlr_output->modes)) {
|
|
|
|
|
struct wlr_output_mode *mode =
|
|
|
|
|
wlr_output_preferred_mode(wlr_output);
|
|
|
|
|
wlr_output_set_mode(wlr_output, mode);
|
|
|
|
|
wlr_output_enable(wlr_output, true);
|
|
|
|
|
if (!wlr_output_commit(wlr_output)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Allocates and configures our state for this output */
|
|
|
|
|
struct output *output = calloc(1, sizeof(struct output));
|
|
|
|
|
output->wlr_output = wlr_output;
|
|
|
|
|
output->server = server;
|
|
|
|
|
/* Sets up a listener for the frame notify event. */
|
|
|
|
|
output->frame.notify = output_frame;
|
|
|
|
|
wl_signal_add(&wlr_output->events.frame, &output->frame);
|
|
|
|
|
wl_list_insert(&server->outputs, &output->link);
|
|
|
|
|
|
|
|
|
|
/* Adds this to the output layout. The add_auto function arranges
|
|
|
|
|
* outputs from left-to-right in the order they appear. A more
|
|
|
|
|
* sophisticated compositor would let the user configure the arrangement
|
|
|
|
|
* of outputs in the layout.
|
|
|
|
|
*
|
|
|
|
|
* The output layout utility automatically adds a wl_output global to
|
|
|
|
|
* the display, which Wayland clients can see to find out information
|
|
|
|
|
* about the output (such as DPI, scale factor, manufacturer, etc).
|
|
|
|
|
*/
|
|
|
|
|
wlr_output_layout_add_auto(server->output_layout, wlr_output);
|
|
|
|
|
}
|