mirror of
https://github.com/libratbag/libratbag.git
synced 2026-11-07 23:23:52 +01:00
ratbagd: create libratbag_device contexts
Make sure to create libratbag_device contexts for each device we add. If libratbag returns NULL, we just ignore the devices. Otherwise, we keep the context around and manage the device. Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <libratbag.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -38,6 +39,7 @@ struct ratbagd_device {
|
||||
struct ratbagd *ctx;
|
||||
RBNode node;
|
||||
char *name;
|
||||
struct ratbag_device *lib_device;
|
||||
};
|
||||
|
||||
#define ratbagd_device_from_node(_ptr) \
|
||||
@@ -117,7 +119,8 @@ static const sd_bus_vtable ratbagd_device_vtable[] = {
|
||||
|
||||
int ratbagd_device_new(struct ratbagd_device **out,
|
||||
struct ratbagd *ctx,
|
||||
const char *name)
|
||||
const char *name,
|
||||
struct ratbag_device *lib_device)
|
||||
{
|
||||
_cleanup_(ratbagd_device_freep) struct ratbagd_device *device = NULL;
|
||||
|
||||
@@ -131,6 +134,7 @@ int ratbagd_device_new(struct ratbagd_device **out,
|
||||
|
||||
device->ctx = ctx;
|
||||
rbnode_init(&device->node);
|
||||
device->lib_device = ratbag_device_ref(lib_device);
|
||||
|
||||
device->name = strdup(name);
|
||||
if (!device->name)
|
||||
@@ -148,7 +152,11 @@ struct ratbagd_device *ratbagd_device_free(struct ratbagd_device *device)
|
||||
|
||||
assert(!ratbagd_device_linked(device));
|
||||
|
||||
device->lib_device = ratbag_device_unref(device->lib_device);
|
||||
device->name = mfree(device->name);
|
||||
|
||||
assert(!device->lib_device); /* ratbag yields !NULL if still pinned */
|
||||
|
||||
return mfree(device);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,18 +37,12 @@
|
||||
static void ratbagd_process_device(struct ratbagd *ctx,
|
||||
struct udev_device *udevice)
|
||||
{
|
||||
struct ratbag_device *lib_device;
|
||||
struct ratbagd_device *device;
|
||||
const char *name;
|
||||
int r;
|
||||
|
||||
/*
|
||||
* TODO: So far we just enumerate udev input devices and add a new
|
||||
* ratbag device for each one we find. That's a bit easy. We
|
||||
* should really try to create a libratbag device for the udev
|
||||
* device and only add devices that are actually accepted.
|
||||
* Furthermore, the libratbag object should really be kept as
|
||||
* context in ratbagd_device.
|
||||
*
|
||||
* TODO: libratbag should provide some mechanism to allow
|
||||
* device-grouping, just like libinput does. If multiple input
|
||||
* devices belong to the same virtual device, we should not add
|
||||
@@ -73,7 +67,16 @@ static void ratbagd_process_device(struct ratbagd *ctx,
|
||||
/* device already known, refresh our view of the device */
|
||||
} else {
|
||||
/* device unknown, create new one and link it */
|
||||
r = ratbagd_device_new(&device, ctx, name);
|
||||
lib_device = ratbag_device_new_from_udev_device(ctx->lib_ctx,
|
||||
udevice);
|
||||
if (!lib_device)
|
||||
return; /* unsupported device */
|
||||
|
||||
r = ratbagd_device_new(&device, ctx, name, lib_device);
|
||||
|
||||
/* the ratbagd_device takes its own reference, drop ours */
|
||||
ratbag_device_unref(lib_device);
|
||||
|
||||
if (r < 0) {
|
||||
fprintf(stderr, "Cannot track device '%s'\n", name);
|
||||
return;
|
||||
|
||||
@@ -45,7 +45,8 @@ struct ratbagd_device;
|
||||
|
||||
int ratbagd_device_new(struct ratbagd_device **out,
|
||||
struct ratbagd *ctx,
|
||||
const char *name);
|
||||
const char *name,
|
||||
struct ratbag_device *lib_device);
|
||||
struct ratbagd_device *ratbagd_device_free(struct ratbagd_device *device);
|
||||
bool ratbagd_device_linked(struct ratbagd_device *device);
|
||||
void ratbagd_device_link(struct ratbagd_device *device);
|
||||
|
||||
Reference in New Issue
Block a user