Win32Window.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "../DFPSR/api/imageAPI.h"
  2. #include "../DFPSR/gui/BackendWindow.h"
  3. /*
  4. Link to these dependencies for MS Windows:
  5. gdi32
  6. user32
  7. kernel32
  8. comctl32
  9. */
  10. #include <tchar.h>
  11. #include <windows.h>
  12. #include <windowsx.h>
  13. class Win32Window : public dsr::BackendWindow {
  14. public:
  15. // The native windows handle
  16. HWND hwnd;
  17. // Double buffering to allow drawing to a canvas while displaying the previous one
  18. // The image which can be drawn to, sharing memory with the X11 image
  19. dsr::AlignedImageRgbaU8 canvas;
  20. // Remembers the dimensions of the window from creation and resize events
  21. // This allow requesting the size of the window at any time
  22. int windowWidth = 0, windowHeight = 0;
  23. private:
  24. // Called before the application fetches events from the input queue
  25. // Closing the window, moving the mouse, pressing a key, et cetera
  26. void prefetchEvents() override;
  27. private:
  28. // Helper methods specific to calling XLib
  29. void updateTitle();
  30. private:
  31. // Canvas methods
  32. dsr::AlignedImageRgbaU8 getCanvas() override { return this->canvas; }
  33. void resizeCanvas(int width, int height) override;
  34. // Window methods
  35. void setTitle(const dsr::String &newTitle) override {
  36. this->title = newTitle;
  37. this->updateTitle();
  38. }
  39. void removeOldWindow();
  40. void createWindow(const dsr::String& title, int width, int height);
  41. void createWindowed(const dsr::String& title, int width, int height);
  42. void createFullscreen();
  43. void prepareWindow();
  44. int windowState = 0; // 0=none, 1=windowed, 2=fullscreen
  45. public:
  46. // Constructors
  47. Win32Window(const Win32Window&) = delete; // Non-copyable because of pointer aliasing.
  48. Win32Window(const dsr::String& title, int width, int height);
  49. int getWidth() const override { return this->windowWidth; };
  50. int getHeight() const override { return this->windowHeight; };
  51. // Destructor
  52. ~Win32Window();
  53. // Interface
  54. void setFullScreen(bool enabled) override;
  55. bool isFullScreen() override { return this->windowState == 2; }
  56. void redraw(HWND& hwnd); // HWND is passed by argument because drawing might be called before the constructor has assigned it to this->hwnd
  57. void showCanvas() override;
  58. };
  59. static LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  60. static TCHAR windowClassName[] = _T("DfpsrWindowApplication");
  61. void Win32Window::updateTitle() {
  62. //XSetStandardProperties(this->display, this->window, this->title.toStdString().c_str(), "Icon", None, NULL, 0, NULL);
  63. }
  64. void Win32Window::setFullScreen(bool enabled) {
  65. if (this->windowState == 1 && enabled) {
  66. // Clean up any previous X11 window
  67. removeOldWindow();
  68. // Create the new window and graphics context
  69. this->createFullscreen();
  70. } else if (this->windowState == 2 && !enabled) {
  71. // Clean up any previous X11 window
  72. removeOldWindow();
  73. // Create the new window and graphics context
  74. this->createWindowed(this->title, 800, 600); // TODO: Remember the dimensions from last windowed mode
  75. }
  76. }
  77. void Win32Window::removeOldWindow() {
  78. if (this->windowState != 0) {
  79. DestroyWindow(this->hwnd);
  80. }
  81. this->windowState = 0;
  82. }
  83. void Win32Window::prepareWindow() {
  84. // Reallocate the canvas
  85. this->resizeCanvas(this->windowWidth, this->windowHeight);
  86. // Show the window
  87. ShowWindow(this->hwnd, SW_NORMAL);
  88. // Repaint
  89. UpdateWindow(this->hwnd);
  90. }
  91. void Win32Window::createWindow(const dsr::String& title, int width, int height) {
  92. // Request to resize the canvas and interface according to the new window
  93. this->windowWidth = width;
  94. this->windowHeight = height;
  95. this->receivedWindowResize(width, height);
  96. // The Window structure
  97. WNDCLASSEX wincl;
  98. memset(&wincl, 0, sizeof(WNDCLASSEX));
  99. wincl.hInstance = NULL;
  100. wincl.lpszClassName = windowClassName;
  101. wincl.lpfnWndProc = WindowProcedure;
  102. wincl.style = 0;
  103. wincl.cbSize = sizeof(WNDCLASSEX);
  104. // Use default icon and mouse-pointer
  105. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  106. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  107. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  108. wincl.lpszMenuName = NULL; // No menu
  109. wincl.cbClsExtra = 0; // No extra bytes after the window class
  110. wincl.cbWndExtra = sizeof(LPVOID); // structure or the window instance
  111. // Use Windows's default color as the background of the window
  112. wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND; // TODO: Make black
  113. // Register the window class, and if it fails quit the program
  114. if (!RegisterClassEx (&wincl)) {
  115. dsr::throwError("Call to RegisterClassEx failed!\n");
  116. }
  117. // The class is registered, let's create the program
  118. this->hwnd = CreateWindowEx(
  119. 0, // Extended possibilites for variation
  120. windowClassName, // Classname
  121. _T("MyWindow"), // Title Text
  122. WS_OVERLAPPEDWINDOW, // default window
  123. CW_USEDEFAULT, // Windows decides the position
  124. CW_USEDEFAULT, // where the window ends up on the screen
  125. width, // The programs width
  126. height, // and height in pixels
  127. HWND_DESKTOP, // The window is a child-window to desktop
  128. NULL, // No menu
  129. NULL, // Program Instance handler
  130. (LPVOID)this // Pointer to the window wrapper
  131. );
  132. // TODO: Set the title
  133. this->updateTitle();
  134. }
  135. void Win32Window::createWindowed(const dsr::String& title, int width, int height) {
  136. // Create the window
  137. this->createWindow(title, width, height);
  138. this->windowState = 1;
  139. this->prepareWindow();
  140. }
  141. void Win32Window::createFullscreen() {
  142. // TODO: Implement borderless, decorationless, maximized full-screen
  143. createWindowed("Full-screen is not yet supported on Windows", 800, 600);
  144. this->windowState = 2;
  145. this->prepareWindow();
  146. }
  147. Win32Window::Win32Window(const dsr::String& title, int width, int height) {
  148. bool fullScreen = false;
  149. if (width < 1 || height < 1) {
  150. fullScreen = true;
  151. }
  152. // Remember the title
  153. this->title = title;
  154. // Create a window
  155. if (fullScreen) {
  156. this->createFullscreen();
  157. } else {
  158. this->createWindowed(title, width, height);
  159. }
  160. }
  161. static dsr::DsrKey getDsrKey(WPARAM keyCode) {
  162. dsr::DsrKey result = dsr::DsrKey_Unhandled;
  163. if (keyCode == VK_ESCAPE) {
  164. result = dsr::DsrKey_Escape;
  165. } else if (keyCode == VK_F1) {
  166. result = dsr::DsrKey_F1;
  167. } else if (keyCode == VK_F2) {
  168. result = dsr::DsrKey_F2;
  169. } else if (keyCode == VK_F3) {
  170. result = dsr::DsrKey_F3;
  171. } else if (keyCode == VK_F4) {
  172. result = dsr::DsrKey_F4;
  173. } else if (keyCode == VK_F5) {
  174. result = dsr::DsrKey_F5;
  175. } else if (keyCode == VK_F6) {
  176. result = dsr::DsrKey_F6;
  177. } else if (keyCode == VK_F7) {
  178. result = dsr::DsrKey_F7;
  179. } else if (keyCode == VK_F8) {
  180. result = dsr::DsrKey_F8;
  181. } else if (keyCode == VK_F9) {
  182. result = dsr::DsrKey_F9;
  183. } else if (keyCode == VK_F10) {
  184. result = dsr::DsrKey_F10;
  185. } else if (keyCode == VK_F11) {
  186. result = dsr::DsrKey_F11;
  187. } else if (keyCode == VK_F12) {
  188. result = dsr::DsrKey_F12;
  189. } else if (keyCode == VK_PAUSE) {
  190. result = dsr::DsrKey_Pause;
  191. } else if (keyCode == VK_SPACE) {
  192. result = dsr::DsrKey_Space;
  193. } else if (keyCode == VK_TAB) {
  194. result = dsr::DsrKey_Tab;
  195. } else if (keyCode == VK_RETURN) {
  196. result = dsr::DsrKey_Return;
  197. } else if (keyCode == VK_BACK) {
  198. result = dsr::DsrKey_BackSpace;
  199. } else if (keyCode == VK_LSHIFT) {
  200. result = dsr::DsrKey_LeftShift;
  201. } else if (keyCode == VK_RSHIFT) {
  202. result = dsr::DsrKey_RightShift;
  203. } else if (keyCode == VK_LCONTROL) {
  204. result = dsr::DsrKey_LeftControl;
  205. } else if (keyCode == VK_RCONTROL) {
  206. result = dsr::DsrKey_RightControl;
  207. } else if (keyCode == VK_LMENU) {
  208. result = dsr::DsrKey_LeftAlt;
  209. } else if (keyCode == VK_RMENU) {
  210. result = dsr::DsrKey_RightAlt;
  211. } else if (keyCode == VK_DELETE) {
  212. result = dsr::DsrKey_Delete;
  213. } else if (keyCode == VK_LEFT) {
  214. result = dsr::DsrKey_LeftArrow;
  215. } else if (keyCode == VK_RIGHT) {
  216. result = dsr::DsrKey_RightArrow;
  217. } else if (keyCode == VK_UP) {
  218. result = dsr::DsrKey_UpArrow;
  219. } else if (keyCode == VK_DOWN) {
  220. result = dsr::DsrKey_DownArrow;
  221. } else if (keyCode == 0x30) {
  222. result = dsr::DsrKey_0;
  223. } else if (keyCode == 0x31) {
  224. result = dsr::DsrKey_1;
  225. } else if (keyCode == 0x32) {
  226. result = dsr::DsrKey_2;
  227. } else if (keyCode == 0x33) {
  228. result = dsr::DsrKey_3;
  229. } else if (keyCode == 0x34) {
  230. result = dsr::DsrKey_4;
  231. } else if (keyCode == 0x35) {
  232. result = dsr::DsrKey_5;
  233. } else if (keyCode == 0x36) {
  234. result = dsr::DsrKey_6;
  235. } else if (keyCode == 0x37) {
  236. result = dsr::DsrKey_7;
  237. } else if (keyCode == 0x38) {
  238. result = dsr::DsrKey_8;
  239. } else if (keyCode == 0x39) {
  240. result = dsr::DsrKey_9;
  241. } else if (keyCode == 0x41) {
  242. result = dsr::DsrKey_A;
  243. } else if (keyCode == 0x42) {
  244. result = dsr::DsrKey_B;
  245. } else if (keyCode == 0x43) {
  246. result = dsr::DsrKey_C;
  247. } else if (keyCode == 0x44) {
  248. result = dsr::DsrKey_D;
  249. } else if (keyCode == 0x45) {
  250. result = dsr::DsrKey_E;
  251. } else if (keyCode == 0x46) {
  252. result = dsr::DsrKey_F;
  253. } else if (keyCode == 0x47) {
  254. result = dsr::DsrKey_G;
  255. } else if (keyCode == 0x48) {
  256. result = dsr::DsrKey_H;
  257. } else if (keyCode == 0x49) {
  258. result = dsr::DsrKey_I;
  259. } else if (keyCode == 0x4A) {
  260. result = dsr::DsrKey_J;
  261. } else if (keyCode == 0x4B) {
  262. result = dsr::DsrKey_K;
  263. } else if (keyCode == 0x4C) {
  264. result = dsr::DsrKey_L;
  265. } else if (keyCode == 0x4D) {
  266. result = dsr::DsrKey_M;
  267. } else if (keyCode == 0x4E) {
  268. result = dsr::DsrKey_N;
  269. } else if (keyCode == 0x4F) {
  270. result = dsr::DsrKey_O;
  271. } else if (keyCode == 0x50) {
  272. result = dsr::DsrKey_P;
  273. } else if (keyCode == 0x51) {
  274. result = dsr::DsrKey_Q;
  275. } else if (keyCode == 0x52) {
  276. result = dsr::DsrKey_R;
  277. } else if (keyCode == 0x53) {
  278. result = dsr::DsrKey_S;
  279. } else if (keyCode == 0x54) {
  280. result = dsr::DsrKey_T;
  281. } else if (keyCode == 0x55) {
  282. result = dsr::DsrKey_U;
  283. } else if (keyCode == 0x56) {
  284. result = dsr::DsrKey_V;
  285. } else if (keyCode == 0x57) {
  286. result = dsr::DsrKey_W;
  287. } else if (keyCode == 0x58) {
  288. result = dsr::DsrKey_X;
  289. } else if (keyCode == 0x59) {
  290. result = dsr::DsrKey_Y;
  291. } else if (keyCode == 0x5A) {
  292. result = dsr::DsrKey_Z;
  293. }
  294. return result;
  295. }
  296. // Called from DispatchMessage via prefetchEvents
  297. static LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  298. // Get the Win32Window owning the given hwnd
  299. Win32Window *parent = nullptr;
  300. if (message == WM_CREATE) {
  301. // Cast the pointer argument into CREATESTRUCT and get the lParam given to the window on creation
  302. CREATESTRUCT *createStruct = (CREATESTRUCT*)lParam;
  303. parent = (Win32Window*)createStruct->lpCreateParams;
  304. if (parent == nullptr) {
  305. dsr::throwError("Null handle retreived from lParam (", (intptr_t)parent, ") in WM_CREATE message.\n");
  306. }
  307. SetWindowLongPtr(hwnd, GWLP_USERDATA, (intptr_t)parent);
  308. } else {
  309. // Get the parent
  310. parent = (Win32Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  311. if (parent == nullptr) {
  312. // Don't try to handle global events unrelated to any window
  313. return DefWindowProc(hwnd, message, wParam, lParam);
  314. }
  315. }
  316. // Check that we're using the correct window instance (This might not be the case while toggling full-screen)
  317. // Handle the message
  318. int result = 0;
  319. switch (message) {
  320. case -1:
  321. dsr::throwError("Unknown error in Window handler!\n");
  322. break;
  323. case WM_QUIT:
  324. PostQuitMessage(wParam);
  325. break;
  326. case WM_CLOSE:
  327. parent->queueInputEvent(new dsr::WindowEvent(dsr::WindowEventType::Close, parent->windowWidth, parent->windowHeight));
  328. DestroyWindow(hwnd);
  329. break;
  330. case WM_LBUTTONDOWN:
  331. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseDown, dsr::MouseKeyEnum::Left, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  332. break;
  333. case WM_LBUTTONUP:
  334. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseUp, dsr::MouseKeyEnum::Left, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  335. break;
  336. case WM_RBUTTONDOWN:
  337. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseDown, dsr::MouseKeyEnum::Right, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  338. break;
  339. case WM_RBUTTONUP:
  340. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseUp, dsr::MouseKeyEnum::Right, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  341. break;
  342. case WM_MBUTTONDOWN:
  343. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseDown, dsr::MouseKeyEnum::Middle, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  344. break;
  345. case WM_MBUTTONUP:
  346. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseUp, dsr::MouseKeyEnum::Middle, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  347. break;
  348. case WM_MOUSEMOVE:
  349. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseMove, dsr::MouseKeyEnum::NoKey, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  350. break;
  351. case WM_MOUSEWHEEL:
  352. {
  353. int delta = GET_WHEEL_DELTA_WPARAM(wParam);
  354. if (delta > 0) {
  355. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::Scroll, dsr::MouseKeyEnum::ScrollUp, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  356. } else if (delta < 0) {
  357. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::Scroll, dsr::MouseKeyEnum::ScrollDown, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  358. }
  359. }
  360. break;
  361. case WM_KEYDOWN: case WM_KEYUP:
  362. {
  363. char character = wParam; // System specific key-code
  364. dsr::DsrKey dsrKey = getDsrKey(wParam); // Portable key-code
  365. if (message == WM_KEYDOWN) {
  366. // Physical key down
  367. parent->queueInputEvent(new dsr::KeyboardEvent(dsr::KeyboardEventType::KeyDown, character, dsrKey));
  368. // First press typing
  369. parent->queueInputEvent(new dsr::KeyboardEvent(dsr::KeyboardEventType::KeyType, character, dsrKey));
  370. } else { // message == WM_KEYUP
  371. // Physical key up
  372. parent->queueInputEvent(new dsr::KeyboardEvent(dsr::KeyboardEventType::KeyUp, character, dsrKey));
  373. }
  374. }
  375. break;
  376. case WM_PAINT:
  377. parent->queueInputEvent(new dsr::WindowEvent(dsr::WindowEventType::Redraw, parent->windowWidth, parent->windowHeight));
  378. // BeginPaint and EndPaint must be called with the given hwnd to prevent having the redraw message sent again
  379. parent->redraw(hwnd);
  380. // Passing on the event to prevent flooding with more messages. This is only a temporary solution.
  381. // TODO: Avoid overwriting the result with any background color.
  382. //result = DefWindowProc(hwnd, message, wParam, lParam);
  383. break;
  384. case WM_SIZE:
  385. // If there's no size during minimization, don't try to resize the canvas
  386. if (wParam != SIZE_MINIMIZED) {
  387. int width = LOWORD(lParam);
  388. int height = HIWORD(lParam);
  389. parent->windowWidth = width;
  390. parent->windowHeight = height;
  391. parent->receivedWindowResize(width, height);
  392. }
  393. // Resize the window as requested
  394. result = DefWindowProc(hwnd, message, wParam, lParam);
  395. break;
  396. // TODO: Keyboard presses & typing
  397. default:
  398. result = DefWindowProc(hwnd, message, wParam, lParam);
  399. }
  400. return result;
  401. }
  402. void Win32Window::prefetchEvents() {
  403. MSG messages;
  404. // Windows hangs unless we process application events for each window
  405. while (PeekMessage(&messages, NULL, 0, 0, PM_REMOVE)) {
  406. //dsr::printText("Received an event ", messages.message, "(", messages.wParam, ", ", (intptr_t)messages.lParam, ")\n");
  407. TranslateMessage(&messages);
  408. DispatchMessage(&messages); // Calling WindowProcedure for each window instance
  409. }
  410. }
  411. // Locked because it overrides
  412. void Win32Window::resizeCanvas(int width, int height) {
  413. // Create a new canvas
  414. // Even thou Windows is using RGBA pack order for the window, the bitmap format used for drawing is using BGRA order
  415. this->canvas = dsr::image_create_RgbaU8_native(width, height, dsr::PackOrderIndex::BGRA);
  416. }
  417. Win32Window::~Win32Window() {
  418. // Destroy the native window
  419. DestroyWindow(this->hwnd);
  420. }
  421. void Win32Window::redraw(HWND& hwnd) {
  422. // Let the source bitmap use a padded width to safely handle the stride
  423. // Windows require 8-byte alignment, but the image format uses 16-byte alignment.
  424. int paddedWidth = dsr::image_getStride(this->canvas) / 4;
  425. int width = dsr::image_getWidth(this->canvas);
  426. int height = dsr::image_getHeight(this->canvas);
  427. InvalidateRect(this->hwnd, NULL, false);
  428. PAINTSTRUCT paintStruct;
  429. HDC targetContext = BeginPaint(this->hwnd, &paintStruct);
  430. BITMAPINFO bmi = {};
  431. bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
  432. bmi.bmiHeader.biWidth = paddedWidth;
  433. bmi.bmiHeader.biHeight = -height;
  434. bmi.bmiHeader.biPlanes = 1;
  435. bmi.bmiHeader.biBitCount = 32;
  436. bmi.bmiHeader.biCompression = BI_RGB;
  437. SetDIBitsToDevice(targetContext, 0, 0, paddedWidth, height, 0, 0, 0, height, dsr::image_dangerous_getData(this->canvas), &bmi, DIB_RGB_COLORS);
  438. EndPaint(this->hwnd, &paintStruct);
  439. }
  440. void Win32Window::showCanvas() {
  441. this->prefetchEvents();
  442. this->redraw(this->hwnd);
  443. }
  444. std::shared_ptr<dsr::BackendWindow> createBackendWindow(const dsr::String& title, int width, int height) {
  445. auto backend = std::make_shared<Win32Window>(title, width, height);
  446. return std::dynamic_pointer_cast<dsr::BackendWindow>(backend);
  447. }