SDL_rpimouse.c
Go to the documentation of this file.
/*
Simple DirectMedia Layer
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
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"
#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);
static int RPI_WarpMouseGlobal(int x, int y);
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));
curdata = (RPI_CursorData *) SDL_calloc(1, sizeof(*curdata));
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 */
curdata->resource = vc_dispmanx_resource_create( VC_IMAGE_ARGB8888, surface->w | (surface->pitch << 16), surface->h | (surface->h << 16), &dummy );
SDL_assert(curdata->resource);
vc_dispmanx_rect_set( &dst_rect, 0, 0, curdata->w, curdata->h);
/* 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.
*/
ret = vc_dispmanx_resource_write_data( curdata->resource, VC_IMAGE_ARGB8888, surface->pitch, surface->pixels, &dst_rect );
SDL_assert ( ret == DISPMANX_SUCCESS );
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 */ };
mouse = SDL_GetMouse();
if (mouse == NULL) {
return -1;
}
if (cursor == NULL) {
/* FIXME: We hide the current mouse's cursor, what we actually need is *_HideCursor */
if ( mouse->cur_cursor != NULL && mouse->cur_cursor->driverdata != NULL) {
curdata = (RPI_CursorData *) mouse->cur_cursor->driverdata;
if (curdata->element > DISPMANX_NO_HANDLE) {
update = vc_dispmanx_update_start( 10 );
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;
}
}
return 0;
}
curdata = (RPI_CursorData *) cursor->driverdata;
if (curdata == NULL) {
return -1;
}
if (mouse->focus == NULL) {
return -1;
}
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) {
vc_dispmanx_rect_set( &src_rect, 0, 0, curdata->w << 16, curdata->h << 16 );
vc_dispmanx_rect_set( &dst_rect, 0, 0, curdata->w, curdata->h);
update = vc_dispmanx_update_start( 10 );
SDL_assert( update );
curdata->element = vc_dispmanx_element_add( update,
data->dispman_display,
SDL_RPI_MOUSELAYER, // layer
&dst_rect,
curdata->resource,
&src_rect,
DISPMANX_PROTECTION_NONE,
&alpha,
DISPMANX_NO_HANDLE, // clamp
VC_IMAGE_ROT0 );
SDL_assert( curdata->element > DISPMANX_NO_HANDLE);
ret = vc_dispmanx_update_submit_sync( update );
SDL_assert( ret == DISPMANX_SUCCESS );
}
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) {
update = vc_dispmanx_update_start( 10 );
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 );
}
if (curdata->resource != DISPMANX_NO_HANDLE) {
ret = vc_dispmanx_resource_delete( curdata->resource );
SDL_assert( ret == DISPMANX_SUCCESS );
}
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) */
static int
RPI_WarpMouseGlobal(int x, int y)
{
RPI_CursorData *curdata;
DISPMANX_UPDATE_HANDLE_T update;
VC_RECT_T dst_rect;
SDL_Mouse *mouse = SDL_GetMouse();
if (mouse != NULL && mouse->cur_cursor != NULL && mouse->cur_cursor->driverdata != NULL) {
curdata = (RPI_CursorData *) mouse->cur_cursor->driverdata;
if (curdata->element != DISPMANX_NO_HANDLE) {
int ret;
update = vc_dispmanx_update_start( 10 );
SDL_assert( update );
vc_dispmanx_rect_set( &dst_rect, x, y, curdata->w, curdata->h);
ret = vc_dispmanx_element_change_attributes(
update,
curdata->element,
ELEMENT_CHANGE_DEST_RECT,
0,
0,
&dst_rect,
NULL,
DISPMANX_NO_HANDLE,
DISPMANX_NO_ROTATE);
SDL_assert( ret == DISPMANX_SUCCESS );
/* Submit asynchronously, otherwise the peformance suffers a lot */
ret = vc_dispmanx_update_submit( update, 0, NULL );
SDL_assert( ret == DISPMANX_SUCCESS );
return (ret == DISPMANX_SUCCESS) ? 0 : -1;
}
}
return -1; /* !!! FIXME: this should SDL_SetError() somewhere. */
}
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();
RPI_WarpMouse(mouse->focus, mouse->x, mouse->y);
}
#endif /* SDL_VIDEO_DRIVER_RPI */
/* vi: set ts=4 sw=4 expandtab: */
SDL_PIXELFORMAT_ARGB8888
Definition: SDL_pixels.h:247
SDL_Mouse::y
int y
Definition: SDL_mouse_c.h:79
DEFAULT_CWIDTH
#define DEFAULT_CWIDTH
Definition: default_cursor.h:25
SDL_GetMouse
SDL_Mouse * SDL_GetMouse(void)
Definition: SDL_mouse.c:66
SDL_Mouse::ShowCursor
int(* ShowCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:52
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
SDL_Surface::h
int h
Definition: SDL_surface.h:73
SDL_RPI_MOUSELAYER
#define SDL_RPI_MOUSELAYER
Definition: SDL_rpivideo.h:54
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_Mouse::focus
SDL_Window * focus
Definition: SDL_mouse_c.h:77
window
SDL_Window * window
Definition: testautomation_render.c:26
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
SDL_Cursor
Definition: SDL_mouse_c.h:30
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
DEFAULT_CHOTY
#define DEFAULT_CHOTY
Definition: default_cursor.h:28
SDL_Surface::w
int w
Definition: SDL_surface.h:73
DEFAULT_CHOTX
#define DEFAULT_CHOTX
Definition: default_cursor.h:27
SDL_DisplayData
Definition: SDL_cocoamodes.h:26
alpha
GLfloat GLfloat GLfloat alpha
Definition: SDL_opengl_glext.h:412
SDL_surface.h
SDL_Mouse::CreateCursor
SDL_Cursor *(* CreateCursor)(SDL_Surface *surface, int hot_x, int hot_y)
Definition: SDL_mouse_c.h:46
SDL_calloc
void * SDL_calloc(size_t nmemb, size_t size)
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:75
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_free
void SDL_free(void *mem)
SDL_DisplayData::dispman_display
DISPMANX_DISPLAY_HANDLE_T dispman_display
Definition: SDL_rpivideo.h:41
DEFAULT_CHEIGHT
#define DEFAULT_CHEIGHT
Definition: default_cursor.h:26
cursor
SDL_Cursor * cursor
Definition: testwm2.c:40
SDL_SetDefaultCursor
void SDL_SetDefaultCursor(SDL_Cursor *cursor)
Definition: SDL_mouse.c:55
SDL_Mouse::WarpMouseGlobal
int(* WarpMouseGlobal)(int x, int y)
Definition: SDL_mouse_c.h:64
SDL_VideoDisplay
Definition: SDL_sysvideo.h:120
SDL_Mouse::x
int x
Definition: SDL_mouse_c.h:78
SDL_Mouse::FreeCursor
void(* FreeCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:58
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:167
NULL
#define NULL
Definition: begin_code.h:143
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:72
default_cmask
static const unsigned char default_cmask[]
Definition: default_cursor.h:54
SDL_CreateCursor
#define SDL_CreateCursor
Definition: SDL_dynapi_overrides.h:257
SDL_GetDisplayForWindow
SDL_VideoDisplay * SDL_GetDisplayForWindow(SDL_Window *window)
Definition: SDL_video.c:1039
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:71
RPI_QuitMouse
void RPI_QuitMouse(_THIS)
SDL_Mouse::WarpMouse
void(* WarpMouse)(SDL_Window *window, int x, int y)
Definition: SDL_mouse_c.h:61
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:74
SDL_Mouse
Definition: SDL_mouse_c.h:43
SDL_PixelFormat::format
Uint32 format
Definition: SDL_pixels.h:301
default_cdata
static const unsigned char default_cdata[]
Definition: default_cursor.h:35
SDL_Mouse::cur_cursor
SDL_Cursor * cur_cursor
Definition: SDL_mouse_c.h:93
SDL_Mouse::MoveCursor
void(* MoveCursor)(SDL_Cursor *cursor)
Definition: SDL_mouse_c.h:55
SDL_rpimouse.h
SDL_Cursor::driverdata
void * driverdata
Definition: SDL_mouse_c.h:33
RPI_InitMouse
void RPI_InitMouse(_THIS)
SDL_rpivideo.h
SDL_VideoDisplay::driverdata
void * driverdata
Definition: SDL_sysvideo.h:133
SDL_assert.h