summaryrefslogtreecommitdiff
path: root/src/common.h
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2010-06-17 18:12:58 +0200
committerHenrik Rydberg <rydberg@euromail.se>2010-06-17 18:12:58 +0200
commit66e5de9eaefc33ffa6af3617f9ec7a50f10af50d (patch)
tree66535311e0b9c422b30341237fd596fb9ebbdd75 /src/common.h
Initial load of mtdev project
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..03bc7bd
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,87 @@
1/*****************************************************************************
2 *
3 * mtdev - MT device event converter (MIT license)
4 *
5 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (C) 2010 Canonical Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 ****************************************************************************/
28
29#ifndef COMMON_H
30#define COMMON_H
31
32#include <mtdev-mapping.h>
33#include <malloc.h>
34#include <string.h>
35#include <errno.h>
36
37#define DIM_FINGER 32
38#define DIM2_FINGER (DIM_FINGER * DIM_FINGER)
39
40/* event buffer size (must be a power of two) */
41#define DIM_EVENTS 512
42
43/* all bit masks have this type */
44typedef unsigned int bitmask_t;
45
46#define BITMASK(x) (1U << (x))
47#define BITONES(x) (BITMASK(x) - 1U)
48#define GETBIT(m, x) (((m) >> (x)) & 1U)
49#define SETBIT(m, x) (m |= BITMASK(x))
50#define CLEARBIT(m, x) (m &= ~BITMASK(x))
51#define MODBIT(m, x, b) ((b) ? SETBIT(m, x) : CLEARBIT(m, x))
52
53static inline int maxval(int x, int y) { return x > y ? x : y; }
54static inline int minval(int x, int y) { return x < y ? x : y; }
55
56static inline int clamp15(int x)
57{
58 return x < -32767 ? -32767 : x > 32767 ? 32767 : x;
59}
60
61/* absolute scale is assumed to fit in 15 bits */
62static inline int dist2(int dx, int dy)
63{
64 dx = clamp15(dx);
65 dy = clamp15(dy);
66 return dx * dx + dy * dy;
67}
68
69/* Count number of bits (Sean Eron Andersson's Bit Hacks) */
70static inline int bitcount(unsigned v)
71{
72 v -= ((v>>1) & 0x55555555);
73 v = (v&0x33333333) + ((v>>2) & 0x33333333);
74 return (((v + (v>>4)) & 0xF0F0F0F) * 0x1010101) >> 24;
75}
76
77/* Return index of first bit [0-31], -1 on zero */
78#define firstbit(v) (__builtin_ffs(v) - 1)
79
80/* boost-style foreach bit */
81#define foreach_bit(i, m) \
82 for (i = firstbit(m); i >= 0; i = firstbit((m) & (~0U << i + 1)))
83
84/* robust system ioctl calls */
85#define SYSCALL(call) while (((call) == -1) && (errno == EINTR))
86
87#endif