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>
53 #define TTRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_INPUT, NAME)
54 #define TTRACE_END() traceEnd(TTRACE_TAG_INPUT)
56 #define TTRACE_BEGIN(NAME)
58 #endif //ENABLE_TTRACE
60 #ifdef _F_GESTURE_EXTENSION_
61 typedef enum _MTSyncType
63 MTOUCH_FRAME_SYNC_END,
64 MTOUCH_FRAME_SYNC_BEGIN,
65 MTOUCH_FRAME_SYNC_UPDATE,
66 ROTARY_FRAME_SYNC_END,
67 ROTARY_FRAME_SYNC_BEGIN
72 #ifdef _F_PROXY_DEVICE_ENABLED_
74 ET_DeviceChanged = 17,
76 #endif /* #ifdef _F_PROXY_DEVICE_ENABLED_ */
78 ET_Internal = 0xFF /* First byte */
81 typedef struct _AnyEvent AnyEvent;
84 unsigned char header; /**< Always ET_Internal */
85 enum EventType type; /**< One of EventType */
86 int length; /**< Length in bytes */
87 Time time; /**< Time in ms */
94 #ifdef _F_PROXY_DEVICE_ENABLED_
96 #define DEVCHANGE_SLAVE_SWITCH 0x2
97 #define DEVCHANGE_KEYBOARD_EVENT 0x8
99 struct _DeviceChangedEvent {
100 unsigned char header; /**< Always ET_Internal */
101 enum EventType type; /**< ET_DeviceChanged */
102 int length; /**< Length in bytes */
103 Time time; /**< Time in ms */
104 int deviceid; /**< Device whose capabilities have changed */
105 int flags; /**< Mask of ::HAS_NEW_SLAVE,
106 ::POINTER_EVENT, ::KEYBOARD_EVENT */
107 int masterid; /**< MD when event was generated */
108 int sourceid; /**< The device that caused the change */
111 int num_buttons; /**< Number of buttons */
112 Atom names[MAX_BUTTONS];/**< Button names */
115 int num_valuators; /**< Number of axes */
117 uint32_t min; /**< Minimum value */
118 uint32_t max; /**< Maximum value */
119 double value; /**< Current value */
120 /* FIXME: frac parts of min/max */
121 uint32_t resolution; /**< Resolution counts/m */
122 uint8_t mode; /**< Relative or Absolute */
123 Atom name; /**< Axis name */
124 ScrollInfo scroll; /**< Smooth scrolling info */
125 } valuators[MAX_VALUATORS];
132 #endif /* #ifdef _F_PROXY_DEVICE_ENABLED_ */
134 union _InternalEvent {
136 unsigned char header; /**< Always ET_Internal */
137 enum EventType type; /**< One of ET_* */
138 int length; /**< Length in bytes */
139 Time time; /**< Time in ms. */
142 #ifdef _F_PROXY_DEVICE_ENABLED_
143 DeviceChangedEvent changed_event;
144 #endif /* #ifdef _F_PROXY_DEVICE_ENABLED_ */
147 #define GESTURE_DEV_NAME "Gesture"
148 /* Gesture driver will query devices information
149 * if a gesture driver's control function is called using DEVICE_READY*/
150 #define DEVICE_READY 11
152 #endif /* #ifdef _F_GESTURE_EXTENSION_ */
154 #ifndef EV_CNT /* linux 2.6.23 kernels and earlier lack _CNT defines */
155 #define EV_CNT (EV_MAX+1)
158 #define KEY_CNT (KEY_MAX+1)
161 #define REL_CNT (REL_MAX+1)
164 #define ABS_CNT (ABS_MAX+1)
167 #define LED_CNT (LED_MAX+1)
170 #if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
171 #define HAVE_SMOOTH_SCROLLING 1
174 #define EVDEV_MAXBUTTONS 32
175 #define EVDEV_MAXQUEUE 32
177 #define EVDEV_PRESS 1
178 #define EVDEV_RELEASE 0
181 #define EVDEV_RMS_TIMEOUT 5000
183 #define EVDEV_RMS_TIMEOUT 15000
187 #define EVDEV_KEYBOARD_EVENTS (1 << 0)
188 #define EVDEV_BUTTON_EVENTS (1 << 1)
189 #define EVDEV_RELATIVE_EVENTS (1 << 2)
190 #define EVDEV_ABSOLUTE_EVENTS (1 << 3)
191 #define EVDEV_TOUCHPAD (1 << 4)
192 #define EVDEV_INITIALIZED (1 << 5) /* WheelInit etc. called already? */
193 #define EVDEV_TOUCHSCREEN (1 << 6)
194 #define EVDEV_CALIBRATED (1 << 7) /* run-time calibrated? */
195 #define EVDEV_TABLET (1 << 8) /* device looks like a tablet? */
196 #define EVDEV_UNIGNORE_ABSOLUTE (1 << 9) /* explicitly unignore abs axes */
197 #define EVDEV_UNIGNORE_RELATIVE (1 << 10) /* explicitly unignore rel axes */
198 #define EVDEV_RELATIVE_MODE (1 << 11) /* Force relative events for devices with absolute axes */
199 #ifdef _F_EVDEV_CONFINE_REGION_
200 #define EVDEV_CONFINE_REGION (1 << 12)
201 #endif /* #ifdef _F_EVDEV_CONFINE_REGION_ */
202 #ifdef _F_EVDEV_SUPPORT_GAMEPAD
203 #define EVDEV_GAMEPAD (1 << 13)
205 #define MAX_GAMEPAD_DEFINITION_ABS 3
206 #define MAX_GAMEPAD_DEFINITION_KEY 10
207 #endif //_F_EVDEV_SUPPORT_GAMEPAD
209 #ifdef _F_EVDEV_SUPPORT_ROTARY_
210 #define EVDEV_OFM (1 << 14)
211 #define EVDEV_HALLIC (1 << 15)
213 #define DEFAULT_HW_ROTARY_MAX 4360
214 #endif //_F_EVDEV_SUPPORT_ROTARY_
216 #ifdef _F_EVDEV_SUPPORT_SMARTRC_
217 #define EVDEV_SMART_RC (1 << 16) /* air touch mouse is special remote controller for smart tv */
218 #define AIR_TOUCH_MOUSE "Advanced Touch REMOTE"
219 #endif //_F_EVDEV_SUPPORT_SMARTRC_
221 #ifndef MAX_VALUATORS
222 #define MAX_VALUATORS 36
225 #ifndef XI_PROP_DEVICE_NODE
226 #define XI_PROP_DEVICE_NODE "Device Node"
229 #ifndef XI_PROP_DEVICE_TYPE
230 #define XI_PROP_DEVICE_TYPE "Device Type"
233 #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_
234 #define XI_PROP_REL_MOVE_STATUS "Relative Move Status"
235 #define XI_PROP_REL_MOVE_ACK "Relative Move Acknowledge"
236 #endif /* #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_ */
237 #ifdef _F_BLOCK_MOTION_DEVICE_
238 #define XI_PROP_BLOCK_MOTION_DEVICE "Block Motion Device" /*Atom to block motion device */
239 #endif //_F_BLOCK_MOTION_DEVICE_
240 #ifdef _F_PICTURE_OFF_MODE_ENABLE_
241 #define XI_PROP_PICTURE_OFF_MODE "PICTURE_OFF_MODE" /* Atom to check picture off mode */
242 #endif //_F_PICTURE_OFF_MODE_ENABLE_
244 #ifdef _F_PROXY_DEVICE_ENABLED_
245 #define XI_PROP_USE_PROXY_SLAVE_DEVICE "Use Proxy Slave Device"
246 #endif /* #ifdef _F_PROXY_DEVICE_ENABLED_ */
248 #define XI_PROP_UNGRAB_DEVICE "Ungrab Device"
250 #define LONG_BITS (sizeof(long) * 8)
252 /* Number of longs needed to hold the given number of bits */
253 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
255 /* Function key mode */
257 FKEYMODE_UNKNOWN = 0,
258 FKEYMODE_FKEYS, /* function keys send function keys */
259 FKEYMODE_MMKEYS, /* function keys send multimedia keys */
274 /* axis specific data for wheel emulation */
278 int traveled_distance;
279 } WheelAxis, *WheelAxisPtr;
281 #ifdef _F_MULTI_PASSIVE_GRAB_
282 typedef struct _ActivatedKeyList
285 struct xorg_list list;
287 #endif //_F_MULTI_PASSIVE_GRAB_
289 /* Event queue used to defer keyboard/button events until EV_SYN time. */
292 EV_QUEUE_KEY, /* xf86PostKeyboardEvent() */
293 EV_QUEUE_BTN, /* xf86PostButtonEvent() */
294 EV_QUEUE_PROXIMITY, /* xf86PostProximityEvent() */
296 EV_QUEUE_TOUCH, /*xf86PostTouchEvent() */
300 int key; /* May be either a key code or button number. */
302 unsigned int touch; /* Touch ID */
305 int val; /* State of the key/button/touch; pressed or released. */
307 ValuatorMask *touchMask;
309 } EvdevEventQueueRec, *EvdevEventQueuePtr;
311 #ifdef _F_REMAP_KEYS_
314 } EvdevKeyRemapSlice;
316 EvdevKeyRemapSlice* sl[256];
317 } EvdevKeyRemap, *EvdevKeyRemapPtr;
318 #endif //_F_REMAP_KEYS_
321 #ifdef _F_MULTI_PASSIVE_GRAB_
322 ActivatedKeyList *activatedkeylist;
323 #endif //_F_MULTI_PASSIVE_GRAB_
325 unsigned short id_vendor;
326 unsigned short id_product;
329 int grabDevice; /* grab the event device? */
331 int num_vals; /* number of valuators */
332 int num_mt_vals; /* number of multitouch valuators */
333 int axis_map[max(ABS_CNT, REL_CNT)]; /* Map evdev <axis> to index */
334 ValuatorMask *vals; /* new values coming in */
335 ValuatorMask *old_vals; /* old values for calculating relative motion */
336 ValuatorMask *prox; /* last values set while not in proximity */
337 ValuatorMask *mt_mask;
338 ValuatorMask **last_mt_vals;
340 enum SlotState slot_state;
346 int in_proximity; /* device in proximity */
347 int use_proximity; /* using the proximity bit? */
348 int num_buttons; /* number of buttons */
354 unsigned int abs_queued, rel_queued, prox_queued;
356 /* XKB stuff has to be per-device rather than per-driver */
359 /* Middle mouse button emulation */
362 BOOL pending; /* timer waiting? */
363 int buttonstate; /* phys. button state */
364 int state; /* state machine (see bt3emu.c) */
365 Time expires; /* time of expiry */
368 /* Third mouse button emulation */
371 BOOL state; /* current state */
372 Time timeout; /* timeout until third button press */
373 int buttonstate; /* phys. button state */
374 int button; /* phys button to emit */
375 int threshold; /* move threshold in dev coords */
377 int delta[2]; /* delta x/y, accumulating */
378 int startpos[2]; /* starting pos for abs devices */
379 int flags; /* remember if we had rel or abs movement */
382 int meta; /* meta key to lock any button */
383 BOOL meta_state; /* meta_button state */
384 unsigned int lock_pair[EVDEV_MAXBUTTONS]; /* specify a meta/lock pair */
385 BOOL lock_state[EVDEV_MAXBUTTONS]; /* state of any locked buttons */
394 Time expires; /* time of expiry */
397 /* run-time calibration */
405 unsigned char btnmap[32]; /* config-file specified button mapping */
407 #ifdef _F_REMAP_KEYS_
408 EvdevKeyRemapPtr keyremap;
409 #endif //_F_REMAP_KEYS_
411 int reopen_attempts; /* max attempts to re-open after read failure */
412 int reopen_left; /* number of attempts left to re-open the device */
413 OsTimerPtr reopen_timer;
415 #ifdef _F_EVDEV_CONFINE_REGION_
416 //Backup pointer(s) for cursor
417 CursorLimitsProcPtr pOrgCursorLimits;
418 ConstrainCursorProcPtr pOrgConstrainCursor;
420 //Confining information
422 BoxPtr pointer_confine_region;
423 #endif /* #ifdef _F_EVDEV_CONFINE_REGION_ */
424 #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_
425 BOOL rel_move_status;
426 BOOL rel_move_prop_set;
428 OsTimerPtr rel_move_timer;
429 #endif /* #ifdef _F_ENABLE_REL_MOVE_STATUS_PROP_ */
430 Bool block_handler_registered;
431 #ifdef _F_GESTURE_EXTENSION_
433 #endif /* #ifdef _F_GESTURE_EXTENSION_ */
435 #ifdef _F_TOUCH_TRANSFORM_MATRIX_
438 struct pixman_transform inv_transform;
439 #endif /* #ifdef _F_TOUCH_TRANSFORM_MATRIX_ */
441 /* Cached info from device. */
443 unsigned long bitmask[NLONGS(EV_CNT)];
444 unsigned long key_bitmask[NLONGS(KEY_CNT)];
445 unsigned long rel_bitmask[NLONGS(REL_CNT)];
446 unsigned long abs_bitmask[NLONGS(ABS_CNT)];
447 unsigned long led_bitmask[NLONGS(LED_CNT)];
448 struct input_absinfo absinfo[ABS_CNT];
449 #ifdef _F_EVDEV_SUPPORT_GAMEPAD
460 int keycode_btnStart;
461 int keycode_btnSelect;
464 BOOL support_directional_key;
466 int abs_gamepad_labels[MAX_GAMEPAD_DEFINITION_ABS];
467 int key_gamepad_labels[MAX_GAMEPAD_DEFINITION_KEY];
468 #endif//_F_EVDEV_SUPPORT_GAMEPAD
470 #ifdef _F_USE_DEFAULT_XKB_RULES_
471 BOOL use_default_xkb_rmlvo;
472 #endif//_F_USE_DEFAULT_XKB_RULES_
474 #ifdef _F_SUPPORT_ROTATION_ANGLE_
477 #endif// _F_SUPPORT_ROTATION_ANGLE_
479 /* minor/major number */
482 /* Event queue used to defer keyboard/button events until EV_SYN time. */
484 EvdevEventQueueRec queue[EVDEV_MAXQUEUE];
486 enum fkeymode fkeymode;
488 #ifdef _F_EVDEV_SUPPORT_ROTARY_
489 void (*extra_rel_post_ofm) (InputInfoPtr pInfo, int num_v, int first_v, int v[MAX_VALUATORS]);
490 void (*extra_rel_post_hallic) (InputInfoPtr pInfo, int num_v, int first_v, int v[MAX_VALUATORS]);
492 #endif //_F_EVDEV_SUPPORT_ROTARY_
493 #ifdef _F_EVDEV_SUPPORT_SMARTRC_
495 #ifdef _F_SMART_RC_CHG_KBD_SRC_DEV_
496 int (*extra_input_process) (InputInfoPtr *ppInfo, struct input_event *ev);
498 int (*extra_input_process) (InputInfoPtr pInfo, struct input_event *ev);
500 void (*origin_input_process) (InputInfoPtr pInfo, struct input_event *ev);
501 #endif //_F_EVDEV_SUPPORT_SMARTRC_
503 #ifdef _F_PROXY_DEVICE_ENABLED_
506 InputInfoPtr proxy_device;
507 #endif //_F_PROXY_DEVICE_ENABLED_
508 } EvdevRec, *EvdevPtr;
510 /* Event posting functions */
511 void EvdevQueueKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value);
512 void EvdevQueueButtonEvent(InputInfoPtr pInfo, int button, int value);
513 void EvdevQueueProximityEvent(InputInfoPtr pInfo, int value);
515 void EvdevQueueTouchEvent(InputInfoPtr pInfo, unsigned int touch,
516 ValuatorMask *mask, uint16_t type);
518 void EvdevPostButtonEvent(InputInfoPtr pInfo, int button, enum ButtonAction act);
519 void EvdevQueueButtonClicks(InputInfoPtr pInfo, int button, int count);
520 void EvdevPostRelativeMotionEvents(InputInfoPtr pInfo, int num_v, int first_v,
521 int v[MAX_VALUATORS]);
522 void EvdevPostAbsoluteMotionEvents(InputInfoPtr pInfo, int num_v, int first_v,
523 int v[MAX_VALUATORS]);
524 unsigned int EvdevUtilButtonEventToButtonNumber(EvdevPtr pEvdev, int code);
526 /* Middle Button emulation */
527 int EvdevMBEmuTimer(InputInfoPtr);
528 BOOL EvdevMBEmuFilterEvent(InputInfoPtr, int, BOOL);
529 void EvdevMBEmuWakeupHandler(pointer, int, pointer);
530 void EvdevMBEmuBlockHandler(pointer, struct timeval**, pointer);
531 void EvdevMBEmuPreInit(InputInfoPtr);
532 void EvdevMBEmuOn(InputInfoPtr);
533 void EvdevMBEmuFinalize(InputInfoPtr);
535 /* Third button emulation */
536 CARD32 Evdev3BEmuTimer(OsTimerPtr timer, CARD32 time, pointer arg);
537 BOOL Evdev3BEmuFilterEvent(InputInfoPtr, int, BOOL);
538 void Evdev3BEmuPreInit(InputInfoPtr pInfo);
539 void Evdev3BEmuOn(InputInfoPtr);
540 void Evdev3BEmuFinalize(InputInfoPtr);
541 void Evdev3BEmuProcessRelMotion(InputInfoPtr pInfo, int dx, int dy);
542 void Evdev3BEmuProcessAbsMotion(InputInfoPtr pInfo, ValuatorMask *vals);
544 /* Mouse Wheel emulation */
545 void EvdevWheelEmuPreInit(InputInfoPtr pInfo);
546 BOOL EvdevWheelEmuFilterButton(InputInfoPtr pInfo, unsigned int button, int value);
547 BOOL EvdevWheelEmuFilterMotion(InputInfoPtr pInfo, struct input_event *pEv);
550 void EvdevDragLockPreInit(InputInfoPtr pInfo);
551 BOOL EvdevDragLockFilterEvent(InputInfoPtr pInfo, unsigned int button, int value);
553 void EvdevMBEmuInitProperty(DeviceIntPtr);
554 void Evdev3BEmuInitProperty(DeviceIntPtr);
555 void EvdevWheelEmuInitProperty(DeviceIntPtr);
556 void EvdevDragLockInitProperty(DeviceIntPtr);
557 void EvdevAppleInitProperty(DeviceIntPtr);
558 #ifdef _F_EVDEV_SUPPORT_ROTARY_
559 void EvdevRotaryInit(DeviceIntPtr device);
560 void EvdevRotaryUnInit(EvdevPtr pEvdev);
561 #endif //_F_EVDEV_SUPPORT_ROTARY_
562 #ifdef _F_EVDEV_SUPPORT_GAMEPAD
563 static void EvdevMappingGamepadAbsToKey(InputInfoPtr pInfo, struct input_event *ev);
564 static void EvdevMappingGamepadKeyToKey(InputInfoPtr pInfo, struct input_event *ev);
565 static int EvdevIsGamePad(InputInfoPtr pInfo);
566 #endif//_F_EVDEV_SUPPORT_GAMEPAD
567 static void EvdevProcessEvent(InputInfoPtr pInfo, struct input_event *ev);
568 #ifdef _F_USE_DEFAULT_XKB_RULES_
569 void EvdevGetXkbRules(DeviceIntPtr device, XkbRMLVOSet * rmlvo);
570 #endif //_F_USE_DEFAULT_XKB_RULES_
571 #ifdef _F_EVDEV_SUPPORT_SMARTRC_
572 void EvdevRCInit(DeviceIntPtr device);
573 #endif //_F_EVDEV_SUPPORT_SMARTRC_
574 #ifdef _F_PROXY_DEVICE_ENABLED_
575 void EvdevProxyInit(DeviceIntPtr device);
576 #endif //_F_PROXY_DEVICE_ENABLED_