1 /***************************************************************************
4 * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
5 * Copyright (C) 2011 Ryan Bourgeois <bluedragonx@gmail.com>
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.
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.
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
21 **************************************************************************/
30 #include <xorg-server.h>
32 #include <xf86_OSproc.h>
33 #include <xf86Xinput.h>
35 #include <mtdev-mapping.h>
36 #include <mtdev-plumbing.h>
41 #define DIM_TOUCHES 32
43 /* year-proof millisecond event time */
44 typedef __u64 mstime_t;
46 /* all bit masks have this type */
47 typedef unsigned int bitmask_t;
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"
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)
64 /* Retrieve the current time and place it in tv.
66 static inline void microtime(struct timeval* tv)
68 gettimeofday(tv, NULL);
71 /* Copy one time value to another.
73 static inline void timercp(struct timeval* dest, const struct timeval* src)
75 memcpy(dest, src, sizeof(struct timeval));
78 /* Convert a timeval to milliseconds since the epoch. Truncates additional
79 * timer resolution effectively rounding down.
81 static inline mstime_t timertoms(const struct timeval* tv)
83 return (mstime_t)(tv->tv_sec*1000) + (mstime_t)(tv->tv_usec/1000);
86 /* Convert a value in milliseconds to a timeval and place the value in tv.
88 static inline void timerfromms(struct timeval* tv, const mstime_t ms)
90 tv->tv_sec = (time_t)(ms/1000);
91 tv->tv_usec = (suseconds_t)((ms%1000)*1000);
94 /* Convert a timeval to microseconds.
96 static inline suseconds_t timertomicro(const struct timeval* tv)
98 return tv->tv_sec * 1000000 + tv->tv_usec;
101 /* Add milliseconds to a timeval and place the resulting value in dest.
103 static inline void timeraddms(const struct timeval* a, const mstime_t b, struct timeval* dest)
107 timeradd(a, &tv, dest);
110 /* Clamp value to 15 bits.
112 static inline int clamp15(int x)
114 return x < -32767 ? -32767 : x > 32767 ? 32767 : x;
117 /* Absolute scale is assumed to fit in 15 bits.
119 static inline int dist2(int dx, int dy)
123 return dx * dx + dy * dy;
126 /* Count number of bits (Sean Eron Andersson's Bit Hacks).
128 static inline int bitcount(unsigned v)
130 v -= ((v>>1) & 0x55555555);
131 v = (v&0x33333333) + ((v>>2) & 0x33333333);
132 return (((v + (v>>4)) & 0xF0F0F0F) * 0x1010101) >> 24;
135 /* Return index of first bit [0-31], -1 on zero\
137 #define firstbit(v) (__builtin_ffs(v) - 1)
139 /* Boost-style foreach bit.
141 #define foreach_bit(i, m) \
142 for (i = firstbit(m); i >= 0; i = firstbit((m) & (~0U << i + 1)))
144 /* Robust system ioctl calls.
146 #define SYSCALL(call) while (((call) == -1) && (errno == EINTR))