chiark / gitweb /
Offsets: In button zone code, cope with negative "pos" values
[xf86-input-mtrack.git] / include / common.h
1 /***************************************************************************
2  *
3  * Multitouch X driver
4  * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
5  * Copyright (C) 2011 Ryan Bourgeois <bluedragonx@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  **************************************************************************/
22
23 #ifndef COMMON_H
24 #define COMMON_H
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <xorg-server.h>
31 #include <xf86.h>
32 #include <xf86_OSproc.h>
33 #include <xf86Xinput.h>
34 #include <errno.h>
35 #include <mtdev-mapping.h>
36 #include <mtdev-plumbing.h>
37 #include <stdint.h>
38 #include <sys/time.h>
39
40 #define DIM_FINGER 32
41 #define DIM_TOUCHES 32
42
43 /* year-proof millisecond event time */
44 typedef __u64 mstime_t;
45
46 /* all bit masks have this type */
47 typedef unsigned int bitmask_t;
48
49 #define BITMASK(x) (1U << (x))
50 #define BITONES(x) (BITMASK(x) - 1U)
51 #define GETBIT(m, x) (((m) >> (x)) & 1U)
52 #define SETBIT(m, x) (m |= BITMASK(x))
53 #define CLEARBIT(m, x) (m &= ~BITMASK(x))
54 #define MODBIT(m, x, b) ((b) ? SETBIT(m, x) : CLEARBIT(m, x))
55 #define PRBITMASK "0x%x"
56
57 #define ABSVAL(x) ((x) < 0 ? -1*(x) : (x))
58 #define MINVAL(x, y) ((x) < (y) ? (x) : (y)) 
59 #define MAXVAL(x, y) ((x) > (y) ? (x) : (y))
60 #define MODVAL(x, y) ((x) - ((int)((x) / (y))) * (y))
61 #define SQRVAL(x) ((x) * (x))
62 #define CLAMPVAL(x, min, max) MAXVAL(MINVAL(x, max), min)
63
64 /* Retrieve the current time and place it in tv.
65  */
66 static inline void microtime(struct timeval* tv)
67 {
68         gettimeofday(tv, NULL);
69 }
70
71 /* Copy one time value to another.
72  */
73 static inline void timercp(struct timeval* dest, const struct timeval* src)
74 {
75         memcpy(dest, src, sizeof(struct timeval));
76 }
77
78 /* Convert a timeval to milliseconds since the epoch. Truncates additional
79  * timer resolution effectively rounding down.
80  */
81 static inline mstime_t timertoms(const struct timeval* tv)
82 {
83         return (mstime_t)(tv->tv_sec*1000) + (mstime_t)(tv->tv_usec/1000);
84 }
85
86 /* Convert a value in milliseconds to a timeval and place the value in tv.
87  */
88 static inline void timerfromms(struct timeval* tv, const mstime_t ms)
89 {
90         tv->tv_sec = (time_t)(ms/1000);
91         tv->tv_usec = (suseconds_t)((ms%1000)*1000);
92 }
93
94 /* Convert a timeval to microseconds.
95  */
96 static inline suseconds_t timertomicro(const struct timeval* tv)
97 {
98         return tv->tv_sec * 1000000 + tv->tv_usec;
99 }
100
101 /* Add milliseconds to a timeval and place the resulting value in dest.
102  */
103 static inline void timeraddms(const struct timeval* a, const mstime_t b, struct timeval* dest)
104 {
105         struct timeval tv;
106         timerfromms(&tv, b);
107         timeradd(a, &tv, dest);
108 }
109
110 /* Clamp value to 15 bits.
111  */
112 static inline int clamp15(int x)
113 {
114         return x < -32767 ? -32767 : x > 32767 ? 32767 : x;
115 }
116
117 /* Absolute scale is assumed to fit in 15 bits.
118  */
119 static inline int dist2(int dx, int dy)
120 {
121         dx = clamp15(dx);
122         dy = clamp15(dy);
123         return dx * dx + dy * dy;
124 }
125
126 /* Count number of bits (Sean Eron Andersson's Bit Hacks).
127  */
128 static inline int bitcount(unsigned v)
129 {
130         v -= ((v>>1) & 0x55555555);
131         v = (v&0x33333333) + ((v>>2) & 0x33333333);
132         return (((v + (v>>4)) & 0xF0F0F0F) * 0x1010101) >> 24;
133 }
134
135 /* Return index of first bit [0-31], -1 on zero\
136  */
137 #define firstbit(v) (__builtin_ffs(v) - 1)
138
139 /* Boost-style foreach bit.
140  */
141 #define foreach_bit(i, m)                                               \
142         for (i = firstbit(m); i >= 0; i = firstbit((m) & (~0U << i + 1)))
143
144 /* Robust system ioctl calls.
145  */
146 #define SYSCALL(call) while (((call) == -1) && (errno == EINTR))
147
148 #endif