Fixed bug 2293 - Precise scrolling events

Martijn Courteaux

I implemented precise scrolling events. I have been through all the folders in /src/video/[platform] to implement where possible. This works on OS X, but I can't speak for others. Build farm will figure that out, I guess. I think this patch should introduce precise scrolling on OS X, Wayland, Mir, Windows, Android, Nacl, Windows RT.

The way I provide precise scrolling events is by adding two float fields to the SDL_MouseWheelScrollEvent datastructure, called "preciseX" and "preciseY". The old integer fields "x" and "y" are still present. The idea is that every platform specific code normalises the scroll amounts and forwards them to the SDL_SendMouseWheel function. It is this function that will now accumulate these (using a static variable, as I have seen how it was implemented in the Windows specific code) and once we hit a unit size, set the traditional integer "x" and "y" fields.

I believe this is pretty solid way of doing it, although I'm not the expert here.

There is also a fix in the patch for a typo recently introduced, that might need to be taken away by the time anybody merges this in. There is also a file in Nacl which I have stripped a horrible amount of trailing whitespaces. (Leave that part out if you want).
This commit is contained in:
Sam Lantinga 2017-08-14 21:28:04 -07:00
parent 72b195d27c
commit a4cfa93670
10 changed files with 80 additions and 90 deletions

View File

@ -508,10 +508,11 @@ SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8
}
int
SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction)
SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
{
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
int integral_x, integral_y;
if (window) {
SDL_SetMouseFocus(window);
@ -521,6 +522,26 @@ SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_M
return 0;
}
mouse->accumulated_wheel_x += x;
if (mouse->accumulated_wheel_x > 0) {
integral_x = (int)SDL_floor(mouse->accumulated_wheel_x);
} else if (mouse->accumulated_wheel_x < 0) {
integral_x = (int)SDL_ceil(mouse->accumulated_wheel_x);
} else {
integral_x = 0;
}
mouse->accumulated_wheel_x -= integral_x;
mouse->accumulated_wheel_y += y;
if (mouse->accumulated_wheel_y > 0) {
integral_y = (int)SDL_floor(mouse->accumulated_wheel_y);
} else if (mouse->accumulated_wheel_y < 0) {
integral_y = (int)SDL_ceil(mouse->accumulated_wheel_y);
} else {
integral_y = 0;
}
mouse->accumulated_wheel_y -= integral_y;
/* Post the event, if desired */
posted = 0;
if (SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE) {
@ -528,8 +549,12 @@ SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_M
event.type = SDL_MOUSEWHEEL;
event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
event.wheel.which = mouseID;
event.wheel.x = x;
event.wheel.y = y;
#if 0 /* Uncomment this when it goes in for SDL 2.1 */
event.wheel.preciseX = x;
event.wheel.preciseY = y;
#endif
event.wheel.x = integral_x;
event.wheel.y = integral_y;
event.wheel.direction = (Uint32)direction;
posted = (SDL_PushEvent(&event) > 0);
}

View File

@ -80,6 +80,8 @@ typedef struct
int xdelta;
int ydelta;
int last_x, last_y; /* the last reported x and y coordinates */
float accumulated_wheel_x;
float accumulated_wheel_y;
Uint32 buttonstate;
SDL_bool has_position;
SDL_bool relative_mode;
@ -129,7 +131,7 @@ extern int SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 s
extern int SDL_SendMouseButtonClicks(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks);
/* Send a mouse wheel event */
extern int SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y, SDL_MouseWheelDirection direction);
extern int SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction);
/* Shutdown the mouse subsystem */
extern void SDL_MouseQuit(void);

View File

@ -432,17 +432,7 @@ Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
}
}
if (x > 0) {
x = SDL_ceil(x);
} else if (x < 0) {
x = SDL_floor(x);
}
if (y > 0) {
y = SDL_ceil(y);
} else if (y < 0) {
y = SDL_floor(y);
}
SDL_SendMouseWheel(window, mouse->mouseID, (int)x, (int)y, direction);
SDL_SendMouseWheel(window, mouse->mouseID, x, y, direction);
}
void

View File

