OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 //#include <tchar.h> | |
9 | |
10 #include "SkUtils.h" | 8 #include "SkUtils.h" |
11 #include "Timer.h" | 9 #include "Timer.h" |
12 #include "WindowContextFactory_mac.h" | 10 #include "WindowContextFactory_mac.h" |
13 #include "Window_mac.h" | 11 #include "Window_mac.h" |
14 | 12 |
15 namespace sk_app { | 13 namespace sk_app { |
16 | 14 |
17 Window* Window::CreateNativeWindow(void* platformData) { | 15 SkTDynamicHash<Window_mac, Uint32> Window_mac::gWindowMap; |
18 #if 0 | 16 |
19 // TODO: platform-specific window creation | 17 Window* Window::CreateNativeWindow(void*) { |
20 Display* display = (Display*)platformData; | |
21 | |
22 Window_mac* window = new Window_mac(); | 18 Window_mac* window = new Window_mac(); |
23 if (!window->initWindow(display, nullptr)) { | 19 if (!window->initWindow(nullptr)) { |
24 delete window; | 20 delete window; |
25 return nullptr; | 21 return nullptr; |
26 } | 22 } |
27 | 23 |
28 return window; | 24 return window; |
29 #else | 25 } |
30 return nullptr; | 26 |
31 #endif | 27 bool Window_mac::initWindow(const DisplayParams* params) { |
32 } | |
33 | |
34 #if 0 | |
35 // TODO: Implement Mac window code | |
36 | |
37 const long kEventMask = ExposureMask | StructureNotifyMask | | |
38 KeyPressMask | KeyReleaseMask | | |
39 PointerMotionMask | ButtonPressMask | ButtonReleaseMask; | |
40 | |
41 bool Window_mac::initWindow(Display* display, const DisplayParams* params) { | |
42 if (params && params->fMSAASampleCount != fMSAASampleCount) { | 28 if (params && params->fMSAASampleCount != fMSAASampleCount) { |
43 this->closeWindow(); | 29 this->closeWindow(); |
44 } | 30 } |
45 // we already have a window | 31 // we already have a window |
46 if (fDisplay) { | 32 if (fWindow) { |
47 return true; | 33 return true; |
48 } | 34 } |
49 fDisplay = display; | |
50 | 35 |
51 fWidth = 1280; | 36 fWidth = 1280; |
52 fHeight = 960; | 37 fHeight = 960; |
53 | 38 |
54 // Attempt to create a window that supports GL | 39 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
55 GLint att[] = { | 40 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); |
56 GLX_RGBA, | 41 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE
); |
57 GLX_DEPTH_SIZE, 24, | 42 |
58 GLX_DOUBLEBUFFER, | 43 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); |
59 GLX_STENCIL_SIZE, 8, | 44 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); |
60 None | 45 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); |
61 }; | 46 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
62 SkASSERT(nullptr == fVisualInfo); | 47 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0); |
| 48 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); |
| 49 |
| 50 SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); |
| 51 |
63 if (params && params->fMSAASampleCount > 0) { | 52 if (params && params->fMSAASampleCount > 0) { |
64 static const GLint kAttCount = SK_ARRAY_COUNT(att); | 53 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); |
65 GLint msaaAtt[kAttCount + 4]; | 54 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, params->fMSAASampleCount)
; |
66 memcpy(msaaAtt, att, sizeof(att)); | |
67 SkASSERT(None == msaaAtt[kAttCount - 1]); | |
68 msaaAtt[kAttCount - 1] = GLX_SAMPLE_BUFFERS_ARB; | |
69 msaaAtt[kAttCount + 0] = 1; | |
70 msaaAtt[kAttCount + 1] = GLX_SAMPLES_ARB; | |
71 msaaAtt[kAttCount + 2] = params->fMSAASampleCount; | |
72 msaaAtt[kAttCount + 3] = None; | |
73 fVisualInfo = glXChooseVisual(display, DefaultScreen(display), msaaAtt); | |
74 fMSAASampleCount = params->fMSAASampleCount; | |
75 } | |
76 if (nullptr == fVisualInfo) { | |
77 fVisualInfo = glXChooseVisual(display, DefaultScreen(display), att); | |
78 fMSAASampleCount = 0; | |
79 } | |
80 | |
81 if (fVisualInfo) { | |
82 Colormap colorMap = XCreateColormap(display, | |
83 RootWindow(display, fVisualInfo->scr
een), | |
84 fVisualInfo->visual, | |
85 AllocNone); | |
86 XSetWindowAttributes swa; | |
87 swa.colormap = colorMap; | |
88 swa.event_mask = kEventMask; | |
89 fWindow = XCreateWindow(display, | |
90 RootWindow(display, fVisualInfo->screen), | |
91 0, 0, // x, y | |
92 fWidth, fHeight, | |
93 0, // border width | |
94 fVisualInfo->depth, | |
95 InputOutput, | |
96 fVisualInfo->visual, | |
97 CWEventMask | CWColormap, | |
98 &swa); | |
99 } else { | 55 } else { |
100 // Create a simple window instead. We will not be able to show GL | 56 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); |
101 fWindow = XCreateSimpleWindow(display, | 57 } |
102 DefaultRootWindow(display), | 58 // TODO: handle other display params |
103 0, 0, // x, y | 59 |
104 fWidth, fHeight, | 60 uint32_t windowFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; |
105 0, // border width | 61 fWindow = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWP
OS_CENTERED, |
106 0, // border value | 62 fWidth, fHeight, windowFlags); |
107 0); // background value | |
108 XSelectInput(display, fWindow, kEventMask); | |
109 } | |
110 | 63 |
111 if (!fWindow) { | 64 if (!fWindow) { |
112 return false; | 65 return false; |
113 } | 66 } |
114 | 67 |
115 // set up to catch window delete message | |
116 fWmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False); | |
117 XSetWMProtocols(display, fWindow, &fWmDeleteMessage, 1); | |
118 | |
119 // add to hashtable of windows | 68 // add to hashtable of windows |
| 69 fWindowID = SDL_GetWindowID(fWindow); |
120 gWindowMap.add(this); | 70 gWindowMap.add(this); |
121 | 71 |
122 // init event variables | |
123 fPendingPaint = false; | |
124 fPendingResize = false; | |
125 | |
126 return true; | 72 return true; |
127 } | 73 } |
128 | 74 |
129 void Window_mac::closeWindow() { | 75 void Window_mac::closeWindow() { |
130 if (fDisplay) { | 76 if (fWindow) { |
131 this->detach(); | 77 gWindowMap.remove(fWindowID); |
132 SkASSERT(fGC); | 78 SDL_DestroyWindow(fWindow); |
133 XFreeGC(fDisplay, fGC); | 79 fWindowID = 0; |
134 fGC = nullptr; | 80 fWindow = nullptr; |
135 gWindowMap.remove(fWindow); | 81 } |
136 XDestroyWindow(fDisplay, fWindow); | 82 } |
137 fWindow = 0; | 83 |
138 fVisualInfo = nullptr; | 84 static Window::Key get_key(const SDL_Keysym& keysym) { |
139 fDisplay = nullptr; | |
140 fMSAASampleCount = 0; | |
141 } | |
142 } | |
143 | |
144 static Window::Key get_key(KeySym keysym) { | |
145 static const struct { | 85 static const struct { |
146 KeySym fXK; | 86 SDL_Keycode fSDLK; |
147 Window::Key fKey; | 87 Window::Key fKey; |
148 } gPair[] = { | 88 } gPair[] = { |
149 { XK_BackSpace, Window::Key::kBack }, | 89 { SDLK_BACKSPACE, Window::Key::kBack }, |
150 { XK_Clear, Window::Key::kBack }, | 90 { SDLK_CLEAR, Window::Key::kBack }, |
151 { XK_Return, Window::Key::kOK }, | 91 { SDLK_RETURN, Window::Key::kOK }, |
152 { XK_Up, Window::Key::kUp }, | 92 { SDLK_UP, Window::Key::kUp }, |
153 { XK_Down, Window::Key::kDown }, | 93 { SDLK_DOWN, Window::Key::kDown }, |
154 { XK_Left, Window::Key::kLeft }, | 94 { SDLK_LEFT, Window::Key::kLeft }, |
155 { XK_Right, Window::Key::kRight } | 95 { SDLK_RIGHT, Window::Key::kRight } |
156 }; | 96 }; |
157 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) { | 97 for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) { |
158 if (gPair[i].fXK == keysym) { | 98 if (gPair[i].fSDLK == keysym.sym) { |
159 return gPair[i].fKey; | 99 return gPair[i].fKey; |
160 } | 100 } |
161 } | 101 } |
162 return Window::Key::kNONE; | 102 return Window::Key::kNONE; |
163 } | 103 } |
164 | 104 |
165 static uint32_t get_modifiers(const XEvent& event) { | 105 static uint32_t get_modifiers(const SDL_Event& event) { |
166 static const struct { | 106 static const struct { |
167 unsigned fXMask; | 107 unsigned fSDLMask; |
168 unsigned fSkMask; | 108 unsigned fSkMask; |
169 } gModifiers[] = { | 109 } gModifiers[] = { |
170 { ShiftMask, Window::kShift_ModifierKey }, | 110 { KMOD_SHIFT, Window::kShift_ModifierKey }, |
171 { ControlMask, Window::kControl_ModifierKey }, | 111 { KMOD_CTRL, Window::kControl_ModifierKey }, |
172 { Mod1Mask, Window::kOption_ModifierKey }, | 112 { KMOD_ALT, Window::kOption_ModifierKey }, |
173 }; | 113 }; |
174 | 114 |
175 auto modifiers = 0; | 115 auto modifiers = 0; |
176 for (size_t i = 0; i < SK_ARRAY_COUNT(gModifiers); ++i) { | 116 |
177 if (event.xkey.state & gModifiers[i].fXMask) { | 117 switch (event.type) { |
178 modifiers |= gModifiers[i].fSkMask; | 118 case SDL_KEYDOWN: |
| 119 // fall through |
| 120 case SDL_KEYUP: { |
| 121 for (size_t i = 0; i < SK_ARRAY_COUNT(gModifiers); ++i) { |
| 122 if (event.key.keysym.mod & gModifiers[i].fSDLMask) { |
| 123 modifiers |= gModifiers[i].fSkMask; |
| 124 } |
| 125 } |
| 126 if (0 == event.key.repeat) { |
| 127 modifiers |= Window::kFirstPress_ModifierKey; |
| 128 } |
| 129 break; |
179 } | 130 } |
| 131 |
| 132 default: { |
| 133 SDL_Keymod mod = SDL_GetModState(); |
| 134 for (size_t i = 0; i < SK_ARRAY_COUNT(gModifiers); ++i) { |
| 135 if (mod & gModifiers[i].fSDLMask) { |
| 136 modifiers |= gModifiers[i].fSkMask; |
| 137 } |
| 138 } |
| 139 break; |
| 140 } |
180 } | 141 } |
181 return modifiers; | 142 return modifiers; |
182 } | 143 } |
183 | 144 |
184 bool Window_mac::handleEvent(const XEvent& event) { | 145 bool Window_mac::HandleWindowEvent(const SDL_Event& event) { |
| 146 Window_mac* win = gWindowMap.find(event.window.windowID); |
| 147 if (win && win->handleEvent(event)) { |
| 148 return true; |
| 149 } |
| 150 |
| 151 return false; |
| 152 } |
| 153 |
| 154 bool Window_mac::handleEvent(const SDL_Event& event) { |
185 switch (event.type) { | 155 switch (event.type) { |
186 case MapNotify: | 156 case SDL_WINDOWEVENT: |
187 if (!fGC) { | 157 if (SDL_WINDOWEVENT_EXPOSED == event.window.event) { |
188 fGC = XCreateGC(fDisplay, fWindow, 0, nullptr); | 158 this->onPaint(); |
189 } | 159 } else if (SDL_WINDOWEVENT_RESIZED == event.window.event) { |
190 break; | 160 this->onResize(event.window.data1, event.window.data2); |
191 | 161 } |
192 case ClientMessage: | 162 break; |
193 if ((Atom)event.xclient.data.l[0] == fWmDeleteMessage && | 163 |
194 gWindowMap.count() == 1) { | 164 case SDL_MOUSEBUTTONDOWN: |
| 165 if (event.button.button == SDL_BUTTON_LEFT) { |
| 166 this->onMouse(event.button.x, event.button.y, |
| 167 Window::kDown_InputState, get_modifiers(event)); |
| 168 } |
| 169 break; |
| 170 |
| 171 case SDL_MOUSEBUTTONUP: |
| 172 if (event.button.button == SDL_BUTTON_LEFT) { |
| 173 this->onMouse(event.button.x, event.button.y, |
| 174 Window::kUp_InputState, get_modifiers(event)); |
| 175 } |
| 176 break; |
| 177 |
| 178 case SDL_MOUSEMOTION: |
| 179 // only track if left button is down |
| 180 if (event.motion.state & SDL_BUTTON_LMASK) { |
| 181 this->onMouse(event.motion.x, event.motion.y, |
| 182 Window::kMove_InputState, get_modifiers(event)); |
| 183 } |
| 184 break; |
| 185 |
| 186 case SDL_KEYDOWN: { |
| 187 if (event.key.keysym.sym == SDLK_ESCAPE) { |
195 return true; | 188 return true; |
196 } | 189 } |
197 break; | 190 Window::Key key = get_key(event.key.keysym); |
198 | |
199 case ButtonPress: | |
200 if (event.xbutton.button == Button1) { | |
201 this->onMouse(event.xbutton.x, event.xbutton.y, | |
202 Window::kDown_InputState, get_modifiers(event)); | |
203 } | |
204 break; | |
205 | |
206 case ButtonRelease: | |
207 if (event.xbutton.button == Button1) { | |
208 this->onMouse(event.xbutton.x, event.xbutton.y, | |
209 Window::kUp_InputState, get_modifiers(event)); | |
210 } | |
211 break; | |
212 | |
213 case MotionNotify: | |
214 // only track if left button is down | |
215 if (event.xmotion.state & Button1Mask) { | |
216 this->onMouse(event.xmotion.x, event.xmotion.y, | |
217 Window::kMove_InputState, get_modifiers(event)); | |
218 } | |
219 break; | |
220 | |
221 case KeyPress: { | |
222 int shiftLevel = (event.xkey.state & ShiftMask) ? 1 : 0; | |
223 KeySym keysym = XkbKeycodeToKeysym(fDisplay, event.xkey.keycode, | |
224 0, shiftLevel); | |
225 if (keysym == XK_Escape) { | |
226 return true; | |
227 } | |
228 Window::Key key = get_key(keysym); | |
229 if (key != Window::Key::kNONE) { | 191 if (key != Window::Key::kNONE) { |
230 (void) this->onKey(key, Window::kDown_InputState, | 192 (void) this->onKey(key, Window::kDown_InputState, |
231 get_modifiers(event)); | 193 get_modifiers(event)); |
232 } else { | 194 } else { |
233 long uni = keysym2ucs(keysym); | 195 (void) this->onChar((SkUnichar) event.key.keysym.sym, |
234 if (uni != -1) { | 196 get_modifiers(event)); |
235 (void) this->onChar((SkUnichar) uni, | |
236 get_modifiers(event)); | |
237 } | |
238 } | 197 } |
239 } break; | 198 } break; |
240 | 199 |
241 case KeyRelease: { | 200 case SDL_KEYUP: { |
242 int shiftLevel = (event.xkey.state & ShiftMask) ? 1 : 0; | 201 Window::Key key = get_key(event.key.keysym); |
243 KeySym keysym = XkbKeycodeToKeysym(fDisplay, event.xkey.keycode, | 202 if (key != Window::Key::kNONE) { |
244 0, shiftLevel); | 203 (void) this->onKey(key, Window::kUp_InputState, |
245 Window::Key key = get_key(keysym); | 204 get_modifiers(event)); |
246 (void) this->onKey(key, Window::kUp_InputState, | 205 } |
247 get_modifiers(event)); | |
248 } break; | 206 } break; |
249 | |
250 | 207 |
251 default: | 208 default: |
252 // these events should be handled in the main event loop | 209 break; |
253 SkASSERT(event.type != Expose && event.type != ConfigureNotify); | 210 } |
254 break; | 211 |
255 } | |
256 | |
257 return false; | 212 return false; |
258 } | 213 } |
259 | 214 |
260 void Window_mac::setTitle(const char* title) { | 215 void Window_mac::setTitle(const char* title) { |
261 XTextProperty textproperty; | 216 SDL_SetWindowTitle(fWindow, title); |
262 XStringListToTextProperty(const_cast<char**>(&title), 1, &textproperty); | |
263 XSetWMName(fDisplay, fWindow, &textproperty); | |
264 } | 217 } |
265 | 218 |
266 void Window_mac::show() { | 219 void Window_mac::show() { |
267 XMapWindow(fDisplay, fWindow); | 220 SDL_ShowWindow(fWindow); |
268 } | 221 } |
269 | 222 |
270 bool Window_mac::attach(BackendType attachType, const DisplayParams& params) { | 223 bool Window_mac::attach(BackendType attachType, const DisplayParams& params) { |
271 this->initWindow(fDisplay, ¶ms); | 224 this->initWindow(¶ms); |
272 | 225 |
273 MacWindowInfo info; | 226 window_context_factory::MacWindowInfo info; |
274 #if 0 | 227 info.fWindow = fWindow; |
275 // Init Mac window info here | |
276 info.foo = foo; | |
277 #endif | |
278 switch (attachType) { | 228 switch (attachType) { |
279 #ifdef SK_VULKAN | |
280 case kVulkan_BackendType: | |
281 fWindowContext = NewVulkanForMac(info, params); | |
282 break; | |
283 #endif | |
284 case kNativeGL_BackendType: | 229 case kNativeGL_BackendType: |
285 default: | 230 default: |
286 fWindowContext = NewGLForMac(info, params); | 231 fWindowContext = NewGLForMac(info, params); |
287 break; | 232 break; |
288 } | 233 } |
289 | 234 |
290 return (SkToBool(fWindowContext)); | 235 return (SkToBool(fWindowContext)); |
291 } | 236 } |
292 | 237 |
293 void Window_mac::onInval() { | 238 void Window_mac::onInval() { |
294 XEvent event; | 239 SDL_Event sdlevent; |
295 event.type = Expose; | 240 sdlevent.type = SDL_WINDOWEVENT; |
296 event.xexpose.send_event = True; | 241 sdlevent.window.windowID = fWindowID; |
297 event.xexpose.display = fDisplay; | 242 sdlevent.window.event = SDL_WINDOWEVENT_EXPOSED; |
298 event.xexpose.window = fWindow; | 243 SDL_PushEvent(&sdlevent); |
299 event.xexpose.x = 0; | |
300 event.xexpose.y = 0; | |
301 event.xexpose.width = fWidth; | |
302 event.xexpose.height = fHeight; | |
303 event.xexpose.count = 0; | |
304 | |
305 XSendEvent(fDisplay, fWindow, False, 0, &event); | |
306 } | 244 } |
307 #endif | 245 |
308 | |
309 } // namespace sk_app | 246 } // namespace sk_app |
OLD | NEW |