We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 1990

Summary: focus/keyboard events not generated correctly for multiple windows
Product: SDL Reporter: Mai Lavelle <mai.lavelle>
Component: eventsAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: marack
Version: HG 2.1   
Hardware: x86_64   
OS: Linux   
Attachments: Patch to prevent redundant focus out events stealing keyboard focus when using multiple window

Description Mai Lavelle 2013-07-25 16:55:32 UTC
I've recently tried to create multiple windows and process key events for them, and found that key events weren't being generated for most of the windows. After some investigating I've observed the following effects. All but the most recently created window experience these effects...

- a focus lost event is generated immediately after the focus gained event, even tho window still has focus
- key events report window id 0 rather than the id of the window which has focus, SDL thinks no window has focus?
- giving focus to a non SDL window and then selecting an SDL window causes events to be generated as expected, but only until focus changes again

This happens with a build of the rc, and builds from recent and old hg clones. I'm using an updated Ubuntu 12.04 LTS.
Comment 1 Mark Curtis 2013-10-30 05:21:06 UTC
Created attachment 1399 [details]
Patch to prevent redundant focus out events stealing keyboard focus when using multiple window

Focus change events are queued and delayed (200 ticks) before they are dispatched.  The problem occurs when a focus out and focus in event are received on the same tick.  When these delayed events are dispatched they will be sent in the order determined by the window list rather than the order in which they are received.

The focus out dispatch is implemented by calling SDL_SetKeyboardFocus(NULL).  This will remove focus from any window, regardless of whether it is the one originally targeted by the X11 event.

Since SDL_SetKeyboardFocus() will always dispatch a focus lost event as needed, the easiest solution is simply to only call SDL_SetKeyboardFocus(NULL) when SDL_GetKeyboardFocus() matches the target window.

Attached patch implements this solution.
Comment 2 Sam Lantinga 2013-11-03 17:55:47 UTC
The patch looks good, thanks!
https://hg.libsdl.org/SDL/rev/0c2e2b91eeea