#define NEED_EVENTS
#include "X.h"
#include "Xproto.h"
#include "inputstr.h"
#include "scrnintstr.h"
#include "mipointer.h"
#include "xf86.h"
#include "xf86Xinput.h"
#include "xf86OSmouse.h"
#include "xf86_OSlib.h"
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/file.h>
#include <assert.h>
#include <mach.h>
#include <sys/ioctl.h>
typedef unsigned short kev_type;
typedef unsigned char Scancode;
struct mouse_motion {
short mm_deltaX;
short mm_deltaY;
};
typedef struct {
kev_type type;
struct timeval time;
union {
boolean_t up;
Scancode sc;
struct mouse_motion mmotion;
} value;
} kd_event;
#define MOUSE_LEFT 1
#define MOUSE_MIDDLE 2
#define MOUSE_RIGHT 3
#define MOUSE_MOTION 4
#define KEYBD_EVENT 5
#define NUMEVENTS 64
static int
OsMouseProc(DeviceIntPtr pPointer, int what)
{
InputInfoPtr pInfo;
MouseDevPtr pMse;
unsigned char map[MSE_MAXBUTTONS + 1];
int nbuttons;
pInfo = pPointer->public.devicePrivate;
pMse = pInfo->private;
pMse->device = pPointer;
switch (what) {
case DEVICE_INIT:
pPointer->public.on = FALSE;
for (nbuttons = 0; nbuttons < MSE_MAXBUTTONS; ++nbuttons)
map[nbuttons + 1] = nbuttons + 1;
InitPointerDeviceStruct((DevicePtr)pPointer,
map,
min(pMse->buttons, MSE_MAXBUTTONS),
miPointerGetMotionEvents,
pMse->Ctrl,
miPointerGetMotionBufferSize());
xf86InitValuatorAxisStruct(pPointer, 0, 0, -1, 1, 0, 1);
xf86InitValuatorDefaults(pPointer, 0);
xf86InitValuatorAxisStruct(pPointer, 1, 0, -1, 1, 0, 1);
xf86InitValuatorDefaults(pPointer, 1);
xf86MotionHistoryAllocate(pInfo);
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,
NUMEVENTS * sizeof(kd_event));
if (!pMse->buffer) {
xfree(pMse);
xf86CloseSerial(pInfo->fd);
pInfo->fd = -1;
} else {
xf86FlushInput(pInfo->fd);
AddEnabledDevice(pInfo->fd);
}
}
pMse->lastButtons = 0;
pMse->emulateState = 0;
pPointer->public.on = TRUE;
break;
case DEVICE_OFF:
case DEVICE_CLOSE:
if (pInfo->fd != -1) {
RemoveEnabledDevice(pInfo->fd);
if (pMse->buffer) {
XisbFree(pMse->buffer);
pMse->buffer = NULL;
}
xf86CloseSerial(pInfo->fd);
pInfo->fd = -1;
}
pPointer->public.on = FALSE;
usleep(300000);
break;
}
return Success;
}
static void
OsMouseReadInput(InputInfoPtr pInfo)
{
MouseDevPtr pMse;
static kd_event eventList[NUMEVENTS];
int n, c;
kd_event *event = eventList;
unsigned char *pBuf;
pMse = pInfo->private;
XisbBlockDuration(pMse->buffer, -1);
pBuf = (unsigned char *)eventList;
n = 0;
while ((c = XisbRead(pMse->buffer)) >= 0 && n < sizeof(eventList))
pBuf[n++] = (unsigned char)c;
if (n == 0)
return;
n /= sizeof(kd_event);
while( n-- ) {
int buttons = pMse->lastButtons;
int dx = 0, dy = 0;
switch (event->type) {
case MOUSE_RIGHT:
buttons = buttons & 6 |(event->value.up ? 0 : 1);
break;
case MOUSE_MIDDLE:
buttons = buttons & 5 |(event->value.up ? 0 : 2);
break;
case MOUSE_LEFT:
buttons = buttons & 3 |(event->value.up ? 0 : 4) ;
break;
case MOUSE_MOTION:
dx = event->value.mmotion.mm_deltaX;
dy = - event->value.mmotion.mm_deltaY;
break;
default:
ErrorF("Bad mouse event (%d)\n",event->type);
continue;
}
pMse->PostEvent(pInfo, buttons, dx, dy, 0, 0);
++event;
}
return;
}
static Bool
OsMousePreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
MouseDevPtr pMse;
pMse = pInfo->private;
pMse->protocol = protocol;
xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, protocol);
xf86CollectInputOptions(pInfo, NULL, NULL);
xf86ProcessCommonOptions(pInfo, pInfo->options);
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);
xfree(pMse);
return FALSE;
}
}
xf86CloseSerial(pInfo->fd);
pInfo->fd = -1;
pMse->CommonOptions(pInfo);
pInfo->device_control = OsMouseProc;
pInfo->read_input = OsMouseReadInput;
pInfo->flags |= XI86_CONFIGURED;
return TRUE;
}
static int
SupportedInterfaces(void)
{
return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_XPS2 | MSE_AUTO;
}
static const char *internalNames[] = {
"OSMouse",
NULL
};
static const char **
BuiltinNames(void)
{
return internalNames;
}
static Bool
CheckProtocol(const char *protocol)
{
int i;
for (i = 0; internalNames[i]; i++)
if (xf86NameCmp(protocol, internalNames[i]) == 0)
return TRUE;
return FALSE;
}
static const char *
DefaultProtocol(void)
{
return "OSMouse";
}
OSMouseInfoPtr
xf86OSMouseInit(int flags)
{
OSMouseInfoPtr p;
p = xcalloc(sizeof(OSMouseInfoRec), 1);
if (!p)
return NULL;
p->SupportedInterfaces = SupportedInterfaces;
p->BuiltinNames = BuiltinNames;
p->DefaultProtocol = DefaultProtocol;
p->CheckProtocol = CheckProtocol;
p->PreInit = OsMousePreInit;
return p;
}