mouse.cpp 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "mouse.hpp"
  2. void Mouse::setButton( int button, int state ) {
  3. for (unsigned int i=0;i<buttons.size();i++ ) {
  4. if ( buttons[i].x == button ) {
  5. buttons[i].y = state;
  6. return;
  7. }
  8. }
  9. buttons.push_back(glm::ivec2(button,state));
  10. }
  11. int Mouse::getButton( int button ) {
  12. for (unsigned int i=0;i<buttons.size();i++ ) {
  13. if ( buttons[i].x == button ) {
  14. return buttons[i].y;
  15. }
  16. }
  17. return 0;
  18. }
  19. glm::vec2 Mouse::getMousePos() {
  20. Window root, child;
  21. int mx, my;
  22. int wx, wy;
  23. unsigned int mask;
  24. XQueryPointer( x11->display, x11->root, &root, &child, &mx, &my, &wx, &wy, &mask );
  25. return glm::vec2( mx, my );
  26. }
  27. void Mouse::setCursor( int cursor ) {
  28. if ( currentCursor == cursor ) {
  29. return;
  30. }
  31. XFreeCursor( x11->display, xcursor );
  32. xcursor = XCreateFontCursor( x11->display, cursor );
  33. XChangeActivePointerGrab( x11->display,
  34. PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
  35. xcursor, CurrentTime );
  36. }
  37. Mouse::Mouse(X11* x11) {
  38. this->x11 = x11;
  39. currentCursor = XC_cross;
  40. xcursor = XCreateFontCursor( x11->display, XC_cross );
  41. XGrabPointer( x11->display, x11->root, True,
  42. PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
  43. GrabModeAsync, GrabModeAsync, None, xcursor, CurrentTime );
  44. }
  45. Mouse::~Mouse() {
  46. XUngrabPointer( x11->display, CurrentTime );
  47. }
  48. void Mouse::tick() {
  49. XEvent event;
  50. while ( XCheckTypedEvent( x11->display, ButtonPress, &event ) ) {
  51. setButton( event.xbutton.button, 1 );
  52. }
  53. while ( XCheckTypedEvent( x11->display, ButtonRelease, &event ) ) {
  54. setButton( event.xbutton.button, 0 );
  55. }
  56. while ( XCheckTypedEvent( x11->display, EnterNotify, &event ) ) {
  57. if ( event.xcrossing.subwindow != None ) {
  58. hoverWindow = event.xcrossing.subwindow;
  59. } else {
  60. hoverWindow = event.xcrossing.window;
  61. }
  62. }
  63. }