Collection of DPF-based plugins for packaging
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.

490 lines
12KB

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