CmWindowEventUtilities.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmWindowEventUtilities.h"
  25. #include "CmRenderWindow.h"
  26. #include "CmApplication.h"
  27. #include "CmException.h"
  28. #include "CmCursor.h"
  29. #if CM_PLATFORM == CM_PLATFORM_LINUX
  30. #include <X11/Xlib.h>
  31. void GLXProc( CamelotFramework::RenderWindow *win, const XEvent &event );
  32. #endif
  33. using namespace CamelotFramework;
  34. WindowEventUtilities::WindowEventListeners WindowEventUtilities::_msListeners;
  35. WindowEventUtilities::Windows WindowEventUtilities::_msWindows;
  36. //--------------------------------------------------------------------------------//
  37. void WindowEventUtilities::messagePump()
  38. {
  39. #if CM_PLATFORM == CM_PLATFORM_WIN32
  40. // Windows Message Loop (NULL means check all HWNDs belonging to this context)
  41. MSG msg;
  42. while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  43. {
  44. TranslateMessage( &msg );
  45. DispatchMessage( &msg );
  46. }
  47. #elif CM_PLATFORM == CM_PLATFORM_LINUX
  48. //GLX Message Pump
  49. Windows::iterator win = _msWindows.begin();
  50. Windows::iterator end = _msWindows.end();
  51. Display* xDisplay = 0; // same for all windows
  52. for (; win != end; win++)
  53. {
  54. XID xid;
  55. XEvent event;
  56. if (!xDisplay)
  57. (*win)->getCustomAttribute("XDISPLAY", &xDisplay);
  58. (*win)->getCustomAttribute("WINDOW", &xid);
  59. while (XCheckWindowEvent (xDisplay, xid, StructureNotifyMask | VisibilityChangeMask | FocusChangeMask, &event))
  60. {
  61. GLXProc(*win, event);
  62. }
  63. // The ClientMessage event does not appear under any Event Mask
  64. while (XCheckTypedWindowEvent (xDisplay, xid, ClientMessage, &event))
  65. {
  66. GLXProc(*win, event);
  67. }
  68. }
  69. #elif CM_PLATFORM == CM_PLATFORM_APPLE && !defined __OBJC__ && !defined __LP64__
  70. // OSX Message Pump
  71. EventRef event = NULL;
  72. EventTargetRef targetWindow;
  73. targetWindow = GetEventDispatcherTarget();
  74. // If we are unable to get the target then we no longer care about events.
  75. if( !targetWindow ) return;
  76. // Grab the next event, process it if it is a window event
  77. if( ReceiveNextEvent( 0, NULL, kEventDurationNoWait, true, &event ) == noErr )
  78. {
  79. // Dispatch the event
  80. SendEventToEventTarget( event, targetWindow );
  81. ReleaseEvent( event );
  82. }
  83. #endif
  84. }
  85. //--------------------------------------------------------------------------------//
  86. void WindowEventUtilities::addWindowEventListener( RenderWindow* window, WindowEventListener* listener )
  87. {
  88. _msListeners.insert(std::make_pair(window, listener));
  89. }
  90. //--------------------------------------------------------------------------------//
  91. void WindowEventUtilities::removeWindowEventListener( RenderWindow* window, WindowEventListener* listener )
  92. {
  93. WindowEventListeners::iterator i = _msListeners.begin(), e = _msListeners.end();
  94. for( ; i != e; ++i )
  95. {
  96. if( i->first == window && i->second == listener )
  97. {
  98. _msListeners.erase(i);
  99. break;
  100. }
  101. }
  102. }
  103. //--------------------------------------------------------------------------------//
  104. void WindowEventUtilities::_addRenderWindow(RenderWindow* window)
  105. {
  106. _msWindows.push_back(window);
  107. }
  108. //--------------------------------------------------------------------------------//
  109. void WindowEventUtilities::_removeRenderWindow(RenderWindow* window)
  110. {
  111. Windows::iterator i = std::find(_msWindows.begin(), _msWindows.end(), window);
  112. if( i != _msWindows.end() )
  113. _msWindows.erase( i );
  114. }
  115. #if CM_PLATFORM == CM_PLATFORM_WIN32
  116. //--------------------------------------------------------------------------------//
  117. LRESULT CALLBACK WindowEventUtilities::_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  118. {
  119. if (uMsg == WM_CREATE)
  120. { // Store pointer to Win32Window in user data area
  121. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  122. return 0;
  123. }
  124. // look up window instance
  125. // note: it is possible to get a WM_SIZE before WM_CREATE
  126. RenderWindow* win = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  127. if (!win)
  128. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  129. //LogManager* log = LogManager::getSingletonPtr();
  130. //Iterator of all listeners registered to this RenderWindow
  131. WindowEventListeners::iterator index,
  132. start = _msListeners.lower_bound(win),
  133. end = _msListeners.upper_bound(win);
  134. switch( uMsg )
  135. {
  136. case WM_ACTIVATE:
  137. {
  138. bool active = (LOWORD(wParam) != WA_INACTIVE);
  139. if( active )
  140. {
  141. win->setActive( true );
  142. if(!win->getHasFocus())
  143. win->setHasFocus(true);
  144. }
  145. else
  146. {
  147. if( win->isDeactivatedOnFocusChange() )
  148. {
  149. win->setActive( false );
  150. }
  151. if(win->getHasFocus())
  152. win->setHasFocus(false);
  153. }
  154. for( ; start != end; ++start )
  155. (start->second)->windowFocusChange(win);
  156. break;
  157. }
  158. case WM_SYSKEYDOWN:
  159. switch( wParam )
  160. {
  161. case VK_CONTROL:
  162. case VK_SHIFT:
  163. case VK_MENU: //ALT
  164. //return zero to bypass defProc and signal we processed the message
  165. return 0;
  166. }
  167. break;
  168. case WM_SYSKEYUP:
  169. switch( wParam )
  170. {
  171. case VK_CONTROL:
  172. case VK_SHIFT:
  173. case VK_MENU: //ALT
  174. case VK_F10:
  175. //return zero to bypass defProc and signal we processed the message
  176. return 0;
  177. }
  178. break;
  179. case WM_SYSCHAR:
  180. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  181. if (wParam != VK_SPACE)
  182. return 0;
  183. break;
  184. case WM_ENTERSIZEMOVE:
  185. //log->logMessage("WM_ENTERSIZEMOVE");
  186. break;
  187. case WM_EXITSIZEMOVE:
  188. //log->logMessage("WM_EXITSIZEMOVE");
  189. break;
  190. case WM_MOVE:
  191. //log->logMessage("WM_MOVE");
  192. win->windowMovedOrResized();
  193. for(index = start; index != end; ++index)
  194. (index->second)->windowMoved(win);
  195. break;
  196. case WM_DISPLAYCHANGE:
  197. win->windowMovedOrResized();
  198. for(index = start; index != end; ++index)
  199. (index->second)->windowResized(win);
  200. break;
  201. case WM_SIZE:
  202. //log->logMessage("WM_SIZE");
  203. win->windowMovedOrResized();
  204. for(index = start; index != end; ++index)
  205. (index->second)->windowResized(win);
  206. break;
  207. case WM_SETCURSOR:
  208. if(Cursor::isHidden())
  209. SetCursor(0);
  210. else
  211. SetCursor(Cursor::getHCursor());
  212. return true;
  213. case WM_GETMINMAXINFO:
  214. // Prevent the window from going smaller than some minimu size
  215. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  216. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  217. break;
  218. case WM_CLOSE:
  219. {
  220. //log->logMessage("WM_CLOSE");
  221. bool close = true;
  222. for(index = start; index != end; ++index)
  223. {
  224. if (!(index->second)->windowClosing(win))
  225. close = false;
  226. }
  227. if (!close) return 0;
  228. for(index = _msListeners.lower_bound(win); index != end; ++index)
  229. (index->second)->windowClosed(win);
  230. gApplication().stopMainLoop();
  231. return 0;
  232. }
  233. }
  234. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  235. }
  236. #elif CM_PLATFORM == CM_PLATFORM_LINUX
  237. //--------------------------------------------------------------------------------//
  238. void GLXProc( RenderWindow *win, const XEvent &event )
  239. {
  240. //An iterator for the window listeners
  241. WindowEventUtilities::WindowEventListeners::iterator index,
  242. start = WindowEventUtilities::_msListeners.lower_bound(win),
  243. end = WindowEventUtilities::_msListeners.upper_bound(win);
  244. switch(event.type)
  245. {
  246. case ClientMessage:
  247. {
  248. ::Atom atom;
  249. win->getCustomAttribute("ATOM", &atom);
  250. if(event.xclient.format == 32 && event.xclient.data.l[0] == (long)atom)
  251. { //Window closed by window manager
  252. //Send message first, to allow app chance to unregister things that need done before
  253. //window is shutdown
  254. bool close = true;
  255. for(index = start ; index != end; ++index)
  256. {
  257. if (!(index->second)->windowClosing(win))
  258. close = false;
  259. }
  260. if (!close) return;
  261. for(index = start ; index != end; ++index)
  262. (index->second)->windowClosed(win);
  263. }
  264. break;
  265. }
  266. case DestroyNotify:
  267. {
  268. if (!win->isClosed())
  269. {
  270. // Window closed without window manager warning.
  271. for(index = start ; index != end; ++index)
  272. (index->second)->windowClosed(win);
  273. }
  274. break;
  275. }
  276. case ConfigureNotify:
  277. {
  278. // This could be slightly more efficient if windowMovedOrResized took arguments:
  279. unsigned int oldWidth, oldHeight, oldDepth;
  280. int oldLeft, oldTop;
  281. win->getMetrics(oldWidth, oldHeight, oldDepth, oldLeft, oldTop);
  282. win->windowMovedOrResized();
  283. unsigned int newWidth, newHeight, newDepth;
  284. int newLeft, newTop;
  285. win->getMetrics(newWidth, newHeight, newDepth, newLeft, newTop);
  286. if (newLeft != oldLeft || newTop != oldTop)
  287. {
  288. for(index = start ; index != end; ++index)
  289. (index->second)->windowMoved(win);
  290. }
  291. if (newWidth != oldWidth || newHeight != oldHeight)
  292. {
  293. for(index = start ; index != end; ++index)
  294. (index->second)->windowResized(win);
  295. }
  296. break;
  297. }
  298. case FocusIn: // Gained keyboard focus
  299. case FocusOut: // Lost keyboard focus
  300. for(index = start ; index != end; ++index)
  301. (index->second)->windowFocusChange(win);
  302. break;
  303. case MapNotify: //Restored
  304. win->setActive( true );
  305. for(index = start ; index != end; ++index)
  306. (index->second)->windowFocusChange(win);
  307. break;
  308. case UnmapNotify: //Minimised
  309. win->setActive( false );
  310. win->setVisible( false );
  311. for(index = start ; index != end; ++index)
  312. (index->second)->windowFocusChange(win);
  313. break;
  314. case VisibilityNotify:
  315. switch(event.xvisibility.state)
  316. {
  317. case VisibilityUnobscured:
  318. win->setActive( true );
  319. win->setVisible( true );
  320. break;
  321. case VisibilityPartiallyObscured:
  322. win->setActive( true );
  323. win->setVisible( true );
  324. break;
  325. case VisibilityFullyObscured:
  326. win->setActive( false );
  327. win->setVisible( false );
  328. break;
  329. }
  330. for(index = start ; index != end; ++index)
  331. (index->second)->windowFocusChange(win);
  332. break;
  333. default:
  334. break;
  335. } //End switch event.type
  336. }
  337. #elif CM_PLATFORM == CM_PLATFORM_APPLE && !defined __OBJC__ && !defined __LP64__
  338. //--------------------------------------------------------------------------------//
  339. OSStatus WindowEventUtilities::_CarbonWindowHandler(EventHandlerCallRef nextHandler, EventRef event, void* wnd)
  340. {
  341. OSStatus status = noErr;
  342. // Only events from our window should make it here
  343. // This ensures that our user data is our WindowRef
  344. RenderWindow* curWindow = (RenderWindow*)wnd;
  345. if(!curWindow) return eventNotHandledErr;
  346. //Iterator of all listeners registered to this RenderWindow
  347. WindowEventListeners::iterator index,
  348. start = _msListeners.lower_bound(curWindow),
  349. end = _msListeners.upper_bound(curWindow);
  350. // We only get called if a window event happens
  351. UInt32 eventKind = GetEventKind( event );
  352. switch( eventKind )
  353. {
  354. case kEventWindowActivated:
  355. curWindow->setActive( true );
  356. for( ; start != end; ++start )
  357. (start->second)->windowFocusChange(curWindow);
  358. break;
  359. case kEventWindowDeactivated:
  360. if( curWindow->isDeactivatedOnFocusChange() )
  361. {
  362. curWindow->setActive( false );
  363. }
  364. for( ; start != end; ++start )
  365. (start->second)->windowFocusChange(curWindow);
  366. break;
  367. case kEventWindowShown:
  368. case kEventWindowExpanded:
  369. curWindow->setActive( true );
  370. curWindow->setVisible( true );
  371. for( ; start != end; ++start )
  372. (start->second)->windowFocusChange(curWindow);
  373. break;
  374. case kEventWindowHidden:
  375. case kEventWindowCollapsed:
  376. curWindow->setActive( false );
  377. curWindow->setVisible( false );
  378. for( ; start != end; ++start )
  379. (start->second)->windowFocusChange(curWindow);
  380. break;
  381. case kEventWindowDragCompleted:
  382. curWindow->windowMovedOrResized();
  383. for( ; start != end; ++start )
  384. (start->second)->windowMoved(curWindow);
  385. break;
  386. case kEventWindowBoundsChanged:
  387. curWindow->windowMovedOrResized();
  388. for( ; start != end; ++start )
  389. (start->second)->windowResized(curWindow);
  390. break;
  391. case kEventWindowClose:
  392. {
  393. bool close = true;
  394. for( ; start != end; ++start )
  395. {
  396. if (!(start->second)->windowClosing(curWindow))
  397. close = false;
  398. }
  399. if (close)
  400. // This will cause event handling to continue on to the standard handler, which calls
  401. // DisposeWindow(), which leads to the 'kEventWindowClosed' event
  402. status = eventNotHandledErr;
  403. break;
  404. }
  405. case kEventWindowClosed:
  406. for( ; start != end; ++start )
  407. (start->second)->windowClosed(curWindow);
  408. break;
  409. default:
  410. status = eventNotHandledErr;
  411. break;
  412. }
  413. return status;
  414. }
  415. #endif