3dd395335ec116310456945c294114ab532b522e
[adaptation/xorg/driver/xserver-xorg-input-evdev.git] / src / evdev.h
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 <linux/types.h>
40
41 #include <xorg-server.h>
42 #include <xf86Xinput.h>
43 #include <xf86_OSproc.h>
44 #include <xkbstr.h>
45
46 #ifdef MULTITOUCH
47 #include <mtdev.h>
48 #endif
49
50 #ifdef _F_GESTURE_EXTENSION_
51 typedef enum _MTSyncType
52 {
53     MTOUCH_FRAME_SYNC_END,
54     MTOUCH_FRAME_SYNC_BEGIN,
55     MTOUCH_FRAME_SYNC_UPDATE,
56     ROTARY_FRAME_SYNC_END,
57     ROTARY_FRAME_SYNC_BEGIN
58 } MTSyncType;
59
60 enum EventType
61 {
62     ET_MTSync = 0x7E,
63     ET_Internal = 0xFF /* First byte */
64 };
65
66 typedef struct _AnyEvent AnyEvent;
67 struct _AnyEvent
68 {
69     unsigned char header; /**< Always ET_Internal */
70     enum EventType type;  /**< One of EventType */
71     int length;           /**< Length in bytes */
72     Time time;            /**< Time in ms */
73     int deviceid;
74     MTSyncType sync;
75     int x;
76     int y;
77 };
78
79 union _InternalEvent {
80         struct {
81             unsigned char header; /**< Always ET_Internal */
82             enum EventType type;  /**< One of ET_* */
83             int length;           /**< Length in bytes */
84             Time time;            /**< Time in ms. */
85         } any;
86         AnyEvent any_event;
87 };
88 #endif /* #ifdef _F_GESTURE_EXTENSION_ */
89
90 #ifndef EV_CNT /* linux 2.6.23 kernels and earlier lack _CNT defines */
91 #define EV_CNT (EV_MAX+1)
92 #endif
93 #ifndef KEY_CNT
94 #define KEY_CNT (KEY_MAX+1)
95 #endif
96 #ifndef REL_CNT
97 #define REL_CNT (REL_MAX+1)
98 #endif
99 #ifndef ABS_CNT
100 #define ABS_CNT (ABS_MAX+1)
101 #endif
102 #ifndef LED_CNT
103 #define LED_CNT (LED_MAX+1)
104 #endif
105
106 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
107 #define HAVE_SMOOTH_SCROLLING 1
108 #endif
109
110 #define EVDEV_MAXBUTTONS 32
111 #define EVDEV_MAXQUEUE 32
112
113 #define EVDEV_PRESS 1
114 #define EVDEV_RELEASE 0
115
116 /* evdev flags */
117 #define EVDEV_KEYBOARD_EVENTS   (1 << 0)
118 #define EVDEV_BUTTON_EVENTS     (1 << 1)
119 #define EVDEV_RELATIVE_EVENTS   (1 << 2)
120 #define EVDEV_ABSOLUTE_EVENTS   (1 << 3)
121 #define EVDEV_TOUCHPAD          (1 << 4)
122 #define EVDEV_INITIALIZED       (1 << 5) /* WheelInit etc. called already? */
123 #define EVDEV_TOUCHSCREEN       (1 << 6)
124 #define EVDEV_CALIBRATED        (1 << 7) /* run-time calibrated? */
125 #define EVDEV_TABLET            (1 << 8) /* device looks like a tablet? */
126 #define EVDEV_UNIGNORE_ABSOLUTE (1 << 9) /* explicitly unignore abs axes */
127 #define EVDEV_UNIGNORE_RELATIVE (1 << 10) /* explicitly unignore rel axes */
128 #define EVDEV_RELATIVE_MODE     (1 << 11) /* Force relative events for devices with absolute axes */
129 #ifdef _F_EVDEV_CONFINE_REGION_
130 #define EVDEV_CONFINE_REGION    (1 << 12)
131 #endif /* #ifdef _F_EVDEV_CONFINE_REGION_ */
132 #ifdef _F_EVDEV_SUPPORT_GAMEPAD
133 #define EVDEV_GAMEPAD   (1 << 13)
134
135 #define MAX_GAMEPAD_DEFINITION_ABS 3
136 #define MAX_GAMEPAD_DEFINITION_KEY 10
137 #endif //_F_EVDEV_SUPPORT_GAMEPAD
138
139 #ifdef _F_EVDEV_SUPPORT_ROTARY_
140 #define EVDEV_OFM  (1 << 14)
141 #define EVDEV_HALLIC (1 << 15)
142
143 #define DEFAULT_HW_ROTARY_MAX 4360
144 #endif //_F_EVDEV_SUPPORT_ROTARY_
145
146 #ifndef MAX_VALUATORS
147 #define MAX_VALUATORS 36
148 #endif
149
150 #ifndef XI_PROP_DEVICE_NODE
151 #define XI_PROP_DEVICE_NODE "Device Node"
152 #endif
153
154 #ifndef XI_PROP_DEVICE_TYPE
155 #define XI_PROP_DEVICE_TYPE "Device Type"
156 #endif
157
158 #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_
159 #define XI_PROP_REL_MOVE_STATUS "Relative Move Status"
160 #define XI_PROP_REL_MOVE_ACK "Relative Move Acknowledge"
161 #endif /* #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_ */
162
163 #define LONG_BITS (sizeof(long) * 8)
164
165 /* Number of longs needed to hold the given number of bits */
166 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
167
168 /* Function key mode */
169 enum fkeymode {
170     FKEYMODE_UNKNOWN = 0,
171     FKEYMODE_FKEYS,       /* function keys send function keys */
172     FKEYMODE_MMKEYS,      /* function keys send multimedia keys */
173 };
174
175 enum SlotState {
176     SLOTSTATE_OPEN = 8,
177     SLOTSTATE_CLOSE,
178     SLOTSTATE_UPDATE,
179     SLOTSTATE_EMPTY,
180 };
181
182 enum ButtonAction {
183     BUTTON_RELEASE = 0,
184     BUTTON_PRESS = 1
185 };
186
187 /* axis specific data for wheel emulation */
188 typedef struct {
189     int up_button;
190     int down_button;
191     int traveled_distance;
192 } WheelAxis, *WheelAxisPtr;
193
194 /* Event queue used to defer keyboard/button events until EV_SYN time. */
195 typedef struct {
196     enum {
197         EV_QUEUE_KEY,   /* xf86PostKeyboardEvent() */
198         EV_QUEUE_BTN,   /* xf86PostButtonEvent() */
199         EV_QUEUE_PROXIMITY, /* xf86PostProximityEvent() */
200 #ifdef MULTITOUCH
201         EV_QUEUE_TOUCH, /*xf86PostTouchEvent() */
202 #endif
203     } type;
204     union {
205         int key;        /* May be either a key code or button number. */
206 #ifdef MULTITOUCH
207         unsigned int touch; /* Touch ID */
208 #endif
209     } detail;
210     int val;    /* State of the key/button/touch; pressed or released. */
211 #ifdef MULTITOUCH
212     ValuatorMask *touchMask;
213 #endif
214 } EventQueueRec, *EventQueuePtr;
215
216 #ifdef _F_REMAP_KEYS_
217 typedef struct {
218     uint8_t cd[256];
219 } EvdevKeyRemapSlice;
220 typedef struct {
221     EvdevKeyRemapSlice* sl[256];
222 } EvdevKeyRemap, *EvdevKeyRemapPtr;
223 #endif //_F_REMAP_KEYS_
224
225 typedef struct {
226     unsigned short id_vendor;
227     unsigned short id_product;
228
229     char *device;
230     int grabDevice;         /* grab the event device? */
231
232     int num_vals;           /* number of valuators */
233     int axis_map[max(ABS_CNT, REL_CNT)]; /* Map evdev <axis> to index */
234     ValuatorMask *vals;     /* new values coming in */
235     ValuatorMask *old_vals; /* old values for calculating relative motion */
236     ValuatorMask *prox;     /* last values set while not in proximity */
237     ValuatorMask *mt_mask;
238     ValuatorMask **last_mt_vals;
239     int cur_slot;
240     enum SlotState slot_state;
241 #ifdef MULTITOUCH
242     struct mtdev *mtdev;
243 #endif
244
245     int flags;
246     int in_proximity;           /* device in proximity */
247     int use_proximity;          /* using the proximity bit? */
248     int num_buttons;            /* number of buttons */
249     BOOL swap_axes;
250     BOOL invert_x;
251     BOOL invert_y;
252
253     int delta[REL_CNT];
254     unsigned int abs_queued, rel_queued, prox_queued;
255
256     /* XKB stuff has to be per-device rather than per-driver */
257     XkbRMLVOSet rmlvo;
258
259     /* Middle mouse button emulation */
260     struct {
261         BOOL                enabled;
262         BOOL                pending;     /* timer waiting? */
263         int                 buttonstate; /* phys. button state */
264         int                 state;       /* state machine (see bt3emu.c) */
265         Time                expires;     /* time of expiry */
266         Time                timeout;
267     } emulateMB;
268     /* Third mouse button emulation */
269     struct emulate3B {
270         BOOL                enabled;
271         BOOL                state;       /* current state */
272         Time                timeout;     /* timeout until third button press */
273         int                 buttonstate; /* phys. button state */
274         int                 button;      /* phys button to emit */
275         int                 threshold;   /* move threshold in dev coords */
276         OsTimerPtr          timer;
277         int                 delta[2];    /* delta x/y, accumulating */
278         int                 startpos[2]; /* starting pos for abs devices */
279         int                 flags;       /* remember if we had rel or abs movement */
280     } emulate3B;
281     struct {
282         int                 meta;           /* meta key to lock any button */
283         BOOL                meta_state;     /* meta_button state */
284         unsigned int        lock_pair[EVDEV_MAXBUTTONS];  /* specify a meta/lock pair */
285         BOOL                lock_state[EVDEV_MAXBUTTONS]; /* state of any locked buttons */
286     } dragLock;
287     struct {
288         BOOL                enabled;
289         int                 button;
290         int                 button_state;
291         int                 inertia;
292         WheelAxis           X;
293         WheelAxis           Y;
294         Time                expires;     /* time of expiry */
295         Time                timeout;
296     } emulateWheel;
297     /* run-time calibration */
298     struct {
299         int                 min_x;
300         int                 max_x;
301         int                 min_y;
302         int                 max_y;
303     } calibration;
304
305     unsigned char btnmap[32];           /* config-file specified button mapping */
306
307 #ifdef _F_REMAP_KEYS_
308     EvdevKeyRemapPtr keyremap;
309 #endif //_F_REMAP_KEYS_
310
311     int reopen_attempts; /* max attempts to re-open after read failure */
312     int reopen_left;     /* number of attempts left to re-open the device */
313     OsTimerPtr reopen_timer;
314
315 #ifdef _F_EVDEV_CONFINE_REGION_
316     //Backup pointer(s) for cursor
317     CursorLimitsProcPtr pOrgCursorLimits;
318     ConstrainCursorProcPtr pOrgConstrainCursor;
319
320     //Confining information
321     int confined_id;
322     BoxPtr pointer_confine_region;
323 #endif /* #ifdef _F_EVDEV_CONFINE_REGION_ */
324 #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_
325     BOOL rel_move_status;
326     BOOL rel_move_prop_set;
327     BOOL rel_move_ack;
328     OsTimerPtr rel_move_timer;
329 #endif /* #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_ */
330     Bool block_handler_registered;
331 #ifdef _F_GESTURE_EXTENSION_
332     int *mt_status;
333 #endif /* #ifdef _F_GESTURE_EXTENSION_ */
334
335 #ifdef _F_TOUCH_TRANSFORM_MATRIX_
336     float transform[9];
337     BOOL use_transform;
338     struct pixman_transform inv_transform;
339 #endif /* #ifdef _F_TOUCH_TRANSFORM_MATRIX_ */
340
341     /* Cached info from device. */
342     char name[1024];
343     unsigned long bitmask[NLONGS(EV_CNT)];
344     unsigned long key_bitmask[NLONGS(KEY_CNT)];
345     unsigned long rel_bitmask[NLONGS(REL_CNT)];
346     unsigned long abs_bitmask[NLONGS(ABS_CNT)];
347     unsigned long led_bitmask[NLONGS(LED_CNT)];
348     struct input_absinfo absinfo[ABS_CNT];
349 #ifdef _F_EVDEV_SUPPORT_GAMEPAD
350     int pre_hatx;
351     int pre_haty;
352     int pre_x;
353     int pre_y;
354     int keycode_btnA;
355     int keycode_btnB;
356     int keycode_btnX;
357     int keycode_btnY;
358     int keycode_btnTL;
359     int keycode_btnTR;
360     int keycode_btnStart;
361     int keycode_btnSelect;
362     int keycode_btnPlay;
363
364     BOOL support_directional_key;
365
366     int abs_gamepad_labels[MAX_GAMEPAD_DEFINITION_ABS];
367     int key_gamepad_labels[MAX_GAMEPAD_DEFINITION_KEY];
368 #endif//_F_EVDEV_SUPPORT_GAMEPAD
369
370 #ifdef _F_USE_DEFAULT_XKB_RULES_
371     BOOL use_default_xkb_rmlvo;
372 #endif//_F_USE_DEFAULT_XKB_RULES_
373
374     /* minor/major number */
375     dev_t min_maj;
376
377     /* Event queue used to defer keyboard/button events until EV_SYN time. */
378     int                     num_queue;
379     EventQueueRec           queue[EVDEV_MAXQUEUE];
380
381     enum fkeymode           fkeymode;
382
383 #ifdef _F_EVDEV_SUPPORT_ROTARY_
384     void (*extra_rel_post_ofm) (InputInfoPtr pInfo, int num_v, int first_v, int v[MAX_VALUATORS]);
385     void (*extra_rel_post_hallic) (InputInfoPtr pInfo, int num_v, int first_v, int v[MAX_VALUATORS]);
386     int HW_Calibration;
387 #endif //_F_EVDEV_SUPPORT_ROTARY_
388 } EvdevRec, *EvdevPtr;
389
390 /* Event posting functions */
391 void EvdevQueueKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value);
392 void EvdevQueueButtonEvent(InputInfoPtr pInfo, int button, int value);
393 void EvdevQueueProximityEvent(InputInfoPtr pInfo, int value);
394 #ifdef MULTITOUCH
395 void EvdevQueueTouchEvent(InputInfoPtr pInfo, unsigned int touch,
396                           ValuatorMask *mask, uint16_t type);
397 #endif
398 void EvdevPostButtonEvent(InputInfoPtr pInfo, int button, enum ButtonAction act);
399 void EvdevQueueButtonClicks(InputInfoPtr pInfo, int button, int count);
400 void EvdevPostRelativeMotionEvents(InputInfoPtr pInfo, int num_v, int first_v,
401                                    int v[MAX_VALUATORS]);
402 void EvdevPostAbsoluteMotionEvents(InputInfoPtr pInfo, int num_v, int first_v,
403                                    int v[MAX_VALUATORS]);
404 unsigned int EvdevUtilButtonEventToButtonNumber(EvdevPtr pEvdev, int code);
405
406 /* Middle Button emulation */
407 int  EvdevMBEmuTimer(InputInfoPtr);
408 BOOL EvdevMBEmuFilterEvent(InputInfoPtr, int, BOOL);
409 void EvdevMBEmuWakeupHandler(pointer, int, pointer);
410 void EvdevMBEmuBlockHandler(pointer, struct timeval**, pointer);
411 void EvdevMBEmuPreInit(InputInfoPtr);
412 void EvdevMBEmuOn(InputInfoPtr);
413 void EvdevMBEmuFinalize(InputInfoPtr);
414
415 /* Third button emulation */
416 CARD32 Evdev3BEmuTimer(OsTimerPtr timer, CARD32 time, pointer arg);
417 BOOL Evdev3BEmuFilterEvent(InputInfoPtr, int, BOOL);
418 void Evdev3BEmuPreInit(InputInfoPtr pInfo);
419 void Evdev3BEmuOn(InputInfoPtr);
420 void Evdev3BEmuFinalize(InputInfoPtr);
421 void Evdev3BEmuProcessRelMotion(InputInfoPtr pInfo, int dx, int dy);
422 void Evdev3BEmuProcessAbsMotion(InputInfoPtr pInfo, ValuatorMask *vals);
423
424 /* Mouse Wheel emulation */
425 void EvdevWheelEmuPreInit(InputInfoPtr pInfo);
426 BOOL EvdevWheelEmuFilterButton(InputInfoPtr pInfo, unsigned int button, int value);
427 BOOL EvdevWheelEmuFilterMotion(InputInfoPtr pInfo, struct input_event *pEv);
428
429 /* Draglock code */
430 void EvdevDragLockPreInit(InputInfoPtr pInfo);
431 BOOL EvdevDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value);
432
433 void EvdevMBEmuInitProperty(DeviceIntPtr);
434 void Evdev3BEmuInitProperty(DeviceIntPtr);
435 void EvdevWheelEmuInitProperty(DeviceIntPtr);
436 void EvdevDragLockInitProperty(DeviceIntPtr);
437 void EvdevAppleInitProperty(DeviceIntPtr);
438 #ifdef _F_EVDEV_SUPPORT_ROTARY_
439 void EvdevRotaryInit(DeviceIntPtr device);
440 void EvdevRotaryUnInit(EvdevPtr pEvdev);
441 #endif //_F_EVDEV_SUPPORT_ROTARY_
442 #ifdef _F_EVDEV_SUPPORT_GAMEPAD
443 static void EvdevMappingGamepadAbsToKey(InputInfoPtr pInfo,  struct input_event *ev);
444 static void EvdevMappingGamepadKeyToKey(InputInfoPtr pInfo,  struct input_event *ev);
445 static int EvdevIsGamePad(InputInfoPtr pInfo);
446 #endif//_F_EVDEV_SUPPORT_GAMEPAD
447 static void EvdevProcessEvent(InputInfoPtr pInfo, struct input_event *ev);
448 #endif
449 #ifdef _F_USE_DEFAULT_XKB_RULES_
450 void EvdevGetXkbRules(DeviceIntPtr device, XkbRMLVOSet * rmlvo);
451 #endif //_F_USE_DEFAULT_XKB_RULES_