[PATCH libevdev 1/2] Add libevdev_new_from_libevdev() copy constructor
David Herrmann
dh.herrmann at gmail.com
Mon Nov 4 06:51:18 PST 2013
This new constructor takes a libevdev object as source and initializes a
new one from its values. This is highly useful if you want to clone an
existing device but cannot modify it.
Another use-case is to use libevdev objects as "capability" indicator. We
could simply allocate a libevdev object from another one and pass it
around. Other code can use it as indicator which capabilities are
supported on a given interface. We usually don't want to export our real
libevdev object there as users might get access to the underlying fd then
(or might modify the object).
Signed-off-by: David Herrmann <dh.herrmann at gmail.com>
---
libevdev/libevdev.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
libevdev/libevdev.h | 19 +++++++++++++++++++
2 files changed, 67 insertions(+)
diff --git a/libevdev/libevdev.c b/libevdev/libevdev.c
index ca57e26..b4ab47a 100644
--- a/libevdev/libevdev.c
+++ b/libevdev/libevdev.c
@@ -155,6 +155,54 @@ libevdev_new_from_fd(int fd, struct libevdev **dev)
return rc;
}
+LIBEVDEV_EXPORT int
+libevdev_new_from_libevdev(struct libevdev *src, struct libevdev **dev)
+{
+ struct libevdev *d;
+
+ d = libevdev_new();
+ if (!d)
+ return -ENOMEM;
+
+ libevdev_set_name(d, src->name);
+ libevdev_set_phys(d, src->phys);
+ libevdev_set_uniq(d, src->uniq);
+ if (!d->name != !src->name ||
+ !d->phys != !src->phys ||
+ !d->uniq != !src->uniq)
+ goto err_mem;
+
+ d->ids = src->ids;
+ d->driver_version = src->driver_version;
+ memcpy(d->bits, src->bits, sizeof(d->bits));
+ memcpy(d->props, src->props, sizeof(d->props));
+ memcpy(d->key_bits, src->key_bits, sizeof(d->key_bits));
+ memcpy(d->rel_bits, src->rel_bits, sizeof(d->rel_bits));
+ memcpy(d->abs_bits, src->abs_bits, sizeof(d->abs_bits));
+ memcpy(d->led_bits, src->led_bits, sizeof(d->led_bits));
+ memcpy(d->msc_bits, src->msc_bits, sizeof(d->msc_bits));
+ memcpy(d->sw_bits, src->sw_bits, sizeof(d->sw_bits));
+ memcpy(d->rep_bits, src->rep_bits, sizeof(d->rep_bits));
+ memcpy(d->ff_bits, src->ff_bits, sizeof(d->ff_bits));
+ memcpy(d->snd_bits, src->snd_bits, sizeof(d->snd_bits));
+ memcpy(d->key_values, src->key_values, sizeof(d->key_values));
+ memcpy(d->led_values, src->led_values, sizeof(d->led_values));
+ memcpy(d->sw_values, src->sw_values, sizeof(d->sw_values));
+ memcpy(d->abs_info, src->abs_info, sizeof(d->abs_info));
+ memcpy(d->mt_slot_vals, src->mt_slot_vals, sizeof(d->mt_slot_vals));
+ d->num_slots = src->num_slots;
+ d->current_slot = src->current_slot;
+ memcpy(d->rep_values, src->rep_values, sizeof(d->rep_values));
+ d->last_event_time = src->last_event_time;
+
+ *dev = d;
+ return 0;
+
+err_mem:
+ libevdev_free(d);
+ return -ENOMEM;
+}
+
LIBEVDEV_EXPORT void
libevdev_free(struct libevdev *dev)
{
diff --git a/libevdev/libevdev.h b/libevdev/libevdev.h
index 1ee74b9..b2bcbc8 100644
--- a/libevdev/libevdev.h
+++ b/libevdev/libevdev.h
@@ -399,6 +399,25 @@ int libevdev_new_from_fd(int fd, struct libevdev **dev);
/**
* @ingroup init
*
+ * Initialize a new libevdev device from an existing libevdev device. This does
+ * not copy the file-descriptor from the source device. Instead, this creates a
+ * new plain libevdev device and then copies all attributes, names and all other
+ * static parameters.
+ *
+ * @param src An existing libevdev device.
+ * @param[out] dev The newly initialized evdev device.
+ *
+ * @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.
+ *
+ * @see libevdev_free
+ */
+int libevdev_new_from_libevdev(struct libevdev *src, struct libevdev **dev);
+
+/**
+ * @ingroup init
+ *
* Clean up and free the libevdev struct. After completion, the <code>struct
* libevdev</code> is invalid and must not be used.
*
--
1.8.4.2
More information about the Input-tools
mailing list