hidraw: move ratbag_device_init_hidraw() in libratbag-hidraw

We are touching hidraw internals here, better having the function in
libratbag-hidraw.c.

No functional changes.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Benjamin Tissoires
2015-10-07 16:13:33 -04:00
parent 936ad60da4
commit 7dfa8cb75e
3 changed files with 77 additions and 68 deletions

View File

@@ -88,6 +88,74 @@ ratbag_hidraw_parse_report_descriptor(struct ratbag_device *device)
return 0;
}
static struct udev_device *
udev_find_hidraw(struct ratbag_device *device)
{
struct ratbag *ratbag = device->ratbag;
struct udev_enumerate *e;
struct udev_list_entry *entry;
struct udev_device *udev_device;
const char *path, *sysname;
struct udev_device *hid_udev;
struct udev_device *hidraw_udev = NULL;
struct udev *udev = ratbag->udev;
hid_udev = udev_device_get_parent_with_subsystem_devtype(device->udev_device, "hid", NULL);
if (!hid_udev)
return NULL;
e = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(e, "hidraw");
udev_enumerate_add_match_parent(e, hid_udev);
udev_enumerate_scan_devices(e);
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
path = udev_list_entry_get_name(entry);
udev_device = udev_device_new_from_syspath(udev, path);
if (!udev_device)
continue;
sysname = udev_device_get_sysname(udev_device);
if (!strneq("hidraw", sysname, 6)) {
udev_device_unref(udev_device);
continue;
}
hidraw_udev = udev_device_ref(udev_device);
udev_device_unref(udev_device);
goto out;
}
out:
udev_enumerate_unref(e);
return hidraw_udev;
}
int
ratbag_device_init_hidraw(struct ratbag_device *device)
{
struct udev_device *hidraw_udev;
int rc = -ENODEV;
hidraw_udev = udev_find_hidraw(device);
if (!hidraw_udev)
goto out;
device->udev_hidraw = udev_device_ref(hidraw_udev);
log_debug(device->ratbag,
"%s is device '%s'.\n",
device->name,
udev_device_get_devnode(device->udev_hidraw));
rc = 0;
udev_device_unref(hidraw_udev);
out:
return rc;
}
int
ratbag_open_hidraw(struct ratbag_device *device)
{

View File

@@ -40,6 +40,15 @@ struct ratbag_hidraw {
unsigned num_report_ids;
};
/**
* Initialize the hidraw internals.
*
* @param device the ratbag device
*
* @return 0 on success or a negative errno on error
*/
int ratbag_device_init_hidraw(struct ratbag_device *device);
/**
* Open the hidraw device associated with the device.
*

View File

@@ -150,74 +150,6 @@ ratbag_log_set_handler(struct ratbag *ratbag,
ratbag->log_handler = log_handler;
}
static struct udev_device *
udev_find_hidraw(struct ratbag_device *device)
{
struct ratbag *ratbag = device->ratbag;
struct udev_enumerate *e;
struct udev_list_entry *entry;
struct udev_device *udev_device;
const char *path, *sysname;
struct udev_device *hid_udev;
struct udev_device *hidraw_udev = NULL;
struct udev *udev = ratbag->udev;
hid_udev = udev_device_get_parent_with_subsystem_devtype(device->udev_device, "hid", NULL);
if (!hid_udev)
return NULL;
e = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(e, "hidraw");
udev_enumerate_add_match_parent(e, hid_udev);
udev_enumerate_scan_devices(e);
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
path = udev_list_entry_get_name(entry);
udev_device = udev_device_new_from_syspath(udev, path);
if (!udev_device)
continue;
sysname = udev_device_get_sysname(udev_device);
if (!strneq("hidraw", sysname, 6)) {
udev_device_unref(udev_device);
continue;
}
hidraw_udev = udev_device_ref(udev_device);
udev_device_unref(udev_device);
goto out;
}
out:
udev_enumerate_unref(e);
return hidraw_udev;
}
static int
ratbag_device_init_hidraw(struct ratbag_device *device)
{
struct udev_device *hidraw_udev;
int rc = -ENODEV;
hidraw_udev = udev_find_hidraw(device);
if (!hidraw_udev)
goto out;
device->udev_hidraw = udev_device_ref(hidraw_udev);
log_debug(device->ratbag,
"%s is device '%s'.\n",
device->name,
udev_device_get_devnode(device->udev_hidraw));
rc = 0;
udev_device_unref(hidraw_udev);
out:
return rc;
}
struct ratbag_device*
ratbag_device_new(struct ratbag *ratbag, struct udev_device *udev_device,
const char *name, const struct input_id *id)