Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. {-# LANGUAGE ScopedTypeVariables #-}
  2. {-# LANGUAGE QuasiQuotes #-}
  3. {-# LANGUAGE TemplateHaskell #-}
  4.  
  5. import qualified Graphics.Win32
  6. import System.Win32.DLL (getModuleHandle)
  7. import Control.Exception (SomeException, catch)
  8. import System.Exit (ExitCode(ExitSuccess), exitWith)
  9. import Foreign
  10. import Data.Bits
  11.  
  12. import Graphics.Win32.Key
  13. import qualified Language.C.Inline as C
  14.  
  15. C.include "windows.h"
  16.  
  17. createWindow :: Int -> Int -> Graphics.Win32.WindowClosure -> IO Graphics.Win32.HWND
  18. createWindow width height wndProc = do
  19. let winClass = Graphics.Win32.mkClassName "Hello"
  20. icon <- Graphics.Win32.loadIcon Nothing Graphics.Win32.iDI_APPLICATION
  21. cursor <- Graphics.Win32.loadCursor Nothing Graphics.Win32.iDC_ARROW
  22. bgBrush <- Graphics.Win32.createSolidBrush (Graphics.Win32.rgb 0 0 255)
  23. mainInstance <- getModuleHandle Nothing
  24. Graphics.Win32.registerClass
  25. ( Graphics.Win32.cS_VREDRAW + Graphics.Win32.cS_HREDRAW
  26. , mainInstance
  27. , Just icon
  28. , Just cursor
  29. , Just bgBrush
  30. , Nothing
  31. , winClass
  32. )
  33. w <- Graphics.Win32.createWindow
  34. winClass
  35. "Hello, World example"
  36. Graphics.Win32.wS_OVERLAPPEDWINDOW
  37. Nothing Nothing -- leave it to the shell to decide the position
  38. -- at where to put the window initially
  39. (Just width)
  40. (Just height)
  41. Nothing -- no parent, i.e, root window is the parent.
  42. Nothing -- no menu handle
  43. mainInstance
  44. wndProc
  45. Graphics.Win32.showWindow w Graphics.Win32.sW_SHOWNORMAL
  46. Graphics.Win32.updateWindow w
  47. return w
  48.  
  49. wndProc :: Graphics.Win32.HWND
  50. -> Graphics.Win32.WindowMessage
  51. -> Graphics.Win32.WPARAM
  52. -> Graphics.Win32.LPARAM
  53. -> IO Graphics.Win32.LRESULT
  54. wndProc hwnd wmsg wParam lParam
  55. | wmsg == Graphics.Win32.wM_DESTROY = do
  56. Graphics.Win32.sendMessage hwnd Graphics.Win32.wM_QUIT 1 0
  57. return 0
  58. | wmsg == 0x00FF = do -- WM_INPUT
  59. alloca $ \(x :: Ptr C.CInt) ->
  60. alloca $ \(y :: Ptr C.CInt) -> [C.block|
  61. void
  62. {
  63. UINT dwSize;
  64. GetRawInputData((HRAWINPUT)$(intptr_t lParam), RID_INPUT, NULL, &dwSize,
  65. sizeof(RAWINPUTHEADER));
  66. BYTE lpb[dwSize];
  67.  
  68. if (lpb == NULL)
  69. {
  70. return;
  71. }
  72.  
  73. if (GetRawInputData((HRAWINPUT)$(intptr_t lParam), RID_INPUT, lpb, &dwSize,
  74. sizeof(RAWINPUTHEADER)) != dwSize );
  75.  
  76. RAWINPUT* raw = (RAWINPUT*)lpb;
  77.  
  78. if (raw->header.dwType == RIM_TYPEMOUSE && raw->data.mouse.usFlags == 0)
  79. {
  80. $(int *x) = raw->data.mouse.lLastX;
  81. $(int *y) = raw->data.mouse.lLastY;
  82. }
  83. } |] >> do
  84. x' <- peek x
  85. y' <- peek y
  86. print [x', y']
  87. return 0
  88.  
  89. | otherwise =
  90. Graphics.Win32.defWindowProc (Just hwnd) wmsg wParam lParam
  91.  
  92. msgPump :: Graphics.Win32.HWND
  93. -> IO ()
  94. msgPump hwnd = Graphics.Win32.allocaMessage $ \ msg ->
  95. let pump = do
  96. Graphics.Win32.getMessage msg (Just hwnd)
  97. `catch` \(_::SomeException) -> exitWith ExitSuccess
  98. Graphics.Win32.translateMessage msg
  99. Graphics.Win32.dispatchMessage msg
  100. pump
  101. in pump
  102.  
  103. main :: IO ()
  104. main = do
  105. putStrLn "Hello World"
  106. hWnd <- createWindow 300 400 wndProc
  107. [C.block| void {
  108. RAWINPUTDEVICE Rid[1];
  109. Rid[0].usUsagePage = 0x01;
  110. Rid[0].usUsage = 0x02;
  111. Rid[0].dwFlags = RIDEV_INPUTSINK | RIDEV_NOLEGACY;
  112. Rid[0].hwndTarget = $(void *hWnd);
  113. RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]));
  114. }|]
  115. msgPump hWnd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement