SDL_rpimouse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #if SDL_VIDEO_DRIVER_RPI
  20. #include "SDL_surface.h"
  21. #include "SDL_hints.h"
  22. #include "SDL_rpivideo.h"
  23. #include "SDL_rpimouse.h"
  24. #include "../SDL_sysvideo.h"
  25. #include "../../events/SDL_mouse_c.h"
  26. #include "../../events/default_cursor.h"
  27. /* Copied from vc_vchi_dispmanx.h which is bugged and tries to include a non existing file */
  28. /* Attributes changes flag mask */
  29. #define ELEMENT_CHANGE_LAYER (1<<0)
  30. #define ELEMENT_CHANGE_OPACITY (1<<1)
  31. #define ELEMENT_CHANGE_DEST_RECT (1<<2)
  32. #define ELEMENT_CHANGE_SRC_RECT (1<<3)
  33. #define ELEMENT_CHANGE_MASK_RESOURCE (1<<4)
  34. #define ELEMENT_CHANGE_TRANSFORM (1<<5)
  35. /* End copied from vc_vchi_dispmanx.h */
  36. static SDL_Cursor *RPI_CreateDefaultCursor(void);
  37. static SDL_Cursor *RPI_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y);
  38. static int RPI_ShowCursor(SDL_Cursor * cursor);
  39. static void RPI_MoveCursor(SDL_Cursor * cursor);
  40. static void RPI_FreeCursor(SDL_Cursor * cursor);
  41. static void RPI_WarpMouse(SDL_Window * window, int x, int y);
  42. static int RPI_WarpMouseGlobal(int x, int y);
  43. static SDL_Cursor *global_cursor;
  44. static SDL_Cursor *
  45. RPI_CreateDefaultCursor(void)
  46. {
  47. return SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
  48. }
  49. /* Create a cursor from a surface */
  50. static SDL_Cursor *
  51. RPI_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
  52. {
  53. RPI_CursorData *curdata;
  54. SDL_Cursor *cursor;
  55. int ret;
  56. VC_RECT_T dst_rect;
  57. Uint32 dummy;
  58. SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
  59. SDL_assert(surface->pitch == surface->w * 4);
  60. cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
  61. if (cursor == NULL) {
  62. SDL_OutOfMemory();
  63. return NULL;
  64. }
  65. curdata = (RPI_CursorData *) SDL_calloc(1, sizeof(*curdata));
  66. if (curdata == NULL) {
  67. SDL_OutOfMemory();
  68. SDL_free(cursor);
  69. return NULL;
  70. }
  71. curdata->hot_x = hot_x;
  72. curdata->hot_y = hot_y;
  73. curdata->w = surface->w;
  74. curdata->h = surface->h;
  75. /* This usage is inspired by Wayland/Weston RPI code, how they figured this out is anyone's guess */
  76. curdata->resource = vc_dispmanx_resource_create(VC_IMAGE_ARGB8888, surface->w | (surface->pitch << 16), surface->h | (surface->h << 16), &dummy);
  77. SDL_assert(curdata->resource);
  78. vc_dispmanx_rect_set(&dst_rect, 0, 0, curdata->w, curdata->h);
  79. /* A note from Weston:
  80. * vc_dispmanx_resource_write_data() ignores ifmt,
  81. * rect.x, rect.width, and uses stride only for computing
  82. * the size of the transfer as rect.height * stride.
  83. * Therefore we can only write rows starting at x=0.
  84. */
  85. ret = vc_dispmanx_resource_write_data(curdata->resource, VC_IMAGE_ARGB8888, surface->pitch, surface->pixels, &dst_rect);
  86. SDL_assert (ret == DISPMANX_SUCCESS);
  87. cursor->driverdata = curdata;
  88. return cursor;
  89. }
  90. /* Show the specified cursor, or hide if cursor is NULL */
  91. static int
  92. RPI_ShowCursor(SDL_Cursor * cursor)
  93. {
  94. int ret;
  95. DISPMANX_UPDATE_HANDLE_T update;
  96. RPI_CursorData *curdata;
  97. VC_RECT_T src_rect, dst_rect;
  98. SDL_Mouse *mouse;
  99. SDL_VideoDisplay *display;
  100. SDL_DisplayData *data;
  101. VC_DISPMANX_ALPHA_T alpha = { DISPMANX_FLAGS_ALPHA_FROM_SOURCE /* flags */ , 255 /*opacity 0->255*/, 0 /* mask */ };
  102. uint32_t layer = SDL_RPI_MOUSELAYER;
  103. const char *env;
  104. mouse = SDL_GetMouse();
  105. if (mouse == NULL) {
  106. return -1;
  107. }
  108. if (cursor != global_cursor) {
  109. if (global_cursor != NULL) {
  110. curdata = (RPI_CursorData *) global_cursor->driverdata;
  111. if (curdata && curdata->element > DISPMANX_NO_HANDLE) {
  112. update = vc_dispmanx_update_start(0);
  113. SDL_assert(update);
  114. ret = vc_dispmanx_element_remove(update, curdata->element);
  115. SDL_assert(ret == DISPMANX_SUCCESS);
  116. ret = vc_dispmanx_update_submit_sync(update);
  117. SDL_assert(ret == DISPMANX_SUCCESS);
  118. curdata->element = DISPMANX_NO_HANDLE;
  119. }
  120. }
  121. global_cursor = cursor;
  122. }
  123. if (cursor == NULL) {
  124. return 0;
  125. }
  126. curdata = (RPI_CursorData *) cursor->driverdata;
  127. if (curdata == NULL) {
  128. return -1;
  129. }
  130. if (mouse->focus == NULL) {
  131. return -1;
  132. }
  133. display = SDL_GetDisplayForWindow(mouse->focus);
  134. if (display == NULL) {
  135. return -1;
  136. }
  137. data = (SDL_DisplayData*) display->driverdata;
  138. if (data == NULL) {
  139. return -1;
  140. }
  141. if (curdata->element == DISPMANX_NO_HANDLE) {
  142. vc_dispmanx_rect_set(&src_rect, 0, 0, curdata->w << 16, curdata->h << 16);
  143. vc_dispmanx_rect_set(&dst_rect, mouse->x - curdata->hot_x, mouse->y - curdata->hot_y, curdata->w, curdata->h);
  144. update = vc_dispmanx_update_start(0);
  145. SDL_assert(update);
  146. env = SDL_GetHint(SDL_HINT_RPI_VIDEO_LAYER);
  147. if (env) {
  148. layer = SDL_atoi(env) + 1;
  149. }
  150. curdata->element = vc_dispmanx_element_add(update,
  151. data->dispman_display,
  152. layer,
  153. &dst_rect,
  154. curdata->resource,
  155. &src_rect,
  156. DISPMANX_PROTECTION_NONE,
  157. &alpha,
  158. DISPMANX_NO_HANDLE, // clamp
  159. DISPMANX_NO_ROTATE);
  160. SDL_assert(curdata->element > DISPMANX_NO_HANDLE);
  161. ret = vc_dispmanx_update_submit_sync(update);
  162. SDL_assert(ret == DISPMANX_SUCCESS);
  163. }
  164. return 0;
  165. }
  166. /* Free a window manager cursor */
  167. static void
  168. RPI_FreeCursor(SDL_Cursor * cursor)
  169. {
  170. int ret;
  171. DISPMANX_UPDATE_HANDLE_T update;
  172. RPI_CursorData *curdata;
  173. if (cursor != NULL) {
  174. curdata = (RPI_CursorData *) cursor->driverdata;
  175. if (curdata != NULL) {
  176. if (curdata->element != DISPMANX_NO_HANDLE) {
  177. update = vc_dispmanx_update_start(0);
  178. SDL_assert(update);
  179. ret = vc_dispmanx_element_remove(update, curdata->element);
  180. SDL_assert(ret == DISPMANX_SUCCESS);
  181. ret = vc_dispmanx_update_submit_sync(update);
  182. SDL_assert(ret == DISPMANX_SUCCESS);
  183. }
  184. if (curdata->resource != DISPMANX_NO_HANDLE) {
  185. ret = vc_dispmanx_resource_delete(curdata->resource);
  186. SDL_assert(ret == DISPMANX_SUCCESS);
  187. }
  188. SDL_free(cursor->driverdata);
  189. }
  190. SDL_free(cursor);
  191. if (cursor == global_cursor) {
  192. global_cursor = NULL;
  193. }
  194. }
  195. }
  196. /* Warp the mouse to (x,y) */
  197. static void
  198. RPI_WarpMouse(SDL_Window * window, int x, int y)
  199. {
  200. RPI_WarpMouseGlobal(x, y);
  201. }
  202. /* Warp the mouse to (x,y) */
  203. static int
  204. RPI_WarpMouseGlobal(int x, int y)
  205. {
  206. RPI_CursorData *curdata;
  207. DISPMANX_UPDATE_HANDLE_T update;
  208. int ret;
  209. VC_RECT_T dst_rect;
  210. VC_RECT_T src_rect;
  211. SDL_Mouse *mouse = SDL_GetMouse();
  212. if (mouse == NULL || mouse->cur_cursor == NULL || mouse->cur_cursor->driverdata == NULL) {
  213. return 0;
  214. }
  215. /* Update internal mouse position. */
  216. SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y);
  217. curdata = (RPI_CursorData *) mouse->cur_cursor->driverdata;
  218. if (curdata->element == DISPMANX_NO_HANDLE) {
  219. return 0;
  220. }
  221. update = vc_dispmanx_update_start(0);
  222. if (!update) {
  223. return 0;
  224. }
  225. src_rect.x = 0;
  226. src_rect.y = 0;
  227. src_rect.width = curdata->w << 16;
  228. src_rect.height = curdata->h << 16;
  229. dst_rect.x = x - curdata->hot_x;
  230. dst_rect.y = y - curdata->hot_y;
  231. dst_rect.width = curdata->w;
  232. dst_rect.height = curdata->h;
  233. ret = vc_dispmanx_element_change_attributes(
  234. update,
  235. curdata->element,
  236. 0,
  237. 0,
  238. 0,
  239. &dst_rect,
  240. &src_rect,
  241. DISPMANX_NO_HANDLE,
  242. DISPMANX_NO_ROTATE);
  243. if (ret != DISPMANX_SUCCESS) {
  244. return SDL_SetError("vc_dispmanx_element_change_attributes() failed");
  245. }
  246. /* Submit asynchronously, otherwise the peformance suffers a lot */
  247. ret = vc_dispmanx_update_submit(update, 0, NULL);
  248. if (ret != DISPMANX_SUCCESS) {
  249. return SDL_SetError("vc_dispmanx_update_submit() failed");
  250. }
  251. return 0;
  252. }
  253. /* Warp the mouse to (x,y) */
  254. static int
  255. RPI_WarpMouseGlobalGraphicOnly(int x, int y)
  256. {
  257. RPI_CursorData *curdata;
  258. DISPMANX_UPDATE_HANDLE_T update;
  259. int ret;
  260. VC_RECT_T dst_rect;
  261. VC_RECT_T src_rect;
  262. SDL_Mouse *mouse = SDL_GetMouse();
  263. if (mouse == NULL || mouse->cur_cursor == NULL || mouse->cur_cursor->driverdata == NULL) {
  264. return 0;
  265. }
  266. curdata = (RPI_CursorData *) mouse->cur_cursor->driverdata;
  267. if (curdata->element == DISPMANX_NO_HANDLE) {
  268. return 0;
  269. }
  270. update = vc_dispmanx_update_start(0);
  271. if (!update) {
  272. return 0;
  273. }
  274. src_rect.x = 0;
  275. src_rect.y = 0;
  276. src_rect.width = curdata->w << 16;
  277. src_rect.height = curdata->h << 16;
  278. dst_rect.x = x - curdata->hot_x;
  279. dst_rect.y = y - curdata->hot_y;
  280. dst_rect.width = curdata->w;
  281. dst_rect.height = curdata->h;
  282. ret = vc_dispmanx_element_change_attributes(
  283. update,
  284. curdata->element,
  285. 0,
  286. 0,
  287. 0,
  288. &dst_rect,
  289. &src_rect,
  290. DISPMANX_NO_HANDLE,
  291. DISPMANX_NO_ROTATE);
  292. if (ret != DISPMANX_SUCCESS) {
  293. return SDL_SetError("vc_dispmanx_element_change_attributes() failed");
  294. }
  295. /* Submit asynchronously, otherwise the peformance suffers a lot */
  296. ret = vc_dispmanx_update_submit(update, 0, NULL);
  297. if (ret != DISPMANX_SUCCESS) {
  298. return SDL_SetError("vc_dispmanx_update_submit() failed");
  299. }
  300. return 0;
  301. }
  302. void
  303. RPI_InitMouse(_THIS)
  304. {
  305. /* FIXME: Using UDEV it should be possible to scan all mice
  306. * but there's no point in doing so as there's no multimice support...yet!
  307. */
  308. SDL_Mouse *mouse = SDL_GetMouse();
  309. mouse->CreateCursor = RPI_CreateCursor;
  310. mouse->ShowCursor = RPI_ShowCursor;
  311. mouse->MoveCursor = RPI_MoveCursor;
  312. mouse->FreeCursor = RPI_FreeCursor;
  313. mouse->WarpMouse = RPI_WarpMouse;
  314. mouse->WarpMouseGlobal = RPI_WarpMouseGlobal;
  315. SDL_SetDefaultCursor(RPI_CreateDefaultCursor());
  316. }
  317. void
  318. RPI_QuitMouse(_THIS)
  319. {
  320. }
  321. /* This is called when a mouse motion event occurs */
  322. static void
  323. RPI_MoveCursor(SDL_Cursor * cursor)
  324. {
  325. SDL_Mouse *mouse = SDL_GetMouse();
  326. /* We must NOT call SDL_SendMouseMotion() on the next call or we will enter recursivity,
  327. * so we create a version of WarpMouseGlobal without it. */
  328. RPI_WarpMouseGlobalGraphicOnly(mouse->x, mouse->y);
  329. }
  330. #endif /* SDL_VIDEO_DRIVER_RPI */
  331. /* vi: set ts=4 sw=4 expandtab: */