wlroots-hyprland/backend/wayland/output.c

481 lines
14 KiB
C
Raw Normal View History

2017-06-20 18:27:05 +02:00
#include <assert.h>
#include <stdint.h>
2018-02-12 21:29:23 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2017-08-14 18:19:42 +02:00
#include <sys/mman.h>
2018-02-12 21:29:23 +01:00
#include <sys/types.h>
#include <unistd.h>
#include <wayland-client.h>
2017-06-21 16:27:45 +02:00
#include <wlr/interfaces/wlr_output.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_matrix.h>
2017-06-21 18:10:07 +02:00
#include <wlr/util/log.h>
#include "backend/wayland.h"
2018-02-12 21:29:23 +01:00
#include "util/signal.h"
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include "xdg-decoration-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
2018-09-17 22:10:34 +02:00
static struct wlr_wl_output *get_wl_output_from_output(
struct wlr_output *wlr_output) {
assert(wlr_output_is_wl(wlr_output));
return (struct wlr_wl_output *)wlr_output;
}
2017-08-14 18:19:42 +02:00
2017-06-20 18:27:05 +02:00
static struct wl_callback_listener frame_listener;
2018-01-26 22:39:23 +01:00
static void surface_frame_callback(void *data, struct wl_callback *cb,
uint32_t time) {
struct wlr_wl_output *output = data;
2018-01-26 22:39:23 +01:00
assert(output);
2017-06-26 17:34:22 +02:00
wl_callback_destroy(cb);
output->frame_callback = NULL;
wlr_output_send_frame(&output->wlr_output);
2017-06-26 17:34:22 +02:00
}
static struct wl_callback_listener frame_listener = {
.done = surface_frame_callback
};
2018-09-17 22:10:34 +02:00
static bool output_set_custom_mode(struct wlr_output *wlr_output,
2017-12-11 12:14:23 +01:00
int32_t width, int32_t height, int32_t refresh) {
2018-09-17 22:10:34 +02:00
struct wlr_wl_output *output = get_wl_output_from_output(wlr_output);
wlr_egl_swap_buffers(&output->backend->egl, output->egl_surface, NULL);
2017-12-11 12:14:23 +01:00
wl_egl_window_resize(output->egl_window, width, height, 0, 0);
wlr_output_update_custom_mode(&output->wlr_output, width, height, 0);
2017-12-11 12:14:23 +01:00
return true;
}
static bool output_attach_render(struct wlr_output *wlr_output,
int *buffer_age) {
struct wlr_wl_output *output =
2018-09-17 22:10:34 +02:00
get_wl_output_from_output(wlr_output);
return wlr_egl_make_current(&output->backend->egl, output->egl_surface,
buffer_age);
2017-06-26 17:34:22 +02:00
}
2017-06-20 18:27:05 +02:00
static bool output_attach_buffer(struct wlr_output *wlr_output,
struct wlr_buffer *buffer) {
struct wlr_wl_output *output =
get_wl_output_from_output(wlr_output);
struct wlr_wl_backend *wl = output->backend;
struct wlr_dmabuf_attributes attribs;
if (!wlr_buffer_get_dmabuf(buffer, &attribs)) {
return false;
}
if (attribs.width != wlr_output->width ||
attribs.height != wlr_output->height) {
return false;
}
if (!wlr_drm_format_set_has(&wl->linux_dmabuf_v1_formats,
attribs.format, attribs.modifier)) {
return false;
}
uint32_t modifier_hi = attribs.modifier >> 32;
uint32_t modifier_lo = (uint32_t)attribs.modifier;
struct zwp_linux_buffer_params_v1 *params =
zwp_linux_dmabuf_v1_create_params(wl->zwp_linux_dmabuf_v1);
for (int i = 0; i < attribs.n_planes; i++) {
zwp_linux_buffer_params_v1_add(params, attribs.fd[i], i,
attribs.offset[i], attribs.stride[i], modifier_hi, modifier_lo);
}
uint32_t flags = 0;
if (attribs.flags & WLR_DMABUF_ATTRIBUTES_FLAGS_Y_INVERT) {
flags |= ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT;
}
if (attribs.flags & WLR_DMABUF_ATTRIBUTES_FLAGS_INTERLACED) {
flags |= ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_INTERLACED;
}
if (attribs.flags & WLR_DMABUF_ATTRIBUTES_FLAGS_BOTTOM_FIRST) {
flags |= ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_BOTTOM_FIRST;
}
struct wl_buffer *wl_buffer = zwp_linux_buffer_params_v1_create_immed(
params, attribs.width, attribs.height, attribs.format, flags);
// TODO: handle create() errors
wl_surface_attach(output->surface, wl_buffer, 0, 0);
if (output->pending_wl_buffer != NULL) {
wl_buffer_destroy(output->pending_wl_buffer);
}
output->pending_wl_buffer = wl_buffer;
return true;
}
static bool output_commit(struct wlr_output *wlr_output) {
struct wlr_wl_output *output =
2018-09-17 22:10:34 +02:00
get_wl_output_from_output(wlr_output);
if (output->frame_callback != NULL) {
wlr_log(WLR_ERROR, "Skipping buffer swap");
return false;
}
output->frame_callback = wl_surface_frame(output->surface);
wl_callback_add_listener(output->frame_callback, &frame_listener, output);
pixman_region32_t *damage = NULL;
if (wlr_output->pending.committed & WLR_OUTPUT_STATE_DAMAGE) {
damage = &wlr_output->pending.damage;
}
wlr_buffer_unref(output->current_buffer);
output->current_buffer = NULL;
if (output->current_wl_buffer != NULL) {
wl_buffer_destroy(output->current_wl_buffer);
output->current_wl_buffer = NULL;
}
assert(wlr_output->pending.committed & WLR_OUTPUT_STATE_BUFFER);
switch (wlr_output->pending.buffer_type) {
case WLR_OUTPUT_STATE_BUFFER_RENDER:
if (!wlr_egl_swap_buffers(&output->backend->egl,
output->egl_surface, damage)) {
return false;
}
break;
case WLR_OUTPUT_STATE_BUFFER_SCANOUT:
if (damage == NULL) {
wl_surface_damage_buffer(output->surface,
0, 0, INT32_MAX, INT32_MAX);
} else {
int rects_len;
pixman_box32_t *rects =
pixman_region32_rectangles(damage, &rects_len);
for (int i = 0; i < rects_len; i++) {
pixman_box32_t *r = &rects[i];
wl_surface_damage_buffer(output->surface, r->x1, r->y1,
r->x2 - r->x1, r->y2 - r->y1);
}
}
wl_surface_commit(output->surface);
output->current_buffer = wlr_buffer_ref(wlr_output->pending.buffer);
output->current_wl_buffer = output->pending_wl_buffer;
output->pending_wl_buffer = NULL;
break;
}
// TODO: if available, use the presentation-time protocol
wlr_output_send_present(wlr_output, NULL);
return true;
2017-06-20 18:27:05 +02:00
}
2018-05-09 20:58:18 +02:00
static bool output_set_cursor(struct wlr_output *wlr_output,
struct wlr_texture *texture, int32_t scale,
2018-09-17 22:10:34 +02:00
enum wl_output_transform transform,
int32_t hotspot_x, int32_t hotspot_y, bool update_texture) {
struct wlr_wl_output *output = get_wl_output_from_output(wlr_output);
2017-08-14 18:19:42 +02:00
struct wlr_wl_backend *backend = output->backend;
2017-10-08 21:21:06 +02:00
2018-05-09 20:58:18 +02:00
struct wlr_box hotspot = { .x = hotspot_x, .y = hotspot_y };
wlr_box_transform(&hotspot, &hotspot,
2018-09-17 22:10:34 +02:00
wlr_output_transform_invert(wlr_output->transform),
output->cursor.width, output->cursor.height);
2018-05-09 20:58:18 +02:00
// TODO: use output->wlr_output.transform to transform pixels and hotpot
2018-05-09 20:58:18 +02:00
output->cursor.hotspot_x = hotspot.x;
output->cursor.hotspot_y = hotspot.y;
if (!update_texture) {
// Update hotspot without changing cursor image
update_wl_output_cursor(output);
return true;
}
2017-10-08 21:21:06 +02:00
if (output->cursor.surface == NULL) {
output->cursor.surface =
wl_compositor_create_surface(backend->compositor);
2017-08-14 18:19:42 +02:00
}
struct wl_surface *surface = output->cursor.surface;
2017-08-14 18:19:42 +02:00
if (texture != NULL) {
int width, height;
wlr_texture_get_size(texture, &width, &height);
2018-05-09 20:58:18 +02:00
width = width * wlr_output->scale / scale;
height = height * wlr_output->scale / scale;
output->cursor.width = width;
output->cursor.height = height;
2017-08-14 18:19:42 +02:00
if (output->cursor.egl_window == NULL) {
output->cursor.egl_window =
wl_egl_window_create(surface, width, height);
2017-08-14 18:19:42 +02:00
}
wl_egl_window_resize(output->cursor.egl_window, width, height, 0, 0);
2017-08-14 18:19:42 +02:00
EGLSurface egl_surface =
wlr_egl_create_surface(&backend->egl, output->cursor.egl_window);
2017-08-14 18:19:42 +02:00
wlr_egl_make_current(&backend->egl, egl_surface, NULL);
2017-08-14 18:19:42 +02:00
2018-05-09 20:58:18 +02:00
struct wlr_box cursor_box = {
.width = width,
.height = height,
};
float projection[9];
wlr_matrix_projection(projection, width, height, wlr_output->transform);
float matrix[9];
2018-05-09 20:58:18 +02:00
wlr_matrix_project_box(matrix, &cursor_box, transform, 0, projection);
wlr_renderer_begin(backend->renderer, width, height);
wlr_renderer_clear(backend->renderer, (float[]){ 0.0, 0.0, 0.0, 0.0 });
2018-05-09 20:58:18 +02:00
wlr_render_texture_with_matrix(backend->renderer, texture, matrix, 1.0);
wlr_renderer_end(backend->renderer);
wlr_egl_swap_buffers(&backend->egl, egl_surface, NULL);
wlr_egl_destroy_surface(&backend->egl, egl_surface);
} else {
wl_surface_attach(surface, NULL, 0, 0);
wl_surface_commit(surface);
}
2017-08-14 18:19:42 +02:00
update_wl_output_cursor(output);
2017-08-14 18:19:42 +02:00
return true;
}
static void output_destroy(struct wlr_output *wlr_output) {
2018-09-17 22:10:34 +02:00
struct wlr_wl_output *output = get_wl_output_from_output(wlr_output);
if (output == NULL) {
return;
}
wl_list_remove(&output->link);
if (output->cursor.egl_window != NULL) {
wl_egl_window_destroy(output->cursor.egl_window);
2017-08-14 18:19:42 +02:00
}
if (output->cursor.surface) {
wl_surface_destroy(output->cursor.surface);
2017-08-14 18:19:42 +02:00
}
if (output->frame_callback) {
wl_callback_destroy(output->frame_callback);
}
wlr_egl_destroy_surface(&output->backend->egl, output->egl_surface);
wl_egl_window_destroy(output->egl_window);
if (output->zxdg_toplevel_decoration_v1) {
zxdg_toplevel_decoration_v1_destroy(output->zxdg_toplevel_decoration_v1);
}
xdg_toplevel_destroy(output->xdg_toplevel);
xdg_surface_destroy(output->xdg_surface);
wl_surface_destroy(output->surface);
free(output);
}
void update_wl_output_cursor(struct wlr_wl_output *output) {
if (output->backend->pointer && output->enter_serial) {
wl_pointer_set_cursor(output->backend->pointer, output->enter_serial,
output->cursor.surface, output->cursor.hotspot_x,
output->cursor.hotspot_y);
2017-08-14 18:19:42 +02:00
}
}
static bool output_move_cursor(struct wlr_output *_output, int x, int y) {
2017-08-14 18:19:42 +02:00
// TODO: only return true if x == current x and y == current y
return true;
}
static bool output_schedule_frame(struct wlr_output *wlr_output) {
struct wlr_wl_output *output = get_wl_output_from_output(wlr_output);
if (output->frame_callback != NULL) {
wlr_log(WLR_ERROR, "Skipping frame scheduling");
return true;
}
output->frame_callback = wl_surface_frame(output->surface);
wl_callback_add_listener(output->frame_callback, &frame_listener, output);
wl_surface_commit(output->surface);
return true;
}
static const struct wlr_output_impl output_impl = {
.set_custom_mode = output_set_custom_mode,
.destroy = output_destroy,
.attach_render = output_attach_render,
.attach_buffer = output_attach_buffer,
.commit = output_commit,
.set_cursor = output_set_cursor,
.move_cursor = output_move_cursor,
.schedule_frame = output_schedule_frame,
};
2017-12-19 19:55:18 +01:00
bool wlr_output_is_wl(struct wlr_output *wlr_output) {
return wlr_output->impl == &output_impl;
}
static void xdg_surface_handle_configure(void *data,
struct xdg_surface *xdg_surface, uint32_t serial) {
struct wlr_wl_output *output = data;
assert(output && output->xdg_surface == xdg_surface);
xdg_surface_ack_configure(xdg_surface, serial);
// nothing else?
2017-06-20 18:27:05 +02:00
}
static struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_handle_configure,
};
2018-09-17 22:10:34 +02:00
static void xdg_toplevel_handle_configure(void *data,
struct xdg_toplevel *xdg_toplevel,
int32_t width, int32_t height, struct wl_array *states) {
struct wlr_wl_output *output = data;
assert(output && output->xdg_toplevel == xdg_toplevel);
if (width == 0 || height == 0) {
return;
}
// loop over states for maximized etc?
output_set_custom_mode(&output->wlr_output, width, height, 0);
2017-06-20 18:27:05 +02:00
}
2018-09-17 22:10:34 +02:00
static void xdg_toplevel_handle_close(void *data,
struct xdg_toplevel *xdg_toplevel) {
struct wlr_wl_output *output = data;
assert(output && output->xdg_toplevel == xdg_toplevel);
wlr_output_destroy((struct wlr_output *)output);
2017-06-20 18:27:05 +02:00
}
static struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_handle_configure,
.close = xdg_toplevel_handle_close,
2017-06-20 18:27:05 +02:00
};
2018-09-17 22:10:34 +02:00
struct wlr_output *wlr_wl_output_create(struct wlr_backend *wlr_backend) {
struct wlr_wl_backend *backend = get_wl_backend_from_backend(wlr_backend);
if (!backend->started) {
2017-06-21 00:22:21 +02:00
++backend->requested_outputs;
return NULL;
}
struct wlr_wl_output *output;
if (!(output = calloc(sizeof(struct wlr_wl_output), 1))) {
2018-07-09 23:49:54 +02:00
wlr_log(WLR_ERROR, "Failed to allocate wlr_wl_output");
return NULL;
}
2018-01-04 12:46:15 +01:00
wlr_output_init(&output->wlr_output, &backend->backend, &output_impl,
backend->local_display);
struct wlr_output *wlr_output = &output->wlr_output;
wlr_output_update_custom_mode(wlr_output, 1280, 720, 0);
strncpy(wlr_output->make, "wayland", sizeof(wlr_output->make));
strncpy(wlr_output->model, "wayland", sizeof(wlr_output->model));
snprintf(wlr_output->name, sizeof(wlr_output->name), "WL-%zd",
++backend->last_output_num);
output->backend = backend;
2017-06-20 18:27:05 +02:00
output->surface = wl_compositor_create_surface(backend->compositor);
if (!output->surface) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Could not create output surface");
goto error;
}
wl_surface_set_user_data(output->surface, output);
output->xdg_surface =
xdg_wm_base_get_xdg_surface(backend->xdg_wm_base, output->surface);
if (!output->xdg_surface) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Could not get xdg surface");
goto error;
}
output->xdg_toplevel =
xdg_surface_get_toplevel(output->xdg_surface);
if (!output->xdg_toplevel) {
2018-07-09 23:49:54 +02:00
wlr_log_errno(WLR_ERROR, "Could not get xdg toplevel");
goto error;
}
if (backend->zxdg_decoration_manager_v1) {
output->zxdg_toplevel_decoration_v1 =
zxdg_decoration_manager_v1_get_toplevel_decoration(
backend->zxdg_decoration_manager_v1, output->xdg_toplevel);
if (!output->zxdg_toplevel_decoration_v1) {
wlr_log_errno(WLR_ERROR, "Could not get xdg toplevel decoration");
goto error;
}
zxdg_toplevel_decoration_v1_set_mode(output->zxdg_toplevel_decoration_v1,
ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
}
wlr_wl_output_set_title(wlr_output, NULL);
xdg_toplevel_set_app_id(output->xdg_toplevel, "wlroots");
xdg_surface_add_listener(output->xdg_surface,
&xdg_surface_listener, output);
xdg_toplevel_add_listener(output->xdg_toplevel,
&xdg_toplevel_listener, output);
wl_surface_commit(output->surface);
2017-06-20 18:27:05 +02:00
output->egl_window = wl_egl_window_create(output->surface,
wlr_output->width, wlr_output->height);
output->egl_surface = wlr_egl_create_surface(&backend->egl,
output->egl_window);
wl_display_roundtrip(output->backend->remote_display);
// start rendering loop per callbacks by rendering first frame
if (!wlr_egl_make_current(&output->backend->egl, output->egl_surface,
NULL)) {
goto error;
}
wlr_renderer_begin(backend->renderer, wlr_output->width, wlr_output->height);
wlr_renderer_clear(backend->renderer, (float[]){ 1.0, 1.0, 1.0, 1.0 });
wlr_renderer_end(backend->renderer);
output->frame_callback = wl_surface_frame(output->surface);
wl_callback_add_listener(output->frame_callback, &frame_listener, output);
if (!wlr_egl_swap_buffers(&output->backend->egl, output->egl_surface,
NULL)) {
goto error;
}
wl_list_insert(&backend->outputs, &output->link);
2018-01-04 12:46:15 +01:00
wlr_output_update_enabled(wlr_output, true);
wlr_signal_emit_safe(&backend->backend.events.new_output, wlr_output);
if (backend->pointer != NULL) {
create_wl_pointer(backend->pointer, output);
}
return wlr_output;
error:
wlr_output_destroy(&output->wlr_output);
return NULL;
}
2019-01-11 03:53:32 +01:00
void wlr_wl_output_set_title(struct wlr_output *output, const char *title) {
struct wlr_wl_output *wl_output = get_wl_output_from_output(output);
char wl_title[32];
if (title == NULL) {
if (snprintf(wl_title, sizeof(wl_title), "wlroots - %s", output->name) <= 0) {
return;
}
title = wl_title;
}
2019-01-11 03:53:32 +01:00
xdg_toplevel_set_title(wl_output->xdg_toplevel, title);
}