sdlWindowMgr.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "windowManager/sdl/sdlWindowMgr.h"
  23. #include "platformSDL/sdlInputManager.h"
  24. #include "platform/platformVolume.h"
  25. #include "core/util/path.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "core/util/journal/process.h"
  28. #include "core/strings/unicode.h"
  29. #include "gfx/bitmap/gBitmap.h"
  30. #include "SDL.h"
  31. // ------------------------------------------------------------------------
  32. void sdl_CloseSplashWindow(void* hinst);
  33. #ifdef TORQUE_SDL
  34. PlatformWindowManager * CreatePlatformWindowManager()
  35. {
  36. return new PlatformWindowManagerSDL();
  37. }
  38. #endif
  39. // ------------------------------------------------------------------------
  40. PlatformWindowManagerSDL::PlatformWindowManagerSDL()
  41. {
  42. // Register in the process list.
  43. mOnProcessSignalSlot.setDelegate( this, &PlatformWindowManagerSDL::_process );
  44. Process::notify( mOnProcessSignalSlot, PROCESS_INPUT_ORDER );
  45. // Init our list of allocated windows.
  46. mWindowListHead = NULL;
  47. // By default, we have no parent window.
  48. mParentWindow = NULL;
  49. mCurtainWindow = NULL;
  50. mDisplayWindow = true;
  51. mOffscreenRender = false;
  52. mInputState = KeyboardInputState::NONE;
  53. }
  54. PlatformWindowManagerSDL::~PlatformWindowManagerSDL()
  55. {
  56. // Kill all our windows first.
  57. while(mWindowListHead)
  58. // The destructors update the list, so this works just fine.
  59. delete mWindowListHead;
  60. }
  61. RectI PlatformWindowManagerSDL::getPrimaryDesktopArea()
  62. {
  63. // Primary is monitor 0
  64. return getMonitorRect(0);
  65. }
  66. Point2I PlatformWindowManagerSDL::getDesktopResolution()
  67. {
  68. SDL_DisplayMode mode;
  69. SDL_GetDesktopDisplayMode(0, &mode);
  70. // Return Resolution
  71. return Point2I(mode.w, mode.h);
  72. }
  73. S32 PlatformWindowManagerSDL::getDesktopBitDepth()
  74. {
  75. // Return Bits per Pixel
  76. SDL_DisplayMode mode;
  77. SDL_GetDesktopDisplayMode(0, &mode);
  78. int bbp;
  79. unsigned int r,g,b,a;
  80. SDL_PixelFormatEnumToMasks(mode.format, &bbp, &r, &g, &b, &a);
  81. return bbp;
  82. }
  83. S32 PlatformWindowManagerSDL::findFirstMatchingMonitor(const char* name)
  84. {
  85. S32 count = SDL_GetNumVideoDisplays();
  86. for (U32 index = 0; index < count; ++index)
  87. {
  88. if (dStrstr(name, SDL_GetDisplayName(index)) == name)
  89. return index;
  90. }
  91. return 0;
  92. }
  93. U32 PlatformWindowManagerSDL::getMonitorCount()
  94. {
  95. S32 monitorCount = SDL_GetNumVideoDisplays();
  96. if (monitorCount < 0)
  97. {
  98. Con::errorf("SDL_GetNumVideoDisplays() failed: %s", SDL_GetError());
  99. monitorCount = 0;
  100. }
  101. return (U32)monitorCount;
  102. }
  103. const char* PlatformWindowManagerSDL::getMonitorName(U32 index)
  104. {
  105. const char* monitorName = SDL_GetDisplayName(index);
  106. if (monitorName == NULL)
  107. Con::errorf("SDL_GetDisplayName() failed: %s", SDL_GetError());
  108. return monitorName;
  109. }
  110. RectI PlatformWindowManagerSDL::getMonitorRect(U32 index)
  111. {
  112. SDL_Rect sdlRect;
  113. if (0 != SDL_GetDisplayBounds(index, &sdlRect))
  114. {
  115. Con::errorf("SDL_GetDisplayBounds() failed: %s", SDL_GetError());
  116. return RectI(0, 0, 0, 0);
  117. }
  118. return RectI(sdlRect.x, sdlRect.y, sdlRect.w, sdlRect.h);
  119. }
  120. RectI PlatformWindowManagerSDL::getMonitorUsableRect(U32 index)
  121. {
  122. SDL_Rect sdlRect;
  123. if (0 != SDL_GetDisplayUsableBounds(index, &sdlRect))
  124. {
  125. Con::errorf("SDL_GetDisplayUsableBounds() failed: %s", SDL_GetError());
  126. return RectI(0, 0, 0, 0);
  127. }
  128. return RectI(sdlRect.x, sdlRect.y, sdlRect.w, sdlRect.h);
  129. }
  130. U32 PlatformWindowManagerSDL::getMonitorModeCount(U32 monitorIndex)
  131. {
  132. S32 modeCount = SDL_GetNumDisplayModes(monitorIndex);
  133. if (modeCount < 0)
  134. {
  135. Con::errorf("SDL_GetNumDisplayModes(%d) failed: %s", monitorIndex, SDL_GetError());
  136. modeCount = 0;
  137. }
  138. return (U32)modeCount;
  139. }
  140. const String PlatformWindowManagerSDL::getMonitorMode(U32 monitorIndex, U32 modeIndex)
  141. {
  142. SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
  143. if (SDL_GetDisplayMode(monitorIndex, modeIndex, &mode) != 0)
  144. {
  145. Con::errorf("SDL_GetDisplayMode(%d, %d) failed: %s", monitorIndex, modeIndex, SDL_GetError());
  146. return String::EmptyString;
  147. }
  148. GFXVideoMode vm;
  149. vm.resolution.set(mode.w, mode.h);
  150. vm.refreshRate = mode.refresh_rate;
  151. vm.bitDepth = 32;
  152. vm.antialiasLevel = 0;
  153. vm.fullScreen = false;
  154. vm.wideScreen = false;
  155. return vm.toString();
  156. }
  157. const String PlatformWindowManagerSDL::getMonitorDesktopMode(U32 monitorIndex)
  158. {
  159. SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
  160. if (SDL_GetDesktopDisplayMode(monitorIndex, &mode) != 0)
  161. {
  162. Con::errorf("SDL_GetDesktopDisplayMode(%d) failed: %s", monitorIndex, SDL_GetError());
  163. return String::EmptyString;
  164. }
  165. GFXVideoMode vm;
  166. vm.resolution.set(mode.w, mode.h);
  167. vm.refreshRate = mode.refresh_rate;
  168. int bbp;
  169. unsigned int r, g, b, a;
  170. SDL_PixelFormatEnumToMasks(mode.format, &bbp, &r, &g, &b, &a);
  171. vm.bitDepth = bbp;
  172. vm.antialiasLevel = 0;
  173. vm.fullScreen = false;
  174. vm.wideScreen = ((mode.w / 16) * 9) == mode.h;
  175. return vm.toString();
  176. }
  177. void PlatformWindowManagerSDL::getMonitorRegions(Vector<RectI> &regions)
  178. {
  179. SDL_Rect sdlRect;
  180. S32 monitorCount = SDL_GetNumVideoDisplays();
  181. for (S32 index = 0; index < monitorCount; ++index)
  182. {
  183. if (0 == SDL_GetDisplayBounds(index, &sdlRect))
  184. regions.push_back(RectI(sdlRect.x, sdlRect.y, sdlRect.w, sdlRect.h));
  185. }
  186. }
  187. void PlatformWindowManagerSDL::getWindows(VectorPtr<PlatformWindow*> &windows)
  188. {
  189. PlatformWindowSDL *win = mWindowListHead;
  190. while(win)
  191. {
  192. windows.push_back(win);
  193. win = win->mNextWindow;
  194. }
  195. }
  196. PlatformWindow *PlatformWindowManagerSDL::createWindow(GFXDevice *device, const GFXVideoMode &mode)
  197. {
  198. // Do the allocation.
  199. PlatformWindowSDL *window = new PlatformWindowSDL();
  200. U32 windowFlags = /*SDL_WINDOW_SHOWN |*/ SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
  201. if(GFX->getAdapterType() == OpenGL)
  202. windowFlags |= SDL_WINDOW_OPENGL;
  203. window->mWindowHandle = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, mode.resolution.x, mode.resolution.y, windowFlags );
  204. window->mWindowId = SDL_GetWindowID( window->mWindowHandle );
  205. window->mOwningManager = this;
  206. mWindowMap[ window->mWindowId ] = window;
  207. //Now, fetch our window icon, if any
  208. Torque::Path iconPath = Torque::Path(Con::getVariable( "$Core::windowIcon" ));
  209. if (iconPath.getExtension() == String("bmp"))
  210. {
  211. Con::errorf("Unable to use bmp format images for the window icon. Please use a different format.");
  212. }
  213. else
  214. {
  215. Resource<GBitmap> img = GBitmap::load(iconPath);
  216. if (img != NULL)
  217. {
  218. U32 pitch;
  219. U32 width = img->getWidth();
  220. bool hasAlpha = img->getHasTransparency();
  221. U32 depth;
  222. if (hasAlpha)
  223. {
  224. pitch = 4 * width;
  225. depth = 32;
  226. }
  227. else
  228. {
  229. pitch = 3 * width;
  230. depth = 24;
  231. }
  232. Uint32 rmask, gmask, bmask, amask;
  233. if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  234. {
  235. S32 shift = hasAlpha ? 8 : 0;
  236. rmask = 0xff000000 >> shift;
  237. gmask = 0x00ff0000 >> shift;
  238. bmask = 0x0000ff00 >> shift;
  239. amask = 0x000000ff >> shift;
  240. }
  241. else
  242. {
  243. rmask = 0x000000ff;
  244. gmask = 0x0000ff00;
  245. bmask = 0x00ff0000;
  246. amask = hasAlpha ? 0xff000000 : 0;
  247. }
  248. SDL_Surface* iconSurface = SDL_CreateRGBSurfaceFrom(img->getAddress(0, 0), img->getWidth(), img->getHeight(), depth, pitch, rmask, gmask, bmask, amask);
  249. SDL_SetWindowIcon(window->mWindowHandle, iconSurface);
  250. SDL_FreeSurface(iconSurface);
  251. }
  252. }
  253. if(device)
  254. {
  255. window->mDevice = device;
  256. window->mTarget = device->allocWindowTarget(window);
  257. AssertISV(window->mTarget, "PlatformWindowManagerSDL::createWindow - failed to get a window target back from the device.");
  258. }
  259. else
  260. {
  261. Con::warnf("PlatformWindowManagerSDL::createWindow - created a window with no device!");
  262. }
  263. //Set it up for drag-n-drop events
  264. #ifdef TORQUE_TOOLS
  265. SDL_EventState(SDL_DROPBEGIN, SDL_ENABLE);
  266. SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
  267. SDL_EventState(SDL_DROPCOMPLETE, SDL_ENABLE);
  268. #endif
  269. linkWindow(window);
  270. SDL_SetWindowMinimumSize(window->mWindowHandle, Con::getIntVariable("$Video::minimumXResolution", 1024),
  271. Con::getIntVariable("$Video::minimumYResolution", 720));
  272. return window;
  273. }
  274. void PlatformWindowManagerSDL::setParentWindow(void* newParent)
  275. {
  276. }
  277. void* PlatformWindowManagerSDL::getParentWindow()
  278. {
  279. return NULL;
  280. }
  281. void PlatformWindowManagerSDL::_process()
  282. {
  283. SDL_Event evt;
  284. while( SDL_PollEvent(&evt) )
  285. {
  286. if (evt.type >= SDL_JOYAXISMOTION && evt.type <= SDL_CONTROLLERDEVICEREMAPPED)
  287. {
  288. SDLInputManager* mgr = static_cast<SDLInputManager*>(Input::getManager());
  289. if (mgr)
  290. mgr->processEvent(evt);
  291. continue;
  292. }
  293. switch(evt.type)
  294. {
  295. case SDL_QUIT:
  296. {
  297. PlatformWindowSDL *window = static_cast<PlatformWindowSDL*>( getFirstWindow() );
  298. if(window)
  299. window->appEvent.trigger( window->getWindowId(), WindowClose );
  300. break;
  301. }
  302. case SDL_KEYDOWN:
  303. case SDL_KEYUP:
  304. {
  305. PlatformWindowSDL *window = mWindowMap[evt.key.windowID];
  306. if(window)
  307. window->_processSDLEvent(evt);
  308. break;
  309. }
  310. case SDL_MOUSEWHEEL:
  311. {
  312. PlatformWindowSDL *window = mWindowMap[evt.wheel.windowID];
  313. if (window)
  314. window->_processSDLEvent(evt);
  315. break;
  316. }
  317. case SDL_MOUSEMOTION:
  318. {
  319. PlatformWindowSDL *window = mWindowMap[evt.motion.windowID];
  320. if(window)
  321. window->_processSDLEvent(evt);
  322. break;
  323. }
  324. case SDL_MOUSEBUTTONDOWN:
  325. case SDL_MOUSEBUTTONUP:
  326. {
  327. PlatformWindowSDL *window = mWindowMap[evt.button.windowID];
  328. if(window)
  329. window->_processSDLEvent(evt);
  330. break;
  331. }
  332. case SDL_TEXTINPUT:
  333. {
  334. PlatformWindowSDL *window = mWindowMap[evt.text.windowID];
  335. if(window)
  336. window->_processSDLEvent(evt);
  337. break;
  338. }
  339. case SDL_WINDOWEVENT:
  340. {
  341. PlatformWindowSDL *window = mWindowMap[evt.window.windowID];
  342. if(window)
  343. window->_processSDLEvent(evt);
  344. break;
  345. }
  346. case(SDL_DROPBEGIN):
  347. {
  348. if (!Con::isFunction("onDropBegin"))
  349. break;
  350. Con::executef("onDropBegin");
  351. }
  352. case (SDL_DROPFILE):
  353. {
  354. // In case if dropped file
  355. if (!Con::isFunction("onDropFile"))
  356. break;
  357. char* fileName = evt.drop.file;
  358. if (!Platform::isDirectory(fileName) && !Platform::isFile(fileName))
  359. break;
  360. Con::executef("onDropFile", StringTable->insert(fileName));
  361. SDL_free(fileName); // Free dropped_filedir memory
  362. break;
  363. }
  364. case(SDL_DROPCOMPLETE):
  365. {
  366. if (Con::isFunction("onDropEnd"))
  367. Con::executef("onDropEnd");
  368. break;
  369. }
  370. default:
  371. {
  372. #ifdef TORQUE_DEBUG
  373. Con::warnf("Unhandled SDL input event: 0x%04x", evt.type);
  374. #endif
  375. }
  376. }
  377. }
  378. // After the event loop is processed, we can now see if we have to notify
  379. // SDL that we want text based events. This fixes a bug where text based
  380. // events would be generated while key presses would still be happening.
  381. // See KeyboardInputState for further documentation.
  382. if (mInputState != KeyboardInputState::NONE)
  383. {
  384. // Update text mode toggling.
  385. if (mInputState == KeyboardInputState::TEXT_INPUT)
  386. SDL_StartTextInput();
  387. else
  388. SDL_StopTextInput();
  389. // Done until we need to update it again.
  390. mInputState = KeyboardInputState::NONE;
  391. }
  392. }
  393. PlatformWindow * PlatformWindowManagerSDL::getWindowById( WindowId id )
  394. {
  395. // Walk the list and find the matching id, if any.
  396. PlatformWindowSDL *win = mWindowListHead;
  397. while(win)
  398. {
  399. if(win->getWindowId() == id)
  400. return win;
  401. win = win->mNextWindow;
  402. }
  403. return NULL;
  404. }
  405. PlatformWindow * PlatformWindowManagerSDL::getFirstWindow()
  406. {
  407. return mWindowListHead != NULL ? mWindowListHead : NULL;
  408. }
  409. PlatformWindow* PlatformWindowManagerSDL::getFocusedWindow()
  410. {
  411. PlatformWindowSDL* window = mWindowListHead;
  412. while( window )
  413. {
  414. if( window->isFocused() )
  415. return window;
  416. window = window->mNextWindow;
  417. }
  418. return NULL;
  419. }
  420. void PlatformWindowManagerSDL::linkWindow( PlatformWindowSDL *w )
  421. {
  422. w->mNextWindow = mWindowListHead;
  423. mWindowListHead = w;
  424. }
  425. void PlatformWindowManagerSDL::unlinkWindow( PlatformWindowSDL *w )
  426. {
  427. PlatformWindowSDL **walk = &mWindowListHead;
  428. while(*walk)
  429. {
  430. if(*walk != w)
  431. {
  432. // Advance to next item in list.
  433. walk = &(*walk)->mNextWindow;
  434. continue;
  435. }
  436. // Got a match - unlink and return.
  437. *walk = (*walk)->mNextWindow;
  438. return;
  439. }
  440. }
  441. void PlatformWindowManagerSDL::_processCmdLineArgs( const S32 argc, const char **argv )
  442. {
  443. // TODO SDL
  444. }
  445. void PlatformWindowManagerSDL::lowerCurtain()
  446. {
  447. if(mCurtainWindow)
  448. return;
  449. // TODO SDL
  450. }
  451. void PlatformWindowManagerSDL::raiseCurtain()
  452. {
  453. if(!mCurtainWindow)
  454. return;
  455. // TODO SDL
  456. }
  457. void PlatformWindowManagerSDL::updateSDLTextInputState(KeyboardInputState state)
  458. {
  459. // Force update state. This will respond at the end of the event loop.
  460. mInputState = state;
  461. }
  462. void Platform::openFolder(const char* path )
  463. {
  464. AssertFatal(0, "Not Implemented");
  465. }
  466. void Platform::openFile(const char* path )
  467. {
  468. AssertFatal(0, "Not Implemented");
  469. }
  470. //------------------------------------------------------------------------------
  471. namespace GL
  472. {
  473. void gglPerformBinds();
  474. }
  475. void InitWindowingSystem()
  476. {
  477. }
  478. AFTER_MODULE_INIT(gfx)
  479. {
  480. #if !defined(TORQUE_DEDICATED)
  481. int res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS | SDL_INIT_NOPARACHUTE);
  482. AssertFatal(res != -1, avar("SDL error:%s", SDL_GetError()));
  483. // By default, SDL enables text input. We disable it on initialization, and
  484. // we will enable it whenever the time is right.
  485. SDL_StopTextInput();
  486. #endif
  487. }