Cleanup spec file and remove obsolete yaml files
[external/mtdev.git] / src / common.h
1 /*****************************************************************************
2  *
3  * mtdev - Multitouch Protocol Translation Library (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 <mtdev-plumbing.h>
34 #include <malloc.h>
35 #include <string.h>
36 #include <errno.h>
37
38 #define DIM_FINGER 32
39 #define DIM2_FINGER (DIM_FINGER * DIM_FINGER)
40
41 /* event buffer size (must be a power of two) */
42 #define DIM_EVENTS 512
43
44 /* all bit masks have this type */
45 typedef unsigned int bitmask_t;
46
47 #define BITMASK(x) (1U << (x))
48 #define BITONES(x) (BITMASK(x) - 1U)
49 #define GETBIT(m, x) (((m) >> (x)) & 1U)
50 #define SETBIT(m, x) (m |= BITMASK(x))
51 #define CLEARBIT(m, x) (m &= ~BITMASK(x))
52 #define MODBIT(m, x, b) ((b) ? SETBIT(m, x) : CLEARBIT(m, x))
53
54 static inline int maxval(int x, int y) { return x > y ? x : y; }
55 static inline int minval(int x, int y) { return x < y ? x : y; }
56
57 static inline int clamp15(int x)
58 {
59         return x < -32767 ? -32767 : x > 32767 ? 32767 : x;
60 }
61
62 /* absolute scale is assumed to fit in 15 bits */
63 static inline int dist2(int dx, int dy)
64 {
65         dx = clamp15(dx);
66         dy = clamp15(dy);
67         return dx * dx + dy * dy;
68 }
69
70 /* Count number of bits (Sean Eron Andersson's Bit Hacks) */
71 static inline int bitcount(unsigned v)
72 {
73         v -= ((v>>1) & 0x55555555);
74         v = (v&0x33333333) + ((v>>2) & 0x33333333);
75         return (((v + (v>>4)) & 0xF0F0F0F) * 0x1010101) >> 24;
76 }
77
78 /* Return index of first bit [0-31], -1 on zero */
79 #define firstbit(v) (__builtin_ffs(v) - 1)
80
81 /* boost-style foreach bit */
82 #define foreach_bit(i, m)                                               \
83         for (i = firstbit(m); i >= 0; i = firstbit((m) & (~0U << (i + 1))))
84
85 /* robust system ioctl calls */
86 #define SYSCALL(call) while (((call) == -1) && (errno == EINTR))
87
88 #endif