Skip to content

Allow ignoring input events for unfocused X11 windows #1775

Closed

Description

@chrisduerr

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.

Activity

  1. added
    S - enhancement
    DS - x11
    on Nov 26, 2020
  2. garasubo commented on Feb 12, 2021

    @garasubo
    Contributor

    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?

  3. chrisduerr commented on Feb 12, 2021

    @chrisduerr
    ContributorAuthor

    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.

  4. garasubo commented on Feb 13, 2021

    @garasubo
    Contributor

    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.

  5. chrisduerr commented on Feb 13, 2021

    @chrisduerr
    ContributorAuthor

    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.

  6. garasubo commented on Feb 13, 2021

    @garasubo
    Contributor

    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.

  7. chrisduerr commented on Feb 13, 2021

    @chrisduerr
    ContributorAuthor

    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.

  8. garasubo commented on Feb 14, 2021

    @garasubo
    Contributor

    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.

  9. chrisduerr commented on Feb 14, 2021

    @chrisduerr
    ContributorAuthor

    Doesn't other platforms receive DeviceEvents when the window is inactive?

    Generally no, due to Wayland. Especially mouse/key events.

  10. garasubo commented on Feb 14, 2021

    @garasubo
    Contributor

    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.

  11. garasubo commented on Feb 14, 2021

    @garasubo
    Contributor

    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

  12. maroider commented on Feb 14, 2021

    @maroider
    Member

    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.

  13. garasubo commented on Feb 14, 2021

    @garasubo
    Contributor

    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?

  14. maroider commented on Feb 14, 2021

    @maroider
    Member

    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.

  15. chrisduerr commented on Feb 14, 2021

    @chrisduerr
    ContributorAuthor

    These examples are only relevant when the window is focused, so it doesn't matter in the context of this issue.

  16. Ciantic commented on Feb 17, 2021

    @Ciantic

    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.

  17. chrisduerr commented on Feb 17, 2021

    @chrisduerr
    ContributorAuthor

    Good point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DS - x11S - enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions