DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

566 lines
13KB

  1. /*
  2. Copyright 2012-2014 David Robillard <http://drobilla.net>
  3. Copyright 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. /**
  16. @file pugl_win.cpp Windows/WGL Pugl Implementation.
  17. */
  18. #include <ctime>
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <winsock2.h>
  22. #include <windows.h>
  23. #include <windowsx.h>
  24. #ifdef PUGL_CAIRO
  25. #include <cairo/cairo.h>
  26. #include <cairo/cairo-win32.h>
  27. #endif
  28. #ifdef PUGL_OPENGL
  29. #include <GL/gl.h>
  30. #endif
  31. #include "pugl_internal.h"
  32. #ifndef WM_MOUSEWHEEL
  33. # define WM_MOUSEWHEEL 0x020A
  34. #endif
  35. #ifndef WM_MOUSEHWHEEL
  36. # define WM_MOUSEHWHEEL 0x020E
  37. #endif
  38. #ifndef WHEEL_DELTA
  39. # define WHEEL_DELTA 120
  40. #endif
  41. #ifndef GWLP_USERDATA
  42. # define GWLP_USERDATA (-21)
  43. #endif
  44. #define PUGL_LOCAL_CLOSE_MSG (WM_USER + 50)
  45. HINSTANCE hInstance = NULL;
  46. struct PuglInternalsImpl {
  47. HWND hwnd;
  48. #ifdef PUGL_OPENGL
  49. HDC hdc;
  50. HGLRC hglrc;
  51. #endif
  52. #ifdef PUGL_CAIRO
  53. cairo_t* buffer_cr;
  54. cairo_surface_t* buffer_surface;
  55. #endif
  56. HDC paintHdc;
  57. WNDCLASS wc;
  58. };
  59. LRESULT CALLBACK
  60. wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  61. #if 0
  62. extern "C" {
  63. BOOL WINAPI
  64. DllMain(HINSTANCE hInst, DWORD, LPVOID)
  65. {
  66. hInstance = hInst;
  67. return 1;
  68. }
  69. } // extern "C"
  70. #endif
  71. PuglInternals*
  72. puglInitInternals()
  73. {
  74. return (PuglInternals*)calloc(1, sizeof(PuglInternals));
  75. }
  76. void
  77. puglEnterContext(PuglView* view)
  78. {
  79. #ifdef PUGL_OPENGL
  80. wglMakeCurrent(view->impl->hdc, view->impl->hglrc);
  81. #endif
  82. }
  83. void
  84. puglLeaveContext(PuglView* view, bool flush)
  85. {
  86. #ifdef PUGL_OPENGL
  87. if (flush) {
  88. glFlush();
  89. SwapBuffers(view->impl->hdc);
  90. }
  91. wglMakeCurrent(NULL, NULL);
  92. #endif
  93. }
  94. int
  95. puglCreateWindow(PuglView* view, const char* title)
  96. {
  97. PuglInternals* impl = view->impl;
  98. if (!title) {
  99. title = "Window";
  100. }
  101. // FIXME: This is nasty, and pugl should not have static anything.
  102. // Should class be a parameter? Does this make sense on other platforms?
  103. static int wc_count = 0;
  104. char classNameBuf[256];
  105. std::srand((std::time(NULL)));
  106. #ifdef __WINE__
  107. std::snprintf(classNameBuf, sizeof(classNameBuf), "%s_%d-%d", title, std::rand(), ++wc_count);
  108. #else
  109. _snprintf(classNameBuf, sizeof(classNameBuf), "%s_%d-%d", title, std::rand(), ++wc_count);
  110. #endif
  111. classNameBuf[sizeof(classNameBuf)-1] = '\0';
  112. impl->wc.style = CS_OWNDC;
  113. impl->wc.lpfnWndProc = wndProc;
  114. impl->wc.cbClsExtra = 0;
  115. impl->wc.cbWndExtra = 0;
  116. impl->wc.hInstance = hInstance;
  117. impl->wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
  118. impl->wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
  119. impl->wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  120. impl->wc.lpszMenuName = NULL;
  121. impl->wc.lpszClassName = strdup(classNameBuf);
  122. if (!RegisterClass(&impl->wc)) {
  123. free((void*)impl->wc.lpszClassName);
  124. free(impl);
  125. return 1;
  126. }
  127. int winFlags = WS_POPUPWINDOW | WS_CAPTION;
  128. if (view->user_resizable) {
  129. winFlags |= WS_SIZEBOX;
  130. if (view->min_width > 0 && view->min_height > 0) {
  131. // Adjust the minimum window size to accomodate requested view size
  132. RECT mr = { 0, 0, view->min_width, view->min_height };
  133. AdjustWindowRectEx(&mr, view->parent ? WS_CHILD : winFlags, FALSE, WS_EX_TOPMOST);
  134. view->min_width = mr.right - mr.left;
  135. view->min_height = mr.bottom - mr.top;
  136. }
  137. }
  138. // Adjust the window size to accomodate requested view size
  139. RECT wr = { 0, 0, view->width, view->height };
  140. AdjustWindowRectEx(&wr, view->parent ? WS_CHILD : winFlags, FALSE, WS_EX_TOPMOST);
  141. impl->hwnd = CreateWindowEx(
  142. WS_EX_TOPMOST,
  143. classNameBuf, title,
  144. view->parent ? (WS_CHILD | WS_VISIBLE) : winFlags,
  145. CW_USEDEFAULT, CW_USEDEFAULT, wr.right-wr.left, wr.bottom-wr.top,
  146. (HWND)view->parent, NULL, hInstance, NULL);
  147. if (!impl->hwnd) {
  148. UnregisterClass(impl->wc.lpszClassName, NULL);
  149. free((void*)impl->wc.lpszClassName);
  150. free(impl);
  151. return 1;
  152. }
  153. SetWindowLongPtr(impl->hwnd, GWLP_USERDATA, (LONG_PTR)view);
  154. #ifdef PUGL_OPENGL
  155. impl->hdc = GetDC(impl->hwnd);
  156. PIXELFORMATDESCRIPTOR pfd;
  157. ZeroMemory(&pfd, sizeof(pfd));
  158. pfd.nSize = sizeof(pfd);
  159. pfd.nVersion = 1;
  160. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  161. pfd.iPixelType = PFD_TYPE_RGBA;
  162. pfd.cColorBits = 24;
  163. pfd.cDepthBits = 16;
  164. pfd.iLayerType = PFD_MAIN_PLANE;
  165. int format = ChoosePixelFormat(impl->hdc, &pfd);
  166. SetPixelFormat(impl->hdc, format, &pfd);
  167. impl->hglrc = wglCreateContext(impl->hdc);
  168. if (!impl->hglrc) {
  169. ReleaseDC (impl->hwnd, impl->hdc);
  170. DestroyWindow (impl->hwnd);
  171. UnregisterClass (impl->wc.lpszClassName, NULL);
  172. free((void*)impl->wc.lpszClassName);
  173. free(impl);
  174. return 1;
  175. }
  176. #endif
  177. return PUGL_SUCCESS;
  178. }
  179. void
  180. puglShowWindow(PuglView* view)
  181. {
  182. ShowWindow(view->impl->hwnd, SW_SHOWNORMAL);
  183. }
  184. void
  185. puglHideWindow(PuglView* view)
  186. {
  187. ShowWindow(view->impl->hwnd, SW_HIDE);
  188. }
  189. void
  190. puglDestroy(PuglView* view)
  191. {
  192. if (!view) {
  193. return;
  194. }
  195. PuglInternals* const impl = view->impl;
  196. #ifdef PUGL_OPENGL
  197. wglMakeCurrent(NULL, NULL);
  198. wglDeleteContext(impl->hglrc);
  199. ReleaseDC(impl->hwnd, impl->hdc);
  200. #endif
  201. #ifdef PUGL_CAIRO
  202. cairo_destroy(impl->buffer_cr);
  203. cairo_surface_destroy(impl->buffer_surface);
  204. #endif
  205. DestroyWindow(impl->hwnd);
  206. UnregisterClass(impl->wc.lpszClassName, NULL);
  207. free((void*)impl->wc.lpszClassName);
  208. free(impl);
  209. free(view);
  210. }
  211. static void
  212. puglReshape(PuglView* view, int width, int height)
  213. {
  214. puglEnterContext(view);
  215. if (view->reshapeFunc) {
  216. view->reshapeFunc(view, width, height);
  217. } else {
  218. puglDefaultReshape(width, height);
  219. }
  220. view->width = width;
  221. view->height = height;
  222. }
  223. static void
  224. puglDisplay(PuglView* view)
  225. {
  226. PuglInternals* impl = view->impl;
  227. bool success = true;
  228. puglEnterContext(view);
  229. #ifdef PUGL_CAIRO
  230. cairo_t *wc = NULL;
  231. cairo_t *bc = NULL;
  232. cairo_surface_t *ws = NULL;
  233. cairo_surface_t *bs = NULL;
  234. HDC hdc = impl->paintHdc;
  235. bc = impl->buffer_cr;
  236. bs = impl->buffer_surface;
  237. int w = view->width;
  238. int h = view->height;
  239. int bw = bs ? cairo_image_surface_get_width(bs) : -1;
  240. int bh = bs ? cairo_image_surface_get_height(bs) : -1;
  241. ws = hdc ? cairo_win32_surface_create(hdc) : NULL;
  242. wc = ws ? cairo_create(ws) : NULL;
  243. if (wc && (!bc || bw != w || bh != h)) {
  244. cairo_destroy(bc);
  245. cairo_surface_destroy(bs);
  246. bs = cairo_surface_create_similar_image(ws, CAIRO_FORMAT_ARGB32, w, h);
  247. bc = bs ? cairo_create(bs) : NULL;
  248. impl->buffer_cr = bc;
  249. impl->buffer_surface = bs;
  250. }
  251. success = wc != NULL && bc != NULL;
  252. #endif
  253. if (success) {
  254. view->redisplay = false;
  255. if (view->displayFunc) {
  256. view->displayFunc(view);
  257. }
  258. #ifdef PUGL_CAIRO
  259. cairo_set_source_surface(wc, bs, 0, 0);
  260. cairo_paint(wc);
  261. #endif
  262. }
  263. puglLeaveContext(view, success);
  264. #ifdef PUGL_CAIRO
  265. cairo_destroy(wc);
  266. cairo_surface_destroy(ws);
  267. #endif
  268. return;
  269. (void)impl;
  270. }
  271. static PuglKey
  272. keySymToSpecial(int sym)
  273. {
  274. switch (sym) {
  275. case VK_F1: return PUGL_KEY_F1;
  276. case VK_F2: return PUGL_KEY_F2;
  277. case VK_F3: return PUGL_KEY_F3;
  278. case VK_F4: return PUGL_KEY_F4;
  279. case VK_F5: return PUGL_KEY_F5;
  280. case VK_F6: return PUGL_KEY_F6;
  281. case VK_F7: return PUGL_KEY_F7;
  282. case VK_F8: return PUGL_KEY_F8;
  283. case VK_F9: return PUGL_KEY_F9;
  284. case VK_F10: return PUGL_KEY_F10;
  285. case VK_F11: return PUGL_KEY_F11;
  286. case VK_F12: return PUGL_KEY_F12;
  287. case VK_LEFT: return PUGL_KEY_LEFT;
  288. case VK_UP: return PUGL_KEY_UP;
  289. case VK_RIGHT: return PUGL_KEY_RIGHT;
  290. case VK_DOWN: return PUGL_KEY_DOWN;
  291. case VK_PRIOR: return PUGL_KEY_PAGE_UP;
  292. case VK_NEXT: return PUGL_KEY_PAGE_DOWN;
  293. case VK_HOME: return PUGL_KEY_HOME;
  294. case VK_END: return PUGL_KEY_END;
  295. case VK_INSERT: return PUGL_KEY_INSERT;
  296. case VK_SHIFT: return PUGL_KEY_SHIFT;
  297. case VK_CONTROL: return PUGL_KEY_CTRL;
  298. case VK_MENU: return PUGL_KEY_ALT;
  299. case VK_LWIN: return PUGL_KEY_SUPER;
  300. case VK_RWIN: return PUGL_KEY_SUPER;
  301. }
  302. return (PuglKey)0;
  303. }
  304. static void
  305. processMouseEvent(PuglView* view, int button, bool press, LPARAM lParam)
  306. {
  307. view->event_timestamp_ms = GetMessageTime();
  308. if (press) {
  309. SetCapture(view->impl->hwnd);
  310. } else {
  311. ReleaseCapture();
  312. }
  313. if (view->mouseFunc) {
  314. view->mouseFunc(view, button, press,
  315. GET_X_LPARAM(lParam),
  316. GET_Y_LPARAM(lParam));
  317. }
  318. }
  319. static void
  320. setModifiers(PuglView* view)
  321. {
  322. view->mods = 0;
  323. view->mods |= (GetKeyState(VK_SHIFT) < 0) ? PUGL_MOD_SHIFT : 0;
  324. view->mods |= (GetKeyState(VK_CONTROL) < 0) ? PUGL_MOD_CTRL : 0;
  325. view->mods |= (GetKeyState(VK_MENU) < 0) ? PUGL_MOD_ALT : 0;
  326. view->mods |= (GetKeyState(VK_LWIN) < 0) ? PUGL_MOD_SUPER : 0;
  327. view->mods |= (GetKeyState(VK_RWIN) < 0) ? PUGL_MOD_SUPER : 0;
  328. }
  329. static LRESULT
  330. handleMessage(PuglView* view, UINT message, WPARAM wParam, LPARAM lParam)
  331. {
  332. PAINTSTRUCT ps;
  333. PuglKey key;
  334. RECT rect;
  335. MINMAXINFO* mmi;
  336. setModifiers(view);
  337. switch (message) {
  338. case WM_CREATE:
  339. case WM_SHOWWINDOW:
  340. case WM_SIZE:
  341. GetClientRect(view->impl->hwnd, &rect);
  342. puglReshape(view, rect.right, rect.bottom);
  343. break;
  344. case WM_GETMINMAXINFO:
  345. mmi = (MINMAXINFO*)lParam;
  346. mmi->ptMinTrackSize.x = view->min_width;
  347. mmi->ptMinTrackSize.y = view->min_height;
  348. break;
  349. case WM_PAINT:
  350. view->impl->paintHdc = BeginPaint(view->impl->hwnd, &ps);
  351. puglDisplay(view);
  352. view->impl->paintHdc = NULL;
  353. EndPaint(view->impl->hwnd, &ps);
  354. break;
  355. case WM_MOUSEMOVE:
  356. if (view->motionFunc) {
  357. view->event_timestamp_ms = GetMessageTime();
  358. view->motionFunc(view, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  359. }
  360. break;
  361. case WM_LBUTTONDOWN:
  362. processMouseEvent(view, 1, true, lParam);
  363. break;
  364. case WM_MBUTTONDOWN:
  365. processMouseEvent(view, 2, true, lParam);
  366. break;
  367. case WM_RBUTTONDOWN:
  368. processMouseEvent(view, 3, true, lParam);
  369. break;
  370. case WM_LBUTTONUP:
  371. processMouseEvent(view, 1, false, lParam);
  372. break;
  373. case WM_MBUTTONUP:
  374. processMouseEvent(view, 2, false, lParam);
  375. break;
  376. case WM_RBUTTONUP:
  377. processMouseEvent(view, 3, false, lParam);
  378. break;
  379. case WM_MOUSEWHEEL:
  380. if (view->scrollFunc) {
  381. view->event_timestamp_ms = GetMessageTime();
  382. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  383. ScreenToClient(view->impl->hwnd, &pt);
  384. view->scrollFunc(
  385. view, pt.x, pt.y,
  386. 0.0f, GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA);
  387. }
  388. break;
  389. case WM_MOUSEHWHEEL:
  390. if (view->scrollFunc) {
  391. view->event_timestamp_ms = GetMessageTime();
  392. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  393. ScreenToClient(view->impl->hwnd, &pt);
  394. view->scrollFunc(
  395. view, pt.x, pt.y,
  396. GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA, 0.0f);
  397. }
  398. break;
  399. case WM_KEYDOWN:
  400. if (view->ignoreKeyRepeat && (lParam & (1 << 30))) {
  401. break;
  402. } // else nobreak
  403. case WM_KEYUP:
  404. view->event_timestamp_ms = GetMessageTime();
  405. if ((key = keySymToSpecial(wParam))) {
  406. if (view->specialFunc) {
  407. view->specialFunc(view, message == WM_KEYDOWN, key);
  408. }
  409. } else if (view->keyboardFunc) {
  410. static BYTE kbs[256];
  411. if (GetKeyboardState(kbs) != FALSE) {
  412. char lb[2];
  413. UINT scanCode = (lParam >> 8) & 0xFFFFFF00;
  414. if ( 1 == ToAscii(wParam, scanCode, kbs, (LPWORD)lb, 0)) {
  415. view->keyboardFunc(view, message == WM_KEYDOWN, (char)lb[0]);
  416. }
  417. }
  418. }
  419. break;
  420. case WM_QUIT:
  421. case PUGL_LOCAL_CLOSE_MSG:
  422. if (view->closeFunc) {
  423. view->closeFunc(view);
  424. view->redisplay = false;
  425. }
  426. break;
  427. default:
  428. return DefWindowProc(
  429. view->impl->hwnd, message, wParam, lParam);
  430. }
  431. return 0;
  432. }
  433. void
  434. puglGrabFocus(PuglView* /*view*/)
  435. {
  436. // TODO
  437. }
  438. PuglStatus
  439. puglProcessEvents(PuglView* view)
  440. {
  441. MSG msg;
  442. while (PeekMessage(&msg, view->impl->hwnd, 0, 0, PM_REMOVE)) {
  443. handleMessage(view, msg.message, msg.wParam, msg.lParam);
  444. }
  445. if (view->redisplay) {
  446. InvalidateRect(view->impl->hwnd, NULL, FALSE);
  447. }
  448. return PUGL_SUCCESS;
  449. }
  450. LRESULT CALLBACK
  451. wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  452. {
  453. PuglView* view = (PuglView*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  454. switch (message) {
  455. case WM_CREATE:
  456. PostMessage(hwnd, WM_SHOWWINDOW, TRUE, 0);
  457. return 0;
  458. case WM_CLOSE:
  459. PostMessage(hwnd, PUGL_LOCAL_CLOSE_MSG, wParam, lParam);
  460. return 0;
  461. case WM_DESTROY:
  462. return 0;
  463. default:
  464. if (view && hwnd == view->impl->hwnd) {
  465. return handleMessage(view, message, wParam, lParam);
  466. } else {
  467. return DefWindowProc(hwnd, message, wParam, lParam);
  468. }
  469. }
  470. }
  471. void
  472. puglPostRedisplay(PuglView* view)
  473. {
  474. view->redisplay = true;
  475. }
  476. PuglNativeWindow
  477. puglGetNativeWindow(PuglView* view)
  478. {
  479. return (PuglNativeWindow)view->impl->hwnd;
  480. }
  481. void*
  482. puglGetContext(PuglView* view)
  483. {
  484. #ifdef PUGL_CAIRO
  485. return view->impl->buffer_cr;
  486. #endif
  487. return NULL;
  488. // may be unused
  489. (void)view;
  490. }
  491. int
  492. puglUpdateGeometryConstraints(PuglView* view, int min_width, int min_height, bool aspect)
  493. {
  494. // TODO
  495. return 1;
  496. (void)view;
  497. (void)min_width;
  498. (void)min_height;
  499. (void)aspect;
  500. }