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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/* $XFree86$ */
/*
* Copyright 2001,2002 Red Hat Inc., Durham, North Carolina.
*
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation on the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Authors:
* David H. Dawes <dawes@xfree86.org>
* Kevin E. Martin <kem@redhat.com>
* Rickard E. (Rik) Faith <faith@redhat.com>
*/
#define DMX_BACKEND_DEBUG 0
/* Core devices on the back end X server -- like Xnest. */
#include "inputstr.h"
#include "input.h"
#include "keysym.h"
#include "mipointer.h"
#include "mi.h"
#include "scrnintstr.h"
#include "dmxinputinit.h"
#include "dmxevents.h"
#include "dmxbackend.h"
#include "dmxcommon.h"
#include "dmxcursor.h"
#include "dmxprop.h"
#include "globals.h"
#include "XInput.h"
typedef struct _myPrivate {
DMX_COMMON_PRIVATE;
int myScreen;
int lastX;
int lastY;
int screen;
} myPrivate;
pointer dmxBackendCreatePrivate(DMXInputInfo *dmxInput)
{
myPrivate *priv = xalloc(sizeof(*priv));
memset(priv, 0, sizeof(*priv));
priv->dmxInput = dmxInput;
return priv;
}
void dmxBackendDestroyPrivate(pointer private)
{
if (private) xfree(private);
}
static void *dmxBackendTestScreen(DMXScreenInfo *dmxScreen, void *closure)
{
int target = (int)closure;
if (dmxScreen->index == target) return dmxScreen;
return NULL;
}
/* Return non-zero if priv->screen and priv->myScreen are on the same
* physical backend display. */
static int dmxBackendSameDisplay(myPrivate *priv)
{
DMXScreenInfo *dmxScreen;
if (priv->screen == priv->myScreen) return 1;
if (priv->screen < 0 || priv->screen >= dmxNumScreens) return 0;
if ((dmxScreen = dmxPropertyIterate(priv->backendScreen,
dmxBackendTestScreen,
(void *)priv->screen))) {
return 1;
}
return 0;
}
static void *dmxBackendTestEvents(DMXScreenInfo *dmxScreen, void *closure)
{
if (XPending(dmxScreen->display)) return dmxScreen;
return NULL;
}
static void *dmxBackendTestMotionEvent(DMXScreenInfo *dmxScreen, void *closure)
{
XEvent N;
if (XPending(dmxScreen->display)) {
XPeekEvent(dmxScreen->display, &N);
if (N.type == MotionNotify) return dmxScreen;
}
return NULL;
}
static DMXScreenInfo *dmxBackendGetEvent(myPrivate *priv, XEvent *X)
{
DMXScreenInfo *dmxScreen;
if ((dmxScreen = dmxPropertyIterate(priv->backendScreen,
dmxBackendTestEvents, NULL))) {
XNextEvent(dmxScreen->display, X);
return dmxScreen;
}
return NULL;
}
static DMXScreenInfo *dmxBackendPendingMotionEvent(myPrivate *priv)
{
DMXScreenInfo *dmxScreen;
if ((dmxScreen = dmxPropertyIterate(priv->backendScreen,
dmxBackendTestMotionEvent, NULL))) {
return dmxScreen;
}
return NULL;
}
static void *dmxBackendTestWindow(DMXScreenInfo *dmxScreen, void *closure)
{
Window win = (Window)closure;
if (dmxScreen->window == win) return dmxScreen;
return NULL;
}
static DMXScreenInfo *dmxBackendFindWindow(myPrivate *priv, Window win)
{
return dmxPropertyIterate(priv->backendScreen,
dmxBackendTestWindow, (void *)win);
}
/* This routine is called from dmxAbsoluteMotion for each motion event. */
void dmxBackendUpdatePosition(pointer private, int x, int y)
{
GETONLYPRIVFROMPRIVATE;
int screen;
DMXScreenInfo *dmxScreen;
#if DMX_BACKEND_DEBUG
dmxLog(dmxDebug, "dmxBackendUpdatePosition(,%d,%d)\n", x, y);
#endif
if (priv->screen != (screen = miPointerCurrentScreen()->myNum)) {
#if DMX_BACKEND_DEBUG
dmxLog(dmxDebug,
"dmxBackendUpdatePostion: Moved from screen %d to %d\n",
priv->screen, screen);
#endif
priv->screen = screen;
dmxScreen = &dmxScreens[priv->screen];
if (dmxBackendSameDisplay(priv)) {
priv->myScreen = priv->screen;
} else {
priv->lastX = priv->width / 2;
priv->lastY = priv->height / 2;
XWarpPointer(priv->display, None, priv->window,
0, 0, 0, 0, priv->lastX, priv->lastY);
XSync(priv->display, False);
}
}
}
void dmxBackendCollectEvents(pointer private)
{
GETPRIVFROMPRIVATE;
XEvent X;
DMXScreenInfo *dmxScreen;
int enterred = 0;
int left = 0;
while ((dmxScreen = dmxBackendGetEvent(priv, &X))) {
switch (X.type) {
case EnterNotify:
if (enterred++) continue;
#if DMX_BACKEND_DEBUG
dmxLog(dmxDebug,
"dmxBackendCollectEvents: Enter %lu\n", dmxScreen->window);
#endif
XRaiseWindow(dmxScreen->display, dmxScreen->window);
XGrabPointer(dmxScreen->display, dmxScreen->window,
True, 0, GrabModeAsync, GrabModeAsync, None,
None, CurrentTime);
break;
case LeaveNotify:
if (left++) continue;
#if DMX_BACKEND_DEBUG
dmxLog(dmxDebug,
"dmxBackendCollectEvents: Leave %lu\n", dmxScreen->window);
#endif
XUngrabPointer(dmxScreen->display, CurrentTime);
break;
case MotionNotify:
#if DMX_BACKEND_DEBUG
dmxLog(dmxDebug,
"dmxBackendCollectEvents: MotionNotify from screen %d:"
" %d %d win=%lu/%lu\n",
dmxScreen->index, X.xmotion.x, X.xmotion.y,
X.xmotion.window, dmxScreen->window);
#endif
if (dmxBackendPendingMotionEvent(priv)) continue;
if (!(dmxScreen = dmxBackendFindWindow(priv, X.xmotion.window)))
dmxLog(dmxFatal,
"Event on non-existant window %lu\n", X.xmotion.window);
if (priv->screen == priv->myScreen) {
int newX = X.xmotion.x;
int newY = X.xmotion.y;
if (!newX) newX = -1;
if (newX == priv->width - 1) newX = priv->width;
if (!newY) newY = -1;
if (newY == priv->height - 1) newY = priv->height;
dmxAbsoluteMotion(dmxScreen->originX + newX,
dmxScreen->originY + newY);
} else {
dmxRelativeMotion(dmxScreen->originX
+ priv->lastX - X.xmotion.x,
dmxScreen->originY
+ priv->lastY - X.xmotion.y);
priv->lastX = X.xmotion.x;
priv->lastY = X.xmotion.y;
}
break;
default:
dmxEnqueueXEvent(dmxInput, &X);
break;
}
}
}
void dmxBackendProcessInput(pointer private)
{
GETONLYPRIVFROMPRIVATE;
if (!dmxBackendSameDisplay(priv)
&& (priv->lastX != priv->width / 2
|| priv->lastY != priv->height / 2)) {
priv->lastX = priv->width / 2;
priv->lastY = priv->height / 2;
XWarpPointer(priv->display, None, priv->window,
0, 0, 0, 0, priv->lastX, priv->lastY);
XSync(priv->display, False);
}
}
void dmxBackendInitialize(pointer private)
{
GETPRIVFROMPRIVATE;
XExtensionVersion *ext;
XDeviceInfo *devices;
DMXScreenInfo *dmxScreen;
int num;
int i;
/* Fill in myPrivate */
for (i = 0,dmxScreen = &dmxScreens[0]; i<dmxNumScreens; i++,dmxScreen++) {
if (!strcmp(dmxInput->name, dmxScreen->name)) {
priv->display = dmxScreen->display;
priv->window = dmxScreen->window;
priv->width = dmxScreen->width;
priv->height = dmxScreen->height;
priv->backendScreen = dmxScreen;
break;
}
}
if (i >= dmxNumScreens)
dmxLog(dmxFatal,
"%s is not an existing backend display - cannot initialize\n",
dmxInput->name);
/* Finish dmxInput initialization using computed values or constants. */
priv->initPointerX = priv->width / 2;
priv->initPointerY = priv->height / 2;
priv->eventMask = (EnterWindowMask|LeaveWindowMask);
priv->myScreen = dmxScreen->index;
priv->lastX = priv->width / 2; /* FIXME? put cursor here? */
priv->lastY = priv->height / 2;
priv->screen = priv->myScreen;
/* Print out information about the XInput Extension. */
ext = XGetExtensionVersion(priv->display, INAME);
if (ext && ext != (XExtensionVersion *)NoSuchExtension) {
dmxLogInput(dmxInput, "%s version %d.%d\n",
INAME, ext->major_version, ext->minor_version);
devices = XListInputDevices(priv->display, &num);
if (num)
dmxLogInput(dmxInput, "Extended devices:\n");
for (i = 0; i < num; i++) {
const char *kind = "Unknown";
switch (devices[i].use) {
case IsXPointer: kind = "XPointer"; break;
case IsXKeyboard: kind = "XKeyboard"; break;
case IsXExtensionDevice: kind = "XExtensionDevice"; break;
}
dmxLogInput(dmxInput, " \"%s\": %s\n",
devices[i].name ? devices[i].name : "<noname>", kind);
}
XFreeDeviceList(devices);
}
}
int dmxBackendFunctions(pointer private, DMXFunctionType function)
{
switch (function) {
case DMX_FUNCTION_TERMINATE:
return 1;
default:
return 0;
}
}
|