# SDL\_PollEvent() and GetRelativeMouseState()

**URL:** <https://discourse.libsdl.org/t/sdl-pollevent-and-getrelativemousestate/15160>\
**Category:** SDL Development\
**Created:** [February 25, 2008, 6:31pm UTC](https://discourse.libsdl.org/t/sdl-pollevent-and-getrelativemousestate/15160 "2008-02-25T18:31:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_Thibault](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@Samuel\_Thibault](https://discourse.libsdl.org/u/Samuel_Thibault)\
**Post date:** [February 25, 2008, 6:31pm UTC](https://discourse.libsdl.org/t/sdl-pollevent-and-getrelativemousestate/15160/1 "2008-02-25T18:31:56Z")

</div>

Hello,

Qemu uses SDL roughly this way:

while(!quit) {  
select(a lot of fds, 30ms timeout);  
while (SDL\_PollEvent(&ev)) {  
switch(ev.type) {  
…  
case SDL\_MOUSEMOTION:  
int dx, dy, state;  
state = SDL\_GetRelativeMouseState(&dx, &dy);  
send\_mouse\_event(dx, dy, state);  
}  
}  
}

And in my case, SDL is using X11, and hence what happens is:

select();  
SDL\_PollEvent()  
-\> X11\_PumpEvents()  
while(X11\_Pending(SDL\_Display))  
X11\_DispatchEvent()  
-\> SDL\_PrivateMouseMotion()  
SDL\_DeltaX += Xrel;  
X11\_DispatchEvent()  
-\> SDL\_PrivateMouseMotion()  
SDL\_DeltaX += Xrel;  
X11\_DispatchEvent()  
-\> SDL\_PrivateMouseMotion()  
SDL\_DeltaX += Xrel;  
SDL\_PeepEvent()  
send\_mouse\_event(dx, dy, state);  
SDL\_PollEvent()  
-\> X11\_PumpEvents()  
while(X11\_Pending(SDL\_Display)), but nothing left  
SDL\_PeepEvent()  
SDL\_GetRelativeMouseState();  
send\_mouse\_event(0, 0, state);  
SDL\_PollEvent()  
-\> X11\_PumpEvents()  
while(X11\_Pending(SDL\_Display)), but nothing left  
SDL\_PeepEvent()  
SDL\_GetRelativeMouseState();  
send\_mouse\_event(0, 0, state);  
and back to select() again.

I.e. since select() has slept 30ms, 3 X11 mouse motion events were  
pending and they are all processed in one go on the first call to  
SDL\_PollEvent(). In response to the first SDL event, the main loop hence  
sends a mouse event which is the combination of the three X11 events,  
and in response to the other SDL events, the main loop just sends  
no-move events. The result is that the mouse doesn’t move so smoothly.

Could this be considered as a bug of SDL, or should qemu be fixed to  
just use the values from the events instead of calling  
SDL\_GetRelativeMouseState?

Samuel  
PS: There is also a missing feature in the events: the button events  
don’t provide the current button status, so that you have to record it.

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [February 26, 2008, 12:17pm UTC](https://discourse.libsdl.org/t/sdl-pollevent-and-getrelativemousestate/15160/2 "2008-02-26T12:17:34Z")

</div>

> Could this be considered as a bug of SDL, or should qemu be fixed to  
> just use the values from the events instead of calling  
> SDL\_GetRelativeMouseState?

Yes, SDL\_GetRelativeMouseState() only returns the current state for the  
frame. If you want each event that gets you there, you should process  
the events.

> PS: There is also a missing feature in the events: the button events  
> don’t provide the current button status, so that you have to record it.

You can’t query it with SDL\_GetMouseState()?

We can certainly add it to the event, for SDL 1.3 if you want.  
[http://bugzilla.libsdl.org/](http://bugzilla.libsdl.org/)

See ya,  
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

---

<div class="post-metadata">

**Author:** ![Samuel\_Thibault](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/a698b9/32.png) [@Samuel\_Thibault](https://discourse.libsdl.org/u/Samuel_Thibault)\
**Post date:** [February 26, 2008, 12:30pm UTC](https://discourse.libsdl.org/t/sdl-pollevent-and-getrelativemousestate/15160/3 "2008-02-26T12:30:02Z")

</div>

Sam Lantinga, le Tue 26 Feb 2008 04:17:34 -0800, a ?crit :

> > Could this be considered as a bug of SDL, or should qemu be fixed to  
> > just use the values from the events instead of calling  
> > SDL\_GetRelativeMouseState?
> 
> Yes, SDL\_GetRelativeMouseState() only returns the current state for the  
> frame. If you want each event that gets you there, you should process  
> the events.

Ok.

> > PS: There is also a missing feature in the events: the button events  
> > don’t provide the current button status, so that you have to record it.
> 
> You can’t query it with SDL\_GetMouseState()?

Yes, but again, that will give me the current button status, not the  
button status corresponding to the event.

> We can certainly add it to the event, for SDL 1.3 if you want.  
> [http://bugzilla.libsdl.org/](http://bugzilla.libsdl.org/)

Samuel
