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
|
/*
* 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.
*
* Author: Paulo Zanoni <pzanoni@mandriva.com>
*/
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/XShm.h>
#include <xorg/xf86.h>
#include "client.h"
#include "xNestedMouse.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;
void* dev;
};
/* Checks if a display is open */
Bool
NestedClientCheckDisplay(char *displayName) {
Display *d;
d = XOpenDisplay(displayName);
if (!d) {
return FALSE;
} else {
XCloseDisplay(d);
return TRUE;
}
}
Bool
NestedClientValidDepth(int depth) {
/* XXX: implement! */
return TRUE;
}
NestedClientPrivatePtr
NestedClientCreateScreen(int scrnIndex,
char *displayName,
int width,
int height,
int originX,
int originY,
int depth,
int bitsPerPixel,
Pixel *retRedMask,
Pixel *retGreenMask,
Pixel *retBlueMask) {
NestedClientPrivatePtr pPriv;
XSizeHints sizeHints;
int shmMajor, shmMinor;
Bool hasSharedPixmaps;
pPriv = malloc(sizeof(struct NestedClientPrivate));
pPriv->scrnIndex = scrnIndex;
pPriv->display = XOpenDisplay(displayName);
if (!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);
XStoreName(pPriv->display, pPriv->window, "Title");
XMapWindow(pPriv->display, pPriv->window);
XSelectInput(pPriv->display, pPriv->window, ExposureMask |
PointerMotionMask | EnterWindowMask | LeaveWindowMask | ButtonPressMask |
ButtonReleaseMask | KeyPressMask |KeyReleaseMask);
if (XShmQueryExtension(pPriv->display)) {
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)
return NULL;
/* 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) {
XDestroyImage(pPriv->img);
return NULL;
}
pPriv->shminfo.shmaddr = (char *)shmat(pPriv->shminfo.shmid, NULL, 0);
if (pPriv->shminfo.shmaddr == (char *) -1) {
XDestroyImage(pPriv->img);
return NULL;
}
pPriv->img->data = pPriv->shminfo.shmaddr;
pPriv->shminfo.readOnly = FALSE;
XShmAttach(pPriv->display, &pPriv->shminfo);
pPriv->usingShm = TRUE;
} else {
xf86DrvMsg(scrnIndex, X_INFO, "XShm not supported\n");
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 = 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
NestedClientTimerCallback(NestedClientPrivatePtr pPriv) {
XEvent ev;
char *msg = "Cursor";
char *msg2 = "Root";
while(XCheckMaskEvent(pPriv->display, ~0, &ev)) {
if (ev.type == Expose) {
NestedClientUpdateScreen(pPriv,
((XExposeEvent*)&ev)->x,
((XExposeEvent*)&ev)->y,
((XExposeEvent*)&ev)->x +
((XExposeEvent*)&ev)->width,
((XExposeEvent*)&ev)->y +
((XExposeEvent*)&ev)->height);
}
if (ev.type == MotionNotify) {
int x = ((XMotionEvent*)&ev)->x;
int y = ((XMotionEvent*)&ev)->y;
NestedPostMouseMotion(pPriv->dev, x, y);
/*
XDrawString(pPriv->display, pPriv->window,
DefaultGC(pPriv->display, pPriv->screenNumber),
((XMotionEvent*)&ev)->x, ((XMotionEvent*)&ev)->y,
msg, strlen(msg));
XDrawString(pPriv->display, pPriv->window,
DefaultGC(pPriv->display, pPriv->screenNumber),
((XMotionEvent*)&ev)->x_root,
((XMotionEvent*)&ev)->y_root, msg2, strlen(msg2));
*/
}
if (ev.type == EnterNotify) {
xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Cursor entered window!\n");
}
if (ev.type == LeaveNotify) {
xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Cursor left window!\n");
}
if (ev.type == ButtonPress) {
switch (ev.xbutton.button) {
case Button1: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Left Mouse Button Pressed\n");
NestedPostMouseButton(pPriv->dev, 1, TRUE);
break;
case Button2: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Middle Mouse Button Pressed\n");
break;
case Button3: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Right Mouse Button Pressed\n");
break;
case Button4: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Wheel Up Pressed\n");
break;
case Button5: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Wheel Down Pressed\n");
}
}
if (ev.type == ButtonRelease) {
switch (ev.xbutton.button) {
case Button1: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Left Mouse Button Released\n");
NestedPostMouseButton(pPriv->dev, 1, FALSE);
break;
case Button2: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Middle Mouse Button Released\n");
break;
case Button3: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Right Mouse Button Released\n");
break;
case Button4: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Wheel Up Released\n");
break;
case Button5: xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Wheel Down Released\n");
}
}
if (ev.type == KeyPress) {
xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Key Pressed!--%d\n",ev.xkey.keycode);
}
if (ev.type == KeyRelease) {
xf86DrvMsg(pPriv->scrnIndex, X_INFO, "Key Released!--%d\n",ev.xkey.keycode);
}
}
}
void
NestedClientCloseScreen(NestedClientPrivatePtr pPriv) {
if (pPriv->usingShm) {
XShmDetach(pPriv->display, &pPriv->shminfo);
shmdt(pPriv->shminfo.shmaddr);
} else {
free(pPriv->img->data);
}
XDestroyImage(pPriv->img);
XCloseDisplay(pPriv->display);
}
void NestedClientSetDevicePtr(NestedClientPrivatePtr pPriv, void *dev) {
pPriv->dev = dev;
}
|