ws.c revision 240a9a23
1/*
2 * Copyright © 2005-2009 Matthieu Herrb
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16/* $OpenBSD: ws.c,v 1.26 2009/11/26 18:18:34 matthieu Exp $ */
17
18#ifdef HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include <unistd.h>
23#include <errno.h>
24#include <sys/ioctl.h>
25#include <sys/time.h>
26#include <dev/wscons/wsconsio.h>
27
28#include <xf86.h>
29
30#include <xf86_OSproc.h>
31#include <X11/extensions/XI.h>
32#include <X11/extensions/XIproto.h>
33#include <xf86Xinput.h>
34#include <exevents.h>
35#include <xisb.h>
36#include <mipointer.h>
37#include <extinit.h>
38
39#include "ws.h"
40
41#ifdef HAVE_PROPERTIES
42#include <X11/Xatom.h>
43#include "ws-properties.h"
44#endif
45
46
47#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
48#include <X11/Xatom.h>
49#include <xserver-properties.h>
50#endif
51
52
53static MODULESETUPPROTO(SetupProc);
54static void TearDownProc(pointer);
55
56static InputInfoPtr wsPreInit(InputDriverPtr, IDevPtr, int);
57static int wsProc(DeviceIntPtr, int);
58static int wsDeviceInit(DeviceIntPtr);
59static int wsDeviceOn(DeviceIntPtr);
60static void wsDeviceOff(DeviceIntPtr);
61static void wsReadInput(InputInfoPtr);
62static void wsSendButtons(InputInfoPtr, int);
63static int wsChangeControl(InputInfoPtr, xDeviceCtl *);
64static int wsSwitchMode(ClientPtr, DeviceIntPtr, int);
65static Bool wsOpen(InputInfoPtr);
66static void wsClose(InputInfoPtr);
67static void wsControlProc(DeviceIntPtr , PtrCtrl *);
68
69#ifdef HAVE_PROPERTIES
70static void wsInitProperty(DeviceIntPtr);
71static int wsSetProperty(DeviceIntPtr, Atom, XIPropertyValuePtr, BOOL);
72
73static Atom prop_calibration = 0;
74static Atom prop_swap = 0;
75#endif
76
77#ifdef DEBUG
78int ws_debug_level = 0;
79#endif
80
81static XF86ModuleVersionInfo VersionRec = {
82	"ws",
83	MODULEVENDORSTRING,
84	MODINFOSTRING1,
85	MODINFOSTRING2,
86	XORG_VERSION_CURRENT,
87	PACKAGE_VERSION_MAJOR,
88	PACKAGE_VERSION_MINOR,
89	PACKAGE_VERSION_PATCHLEVEL,
90	ABI_CLASS_XINPUT,
91	ABI_XINPUT_VERSION,
92	MOD_CLASS_XINPUT,
93	{0, 0, 0, 0}
94};
95
96#define WS_NOZMAP 0
97
98XF86ModuleData wsModuleData = {&VersionRec,
99			       SetupProc, TearDownProc };
100
101
102InputDriverRec WS = {
103	1,
104	"ws",
105	NULL,
106	wsPreInit,
107	NULL,
108	NULL,
109	0
110};
111
112static pointer
113SetupProc(pointer module, pointer options, int *errmaj, int *errmin)
114{
115	static Bool Initialised = FALSE;
116
117	if (!Initialised) {
118		xf86AddInputDriver(&WS, module, 0);
119		Initialised = TRUE;
120	}
121	return module;
122}
123
124static void
125TearDownProc(pointer p)
126{
127	DBG(1, ErrorF("WS TearDownProc called\n"));
128}
129
130static InputInfoPtr
131wsPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
132{
133	InputInfoPtr pInfo = NULL;
134	WSDevicePtr priv;
135	MessageType buttons_from = X_CONFIG;
136	char *s;
137
138	pInfo = xf86AllocateInput(drv, 0);
139	if (pInfo == NULL) {
140		return NULL;
141	}
142	priv = (WSDevicePtr)xcalloc(1, sizeof(WSDeviceRec));
143	if (priv == NULL)
144		goto fail;
145	pInfo->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
146	pInfo->conf_idev = dev;
147	pInfo->name = "ws";
148	pInfo->private = priv;
149
150	xf86CollectInputOptions(pInfo, NULL, NULL);
151	xf86ProcessCommonOptions(pInfo, pInfo->options);
152#ifdef DEBUG
153	ws_debug_level = xf86SetIntOption(pInfo->options, "DebugLevel",
154	    ws_debug_level);
155	xf86Msg(X_INFO, "%s: debuglevel %d\n", dev->identifier,
156	    ws_debug_level);
157#endif
158	priv->devName = xf86FindOptionValue(pInfo->options, "Device");
159	if (priv->devName == NULL) {
160		xf86Msg(X_ERROR, "%s: No Device specified.\n",
161			dev->identifier);
162		goto fail;
163	}
164	priv->buttons = xf86SetIntOption(pInfo->options, "Buttons", 0);
165	if (priv->buttons == 0) {
166		priv->buttons = DFLTBUTTONS;
167		buttons_from = X_DEFAULT;
168	}
169	priv->negativeZ =  priv->positiveZ = WS_NOZMAP;
170	s = xf86SetStrOption(pInfo->options, "ZAxisMapping", NULL);
171	if (s) {
172		int b1, b2;
173
174		if (sscanf(s, "%d %d", &b1, &b2) == 2 &&
175		    b1 > 0 && b1 <= NBUTTONS &&
176		    b2 > 0 && b2 <= NBUTTONS) {
177			priv->negativeZ = b1;
178			priv->positiveZ = b2;
179			xf86Msg(X_CONFIG,
180			    "%s: ZAxisMapping: buttons %d and %d\n",
181			    pInfo->name, b1, b2);
182		} else {
183			xf86Msg(X_WARNING, "%s: invalid ZAxisMapping value: "
184			    "\"%s\"\n", pInfo->name, s);
185		}
186	}
187	if (priv->negativeZ > priv->buttons) {
188		priv->buttons = priv->negativeZ;
189		buttons_from = X_CONFIG;
190	}
191	if (priv->positiveZ > priv->buttons) {
192		priv->buttons = priv->positiveZ;
193		buttons_from = X_CONFIG;
194	}
195	priv->negativeW =  priv->positiveW = WS_NOZMAP;
196	s = xf86SetStrOption(pInfo->options, "WAxisMapping", NULL);
197	if (s) {
198		int b1, b2;
199
200		if (sscanf(s, "%d %d", &b1, &b2) == 2 &&
201		    b1 > 0 && b1 <= NBUTTONS &&
202		    b2 > 0 && b2 <= NBUTTONS) {
203			priv->negativeW = b1;
204			priv->positiveW = b2;
205			xf86Msg(X_CONFIG,
206			    "%s: WAxisMapping: buttons %d and %d\n",
207			    pInfo->name, b1, b2);
208		} else {
209			xf86Msg(X_WARNING, "%s: invalid WAxisMapping value: "
210			    "\"%s\"\n", pInfo->name, s);
211		}
212	}
213	if (priv->negativeW > priv->buttons) {
214		priv->buttons = priv->negativeW;
215		buttons_from = X_CONFIG;
216	}
217	if (priv->positiveW > priv->buttons) {
218		priv->buttons = priv->positiveW;
219		buttons_from = X_CONFIG;
220	}
221
222	priv->screen_no = xf86SetIntOption(pInfo->options, "ScreenNo", 0);
223	xf86Msg(X_CONFIG, "%s associated screen: %d\n",
224	    dev->identifier, priv->screen_no);
225	if (priv->screen_no >= screenInfo.numScreens ||
226	    priv->screen_no < 0) {
227		priv->screen_no = 0;
228	}
229
230
231	priv->swap_axes = xf86SetBoolOption(pInfo->options, "SwapXY", 0);
232	if (priv->swap_axes) {
233		xf86Msg(X_CONFIG,
234		    "%s device will work with X and Y axes swapped\n",
235		    dev->identifier);
236	}
237	priv->inv_x = 0;
238	priv->inv_y = 0;
239	s = xf86FindOptionValue(pInfo->options, "Rotate");
240	if (s) {
241		if (xf86NameCmp(s, "CW") == 0) {
242			priv->inv_x = 1;
243			priv->inv_y = 0;
244			priv->swap_axes = 1;
245		} else if (xf86NameCmp(s, "CCW") == 0) {
246			priv->inv_x = 0;
247			priv->inv_y = 1;
248			priv->swap_axes = 1;
249		} else if (xf86NameCmp(s, "UD") == 0) {
250			priv->inv_x = 1;
251			priv->inv_y = 1;
252		} else {
253			xf86Msg(X_ERROR, "\"%s\" is not a valid value "
254				"for Option \"Rotate\"\n", s);
255			xf86Msg(X_ERROR, "Valid options are \"CW\", \"CCW\","
256				" or \"UD\"\n");
257		}
258	}
259	if (wsOpen(pInfo) != Success) {
260		goto fail;
261	}
262	if (ioctl(pInfo->fd, WSMOUSEIO_GTYPE, &priv->type) != 0) {
263		wsClose(pInfo);
264		goto fail;
265	}
266	if (priv->type == WSMOUSE_TYPE_TPANEL) {
267		pInfo->type_name = XI_TOUCHSCREEN;
268		priv->raw = xf86SetBoolOption(pInfo->options, "Raw", 1);
269	} else {
270		pInfo->type_name = XI_MOUSE;
271		priv->raw = xf86SetBoolOption(pInfo->options, "Raw", 0);
272		if (priv->raw) {
273			xf86Msg(X_WARNING, "Device is not a touch panel,"
274			    "ignoring 'Option \"Raw\"'\n");
275			priv->raw = 0;
276		}
277	}
278	if (priv->raw) {
279		xf86Msg(X_CONFIG,
280		    "%s device will work in raw mode\n",
281		    dev->identifier);
282	}
283
284#ifndef __NetBSD__
285	if (priv->type == WSMOUSE_TYPE_TPANEL && priv->raw) {
286		if (ioctl(pInfo->fd, WSMOUSEIO_GCALIBCOORDS,
287			&priv->coords) != 0) {
288			xf86Msg(X_ERROR, "GCALIBCOORS failed %s\n",
289			    strerror(errno));
290			wsClose(pInfo);
291			goto fail;
292		}
293
294		/* get default coordinate space from kernel */
295		priv->min_x = priv->coords.minx;
296		priv->max_x = priv->coords.maxx;
297		priv->min_y = priv->coords.miny;
298		priv->max_y = priv->coords.maxy;
299	} else {
300#endif
301		/* in calibrated mode, coordinate space, is screen coords */
302		priv->min_x = 0;
303		priv->max_x = screenInfo.screens[priv->screen_no]->width - 1;
304		priv->min_y = 0;
305		priv->max_y = screenInfo.screens[priv->screen_no]->height - 1;
306#ifndef __NetBSD__
307	}
308#endif
309	/* Allow options to override this */
310	priv->min_x = xf86SetIntOption(pInfo->options, "MinX", priv->min_x);
311	xf86Msg(X_INFO, "%s minimum x position: %d\n",
312	    dev->identifier, priv->min_x);
313	priv->max_x = xf86SetIntOption(pInfo->options, "MaxX", priv->max_x);
314	xf86Msg(X_INFO, "%s maximum x position: %d\n",
315	    dev->identifier, priv->max_x);
316	priv->min_y = xf86SetIntOption(pInfo->options, "MinY", priv->min_y);
317	xf86Msg(X_INFO, "%s minimum y position: %d\n",
318	    dev->identifier, priv->min_y);
319	priv->max_y = xf86SetIntOption(pInfo->options, "MaxY", priv->max_y);
320	xf86Msg(X_INFO, "%s maximum y position: %d\n",
321	    dev->identifier, priv->max_y);
322
323	pInfo->name = dev->identifier;
324	pInfo->device_control = wsProc;
325	pInfo->read_input = wsReadInput;
326	pInfo->control_proc = wsChangeControl;
327	pInfo->switch_mode = wsSwitchMode;
328	pInfo->conversion_proc = NULL;
329	pInfo->reverse_conversion_proc = NULL;
330	pInfo->private = priv;
331	pInfo->old_x = -1;
332	pInfo->old_y = -1;
333	xf86Msg(buttons_from, "%s: Buttons: %d\n", pInfo->name, priv->buttons);
334
335	wsClose(pInfo);
336
337	wsmbEmuPreInit(pInfo);
338
339	/* mark the device configured */
340	pInfo->flags |= XI86_CONFIGURED;
341	return pInfo;
342fail:
343	if (priv != NULL)
344		xfree(priv);
345	if (pInfo != NULL)
346		xfree(pInfo);
347	return NULL;
348}
349
350static int
351wsProc(DeviceIntPtr pWS, int what)
352{
353	InputInfoPtr pInfo = (InputInfoPtr)pWS->public.devicePrivate;
354
355	switch (what) {
356	case DEVICE_INIT:
357		return wsDeviceInit(pWS);
358
359	case DEVICE_ON:
360		return wsDeviceOn(pWS);
361
362	case DEVICE_OFF:
363		wsDeviceOff(pWS);
364		break;
365
366	case DEVICE_CLOSE:
367		DBG(1, ErrorF("WS DEVICE_CLOSE\n"));
368		wsClose(pInfo);
369		break;
370
371	default:
372		xf86Msg(X_ERROR, "WS: unknown command %d\n", what);
373		return !Success;
374	} /* switch */
375	return Success;
376} /* wsProc */
377
378static int
379wsDeviceInit(DeviceIntPtr pWS)
380{
381	InputInfoPtr pInfo = (InputInfoPtr)pWS->public.devicePrivate;
382	WSDevicePtr priv = (WSDevicePtr)XI_PRIVATE(pWS);
383	unsigned char map[NBUTTONS + 1];
384	int i, xmin, xmax, ymin, ymax;
385#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
386	Atom btn_labels[NBUTTONS] = {0};
387	Atom axes_labels[NAXES] = {0};
388#endif
389
390	DBG(1, ErrorF("WS DEVICE_INIT\n"));
391
392#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
393	btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
394	btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
395	btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
396#endif
397	priv->screen_width = screenInfo.screens[priv->screen_no]->width;
398	priv->screen_height = screenInfo.screens[priv->screen_no]->height;
399
400	for (i = 0; i < NBUTTONS; i++)
401		map[i + 1] = i + 1;
402	if (!InitButtonClassDeviceStruct(pWS,
403		min(priv->buttons, NBUTTONS),
404#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
405		btn_labels,
406#endif
407		map))
408		return !Success;
409
410	if (priv->type == WSMOUSE_TYPE_TPANEL) {
411		xmin = priv->min_x;
412		xmax = priv->max_x;
413		ymin = priv->min_y;
414		ymax = priv->max_y;
415	} else {
416		xmin = -1;
417		xmax = -1;
418		ymin = -1;
419		ymax = -1;
420	}
421
422#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
423	if ((priv->type == WSMOUSE_TYPE_TPANEL)) {
424		axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
425		axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
426	} else {
427		axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
428		axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
429	}
430#endif
431	if (!InitValuatorClassDeviceStruct(pWS,
432		NAXES,
433#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
434		axes_labels,
435#endif
436#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 3
437		xf86GetMotionEvents,
438#endif
439		GetMotionHistorySize(),
440		priv->type == WSMOUSE_TYPE_TPANEL ?
441		Absolute : Relative))
442		return !Success;
443	if (!InitPtrFeedbackClassDeviceStruct(pWS, wsControlProc))
444		return !Success;
445
446	xf86InitValuatorAxisStruct(pWS, 0,
447#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
448	    axes_labels[0],
449#endif
450	    xmin, xmax, 1, 0, 1);
451	xf86InitValuatorDefaults(pWS, 0);
452
453	xf86InitValuatorAxisStruct(pWS, 1,
454#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
455	    axes_labels[1],
456#endif
457	    ymin, ymax, 1, 0, 1);
458	xf86InitValuatorDefaults(pWS, 1);
459	xf86MotionHistoryAllocate(pInfo);
460	AssignTypeAndName(pWS, pInfo->atom, pInfo->name);
461	pWS->public.on = FALSE;
462	if (wsOpen(pInfo) != Success) {
463		return !Success;
464	}
465#ifdef HAVE_PROPERTIES
466	wsInitProperty(pWS);
467	XIRegisterPropertyHandler(pWS, wsSetProperty, NULL, NULL);
468	wsmbEmuInitProperty(pWS);
469#endif
470	return Success;
471}
472
473static int
474wsDeviceOn(DeviceIntPtr pWS)
475{
476	InputInfoPtr pInfo = (InputInfoPtr)pWS->public.devicePrivate;
477	WSDevicePtr priv = (WSDevicePtr)XI_PRIVATE(pWS);
478	struct wsmouse_calibcoords coords;
479
480	DBG(1, ErrorF("WS DEVICE ON\n"));
481	if ((pInfo->fd < 0) && (wsOpen(pInfo) != Success)) {
482		xf86Msg(X_ERROR, "wsOpen failed %s\n",
483		    strerror(errno));
484			return !Success;
485	}
486
487#ifndef __NetBSD__
488	if (priv->type == WSMOUSE_TYPE_TPANEL) {
489		/* save calibration values */
490		if (ioctl(pInfo->fd, WSMOUSEIO_GCALIBCOORDS, &coords) != 0) {
491			xf86Msg(X_ERROR, "GCALIBCOORS failed %s\n",
492			    strerror(errno));
493			return !Success;
494		}
495		memcpy(&priv->coords, &coords, sizeof coords);
496		/* set raw mode */
497		if (coords.samplelen != priv->raw) {
498			coords.samplelen = priv->raw;
499			if (ioctl(pInfo->fd, WSMOUSEIO_SCALIBCOORDS,
500				&coords) != 0) {
501				xf86Msg(X_ERROR, "GCALIBCOORS failed %s\n",
502				    strerror(errno));
503				return !Success;
504			}
505		}
506	}
507#endif
508	priv->buffer = XisbNew(pInfo->fd,
509	    sizeof(struct wscons_event) * NUMEVENTS);
510	if (priv->buffer == NULL) {
511		xf86Msg(X_ERROR, "cannot alloc xisb buffer\n");
512		wsClose(pInfo);
513		return !Success;
514	}
515	xf86AddEnabledDevice(pInfo);
516	wsmbEmuOn(pInfo);
517	pWS->public.on = TRUE;
518	return Success;
519}
520
521static void
522wsDeviceOff(DeviceIntPtr pWS)
523{
524	InputInfoPtr pInfo = (InputInfoPtr)pWS->public.devicePrivate;
525	WSDevicePtr priv = (WSDevicePtr)XI_PRIVATE(pWS);
526	struct wsmouse_calibcoords coords;
527
528	DBG(1, ErrorF("WS DEVICE OFF\n"));
529	wsmbEmuFinalize(pInfo);
530
531#ifndef __NetBSD__
532	if (priv->type == WSMOUSE_TYPE_TPANEL) {
533		/* Restore calibration data */
534		memcpy(&coords, &priv->coords, sizeof coords);
535		if (ioctl(pInfo->fd, WSMOUSEIO_SCALIBCOORDS, &coords) != 0) {
536			xf86Msg(X_ERROR, "SCALIBCOORS failed %s\n",
537			    strerror(errno));
538		}
539	}
540#endif
541	if (pInfo->fd >= 0) {
542		xf86RemoveEnabledDevice(pInfo);
543		wsClose(pInfo);
544	}
545	if (priv->buffer) {
546		XisbFree(priv->buffer);
547		priv->buffer = NULL;
548	}
549	pWS->public.on = FALSE;
550}
551
552static void
553wsReadInput(InputInfoPtr pInfo)
554{
555	WSDevicePtr priv;
556	static struct wscons_event eventList[NUMEVENTS];
557	int n, c;
558	struct wscons_event *event = eventList;
559	unsigned char *pBuf;
560	int ax, ay;
561
562	priv = pInfo->private;
563
564	XisbBlockDuration(priv->buffer, -1);
565	pBuf = (unsigned char *)eventList;
566	n = 0;
567	while (n < sizeof(eventList) && (c = XisbRead(priv->buffer)) >= 0) {
568		pBuf[n++] = (unsigned char)c;
569	}
570
571	if (n == 0)
572		return;
573
574	n /= sizeof(struct wscons_event);
575	while( n-- ) {
576		int buttons = priv->lastButtons;
577		int dx = 0, dy = 0, dz = 0, dw = 0;
578		int zbutton = 0, wbutton = 0;
579
580		ax = 0; ay = 0;
581		switch (event->type) {
582		case WSCONS_EVENT_MOUSE_UP:
583
584			buttons &= ~(1 << event->value);
585			DBG(4, ErrorF("Button %d up %x\n", event->value,
586				buttons));
587		break;
588		case WSCONS_EVENT_MOUSE_DOWN:
589			buttons |= (1 << event->value);
590			DBG(4, ErrorF("Button %d down %x\n", event->value,
591				buttons));
592			break;
593		case WSCONS_EVENT_MOUSE_DELTA_X:
594			dx = event->value;
595			DBG(4, ErrorF("Relative X %d\n", event->value));
596			break;
597		case WSCONS_EVENT_MOUSE_DELTA_Y:
598			dy = -event->value;
599			DBG(4, ErrorF("Relative Y %d\n", event->value));
600			break;
601		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
602			DBG(4, ErrorF("Absolute X %d\n", event->value));
603			if (event->value != 4095) {
604				ax = event->value;
605				if (priv->inv_x)
606					ax = priv->max_x - ax + priv->min_x;
607			}
608			break;
609		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
610			DBG(4, ErrorF("Absolute Y %d\n", event->value));
611			ay = event->value;
612			if (priv->inv_y)
613				ay = priv->max_y - ay + priv->min_y;
614			break;
615#ifdef WSCONS_EVENT_MOUSE_DELTA_Z
616		case WSCONS_EVENT_MOUSE_DELTA_Z:
617			DBG(4, ErrorF("Relative Z %d\n", event->value));
618			dz = event->value;
619			break;
620#endif
621#ifdef WSCONS_EVENT_MOUSE_ABSOLUTE_Z
622		case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
623			/* ignore those */
624			++event;
625			continue;
626			break;
627#endif
628#ifdef WSCONS_EVENT_MOUSE_DELTA_W
629		case WSCONS_EVENT_MOUSE_DELTA_W:
630			DBG(4, ErrorF("Relative W %d\n", event->value));
631			dw = event->value;
632			break;
633#endif
634		default:
635			xf86Msg(X_WARNING, "%s: bad wsmouse event type=%d\n",
636			    pInfo->name, event->type);
637			++event;
638			continue;
639		} /* case */
640
641		if (dx || dy) {
642			/* relative motion event */
643			DBG(3, ErrorF("postMotionEvent dX %d dY %d\n",
644				      dx, dy));
645			xf86PostMotionEvent(pInfo->dev, 0, 0, 2,
646			    dx, dy);
647		}
648		if (dz && priv->negativeZ != WS_NOZMAP
649		    && priv->positiveZ != WS_NOZMAP) {
650			buttons &= ~(priv->negativeZ | priv->positiveZ);
651			if (dz < 0) {
652				DBG(4, ErrorF("Z -> button %d\n",
653					priv->negativeZ));
654				zbutton = 1 << (priv->negativeZ - 1);
655			} else {
656				DBG(4, ErrorF("Z -> button %d\n",
657					priv->positiveZ));
658				zbutton = 1 << (priv->positiveZ - 1);
659			}
660			buttons |= zbutton;
661			dz = 0;
662		}
663		if (dw && priv->negativeW != WS_NOZMAP
664		    && priv->positiveW != WS_NOZMAP) {
665			buttons &= ~(priv->negativeW | priv->positiveW);
666			if (dw < 0) {
667				DBG(4, ErrorF("W -> button %d\n",
668					priv->negativeW));
669				wbutton = 1 << (priv->negativeW - 1);
670			} else {
671				DBG(4, ErrorF("W -> button %d\n",
672					priv->positiveW));
673				wbutton = 1 << (priv->positiveW - 1);
674			}
675			buttons |= wbutton;
676			dw = 0;
677		}
678		if (priv->lastButtons != buttons) {
679			/* button event */
680			wsSendButtons(pInfo, buttons);
681		}
682		if (zbutton != 0) {
683			/* generate a button up event */
684			buttons &= ~zbutton;
685			wsSendButtons(pInfo, buttons);
686		}
687		if (ax) {
688			/* absolute position event */
689			DBG(3, ErrorF("postMotionEvent X %d\n", ax));
690			xf86PostMotionEvent(pInfo->dev, 1, 0, 1, ax);
691		}
692		if (ay) {
693			/* absolute position event */
694			DBG(3, ErrorF("postMotionEvent y %d\n", ay));
695			xf86PostMotionEvent(pInfo->dev, 1, 1, 1, ay);
696		}
697		++event;
698	}
699	return;
700} /* wsReadInput */
701
702static void
703wsSendButtons(InputInfoPtr pInfo, int buttons)
704{
705	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
706	int button, mask;
707
708
709	for (button = 1; button < NBUTTONS; button++) {
710		mask = 1 << (button - 1);
711		if ((mask & priv->lastButtons) != (mask & buttons)) {
712			if (!wsmbEmuFilterEvent(pInfo, button,
713				(buttons & mask) != 0)) {
714				xf86PostButtonEvent(pInfo->dev, TRUE,
715				    button, (buttons & mask) != 0,
716				    0, 0);
717				DBG(3, ErrorF("post button event %d %d\n",
718					button, (buttons & mask) != 0))
719				    }
720		}
721	} /* for */
722	priv->lastButtons = buttons;
723} /* wsSendButtons */
724
725
726static int
727wsChangeControl(InputInfoPtr pInfo, xDeviceCtl *control)
728{
729	return BadMatch;
730}
731
732static int
733wsSwitchMode(ClientPtr client, DeviceIntPtr dev, int mode)
734{
735	return BadMatch;
736}
737
738static Bool
739wsOpen(InputInfoPtr pInfo)
740{
741	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
742#ifdef __NetBSD__
743	int version = WSMOUSE_EVENT_VERSION;
744#endif
745
746	DBG(1, ErrorF("WS open %s\n", priv->devName));
747	pInfo->fd = xf86OpenSerial(pInfo->options);
748	if (pInfo->fd == -1) {
749	    xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
750	    return !Success;
751	}
752#ifdef __NetBSD__
753	if (ioctl(pInfo->fd, WSMOUSEIO_SETVERSION, &version) == -1) {
754		xf86Msg(X_ERROR, "%s: cannot set wsmouse event version\n",
755		    pInfo->name);
756		return !Success;
757	}
758#endif
759	return Success;
760}
761
762static void
763wsClose(InputInfoPtr pInfo)
764{
765	xf86CloseSerial(pInfo->fd);
766	pInfo->fd = -1;
767}
768
769static void
770wsControlProc(DeviceIntPtr device, PtrCtrl *ctrl)
771{
772	InputInfoPtr pInfo = device->public.devicePrivate;
773	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
774
775	DBG(1, ErrorF("wsControlProc\n"));
776	priv->num = ctrl->num;
777	priv->den = ctrl->den;
778	priv->threshold = ctrl->threshold;
779}
780
781#ifdef HAVE_PROPERTIES
782static void
783wsInitProperty(DeviceIntPtr device)
784{
785	InputInfoPtr pInfo = device->public.devicePrivate;
786	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
787	int rc;
788
789	DBG(1, ErrorF("wsInitProperty\n"));
790	if (priv->type != WSMOUSE_TYPE_TPANEL)
791		return;
792
793	prop_calibration = MakeAtom(WS_PROP_CALIBRATION,
794	    strlen(WS_PROP_CALIBRATION), TRUE);
795	rc = XIChangeDeviceProperty(device, prop_calibration, XA_INTEGER, 32,
796	    PropModeReplace, 4, &priv->min_x, FALSE);
797	if (rc != Success)
798		return;
799
800	XISetDevicePropertyDeletable(device, prop_calibration, FALSE);
801
802	prop_swap = MakeAtom(WS_PROP_SWAP_AXES,
803	    strlen(WS_PROP_SWAP_AXES), TRUE);
804	rc = XIChangeDeviceProperty(device, prop_swap, XA_INTEGER, 8,
805	    PropModeReplace, 1, &priv->swap_axes, FALSE);
806	if (rc != Success)
807		return;
808	return;
809}
810
811static int
812wsSetProperty(DeviceIntPtr device, Atom atom, XIPropertyValuePtr val,
813    BOOL checkonly)
814{
815	InputInfoPtr pInfo = device->public.devicePrivate;
816	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
817	struct wsmouse_calibcoords coords;
818	int need_update = 0;
819	AxisInfoPtr ax = device->valuator->axes,
820		    ay = device->valuator->axes + 1;
821
822	DBG(1, ErrorF("wsSetProperty %s\n", NameForAtom(atom)));
823
824	/* Ignore non panel devices */
825	if (priv->type != WSMOUSE_TYPE_TPANEL)
826		return Success;
827
828	if (atom == prop_calibration) {
829		if (val->format != 32 || val->type != XA_INTEGER)
830			return BadMatch;
831		if (val->size != 4 && val->size != 0)
832			return BadMatch;
833		if (!checkonly) {
834			if (val->size == 0) {
835				DBG(1, ErrorF(" uncalibrate\n"));
836				priv->min_x = 0;
837				priv->max_x = -1;
838				priv->min_y = 0;
839				priv->max_y = -1;
840			} else {
841				priv->min_x = ((int *)(val->data))[0];
842				priv->max_x = ((int *)(val->data))[1];
843				priv->min_y = ((int *)(val->data))[2];
844				priv->max_y = ((int *)(val->data))[3];
845				DBG(1, ErrorF(" calibrate %d %d %d %d\n",
846					priv->min_x, priv->max_x,
847					priv->min_y, priv->max_y));
848				need_update++;
849			}
850			/* Update axes descriptors */
851			ax->min_value = priv->min_x;
852			ax->max_value = priv->max_x;
853			ay->min_value = priv->min_y;
854			ay->max_value = priv->max_y;
855		}
856	} else if (atom == prop_swap) {
857		if (val->format != 8 || val->type != XA_INTEGER ||
858		    val->size != 1)
859			return BadMatch;
860		if (!checkonly) {
861			priv->swap_axes = *((BOOL *)val->data);
862			DBG(1, ErrorF("swap_axes %d\n", priv->swap_axes));
863			need_update++;
864		}
865	}
866	if (need_update) {
867		/* Update the saved values to be restored on device off */
868		priv->coords.minx = priv->min_x;
869		priv->coords.maxx = priv->max_x;
870		priv->coords.miny = priv->min_y;
871		priv->coords.maxy = priv->max_y;
872#ifndef __NetBSD__
873		priv->coords.swapxy = priv->swap_axes;
874#endif
875		/* Update the kernel calibration table */
876		coords.minx = priv->min_x;
877		coords.maxx = priv->max_x;
878		coords.miny = priv->min_y;
879		coords.maxy = priv->max_y;
880		coords.samplelen = priv->raw;
881#ifndef __NetBSD__
882		coords.swapxy = priv->swap_axes;
883		coords.resx = priv->coords.resx;
884		coords.resy = priv->coords.resy;
885#endif
886		if (ioctl(pInfo->fd, WSMOUSEIO_SCALIBCOORDS, &coords) != 0) {
887			xf86Msg(X_ERROR, "SCALIBCOORDS failed %s\n",
888			    strerror(errno));
889		}
890	}
891	return Success;
892}
893#endif
894