You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow ignoring input events for unfocused X11 windows #1775
Currently every X11 window will receive every input event globally, regardless of window focus. Even if the events are not processed by the application and just discarded, this has a significant impact on the CPU usage by winit applications.
Ideally an application running in the background without focus should not be impacted by mouse motion at all. Currently there's about a 1-2% CPU usage for my system whenever the user starts moving the mouse. This affects every single Alacritty window of course which leads to a bunch of just pointlessly wasted CPU resources.
There's probably two options to solve this. Either we introduce a way for people to tell winit that they don't care about input events on unfocused windows/don't care about input events anymore, or alternatively winit strives for consistency between platforms and emulates Wayland's behavior where mouse events are never sent to unfocused applications.
You mean that you want to disable dispatching winit::event::DeviceEventwhen the windows are unfocused, right?
In alacritty, this event is not used at all, so one easy solution for this problem is to provide a way to disable all DeviceEvent by removing XISelectEvents calls when creating EventLoop.
By this change, x11 won't send XI_Raw* events, so DeviceEvent won't trigger in any cases. But we can get keyboard and mouse events using window events when the window is active. What do you think?
I don't want to just not dispatch it, I want winit to not wakeup on these events. Which should be possible by telling X that winit does not care about these events.
Disabling all DeviceEvents would work for Alacritty, though that seems more like a hack than a solution. I think it might be possible to specify an event mask that always filters these device events while unfocused. For winit that would probably be the correct choice, since its goal should be to provide similar APIs on all platforms.
I think it might be possible to specify an event mask that always filters these device events while unfocused.
I meant that disabling all DeviceEvent by event mask in x11 (XISelectEvetns).
My suggestion is that providing a way to filter these device events both in unfocused state and in focused state. DeviceEvent itself is an global event, so we need to think about the case that the application has multiple windows. In that case, in some application, they want to disable DeviceEvent only when some particular windows are unfocused. So, rather than switching the state automatically in winit side, I think it will be better to control the application side.
No matter what you do, winit will not be able to handle mouse motions as DeviceEvents with the window unfocused across all platforms. So there's little reason to go out of your way to support that.
No, my proposal is providing an option for application to choose they want to receive DeviceEvents or not.
So, if the application need to use DeviceEvents to get mouse motion when the window is unfocused, they still can get DeviceEvents by this option (and this option should be enabled by default).
And this will also satisfy your case as well by unset this option since your applicaiton doesn't use DeviceEvents at all.
So, if the application need to use DeviceEvents to get mouse motion when the window is unfocused, they still can get DeviceEvents by this option (and this option should be enabled by default).
But they can't. This is impossible to do in a cross-platform manner. Which is the only reason why anyone would use winit.
A cross-platform manner is very important point. Doesn't other platforms receive DeviceEvents when the window is inactive?
Since DeviceEvent in winit is an global event and doesn't have window ID, so I feel it is weird to change its behavior based on window status.
If DeviceEvent behavior automatically changes based on window status based on the window status, it is worth implementing the same behavior in x11 as well. Could you tell me how other platforms changes DeviceEvents behavior, especially when the application has multiple windows.
Hmmm, in x11, these raw events seem used to receive events when the window is unfocused, for example xeyes.
I don't know whether Mac and Windows have similar APIs, but if they have, it will be better not to disable it even when the window is unfocused.
Found that in windows, the application can receive DeviceEvent (Raw Input) even if the application runs in background,
Yes, that is indeed the case on Windows.
Perhaps receiving DeviceEvents while windows are unfocused should be opt-in, rather than opt-out; Most applications probably don't want to receive input while in the background.
Perhaps receiving DeviceEvents while windows are unfocused should be opt-in, rather than opt-out; Most applications probably don't want to receive input while in the background.
Then, what is the use case of DeviceEvents? Is there any example that the application need DeviceEvents rather than WindowEvent when the window is focused?
Is there any example that the application need DeviceEvents rather than WindowEvent when the window is focused?
If you're making a 3D game with an FPS camera (or an over-the-shoulder camera), then you'll want to use DeviceEvent::MouseMotion to get relative mouse motion, rather than an absolute position. DeviceEvent::MouseMotion also ignores pointer acceleration (on Windows, at least), which is what's generally recommended for those kinds of games.
Activity
garasubo commentedon Feb 12, 2021
You mean that you want to disable dispatching
winit::event::DeviceEventwhen the windows are unfocused, right?In alacritty, this event is not used at all, so one easy solution for this problem is to provide a way to disable all
DeviceEventby removingXISelectEventscalls when creatingEventLoop.By this change, x11 won't send
XI_Raw*events, soDeviceEventwon't trigger in any cases. But we can get keyboard and mouse events using window events when the window is active. What do you think?chrisduerr commentedon Feb 12, 2021
I don't want to just not dispatch it, I want winit to not wakeup on these events. Which should be possible by telling X that winit does not care about these events.
Disabling all
DeviceEvents would work for Alacritty, though that seems more like a hack than a solution. I think it might be possible to specify an event mask that always filters these device events while unfocused. For winit that would probably be the correct choice, since its goal should be to provide similar APIs on all platforms.garasubo commentedon Feb 13, 2021
I meant that disabling all
DeviceEventby event mask in x11 (XISelectEvetns).My suggestion is that providing a way to filter these device events both in unfocused state and in focused state.
DeviceEventitself is an global event, so we need to think about the case that the application has multiple windows. In that case, in some application, they want to disableDeviceEventonly when some particular windows are unfocused. So, rather than switching the state automatically in winit side, I think it will be better to control the application side.chrisduerr commentedon Feb 13, 2021
No matter what you do, winit will not be able to handle mouse motions as DeviceEvents with the window unfocused across all platforms. So there's little reason to go out of your way to support that.
garasubo commentedon Feb 13, 2021
No, my proposal is providing an option for application to choose they want to receive DeviceEvents or not.
So, if the application need to use DeviceEvents to get mouse motion when the window is unfocused, they still can get DeviceEvents by this option (and this option should be enabled by default).
And this will also satisfy your case as well by unset this option since your applicaiton doesn't use DeviceEvents at all.
chrisduerr commentedon Feb 13, 2021
But they can't. This is impossible to do in a cross-platform manner. Which is the only reason why anyone would use winit.
garasubo commentedon Feb 14, 2021
A cross-platform manner is very important point. Doesn't other platforms receive DeviceEvents when the window is inactive?
Since DeviceEvent in winit is an global event and doesn't have window ID, so I feel it is weird to change its behavior based on window status.
If DeviceEvent behavior automatically changes based on window status based on the window status, it is worth implementing the same behavior in x11 as well. Could you tell me how other platforms changes DeviceEvents behavior, especially when the application has multiple windows.
chrisduerr commentedon Feb 14, 2021
Generally no, due to Wayland. Especially mouse/key events.
garasubo commentedon Feb 14, 2021
Hmmm, in x11, these raw events seem used to receive events when the window is unfocused, for example xeyes.
I don't know whether Mac and Windows have similar APIs, but if they have, it will be better not to disable it even when the window is unfocused.
garasubo commentedon Feb 14, 2021
Found that in windows, the application can receive DeviceEvent (Raw Input) even if the application runs in background, according to this document. https://docs.microsoft.com/en-us/windows/win32/inputdev/about-raw-input
maroider commentedon Feb 14, 2021
Yes, that is indeed the case on Windows.
Perhaps receiving
DeviceEvents while windows are unfocused should be opt-in, rather than opt-out; Most applications probably don't want to receive input while in the background.garasubo commentedon Feb 14, 2021
Then, what is the use case of DeviceEvents? Is there any example that the application need DeviceEvents rather than WindowEvent when the window is focused?
maroider commentedon Feb 14, 2021
If you're making a 3D game with an FPS camera (or an over-the-shoulder camera), then you'll want to use
DeviceEvent::MouseMotionto get relative mouse motion, rather than an absolute position.DeviceEvent::MouseMotionalso ignores pointer acceleration (on Windows, at least), which is what's generally recommended for those kinds of games.chrisduerr commentedon Feb 14, 2021
These examples are only relevant when the window is focused, so it doesn't matter in the context of this issue.
Ciantic commentedon Feb 17, 2021
I opened similar issue sometimes back: #1634
I can't use Winit if it acts as keylogger (and also hogs CPU) when the window is minimized or not even visible.
chrisduerr commentedon Feb 17, 2021
Good point.