EmscriptenWindow.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. // Portions Copyright (c) 2014 James S Urquhart
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "console/console.h"
  24. #include "io/fileStream.h"
  25. #include "game/resource.h"
  26. #include "game/version.h"
  27. #include "math/mRandom.h"
  28. #include "platformEmscripten/platformEmscripten.h"
  29. #include "platformEmscripten/EmscriptenConsole.h"
  30. #include "platform/event.h"
  31. #include "game/gameInterface.h"
  32. #include "platform/platform.h"
  33. #include "platform/platformAL.h"
  34. #include "platform/platformInput.h"
  35. #include "platform/platformVideo.h"
  36. #include "debug/profiler.h"
  37. #include "platformEmscripten/platformGL.h"
  38. #include "platformEmscripten/EmscriptenOGLVideo.h"
  39. #ifndef DEDICATED
  40. #include "platformEmscripten/EmscriptenInputManager.h"
  41. #endif
  42. #include <errno.h>
  43. #include <signal.h>
  44. #include <stdlib.h>
  45. #include <unistd.h> // fork, execvp, chdir
  46. #include <time.h> // nanosleep
  47. #ifndef DEDICATED
  48. #include <SDL/SDL.h>
  49. #include <SDL/SDL_syswm.h>
  50. #include <SDL/SDL_version.h>
  51. extern bool InitOpenGL();
  52. extern void Cleanup(bool minimal=false);
  53. #endif
  54. extern "C"
  55. {
  56. extern U32 _EmscriptenGetDesktopHeight();
  57. extern U32 _EmscriptenGetDesktopWidth();
  58. extern U32 _EmscriptenGetDesktopBpp();
  59. }
  60. EmscriptenPlatState::EmscriptenPlatState()
  61. {
  62. captureDisplay = true;
  63. fadeWindows = true;
  64. backgrounded = false;
  65. minimized = false;
  66. quit = false;
  67. portrait = true;//-Mat Android is in portrait mode by default
  68. // start with something reasonable.
  69. fullscreen = true;
  70. osVersion = 0;
  71. dStrcpy(appWindowTitle, "Emscripten Torque Game Engine");
  72. // directory that contains main.cs . This will help us detect whether we are
  73. // running with the scripts in the bundle or not.
  74. mainDotCsDir = NULL;
  75. useRedirect = true;
  76. windowCreated = false;
  77. dedicated = false;
  78. printf("platstate init\n");
  79. }
  80. //------------------------------------------------------------------------------
  81. static void InitWindow(const Point2I &initialSize, const char *name)
  82. {
  83. gPlatState.windowSize = initialSize;
  84. gPlatState.setWindowTitle(name);
  85. }
  86. #ifndef DEDICATED
  87. //------------------------------------------------------------------------------
  88. static bool InitSDL()
  89. {
  90. if (SDL_Init(SDL_INIT_VIDEO) != 0)
  91. return false;
  92. atexit(SDL_Quit);
  93. SDL_SysWMinfo sysinfo;
  94. SDL_VERSION(&sysinfo.version);
  95. //if (SDL_GetWMInfo(&sysinfo) == 0)
  96. // return false;
  97. return true;
  98. }
  99. //------------------------------------------------------------------------------
  100. static void SetAppState()
  101. {
  102. U8 state = SDL_GetAppState();
  103. // if we're not active but we have appactive and inputfocus, set window
  104. // active and reactivate input
  105. if ((gPlatState.backgrounded || !Input::isActive()) &&
  106. state & SDL_APPACTIVE &&
  107. state & SDL_APPINPUTFOCUS)
  108. {
  109. gPlatState.backgrounded = false;
  110. Input::reactivate();
  111. Con::printf("Input activated by SetAppState");
  112. }
  113. // if we are active, but we don't have appactive or input focus,
  114. // deactivate input (if window not locked) and clear windowActive
  115. else if (!gPlatState.backgrounded &&
  116. !(state & SDL_APPACTIVE && state & SDL_APPINPUTFOCUS))
  117. {
  118. if (gPlatState.mouseLocked)
  119. Input::deactivate();
  120. gPlatState.backgrounded = true;
  121. Con::printf("Input deactivated by SetAppState");
  122. }
  123. }
  124. //------------------------------------------------------------------------------
  125. static S32 NumEventsPending()
  126. {
  127. static const int MaxEvents = 255;
  128. static SDL_Event events[MaxEvents];
  129. SDL_PumpEvents();
  130. #ifdef DUMMY_PLATFORM
  131. return 0;
  132. #else
  133. return SDL_PeepEvents(events, MaxEvents, SDL_PEEKEVENT, 0, SDL_LASTEVENT);
  134. #endif
  135. }
  136. //------------------------------------------------------------------------------
  137. static bool ProcessMessages()
  138. {
  139. #ifndef DUMMY_PLATFORM
  140. SDL_PumpEvents();
  141. // We're going to have to maintain our own list of events here, since the emscripten API
  142. // is a bit broken for our purposes...
  143. gPlatState.eventList.clear();
  144. gPlatState.eventList.reserve(255);
  145. SDL_Event e;
  146. while (SDL_PollEvent(&e))
  147. {
  148. gPlatState.eventList.push_back(e);
  149. }
  150. S32 numEvents = gPlatState.eventList.size();
  151. //if (numEvents > 0)
  152. // Con::printf("ProcessMessages: %i events pending", numEvents);
  153. if (numEvents == 0)
  154. return true;
  155. for (int i = 0; i < numEvents; ++i)
  156. {
  157. SDL_Event& event = gPlatState.eventList[i];
  158. //Con::printf("Event.type == %u", event.type);
  159. switch (event.type)
  160. {
  161. case SDL_QUIT:
  162. return false;
  163. break;
  164. case SDL_VIDEORESIZE:
  165. case SDL_VIDEOEXPOSE:
  166. Game->refreshWindow();
  167. break;
  168. case SDL_USEREVENT:
  169. Con::printf("User event handled");
  170. if (event.user.code == TORQUE_SETVIDEOMODE)
  171. {
  172. SetAppState();
  173. // SDL will send a motion event to restore the mouse position
  174. // on the new window. Ignore that if the window is locked.
  175. if (gPlatState.mouseLocked)
  176. {
  177. SDL_Event tempEvent;
  178. SDL_PeepEvents(&tempEvent, 1, SDL_GETEVENT, SDL_MOUSEMOTION,
  179. SDL_MOUSEMOTION);
  180. }
  181. }
  182. break;
  183. case SDL_ACTIVEEVENT:
  184. Con::printf("Active event");
  185. SetAppState();
  186. break;
  187. }
  188. }
  189. #endif
  190. return true;
  191. }
  192. //------------------------------------------------------------------------------
  193. // send a destroy window event to the window. assumes
  194. // window is created.
  195. void SendQuitEvent()
  196. {
  197. SDL_Event quitevent;
  198. quitevent.type = SDL_QUIT;
  199. SDL_PushEvent(&quitevent);
  200. }
  201. #endif // DEDICATED
  202. //------------------------------------------------------------------------------
  203. static inline void Sleep(int secs, int nanoSecs)
  204. {
  205. timespec sleeptime;
  206. sleeptime.tv_sec = secs;
  207. sleeptime.tv_nsec = nanoSecs;
  208. nanosleep(&sleeptime, NULL);
  209. }
  210. #ifndef DEDICATED
  211. struct AlertWinState
  212. {
  213. bool fullScreen;
  214. bool cursorHidden;
  215. bool inputGrabbed;
  216. };
  217. //------------------------------------------------------------------------------
  218. void DisplayErrorAlert(const char* errMsg, bool showSDLError)
  219. {
  220. char fullErrMsg[2048];
  221. dStrncpy(fullErrMsg, errMsg, sizeof(fullErrMsg));
  222. if (showSDLError)
  223. {
  224. const char* sdlerror = SDL_GetError();
  225. if (sdlerror != NULL && dStrlen(sdlerror) > 0)
  226. {
  227. dStrcat(fullErrMsg, " (Error: ");
  228. dStrcat(fullErrMsg, sdlerror);
  229. dStrcat(fullErrMsg, ")");
  230. }
  231. }
  232. Platform::AlertOK("Error", fullErrMsg);
  233. }
  234. //------------------------------------------------------------------------------
  235. static inline void AlertDisableVideo(AlertWinState& state)
  236. {
  237. state.fullScreen = Video::isFullScreen();
  238. state.cursorHidden = (SDL_ShowCursor(SDL_QUERY) == SDL_DISABLE);
  239. state.inputGrabbed = (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON);
  240. if (state.fullScreen)
  241. SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
  242. if (state.cursorHidden)
  243. SDL_ShowCursor(SDL_ENABLE);
  244. if (state.inputGrabbed)
  245. SDL_WM_GrabInput(SDL_GRAB_OFF);
  246. }
  247. //------------------------------------------------------------------------------
  248. static inline void AlertEnableVideo(AlertWinState& state)
  249. {
  250. if (state.fullScreen)
  251. SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
  252. if (state.cursorHidden)
  253. SDL_ShowCursor(SDL_DISABLE);
  254. if (state.inputGrabbed)
  255. SDL_WM_GrabInput(SDL_GRAB_ON);
  256. }
  257. #endif // DEDICATED
  258. //------------------------------------------------------------------------------
  259. void Platform::setMouseLock(bool locked)
  260. {
  261. #ifndef DEDICATED
  262. gPlatState.mouseLocked = locked;
  263. UInputManager* uInputManager =
  264. dynamic_cast<UInputManager*>( Input::getManager() );
  265. if ( uInputManager && uInputManager->isEnabled() &&
  266. Input::isActive() )
  267. uInputManager->setWindowLocked(locked);
  268. #endif
  269. }
  270. //------------------------------------------------------------------------------
  271. void Platform::process()
  272. {
  273. PROFILE_START(XUX_PlatformProcess);
  274. if (gPlatState.windowCreated)
  275. {
  276. #ifndef DEDICATED
  277. // process window events
  278. PROFILE_START(XUX_ProcessMessages);
  279. bool quit = !ProcessMessages();
  280. PROFILE_END();
  281. if(quit)
  282. {
  283. // generate a quit event
  284. Event quitEvent;
  285. quitEvent.type = QuitEventType;
  286. Game->postEvent(quitEvent);
  287. }
  288. // process input events
  289. PROFILE_START(XUX_InputProcess);
  290. Input::process();
  291. PROFILE_END();
  292. // if we're not the foreground window, sleep for 1 ms
  293. if (gPlatState.backgrounded)
  294. Sleep(0, getBackgroundSleepTime() * 1000000);
  295. #endif
  296. }
  297. else
  298. {
  299. Sleep(0, getBackgroundSleepTime() * 1000000);
  300. }
  301. PROFILE_END();
  302. }
  303. //------------------------------------------------------------------------------
  304. const Point2I &Platform::getWindowSize()
  305. {
  306. return gPlatState.windowSize;
  307. }
  308. //------------------------------------------------------------------------------
  309. void Platform::setWindowSize( U32 newWidth, U32 newHeight )
  310. {
  311. gPlatState.windowSize = Point2I(newWidth, newHeight);
  312. }
  313. //------------------------------------------------------------------------------
  314. void Platform::minimizeWindow()
  315. {
  316. #ifndef DEDICATED
  317. if (gPlatState.windowCreated)
  318. SDL_WM_IconifyWindow();
  319. #endif
  320. }
  321. //------------------------------------------------------------------------------
  322. void Platform::restoreWindow()
  323. {
  324. }
  325. //------------------------------------------------------------------------------
  326. void Platform::shutdown()
  327. {
  328. Cleanup();
  329. }
  330. //------------------------------------------------------------------------------
  331. void Platform::init()
  332. {
  333. Con::printf("Platform::init");
  334. // Set the platform variable for the scripts
  335. Con::setVariable( "$platform", "x86UNIX" );
  336. Con::setVariable( "$platformUnixType", "emscripten" );
  337. EmscriptenConsole::create();
  338. #ifndef DEDICATED
  339. Con::printf("Dedicated == %i", gPlatState.dedicated);
  340. // if we're not dedicated do more initialization
  341. //if (!gPlatState.dedicated)
  342. {
  343. printf("Init non-dedicated\n");
  344. // init SDL
  345. if (!InitSDL())
  346. {
  347. Con::printf( " Unable to initialize SDL." );
  348. Platform::AlertOK("Error", "Unable to initialize SDL.");
  349. exit(1);
  350. }
  351. // initialize input
  352. Input::init();
  353. Con::printf( "Video Init:" );
  354. // initialize video
  355. Video::init();
  356. if ( Video::installDevice( OpenGLDevice::create() ) )
  357. Con::printf( " OpenGL display device detected." );
  358. else
  359. Con::printf( " OpenGL display device not detected." );
  360. Con::printf(" ");
  361. }
  362. #endif
  363. }
  364. //------------------------------------------------------------------------------
  365. void Platform::initWindow(const Point2I &initialSize, const char *name)
  366. {
  367. #ifndef DEDICATED
  368. // initialize window
  369. InitWindow(initialSize, name);
  370. if (!InitOpenGL()) {
  371. Con::printf( " Unable to init OpenGL window." );
  372. exit(1);
  373. }
  374. #endif
  375. }
  376. //------------------------------------------------------------------------------
  377. // Web browser function:
  378. //------------------------------------------------------------------------------
  379. bool Platform::openWebBrowser( const char* webAddress )
  380. {
  381. return false;
  382. }
  383. //------------------------------------------------------------------------------
  384. ConsoleFunction( getDesktopResolution, const char*, 1, 1,
  385. "getDesktopResolution()" )
  386. {
  387. if (!gPlatState.windowCreated)
  388. return "0 0 0";
  389. char buffer[256];
  390. char* returnString = Con::getReturnBuffer( dStrlen( buffer ) + 1 );
  391. Resolution res = Video::getDesktopResolution();
  392. dSprintf( buffer, sizeof( buffer ), "%d %d %d",
  393. res.w,
  394. res.h,
  395. res.bpp );
  396. dStrcpy( returnString, buffer );
  397. return( returnString );
  398. }
  399. //------------------------------------------------------------------------------
  400. // Silly Korean registry key checker:
  401. //------------------------------------------------------------------------------
  402. ConsoleFunction( isKoreanBuild, bool, 1, 1, "isKoreanBuild()" )
  403. {
  404. Con::printf("WARNING: isKoreanBuild() is unimplemented");
  405. return false;
  406. }
  407. //------------------------------------------------------------------------------
  408. void Platform::setWindowTitle( const char* title )
  409. {
  410. #ifndef DEDICATED
  411. gPlatState.setWindowTitle(title);
  412. SDL_WM_SetCaption(gPlatState.appWindowTitle, NULL);
  413. #endif
  414. }
  415. //------------------------------------------------------------------------------
  416. Resolution Video::getDesktopResolution()
  417. {
  418. Resolution Result;
  419. Result.h = _EmscriptenGetDesktopHeight();
  420. Result.w = _EmscriptenGetDesktopWidth();
  421. Result.bpp = _EmscriptenGetDesktopBpp();
  422. return Result;
  423. }
  424. //-----------------------------------------------------------------------------
  425. void Platform::outputDebugString( const char *string )
  426. {
  427. fprintf(stderr, "%s", string);
  428. fprintf(stderr, "\n" );
  429. fflush(stderr);
  430. }