summaryrefslogtreecommitdiffstats
path: root/x11-drivers/xf86-input-mouse/files/patch-src-bsd_mouse.c
blob: b1da04c548d7010acd10eef15f9344d1e84b887d (plain) (blame)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
--- src/bsd_mouse.c.orig    2008-11-26 23:11:36.000000000 -0500
+++ src/bsd_mouse.c 2009-04-07 17:10:17.000000000 -0400
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (c) 1999-2003 by The XFree86 Project, Inc.
  *
@@ -71,15 +70,20 @@
 static const char *FindDevice(InputInfoPtr, const char *, int);
 
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
+#if !defined(XPS2_SUPPORT) && (__FreeBSD_kernel_version >= 700106)
+#define XPS2_SUPPORT
+#endif
 /* These are for FreeBSD and DragonFly */
 #define DEFAULT_MOUSE_DEV      "/dev/mouse"
 #define DEFAULT_SYSMOUSE_DEV       "/dev/sysmouse"
 #define DEFAULT_PS2_DEV            "/dev/psm0"
+#define DEFAULT_USB_DEV            "/dev/ums0"
 
 static const char *mouseDevs[] = {
    DEFAULT_MOUSE_DEV,
    DEFAULT_SYSMOUSE_DEV,
    DEFAULT_PS2_DEV,
+   DEFAULT_USB_DEV,
    NULL
 };
 #elif (defined(__OpenBSD__) || defined(__NetBSD__)) && defined(WSCONS_SUPPORT)
@@ -100,7 +104,11 @@
 #if defined(__NetBSD__)
     return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_AUTO;
 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
-    return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_AUTO | MSE_MISC;
+    return MSE_SERIAL | MSE_BUS | MSE_PS2 |
+#ifdef XPS2_SUPPORT
+   MSE_XPS2 |
+#endif
+   MSE_AUTO | MSE_MISC;
 #else
     return MSE_SERIAL | MSE_BUS | MSE_PS2 | MSE_XPS2 | MSE_AUTO;
 #endif
@@ -179,10 +187,31 @@
    { MOUSE_PROTO_THINK,        "ThinkingMouse" },
    { MOUSE_PROTO_SYSMOUSE,     "SysMouse" }
 };
-   
+
+#ifdef XPS2_SUPPORT
+static struct {
+   int dmodel;
+   char *name;
+} ps2proto[] = {
+   { MOUSE_MODEL_NETSCROLL,    "NetScrollPS/2" },
+   { MOUSE_MODEL_NET,      "NetMousePS/2" },
+   { MOUSE_MODEL_GLIDEPOINT,   "GlidePointPS/2" },
+   { MOUSE_MODEL_THINK,        "ThinkingMousePS/2" },
+   { MOUSE_MODEL_INTELLI,      "IMPS/2" },
+   { MOUSE_MODEL_MOUSEMANPLUS, "MouseManPlusPS/2" },
+   { MOUSE_MODEL_EXPLORER,     "ExplorerPS/2" },
+   { MOUSE_MODEL_4D,       "IMPS/2" },
+   { MOUSE_MODEL_4DPLUS,       "IMPS/2" },
+};
+#endif
+
 static const char *
 SetupAuto(InputInfoPtr pInfo, int *protoPara)
 {
+#ifdef XPS2_SUPPORT
+    char *dev;
+#endif
+    const char *proto;
     int i;
     mousehw_t hw;
     mousemode_t mode;
@@ -190,10 +219,16 @@
     if (pInfo->fd == -1)
    return NULL;
 
+#ifdef XPS2_SUPPORT
     /* set the driver operation level, if applicable */
+    dev = xf86FindOptionValue(pInfo->options, "Device");
+    if (dev != NULL && !strncmp(dev, DEFAULT_PS2_DEV, 8))
+   i = 2;
+    else
+#endif
     i = 1;
     ioctl(pInfo->fd, MOUSE_SETLEVEL, &i);
-    
+
     /* interrogate the driver and get some intelligence on the device. */
     hw.iftype = MOUSE_IF_UNKNOWN;
     hw.model = MOUSE_MODEL_GENERIC;
@@ -209,9 +244,18 @@
            protoPara[0] = mode.syncmask[0];
            protoPara[1] = mode.syncmask[1];
        }
+       proto = devproto[i].name;
+#ifdef XPS2_SUPPORT
+       if (mode.protocol == MOUSE_PROTO_PS2)
+           for (i = 0; i < sizeof(ps2proto)/sizeof(ps2proto[0]); ++i)
+           if (hw.model == ps2proto[i].dmodel) {
+               proto = ps2proto[i].name;
+               break;
+           }
+#endif
        xf86MsgVerb(X_INFO, 3, "%s: SetupAuto: protocol is %s\n",
-               pInfo->name, devproto[i].name);
-       return devproto[i].name;
+               pInfo->name, proto);
+       return proto;
        }
    }
     }
