x68kMouse.c revision 45f60b5b
1/* $NetBSD: x68kMouse.c,v 1.15 2025/06/22 22:29:23 tsutsui Exp $ */
2/*-------------------------------------------------------------------------
3 * Copyright (c) 1996 Yasushi Yamasaki
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *-----------------------------------------------------------------------*/
26
27/*-
28 * Copyright (c) 1987 by the Regents of the University of California
29 *
30 * Permission to use, copy, modify, and distribute this
31 * software and its documentation for any purpose and without
32 * fee is hereby granted, provided that the above copyright
33 * notice appear in all copies.  The University of California
34 * makes no representations about the suitability of this
35 * software for any purpose.  It is provided "as is" without
36 * express or implied warranty.
37 */
38
39/************************************************************
40Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
41
42                    All Rights Reserved
43
44Permission  to  use,  copy,  modify,  and  distribute   this
45software  and  its documentation for any purpose and without
46fee is hereby granted, provided that the above copyright no-
47tice  appear  in all copies and that both that copyright no-
48tice and this permission notice appear in  supporting  docu-
49mentation,  and  that the names of Sun or X Consortium
50not be used in advertising or publicity pertaining to
51distribution  of  the software  without specific prior
52written permission. Sun and X Consortium make no
53representations about the suitability of this software for
54any purpose. It is provided "as is" without any express or
55implied warranty.
56
57SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
58INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
59NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
60ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
61ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
62PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
63OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
64THE USE OR PERFORMANCE OF THIS SOFTWARE.
65
66********************************************************/
67/*
68 * Copyright 1991, 1992, 1993 Kaleb S. Keithley
69 *
70 * Permission to use, copy, modify, and distribute this
71 * software and its documentation for any purpose and without
72 * fee is hereby granted, provided that the above copyright
73 * notice appear in all copies.  Kaleb S. Keithley makes no
74 * representations about the suitability of this software for
75 * any purpose.  It is provided "as is" without express or
76 * implied warranty.
77 */
78
79#include "x68k.h"
80#include "mi.h"
81#include "input.h"
82#include "inpututils.h"
83
84#include "exevents.h"
85#include "events.h"
86#include "eventstr.h"
87#include <X11/Xatom.h>
88#include "xserver-properties.h"
89
90static void x68kMouseEvents(int, int, void *);
91static Firm_event *x68kMouseGetEvents(int, int *, Bool *);
92static void x68kMouseEnqueueEvent(DeviceIntPtr, Firm_event *);
93static Bool x68kCursorOffScreen(ScreenPtr *, int *, int *);
94static void x68kCrossScreen(ScreenPtr, int);
95static void x68kWarpCursor(DeviceIntPtr, ScreenPtr, int, int);
96static void x68kMouseCtrl(DeviceIntPtr, PtrCtrl*);
97
98miPointerScreenFuncRec x68kPointerScreenFuncs = {
99    x68kCursorOffScreen,
100    x68kCrossScreen,
101    x68kWarpCursor,
102};
103
104DeviceIntPtr x68kPointerDevice = NULL;
105
106static X68kMousePriv x68kMousePriv;
107
108/*------------------------------------------------------------------------
109 * x68kMouseEvents --
110 *	When registered polled mouse input event handler is invoked,
111 *	read device events and enqueue them using the mi event queue.
112 * Results:
113 *	None.
114 *
115 *----------------------------------------------------------------------*/
116static void
117x68kMouseEvents(int fd, int ready, void *data)
118{
119    int i, numEvents = 0;
120    Bool again = FALSE;
121    Firm_event *events;
122    DeviceIntPtr device = (DeviceIntPtr)data;
123
124    input_lock();
125
126    do {
127	events = x68kMouseGetEvents(fd, &numEvents, &again);
128	for (i = 0; i < numEvents; i++) {
129	    x68kMouseEnqueueEvent(device, &events[i]);
130	}
131    } while (again);
132
133    input_unlock();
134}
135
136/*-
137 *-----------------------------------------------------------------------
138 * x68kMouseProc --
139 *	Handle the initialization, etc. of a mouse
140 *
141 * Results:
142 *	none.
143 *
144 * Side Effects:
145 *-----------------------------------------------------------------------
146 */
147int
148x68kMouseProc(DeviceIntPtr device, int what)
149{
150    DevicePtr   pMouse = &device->public;
151    int		format;
152    static int	oformat;
153    BYTE	map[4];
154    Atom btn_labels[3] = {0};
155    Atom axes_labels[2] = { 0, 0 };
156    MouseEmu3btnPtr pEmu3btn;
157    Bool emu3enable;
158    int emu3timeout;
159
160    switch (what) {
161	case DEVICE_INIT:
162            pMouse->devicePrivate = (void *) &x68kMousePriv;
163            if( (x68kMousePriv.fd = open("/dev/mouse", O_RDONLY | O_NONBLOCK)) == -1 ) {
164                ErrorF("Can't open mouse device\n");
165                return !Success;
166            }
167	    pMouse->on = FALSE;
168	    map[1] = 1;
169	    map[2] = 2;
170	    map[3] = 3;
171	    btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
172	    btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
173	    btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
174	    axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
175	    axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);
176
177	    InitPointerDeviceStruct(pMouse, map, 3, btn_labels,
178		x68kMouseCtrl, GetMotionHistorySize(),
179		2, axes_labels);
180
181	    /* X valuator */
182	    InitValuatorAxisStruct(device, 0, axes_labels[0],
183		NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);
184	    device->valuator->axisVal[0] = screenInfo.screens[0]->width / 2;
185	    device->last.valuators[0] = device->valuator->axisVal[0];
186
187	    /* Y valuator */
188	    InitValuatorAxisStruct(device, 1, axes_labels[1],
189		NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);
190	    device->valuator->axisVal[1] = screenInfo.screens[0]->height / 2;
191	    device->last.valuators[1] = device->valuator->axisVal[1];
192
193	    /* Initialize emulation 3 buttons settings */
194	    emu3enable = TRUE;			/* XXX should be configurable */
195	    emu3timeout = EMU3B_DEF_TIMEOUT;	/* XXX should be configurable */
196	    if (emu3enable) {
197		pEmu3btn = &x68kMousePriv.emu3btn;
198		Emulate3ButtonsEnable(pEmu3btn, device, emu3timeout);
199	    }
200
201	    break;
202
203	case DEVICE_ON:
204	    if (ioctl (x68kMousePriv.fd, VUIDGFORMAT, &oformat) == -1) {
205		ErrorF("x68kMouseProc ioctl VUIDGFORMAT\n");
206		return !Success;
207	    }
208	    format = VUID_FIRM_EVENT;
209	    if (ioctl (x68kMousePriv.fd, VUIDSFORMAT, &format) == -1) {
210		ErrorF("x68kMouseProc ioctl VUIDSFORMAT\n");
211		return !Success;
212	    }
213	    SetNotifyFd(x68kMousePriv.fd, x68kMouseEvents,
214		X_NOTIFY_READ, device);
215	    x68kMousePriv.bmask = 0;
216	    pMouse->on = TRUE;
217	    break;
218
219	case DEVICE_OFF:
220	    pMouse->on = FALSE;
221	    RemoveNotifyFd(x68kMousePriv.fd);
222	    break;
223
224	case DEVICE_CLOSE:
225	    if (ioctl (x68kMousePriv.fd, VUIDSFORMAT, &oformat) == -1)
226		ErrorF("x68kMouseProc ioctl VUIDSFORMAT\n");
227	    break;
228
229	case DEVICE_ABORT:
230	    break;
231    }
232    return Success;
233}
234
235/*-
236 *-----------------------------------------------------------------------
237 * x68kMouseCtrl --
238 *	Alter the control parameters for the mouse. Since acceleration
239 *	etc. is done from the PtrCtrl record in the mouse's device record,
240 *	there's nothing to do here.
241 *
242 * Results:
243 *	None.
244 *
245 * Side Effects:
246 *	None.
247 *
248 *-----------------------------------------------------------------------
249 */
250/*ARGSUSED*/
251static void
252x68kMouseCtrl(DeviceIntPtr device, PtrCtrl* ctrl)
253{
254}
255
256/*-
257 *-----------------------------------------------------------------------
258 * x68kMouseGetEvents --
259 *	Return the events waiting in the wings for the given mouse.
260 *
261 * Results:
262 *	A pointer to an array of Firm_events or (Firm_event *)0 if no events
263 *	The number of events contained in the array.
264 *	A boolean as to whether more events might be available.
265 *
266 * Side Effects:
267 *	None.
268 *-----------------------------------------------------------------------
269 */
270
271static Firm_event *
272x68kMouseGetEvents(int fd, int *pNumEvents, Bool *pAgain)
273{
274    int nBytes;               /* number of bytes of events available. */
275    static Firm_event evBuf[X68K_MAXEVENTS];     /* Buffer for Firm_events */
276
277    if ((nBytes = read (fd, (char *)evBuf, sizeof(evBuf))) == -1) {
278	if (errno == EWOULDBLOCK) {
279	    *pNumEvents = 0;
280	    *pAgain = FALSE;
281	} else {
282	    ErrorF("x68kMouseGetEvents read\n");
283	    FatalError ("Could not read from mouse");
284	}
285    } else {
286	*pNumEvents = nBytes / sizeof (Firm_event);
287	*pAgain = (nBytes == sizeof (evBuf));
288    }
289    return evBuf;
290}
291
292/*-
293 *-----------------------------------------------------------------------
294 * x68kMouseEnqueueEvent --
295 *	Given a Firm_event for a mouse, pass it off the the dix layer
296 *	properly converted...
297 *
298 * Results:
299 *	None.
300 *
301 * Side Effects:
302 *	The cursor may be redrawn...? devPrivate/x/y will be altered.
303 *
304 *-----------------------------------------------------------------------
305 */
306
307static void
308x68kMouseEnqueueEvent(DeviceIntPtr device, Firm_event *fe)
309{
310    X68kMousePrivPtr	pPriv;	/* Private data for pointer */
311    int			bmask;	/* Temporary button mask */
312    int			type, buttons, flag;
313    int			valuators[2];
314    ValuatorMask	mask;
315
316    pPriv = (X68kMousePrivPtr)device->public.devicePrivate;
317
318    switch (fe->id) {
319    case MS_LEFT:
320    case MS_MIDDLE:
321    case MS_RIGHT:
322	/*
323	 * A button changed state. Sometimes we will get two events
324	 * for a single state change. Should we get a button event which
325	 * reflects the current state of affairs, that event is discarded.
326	 *
327	 * Mouse buttons start at 1 as defined in <X11/X.h>.
328	 *
329	 * The bmask stores which buttons are currently pressed.
330	 * This bmask is also used for Emulate3Buttons functions that
331	 * assume the left button is LSB as defined in mouseEmu3btn.c.
332	 */
333	buttons = (fe->id - MS_LEFT) + 1;
334	bmask = 1 << (buttons - 1);
335	if (fe->value == VKEY_UP) {
336	    if (pPriv->bmask & bmask) {
337		type = ButtonRelease;
338		pPriv->bmask &= ~bmask;
339	    } else {
340		return;
341	    }
342	} else {
343	    if ((pPriv->bmask & bmask) == 0) {
344		type = ButtonPress;
345		pPriv->bmask |= bmask;
346	    } else {
347		return;
348	    }
349	}
350	if (buttons == Button1 || buttons == Button3) {
351	    /* Handle middle button emulation */
352	    Emulate3ButtonsQueueEvent(&pPriv->emu3btn, type, buttons, pPriv->bmask);
353	} else {
354	    flag = POINTER_RELATIVE;
355	    valuator_mask_zero(&mask);
356	    QueuePointerEvents(device, type, buttons, flag, &mask);
357	}
358	break;
359    case LOC_X_DELTA:
360	valuators[0] = fe->value;
361	valuators[1] = 0;
362	valuator_mask_set_range(&mask, 0, 2, valuators);
363        flag = POINTER_RELATIVE | POINTER_ACCELERATE;
364	QueuePointerEvents(device, MotionNotify, 0, flag, &mask);
365	break;
366    case LOC_Y_DELTA:
367	/*
368	 * For some reason, motion up generates a positive y delta
369	 * and motion down a negative delta, so we must subtract
370	 * here instead of add...
371	 */
372	valuators[0] = 0;
373	valuators[1] = -fe->value;
374	valuator_mask_set_range(&mask, 0, 2, valuators);
375        flag = POINTER_RELATIVE | POINTER_ACCELERATE;
376	QueuePointerEvents(device, MotionNotify, 0, flag, &mask);
377	break;
378    case LOC_X_ABSOLUTE:
379    case LOC_Y_ABSOLUTE:
380	/* XXX not sure how to get current X and Y position */
381    default:
382	FatalError ("%s: unrecognized id\n", __func__);
383	break;
384    }
385}
386
387/*ARGSUSED*/
388static Bool
389x68kCursorOffScreen(ScreenPtr *pScreen, int *x, int *y)
390{
391    return FALSE;
392}
393
394static void
395x68kCrossScreen(ScreenPtr pScreen, int entering)
396{
397}
398
399static void
400x68kWarpCursor(DeviceIntPtr device, ScreenPtr pScreen, int x, int y)
401{
402    input_lock();
403    miPointerWarpCursor(device, pScreen, x, y);
404    input_unlock();
405}
406
407/* EOF x68kMouse.c */
408