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