From af69591e6233c83ed749b2f51922edb45bdaef2e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 11:38:38 -0400 Subject: Add error handling and init in backend_autocreate --- include/wlr/backend/libinput.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 include/wlr/backend/libinput.h (limited to 'include/wlr/backend') diff --git a/include/wlr/backend/libinput.h b/include/wlr/backend/libinput.h new file mode 100644 index 00000000..1e8c3555 --- /dev/null +++ b/include/wlr/backend/libinput.h @@ -0,0 +1,12 @@ +#ifndef WLR_BACKEND_LIBINPUT_H +#define WLR_BACKEND_LIBINPUT_H + +#include +#include +#include +#include + +struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, + struct wlr_session *session, struct wlr_udev *udev); + +#endif -- cgit v1.2.3 From 7a5f35b5bb2a30e46defe9ea1bd610cb06df95e8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 12:21:54 -0400 Subject: Reassign ownership of libinput handle --- backend/backend.c | 6 ++++++ backend/libinput/events.c | 15 ++++++++++++++- backend/libinput/keyboard.c | 15 +-------------- include/backend/libinput.h | 8 ++++---- include/types.h | 10 ++++++++-- include/wlr/backend/libinput.h | 3 +++ include/wlr/types.h | 7 +++++++ types/wlr_input_device.c | 23 +++++++++++++++++++++-- 8 files changed, 64 insertions(+), 23 deletions(-) (limited to 'include/wlr/backend') diff --git a/backend/backend.c b/backend/backend.c index 83249a8c..03c662c3 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -3,10 +3,12 @@ #include #include #include +#include #include #include #include #include +#include "backend/libinput.h" #include "backend/udev.h" #include "common/log.h" @@ -64,3 +66,7 @@ error_udev: error: return NULL; } + +struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev) { + return dev->state->handle; +} diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 3c623de4..60a2fd5e 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -24,6 +24,15 @@ struct wlr_input_device *get_appropriate_device( return NULL; } +static void wlr_libinput_device_destroy(struct wlr_input_device_state *state) { + libinput_device_unref(state->handle); + free(state); +} + +static struct wlr_input_device_impl input_device_impl = { + .destroy = wlr_libinput_device_destroy +}; + static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); @@ -38,10 +47,13 @@ static void handle_device_added(struct wlr_backend_state *state, const char *name = libinput_device_get_name(device); list_t *devices = list_create(); wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { struct wlr_input_device *wlr_device = wlr_input_device_create( - WLR_INPUT_DEVICE_KEYBOARD, name, vendor, product); + WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, + name, vendor, product); wlr_device->keyboard = wlr_libinput_keyboard_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); list_add(devices, wlr_device); @@ -68,6 +80,7 @@ static void handle_device_added(struct wlr_backend_state *state, if (devices->length > 0) { libinput_device_set_user_data(device, devices); } else { + wlr_libinput_device_destroy(devstate); list_free(devices); } } diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index afae960e..158ded28 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -8,23 +8,10 @@ #include "common/log.h" #include "types.h" -static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { - libinput_device_unref(state->handle); - free(state); -} - -static struct wlr_keyboard_impl keyboard_impl = { - .destroy = wlr_libinput_keyboard_destroy -}; - struct wlr_keyboard *wlr_libinput_keyboard_create( struct libinput_device *device) { assert(device); - struct wlr_keyboard_state *kbstate = - calloc(1, sizeof(struct wlr_keyboard_state)); - kbstate->handle = device; - libinput_device_ref(device); - return wlr_keyboard_create(&keyboard_impl, kbstate); + return wlr_keyboard_create(NULL, NULL); } void handle_keyboard_key(struct libinput_event *event, diff --git a/include/backend/libinput.h b/include/backend/libinput.h index a59ab401..f6622695 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -19,6 +19,10 @@ struct wlr_backend_state { list_t *keyboards; }; +struct wlr_input_device_state { + struct libinput_device *handle; +}; + void wlr_libinput_event(struct wlr_backend_state *state, struct libinput_event *event); @@ -26,10 +30,6 @@ struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *device); -struct wlr_keyboard_state { - struct libinput_device *handle; -}; - void handle_keyboard_key(struct libinput_event *event, struct libinput_device *device); struct wlr_keyboard *wlr_libinput_keyboard_create( diff --git a/include/types.h b/include/types.h index 564b24ec..20f5ed13 100644 --- a/include/types.h +++ b/include/types.h @@ -34,9 +34,15 @@ struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, struct wlr_pointer_state *state); void wlr_pointer_destroy(struct wlr_pointer *pointer); +struct wlr_input_device_impl { + void (*destroy)(struct wlr_input_device_state *state); +}; + struct wlr_input_device *wlr_input_device_create( - enum wlr_input_device_type type, const char *name, - int vendor, int product); + enum wlr_input_device_type type, + struct wlr_input_device_impl *impl, + struct wlr_input_device_state *state, + const char *name, int vendor, int product); void wlr_input_device_destroy(struct wlr_input_device *dev); #endif diff --git a/include/wlr/backend/libinput.h b/include/wlr/backend/libinput.h index 1e8c3555..7108f42c 100644 --- a/include/wlr/backend/libinput.h +++ b/include/wlr/backend/libinput.h @@ -1,12 +1,15 @@ #ifndef WLR_BACKEND_LIBINPUT_H #define WLR_BACKEND_LIBINPUT_H +#include #include #include #include #include +#include struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_session *session, struct wlr_udev *udev); +struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev); #endif diff --git a/include/wlr/types.h b/include/wlr/types.h index e2f3075f..389989e8 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -152,10 +152,17 @@ enum wlr_input_device_type { WLR_INPUT_DEVICE_SWITCH, }; +struct wlr_input_device_state; +struct wlr_input_device_impl; + struct wlr_input_device { + struct wlr_input_device_state *state; + struct wlr_input_device_impl *impl; + enum wlr_input_device_type type; int vendor, product; char *name; + union { void *_device; struct wlr_keyboard *keyboard; diff --git a/types/wlr_input_device.c b/types/wlr_input_device.c index 7cbe2f7e..dbb25487 100644 --- a/types/wlr_input_device.c +++ b/types/wlr_input_device.c @@ -4,13 +4,18 @@ #include #include #include +#include "common/log.h" #include "types.h" struct wlr_input_device *wlr_input_device_create( - enum wlr_input_device_type type, const char *name, - int vendor, int product) { + enum wlr_input_device_type type, + struct wlr_input_device_impl *impl, + struct wlr_input_device_state *state, + const char *name, int vendor, int product) { struct wlr_input_device *dev = calloc(1, sizeof(struct wlr_input_device)); dev->type = type; + dev->impl = impl; + dev->state = state; dev->name = strdup(name); dev->vendor = vendor; dev->product = product; @@ -19,6 +24,20 @@ struct wlr_input_device *wlr_input_device_create( void wlr_input_device_destroy(struct wlr_input_device *dev) { if (!dev) return; + if (dev->impl && dev->impl->destroy && dev->state) { + dev->impl->destroy(dev->state); + } + if (dev->_device) { + switch (dev->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + wlr_keyboard_destroy(dev->keyboard); + break; + default: + wlr_log(L_DEBUG, "Warning: leaking memory %p %p %d", + dev->_device, dev, dev->type); + break; + } + } free(dev->name); free(dev); } -- cgit v1.2.3 From e257afeb7ea72a035202be02c90490eb3cb7129b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 12 Jun 2017 22:22:40 -0400 Subject: Establish multi backend support --- backend/CMakeLists.txt | 1 + backend/backend.c | 26 ++++++++--- backend/multi/backend.c | 107 ++++++++++++++++++++++++++++++++++++++++++++ include/backend/multi.h | 13 ++++++ include/wlr/backend/multi.h | 10 +++++ 5 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 backend/multi/backend.c create mode 100644 include/backend/multi.h create mode 100644 include/wlr/backend/multi.h (limited to 'include/wlr/backend') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 5505e417..23914240 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -18,6 +18,7 @@ add_library(wlr-backend libinput/events.c libinput/keyboard.c + multi/backend.c backend.c egl.c udev.c diff --git a/backend/backend.c b/backend/backend.c index 03c662c3..02fd7479 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "backend/libinput.h" #include "backend/udev.h" #include "common/log.h" @@ -46,19 +47,32 @@ struct wlr_backend *wlr_backend_autocreate(struct wl_display *display, wlr_log(L_ERROR, "Failed to start udev"); goto error; } - struct wlr_backend *wlr; - wlr = wlr_libinput_backend_create(display, session, udev); - return wlr; int gpu = wlr_udev_find_gpu(udev, session); if (gpu == -1) { wlr_log(L_ERROR, "Failed to open DRM device"); goto error_udev; } - wlr = wlr_drm_backend_create(display, session, udev, gpu); - if (!wlr) { + struct wlr_backend *multi = wlr_multi_backend_create(); + if (!multi) { goto error_gpu; } - return wlr; + struct wlr_backend *libinput = + wlr_libinput_backend_create(display, session, udev); + if (!libinput) { + goto error_multi; + } + struct wlr_backend *drm = + wlr_drm_backend_create(display, session, udev, gpu); + if (!drm) { + goto error_libinput; + } + wlr_multi_backend_add(multi, libinput); + wlr_multi_backend_add(multi, drm); + return multi; +error_libinput: + wlr_backend_destroy(libinput); +error_multi: + wlr_backend_destroy(multi); error_gpu: close(gpu); error_udev: diff --git a/backend/multi/backend.c b/backend/multi/backend.c new file mode 100644 index 00000000..e3d87ccc --- /dev/null +++ b/backend/multi/backend.c @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include "backend/multi.h" +#include "common/log.h" + +struct subbackend_state { + struct wlr_backend *backend; + struct wlr_backend *container; + struct wl_listener input_add; + struct wl_listener input_remove; + struct wl_listener output_add; + struct wl_listener output_remove; +}; + +static bool multi_backend_init(struct wlr_backend_state *state) { + for (size_t i = 0; i < state->backends->length; ++i) { + struct subbackend_state *sub = state->backends->items[i]; + if (!wlr_backend_init(sub->backend)) { + wlr_log(L_ERROR, "Failed to initialize backend %zd", i); + return false; + } + } + return true; +} + +static void multi_backend_destroy(struct wlr_backend_state *state) { + for (size_t i = 0; i < state->backends->length; ++i) { + struct subbackend_state *sub = state->backends->items[i]; + wlr_backend_destroy(sub->backend); + free(sub); + } + list_free(state->backends); + free(state); +} + +struct wlr_backend_impl backend_impl = { + .init = multi_backend_init, + .destroy = multi_backend_destroy +}; + +struct wlr_backend *wlr_multi_backend_create() { + struct wlr_backend_state *state = + calloc(1, sizeof(struct wlr_backend_state)); + if (!state) { + wlr_log(L_ERROR, "Backend allocation failed"); + return NULL; + } + state->backends = list_create(); + if (!state->backends) { + free(state); + wlr_log(L_ERROR, "Backend allocation failed"); + return NULL; + } + struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); + state->backend = backend; + return backend; +} + +static void input_add_reemit(struct wl_listener *listener, void *data) { + struct subbackend_state *state = wl_container_of(listener, + state, input_add); + wl_signal_emit(&state->container->events.input_add, data); +} + +static void input_remove_reemit(struct wl_listener *listener, void *data) { + struct subbackend_state *state = wl_container_of(listener, + state, input_remove); + wl_signal_emit(&state->container->events.input_remove, data); +} + +static void output_add_reemit(struct wl_listener *listener, void *data) { + struct subbackend_state *state = wl_container_of(listener, + state, output_add); + wl_signal_emit(&state->container->events.output_add, data); +} + +static void output_remove_reemit(struct wl_listener *listener, void *data) { + struct subbackend_state *state = wl_container_of(listener, + state, output_remove); + wl_signal_emit(&state->container->events.output_remove, data); +} + +void wlr_multi_backend_add(struct wlr_backend *multi, + struct wlr_backend *backend) { + struct subbackend_state *sub = calloc(1, sizeof(struct subbackend_state)); + sub->backend = backend; + sub->container = multi; + + sub->input_add.notify = input_add_reemit; + sub->input_remove.notify = input_remove_reemit; + sub->output_add.notify = output_add_reemit; + sub->output_remove.notify = output_remove_reemit; + + wl_list_init(&sub->input_add.link); + wl_list_init(&sub->input_remove.link); + wl_list_init(&sub->output_add.link); + wl_list_init(&sub->output_remove.link); + + wl_signal_add(&backend->events.input_add, &sub->input_add); + wl_signal_add(&backend->events.input_remove, &sub->input_remove); + wl_signal_add(&backend->events.output_add, &sub->output_add); + wl_signal_add(&backend->events.output_remove, &sub->output_remove); + + list_add(multi->state->backends, sub); +} diff --git a/include/backend/multi.h b/include/backend/multi.h new file mode 100644 index 00000000..5e6930bb --- /dev/null +++ b/include/backend/multi.h @@ -0,0 +1,13 @@ +#ifndef _WLR_MULTI_BACKEND_INTERNAL +#define _WLR_MULTI_BACKEND_INTERNAL + +#include +#include +#include + +struct wlr_backend_state { + struct wlr_backend *backend; + list_t *backends; +}; + +#endif diff --git a/include/wlr/backend/multi.h b/include/wlr/backend/multi.h new file mode 100644 index 00000000..a07ca770 --- /dev/null +++ b/include/wlr/backend/multi.h @@ -0,0 +1,10 @@ +#ifndef _WLR_BACKEND_MULTI_H +#define _WLR_BACKEND_MULTI_H + +#include + +struct wlr_backend *wlr_multi_backend_create(); +void wlr_multi_backend_add(struct wlr_backend *multi, + struct wlr_backend *backend); + +#endif -- cgit v1.2.3