Rework wayland backend output implementation

This commit is contained in:
nyorain 2017-06-19 22:36:40 +02:00
parent e65ca967f9
commit 792d535225
8 changed files with 156 additions and 154 deletions

1
.gitignore vendored
View File

@ -3,6 +3,7 @@ CMakeFiles
Makefile
cmake_install.cmake
install_manifest.txt
.clang_complete
*.swp
*.o
*.a

View File

@ -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

View File

@ -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;
}

View File

@ -6,6 +6,8 @@
#include <wlr/types.h>
#include "backend/wayland.h"
#include "common/log.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
/*
* 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");
goto error;
}
if (!(state->devices = list_create())) {
wlr_log(L_ERROR, "Could not allocate devices list");
goto error;
}
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;

93
backend/wayland/output.c Normal file
View File

@ -0,0 +1,93 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
#include <wlr/types.h>
#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;
}

View File

@ -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, &registry_listener, state);
wl_display_dispatch(state->remote_display);
wl_display_roundtrip(state->remote_display);

View File

@ -1,71 +0,0 @@
#define _XOPEN_SOURCE 500
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <wayland-client.h>
#include <wlr/types.h>
#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
};

View File

@ -3,8 +3,10 @@
#include <wayland-client.h>
#include <wayland-server.h>
#include <wayland-egl.h>
#include <wlr/common/list.h>
#include <wlr/backend/wayland.h>
#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