sdlWindowMgr.cpp 15 KB

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