ws.c revision 30bf185b
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	const char *cs;
136	int rc;
137
138	priv = (WSDevicePtr)calloc(1, sizeof(WSDeviceRec));
139	if (priv == NULL) {
140		rc = BadAlloc;
141		goto fail;
142	}
143	pInfo->private = priv;
144
145#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
146	xf86CollectInputOptions(pInfo, NULL, NULL);
147	xf86ProcessCommonOptions(pInfo, pInfo->options);
148#else
149	xf86CollectInputOptions(pInfo, NULL);
150#endif
151#ifdef DEBUG
152	ws_debug_level = xf86SetIntOption(pInfo->options, "DebugLevel",
153	    ws_debug_level);
154	xf86Msg(X_INFO, "%s: debuglevel %d\n", pInfo->name,
155	    ws_debug_level);
156#endif
157	priv->devName = xf86FindOptionValue(pInfo->options, "Device");
158	if (priv->devName == NULL) {
159		xf86Msg(X_ERROR, "%s: No Device specified.\n",
160			pInfo->name);
161		rc = BadValue;
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", "4 5 6 7");
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	    pInfo->name, 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		    pInfo->name);
236	}
237	priv->inv_x = 0;
238	priv->inv_y = 0;
239	cs = xf86FindOptionValue(pInfo->options, "Rotate");
240	if (cs) {
241		if (xf86NameCmp(cs, "CW") == 0) {
242			priv->inv_x = 1;
243			priv->inv_y = 0;
244			priv->swap_axes = 1;
245		} else if (xf86NameCmp(cs, "CCW") == 0) {
246			priv->inv_x = 0;
247			priv->inv_y = 1;
248			priv->swap_axes = 1;
249		} else if (xf86NameCmp(cs, "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", cs);
255			xf86Msg(X_ERROR, "Valid options are \"CW\", \"CCW\","
256				" or \"UD\"\n");
257		}
258	}
259	if (wsOpen(pInfo) != Success) {
260		rc = BadValue;
261		goto fail;
262	}
263	if (ioctl(pInfo->fd, WSMOUSEIO_GTYPE, &priv->type) != 0) {
264		wsClose(pInfo);
265		rc = BadValue;
266		goto fail;
267	}
268
269	/* assume screen coordinate space until proven wrong */
270	priv->min_x = 0;
271	priv->max_x = screenInfo.screens[priv->screen_no]->width - 1;
272	priv->min_y = 0;
273	priv->max_y = screenInfo.screens[priv->screen_no]->height - 1;
274	priv->raw = 0;
275
276	/* don't rely on the device type - we may be listening to a mux */
277	if (ioctl(pInfo->fd, WSMOUSEIO_GCALIBCOORDS,
278		&priv->coords) != 0) {
279		/* can't get absolute coordinate space - assume mouse */
280		pInfo->type_name = XI_MOUSE;
281	} else if (priv->coords.samplelen == WSMOUSE_CALIBCOORDS_RESET) {
282		/*
283		 * we're getting raw coordinates - update accordingly and hope
284		 * that there is no other absolute positioning device on the
285		 * same mux
286		 */
287		priv->min_x = priv->coords.minx;
288		priv->max_x = priv->coords.maxx;
289		priv->min_y = priv->coords.miny;
290		priv->max_y = priv->coords.maxy;
291		priv->raw = 1;
292		pInfo->type_name = XI_TOUCHSCREEN;
293	} else {
294		/*
295		 * touchscreen not in raw mode, should send us screen
296		 * coordinates
297		 */
298		pInfo->type_name = XI_TOUCHSCREEN;
299	}
300
301	if (priv->raw) {
302		xf86Msg(X_CONFIG,
303		    "%s device will work in raw mode\n",
304		    pInfo->name);
305	}
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 GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
454	axes_labels[HSCROLL_AXIS] =
455	    XIGetKnownProperty(AXIS_LABEL_PROP_REL_HSCROLL);
456	axes_labels[VSCROLL_AXIS] =
457	    XIGetKnownProperty(AXIS_LABEL_PROP_REL_VSCROLL);
458#endif
459	if (!InitValuatorClassDeviceStruct(pWS,
460		NAXES,
461		axes_labels,
462		GetMotionHistorySize(),
463		priv->type == WSMOUSE_TYPE_TPANEL ?
464		Absolute : Relative))
465		return !Success;
466	if (!InitPtrFeedbackClassDeviceStruct(pWS, wsControlProc))
467		return !Success;
468
469	xf86InitValuatorAxisStruct(pWS, 0,
470	    axes_labels[0],
471	    xmin, xmax, 1, 0, 1
472#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 12
473	    , priv->type == WSMOUSE_TYPE_TPANEL  ? Absolute : Relative
474#endif
475	);
476	xf86InitValuatorDefaults(pWS, 0);
477
478	xf86InitValuatorAxisStruct(pWS, 1,
479	    axes_labels[1],
480	    ymin, ymax, 1, 0, 1
481#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 12
482	    , priv->type == WSMOUSE_TYPE_TPANEL ? Absolute : Relative
483#endif
484	);
485	xf86InitValuatorDefaults(pWS, 1);
486
487
488#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
489	xf86InitValuatorAxisStruct(pWS, HSCROLL_AXIS,
490	    axes_labels[HSCROLL_AXIS], 0, -1, 0, 0, 0, Relative);
491	xf86InitValuatorAxisStruct(pWS, VSCROLL_AXIS,
492	    axes_labels[VSCROLL_AXIS], 0, -1, 0, 0, 0, Relative);
493	priv->scroll_mask = valuator_mask_new(MAX_VALUATORS);
494	if (!priv->scroll_mask) {
495		return !Success;
496	}
497
498	/*
499	 * The value of an HSCROLL or VSCROLL event is the fraction
500	 *         motion_delta / scroll_distance
501	 * in [*.12] fixed-point format.  The 'increment' attribute of the
502	 * scroll axes is constant:
503	 */
504	SetScrollValuator(pWS, HSCROLL_AXIS, SCROLL_TYPE_HORIZONTAL, 4096, 0);
505	SetScrollValuator(pWS, VSCROLL_AXIS, SCROLL_TYPE_VERTICAL, 4096, 0);
506#endif
507
508#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
509	xf86MotionHistoryAllocate(pInfo);
510	AssignTypeAndName(pWS, pInfo->atom, pInfo->name);
511#endif
512	pWS->public.on = FALSE;
513	if (wsOpen(pInfo) != Success) {
514		return !Success;
515	}
516	wsInitProperty(pWS);
517	XIRegisterPropertyHandler(pWS, wsSetProperty, NULL, NULL);
518	wsmbEmuInitProperty(pWS);
519	return Success;
520}
521
522static int
523wsDeviceOn(DeviceIntPtr pWS)
524{
525	InputInfoPtr pInfo = (InputInfoPtr)pWS->public.devicePrivate;
526	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
527#ifndef __NetBSD__
528	struct wsmouse_calibcoords coords;
529#endif
530
531	DBG(1, ErrorF("WS DEVICE ON\n"));
532	if ((pInfo->fd < 0) && (wsOpen(pInfo) != Success)) {
533		xf86Msg(X_ERROR, "wsOpen failed %s\n",
534		    strerror(errno));
535			return !Success;
536	}
537
538#ifndef __NetBSD__
539	if (priv->type == WSMOUSE_TYPE_TPANEL) {
540		/* get calibration values */
541		if (ioctl(pInfo->fd, WSMOUSEIO_GCALIBCOORDS, &coords) != 0) {
542			xf86Msg(X_ERROR, "GCALIBCOORS failed %s\n",
543			    strerror(errno));
544			return !Success;
545		}
546		memcpy(&priv->coords, &coords, sizeof coords);
547		/* set raw mode */
548		if (coords.samplelen != priv->raw) {
549			coords.samplelen = priv->raw;
550			if (ioctl(pInfo->fd, WSMOUSEIO_SCALIBCOORDS,
551				&coords) != 0) {
552				xf86Msg(X_ERROR, "SCALIBCOORS failed %s\n",
553				    strerror(errno));
554				return !Success;
555			}
556		}
557	}
558#endif
559	priv->buffer = XisbNew(pInfo->fd,
560	    sizeof(struct wscons_event) * NUMEVENTS);
561	if (priv->buffer == NULL) {
562		xf86Msg(X_ERROR, "cannot alloc xisb buffer\n");
563		wsClose(pInfo);
564		return !Success;
565	}
566	xf86AddEnabledDevice(pInfo);
567	wsmbEmuOn(pInfo);
568	pWS->public.on = TRUE;
569	return Success;
570}
571
572static void
573wsDeviceOff(DeviceIntPtr pWS)
574{
575	InputInfoPtr pInfo = (InputInfoPtr)pWS->public.devicePrivate;
576	WSDevicePtr priv = pInfo->private;
577#ifndef __NetBSD__
578	struct wsmouse_calibcoords coords;
579#endif
580
581	DBG(1, ErrorF("WS DEVICE OFF\n"));
582	wsmbEmuFinalize(pInfo);
583#ifndef __NetBSD__
584	if (priv->type == WSMOUSE_TYPE_TPANEL) {
585		/* Restore calibration data */
586		memcpy(&coords, &priv->coords, sizeof coords);
587		if (ioctl(pInfo->fd, WSMOUSEIO_SCALIBCOORDS, &coords) != 0) {
588			xf86Msg(X_ERROR, "SCALIBCOORS failed %s\n",
589			    strerror(errno));
590		}
591	}
592#endif
593	if (pInfo->fd >= 0) {
594		xf86RemoveEnabledDevice(pInfo);
595		wsClose(pInfo);
596	}
597	if (priv->buffer) {
598		XisbFree(priv->buffer);
599		priv->buffer = NULL;
600	}
601	pWS->public.on = FALSE;
602}
603
604static void
605wsReadInput(InputInfoPtr pInfo)
606{
607	WSDevicePtr priv;
608	static struct wscons_event eventList[NUMEVENTS];
609	int n, c;
610	struct wscons_event *event = eventList;
611	unsigned char *pBuf;
612	int ax, ay;
613
614	priv = pInfo->private;
615
616	XisbBlockDuration(priv->buffer, -1);
617	pBuf = (unsigned char *)eventList;
618	n = 0;
619	while (n < sizeof(eventList) && (c = XisbRead(priv->buffer)) >= 0) {
620		pBuf[n++] = (unsigned char)c;
621	}
622
623	if (n == 0)
624		return;
625
626	n /= sizeof(struct wscons_event);
627	while( n-- ) {
628		int buttons = priv->lastButtons;
629		int dx = 0, dy = 0, dz = 0, dw = 0;
630		int zbutton = 0, wbutton = 0;
631#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
632		int hscroll = 0, vscroll = 0;
633#endif
634
635		ax = 0; ay = 0;
636		switch (event->type) {
637		case WSCONS_EVENT_MOUSE_UP:
638
639			buttons &= ~(1 << event->value);
640			DBG(4, ErrorF("Button %d up %x\n", event->value,
641				buttons));
642		break;
643		case WSCONS_EVENT_MOUSE_DOWN:
644			buttons |= (1 << event->value);
645			DBG(4, ErrorF("Button %d down %x\n", event->value,
646				buttons));
647			break;
648		case WSCONS_EVENT_MOUSE_DELTA_X:
649			dx = event->value;
650			DBG(4, ErrorF("Relative X %d\n", event->value));
651			break;
652		case WSCONS_EVENT_MOUSE_DELTA_Y:
653			dy = -event->value;
654			DBG(4, ErrorF("Relative Y %d\n", event->value));
655			break;
656		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
657			DBG(4, ErrorF("Absolute X %d\n", event->value));
658			if (event->value == 4095)
659				break;
660			ax = event->value;
661			if (priv->inv_x)
662				ax = priv->max_x - ax + priv->min_x;
663			break;
664		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
665			DBG(4, ErrorF("Absolute Y %d\n", event->value));
666			ay = event->value;
667			if (priv->inv_y)
668				ay = priv->max_y - ay + priv->min_y;
669			break;
670		case WSCONS_EVENT_MOUSE_DELTA_Z:
671			DBG(4, ErrorF("Relative Z %d\n", event->value));
672			dz = event->value;
673			break;
674		case WSCONS_EVENT_MOUSE_ABSOLUTE_Z:
675			/* ignore those */
676			++event;
677			continue;
678			break;
679		case WSCONS_EVENT_MOUSE_DELTA_W:
680			DBG(4, ErrorF("Relative W %d\n", event->value));
681			dw = event->value;
682			break;
683#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
684		case WSCONS_EVENT_HSCROLL:
685			hscroll = event->value;
686			DBG(4, ErrorF("Horiz. Scrolling %d\n", event->value));
687			break;
688		case WSCONS_EVENT_VSCROLL:
689			vscroll = event->value;
690			DBG(4, ErrorF("Vert. Scrolling %d\n", event->value));
691			break;
692#endif
693		default:
694			xf86Msg(X_WARNING, "%s: bad wsmouse event type=%d\n",
695			    pInfo->name, event->type);
696			++event;
697			continue;
698		} /* case */
699
700		if (dx || dy) {
701			/* relative motion event */
702			DBG(3, ErrorF("postMotionEvent dX %d dY %d\n",
703				      dx, dy));
704			xf86PostMotionEvent(pInfo->dev, 0, 0, 2,
705			    dx, dy);
706		}
707		if (dz && priv->negativeZ != WS_NOZMAP
708		    && priv->positiveZ != WS_NOZMAP) {
709			buttons &= ~(priv->negativeZ | priv->positiveZ);
710			if (dz < 0) {
711				DBG(4, ErrorF("Z -> button %d\n",
712					priv->negativeZ));
713				zbutton = 1 << (priv->negativeZ - 1);
714			} else {
715				DBG(4, ErrorF("Z -> button %d\n",
716					priv->positiveZ));
717				zbutton = 1 << (priv->positiveZ - 1);
718			}
719			buttons |= zbutton;
720			dz = 0;
721		}
722		if (dw && priv->negativeW != WS_NOZMAP
723		    && priv->positiveW != WS_NOZMAP) {
724			buttons &= ~(priv->negativeW | priv->positiveW);
725			if (dw < 0) {
726				DBG(4, ErrorF("W -> button %d\n",
727					priv->negativeW));
728				wbutton = 1 << (priv->negativeW - 1);
729			} else {
730				DBG(4, ErrorF("W -> button %d\n",
731					priv->positiveW));
732				wbutton = 1 << (priv->positiveW - 1);
733			}
734			buttons |= wbutton;
735			dw = 0;
736		}
737#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 14
738		if (hscroll || vscroll) {
739			xf86Msg(X_WARNING, "%s: hscroll=%d, vscroll=%d\n",
740			    pInfo->name, hscroll, vscroll);
741			valuator_mask_zero(priv->scroll_mask);
742			valuator_mask_set_double(priv->scroll_mask,
743			    HSCROLL_AXIS, (double) hscroll);
744			valuator_mask_set_double(priv->scroll_mask,
745			    VSCROLL_AXIS, (double) vscroll);
746			xf86PostMotionEventM(pInfo->dev, FALSE, priv->scroll_mask);
747 		}
748#endif
749		if (priv->lastButtons != buttons) {
750			/* button event */
751			wsSendButtons(pInfo, buttons);
752		}
753		if (zbutton != 0) {
754			/* generate a button up event */
755			buttons &= ~zbutton;
756			wsSendButtons(pInfo, buttons);
757		}
758		if (priv->swap_axes) {
759			int tmp;
760
761			tmp = ax;
762			ax = ay;
763			ay = tmp;
764		}
765		if (ax) {
766			/* absolute position event */
767			DBG(3, ErrorF("postMotionEvent X %d\n", ax));
768			xf86PostMotionEvent(pInfo->dev, 1, 0, 1, ax);
769		}
770		if (ay) {
771			/* absolute position event */
772			DBG(3, ErrorF("postMotionEvent y %d\n", ay));
773			xf86PostMotionEvent(pInfo->dev, 1, 1, 1, ay);
774		}
775		++event;
776	}
777	return;
778} /* wsReadInput */
779
780static void
781wsSendButtons(InputInfoPtr pInfo, int buttons)
782{
783	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
784	int button, mask;
785
786	for (button = 1; button < NBUTTONS; button++) {
787		mask = 1 << (button - 1);
788		if ((mask & priv->lastButtons) != (mask & buttons)) {
789			if (!wsmbEmuFilterEvent(pInfo, button,
790				(buttons & mask) != 0)) {
791				xf86PostButtonEvent(pInfo->dev, TRUE,
792				    button, (buttons & mask) != 0,
793				    0, 0);
794				DBG(3, ErrorF("post button event %d %d\n",
795					button, (buttons & mask) != 0))
796				    }
797		}
798	} /* for */
799	priv->lastButtons = buttons;
800} /* wsSendButtons */
801
802
803static int
804wsChangeControl(InputInfoPtr pInfo, xDeviceCtl *control)
805{
806	return BadMatch;
807}
808
809static int
810wsSwitchMode(ClientPtr client, DeviceIntPtr dev, int mode)
811{
812	return BadMatch;
813}
814
815static Bool
816wsOpen(InputInfoPtr pInfo)
817{
818	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
819#ifdef __NetBSD__
820	int version = WSMOUSE_EVENT_VERSION;
821#endif
822
823	DBG(1, ErrorF("WS open %s\n", priv->devName));
824	pInfo->fd = xf86OpenSerial(pInfo->options);
825	if (pInfo->fd == -1) {
826	    xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
827	    return !Success;
828	}
829#ifdef __NetBSD__
830	if (ioctl(pInfo->fd, WSMOUSEIO_SETVERSION, &version) == -1) {
831		xf86Msg(X_ERROR, "%s: cannot set wsmouse event version\n",
832		    pInfo->name);
833		return !Success;
834	}
835#endif
836	return Success;
837}
838
839static void
840wsClose(InputInfoPtr pInfo)
841{
842	xf86CloseSerial(pInfo->fd);
843	pInfo->fd = -1;
844}
845
846static void
847wsControlProc(DeviceIntPtr device, PtrCtrl *ctrl)
848{
849	InputInfoPtr pInfo = device->public.devicePrivate;
850	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
851
852	DBG(1, ErrorF("wsControlProc\n"));
853	priv->num = ctrl->num;
854	priv->den = ctrl->den;
855	priv->threshold = ctrl->threshold;
856}
857
858static void
859wsInitProperty(DeviceIntPtr device)
860{
861	InputInfoPtr pInfo = device->public.devicePrivate;
862	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
863	int rc;
864
865	DBG(1, ErrorF("wsInitProperty\n"));
866	if (priv->type != WSMOUSE_TYPE_TPANEL)
867		return;
868
869	prop_calibration = MakeAtom(WS_PROP_CALIBRATION,
870	    strlen(WS_PROP_CALIBRATION), TRUE);
871	rc = XIChangeDeviceProperty(device, prop_calibration, XA_INTEGER, 32,
872	    PropModeReplace, 4, &priv->min_x, FALSE);
873	if (rc != Success)
874		return;
875
876	XISetDevicePropertyDeletable(device, prop_calibration, FALSE);
877
878	prop_swap = MakeAtom(WS_PROP_SWAP_AXES,
879	    strlen(WS_PROP_SWAP_AXES), TRUE);
880	rc = XIChangeDeviceProperty(device, prop_swap, XA_INTEGER, 8,
881	    PropModeReplace, 1, &priv->swap_axes, FALSE);
882	if (rc != Success)
883		return;
884	return;
885}
886
887static int
888wsSetProperty(DeviceIntPtr device, Atom atom, XIPropertyValuePtr val,
889    BOOL checkonly)
890{
891	InputInfoPtr pInfo = device->public.devicePrivate;
892	WSDevicePtr priv = (WSDevicePtr)pInfo->private;
893	struct wsmouse_calibcoords coords;
894	int need_update = 0;
895	AxisInfoPtr ax = device->valuator->axes,
896		    ay = device->valuator->axes + 1;
897
898	DBG(1, ErrorF("wsSetProperty %s\n", NameForAtom(atom)));
899
900	/* Ignore non panel devices */
901	if (priv->type != WSMOUSE_TYPE_TPANEL)
902		return Success;
903
904	if (atom == prop_calibration) {
905		if (val->format != 32 || val->type != XA_INTEGER)
906			return BadMatch;
907		if (val->size != 4 && val->size != 0)
908			return BadMatch;
909		if (!checkonly) {
910			if (val->size == 0) {
911				DBG(1, ErrorF(" uncalibrate\n"));
912				priv->min_x = 0;
913				priv->max_x = -1;
914				priv->min_y = 0;
915				priv->max_y = -1;
916			} else {
917				priv->min_x = ((int *)(val->data))[0];
918				priv->max_x = ((int *)(val->data))[1];
919				priv->min_y = ((int *)(val->data))[2];
920				priv->max_y = ((int *)(val->data))[3];
921				DBG(1, ErrorF(" calibrate %d %d %d %d\n",
922					priv->min_x, priv->max_x,
923					priv->min_y, priv->max_y));
924				need_update++;
925			}
926			/* Update axes descriptors */
927			if (!priv->swap_axes) {
928				ax->min_value = priv->min_x;
929				ax->max_value = priv->max_x;
930				ay->min_value = priv->min_y;
931				ay->max_value = priv->max_y;
932			} else {
933				ax->min_value = priv->min_y;
934				ax->max_value = priv->max_y;
935				ay->min_value = priv->min_x;
936				ay->max_value = priv->max_x;
937			}
938		}
939	} else if (atom == prop_swap) {
940		if (val->format != 8 || val->type != XA_INTEGER ||
941		    val->size != 1)
942			return BadMatch;
943		if (!checkonly) {
944			priv->swap_axes = *((BOOL *)val->data);
945			DBG(1, ErrorF("swap_axes %d\n", priv->swap_axes));
946			need_update++;
947		}
948	}
949	if (need_update) {
950		/* Update the saved values to be restored on device off */
951		priv->coords.minx = priv->min_x;
952		priv->coords.maxx = priv->max_x;
953		priv->coords.miny = priv->min_y;
954		priv->coords.maxy = priv->max_y;
955#ifndef __NetBSD__
956		priv->coords.swapxy = priv->swap_axes;
957#endif
958
959		/* Update the kernel calibration table */
960		coords.minx = priv->min_x;
961		coords.maxx = priv->max_x;
962		coords.miny = priv->min_y;
963		coords.maxy = priv->max_y;
964#ifndef __NetBSD__
965		coords.swapxy = priv->swap_axes;
966#endif
967		coords.samplelen = priv->raw;
968#ifndef __NetBSD__
969		coords.resx = priv->coords.resx;
970		coords.resy = priv->coords.resy;
971#endif
972		if (ioctl(pInfo->fd, WSMOUSEIO_SCALIBCOORDS, &coords) != 0) {
973			xf86Msg(X_ERROR, "SCALIBCOORDS failed %s\n",
974			    strerror(errno));
975		}
976	}
977	return Success;
978}
979