2 * Copyright © 2004-2008 Red Hat, Inc.
3 * Copyright © 2008 University of South Australia
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
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.
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)
38 #include <linux/input.h>
39 #include <linux/types.h>
41 #include <xorg-server.h>
42 #include <xf86Xinput.h>
43 #include <xf86_OSproc.h>
50 #ifdef _F_GESTURE_EXTENSION_
51 typedef enum _MTSyncType
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
63 ET_Internal = 0xFF /* First byte */
66 typedef struct _AnyEvent AnyEvent;
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 */
79 union _InternalEvent {
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. */
88 #endif /* #ifdef _F_GESTURE_EXTENSION_ */
90 #ifndef EV_CNT /* linux 2.6.23 kernels and earlier lack _CNT defines */
91 #define EV_CNT (EV_MAX+1)
94 #define KEY_CNT (KEY_MAX+1)
97 #define REL_CNT (REL_MAX+1)
100 #define ABS_CNT (ABS_MAX+1)
103 #define LED_CNT (LED_MAX+1)
106 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
107 #define HAVE_SMOOTH_SCROLLING 1
110 #define EVDEV_MAXBUTTONS 32
111 #define EVDEV_MAXQUEUE 32
113 #define EVDEV_PRESS 1
114 #define EVDEV_RELEASE 0
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)
135 #define MAX_GAMEPAD_DEFINITION_ABS 3
136 #define MAX_GAMEPAD_DEFINITION_KEY 10
137 #endif //_F_EVDEV_SUPPORT_GAMEPAD
139 #ifdef _F_EVDEV_SUPPORT_ROTARY_
140 #define EVDEV_OFM (1 << 14)
141 #define EVDEV_HALLIC (1 << 15)
143 #define DEFAULT_HW_ROTARY_MAX 4360
144 #endif //_F_EVDEV_SUPPORT_ROTARY_
146 #ifndef MAX_VALUATORS
147 #define MAX_VALUATORS 36
150 #ifndef XI_PROP_DEVICE_NODE
151 #define XI_PROP_DEVICE_NODE "Device Node"
154 #ifndef XI_PROP_DEVICE_TYPE
155 #define XI_PROP_DEVICE_TYPE "Device Type"
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_ */
163 #define LONG_BITS (sizeof(long) * 8)
165 /* Number of longs needed to hold the given number of bits */
166 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
168 /* Function key mode */
170 FKEYMODE_UNKNOWN = 0,
171 FKEYMODE_FKEYS, /* function keys send function keys */
172 FKEYMODE_MMKEYS, /* function keys send multimedia keys */
187 /* axis specific data for wheel emulation */
191 int traveled_distance;
192 } WheelAxis, *WheelAxisPtr;
194 /* Event queue used to defer keyboard/button events until EV_SYN time. */
197 EV_QUEUE_KEY, /* xf86PostKeyboardEvent() */
198 EV_QUEUE_BTN, /* xf86PostButtonEvent() */
199 EV_QUEUE_PROXIMITY, /* xf86PostProximityEvent() */
201 EV_QUEUE_TOUCH, /*xf86PostTouchEvent() */
205 int key; /* May be either a key code or button number. */
207 unsigned int touch; /* Touch ID */
210 int val; /* State of the key/button/touch; pressed or released. */
212 ValuatorMask *touchMask;
214 } EventQueueRec, *EventQueuePtr;
216 #ifdef _F_REMAP_KEYS_
219 } EvdevKeyRemapSlice;
221 EvdevKeyRemapSlice* sl[256];
222 } EvdevKeyRemap, *EvdevKeyRemapPtr;
223 #endif //_F_REMAP_KEYS_
226 unsigned short id_vendor;
227 unsigned short id_product;
230 int grabDevice; /* grab the event device? */
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;
240 enum SlotState slot_state;
246 int in_proximity; /* device in proximity */
247 int use_proximity; /* using the proximity bit? */
248 int num_buttons; /* number of buttons */
254 unsigned int abs_queued, rel_queued, prox_queued;
256 /* XKB stuff has to be per-device rather than per-driver */
259 /* Middle mouse button emulation */
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 */
268 /* Third mouse button emulation */
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 */
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 */
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 */
294 Time expires; /* time of expiry */
297 /* run-time calibration */
305 unsigned char btnmap[32]; /* config-file specified button mapping */
307 #ifdef _F_REMAP_KEYS_
308 EvdevKeyRemapPtr keyremap;
309 #endif //_F_REMAP_KEYS_
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;
315 #ifdef _F_EVDEV_CONFINE_REGION_
316 //Backup pointer(s) for cursor
317 CursorLimitsProcPtr pOrgCursorLimits;
318 ConstrainCursorProcPtr pOrgConstrainCursor;
320 //Confining information
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;
328 OsTimerPtr rel_move_timer;
329 #endif /* #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_ */
330 Bool block_handler_registered;
331 #ifdef _F_GESTURE_EXTENSION_
333 #endif /* #ifdef _F_GESTURE_EXTENSION_ */
335 #ifdef _F_TOUCH_TRANSFORM_MATRIX_
338 struct pixman_transform inv_transform;
339 #endif /* #ifdef _F_TOUCH_TRANSFORM_MATRIX_ */
341 /* Cached info from device. */
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
360 int keycode_btnStart;
361 int keycode_btnSelect;
364 BOOL support_directional_key;
366 int abs_gamepad_labels[MAX_GAMEPAD_DEFINITION_ABS];
367 int key_gamepad_labels[MAX_GAMEPAD_DEFINITION_KEY];
368 #endif//_F_EVDEV_SUPPORT_GAMEPAD
370 #ifdef _F_USE_DEFAULT_XKB_RULES_
371 BOOL use_default_xkb_rmlvo;
372 #endif//_F_USE_DEFAULT_XKB_RULES_
374 /* minor/major number */
377 /* Event queue used to defer keyboard/button events until EV_SYN time. */
379 EventQueueRec queue[EVDEV_MAXQUEUE];
381 enum fkeymode fkeymode;
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]);
387 #endif //_F_EVDEV_SUPPORT_ROTARY_
388 } EvdevRec, *EvdevPtr;
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);
395 void EvdevQueueTouchEvent(InputInfoPtr pInfo, unsigned int touch,
396 ValuatorMask *mask, uint16_t type);
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);
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);
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);
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);
430 void EvdevDragLockPreInit(InputInfoPtr pInfo);
431 BOOL EvdevDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value);
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);
449 #ifdef _F_USE_DEFAULT_XKB_RULES_
450 void EvdevGetXkbRules(DeviceIntPtr device, XkbRMLVOSet * rmlvo);
451 #endif //_F_USE_DEFAULT_XKB_RULES_