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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
/*
* 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
* 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 NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS 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:
*
* Paulo Zanoni <pzanoni@mandriva.com>
* Tuan Bui <tuanbui918@gmail.com>
* Colin Cornaby <colin.cornaby@mac.com>
* Timothy Fleck <tim.cs.pdx@gmail.com>
* Colin Hill <colin.james.hill@gmail.com>
* Weseung Hwang <weseung@gmail.com>
* Nathaniel Way <nathanielcw@hotmail.com>
*/
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XShm.h>
#include <xorg-server.h>
#include <xf86.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "client.h"
#include "nested_input.h"
struct NestedClientPrivate {
Display *display;
int screenNumber;
Screen *screen;
Window rootWindow;
Window window;
XImage *img;
GC gc;
Bool usingShm;
XShmSegmentInfo shminfo;
int scrnIndex; /* stored only for xf86DrvMsg usage */
Cursor mycursor; /* Test cursor */
Pixmap bitmapNoData;
XColor color1;
DeviceIntPtr dev; // The pointer to the input device. Passed back to the
// input driver when posting input events.
struct {
int op;
int event;
int error;
int major;
int minor;
} xkb;
};
/* Checks if a display is open */
Bool
NestedClientCheckDisplay(const char *displayName) {
Display *d;
d = XOpenDisplay(displayName);
if (!d) {
return FALSE;
} else {
XCloseDisplay(d);
return TRUE;
}
}
Bool
NestedClientValidDepth(int depth) {
/* XXX: implement! */
return TRUE;
}
static Bool
NestedClientTryXShm(NestedClientPrivatePtr pPriv, int scrnIndex, int width, int height, int depth) {
int shmMajor, shmMinor;
Bool hasSharedPixmaps;
if (!XShmQueryExtension(pPriv->display)) {
xf86DrvMsg(scrnIndex, X_INFO, "XShmQueryExtension failed. Dropping XShm support.\n");
return FALSE;
}
if (XShmQueryVersion(pPriv->display, &shmMajor, &shmMinor,
&hasSharedPixmaps)) {
xf86DrvMsg(scrnIndex, X_INFO,
"XShm extension version %d.%d %s shared pixmaps\n",
shmMajor, shmMinor, (hasSharedPixmaps) ? "with" : "without");
}
pPriv->img = XShmCreateImage(pPriv->display,
DefaultVisualOfScreen(pPriv->screen),
depth,
ZPixmap,
NULL, /* data */
&pPriv->shminfo,
width,
height);
if (!pPriv->img) {
xf86DrvMsg(scrnIndex, X_ERROR, "XShmCreateImage failed. Dropping XShm support.\n");
return FALSE;
}
/* XXX: change the 0777 mask? */
pPriv->shminfo.shmid = shmget(IPC_PRIVATE,
pPriv->img->bytes_per_line *
pPriv->img->height,
IPC_CREAT | 0777);
if (pPriv->shminfo.shmid == -1) {
xf86DrvMsg(scrnIndex, X_ERROR, "shmget failed. Dropping XShm support.\n");
XDestroyImage(pPriv->img);
return FALSE;
}
pPriv->shminfo.shmaddr = (char *)shmat(pPriv->shminfo.shmid, NULL, 0);
if (pPriv->shminfo.shmaddr == (char *) -1) {
xf86DrvMsg(scrnIndex, X_ERROR, "shmaddr failed. Dropping XShm support.\n");
XDestroyImage(pPriv->img);
return FALSE;
}
pPriv->img->data = pPriv->shminfo.shmaddr;
pPriv->shminfo.readOnly = FALSE;
XShmAttach(pPriv->display, &pPriv->shminfo);
pPriv->usingShm = TRUE;
return TRUE;
}
NestedClientPrivatePtr
NestedClientCreateScreen(int scrnIndex,
const char *displayName,
int width,
int height,
int originX,
int originY,
int depth,
int bitsPerPixel,
Pixel *retRedMask,
Pixel *retGreenMask,
Pixel *retBlueMask) {
NestedClientPrivatePtr pPriv;
XSizeHints sizeHints;
Bool supported;
char windowTitle[32];
pPriv = malloc(sizeof(struct NestedClientPrivate));
pPriv->scrnIndex = scrnIndex;
pPriv->display = XOpenDisplay(displayName);
if (!pPriv->display)
return NULL;
supported = XkbQueryExtension(pPriv->display, &pPriv->xkb.op, &pPriv->xkb.event,
&pPriv->xkb.error, &pPriv->xkb.major, &pPriv->xkb.minor);
if (!supported) {
xf86DrvMsg(pPriv->scrnIndex, X_ERROR, "The remote server does not support the XKEYBOARD extension.\n");
XCloseDisplay(pPriv->display);
return NULL;
}
pPriv->screenNumber = DefaultScreen(pPriv->display);
pPriv->screen = ScreenOfDisplay(pPriv->display, pPriv->screenNumber);
pPriv->rootWindow = RootWindow(pPriv->display, pPriv->screenNumber);
pPriv->gc = DefaultGC(pPriv->display, pPriv->screenNumber);
pPriv->window = XCreateSimpleWindow(pPriv->display, pPriv->rootWindow,
originX, originY, width, height, 0, 0, 0);
sizeHints.flags = PPosition | PSize | PMinSize | PMaxSize;
sizeHints.min_width = width;
sizeHints.max_width = width;
sizeHints.min_height = height;
sizeHints.max_height = height;
XSetWMNormalHints(pPriv->display, pPriv->window, &sizeHints);
snprintf(windowTitle, sizeof(windowTitle), "Screen %d", scrnIndex);
XStoreName(pPriv->display, pPriv->window, windowTitle);
XMapWindow(pPriv->display, pPriv->window);
XSelectInput(pPriv->display, pPriv->window, ExposureMask |
PointerMotionMask | EnterWindowMask | LeaveWindowMask |
ButtonPressMask | ButtonReleaseMask | KeyPressMask |
KeyReleaseMask);
if (!NestedClientTryXShm(pPriv, scrnIndex, width, height, depth)) {
pPriv->img = XCreateImage(pPriv->display,
DefaultVisualOfScreen(pPriv->screen),
depth,
ZPixmap,
0, /* offset */
NULL, /* data */
width,
height,
32, /* XXX: bitmap_pad */
0 /* XXX: bytes_per_line */);
if (!pPriv->img)
return NULL;
pPriv->img->data = malloc(pPriv->img->bytes_per_line * pPriv->img->height);
pPriv->usingShm = FALSE;
}
if (!pPriv->img->data)
return NULL;
NestedClientHideCursor(pPriv); /* Hide cursor */
#if 0
xf86DrvMsg(scrnIndex, X_INFO, "width: %d\n", pPriv->img->width);
xf86DrvMsg(scrnIndex, X_INFO, "height: %d\n", pPriv->img->height);
xf86DrvMsg(scrnIndex, X_INFO, "xoffset: %d\n", pPriv->img->xoffset);
xf86DrvMsg(scrnIndex, X_INFO, "depth: %d\n", pPriv->img->depth);
xf86DrvMsg(scrnIndex, X_INFO, "bpp: %d\n", pPriv->img->bits_per_pixel);
xf86DrvMsg(scrnIndex, X_INFO, "red_mask: 0x%lx\n", pPriv->img->red_mask);
xf86DrvMsg(scrnIndex, X_INFO, "gre_mask: 0x%lx\n", pPriv->img->green_mask);
xf86DrvMsg(scrnIndex, X_INFO, "blu_mask: 0x%lx\n", pPriv->img->blue_mask);
#endif
*retRedMask = pPriv->img->red_mask;
*retGreenMask = pPriv->img->green_mask;
*retBlueMask = pPriv->img->blue_mask;
XEvent ev;
while (1) {
XNextEvent(pPriv->display, &ev);
if (ev.type == Expose) {
break;
}
}
pPriv->dev = (DeviceIntPtr)NULL;
return pPriv;
}
void NestedClientHideCursor(NestedClientPrivatePtr pPriv) {
char noData[]= {0,0,0,0,0,0,0,0};
pPriv->color1.red = pPriv->color1.green = pPriv->color1.blue = 0;
pPriv->bitmapNoData = XCreateBitmapFromData(pPriv->display,
pPriv->window, noData, 7, 7);
pPriv->mycursor = XCreatePixmapCursor(pPriv->display,
pPriv->bitmapNoData, pPriv->bitmapNoData,
&pPriv->color1, &pPriv->color1, 0, 0);
XDefineCursor(pPriv->display, pPriv->window, pPriv->mycursor);
XFreeCursor(pPriv->display, pPriv->mycursor);
}
char *
NestedClientGetFrameBuffer(NestedClientPrivatePtr pPriv) {
return pPriv->img->data;
}
void
NestedClientUpdateScreen(NestedClientPrivatePtr pPriv, int16_t x1,
int16_t y1, int16_t x2, int16_t y2) {
if (pPriv->usingShm) {
XShmPutImage(pPriv->display, pPriv->window, pPriv->gc, pPriv->img,
x1, y1, x1, y1, x2 - x1, y2 - y1, FALSE);
/* Without this sync we get some freezes, probably due to some lock
* in the shm usage */
XSync(pPriv->display, FALSE);
} else {
XPutImage(pPriv->display, pPriv->window, pPriv->gc, pPriv->img,
x1, y1, x1, y1, x2 - x1, y2 - y1);
}
}
void
NestedClientCheckEvents(NestedClientPrivatePtr pPriv) {
XEvent ev;
while(XCheckMaskEvent(pPriv->display, ~0, &ev)) {
switch (ev.type) {
case Expose:
NestedClientUpdateScreen(pPriv,
((XExposeEvent*)&ev)->x,
((XExposeEvent*)&ev)->y,
((XExposeEvent*)&ev)->x +
((XExposeEvent*)&ev)->width,
((XExposeEvent*)&ev)->y +
((XExposeEvent*)&ev)->height);
break;
case MotionNotify:
if (!pPriv->dev) {
xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Input device is not yet initialized, ignoring input.\n");
break;
}
NestedInputPostMouseMotionEvent(pPriv->dev,
((XMotionEvent*)&ev)->x,
((XMotionEvent*)&ev)->y);
break;
case ButtonPress:
case ButtonRelease:
if (!pPriv->dev) {
xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Input device is not yet initialized, ignoring input.\n");
break;
}
NestedInputPostButtonEvent(pPriv->dev, ev.xbutton.button, ev.type == ButtonPress);
break;
case KeyPress:
case KeyRelease:
if (!pPriv->dev) {
xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Input device is not yet initialized, ignoring input.\n");
break;
}
NestedInputPostKeyboardEvent(pPriv->dev, ev.xkey.keycode, ev.type == KeyPress);
break;
}
}
}
void
NestedClientCloseScreen(NestedClientPrivatePtr pPriv) {
if (pPriv->usingShm) {
XShmDetach(pPriv->display, &pPriv->shminfo);
shmdt(pPriv->shminfo.shmaddr);
}
XDestroyImage(pPriv->img);
XCloseDisplay(pPriv->display);
}
void
NestedClientSetDevicePtr(NestedClientPrivatePtr pPriv, DeviceIntPtr dev) {
pPriv->dev = dev;
}
int
NestedClientGetFileDescriptor(NestedClientPrivatePtr pPriv) {
return ConnectionNumber(pPriv->display);
}
Bool NestedClientGetKeyboardMappings(NestedClientPrivatePtr pPriv, KeySymsPtr keySyms, CARD8 *modmap, XkbControlsPtr ctrls) {
XModifierKeymap *modifier_keymap;
KeySym *keymap;
int mapWidth;
int min_keycode, max_keycode;
int i, j;
XkbDescPtr xkb;
XDisplayKeycodes(pPriv->display, &min_keycode, &max_keycode);
keymap = XGetKeyboardMapping(pPriv->display,
min_keycode,
max_keycode - min_keycode + 1,
&mapWidth);
memset(modmap, 0, sizeof(CARD8) * MAP_LENGTH);
modifier_keymap = XGetModifierMapping(pPriv->display);
for (j = 0; j < 8; j++)
for(i = 0; i < modifier_keymap->max_keypermod; i++) {
CARD8 keycode;
if ((keycode = modifier_keymap->modifiermap[j * modifier_keymap->max_keypermod + i]))
modmap[keycode] |= 1<<j;
}
XFreeModifiermap(modifier_keymap);
keySyms->minKeyCode = min_keycode;
keySyms->maxKeyCode = max_keycode;
keySyms->mapWidth = mapWidth;
keySyms->map = keymap;
xkb = XkbGetKeyboard(pPriv->display, XkbGBN_AllComponentsMask, XkbUseCoreKbd);
if (xkb == NULL || xkb->geom == NULL) {
xf86DrvMsg(pPriv->scrnIndex, X_ERROR, "Couldn't get XKB keyboard.\n");
free(keymap);
return FALSE;
}
if(XkbGetControls(pPriv->display, XkbAllControlsMask, xkb) != Success) {
xf86DrvMsg(pPriv->scrnIndex, X_ERROR, "Couldn't get XKB keyboard controls.\n");
free(keymap);
return FALSE;
}
memcpy(ctrls, xkb->ctrls, sizeof(XkbControlsRec));
XkbFreeKeyboard(xkb, 0, False);
return TRUE;
}
|