Add a context for libratbag

This is useful to have some udev helpers, or the list of drivers.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@gmail.com>
This commit is contained in:
Benjamin Tissoires
2015-08-14 17:38:35 -04:00
parent 4e232834b3
commit 5c206ff205
5 changed files with 132 additions and 2 deletions

View File

@@ -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)

35
src/libratbag-util.h Normal file
View File

@@ -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 */

View File

@@ -22,3 +22,49 @@
*/
#include "config.h"
#include <assert.h>
#include <stdlib.h>
#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;
}

View File

@@ -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

View File

@@ -1,6 +1,10 @@
/* in alphabetical order! */
LIBRATBAG_0.1.0 {
global:
libratbag_create_context;
libratbag_ref;
libratbag_unref;
local:
*;
};