SFML / SFML

Simple and Fast Multimedia Library
https://www.sfml-dev.org/
zlib License
9.93k stars 1.68k forks source link

GainedFocus/LostFocus events with i3wm borderless fullscreen mode #1660

Open Unarelith opened 4 years ago

Unarelith commented 4 years ago

Subject of the issue

When using i3 borderless fullscreen mode (usually Super+F or Alt+F), SFML considers that the window loses focus, even if it doesn't.

I tried to compare with other programs and it seems that it is a SFML-specific issue.

Your environment

Steps to reproduce

#include <ctime>
#include <iostream>

#include <SFML/OpenGL.hpp>
#include <SFML/Window.hpp>

int main(int, char **) {
    sf::Window window;
    window.create(sf::VideoMode(800, 600), "SFML", sf::Style::Close);

    bool running = true;
    while (running) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)) {
                running = false;
            }
            else if (event.type == sf::Event::LostFocus) {
                std::cout << "[" << std::time(nullptr) << "] " << "Window lost focus" << std::endl;
            }
            else if (event.type == sf::Event::GainedFocus) {
                std::cout << "[" << std::time(nullptr) << "] " << "Window gained focus" << std::endl;
            }
        }

        glClear(GL_COLOR_BUFFER_BIT);
        window.display();
    }

    window.close();

    return 0;
}

Expected behavior

# opening window
[1589323207] Window gained focus
# toggling i3 fullscreen mode
# toggling back to windowed mode
# closing window
[1589323224] Window lost focus

Actual behavior

# opening window
[1589323207] Window gained focus
# toggling i3 fullscreen mode
[1589323215] Window lost focus
[1589323215] Window lost focus
[1589323215] Window gained focus
# toggling back to windowed mode
[1589323221] Window lost focus
[1589323221] Window lost focus
[1589323221] Window gained focus
# closing window
[1589323224] Window lost focus

Note that the LostFocus event is sent twice, I assume this shouldn't be happening?

Unarelith commented 4 years ago

I noticed that it's not only for i3 fullscreen mode but also for a different situation.

I'm using dunst for notifications, and each time I interact with them (using Ctrl+Space to discard a notification or Ctrl+) to show last notification), SFML considers the window has lost the focus, even if it actually didn't.

Also, when I'm taking a screenshot using this script, the window loses focus, preventing me to take a screenshot without my pause menu (which is triggered by LostFocus event)