Add hint SDL_HINT_MOUSE_TOUCH_EVENTS for mouse events to generate touch events

controlling whether mouse events should generate synthetic touch events
By default SDL will *not* generate touch events for mouse events
This commit is contained in:
Sylvain Becker 2019-04-04 16:51:50 +02:00
parent ab03892ddf
commit e41576188d
5 changed files with 91 additions and 22 deletions

View File

@ -315,6 +315,18 @@ extern "C" {
*/ */
#define SDL_HINT_TOUCH_MOUSE_EVENTS "SDL_TOUCH_MOUSE_EVENTS" #define SDL_HINT_TOUCH_MOUSE_EVENTS "SDL_TOUCH_MOUSE_EVENTS"
/**
* \brief A variable controlling whether mouse events should generate synthetic touch events
*
* This variable can be set to the following values:
* "0" - Mouse events will not generate touch events
* "1" - Mouse events will generate touch events
*
* By default SDL will *not* generate touch events for mouse events
*/
#define SDL_HINT_MOUSE_TOUCH_EVENTS "SDL_MOUSE_TOUCH_EVENTS"
/** /**
* \brief Minimize your SDL_Window if it loses key focus when in fullscreen mode. Defaults to true. * \brief Minimize your SDL_Window if it loses key focus when in fullscreen mode. Defaults to true.
* *

View File

@ -60,6 +60,9 @@ typedef struct SDL_Finger
/* Used as the device ID for mouse events simulated with touch input */ /* Used as the device ID for mouse events simulated with touch input */
#define SDL_TOUCH_MOUSEID ((Uint32)-1) #define SDL_TOUCH_MOUSEID ((Uint32)-1)
/* Used as the SDL_TouchID for touch events simulated with mouse input */
#define SDL_MOUSE_TOUCHID ((Sint64)-1)
/* Function prototypes */ /* Function prototypes */

View File

@ -37,6 +37,9 @@
/* The mouse state */ /* The mouse state */
static SDL_Mouse SDL_mouse; static SDL_Mouse SDL_mouse;
/* for mapping mouse events to touch */
static SDL_bool track_mouse_down = SDL_FALSE;
static int static int
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y); SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
@ -104,6 +107,21 @@ SDL_TouchMouseEventsChanged(void *userdata, const char *name, const char *oldVal
} }
} }
static void SDLCALL
SDL_MouseTouchEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_Mouse *mouse = (SDL_Mouse *)userdata;
if (hint && (*hint == '1' || SDL_strcasecmp(hint, "true") == 0)) {
SDL_AddTouch(SDL_MOUSE_TOUCHID, SDL_TOUCH_DEVICE_DIRECT, "mouse_input");
mouse->mouse_touch_events = SDL_TRUE;
} else {
mouse->mouse_touch_events = SDL_FALSE;
}
}
/* Public functions */ /* Public functions */
int int
SDL_MouseInit(void) SDL_MouseInit(void)
@ -127,6 +145,9 @@ SDL_MouseInit(void)
SDL_AddHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS, SDL_AddHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS,
SDL_TouchMouseEventsChanged, mouse); SDL_TouchMouseEventsChanged, mouse);
SDL_AddHintCallback(SDL_HINT_MOUSE_TOUCH_EVENTS,
SDL_MouseTouchEventsChanged, mouse);
mouse->cursor_shown = SDL_TRUE; mouse->cursor_shown = SDL_TRUE;
return (0); return (0);
@ -298,6 +319,17 @@ SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relativ
int xrel; int xrel;
int yrel; int yrel;
/* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
if (mouse->mouse_touch_events) {
if (mouseID != SDL_TOUCH_MOUSEID && !relative && track_mouse_down) {
if (window) {
float fx = (float)x / (float)window->w;
float fy = (float)y / (float)window->h;
SDL_SendTouchMotion(SDL_MOUSE_TOUCHID, 0, fx, fy, 1.0f);
}
}
}
if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) { if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) {
int center_x = 0, center_y = 0; int center_x = 0, center_y = 0;
SDL_GetWindowSize(window, &center_x, &center_y); SDL_GetWindowSize(window, &center_x, &center_y);
@ -443,6 +475,22 @@ SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state
Uint32 type; Uint32 type;
Uint32 buttonstate = mouse->buttonstate; Uint32 buttonstate = mouse->buttonstate;
/* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
if (mouse->mouse_touch_events) {
if (mouseID != SDL_TOUCH_MOUSEID && button == SDL_BUTTON_LEFT) {
if (window) {
float fx = (float)mouse->x / (float)window->w;
float fy = (float)mouse->y / (float)window->h;
if (state == SDL_PRESSED) {
track_mouse_down = SDL_TRUE;
} else {
track_mouse_down = SDL_FALSE;
}
SDL_SendTouch(SDL_MOUSE_TOUCHID, 0, track_mouse_down, fx, fy, 1.0f);
}
}
}
/* Figure out which event to perform */ /* Figure out which event to perform */
switch (state) { switch (state) {
case SDL_PRESSED: case SDL_PRESSED:

View File

@ -93,6 +93,7 @@ typedef struct
Uint32 double_click_time; Uint32 double_click_time;
int double_click_radius; int double_click_radius;
SDL_bool touch_mouse_events; SDL_bool touch_mouse_events;
SDL_bool mouse_touch_events;
/* Data for double-click tracking */ /* Data for double-click tracking */
int num_clickstates; int num_clickstates;

View File

@ -249,6 +249,8 @@ SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
{ {
SDL_Mouse *mouse = SDL_GetMouse(); SDL_Mouse *mouse = SDL_GetMouse();
if (mouse->touch_mouse_events) { if (mouse->touch_mouse_events) {
/* FIXME: maybe we should only restrict to a few SDL_TouchDeviceType */
if (id != SDL_MOUSE_TOUCHID) {
SDL_Window *window = SDL_GetMouseFocus(); SDL_Window *window = SDL_GetMouseFocus();
if (window) { if (window) {
if (down) { if (down) {
@ -270,6 +272,7 @@ SDL_SendTouch(SDL_TouchID id, SDL_FingerID fingerid,
} }
} }
} }
}
finger = SDL_GetFinger(touch, fingerid); finger = SDL_GetFinger(touch, fingerid);
if (down) { if (down) {
@ -339,6 +342,7 @@ SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
{ {
SDL_Mouse *mouse = SDL_GetMouse(); SDL_Mouse *mouse = SDL_GetMouse();
if (mouse->touch_mouse_events) { if (mouse->touch_mouse_events) {
if (id != SDL_MOUSE_TOUCHID) {
SDL_Window *window = SDL_GetMouseFocus(); SDL_Window *window = SDL_GetMouseFocus();
if (window) { if (window) {
if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) { if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) {
@ -349,6 +353,7 @@ SDL_SendTouchMotion(SDL_TouchID id, SDL_FingerID fingerid,
} }
} }
} }
}
finger = SDL_GetFinger(touch,fingerid); finger = SDL_GetFinger(touch,fingerid);
if (!finger) { if (!finger) {