chiark / gitweb /
common.h: Add missing include of <mtdev-plumbing.h>
[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
56 #define ABSVAL(x) ((x) < 0 ? -1*(x) : (x))
57 #define MINVAL(x, y) ((x) < (y) ? (x) : (y)) 
58 #define MAXVAL(x, y) ((x) > (y) ? (x) : (y))
59 #define MODVAL(x, y) ((x) - ((int)((x) / (y))) * (y))
60 #define SQRVAL(x) ((x) * (x))
61 #define CLAMPVAL(x, min, max) MAXVAL(MINVAL(x, max), min)
62
63 /* Retrieve the current time and place it in tv.
64  */
65 static inline void microtime(struct timeval* tv)
66 {
67         gettimeofday(tv, NULL);
68 }
69
70 /* Copy one time value to another.
71  */
72 static inline void timercp(struct timeval* dest, const struct timeval* src)
73 {
74         memcpy(dest, src, sizeof(struct timeval));
75 }
76
77 /* Convert a timeval to milliseconds since the epoch. Truncates additional
78  * timer resolution effectively rounding down.
79  */
80 static inline mstime_t timertoms(const struct timeval* tv)
81 {
82         return (mstime_t)(tv->tv_sec*1000) + (mstime_t)(tv->tv_usec/1000);
83 }
84
85 /* Convert a value in milliseconds to a timeval and place the value in tv.
86  */
87 static inline void timerfromms(struct timeval* tv, const mstime_t ms)
88 {
89         tv->tv_sec = (time_t)(ms/1000);
90         tv->tv_usec = (suseconds_t)((ms%1000)*1000);
91 }
92
93 /* Convert a timeval to microseconds.
94  */
95 static inline suseconds_t timertomicro(const struct timeval* tv)
96 {
97         return tv->tv_sec * 1000000 + tv->tv_usec;
98 }
99
100 /* Add milliseconds to a timeval and place the resulting value in dest.
101  */
102 static inline void timeraddms(const struct timeval* a, const mstime_t b, struct timeval* dest)
103 {
104         struct timeval tv;
105         timerfromms(&tv, b);
106         timeradd(a, &tv, dest);
107 }
108
109 /* Clamp value to 15 bits.
110  */
111 static inline int clamp15(int x)
112 {
113         return x < -32767 ? -32767 : x > 32767 ? 32767 : x;
114 }
115
116 /* Absolute scale is assumed to fit in 15 bits.
117  */
118 static inline int dist2(int dx, int dy)
119 {
120         dx = clamp15(dx);
121         dy = clamp15(dy);
122         return dx * dx + dy * dy;
123 }
124
125 /* Count number of bits (Sean Eron Andersson's Bit Hacks).
126  */
127 static inline int bitcount(unsigned v)
128 {
129         v -= ((v>>1) & 0x55555555);
130         v = (v&0x33333333) + ((v>>2) & 0x33333333);
131         return (((v + (v>>4)) & 0xF0F0F0F) * 0x1010101) >> 24;
132 }
133
134 /* Return index of first bit [0-31], -1 on zero\
135  */
136 #define firstbit(v) (__builtin_ffs(v) - 1)
137
138 /* Boost-style foreach bit.
139  */
140 #define foreach_bit(i, m)                                               \
141         for (i = firstbit(m); i >= 0; i = firstbit((m) & (~0U << i + 1)))
142
143 /* Robust system ioctl calls.
144  */
145 #define SYSCALL(call) while (((call) == -1) && (errno == EINTR))
146
147 #endif