/src/ois/src/linux/LinuxMouse.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 272 lines · 190 code · 35 blank · 47 comment · 57 complexity · 17d80b45e229581d31368ccc6d342f92 MD5 · raw file

  1. /*
  2. The zlib/libpng License
  3. Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
  4. This software is provided 'as-is', without any express or implied warranty. In no event will
  5. the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose, including commercial
  7. applications, and to alter it and redistribute it freely, subject to the following
  8. restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that
  10. you wrote the original software. If you use this software in a product,
  11. an acknowledgment in the product documentation would be appreciated but is
  12. not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered friosom any source distribution.
  16. */
  17. #include "linux/LinuxMouse.h"
  18. #include "linux/LinuxInputManager.h"
  19. #include "OISException.h"
  20. #include "OISEvents.h"
  21. using namespace OIS;
  22. //-------------------------------------------------------------------//
  23. LinuxMouse::LinuxMouse(InputManager* creator, bool buffered, bool grab, bool hide)
  24. : Mouse(creator->inputSystemName(), buffered, 0, creator)
  25. {
  26. display = 0;
  27. window = 0;
  28. cursor = 0;
  29. grabMouse = grab;
  30. hideMouse = hide;
  31. static_cast<LinuxInputManager*>(mCreator)->_setMouseUsed(true);
  32. }
  33. //-------------------------------------------------------------------//
  34. void LinuxMouse::_initialize()
  35. {
  36. //Clear old state
  37. mState.clear();
  38. mMoved = false;
  39. mWarped = false;
  40. //6 is just some random value... hardly ever would anyone have a window smaller than 6
  41. oldXMouseX = oldXMouseY = 6;
  42. oldXMouseZ = 0;
  43. if( display ) XCloseDisplay(display);
  44. display = 0;
  45. window = static_cast<LinuxInputManager*>(mCreator)->_getWindow();
  46. //Create our local X mListener connection
  47. if( !(display = XOpenDisplay(0)) )
  48. OIS_EXCEPT(E_General, "LinuxMouse::_initialize >> Error opening X!");
  49. //Set it to recieve Mouse Input events
  50. if( XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask) == BadWindow )
  51. OIS_EXCEPT(E_General, "LinuxMouse::_initialize >> X error!");
  52. //Warp mouse inside window
  53. XWarpPointer(display,None,window,0,0,0,0, 6,6);
  54. //Create a blank cursor:
  55. Pixmap bm_no;
  56. XColor black, dummy;
  57. Colormap colormap;
  58. static char no_data[] = { 0,0,0,0,0,0,0,0 };
  59. colormap = DefaultColormap( display, DefaultScreen(display) );
  60. XAllocNamedColor( display, colormap, "black", &black, &dummy );
  61. bm_no = XCreateBitmapFromData( display, window, no_data, 8, 8 );
  62. cursor = XCreatePixmapCursor( display, bm_no, bm_no, &black, &black, 0, 0 );
  63. grab( grabMouse );
  64. hide( hideMouse );
  65. mouseFocusLost = false;
  66. }
  67. //-------------------------------------------------------------------//
  68. LinuxMouse::~LinuxMouse()
  69. {
  70. if( display )
  71. {
  72. grab(false);
  73. hide(false);
  74. XFreeCursor(display, cursor);
  75. XCloseDisplay(display);
  76. }
  77. static_cast<LinuxInputManager*>(mCreator)->_setMouseUsed(false);
  78. }
  79. //-------------------------------------------------------------------//
  80. void LinuxMouse::setBuffered(bool buffered)
  81. {
  82. mBuffered = buffered;
  83. }
  84. //-------------------------------------------------------------------//
  85. void LinuxMouse::capture()
  86. {
  87. //Clear out last frames values
  88. mState.X.rel = 0;
  89. mState.Y.rel = 0;
  90. mState.Z.rel = 0;
  91. _processXEvents();
  92. mWarped = false;
  93. if( mMoved == true )
  94. {
  95. if( mBuffered && mListener )
  96. mListener->mouseMoved( MouseEvent( this, mState ) );
  97. mMoved = false;
  98. }
  99. //Check for losing/gaining mouse grab focus (alt-tab, etc)
  100. if( grabMouse )
  101. {
  102. if( static_cast<LinuxInputManager*>(mCreator)->_getGrabState() )
  103. {
  104. if( mouseFocusLost ) //We just regained mouse grab focus
  105. {
  106. grab( true );
  107. hide( hideMouse );
  108. mouseFocusLost = false;
  109. }
  110. }
  111. else
  112. {
  113. if( mouseFocusLost == false ) //We just lost mouse grab focus
  114. {
  115. grab( false );
  116. hide( false );
  117. mouseFocusLost = true;
  118. }
  119. }
  120. }
  121. }
  122. //-------------------------------------------------------------------//
  123. void LinuxMouse::_processXEvents()
  124. {
  125. //X11 Button Events: 1=left 2=middle 3=right; Our Bit Postion: 1=Left 2=Right 3=Middle
  126. char mask[4] = {0,1,4,2};
  127. XEvent event;
  128. //Poll x11 for events mouse events
  129. while( XPending(display) > 0 )
  130. {
  131. XNextEvent(display, &event);
  132. if( event.type == MotionNotify )
  133. { //Mouse moved
  134. //Ignore out of bounds mouse if we just warped
  135. if( mWarped )
  136. {
  137. if(event.xmotion.x < 5 || event.xmotion.x > mState.width - 5 ||
  138. event.xmotion.y < 5 || event.xmotion.y > mState.height - 5)
  139. continue;
  140. }
  141. //Compute this frames Relative X & Y motion
  142. int dx = event.xmotion.x - oldXMouseX;
  143. int dy = event.xmotion.y - oldXMouseY;
  144. //Store old values for next time to compute relative motion
  145. oldXMouseX = event.xmotion.x;
  146. oldXMouseY = event.xmotion.y;
  147. mState.X.abs += dx;
  148. mState.Y.abs += dy;
  149. mState.X.rel += dx;
  150. mState.Y.rel += dy;
  151. //Check to see if we are grabbing the mouse to the window (requires clipping and warping)
  152. if( grabMouse )
  153. {
  154. if( mState.X.abs < 0 )
  155. mState.X.abs = 0;
  156. else if( mState.X.abs > mState.width )
  157. mState.X.abs = mState.width;
  158. if( mState.Y.abs < 0 )
  159. mState.Y.abs = 0;
  160. else if( mState.Y.abs > mState.height )
  161. mState.Y.abs = mState.height;
  162. if( mouseFocusLost == false )
  163. {
  164. //Keep mouse in window (fudge factor)
  165. if(event.xmotion.x < 5 || event.xmotion.x > mState.width - 5 ||
  166. event.xmotion.y < 5 || event.xmotion.y > mState.height - 5 )
  167. {
  168. oldXMouseX = mState.width >> 1; //center x
  169. oldXMouseY = mState.height >> 1; //center y
  170. XWarpPointer(display, None, window, 0, 0, 0, 0, oldXMouseX, oldXMouseY);
  171. mWarped = true;
  172. }
  173. }
  174. }
  175. mMoved = true;
  176. }
  177. else if( event.type == ButtonPress )
  178. { //Button down
  179. static_cast<LinuxInputManager*>(mCreator)->_setGrabState(true);
  180. if( event.xbutton.button < 4 )
  181. {
  182. mState.buttons |= mask[event.xbutton.button];
  183. if( mBuffered && mListener )
  184. if( mListener->mousePressed( MouseEvent( this, mState ),
  185. (MouseButtonID)(mask[event.xbutton.button] >> 1)) == false )
  186. return;
  187. }
  188. }
  189. else if( event.type == ButtonRelease )
  190. { //Button up
  191. if( event.xbutton.button < 4 )
  192. {
  193. mState.buttons &= ~mask[event.xbutton.button];
  194. if( mBuffered && mListener )
  195. if( mListener->mouseReleased( MouseEvent( this, mState ),
  196. (MouseButtonID)(mask[event.xbutton.button] >> 1)) == false )
  197. return;
  198. }
  199. //The Z axis gets pushed/released pair message (this is up)
  200. else if( event.xbutton.button == 4 )
  201. {
  202. mState.Z.rel += 120;
  203. mState.Z.abs += 120;
  204. mMoved = true;
  205. }
  206. //The Z axis gets pushed/released pair message (this is down)
  207. else if( event.xbutton.button == 5 )
  208. {
  209. mState.Z.rel -= 120;
  210. mState.Z.abs -= 120;
  211. mMoved = true;
  212. }
  213. }
  214. }
  215. }
  216. //-------------------------------------------------------------------//
  217. void LinuxMouse::grab(bool grab)
  218. {
  219. if( grab )
  220. XGrabPointer(display, window, True, 0, GrabModeAsync, GrabModeAsync, window, None, CurrentTime);
  221. else
  222. XUngrabPointer(display, CurrentTime);
  223. }
  224. //-------------------------------------------------------------------//
  225. void LinuxMouse::hide(bool hide)
  226. {
  227. if( hide )
  228. XDefineCursor(display, window, cursor);
  229. else
  230. XUndefineCursor(display, window);
  231. }