PolySDLCore.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #include "PolySDLCore.h"
  20. #include "PolycodeView.h"
  21. #include "PolyCoreServices.h"
  22. #include "PolyCoreInput.h"
  23. #include "PolyMaterialManager.h"
  24. #include "PolyThreaded.h"
  25. #include "PolyGLRenderer.h"
  26. #include "PolyGLSLShaderModule.h"
  27. #include "PolyRectangle.h"
  28. #include <SDL/SDL.h>
  29. #include <iostream>
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <sys/wait.h>
  34. #include <pwd.h>
  35. using namespace Polycode;
  36. using std::vector;
  37. long getThreadID() {
  38. return (long)pthread_self();
  39. }
  40. void Core::getScreenInfo(int *width, int *height, int *hz) {
  41. SDL_Init(SDL_INIT_VIDEO); // Or GetVideoInfo will not work
  42. const SDL_VideoInfo *video = SDL_GetVideoInfo();
  43. if (width) *width = video->current_w;
  44. if (height) *height = video->current_h;
  45. if (hz) *hz = 0;
  46. }
  47. SDLCore::SDLCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex, bool resizableWindow) : Core(_xRes, _yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate, monitorIndex) {
  48. this->resizableWindow = resizableWindow;
  49. char *buffer = getcwd(NULL, 0);
  50. defaultWorkingDirectory = String(buffer);
  51. free(buffer);
  52. struct passwd *pw = getpwuid(getuid());
  53. const char *homedir = pw->pw_dir;
  54. userHomeDirectory = String(homedir);
  55. String *windowTitle = (String*)view->windowData;
  56. if(resizableWindow) {
  57. unsetenv("SDL_VIDEO_CENTERED");
  58. } else {
  59. setenv("SDL_VIDEO_CENTERED", "1", 1);
  60. }
  61. if(SDL_Init(SDL_INIT_VIDEO) < 0) {
  62. }
  63. eventMutex = createMutex();
  64. renderer = new OpenGLRenderer();
  65. services->setRenderer(renderer);
  66. setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel);
  67. SDL_WM_SetCaption(windowTitle->c_str(), windowTitle->c_str());
  68. SDL_EnableUNICODE(1);
  69. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  70. ((OpenGLRenderer*)renderer)->initOSSpecific();
  71. CoreServices::getInstance()->installModule(new GLSLShaderModule());
  72. }
  73. void SDLCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel) {
  74. this->xRes = xRes;
  75. this->yRes = yRes;
  76. this->fullScreen = fullScreen;
  77. this->aaLevel = aaLevel;
  78. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24);
  79. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
  80. SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8);
  81. SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
  82. SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8);
  83. SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
  84. if(aaLevel > 0) {
  85. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1);
  86. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, aaLevel); //0, 2, 4
  87. } else {
  88. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0);
  89. SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0);
  90. }
  91. SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
  92. flags = SDL_OPENGL;
  93. if(fullScreen) {
  94. flags |= SDL_FULLSCREEN;
  95. }
  96. if(resizableWindow) {
  97. flags |= SDL_RESIZABLE;
  98. }
  99. SDL_SetVideoMode(xRes, yRes, 0, flags);
  100. renderer->Resize(xRes, yRes);
  101. //CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();
  102. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  103. }
  104. vector<Polycode::Rectangle> SDLCore::getVideoModes() {
  105. vector<Polycode::Rectangle> retVector;
  106. SDL_Rect **modes;
  107. modes=SDL_ListModes(NULL, SDL_FULLSCREEN);
  108. for(int i=0;modes[i];++i) {
  109. Rectangle res;
  110. res.w = modes[i]->w;
  111. res.h = modes[i]->h;
  112. retVector.push_back(res);
  113. }
  114. return retVector;
  115. }
  116. SDLCore::~SDLCore() {
  117. SDL_Quit();
  118. }
  119. void SDLCore::openURL(String url) {
  120. }
  121. String SDLCore::executeExternalCommand(String command) {
  122. FILE *fp = popen(command.c_str(), "r");
  123. if(!fp) {
  124. return "Unable to execute command";
  125. }
  126. int fd = fileno(fp);
  127. char path[2048];
  128. String retString;
  129. while (fgets(path, sizeof(path), fp) != NULL) {
  130. retString = retString + String(path);
  131. }
  132. pclose(fp);
  133. return retString;
  134. }
  135. int SDLThreadFunc(void *data) {
  136. Threaded *target = (Threaded*)data;
  137. target->runThread();
  138. return 1;
  139. }
  140. void SDLCore::createThread(Threaded *target) {
  141. SDL_CreateThread(SDLThreadFunc, (void*)target);
  142. }
  143. unsigned int SDLCore::getTicks() {
  144. return SDL_GetTicks();
  145. }
  146. void SDLCore::enableMouse(bool newval) {
  147. if(newval) {
  148. SDL_ShowCursor(1);
  149. SDL_WM_GrabInput(SDL_GRAB_OFF);
  150. } else {
  151. SDL_ShowCursor(0);
  152. SDL_WM_GrabInput(SDL_GRAB_ON);
  153. }
  154. Core::enableMouse(newval);
  155. }
  156. bool SDLCore::Update() {
  157. if(!running)
  158. return false;
  159. renderer->BeginRender();
  160. updateCore();
  161. renderer->EndRender();
  162. SDL_GL_SwapBuffers();
  163. SDL_Event event;
  164. while ( SDL_PollEvent(&event) ) {
  165. switch (event.type) {
  166. case SDL_QUIT:
  167. running = false;
  168. break;
  169. case SDL_VIDEORESIZE:
  170. if(resizableWindow) {
  171. unsetenv("SDL_VIDEO_CENTERED");
  172. } else {
  173. setenv("SDL_VIDEO_CENTERED", "1", 1);
  174. }
  175. this->xRes = event.resize.w;
  176. this->yRes = event.resize.h;
  177. SDL_SetVideoMode(xRes, yRes, 0, flags);
  178. renderer->Resize(xRes, yRes);
  179. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  180. break;
  181. case SDL_JOYBUTTONDOWN:
  182. // input->setKeyState((PolyKEY)(event.key.keysym.sym), true);
  183. break;
  184. case SDL_KEYDOWN:
  185. input->setKeyState((PolyKEY)(event.key.keysym.sym), (char)event.key.keysym.unicode, true, getTicks());
  186. break;
  187. case SDL_KEYUP:
  188. input->setKeyState((PolyKEY)(event.key.keysym.sym), (char)event.key.keysym.unicode, false, getTicks());
  189. break;
  190. case SDL_MOUSEBUTTONDOWN:
  191. if(event.button.button == SDL_BUTTON_WHEELUP) {
  192. input->mouseWheelUp(getTicks());
  193. } else if(event.button.button == SDL_BUTTON_WHEELDOWN) {
  194. input->mouseWheelDown(getTicks());
  195. } else {
  196. input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, true, getTicks());
  197. }
  198. break;
  199. case SDL_MOUSEBUTTONUP:
  200. if(event.button.button == SDL_BUTTON_WHEELUP || event.button.button == SDL_BUTTON_WHEELDOWN) {
  201. } else {
  202. input->setMouseButtonState(CoreInput::MOUSE_BUTTON1, false, getTicks());
  203. }
  204. break;
  205. case SDL_MOUSEMOTION:
  206. input->setDeltaPosition(event.motion.xrel, event.motion.yrel);
  207. input->setMousePosition(event.motion.x, event.motion.y, getTicks());
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. doSleep();
  214. return running;
  215. }
  216. void SDLCore::setCursor(int cursorType) {
  217. }
  218. void SDLCore::warpCursor(int x, int y) {
  219. SDL_WarpMouse(x, y);
  220. }
  221. void SDLCore::lockMutex(CoreMutex *mutex) {
  222. SDLCoreMutex *smutex = (SDLCoreMutex*)mutex;
  223. SDL_mutexP(smutex->pMutex);
  224. }
  225. void SDLCore::unlockMutex(CoreMutex *mutex) {
  226. SDLCoreMutex *smutex = (SDLCoreMutex*)mutex;
  227. SDL_mutexV(smutex->pMutex);
  228. }
  229. CoreMutex *SDLCore::createMutex() {
  230. SDLCoreMutex *mutex = new SDLCoreMutex();
  231. mutex->pMutex = SDL_CreateMutex();
  232. return mutex;
  233. }
  234. void SDLCore::copyStringToClipboard(const String& str) {
  235. }
  236. String SDLCore::getClipboardString() {
  237. }
  238. void SDLCore::createFolder(const String& folderPath) {
  239. mkdir(folderPath.c_str(), 0700);
  240. }
  241. void SDLCore::copyDiskItem(const String& itemPath, const String& destItemPath) {
  242. int childExitStatus;
  243. pid_t pid = fork();
  244. if (pid == 0) {
  245. execl("/bin/cp", "/bin/cp", itemPath.c_str(), destItemPath.c_str(), (char *)0);
  246. } else {
  247. pid_t ws = waitpid( pid, &childExitStatus, WNOHANG);
  248. }
  249. }
  250. void SDLCore::moveDiskItem(const String& itemPath, const String& destItemPath) {
  251. int childExitStatus;
  252. pid_t pid = fork();
  253. if (pid == 0) {
  254. execl("/bin/mv", "/bin/mv", itemPath.c_str(), destItemPath.c_str(), (char *)0);
  255. } else {
  256. pid_t ws = waitpid( pid, &childExitStatus, WNOHANG);
  257. }
  258. }
  259. void SDLCore::removeDiskItem(const String& itemPath) {
  260. int childExitStatus;
  261. pid_t pid = fork();
  262. if (pid == 0) {
  263. execl("/bin/rm", "/bin/rm", "-rf", itemPath.c_str(), (char *)0);
  264. } else {
  265. pid_t ws = waitpid( pid, &childExitStatus, WNOHANG);
  266. }
  267. }
  268. String SDLCore::openFolderPicker() {
  269. }
  270. vector<String> SDLCore::openFilePicker(vector<CoreFileExtension> extensions, bool allowMultiple) {
  271. }
  272. void SDLCore::resizeTo(int xRes, int yRes) {
  273. renderer->Resize(xRes, yRes);
  274. }