Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine sequential mouse movement events #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/gui/filebox.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,10 @@ int filebox(const char *title, int mode, char *buffer, size_t buffer_size, const

if (e.type != SDL_MOUSEMOTION || (e.motion.state)) ++got_event;

// ensure the last event is a mouse click so it gets passed to the draw/event code

if (e.type == SDL_MOUSEBUTTONDOWN || (e.type == SDL_MOUSEMOTION && e.motion.state)) break;
// Process mouse click events immediately, and batch mouse motion events
// (process the last one) to fix lag with high poll rate mice on Linux.
if (should_process_mouse(&e))
break;
}

if (data.selected)
Expand Down
21 changes: 21 additions & 0 deletions src/gui/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,24 @@ int check_drag_event(const SDL_Event *event, const SDL_Rect *rect, void (*action
}


int should_process_mouse(const SDL_Event *event)
{
// If current event is a mouse click, break immediately to pass the event to the
// draw/event code.
if (event->type == SDL_MOUSEBUTTONDOWN)
return 1;

if (event->type == SDL_MOUSEMOTION)
{
// If the next event is not a mouse movement event with the same buttons held,
// process current mouse movement event immediately.
SDL_Event next_event;
if (SDL_PeepEvents(&next_event, 1, SDL_PEEKEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) <= 0)
return 1;
if (next_event.motion.state != event->motion.state)
return 1;
}
return 0;
}


3 changes: 3 additions & 0 deletions src/gui/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ void set_motion_target(void (*action)(int,int,void*), void *param);
int check_event(const SDL_Event *event, const SDL_Rect *rect, void (*action)(void*,void*,void*), void *param1, void *param2, void *param3);
int check_drag_event(const SDL_Event *event, const SDL_Rect *rect, void (*action)(int,int,void*), void *param);
void set_repeat_timer(const SDL_Event *event);

// Whether to immediately break the input polling loop and process the current mouse event.
int should_process_mouse(const SDL_Event *event);
7 changes: 4 additions & 3 deletions src/gui/msgbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ int msgbox(GfxDomain *domain, GfxSurface *gfx, const Font *font, const char *msg

if (e.type != SDL_MOUSEMOTION || (e.motion.state)) ++got_event;

// ensure the last event is a mouse click so it gets passed to the draw/event code

if (e.type == SDL_MOUSEBUTTONDOWN || (e.type == SDL_MOUSEMOTION && e.motion.state)) break;
// Process mouse click events immediately, and batch mouse motion events
// (process the last one) to fix lag with high poll rate mice on Linux.
if (should_process_mouse(&e))
break;
}

if (got_event || gfx_domain_is_next_frame(domain))
Expand Down