summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rcm-server-evdev.c19
-rw-r--r--rcm-server-evdev.h2
-rw-r--r--rcm-server-lirc.c28
-rw-r--r--rcm-server-main.c16
-rw-r--r--rcm-server-udev.c38
-rw-r--r--rcm-server-udev.h2
6 files changed, 70 insertions, 35 deletions
diff --git a/rcm-server-evdev.c b/rcm-server-evdev.c
index 15ae5ff..e39ef3d 100644
--- a/rcm-server-evdev.c
+++ b/rcm-server-evdev.c
@@ -372,11 +372,24 @@ evdev_setup(struct device *device, const char *path)
device->evdev_fd, EPOLLIN, evdev_read, device) < 0) {
printf("Failed to add event source for evdev device %s: %s\n",
path, strerror(errno));
- close(device->evdev_fd);
- device->evdev_fd = -1;
- return -errno;
+ r = -errno;
+ evdev_close(device);
+ return r;
}
return r;
}
+void
+evdev_close(struct device *device)
+{
+ if (!device)
+ return;
+
+ if (device->evdev_fd < 0)
+ return;
+
+ close(device->evdev_fd);
+ device->evdev_fd = -1;
+}
+
diff --git a/rcm-server-evdev.h b/rcm-server-evdev.h
index e9baa6b..a2f4283 100644
--- a/rcm-server-evdev.h
+++ b/rcm-server-evdev.h
@@ -9,5 +9,7 @@ int evdev_get_mapping(struct device *device, unsigned index, unsigned *ret_proto
int evdev_setup(struct device *device, const char *path);
+void evdev_close(struct device *device);
+
#endif
diff --git a/rcm-server-lirc.c b/rcm-server-lirc.c
index e9a82a6..4342cc1 100644
--- a/rcm-server-lirc.c
+++ b/rcm-server-lirc.c
@@ -54,19 +54,11 @@ lirc_read(sd_event_source *s, int fd, uint32_t revents, void *userdata)
return 0;
}
-void
-lirc_close(struct device *device)
-{
- if (device->lirc_fd < 0)
- return;
-
- close(device->lirc_fd);
- device->lirc_fd = -1;
-}
-
int
lirc_setup(struct device *device, const char *path)
{
+ int r;
+
if (!device)
return -EINVAL;
@@ -85,10 +77,24 @@ lirc_setup(struct device *device, const char *path)
device->lirc_fd, EPOLLIN, lirc_read, device) < 0) {
printf("Failed to add event source for lirc device %s: %s\n",
path, strerror(errno));
+ r = -errno;
lirc_close(device);
- return -errno;
+ return r;
}
return 0;
}
+void
+lirc_close(struct device *device)
+{
+ if (!device)
+ return;
+
+ if (device->lirc_fd < 0)
+ return;
+
+ close(device->lirc_fd);
+ device->lirc_fd = -1;
+}
+
diff --git a/rcm-server-main.c b/rcm-server-main.c
index 0e245e3..33ef8a7 100644
--- a/rcm-server-main.c
+++ b/rcm-server-main.c
@@ -883,21 +883,7 @@ free_manager(struct manager *mgr) {
while (!list_empty(&mgr->devices)) {
struct device *dev = list_first_entry(&mgr->devices, typeof(*dev), list);
- list_del(&dev->list);
- free(dev->path);
- free(dev->input_name);
- free(dev->driver_name);
- free(dev->keymap_name);
- free(dev->dev_name);
-
- while (!list_empty(&dev->keymaps)) {
- struct keymap *keymap = list_first_entry(&dev->keymaps, typeof(*keymap), list);
- list_del(&keymap->list);
- free(keymap->name);
- free(keymap);
- }
-
- free(dev);
+ device_free(dev);
}
free(mgr);
}
diff --git a/rcm-server-udev.c b/rcm-server-udev.c
index 797335b..77fb403 100644
--- a/rcm-server-udev.c
+++ b/rcm-server-udev.c
@@ -15,8 +15,34 @@
#include "rcm-server-lirc.h"
#include "rcm-server-udev.h"
+void
+device_free(struct device *device)
+{
+ if (!device)
+ return;
+
+ list_del(&device->list);
+ lirc_close(device);
+ evdev_close(device);
+
+ free(device->path);
+ free(device->input_name);
+ free(device->driver_name);
+ free(device->keymap_name);
+ free(device->dev_name);
+
+ while (!list_empty(&device->keymaps)) {
+ struct keymap *keymap = list_first_entry(&device->keymaps, typeof(*keymap), list);
+ list_del(&keymap->list);
+ free(keymap->name);
+ free(keymap);
+ }
+
+ free(device);
+}
+
static void
-remove_device(struct manager *mgr, struct udev_device *udev)
+device_remove(struct manager *mgr, struct udev_device *udev)
{
const char *name;
struct device *device;
@@ -27,15 +53,15 @@ remove_device(struct manager *mgr, struct udev_device *udev)
list_for_each_entry(device, &mgr->devices, list) {
if (strcmp(device->name, name))
continue;
- list_del(&device->list);
sd_bus_emit_object_removed(mgr->bus, device->path);
+ device_free(device);
mgr->num_devices--;
break;
}
}
static void
-add_device(struct manager *mgr, struct udev_device *udev)
+device_add(struct manager *mgr, struct udev_device *udev)
{
const char *name;
const char *str;
@@ -205,9 +231,9 @@ udev_read(sd_event_source *s, int fd, uint32_t revents, void *userdata)
while ((dev = udev_monitor_receive_device(mgr->udev_mon))) {
printf("Read device: %s\n", udev_device_get_syspath(dev));
if (!strcmp(udev_device_get_action(dev), "add"))
- add_device(mgr, dev);
+ device_add(mgr, dev);
else if (!strcmp(udev_device_get_action(dev), "remove"))
- remove_device(mgr, dev);
+ device_remove(mgr, dev);
udev_device_unref(dev);
}
@@ -249,7 +275,7 @@ udev_setup(struct manager *mgr)
continue;
}
- add_device(mgr, dev);
+ device_add(mgr, dev);
udev_device_unref(dev);
}
diff --git a/rcm-server-udev.h b/rcm-server-udev.h
index 58ccd1b..aea638f 100644
--- a/rcm-server-udev.h
+++ b/rcm-server-udev.h
@@ -1,6 +1,8 @@
#ifndef foorcmserverudevhfoo
#define foorcmserverudevhfoo
+void device_free(struct device *device);
+
int udev_setup(struct manager *mgr);
void udev_close(struct manager *mgr);