#define NEED_EVENTS
#include "X.h"
#include "Xproto.h"
#include "xf86.h"
#ifdef XINPUT
#include "XI.h"
#include "XIproto.h"
#include "extnsionst.h"
#include "extinit.h"
#else
#include "inputstr.h"
#endif
#include "xf86Xinput.h"
#include "xf86_OSproc.h"
#include "xf86OSmouse.h"
#define NEED_XF86_TYPES
#include "xf86_ansic.h"
#include "compiler.h"
#include "xisb.h"
#include "mouse.h"
#include "mousePriv.h"
#include "mipointer.h"
enum {
NIB_BITS=4,
NIB_SIZE= (1 << NIB_BITS),
NIB_MASK= (NIB_SIZE -1),
NIB_COUNT = ((MSE_MAXBUTTONS+NIB_BITS-1)/NIB_BITS)
};
typedef struct _DragLockRec {
int lockButtonsM;
int masterLockM;
int lockLastButtons;
int masterTS;
int simulatedDown;
int nib_table[NIB_COUNT][NIB_SIZE];
} DragLockRec, *DragLockPtr;
#ifdef XFree86LOADER
static const OptionInfoRec *MouseAvailableOptions(void *unused);
#endif
static InputInfoPtr MousePreInit(InputDriverPtr drv, IDevPtr dev, int flags);
#if 0
static void MouseUnInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags);
#endif
static int MouseProc(DeviceIntPtr device, int what);
static Bool MouseConvert(LocalDevicePtr local, int first, int num, int v0,
int v1, int v2, int v3, int v4, int v5, int *x,
int *y);
static void MouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl);
static void MousePostEvent(InputInfoPtr pInfo, int buttons,
int dx, int dy, int dz, int dw);
static void MouseReadInput(InputInfoPtr pInfo);
static void MouseBlockHandler(pointer data, struct timeval **waitTime,
pointer LastSelectMask);
static void MouseWakeupHandler(pointer data, int i, pointer LastSelectMask);
static void FlushButtons(MouseDevPtr pMse);
static Bool SetupMouse(InputInfoPtr pInfo);
static Bool initMouseHW(InputInfoPtr pInfo);
#ifdef SUPPORT_MOUSE_RESET
static Bool mouseReset(InputInfoPtr pInfo, unsigned char val);
static void ps2WakeupHandler(pointer data, int i, pointer LastSelectMask);
static void ps2BlockHandler(pointer data, struct timeval **waitTime,
pointer LastSelectMask);
#endif
static const char *autoOSProtocol(InputInfoPtr pInfo, int *protoPara);
static void autoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync);
static void checkForErraticMovements(InputInfoPtr pInfo, int dx, int dy);
static Bool collectData(MouseDevPtr pMse, unsigned char u);
static void SetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID);
static Bool autoGood(MouseDevPtr pMse);
#undef MOUSE
InputDriverRec MOUSE = {
1,
"mouse",
NULL,
MousePreInit,
NULL,
NULL,
0
};
typedef enum {
OPTION_ALWAYS_CORE,
OPTION_SEND_CORE_EVENTS,
OPTION_CORE_POINTER,
OPTION_SEND_DRAG_EVENTS,
OPTION_HISTORY_SIZE,
OPTION_DEVICE,
OPTION_PROTOCOL,
OPTION_BUTTONS,
OPTION_EMULATE_3_BUTTONS,
OPTION_EMULATE_3_TIMEOUT,
OPTION_CHORD_MIDDLE,
OPTION_FLIP_XY,
OPTION_INV_X,
OPTION_INV_Y,
OPTION_ANGLE_OFFSET,
OPTION_Z_AXIS_MAPPING,
OPTION_SAMPLE_RATE,
OPTION_RESOLUTION,
OPTION_EMULATE_WHEEL,
OPTION_EMU_WHEEL_BUTTON,
OPTION_EMU_WHEEL_INERTIA,
OPTION_X_AXIS_MAPPING,
OPTION_Y_AXIS_MAPPING,
OPTION_AUTO_SOFT,
OPTION_CLEAR_DTR,
OPTION_CLEAR_RTS,
OPTION_BAUD_RATE,
OPTION_DATA_BITS,
OPTION_STOP_BITS,
OPTION_PARITY,
OPTION_FLOW_CONTROL,
OPTION_VTIME,
OPTION_VMIN,
OPTION_DRAGLOCKBUTTONS
} MouseOpts;
#ifdef XFree86LOADER
static const OptionInfoRec mouseOptions[] = {
{ OPTION_ALWAYS_CORE, "AlwaysCore", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_SEND_CORE_EVENTS, "SendCoreEvents", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_CORE_POINTER, "CorePointer", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_SEND_DRAG_EVENTS, "SendDragEvents", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_HISTORY_SIZE, "HistorySize", OPTV_INTEGER, {0}, FALSE },
{ OPTION_DEVICE, "Device", OPTV_STRING, {0}, FALSE },
{ OPTION_PROTOCOL, "Protocol", OPTV_STRING, {0}, FALSE },
{ OPTION_BUTTONS, "Buttons", OPTV_INTEGER, {0}, FALSE },
{ OPTION_EMULATE_3_BUTTONS, "Emulate3Buttons",OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_EMULATE_3_TIMEOUT, "Emulate3Timeout",OPTV_INTEGER, {0}, FALSE },
{ OPTION_CHORD_MIDDLE, "ChordMiddle", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_FLIP_XY, "FlipXY", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_INV_X, "InvX", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_INV_Y, "InvY", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_ANGLE_OFFSET, "AngleOffset", OPTV_INTEGER, {0}, FALSE },
{ OPTION_Z_AXIS_MAPPING, "ZAxisMapping", OPTV_STRING, {0}, FALSE },
{ OPTION_SAMPLE_RATE, "SampleRate", OPTV_INTEGER, {0}, FALSE },
{ OPTION_RESOLUTION, "Resolution", OPTV_INTEGER, {0}, FALSE },
{ OPTION_EMULATE_WHEEL, "EmulateWheel", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_EMU_WHEEL_BUTTON, "EmulateWheelButton", OPTV_INTEGER, {0}, FALSE },
{ OPTION_EMU_WHEEL_INERTIA, "EmulateWheelInertia", OPTV_INTEGER, {0}, FALSE },
{ OPTION_X_AXIS_MAPPING, "XAxisMapping", OPTV_STRING, {0}, FALSE },
{ OPTION_Y_AXIS_MAPPING, "YAxisMapping", OPTV_STRING, {0}, FALSE },
{ OPTION_AUTO_SOFT, "AutoSoft", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_CLEAR_DTR, "ClearDTR", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_CLEAR_RTS, "ClearRTS", OPTV_BOOLEAN, {0}, FALSE },
{ OPTION_BAUD_RATE, "BaudRate", OPTV_INTEGER, {0}, FALSE },
{ OPTION_DATA_BITS, "DataBits", OPTV_INTEGER, {0}, FALSE },
{ OPTION_STOP_BITS, "StopBits", OPTV_INTEGER, {0}, FALSE },
{ OPTION_PARITY, "Parity", OPTV_STRING, {0}, FALSE },
{ OPTION_FLOW_CONTROL, "FlowControl", OPTV_STRING, {0}, FALSE },
{ OPTION_VTIME, "VTime", OPTV_INTEGER, {0}, FALSE },
{ OPTION_VMIN, "VMin", OPTV_INTEGER, {0}, FALSE },
{ OPTION_DRAGLOCKBUTTONS, "DragLockButtons",OPTV_STRING, {0}, FALSE },
{ -1, NULL, OPTV_NONE, {0}, FALSE }
};
#endif
#define RETRY_COUNT 4
static const char *msDefaults[] = {
"BaudRate", "1200",
"DataBits", "7",
"StopBits", "1",
"Parity", "None",
"FlowControl", "None",
"VTime", "0",
"VMin", "1",
NULL
};
static const char *mlDefaults[] = {
"BaudRate", "1200",
"DataBits", "8",
"StopBits", "2",
"Parity", "None",
"FlowControl", "None",
"VTime", "0",
"VMin", "1",
NULL
};
static const char *mmDefaults[] = {
"BaudRate", "1200",
"DataBits", "8",
"StopBits", "1",
"Parity", "Odd",
"FlowControl", "None",
"VTime", "0",
"VMin", "1",
NULL
};
#if 0
static const char *logiDefaults[] = {
"BaudRate", "1200",
"DataBits", "8",
"StopBits", "2",
"Parity", "None",
"FlowControl", "None",
"VTime", "0",
"VMin", "1",
NULL
};
#endif
static const char *mmhitDefaults[] = {
"BaudRate", "1200",
"DataBits", "8",
"StopBits", "1",
"Parity", "None",
"FlowControl", "None",
"VTime", "0",
"VMin", "1",
NULL
};
static const char *acecadDefaults[] = {
"BaudRate", "9600",
"DataBits", "8",
"StopBits", "1",
"Parity", "Odd",
"FlowControl", "None",
"VTime", "0",
"VMin", "1",
NULL
};
static MouseProtocolRec mouseProtocols[] = {
{ "Microsoft", MSE_SERIAL, msDefaults, PROT_MS },
{ "MouseSystems", MSE_SERIAL, mlDefaults, PROT_MSC },
{ "MMSeries", MSE_SERIAL, mmDefaults, PROT_MM },
{ "Logitech", MSE_SERIAL, mlDefaults, PROT_LOGI },
{ "MouseMan", MSE_SERIAL, msDefaults, PROT_LOGIMAN },
{ "MMHitTab", MSE_SERIAL, mmhitDefaults, PROT_MMHIT },
{ "GlidePoint", MSE_SERIAL, msDefaults, PROT_GLIDE },
{ "IntelliMouse", MSE_SERIAL, msDefaults, PROT_IMSERIAL },
{ "ThinkingMouse", MSE_SERIAL, msDefaults, PROT_THINKING },
{ "AceCad", MSE_SERIAL, acecadDefaults, PROT_ACECAD },
{ "ValuMouseScroll", MSE_SERIAL, msDefaults, PROT_VALUMOUSESCROLL },
{ "PS/2", MSE_PS2, NULL, PROT_PS2 },
{ "GenericPS/2", MSE_PS2, NULL, PROT_GENPS2 },
{ "ImPS/2", MSE_XPS2, NULL, PROT_IMPS2 },
{ "ExplorerPS/2", MSE_XPS2, NULL, PROT_EXPPS2 },
{ "ThinkingMousePS/2", MSE_XPS2, NULL, PROT_THINKPS2 },
{ "MouseManPlusPS/2", MSE_XPS2, NULL, PROT_MMPS2 },
{ "GlidePointPS/2", MSE_XPS2, NULL, PROT_GLIDEPS2 },
{ "NetMousePS/2", MSE_XPS2, NULL, PROT_NETPS2 },
{ "NetScrollPS/2", MSE_XPS2, NULL, PROT_NETSCPS2 },
{ "BusMouse", MSE_BUS, NULL, PROT_BM },
{ "Auto", MSE_AUTO, NULL, PROT_AUTO },
{ "SysMouse", MSE_MISC, mlDefaults, PROT_SYSMOUSE },
{ NULL, MSE_NONE, NULL, PROT_UNKNOWN }
};
#ifdef XFree86LOADER
static const OptionInfoRec *
MouseAvailableOptions(void *unused)
{
return (mouseOptions);
}
#endif
static void
MouseCommonOptions(InputInfoPtr pInfo)
{
MouseDevPtr pMse;
MessageType from = X_DEFAULT;
char *s;
int origButtons;
pMse = pInfo->private;
pMse->buttons = xf86SetIntOption(pInfo->options, "Buttons", 0);
from = X_CONFIG;
if (!pMse->buttons) {
pMse->buttons = MSE_DFLTBUTTONS;
from = X_DEFAULT;
}
origButtons = pMse->buttons;
pMse->emulate3Buttons = xf86SetBoolOption(pInfo->options,
"Emulate3Buttons", FALSE);
if (!xf86FindOptionValue(pInfo->options,"Emulate3Buttons"))
pMse->emulate3ButtonsSoft = TRUE;
pMse->emulate3Timeout = xf86SetIntOption(pInfo->options,
"Emulate3Timeout", 50);
if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft) {
xf86Msg(X_CONFIG, "%s: Emulate3Buttons, Emulate3Timeout: %d\n",
pInfo->name, pMse->emulate3Timeout);
}
pMse->chordMiddle = xf86SetBoolOption(pInfo->options, "ChordMiddle", FALSE);
if (pMse->chordMiddle)
xf86Msg(X_CONFIG, "%s: ChordMiddle\n", pInfo->name);
pMse->flipXY = xf86SetBoolOption(pInfo->options, "FlipXY", FALSE);
if (pMse->flipXY)
xf86Msg(X_CONFIG, "%s: FlipXY\n", pInfo->name);
if (xf86SetBoolOption(pInfo->options, "InvX", FALSE)) {
pMse->invX = -1;
xf86Msg(X_CONFIG, "%s: InvX\n", pInfo->name);
} else
pMse->invX = 1;
if (xf86SetBoolOption(pInfo->options, "InvY", FALSE)) {
pMse->invY = -1;
xf86Msg(X_CONFIG, "%s: InvY\n", pInfo->name);
} else
pMse->invY = 1;
pMse->angleOffset = xf86SetIntOption(pInfo->options, "AngleOffset", 0);
if (pMse->pDragLock)
xfree(pMse->pDragLock);
pMse->pDragLock = NULL;
s = xf86SetStrOption(pInfo->options, "DragLockButtons", NULL);
if (s) {
int lock;
int target;
int lockM,targetM;
int i, j;
char *s1;
DragLockPtr pLock;
pLock = pMse->pDragLock = xcalloc(1, sizeof(DragLockRec));
s1 = s;
while ((s1 != NULL) && (lock = strtol(s1, &s1, 10)) != 0) {
if ((lock < 0) || (lock > MSE_MAXBUTTONS)) {
xf86Msg(X_WARNING, "DragLock: Invalid button number = %d\n",
lock);
break;
};
lockM = 1 << (lock - 1);
if ((s1 == NULL) || ((target=strtol(s1, &s1, 10)) == 0)) {
pLock->masterLockM = lockM;
xf86Msg(X_CONFIG,
"DragLock button %d is master drag lock",
lock);
} else {
if ((target < 0) || (target > MSE_MAXBUTTONS)) {
xf86Msg(X_WARNING,
"DragLock: Invalid button number for target=%d\n",
target);
break;
}
targetM = 1 << (target - 1);
xf86Msg(X_CONFIG,
"DragLock: button %d is drag lock for button %d\n",
lock,target);
lock--;
pLock->nib_table[lock / NIB_BITS][1 << (lock % NIB_BITS)] =
targetM;
pLock->lockButtonsM |= lockM;
}
}
for (i = 0; i < NIB_COUNT; i++) {
for (j = 0; j < NIB_SIZE; j++) {
int ff, fM, otherbits;
ff = ffs(j) - 1;
if (ff >= 0) {
fM = 1 << ff;
otherbits = j & ~fM;
if (otherbits)
pLock->nib_table[i][j] =
pLock->nib_table[i][fM] |
pLock->nib_table[i][otherbits];
}
}
}
}
s = xf86SetStrOption(pInfo->options, "ZAxisMapping", NULL);
if (s) {
int b1 = 0, b2 = 0, b3 = 0, b4 = 0;
char *msg = NULL;
if (!xf86NameCmp(s, "x")) {
pMse->negativeZ = pMse->positiveZ = MSE_MAPTOX;
pMse->negativeW = pMse->positiveW = MSE_MAPTOX;
msg = xstrdup("X axis");
} else if (!xf86NameCmp(s, "y")) {
pMse->negativeZ = pMse->positiveZ = MSE_MAPTOY;
pMse->negativeW = pMse->positiveW = MSE_MAPTOY;
msg = xstrdup("Y axis");
} else if (sscanf(s, "%d %d %d %d", &b1, &b2, &b3, &b4) >= 2 &&
b1 > 0 && b1 <= MSE_MAXBUTTONS &&
b2 > 0 && b2 <= MSE_MAXBUTTONS) {
msg = xstrdup("buttons XX and YY");
if (msg)
sprintf(msg, "buttons %d and %d", b1, b2);
pMse->negativeZ = pMse->negativeW = 1 << (b1-1);
pMse->positiveZ = pMse->positiveW = 1 << (b2-1);
if (b3 > 0 && b3 <= MSE_MAXBUTTONS &&
b4 > 0 && b4 <= MSE_MAXBUTTONS) {
if (msg)
xfree(msg);
msg = xstrdup("buttons XX, YY, ZZ and WW");
if (msg)
sprintf(msg, "buttons %d, %d, %d and %d", b1, b2, b3, b4);
pMse->negativeW = 1 << (b3-1);
pMse->positiveW = 1 << (b4-1);
}
if (b1 > pMse->buttons) pMse->buttons = b1;
if (b2 > pMse->buttons) pMse->buttons = b2;
if (b3 > pMse->buttons) pMse->buttons = b3;
if (b4 > pMse->buttons) pMse->buttons = b4;
} else {
pMse->negativeZ = pMse->positiveZ = MSE_NOZMAP;
pMse->negativeW = pMse->positiveW = MSE_NOZMAP;
}
if (msg) {
xf86Msg(X_CONFIG, "%s: ZAxisMapping: %s\n", pInfo->name, msg);
xfree(msg);
} else {
xf86Msg(X_WARNING, "%s: Invalid ZAxisMapping value: \"%s\"\n",
pInfo->name, s);
}
}
if (xf86SetBoolOption(pInfo->options, "EmulateWheel", FALSE)) {
Bool yFromConfig = FALSE;
int wheelButton;
pMse->emulateWheel = TRUE;
wheelButton = xf86SetIntOption(pInfo->options,
"EmulateWheelButton", 4);
if (wheelButton < 0 || wheelButton > MSE_MAXBUTTONS) {
xf86Msg(X_WARNING, "%s: Invalid EmulateWheelButton value: %d\n",
pInfo->name, wheelButton);
wheelButton = 4;
}
pMse->wheelButtonMask = 1 << (wheelButton - 1);
pMse->wheelInertia = xf86SetIntOption(pInfo->options,
"EmulateWheelInertia", 10);
if (pMse->wheelInertia <= 0) {
xf86Msg(X_WARNING, "%s: Invalid EmulateWheelInertia value: %d\n",
pInfo->name, pMse->wheelInertia);
pMse->wheelInertia = 50;
}
pMse->negativeX = MSE_NOAXISMAP;
pMse->positiveX = MSE_NOAXISMAP;
s = xf86SetStrOption(pInfo->options, "XAxisMapping", NULL);
if (s) {
int b1 = 0, b2 = 0;
char *msg = NULL;
if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
b1 > 0 && b1 <= MSE_MAXBUTTONS &&
b2 > 0 && b2 <= MSE_MAXBUTTONS) {
msg = xstrdup("buttons XX and YY");
if (msg)
sprintf(msg, "buttons %d and %d", b1, b2);
pMse->negativeX = b1;
pMse->positiveX = b2;
if (b1 > pMse->buttons) pMse->buttons = b1;
if (b2 > pMse->buttons) pMse->buttons = b2;
} else {
xf86Msg(X_WARNING, "%s: Invalid XAxisMapping value: \"%s\"\n",
pInfo->name, s);
}
if (msg) {
xf86Msg(X_CONFIG, "%s: XAxisMapping: %s\n", pInfo->name, msg);
xfree(msg);
}
}
s = xf86SetStrOption(pInfo->options, "YAxisMapping", NULL);
if (s) {
int b1 = 0, b2 = 0;
char *msg = NULL;
if ((sscanf(s, "%d %d", &b1, &b2) == 2) &&
b1 > 0 && b1 <= MSE_MAXBUTTONS &&
b2 > 0 && b2 <= MSE_MAXBUTTONS) {
msg = xstrdup("buttons XX and YY");
if (msg)
sprintf(msg, "buttons %d and %d", b1, b2);
pMse->negativeY = b1;
pMse->positiveY = b2;
if (b1 > pMse->buttons) pMse->buttons = b1;
if (b2 > pMse->buttons) pMse->buttons = b2;
yFromConfig = TRUE;
} else {
xf86Msg(X_WARNING, "%s: Invalid YAxisMapping value: \"%s\"\n",
pInfo->name, s);
}
if (msg) {
xf86Msg(X_CONFIG, "%s: YAxisMapping: %s\n", pInfo->name, msg);
xfree(msg);
}
}
if (!yFromConfig) {
pMse->negativeY = 4;
pMse->positiveY = 5;
if (pMse->negativeY > pMse->buttons)
pMse->buttons = pMse->negativeY;
if (pMse->positiveY > pMse->buttons)
pMse->buttons = pMse->positiveY;
xf86Msg(X_DEFAULT, "%s: YAxisMapping: buttons %d and %d\n",
pInfo->name, pMse->negativeY, pMse->positiveY);
}
xf86Msg(X_CONFIG, "%s: EmulateWheel, EmulateWheelButton: %d, "
"EmulateWheelInertia: %d\n",
pInfo->name, wheelButton, pMse->wheelInertia);
}
if (origButtons != pMse->buttons)
from = X_CONFIG;
xf86Msg(from, "%s: Buttons: %d\n", pInfo->name, pMse->buttons);
}
static int
lock2targetMap(DragLockPtr pLock, int lockMask)
{
int result,i;
result = 0;
for (i = 0; (i < NIB_COUNT) && lockMask; i++) {
result |= pLock->nib_table[i][lockMask& NIB_MASK];
lockMask &= ~NIB_MASK;
lockMask >>= NIB_BITS;
}
return result;
}
static void
MouseHWOptions(InputInfoPtr pInfo)
{
MouseDevPtr pMse = pInfo->private;
mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
if (mPriv == NULL)
return;
if ((mPriv->soft
= xf86SetBoolOption(pInfo->options, "AutoSoft", FALSE))) {
xf86Msg(X_CONFIG, "Don't initialize mouse when auto-probing\n");
}
pMse->sampleRate = xf86SetIntOption(pInfo->options, "SampleRate", 0);
if (pMse->sampleRate) {
xf86Msg(X_CONFIG, "%s: SampleRate: %d\n", pInfo->name,
pMse->sampleRate);
}
pMse->resolution = xf86SetIntOption(pInfo->options, "Resolution", 0);
if (pMse->resolution) {
xf86Msg(X_CONFIG, "%s: Resolution: %d\n", pInfo->name,
pMse->resolution);
}
}
static void
MouseSerialOptions(InputInfoPtr pInfo)
{
MouseDevPtr pMse = pInfo->private;
Bool clearDTR, clearRTS;
pMse->baudRate = xf86SetIntOption(pInfo->options, "BaudRate", 0);
if (pMse->baudRate) {
xf86Msg(X_CONFIG, "%s: BaudRate: %d\n", pInfo->name,
pMse->baudRate);
}
if ((clearDTR = xf86SetBoolOption(pInfo->options, "ClearDTR",FALSE)))
pMse->mouseFlags |= MF_CLEAR_DTR;
if ((clearRTS = xf86SetBoolOption(pInfo->options, "ClearRTS",FALSE)))
pMse->mouseFlags |= MF_CLEAR_RTS;
if (clearDTR || clearRTS) {
xf86Msg(X_CONFIG, "%s: ", pInfo->name);
if (clearDTR) {
xf86ErrorF("ClearDTR");
if (clearRTS)
xf86ErrorF(", ");
}
if (clearRTS) {
xf86ErrorF("ClearRTS");
}
xf86ErrorF("\n");
}
}
static MouseProtocolID
ProtocolNameToID(const char *name)
{
int i;
for (i = 0; mouseProtocols[i].name; i++)
if (xf86NameCmp(name, mouseProtocols[i].name) == 0)
return mouseProtocols[i].id;
return PROT_UNKNOWN;
}
static const char *
ProtocolIDToName(MouseProtocolID id)
{
int i;
switch (id) {
case PROT_UNKNOWN:
return "Unknown";
break;
case PROT_UNSUP:
return "Unsupported";
break;
default:
for (i = 0; mouseProtocols[i].name; i++)
if (id == mouseProtocols[i].id)
return mouseProtocols[i].name;
return "Invalid";
}
}
const char *
xf86MouseProtocolIDToName(MouseProtocolID id)
{
return ProtocolIDToName(id);
}
MouseProtocolID
xf86MouseProtocolNameToID(const char *name)
{
return ProtocolNameToID(name);
}
static int
ProtocolIDToClass(MouseProtocolID id)
{
int i;
switch (id) {
case PROT_UNKNOWN:
case PROT_UNSUP:
return MSE_NONE;
break;
default:
for (i = 0; mouseProtocols[i].name; i++)
if (id == mouseProtocols[i].id)
return mouseProtocols[i].class;
return MSE_NONE;
}
}
static MouseProtocolPtr
GetProtocol(MouseProtocolID id) {
int i;
switch (id) {
case PROT_UNKNOWN:
case PROT_UNSUP:
return NULL;
break;
default:
for (i = 0; mouseProtocols[i].name; i++)
if (id == mouseProtocols[i].id) {
return &mouseProtocols[i];
}
return NULL;
}
}
static OSMouseInfoPtr osInfo = NULL;
static Bool
InitProtocols(void)
{
int classes;
int i;
const char *osname = NULL;
if (osInfo)
return TRUE;
osInfo = xf86OSMouseInit(0);
if (!osInfo)
return FALSE;
if (!osInfo->SupportedInterfaces)
return FALSE;
classes = osInfo->SupportedInterfaces();
if (!classes)
return FALSE;
for (i = 0; mouseProtocols[i].name; i++)
if (!(mouseProtocols[i].class & classes))
mouseProtocols[i].id = PROT_UNSUP;
for (i = 0; mouseProtocols[i].name; i++)
if (mouseProtocols[i].class & MSE_MISC)
if (!osInfo->CheckProtocol ||
!osInfo->CheckProtocol(mouseProtocols[i].name))
mouseProtocols[i].id = PROT_UNSUP;
xf86GetOS(&osname, NULL, NULL, NULL);
if (osname && xf86NameCmp(osname, "netbsd") == 0)
for (i = 0; mouseProtocols[i].name; i++)
if (mouseProtocols[i].id == PROT_PS2)
mouseProtocols[i].id = PROT_BM;
return TRUE;
}
static InputInfoPtr
MousePreInit(InputDriverPtr drv, IDevPtr dev, int flags)
{
InputInfoPtr pInfo;
MouseDevPtr pMse;
mousePrivPtr mPriv;
MessageType protocolFrom = X_DEFAULT, deviceFrom = X_CONFIG;
const char *protocol, *osProt = NULL;
const char *device;
MouseProtocolID protocolID;
MouseProtocolPtr pProto;
Bool detected;
if (!InitProtocols())
return NULL;
if (!(pInfo = xf86AllocateInput(drv, 0)))
return NULL;
pInfo->name = dev->identifier;
pInfo->type_name = XI_MOUSE;
pInfo->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
pInfo->device_control = MouseProc;
pInfo->read_input = MouseReadInput;
pInfo->motion_history_proc = xf86GetMotionEvents;
pInfo->history_size = 0;
pInfo->control_proc = NULL;
pInfo->close_proc = NULL;
pInfo->switch_mode = NULL;
pInfo->conversion_proc = MouseConvert;
pInfo->reverse_conversion_proc = NULL;
pInfo->fd = -1;
pInfo->dev = NULL;
pInfo->private_flags = 0;
pInfo->always_core_feedback = 0;
pInfo->conf_idev = dev;
if (!xf86SetBoolOption(dev->commonOptions, "SendDragEvents", TRUE)) {
pInfo->flags &= ~XI86_SEND_DRAG_EVENTS;
}
if (!(pMse = xcalloc(sizeof(MouseDevRec), 1)))
return pInfo;
pInfo->private = pMse;
pMse->Ctrl = MouseCtrl;
pMse->PostEvent = MousePostEvent;
pMse->CommonOptions = MouseCommonOptions;
protocol = xf86SetStrOption(dev->commonOptions, "Protocol", NULL);
if (protocol) {
protocolFrom = X_CONFIG;
} else if (osInfo->DefaultProtocol) {
protocol = osInfo->DefaultProtocol();
protocolFrom = X_DEFAULT;
}
if (!protocol) {
xf86Msg(X_ERROR, "%s: No Protocol specified\n", pInfo->name);
return pInfo;
}
protocolID = ProtocolNameToID(protocol);
do {
detected = TRUE;
switch (protocolID) {
case PROT_AUTO:
if (osInfo->SetupAuto) {
if ((osProt = osInfo->SetupAuto(pInfo,NULL))) {
MouseProtocolID id = ProtocolNameToID(osProt);
if (id == PROT_UNKNOWN || id == PROT_UNSUP) {
protocolID = id;
protocol = osProt;
detected = FALSE;
}
}
}
break;
case PROT_UNKNOWN:
if (osInfo->CheckProtocol
&& osInfo->CheckProtocol(protocol)) {
if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
HAVE_FIND_DEVICE && osInfo->FindDevice) {
xf86Msg(X_WARNING, "%s: No Device specified, "
"looking for one...\n", pInfo->name);
if (!osInfo->FindDevice(pInfo, protocol, 0)) {
xf86Msg(X_ERROR, "%s: Cannot find which device "
"to use.\n", pInfo->name);
} else
deviceFrom = X_PROBED;
}
if (osInfo->PreInit) {
osInfo->PreInit(pInfo, protocol, 0);
}
return pInfo;
}
xf86Msg(X_ERROR, "%s: Unknown protocol \"%s\"\n",
pInfo->name, protocol);
return pInfo;
break;
case PROT_UNSUP:
xf86Msg(X_ERROR,
"%s: Protocol \"%s\" is not supported on this "
"platform\n", pInfo->name, protocol);
return pInfo;
break;
default:
break;
}
} while (!detected);
if (!xf86CheckStrOption(dev->commonOptions, "Device", NULL) &&
HAVE_FIND_DEVICE && osInfo->FindDevice) {
xf86Msg(X_WARNING, "%s: No Device specified, looking for one...\n",
pInfo->name);
if (!osInfo->FindDevice(pInfo, protocol, 0)) {
xf86Msg(X_ERROR, "%s: Cannot find which device to use.\n",
pInfo->name);
} else {
deviceFrom = X_PROBED;
xf86MarkOptionUsedByName(dev->commonOptions, "Device");
}
}
device = xf86CheckStrOption(dev->commonOptions, "Device", NULL);
if (device)
xf86Msg(deviceFrom, "%s: Device: \"%s\"\n", pInfo->name, device);
xf86Msg(protocolFrom, "%s: Protocol: \"%s\"\n", pInfo->name, protocol);
if (!(pProto = GetProtocol(protocolID)))
return pInfo;
pMse->protocolID = protocolID;
pMse->oldProtocolID = protocolID;
pMse->autoProbe = FALSE;
xf86CollectInputOptions(pInfo, pProto->defaults, NULL);
xf86ProcessCommonOptions(pInfo, pInfo->options);
#ifndef __OS2ELF__
pInfo->fd = xf86OpenSerial(pInfo->options);
if (pInfo->fd == -1) {
if (xf86GetAllowMouseOpenFail())
xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
else {
xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
if (pMse->mousePriv)
xfree(pMse->mousePriv);
xfree(pMse);
pInfo->private = NULL;
return pInfo;
}
}
xf86CloseSerial(pInfo->fd);
#endif
pInfo->fd = -1;
if (!(mPriv = (pointer) xcalloc(sizeof(mousePrivRec), 1)))
return pInfo;
pMse->mousePriv = mPriv;
pMse->CommonOptions(pInfo);
pMse->checkMovements = checkForErraticMovements;
pMse->autoProbeMouse = autoProbeMouse;
pMse->collectData = collectData;
pMse->dataGood = autoGood;
MouseHWOptions(pInfo);
MouseSerialOptions(pInfo);
pInfo->flags |= XI86_CONFIGURED;
return pInfo;
}
static void
MouseReadInput(InputInfoPtr pInfo)
{
MouseDevPtr pMse;
int j, buttons, dx, dy, dz, dw, baddata;
int pBufP;
int c;
unsigned char *pBuf, u;
pMse = pInfo->private;
pBufP = pMse->protoBufTail;
pBuf = pMse->protoBuf;
XisbBlockDuration(pMse->buffer, -1);
while ((c = XisbRead(pMse->buffer)) >= 0) {
u = (unsigned char)c;
#if defined (EXTMOUSEDEBUG) || defined (MOUSEDATADEBUG)
ErrorF("mouse byte: %2.2x\n",u);
#endif
#if 1
if (pMse->collectData && pMse->autoProbe)
if (pMse->collectData(pMse,u))
continue;
#endif
#ifdef SUPPORT_MOUSE_RESET
if (mouseReset(pInfo,u)) {
pBufP = 0;
continue;
}
#endif
if (pBufP >= pMse->protoPara[4]) {
pBufP = 0;
if ((u & pMse->protoPara[0]) != pMse->protoPara[1] &&
(u & pMse->protoPara[5]) == pMse->protoPara[6]) {
#ifdef EXTMOUSEDEBUG
ErrorF("mouse 4th byte %02x\n",u);
#endif
dx = dy = dz = dw = 0;
buttons = 0;
switch (pMse->protocolID) {
case PROT_IMSERIAL:
dz = (u & 0x08) ?
(u & 0x0f) - 16 : (u & 0x0f);
if ((dz >= 7) || (dz <= -7))
dz = 0;
buttons |= ((int)(u & 0x10) >> 3)
| ((int)(u & 0x20) >> 2)
| (pMse->lastButtons & 0x05);
break;
case PROT_GLIDE:
case PROT_THINKING:
buttons |= ((int)(u & 0x10) >> 1);
default:
buttons |= ((int)(u & 0x20) >> 4) |
(pMse->lastButtons & 0x05);
break;
}
goto post_event;
}
}
pBuf[pBufP++] = u;
if (pBufP != pMse->protoPara[4]) continue;
#ifdef EXTMOUSEDEBUG2
{
int i;
ErrorF("received %d bytes",pBufP);
for ( i=0; i < pBufP; i++)
ErrorF(" %02x",pBuf[i]);
ErrorF("\n");
}
#endif
baddata = 0;
for (j = 1; j < pBufP; j++ )
if ((pBuf[j] & pMse->protoPara[2]) != pMse->protoPara[3])
baddata = 1;
if ((pMse->protoPara[7] & MPF_SAFE) && !pMse->inSync)
for (j = 1; j < pBufP; j++ )
if ((pBuf[j] & pMse->protoPara[0]) == pMse->protoPara[1])
baddata = 1;
if ((pBuf[0] & pMse->protoPara[0]) != pMse->protoPara[1] || baddata) {
if (pMse->inSync) {
#ifdef EXTMOUSEDEBUG
ErrorF("mouse driver lost sync\n");
#endif
}
#ifdef EXTMOUSEDEBUG
ErrorF("skipping byte %02x\n",*pBuf);
#endif
if (pMse->autoProbeMouse && pMse->autoProbe)
pMse->autoProbeMouse(pInfo, FALSE, pMse->inSync);
pMse->protoBufTail = --pBufP;
for (j = 0; j < pBufP; j++)
pBuf[j] = pBuf[j+1];
pMse->inSync = 0;
continue;
}
if (pMse->autoProbeMouse && pMse->autoProbe)
pMse->autoProbeMouse(pInfo, TRUE, FALSE);
if (!pMse->inSync) {
#ifdef EXTMOUSEDEBUG
ErrorF("mouse driver back in sync\n");
#endif
pMse->inSync = 1;
}
if (!pMse->dataGood(pMse))
continue;
REDO_INTERPRET:
dz = dw = 0;
switch (pMse->protocolID) {
case PROT_LOGIMAN:
case PROT_MS:
if (pMse->chordMiddle)
buttons = (((int) pBuf[0] & 0x30) == 0x30) ? 2 :
((int)(pBuf[0] & 0x20) >> 3)
| ((int)(pBuf[0] & 0x10) >> 4);
else
buttons = (pMse->lastButtons & 2)
| ((int)(pBuf[0] & 0x20) >> 3)
| ((int)(pBuf[0] & 0x10) >> 4);
dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
break;
case PROT_GLIDE:
case PROT_THINKING:
case PROT_IMSERIAL:
buttons = (pMse->lastButtons & (8 + 2))
| ((int)(pBuf[0] & 0x20) >> 3)
| ((int)(pBuf[0] & 0x10) >> 4);
dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
break;
case PROT_MSC:
buttons = (~pBuf[0]) & 0x07;
dx = (char)(pBuf[1]) + (char)(pBuf[3]);
dy = - ((char)(pBuf[2]) + (char)(pBuf[4]));
break;
case PROT_MMHIT:
buttons = pBuf[0] & 0x07;
if (buttons != 0)
buttons = 1 << (buttons - 1);
dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
break;
case PROT_ACECAD:
buttons = (pBuf[0] & 0x02) | ((pBuf[0] & 0x04) >> 2) |
((pBuf[0] & 1) << 2);
dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
break;
case PROT_MM:
case PROT_LOGI:
buttons = pBuf[0] & 0x07;
dx = (pBuf[0] & 0x10) ? pBuf[1] : - pBuf[1];
dy = (pBuf[0] & 0x08) ? - pBuf[2] : pBuf[2];
break;
case PROT_BM:
buttons = (~pBuf[0]) & 0x07;
dx = (char)pBuf[1];
dy = - (char)pBuf[2];
break;
case PROT_PS2:
case PROT_GENPS2:
buttons = (pBuf[0] & 0x04) >> 1 |
(pBuf[0] & 0x02) >> 1 |
(pBuf[0] & 0x01) << 2;
dx = (pBuf[0] & 0x10) ? (int)pBuf[1]-256 : (int)pBuf[1];
dy = (pBuf[0] & 0x20) ? -((int)pBuf[2]-256) : -(int)pBuf[2];
break;
case PROT_IMPS2:
case PROT_NETPS2:
buttons = (pBuf[0] & 0x04) >> 1 |
(pBuf[0] & 0x02) >> 1 |
(pBuf[0] & 0x01) << 2 |
(pBuf[0] & 0x40) >> 3 |
(pBuf[0] & 0x80) >> 3;
dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
dz = (signed char)(pBuf[3] | ((pBuf[3] & 0x08) ? 0xf8 : 0));
if ((pBuf[3] & 0xf8) && ((pBuf[3] & 0xf8) != 0xf8)) {
if (pMse->autoProbe) {
SetMouseProto(pMse, PROT_EXPPS2);
xf86Msg(X_INFO,
"Mouse autoprobe: Changing protocol to %s\n",
pMse->protocol);
goto REDO_INTERPRET;
} else
dz = 0;
}
break;
case PROT_EXPPS2:
if (pMse->autoProbe && (pBuf[3] & 0xC0)) {
SetMouseProto(pMse, PROT_IMPS2);
xf86Msg(X_INFO,"Mouse autoprobe: Changing protocol to %s\n",
pMse->protocol);
goto REDO_INTERPRET;
}
buttons = (pBuf[0] & 0x04) >> 1 |
(pBuf[0] & 0x02) >> 1 |
(pBuf[0] & 0x01) << 2 |
(pBuf[3] & 0x10) >> 1 |
(pBuf[3] & 0x20) >> 1;
dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
dz = (pBuf[3] & 0x08) ? (pBuf[3] & 0x0f) - 16 : (pBuf[3] & 0x0f);
break;
case PROT_MMPS2:
buttons = (pBuf[0] & 0x04) >> 1 |
(pBuf[0] & 0x02) >> 1 |
(pBuf[0] & 0x01) << 2;
dx = (pBuf[0] & 0x10) ? pBuf[1] - 256 : pBuf[1];
if (((pBuf[0] & 0x48) == 0x48) &&
(abs(dx) > 191) &&
((((pBuf[2] & 0x03) << 2) | 0x02) == (pBuf[1] & 0x0f))) {
switch ((((pBuf[0] & 0x30) >> 2) | ((pBuf[1] & 0x30) >> 4))) {
case 1:
buttons |= ((pBuf[2] & 0x10) ? 0x08 : 0) |
((pBuf[2] & 0x20) ? 0x10 : 0);
dx = dy = 0;
dz = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
(pBuf[2] & 0x0f);
break;
case 2:
buttons |= (pMse->lastButtons & ~0x07);
dx = dy = 0;
dz = (pBuf[2] & 0x80) ? ((pBuf[2] >> 4) & 0x0f) - 16 :
((pBuf[2] >> 4) & 0x0f);
dw = (pBuf[2] & 0x08) ? (pBuf[2] & 0x0f) - 16 :
(pBuf[2] & 0x0f);
break;
case 0:
default:
buttons |= (pMse->lastButtons & ~0x07);
dx = dy = 0;
dz = 0;
break;
}
} else {
buttons |= (pMse->lastButtons & ~0x07);
dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
}
break;
case PROT_GLIDEPS2:
buttons = (pBuf[0] & 0x04) >> 1 |
(pBuf[0] & 0x02) >> 1 |
(pBuf[0] & 0x01) << 2 |
((pBuf[0] & 0x08) ? 0 : 0x08);
dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
break;
case PROT_NETSCPS2:
buttons = (pBuf[0] & 0x04) >> 1 |
(pBuf[0] & 0x02) >> 1 |
(pBuf[0] & 0x01) << 2 |
((pBuf[3] & 0x02) ? 0x08 : 0) |
((pBuf[3] & 0x01) ? 0x10 : 0);
dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
dz = (pBuf[3] & 0x10) ? pBuf[4] - 256 : pBuf[4];
break;
case PROT_THINKPS2:
buttons = (pBuf[0] & 0x04) >> 1 |
(pBuf[0] & 0x02) >> 1 |
(pBuf[0] & 0x01) << 2 |
((pBuf[0] & 0x08) ? 0x08 : 0);
pBuf[1] |= (pBuf[0] & 0x40) ? 0x80 : 0x00;
dx = (pBuf[0] & 0x10) ? pBuf[1]-256 : pBuf[1];
dy = (pBuf[0] & 0x20) ? -(pBuf[2]-256) : -pBuf[2];
break;
case PROT_SYSMOUSE:
buttons = (~pBuf[0]) & 0x07;
dx = (char)(pBuf[1]) + (char)(pBuf[3]);
dy = - ((char)(pBuf[2]) + (char)(pBuf[4]));
if (pMse->protoPara[4] >= 8) {
dz = ((signed char)(pBuf[5] << 1) +
(signed char)(pBuf[6] << 1)) >> 1;
buttons |= (int)(~pBuf[7] & 0x7f) << 3;
}
break;
case PROT_VALUMOUSESCROLL:
buttons = ((int)(pBuf[0] & 0x20) >> 3)
| ((int)(pBuf[0] & 0x10) >> 4)
| ((int)(pBuf[3] & 0x10) >> 3);
dx = (char)(((pBuf[0] & 0x03) << 6) | (pBuf[1] & 0x3F));
dy = (char)(((pBuf[0] & 0x0C) << 4) | (pBuf[2] & 0x3F));
dz = (pBuf[3] & 0x08) ? ((int)(pBuf[3] & 0x0F) - 0x10) :
((int)(pBuf[3] & 0x0F));
break;
default:
#ifdef EXTMOUSEDEBUG
ErrorF("mouse table error\n");
#endif
continue;
}
#ifdef EXTMOUSEDEBUG
ErrorF("packet");
for ( j=0; j < pBufP; j++)
ErrorF(" %02x",pBuf[j]);
ErrorF("\n");
#endif
post_event:
#ifdef EXTMOUSEDEBUG
ErrorF("dx=%i dy=%i dz=%i dw=%i buttons=%x\n",dx,dy,dz,dw,buttons);
#endif
if (pMse->checkMovements && pMse->autoProbe)
pMse->checkMovements(pInfo,dx,dy);
pMse->PostEvent(pInfo, buttons, dx, dy, dz, dw);
}
pMse->protoBufTail = pBufP;
}
static void
MouseCtrl(DeviceIntPtr device, PtrCtrl *ctrl)
{
InputInfoPtr pInfo;
MouseDevPtr pMse;
pInfo = device->public.devicePrivate;
pMse = pInfo->private;
#ifdef EXTMOUSEDEBUG
ErrorF("MouseCtrl pMse=%p\n", pMse);
#endif
pMse->num = ctrl->num;
pMse->den = ctrl->den;
pMse->threshold = ctrl->threshold;
}
static int
MouseProc(DeviceIntPtr device, int what)
{
InputInfoPtr pInfo;
MouseDevPtr pMse;
mousePrivPtr mPriv;
unsigned char map[MSE_MAXBUTTONS + 1];
int i;
pInfo = device->public.devicePrivate;
pMse = pInfo->private;
pMse->device = device;
switch (what)
{
case DEVICE_INIT:
device->public.on = FALSE;
for (i = 0; i < MSE_MAXBUTTONS; i++)
map[i + 1] = i + 1;
InitPointerDeviceStruct((DevicePtr)device, map,
min(pMse->buttons, MSE_MAXBUTTONS),
miPointerGetMotionEvents, pMse->Ctrl,
miPointerGetMotionBufferSize());
xf86InitValuatorAxisStruct(device, 0, 0, -1, 1, 0, 1);
xf86InitValuatorDefaults(device, 0);
xf86InitValuatorAxisStruct(device, 1, 0, -1, 1, 0, 1);
xf86InitValuatorDefaults(device, 1);
xf86MotionHistoryAllocate(pInfo);
#ifdef EXTMOUSEDEBUG
ErrorF("assigning %p atom=%d name=%s\n", device, pInfo->atom,
pInfo->name);
#endif
break;
case DEVICE_ON:
pInfo->fd = xf86OpenSerial(pInfo->options);
if (pInfo->fd == -1)
xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
else {
pMse->buffer = XisbNew(pInfo->fd, 64);
if (!pMse->buffer) {
xf86CloseSerial(pInfo->fd);
pInfo->fd = -1;
} else {
if (!SetupMouse(pInfo)) {
xf86CloseSerial(pInfo->fd);
pInfo->fd = -1;
XisbFree(pMse->buffer);
pMse->buffer = NULL;
} else {
mPriv = (mousePrivPtr)pMse->mousePriv;
if (mPriv != NULL) {
if ( pMse->protocolID != PROT_AUTO) {
pMse->inSync = TRUE;
if (mPriv->soft)
mPriv->autoState = AUTOPROBE_GOOD;
else
mPriv->autoState = AUTOPROBE_H_GOOD;
} else {
if (mPriv->soft)
mPriv->autoState = AUTOPROBE_NOPROTO;
else
mPriv->autoState = AUTOPROBE_H_NOPROTO;
}
}
xf86FlushInput(pInfo->fd);
xf86AddEnabledDevice(pInfo);
}
}
}
pMse->lastButtons = 0;
pMse->emulateState = 0;
pMse->emulate3Pending = FALSE;
device->public.on = TRUE;
FlushButtons(pMse);
if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
{
RegisterBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
(pointer) pInfo);
}
break;
case DEVICE_OFF:
case DEVICE_CLOSE:
if (pInfo->fd != -1) {
xf86RemoveEnabledDevice(pInfo);
if (pMse->buffer) {
XisbFree(pMse->buffer);
pMse->buffer = NULL;
}
xf86CloseSerial(pInfo->fd);
pInfo->fd = -1;
if (pMse->emulate3Buttons || pMse->emulate3ButtonsSoft)
{
RemoveBlockAndWakeupHandlers (MouseBlockHandler, MouseWakeupHandler,
(pointer) pInfo);
}
}
device->public.on = FALSE;
usleep(300000);
break;
}
return Success;
}
static Bool
MouseConvert(InputInfoPtr pInfo, int first, int num, int v0, int v1, int v2,
int v3, int v4, int v5, int *x, int *y)
{
if (first != 0 || num != 2)
return FALSE;
*x = v0;
*y = v1;
return TRUE;
}
static void
FlushButtons(MouseDevPtr pMse)
{
int i, blocked;
pMse->lastButtons = 0;
blocked = xf86BlockSIGIO ();
for (i = 1; i <= 5; i++)
xf86PostButtonEvent(pMse->device,0,i,0,0,0);
xf86UnblockSIGIO (blocked);
}
static signed char stateTab[11][5][3] = {
{
{ 0, 0, 0 },
{ 0, 0, 1 },
{ 0, 0, 2 },
{ 2, 0, 3 },
{ 0, 0, -1 }
},
{
{ 1, -1, 0 },
{ 0, 0, 1 },
{ 1, -1, 2 },
{ 2, 0, 3 },
{ 1, 0, 4 },
},
{
{ 3, -3, 0 },
{ 3, -3, 1 },
{ 0, 0, 2 },
{ 2, 0, 3 },
{ 3, 0, 5 },
},
{
{ -2, 0, 0 },
{ 0, 0, 7 },
{ 0, 0, 6 },
{ 0, 0, 3 },
{ 0, 0, -1 },
},
{
{ -1, 0, 0 },
{ 0, 0, 4 },
{ -1, 0, 2 },
{ 3, 0, 10 },
{ 0, 0, -1 },
},
{
{ -3, 0, 0 },
{ -3, 0, 1 },
{ 0, 0, 5 },
{ 1, 0, 10 },
{ 0, 0, -1 },
},
{
{ -2, 0, 0 },
{ -2, 0, 1 },
{ 0, 0, 6 },
{ 1, 0, 8 },
{ 0, 0, -1 },
},
{
{ -2, 0, 0 },
{ 0, 0, 7 },
{ -2, 0, 2 },
{ 3, 0, 9 },
{ 0, 0, -1 },
},
{
{ -2, -1, 0 },
{ -2, 0, 4 },
{ -1, 0, 6 },
{ 0, 0, 8 },
{ 0, 0, -1 },
},
{
{ -2, -3, 0 },
{ -3, 0, 7 },
{ -2, 0, 5 },
{ 0, 0, 9 },
{ 0, 0, -1 },
},
{
{ -1, -3, 0 },
{ -3, 0, 4 },
{ -1, 0, 5 },
{ 0, 0, 10 },
{ 0, 0, -1 },
},
};
static char reverseMap[32] = { 0, 4, 2, 6, 1, 5, 3, 7,
8, 12, 10, 14, 9, 13, 11, 15,
16, 20, 18, 22, 17, 21, 19, 23,
24, 28, 26, 30, 25, 29, 27, 31};
static char hitachMap[16] = { 0, 2, 1, 3,
8, 10, 9, 11,
4, 6, 5, 7,
12, 14, 13, 15 };
#define reverseBits(map, b) (((b) & ~0x0f) | map[(b) & 0x0f])
static CARD32
buttonTimer(InputInfoPtr pInfo)
{
MouseDevPtr pMse;
int sigstate;
int id;
pMse = pInfo->private;
sigstate = xf86BlockSIGIO ();
pMse->emulate3Pending = FALSE;
if ((id = stateTab[pMse->emulateState][4][0]) != 0) {
xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
pMse->emulateState = stateTab[pMse->emulateState][4][2];
} else {
ErrorF("Got unexpected buttonTimer in state %d\n", pMse->emulateState);
}
xf86UnblockSIGIO (sigstate);
return 0;
}
static Bool
Emulate3ButtonsSoft(InputInfoPtr pInfo)
{
MouseDevPtr pMse = pInfo->private;
if (!pMse->emulate3ButtonsSoft)
return TRUE;
pMse->emulate3Buttons = FALSE;
if (pMse->emulate3Pending)
buttonTimer(pInfo);
xf86Msg(X_INFO,"3rd Button detected: disabling emulate3Button\n");
return FALSE;
}
static void MouseBlockHandler(pointer data,
struct timeval **waitTime,
pointer LastSelectMask)
{
InputInfoPtr pInfo = (InputInfoPtr) data;
MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
int ms;
if (pMse->emulate3Pending)
{
ms = pMse->emulate3Expires - GetTimeInMillis ();
if (ms <= 0)
ms = 0;
AdjustWaitForDelay (waitTime, ms);
}
}
static void MouseWakeupHandler(pointer data,
int i,
pointer LastSelectMask)
{
InputInfoPtr pInfo = (InputInfoPtr) data;
MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
int ms;
if (pMse->emulate3Pending)
{
ms = pMse->emulate3Expires - GetTimeInMillis ();
if (ms <= 0)
buttonTimer (pInfo);
}
}
static void
MouseDoPostEvent(InputInfoPtr pInfo, int buttons, int dx, int dy)
{
MouseDevPtr pMse;
int truebuttons, emulateButtons;
int id, change;
int emuWheelDelta, emuWheelButton, emuWheelButtonMask;
pMse = pInfo->private;
truebuttons = buttons;
if (pMse->protocolID == PROT_MMHIT)
buttons = reverseBits(hitachMap, buttons);
else
buttons = reverseBits(reverseMap, buttons);
if (pMse->emulateWheel && (buttons & pMse->wheelButtonMask)) {
if (pMse->negativeY != MSE_NOAXISMAP) {
pMse->wheelYDistance += dy;
if (pMse->wheelYDistance < 0) {
emuWheelDelta = -pMse->wheelInertia;
emuWheelButton = pMse->negativeY;
} else {
emuWheelDelta = pMse->wheelInertia;
emuWheelButton = pMse->positiveY;
}
emuWheelButtonMask = 1 << (emuWheelButton - 1);
while (abs(pMse->wheelYDistance) > pMse->wheelInertia) {
pMse->wheelYDistance -= emuWheelDelta;
if (!(emuWheelButtonMask & buttons) ||
(emuWheelButtonMask & pMse->wheelButtonMask)) {
xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
}
}
}
if (pMse->negativeX != MSE_NOAXISMAP) {
pMse->wheelXDistance += dx;
if (pMse->wheelXDistance < 0) {
emuWheelDelta = -pMse->wheelInertia;
emuWheelButton = pMse->negativeX;
} else {
emuWheelDelta = pMse->wheelInertia;
emuWheelButton = pMse->positiveX;
}
emuWheelButtonMask = 1 << (emuWheelButton - 1);
while (abs(pMse->wheelXDistance) > pMse->wheelInertia) {
pMse->wheelXDistance -= emuWheelDelta;
if (!(emuWheelButtonMask & buttons) ||
(emuWheelButtonMask & pMse->wheelButtonMask)) {
xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 1, 0, 0);
xf86PostButtonEvent(pInfo->dev, 0, emuWheelButton, 0, 0, 0);
}
}
}
dx = 0;
dy = 0;
buttons &= ~pMse->wheelButtonMask;
}
if (dx || dy)
xf86PostMotionEvent(pInfo->dev, 0, 0, 2, dx, dy);
if (truebuttons != pMse->lastButtons) {
if (pMse->protocolID == PROT_MMHIT)
change = buttons ^ reverseBits(hitachMap, pMse->lastButtons);
else
change = buttons ^ reverseBits(reverseMap, pMse->lastButtons);
if (pMse->pDragLock) {
DragLockPtr pLock;
int tarOfGoingDown, tarOfDown;
int realbuttons;
pLock = pMse->pDragLock;
realbuttons = buttons;
buttons &= ~pLock->lockButtonsM;
tarOfGoingDown = lock2targetMap(pLock,
realbuttons & change & pLock->lockButtonsM);
pLock->simulatedDown ^= tarOfGoingDown;
tarOfDown = lock2targetMap(pLock,
realbuttons & pLock->lockButtonsM);
pLock->simulatedDown &= ~(realbuttons & change);
pLock->masterTS |= (~realbuttons & change) & pLock->masterLockM;
if (pLock->masterTS)
pLock->simulatedDown |= (realbuttons & change);
if (realbuttons & change)
pLock->masterTS = 0;
buttons |= (pLock->simulatedDown | tarOfDown);
buttons &= ~(pLock->masterLockM);
change = buttons ^ pLock->lockLastButtons;
pLock->lockLastButtons = buttons;
}
if (pMse->emulate3Buttons
&& (!(buttons & 0x02) || Emulate3ButtonsSoft(pInfo))) {
change &= ~05;
emulateButtons = (buttons & 01) | ((buttons &04) >> 1);
if ((id = stateTab[pMse->emulateState][emulateButtons][0]) != 0)
xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
if ((id = stateTab[pMse->emulateState][emulateButtons][1]) != 0)
xf86PostButtonEvent(pInfo->dev, 0, abs(id), (id >= 0), 0, 0);
pMse->emulateState =
stateTab[pMse->emulateState][emulateButtons][2];
if (stateTab[pMse->emulateState][4][0] != 0) {
pMse->emulate3Expires = GetTimeInMillis () + pMse->emulate3Timeout;
pMse->emulate3Pending = TRUE;
} else {
pMse->emulate3Pending = FALSE;
}
}
while (change) {
id = ffs(change);
change &= ~(1 << (id - 1));
xf86PostButtonEvent(pInfo->dev, 0, id,
(buttons & (1 << (id - 1))), 0, 0);
}
pMse->lastButtons = truebuttons;
}
}
static void
MousePostEvent(InputInfoPtr pInfo, int buttons, int dx, int dy, int dz, int dw)
{
MouseDevPtr pMse;
int zbutton = 0;
pMse = pInfo->private;
switch (pMse->negativeZ) {
case MSE_NOZMAP:
break;
case MSE_MAPTOX:
if (dz != 0) {
dx = dz;
dz = 0;
}
break;
case MSE_MAPTOY:
if (dz != 0) {
dy = dz;
dz = 0;
}
break;
default:
buttons &= ~(pMse->negativeZ | pMse->positiveZ
| pMse->negativeW | pMse->positiveW);
if (dw < 0 || dz < -1)
zbutton = pMse->negativeW;
else if (dz < 0)
zbutton = pMse->negativeZ;
else if (dw > 0 || dz > 1)
zbutton = pMse->positiveW;
else if (dz > 0)
zbutton = pMse->positiveZ;
buttons |= zbutton;
dz = 0;
break;
}
if (pMse->angleOffset != 0) {
double rad = 3.141592653 * pMse->angleOffset / 180.0;
int ndx = dx;
dx = (int)((dx * cos(rad)) + (dy * sin(rad)) + 0.5);
dy = (int)((dy * cos(rad)) - (ndx * sin(rad)) + 0.5);
}
dx = pMse->invX * dx;
dy = pMse->invY * dy;
if (pMse->flipXY) {
int tmp = dx;
dx = dy;
dy = tmp;
}
MouseDoPostEvent(pInfo, buttons, dx, dy);
if (zbutton) {
buttons &= ~zbutton;
MouseDoPostEvent(pInfo, buttons, 0, 0);
}
}
static unsigned char proto[PROT_NUMPROTOS][8] = {
{ 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00, MPF_NONE },
{ 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_SAFE },
{ 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0x40, 0x40, 0x40, 0x00, 3, ~0x23, 0x00, MPF_NONE },
{ 0xe0, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00, MPF_NONE },
{ 0x40, 0x40, 0x40, 0x00, 3, ~0x3f, 0x00, MPF_NONE },
{ 0x40, 0x40, 0x40, 0x00, 3, ~0x33, 0x00, MPF_NONE },
{ 0x80, 0x80, 0x80, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0x40, 0x40, 0x40, 0x00, 4, 0x00, 0xff, MPF_NONE },
{ 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0xc8, 0x08, 0x00, 0x00, 3, 0x00, 0x00, MPF_NONE },
{ 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE },
{ 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE },
{ 0x80, 0x80, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0x08, 0x08, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0xc0, 0x00, 0x00, 0x00, 3, 0x00, 0xff, MPF_NONE },
{ 0x08, 0x08, 0x00, 0x00, 4, 0x00, 0xff, MPF_NONE },
{ 0xc0, 0x00, 0x00, 0x00, 6, 0x00, 0xff, MPF_NONE },
{ 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_NONE },
{ 0xf8, 0x80, 0x00, 0x00, 5, 0x00, 0xff, MPF_NONE },
{ 0xf8, 0x80, 0x00, 0x00, 8, 0x00, 0xff, MPF_NONE },
};
static Bool
SetupMouse(InputInfoPtr pInfo)
{
MouseDevPtr pMse;
int i;
int protoPara[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
const char *name = NULL;
Bool automatic = FALSE;
pMse = pInfo->private;
if (pMse->protocolID == PROT_AUTO) {
automatic = TRUE;
pMse->autoProbe = TRUE;
name = autoOSProtocol(pInfo,protoPara);
if (name) {
#ifdef EXTMOUSEDEBUG
ErrorF("PnP/OS Mouse detected: %s\n",name);
#endif
}
}
SetMouseProto(pMse, pMse->protocolID);
if (automatic) {
if (name) {
for (i = 0; i < sizeof(pMse->protoPara); i++)
if (protoPara[i] != -1)
pMse->protoPara[i] = protoPara[i];
} else {
#if 1
#else
xf86Msg(X_ERROR, "%s: cannot determine the mouse protocol\n",
pInfo->name);
return FALSE;
#endif
}
}
if (pMse->oldProtocolID != pMse->protocolID) {
pointer tmp = NULL;
if ((pMse->protocolID >= 0)
&& (pMse->protocolID < PROT_NUMPROTOS)
&& mouseProtocols[pMse->protocolID].defaults)
tmp = xf86OptionListCreate(
mouseProtocols[pMse->protocolID].defaults, -1, 0);
pInfo->options = xf86OptionListMerge(pInfo->options, tmp);
if (pMse->baudRate)
xf86ReplaceIntOption(pInfo->options, "BaudRate", pMse->baudRate);
pMse->oldProtocolID = pMse->protocolID;
}
if (!automatic)
xf86SetSerial(pInfo->fd, pInfo->options);
if (!initMouseHW(pInfo))
return FALSE;
pMse->protoBufTail = 0;
pMse->inSync = 0;
return TRUE;
}
#define do_ps2Reset(x) { \
int i = RETRY_COUNT;\
while (i-- > 0) { \
xf86FlushInput(x->fd); \
if (ps2Reset(x)) break; \
} \
}
static Bool
initMouseHW(InputInfoPtr pInfo)
{
MouseDevPtr pMse = pInfo->private;
const char *s;
unsigned char c;
int speed;
pointer options;
unsigned char *param = NULL;
int paramlen = 0;
int count = RETRY_COUNT;
Bool ps2Init = TRUE;
switch (pMse->protocolID) {
case PROT_LOGI:
speed = pMse->baudRate;
switch (speed) {
case 9600:
s = "*q";
break;
case 4800:
s = "*p";
break;
case 2400:
s = "*o";
break;
case 1200:
s = "*n";
break;
default:
speed = 1200;
s = "*n";
}
xf86SetSerialSpeed(pInfo->fd, 9600);
xf86WriteSerial(pInfo->fd, s, 2);
usleep(100000);
xf86SetSerialSpeed(pInfo->fd, 4800);
xf86WriteSerial(pInfo->fd, s, 2);
usleep(100000);
xf86SetSerialSpeed(pInfo->fd, 2400);
xf86WriteSerial(pInfo->fd, s, 2);
usleep(100000);
xf86SetSerialSpeed(pInfo->fd, 1200);
xf86WriteSerial(pInfo->fd, s, 2);
usleep(100000);
xf86SetSerialSpeed(pInfo->fd, speed);
xf86WriteSerial(pInfo->fd, "S", 1);
usleep(100000);
options = pInfo->options;
xf86CollectInputOptions(pInfo, mmDefaults, NULL);
xf86SetSerial(pInfo->fd, pInfo->options);
pInfo->options = options;
if (pMse->sampleRate <= 0) c = 'O';
else if (pMse->sampleRate <= 15) c = 'J';
else if (pMse->sampleRate <= 27) c = 'K';
else if (pMse->sampleRate <= 42) c = 'L';
else if (pMse->sampleRate <= 60) c = 'R';
else if (pMse->sampleRate <= 85) c = 'M';
else if (pMse->sampleRate <= 125) c = 'Q';
else c = 'N';
xf86WriteSerial(pInfo->fd, &c, 1);
break;
case PROT_LOGIMAN:
speed = pMse->baudRate;
switch (speed) {
case 9600:
s = "*q";
break;
case 1200:
s = "*n";
break;
default:
speed = 1200;
s = "*n";
}
xf86SetSerialSpeed(pInfo->fd, 1200);
xf86WriteSerial(pInfo->fd, "*n", 2);
xf86WriteSerial(pInfo->fd, "*X", 2);
xf86WriteSerial(pInfo->fd, s, 2);
usleep(100000);
xf86SetSerialSpeed(pInfo->fd, speed);
break;
case PROT_MMHIT:
xf86WriteSerial(pInfo->fd, "z8", 2);
usleep(50000);
xf86WriteSerial(pInfo->fd, "zb", 2);
usleep(50000);
xf86WriteSerial(pInfo->fd, "@", 1);
usleep(50000);
xf86WriteSerial(pInfo->fd, "R", 1);
usleep(50000);
xf86WriteSerial(pInfo->fd, "I\x20", 2);
usleep(50000);
xf86WriteSerial(pInfo->fd, "E", 1);
usleep(50000);
if (pMse->sampleRate <= 40) c = 'g';
else if (pMse->sampleRate <= 100) c = 'd';
else if (pMse->sampleRate <= 200) c = 'e';
else if (pMse->sampleRate <= 500) c = 'h';
else if (pMse->sampleRate <= 1000) c = 'j';
else c = 'd';
xf86WriteSerial(pInfo->fd, &c, 1);
usleep(50000);
xf86WriteSerial(pInfo->fd, "\021", 1);
break;
case PROT_THINKING:
usleep(200000);
xf86FlushInput(pInfo->fd);
for (s = "E5E5"; *s; ++s) {
xf86WriteSerial(pInfo->fd, s, 1);
if ((xf86WaitForInput(pInfo->fd, 1000000) <= 0))
break;
xf86ReadSerial(pInfo->fd, &c, 1);
if (c != *s)
break;
}
break;
case PROT_MSC:
usleep(100000);
xf86FlushInput(pInfo->fd);
break;
case PROT_ACECAD:
xf86WriteSerial(pInfo->fd, "", 1);
usleep(50000);
xf86WriteSerial(pInfo->fd, "@EeI!", 5);
break;
case PROT_BM:
if (osInfo->SetBMRes)
osInfo->SetBMRes(pInfo, pMse->protocol, pMse->sampleRate,
pMse->resolution);
break;
case PROT_GENPS2:
ps2Init = FALSE;
break;
case PROT_PS2:
case PROT_GLIDEPS2:
break;
case PROT_IMPS2:
{
static unsigned char seq[] = { 243, 200, 243, 100, 243, 80 };
param = seq;
paramlen = sizeof(seq);
}
break;
case PROT_EXPPS2:
{
static unsigned char seq[] = { 243, 200, 243, 100, 243, 80,
243, 200, 243, 200, 243, 80 };
param = seq;
paramlen = sizeof(seq);
}
break;
case PROT_NETPS2:
case PROT_NETSCPS2:
{
static unsigned char seq[] = { 232, 3, 230, 230, 230 };
param = seq;
paramlen = sizeof(seq);
}
break;
case PROT_MMPS2:
{
static unsigned char seq[] = { 230, 232, 0, 232, 3, 232, 2, 232, 1,
230, 232, 3, 232, 1, 232, 2, 232, 3 };
param = seq;
paramlen = sizeof(seq);
}
break;
case PROT_THINKPS2:
{
static unsigned char seq[] = { 243, 10, 232, 0, 243, 20, 243, 60,
243, 40, 243, 20, 243, 20, 243, 60,
243, 40, 243, 20, 243, 20 };
param = seq;
paramlen = sizeof(seq);
}
break;
case PROT_SYSMOUSE:
if (osInfo->SetMiscRes)
osInfo->SetMiscRes(pInfo, pMse->protocol, pMse->sampleRate,
pMse->resolution);
break;
default:
break;
}
if (pMse->class & (MSE_PS2 | MSE_XPS2)) {
if (ps2Init) {
REDO:
do_ps2Reset(pInfo);
if (paramlen > 0) {
if (!ps2SendPacket(pInfo,param,paramlen)) {
usleep(30000);
xf86FlushInput(pInfo->fd);
if (!count--)
return TRUE;
goto REDO;
}
ps2GetDeviceID(pInfo);
usleep(30000);
xf86FlushInput(pInfo->fd);
}
if (osInfo->SetPS2Res) {
osInfo->SetPS2Res(pInfo, pMse->protocol, pMse->sampleRate,
pMse->resolution);
} else {
unsigned char c2[2];
c = 0xE6;
if (!ps2SendPacket(pInfo,&c,1)) {
if (!count--)
return TRUE;
goto REDO;
}
c2[0] = 0xF3;
if (pMse->sampleRate > 0) {
if (pMse->sampleRate >= 200)
c2[1] = 200;
else if (pMse->sampleRate >= 100)
c2[1] = 100;
else if (pMse->sampleRate >= 80)
c2[1] = 80;
else if (pMse->sampleRate >= 60)
c2[1] = 60;
else if (pMse->sampleRate >= 40)
c2[1] = 40;
else
c2[1] = 20;
} else {
c2[1] = 100;
}
if (!ps2SendPacket(pInfo,c2,2)) {
if (!count--)
return TRUE;
goto REDO;
}
c2[0] = 0xE8;
if (pMse->resolution > 0) {
if (pMse->resolution >= 200)
c2[1] = 3;
else if (pMse->resolution >= 100)
c2[1] = 2;
else if (pMse->resolution >= 50)
c2[1] = 1;
else
c2[1] = 0;
} else {
c2[1] = 3;
}
if (!ps2SendPacket(pInfo,c2,2)) {
if (!count--)
return TRUE;
goto REDO;
}
usleep(30000);
xf86FlushInput(pInfo->fd);
if (!ps2EnableDataReporting(pInfo)) {
xf86Msg(X_INFO, "%s: ps2EnableDataReporting: failed\n",
pInfo->name);
xf86FlushInput(pInfo->fd);
if (!count--)
return TRUE;
goto REDO;
} else {
xf86Msg(X_INFO, "%s: ps2EnableDataReporting: succeeded\n",
pInfo->name);
}
}
}
} else {
if (paramlen > 0) {
if (xf86WriteSerial(pInfo->fd, param, paramlen) != paramlen)
xf86Msg(X_ERROR, "%s: Mouse initialization failed\n",
pInfo->name);
usleep(30000);
xf86FlushInput(pInfo->fd);
}
}
return TRUE;
}
#ifdef SUPPORT_MOUSE_RESET
static Bool
mouseReset(InputInfoPtr pInfo, unsigned char val)
{
MouseDevPtr pMse = pInfo->private;
mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
CARD32 prevEvent = mousepriv->lastEvent;
Bool expectReset = FALSE;
Bool ret = FALSE;
mousepriv->lastEvent = GetTimeInMillis();
#ifdef EXTMOUSEDEBUG
ErrorF("byte: 0x%x time: %li\n",val,mousepriv->lastEvent);
#endif
if (mousepriv->current == 0
&& (mousepriv->lastEvent - prevEvent) < 4000)
return FALSE;
if (mousepriv->current > 0
&& (mousepriv->lastEvent - prevEvent) >= 1000) {
mousepriv->inReset = FALSE;
mousepriv->current = 0;
return FALSE;
}
if (mousepriv->inReset)
mousepriv->inReset = FALSE;
#ifdef EXTMOUSEDEBUG
ErrorF("Mouse Current: %i 0x%x\n",mousepriv->current, val);
#endif
{
unsigned char seq[] = { 0xaa, 0x00 };
int len = sizeof(seq);
if (seq[mousepriv->current] == val)
expectReset = TRUE;
if (len == mousepriv->current + 1) {
mousepriv->inReset = TRUE;
mousepriv->expires = GetTimeInMillis() + 1000;
#ifdef EXTMOUSEDEBUG
ErrorF("Found PS/2 Reset string\n");
#endif
RegisterBlockAndWakeupHandlers (ps2BlockHandler,
ps2WakeupHandler, (pointer) pInfo);
ret = TRUE;
}
}
if (!expectReset)
mousepriv->current = 0;
else
mousepriv->current++;
return ret;
}
static void
ps2BlockHandler(pointer data, struct timeval **waitTime,
pointer LastSelectMask)
{
InputInfoPtr pInfo = (InputInfoPtr) data;
MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
int ms;
if (mousepriv->inReset) {
ms = mousepriv->expires - GetTimeInMillis ();
if (ms <= 0)
ms = 0;
AdjustWaitForDelay (waitTime, ms);
} else
RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
(pointer) pInfo);
}
static void
ps2WakeupHandler(pointer data, int i, pointer LastSelectMask)
{
InputInfoPtr pInfo = (InputInfoPtr) data;
MouseDevPtr pMse = (MouseDevPtr) pInfo->private;
mousePrivPtr mousepriv = (mousePrivPtr)pMse->mousePriv;
int ms;
if (mousepriv->inReset) {
unsigned char val;
int blocked;
ms = mousepriv->expires - GetTimeInMillis();
if (ms > 0)
return;
blocked = xf86BlockSIGIO ();
xf86MsgVerb(X_INFO,3,
"Got reinsert event: reinitializing PS/2 mouse\n");
val = 0xf4;
if (xf86WriteSerial(pInfo->fd, &val, 1) != 1)
xf86Msg(X_ERROR, "%s: Write to mouse failed\n",
pInfo->name);
xf86UnblockSIGIO(blocked);
}
RemoveBlockAndWakeupHandlers (ps2BlockHandler, ps2WakeupHandler,
(pointer) pInfo);
}
#endif
#ifdef EXTMOUSEDEBUG
# define AP_DBG(x) { ErrorF("Autoprobe: "); ErrorF x; }
# define AP_DBGC(x) ErrorF x ;
# else
# define AP_DBG(x)
# define AP_DBGC(x)
#endif
MouseProtocolID hardProtocolList[] = { PROT_MSC, PROT_MM, PROT_LOGI,
PROT_LOGIMAN, PROT_MMHIT,
PROT_GLIDE, PROT_IMSERIAL,
PROT_THINKING, PROT_ACECAD,
PROT_THINKPS2, PROT_MMPS2,
PROT_GLIDEPS2,
PROT_NETSCPS2, PROT_EXPPS2,PROT_IMPS2,
PROT_GENPS2, PROT_NETPS2,
PROT_MS,
PROT_UNKNOWN
};
MouseProtocolID softProtocolList[] = { PROT_MSC, PROT_MM, PROT_LOGI,
PROT_LOGIMAN, PROT_MMHIT,
PROT_GLIDE, PROT_IMSERIAL,
PROT_THINKING, PROT_ACECAD,
PROT_THINKPS2, PROT_MMPS2,
PROT_GLIDEPS2,
PROT_NETSCPS2 ,PROT_IMPS2,
PROT_GENPS2,
PROT_MS,
PROT_UNKNOWN
};
static const char *
autoOSProtocol(InputInfoPtr pInfo, int *protoPara)
{
MouseDevPtr pMse = pInfo->private;
const char *name = NULL;
MouseProtocolID protocolID = PROT_UNKNOWN;
if (osInfo->SetupAuto) {
name = osInfo->SetupAuto(pInfo, protoPara);
if (name) {
protocolID = ProtocolNameToID(name);
switch (protocolID) {
case PROT_UNKNOWN:
if (osInfo->CheckProtocol && osInfo->CheckProtocol(name)) {
name = NULL;
} else
name = NULL;
break;
case PROT_UNSUP:
name = NULL;
break;
default:
break;
}
}
}
#ifdef PNP_MOUSE
if (!name) {
protocolID = MouseGetPnpProtocol(pInfo);
if (protocolID >= 0 && protocolID < PROT_NUMPROTOS) {
name = ProtocolIDToName(protocolID);
xf86Msg(X_PROBED, "%s: PnP-detected protocol: \"%s\"\n",
pInfo->name, name);
}
}
#endif
if (!name && HAVE_GUESS_PROTOCOL && osInfo->GuessProtocol) {
name = osInfo->GuessProtocol(pInfo, 0);
if (name)
protocolID = ProtocolNameToID(name);
}
if (name) {
pMse->protocolID = protocolID;
}
return name;
}
static void
createProtoList(MouseDevPtr pMse, MouseProtocolID *protoList)
{
int i, j, k = 0;
MouseProtocolID prot;
unsigned char *para;
mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
MouseProtocolID *tmplist = NULL;
int blocked;
AP_DBGC(("Autoprobe: "));
for (i = 0; i < mPriv->count; i++)
AP_DBGC(("%2.2x ", (unsigned char) mPriv->data[i]));
AP_DBGC(("\n"));
blocked = xf86BlockSIGIO ();
if ((tmplist = xalloc(sizeof(MouseProtocolID) * NUM_AUTOPROBE_PROTOS))){
for (i = 0; protoList[i] != PROT_UNKNOWN; i++) {
tmplist[i] = protoList[i];
}
tmplist[i] = PROT_UNKNOWN;
protoList = tmplist;
} else
return;
for (i = 0; ((prot = protoList[i]) != PROT_UNKNOWN
&& (k < NUM_AUTOPROBE_PROTOS - 1)) ; i++) {
Bool bad = TRUE;
unsigned char byte = 0;
int count = 0;
int next_header_candidate = 0;
int header_count = 0;
if (!GetProtocol(prot))
continue;
para = proto[prot];
AP_DBG(("Protocol: %s ", ProtocolIDToName(prot)));
#ifdef EXTMOUSEDEBUG
for (j = 0; j < 7; j++)
AP_DBGC(("%2.2x ", (unsigned char) para[j]));
AP_DBGC(("\n"));
#endif
j = 0;
while (1) {
while (j < mPriv->count) {
if (((byte = mPriv->data[j++]) & para[0]) == para[1]){
AP_DBG(("found header %2.2x\n",byte));
next_header_candidate = j;
count = 1;
break;
} else {
if (j > para[4] && ++header_count > para[4]) {
j = mPriv->count;
break;
}
}
}
while (j < mPriv->count) {
byte = mPriv->data[j++];
if (count == para[4]) {
count = 0;
if (((byte & para[0]) != para[1])
&& ((byte & para[5]) == para[6])) {
AP_DBG(("excess byte found\n"));
continue;
}
}
if (count == 0) {
bad = FALSE;
AP_DBG(("Complete set found\n"));
if ((byte & para[0]) != para[1]) {
AP_DBG(("Autoprobe: header bad\n"));
bad = TRUE;
break;
} else {
count++;
continue;
}
}
else if (((byte & para[2]) != para[3])
|| ((para[7] & MPF_SAFE)
&& ((byte & para[0]) == para[1]))) {
AP_DBG(("data bad\n"));
bad = TRUE;
break;
} else {
count ++;
continue;
}
}
if (!bad) {
mPriv->protoList[k++] = prot;
AP_DBG(("Autoprobe: Adding protocol %s to list (entry %i)\n",
ProtocolIDToName(prot),k-1));
break;
}
j = next_header_candidate;
next_header_candidate = 0;
if (j > para[4] && ++header_count > para[4])
break;
if (!next_header_candidate)
break;
AP_DBG(("Looking for new header\n"));
}
}
xf86UnblockSIGIO(blocked);
mPriv->protoList[k] = PROT_UNKNOWN;
xfree(tmplist);
}
void **serialDefaultsList = NULL;
static void
createSerialDefaultsList(void)
{
int i = 0, j, k;
serialDefaultsList = (void **)xnfalloc(sizeof(void*));
serialDefaultsList[0] = NULL;
for (j = 0; mouseProtocols[j].name; j++) {
if (!mouseProtocols[j].defaults)
continue;
for (k = 0; k < i; k++)
if (mouseProtocols[j].defaults == serialDefaultsList[k])
continue;
i++;
serialDefaultsList = (void**)xnfrealloc(serialDefaultsList,
sizeof(void*)*(i+1));
serialDefaultsList[i-1] = mouseProtocols[j].defaults;
serialDefaultsList[i] = NULL;
}
}
typedef enum {
STATE_INVALID,
STATE_UNCERTAIN,
STATE_VALID
} validState;
#define PROBE_UNCERTAINTY 50
#define BAD_CERTAINTY 6
#define BAD_INC_CERTAINTY 1
#define BAD_INC_CERTAINTY_WHEN_SYNC_LOST 2
static validState
validCount(mousePrivPtr mPriv, Bool inSync, Bool lostSync)
{
if (inSync) {
if (!--mPriv->goodCount) {
mPriv->badCount = 0;
return STATE_VALID;
}
AP_DBG(("%i successful rounds to go\n",
mPriv->goodCount));
return STATE_UNCERTAIN;
}
mPriv->goodCount = PROBE_UNCERTAINTY;
mPriv->badCount+= lostSync ? BAD_INC_CERTAINTY_WHEN_SYNC_LOST
: BAD_INC_CERTAINTY;
if (mPriv->badCount < BAD_CERTAINTY) {
AP_DBG(("Changing protocol after: %i rounds\n",
BAD_CERTAINTY - mPriv->badCount));
return STATE_UNCERTAIN;
}
return STATE_INVALID;
}
#define RESET_VALIDATION mPriv->goodCount = PROBE_UNCERTAINTY;\
mPriv->badCount = 0;\
mPriv->prevDx = 0;\
mPriv->prevDy = 0;\
mPriv->accDx = 0;\
mPriv->accDy = 0;\
mPriv->acc = 0;
static void
autoProbeMouse(InputInfoPtr pInfo, Bool inSync, Bool lostSync)
{
MouseDevPtr pMse = pInfo->private;
mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
MouseProtocolID *protocolList = NULL;
while (1) {
switch (mPriv->autoState) {
case AUTOPROBE_GOOD:
if (inSync)
return;
AP_DBG(("State GOOD\n"));
RESET_VALIDATION;
mPriv->autoState = AUTOPROBE_VALIDATE1;
return;
case AUTOPROBE_H_GOOD:
if (inSync)
return;
AP_DBG(("State H_GOOD\n"));
RESET_VALIDATION;
mPriv->autoState = AUTOPROBE_H_VALIDATE2;
return;
case AUTOPROBE_H_NOPROTO:
AP_DBG(("State H_NOPROTO\n"));
mPriv->protocolID = 0;
mPriv->autoState = AUTOPROBE_H_SETPROTO;
break;
case AUTOPROBE_H_SETPROTO:
AP_DBG(("State H_SETPROTO\n"));
if ((pMse->protocolID = hardProtocolList[mPriv->protocolID++])
== PROT_UNKNOWN) {
mPriv->protocolID = 0;
break;
} else if (GetProtocol(pMse->protocolID) && SetupMouse(pInfo)) {
FlushButtons(pMse);
RESET_VALIDATION;
AP_DBG(("Autoprobe: Trying Protocol: %s\n",
ProtocolIDToName(pMse->protocolID)));
mPriv->autoState = AUTOPROBE_H_VALIDATE1;
return;
}
break;
case AUTOPROBE_H_VALIDATE1:
AP_DBG(("State H_VALIDATE1\n"));
switch (validCount(mPriv,inSync,lostSync)) {
case STATE_INVALID:
mPriv->autoState = AUTOPROBE_H_SETPROTO;
break;
case STATE_VALID:
xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
ProtocolIDToName(pMse->protocolID));
mPriv->autoState = AUTOPROBE_H_GOOD;
return;
case STATE_UNCERTAIN:
return;
default:
break;
}
break;
case AUTOPROBE_H_VALIDATE2:
AP_DBG(("State H_VALIDATE2\n"));
switch (validCount(mPriv,inSync,lostSync)) {
case STATE_INVALID:
mPriv->autoState = AUTOPROBE_H_AUTODETECT;
break;
case STATE_VALID:
xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
ProtocolIDToName(pMse->protocolID));
mPriv->autoState = AUTOPROBE_H_GOOD;
return;
case STATE_UNCERTAIN:
return;
}
break;
case AUTOPROBE_H_AUTODETECT:
AP_DBG(("State H_AUTODETECT\n"));
pMse->protocolID = PROT_AUTO;
AP_DBG(("Looking for PnP/OS mouse\n"));
mPriv->count = 0;
SetupMouse(pInfo);
if (pMse->protocolID != PROT_AUTO)
mPriv->autoState = AUTOPROBE_H_GOOD;
else
mPriv->autoState = AUTOPROBE_H_NOPROTO;
break;
case AUTOPROBE_NOPROTO:
AP_DBG(("State NOPROTO\n"));
mPriv->count = 0;
mPriv->serialDefaultsNum = -1;
mPriv->autoState = AUTOPROBE_COLLECT;
break;
case AUTOPROBE_COLLECT:
AP_DBG(("State COLLECT\n"));
if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES)
return;
protocolList = softProtocolList;
mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
break;
case AUTOPROBE_CREATE_PROTOLIST:
AP_DBG(("State CREATE_PROTOLIST\n"));
createProtoList(pMse, protocolList);
mPriv->protocolID = 0;
mPriv->autoState = AUTOPROBE_SWITCH_PROTOCOL;
break;
case AUTOPROBE_AUTODETECT:
AP_DBG(("State AUTODETECT\n"));
pMse->protocolID = PROT_AUTO;
AP_DBG(("Looking for PnP/OS mouse\n"));
mPriv->count = 0;
SetupMouse(pInfo);
if (pMse->protocolID != PROT_AUTO)
mPriv->autoState = AUTOPROBE_GOOD;
else
mPriv->autoState = AUTOPROBE_NOPROTO;
break;
case AUTOPROBE_VALIDATE1:
AP_DBG(("State VALIDATE1\n"));
switch (validCount(mPriv,inSync,lostSync)) {
case STATE_INVALID:
mPriv->autoState = AUTOPROBE_AUTODETECT;
break;
case STATE_VALID:
xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
ProtocolIDToName(pMse->protocolID));
mPriv->autoState = AUTOPROBE_GOOD;
break;
case STATE_UNCERTAIN:
return;
}
break;
case AUTOPROBE_VALIDATE2:
AP_DBG(("State VALIDATE2\n"));
switch (validCount(mPriv,inSync,lostSync)) {
case STATE_INVALID:
protocolList = &mPriv->protoList[mPriv->protocolID];
mPriv->autoState = AUTOPROBE_CREATE_PROTOLIST;
break;
case STATE_VALID:
xf86Msg(X_INFO,"Mouse autoprobe: selecting %s protocol\n",
ProtocolIDToName(pMse->protocolID));
mPriv->autoState = AUTOPROBE_GOOD;
break;
case STATE_UNCERTAIN:
return;
}
break;
case AUTOPROBE_SWITCHSERIAL:
{
pointer serialDefaults;
AP_DBG(("State SWITCHSERIAL\n"));
if (!serialDefaultsList)
createSerialDefaultsList();
AP_DBG(("Switching serial params\n"));
if ((serialDefaults =
serialDefaultsList[++mPriv->serialDefaultsNum]) == NULL) {
mPriv->serialDefaultsNum = 0;
} else {
pointer tmp = xf86OptionListCreate(serialDefaults, -1, 0);
xf86SetSerial(pInfo->fd, tmp);
xf86OptionListFree(tmp);
mPriv->count = 0;
mPriv->autoState = AUTOPROBE_COLLECT;
}
break;
}
case AUTOPROBE_SWITCH_PROTOCOL:
{
MouseProtocolID proto;
void *defaults;
AP_DBG(("State SWITCH_PROTOCOL\n"));
proto = mPriv->protoList[mPriv->protocolID++];
if (proto == PROT_UNKNOWN)
mPriv->autoState = AUTOPROBE_SWITCHSERIAL;
else if (!(defaults = GetProtocol(proto)->defaults)
|| (mPriv->serialDefaultsNum == -1
&& (defaults == msDefaults))
|| (mPriv->serialDefaultsNum != -1
&& serialDefaultsList[mPriv->serialDefaultsNum]
== defaults)) {
AP_DBG(("Changing Protocol to %s\n",
ProtocolIDToName(proto)));
SetMouseProto(pMse,proto);
FlushButtons(pMse);
RESET_VALIDATION;
mPriv->autoState = AUTOPROBE_VALIDATE2;
return;
}
break;
}
}
}
}
static Bool
autoGood(MouseDevPtr pMse)
{
mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
if (!pMse->autoProbe)
return TRUE;
switch (mPriv->autoState) {
case AUTOPROBE_GOOD:
case AUTOPROBE_H_GOOD:
return TRUE;
case AUTOPROBE_VALIDATE1:
case AUTOPROBE_H_VALIDATE1:
case AUTOPROBE_VALIDATE2:
case AUTOPROBE_H_VALIDATE2:
if (mPriv->goodCount < PROBE_UNCERTAINTY/2)
return TRUE;
default:
return FALSE;
}
}
#define TOT_THRESHOLD 3000
#define VAL_THRESHOLD 40
static void
checkForErraticMovements(InputInfoPtr pInfo, int dx, int dy)
{
MouseDevPtr pMse = pInfo->private;
mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
#if 1
if (!mPriv->goodCount)
return;
#endif
#if 0
if (abs(dx - mPriv->prevDx) > 300
|| abs(dy - mPriv->prevDy) > 300)
AP_DBG(("erratic1 behaviour\n"));
#endif
if (abs(dx) > VAL_THRESHOLD) {
if (sign(dx) == sign(mPriv->prevDx)) {
mPriv->accDx += dx;
if (abs(mPriv->accDx) > mPriv->acc) {
mPriv->acc = abs(mPriv->accDx);
AP_DBG(("acc=%i\n",mPriv->acc));
}
else
AP_DBG(("accDx=%i\n",mPriv->accDx));
} else {
mPriv->accDx = 0;
}
}
if (abs(dy) > VAL_THRESHOLD) {
if (sign(dy) == sign(mPriv->prevDy)) {
mPriv->accDy += dy;
if (abs(mPriv->accDy) > mPriv->acc) {
mPriv->acc = abs(mPriv->accDy);
AP_DBG(("acc: %i\n",mPriv->acc));
} else
AP_DBG(("accDy=%i\n",mPriv->accDy));
} else {
mPriv->accDy = 0;
}
}
mPriv->prevDx = dx;
mPriv->prevDy = dy;
if (mPriv->acc > TOT_THRESHOLD) {
mPriv->goodCount = PROBE_UNCERTAINTY;
mPriv->prevDx = 0;
mPriv->prevDy = 0;
mPriv->accDx = 0;
mPriv->accDy = 0;
mPriv->acc = 0;
AP_DBG(("erratic2 behaviour\n"));
autoProbeMouse(pInfo, FALSE,TRUE);
}
}
static void
SetMouseProto(MouseDevPtr pMse, MouseProtocolID protocolID)
{
pMse->protocolID = protocolID;
pMse->protocol = ProtocolIDToName(pMse->protocolID);
pMse->class = ProtocolIDToClass(pMse->protocolID);
if ((pMse->protocolID >= 0) && (pMse->protocolID < PROT_NUMPROTOS))
memcpy(pMse->protoPara, proto[pMse->protocolID],
sizeof(pMse->protoPara));
if (pMse->emulate3ButtonsSoft)
pMse->emulate3Buttons = TRUE;
}
static Bool
collectData(MouseDevPtr pMse, unsigned char u)
{
mousePrivPtr mPriv = (mousePrivPtr)pMse->mousePriv;
if (mPriv->count < NUM_MSE_AUTOPROBE_TOTAL) {
mPriv->data[mPriv->count++] = u;
if (mPriv->count <= NUM_MSE_AUTOPROBE_BYTES) {
return TRUE;
}
}
return FALSE;
}
#ifdef XFree86LOADER
ModuleInfoRec MouseInfo = {
1,
"MOUSE",
NULL,
0,
MouseAvailableOptions,
};
static void
xf86MouseUnplug(pointer p)
{
}
static pointer
xf86MousePlug(pointer module,
pointer options,
int *errmaj,
int *errmin)
{
static Bool Initialised = FALSE;
if (!Initialised) {
Initialised = TRUE;
#ifndef REMOVE_LOADER_CHECK_MODULE_INFO
if (xf86LoaderCheckSymbol("xf86AddModuleInfo"))
#endif
xf86AddModuleInfo(&MouseInfo, module);
}
xf86AddInputDriver(&MOUSE, module, 0);
return module;
}
static XF86ModuleVersionInfo xf86MouseVersionRec =
{
"mouse",
MODULEVENDORSTRING,
MODINFOSTRING1,
MODINFOSTRING2,
XF86_VERSION_CURRENT,
1, 0, 0,
ABI_CLASS_XINPUT,
ABI_XINPUT_VERSION,
MOD_CLASS_XINPUT,
{0, 0, 0, 0}
};
XF86ModuleData mouseModuleData = {&xf86MouseVersionRec,
xf86MousePlug,
xf86MouseUnplug};
#endif