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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
* nativewindow_x11.cc
*
* Fri Dec 28 18:45:57 CET 2012
* Copyright 2012 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of CamSync.
*
* CamSync is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CamSync is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CamSync; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "nativewindow_x11.h"
#ifdef X11
#include <X11/Xutil.h>
#include <stdlib.h>
#include "window.h"
GUI::NativeWindowX11::NativeWindowX11(GUI::Window *window)
: GUI::NativeWindow()
{
display = XOpenDisplay(NULL);
this->window = window;
buffer = NULL;
// Get some colors
int blackColor = BlackPixel(display, DefaultScreen(display));
::Window w = DefaultRootWindow(display);
// Create the window
xwindow = XCreateSimpleWindow(display,
w,
window->x(), window->y(),
window->width(), window->height(),
0,
blackColor, blackColor);
XSelectInput(display, xwindow,
StructureNotifyMask |
PointerMotionMask |
ButtonPressMask |
ButtonReleaseMask |
KeyPressMask |
KeyReleaseMask|
ExposureMask |
StructureNotifyMask |
SubstructureNotifyMask);
// register interest in the delete window message
wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", false);
XSetWMProtocols(display, xwindow, &wmDeleteMessage, 1);
// "Map" the window (that is, make it appear on the screen)
XMapWindow(display, xwindow);
// Create a "Graphics Context"
gc = XCreateGC(display, xwindow, 0, NULL);
}
GUI::NativeWindowX11::~NativeWindowX11()
{
XDestroyWindow(display, xwindow);
//widgets.erase(window);
XCloseDisplay(display);
}
void GUI::NativeWindowX11::setFixedSize(int width, int height)
{
resize(width, height);
XSizeHints *size_hints;
size_hints = XAllocSizeHints();
if(size_hints == NULL) {
//fprintf(stderr,"XMallocSizeHints() failed\n");
//exit(1);
return;
}
size_hints->flags = USPosition | PMinSize | PMaxSize;
size_hints->min_width = size_hints->max_width = width;
size_hints->min_height = size_hints->max_height = height;
/*
size_hints->min_aspect.x = window->width()/window->height();
size_hints->max_aspect.x = window->width()/window->height();
size_hints->min_aspect.y = window->width()/window->height();
size_hints->max_aspect.y = size_hints->min_aspect.y;
*/
XSetWMNormalHints(display, xwindow, size_hints);
}
void GUI::NativeWindowX11::resize(int width, int height)
{
XResizeWindow(display, xwindow, width, height);
}
void GUI::NativeWindowX11::move(int x, int y)
{
XMoveWindow(display, xwindow, x, y);
}
void GUI::NativeWindowX11::show()
{
XMapWindow(display, xwindow);
}
void GUI::NativeWindowX11::hide()
{
XUnmapWindow(display, xwindow);
}
static int get_byte_order (void)
{
union {
char c[sizeof(short)];
short s;
} order;
order.s = 1;
if ((1 == order.c[0])) {
return LSBFirst;
} else {
return MSBFirst;
}
}
static XImage *create_image_from_buffer(Display *dis, int screen,
unsigned char *buf,
int width, int height)
{
int depth;
XImage *img = NULL;
Visual *vis;
double rRatio;
double gRatio;
double bRatio;
int outIndex = 0;
int i;
int numBufBytes = (3 * (width * height));
depth = DefaultDepth(dis, screen);
vis = DefaultVisual(dis, screen);
rRatio = vis->red_mask / 255.0;
gRatio = vis->green_mask / 255.0;
bRatio = vis->blue_mask / 255.0;
if (depth >= 24) {
size_t numNewBufBytes = (4 * (width * height));
u_int32_t *newBuf = (u_int32_t *)malloc (numNewBufBytes);
for (i = 0; i < numBufBytes; ++i) {
unsigned int r, g, b;
r = (buf[i] * rRatio);
++i;
g = (buf[i] * gRatio);
++i;
b = (buf[i] * bRatio);
r &= vis->red_mask;
g &= vis->green_mask;
b &= vis->blue_mask;
newBuf[outIndex] = r | g | b;
++outIndex;
}
img = XCreateImage (dis,
CopyFromParent, depth,
ZPixmap, 0,
(char *) newBuf,
width, height,
32, 0
);
} else if (depth >= 15) {
size_t numNewBufBytes = (2 * (width * height));
u_int16_t *newBuf = (u_int16_t *)malloc (numNewBufBytes);
for (i = 0; i < numBufBytes; ++i) {
unsigned int r, g, b;
r = (buf[i] * rRatio);
++i;
g = (buf[i] * gRatio);
++i;
b = (buf[i] * bRatio);
r &= vis->red_mask;
g &= vis->green_mask;
b &= vis->blue_mask;
newBuf[outIndex] = r | g | b;
++outIndex;
}
img = XCreateImage(dis, CopyFromParent, depth, ZPixmap, 0, (char *) newBuf,
width, height, 16, 0);
} else {
//fprintf (stderr, "This program does not support displays with a depth less than 15.");
return NULL;
}
XInitImage (img);
/*Set the client's byte order, so that XPutImage knows what to do with the data.*/
/*The default in a new X image is the server's format, which may not be what we want.*/
if ((LSBFirst == get_byte_order ())) {
img->byte_order = LSBFirst;
} else {
img->byte_order = MSBFirst;
}
/*The bitmap_bit_order doesn't matter with ZPixmap images.*/
img->bitmap_bit_order = MSBFirst;
return img;
}
void GUI::NativeWindowX11::handleBuffer()
{
if(buffer) XDestroyImage(buffer);
buffer =
create_image_from_buffer(display, DefaultScreen(display),
window->wpixbuf.buf,
window->wpixbuf.width,
window->wpixbuf.height);
}
void GUI::NativeWindowX11::redraw()
{
// http://stackoverflow.com/questions/6384987/load-image-onto-a-window-using-xlib
if(buffer == NULL) window->updateBuffer();
XPutImage(display, xwindow, gc, buffer, 0, 0, 0, 0,
window->width(), window->height());
XFlush(display);
}
void GUI::NativeWindowX11::setCaption(const std::string &caption)
{
XStoreName(display, xwindow, caption.c_str());
}
void GUI::NativeWindowX11::grabMouse(bool grab)
{
(void)grab;
// Don't need to do anything on this platoform...
}
bool GUI::NativeWindowX11::hasEvent()
{
return XPending(display);
}
GUI::Event *GUI::NativeWindowX11::getNextEvent()
{
Event *event = NULL;
XEvent xe;
XNextEvent(display, &xe);
if(xe.type == MotionNotify) {
while(true) { // Hack to make sure only the last event is played.
if(!hasEvent()) break;
XEvent nxe;
XPeekEvent(display, &nxe);
if(nxe.type != MotionNotify) break;
XNextEvent(display, &xe);
}
MouseMoveEvent *e = new MouseMoveEvent();
e->window_id = xe.xmotion.window;
e->x = xe.xmotion.x;
e->y = xe.xmotion.y;
event = e;
}
if(xe.type == Expose && xe.xexpose.count == 0) {
RepaintEvent *e = new RepaintEvent();
e->window_id = xe.xexpose.window;
e->x = xe.xexpose.x;
e->y = xe.xexpose.y;
e->width = xe.xexpose.width;
e->height = xe.xexpose.height;
event = e;
}
if(xe.type == ConfigureNotify) {
ResizeEvent *e = new ResizeEvent();
e->window_id = xe.xconfigure.window;
// e->x = xe.xconfigure.x;
// e->y = xe.xconfigure.y;
e->width = xe.xconfigure.width;
e->height = xe.xconfigure.height;
event = e;
}
if(xe.type == ButtonPress || xe.type == ButtonRelease) {
if(xe.xbutton.button == 4 || xe.xbutton.button == 5) {
int scroll = 1;
while(true) { // Hack to make sure only the last event is played.
if(!hasEvent()) break;
XEvent nxe;
XPeekEvent(display, &nxe);
if(nxe.type != ButtonPress && nxe.type != ButtonRelease) break;
scroll += 1;
XNextEvent(display, &xe);
}
ScrollEvent *e = new ScrollEvent();
e->window_id = xe.xbutton.window;
e->x = xe.xbutton.x;
e->y = xe.xbutton.y;
e->delta = scroll * (xe.xbutton.button==4?-1:1);
event = e;
} else {
ButtonEvent *e = new ButtonEvent();
e->window_id = xe.xbutton.window;
e->x = xe.xbutton.x;
e->y = xe.xbutton.y;
e->button = 0;
e->direction = xe.type == ButtonPress?1:-1;
e->doubleclick =
xe.type == ButtonPress && (xe.xbutton.time - last_click) < 200;
if(xe.type == ButtonPress) last_click = xe.xbutton.time;
event = e;
}
}
if(xe.type == KeyPress || xe.type == KeyRelease) {
//printf("key: %d\n", xe.xkey.keycode);
KeyEvent *e = new KeyEvent();
e->window_id = xe.xkey.window;
switch(xe.xkey.keycode) {
case 113: e->keycode = KeyEvent::KEY_LEFT; break;
case 114: e->keycode = KeyEvent::KEY_RIGHT; break;
case 111: e->keycode = KeyEvent::KEY_UP; break;
case 116: e->keycode = KeyEvent::KEY_DOWN; break;
case 119: e->keycode = KeyEvent::KEY_DELETE; break;
case 22: e->keycode = KeyEvent::KEY_BACKSPACE; break;
case 110: e->keycode = KeyEvent::KEY_HOME; break;
case 115: e->keycode = KeyEvent::KEY_END; break;
case 117: e->keycode = KeyEvent::KEY_PGDOWN; break;
case 112: e->keycode = KeyEvent::KEY_PGUP; break;
case 36: e->keycode = KeyEvent::KEY_ENTER; break;
default: e->keycode = KeyEvent::KEY_UNKNOWN; break;
}
char buf[1024];
int sz = XLookupString(&xe.xkey, buf, sizeof(buf), NULL, NULL);
if(sz && e->keycode == KeyEvent::KEY_UNKNOWN) {
e->keycode = KeyEvent::KEY_CHARACTER;
}
e->text.append(buf, sz);
e->direction = xe.type == KeyPress?1:-1;
event = e;
}
if(xe.type == ClientMessage &&
(unsigned int)xe.xclient.data.l[0] == wmDeleteMessage) {
CloseEvent *e = new CloseEvent();
event = e;
}
return event;
}
#endif/*X11*/
|