Reassign ownership of libinput handle

This commit is contained in:
Drew DeVault 2017-06-10 12:21:54 -04:00
parent f479b7c8c7
commit 7a5f35b5bb
8 changed files with 64 additions and 23 deletions

View File

@ -3,10 +3,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <libinput.h>
#include <wlr/session.h> #include <wlr/session.h>
#include <wlr/backend/interface.h> #include <wlr/backend/interface.h>
#include <wlr/backend/drm.h> #include <wlr/backend/drm.h>
#include <wlr/backend/libinput.h> #include <wlr/backend/libinput.h>
#include "backend/libinput.h"
#include "backend/udev.h" #include "backend/udev.h"
#include "common/log.h" #include "common/log.h"
@ -64,3 +66,7 @@ error_udev:
error: error:
return NULL; return NULL;
} }
struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev) {
return dev->state->handle;
}

View File

@ -24,6 +24,15 @@ struct wlr_input_device *get_appropriate_device(
return NULL; 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, static void handle_device_added(struct wlr_backend_state *state,
struct libinput_device *device) { struct libinput_device *device) {
assert(state && 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); const char *name = libinput_device_get_name(device);
list_t *devices = list_create(); list_t *devices = list_create();
wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); 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)) { if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
struct wlr_input_device *wlr_device = wlr_input_device_create( 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); wlr_device->keyboard = wlr_libinput_keyboard_create(device);
wl_signal_emit(&state->backend->events.input_add, wlr_device); wl_signal_emit(&state->backend->events.input_add, wlr_device);
list_add(devices, 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) { if (devices->length > 0) {
libinput_device_set_user_data(device, devices); libinput_device_set_user_data(device, devices);
} else { } else {
wlr_libinput_device_destroy(devstate);
list_free(devices); list_free(devices);
} }
} }

View File

@ -8,23 +8,10 @@
#include "common/log.h" #include "common/log.h"
#include "types.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 wlr_keyboard *wlr_libinput_keyboard_create(
struct libinput_device *device) { struct libinput_device *device) {
assert(device); assert(device);
struct wlr_keyboard_state *kbstate = return wlr_keyboard_create(NULL, NULL);
calloc(1, sizeof(struct wlr_keyboard_state));
kbstate->handle = device;
libinput_device_ref(device);
return wlr_keyboard_create(&keyboard_impl, kbstate);
} }
void handle_keyboard_key(struct libinput_event *event, void handle_keyboard_key(struct libinput_event *event,

View File

@ -19,6 +19,10 @@ struct wlr_backend_state {
list_t *keyboards; list_t *keyboards;
}; };
struct wlr_input_device_state {
struct libinput_device *handle;
};
void wlr_libinput_event(struct wlr_backend_state *state, void wlr_libinput_event(struct wlr_backend_state *state,
struct libinput_event *event); struct libinput_event *event);
@ -26,10 +30,6 @@ struct wlr_input_device *get_appropriate_device(
enum wlr_input_device_type desired_type, enum wlr_input_device_type desired_type,
struct libinput_device *device); struct libinput_device *device);
struct wlr_keyboard_state {
struct libinput_device *handle;
};
void handle_keyboard_key(struct libinput_event *event, void handle_keyboard_key(struct libinput_event *event,
struct libinput_device *device); struct libinput_device *device);
struct wlr_keyboard *wlr_libinput_keyboard_create( struct wlr_keyboard *wlr_libinput_keyboard_create(

View File

@ -34,9 +34,15 @@ struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl,
struct wlr_pointer_state *state); struct wlr_pointer_state *state);
void wlr_pointer_destroy(struct wlr_pointer *pointer); 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( struct wlr_input_device *wlr_input_device_create(
enum wlr_input_device_type type, const char *name, enum wlr_input_device_type type,
int vendor, int product); 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); void wlr_input_device_destroy(struct wlr_input_device *dev);
#endif #endif

View File

@ -1,12 +1,15 @@
#ifndef WLR_BACKEND_LIBINPUT_H #ifndef WLR_BACKEND_LIBINPUT_H
#define WLR_BACKEND_LIBINPUT_H #define WLR_BACKEND_LIBINPUT_H
#include <libinput.h>
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/session.h> #include <wlr/session.h>
#include <wlr/backend.h> #include <wlr/backend.h>
#include <wlr/backend/udev.h> #include <wlr/backend/udev.h>
#include <wlr/types.h>
struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display,
struct wlr_session *session, struct wlr_udev *udev); struct wlr_session *session, struct wlr_udev *udev);
struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev);
#endif #endif

View File

@ -152,10 +152,17 @@ enum wlr_input_device_type {
WLR_INPUT_DEVICE_SWITCH, WLR_INPUT_DEVICE_SWITCH,
}; };
struct wlr_input_device_state;
struct wlr_input_device_impl;
struct wlr_input_device { struct wlr_input_device {
struct wlr_input_device_state *state;
struct wlr_input_device_impl *impl;
enum wlr_input_device_type type; enum wlr_input_device_type type;
int vendor, product; int vendor, product;
char *name; char *name;
union { union {
void *_device; void *_device;
struct wlr_keyboard *keyboard; struct wlr_keyboard *keyboard;

View File

@ -4,13 +4,18 @@
#include <wayland-server.h> #include <wayland-server.h>
#include <wlr/types.h> #include <wlr/types.h>
#include <wlr/common/list.h> #include <wlr/common/list.h>
#include "common/log.h"
#include "types.h" #include "types.h"
struct wlr_input_device *wlr_input_device_create( struct wlr_input_device *wlr_input_device_create(
enum wlr_input_device_type type, const char *name, enum wlr_input_device_type type,
int vendor, int product) { 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)); struct wlr_input_device *dev = calloc(1, sizeof(struct wlr_input_device));
dev->type = type; dev->type = type;
dev->impl = impl;
dev->state = state;
dev->name = strdup(name); dev->name = strdup(name);
dev->vendor = vendor; dev->vendor = vendor;
dev->product = product; 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) { void wlr_input_device_destroy(struct wlr_input_device *dev) {
if (!dev) return; 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->name);
free(dev); free(dev);
} }