aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/hw/xfree86/os-support/nto/nto_mouse.c
blob: 2dda8aa320a09afcfbffbc9cef733f996a796fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Written by Frank Liu Oct 10, 2001
 */
/* $XFree86: xc/programs/Xserver/hw/xfree86/os-support/nto/nto_mouse.c,v 1.1 2001/11/16 16:47:56 dawes Exp $ */


#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif

#include <X11/X.h>
#include "xf86.h"
#include "xf86Xinput.h"
#include "xf86OSmouse.h"

/* copied from mipointer.h */
extern int miPointerGetMotionEvents(
    DeviceIntPtr /*pPtr*/,
    xTimecoord * /*coords*/,
    unsigned long /*start*/,
    unsigned long /*stop*/,
    ScreenPtr /*pScreen*/
);

#include <sys/dcmd_input.h>
#define NUMEVENTS    64   /* don't want to stuck in the mouse read loop */

/*
 * OsMouseReadInput --
 *      Get some events from our queue.  Process outstanding events now.
 */
static void
OsMouseReadInput(InputInfoPtr pInfo)
{
        int n = 0;
	int buttons, col, row;
        struct _mouse_packet mp;
	MouseDevPtr pMse;

	pMse = pInfo->private;

        while ( (read(pInfo->fd, &mp, sizeof(struct _mouse_packet)) > 0 )
                && (n < NUMEVENTS ) ) 
        {
              col = mp.dx;
              row = -mp.dy;
              buttons = mp.hdr.buttons;
              pMse->PostEvent(pInfo, buttons, col, row, 0, 0);
              n++;
        }
}

/*
 * OsMouseProc --
 *      Handle the initialization, etc. of a mouse
 */
static int
OsMouseProc(pPointer, what)
DeviceIntPtr pPointer;
int what;
{
	int nbuttons;
	unsigned char map[MSE_MAXBUTTONS + 1];
        MouseDevPtr pMse;
        InputInfoPtr pInfo;

        pInfo = pPointer->public.devicePrivate;
        pMse = pInfo->private;
        pMse->device = pPointer;

	switch (what) {
	case DEVICE_INIT:
		pPointer->public.on = FALSE;

                for (nbuttons = 0; nbuttons < MSE_MAXBUTTONS; ++nbuttons)
                    map[nbuttons + 1] = nbuttons + 1;

                InitPointerDeviceStruct((DevicePtr)pPointer,
                                        map,
                                        min(pMse->buttons, MSE_MAXBUTTONS),
                                        miPointerGetMotionEvents,
                                        pMse->Ctrl,
                                        miPointerGetMotionBufferSize());

                /* X valuator */
                xf86InitValuatorAxisStruct(pPointer, 0, 0, -1, 1, 0, 1);
                xf86InitValuatorDefaults(pPointer, 0);
                /* Y valuator */
                xf86InitValuatorAxisStruct(pPointer, 1, 0, -1, 1, 0, 1);
                xf86InitValuatorDefaults(pPointer, 1);
                xf86MotionHistoryAllocate(pInfo);
                break;

	case DEVICE_ON:
                pInfo->fd = xf86OpenSerial(pInfo->options);
                if (pInfo->fd == -1)
                    xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
                else {
                    AddEnabledDevice(pInfo->fd);
                }
                pMse->lastButtons = 0;
                pMse->lastMappedButtons = 0;
                pMse->emulateState = 0;
                pPointer->public.on = TRUE;
                break;

	case DEVICE_CLOSE:
	case DEVICE_OFF:
                if (pInfo->fd != -1) {
                    RemoveEnabledDevice(pInfo->fd);
                    xf86CloseSerial(pInfo->fd);
                    pInfo->fd = -1;
                }
                pPointer->public.on = FALSE;
                break;
	}
	return (Success);
}				

static int
SupportedInterfaces(void)
{
  /* FIXME: Is this correct? Should we just return MSE_MISC? */
  return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_XPS2 | MSE_MISC | MSE_AUTO;
}

static const char *internalNames[] = {
        "OSMouse",
        NULL
};

static const char **
BuiltinNames(void)
{
    return internalNames;
}

static Bool
CheckProtocol(const char *protocol)
{
    int i;

    for (i = 0; internalNames[i]; i++)
        if (xf86NameCmp(protocol, internalNames[i]) == 0)
            return TRUE;
    return FALSE;
}

/* XXX Is this appropriate?  If not, this function should be removed. */
static const char *
DefaultProtocol(void)
{
    return "OSMouse";
}

static Bool
OsMousePreInit(InputInfoPtr pInfo, const char *protocol, int flags)
{
    MouseDevPtr pMse;

    /* This is called when the protocol is "OSMouse". */

    pMse = pInfo->private;
    pMse->protocol = protocol;
    xf86Msg(X_CONFIG, "%s: Protocol: %s\n", pInfo->name, protocol);

    /* Collect the options, and process the common options. */
    xf86CollectInputOptions(pInfo, NULL, NULL);
    xf86ProcessCommonOptions(pInfo, pInfo->options);

    /* Check if the device can be opened. */
    pInfo->fd = xf86OpenSerial(pInfo->options);
    if (pInfo->fd == -1) {
        if (xf86GetAllowMouseOpenFail())
            xf86Msg(X_WARNING, "%s: cannot open input device\n", pInfo->name);
        else {
            xf86Msg(X_ERROR, "%s: cannot open input device\n", pInfo->name);
            xfree(pMse);
            return FALSE;
        }
    }
    xf86CloseSerial(pInfo->fd);
    pInfo->fd = -1;

    /* Process common mouse options (like Emulate3Buttons, etc). */
    pMse->CommonOptions(pInfo);

    /* Setup the local procs. */
    pInfo->device_control = OsMouseProc;
    pInfo->read_input = OsMouseReadInput;

    pInfo->flags |= XI86_CONFIGURED;
    return TRUE;
}

OSMouseInfoPtr
xf86OSMouseInit(int flags)
{
    OSMouseInfoPtr p;

    p = xcalloc(sizeof(OSMouseInfoRec), 1);
    if (!p)
        return NULL;
    p->SupportedInterfaces = SupportedInterfaces;
    p->BuiltinNames = BuiltinNames;
    p->DefaultProtocol = DefaultProtocol;
    p->CheckProtocol = CheckProtocol;
    p->PreInit = OsMousePreInit;
    return p;
}