@ -400,7 +400,7 @@ static EM_BOOL
Emscripten_HandleWheel(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData)
{
SDL_WindowData *window_data = userData;
SDL_SendMouseWheel(window_data->window, 0, wheelEvent->deltaX, -wheelEvent->deltaY, SDL_MOUSEWHEEL_NORMAL);
SDL_SendMouseWheel(window_data->window, 0, (float)wheelEvent->deltaX, (float)-wheelEvent->deltaY, SDL_MOUSEWHEEL_NORMAL);
return SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE;
}

View File

@ -141,7 +141,7 @@ HandleTouchMotion(int device_id, int source_id, float x, float y, float pressure
}
static void
HandleMouseScroll(SDL_Window* sdl_window, int hscroll, int vscroll)
HandleMouseScroll(SDL_Window* sdl_window, float hscroll, float vscroll)
{
SDL_SendMouseWheel(sdl_window, 0, hscroll, vscroll, SDL_MOUSEWHEEL_NORMAL);
}
@ -205,7 +205,7 @@ HandleMouseEvent(MirPointerEvent const* pointer, SDL_Window* sdl_window)
break;
case mir_pointer_action_motion: {
int x, y;
int hscroll, vscroll;
float hscroll, vscroll;
SDL_Mouse* mouse = SDL_GetMouse();
x = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_x);
y = MIR_mir_pointer_event_axis_value(pointer, mir_pointer_axis_y);

View File

