1 /*
2  * Copyright © 2004-2008 Red Hat, Inc.
3  * Copyright © 2008 University of South Australia
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of Red Hat
10  * not be used in advertising or publicity pertaining to distribution
11  * of the software without specific, written prior permission.  Red
12  * Hat makes no representations about the suitability of this software
13  * for any purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
18  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
20  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors:
25  *	Kristian Høgsberg (krh@redhat.com)
26  *	Adam Jackson (ajax@redhat.com)
27  *	Peter Hutterer (peter@cs.unisa.edu.au)
28  *	Oliver McFadden (oliver.mcfadden@nokia.com)
29  */
30 
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 
35 #ifndef EVDEV_H
36 #define EVDEV_H
37 
38 #include <linux/input.h>
39 #include <sys/types.h>
40 
41 /* XXX Copied from <xf86str.h> for linux */
42 /* Tolerate prior #include <linux/input.h> */
43 #undef BUS_NONE
44 #undef BUS_PCI
45 #undef BUS_SBUS
46 #undef BUS_PLATFORM
47 #undef BUS_last
48 
49 #include <xorg-server.h>
50 #include <xf86Xinput.h>
51 #include <xf86_OSproc.h>
52 #include <xkbstr.h>
53 
54 #include <mtdev.h>
55 
56 #include <libevdev/libevdev.h>
57 
58 #ifndef EV_CNT /* linux 2.6.23 kernels and earlier lack _CNT defines */
59 #define EV_CNT (EV_MAX+1)
60 #endif
61 #ifndef KEY_CNT
62 #define KEY_CNT (KEY_MAX+1)
63 #endif
64 #ifndef REL_CNT
65 #define REL_CNT (REL_MAX+1)
66 #endif
67 #ifndef ABS_CNT
68 #define ABS_CNT (ABS_MAX+1)
69 #endif
70 #ifndef LED_CNT
71 #define LED_CNT (LED_MAX+1)
72 #endif
73 
74 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 18
75 #define LogMessageVerbSigSafe xf86MsgVerb
76 #endif
77 
78 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 23
79 #define HAVE_THREADED_INPUT	1
80 #endif
81 
82 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 24
83 #define BLOCK_HANDLER_ARGS     	void *data, void *waitTime
84 #define WAKEUP_HANDLER_ARGS	void *data, int i
85 #else
86 #define BLOCK_HANDLER_ARGS	pointer data, struct timeval **waitTime, pointer LastSelectMask
87 #define WAKEUP_HANDLER_ARGS	void *data, int i, pointer LastSelectMask
88 #endif
89 
90 #define EVDEV_MAXBUTTONS 32
91 #define EVDEV_MAXQUEUE 32
92 
93 /* evdev flags */
94 #define EVDEV_KEYBOARD_EVENTS	(1 << 0)
95 #define EVDEV_BUTTON_EVENTS	(1 << 1)
96 #define EVDEV_RELATIVE_EVENTS	(1 << 2)
97 #define EVDEV_ABSOLUTE_EVENTS	(1 << 3)
98 #define EVDEV_TOUCHPAD		(1 << 4)
99 #define EVDEV_INITIALIZED	(1 << 5) /* WheelInit etc. called already? */
100 #define EVDEV_TOUCHSCREEN	(1 << 6)
101 #define EVDEV_CALIBRATED	(1 << 7) /* run-time calibrated? */
102 #define EVDEV_TABLET		(1 << 8) /* device looks like a tablet? */
103 #define EVDEV_UNIGNORE_ABSOLUTE (1 << 9) /* explicitly unignore abs axes */
104 #define EVDEV_UNIGNORE_RELATIVE (1 << 10) /* explicitly unignore rel axes */
105 #define EVDEV_RELATIVE_MODE	(1 << 11) /* Force relative events for devices with absolute axes */
106 
107 #ifndef MAX_VALUATORS
108 #define MAX_VALUATORS 36
109 #endif
110 
111 #ifndef XI_PROP_DEVICE_NODE
112 #define XI_PROP_DEVICE_NODE "Device Node"
113 #endif
114 
115 #define LONG_BITS (sizeof(long) * 8)
116 
117 /* Number of longs needed to hold the given number of bits */
118 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
119 
120 #define DEFAULT_MOUSE_DPI 1000.0
121 
122 /* Function key mode */
123 enum fkeymode {
124     FKEYMODE_UNKNOWN = 0,
125     FKEYMODE_FKEYS,       /* function keys send function keys */
126     FKEYMODE_MMKEYS,      /* function keys send multimedia keys */
127 };
128 
129 enum SlotState {
130     SLOTSTATE_OPEN = 8,
131     SLOTSTATE_CLOSE,
132     SLOTSTATE_UPDATE,
133     SLOTSTATE_EMPTY,
134 };
135 
136 enum ButtonAction {
137     BUTTON_RELEASE = 0,
138     BUTTON_PRESS = 1
139 };
140 
141 /* axis specific data for wheel emulation */
142 typedef struct {
143     int up_button;
144     int down_button;
145     int traveled_distance;
146 } WheelAxis, *WheelAxisPtr;
147 
148 /* Event queue used to defer keyboard/button events until EV_SYN time. */
149 typedef struct {
150     enum {
151         EV_QUEUE_KEY,	/* xf86PostKeyboardEvent() */
152         EV_QUEUE_BTN,	/* xf86PostButtonEvent() */
153         EV_QUEUE_PROXIMITY, /* xf86PostProximityEvent() */
154         EV_QUEUE_TOUCH,	/*xf86PostTouchEvent() */
155     } type;
156     union {
157         int key;	/* May be either a key code or button number. */
158         unsigned int touch; /* Touch ID */
159     } detail;
160     int val;	/* State of the key/button/touch; pressed or released. */
161     ValuatorMask *touchMask;
162 } EventQueueRec, *EventQueuePtr;
163 
164 typedef struct {
165     struct libevdev *dev;
166 
167     char *device;
168     int grabDevice;         /* grab the event device? */
169 
170     int num_vals;           /* number of valuators */
171     int num_mt_vals;        /* number of multitouch valuators */
172     int abs_axis_map[ABS_CNT]; /* Map evdev ABS_* to index */
173     int rel_axis_map[REL_CNT]; /* Map evdev REL_* to index */
174     ValuatorMask *abs_vals;     /* values for absolute axis */
175     ValuatorMask *rel_vals;     /* values for relative axis */
176     ValuatorMask *old_vals; /* old absolute values for calculating relative motion */
177     ValuatorMask *prox;     /* last absolute values set while not in proximity */
178     ValuatorMask *mt_mask;
179     ValuatorMask **last_mt_vals;
180     int cur_slot;
181     struct slot {
182         int dirty;
183         enum SlotState state;
184     } *slots;
185     struct mtdev *mtdev;
186     BOOL fake_mt;
187 
188     int flags;
189     int in_proximity;           /* device in proximity */
190     int use_proximity;          /* using the proximity bit? */
191     int num_buttons;            /* number of buttons */
192     BOOL swap_axes;
193     BOOL invert_x;
194     BOOL invert_y;
195     int resolution;
196 
197     unsigned int abs_queued, rel_queued, prox_queued;
198 
199     /* Middle mouse button emulation */
200     struct {
201         BOOL                enabled;
202         BOOL                pending;     /* timer waiting? */
203         int                 buttonstate; /* phys. button state */
204         int                 state;       /* state machine (see bt3emu.c) */
205         Time                expires;     /* time of expiry */
206         Time                timeout;
207         uint8_t             button;      /* phys button to emit */
208     } emulateMB;
209     /* Third mouse button emulation */
210     struct emulate3B {
211         BOOL                enabled;
212         BOOL                state;       /* current state */
213         Time                timeout;     /* timeout until third button press */
214         int                 buttonstate; /* phys. button state */
215         int                 button;      /* phys button to emit */
216         int                 threshold;   /* move threshold in dev coords */
217         OsTimerPtr          timer;
218         double              delta[2];    /* delta x/y, accumulating */
219         int                 startpos[2]; /* starting pos for abs devices */
220         int                 flags;       /* remember if we had rel or abs movement */
221     } emulate3B;
222     struct {
223 	int                 meta;           /* meta key to lock any button */
224 	BOOL                meta_state;     /* meta_button state */
225 	unsigned int        lock_pair[EVDEV_MAXBUTTONS];  /* specify a meta/lock pair */
226 	BOOL                lock_state[EVDEV_MAXBUTTONS]; /* state of any locked buttons */
227     } dragLock;
228     struct {
229         BOOL                enabled;
230         int                 button;
231         int                 button_state;
232         int                 inertia;
233         WheelAxis           X;
234         WheelAxis           Y;
235         Time                expires;     /* time of expiry */
236         Time                timeout;
237     } emulateWheel;
238     struct {
239         int                 vert_delta;
240         int                 horiz_delta;
241         int                 dial_delta;
242     } smoothScroll;
243     /* run-time calibration */
244     struct {
245         int                 min_x;
246         int                 max_x;
247         int                 min_y;
248         int                 max_y;
249     } calibration;
250 
251     unsigned char btnmap[32];           /* config-file specified button mapping */
252 
253     int reopen_attempts; /* max attempts to re-open after read failure */
254     int reopen_left;     /* number of attempts left to re-open the device */
255     OsTimerPtr reopen_timer;
256 
257     /* minor/major number */
258     dev_t min_maj;
259 
260     /* Event queue used to defer keyboard/button events until EV_SYN time. */
261     int                     num_queue;
262     EventQueueRec           queue[EVDEV_MAXQUEUE];
263 
264     enum fkeymode           fkeymode;
265 
266     char *type_name;
267 } EvdevRec, *EvdevPtr;
268 
269 /* Event posting functions */
270 void EvdevQueueKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value);
271 void EvdevQueueButtonEvent(InputInfoPtr pInfo, int button, int value);
272 void EvdevQueueProximityEvent(InputInfoPtr pInfo, int value);
273 void EvdevQueueTouchEvent(InputInfoPtr pInfo, unsigned int touch,
274                           ValuatorMask *mask, uint16_t type);
275 void EvdevPostButtonEvent(InputInfoPtr pInfo, int button, enum ButtonAction act);
276 void EvdevQueueButtonClicks(InputInfoPtr pInfo, int button, int count);
277 void EvdevPostRelativeMotionEvents(InputInfoPtr pInfo);
278 void EvdevPostAbsoluteMotionEvents(InputInfoPtr pInfo);
279 unsigned int EvdevUtilButtonEventToButtonNumber(EvdevPtr pEvdev, int code);
280 
281 /* Middle Button emulation */
282 int  EvdevMBEmuTimer(InputInfoPtr);
283 BOOL EvdevMBEmuFilterEvent(InputInfoPtr, int, BOOL);
284 void EvdevMBEmuWakeupHandler(WAKEUP_HANDLER_ARGS);
285 void EvdevMBEmuBlockHandler(BLOCK_HANDLER_ARGS);
286 void EvdevMBEmuPreInit(InputInfoPtr);
287 void EvdevMBEmuOn(InputInfoPtr);
288 void EvdevMBEmuFinalize(InputInfoPtr);
289 
290 /* Third button emulation */
291 CARD32 Evdev3BEmuTimer(OsTimerPtr timer, CARD32 time, pointer arg);
292 BOOL Evdev3BEmuFilterEvent(InputInfoPtr, int, BOOL);
293 void Evdev3BEmuPreInit(InputInfoPtr pInfo);
294 void Evdev3BEmuOn(InputInfoPtr);
295 void Evdev3BEmuFinalize(InputInfoPtr);
296 void Evdev3BEmuProcessRelMotion(InputInfoPtr pInfo, double dx, double dy);
297 void Evdev3BEmuProcessAbsMotion(InputInfoPtr pInfo, ValuatorMask *vals);
298 
299 /* Mouse Wheel emulation */
300 void EvdevWheelEmuPreInit(InputInfoPtr pInfo);
301 BOOL EvdevWheelEmuFilterButton(InputInfoPtr pInfo, unsigned int button, int value);
302 BOOL EvdevWheelEmuFilterMotion(InputInfoPtr pInfo, struct input_event *pEv);
303 
304 /* Draglock code */
305 void EvdevDragLockPreInit(InputInfoPtr pInfo);
306 BOOL EvdevDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value);
307 
308 void EvdevMBEmuInitProperty(DeviceIntPtr);
309 void Evdev3BEmuInitProperty(DeviceIntPtr);
310 void EvdevWheelEmuInitProperty(DeviceIntPtr);
311 void EvdevDragLockInitProperty(DeviceIntPtr);
312 void EvdevAppleInitProperty(DeviceIntPtr);
313 #endif
314