From 4fca750977902fd7ca3b1745e5fe46513a9c053f Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 25 Aug 2015 12:56:51 +1000 Subject: [PATCH] hidpp10: make the device init API slightly more sane Signed-off-by: Peter Hutterer --- src/driver-hidpp10.c | 21 ++++++------ src/hidpp10.c | 76 ++++++++++++++++++++++++++++---------------- src/hidpp10.h | 7 ++-- 3 files changed, 61 insertions(+), 43 deletions(-) diff --git a/src/driver-hidpp10.c b/src/driver-hidpp10.c index ccb60a05..806707fd 100644 --- a/src/driver-hidpp10.c +++ b/src/driver-hidpp10.c @@ -50,7 +50,7 @@ #include "libratbag-hidraw.h" struct hidpp10drv_data { - struct hidpp10_device dev; + struct hidpp10_device *dev; }; static void @@ -91,7 +91,7 @@ hidpp10drv_read_profile(struct ratbag_profile *profile, unsigned int index) uint16_t xres, yres, refresh; drv_data = ratbag_get_drv_data(device); - hidpp10 = &drv_data->dev; + hidpp10 = drv_data->dev; hidpp10_get_current_resolution(hidpp10, &xres, &yres); hidpp10_get_usb_refresh_rate(hidpp10, &refresh); @@ -186,11 +186,9 @@ hidpp10drv_probe(struct ratbag_device *device, const struct ratbag_id id) { int rc; struct hidpp10drv_data *drv_data; - struct hidpp10_device dev; + struct hidpp10_device *dev; bool is_unifying_receiver; - hidpp10_device_init(device, &dev); - /* check if the device is a unifying receiver first so we can update * the hidraw path before we open it */ is_unifying_receiver = hidpp10drv_is_unifying_receiver(device); @@ -214,12 +212,11 @@ hidpp10drv_probe(struct ratbag_device *device, const struct ratbag_id id) } if (is_unifying_receiver) - rc = hidpp10_get_device_from_wpid(&dev, - id.id.product); + dev = hidpp10_device_new_from_wpid(device, id.id.product); else /* wired devices are 0 */ - rc = hidpp10_get_device_from_idx(&dev, 0); + dev = hidpp10_device_new_from_idx(device, 0); - if (rc) { + if (!dev) { log_error(device->ratbag, "Failed to get HID++1.0 device for %s\n", device->name); @@ -236,7 +233,7 @@ hidpp10drv_probe(struct ratbag_device *device, const struct ratbag_id id) err: free(drv_data); ratbag_set_drv_data(device, NULL); - hidpp10_device_cleanup(&dev); + hidpp10_device_destroy(dev); return rc; } @@ -248,9 +245,9 @@ hidpp10drv_remove(struct ratbag_device *device) struct hidpp10_device *dev; drv_data = ratbag_get_drv_data(device); - dev = &drv_data->dev; + dev = drv_data->dev; - hidpp10_device_cleanup(dev); + hidpp10_device_destroy(dev); free(drv_data); } diff --git a/src/hidpp10.c b/src/hidpp10.c index 269d7672..feb7c9d7 100644 --- a/src/hidpp10.c +++ b/src/hidpp10.c @@ -587,20 +587,21 @@ hidpp10_get_firmare_information(struct hidpp10_device *dev, } void hidpp10_list_devices(struct ratbag_device *device) { - struct hidpp10_device dev; - int i, res; - - hidpp10_device_init(device, &dev); + struct hidpp10_device *dev; + int i; for (i = 0; i < 6; ++i) { - res = hidpp10_get_device_from_idx(&dev, i); - if (res) + dev = hidpp10_device_new_from_idx(device, i); + if (!dev) continue; - log_info(device->ratbag, "[%d] %s %s (Wireless PID: %04x)\n", i, device_types[dev.device_type] ? device_types[dev.device_type] : "", dev.name, dev.wpid); + log_info(device->ratbag, "[%d] %s %s (Wireless PID: %04x)\n", + i, + device_types[dev->device_type] ? device_types[dev->device_type] : "", + dev->name, dev->wpid); + hidpp10_device_destroy(dev); } - hidpp10_device_cleanup(&dev); } /* -------------------------------------------------------------------------- */ @@ -633,41 +634,62 @@ hidpp10_get_device_info(struct hidpp10_device *dev) return 0; } -int -hidpp10_get_device_from_wpid(struct hidpp10_device* dev, uint16_t wpid) +static struct hidpp10_device* +hidpp10_device_new(struct ratbag_device *device, int index) { - int i, res; + struct hidpp10_device *dev; + + dev = zalloc(sizeof(*dev)); + if (!dev) + return NULL; + + dev->index = index; + dev->ratbag_device = ratbag_device_ref(device); + + return dev; +} + +struct hidpp10_device* +hidpp10_device_new_from_wpid(struct ratbag_device* device, uint16_t wpid) +{ + struct hidpp10_device *dev; + int i; for (i = 1; i < 7; i++) { - res = hidpp10_get_device_from_idx(dev, i); - if (res) + dev = hidpp10_device_new_from_idx(device, i); + if (!dev) continue; if (dev->wpid == wpid) - break; + return dev; - res = -1; + hidpp10_device_destroy(dev); } - return res; + return NULL; } -int -hidpp10_get_device_from_idx(struct hidpp10_device *dev, int idx) +struct hidpp10_device* +hidpp10_device_new_from_idx(struct ratbag_device *device, int idx) { + struct hidpp10_device *dev; + + dev = hidpp10_device_new(device, idx); + if (!dev) + return NULL; + dev->index = idx; - return hidpp10_get_device_info(dev); + if (hidpp10_get_device_info(dev) != 0) { + hidpp10_device_destroy(dev); + dev = NULL; + } + + return dev; } void -hidpp10_device_init(struct ratbag_device *device, struct hidpp10_device *dev) -{ - dev->index = 0x00; /* default for wired */ - dev->ratbag_device = ratbag_device_ref(device); -} - -void -hidpp10_device_cleanup(struct hidpp10_device *dev) +hidpp10_device_destroy(struct hidpp10_device *dev) { ratbag_device_unref(dev->ratbag_device); + free(dev); } diff --git a/src/hidpp10.h b/src/hidpp10.h index e1a39e35..a829ac30 100644 --- a/src/hidpp10.h +++ b/src/hidpp10.h @@ -67,15 +67,14 @@ union hidpp10_message { uint8_t data[LONG_MESSAGE_LENGTH]; }; -void hidpp10_device_init(struct ratbag_device *device, struct hidpp10_device *dev); -void hidpp10_device_cleanup(struct hidpp10_device *dev); +void hidpp10_device_destroy(struct hidpp10_device *dev); int hidpp10_request_command(struct hidpp10_device *device, union hidpp10_message *msg); int hidpp10_open_lock(struct hidpp10_device *device); int hidpp10_disconnect(struct hidpp10_device *device, int idx); void hidpp10_list_devices(struct ratbag_device *device); -int hidpp10_get_device_from_wpid(struct hidpp10_device *dev, uint16_t wpid); -int hidpp10_get_device_from_idx(struct hidpp10_device *dev, int idx); +struct hidpp10_device *hidpp10_device_new_from_wpid(struct ratbag_device *device, uint16_t wpid); +struct hidpp10_device *hidpp10_device_new_from_idx(struct ratbag_device *device, int idx); /* -------------------------------------------------------------------------- */ /* 0x00: Enable HID++ Notifications */