mouse.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "mouse.hpp"
  2. static void pointer_handle_enter(void *data, struct wl_pointer *pointer,
  3. uint32_t serial, struct wl_surface *surface,
  4. wl_fixed_t sx_w, wl_fixed_t sy_w) {
  5. Mouse* m = (Mouse*)data;
  6. float sx = wl_fixed_to_double(sx_w);
  7. float sy = wl_fixed_to_double(sy_w);
  8. m->setMousePos( sx, sy );
  9. m->serial = serial;
  10. m->pointer = pointer;
  11. struct wl_cursor_image *image = m->cursor->images[0];
  12. wl_pointer_set_cursor(pointer, serial, m->surface, image->hotspot_x, image->hotspot_y);
  13. }
  14. static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
  15. uint32_t serial, struct wl_surface *surface) {
  16. }
  17. static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
  18. uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
  19. {
  20. Mouse* m = (Mouse*)data;
  21. float sx = wl_fixed_to_double(sx_w);
  22. float sy = wl_fixed_to_double(sy_w);
  23. m->pointer = pointer;
  24. m->setMousePos( sx, sy );
  25. }
  26. static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
  27. uint32_t time, uint32_t button, uint32_t state_w) {
  28. Mouse* m = (Mouse*)data;
  29. m->pointer = pointer;
  30. m->setButton( button, state_w );
  31. }
  32. static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
  33. uint32_t time, uint32_t axis, wl_fixed_t value) {
  34. }
  35. static const struct wl_pointer_listener pointer_listener = {
  36. pointer_handle_enter,
  37. pointer_handle_leave,
  38. pointer_handle_motion,
  39. pointer_handle_button,
  40. pointer_handle_axis,
  41. };
  42. void Mouse::setButton( int button, int state ) {
  43. for (unsigned int i=0;i<buttons.size();i++ ) {
  44. if ( buttons[i].x == button ) {
  45. buttons[i].y = state;
  46. return;
  47. }
  48. }
  49. buttons.push_back(glm::ivec2(button,state));
  50. }
  51. int Mouse::getButton( int button ) {
  52. for (unsigned int i=0;i<buttons.size();i++ ) {
  53. if ( buttons[i].x == button ) {
  54. return buttons[i].y;
  55. }
  56. }
  57. return 0;
  58. }
  59. void Mouse::setMousePos(float x, float y) {
  60. this->x = x;
  61. this->y = y;
  62. }
  63. glm::vec2 Mouse::getMousePos() {
  64. return glm::vec2( x, y );
  65. }
  66. void Mouse::setCursor( std::string name ) {
  67. if ( name == currentMouseCursor ) {
  68. return;
  69. }
  70. // First we gotta delete the old mouse surface.
  71. wl_surface_destroy( surface );
  72. // Then we get the new cursor image.
  73. cursor = wl_cursor_theme_get_cursor(theme, name.c_str());
  74. struct wl_cursor_image *image = cursor->images[0];
  75. struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image);
  76. // Then we create the new surface
  77. surface = wl_compositor_create_surface(wayland->compositor);
  78. wl_surface_attach(surface, cursor_buf, 0, 0);
  79. wl_surface_damage(surface, 0, 0, image->width, image->height);
  80. wl_surface_commit(surface);
  81. // Finally assign the surface as a pointer
  82. wl_pointer_set_cursor(pointer, serial, surface, image->hotspot_x, image->hotspot_y);
  83. }
  84. Mouse::Mouse(Wayland* wayland) {
  85. wl_pointer_add_listener(wl_seat_get_pointer(wayland->seat), &pointer_listener, this);
  86. theme = wl_cursor_theme_load("default", 16, wayland->shm );
  87. cursor = wl_cursor_theme_get_cursor(theme, "cross");
  88. std::string currentMouseCursor = "cross";
  89. surface = wl_compositor_create_surface(wayland->compositor);
  90. struct wl_cursor_image *image = cursor->images[0];
  91. struct wl_buffer *cursor_buf = wl_cursor_image_get_buffer(image);
  92. wl_surface_attach(surface, cursor_buf, 0, 0);
  93. wl_surface_damage(surface, 0, 0, image->width, image->height);
  94. wl_surface_commit(surface);
  95. }