2013-11-17 11:19:50 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright © 2008 Kristian Høgsberg
|
|
|
|
|
*
|
|
|
|
|
* 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 LIBINPUT_UTIL_H
|
|
|
|
|
#define LIBINPUT_UTIL_H
|
|
|
|
|
|
2014-06-20 14:06:19 +10:00
|
|
|
#include <unistd.h>
|
2014-07-14 16:19:33 +10:00
|
|
|
#include <math.h>
|
2014-06-20 14:06:19 +10:00
|
|
|
|
2013-11-23 12:55:44 +01:00
|
|
|
#include "libinput.h"
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
set_logging_enabled(int enabled);
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
log_info(const char *format, ...);
|
|
|
|
|
|
2013-11-17 11:19:50 +01:00
|
|
|
/*
|
|
|
|
|
* 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 ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
|
2013-12-06 14:35:22 +10:00
|
|
|
#define ARRAY_FOR_EACH(_arr, _elem) \
|
2014-03-25 11:19:52 +10:00
|
|
|
for (size_t _i = 0; (_elem = &_arr[_i]) && _i < ARRAY_LENGTH(_arr); _i++)
|
2013-11-17 11:19:50 +01:00
|
|
|
|
2014-02-06 15:26:02 +10:00
|
|
|
#define min(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
|
|
|
|
|
2013-11-17 11:19:50 +01:00
|
|
|
#define LIBINPUT_EXPORT __attribute__ ((visibility("default")))
|
|
|
|
|
|
2013-12-06 14:47:09 +10:00
|
|
|
static inline void *
|
|
|
|
|
zalloc(size_t size)
|
|
|
|
|
{
|
|
|
|
|
return calloc(1, size);
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 14:06:19 +10:00
|
|
|
static inline void
|
|
|
|
|
msleep(unsigned int ms)
|
|
|
|
|
{
|
|
|
|
|
usleep(ms * 1000);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 16:19:33 +10:00
|
|
|
enum directions {
|
|
|
|
|
N = 1 << 0,
|
|
|
|
|
NE = 1 << 1,
|
|
|
|
|
E = 1 << 2,
|
|
|
|
|
SE = 1 << 3,
|
|
|
|
|
S = 1 << 4,
|
|
|
|
|
SW = 1 << 5,
|
|
|
|
|
W = 1 << 6,
|
|
|
|
|
NW = 1 << 7,
|
|
|
|
|
UNDEFINED_DIRECTION = 0xff
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
|
vector_get_direction(int dx, int dy)
|
|
|
|
|
{
|
|
|
|
|
int dir = UNDEFINED_DIRECTION;
|
|
|
|
|
int d1, d2;
|
|
|
|
|
double r;
|
|
|
|
|
|
|
|
|
|
if (abs(dx) < 2 && abs(dy) < 2) {
|
|
|
|
|
if (dx > 0 && dy > 0)
|
|
|
|
|
dir = S | SE | E;
|
|
|
|
|
else if (dx > 0 && dy < 0)
|
|
|
|
|
dir = N | NE | E;
|
|
|
|
|
else if (dx < 0 && dy > 0)
|
|
|
|
|
dir = S | SW | W;
|
|
|
|
|
else if (dx < 0 && dy < 0)
|
|
|
|
|
dir = N | NW | W;
|
|
|
|
|
else if (dx > 0)
|
|
|
|
|
dir = NE | E | SE;
|
|
|
|
|
else if (dx < 0)
|
|
|
|
|
dir = NW | W | SW;
|
|
|
|
|
else if (dy > 0)
|
|
|
|
|
dir = SE | S | SW;
|
|
|
|
|
else if (dy < 0)
|
|
|
|
|
dir = NE | N | NW;
|
|
|
|
|
} else {
|
|
|
|
|
/* Calculate r within the interval [0 to 8)
|
|
|
|
|
*
|
|
|
|
|
* r = [0 .. 2π] where 0 is North
|
|
|
|
|
* d_f = r / 2π ([0 .. 1))
|
|
|
|
|
* d_8 = 8 * d_f
|
|
|
|
|
*/
|
|
|
|
|
r = atan2(dy, dx);
|
|
|
|
|
r = fmod(r + 2.5*M_PI, 2*M_PI);
|
|
|
|
|
r *= 4*M_1_PI;
|
|
|
|
|
|
|
|
|
|
/* Mark one or two close enough octants */
|
|
|
|
|
d1 = (int)(r + 0.9) % 8;
|
|
|
|
|
d2 = (int)(r + 0.1) % 8;
|
|
|
|
|
|
|
|
|
|
dir = (1 << d1) | (1 << d2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-17 11:19:50 +01:00
|
|
|
#endif /* LIBINPUT_UTIL_H */
|