From 904739c40596cb4e9e9d48c89b1d864051237441 Mon Sep 17 00:00:00 2001 From: nyorain Date: Mon, 19 Jun 2017 17:46:50 +0200 Subject: [PATCH 01/11] Start to fix wayland backend Starts to update the wayland backend to the new backend api. Not yet updated to libinput branch (not yet compiling). --- backend/CMakeLists.txt | 8 +-- backend/wayland/backend.c | 100 +++++++++++++++++++++------------- include/backend/wayland.h | 5 +- include/wlr/backend/wayland.h | 11 ++-- 4 files changed, 74 insertions(+), 50 deletions(-) diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 91f0f06eaa..a566930779 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -5,10 +5,10 @@ include_directories( ) add_library(wlr-backend - #wayland/backend.c - #wayland/registry.c - #wayland/wl_seat.c - #wayland/wl_output.c + wayland/backend.c + wayland/registry.c + wayland/wl_seat.c + wayland/wl_output.c drm/backend.c drm/drm.c udev.c diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 7f73c5015b..dec559f380 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -2,58 +2,84 @@ #include #include #include +#include #include "backend/wayland.h" #include "common/log.h" -void wlr_wl_backend_free(struct wlr_wl_backend *backend) { - if (!backend) { +/* + * Initializes the wayland backend. Opens a connection to a remote wayland + * compositor and creates surfaces for each output, then registers globals on + * the specified display. + */ +static bool wlr_wl_backend_init(struct wlr_backend_state* state) { + state->remote_display = wl_display_connect(getenv("_WAYLAND_DISPLAY")); + if (!state->remote_display) { + wlr_log(L_ERROR, "Could not connect to remote display"); + return false; + } + + if (!(state->registry = wl_display_get_registry(state->remote_display))) { + wlr_log(L_ERROR, "Could not obtain reference to remote registry"); + return false; + } + + wlr_wlb_registry_poll(state); + return true; +} + +static void wlr_wl_backend_destroy(struct wlr_backend_state *state) { + if (!state) { return; } + // TODO: Free surfaces - for (size_t i = 0; backend->outputs && i < backend->outputs->length; ++i) { - struct wlr_wl_output *output = backend->outputs->items[i]; + for (size_t i = 0; state->outputs && i < state->outputs->length; ++i) { + struct wlr_wl_output *output = state->outputs->items[i]; wlr_wl_output_free(output); } - list_free(backend->outputs); - if (backend->seat) wlr_wl_seat_free(backend->seat); - if (backend->shm) wl_shm_destroy(backend->shm); - if (backend->shell) wl_shell_destroy(backend->shell); - if (backend->compositor) wl_compositor_destroy(backend->compositor); - if (backend->registry) wl_registry_destroy(backend->registry); - if (backend->remote_display) wl_display_disconnect(backend->remote_display); - free(backend); + + list_free(state->outputs); + if (state->seat) wlr_wl_seat_free(state->seat); + if (state->shm) wl_shm_destroy(state->shm); + if (state->shell) wl_shell_destroy(state->shell); + if (state->compositor) wl_compositor_destroy(state->compositor); + if (state->registry) wl_registry_destroy(state->registry); + if (state->remote_display) wl_display_disconnect(state->remote_display); + free(state); } -/* - * Initializes the wayland backend. Opens a connection to a remote wayland - * compositor and creates surfaces for each output, then registers globals on - * the specified display. - */ -struct wlr_wl_backend *wlr_wl_backend_init( - struct wl_display *display, size_t outputs) { - assert(display); - struct wlr_wl_backend *backend; - if (!(backend = calloc(sizeof(struct wlr_wl_backend), 1))) { - wlr_log(L_ERROR, "Could not allocate backend"); - goto error; - } - if (!(backend->outputs = list_create())) { - wlr_log(L_ERROR, "Could not allocate output list"); - goto error; +static struct wlr_backend_impl backend_impl = { + .init = wlr_wl_backend_init, + .destroy = wlr_wl_backend_destroy +}; + + +struct wlr_backend *wlr_wl_backend_create(struct wl_display *display, + size_t outputs) { + wlr_log(L_INFO, "Initalizing wayland backend"); + + struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); + if (!state) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; } - backend->local_display = display; - backend->remote_display = wl_display_connect(getenv("_WAYLAND_DISPLAY")); - if (!backend->remote_display) { - wlr_log(L_ERROR, "Could not connect to remote display"); - goto error; + + struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); + if (!backend) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; } - if (!(backend->registry = wl_display_get_registry(backend->remote_display))) { - wlr_log(L_ERROR, "Could not obtain reference to remote registry"); + + if (!(state->outputs = list_create())) { + wlr_log(L_ERROR, "Could not allocate output list"); goto error; } - wlr_wlb_registry_poll(backend); + state->local_display = display; + return backend; + error: - wlr_wl_backend_free(backend); + free(state); + free(backend); return NULL; } diff --git a/include/backend/wayland.h b/include/backend/wayland.h index 94aa096205..b576e56449 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -5,8 +5,9 @@ #include #include #include +#include -struct wlr_wl_backend { +struct wlr_backend_state { /* local state */ struct wl_display *local_display; /* remote state */ @@ -19,7 +20,7 @@ struct wlr_wl_backend { list_t *outputs; }; -void wlr_wlb_registry_poll(struct wlr_wl_backend *backend); +void wlr_wlb_registry_poll(struct wlr_backend_state *backend); extern const struct wl_seat_listener seat_listener; extern const struct wl_output_listener output_listener; diff --git a/include/wlr/backend/wayland.h b/include/wlr/backend/wayland.h index 4318cc2688..83b465c1db 100644 --- a/include/wlr/backend/wayland.h +++ b/include/wlr/backend/wayland.h @@ -1,14 +1,11 @@ -#ifndef _WLR_BACKEND_WAYLAND_INTERNAL_H -#define _WLR_BACKEND_WAYLAND_INTERNAL_H +#ifndef WLR_BACKEND_WAYLAND_H +#define WLR_BACKEND_WAYLAND_H #include #include -#include +#include -struct wlr_wl_backend; - -void wlr_wl_backend_free(struct wlr_wl_backend *backend); -struct wlr_wl_backend *wlr_wl_backend_init(struct wl_display *display, +struct wlr_backend *wlr_wl_backend_create(struct wl_display *display, size_t outputs); #endif From 8fbf1ca3ff060c54ce0855e10a96ce968b89cd5e Mon Sep 17 00:00:00 2001 From: nyorain Date: Mon, 19 Jun 2017 19:05:10 +0200 Subject: [PATCH 02/11] Continue wayland backend update Update wayland backend to new api. Start to use the input interfaces. Compiling now, not tested. --- backend/wayland/backend.c | 9 ++- backend/wayland/registry.c | 110 ++++++++++++++++++++---------------- backend/wayland/wl_output.c | 33 ++++++----- backend/wayland/wl_seat.c | 76 ++++++++++++++++++------- include/backend/wayland.h | 14 ++++- 5 files changed, 157 insertions(+), 85 deletions(-) diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index dec559f380..53a966964b 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "backend/wayland.h" #include "common/log.h" @@ -34,12 +35,12 @@ static void wlr_wl_backend_destroy(struct wlr_backend_state *state) { // TODO: Free surfaces for (size_t i = 0; state->outputs && i < state->outputs->length; ++i) { - struct wlr_wl_output *output = state->outputs->items[i]; - wlr_wl_output_free(output); + struct wlr_output *output = state->outputs->items[i]; + wlr_output_destroy(output); } list_free(state->outputs); - if (state->seat) wlr_wl_seat_free(state->seat); + if (state->seat) wl_seat_destroy(state->seat); if (state->shm) wl_shm_destroy(state->shm); if (state->shell) wl_shell_destroy(state->shell); if (state->compositor) wl_compositor_destroy(state->compositor); @@ -74,7 +75,9 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display, wlr_log(L_ERROR, "Could not allocate output list"); goto error; } + state->local_display = display; + state->backend = backend; return backend; diff --git a/backend/wayland/registry.c b/backend/wayland/registry.c index 4a639f6277..66ea4b728e 100644 --- a/backend/wayland/registry.c +++ b/backend/wayland/registry.c @@ -2,77 +2,91 @@ #include #include #include +#include +#include "types.h" #include "backend/wayland.h" #include "common/log.h" -static void registry_wl_seat(struct wlr_wl_backend *backend, - struct wl_seat *wl_seat, struct wl_registry *registry, uint32_t version) { - struct wlr_wl_seat *seat; - if (!(seat = calloc(sizeof(struct wlr_wl_seat), 1))) { - wlr_log(L_ERROR, "Failed to allocate wlr_wl_seat"); - goto error; - } - if (!(seat->keyboards = list_create())) { - wlr_log(L_ERROR, "Failed to allocate wlr_wl_seat"); - goto error; - } - if (!(seat->pointers = list_create())) { - wlr_log(L_ERROR, "Failed to allocate wlr_wl_pointer"); - goto error; - } - seat->wl_seat = wl_seat; - wl_seat_set_user_data(wl_seat, backend); - wl_seat_add_listener(wl_seat, &seat_listener, seat); - return; -error: - wlr_wl_seat_free(seat); - return; +// TODO +static void wlr_wl_output_enable(struct wlr_output_state *output, bool enable) { +} + +static bool wlr_wl_output_set_mode(struct wlr_output_state *output, + struct wlr_output_mode *mode) { + return false; +} + +static void wlr_wl_output_transform(struct wlr_output_state *output, + enum wl_output_transform transform) { +} + +static bool wlr_wl_output_set_cursor(struct wlr_output_state *output, + const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) { + return false; } -static void registry_wl_output(struct wlr_wl_backend *backend, - struct wl_output *wl_output, struct wl_registry *registry, uint32_t version) { - struct wlr_wl_output *output; - if (!(output = calloc(sizeof(struct wlr_wl_output), 1))) { +static bool wlr_wl_output_move_cursor(struct wlr_output_state *output, + int x, int y) { + return false; +} + +static void wlr_wl_output_destroy(struct wlr_output_state *output) { + free(output); +} + +static struct wlr_output_impl output_impl = { + .enable = wlr_wl_output_enable, + .set_mode = wlr_wl_output_set_mode, + .transform = wlr_wl_output_transform, + .set_cursor = wlr_wl_output_set_cursor, + .move_cursor = wlr_wl_output_move_cursor, + .destroy = wlr_wl_output_destroy, +}; + +static void registry_wl_output(struct wlr_backend_state *state, + struct wl_output *wl_output, struct wl_registry *registry, uint32_t version) { + struct wlr_output_state *output; + if (!(output = calloc(sizeof(struct wlr_output_state), 1))) { wlr_log(L_ERROR, "Failed to allocate wlr_wl_output"); - goto error; + return; } - if (!(output->modes = list_create())) { - wlr_log(L_ERROR, "Failed to allocate wlr_wl_output"); - goto error; + + struct wlr_output *wlr_output = wlr_output_create(&output_impl, output); + if (!wlr_output) { + free(state); + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return; } - output->wl_output = wl_output; - output->scale = 1; - list_add(backend->outputs, output); - wl_output_set_user_data(wl_output, backend); + + output->output = wl_output; + list_add(state->outputs, output); wl_output_add_listener(wl_output, &output_listener, output); - return; -error: - wlr_wl_output_free(output); + wl_signal_emit(&state->backend->events.output_add, wlr_output); return; } static void registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { - struct wlr_wl_backend *backend = data; + struct wlr_backend_state *state = data; wlr_log(L_DEBUG, "Remote wayland global: %s v%d", interface, version); if (strcmp(interface, wl_compositor_interface.name) == 0) { - backend->compositor = wl_registry_bind(registry, name, + state->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, version); } else if (strcmp(interface, wl_shell_interface.name) == 0) { - backend->shell = wl_registry_bind(registry, name, + state->shell = wl_registry_bind(registry, name, &wl_shell_interface, version); } else if (strcmp(interface, wl_shm_interface.name) == 0) { - backend->shm = wl_registry_bind(registry, name, + state->shm = wl_registry_bind(registry, name, &wl_shm_interface, version); } else if (strcmp(interface, wl_seat_interface.name) == 0) { - struct wl_seat *wl_seat = wl_registry_bind(registry, name, + state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version); - registry_wl_seat(backend, wl_seat, registry, version); + wl_seat_add_listener(state->seat, &seat_listener, state); } else if (strcmp(interface, wl_output_interface.name) == 0) { struct wl_output *wl_output = wl_registry_bind(registry, name, &wl_output_interface, version); - registry_wl_output(backend, wl_output, registry, version); + registry_wl_output(state, wl_output, registry, version); } } @@ -86,8 +100,8 @@ static const struct wl_registry_listener registry_listener = { .global_remove = registry_global_remove }; -void wlr_wlb_registry_poll(struct wlr_wl_backend *backend) { - wl_registry_add_listener(backend->registry, ®istry_listener, backend); - wl_display_dispatch(backend->remote_display); - wl_display_roundtrip(backend->remote_display); +void wlr_wlb_registry_poll(struct wlr_backend_state *state) { + wl_registry_add_listener(state->registry, ®istry_listener, state); + wl_display_dispatch(state->remote_display); + wl_display_roundtrip(state->remote_display); } diff --git a/backend/wayland/wl_output.c b/backend/wayland/wl_output.c index cc4e9ccee7..b3ad575a7c 100644 --- a/backend/wayland/wl_output.c +++ b/backend/wayland/wl_output.c @@ -4,23 +4,27 @@ #include #include #include +#include +#include "types.h" #include "backend/wayland.h" #include "common/log.h" static void wl_output_handle_mode(void *data, struct wl_output *wl_output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { - struct wlr_wl_output *output = data; - assert(output->wl_output == wl_output); - struct wlr_wl_output_mode *mode; - if (!(mode = calloc(sizeof(struct wlr_wl_output_mode), 1))) { - wlr_log(L_ERROR, "Failed to allocate wlr_wl_output_mode"); + struct wlr_output *output = data; + assert(output->state->output == wl_output); + struct wlr_output_mode *mode; + if (!(mode = calloc(sizeof(struct wlr_output_mode), 1))) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); return; } + mode->flags = flags; mode->width = width; mode->height = height; mode->refresh = refresh; list_add(output->modes, mode); + wlr_log(L_DEBUG, "Got mode for output %p: %dx%d @ %.2fHz%s%s", wl_output, width, height, refresh / 1000.0, (flags & WL_OUTPUT_MODE_PREFERRED) ? " (preferred)" : "", @@ -30,15 +34,18 @@ static void wl_output_handle_mode(void *data, struct wl_output *wl_output, static void wl_output_handle_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, int32_t subpixel, const char *make, const char *model, int32_t transform) { - struct wlr_wl_output *output = data; - assert(output->wl_output == wl_output); - output->x = x; - output->y = y; + struct wlr_output *output = data; + assert(output->state->output == wl_output); + + // TODO + // output->x = x; + // output->y = y; + output->phys_width = physical_width; output->phys_height = physical_height; output->subpixel = subpixel; - output->make = strdup(make); - output->model = strdup(model); + strncpy(output->make, make, sizeof(output->make)); + strncpy(output->model, model, sizeof(output->model)); output->transform = transform; wlr_log(L_DEBUG, "Got info for output %p %dx%d (%dmm x %dmm) %s %s", wl_output, (int)x, (int)y, (int)physical_width, (int)physical_height, @@ -46,8 +53,8 @@ static void wl_output_handle_geometry(void *data, struct wl_output *wl_output, } static void wl_output_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) { - struct wlr_wl_output *output = data; - assert(output->wl_output == wl_output); + struct wlr_output *output = data; + assert(output->state->output == wl_output); output->scale = factor; wlr_log(L_DEBUG, "Got scale factor for output %p: %d", wl_output, factor); } diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index bc2b21a815..d56f7d74e1 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -4,49 +4,87 @@ #include #include #include +#include +#include "types.h" #include "backend/wayland.h" #include "common/log.h" +static void wlr_wl_device_destroy(struct wlr_input_device_state *state) { + free(state); +} + +static struct wlr_input_device_impl input_device_impl = { + .destroy = wlr_wl_device_destroy +}; + +static struct wlr_input_device *allocate_device(struct wlr_backend_state *state, + enum wlr_input_device_type type) { + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); + if(!devstate) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return NULL; + } + + // TODO: any way to retrieve those information? + int vendor = 0; + int product = 0; + const char *name = "unknown;wayland"; + struct wlr_input_device *wlr_device = wlr_input_device_create( + type, &input_device_impl, devstate, + name, vendor, product); + if(!wlr_device) { + free(devstate); + return NULL; + } + + list_add(state->devices, wlr_device); + return wlr_device; +} + static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, enum wl_seat_capability caps) { - struct wlr_wl_seat *seat = data; - assert(seat->wl_seat == wl_seat); - struct wlr_wl_backend *backend = wl_seat_get_user_data(wl_seat); - assert(backend); + struct wlr_backend_state *state = data; + assert(state->seat == wl_seat); + // TODO: add listeners and receive input if ((caps & WL_SEAT_CAPABILITY_POINTER)) { wlr_log(L_DEBUG, "seat %p offered pointer", wl_seat); struct wl_pointer *wl_pointer = wl_seat_get_pointer(wl_seat); - struct wlr_wl_pointer *pointer; - if (!(pointer = calloc(sizeof(struct wlr_wl_pointer), 1))) { + + struct wlr_input_device *wlr_device = allocate_device(state, + WLR_INPUT_DEVICE_POINTER); + if(!wlr_device) { wl_pointer_destroy(wl_pointer); - wlr_log(L_ERROR, "Unable to allocate wlr_wl_pointer"); + wlr_log(L_ERROR, "Unable to allocate wl_pointer device"); return; } - pointer->wl_pointer = wl_pointer; - //wl_pointer_add_listener(wl_pointer, &pointer_listener, backend->registry); TODO - } + wlr_device->pointer = wlr_pointer_create(NULL, NULL); + wl_signal_emit(&state->backend->events.input_add, wlr_device); + } if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { wlr_log(L_DEBUG, "seat %p offered keyboard", wl_seat); struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat); - struct wlr_wl_keyboard *keyboard; - if (!(keyboard = calloc(sizeof(struct wlr_wl_pointer), 1))) { - wl_keyboard_destroy(wl_keyboard); - wlr_log(L_ERROR, "Unable to allocate wlr_wl_keyboard"); + struct wlr_input_device *wlr_device = allocate_device(state, + WLR_INPUT_DEVICE_KEYBOARD); + if(!wlr_device) { + wl_keyboard_release(wl_keyboard); + wlr_log(L_ERROR, "Unable to allocate wl_pointer device"); return; } - keyboard->wl_keyboard = wl_keyboard; - //wl_keyboard_add_listener(wl_keyboard, &keyboard_listener, backend->registry); TODO + + wlr_device->keyboard = wlr_keyboard_create(NULL, NULL); + wl_signal_emit(&state->backend->events.input_add, wlr_device); } // TODO: touch } static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name) { - struct wlr_wl_seat *seat = data; - assert(seat->wl_seat == wl_seat); - seat->name = strdup(name); + struct wlr_backend_state *state = data; + assert(state->seat == wl_seat); + state->seatName = strdup(name); } const struct wl_seat_listener seat_listener = { diff --git a/include/backend/wayland.h b/include/backend/wayland.h index b576e56449..9c1a7a00b3 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -4,7 +4,6 @@ #include #include #include -#include #include struct wlr_backend_state { @@ -16,8 +15,19 @@ struct wlr_backend_state { struct wl_compositor *compositor; struct wl_shell *shell; struct wl_shm *shm; - struct wlr_wl_seat *seat; + struct wl_seat *seat; + const char *seatName; + + struct wlr_backend *backend; list_t *outputs; + list_t *devices; +}; + +struct wlr_output_state { + struct wl_output* output; +}; + +struct wlr_input_device_state { }; void wlr_wlb_registry_poll(struct wlr_backend_state *backend); From 10526de444b58ab4959d086538845303a06acfc1 Mon Sep 17 00:00:00 2001 From: nyorain Date: Mon, 19 Jun 2017 19:40:58 +0200 Subject: [PATCH 03/11] Fix furhter wayland backend issues --- backend/backend.c | 4 ++++ backend/wayland/backend.c | 13 ++++++++++--- backend/wayland/registry.c | 19 ++++++++++--------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/backend/backend.c b/backend/backend.c index 02fd74791f..087696eca9 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "backend/libinput.h" #include "backend/udev.h" @@ -42,6 +43,9 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display, struct wlr_session *session) { // TODO: Choose the most appropriate backend for the situation // Attempt DRM+libinput + if(getenv("WAYLAND_DISPLAY") || getenv("_WAYLAND_DISPLAY")) { + return wlr_wl_backend_create(display, 1); + } struct wlr_udev *udev; if (!(udev = wlr_udev_create(display))) { wlr_log(L_ERROR, "Failed to start udev"); diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 53a966964b..cbc5c5d667 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -13,14 +13,16 @@ * the specified display. */ static bool wlr_wl_backend_init(struct wlr_backend_state* state) { + wlr_log(L_INFO, "Initializating wayland backend"); + state->remote_display = wl_display_connect(getenv("_WAYLAND_DISPLAY")); if (!state->remote_display) { - wlr_log(L_ERROR, "Could not connect to remote display"); + wlr_log_errno(L_ERROR, "Could not connect to remote display"); return false; } if (!(state->registry = wl_display_get_registry(state->remote_display))) { - wlr_log(L_ERROR, "Could not obtain reference to remote registry"); + wlr_log_errno(L_ERROR, "Could not obtain reference to remote registry"); return false; } @@ -57,7 +59,7 @@ static struct wlr_backend_impl backend_impl = { struct wlr_backend *wlr_wl_backend_create(struct wl_display *display, size_t outputs) { - wlr_log(L_INFO, "Initalizing wayland backend"); + wlr_log(L_INFO, "Creating wayland backend"); struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); if (!state) { @@ -76,6 +78,11 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display, goto error; } + if (!(state->devices = list_create())) { + wlr_log(L_ERROR, "Could not allocate devices list"); + goto error; + } + state->local_display = display; state->backend = backend; diff --git a/backend/wayland/registry.c b/backend/wayland/registry.c index 66ea4b728e..1cd49ed94b 100644 --- a/backend/wayland/registry.c +++ b/backend/wayland/registry.c @@ -44,23 +44,24 @@ static struct wlr_output_impl output_impl = { }; static void registry_wl_output(struct wlr_backend_state *state, - struct wl_output *wl_output, struct wl_registry *registry, uint32_t version) { - struct wlr_output_state *output; - if (!(output = calloc(sizeof(struct wlr_output_state), 1))) { + struct wl_output *wl_output, struct wl_registry *registry, + uint32_t version) { + struct wlr_output_state *ostate; + if (!(ostate = calloc(sizeof(struct wlr_output_state), 1))) { wlr_log(L_ERROR, "Failed to allocate wlr_wl_output"); return; } - struct wlr_output *wlr_output = wlr_output_create(&output_impl, output); + struct wlr_output *wlr_output = wlr_output_create(&output_impl, ostate); if (!wlr_output) { - free(state); - wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + free(ostate); + wlr_log_errno(L_ERROR, "Allocation failed"); return; } - output->output = wl_output; - list_add(state->outputs, output); - wl_output_add_listener(wl_output, &output_listener, output); + ostate->output = wl_output; + list_add(state->outputs, wlr_output); + wl_output_add_listener(wl_output, &output_listener, wlr_output); wl_signal_emit(&state->backend->events.output_add, wlr_output); return; } From 792d53522578a1410160e40cbb01c206b2bdf840 Mon Sep 17 00:00:00 2001 From: nyorain Date: Mon, 19 Jun 2017 22:36:40 +0200 Subject: [PATCH 04/11] Rework wayland backend output implementation --- .gitignore | 1 + backend/CMakeLists.txt | 2 +- backend/egl.c | 13 +++++- backend/wayland/backend.c | 42 ++++++++++++----- backend/wayland/output.c | 93 +++++++++++++++++++++++++++++++++++++ backend/wayland/registry.c | 65 +------------------------- backend/wayland/wl_output.c | 71 ---------------------------- include/backend/wayland.h | 21 +++++++-- 8 files changed, 155 insertions(+), 153 deletions(-) create mode 100644 backend/wayland/output.c delete mode 100644 backend/wayland/wl_output.c diff --git a/.gitignore b/.gitignore index 5d4a2eca7b..ce85118cb8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ CMakeFiles Makefile cmake_install.cmake install_manifest.txt +.clang_complete *.swp *.o *.a diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 568bfd1b6b..37cee8923f 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -9,7 +9,7 @@ add_library(wlr-backend wayland/backend.c wayland/registry.c wayland/wl_seat.c - wayland/wl_output.c + wayland/output.c drm/backend.c drm/drm.c diff --git a/backend/egl.c b/backend/egl.c index 2aac25b75f..3462113c68 100644 --- a/backend/egl.c +++ b/backend/egl.c @@ -73,6 +73,7 @@ static bool egl_get_config(EGLDisplay disp, EGLConfig *out) { ret = eglGetConfigs(disp, NULL, 0, &count); if (ret == EGL_FALSE || count == 0) { + wlr_log(L_ERROR, "eglGetConfigs returned no configs"); return false; } @@ -80,12 +81,19 @@ static bool egl_get_config(EGLDisplay disp, EGLConfig *out) { ret = eglChooseConfig(disp, NULL, configs, count, &matched); if (ret == EGL_FALSE) { + wlr_log(L_ERROR, "eglChooseConfig failed"); return false; } for (int i = 0; i < matched; ++i) { EGLint gbm_format; + // TODO, see below + // best would probably be to propagate parameter or config + // choose callback + *out = configs[i]; + return true; + if (!eglGetConfigAttrib(disp, configs[i], EGL_NATIVE_VISUAL_ID, @@ -101,6 +109,7 @@ static bool egl_get_config(EGLDisplay disp, EGLConfig *out) { } } + wlr_log(L_ERROR, "no valid egl config found"); return false; } @@ -109,7 +118,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display) { if (!egl_exts()) { return false; } - + if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) { wlr_log(L_ERROR, "Failed to bind to the OpenGL ES API: %s", egl_error()); goto error; @@ -163,7 +172,7 @@ void wlr_egl_free(struct wlr_egl *egl) { eglDestroyContext(egl->display, egl->context); eglTerminate(egl->display); eglReleaseThread(); - eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); } EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window) { diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index cbc5c5d667..41fc7aadf9 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -6,6 +6,8 @@ #include #include "backend/wayland.h" #include "common/log.h" +#include +#include /* * Initializes the wayland backend. Opens a connection to a remote wayland @@ -26,7 +28,20 @@ static bool wlr_wl_backend_init(struct wlr_backend_state* state) { return false; } - wlr_wlb_registry_poll(state); + wlr_wl_registry_poll(state); + if (!(state->compositor) || (!(state->shell))) { + wlr_log_errno(L_ERROR, "Could not obtain retrieve required globals"); + return false; + } + + wlr_egl_init(&state->egl, EGL_PLATFORM_WAYLAND_KHR, state->remote_display); + for(size_t i = 0; i < state->num_outputs; ++i) { + if(!(state->outputs[i] = wlr_wl_output_create(state, i))) { + wlr_log_errno(L_ERROR, "Failed to create %zuth output", i); + return false; + } + } + return true; } @@ -35,13 +50,12 @@ static void wlr_wl_backend_destroy(struct wlr_backend_state *state) { return; } - // TODO: Free surfaces - for (size_t i = 0; state->outputs && i < state->outputs->length; ++i) { - struct wlr_output *output = state->outputs->items[i]; - wlr_output_destroy(output); + for (size_t i = 0; i < state->num_outputs; ++i) { + wlr_output_destroy(state->outputs[i]); } - list_free(state->outputs); + wlr_egl_free(&state->egl); + free(state->outputs); if (state->seat) wl_seat_destroy(state->seat); if (state->shm) wl_shm_destroy(state->shm); if (state->shell) wl_shell_destroy(state->shell); @@ -58,7 +72,7 @@ static struct wlr_backend_impl backend_impl = { struct wlr_backend *wlr_wl_backend_create(struct wl_display *display, - size_t outputs) { + size_t num_outputs) { wlr_log(L_INFO, "Creating wayland backend"); struct wlr_backend_state *state = calloc(1, sizeof(struct wlr_backend_state)); @@ -73,22 +87,28 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display, return NULL; } - if (!(state->outputs = list_create())) { - wlr_log(L_ERROR, "Could not allocate output list"); + if (!(state->devices = list_create())) { + wlr_log(L_ERROR, "Could not allocate devices list"); goto error; } - if (!(state->devices = list_create())) { - wlr_log(L_ERROR, "Could not allocate devices list"); + if (!(state->outputs = calloc(sizeof(void*), num_outputs))) { + wlr_log(L_ERROR, "Could not allocate outputs list"); goto error; } state->local_display = display; state->backend = backend; + state->num_outputs = num_outputs; return backend; error: + if (state) { + free(state->outputs); + free(state->devices); + free(state->devices); + } free(state); free(backend); return NULL; diff --git a/backend/wayland/output.c b/backend/wayland/output.c new file mode 100644 index 0000000000..e6c7cf9930 --- /dev/null +++ b/backend/wayland/output.c @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include "types.h" +#include "backend/wayland.h" +#include "common/log.h" + +// TODO +static void wlr_wl_output_enable(struct wlr_output_state *output, bool enable) { +} + +static bool wlr_wl_output_set_mode(struct wlr_output_state *output, + struct wlr_output_mode *mode) { + output->output->current_mode = mode; + return true; +} + +static void wlr_wl_output_transform(struct wlr_output_state *output, + enum wl_output_transform transform) { +} + +static bool wlr_wl_output_set_cursor(struct wlr_output_state *output, + const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) { + return false; +} + +static bool wlr_wl_output_move_cursor(struct wlr_output_state *output, + int x, int y) { + return false; +} + +static void wlr_wl_output_destroy(struct wlr_output_state *output) { + // TODO: free egl surface + wl_shell_surface_destroy(output->shell_surface); + wl_surface_destroy(output->surface); + free(output); +} + +static struct wlr_output_impl output_impl = { + .enable = wlr_wl_output_enable, + .set_mode = wlr_wl_output_set_mode, + .transform = wlr_wl_output_transform, + .set_cursor = wlr_wl_output_set_cursor, + .move_cursor = wlr_wl_output_move_cursor, + .destroy = wlr_wl_output_destroy, +}; + +struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, + size_t id) { + // TODO: dont hardcode stuff like size + static unsigned int width = 1100; + static unsigned int height = 720; + + struct wlr_output_state *ostate; + if (!(ostate = calloc(sizeof(struct wlr_output_state), 1))) { + wlr_log(L_ERROR, "Failed to allocate wlr_output_state"); + return NULL; + } + + struct wlr_output *wlr_output = wlr_output_create(&output_impl, ostate); + if (!wlr_output) { + free(ostate); + wlr_log_errno(L_ERROR, "Allocation failed"); + return NULL; + } + + wlr_output->width = width; + wlr_output->height = height; + wlr_output->scale = 1; + strncpy(wlr_output->make, "wayland-output", sizeof(wlr_output->make)); + strncpy(wlr_output->model, "wayland-output", sizeof(wlr_output->model)); + strncpy(wlr_output->name, "wayland-output", sizeof(wlr_output->name)); + + struct wlr_output_mode mode = { + .width = width, + .height = height, + .refresh = 60, + .flags = 0, + }; + list_add(wlr_output->modes, &mode); + + ostate->id = id; + ostate->output = wlr_output; + ostate->surface = wl_compositor_create_surface(backend->compositor); + ostate->shell_surface = wl_shell_get_shell_surface(backend->shell, ostate->surface); + ostate->egl_window = wl_egl_window_create(ostate->surface, width, height); + ostate->egl_surface = wlr_egl_create_surface(&backend->egl, ostate->egl_window); + + wl_signal_emit(&backend->backend->events.output_add, wlr_output); + return wlr_output; +} diff --git a/backend/wayland/registry.c b/backend/wayland/registry.c index 1cd49ed94b..dfe3e37ab0 100644 --- a/backend/wayland/registry.c +++ b/backend/wayland/registry.c @@ -7,65 +7,6 @@ #include "backend/wayland.h" #include "common/log.h" -// TODO -static void wlr_wl_output_enable(struct wlr_output_state *output, bool enable) { -} - -static bool wlr_wl_output_set_mode(struct wlr_output_state *output, - struct wlr_output_mode *mode) { - return false; -} - -static void wlr_wl_output_transform(struct wlr_output_state *output, - enum wl_output_transform transform) { -} - -static bool wlr_wl_output_set_cursor(struct wlr_output_state *output, - const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) { - return false; -} - -static bool wlr_wl_output_move_cursor(struct wlr_output_state *output, - int x, int y) { - return false; -} - -static void wlr_wl_output_destroy(struct wlr_output_state *output) { - free(output); -} - -static struct wlr_output_impl output_impl = { - .enable = wlr_wl_output_enable, - .set_mode = wlr_wl_output_set_mode, - .transform = wlr_wl_output_transform, - .set_cursor = wlr_wl_output_set_cursor, - .move_cursor = wlr_wl_output_move_cursor, - .destroy = wlr_wl_output_destroy, -}; - -static void registry_wl_output(struct wlr_backend_state *state, - struct wl_output *wl_output, struct wl_registry *registry, - uint32_t version) { - struct wlr_output_state *ostate; - if (!(ostate = calloc(sizeof(struct wlr_output_state), 1))) { - wlr_log(L_ERROR, "Failed to allocate wlr_wl_output"); - return; - } - - struct wlr_output *wlr_output = wlr_output_create(&output_impl, ostate); - if (!wlr_output) { - free(ostate); - wlr_log_errno(L_ERROR, "Allocation failed"); - return; - } - - ostate->output = wl_output; - list_add(state->outputs, wlr_output); - wl_output_add_listener(wl_output, &output_listener, wlr_output); - wl_signal_emit(&state->backend->events.output_add, wlr_output); - return; -} - static void registry_global(void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) { struct wlr_backend_state *state = data; @@ -84,10 +25,6 @@ static void registry_global(void *data, struct wl_registry *registry, state->seat = wl_registry_bind(registry, name, &wl_seat_interface, version); wl_seat_add_listener(state->seat, &seat_listener, state); - } else if (strcmp(interface, wl_output_interface.name) == 0) { - struct wl_output *wl_output = wl_registry_bind(registry, name, - &wl_output_interface, version); - registry_wl_output(state, wl_output, registry, version); } } @@ -101,7 +38,7 @@ static const struct wl_registry_listener registry_listener = { .global_remove = registry_global_remove }; -void wlr_wlb_registry_poll(struct wlr_backend_state *state) { +void wlr_wl_registry_poll(struct wlr_backend_state *state) { wl_registry_add_listener(state->registry, ®istry_listener, state); wl_display_dispatch(state->remote_display); wl_display_roundtrip(state->remote_display); diff --git a/backend/wayland/wl_output.c b/backend/wayland/wl_output.c deleted file mode 100644 index b3ad575a7c..0000000000 --- a/backend/wayland/wl_output.c +++ /dev/null @@ -1,71 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include -#include "types.h" -#include "backend/wayland.h" -#include "common/log.h" - -static void wl_output_handle_mode(void *data, struct wl_output *wl_output, - uint32_t flags, int32_t width, int32_t height, int32_t refresh) { - struct wlr_output *output = data; - assert(output->state->output == wl_output); - struct wlr_output_mode *mode; - if (!(mode = calloc(sizeof(struct wlr_output_mode), 1))) { - wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); - return; - } - - mode->flags = flags; - mode->width = width; - mode->height = height; - mode->refresh = refresh; - list_add(output->modes, mode); - - wlr_log(L_DEBUG, "Got mode for output %p: %dx%d @ %.2fHz%s%s", - wl_output, width, height, refresh / 1000.0, - (flags & WL_OUTPUT_MODE_PREFERRED) ? " (preferred)" : "", - (flags & WL_OUTPUT_MODE_CURRENT) ? " (current)" : ""); -} - -static void wl_output_handle_geometry(void *data, struct wl_output *wl_output, - int32_t x, int32_t y, int32_t physical_width, int32_t physical_height, - int32_t subpixel, const char *make, const char *model, int32_t transform) { - struct wlr_output *output = data; - assert(output->state->output == wl_output); - - // TODO - // output->x = x; - // output->y = y; - - output->phys_width = physical_width; - output->phys_height = physical_height; - output->subpixel = subpixel; - strncpy(output->make, make, sizeof(output->make)); - strncpy(output->model, model, sizeof(output->model)); - output->transform = transform; - wlr_log(L_DEBUG, "Got info for output %p %dx%d (%dmm x %dmm) %s %s", - wl_output, (int)x, (int)y, (int)physical_width, (int)physical_height, - make, model); -} - -static void wl_output_handle_scale(void *data, struct wl_output *wl_output, int32_t factor) { - struct wlr_output *output = data; - assert(output->state->output == wl_output); - output->scale = factor; - wlr_log(L_DEBUG, "Got scale factor for output %p: %d", wl_output, factor); -} - -static void wl_output_handle_done(void *data, struct wl_output *wl_output) { - // TODO: notify of changes? Probably not necessary for this backend -} - -const struct wl_output_listener output_listener = { - .mode = wl_output_handle_mode, - .geometry = wl_output_handle_geometry, - .scale = wl_output_handle_scale, - .done = wl_output_handle_done -}; diff --git a/include/backend/wayland.h b/include/backend/wayland.h index 9c1a7a00b3..4cad565d76 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -3,8 +3,10 @@ #include #include +#include #include #include +#include "backend/egl.h" struct wlr_backend_state { /* local state */ @@ -19,20 +21,31 @@ struct wlr_backend_state { const char *seatName; struct wlr_backend *backend; - list_t *outputs; list_t *devices; + + size_t num_outputs; + struct wlr_output **outputs; + struct wlr_egl egl; }; struct wlr_output_state { - struct wl_output* output; + size_t id; + struct wlr_output *output; + struct wl_surface *surface; + struct wl_shell_surface *shell_surface; + struct wl_egl_window* egl_window; + void* egl_surface; }; struct wlr_input_device_state { + enum wlr_input_device_type type; + void *resource; }; -void wlr_wlb_registry_poll(struct wlr_backend_state *backend); +void wlr_wl_registry_poll(struct wlr_backend_state *backend); +struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, + size_t id); extern const struct wl_seat_listener seat_listener; -extern const struct wl_output_listener output_listener; #endif From e22626ef6ae4769f37972ccd161e9d029d60f497 Mon Sep 17 00:00:00 2001 From: nyorain Date: Mon, 19 Jun 2017 22:39:20 +0200 Subject: [PATCH 05/11] Adapt codestyle --- backend/backend.c | 2 +- backend/wayland/backend.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/backend.c b/backend/backend.c index 087696eca9..30ced3e807 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -43,7 +43,7 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display, struct wlr_session *session) { // TODO: Choose the most appropriate backend for the situation // Attempt DRM+libinput - if(getenv("WAYLAND_DISPLAY") || getenv("_WAYLAND_DISPLAY")) { + if (getenv("WAYLAND_DISPLAY") || getenv("_WAYLAND_DISPLAY")) { return wlr_wl_backend_create(display, 1); } struct wlr_udev *udev; diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 41fc7aadf9..6d3e2f321b 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -35,7 +35,7 @@ static bool wlr_wl_backend_init(struct wlr_backend_state* state) { } wlr_egl_init(&state->egl, EGL_PLATFORM_WAYLAND_KHR, state->remote_display); - for(size_t i = 0; i < state->num_outputs; ++i) { + for (size_t i = 0; i < state->num_outputs; ++i) { if(!(state->outputs[i] = wlr_wl_output_create(state, i))) { wlr_log_errno(L_ERROR, "Failed to create %zuth output", i); return false; From 62b1421635581fbffc50eac680bb5a5d7a0b8535 Mon Sep 17 00:00:00 2001 From: nyorain Date: Tue, 20 Jun 2017 18:26:29 +0200 Subject: [PATCH 06/11] Make egl_error function part of the egl interface The function may be useful for backends using egl. --- backend/egl.c | 2 +- include/backend/egl.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/egl.c b/backend/egl.c index 3462113c68..cfee59cefe 100644 --- a/backend/egl.c +++ b/backend/egl.c @@ -6,7 +6,7 @@ #include "backend/egl.h" #include "common/log.h" -static const char *egl_error(void) { +const char *egl_error(void) { switch (eglGetError()) { case EGL_SUCCESS: return "Success"; diff --git a/include/backend/egl.h b/include/backend/egl.h index 8cef36b76f..0422a52bbf 100644 --- a/include/backend/egl.h +++ b/include/backend/egl.h @@ -10,6 +10,7 @@ struct wlr_egl { EGLContext context; }; +const char *egl_error(void); bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display); void wlr_egl_free(struct wlr_egl *egl); EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window); From 50ce35e9da4e2604b0adb15703cfcd87204472e5 Mon Sep 17 00:00:00 2001 From: nyorain Date: Tue, 20 Jun 2017 18:27:05 +0200 Subject: [PATCH 07/11] Implement wayland backend output --- backend/wayland/backend.c | 27 +++++++++++- backend/wayland/output.c | 88 +++++++++++++++++++++++++++++++++++++-- include/backend/wayland.h | 6 +++ 3 files changed, 117 insertions(+), 4 deletions(-) diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 6d3e2f321b..e57cfed97d 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -9,6 +9,23 @@ #include #include +static int dispatch_events(int fd, uint32_t mask, void *data) { + struct wlr_backend_state *state = data; + + int count = 0; + if(mask & WL_EVENT_READABLE) + count = wl_display_dispatch(state->remote_display); + if(mask & WL_EVENT_WRITABLE) + count = wl_display_flush(state->remote_display); + + if (mask == 0) { + count = wl_display_dispatch_pending(state->remote_display); + wl_display_flush(state->remote_display); + } + + return count; +} + /* * Initializes the wayland backend. Opens a connection to a remote wayland * compositor and creates surfaces for each output, then registers globals on @@ -34,7 +51,7 @@ static bool wlr_wl_backend_init(struct wlr_backend_state* state) { return false; } - wlr_egl_init(&state->egl, EGL_PLATFORM_WAYLAND_KHR, state->remote_display); + wlr_egl_init(&state->egl, EGL_PLATFORM_WAYLAND_EXT, state->remote_display); for (size_t i = 0; i < state->num_outputs; ++i) { if(!(state->outputs[i] = wlr_wl_output_create(state, i))) { wlr_log_errno(L_ERROR, "Failed to create %zuth output", i); @@ -42,6 +59,14 @@ static bool wlr_wl_backend_init(struct wlr_backend_state* state) { } } + struct wl_event_loop *loop = wl_display_get_event_loop(state->local_display); + int fd = wl_display_get_fd(state->remote_display); + int events = WL_EVENT_READABLE | WL_EVENT_ERROR | + WL_EVENT_HANGUP | WL_EVENT_WRITABLE; + state->remote_display_src = wl_event_loop_add_fd(loop, fd, events, + dispatch_events, state); + wl_event_source_check(state->remote_display_src); + return true; } diff --git a/backend/wayland/output.c b/backend/wayland/output.c index e6c7cf9930..f8751c3ca0 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -1,19 +1,70 @@ +#include #include #include #include #include #include +#include #include "types.h" #include "backend/wayland.h" #include "common/log.h" -// TODO +static struct wl_callback_listener frame_listener; + +static void surface_frame_callback(void *data, struct wl_callback *cb, uint32_t time) { + struct wlr_output_state *output = data; + assert(output); + + if (!eglMakeCurrent(output->backend->egl.display, + output->egl_surface, output->egl_surface, + output->backend->egl.context)) { + wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + return; + } + + wl_signal_emit(&output->output->events.frame, output->output); + output->frame_callback = wl_surface_frame(output->surface); + wl_callback_add_listener(output->frame_callback, &frame_listener, output); + wl_callback_destroy(cb); + + if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) { + wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error()); + return; + } +} + +static struct wl_callback_listener frame_listener = { + .done = surface_frame_callback +}; + +// TODO: enable/cursor etc static void wlr_wl_output_enable(struct wlr_output_state *output, bool enable) { } static bool wlr_wl_output_set_mode(struct wlr_output_state *output, struct wlr_output_mode *mode) { output->output->current_mode = mode; + + // start rendering loop per callbacks by rendering first frame + if (!eglMakeCurrent(output->backend->egl.display, + output->egl_surface, output->egl_surface, + output->backend->egl.context)) { + wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + return false; + } + + glViewport(0, 0, output->width, output->height); + glClearColor(1.0, 1.0, 1.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + output->frame_callback = wl_surface_frame(output->surface); + wl_callback_add_listener(output->frame_callback, &frame_listener, output); + + if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) { + wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error()); + return false; + } + return true; } @@ -47,11 +98,32 @@ static struct wlr_output_impl output_impl = { .destroy = wlr_wl_output_destroy, }; +void handle_ping(void* data, struct wl_shell_surface* ssurface, uint32_t serial) { + struct wlr_output_state *output = data; + assert(output && output->shell_surface == ssurface); + wl_shell_surface_pong(ssurface, serial); +} + +void handle_configure(void *data, struct wl_shell_surface *wl_shell_surface, + uint32_t edges, int32_t width, int32_t height){ + wlr_log(L_DEBUG, "resize %d %d", width, height); +} + +void handle_popup_done(void *data, struct wl_shell_surface *wl_shell_surface) { + wlr_log(L_ERROR, "Unexpected call"); +} + +static struct wl_shell_surface_listener shell_surface_listener = { + .ping = handle_ping, + .configure = handle_configure, + .popup_done = handle_popup_done +}; + struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, size_t id) { // TODO: dont hardcode stuff like size - static unsigned int width = 1100; - static unsigned int height = 720; + static unsigned int width = 800; + static unsigned int height = 500; struct wlr_output_state *ostate; if (!(ostate = calloc(sizeof(struct wlr_output_state), 1))) { @@ -82,9 +154,19 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, list_add(wlr_output->modes, &mode); ostate->id = id; + ostate->width = width; + ostate->height = height; + ostate->backend = backend; ostate->output = wlr_output; + + // TODO: error handling ostate->surface = wl_compositor_create_surface(backend->compositor); ostate->shell_surface = wl_shell_get_shell_surface(backend->shell, ostate->surface); + + wl_shell_surface_set_class(ostate->shell_surface, "sway"); + wl_shell_surface_set_title(ostate->shell_surface, "sway-wl"); + wl_shell_surface_add_listener(ostate->shell_surface, &shell_surface_listener, ostate); + ostate->egl_window = wl_egl_window_create(ostate->surface, width, height); ostate->egl_surface = wlr_egl_create_surface(&backend->egl, ostate->egl_window); diff --git a/include/backend/wayland.h b/include/backend/wayland.h index 4cad565d76..d2f1fb1db9 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -23,6 +23,8 @@ struct wlr_backend_state { struct wlr_backend *backend; list_t *devices; + struct wl_event_source* remote_display_src; + size_t num_outputs; struct wlr_output **outputs; struct wlr_egl egl; @@ -30,10 +32,14 @@ struct wlr_backend_state { struct wlr_output_state { size_t id; + unsigned int width; + unsigned int height; + struct wlr_backend_state *backend; struct wlr_output *output; struct wl_surface *surface; struct wl_shell_surface *shell_surface; struct wl_egl_window* egl_window; + struct wl_callback* frame_callback; void* egl_surface; }; From 500e9184f1081753f85df7f0a6d13b6b62a53ee3 Mon Sep 17 00:00:00 2001 From: nyorain Date: Tue, 20 Jun 2017 18:44:11 +0200 Subject: [PATCH 08/11] Fix egl workaround; Test with weston --- backend/egl.c | 13 ++++++------- backend/wayland/output.c | 1 + 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/backend/egl.c b/backend/egl.c index cfee59cefe..1ce2d8a763 100644 --- a/backend/egl.c +++ b/backend/egl.c @@ -68,7 +68,7 @@ static bool egl_exts() { return true; } -static bool egl_get_config(EGLDisplay disp, EGLConfig *out) { +static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLenum platform) { EGLint count = 0, matched = 0, ret; ret = eglGetConfigs(disp, NULL, 0, &count); @@ -88,11 +88,10 @@ static bool egl_get_config(EGLDisplay disp, EGLConfig *out) { for (int i = 0; i < matched; ++i) { EGLint gbm_format; - // TODO, see below - // best would probably be to propagate parameter or config - // choose callback - *out = configs[i]; - return true; + if(platform == EGL_PLATFORM_WAYLAND_EXT) { + *out = configs[i]; + return true; + } if (!eglGetConfigAttrib(disp, configs[i], @@ -136,7 +135,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display) { goto error; } - if (!egl_get_config(egl->display, &egl->config)) { + if (!egl_get_config(egl->display, &egl->config, platform)) { wlr_log(L_ERROR, "Failed to get EGL config"); goto error; } diff --git a/backend/wayland/output.c b/backend/wayland/output.c index f8751c3ca0..3ec167bcf8 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -166,6 +166,7 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, wl_shell_surface_set_class(ostate->shell_surface, "sway"); wl_shell_surface_set_title(ostate->shell_surface, "sway-wl"); wl_shell_surface_add_listener(ostate->shell_surface, &shell_surface_listener, ostate); + wl_shell_surface_set_toplevel(ostate->shell_surface); ostate->egl_window = wl_egl_window_create(ostate->surface, width, height); ostate->egl_surface = wlr_egl_create_surface(&backend->egl, ostate->egl_window); From 51f35e7af0f7f01381b702b7630763fb9354701a Mon Sep 17 00:00:00 2001 From: nyorain Date: Tue, 20 Jun 2017 19:01:20 +0200 Subject: [PATCH 09/11] Cleanup some leaks/incorrect destructions --- backend/wayland/backend.c | 7 +++++++ backend/wayland/output.c | 4 +++- backend/wayland/wl_seat.c | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index e57cfed97d..9623a168b3 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -6,6 +6,7 @@ #include #include "backend/wayland.h" #include "common/log.h" +#include "types.h" #include #include @@ -79,6 +80,12 @@ static void wlr_wl_backend_destroy(struct wlr_backend_state *state) { wlr_output_destroy(state->outputs[i]); } + for (size_t i = 0; state->devices && i < state->devices->length; ++i) { + wlr_input_device_destroy(state->devices->items[i]); + } + + list_free(state->devices); + wlr_egl_free(&state->egl); free(state->outputs); if (state->seat) wl_seat_destroy(state->seat); diff --git a/backend/wayland/output.c b/backend/wayland/output.c index 3ec167bcf8..32f2deae87 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -83,7 +83,9 @@ static bool wlr_wl_output_move_cursor(struct wlr_output_state *output, } static void wlr_wl_output_destroy(struct wlr_output_state *output) { - // TODO: free egl surface + if(output->frame_callback) wl_callback_destroy(output->frame_callback); + eglDestroySurface(output->backend->egl.display, output->surface); + wl_egl_window_destroy(output->egl_window); wl_shell_surface_destroy(output->shell_surface); wl_surface_destroy(output->surface); free(output); diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index d56f7d74e1..240cbbb08d 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -61,6 +61,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, } wlr_device->pointer = wlr_pointer_create(NULL, NULL); + list_add(state->devices, wlr_device); wl_signal_emit(&state->backend->events.input_add, wlr_device); } if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { @@ -75,6 +76,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, } wlr_device->keyboard = wlr_keyboard_create(NULL, NULL); + list_add(state->devices, wlr_device); wl_signal_emit(&state->backend->events.input_add, wlr_device); } From c9d5a0b2fb5686627344922138c3c603cae364bc Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 20 Jun 2017 15:29:27 -0400 Subject: [PATCH 10/11] Clean up Wayland backend GL context and such --- backend/wayland/output.c | 7 ++++--- example/pointer.c | 18 +++++++++--------- example/rotation.c | 14 ++++++++------ example/shared.c | 6 ++---- example/simple.c | 10 +++++----- example/tablet.c | 18 +++++++++--------- example/touch.c | 16 ++++++++-------- render/gles3/renderer.c | 3 +++ 8 files changed, 48 insertions(+), 44 deletions(-) diff --git a/backend/wayland/output.c b/backend/wayland/output.c index 32f2deae87..ee97741534 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -143,9 +144,9 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, wlr_output->width = width; wlr_output->height = height; wlr_output->scale = 1; - strncpy(wlr_output->make, "wayland-output", sizeof(wlr_output->make)); - strncpy(wlr_output->model, "wayland-output", sizeof(wlr_output->model)); - strncpy(wlr_output->name, "wayland-output", sizeof(wlr_output->name)); + 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-%d", 1); struct wlr_output_mode mode = { .width = width, diff --git a/example/pointer.c b/example/pointer.c index ddb665a10b..1da77b854a 100644 --- a/example/pointer.c +++ b/example/pointer.c @@ -110,22 +110,22 @@ int main(int argc, char *argv[]) { .default_color = { 0.25f, 0.25f, 0.25f, 1 }, .clear_color = { 0.25f, 0.25f, 0.25f, 1 } }; - struct compositor_state compositor; - + struct compositor_state compositor = { 0, + .data = &state, + .output_add_cb = handle_output_add, + .output_frame_cb = handle_output_frame, + .keyboard_key_cb = handle_keyboard_key, + .pointer_motion_cb = handle_pointer_motion, + .pointer_button_cb = handle_pointer_button, + .pointer_axis_cb = handle_pointer_axis, + }; compositor_init(&compositor); - compositor.output_add_cb = handle_output_add; - compositor.output_frame_cb = handle_output_frame; - compositor.keyboard_key_cb = handle_keyboard_key; - compositor.pointer_motion_cb = handle_pointer_motion; - compositor.pointer_button_cb = handle_pointer_button; - compositor.pointer_axis_cb = handle_pointer_axis; state.renderer = wlr_gles3_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, cat_tex.width, cat_tex.height, cat_tex.pixel_data); - compositor.data = &state; compositor_run(&compositor); wlr_surface_destroy(state.cat_texture); diff --git a/example/rotation.c b/example/rotation.c index f4b7625e17..a1c77013fc 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -193,22 +194,23 @@ static void parse_args(int argc, char *argv[], struct wl_list *config) { int main(int argc, char *argv[]) { struct sample_state state = { 0 }; - struct compositor_state compositor; wl_list_init(&state.config); parse_args(argc, argv, &state.config); + struct compositor_state compositor = { 0, + .data = &state, + .output_add_cb = handle_output_add, + .output_remove_cb = handle_output_remove, + .output_frame_cb = handle_output_frame, + .keyboard_key_cb = handle_keyboard_key, + }; compositor_init(&compositor); - compositor.output_add_cb = handle_output_add; - compositor.output_remove_cb = handle_output_remove; - compositor.output_frame_cb = handle_output_frame; - compositor.keyboard_key_cb = handle_keyboard_key; state.renderer = wlr_gles3_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, cat_tex.width, cat_tex.height, cat_tex.pixel_data); - compositor.data = &state; compositor_run(&compositor); wlr_surface_destroy(state.cat_texture); diff --git a/example/shared.c b/example/shared.c index be1bdee7bf..0fe7270fbd 100644 --- a/example/shared.c +++ b/example/shared.c @@ -418,8 +418,6 @@ static void output_remove_notify(struct wl_listener *listener, void *data) { } void compositor_init(struct compositor_state *state) { - memset(state, 0, sizeof(struct compositor_state)); - state->display = wl_display_create(); state->event_loop = wl_display_get_event_loop(state->display); state->session = wlr_session_start(state->display); @@ -457,14 +455,14 @@ void compositor_init(struct compositor_state *state) { state->backend = wlr; clock_gettime(CLOCK_MONOTONIC, &state->last_frame); -} -void compositor_run(struct compositor_state *state) { if (!wlr_backend_init(state->backend)) { fprintf(stderr, "Failed to initialize backend\n"); exit(1); } +} +void compositor_run(struct compositor_state *state) { while (!state->exit) { wl_event_loop_dispatch(state->event_loop, 0); } diff --git a/example/simple.c b/example/simple.c index 5a5a130014..f3a66db9b6 100644 --- a/example/simple.c +++ b/example/simple.c @@ -50,11 +50,11 @@ int main() { .color = { 1.0, 0.0, 0.0 }, .dec = 0, }; - struct compositor_state compositor; - + struct compositor_state compositor = { 0, + .data = &state, + .output_frame_cb = handle_output_frame, + .keyboard_key_cb = handle_keyboard_key, + }; compositor_init(&compositor); - compositor.output_frame_cb = handle_output_frame; - compositor.keyboard_key_cb = handle_keyboard_key; - compositor.data = &state; compositor_run(&compositor); } diff --git a/example/tablet.c b/example/tablet.c index 3c5b583e5f..6be006f090 100644 --- a/example/tablet.c +++ b/example/tablet.c @@ -145,19 +145,19 @@ int main(int argc, char *argv[]) { .tool_color = { 1, 1, 1, 1 }, .pad_color = { 0.75, 0.75, 0.75, 1.0 } }; - struct compositor_state compositor; - + struct compositor_state compositor = { 0, + .data = &state, + .output_frame_cb = handle_output_frame, + .keyboard_key_cb = handle_keyboard_key, + .tool_axis_cb = handle_tool_axis, + .tool_proximity_cb = handle_tool_proximity, + .tool_button_cb = handle_tool_button, + .pad_button_cb = handle_pad_button, + }; compositor_init(&compositor); - compositor.output_frame_cb = handle_output_frame; - compositor.keyboard_key_cb = handle_keyboard_key; - compositor.tool_axis_cb = handle_tool_axis; - compositor.tool_proximity_cb = handle_tool_proximity; - compositor.tool_button_cb = handle_tool_button; - compositor.pad_button_cb = handle_pad_button; state.renderer = wlr_gles3_renderer_init(); - compositor.data = &state; compositor_run(&compositor); wlr_renderer_destroy(state.renderer); diff --git a/example/touch.c b/example/touch.c index 46eddf62ee..1b4233bbcc 100644 --- a/example/touch.c +++ b/example/touch.c @@ -100,21 +100,21 @@ int main(int argc, char *argv[]) { struct sample_state state = { .touch_points = list_create() }; - struct compositor_state compositor; - + struct compositor_state compositor = { 0, + .data = &state, + .output_frame_cb = handle_output_frame, + .keyboard_key_cb = handle_keyboard_key, + .touch_down_cb = handle_touch_down, + .touch_up_cb = handle_touch_up, + .touch_motion_cb = handle_touch_motion, + }; compositor_init(&compositor); - compositor.output_frame_cb = handle_output_frame; - compositor.keyboard_key_cb = handle_keyboard_key; - compositor.touch_down_cb = handle_touch_down; - compositor.touch_up_cb = handle_touch_up; - compositor.touch_motion_cb = handle_touch_motion; state.renderer = wlr_gles3_renderer_init(); state.cat_texture = wlr_render_surface_init(state.renderer); wlr_surface_attach_pixels(state.cat_texture, GL_RGBA, cat_tex.width, cat_tex.height, cat_tex.pixel_data); - compositor.data = &state; compositor_run(&compositor); wlr_surface_destroy(state.cat_texture); diff --git a/render/gles3/renderer.c b/render/gles3/renderer.c index b005619345..d029c0fb77 100644 --- a/render/gles3/renderer.c +++ b/render/gles3/renderer.c @@ -71,6 +71,7 @@ static void init_default_shaders() { if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) { goto error; } + wlr_log(L_DEBUG, "Compiled default shaders"); return; error: wlr_log(L_ERROR, "Failed to set up default shaders!"); @@ -141,8 +142,10 @@ static bool wlr_gles3_render_surface(struct wlr_renderer_state *state, GL_CALL(glUseProgram(shaders.rgba)); break; default: + wlr_log(L_ERROR, "No shader for this surface format"); return false; } + gles3_flush_errors(); GL_CALL(glBindVertexArray(vao)); GL_CALL(glBindBuffer(GL_ARRAY_BUFFER, vbo)); GL_CALL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo)); From b9d36c8149536cff1aa229f59337dcfa2f70a37b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 20 Jun 2017 17:51:45 -0400 Subject: [PATCH 11/11] Add dynamic output resizing for Wayland backend This allows outputs to: - Not support modesetting - Resize themselves --- backend/drm/drm.c | 1 + backend/wayland/backend.c | 9 ++-- backend/wayland/output.c | 98 ++++++++++++++------------------------- example/pointer.c | 17 ++++--- example/rotation.c | 13 +++--- example/shared.c | 4 +- include/backend/wayland.h | 2 - include/types.h | 1 + include/wlr/types.h | 2 + types/wlr_output.c | 18 ++++++- 10 files changed, 75 insertions(+), 90 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index befbfb9406..5afdf3c3da 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -260,6 +260,7 @@ static bool wlr_drm_output_set_mode(struct wlr_output_state *output, output->width = output->wlr_output->width = mode->width; output->height = output->wlr_output->height = mode->height; output->wlr_output->current_mode = mode; + wl_signal_emit(&output->wlr_output->events.resolution, output->wlr_output); if (!display_init_renderer(&state->renderer, output)) { wlr_log(L_ERROR, "Failed to initalise renderer for %s", output->wlr_output->name); diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 9623a168b3..344cc35da3 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -12,18 +12,17 @@ static int dispatch_events(int fd, uint32_t mask, void *data) { struct wlr_backend_state *state = data; - int count = 0; - if(mask & WL_EVENT_READABLE) + if (mask & WL_EVENT_READABLE) { count = wl_display_dispatch(state->remote_display); - if(mask & WL_EVENT_WRITABLE) + } + if (mask & WL_EVENT_WRITABLE) { count = wl_display_flush(state->remote_display); - + } if (mask == 0) { count = wl_display_dispatch_pending(state->remote_display); wl_display_flush(state->remote_display); } - return count; } diff --git a/backend/wayland/output.c b/backend/wayland/output.c index ee97741534..de4d2f4d98 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -38,49 +38,9 @@ static struct wl_callback_listener frame_listener = { .done = surface_frame_callback }; -// TODO: enable/cursor etc -static void wlr_wl_output_enable(struct wlr_output_state *output, bool enable) { -} - -static bool wlr_wl_output_set_mode(struct wlr_output_state *output, - struct wlr_output_mode *mode) { - output->output->current_mode = mode; - - // start rendering loop per callbacks by rendering first frame - if (!eglMakeCurrent(output->backend->egl.display, - output->egl_surface, output->egl_surface, - output->backend->egl.context)) { - wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); - return false; - } - - glViewport(0, 0, output->width, output->height); - glClearColor(1.0, 1.0, 1.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); - - output->frame_callback = wl_surface_frame(output->surface); - wl_callback_add_listener(output->frame_callback, &frame_listener, output); - - if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) { - wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error()); - return false; - } - - return true; -} - static void wlr_wl_output_transform(struct wlr_output_state *output, enum wl_output_transform transform) { -} - -static bool wlr_wl_output_set_cursor(struct wlr_output_state *output, - const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) { - return false; -} - -static bool wlr_wl_output_move_cursor(struct wlr_output_state *output, - int x, int y) { - return false; + // TODO } static void wlr_wl_output_destroy(struct wlr_output_state *output) { @@ -93,11 +53,7 @@ static void wlr_wl_output_destroy(struct wlr_output_state *output) { } static struct wlr_output_impl output_impl = { - .enable = wlr_wl_output_enable, - .set_mode = wlr_wl_output_set_mode, .transform = wlr_wl_output_transform, - .set_cursor = wlr_wl_output_set_cursor, - .move_cursor = wlr_wl_output_move_cursor, .destroy = wlr_wl_output_destroy, }; @@ -109,11 +65,18 @@ void handle_ping(void* data, struct wl_shell_surface* ssurface, uint32_t serial) void handle_configure(void *data, struct wl_shell_surface *wl_shell_surface, uint32_t edges, int32_t width, int32_t height){ - wlr_log(L_DEBUG, "resize %d %d", width, height); + struct wlr_output_state *ostate = data; + assert(ostate && ostate->shell_surface == wl_shell_surface); + struct wlr_output *output = ostate->output; + wl_egl_window_resize(ostate->egl_window, width, height, 0, 0); + output->width = width; + output->height = height; + wlr_output_update_matrix(output); + wl_signal_emit(&output->events.resolution, output); } void handle_popup_done(void *data, struct wl_shell_surface *wl_shell_surface) { - wlr_log(L_ERROR, "Unexpected call"); + wlr_log(L_ERROR, "Unexpected wl_shell_surface.popup_done event"); } static struct wl_shell_surface_listener shell_surface_listener = { @@ -124,10 +87,6 @@ static struct wl_shell_surface_listener shell_surface_listener = { struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, size_t id) { - // TODO: dont hardcode stuff like size - static unsigned int width = 800; - static unsigned int height = 500; - struct wlr_output_state *ostate; if (!(ostate = calloc(sizeof(struct wlr_output_state), 1))) { wlr_log(L_ERROR, "Failed to allocate wlr_output_state"); @@ -141,24 +100,14 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, return NULL; } - wlr_output->width = width; - wlr_output->height = height; + wlr_output->width = 640; + wlr_output->height = 480; wlr_output->scale = 1; 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-%d", 1); - struct wlr_output_mode mode = { - .width = width, - .height = height, - .refresh = 60, - .flags = 0, - }; - list_add(wlr_output->modes, &mode); - ostate->id = id; - ostate->width = width; - ostate->height = height; ostate->backend = backend; ostate->output = wlr_output; @@ -171,9 +120,30 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend_state* backend, wl_shell_surface_add_listener(ostate->shell_surface, &shell_surface_listener, ostate); wl_shell_surface_set_toplevel(ostate->shell_surface); - ostate->egl_window = wl_egl_window_create(ostate->surface, width, height); + ostate->egl_window = wl_egl_window_create(ostate->surface, + wlr_output->width, wlr_output->height); ostate->egl_surface = wlr_egl_create_surface(&backend->egl, ostate->egl_window); + // start rendering loop per callbacks by rendering first frame + if (!eglMakeCurrent(ostate->backend->egl.display, + ostate->egl_surface, ostate->egl_surface, + ostate->backend->egl.context)) { + wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + return false; + } + + glViewport(0, 0, wlr_output->width, wlr_output->height); + glClearColor(1.0, 1.0, 1.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + ostate->frame_callback = wl_surface_frame(ostate->surface); + wl_callback_add_listener(ostate->frame_callback, &frame_listener, ostate); + + if (!eglSwapBuffers(ostate->backend->egl.display, ostate->egl_surface)) { + wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error()); + return false; + } + wl_signal_emit(&backend->backend->events.output_add, wlr_output); return wlr_output; } diff --git a/example/pointer.c b/example/pointer.c index 1da77b854a..8735c61ea5 100644 --- a/example/pointer.c +++ b/example/pointer.c @@ -110,15 +110,14 @@ int main(int argc, char *argv[]) { .default_color = { 0.25f, 0.25f, 0.25f, 1 }, .clear_color = { 0.25f, 0.25f, 0.25f, 1 } }; - struct compositor_state compositor = { 0, - .data = &state, - .output_add_cb = handle_output_add, - .output_frame_cb = handle_output_frame, - .keyboard_key_cb = handle_keyboard_key, - .pointer_motion_cb = handle_pointer_motion, - .pointer_button_cb = handle_pointer_button, - .pointer_axis_cb = handle_pointer_axis, - }; + struct compositor_state compositor = { 0 }; + compositor.data = &state; + compositor.output_add_cb = handle_output_add; + compositor.output_frame_cb = handle_output_frame; + compositor.keyboard_key_cb = handle_keyboard_key; + compositor.pointer_motion_cb = handle_pointer_motion; + compositor.pointer_button_cb = handle_pointer_button; + compositor.pointer_axis_cb = handle_pointer_axis; compositor_init(&compositor); state.renderer = wlr_gles3_renderer_init(); diff --git a/example/rotation.c b/example/rotation.c index a1c77013fc..d7b6b169cc 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -197,13 +197,12 @@ int main(int argc, char *argv[]) { wl_list_init(&state.config); parse_args(argc, argv, &state.config); - struct compositor_state compositor = { 0, - .data = &state, - .output_add_cb = handle_output_add, - .output_remove_cb = handle_output_remove, - .output_frame_cb = handle_output_frame, - .keyboard_key_cb = handle_keyboard_key, - }; + struct compositor_state compositor = { 0 }; + compositor.data = &state; + compositor.output_add_cb = handle_output_add; + compositor.output_remove_cb = handle_output_remove; + compositor.output_frame_cb = handle_output_frame; + compositor.keyboard_key_cb = handle_keyboard_key; compositor_init(&compositor); state.renderer = wlr_gles3_renderer_init(); diff --git a/example/shared.c b/example/shared.c index 0fe7270fbd..6af3042b9c 100644 --- a/example/shared.c +++ b/example/shared.c @@ -383,7 +383,9 @@ static void output_add_notify(struct wl_listener *listener, void *data) { fprintf(stderr, "Output '%s' added\n", output->name); fprintf(stderr, "%s %s %"PRId32"mm x %"PRId32"mm\n", output->make, output->model, output->phys_width, output->phys_height); - wlr_output_set_mode(output, output->modes->items[0]); + if (output->modes->length > 0) { + wlr_output_set_mode(output, output->modes->items[0]); + } struct output_state *ostate = calloc(1, sizeof(struct output_state)); clock_gettime(CLOCK_MONOTONIC, &ostate->last_frame); ostate->output = output; diff --git a/include/backend/wayland.h b/include/backend/wayland.h index d2f1fb1db9..03667c9aa5 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -32,8 +32,6 @@ struct wlr_backend_state { struct wlr_output_state { size_t id; - unsigned int width; - unsigned int height; struct wlr_backend_state *backend; struct wlr_output *output; struct wl_surface *surface; diff --git a/include/types.h b/include/types.h index 54aa56066f..f53cac022e 100644 --- a/include/types.h +++ b/include/types.h @@ -20,6 +20,7 @@ struct wlr_output_impl { struct wlr_output *wlr_output_create(struct wlr_output_impl *impl, struct wlr_output_state *state); void wlr_output_free(struct wlr_output *output); +void wlr_output_update_matrix(struct wlr_output *output); struct wlr_keyboard_impl { void (*destroy)(struct wlr_keyboard_state *state); diff --git a/include/wlr/types.h b/include/wlr/types.h index 1d2abab586..6b6a338987 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -33,11 +33,13 @@ struct wlr_output { float transform_matrix[16]; + /* Note: some backends may have zero modes */ list_t *modes; struct wlr_output_mode *current_mode; struct { struct wl_signal frame; + struct wl_signal resolution; } events; }; diff --git a/types/wlr_output.c b/types/wlr_output.c index 8c21f70682..28996fa75f 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -65,6 +65,10 @@ static void set_matrix(float mat[static 16], int32_t width, int32_t height, mat[15] = 1.0f; } +void wlr_output_update_matrix(struct wlr_output *output) { + set_matrix(output->transform_matrix, output->width, output->height, output->transform); +} + struct wlr_output *wlr_output_create(struct wlr_output_impl *impl, struct wlr_output_state *state) { struct wlr_output *output = calloc(1, sizeof(struct wlr_output)); @@ -73,6 +77,7 @@ struct wlr_output *wlr_output_create(struct wlr_output_impl *impl, output->modes = list_create(); output->transform = WL_OUTPUT_TRANSFORM_NORMAL; wl_signal_init(&output->events.frame); + wl_signal_init(&output->events.resolution); return output; } @@ -81,22 +86,31 @@ void wlr_output_enable(struct wlr_output *output, bool enable) { } bool wlr_output_set_mode(struct wlr_output *output, struct wlr_output_mode *mode) { - set_matrix(output->transform_matrix, mode->width, mode->height, output->transform); + if (!output->impl || !output->impl->set_mode) { + return false; + } + wlr_output_update_matrix(output); return output->impl->set_mode(output->state, mode); } void wlr_output_transform(struct wlr_output *output, enum wl_output_transform transform) { - set_matrix(output->transform_matrix, output->width, output->height, transform); + wlr_output_update_matrix(output); output->impl->transform(output->state, transform); } bool wlr_output_set_cursor(struct wlr_output *output, const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) { + if (!output->impl || !output->impl->set_cursor) { + return false; + } return output->impl->set_cursor(output->state, buf, stride, width, height); } bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) { + if (!output->impl || !output->impl->move_cursor) { + return false; + } return output->impl->move_cursor(output->state, x, y); }