sdlWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 "math/mMath.h"
  23. #include "gfx/gfxStructs.h"
  24. #include "windowManager/sdl/sdlWindow.h"
  25. #include "windowManager/sdl/sdlWindowMgr.h"
  26. #include "windowManager/sdl/sdlCursorController.h"
  27. #include "platformSDL/sdlInput.h"
  28. #include "platform/platformInput.h"
  29. #include "gfx/gfxDevice.h"
  30. #ifdef TORQUE_OS_LINUX
  31. #define SDL_VIDEO_DRIVER_X11 // TODO SDL
  32. #endif
  33. #include "SDL.h"
  34. #include "SDL_syswm.h"
  35. #define SCREENSAVER_QUERY_DENY 0 // Disable screensaver
  36. #ifndef IDI_ICON1
  37. #define IDI_ICON1 107
  38. #endif
  39. namespace
  40. {
  41. U32 getTorqueModFromSDL(U16 mod)
  42. {
  43. U32 ret = 0;
  44. if (mod & KMOD_LSHIFT)
  45. {
  46. ret |= SI_LSHIFT;
  47. ret |= SI_SHIFT;
  48. }
  49. if (mod & KMOD_RSHIFT)
  50. {
  51. ret |= SI_RSHIFT;
  52. ret |= SI_SHIFT;
  53. }
  54. if (mod & KMOD_LCTRL)
  55. {
  56. ret |= SI_LCTRL;
  57. ret |= SI_CTRL;
  58. }
  59. if (mod & KMOD_RCTRL)
  60. {
  61. ret |= SI_RCTRL;
  62. ret |= SI_CTRL;
  63. }
  64. if (mod & KMOD_LALT)
  65. {
  66. ret |= SI_LALT;
  67. ret |= SI_ALT;
  68. }
  69. if (mod & KMOD_RALT)
  70. {
  71. ret |= SI_RALT;
  72. ret |= SI_ALT;
  73. }
  74. return ret;
  75. }
  76. }
  77. PlatformWindowSDL::PlatformWindowSDL():
  78. mOwningManager(NULL),
  79. mNextWindow(NULL),
  80. mWindowHandle(NULL),
  81. mOldParent(NULL),
  82. mDevice(NULL),
  83. mTarget(NULL),
  84. mPosition(0,0),
  85. mMouseLocked(false),
  86. mShouldLockMouse(false),
  87. mSuppressReset(false),
  88. mMenuHandle(NULL)
  89. {
  90. mCursorController = new PlatformCursorControllerSDL( this );
  91. mVideoMode.bitDepth = 32;
  92. mVideoMode.fullScreen = false;
  93. mVideoMode.refreshRate = 60;
  94. mVideoMode.resolution.set(800,600);
  95. }
  96. PlatformWindowSDL::~PlatformWindowSDL()
  97. {
  98. // delete our sdl handle..
  99. SDL_DestroyWindow(mWindowHandle);
  100. // unlink ourselves from the window list...
  101. AssertFatal(mOwningManager, "PlatformWindowSDL::~PlatformWindowSDL - orphan window, cannot unlink!");
  102. mOwningManager->unlinkWindow(this);
  103. }
  104. GFXDevice * PlatformWindowSDL::getGFXDevice()
  105. {
  106. return mDevice;
  107. }
  108. GFXWindowTarget * PlatformWindowSDL::getGFXTarget()
  109. {
  110. return mTarget;
  111. }
  112. const GFXVideoMode & PlatformWindowSDL::getVideoMode()
  113. {
  114. return mVideoMode;
  115. }
  116. void* PlatformWindowSDL::getSystemWindow(const WindowSystem system)
  117. {
  118. SDL_SysWMinfo info;
  119. SDL_VERSION(&info.version);
  120. SDL_GetWindowWMInfo(mWindowHandle,&info);
  121. #ifdef TORQUE_OS_WIN
  122. if( system == WindowSystem_Windows && info.subsystem == SDL_SYSWM_WINDOWS)
  123. return info.info.win.window;
  124. #endif
  125. #if defined(TORQUE_OS_LINUX)
  126. if( system == WindowSystem_X11 && info.subsystem == SDL_SYSWM_X11)
  127. return (void*)info.info.x11.window;
  128. #endif
  129. AssertFatal(0, "");
  130. return NULL;
  131. }
  132. void PlatformWindowSDL::setVideoMode( const GFXVideoMode &mode )
  133. {
  134. mVideoMode = mode;
  135. mSuppressReset = true;
  136. // Set our window to have the right style based on the mode
  137. if(mode.fullScreen && !Platform::getWebDeployment() && !mOffscreenRender)
  138. {
  139. setSize(mode.resolution);
  140. SDL_SetWindowFullscreen( mWindowHandle, SDL_WINDOW_FULLSCREEN);
  141. // When switching to Fullscreen, reset device after setting style
  142. if(mTarget.isValid())
  143. mTarget->resetMode();
  144. }
  145. else
  146. {
  147. // Reset device *first*, so that when we call setSize() and let it
  148. // access the monitor settings, it won't end up with our fullscreen
  149. // geometry that is just about to change.
  150. if(mTarget.isValid())
  151. mTarget->resetMode();
  152. if (!mOffscreenRender)
  153. {
  154. SDL_SetWindowFullscreen( mWindowHandle, 0);
  155. }
  156. setSize(mode.resolution);
  157. centerWindow();
  158. }
  159. mSuppressReset = false;
  160. }
  161. bool PlatformWindowSDL::clearFullscreen()
  162. {
  163. return true;
  164. }
  165. bool PlatformWindowSDL::isFullscreen()
  166. {
  167. U32 flags = SDL_GetWindowFlags( mWindowHandle );
  168. if( flags & SDL_WINDOW_FULLSCREEN || flags & SDL_WINDOW_FULLSCREEN_DESKTOP )
  169. return true;
  170. return false;
  171. }
  172. void PlatformWindowSDL::_setFullscreen(const bool fullscreen)
  173. {
  174. if( isFullscreen() )
  175. return;
  176. if(fullscreen && !mOffscreenRender)
  177. {
  178. Con::printf("PlatformWindowSDL::setFullscreen (full) enter");
  179. SDL_SetWindowFullscreen( mWindowHandle, SDL_WINDOW_FULLSCREEN);
  180. }
  181. else
  182. {
  183. Con::printf("PlatformWindowSDL::setFullscreen (windowed) enter");
  184. if (!mOffscreenRender)
  185. {
  186. SDL_SetWindowFullscreen( mWindowHandle, SDL_WINDOW_FULLSCREEN_DESKTOP);
  187. }
  188. setSize(mVideoMode.resolution);
  189. }
  190. Con::printf("PlatformWindowSDL::setFullscreen exit");
  191. }
  192. bool PlatformWindowSDL::setCaption( const char *cap )
  193. {
  194. SDL_SetWindowTitle(mWindowHandle, cap);
  195. return true;
  196. }
  197. const char * PlatformWindowSDL::getCaption()
  198. {
  199. return StringTable->insert( SDL_GetWindowTitle(mWindowHandle) );
  200. }
  201. void PlatformWindowSDL::setFocus()
  202. {
  203. SDL_SetWindowInputFocus(mWindowHandle);
  204. }
  205. void PlatformWindowSDL::setClientExtent( const Point2I newExtent )
  206. {
  207. Point2I oldExtent = getClientExtent();
  208. if (oldExtent == newExtent)
  209. return;
  210. SDL_SetWindowSize(mWindowHandle, newExtent.x, newExtent.y);
  211. }
  212. const Point2I PlatformWindowSDL::getClientExtent()
  213. {
  214. // Fetch Client Rect from Windows
  215. Point2I size;
  216. SDL_GetWindowSize(mWindowHandle, &size.x, &size.y);
  217. return size;
  218. }
  219. void PlatformWindowSDL::setBounds( const RectI &newBounds )
  220. {
  221. // TODO SDL
  222. }
  223. const RectI PlatformWindowSDL::getBounds() const
  224. {
  225. // TODO SDL
  226. return RectI(0, 0, 0, 0);
  227. }
  228. void PlatformWindowSDL::setPosition( const Point2I newPosition )
  229. {
  230. SDL_SetWindowPosition( mWindowHandle, newPosition.x, newPosition.y );
  231. }
  232. const Point2I PlatformWindowSDL::getPosition()
  233. {
  234. Point2I position;
  235. SDL_GetWindowPosition( mWindowHandle, &position.x, &position.y );
  236. // Return position
  237. return position;
  238. }
  239. Point2I PlatformWindowSDL::clientToScreen( const Point2I& pos )
  240. {
  241. Point2I position;
  242. SDL_GetWindowPosition( mWindowHandle, &position.x, &position.y );
  243. return pos + position;
  244. }
  245. Point2I PlatformWindowSDL::screenToClient( const Point2I& pos )
  246. {
  247. Point2I position;
  248. SDL_GetWindowPosition( mWindowHandle, &position.x, &position.y );
  249. return pos - position;
  250. }
  251. void PlatformWindowSDL::centerWindow()
  252. {
  253. int sizeX, sizeY;
  254. SDL_GetWindowSize(mWindowHandle, &sizeX, &sizeY);
  255. SDL_DisplayMode mode;
  256. SDL_GetDesktopDisplayMode(0, &mode);
  257. U32 posX = (mode.w/2) - (sizeX/2);
  258. U32 posY = (mode.h/2) - (sizeY/2);
  259. SDL_SetWindowPosition( mWindowHandle, posX, posY);
  260. }
  261. bool PlatformWindowSDL::setSize( const Point2I &newSize )
  262. {
  263. SDL_SetWindowSize(mWindowHandle, newSize.x, newSize.y);
  264. // Let GFX get an update about the new resolution
  265. if (mTarget.isValid())
  266. mTarget->resetMode();
  267. return true;
  268. }
  269. bool PlatformWindowSDL::isOpen()
  270. {
  271. return mWindowHandle;
  272. }
  273. bool PlatformWindowSDL::isVisible()
  274. {
  275. // Is the window open and visible, ie. not minimized?
  276. if(!mWindowHandle)
  277. return false;
  278. if (mOffscreenRender)
  279. return true;
  280. U32 flags = SDL_GetWindowFlags( mWindowHandle );
  281. if( flags & SDL_WINDOW_SHOWN)
  282. return true;
  283. return false;
  284. }
  285. bool PlatformWindowSDL::isFocused()
  286. {
  287. if (mOffscreenRender)
  288. return true;
  289. U32 flags = SDL_GetWindowFlags( mWindowHandle );
  290. if( flags & SDL_WINDOW_INPUT_FOCUS || flags & SDL_WINDOW_INPUT_GRABBED || flags & SDL_WINDOW_MOUSE_FOCUS )
  291. return true;
  292. return false;
  293. }
  294. bool PlatformWindowSDL::isMinimized()
  295. {
  296. if (mOffscreenRender)
  297. return false;
  298. U32 flags = SDL_GetWindowFlags( mWindowHandle );
  299. if( flags & SDL_WINDOW_MINIMIZED)
  300. return true;
  301. return false;
  302. }
  303. bool PlatformWindowSDL::isMaximized()
  304. {
  305. if (mOffscreenRender)
  306. return true;
  307. U32 flags = SDL_GetWindowFlags( mWindowHandle );
  308. if( flags & SDL_WINDOW_MAXIMIZED)
  309. return true;
  310. return false;
  311. }
  312. WindowId PlatformWindowSDL::getWindowId()
  313. {
  314. return mWindowId;
  315. }
  316. void PlatformWindowSDL::minimize()
  317. {
  318. if (mOffscreenRender)
  319. return;
  320. SDL_MinimizeWindow( mWindowHandle );
  321. }
  322. void PlatformWindowSDL::maximize()
  323. {
  324. if (mOffscreenRender)
  325. return;
  326. SDL_MaximizeWindow( mWindowHandle );
  327. }
  328. void PlatformWindowSDL::restore()
  329. {
  330. if (mOffscreenRender)
  331. return;
  332. SDL_RestoreWindow( mWindowHandle );
  333. }
  334. void PlatformWindowSDL::hide()
  335. {
  336. if (mOffscreenRender)
  337. return;
  338. SDL_HideWindow( mWindowHandle );
  339. }
  340. void PlatformWindowSDL::show()
  341. {
  342. if (mOffscreenRender)
  343. return;
  344. SDL_ShowWindow( mWindowHandle );
  345. }
  346. void PlatformWindowSDL::close()
  347. {
  348. delete this;
  349. }
  350. void PlatformWindowSDL::defaultRender()
  351. {
  352. // TODO SDL
  353. }
  354. void PlatformWindowSDL::_triggerMouseLocationNotify(const SDL_Event& evt)
  355. {
  356. U32 mods = getTorqueModFromSDL(SDL_GetModState());
  357. if(!mMouseLocked)
  358. mouseEvent.trigger(getWindowId(), mods, evt.motion.x, evt.motion.y, false);
  359. else
  360. mouseEvent.trigger(getWindowId(), mods, evt.motion.xrel, evt.motion.yrel, true);
  361. }
  362. void PlatformWindowSDL::_triggerMouseWheelNotify(const SDL_Event& evt)
  363. {
  364. U32 mods = getTorqueModFromSDL(SDL_GetModState());
  365. S32 wheelDelta = Con::getIntVariable("$pref::Input::MouseWheelSpeed", 120);
  366. wheelEvent.trigger(getWindowId(), mods, evt.wheel.x * wheelDelta, evt.wheel.y * wheelDelta);
  367. }
  368. void PlatformWindowSDL::_triggerMouseButtonNotify(const SDL_Event& event)
  369. {
  370. S32 action = (event.type == SDL_MOUSEBUTTONDOWN) ? SI_MAKE : SI_BREAK;
  371. S32 button = -1;
  372. switch (event.button.button)
  373. {
  374. case SDL_BUTTON_LEFT:
  375. button = 0;
  376. break;
  377. case SDL_BUTTON_RIGHT:
  378. button = 1;
  379. break;
  380. case SDL_BUTTON_MIDDLE:
  381. button = 2;
  382. break;
  383. default:
  384. return;
  385. }
  386. U32 mod = getTorqueModFromSDL( SDL_GetModState() );
  387. buttonEvent.trigger(getWindowId(), mod, action, button );
  388. }
  389. void PlatformWindowSDL::_triggerKeyNotify(const SDL_Event& evt)
  390. {
  391. U32 inputAction = IA_MAKE;
  392. SDL_Keysym tKey = evt.key.keysym;
  393. if(evt.type == SDL_KEYUP)
  394. {
  395. inputAction = IA_BREAK;
  396. }
  397. if(evt.key.repeat)
  398. {
  399. inputAction = IA_REPEAT;
  400. }
  401. U32 torqueModifiers = getTorqueModFromSDL(evt.key.keysym.mod);
  402. U32 torqueKey = KeyMapSDL::getTorqueScanCodeFromSDL(tKey.scancode);
  403. if(tKey.scancode)
  404. {
  405. keyEvent.trigger(getWindowId(), torqueModifiers, inputAction, torqueKey);
  406. //Con::printf("Key %d : %d", tKey.sym, inputAction);
  407. if (inputAction == IA_MAKE && SDL_IsTextInputActive())
  408. {
  409. // We have to check if we already have a first responder active.
  410. // We don't want to type the character if it actually creates another responder!
  411. if (mWindowInputGenerator->lastKeyWasGlobalActionMap())
  412. {
  413. // Turn off Text input, and the next frame turn it back on. This tells SDL
  414. // to not generate a text event for this global action map key.
  415. SDL_StopTextInput();
  416. mOwningManager->updateSDLTextInputState(PlatformWindowManagerSDL::KeyboardInputState::TEXT_INPUT);
  417. }
  418. }
  419. }
  420. }
  421. void PlatformWindowSDL::_triggerTextNotify(const SDL_Event& evt)
  422. {
  423. U32 mod = getTorqueModFromSDL( SDL_GetModState() );
  424. if( !evt.text.text[1] ) // get a char
  425. {
  426. U16 wchar = evt.text.text[0];
  427. charEvent.trigger(getWindowId(), mod, wchar );
  428. //Con::printf("Char: %c", wchar);
  429. return;
  430. }
  431. else // get a wchar string
  432. {
  433. const U32 len = strlen(evt.text.text);
  434. U16 wchar[16] = {};
  435. dMemcpy(wchar, evt.text.text, sizeof(char)*len);
  436. for(int i = 0; i < 16; ++i)
  437. {
  438. if( !wchar[i] )
  439. return;
  440. charEvent.trigger(getWindowId(), mod, wchar[i] );
  441. }
  442. }
  443. }
  444. void PlatformWindowSDL::_processSDLEvent(SDL_Event &evt)
  445. {
  446. switch(evt.type)
  447. {
  448. case SDL_KEYDOWN:
  449. case SDL_KEYUP:
  450. {
  451. _triggerKeyNotify(evt);
  452. break;
  453. }
  454. case SDL_TEXTINPUT:
  455. {
  456. _triggerTextNotify(evt);
  457. break;
  458. }
  459. case SDL_MOUSEWHEEL:
  460. {
  461. _triggerMouseWheelNotify(evt);
  462. break;
  463. }
  464. case SDL_MOUSEMOTION:
  465. {
  466. _triggerMouseLocationNotify(evt);
  467. break;
  468. }
  469. case SDL_MOUSEBUTTONDOWN:
  470. case SDL_MOUSEBUTTONUP:
  471. {
  472. _triggerMouseButtonNotify(evt);
  473. break;
  474. }
  475. case SDL_WINDOWEVENT:
  476. {
  477. switch( evt.window.event )
  478. {
  479. case SDL_WINDOWEVENT_FOCUS_GAINED:
  480. appEvent.trigger(getWindowId(), GainFocus);
  481. break;
  482. case SDL_WINDOWEVENT_FOCUS_LOST:
  483. appEvent.trigger(getWindowId(), LoseFocus);
  484. break;
  485. case SDL_WINDOWEVENT_MAXIMIZED:
  486. case SDL_WINDOWEVENT_RESIZED:
  487. {
  488. int width, height;
  489. SDL_GetWindowSize( mWindowHandle, &width, &height );
  490. mVideoMode.resolution.set( width, height );
  491. getGFXTarget()->resetMode();
  492. break;
  493. }
  494. default:
  495. break;
  496. }
  497. }
  498. }
  499. }
  500. //-----------------------------------------------------------------------------
  501. // Mouse Locking
  502. //-----------------------------------------------------------------------------
  503. void PlatformWindowSDL::setMouseLocked( bool enable )
  504. {
  505. if (mOffscreenRender)
  506. return;
  507. mMouseLocked = enable;
  508. SDL_SetWindowGrab( mWindowHandle, SDL_bool(enable) );
  509. SDL_SetRelativeMouseMode( SDL_bool(enable) );
  510. }
  511. const UTF16 *PlatformWindowSDL::getWindowClassName()
  512. {
  513. // TODO SDL
  514. static String str("WindowClassName");
  515. return str.utf16();
  516. }
  517. const UTF16 *PlatformWindowSDL::getCurtainWindowClassName()
  518. {
  519. // TODO SDL
  520. static String str("CurtainWindowClassName");
  521. return str.utf16();
  522. }
  523. void PlatformWindowSDL::setKeyboardTranslation(const bool enabled)
  524. {
  525. mEnableKeyboardTranslation = enabled;
  526. // Flag for update. Let SDL know what kind of input state we are changing to.
  527. if (enabled)
  528. mOwningManager->updateSDLTextInputState(PlatformWindowManagerSDL::KeyboardInputState::TEXT_INPUT);
  529. else
  530. mOwningManager->updateSDLTextInputState(PlatformWindowManagerSDL::KeyboardInputState::RAW_INPUT);
  531. }