sdlWindowMgr.cpp 12 KB

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