1 /*****************************************************************************
3 * mtdev - Multitouch Protocol Translation Library (MIT license)
5 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
6 * Copyright (C) 2010 Canonical Ltd.
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:
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
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.
27 ****************************************************************************/
32 #include <mtdev-mapping.h>
33 #include <mtdev-plumbing.h>
39 #define DIM2_FINGER (DIM_FINGER * DIM_FINGER)
41 /* event buffer size (must be a power of two) */
42 #define DIM_EVENTS 512
44 /* all bit masks have this type */
45 typedef unsigned int bitmask_t;
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))
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; }
57 static inline int clamp15(int x)
59 return x < -32767 ? -32767 : x > 32767 ? 32767 : x;
62 /* absolute scale is assumed to fit in 15 bits */
63 static inline int dist2(int dx, int dy)
67 return dx * dx + dy * dy;
70 /* Count number of bits (Sean Eron Andersson's Bit Hacks) */
71 static inline int bitcount(unsigned v)
73 v -= ((v>>1) & 0x55555555);
74 v = (v&0x33333333) + ((v>>2) & 0x33333333);
75 return (((v + (v>>4)) & 0xF0F0F0F) * 0x1010101) >> 24;
78 /* Return index of first bit [0-31], -1 on zero */
79 #define firstbit(v) (__builtin_ffs(v) - 1)
81 /* boost-style foreach bit */
82 #define foreach_bit(i, m) \
83 for (i = firstbit(m); i >= 0; i = firstbit((m) & (~0U << (i + 1))))
85 /* robust system ioctl calls */
86 #define SYSCALL(call) while (((call) == -1) && (errno == EINTR))