PolyRPICore.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. Copyright (C) 2015 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. #include "polycode/core/PolyRPICore.h"
  20. #include "polycode/core/PolyBasicFileProvider.h"
  21. #include "polycode/core/PolyOpenGLGraphicsInterface.h"
  22. #include <dirent.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. using namespace Polycode;
  27. void RPICoreMutex::lock() {
  28. pthread_mutex_lock(&pMutex);
  29. }
  30. void RPICoreMutex::unlock() {
  31. pthread_mutex_unlock(&pMutex);
  32. }
  33. RPICore::RPICore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex, bool retinaSupport)
  34. : Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate, monitorIndex) {
  35. bcm_host_init();
  36. fileProviders.push_back(new BasicFileProvider());
  37. renderer = new Renderer();
  38. renderer->setBackingResolutionScale(1.0, 1.0);
  39. graphicsInterface = new OpenGLGraphicsInterface();
  40. renderer->setGraphicsInterface(this, graphicsInterface);
  41. services->setRenderer(renderer);
  42. setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, retinaSupport);
  43. //services->getSoundManager()->setAudioInterface(new XAudio2AudioInterface());
  44. eventMutex = createMutex();
  45. }
  46. RPICore::~RPICore() {
  47. }
  48. void RPICore::Render() {
  49. renderer->beginFrame();
  50. services->Render(Polycode::Rectangle(0, 0, xRes, yRes));
  51. renderer->endFrame();
  52. }
  53. void RPICore::checkEvents() {
  54. eventMutex->lock();
  55. RPIEvent event;
  56. for (int i = 0; i < systemInputEvents.size(); i++) {
  57. event = systemInputEvents[i];
  58. switch (event.eventGroup) {
  59. case RPIEvent::INPUT_EVENT:
  60. switch (event.eventCode) {
  61. case InputEvent::EVENT_MOUSEMOVE:
  62. input->setDeltaPosition(lastMouseX - event.mouseX, lastMouseY - event.mouseY);
  63. lastMouseX = event.mouseX;
  64. lastMouseY = event.mouseY;
  65. input->setMousePosition(event.mouseX, event.mouseY, getTicks());
  66. break;
  67. case InputEvent::EVENT_MOUSEDOWN:
  68. input->mousePosition.x = event.mouseX;
  69. input->mousePosition.y = event.mouseY;
  70. input->setMouseButtonState(event.mouseButton, true, getTicks());
  71. break;
  72. case InputEvent::EVENT_MOUSEWHEEL_UP:
  73. input->mouseWheelUp(getTicks());
  74. break;
  75. case InputEvent::EVENT_MOUSEWHEEL_DOWN:
  76. input->mouseWheelDown(getTicks());
  77. break;
  78. case InputEvent::EVENT_MOUSEUP:
  79. input->setMouseButtonState(event.mouseButton, false, getTicks());
  80. break;
  81. case InputEvent::EVENT_KEYDOWN:
  82. if (!checkSpecialKeyEvents(event.keyCode))
  83. input->setKeyState(event.keyCode, event.unicodeChar, true, getTicks());
  84. break;
  85. case InputEvent::EVENT_KEYUP:
  86. input->setKeyState(event.keyCode, event.unicodeChar, false, getTicks());
  87. break;
  88. case InputEvent::EVENT_TOUCHES_BEGAN:
  89. input->touchesBegan(event.touch, event.touches, getTicks());
  90. break;
  91. case InputEvent::EVENT_TOUCHES_ENDED:
  92. input->touchesEnded(event.touch, event.touches, getTicks());
  93. break;
  94. case InputEvent::EVENT_TOUCHES_MOVED:
  95. input->touchesMoved(event.touch, event.touches, getTicks());
  96. break;
  97. }
  98. break;
  99. case RPIEvent::SYSTEM_FOCUS_EVENT:
  100. switch (event.eventCode) {
  101. case Core::EVENT_LOST_FOCUS:
  102. loseFocus();
  103. break;
  104. case Core::EVENT_GAINED_FOCUS:
  105. gainFocus();
  106. break;
  107. }
  108. break;
  109. }
  110. }
  111. systemInputEvents.clear();
  112. eventMutex->unlock();
  113. }
  114. bool RPICore::systemUpdate() {
  115. if (!running) {
  116. return false;
  117. }
  118. doSleep();
  119. updateCore();
  120. checkEvents();
  121. return running;
  122. }
  123. void RPICore::setCursor(int cursorType) {
  124. }
  125. void launchThread(Threaded *target) {
  126. target->runThread();
  127. target->scheduledForRemoval = true;
  128. }
  129. void *ManagedThreadFunc(void *data) {
  130. Threaded *target = static_cast<Threaded*>(data);
  131. target->runThread();
  132. target->scheduledForRemoval = true;
  133. return NULL;
  134. }
  135. void RPICore::createThread(Threaded * target) {
  136. Core::createThread(target);
  137. pthread_t thread;
  138. pthread_create( &thread, NULL, ManagedThreadFunc, (void*)target);
  139. }
  140. CoreMutex *RPICore::createMutex() {
  141. RPICoreMutex *mutex = new RPICoreMutex();
  142. pthread_mutex_init(&mutex->pMutex, NULL);
  143. return mutex;
  144. }
  145. void RPICore::copyStringToClipboard(const String& str) {
  146. }
  147. String RPICore::getClipboardString() {
  148. return "";
  149. }
  150. void RPICore::createFolder(const String& folderPath) {
  151. }
  152. void RPICore::copyDiskItem(const String& itemPath, const String& destItemPath) {
  153. }
  154. void RPICore::moveDiskItem(const String& itemPath, const String& destItemPath) {
  155. }
  156. void RPICore::removeDiskItem(const String& itemPath) {
  157. }
  158. String RPICore::openFolderPicker() {
  159. return "";
  160. }
  161. std::vector<String> RPICore::openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple) {
  162. std::vector<String> ret;
  163. return ret;
  164. }
  165. String RPICore::saveFilePicker(std::vector<CoreFileExtension> extensions) {
  166. return "";
  167. }
  168. void RPICore::handleVideoModeChange(VideoModeChangeInfo *modeInfo) {
  169. int32_t success = 0;
  170. EGLBoolean result;
  171. EGLint num_config;
  172. this->xRes = modeInfo->xRes;
  173. this->yRes = modeInfo->yRes;
  174. this->fullScreen = modeInfo->fullScreen;
  175. this->aaLevel = modeInfo->aaLevel;
  176. this->vSync = modeInfo->vSync;
  177. static EGL_DISPMANX_WINDOW_T nativewindow;
  178. DISPMANX_ELEMENT_HANDLE_T dispman_element;
  179. DISPMANX_DISPLAY_HANDLE_T dispman_display;
  180. DISPMANX_UPDATE_HANDLE_T dispman_update;
  181. VC_RECT_T dst_rect;
  182. VC_RECT_T src_rect;
  183. static const EGLint attribute_list[] =
  184. {
  185. EGL_RED_SIZE, 8,
  186. EGL_GREEN_SIZE, 8,
  187. EGL_BLUE_SIZE, 8,
  188. EGL_ALPHA_SIZE, 8,
  189. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  190. EGL_NONE
  191. };
  192. static const EGLint context_attributes[] =
  193. {
  194. EGL_CONTEXT_CLIENT_VERSION, 2,
  195. EGL_NONE
  196. };
  197. EGLConfig config;
  198. display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  199. result = eglInitialize(display, NULL, NULL);
  200. result = eglChooseConfig(display, attribute_list, &config, 1, &num_config);
  201. assert(EGL_FALSE != result);
  202. result = eglBindAPI(EGL_OPENGL_ES_API);
  203. assert(EGL_FALSE != result);
  204. context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes);
  205. assert(context!=EGL_NO_CONTEXT);
  206. uint32_t xr;
  207. uint32_t yr;
  208. success = graphics_get_display_size(0 /* LCD */, &xr, &yr);
  209. xRes = xr;
  210. yRes = yr;
  211. assert( success >= 0 );
  212. dst_rect.x = 0;
  213. dst_rect.y = 0;
  214. dst_rect.width = xRes;
  215. dst_rect.height = yRes;
  216. src_rect.x = 0;
  217. src_rect.y = 0;
  218. src_rect.width = xRes << 16;
  219. src_rect.height = yRes << 16;
  220. dispman_display = vc_dispmanx_display_open( 0 /* LCD */);
  221. dispman_update = vc_dispmanx_update_start( 0 );
  222. dispman_element =
  223. vc_dispmanx_element_add(dispman_update, dispman_display,
  224. 0/*layer*/, &dst_rect, 0/*src*/,
  225. &src_rect, DISPMANX_PROTECTION_NONE,
  226. 0 /*alpha*/, 0/*clamp*/, (DISPMANX_TRANSFORM_T)0/*transform*/);
  227. nativewindow.element = dispman_element;
  228. nativewindow.width = xRes;
  229. nativewindow.height = yRes;
  230. vc_dispmanx_update_submit_sync( dispman_update );
  231. surface = eglCreateWindowSurface( display, config, &nativewindow, NULL );
  232. assert(surface != EGL_NO_SURFACE);
  233. // connect the context to the surface
  234. result = eglMakeCurrent(display, surface, surface, context);
  235. assert(EGL_FALSE != result);
  236. }
  237. void RPICore::flushRenderContext() {
  238. glFlush();
  239. eglSwapBuffers(display, surface);
  240. }
  241. void RPICore::openURL(String url) {
  242. }
  243. unsigned int RPICore::getTicks() {
  244. return 0;
  245. }
  246. String RPICore::executeExternalCommand(String command, String args, String inDirectory) {
  247. return "";
  248. }
  249. bool RPICore::systemParseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
  250. DIR *d;
  251. struct dirent *dir;
  252. d = opendir(pathString.c_str());
  253. if(d) {
  254. while ((dir = readdir(d)) != NULL) {
  255. if(dir->d_name[0] != '.' || (dir->d_name[0] == '.' && showHidden)) {
  256. if(dir->d_type == DT_DIR) {
  257. targetVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FOLDER));
  258. } else {
  259. targetVector.push_back(OSFileEntry(pathString, dir->d_name, OSFileEntry::TYPE_FILE));
  260. }
  261. }
  262. }
  263. closedir(d);
  264. }
  265. return true;
  266. }
  267. void RPICore::handleSystemEvent(RPIEvent systemEvent) {
  268. eventMutex->lock();
  269. systemInputEvents.push_back(systemEvent);
  270. eventMutex->unlock();
  271. }
  272. void Core::getScreenInfo(int *width, int *height, int *hz) {
  273. }
  274. void RPICore::setDeviceSize(Number x, Number y) {
  275. deviceWidth = x;
  276. deviceHeight = y;
  277. renderer->setBackingResolutionScale(xRes/deviceWidth, yRes/deviceHeight);
  278. }
  279. Number RPICore::getBackingXRes() {
  280. return deviceWidth;
  281. }
  282. Number RPICore::getBackingYRes() {
  283. return deviceHeight;
  284. }