From 5c206ff205a3b7c4b8de2c4d124a943de10a6b86 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Fri, 14 Aug 2015 17:38:35 -0400 Subject: [PATCH] Add a context for libratbag This is useful to have some udev helpers, or the list of drivers. Signed-off-by: Benjamin Tissoires --- src/Makefile.am | 3 ++- src/libratbag-util.h | 35 +++++++++++++++++++++++++++++++++ src/libratbag.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/libratbag.h | 46 +++++++++++++++++++++++++++++++++++++++++++- src/libratbag.sym | 4 ++++ 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/libratbag-util.h diff --git a/src/Makefile.am b/src/Makefile.am index 36449f77..578e51cf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,7 +6,8 @@ include_HEADERS = \ libratbag_la_SOURCES = \ libratbag.c \ libratbag.h \ - libratbag-private.h + libratbag-private.h \ + libratbag-util.h libratbag_la_LIBADD = $(LIBEVDEV_LIBS) diff --git a/src/libratbag-util.h b/src/libratbag-util.h new file mode 100644 index 00000000..ced25bd4 --- /dev/null +++ b/src/libratbag-util.h @@ -0,0 +1,35 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * Copyright © 2013-2015 Red Hat, Inc. + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#ifndef LIBRATBAG_UTIL_H +#define LIBRATBAG_UTIL_H + +#define LIBRATBAG_EXPORT __attribute__ ((visibility("default"))) + +static inline void * +zalloc(size_t size) +{ + return calloc(1, size); +} + +#endif /* LIBRATBAG_UTIL_H */ diff --git a/src/libratbag.c b/src/libratbag.c index 23172193..106aa4fc 100644 --- a/src/libratbag.c +++ b/src/libratbag.c @@ -22,3 +22,49 @@ */ #include "config.h" +#include +#include + +#include "libratbag-private.h" +#include "libratbag-util.h" + +struct libratbag { + int refcount; +}; + +LIBRATBAG_EXPORT struct libratbag * +libratbag_create_context(void) +{ + struct libratbag *libratbag; + + libratbag = zalloc(sizeof(*libratbag)); + if (!libratbag) + return NULL; + + libratbag->refcount = 1; + + return libratbag; +} + +LIBRATBAG_EXPORT struct libratbag * +libratbag_ref(struct libratbag *libratbag) +{ + libratbag->refcount++; + return libratbag; +} + +LIBRATBAG_EXPORT struct libratbag * +libratbag_unref(struct libratbag *libratbag) +{ + if (libratbag == NULL) + return NULL; + + assert(libratbag->refcount > 0); + libratbag->refcount--; + if (libratbag->refcount > 0) + return libratbag; + + free(libratbag); + + return NULL; +} diff --git a/src/libratbag.h b/src/libratbag.h index 7f0d27b3..320599ed 100644 --- a/src/libratbag.h +++ b/src/libratbag.h @@ -35,6 +35,15 @@ extern "C" { __attribute__ ((format (printf, _format, _args))) #define LIBRATBAG_ATTRIBUTE_DEPRECATED __attribute__ ((deprecated)) +/** + * @ingroup base + * @struct libratbag + * + * A handle for accessing libratbag. This struct is refcounted, use + * libratbag_ref() and libratbag_unref(). + */ +struct libratbag; + /** * @defgroup base Initialization and manipulation of libratbag contexts * @defgroup device Querying and manipulating devices @@ -93,6 +102,41 @@ struct ratbag_profile; */ struct ratbag_button; +/** + * @ingroup base + * + * Create a new libratbag context. + * + * @return An initialized libratbag context or NULL on error + */ +struct libratbag * +libratbag_create_context(void); + +/** + * @ingroup base + * + * Add a reference to the context. A context is destroyed whenever the + * reference count reaches 0. See @ref libratbag_unref. + * + * @param libratbag A previously initialized valid libratbag context + * @return The passed libratbag context + */ +struct libratbag * +libratbag_ref(struct libratbag *libratbag); + +/** + * @ingroup base + * + * Dereference the libratbag context. After this, the context may have been + * destroyed, if the last reference was dereferenced. If so, the context is + * invalid and may not be interacted with. + * + * @param libratbag A previously initialized libratbag context + * @return NULL if context was destroyed otherwise the passed context + */ +struct libratbag * +libratbag_unref(struct libratbag *libratbag); + /** * @ingroup base * @@ -100,7 +144,7 @@ struct ratbag_button; * point to a /dev/input/eventX event node and be opened in O_RDWR. */ struct ratbag* -ratbag_new_from_fd(int fd); +ratbag_new_from_fd(struct libratbag *libratbag, int fd); /** * @ingroup device diff --git a/src/libratbag.sym b/src/libratbag.sym index 4f57a7c5..870be011 100644 --- a/src/libratbag.sym +++ b/src/libratbag.sym @@ -1,6 +1,10 @@ /* in alphabetical order! */ LIBRATBAG_0.1.0 { +global: + libratbag_create_context; + libratbag_ref; + libratbag_unref; local: *; };