PolyWinCore.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyCore.h"
  22. #include "PolyInputKeys.h"
  23. #include "PolyInputEvent.h"
  24. #include "PolyRectangle.h"
  25. #include <winsock2.h>
  26. #include <windows.h>
  27. #include <windowsx.h>
  28. #include <WinUser.h>
  29. #include <MMSystem.h>
  30. #include <regstr.h>
  31. #include <vector>
  32. #ifndef VK_0
  33. #define VK_0 '0'
  34. #define VK_1 '1'
  35. #define VK_2 '2'
  36. #define VK_3 '3'
  37. #define VK_4 '4'
  38. #define VK_5 '5'
  39. #define VK_6 '6'
  40. #define VK_7 '7'
  41. #define VK_8 '8'
  42. #define VK_9 '9'
  43. #define VK_A 'A'
  44. #define VK_B 'B'
  45. #define VK_C 'C'
  46. #define VK_D 'D'
  47. #define VK_E 'E'
  48. #define VK_F 'F'
  49. #define VK_G 'G'
  50. #define VK_H 'H'
  51. #define VK_I 'I'
  52. #define VK_J 'J'
  53. #define VK_K 'K'
  54. #define VK_L 'L'
  55. #define VK_M 'M'
  56. #define VK_N 'N'
  57. #define VK_O 'O'
  58. #define VK_P 'P'
  59. #define VK_Q 'Q'
  60. #define VK_R 'R'
  61. #define VK_S 'S'
  62. #define VK_T 'T'
  63. #define VK_U 'U'
  64. #define VK_V 'V'
  65. #define VK_W 'W'
  66. #define VK_X 'X'
  67. #define VK_Y 'Y'
  68. #define VK_Z 'Z'
  69. #endif /* VK_0 */
  70. /* These keys haven't been defined, but were experimentally determined */
  71. #define VK_SEMICOLON 0xBA
  72. #define VK_EQUALS 0xBB
  73. #define VK_COMMA 0xBC
  74. #define VK_MINUS 0xBD
  75. #define VK_PERIOD 0xBE
  76. #define VK_SLASH 0xBF
  77. #define VK_GRAVE 0xC0
  78. #define VK_LBRACKET 0xDB
  79. #define VK_BACKSLASH 0xDC
  80. #define VK_RBRACKET 0xDD
  81. #define VK_APOSTROPHE 0xDE
  82. #define VK_BACKTICK 0xDF
  83. #define VK_OEM_102 0xE2
  84. #define EXTENDED_KEYMASK (1<<24)
  85. #ifdef _MINGW
  86. #define NO_TOUCH_API 1
  87. #endif
  88. #define POLYCODE_CORE Win32Core
  89. namespace Polycode {
  90. class _PolyExport Win32Mutex : public CoreMutex {
  91. public:
  92. HANDLE winMutex;
  93. };
  94. class _PolyExport Win32Event {
  95. public:
  96. int eventGroup;
  97. int eventCode;
  98. int mouseX;
  99. int mouseY;
  100. TouchInfo touch;
  101. std::vector<TouchInfo> touches;
  102. PolyKEY keyCode;
  103. wchar_t unicodeChar;
  104. char mouseButton;
  105. static const int INPUT_EVENT = 0;
  106. };
  107. class HIDGamepadAxis {
  108. public:
  109. //IOHIDElementCookie cookie;
  110. //CFIndex logicalMin;
  111. //CFIndex logicalMax;
  112. bool hasNullState;
  113. bool isHatSwitch;
  114. bool isHatSwitchSecondAxis;
  115. };
  116. class HIDGamepadButton {
  117. public:
  118. //IOHIDElementCookie cookie;
  119. int something;
  120. };
  121. class Gamepad_devicePrivate {
  122. public:
  123. UINT joystickID;
  124. JOYINFOEX lastState;
  125. int xAxisIndex;
  126. int yAxisIndex;
  127. int zAxisIndex;
  128. int rAxisIndex;
  129. int uAxisIndex;
  130. int vAxisIndex;
  131. int povXAxisIndex;
  132. int povYAxisIndex;
  133. UINT (* axisRanges)[2];
  134. };
  135. class GamepadDeviceEntry {
  136. public:
  137. GamepadDeviceEntry() {
  138. numAxes = 0;
  139. }
  140. std::vector<HIDGamepadAxis> axisElements;
  141. std::vector<HIDGamepadButton> buttonElements;
  142. unsigned int deviceID;
  143. //IOHIDDeviceRef device;
  144. unsigned int numAxes;
  145. unsigned int numButtons;
  146. Gamepad_devicePrivate *privateData;
  147. // CoreInput *input;
  148. };
  149. class _PolyExport Win32Core : public Core {
  150. public:
  151. Win32Core(PolycodeViewBase *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex = -1);
  152. ~Win32Core();
  153. void enableMouse(bool newval);
  154. void warpCursor(int x, int y);
  155. unsigned int getTicks();
  156. bool Update();
  157. void setVSync(bool vSyncVal);
  158. void handleKeyDown(LPARAM lParam, WPARAM wParam, wchar_t unicodeChar);
  159. void handleKeyUp(LPARAM lParam, WPARAM wParam);
  160. void handleMouseMove(LPARAM lParam, WPARAM wParam);
  161. void handleMouseWheel(LPARAM lParam, WPARAM wParam);
  162. void handleMouseDown(int mouseCode,LPARAM lParam, WPARAM wParam);
  163. void handleMouseUp(int mouseCode,LPARAM lParam, WPARAM wParam);
  164. void handleTouchEvent(LPARAM lParam, WPARAM wParam);
  165. bool isMultiTouchEnabled() { return hasMultiTouch; }
  166. void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel);
  167. void initContext(bool usePixelFormat, unsigned int pixelFormat);
  168. void destroyContext();
  169. void createThread(Threaded *target);
  170. PolyKEY mapKey(LPARAM lParam, WPARAM wParam);
  171. void lockMutex(CoreMutex *mutex);
  172. void unlockMutex(CoreMutex *mutex);
  173. CoreMutex *createMutex();
  174. void checkEvents();
  175. void initKeymap();
  176. void platformSleep(int msecs);
  177. std::vector<Polycode::Rectangle> getVideoModes();
  178. void handleAxisChange(GamepadDeviceEntry * device, int axisIndex, DWORD value);
  179. void handleButtonChange(GamepadDeviceEntry * device, DWORD lastValue, DWORD value);
  180. void handlePOVChange(GamepadDeviceEntry * device, DWORD lastValue, DWORD value);
  181. void detectGamepads();
  182. void initGamepad();
  183. void shutdownGamepad();
  184. void Gamepad_processEvents();
  185. void initTouch();
  186. void handleViewResize(int width, int height);
  187. // NEED TO IMPLEMENT:
  188. String executeExternalCommand(String command) { return "";}
  189. void openURL(String url) {}
  190. void setCursor(int cursorType){ }
  191. void copyStringToClipboard(const String& str) { }
  192. String getClipboardString() { return ""; }
  193. void createFolder(const String& folderPath) {}
  194. void copyDiskItem(const String& itemPath, const String& destItemPath) {}
  195. void moveDiskItem(const String& itemPath, const String& destItemPath) {}
  196. String openFolderPicker() { return "";}
  197. void removeDiskItem(const String& itemPath) {}
  198. std::vector<String> openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple) { std::vector<String> ret; return ret;}
  199. void resizeTo(int xRes, int yRes) { }
  200. std::vector<GamepadDeviceEntry*> gamepads;
  201. HWND hWnd;
  202. private:
  203. unsigned int nextDeviceID;
  204. PolyKEY keyMap[1024];
  205. unsigned int lastGamepadDetect;
  206. std::vector<Win32Event> win32Events;
  207. void initMultisample(int numSamples);
  208. int lastMouseX;
  209. int lastMouseY;
  210. bool isFullScreen;
  211. HDC hDC;
  212. HGLRC hRC;
  213. unsigned int PixelFormat;
  214. PIXELFORMATDESCRIPTOR pfd;
  215. // Tracks whether the system supports multitouch at runtime
  216. bool hasMultiTouch;
  217. #ifndef NO_TOUCH_API
  218. // Create generic reference to any multitouch functions we need, so that we
  219. // can make them available at runtime if the operating system supports it
  220. // while still allowing fallback for older systems
  221. // See: http://msdn.microsoft.com/en-us/library/ms683212(v=vs.85).aspx
  222. typedef bool (WINAPI *GetTouchInputInfoType)(HTOUCHINPUT,UINT,PTOUCHINPUT,int);
  223. GetTouchInputInfoType GetTouchInputInfoFunc;
  224. #endif
  225. };
  226. }