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
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
//#include <tchar.h>
#include "WindowContextFactory_unix.h"
#include "SkUtils.h"
#include "Timer.h"
#include "../GLWindowContext.h"
#include "Window_unix.h"
extern "C" {
#include "keysym2ucs.h"
}
#include <X11/Xutil.h>
#include <X11/XKBlib.h>
namespace sk_app {
SkTDynamicHash<Window_unix, XWindow> Window_unix::gWindowMap;
Window* Window::CreateNativeWindow(void* platformData) {
Display* display = (Display*)platformData;
SkASSERT(display);
Window_unix* window = new Window_unix();
if (!window->initWindow(display)) {
delete window;
return nullptr;
}
return window;
}
const long kEventMask = ExposureMask | StructureNotifyMask |
KeyPressMask | KeyReleaseMask |
PointerMotionMask | ButtonPressMask | ButtonReleaseMask;
bool Window_unix::initWindow(Display* display) {
if (fRequestedDisplayParams.fMSAASampleCount != fMSAASampleCount) {
this->closeWindow();
}
// we already have a window
if (fDisplay) {
return true;
}
fDisplay = display;
constexpr int initialWidth = 1280;
constexpr int initialHeight = 960;
// Attempt to create a window that supports GL
// We prefer the more recent glXChooseFBConfig but fall back to glXChooseVisual. They have
// slight differences in how attributes are specified.
static int constexpr kChooseFBConfigAtt[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, True,
GLX_STENCIL_SIZE, 8,
None
};
// For some reason glXChooseVisual takes a non-const pointer to the attributes.
int chooseVisualAtt[] = {
GLX_RGBA,
GLX_DOUBLEBUFFER,
GLX_STENCIL_SIZE, 8,
None
};
SkASSERT(nullptr == fVisualInfo);
if (fRequestedDisplayParams.fMSAASampleCount > 0) {
static const GLint kChooseFBConifgAttCnt = SK_ARRAY_COUNT(kChooseFBConfigAtt);
GLint msaaChooseFBConfigAtt[kChooseFBConifgAttCnt + 4];
memcpy(msaaChooseFBConfigAtt, kChooseFBConfigAtt, sizeof(kChooseFBConfigAtt));
SkASSERT(None == msaaChooseFBConfigAtt[kChooseFBConifgAttCnt - 1]);
msaaChooseFBConfigAtt[kChooseFBConifgAttCnt - 1] = GLX_SAMPLE_BUFFERS_ARB;
msaaChooseFBConfigAtt[kChooseFBConifgAttCnt + 0] = 1;
msaaChooseFBConfigAtt[kChooseFBConifgAttCnt + 1] = GLX_SAMPLES_ARB;
msaaChooseFBConfigAtt[kChooseFBConifgAttCnt + 2] = fRequestedDisplayParams.fMSAASampleCount;
msaaChooseFBConfigAtt[kChooseFBConifgAttCnt + 3] = None;
int n;
fFBConfig = glXChooseFBConfig(fDisplay, DefaultScreen(fDisplay), msaaChooseFBConfigAtt, &n);
if (n > 0) {
fVisualInfo = glXGetVisualFromFBConfig(fDisplay, *fFBConfig);
} else {
static const GLint kChooseVisualAttCnt = SK_ARRAY_COUNT(chooseVisualAtt);
GLint msaaChooseVisualAtt[kChooseVisualAttCnt + 4];
memcpy(msaaChooseVisualAtt, chooseVisualAtt, sizeof(chooseVisualAtt));
SkASSERT(None == msaaChooseVisualAtt[kChooseVisualAttCnt - 1]);
msaaChooseFBConfigAtt[kChooseVisualAttCnt - 1] = GLX_SAMPLE_BUFFERS_ARB;
msaaChooseFBConfigAtt[kChooseVisualAttCnt + 0] = 1;
msaaChooseFBConfigAtt[kChooseVisualAttCnt + 1] = GLX_SAMPLES_ARB;
msaaChooseFBConfigAtt[kChooseVisualAttCnt + 2] =
fRequestedDisplayParams.fMSAASampleCount;
msaaChooseFBConfigAtt[kChooseVisualAttCnt + 3] = None;
fVisualInfo = glXChooseVisual(display, DefaultScreen(display), msaaChooseVisualAtt);
fFBConfig = nullptr;
}
}
if (nullptr == fVisualInfo) {
int n;
fFBConfig = glXChooseFBConfig(fDisplay, DefaultScreen(fDisplay), kChooseFBConfigAtt, &n);
if (n > 0) {
fVisualInfo = glXGetVisualFromFBConfig(fDisplay, *fFBConfig);
} else {
fVisualInfo = glXChooseVisual(display, DefaultScreen(display), chooseVisualAtt);
fFBConfig = nullptr;
}
}
if (fVisualInfo) {
Colormap colorMap = XCreateColormap(display,
RootWindow(display, fVisualInfo->screen),
fVisualInfo->visual,
AllocNone);
XSetWindowAttributes swa;
swa.colormap = colorMap;
swa.event_mask = kEventMask;
fWindow = XCreateWindow(display,
RootWindow(display, fVisualInfo->screen),
0, 0, // x, y
initialWidth, initialHeight,
0, // border width
fVisualInfo->depth,
InputOutput,
fVisualInfo->visual,
CWEventMask | CWColormap,
&swa);
} else {
// Create a simple window instead. We will not be able to show GL
fWindow = XCreateSimpleWindow(display,
DefaultRootWindow(display),
0, 0, // x, y
initialWidth, initialHeight,
0, // border width
0, // border value
0); // background value
XSelectInput(display, fWindow, kEventMask);
}
if (!fWindow) {
return false;
}
fMSAASampleCount = fRequestedDisplayParams.fMSAASampleCount;
// set up to catch window delete message
fWmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, fWindow, &fWmDeleteMessage, 1);
// add to hashtable of windows
gWindowMap.add(this);
// init event variables
fPendingPaint = false;
fPendingResize = false;
return true;
}
void Window_unix::closeWindow() {
if (fDisplay) {
this->detach();
if (fGC) {
XFreeGC(fDisplay, fGC);
fGC = nullptr;
}
gWindowMap.remove(fWindow);
XDestroyWindow(fDisplay, fWindow);
fWindow = 0;
if (fFBConfig) {
XFree(fFBConfig);
fFBConfig = nullptr;
}
if (fVisualInfo) {
XFree(fVisualInfo);
fVisualInfo = nullptr;
}
fDisplay = nullptr;
}
}
static Window::Key get_key(KeySym keysym) {
static const struct {
KeySym fXK;
Window::Key fKey;
} gPair[] = {
{ XK_BackSpace, Window::Key::kBack },
{ XK_Clear, Window::Key::kBack },
{ XK_Return, Window::Key::kOK },
{ XK_Up, Window::Key::kUp },
{ XK_Down, Window::Key::kDown },
{ XK_Left, Window::Key::kLeft },
{ XK_Right, Window::Key::kRight },
{ XK_Tab, Window::Key::kTab },
{ XK_Page_Up, Window::Key::kPageUp },
{ XK_Page_Down, Window::Key::kPageDown },
{ XK_Home, Window::Key::kHome },
{ XK_End, Window::Key::kEnd },
{ XK_Delete, Window::Key::kDelete },
{ XK_Escape, Window::Key::kEscape },
{ XK_Shift_L, Window::Key::kShift },
{ XK_Shift_R, Window::Key::kShift },
{ XK_Control_L, Window::Key::kCtrl },
{ XK_Control_R, Window::Key::kCtrl },
{ XK_Alt_L, Window::Key::kOption },
{ XK_Alt_R, Window::Key::kOption },
{ 'A', Window::Key::kA },
{ 'C', Window::Key::kC },
{ 'V', Window::Key::kV },
{ 'X', Window::Key::kX },
{ 'Y', Window::Key::kY },
{ 'Z', Window::Key::kZ },
};
for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) {
if (gPair[i].fXK == keysym) {
return gPair[i].fKey;
}
}
return Window::Key::kNONE;
}
static uint32_t get_modifiers(const XEvent& event) {
static const struct {
unsigned fXMask;
unsigned fSkMask;
} gModifiers[] = {
{ ShiftMask, Window::kShift_ModifierKey },
{ ControlMask, Window::kControl_ModifierKey },
{ Mod1Mask, Window::kOption_ModifierKey },
};
auto modifiers = 0;
for (size_t i = 0; i < SK_ARRAY_COUNT(gModifiers); ++i) {
if (event.xkey.state & gModifiers[i].fXMask) {
modifiers |= gModifiers[i].fSkMask;
}
}
return modifiers;
}
bool Window_unix::handleEvent(const XEvent& event) {
switch (event.type) {
case MapNotify:
if (!fGC) {
fGC = XCreateGC(fDisplay, fWindow, 0, nullptr);
}
break;
case ClientMessage:
if ((Atom)event.xclient.data.l[0] == fWmDeleteMessage &&
gWindowMap.count() == 1) {
return true;
}
break;
case ButtonPress:
switch (event.xbutton.button) {
case Button1:
this->onMouse(event.xbutton.x, event.xbutton.y,
Window::kDown_InputState, get_modifiers(event));
break;
case Button4:
this->onMouseWheel(1.0f, get_modifiers(event));
break;
case Button5:
this->onMouseWheel(-1.0f, get_modifiers(event));
break;
}
break;
case ButtonRelease:
if (event.xbutton.button == Button1) {
this->onMouse(event.xbutton.x, event.xbutton.y,
Window::kUp_InputState, get_modifiers(event));
}
break;
case MotionNotify:
this->onMouse(event.xmotion.x, event.xmotion.y,
Window::kMove_InputState, get_modifiers(event));
break;
case KeyPress: {
int shiftLevel = (event.xkey.state & ShiftMask) ? 1 : 0;
KeySym keysym = XkbKeycodeToKeysym(fDisplay, event.xkey.keycode, 0, shiftLevel);
Window::Key key = get_key(keysym);
if (key != Window::Key::kNONE) {
if (!this->onKey(key, Window::kDown_InputState, get_modifiers(event))) {
if (keysym == XK_Escape) {
return true;
}
}
}
long uni = keysym2ucs(keysym);
if (uni != -1) {
(void) this->onChar((SkUnichar) uni, get_modifiers(event));
}
} break;
case KeyRelease: {
int shiftLevel = (event.xkey.state & ShiftMask) ? 1 : 0;
KeySym keysym = XkbKeycodeToKeysym(fDisplay, event.xkey.keycode,
0, shiftLevel);
Window::Key key = get_key(keysym);
(void) this->onKey(key, Window::kUp_InputState,
get_modifiers(event));
} break;
default:
// these events should be handled in the main event loop
SkASSERT(event.type != Expose && event.type != ConfigureNotify);
break;
}
return false;
}
void Window_unix::setTitle(const char* title) {
XTextProperty textproperty;
XStringListToTextProperty(const_cast<char**>(&title), 1, &textproperty);
XSetWMName(fDisplay, fWindow, &textproperty);
}
void Window_unix::show() {
XMapWindow(fDisplay, fWindow);
}
bool Window_unix::attach(BackendType attachType) {
this->initWindow(fDisplay);
window_context_factory::XlibWindowInfo winInfo;
winInfo.fDisplay = fDisplay;
winInfo.fWindow = fWindow;
winInfo.fFBConfig = fFBConfig;
winInfo.fVisualInfo = fVisualInfo;
XWindowAttributes attrs;
if (XGetWindowAttributes(fDisplay, fWindow, &attrs)) {
winInfo.fWidth = attrs.width;
winInfo.fHeight = attrs.height;
} else {
winInfo.fWidth = winInfo.fHeight = 0;
}
switch (attachType) {
#ifdef SK_VULKAN
case kVulkan_BackendType:
fWindowContext = window_context_factory::NewVulkanForXlib(winInfo,
fRequestedDisplayParams);
break;
#endif
case kNativeGL_BackendType:
fWindowContext = window_context_factory::NewGLForXlib(winInfo, fRequestedDisplayParams);
break;
case kRaster_BackendType:
fWindowContext = window_context_factory::NewRasterForXlib(winInfo,
fRequestedDisplayParams);
break;
}
this->onBackendCreated();
return (SkToBool(fWindowContext));
}
void Window_unix::onInval() {
XEvent event;
event.type = Expose;
event.xexpose.send_event = True;
event.xexpose.display = fDisplay;
event.xexpose.window = fWindow;
event.xexpose.x = 0;
event.xexpose.y = 0;
event.xexpose.width = this->width();
event.xexpose.height = this->height();
event.xexpose.count = 0;
XSendEvent(fDisplay, fWindow, False, 0, &event);
}
} // namespace sk_app
|