forked from drath/libratbag
hidpp10: make the device init API slightly more sane
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user