PolyCocoaCore.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyLogger.h"
  23. #include "PolyCore.h"
  24. #include "PolyString.h"
  25. #include "PolyRectangle.h"
  26. #include <vector>
  27. #include <OpenGL/gl.h>
  28. #include <OpenGL/OpenGL.h>
  29. #include "PolyGLRenderer.h"
  30. #include <mach/mach_time.h>
  31. #include <unistd.h>
  32. #include "PolyInputEvent.h"
  33. #include "PolyGLSLShaderModule.h"
  34. #include <IOKit/hid/IOHIDLib.h>
  35. #import <Cocoa/Cocoa.h>
  36. #define POLYCODE_CORE CocoaCore
  37. using std::vector;
  38. @class PolycodeView;
  39. namespace Polycode {
  40. class _PolyExport PosixMutex : public CoreMutex {
  41. public:
  42. pthread_mutex_t pMutex;
  43. };
  44. class CocoaEvent {
  45. public:
  46. int eventGroup;
  47. int eventCode;
  48. int mouseX;
  49. int mouseY;
  50. std::vector<TouchInfo> touches;
  51. TouchInfo touch;
  52. PolyKEY keyCode;
  53. wchar_t unicodeChar;
  54. char mouseButton;
  55. static const int EVENTBASE_PLATFORMEVENT = 0x300;
  56. static const int INPUT_EVENT = EVENTBASE_PLATFORMEVENT+0;
  57. static const int FOCUS_EVENT = EVENTBASE_PLATFORMEVENT+1;
  58. };
  59. class HIDGamepadAxis {
  60. public:
  61. IOHIDElementCookie cookie;
  62. CFIndex logicalMin;
  63. CFIndex logicalMax;
  64. bool hasNullState;
  65. bool isHatSwitch;
  66. bool isHatSwitchSecondAxis;
  67. };
  68. class HIDGamepadButton {
  69. public:
  70. IOHIDElementCookie cookie;
  71. };
  72. class GamepadDeviceEntry {
  73. public:
  74. GamepadDeviceEntry() {
  75. numAxes = 0;
  76. numButtons = 0;
  77. }
  78. vector<HIDGamepadAxis> axisElements;
  79. vector<HIDGamepadButton> buttonElements;
  80. unsigned int deviceID;
  81. IOHIDDeviceRef device;
  82. unsigned int numAxes;
  83. unsigned int numButtons;
  84. CoreInput *input;
  85. };
  86. class VideoModeChangeInfo {
  87. public:
  88. int xRes;
  89. int yRes;
  90. bool fullScreen;
  91. bool vSync;
  92. int aaLevel;
  93. int anisotropyLevel;
  94. bool needResolutionChange;
  95. };
  96. class _PolyExport CocoaCore : public Core {
  97. public:
  98. CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex=-1, bool retinaSupport=false);
  99. virtual ~CocoaCore();
  100. void enableMouse(bool newval);
  101. unsigned int getTicks();
  102. bool systemUpdate();
  103. void Render();
  104. void setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, bool retinaSupport = true);
  105. void resizeTo(int xRes, int yRes);
  106. void createThread(Threaded *target);
  107. void createFolder(const String& folderPath);
  108. void copyDiskItem(const String& itemPath, const String& destItemPath);
  109. void moveDiskItem(const String& itemPath, const String& destItemPath);
  110. void removeDiskItem(const String& itemPath);
  111. String openFolderPicker();
  112. vector<String> openFilePicker(vector<CoreFileExtension> extensions, bool allowMultiple);
  113. String saveFilePicker(std::vector<CoreFileExtension> extensions);
  114. String executeExternalCommand(String command, String args, String inDirectory="");
  115. void launchApplicationWithFile(String application, String file);
  116. void openFileWithApplication(String file, String application);
  117. void setCursor(int cursorType);
  118. void warpCursor(int x, int y);
  119. void openURL(String url);
  120. void copyStringToClipboard(const String& str);
  121. String getClipboardString();
  122. void initGamepad();
  123. void shutdownGamepad();
  124. void makeApplicationMain();
  125. void lockMutex(CoreMutex *mutex);
  126. void unlockMutex(CoreMutex *mutex);
  127. CoreMutex *createMutex();
  128. void checkEvents();
  129. int lastMouseY;
  130. int lastMouseX;
  131. vector<CocoaEvent> cocoaEvents;
  132. NSOpenGLContext *context;
  133. vector<GamepadDeviceEntry*> gamepads;
  134. unsigned int nextDeviceID;
  135. bool checkSpecialKeyEvents(PolyKEY key);
  136. Number getBackingXRes();
  137. Number getBackingYRes();
  138. protected:
  139. void _setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel);
  140. PolycodeView *glView;
  141. uint64_t initTime;
  142. bool retinaSupport;
  143. VideoModeChangeInfo modeChangeInfo;
  144. IOHIDManagerRef hidManager;
  145. };
  146. }