• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define MTDEV_NO_LEGACY_API
33 
34 #include <mtdev-mapping.h>
35 #include <mtdev-plumbing.h>
36 #include <malloc.h>
37 #include <string.h>
38 #include <errno.h>
39 
40 #define DIM_FINGER 32
41 #define DIM2_FINGER (DIM_FINGER * DIM_FINGER)
42 
43 /* event buffer size (must be a power of two) */
44 #define DIM_EVENTS 512
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 
maxval(int x,int y)56 static inline int maxval(int x, int y) { return x > y ? x : y; }
minval(int x,int y)57 static inline int minval(int x, int y) { return x < y ? x : y; }
58 
clamp15(int x)59 static inline int clamp15(int x)
60 {
61 	return x < -32767 ? -32767 : x > 32767 ? 32767 : x;
62 }
63 
64 /* absolute scale is assumed to fit in 15 bits */
dist2(int dx,int dy)65 static inline int dist2(int dx, int dy)
66 {
67 	dx = clamp15(dx);
68 	dy = clamp15(dy);
69 	return dx * dx + dy * dy;
70 }
71 
72 /* Count number of bits (Sean Eron Andersson's Bit Hacks) */
bitcount(unsigned v)73 static inline int bitcount(unsigned v)
74 {
75 	v -= ((v>>1) & 0x55555555);
76 	v = (v&0x33333333) + ((v>>2) & 0x33333333);
77 	return (((v + (v>>4)) & 0xF0F0F0F) * 0x1010101) >> 24;
78 }
79 
80 /* Return index of first bit [0-31], -1 on zero */
81 #define firstbit(v) (__builtin_ffs(v) - 1)
82 
83 /* boost-style foreach bit */
84 #define foreach_bit(i, m)						\
85 	for (i = firstbit(m); i >= 0; i = firstbit((m) & (~0U << (i + 1))))
86 
87 /* robust system ioctl calls */
88 #define SYSCALL(call) while (((call) == -1) && (errno == EINTR))
89 
90 /* To be compatible to the original, non-opaque mtdev API, we can only use 11
91  * axes in the basic struct. Everything else is hidden in the state, see the
92  * use of dev->abs[idx] vs dev->state->ext_abs[idx]
93  *
94  * See MT_ABS_SIZE in include/mtdev.h
95  */
96 #define LEGACY_API_NUM_MT_AXES 11
97 
98 /**
99  * struct mtdev - represents an input MT device
100  * @has_mtdata: true if the device has MT capabilities
101  * @has_slot: true if the device sends MT slots
102  * @slot: slot event properties
103  * @abs: ABS_MT event properties
104  * @state: internal mtdev parsing state
105  *
106  * The mtdev structure represents a kernel MT device type B, emitting
107  * MT slot events. The events put into mtdev may be from any MT
108  * device, specifically type A without contact tracking, type A with
109  * contact tracking, or type B with contact tracking. See the kernel
110  * documentation for further details.
111  *
112  */
113 struct mtdev {
114 	int has_mtdata;
115 	int has_slot;
116 	int has_abs[LEGACY_API_NUM_MT_AXES];
117 	struct input_absinfo slot;
118 	struct input_absinfo abs[LEGACY_API_NUM_MT_AXES];
119 	struct mtdev_state *state;
120 };
121 
122 #define MT_ABS_SIZE 14
123 
124 static const unsigned int mtdev_map_abs2mt[ABS_CNT] = {
125  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
126  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
127  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
128  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
129  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
130  0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
131  0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008,
132  0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x0000, 0x0000,
133 };
134 
135 static const unsigned int mtdev_map_mt2abs[MT_ABS_SIZE] = {
136  0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
137  0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,
138 };
139 
mtdev_is_absmt(unsigned int code)140 static inline int mtdev_is_absmt(unsigned int code)
141 {
142 	return mtdev_map_abs2mt[code];
143 }
144 
mtdev_abs2mt(unsigned int code)145 static inline unsigned int mtdev_abs2mt(unsigned int code)
146 {
147 	return mtdev_map_abs2mt[code] - 1;
148 }
149 
mtdev_mt2abs(unsigned int mtcode)150 static inline unsigned int mtdev_mt2abs(unsigned int mtcode)
151 {
152 	return mtdev_map_mt2abs[mtcode];
153 }
154 
155 #endif
156