#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <X11/keysym.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <xf86.h>
#include <xf86Xinput.h>
#include <exevents.h>
#include <xorgVersion.h>
#ifdef XKB
#include <xkbsrv.h>
#endif
#include "evdev.h"
#ifdef HAVE_PROPERTIES
#include <X11/Xatom.h>
#include <evdev-properties.h>
#endif
#ifndef EVIOCGRAB
#define EVIOCGRAB _IOW('E', 0x90, int)
#endif
#ifndef BTN_TASK
#define BTN_TASK 0x117
#endif
#ifndef EV_SYN
#define EV_SYN EV_RST
#endif
#define ArrayLength(a) (sizeof(a) / (sizeof((a)[0])))
#define EVDEV_KEYBOARD_EVENTS (1 << 0)
#define EVDEV_BUTTON_EVENTS (1 << 1)
#define EVDEV_RELATIVE_EVENTS (1 << 2)
#define EVDEV_ABSOLUTE_EVENTS (1 << 3)
#define EVDEV_TOUCHPAD (1 << 4)
#define EVDEV_INITIALIZED (1 << 5)
#define EVDEV_TOUCHSCREEN (1 << 6)
#define EVDEV_CALIBRATED (1 << 7)
#define MIN_KEYCODE 8
#define GLYPHS_PER_KEY 2
#define AltMask Mod1Mask
#define NumLockMask Mod2Mask
#define AltLangMask Mod3Mask
#define KanaMask Mod4Mask
#define ScrollLockMask Mod5Mask
#define CAPSFLAG 1
#define NUMFLAG 2
#define SCROLLFLAG 4
#define MODEFLAG 8
#define COMPOSEFLAG 16
static const char *evdevDefaults[] = {
"XkbRules", "evdev",
"XkbModel", "evdev",
"XkbLayout", "us",
NULL
};
static int EvdevOn(DeviceIntPtr);
static int EvdevCacheCompare(InputInfoPtr pInfo, BOOL compare);
#ifdef HAVE_PROPERTIES
static void EvdevInitProperty(DeviceIntPtr dev);
static int EvdevSetProperty(DeviceIntPtr dev, Atom atom,
XIPropertyValuePtr val, BOOL checkonly);
static Atom prop_invert = 0;
static Atom prop_reopen = 0;
static Atom prop_calibration = 0;
#endif
static void
SetXkbOption(InputInfoPtr pInfo, char *name, char **option)
{
char *s;
if ((s = xf86SetStrOption(pInfo->options, name, NULL))) {
if (!s[0]) {
xfree(s);
*option = NULL;
} else {
*option = s;
xf86Msg(X_CONFIG, "%s: %s: \"%s\"\n", pInfo->name, name, s);
}
}
}
static int wheel_up_button = 4;
static int wheel_down_button = 5;
static int wheel_left_button = 6;
static int wheel_right_button = 7;
static void
PostButtonClicks(InputInfoPtr pInfo, int button, int count)
{
int i;
for (i = 0; i < count; i++) {
xf86PostButtonEvent(pInfo->dev, 0, button, 1, 0, 0);
xf86PostButtonEvent(pInfo->dev, 0, button, 0, 0, 0);
}
}
static void
PostKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value)
{
int code = ev->code + MIN_KEYCODE;
static char warned[KEY_MAX];
if (value == 2 &&
(ev->code == KEY_LEFTCTRL || ev->code == KEY_RIGHTCTRL ||
ev->code == KEY_LEFTSHIFT || ev->code == KEY_RIGHTSHIFT ||
ev->code == KEY_LEFTALT || ev->code == KEY_RIGHTALT ||
ev->code == KEY_LEFTMETA || ev->code == KEY_RIGHTMETA ||
ev->code == KEY_CAPSLOCK || ev->code == KEY_NUMLOCK ||
ev->code == KEY_SCROLLLOCK))
return;
if (code > 255 && ev->code < KEY_MAX) {
if (!warned[ev->code])
xf86Msg(X_WARNING, "%s: unable to handle keycode %d\n",
pInfo->name, ev->code);
warned[ev->code] = 1;
}
if (code > 255)
return;
xf86PostKeyboardEvent(pInfo->dev, code, value);
}
static CARD32
EvdevReopenTimer(OsTimerPtr timer, CARD32 time, pointer arg)
{
InputInfoPtr pInfo = (InputInfoPtr)arg;
EvdevPtr pEvdev = pInfo->private;
do {
pInfo->fd = open(pEvdev->device, O_RDWR, 0);
} while (pInfo->fd < 0 && errno == EINTR);
if (pInfo->fd != -1)
{
if (EvdevCacheCompare(pInfo, TRUE) == Success)
{
xf86Msg(X_INFO, "%s: Device reopened after %d attempts.\n", pInfo->name,
pEvdev->reopen_attempts - pEvdev->reopen_left + 1);
EvdevOn(pInfo->dev);
} else
{
xf86Msg(X_ERROR, "%s: Device has changed - disabling.\n",
pInfo->name);
DisableDevice(pInfo->dev);
close(pInfo->fd);
pInfo->fd = -1;
}
pEvdev->reopen_left = 0;
return 0;
}
pEvdev->reopen_left--;
if (!pEvdev->reopen_left)
{
xf86Msg(X_ERROR, "%s: Failed to reopen device after %d attempts.\n",
pInfo->name, pEvdev->reopen_attempts);
DisableDevice(pInfo->dev);
return 0;
}
return 100;
}
static void
EvdevReadInput(InputInfoPtr pInfo)
{
struct input_event ev;
int len, value;
int dx, dy;
unsigned int abs;
unsigned int button;
EvdevPtr pEvdev = pInfo->private;
dx = 0;
dy = 0;
abs = 0;
while (xf86WaitForInput (pInfo->fd, 0) > 0) {
len = read(pInfo->fd, &ev, sizeof ev);
if (len != sizeof ev) {
xf86Msg(X_ERROR, "%s: Read error: %s\n", pInfo->name, strerror(errno));
if (errno == ENODEV)
{
xf86RemoveEnabledDevice(pInfo);
close(pInfo->fd);
pInfo->fd = -1;
pEvdev->reopen_left = pEvdev->reopen_attempts;
pEvdev->reopen_timer = TimerSet(NULL, 0, 100, EvdevReopenTimer, pInfo);
}
break;
}
value = ev.value;
switch (ev.type) {
case EV_REL:
if (EvdevWheelEmuFilterMotion(pInfo, &ev))
break;
switch (ev.code) {
case REL_X:
dx += value;
break;
case REL_Y:
dy += value;
break;
case REL_WHEEL:
if (value > 0)
PostButtonClicks(pInfo, wheel_up_button, value);
else if (value < 0)
PostButtonClicks(pInfo, wheel_down_button, -value);
break;
case REL_DIAL:
case REL_HWHEEL:
if (value > 0)
PostButtonClicks(pInfo, wheel_right_button, value);
else if (value < 0)
PostButtonClicks(pInfo, wheel_left_button, -value);
break;
}
break;
case EV_ABS:
switch (ev.code) {
case ABS_X:
pEvdev->abs_x = value;
abs = 1;
break;
case ABS_Y:
pEvdev->abs_y = value;
abs = 1;
break;
}
break;
case EV_KEY:
if (ev.code >= BTN_MOUSE && ev.code < KEY_OK)
if (value == 2)
break;
switch (ev.code) {
case BTN_TOUCH:
case BTN_TOOL_PEN:
case BTN_TOOL_RUBBER:
case BTN_TOOL_BRUSH:
case BTN_TOOL_PENCIL:
case BTN_TOOL_AIRBRUSH:
case BTN_TOOL_FINGER:
case BTN_TOOL_MOUSE:
case BTN_TOOL_LENS:
pEvdev->tool = value ? ev.code : 0;
if (!(pEvdev->flags & EVDEV_TOUCHSCREEN))
break;
ev.code = BTN_LEFT;
default:
button = EvdevUtilButtonEventToButtonNumber(pEvdev, ev.code);
if (EvdevDragLockFilterEvent(pInfo, button, value))
break;
if (EvdevWheelEmuFilterButton(pInfo, button, value))
break;
if (EvdevMBEmuFilterEvent(pInfo, button, value))
break;
if (button)
xf86PostButtonEvent(pInfo->dev, 0, button, value, 0, 0);
else
PostKbdEvent(pInfo, &ev, value);
break;
}
break;
case EV_SYN:
break;
}
}
if (pEvdev->flags & EVDEV_TOUCHPAD) {
abs = 0;
if (pEvdev->tool) {
if (pEvdev->old_x != -1)
dx = pEvdev->abs_x - pEvdev->old_x;
if (pEvdev->old_y != -1)
dy = pEvdev->abs_y - pEvdev->old_y;
pEvdev->old_x = pEvdev->abs_x;
pEvdev->old_y = pEvdev->abs_y;
} else {
pEvdev->old_x = pEvdev->old_y = -1;
}
}
if (dx != 0 || dy != 0) {
if (pEvdev->invert_x)
dx *= -1;
if (pEvdev->invert_y)
dy *= -1;
xf86PostMotionEvent(pInfo->dev, FALSE, 0, 2, dx, dy);
}
if (abs && pEvdev->tool) {
int abs_x, abs_y;
abs_x = pEvdev->abs_x;
abs_y = pEvdev->abs_y;
if (pEvdev->flags & EVDEV_CALIBRATED)
{
abs_x = xf86ScaleAxis(abs_x,
pEvdev->max_x, pEvdev->min_x,
pEvdev->calibration.max_x, pEvdev->calibration.min_x);
abs_y = xf86ScaleAxis(abs_y,
pEvdev->max_y, pEvdev->min_y,
pEvdev->calibration.max_y, pEvdev->calibration.min_y);
}
if (pEvdev->invert_x)
abs_x = pEvdev->max_x - (abs_x - pEvdev->min_x);
if (pEvdev->invert_y)
abs_y = pEvdev->max_y - (abs_y - pEvdev->min_y);
xf86PostMotionEvent(pInfo->dev, TRUE, 0, 2, abs_x, abs_y);
}
}
#define TestBit(bit, array) (array[(bit) / LONG_BITS]) & (1L << ((bit) % LONG_BITS))
static void
EvdevPtrCtrlProc(DeviceIntPtr device, PtrCtrl *ctrl)
{
}
static KeySym map[] = {
NoSymbol, NoSymbol,
XK_Escape, NoSymbol,
XK_1, XK_exclam,
XK_2, XK_at,
XK_3, XK_numbersign,
XK_4, XK_dollar,
XK_5, XK_percent,
XK_6, XK_asciicircum,
XK_7, XK_ampersand,
XK_8, XK_asterisk,
XK_9, XK_parenleft,
XK_0, XK_parenright,
XK_minus, XK_underscore,
XK_equal, XK_plus,
XK_BackSpace, NoSymbol,
XK_Tab, XK_ISO_Left_Tab,
XK_Q, NoSymbol,
XK_W, NoSymbol,
XK_E, NoSymbol,
XK_R, NoSymbol,
XK_T, NoSymbol,
XK_Y, NoSymbol,
XK_U, NoSymbol,
XK_I, NoSymbol,
XK_O, NoSymbol,
XK_P, NoSymbol,
XK_bracketleft, XK_braceleft,
XK_bracketright,XK_braceright,
XK_Return, NoSymbol,
XK_Control_L, NoSymbol,
XK_A, NoSymbol,
XK_S, NoSymbol,
XK_D, NoSymbol,
XK_F, NoSymbol,
XK_G, NoSymbol,
XK_H, NoSymbol,
XK_J, NoSymbol,
XK_K, NoSymbol,
XK_L, NoSymbol,
XK_semicolon, XK_colon,
XK_quoteright, XK_quotedbl,
XK_quoteleft, XK_asciitilde,
XK_Shift_L, NoSymbol,
XK_backslash, XK_bar,
XK_Z, NoSymbol,
XK_X, NoSymbol,
XK_C, NoSymbol,
XK_V, NoSymbol,
XK_B, NoSymbol,
XK_N, NoSymbol,
XK_M, NoSymbol,
XK_comma, XK_less,
XK_period, XK_greater,
XK_slash, XK_question,
XK_Shift_R, NoSymbol,
XK_KP_Multiply, NoSymbol,
XK_Alt_L, XK_Meta_L,
XK_space, NoSymbol,
XK_Caps_Lock, NoSymbol,
XK_F1, NoSymbol,
XK_F2, NoSymbol,
XK_F3, NoSymbol,
XK_F4, NoSymbol,
XK_F5, NoSymbol,
XK_F6, NoSymbol,
XK_F7, NoSymbol,
XK_F8, NoSymbol,
XK_F9, NoSymbol,
XK_F10, NoSymbol,
XK_Num_Lock, NoSymbol,
XK_Scroll_Lock, NoSymbol,
XK_KP_Home, XK_KP_7,
XK_KP_Up, XK_KP_8,
XK_KP_Prior, XK_KP_9,
XK_KP_Subtract, NoSymbol,
XK_KP_Left, XK_KP_4,
XK_KP_Begin, XK_KP_5,
XK_KP_Right, XK_KP_6,
XK_KP_Add, NoSymbol,
XK_KP_End, XK_KP_1,
XK_KP_Down, XK_KP_2,
XK_KP_Next, XK_KP_3,
XK_KP_Insert, XK_KP_0,
XK_KP_Delete, XK_KP_Decimal,
NoSymbol, NoSymbol,
XK_F13, NoSymbol,
XK_less, XK_greater,
XK_F11, NoSymbol,
XK_F12, NoSymbol,
XK_F14, NoSymbol,
XK_F15, NoSymbol,
XK_F16, NoSymbol,
XK_F17, NoSymbol,
XK_F18, NoSymbol,
XK_F19, NoSymbol,
XK_F20, NoSymbol,
XK_KP_Enter, NoSymbol,
XK_Control_R, NoSymbol,
XK_KP_Divide, NoSymbol,
XK_Print, XK_Sys_Req,
XK_Alt_R, XK_Meta_R,
NoSymbol, NoSymbol,
XK_Home, NoSymbol,
XK_Up, NoSymbol,
XK_Prior, NoSymbol,
XK_Left, NoSymbol,
XK_Right, NoSymbol,
XK_End, NoSymbol,
XK_Down, NoSymbol,
XK_Next, NoSymbol,
XK_Insert, NoSymbol,
XK_Delete, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
XK_KP_Equal, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
XK_F21, NoSymbol,
XK_F22, NoSymbol,
XK_F23, NoSymbol,
XK_F24, NoSymbol,
XK_KP_Separator, NoSymbol,
XK_Meta_L, NoSymbol,
XK_Meta_R, NoSymbol,
XK_Multi_key, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
NoSymbol, NoSymbol,
};
static void
EvdevKbdCtrl(DeviceIntPtr device, KeybdCtrl *ctrl)
{
static struct { int xbit, code; } bits[] = {
{ CAPSFLAG, LED_CAPSL },
{ NUMFLAG, LED_NUML },
{ SCROLLFLAG, LED_SCROLLL },
{ MODEFLAG, LED_KANA },
{ COMPOSEFLAG, LED_COMPOSE }
};
InputInfoPtr pInfo;
struct input_event ev[ArrayLength(bits)];
int i;
memset(ev, 0, sizeof(ev));
pInfo = device->public.devicePrivate;
for (i = 0; i < ArrayLength(bits); i++) {
ev[i].type = EV_LED;
ev[i].code = bits[i].code;
ev[i].value = (ctrl->leds & bits[i].xbit) > 0;
}
write(pInfo->fd, ev, sizeof ev);
}
static int
EvdevAddKeyClass(DeviceIntPtr device)
{
InputInfoPtr pInfo;
KeySymsRec keySyms;
CARD8 modMap[MAP_LENGTH];
KeySym sym;
int i, j;
EvdevPtr pEvdev;
static struct { KeySym keysym; CARD8 mask; } modifiers[] = {
{ XK_Shift_L, ShiftMask },
{ XK_Shift_R, ShiftMask },
{ XK_Control_L, ControlMask },
{ XK_Control_R, ControlMask },
{ XK_Caps_Lock, LockMask },
{ XK_Alt_L, AltMask },
{ XK_Alt_R, AltMask },
{ XK_Meta_L, Mod4Mask },
{ XK_Meta_R, Mod4Mask },
{ XK_Num_Lock, NumLockMask },
{ XK_Scroll_Lock, ScrollLockMask },
{ XK_Mode_switch, AltLangMask }
};
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
memset(modMap, 0, sizeof modMap);
for (i = 0; i < ArrayLength(map) / GLYPHS_PER_KEY; i++) {
sym = map[i * GLYPHS_PER_KEY];
for (j = 0; j < ArrayLength(modifiers); j++) {
if (modifiers[j].keysym == sym)
modMap[i + MIN_KEYCODE] = modifiers[j].mask;
}
}
keySyms.map = map;
keySyms.mapWidth = GLYPHS_PER_KEY;
keySyms.minKeyCode = MIN_KEYCODE;
keySyms.maxKeyCode = MIN_KEYCODE + ArrayLength(map) / GLYPHS_PER_KEY - 1;
#ifdef XKB
if (pEvdev->noXkb)
#endif
{
xf86Msg(X_CONFIG, "XKB: disabled\n");
if (!InitKeyboardDeviceStruct((DevicePtr)device, &keySyms, modMap,
NULL, EvdevKbdCtrl))
return !Success;
}
#ifdef XKB
else
{
xf86ReplaceStrOption(pInfo->options, "xkb_rules", "evdev");
SetXkbOption(pInfo, "xkb_rules", &pEvdev->xkb_rules);
SetXkbOption(pInfo, "xkb_model", &pEvdev->xkb_model);
if (!pEvdev->xkb_model)
SetXkbOption(pInfo, "XkbModel", &pEvdev->xkb_rules);
SetXkbOption(pInfo, "xkb_layout", &pEvdev->xkb_layout);
if (!pEvdev->xkb_layout)
SetXkbOption(pInfo, "XkbLayout", &pEvdev->xkb_layout);
SetXkbOption(pInfo, "xkb_variant", &pEvdev->xkb_variant);
if (!pEvdev->xkb_variant)
SetXkbOption(pInfo, "XkbVariant", &pEvdev->xkb_variant);
SetXkbOption(pInfo, "xkb_options", &pEvdev->xkb_options);
if (!pEvdev->xkb_options)
SetXkbOption(pInfo, "XkbOptions", &pEvdev->xkb_options);
XkbSetRulesDflts(pEvdev->xkb_rules, pEvdev->xkb_model,
pEvdev->xkb_layout, pEvdev->xkb_variant,
pEvdev->xkb_options);
if (!XkbInitKeyboardDeviceStruct(device, &pEvdev->xkbnames,
&keySyms, modMap, NULL,
EvdevKbdCtrl))
return !Success;
}
#endif
pInfo->flags |= XI86_KEYBOARD_CAPABLE;
return Success;
}
static int
EvdevAddAbsClass(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
struct input_absinfo absinfo_x, absinfo_y;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (ioctl(pInfo->fd,
EVIOCGABS(ABS_X), &absinfo_x) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGABS failed: %s\n", strerror(errno));
return !Success;
}
if (ioctl(pInfo->fd,
EVIOCGABS(ABS_Y), &absinfo_y) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGABS failed: %s\n", strerror(errno));
return !Success;
}
pEvdev->min_x = absinfo_x.minimum;
pEvdev->max_x = absinfo_x.maximum;
pEvdev->min_y = absinfo_y.minimum;
pEvdev->max_y = absinfo_y.maximum;
if (!InitValuatorClassDeviceStruct(device, 2,
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 3
GetMotionHistory,
#endif
GetMotionHistorySize(), Absolute))
return !Success;
xf86InitValuatorAxisStruct(device, 0, pEvdev->min_x, pEvdev->max_x,
10000, 0, 10000);
xf86InitValuatorDefaults(device, 0);
xf86InitValuatorAxisStruct(device, 1, pEvdev->min_y, pEvdev->max_y,
10000, 0, 10000);
xf86InitValuatorDefaults(device, 1);
xf86MotionHistoryAllocate(pInfo);
if (!InitPtrFeedbackClassDeviceStruct(device, EvdevPtrCtrlProc))
return !Success;
pInfo->flags |= XI86_POINTER_CAPABLE;
return Success;
}
static int
EvdevAddRelClass(DeviceIntPtr device)
{
InputInfoPtr pInfo;
pInfo = device->public.devicePrivate;
if (!InitValuatorClassDeviceStruct(device, 2,
#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 3
GetMotionHistory,
#endif
GetMotionHistorySize(), Relative))
return !Success;
xf86InitValuatorAxisStruct(device, 0, -1, -1, 1, 0, 1);
xf86InitValuatorDefaults(device, 0);
xf86InitValuatorAxisStruct(device, 1, -1, -1, 1, 0, 1);
xf86InitValuatorDefaults(device, 1);
xf86MotionHistoryAllocate(pInfo);
if (!InitPtrFeedbackClassDeviceStruct(device, EvdevPtrCtrlProc))
return !Success;
pInfo->flags |= XI86_POINTER_CAPABLE;
xf86MotionHistoryAllocate(pInfo);
return Success;
}
static int
EvdevAddButtonClass(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (!InitButtonClassDeviceStruct(device, ArrayLength(pEvdev->btnmap),
pEvdev->btnmap))
return !Success;
return Success;
}
static void
EvdevInitButtonMapping(InputInfoPtr pInfo)
{
int i, nbuttons = 1;
char *mapping = NULL;
EvdevPtr pEvdev = pInfo->private;
if ((mapping = xf86CheckStrOption(pInfo->options, "ButtonMapping", NULL)))
{
char *s = " ";
int btn = 0;
xf86Msg(X_CONFIG, "%s: ButtonMapping '%s'\n", pInfo->name, mapping);
while (s && *s != '\0' && nbuttons < EVDEV_MAXBUTTONS)
{
btn = strtol(mapping, &s, 10);
if (s == mapping || btn < 0 || btn > EVDEV_MAXBUTTONS)
{
xf86Msg(X_ERROR,
"%s: ... Invalid button mapping. Using defaults\n",
pInfo->name);
nbuttons = 1;
break;
}
pEvdev->btnmap[nbuttons++] = btn;
mapping = s;
}
}
for (i = nbuttons; i < ArrayLength(pEvdev->btnmap); i++)
pEvdev->btnmap[i] = i;
}
static int
EvdevInit(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (pEvdev->flags & EVDEV_KEYBOARD_EVENTS)
EvdevAddKeyClass(device);
if (pEvdev->flags & EVDEV_BUTTON_EVENTS)
EvdevAddButtonClass(device);
if (pEvdev->flags & EVDEV_RELATIVE_EVENTS)
EvdevAddRelClass(device);
else if (pEvdev->flags & EVDEV_ABSOLUTE_EVENTS)
EvdevAddAbsClass(device);
#ifdef HAVE_PROPERTIES
XIRegisterPropertyHandler(device, EvdevSetProperty, NULL, NULL);
EvdevInitProperty(device);
EvdevMBEmuInitProperty(device);
EvdevWheelEmuInitProperty(device);
EvdevDragLockInitProperty(device);
#endif
return Success;
}
static int
EvdevOn(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
int rc = 0;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (pInfo->fd != -1 && pEvdev->grabDevice &&
(rc = ioctl(pInfo->fd, EVIOCGRAB, (void *)1)))
{
xf86Msg(X_WARNING, "%s: Grab failed (%s)\n", pInfo->name,
strerror(errno));
if (rc && errno == ENODEV)
{
close(pInfo->fd);
pInfo->fd = -1;
}
}
if (pInfo->fd == -1)
{
pEvdev->reopen_left = pEvdev->reopen_attempts;
pEvdev->reopen_timer = TimerSet(NULL, 0, 100, EvdevReopenTimer, pInfo);
} else
{
xf86FlushInput(pInfo->fd);
xf86AddEnabledDevice(pInfo);
EvdevMBEmuOn(pInfo);
pEvdev->flags |= EVDEV_INITIALIZED;
device->public.on = TRUE;
}
return Success;
}
static int
EvdevProc(DeviceIntPtr device, int what)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
switch (what)
{
case DEVICE_INIT:
return EvdevInit(device);
case DEVICE_ON:
return EvdevOn(device);
case DEVICE_OFF:
if (pInfo->fd != -1)
{
if (pEvdev->grabDevice && ioctl(pInfo->fd, EVIOCGRAB, (void *)0))
xf86Msg(X_WARNING, "%s: Release failed (%s)\n", pInfo->name,
strerror(errno));
xf86RemoveEnabledDevice(pInfo);
close(pInfo->fd);
pInfo->fd = -1;
}
if (pEvdev->flags & EVDEV_INITIALIZED)
EvdevMBEmuFinalize(pInfo);
pEvdev->flags &= ~EVDEV_INITIALIZED;
device->public.on = FALSE;
if (pEvdev->reopen_timer)
{
TimerFree(pEvdev->reopen_timer);
pEvdev->reopen_timer = NULL;
}
break;
case DEVICE_CLOSE:
xf86Msg(X_INFO, "%s: Close\n", pInfo->name);
if (pInfo->fd != -1) {
close(pInfo->fd);
pInfo->fd = -1;
}
break;
}
return Success;
}
static int
EvdevCacheCompare(InputInfoPtr pInfo, BOOL compare)
{
EvdevPtr pEvdev = pInfo->private;
int i;
char name[1024] = {0};
long bitmask[NBITS(EV_MAX)] = {0};
long key_bitmask[NBITS(KEY_MAX)] = {0};
long rel_bitmask[NBITS(REL_MAX)] = {0};
long abs_bitmask[NBITS(ABS_MAX)] = {0};
long led_bitmask[NBITS(LED_MAX)] = {0};
struct input_absinfo absinfo[ABS_MAX];
if (ioctl(pInfo->fd,
EVIOCGNAME(sizeof(name) - 1), name) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGNAME failed: %s\n", strerror(errno));
goto error;
}
if (compare && strcmp(pEvdev->name, name))
goto error;
if (ioctl(pInfo->fd,
EVIOCGBIT(0, sizeof(bitmask)), bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGNAME failed: %s\n", strerror(errno));
goto error;
}
if (compare && memcmp(pEvdev->bitmask, bitmask, sizeof(bitmask)))
goto error;
if (ioctl(pInfo->fd,
EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGBIT failed: %s\n", strerror(errno));
goto error;
}
if (compare && memcmp(pEvdev->rel_bitmask, rel_bitmask, sizeof(rel_bitmask)))
goto error;
if (ioctl(pInfo->fd,
EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGBIT failed: %s\n", strerror(errno));
goto error;
}
if (compare && memcmp(pEvdev->abs_bitmask, abs_bitmask, sizeof(abs_bitmask)))
goto error;
if (ioctl(pInfo->fd,
EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGBIT failed: %s\n", strerror(errno));
goto error;
}
if (compare && memcmp(pEvdev->key_bitmask, key_bitmask, sizeof(key_bitmask)))
goto error;
if (ioctl(pInfo->fd,
EVIOCGBIT(EV_LED, sizeof(led_bitmask)), led_bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGBIT failed: %s\n", strerror(errno));
goto error;
}
if (compare && memcmp(pEvdev->led_bitmask, led_bitmask, sizeof(led_bitmask)))
goto error;
memset(absinfo, 0, sizeof(absinfo));
for (i = 0; i < ABS_MAX; i++)
{
if (TestBit(i, abs_bitmask))
{
if (ioctl(pInfo->fd, EVIOCGABS(i), &absinfo[i]) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGABS failed: %s\n", strerror(errno));
goto error;
}
}
}
if (compare && memcmp(pEvdev->absinfo, absinfo, sizeof(absinfo)))
goto error;
if (!compare)
{
strcpy(pEvdev->name, name);
memcpy(pEvdev->bitmask, bitmask, sizeof(bitmask));
memcpy(pEvdev->key_bitmask, key_bitmask, sizeof(key_bitmask));
memcpy(pEvdev->rel_bitmask, rel_bitmask, sizeof(rel_bitmask));
memcpy(pEvdev->abs_bitmask, abs_bitmask, sizeof(abs_bitmask));
memcpy(pEvdev->led_bitmask, led_bitmask, sizeof(led_bitmask));
memcpy(pEvdev->absinfo, absinfo, sizeof(absinfo));
}
return Success;
error:
return !Success;
}
static int
EvdevProbe(InputInfoPtr pInfo)
{
long key_bitmask[NBITS(KEY_MAX)] = {0};
long rel_bitmask[NBITS(REL_MAX)] = {0};
long abs_bitmask[NBITS(ABS_MAX)] = {0};
int i, has_axes, has_keys, num_buttons;
int kernel24 = 0;
EvdevPtr pEvdev = pInfo->private;
if (pEvdev->grabDevice && ioctl(pInfo->fd, EVIOCGRAB, (void *)1)) {
if (errno == EINVAL) {
kernel24 = 1;
pEvdev->grabDevice = 0;
} else {
xf86Msg(X_ERROR, "Grab failed. Device already configured?\n");
return 1;
}
} else if (pEvdev->grabDevice) {
ioctl(pInfo->fd, EVIOCGRAB, (void *)0);
}
if (ioctl(pInfo->fd,
EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGBIT failed: %s\n", strerror(errno));
return 1;
}
if (ioctl(pInfo->fd,
EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGBIT failed: %s\n", strerror(errno));
return 1;
}
if (ioctl(pInfo->fd,
EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) < 0) {
xf86Msg(X_ERROR, "ioctl EVIOCGBIT failed: %s\n", strerror(errno));
return 1;
}
has_axes = FALSE;
has_keys = FALSE;
num_buttons = 0;
for (i = BTN_MISC; i < BTN_JOYSTICK; i++)
{
if (TestBit(i, key_bitmask))
num_buttons++;
}
if (num_buttons)
{
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
pEvdev->buttons = num_buttons;
xf86Msg(X_INFO, "%s: Found %d mouse buttons\n", pInfo->name,
num_buttons);
}
if (TestBit(REL_X, rel_bitmask) && TestBit(REL_Y, rel_bitmask)) {
xf86Msg(X_INFO, "%s: Found x and y relative axes\n", pInfo->name);
pEvdev->flags |= EVDEV_RELATIVE_EVENTS;
has_axes = TRUE;
}
if (TestBit(ABS_X, abs_bitmask) && TestBit(ABS_Y, abs_bitmask)) {
xf86Msg(X_INFO, "%s: Found x and y absolute axes\n", pInfo->name);
pEvdev->flags |= EVDEV_ABSOLUTE_EVENTS;
if (TestBit(BTN_TOUCH, key_bitmask)) {
if (num_buttons) {
xf86Msg(X_INFO, "%s: Found absolute touchpad\n", pInfo->name);
pEvdev->flags |= EVDEV_TOUCHPAD;
pEvdev->old_x = pEvdev->old_y = -1;
} else {
xf86Msg(X_INFO, "%s: Found absolute touchscreen\n", pInfo->name);
pEvdev->flags |= EVDEV_TOUCHSCREEN;
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
}
}
has_axes = TRUE;
}
for (i = 0; i < BTN_MISC; i++)
if (TestBit(i, key_bitmask))
break;
if (i < BTN_MISC) {
xf86Msg(X_INFO, "%s: Found keys\n", pInfo->name);
pEvdev->flags |= EVDEV_KEYBOARD_EVENTS;
has_keys = TRUE;
}
if (has_axes && num_buttons) {
xf86Msg(X_INFO, "%s: Configuring as mouse\n", pInfo->name);
pInfo->flags |= XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS |
XI86_CONFIGURED;
pInfo->type_name = XI_MOUSE;
}
if (pEvdev->flags & EVDEV_TOUCHSCREEN) {
xf86Msg(X_INFO, "%s: Configuring as touchscreen\n", pInfo->name);
pInfo->type_name = XI_TOUCHSCREEN;
pInfo->flags |= XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS |
XI86_CONFIGURED;
}
if (has_keys) {
if (kernel24) {
xf86Msg(X_INFO, "%s: Kernel < 2.6 is too old, ignoring keyboard\n",
pInfo->name);
} else {
xf86Msg(X_INFO, "%s: Configuring as keyboard\n", pInfo->name);
pInfo->flags |= XI86_KEYBOARD_CAPABLE | XI86_CONFIGURED;
pInfo->type_name = XI_KEYBOARD;
}
}
if ((pInfo->flags & XI86_CONFIGURED) == 0) {
xf86Msg(X_WARNING, "%s: Don't know how to use device\n",
pInfo->name);
return 1;
}
return 0;
}
static InputInfoPtr
EvdevPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
{
InputInfoPtr pInfo;
const char *device;
EvdevPtr pEvdev;
if (!(pInfo = xf86AllocateInput(drv, 0)))
return NULL;
pInfo->name = dev->identifier;
pInfo->flags = 0;
pInfo->type_name = "UNKNOWN";
pInfo->device_control = EvdevProc;
pInfo->read_input = EvdevReadInput;
pInfo->history_size = 0;
pInfo->control_proc = NULL;
pInfo->close_proc = NULL;
pInfo->switch_mode = NULL;
pInfo->conversion_proc = NULL;
pInfo->reverse_conversion_proc = NULL;
pInfo->dev = NULL;
pInfo->private_flags = 0;
pInfo->always_core_feedback = 0;
pInfo->conf_idev = dev;
if (!(pEvdev = xcalloc(sizeof(EvdevRec), 1)))
return pInfo;
pInfo->private = pEvdev;
xf86CollectInputOptions(pInfo, evdevDefaults, NULL);
xf86ProcessCommonOptions(pInfo, pInfo->options);
pEvdev->tool = 1;
device = xf86CheckStrOption(dev->commonOptions, "Device", NULL);
if (!device) {
xf86Msg(X_ERROR, "%s: No device specified.\n", pInfo->name);
xf86DeleteInput(pInfo, 0);
return NULL;
}
pEvdev->device = device;
xf86Msg(X_CONFIG, "%s: Device: \"%s\"\n", pInfo->name, device);
do {
pInfo->fd = open(device, O_RDWR, 0);
} while (pInfo->fd < 0 && errno == EINTR);
if (pInfo->fd < 0) {
xf86Msg(X_ERROR, "Unable to open evdev device \"%s\".\n", device);
xf86DeleteInput(pInfo, 0);
return NULL;
}
pEvdev->reopen_attempts = xf86SetIntOption(pInfo->options, "ReopenAttempts", 10);
pEvdev->invert_x = xf86SetBoolOption(pInfo->options, "InvertX", FALSE);
pEvdev->invert_y = xf86SetBoolOption(pInfo->options, "InvertY", FALSE);
pEvdev->grabDevice = xf86CheckBoolOption(dev->commonOptions, "GrabDevice", 0);
pEvdev->noXkb = noXkbExtension;
EvdevInitButtonMapping(pInfo);
if (EvdevProbe(pInfo)) {
close(pInfo->fd);
xf86DeleteInput(pInfo, 0);
return NULL;
}
EvdevCacheCompare(pInfo, FALSE);
if (pEvdev->flags & EVDEV_BUTTON_EVENTS)
{
EvdevMBEmuPreInit(pInfo);
EvdevWheelEmuPreInit(pInfo);
EvdevDragLockPreInit(pInfo);
}
return pInfo;
}
_X_EXPORT InputDriverRec EVDEV = {
1,
"evdev",
NULL,
EvdevPreInit,
NULL,
NULL,
0
};
static void
EvdevUnplug(pointer p)
{
}
static pointer
EvdevPlug(pointer module,
pointer options,
int *errmaj,
int *errmin)
{
xf86AddInputDriver(&EVDEV, module, 0);
return module;
}
static XF86ModuleVersionInfo EvdevVersionRec =
{
"evdev",
MODULEVENDORSTRING,
MODINFOSTRING1,
MODINFOSTRING2,
XORG_VERSION_CURRENT,
PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
ABI_CLASS_XINPUT,
ABI_XINPUT_VERSION,
MOD_CLASS_XINPUT,
{0, 0, 0, 0}
};
_X_EXPORT XF86ModuleData evdevModuleData =
{
&EvdevVersionRec,
EvdevPlug,
EvdevUnplug
};
unsigned int
EvdevUtilButtonEventToButtonNumber(EvdevPtr pEvdev, int code)
{
unsigned int button = 0;
switch(code) {
case BTN_LEFT:
button = 1;
break;
case BTN_RIGHT:
button = 3;
break;
case BTN_MIDDLE:
button = 2;
break;
case BTN_0:
button = (TestBit(BTN_LEFT, pEvdev->key_bitmask)) ? 8 : 1;
break;
case BTN_1:
button = (TestBit(BTN_MIDDLE, pEvdev->key_bitmask)) ? 9 : 2;
break;
case BTN_2:
button = (TestBit(BTN_RIGHT, pEvdev->key_bitmask)) ? 10 : 3;
break;
case BTN_SIDE:
case BTN_EXTRA:
case BTN_FORWARD:
case BTN_BACK:
case BTN_TASK:
button = (code - BTN_LEFT + 5);
break;
default:
if ((code > BTN_TASK) && (code < KEY_OK)) {
if (code < BTN_JOYSTICK) {
if (code < BTN_MOUSE)
button = (code - BTN_0 + 5);
else
button = (code - BTN_LEFT + 5);
}
}
}
if (button > EVDEV_MAXBUTTONS)
return 0;
return button;
}
#ifdef HAVE_PROPERTIES
static void
EvdevInitProperty(DeviceIntPtr dev)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
int rc;
BOOL invert[2];
char reopen;
if (pEvdev->flags & (EVDEV_RELATIVE_EVENTS | EVDEV_ABSOLUTE_EVENTS))
{
invert[0] = pEvdev->invert_x;
invert[1] = pEvdev->invert_y;
prop_invert = MakeAtom(EVDEV_PROP_INVERT_AXES, strlen(EVDEV_PROP_INVERT_AXES), TRUE);
rc = XIChangeDeviceProperty(dev, prop_invert, XA_INTEGER, 8,
PropModeReplace, 2,
invert, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_invert, FALSE);
}
prop_reopen = MakeAtom(EVDEV_PROP_REOPEN, strlen(EVDEV_PROP_REOPEN),
TRUE);
reopen = pEvdev->reopen_attempts;
rc = XIChangeDeviceProperty(dev, prop_reopen, XA_INTEGER, 8,
PropModeReplace, 1, &reopen, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_reopen, FALSE);
prop_calibration = MakeAtom(EVDEV_PROP_CALIBRATION,
strlen(EVDEV_PROP_CALIBRATION), TRUE);
rc = XIChangeDeviceProperty(dev, prop_calibration, XA_INTEGER, 32,
PropModeReplace, 0, NULL, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_calibration, FALSE);
}
static int
EvdevSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
BOOL checkonly)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
if (atom == prop_invert)
{
BOOL* data;
if (val->format != 8 || val->size != 2 || val->type != XA_INTEGER)
return BadMatch;
if (!checkonly)
{
data = (BOOL*)val->data;
pEvdev->invert_x = data[0];
pEvdev->invert_y = data[1];
}
} else if (atom == prop_reopen)
{
if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
return BadMatch;
if (!checkonly)
pEvdev->reopen_attempts = *((CARD8*)val->data);
} else if (atom == prop_calibration)
{
if (val->format != 32 || val->type != XA_INTEGER)
return BadMatch;
if (val->size != 4 && val->size != 0)
return BadMatch;
if (!checkonly)
{
if (val->size == 0)
{
pEvdev->flags &= ~EVDEV_CALIBRATED;
pEvdev->calibration.min_x = 0;
pEvdev->calibration.max_x = 0;
pEvdev->calibration.min_y = 0;
pEvdev->calibration.max_y = 0;
} else if (val->size == 4)
{
CARD32 *vals = (CARD32*)val->data;
pEvdev->flags |= EVDEV_CALIBRATED;
pEvdev->calibration.min_x = vals[0];
pEvdev->calibration.max_x = vals[1];
pEvdev->calibration.min_y = vals[2];
pEvdev->calibration.max_y = vals[3];
}
}
}
return Success;
}
#endif