@@ -234,41 +278,41 @@
    (protocol && xf86NameCmp(protocol, "SysMouse") == 0)) {
    /*
     * As the FreeBSD sysmouse driver defaults to protocol level 0
-    * everytime it is opened we enforce protocol level 1 again at
+    * everytime it is closed we enforce protocol level 1 again at
     * this point.
     */
    mode.level = 1;
     } else
-   mode.level = -1;
-#else
-    mode.level = -1;
 #endif
+    mode.level = -1;
     ioctl(pInfo->fd, MOUSE_SETMODE, &mode);
 }
 #endif
 
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
-
-#define MOUSED_PID_FILE "/var/run/moused.pid"
-
 /*
  * Try to check if moused is running.  DEFAULT_SYSMOUSE_DEV is useless without
- * it.  There doesn't seem to be a better way of checking.
+ * it.  Also, try to check if the device is used by moused.  If it is opened
+ * by moused, we do not want to use it directly.  There doesn't seem to be
+ * a better way of checking.
  */
 static Bool
-MousedRunning(void)
+MousedRunning(const char *dev)
 {
+    char cmd[128];
     FILE *f = NULL;
-    unsigned int pid;
+    unsigned int i;
 
-    if ((f = fopen(MOUSED_PID_FILE, "r")) != NULL) {
-   if (fscanf(f, "%u", &pid) == 1 && pid > 0) {
-       if (kill(pid, 0) == 0) {
-       fclose(f);
-       return TRUE;
-       }
+    if (dev)
+   sprintf(cmd, "sh -c 'fstat %s | grep -c moused' 2>/dev/null", dev);
+    else
+   sprintf(cmd, "sh -c 'pgrep -nx moused' 2>/dev/null");
+    if ((f = popen(cmd, "r")) != NULL) {
+   if (fscanf(f, "%u", &i) == 1 && i > 0) {
+       pclose(f);
+       return TRUE;
    }
-   fclose(f);
+   pclose(f);
     }
     return FALSE;
 }
@@ -276,17 +320,17 @@
 static const char *
 FindDevice(InputInfoPtr pInfo, const char *protocol, int flags)
 {
-    int fd = -1;
+    int ret = -1;
     const char **pdev, *dev = NULL;
     Bool devMouse = FALSE;
     struct stat devMouseStat;
     struct stat sb;
 
     for (pdev = mouseDevs; *pdev; pdev++) {
-   SYSCALL (fd = open(*pdev, O_RDWR | O_NONBLOCK));
-   if (fd == -1) {
+   SYSCALL (ret = stat(*pdev, &sb));
+   if (ret == -1) {
 #ifdef DEBUG
-       ErrorF("Cannot open %s (%s)\n", *pdev, strerror(errno));
+       ErrorF("Cannot stat %s (%s)\n", *pdev, strerror(errno));
 #endif
    } else {
        /*
@@ -295,28 +339,32 @@
         * the test for whether /dev/sysmouse is usable can be made.
         */
        if (!strcmp(*pdev, DEFAULT_MOUSE_DEV)) {
-       if (fstat(fd, &devMouseStat) == 0)
-           devMouse = TRUE;
-       close(fd);
+       memcpy(&devMouseStat, &sb, sizeof(devMouseStat));
+       devMouse = TRUE;
        continue;
        } else if (!strcmp(*pdev, DEFAULT_SYSMOUSE_DEV)) {
        /* Check if /dev/mouse is the same as /dev/sysmouse. */
-       if (devMouse && fstat(fd, &sb) == 0 && 
-           devMouseStat.st_dev == sb.st_dev &&
+       if (devMouse && devMouseStat.st_dev == sb.st_dev &&
            devMouseStat.st_ino == sb.st_ino) {
            /* If the same, use /dev/sysmouse. */
            devMouse = FALSE;
        }
-       close(fd);
-       if (MousedRunning())
+       if (MousedRunning(NULL))
            break;
-       else {
-#ifdef DEBUG
-               ErrorF("moused isn't running\n");
-#endif
-       }
        } else {
-       close(fd);
+       /* Check if /dev/mouse is the same as this device. */
+       if (devMouse && devMouseStat.st_dev == sb.st_dev &&
+           devMouseStat.st_ino == sb.st_ino) {
+           /* If the same, use this device. */
+           devMouse = FALSE;
+       }
+       if (MousedRunning(*pdev))
+           continue;
+       /* ums(4) does not support anything but SysMouse protocol. */
+       if (!strncmp(*pdev, DEFAULT_USB_DEV, 8) && protocol &&
+           xf86NameCmp(protocol, "auto") != 0 &&
+           xf86NameCmp(protocol, "sysmouse") != 0)
+           continue;
        break;
        }
    }
@@ -782,7 +830,9 @@
     p->CheckProtocol = CheckProtocol;
 #if (defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)) && defined(MOUSE_PROTO_SYSMOUSE)
     p->SetupAuto = SetupAuto;
+#ifndef XPS2_SUPPORT
     p->SetPS2Res = SetSysMouseRes;
+#endif
     p->SetBMRes = SetSysMouseRes;
     p->SetMiscRes = SetSysMouseRes;
 #endif