#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "evdev.h"
#include <X11/Xatom.h>
#include <xf86.h>
#include <xf86Xinput.h>
#include <exevents.h>
#include <evdev-properties.h>
#define DEFAULT_MOVE_THRESHOLD 20
static Atom prop_3bemu;
static Atom prop_3btimeout;
static Atom prop_3bbutton;
static Atom prop_3bthreshold;
enum EmulationState {
EM3B_OFF,
EM3B_PENDING,
EM3B_EMULATING
};
static void
Evdev3BEmuPostButtonEvent(InputInfoPtr pInfo, int button, enum ButtonAction act)
{
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
int absolute = Relative;
if (emu3B->flags & EVDEV_ABSOLUTE_EVENTS)
absolute = Absolute;
xf86PostButtonEventP(pInfo->dev, absolute, button,
(act == BUTTON_PRESS) ? 1 : 0, 0,
(absolute ? 2 : 0), emu3B->startpos);
}
CARD32
Evdev3BEmuTimer(OsTimerPtr timer, CARD32 time, pointer arg)
{
InputInfoPtr pInfo = (InputInfoPtr)arg;
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
int sigstate = 0;
sigstate = xf86BlockSIGIO ();
emu3B->state = EM3B_EMULATING;
Evdev3BEmuPostButtonEvent(pInfo, emu3B->button, BUTTON_PRESS);
xf86UnblockSIGIO (sigstate);
return 0;
}
static void
Evdev3BCancel(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
if (emu3B->state != EM3B_OFF)
{
TimerCancel(emu3B->timer);
emu3B->state = EM3B_OFF;
memset(emu3B->delta, 0, sizeof(emu3B->delta));
}
emu3B->flags = 0;
}
BOOL
Evdev3BEmuFilterEvent(InputInfoPtr pInfo, int button, BOOL press)
{
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
int ret = FALSE;
if (!emu3B->enabled)
goto out;
if (press)
emu3B->buttonstate |= button;
else
emu3B->buttonstate &= ~button;
if (button != 1)
{
switch (emu3B->state)
{
case EM3B_PENDING:
Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
Evdev3BCancel(pInfo);
break;
case EM3B_EMULATING:
Evdev3BEmuPostButtonEvent(pInfo, emu3B->button, BUTTON_RELEASE);
Evdev3BCancel(pInfo);
break;
default:
break;
}
goto out;
}
if ((emu3B->buttonstate & ~0x1) != 0)
goto out;
if (!press)
{
switch(emu3B->state)
{
case EM3B_PENDING:
Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
Evdev3BCancel(pInfo);
break;
case EM3B_EMULATING:
Evdev3BEmuPostButtonEvent(pInfo, emu3B->button, BUTTON_RELEASE);
Evdev3BCancel(pInfo);
ret = TRUE;
break;
default:
break;
}
goto out;
}
if (press && emu3B->state == EM3B_OFF)
{
emu3B->state = EM3B_PENDING;
emu3B->timer = TimerSet(emu3B->timer, 0, emu3B->timeout,
Evdev3BEmuTimer, pInfo);
ret = TRUE;
goto out;
}
out:
return ret;
}
void
Evdev3BEmuProcessAbsMotion(InputInfoPtr pInfo, ValuatorMask *vals)
{
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
int cancel = FALSE;
int axis = 0;
if (emu3B->state != EM3B_PENDING)
{
if (valuator_mask_isset(vals, 0))
emu3B->startpos[0] = valuator_mask_get(vals, 0);
if (valuator_mask_isset(vals, 1))
emu3B->startpos[1] = valuator_mask_get(vals, 1);
return;
}
if ((emu3B->flags & EVDEV_ABSOLUTE_EVENTS) == 0)
emu3B->flags |= EVDEV_ABSOLUTE_EVENTS;
while (axis <= 1 && !cancel)
{
if (valuator_mask_isset(vals, axis))
{
int delta = valuator_mask_get(vals, axis) - emu3B->startpos[axis];
if (abs(delta) > emu3B->threshold)
cancel = TRUE;
}
axis++;
}
if (cancel)
{
Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
Evdev3BCancel(pInfo);
}
}
void
Evdev3BEmuProcessRelMotion(InputInfoPtr pInfo, int dx, int dy)
{
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
if (emu3B->state != EM3B_PENDING)
return;
emu3B->delta[0] += dx;
emu3B->delta[1] += dy;
emu3B->flags |= EVDEV_RELATIVE_EVENTS;
if (abs(emu3B->delta[0]) > emu3B->threshold ||
abs(emu3B->delta[1]) > emu3B->threshold)
{
Evdev3BEmuPostButtonEvent(pInfo, 1, BUTTON_PRESS);
Evdev3BCancel(pInfo);
}
}
void
Evdev3BEmuPreInit(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
emu3B->enabled = xf86SetBoolOption(pInfo->options,
"EmulateThirdButton",
FALSE);
emu3B->timeout = xf86SetIntOption(pInfo->options,
"EmulateThirdButtonTimeout",
1000);
emu3B->button = xf86SetIntOption(pInfo->options,
"EmulateThirdButtonButton",
3);
emu3B->threshold = xf86SetIntOption(pInfo->options,
"EmulateThirdButtonMoveThreshold",
DEFAULT_MOVE_THRESHOLD);
emu3B->timer = TimerSet(NULL, 0, 0, NULL, NULL);
}
void
Evdev3BEmuOn(InputInfoPtr pInfo)
{
}
void
Evdev3BEmuFinalize(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
TimerFree(emu3B->timer);
emu3B->timer = NULL;
}
static int
Evdev3BEmuSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
BOOL checkonly)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
if (atom == prop_3bemu)
{
if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
return BadMatch;
if (!checkonly)
emu3B->enabled = *((BOOL*)val->data);
} else if (atom == prop_3btimeout)
{
if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
return BadMatch;
if (!checkonly)
emu3B->timeout = *((CARD32*)val->data);
} else if (atom == prop_3bbutton)
{
if (val->format != 8 || val->size != 1 || val->type != XA_INTEGER)
return BadMatch;
if (!checkonly)
emu3B->button = *((CARD8*)val->data);
} else if (atom == prop_3bthreshold)
{
if (val->format != 32 || val->size != 1 || val->type != XA_INTEGER)
return BadMatch;
if (!checkonly)
emu3B->threshold = *((CARD32*)val->data);
}
return Success;
}
void
Evdev3BEmuInitProperty(DeviceIntPtr dev)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
struct emulate3B *emu3B = &pEvdev->emulate3B;
int rc;
if (!dev->button)
return;
prop_3bemu = MakeAtom(EVDEV_PROP_THIRDBUTTON, strlen(EVDEV_PROP_THIRDBUTTON), TRUE);
rc = XIChangeDeviceProperty(dev, prop_3bemu, XA_INTEGER, 8,
PropModeReplace, 1,
&emu3B->enabled,
FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_3bemu, FALSE);
prop_3btimeout = MakeAtom(EVDEV_PROP_THIRDBUTTON_TIMEOUT,
strlen(EVDEV_PROP_THIRDBUTTON_TIMEOUT),
TRUE);
rc = XIChangeDeviceProperty(dev, prop_3btimeout, XA_INTEGER, 32, PropModeReplace, 1,
&emu3B->timeout, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_3btimeout, FALSE);
prop_3bbutton = MakeAtom(EVDEV_PROP_THIRDBUTTON_BUTTON,
strlen(EVDEV_PROP_THIRDBUTTON_BUTTON),
TRUE);
rc = XIChangeDeviceProperty(dev, prop_3bbutton, XA_INTEGER, 8, PropModeReplace, 1,
&emu3B->button, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_3bbutton, FALSE);
prop_3bthreshold = MakeAtom(EVDEV_PROP_THIRDBUTTON_THRESHOLD,
strlen(EVDEV_PROP_THIRDBUTTON_THRESHOLD),
TRUE);
rc = XIChangeDeviceProperty(dev, prop_3bthreshold, XA_INTEGER, 32, PropModeReplace, 1,
&emu3B->threshold, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_3bthreshold, FALSE);
XIRegisterPropertyHandler(dev, Evdev3BEmuSetProperty, NULL, NULL);
}