mirror of
https://gitlab.freedesktop.org/libevdev/libevdev.git
synced 2025-12-20 16:10:07 +01:00
Split libevdev_new() into _new and _new_from_fd()
libevdev_new() can only fail on allocation failures, but with an fd the failure options are more. Split into a new function that can return an error code. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
parent
0867f2f10a
commit
9dee4db9d7
4 changed files with 49 additions and 17 deletions
|
|
@ -144,24 +144,39 @@ libevdev_noop_log_func(const char *format, va_list args)
|
|||
}
|
||||
|
||||
struct libevdev*
|
||||
libevdev_new(int fd)
|
||||
libevdev_new(void)
|
||||
{
|
||||
struct libevdev *dev;
|
||||
|
||||
dev = calloc(1, sizeof(*dev));
|
||||
if (!dev)
|
||||
return NULL;
|
||||
dev->fd = -1;
|
||||
dev->num_slots = -1;
|
||||
dev->current_slot = -1;
|
||||
dev->log = libevdev_noop_log_func;
|
||||
|
||||
if (fd >= 0)
|
||||
libevdev_set_fd(dev, fd);
|
||||
dev->fd = fd;
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
int
|
||||
libevdev_new_from_fd(int fd, struct libevdev **dev)
|
||||
{
|
||||
struct libevdev *d;
|
||||
int rc;
|
||||
|
||||
d = libevdev_new();
|
||||
if (!d)
|
||||
return -ENOSPC;
|
||||
|
||||
rc = libevdev_set_fd(d, fd);
|
||||
if (rc < 0)
|
||||
libevdev_free(d);
|
||||
else
|
||||
*dev = d;
|
||||
return rc;
|
||||
}
|
||||
|
||||
void
|
||||
libevdev_free(struct libevdev *dev)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -34,14 +34,29 @@ enum EvdevReadFlags {
|
|||
|
||||
|
||||
/**
|
||||
* Initialize a new libevdev struct.
|
||||
*
|
||||
* @param fd If fd >= 0, the device is initialised for the fd. Otherwise, a
|
||||
* caller must call evdev_set_fd() before attempting to read events.
|
||||
*
|
||||
* Initialize a new libevdev device.
|
||||
*
|
||||
* @see libevdev_set_fd
|
||||
*/
|
||||
struct libevdev* libevdev_new(int fd);
|
||||
struct libevdev* libevdev_new(void);
|
||||
|
||||
/**
|
||||
* Initialize a new libevdev device from the given fd.
|
||||
*
|
||||
* This is a shortcut for
|
||||
*
|
||||
* <pre>
|
||||
* struct libevdev *dev = libevdev_new();
|
||||
* libevdev_set_fd(dev, fd);
|
||||
* </pre>
|
||||
*
|
||||
* @param fd A file descriptor to the device in O_RDWR or O_RDONLY mode.
|
||||
*
|
||||
* @return On success, zero is returned and dev is set to the newly
|
||||
* allocated struct. On failure, a negative errno is returned and the value
|
||||
* of dev is undefined.
|
||||
*/
|
||||
int libevdev_new_from_fd(int fd, struct libevdev **dev);
|
||||
|
||||
/**
|
||||
* Clean up and free the libevdev struct.
|
||||
|
|
|
|||
|
|
@ -90,9 +90,9 @@ main(int argc, char **argv)
|
|||
goto out;
|
||||
}
|
||||
|
||||
dev = libevdev_new(fd);
|
||||
if (!dev) {
|
||||
fprintf(stderr, "Failed to init libevdev\n");
|
||||
rc = libevdev_new_from_fd(fd, &dev);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
|
||||
goto out;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@
|
|||
#include <config.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
|
@ -111,6 +112,7 @@ main(int argc, char **argv)
|
|||
struct libevdev *dev;
|
||||
const char *file;
|
||||
int fd;
|
||||
int rc;
|
||||
|
||||
|
||||
if (argc < 2)
|
||||
|
|
@ -123,9 +125,9 @@ main(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
dev = libevdev_new(fd);
|
||||
if (!dev) {
|
||||
fprintf(stderr, "Failed to init libevdev\n");
|
||||
rc = libevdev_new_from_fd(fd, &dev);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue