lur: change new_from_hidraw to return an errno

Makes debugging slightly more useful than just returning NULL.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2016-03-02 14:00:58 +10:00
committed by Benjamin Tissoires
parent 7704c102e4
commit b3237fd001
3 changed files with 16 additions and 11 deletions

View File

@@ -24,6 +24,7 @@
*/
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/ioctl.h>
@@ -141,13 +142,13 @@ hidpp10_init(int fd)
HIDPP10_PROFILE_UNKNOWN);
}
_EXPORT_ struct lur_receiver*
lur_receiver_new_from_hidraw(int fd, void *userdata)
_EXPORT_ int
lur_receiver_new_from_hidraw(int fd, void *userdata, struct lur_receiver **out)
{
struct lur_receiver *receiver;
if (!hidraw_is_receiver(fd))
return NULL;
return -EINVAL;
receiver = zalloc(sizeof(*receiver));
receiver->refcount = 1;
@@ -159,11 +160,12 @@ lur_receiver_new_from_hidraw(int fd, void *userdata)
if (!receiver->hidppdev)
goto error;
return receiver;
*out = receiver;
return 0;
error:
free(receiver);
return NULL;
return -errno;
}

View File

@@ -107,11 +107,13 @@ lur_is_receiver(uint16_t vid, uint16_t pid);
*
* @param fd An O_RDWR file descriptor pointing to a /dev/hidraw node
* @param userdata Caller-specific data
* @param lur Set to the lur object on success, otherwise unmodified
*
* @return A lur object, or NULL on error.
* @return 0 on success or a negative errno on error
* @retval -EINVAL The fd does not point to a lur receiver
*/
struct lur_receiver*
lur_receiver_new_from_hidraw(int fd, void *userdata);
int
lur_receiver_new_from_hidraw(int fd, void *userdata, struct lur_receiver **lur);
/**
* Enumerate devices currently paired with the given receiver.

View File

@@ -41,8 +41,9 @@ enum options {
static struct lur_receiver *
open_receiver(const char *path)
{
struct lur_receiver *receiver;
struct lur_receiver *receiver = NULL;
int fd = -1;
int rc;
fd = open(path, O_RDWR);
if (fd < 0) {
@@ -50,8 +51,8 @@ open_receiver(const char *path)
return NULL;
}
receiver = lur_receiver_new_from_hidraw(fd, NULL);
if (!receiver)
rc = lur_receiver_new_from_hidraw(fd, NULL, &receiver);
if (rc != 0)
close(fd);
return receiver;