SDL_wscons_mouse.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include <sys/time.h>
  20. #include <dev/wscons/wsconsio.h>
  21. #include <unistd.h>
  22. #include <sys/ioctl.h>
  23. #include <stdlib.h>
  24. #include <fcntl.h>
  25. #include "../../events/SDL_mouse_c.h"
  26. typedef struct SDL_WSCONS_mouse_input_data
  27. {
  28. int fd;
  29. SDL_MouseID mouseID;
  30. } SDL_WSCONS_mouse_input_data;
  31. SDL_WSCONS_mouse_input_data *SDL_WSCONS_Init_Mouse(void)
  32. {
  33. #ifdef WSMOUSEIO_SETVERSION
  34. int version = WSMOUSE_EVENT_VERSION;
  35. #endif
  36. SDL_WSCONS_mouse_input_data *input = SDL_calloc(1, sizeof(SDL_WSCONS_mouse_input_data));
  37. if (!input) {
  38. return NULL;
  39. }
  40. input->fd = open("/dev/wsmouse", O_RDWR | O_NONBLOCK | O_CLOEXEC);
  41. if (input->fd == -1) {
  42. SDL_free(input);
  43. return NULL;
  44. }
  45. input->mouseID = SDL_GetNextObjectID();
  46. SDL_AddMouse(input->mouseID, NULL, false);
  47. #ifdef WSMOUSEIO_SETMODE
  48. ioctl(input->fd, WSMOUSEIO_SETMODE, WSMOUSE_COMPAT);
  49. #endif
  50. #ifdef WSMOUSEIO_SETVERSION
  51. ioctl(input->fd, WSMOUSEIO_SETVERSION, &version);
  52. #endif
  53. return input;
  54. }
  55. static Uint64 GetEventTimestamp(struct timespec *time)
  56. {
  57. // FIXME: Get the event time in the SDL tick time base
  58. return SDL_GetTicksNS();
  59. }
  60. void updateMouse(SDL_WSCONS_mouse_input_data *input)
  61. {
  62. struct wscons_event events[64];
  63. int n;
  64. SDL_Mouse *mouse = SDL_GetMouse();
  65. if ((n = read(input->fd, events, sizeof(events))) > 0) {
  66. int i;
  67. n /= sizeof(struct wscons_event);
  68. for (i = 0; i < n; i++) {
  69. Uint64 timestamp = GetEventTimestamp(&events[i].time);
  70. int type = events[i].type;
  71. switch (type) {
  72. case WSCONS_EVENT_MOUSE_DOWN:
  73. case WSCONS_EVENT_MOUSE_UP:
  74. {
  75. Uint8 button = SDL_BUTTON_LEFT + events[i].value;
  76. bool down = (type == WSCONS_EVENT_MOUSE_DOWN);
  77. SDL_SendMouseButton(timestamp, mouse->focus, input->mouseID, button, down);
  78. break;
  79. }
  80. case WSCONS_EVENT_MOUSE_DELTA_X:
  81. {
  82. SDL_SendMouseMotion(timestamp, mouse->focus, input->mouseID, true, (float)events[i].value, 0.0f);
  83. break;
  84. }
  85. case WSCONS_EVENT_MOUSE_DELTA_Y:
  86. {
  87. SDL_SendMouseMotion(timestamp, mouse->focus, input->mouseID, true, 0.0f, -(float)events[i].value);
  88. break;
  89. }
  90. case WSCONS_EVENT_MOUSE_DELTA_W:
  91. {
  92. SDL_SendMouseWheel(timestamp, mouse->focus, input->mouseID, events[i].value, 0, SDL_MOUSEWHEEL_NORMAL);
  93. break;
  94. }
  95. case WSCONS_EVENT_MOUSE_DELTA_Z:
  96. {
  97. SDL_SendMouseWheel(timestamp, mouse->focus, input->mouseID, 0, -events[i].value, SDL_MOUSEWHEEL_NORMAL);
  98. break;
  99. }
  100. default:
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. void SDL_WSCONS_Quit_Mouse(SDL_WSCONS_mouse_input_data *input)
  107. {
  108. if (!input) {
  109. return;
  110. }
  111. close(input->fd);
  112. SDL_free(input);
  113. }