@ -298,7 +298,7 @@ static Uint8 SDL_NACL_translate_mouse_button(int32_t button) {
return SDL_BUTTON_MIDDLE;
case PP_INPUTEVENT_MOUSEBUTTON_RIGHT:
return SDL_BUTTON_RIGHT;
case PP_INPUTEVENT_MOUSEBUTTON_NONE:
default:
return 0;
@ -321,7 +321,7 @@ SDL_NACL_translate_keycode(int keycode)
void NACL_PumpEvents(_THIS) {
PSEvent* ps_event;
PP_Resource event;
PP_Resource event;
PP_InputEvent_Type type;
PP_InputEvent_Modifier modifiers;
struct PP_Rect rect;
@ -333,7 +333,7 @@ void NACL_PumpEvents(_THIS) {
Uint32 str_len;
SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
SDL_Mouse *mouse = SDL_GetMouse();
if (driverdata->window) {
while ((ps_event = PSEventTryAcquire()) != NULL) {
event = ps_event->as_resource;
@ -344,9 +344,9 @@ void NACL_PumpEvents(_THIS) {
NACL_SetScreenResolution(rect.size.width, rect.size.height, SDL_PIXELFORMAT_UNKNOWN);
// FIXME: Rebuild context? See life.c UpdateContext
break;
/* From HandleInputEvent, contains an input resource. */
case PSE_INSTANCE_HANDLEINPUT:
case PSE_INSTANCE_HANDLEINPUT:
type = driverdata->ppb_input_event->GetType(event);
modifiers = driverdata->ppb_input_event->GetModifiers(event);
switch(type) {
@ -359,35 +359,35 @@ void NACL_PumpEvents(_THIS) {
case PP_INPUTEVENT_TYPE_WHEEL:
/* FIXME: GetTicks provides high resolution scroll events */
fp = driverdata->ppb_wheel_input_event->GetDelta(event);
SDL_SendMouseWheel(mouse->focus, mouse->mouseID, (int) fp.x, (int) fp.y, SDL_MOUSEWHEEL_NORMAL);
SDL_SendMouseWheel(mouse->focus, mouse->mouseID, fp.x, fp.y, SDL_MOUSEWHEEL_NORMAL);
break;
case PP_INPUTEVENT_TYPE_MOUSEENTER:
case PP_INPUTEVENT_TYPE_MOUSELEAVE:
/* FIXME: Mouse Focus */
break;
case PP_INPUTEVENT_TYPE_MOUSEMOVE:
case PP_INPUTEVENT_TYPE_MOUSEMOVE:
location = driverdata->ppb_mouse_input_event->GetPosition(event);
SDL_SendMouseMotion(mouse->focus, mouse->mouseID, SDL_FALSE, location.x, location.y);
break;
case PP_INPUTEVENT_TYPE_TOUCHSTART:
case PP_INPUTEVENT_TYPE_TOUCHMOVE:
case PP_INPUTEVENT_TYPE_TOUCHEND:
case PP_INPUTEVENT_TYPE_TOUCHCANCEL:
/* FIXME: Touch events */
break;
case PP_INPUTEVENT_TYPE_KEYDOWN:
SDL_SendKeyboardKey(SDL_PRESSED, SDL_NACL_translate_keycode(driverdata->ppb_keyboard_input_event->GetKeyCode(event)));
break;
case PP_INPUTEVENT_TYPE_KEYUP:
SDL_SendKeyboardKey(SDL_RELEASED, SDL_NACL_translate_keycode(driverdata->ppb_keyboard_input_event->GetKeyCode(event)));
break;
case PP_INPUTEVENT_TYPE_CHAR:
var = driverdata->ppb_keyboard_input_event->GetCharacterText(event);
str = driverdata->ppb_var->VarToUtf8(var, &str_len);
@ -397,17 +397,17 @@ void NACL_PumpEvents(_THIS) {
}
SDL_strlcpy(text, str, str_len );
text[str_len] = '\0';
SDL_SendKeyboardText(text);
/* FIXME: Do we have to handle ref counting? driverdata->ppb_var->Release(var);*/
break;
default:
break;
}
break;
/* From HandleMessage, contains a PP_Var. */
case PSE_INSTANCE_HANDLEMESSAGE:
break;
@ -419,7 +419,7 @@ void NACL_PumpEvents(_THIS) {
/* When the 3D context is lost, no resource. */
case PSE_GRAPHICS3D_GRAPHICS3DCONTEXTLOST:
break;
/* When the mouse lock is lost. */
case PSE_MOUSELOCK_MOUSELOCKLOST:
break;
@ -427,7 +427,7 @@ void NACL_PumpEvents(_THIS) {
default:
break;
}
PSEventRelease(ps_event);
}
}

View File

@ -97,7 +97,7 @@ pointer_handle_enter(void *data, struct wl_pointer *pointer,
/* enter event for a window we've just destroyed */
return;
}
/* This handler will be called twice in Wayland 1.4
* Once for the window surface which has valid user data
* and again for the mouse cursor surface which does not have valid user data
@ -105,7 +105,7 @@ pointer_handle_enter(void *data, struct wl_pointer *pointer,
*/
window = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window) {
input->pointer_focus = window;
SDL_SetMouseFocus(window->sdlwindow);
@ -184,7 +184,7 @@ pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_button_state state = state_w;
uint32_t sdl_button;
if (input->pointer_focus) {
switch (button) {
case BTN_LEFT:
@ -231,16 +231,16 @@ pointer_handle_axis_common(struct SDL_WaylandInput *input,
{
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_axis a = axis;
int x, y;
float x, y;
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
x = 0;
y = wl_fixed_to_int(value);
y = wl_fixed_to_float(value);
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
x = wl_fixed_to_int(value);
x = wl_fixed_to_float(value);
y = 0;
break;
default:
@ -324,7 +324,7 @@ keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
/* enter event for a window we've just destroyed */
return;
}
window = wl_surface_get_user_data(surface);
if (window) {

View File

@ -440,11 +440,11 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
if (SDL_GetKeyboardFocus() != data->window) {
SDL_SetKeyboardFocus(data->window);
}
GetCursorPos(&cursorPos);
ScreenToClient(hwnd, &cursorPos);
SDL_SendMouseMotion(data->window, 0, 0, cursorPos.x, cursorPos.y);
WIN_CheckAsyncMouseRelease(data);
/*
@ -566,40 +566,14 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
break;
case WM_MOUSEWHEEL:
{
static short s_AccumulatedMotion;
s_AccumulatedMotion += GET_WHEEL_DELTA_WPARAM(wParam);
if (s_AccumulatedMotion > 0) {
while (s_AccumulatedMotion >= WHEEL_DELTA) {
SDL_SendMouseWheel(data->window, 0, 0, 1, SDL_MOUSEWHEEL_NORMAL);
s_AccumulatedMotion -= WHEEL_DELTA;
}
} else {
while (s_AccumulatedMotion <= -WHEEL_DELTA) {
SDL_SendMouseWheel(data->window, 0, 0, -1, SDL_MOUSEWHEEL_NORMAL);
s_AccumulatedMotion += WHEEL_DELTA;
}
}
}
break;
case WM_MOUSEHWHEEL:
{
static short s_AccumulatedMotion;
s_AccumulatedMotion += GET_WHEEL_DELTA_WPARAM(wParam);
if (s_AccumulatedMotion > 0) {
while (s_AccumulatedMotion >= WHEEL_DELTA) {
SDL_SendMouseWheel(data->window, 0, 1, 0, SDL_MOUSEWHEEL_NORMAL);
s_AccumulatedMotion -= WHEEL_DELTA;
}
} else {
while (s_AccumulatedMotion <= -WHEEL_DELTA) {
SDL_SendMouseWheel(data->window, 0, -1, 0, SDL_MOUSEWHEEL_NORMAL);
s_AccumulatedMotion += WHEEL_DELTA;
}
}
short amount = GET_WHEEL_DELTA_WPARAM(wParam);
float fAmount = (float) amount / WHEEL_DELTA;
if (msg == WM_MOUSEWHEEL)
SDL_SendMouseWheel(data->window, 0, 0.0f, fAmount, SDL_MOUSEWHEEL_NORMAL);
else
SDL_SendMouseWheel(data->window, 0, fAmount, 0.0f, SDL_MOUSEWHEEL_NORMAL);
}
break;
@ -636,7 +610,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
SDL_SendKeyboardKey(SDL_PRESSED, code);
}
}
returnCode = 0;
break;
@ -793,7 +767,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
RECT rect;
int x, y;
int w, h;
if (data->initializing || data->in_border_change) {
break;
}

View File

@ -295,7 +295,7 @@ void WINRT_ProcessPointerReleasedEvent(SDL_Window *window, Windows::UI::Input::P
}
WINRT_LeftFingerDown = 0;
}
SDL_SendTouch(
WINRT_TouchID,
(SDL_FingerID) pointerPoint->PointerId,
@ -335,9 +335,8 @@ WINRT_ProcessPointerWheelChangedEvent(SDL_Window *window, Windows::UI::Input::Po
return;
}
// FIXME: This may need to accumulate deltas up to WHEEL_DELTA
short motion = pointerPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
SDL_SendMouseWheel(window, 0, 0, motion, SDL_MOUSEWHEEL_NORMAL);
float motion = (float) pointerPoint->Properties->MouseWheelDelta / WHEEL_DELTA;
SDL_SendMouseWheel(window, 0, 0, (float) motion, SDL_MOUSEWHEEL_NORMAL);
}
void
@ -369,7 +368,7 @@ WINRT_ProcessMouseMovedEvent(SDL_Window * window, Windows::Devices::Input::Mouse
// http://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.input.mouseeventargs.mousedelta ),
// does not seem to indicate (to me) that its values should be so large. It
// says that its values should be a "change in screen location". I could
// be misinterpreting this, however a post on MSDN from a Microsoft engineer (see:
// be misinterpreting this, however a post on MSDN from a Microsoft engineer (see:
// http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/09a9868e-95bb-4858-ba1a-cb4d2c298d62 ),
// indicates that these values are in DIPs, which is the same unit used
// by CoreWindow's PointerMoved events (via the Position field in its CurrentPoint

View File

@ -1005,7 +1005,7 @@ X11_DispatchEvent(_THIS)
printf("Protocol version to use : %d\n", xdnd_version);
printf("More then 3 data types : %d\n", (int) use_list);
#endif
if (use_list) {
/* fetch conversion targets */
SDL_x11Prop p;
@ -1019,7 +1019,7 @@ X11_DispatchEvent(_THIS)
}
}
else if (xevent.xclient.message_type == videodata->XdndPosition) {
#ifdef DEBUG_XEVENTS
Atom act= videodata->XdndActionCopy;
if(xdnd_version >= 2) {
@ -1027,7 +1027,7 @@ X11_DispatchEvent(_THIS)
}
printf("Action requested by user is : %s\n", X11_XGetAtomName(display , act));
#endif
/* reply with status */
memset(&m, 0, sizeof(XClientMessageEvent));
@ -1130,7 +1130,7 @@ X11_DispatchEvent(_THIS)
printf("window %p: ButtonPress (X11 button = %d)\n", data, xevent.xbutton.button);
#endif
if (X11_IsWheelEvent(display,&xevent,&xticks, &yticks)) {
SDL_SendMouseWheel(data->window, 0, xticks, yticks, SDL_MOUSEWHEEL_NORMAL);
SDL_SendMouseWheel(data->window, 0, (float) xticks, (float) yticks, SDL_MOUSEWHEEL_NORMAL);
} else {
SDL_bool ignore_click = SDL_FALSE;
int button = xevent.xbutton.button;