From b7564682f4e09126451985c7af9e6870bed56429 Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Fri, 14 Aug 2015 14:30:54 -0400 Subject: [PATCH] Borrow libinput-util.c to handle lists, array and bitfields Some valuable helpers from libinput. Signed-off-by: Benjamin Tissoires --- src/Makefile.am | 1 + src/libratbag-util.c | 71 ++++++++++++++++++++++++++++++++++++ src/libratbag-util.h | 85 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 src/libratbag-util.c diff --git a/src/Makefile.am b/src/Makefile.am index 578e51cf..4ab3708a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,6 +6,7 @@ include_HEADERS = \ libratbag_la_SOURCES = \ libratbag.c \ libratbag.h \ + libratbag-util.c \ libratbag-private.h \ libratbag-util.h diff --git a/src/libratbag-util.c b/src/libratbag-util.c new file mode 100644 index 00000000..507eae23 --- /dev/null +++ b/src/libratbag-util.c @@ -0,0 +1,71 @@ +/* + * Copyright © 2008-2011 Kristian Høgsberg + * Copyright © 2011 Intel Corporation + * 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. + */ + +/* + * This list data structure is verbatim copy from wayland-util.h from the + * Wayland project; except that wl_ prefix has been removed. + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include + +#include "libratbag-util.h" +#include "libratbag-private.h" + +void +list_init(struct list *list) +{ + list->prev = list; + list->next = list; +} + +void +list_insert(struct list *list, struct list *elm) +{ + elm->prev = list; + elm->next = list->next; + list->next = elm; + elm->next->prev = elm; +} + +void +list_remove(struct list *elm) +{ + elm->prev->next = elm->next; + elm->next->prev = elm->prev; + elm->next = NULL; + elm->prev = NULL; +} + +int +list_empty(const struct list *list) +{ + return list->next == list; +} diff --git a/src/libratbag-util.h b/src/libratbag-util.h index ced25bd4..7fa16341 100644 --- a/src/libratbag-util.h +++ b/src/libratbag-util.h @@ -24,6 +24,58 @@ #ifndef LIBRATBAG_UTIL_H #define LIBRATBAG_UTIL_H +#include + +/* + * This list data structure is a verbatim copy from wayland-util.h from the + * Wayland project; except that wl_ prefix has been removed. + */ + +struct list { + struct list *prev; + struct list *next; +}; + +void list_init(struct list *list); +void list_insert(struct list *list, struct list *elm); +void list_remove(struct list *elm); +int list_empty(const struct list *list); + +#ifdef __GNUC__ +#define container_of(ptr, sample, member) \ + (__typeof__(sample))((char *)(ptr) - \ + ((char *)&(sample)->member - (char *)(sample))) +#else +#define container_of(ptr, sample, member) \ + (void *)((char *)(ptr) - \ + ((char *)&(sample)->member - (char *)(sample))) +#endif + +#define list_for_each(pos, head, member) \ + for (pos = 0, pos = container_of((head)->next, pos, member); \ + &pos->member != (head); \ + pos = container_of(pos->member.next, pos, member)) + +#define list_for_each_safe(pos, tmp, head, member) \ + for (pos = 0, tmp = 0, \ + pos = container_of((head)->next, pos, member), \ + tmp = container_of((pos)->member.next, tmp, member); \ + &pos->member != (head); \ + pos = tmp, \ + tmp = container_of(pos->member.next, tmp, member)) + +#define LONG_BITS (sizeof(long) * 8) +#define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS) +#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) +#define ARRAY_FOR_EACH(_arr, _elem) \ + for (size_t _i = 0; _i < ARRAY_LENGTH(_arr) && (_elem = &_arr[_i]); _i++) +#define AS_MASK(v) (1 << (v)) + +#define min(a, b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define streq(s1, s2) (strcmp((s1), (s2)) == 0) +#define strneq(s1, s2, n) (strncmp((s1), (s2), (n)) == 0) + #define LIBRATBAG_EXPORT __attribute__ ((visibility("default"))) static inline void * @@ -32,4 +84,37 @@ zalloc(size_t size) return calloc(1, size); } +static inline void +msleep(unsigned int ms) +{ + usleep(ms * 1000); +} + +static inline int +long_bit_is_set(const unsigned long *array, int bit) +{ + return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); +} + +static inline void +long_set_bit(unsigned long *array, int bit) +{ + array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS)); +} + +static inline void +long_clear_bit(unsigned long *array, int bit) +{ + array[bit / LONG_BITS] &= ~(1LL << (bit % LONG_BITS)); +} + +static inline void +long_set_bit_state(unsigned long *array, int bit, int state) +{ + if (state) + long_set_bit(array, bit); + else + long_clear_bit(array, bit); +} + #endif /* LIBRATBAG_UTIL_H */