labwc/src/output.c

475 lines
14 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
2020-12-30 10:29:21 +00:00
#include "config.h"
2020-09-29 19:53:46 +01:00
#include <wlr/types/wlr_xdg_output_v1.h>
2019-12-26 21:37:31 +00:00
#include "labwc.h"
2020-10-19 22:14:17 +01:00
#include "menu/menu.h"
2020-08-03 20:56:38 +01:00
#include "theme/theme.h"
#include "layers.h"
2019-12-26 21:37:31 +00:00
2020-05-30 21:28:17 +01:00
struct draw_data {
struct wlr_output *output;
struct wlr_output_layout *output_layout;
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
};
static void
draw_rect(struct draw_data *d, struct wlr_box box)
2020-05-30 21:28:17 +01:00
{
double ox = 0, oy = 0;
wlr_output_layout_output_coords(d->output_layout, d->output, &ox, &oy);
box.x += ox;
box.y += oy;
2020-05-30 21:28:17 +01:00
wlr_render_rect(d->renderer, &box, d->rgba, d->transform_matrix);
}
static void
draw_line(struct draw_data *d, int x1, int y1, int x2, int y2)
{
struct wlr_box box = {
.x = x1,
.y = y1,
.width = abs(x2 - x1) + 1,
.height = abs(y2 - y1) + 1,
};
wlr_render_rect(d->renderer, &box, d->rgba, d->transform_matrix);
}
static void draw_rect_unfilled(struct draw_data *d, struct wlr_box box)
{
draw_line(d, box.x, box.y, box.x + box.width - 1, box.y);
2020-10-31 15:33:46 +00:00
draw_line(d, box.x + box.width - 1, box.y, box.x + box.width - 1,
box.y + box.height - 1);
draw_line(d, box.x, box.y + box.height - 1, box.x + box.width - 1,
box.y + box.height - 1);
draw_line(d, box.x, box.y, box.x, box.y + box.height - 1);
}
static void
shrink(struct wlr_box *box, int size)
{
box->x += size;
box->y += size;
box->width -= 2 * size;
box->height -= 2 * size;
}
static void
render_cycle_box(struct output *output)
{
struct wlr_output_layout *layout = output->server->output_layout;
double ox = 0, oy = 0;
struct wlr_box box;
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)
goto render_it;
}
return;
render_it:
wlr_output_layout_output_coords(layout, output->wlr_output, &ox, &oy);
box.x = view->x - view->margin.left + ox;
box.y = view->y - view->margin.top + oy;
box.width = view->w + view->margin.left + view->margin.right;
box.height = view->h + view->margin.top + view->margin.bottom;
struct draw_data dd = {
.renderer = view->server->renderer,
.transform_matrix = output->wlr_output->transform_matrix,
};
dd.rgba = (float[4]){ 1.0, 1.0, 1.0, 1.0 };
draw_rect_unfilled(&dd, box);
dd.rgba = (float[4]){ 0.0, 0.0, 0.0, 1.0 };
for (int i = 0; i < 4; i++) {
shrink(&box, 1);
draw_rect_unfilled(&dd, box);
}
dd.rgba = (float[4]){ 1.0, 1.0, 1.0, 1.0 };
shrink(&box, 1);
draw_rect_unfilled(&dd, box);
}
2020-10-19 22:14:17 +01:00
static void
render_rootmenu(struct output *output)
{
struct server *server = output->server;
struct draw_data ddata = {
.renderer = server->renderer,
.transform_matrix = output->wlr_output->transform_matrix,
};
float matrix[9];
struct wlr_output_layout *output_layout = server->output_layout;
double ox = 0, oy = 0;
wlr_output_layout_output_coords(output_layout, output->wlr_output, &ox, &oy);
2020-10-19 22:14:17 +01:00
ddata.rgba = (float[4]){ 0.9, 0.3, 0.3, 0.5 };
struct menuitem *menuitem;
wl_list_for_each (menuitem, &server->rootmenu->menuitems, link) {
struct wlr_texture *t;
t = menuitem->selected ? menuitem->active_texture :
menuitem->inactive_texture;
struct wlr_box box = {
.x = menuitem->geo_box.x + ox,
.y = menuitem->geo_box.y + oy,
.width = menuitem->geo_box.width,
.height = menuitem->geo_box.height,
};
wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL,
0, ddata.transform_matrix);
2020-10-19 22:14:17 +01:00
wlr_render_texture_with_matrix(ddata.renderer, t, matrix, 1);
}
}
static void
render_icon(struct draw_data *d, struct wlr_box box,
struct wlr_texture *texture)
2020-06-29 19:27:59 +01:00
{
if (!texture)
return;
2020-06-29 19:27:59 +01:00
float matrix[9];
/* centre-align icon if smaller than designated box */
struct wlr_box button;
wlr_texture_get_size(texture, &button.width, &button.height);
if (box.width > button.width) {
button.x = box.x + (box.width - button.width) / 2;
} else {
button.x = box.x;
button.width = box.width;
}
if (box.height > button.height) {
button.y = box.y + (box.height - button.height) / 2;
} else {
button.y = box.y;
button.height = box.height;
}
double ox = 0, oy = 0;
wlr_output_layout_output_coords(d->output_layout, d->output, &ox, &oy);
button.x += ox;
button.y += oy;
wlr_matrix_project_box(matrix, &button, WL_OUTPUT_TRANSFORM_NORMAL, 0,
2020-06-29 19:27:59 +01:00
d->transform_matrix);
wlr_render_texture_with_matrix(d->renderer, texture, matrix, 1);
}
static bool
isbutton(enum deco_part deco_part)
{
return deco_part == LAB_DECO_BUTTON_CLOSE ||
deco_part == LAB_DECO_BUTTON_MAXIMIZE ||
deco_part == LAB_DECO_BUTTON_ICONIFY;
}
static void
render_decorations(struct wlr_output *output, struct view *view)
2019-12-27 21:22:45 +00:00
{
if (!view->server_side_deco)
2019-12-28 20:33:01 +00:00
return;
2020-05-30 21:28:17 +01:00
struct draw_data ddata = {
.output = output,
.output_layout = view->server->output_layout,
2020-05-30 21:28:17 +01:00
.renderer = view->server->renderer,
.transform_matrix = output->transform_matrix,
};
2019-12-26 21:37:31 +00:00
/* border */
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));
/* title */
2020-10-07 21:49:58 +01:00
struct wlr_seat *seat = view->server->seat.seat;
if (view->surface == seat->keyboard_state.focused_surface)
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
/* button background */
struct wlr_cursor *cur = view->server->seat.cursor;
enum deco_part deco_part = deco_at(view, cur->x, cur->y);
struct wlr_box box = deco_box(view, deco_part);
if (isbutton(deco_part) &&
wlr_box_contains_point(&box, cur->x, cur->y)) {
ddata.rgba = (float[4]){ 0.5, 0.5, 0.5, 0.5 };
draw_rect(&ddata, deco_box(view, deco_part));
}
/* buttons */
2020-10-07 21:49:58 +01:00
if (view->surface == seat->keyboard_state.focused_surface) {
2020-08-21 20:35:06 +01:00
render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_CLOSE),
theme.xbm_close_active_unpressed);
render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_MAXIMIZE),
theme.xbm_maximize_active_unpressed);
render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_ICONIFY),
theme.xbm_iconify_active_unpressed);
} else {
render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_CLOSE),
theme.xbm_close_inactive_unpressed);
render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_MAXIMIZE),
theme.xbm_maximize_inactive_unpressed);
render_icon(&ddata, deco_box(view, LAB_DECO_BUTTON_ICONIFY),
theme.xbm_iconify_inactive_unpressed);
}
2019-12-26 21:37:31 +00:00
}
struct render_data_layer {
struct lab_layer_surface *layer_surface;
struct timespec *now;
};
static void
render_layer_surface(struct wlr_surface *surface, int sx, int sy, void *data)
{
struct render_data_layer *context = data;
struct lab_layer_surface *layer_surface = context->layer_surface;
struct wlr_texture *texture = wlr_surface_get_texture(surface);
if (texture == NULL) {
return;
}
struct wlr_output *output = layer_surface->layer_surface->output;
double ox = 0, oy = 0;
wlr_output_layout_output_coords(
layer_surface->server->output_layout, output, &ox, &oy);
ox += layer_surface->geo.x + sx, oy += layer_surface->geo.y + sy;
float matrix[9];
enum wl_output_transform transform =
wlr_output_transform_invert(surface->current.transform);
struct wlr_box box;
memcpy(&box, &layer_surface->geo, sizeof(struct wlr_box));
wlr_matrix_project_box(matrix, &box, transform, 0,
output->transform_matrix);
wlr_render_texture_with_matrix(layer_surface->server->renderer,
texture, matrix, 1);
wlr_surface_send_frame_done(surface, context->now);
}
static void
render_layer(struct timespec *now, struct wl_list *layer_surfaces)
{
struct render_data_layer context = {
.now = now,
};
struct lab_layer_surface *layer_surface;
wl_list_for_each(layer_surface, layer_surfaces, link) {
struct wlr_layer_surface_v1 *wlr_layer_surface_v1 =
layer_surface->layer_surface;
context.layer_surface = layer_surface;
wlr_surface_for_each_surface(wlr_layer_surface_v1->surface,
render_layer_surface, &context);
}
}
2020-05-30 21:28:17 +01:00
struct render_data {
struct wlr_output *output;
struct wlr_output_layout *output_layout;
2020-05-30 21:28:17 +01:00
struct wlr_renderer *renderer;
int lx, ly;
2020-05-30 21:28:17 +01:00
struct timespec *when;
};
static void
render_surface(struct wlr_surface *surface, int sx, int sy, void *data)
2019-12-27 21:22:45 +00:00
{
2019-12-26 21:37:31 +00:00
struct render_data *rdata = data;
2019-12-26 21:37:31 +00:00
struct wlr_texture *texture = wlr_surface_get_texture(surface);
2020-09-29 20:48:50 +01:00
if (!texture) {
2019-12-26 21:37:31 +00:00
return;
2020-09-29 20:48:50 +01:00
}
2019-12-26 21:37:31 +00:00
2020-09-29 20:48:50 +01:00
/*
* The view has a position in layout coordinates. If you have two
2019-12-27 21:22:45 +00:00
* displays, one next to the other, both 1080p, a view on the rightmost
* display might have layout coordinates of 2000,100. We need to
2020-09-29 20:48:50 +01:00
* translate that to output-local coordinates, or (2000 - 1920).
*/
2019-12-26 21:37:31 +00:00
double ox = 0, oy = 0;
2020-09-29 20:48:50 +01:00
wlr_output_layout_output_coords(
rdata->output_layout, rdata->output, &ox, &oy);
ox += rdata->lx + sx;
oy += rdata->ly + sy;
2019-12-26 21:37:31 +00:00
struct wlr_box box = {
.x = ox * rdata->output->scale,
.y = oy * rdata->output->scale,
.width = surface->current.width * rdata->output->scale,
.height = surface->current.height * rdata->output->scale,
2019-12-26 21:37:31 +00:00
};
float matrix[9];
enum wl_output_transform transform =
wlr_output_transform_invert(surface->current.transform);
wlr_matrix_project_box(matrix, &box, transform, 0,
2020-09-29 20:48:50 +01:00
rdata->output->transform_matrix);
2019-12-26 21:37:31 +00:00
wlr_render_texture_with_matrix(rdata->renderer, texture, matrix, 1);
wlr_surface_send_frame_done(surface, rdata->when);
}
2020-09-29 19:53:46 +01:00
static void
output_frame_notify(struct wl_listener *listener, void *data)
2019-12-27 21:22:45 +00:00
{
2020-09-29 19:53:46 +01:00
/*
* This function is called every time an output is ready to display a
* frame, generally at the output's refresh rate (e.g. 60Hz).
*/
2019-12-27 21:22:45 +00:00
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;
}
2020-09-29 19:53:46 +01:00
2019-12-26 21:37:31 +00:00
/* The "effective" resolution can change if you rotate your outputs. */
int width, height;
wlr_output_effective_resolution(output->wlr_output, &width, &height);
2020-09-29 19:53:46 +01:00
/* 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);
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]);
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]);
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) {
if (!view->mapped)
2019-12-26 21:37:31 +00:00
continue;
2020-09-29 20:48:50 +01:00
render_decorations(output->wlr_output, view);
2019-12-26 21:37:31 +00:00
struct render_data rdata = {
.output = output->wlr_output,
.output_layout = output->server->output_layout,
.lx = view->x,
.ly = view->y,
2019-12-26 21:37:31 +00:00
.renderer = renderer,
.when = &now,
};
2020-09-29 20:48:50 +01:00
view_for_each_surface(view, render_surface, &rdata);
2019-12-26 21:37:31 +00:00
}
/* If in cycle (alt-tab) mode, highlight selected view */
render_cycle_box(output);
2020-12-30 10:29:21 +00:00
#if HAVE_XWAYLAND
/* Render xwayland override_redirect surfaces */
struct xwayland_unmanaged *unmanaged;
wl_list_for_each_reverse (unmanaged,
&output->server->unmanaged_surfaces, link) {
struct render_data rdata = {
.output = output->wlr_output,
.output_layout = output->server->output_layout,
.lx = unmanaged->lx,
.ly = unmanaged->ly,
.renderer = renderer,
.when = &now,
};
struct wlr_surface *s = unmanaged->xwayland_surface->surface;
render_surface(s, 0, 0, &rdata);
}
2020-12-30 10:29:21 +00:00
#endif
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]);
render_layer(&now, &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]);
2020-10-19 22:14:17 +01:00
2020-10-21 20:30:06 +01:00
if (output->server->input_mode == LAB_INPUT_STATE_MENU) {
2020-10-19 22:14:17 +01:00
render_rootmenu(output);
}
2020-09-29 19:53:46 +01:00
/* Just in case hardware cursors not supported by GPU */
2019-12-26 21:37:31 +00:00
wlr_output_render_software_cursors(output->wlr_output, NULL);
wlr_renderer_end(renderer);
wlr_output_commit(output->wlr_output);
}
2020-05-29 22:18:03 +01:00
2020-09-29 19:53:46 +01:00
static void
output_destroy_notify(struct wl_listener *listener, void *data)
{
struct output *output = wl_container_of(listener, output, destroy);
wl_list_remove(&output->link);
2020-10-08 20:37:42 +01:00
wl_list_remove(&output->frame.link);
wl_list_remove(&output->destroy.link);
2020-10-23 20:19:07 +01:00
free(output);
2020-09-29 19:53:46 +01:00
}
static void
new_output_notify(struct wl_listener *listener, void *data)
2020-05-29 22:18:03 +01:00
{
/* 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;
/*
2020-09-29 19:53:46 +01:00
* The mode is a tuple of (width, height, refresh rate).
2020-05-29 22:18:03 +01:00
* TODO: support user configuration
*/
2020-09-29 19:53:46 +01:00
struct wlr_output_mode *mode = wlr_output_preferred_mode(wlr_output);
if (mode) {
2020-05-29 22:18:03 +01:00
wlr_output_set_mode(wlr_output, mode);
2020-09-29 19:53:46 +01:00
wlr_output_commit(wlr_output);
2020-05-29 22:18:03 +01:00
}
struct output *output = calloc(1, sizeof(struct output));
output->wlr_output = wlr_output;
output->server = server;
2020-09-29 19:53:46 +01:00
output->frame.notify = output_frame_notify;
2020-05-29 22:18:03 +01:00
wl_signal_add(&wlr_output->events.frame, &output->frame);
wl_list_insert(&server->outputs, &output->link);
2020-09-29 19:53:46 +01:00
output->destroy.notify = output_destroy_notify;
wl_signal_add(&wlr_output->events.destroy, &output->destroy);
2020-05-29 22:18:03 +01:00
wl_list_init(&output->layers[0]);
wl_list_init(&output->layers[1]);
wl_list_init(&output->layers[2]);
wl_list_init(&output->layers[3]);
2020-09-29 19:53:46 +01:00
/*
* Arrange outputs from left-to-right in the order they appear.
* TODO: support configuration in run-time
2020-05-29 22:18:03 +01:00
*/
wlr_output_layout_add_auto(server->output_layout, wlr_output);
2020-09-29 19:53:46 +01:00
wlr_output_schedule_frame(wlr_output);
}
void
output_init(struct server *server)
{
server->new_output.notify = new_output_notify;
wl_signal_add(&server->backend->events.new_output, &server->new_output);
/*
* Create an output layout, which is a wlroots utility for working with
* an arrangement of screens in a physical layout.
*/
server->output_layout = wlr_output_layout_create();
if (!server->output_layout) {
wlr_log(WLR_ERROR, "unable to create output layout");
exit(EXIT_FAILURE);
}
/* Enable screen recording with wf-recorder */
wlr_xdg_output_manager_v1_create(server->wl_display,
server->output_layout);
wl_list_init(&server->outputs);
2020-05-29 22:18:03 +01:00
}