aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad_open_device.c
diff options
context:
space:
mode:
Diffstat (limited to 'libgamepad_open_device.c')
-rw-r--r--libgamepad_open_device.c40
1 files changed, 19 insertions, 21 deletions
diff --git a/libgamepad_open_device.c b/libgamepad_open_device.c
index cce0133..3c2a751 100644
--- a/libgamepad_open_device.c
+++ b/libgamepad_open_device.c
@@ -11,42 +11,40 @@ libgamepad_open_device(struct libgamepad_device *devicep, int dirfd, const char
struct input_id id;
devicep->fd = dirfd;
- devicep->close_fd = 0;
- devicep->require_sync = 0;
- devicep->dev = NULL;
+ devicep->internals = NULL;
devicep->buttons = NULL;
devicep->absolute_axes = NULL;
devicep->relative_axes = NULL;
memset(devicep->force_feedback_support, 0, sizeof(devicep->force_feedback_support));
+ devicep->internals = calloc(1, sizeof(*devicep->internals));
+ if (!devicep->internals)
+ return -1;
+
if (path && *path) {
devicep->fd = openat(dirfd, path, mode);
- if (devicep->fd < 0)
+ if (devicep->fd < 0) {
+ libgamepad_close_device(devicep);
return -1;
- devicep->close_fd = 1;
+ }
+ devicep->internals->close_fd = 1;
}
if (ioctl(devicep->fd, EVIOCGID, &id)) {
- if (devicep->close_fd) {
- close(devicep->fd);
- devicep->close_fd = 0;
- }
+ libgamepad_close_device(devicep);
return -1;
}
- err = libevdev_new_from_fd(devicep->fd, &devicep->dev);
+ err = libevdev_new_from_fd(devicep->fd, &devicep->internals->dev);
if (err < 0) {
- if (devicep->close_fd) {
- close(devicep->fd);
- devicep->close_fd = 0;
- }
+ libgamepad_close_device(devicep);
errno = -err;
return -1;
}
- devicep->name = libevdev_get_name(devicep->dev);
- devicep->unique_id = libevdev_get_uniq(devicep->dev);
- devicep->physical_location = libevdev_get_phys(devicep->dev);
+ devicep->name = libevdev_get_name(devicep->internals->dev);
+ devicep->unique_id = libevdev_get_uniq(devicep->internals->dev);
+ devicep->physical_location = libevdev_get_phys(devicep->internals->dev);
devicep->bus_type = (unsigned int)id.bustype;
devicep->vendor = (unsigned int)id.vendor;
devicep->product = (unsigned int)id.product;
@@ -55,26 +53,26 @@ libgamepad_open_device(struct libgamepad_device *devicep, int dirfd, const char
devicep->nbuttons = 0;
for (i = 0; i < KEY_CNT; i++) {
devicep->button_map[i] = -1;
- if (libevdev_has_event_code(devicep->dev, EV_KEY, i))
+ if (libevdev_has_event_code(devicep->internals->dev, EV_KEY, i))
devicep->button_map[i] = (int16_t)devicep->nbuttons++;
}
devicep->nabsolute_axes = 0;
for (i = 0; i < ABS_CNT; i++) {
devicep->absolute_axis_map[i] = -1;
- if (libevdev_has_event_code(devicep->dev, EV_ABS, i))
+ if (libevdev_has_event_code(devicep->internals->dev, EV_ABS, i))
devicep->absolute_axis_map[i] = (int16_t)devicep->nabsolute_axes++;
}
devicep->nrelative_axes = 0;
for (i = 0; i < REL_CNT; i++) {
devicep->relative_axis_map[i] = -1;
- if (libevdev_has_event_code(devicep->dev, EV_REL, i))
+ if (libevdev_has_event_code(devicep->internals->dev, EV_REL, i))
devicep->relative_axis_map[i] = (int16_t)devicep->nrelative_axes++;
}
for (i = 0; i < FF_CNT; i++)
- if (libevdev_has_event_code(devicep->dev, EV_FF, i))
+ if (libevdev_has_event_code(devicep->internals->dev, EV_FF, i))
devicep->force_feedback_support[i / 8] |= (uint8_t)(1 << (i & 7));
if (devicep->nbuttons) {