sdlWindowMgr.cpp 13 KB

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