CmWindowEventUtilities.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include "CmInput.h"
  30. #if CM_PLATFORM == CM_PLATFORM_LINUX
  31. #include <X11/Xlib.h>
  32. void GLXProc( CamelotFramework::RenderWindow *win, const XEvent &event );
  33. #endif
  34. using namespace CamelotFramework;
  35. WindowEventUtilities::Windows WindowEventUtilities::_msWindows;
  36. boost::signal<void(const Int2&)> WindowEventUtilities::onMouseMoved;
  37. boost::signal<void(CamelotFramework::UINT32)> WindowEventUtilities::onCharInput;
  38. //--------------------------------------------------------------------------------//
  39. void WindowEventUtilities::messagePump()
  40. {
  41. #if CM_PLATFORM == CM_PLATFORM_WIN32
  42. // Windows Message Loop (NULL means check all HWNDs belonging to this context)
  43. MSG msg;
  44. while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  45. {
  46. TranslateMessage( &msg );
  47. DispatchMessage( &msg );
  48. }
  49. #elif CM_PLATFORM == CM_PLATFORM_LINUX
  50. //GLX Message Pump
  51. Windows::iterator win = _msWindows.begin();
  52. Windows::iterator end = _msWindows.end();
  53. Display* xDisplay = 0; // same for all windows
  54. for (; win != end; win++)
  55. {
  56. XID xid;
  57. XEvent event;
  58. if (!xDisplay)
  59. (*win)->getCustomAttribute("XDISPLAY", &xDisplay);
  60. (*win)->getCustomAttribute("WINDOW", &xid);
  61. while (XCheckWindowEvent (xDisplay, xid, StructureNotifyMask | VisibilityChangeMask | FocusChangeMask, &event))
  62. {
  63. GLXProc(*win, event);
  64. }
  65. // The ClientMessage event does not appear under any Event Mask
  66. while (XCheckTypedWindowEvent (xDisplay, xid, ClientMessage, &event))
  67. {
  68. GLXProc(*win, event);
  69. }
  70. }
  71. #elif CM_PLATFORM == CM_PLATFORM_APPLE && !defined __OBJC__ && !defined __LP64__
  72. // OSX Message Pump
  73. EventRef event = NULL;
  74. EventTargetRef targetWindow;
  75. targetWindow = GetEventDispatcherTarget();
  76. // If we are unable to get the target then we no longer care about events.
  77. if( !targetWindow ) return;
  78. // Grab the next event, process it if it is a window event
  79. if( ReceiveNextEvent( 0, NULL, kEventDurationNoWait, true, &event ) == noErr )
  80. {
  81. // Dispatch the event
  82. SendEventToEventTarget( event, targetWindow );
  83. ReleaseEvent( event );
  84. }
  85. #endif
  86. }
  87. //--------------------------------------------------------------------------------//
  88. void WindowEventUtilities::_addRenderWindow(RenderWindow* window)
  89. {
  90. _msWindows.push_back(window);
  91. }
  92. //--------------------------------------------------------------------------------//
  93. void WindowEventUtilities::_removeRenderWindow(RenderWindow* window)
  94. {
  95. Windows::iterator i = std::find(_msWindows.begin(), _msWindows.end(), window);
  96. if( i != _msWindows.end() )
  97. _msWindows.erase( i );
  98. }
  99. #if CM_PLATFORM == CM_PLATFORM_WIN32
  100. //--------------------------------------------------------------------------------//
  101. LRESULT CALLBACK WindowEventUtilities::_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  102. {
  103. if (uMsg == WM_CREATE)
  104. { // Store pointer to Win32Window in user data area
  105. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  106. return 0;
  107. }
  108. // look up window instance
  109. // note: it is possible to get a WM_SIZE before WM_CREATE
  110. RenderWindow* win = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  111. if (!win)
  112. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  113. switch( uMsg )
  114. {
  115. case WM_ACTIVATE:
  116. {
  117. bool active = (LOWORD(wParam) != WA_INACTIVE);
  118. if( active )
  119. {
  120. win->setActive(true);
  121. if(!win->hasFocus())
  122. win->_windowFocusReceived();
  123. }
  124. else
  125. {
  126. if(win->hasFocus())
  127. win->_windowFocusLost();
  128. }
  129. break;
  130. }
  131. case WM_SYSKEYDOWN:
  132. switch( wParam )
  133. {
  134. case VK_CONTROL:
  135. case VK_SHIFT:
  136. case VK_MENU: //ALT
  137. //return zero to bypass defProc and signal we processed the message
  138. return 0;
  139. }
  140. break;
  141. case WM_SYSKEYUP:
  142. switch( wParam )
  143. {
  144. case VK_CONTROL:
  145. case VK_SHIFT:
  146. case VK_MENU: //ALT
  147. case VK_F10:
  148. //return zero to bypass defProc and signal we processed the message
  149. return 0;
  150. }
  151. break;
  152. case WM_SYSCHAR:
  153. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  154. if (wParam != VK_SPACE)
  155. return 0;
  156. break;
  157. case WM_ENTERSIZEMOVE:
  158. break;
  159. case WM_EXITSIZEMOVE:
  160. break;
  161. case WM_MOVE:
  162. win->_windowMovedOrResized();
  163. break;
  164. case WM_DISPLAYCHANGE:
  165. win->_windowMovedOrResized();
  166. break;
  167. case WM_SIZE:
  168. win->_windowMovedOrResized();
  169. break;
  170. case WM_SETCURSOR:
  171. if(Cursor::isHidden())
  172. Cursor::_win32HideCursor();
  173. else
  174. Cursor::_win32ShowCursor();
  175. return true;
  176. case WM_GETMINMAXINFO:
  177. // Prevent the window from going smaller than some minimu size
  178. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  179. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  180. break;
  181. case WM_CLOSE:
  182. {
  183. // TODO - Only stop main loop if primary window is closed!!
  184. gApplication().stopMainLoop();
  185. return 0;
  186. }
  187. case WM_NCLBUTTONUP:
  188. {
  189. int b = 5;
  190. break;
  191. }
  192. case WM_LBUTTONUP:
  193. {
  194. int a = 5;
  195. break;
  196. }
  197. case WM_MOUSEMOVE:
  198. {
  199. POINT mousePos;
  200. mousePos.x = GET_X_LPARAM(lParam);
  201. mousePos.y = GET_Y_LPARAM(lParam);
  202. ClientToScreen(hWnd, &mousePos);
  203. if(!onMouseMoved.empty())
  204. onMouseMoved(Int2(mousePos.x, mousePos.y));
  205. return true;
  206. }
  207. case WM_DEADCHAR:
  208. case WM_CHAR:
  209. {
  210. switch (wParam)
  211. {
  212. case VK_BACK:
  213. case 0x0A: // linefeed
  214. case 0x0D: // carriage return
  215. case VK_ESCAPE:
  216. case VK_TAB:
  217. break;
  218. default: // displayable character
  219. {
  220. UINT8 scanCode = (lParam >> 16) & 0xFF;
  221. BYTE keyState[256];
  222. HKL layout = GetKeyboardLayout(0);
  223. if(GetKeyboardState(keyState) == 0)
  224. return 0;
  225. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  226. if(vk == 0)
  227. return 0;
  228. bool isDeadKey = (MapVirtualKeyEx(vk, MAPVK_VK_TO_CHAR, layout) & (1 << 31)) != 0;
  229. if(isDeadKey)
  230. return 0;
  231. wchar_t buff[3] = {0};
  232. int numChars = ToUnicodeEx(vk, scanCode, keyState, buff, 3, 0, layout);
  233. // TODO - I am ignoring dead keys here - primarily because I haven't found a good way of retrieving non-combined dead key
  234. // value. ToUnicodeEx and MapVirtualKeyEx only return precombined (i.e. spacing) versions, which can't be combined using other characters.
  235. // I need non-combined version so I can use it with FoldString to apply to a certain character.
  236. UINT32 finalChar = 0;
  237. if(numChars == 1)
  238. finalChar = buff[0];
  239. else
  240. return 0;
  241. if(!onCharInput.empty())
  242. onCharInput(finalChar);
  243. return 0;
  244. }
  245. }
  246. break;
  247. }
  248. }
  249. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  250. }
  251. #elif CM_PLATFORM == CM_PLATFORM_LINUX
  252. //--------------------------------------------------------------------------------//
  253. void GLXProc( RenderWindow *win, const XEvent &event )
  254. {
  255. switch(event.type)
  256. {
  257. case ClientMessage:
  258. {
  259. ::Atom atom;
  260. win->getCustomAttribute("ATOM", &atom);
  261. if(event.xclient.format == 32 && event.xclient.data.l[0] == (long)atom)
  262. {
  263. //Window closed by window manager
  264. }
  265. break;
  266. }
  267. case DestroyNotify:
  268. {
  269. if (!win->isClosed())
  270. {
  271. // Window closed without window manager warning.
  272. }
  273. break;
  274. }
  275. case ConfigureNotify:
  276. {
  277. // This could be slightly more efficient if windowMovedOrResized took arguments:
  278. unsigned int oldWidth, oldHeight, oldDepth;
  279. int oldLeft, oldTop;
  280. win->getMetrics(oldWidth, oldHeight, oldDepth, oldLeft, oldTop);
  281. win->_windowMovedOrResized();
  282. unsigned int newWidth, newHeight, newDepth;
  283. int newLeft, newTop;
  284. win->getMetrics(newWidth, newHeight, newDepth, newLeft, newTop);
  285. break;
  286. }
  287. case FocusIn: // Gained keyboard focus
  288. case FocusOut: // Lost keyboard focus
  289. break;
  290. case MapNotify: //Restored
  291. win->setActive( true );
  292. break;
  293. case UnmapNotify: //Minimised
  294. win->setActive( false );
  295. win->setVisible( false );
  296. break;
  297. case VisibilityNotify:
  298. switch(event.xvisibility.state)
  299. {
  300. case VisibilityUnobscured:
  301. win->setActive( true );
  302. win->setVisible( true );
  303. break;
  304. case VisibilityPartiallyObscured:
  305. win->setActive( true );
  306. win->setVisible( true );
  307. break;
  308. case VisibilityFullyObscured:
  309. win->setActive( false );
  310. win->setVisible( false );
  311. break;
  312. }
  313. break;
  314. default:
  315. break;
  316. } //End switch event.type
  317. }
  318. #elif CM_PLATFORM == CM_PLATFORM_APPLE && !defined __OBJC__ && !defined __LP64__
  319. //--------------------------------------------------------------------------------//
  320. OSStatus WindowEventUtilities::_CarbonWindowHandler(EventHandlerCallRef nextHandler, EventRef event, void* wnd)
  321. {
  322. OSStatus status = noErr;
  323. // Only events from our window should make it here
  324. // This ensures that our user data is our WindowRef
  325. RenderWindow* curWindow = (RenderWindow*)wnd;
  326. if(!curWindow) return eventNotHandledErr;
  327. // We only get called if a window event happens
  328. UInt32 eventKind = GetEventKind( event );
  329. switch( eventKind )
  330. {
  331. case kEventWindowActivated:
  332. curWindow->setActive( true );
  333. break;
  334. case kEventWindowDeactivated:
  335. break;
  336. case kEventWindowShown:
  337. case kEventWindowExpanded:
  338. curWindow->setActive( true );
  339. curWindow->setVisible( true );
  340. break;
  341. case kEventWindowHidden:
  342. case kEventWindowCollapsed:
  343. curWindow->setActive( false );
  344. curWindow->setVisible( false );
  345. break;
  346. case kEventWindowDragCompleted:
  347. curWindow->_windowMovedOrResized();
  348. break;
  349. case kEventWindowBoundsChanged:
  350. curWindow->_windowMovedOrResized();
  351. break;
  352. case kEventWindowClose:
  353. {
  354. bool close = true;
  355. if (close)
  356. // This will cause event handling to continue on to the standard handler, which calls
  357. // DisposeWindow(), which leads to the 'kEventWindowClosed' event
  358. status = eventNotHandledErr;
  359. break;
  360. }
  361. case kEventWindowClosed:
  362. break;
  363. default:
  364. status = eventNotHandledErr;
  365. break;
  366. }
  367. return status;
  368. }
  369. #endif