Skip to content

Latest commit

 

History

History
393 lines (332 loc) · 11.8 KB

SDL_rpimouse.c

File metadata and controls

393 lines (332 loc) · 11.8 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 5, 2019
Jan 5, 2019
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_RPI
#include "SDL_assert.h"
#include "SDL_surface.h"
Oct 19, 2016
Oct 19, 2016
27
#include "SDL_hints.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "SDL_rpivideo.h"
#include "SDL_rpimouse.h"
#include "../SDL_sysvideo.h"
#include "../../events/SDL_mouse_c.h"
#include "../../events/default_cursor.h"
/* Copied from vc_vchi_dispmanx.h which is bugged and tries to include a non existing file */
/* Attributes changes flag mask */
#define ELEMENT_CHANGE_LAYER (1<<0)
#define ELEMENT_CHANGE_OPACITY (1<<1)
#define ELEMENT_CHANGE_DEST_RECT (1<<2)
#define ELEMENT_CHANGE_SRC_RECT (1<<3)
#define ELEMENT_CHANGE_MASK_RESOURCE (1<<4)
#define ELEMENT_CHANGE_TRANSFORM (1<<5)
/* End copied from vc_vchi_dispmanx.h */
static SDL_Cursor *RPI_CreateDefaultCursor(void);
static SDL_Cursor *RPI_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y);
static int RPI_ShowCursor(SDL_Cursor * cursor);
static void RPI_MoveCursor(SDL_Cursor * cursor);
static void RPI_FreeCursor(SDL_Cursor * cursor);
static void RPI_WarpMouse(SDL_Window * window, int x, int y);
Jul 18, 2015
Jul 18, 2015
52
static int RPI_WarpMouseGlobal(int x, int y);
Nov 27, 2018
Nov 27, 2018
54
55
static SDL_Cursor *global_cursor;
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
static SDL_Cursor *
RPI_CreateDefaultCursor(void)
{
return SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
}
/* Create a cursor from a surface */
static SDL_Cursor *
RPI_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
{
RPI_CursorData *curdata;
SDL_Cursor *cursor;
int ret;
VC_RECT_T dst_rect;
Uint32 dummy;
SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
SDL_assert(surface->pitch == surface->w * 4);
cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
Mar 3, 2016
Mar 3, 2016
76
77
78
79
if (cursor == NULL) {
SDL_OutOfMemory();
return NULL;
}
80
curdata = (RPI_CursorData *) SDL_calloc(1, sizeof(*curdata));
Mar 3, 2016
Mar 3, 2016
81
82
83
84
85
if (curdata == NULL) {
SDL_OutOfMemory();
SDL_free(cursor);
return NULL;
}
86
87
88
89
90
91
92
curdata->hot_x = hot_x;
curdata->hot_y = hot_y;
curdata->w = surface->w;
curdata->h = surface->h;
/* This usage is inspired by Wayland/Weston RPI code, how they figured this out is anyone's guess */
Oct 13, 2016
Oct 13, 2016
93
curdata->resource = vc_dispmanx_resource_create(VC_IMAGE_ARGB8888, surface->w | (surface->pitch << 16), surface->h | (surface->h << 16), &dummy);
94
SDL_assert(curdata->resource);
Oct 13, 2016
Oct 13, 2016
95
vc_dispmanx_rect_set(&dst_rect, 0, 0, curdata->w, curdata->h);
96
97
98
99
100
101
/* A note from Weston:
* vc_dispmanx_resource_write_data() ignores ifmt,
* rect.x, rect.width, and uses stride only for computing
* the size of the transfer as rect.height * stride.
* Therefore we can only write rows starting at x=0.
*/
Oct 13, 2016
Oct 13, 2016
102
103
ret = vc_dispmanx_resource_write_data(curdata->resource, VC_IMAGE_ARGB8888, surface->pitch, surface->pixels, &dst_rect);
SDL_assert (ret == DISPMANX_SUCCESS);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
cursor->driverdata = curdata;
return cursor;
}
/* Show the specified cursor, or hide if cursor is NULL */
static int
RPI_ShowCursor(SDL_Cursor * cursor)
{
int ret;
DISPMANX_UPDATE_HANDLE_T update;
RPI_CursorData *curdata;
VC_RECT_T src_rect, dst_rect;
SDL_Mouse *mouse;
SDL_VideoDisplay *display;
SDL_DisplayData *data;
VC_DISPMANX_ALPHA_T alpha = { DISPMANX_FLAGS_ALPHA_FROM_SOURCE /* flags */ , 255 /*opacity 0->255*/, 0 /* mask */ };
Oct 19, 2016
Oct 19, 2016
123
124
125
uint32_t layer = SDL_RPI_MOUSELAYER;
const char *env;
126
127
128
129
130
mouse = SDL_GetMouse();
if (mouse == NULL) {
return -1;
}
Nov 27, 2018
Nov 27, 2018
131
132
133
if (cursor == global_cursor) {
return 0;
}
Jul 2, 2019
Jul 2, 2019
135
136
137
138
139
140
141
142
143
144
145
146
if (cursor != global_cursor) {
if (global_cursor != NULL) {
curdata = (RPI_CursorData *) global_cursor->driverdata;
if (curdata && curdata->element > DISPMANX_NO_HANDLE) {
update = vc_dispmanx_update_start(0);
SDL_assert(update);
ret = vc_dispmanx_element_remove(update, curdata->element);
SDL_assert(ret == DISPMANX_SUCCESS);
ret = vc_dispmanx_update_submit_sync(update);
SDL_assert(ret == DISPMANX_SUCCESS);
curdata->element = DISPMANX_NO_HANDLE;
}
Jul 2, 2019
Jul 2, 2019
148
global_cursor = cursor;
Nov 27, 2018
Nov 27, 2018
149
150
151
}
if (cursor == NULL) {
152
153
154
155
156
157
158
159
160
161
162
return 0;
}
curdata = (RPI_CursorData *) cursor->driverdata;
if (curdata == NULL) {
return -1;
}
if (mouse->focus == NULL) {
return -1;
}
Nov 27, 2018
Nov 27, 2018
163
164
165
166
167
168
169
170
171
172
173
174
display = SDL_GetDisplayForWindow(mouse->focus);
if (display == NULL) {
return -1;
}
data = (SDL_DisplayData*) display->driverdata;
if (data == NULL) {
return -1;
}
if (curdata->element == DISPMANX_NO_HANDLE) {
Oct 13, 2016
Oct 13, 2016
175
vc_dispmanx_rect_set(&src_rect, 0, 0, curdata->w << 16, curdata->h << 16);
Nov 27, 2018
Nov 27, 2018
176
vc_dispmanx_rect_set(&dst_rect, mouse->x - curdata->hot_x, mouse->y - curdata->hot_y, curdata->w, curdata->h);
Nov 27, 2018
Nov 27, 2018
178
update = vc_dispmanx_update_start(0);
Oct 13, 2016
Oct 13, 2016
179
SDL_assert(update);
Oct 19, 2016
Oct 19, 2016
181
182
183
184
185
env = SDL_GetHint(SDL_HINT_RPI_VIDEO_LAYER);
if (env) {
layer = SDL_atoi(env) + 1;
}
Oct 13, 2016
Oct 13, 2016
186
curdata->element = vc_dispmanx_element_add(update,
187
data->dispman_display,
Oct 19, 2016
Oct 19, 2016
188
layer,
189
190
191
192
193
194
&dst_rect,
curdata->resource,
&src_rect,
DISPMANX_PROTECTION_NONE,
&alpha,
DISPMANX_NO_HANDLE, // clamp
Nov 27, 2018
Nov 27, 2018
195
DISPMANX_NO_ROTATE);
Oct 13, 2016
Oct 13, 2016
196
197
198
SDL_assert(curdata->element > DISPMANX_NO_HANDLE);
ret = vc_dispmanx_update_submit_sync(update);
SDL_assert(ret == DISPMANX_SUCCESS);
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
}
return 0;
}
/* Free a window manager cursor */
static void
RPI_FreeCursor(SDL_Cursor * cursor)
{
int ret;
DISPMANX_UPDATE_HANDLE_T update;
RPI_CursorData *curdata;
if (cursor != NULL) {
curdata = (RPI_CursorData *) cursor->driverdata;
if (curdata != NULL) {
if (curdata->element != DISPMANX_NO_HANDLE) {
Nov 27, 2018
Nov 27, 2018
217
update = vc_dispmanx_update_start(0);
Oct 13, 2016
Oct 13, 2016
218
219
220
221
222
SDL_assert(update);
ret = vc_dispmanx_element_remove(update, curdata->element);
SDL_assert(ret == DISPMANX_SUCCESS);
ret = vc_dispmanx_update_submit_sync(update);
SDL_assert(ret == DISPMANX_SUCCESS);
223
224
225
}
if (curdata->resource != DISPMANX_NO_HANDLE) {
Oct 13, 2016
Oct 13, 2016
226
227
ret = vc_dispmanx_resource_delete(curdata->resource);
SDL_assert(ret == DISPMANX_SUCCESS);
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
}
SDL_free(cursor->driverdata);
}
SDL_free(cursor);
}
}
/* Warp the mouse to (x,y) */
static void
RPI_WarpMouse(SDL_Window * window, int x, int y)
{
RPI_WarpMouseGlobal(x, y);
}
/* Warp the mouse to (x,y) */
Jul 18, 2015
Jul 18, 2015
244
static int
245
246
247
248
RPI_WarpMouseGlobal(int x, int y)
{
RPI_CursorData *curdata;
DISPMANX_UPDATE_HANDLE_T update;
Oct 1, 2016
Oct 1, 2016
249
int ret;
250
VC_RECT_T dst_rect;
Oct 1, 2016
Oct 1, 2016
251
VC_RECT_T src_rect;
252
253
SDL_Mouse *mouse = SDL_GetMouse();
Oct 13, 2016
Oct 13, 2016
254
255
256
if (mouse == NULL || mouse->cur_cursor == NULL || mouse->cur_cursor->driverdata == NULL) {
return 0;
}
Oct 1, 2016
Oct 1, 2016
257
Dec 5, 2017
Dec 5, 2017
258
259
260
261
262
263
264
265
/* Update internal mouse position. */
SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y);
curdata = (RPI_CursorData *) mouse->cur_cursor->driverdata;
if (curdata->element == DISPMANX_NO_HANDLE) {
return 0;
}
Nov 27, 2018
Nov 27, 2018
266
update = vc_dispmanx_update_start(0);
Dec 5, 2017
Dec 5, 2017
267
268
269
270
271
272
273
274
if (!update) {
return 0;
}
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = curdata->w << 16;
src_rect.height = curdata->h << 16;
Nov 27, 2018
Nov 27, 2018
275
276
dst_rect.x = x - curdata->hot_x;
dst_rect.y = y - curdata->hot_y;
Dec 5, 2017
Dec 5, 2017
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
dst_rect.width = curdata->w;
dst_rect.height = curdata->h;
ret = vc_dispmanx_element_change_attributes(
update,
curdata->element,
0,
0,
0,
&dst_rect,
&src_rect,
DISPMANX_NO_HANDLE,
DISPMANX_NO_ROTATE);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_element_change_attributes() failed");
}
/* Submit asynchronously, otherwise the peformance suffers a lot */
ret = vc_dispmanx_update_submit(update, 0, NULL);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_update_submit() failed");
}
return 0;
}
/* Warp the mouse to (x,y) */
static int
RPI_WarpMouseGlobalGraphicOnly(int x, int y)
{
RPI_CursorData *curdata;
DISPMANX_UPDATE_HANDLE_T update;
int ret;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
SDL_Mouse *mouse = SDL_GetMouse();
if (mouse == NULL || mouse->cur_cursor == NULL || mouse->cur_cursor->driverdata == NULL) {
return 0;
}
Oct 13, 2016
Oct 13, 2016
317
318
319
320
curdata = (RPI_CursorData *) mouse->cur_cursor->driverdata;
if (curdata->element == DISPMANX_NO_HANDLE) {
return 0;
}
Oct 1, 2016
Oct 1, 2016
321
Nov 27, 2018
Nov 27, 2018
322
update = vc_dispmanx_update_start(0);
Oct 13, 2016
Oct 13, 2016
323
324
325
326
327
328
329
330
if (!update) {
return 0;
}
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = curdata->w << 16;
src_rect.height = curdata->h << 16;
Nov 27, 2018
Nov 27, 2018
331
332
dst_rect.x = x - curdata->hot_x;
dst_rect.y = y - curdata->hot_y;
Oct 13, 2016
Oct 13, 2016
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
dst_rect.width = curdata->w;
dst_rect.height = curdata->h;
ret = vc_dispmanx_element_change_attributes(
update,
curdata->element,
0,
0,
0,
&dst_rect,
&src_rect,
DISPMANX_NO_HANDLE,
DISPMANX_NO_ROTATE);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_element_change_attributes() failed");
}
/* Submit asynchronously, otherwise the peformance suffers a lot */
ret = vc_dispmanx_update_submit(update, 0, NULL);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_update_submit() failed");
}
Oct 1, 2016
Oct 1, 2016
355
return 0;
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
}
void
RPI_InitMouse(_THIS)
{
/* FIXME: Using UDEV it should be possible to scan all mice
* but there's no point in doing so as there's no multimice support...yet!
*/
SDL_Mouse *mouse = SDL_GetMouse();
mouse->CreateCursor = RPI_CreateCursor;
mouse->ShowCursor = RPI_ShowCursor;
mouse->MoveCursor = RPI_MoveCursor;
mouse->FreeCursor = RPI_FreeCursor;
mouse->WarpMouse = RPI_WarpMouse;
mouse->WarpMouseGlobal = RPI_WarpMouseGlobal;
SDL_SetDefaultCursor(RPI_CreateDefaultCursor());
}
void
RPI_QuitMouse(_THIS)
{
}
/* This is called when a mouse motion event occurs */
static void
RPI_MoveCursor(SDL_Cursor * cursor)
{
SDL_Mouse *mouse = SDL_GetMouse();
Dec 5, 2017
Dec 5, 2017
386
387
388
/* We must NOT call SDL_SendMouseMotion() on the next call or we will enter recursivity,
* so we create a version of WarpMouseGlobal without it. */
RPI_WarpMouseGlobalGraphicOnly(mouse->x, mouse->y);
389
390
391
392
393
}
#endif /* SDL_VIDEO_DRIVER_RPI */
/* vi: set ts=4 sw=4 expandtab: */