1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #include "Window_win.h"
9 
10 #include <tchar.h>
11 #include <windows.h>
12 #include <windowsx.h>
13 
14 #include "SkUtils.h"
15 #include "../WindowContext.h"
16 #include "WindowContextFactory_win.h"
17 #ifdef SK_VULKAN
18 #include "../VulkanWindowContext.h"
19 #endif
20 
21 namespace sk_app {
22 
23 static int gWindowX = CW_USEDEFAULT;
24 static int gWindowY = 0;
25 static int gWindowWidth = CW_USEDEFAULT;
26 static int gWindowHeight = 0;
27 
CreateNativeWindow(void * platformData)28 Window* Window::CreateNativeWindow(void* platformData) {
29     HINSTANCE hInstance = (HINSTANCE)platformData;
30 
31     Window_win* window = new Window_win();
32     if (!window->init(hInstance)) {
33         delete window;
34         return nullptr;
35     }
36 
37     return window;
38 }
39 
closeWindow()40 void Window_win::closeWindow() {
41     RECT r;
42     if (GetWindowRect(fHWnd, &r)) {
43         gWindowX = r.left;
44         gWindowY = r.top;
45         gWindowWidth = r.right - r.left;
46         gWindowHeight = r.bottom - r.top;
47     }
48     DestroyWindow(fHWnd);
49 }
50 
~Window_win()51 Window_win::~Window_win() {
52     this->closeWindow();
53 }
54 
55 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
56 
57 
init(HINSTANCE hInstance)58 bool Window_win::init(HINSTANCE hInstance) {
59     fHInstance = hInstance ? hInstance : GetModuleHandle(nullptr);
60 
61     // The main window class name
62     static const TCHAR gSZWindowClass[] = _T("SkiaApp");
63 
64     static WNDCLASSEX wcex;
65     static bool wcexInit = false;
66     if (!wcexInit) {
67         wcex.cbSize = sizeof(WNDCLASSEX);
68 
69         wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
70         wcex.lpfnWndProc = WndProc;
71         wcex.cbClsExtra = 0;
72         wcex.cbWndExtra = 0;
73         wcex.hInstance = fHInstance;
74         wcex.hIcon = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO);
75         wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);;
76         wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
77         wcex.lpszMenuName = nullptr;
78         wcex.lpszClassName = gSZWindowClass;
79         wcex.hIconSm = LoadIcon(fHInstance, (LPCTSTR)IDI_WINLOGO);;
80 
81         if (!RegisterClassEx(&wcex)) {
82             return false;
83         }
84         wcexInit = true;
85     }
86 
87    /*
88     if (fullscreen)
89     {
90         DEVMODE dmScreenSettings;
91         // If full screen set the screen to maximum size of the users desktop and 32bit.
92         memset(&dmScreenSettings, 0, sizeof(dmScreenSettings));
93         dmScreenSettings.dmSize = sizeof(dmScreenSettings);
94         dmScreenSettings.dmPelsWidth = (unsigned long)width;
95         dmScreenSettings.dmPelsHeight = (unsigned long)height;
96         dmScreenSettings.dmBitsPerPel = 32;
97         dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
98 
99         // Change the display settings to full screen.
100         ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);
101 
102         // Set the position of the window to the top left corner.
103         posX = posY = 0;
104     }
105     */
106  //   gIsFullscreen = fullscreen;
107 
108     fHWnd = CreateWindow(gSZWindowClass, nullptr, WS_OVERLAPPEDWINDOW,
109                          gWindowX, gWindowY, gWindowWidth, gWindowHeight,
110                          nullptr, nullptr, fHInstance, nullptr);
111     if (!fHWnd)
112     {
113         return false;
114     }
115 
116     SetWindowLongPtr(fHWnd, GWLP_USERDATA, (LONG_PTR)this);
117 
118     return true;
119 }
120 
get_key(WPARAM vk)121 static Window::Key get_key(WPARAM vk) {
122     static const struct {
123         WPARAM      fVK;
124         Window::Key fKey;
125     } gPair[] = {
126         { VK_BACK, Window::Key::kBack },
127         { VK_CLEAR, Window::Key::kBack },
128         { VK_RETURN, Window::Key::kOK },
129         { VK_UP, Window::Key::kUp },
130         { VK_DOWN, Window::Key::kDown },
131         { VK_LEFT, Window::Key::kLeft },
132         { VK_RIGHT, Window::Key::kRight },
133         { VK_TAB, Window::Key::kTab },
134         { VK_PRIOR, Window::Key::kPageUp },
135         { VK_NEXT, Window::Key::kPageDown },
136         { VK_HOME, Window::Key::kHome },
137         { VK_END, Window::Key::kEnd },
138         { VK_DELETE, Window::Key::kDelete },
139         { VK_ESCAPE, Window::Key::kEscape },
140         { VK_SHIFT, Window::Key::kShift },
141         { VK_CONTROL, Window::Key::kCtrl },
142         { VK_MENU, Window::Key::kOption },
143         { 'A', Window::Key::kA },
144         { 'C', Window::Key::kC },
145         { 'V', Window::Key::kV },
146         { 'X', Window::Key::kX },
147         { 'Y', Window::Key::kY },
148         { 'Z', Window::Key::kZ },
149     };
150     for (size_t i = 0; i < SK_ARRAY_COUNT(gPair); i++) {
151         if (gPair[i].fVK == vk) {
152             return gPair[i].fKey;
153         }
154     }
155     return Window::Key::kNONE;
156 }
157 
get_modifiers(UINT message,WPARAM wParam,LPARAM lParam)158 static uint32_t get_modifiers(UINT message, WPARAM wParam, LPARAM lParam) {
159     uint32_t modifiers = 0;
160 
161     switch (message) {
162         case WM_UNICHAR:
163         case WM_CHAR:
164             if (0 == (lParam & (1 << 30))) {
165                 modifiers |= Window::kFirstPress_ModifierKey;
166             }
167             if (lParam & (1 << 29)) {
168                 modifiers |= Window::kOption_ModifierKey;
169             }
170             break;
171 
172         case WM_KEYDOWN:
173         case WM_SYSKEYDOWN:
174             if (0 == (lParam & (1 << 30))) {
175                 modifiers |= Window::kFirstPress_ModifierKey;
176             }
177             if (lParam & (1 << 29)) {
178                 modifiers |= Window::kOption_ModifierKey;
179             }
180             break;
181 
182         case WM_KEYUP:
183         case WM_SYSKEYUP:
184             if (lParam & (1 << 29)) {
185                 modifiers |= Window::kOption_ModifierKey;
186             }
187             break;
188 
189         case WM_LBUTTONDOWN:
190         case WM_LBUTTONUP:
191         case WM_MOUSEMOVE:
192         case WM_MOUSEWHEEL:
193             if (wParam & MK_CONTROL) {
194                 modifiers |= Window::kControl_ModifierKey;
195             }
196             if (wParam & MK_SHIFT) {
197                 modifiers |= Window::kShift_ModifierKey;
198             }
199     }
200 
201     return modifiers;
202 }
203 
WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)204 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
205 {
206     PAINTSTRUCT ps;
207     HDC hdc;
208 
209     Window_win* window = (Window_win*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
210 
211     bool eventHandled = false;
212 
213     switch (message) {
214         case WM_PAINT:
215             hdc = BeginPaint(hWnd, &ps);
216             window->onPaint();
217             EndPaint(hWnd, &ps);
218             eventHandled = true;
219             break;
220 
221         case WM_CLOSE:
222             PostQuitMessage(0);
223             eventHandled = true;
224             break;
225 
226         case WM_ACTIVATE:
227             // disable/enable rendering here, depending on wParam != WA_INACTIVE
228             break;
229 
230         case WM_SIZE:
231             window->onResize(LOWORD(lParam), HIWORD(lParam));
232             eventHandled = true;
233             break;
234 
235         case WM_UNICHAR:
236             eventHandled = window->onChar((SkUnichar)wParam,
237                                           get_modifiers(message, wParam, lParam));
238             break;
239 
240         case WM_CHAR: {
241             const uint16_t* c = reinterpret_cast<uint16_t*>(&wParam);
242             eventHandled = window->onChar(SkUTF16_NextUnichar(&c),
243                                           get_modifiers(message, wParam, lParam));
244         } break;
245 
246         case WM_KEYDOWN:
247         case WM_SYSKEYDOWN:
248             eventHandled = window->onKey(get_key(wParam), Window::kDown_InputState,
249                                          get_modifiers(message, wParam, lParam));
250             break;
251 
252         case WM_KEYUP:
253         case WM_SYSKEYUP:
254             eventHandled = window->onKey(get_key(wParam), Window::kUp_InputState,
255                                          get_modifiers(message, wParam, lParam));
256             break;
257 
258         case WM_LBUTTONDOWN:
259         case WM_LBUTTONUP: {
260             int xPos = GET_X_LPARAM(lParam);
261             int yPos = GET_Y_LPARAM(lParam);
262 
263             //if (!gIsFullscreen)
264             //{
265             //    RECT rc = { 0, 0, 640, 480 };
266             //    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
267             //    xPos -= rc.left;
268             //    yPos -= rc.top;
269             //}
270 
271             Window::InputState istate = ((wParam & MK_LBUTTON) != 0) ? Window::kDown_InputState
272                                                                      : Window::kUp_InputState;
273 
274             eventHandled = window->onMouse(xPos, yPos, istate,
275                                             get_modifiers(message, wParam, lParam));
276         } break;
277 
278         case WM_MOUSEMOVE: {
279             int xPos = GET_X_LPARAM(lParam);
280             int yPos = GET_Y_LPARAM(lParam);
281 
282             //if (!gIsFullscreen)
283             //{
284             //    RECT rc = { 0, 0, 640, 480 };
285             //    AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
286             //    xPos -= rc.left;
287             //    yPos -= rc.top;
288             //}
289 
290             eventHandled = window->onMouse(xPos, yPos, Window::kMove_InputState,
291                                            get_modifiers(message, wParam, lParam));
292         } break;
293 
294         case WM_MOUSEWHEEL:
295             eventHandled = window->onMouseWheel(GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f,
296                                                 get_modifiers(message, wParam, lParam));
297             break;
298 
299         default:
300             return DefWindowProc(hWnd, message, wParam, lParam);
301     }
302 
303     return eventHandled ? 0 : 1;
304 }
305 
setTitle(const char * title)306 void Window_win::setTitle(const char* title) {
307     SetWindowTextA(fHWnd, title);
308 }
309 
show()310 void Window_win::show() {
311     ShowWindow(fHWnd, SW_SHOW);
312 }
313 
314 
attach(BackendType attachType)315 bool Window_win::attach(BackendType attachType) {
316     fBackend = attachType;
317 
318     switch (attachType) {
319         case kNativeGL_BackendType:
320             fWindowContext = window_context_factory::NewGLForWin(fHWnd, fRequestedDisplayParams);
321             break;
322         case kRaster_BackendType:
323             fWindowContext = window_context_factory::NewRasterForWin(fHWnd,
324                                                                      fRequestedDisplayParams);
325             break;
326 #ifdef SK_VULKAN
327         case kVulkan_BackendType:
328             fWindowContext = window_context_factory::NewVulkanForWin(fHWnd,
329                                                                      fRequestedDisplayParams);
330             break;
331 #endif
332     }
333     this->onBackendCreated();
334 
335     return (SkToBool(fWindowContext));
336 }
337 
onInval()338 void Window_win::onInval() {
339     InvalidateRect(fHWnd, nullptr, false);
340 }
341 
setRequestedDisplayParams(const DisplayParams & params)342 void Window_win::setRequestedDisplayParams(const DisplayParams& params) {
343     // GL on Windows doesn't let us change MSAA after the window is created
344     if (params.fMSAASampleCount != this->getRequestedDisplayParams().fMSAASampleCount) {
345         // Need to change these early, so attach() creates the window context correctly
346         fRequestedDisplayParams = params;
347 
348         delete fWindowContext;
349         this->closeWindow();
350         this->init(fHInstance);
351         this->attach(fBackend);
352     }
353 
354     INHERITED::setRequestedDisplayParams(params);
355 }
356 
357 }   // namespace sk_app
358