New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force or Track Window Focus #518

Closed
Ruhrmolch opened this Issue Dec 29, 2013 · 13 comments

Comments

Projects
None yet
6 participants
@Ruhrmolch

Ruhrmolch commented Dec 29, 2013

I was told, that there is no way to force an OS to focus a RenderWindow, like it could be useful here:
http://en.sfml-dev.org/forums/index.php?topic=13958.0

I think, it would be a nice feature in the next Version of sfml.

@LaurentGomila

This comment has been minimized.

Show comment
Hide comment
@LaurentGomila

LaurentGomila Dec 29, 2013

Member

New features should be discussed on the forum. Only validated features should be posted to the tracker.

Member

LaurentGomila commented Dec 29, 2013

New features should be discussed on the forum. Only validated features should be posted to the tracker.

@eXpl0it3r

This comment has been minimized.

Show comment
Hide comment
@eXpl0it3r

eXpl0it3r Jan 2, 2014

Member

Uhm, this has already been discussed at least 3 times (1, 2, 3) on the forum and we've all seen its usefulness (at some point). You even said:

It won't be done for SFML 2.0, but I think it's worth adding a ticket to the task tracker. It's not the first time that focus issues are discussed.

But nobody ever got around to open a ticket until now. ;)

Member

eXpl0it3r commented Jan 2, 2014

Uhm, this has already been discussed at least 3 times (1, 2, 3) on the forum and we've all seen its usefulness (at some point). You even said:

It won't be done for SFML 2.0, but I think it's worth adding a ticket to the task tracker. It's not the first time that focus issues are discussed.

But nobody ever got around to open a ticket until now. ;)

@LaurentGomila

This comment has been minimized.

Show comment
Hide comment
@LaurentGomila

LaurentGomila Jan 2, 2014

Member

Ah, sorry.

Member

LaurentGomila commented Jan 2, 2014

Ah, sorry.

@LaurentGomila LaurentGomila reopened this Jan 2, 2014

@ghost ghost assigned LaurentGomila Jan 2, 2014

@Foaly

This comment has been minimized.

Show comment
Hide comment
@Foaly

Foaly Jan 7, 2014

Contributor

I would find this very useful as well! And I also have a use case for it. I use SFML together with OpenCV. Sometimes I use OpenCV windows for debugging/testing output. No matter whats the order of creation, the SFML window always gets created behind the opencv windows. And there is no way of changing that except click in it.
I did a bit of reseach and here is what I found:
The question came up in one of the forum thread (don't remember which one) yes it is possible to find out which window has focus, without tracking the events manually. Yes it is! On windows it would be something as simple as:

bool WindowImplWin32::hasFocus()
{
    return m_handle == GetForegroundWindow();
}

Forcing the focus on windows seems a little more ticky. See this Stack Overflow question.

On Mac there seems to be a method in NSWindow, see here (search for isMainWindow), but I have no experience programming on Mac, so I don't know.

On Linux things seem to be a little more tricky. The only solution I could find was to use XQueryTree and pass it the root window. The function will return a sorted list of all child windows (since we pass in the root window that means all windows). Then we have to compare our window handle against the top one.

I can write a pull request, but I'd need some help on the Mac part and testing on Linux.

Contributor

Foaly commented Jan 7, 2014

I would find this very useful as well! And I also have a use case for it. I use SFML together with OpenCV. Sometimes I use OpenCV windows for debugging/testing output. No matter whats the order of creation, the SFML window always gets created behind the opencv windows. And there is no way of changing that except click in it.
I did a bit of reseach and here is what I found:
The question came up in one of the forum thread (don't remember which one) yes it is possible to find out which window has focus, without tracking the events manually. Yes it is! On windows it would be something as simple as:

bool WindowImplWin32::hasFocus()
{
    return m_handle == GetForegroundWindow();
}

Forcing the focus on windows seems a little more ticky. See this Stack Overflow question.

On Mac there seems to be a method in NSWindow, see here (search for isMainWindow), but I have no experience programming on Mac, so I don't know.

On Linux things seem to be a little more tricky. The only solution I could find was to use XQueryTree and pass it the root window. The function will return a sorted list of all child windows (since we pass in the root window that means all windows). Then we have to compare our window handle against the top one.

I can write a pull request, but I'd need some help on the Mac part and testing on Linux.

@MarioLiebisch

This comment has been minimized.

Show comment
Hide comment
@MarioLiebisch

MarioLiebisch Jan 7, 2014

Member

No need for such a "workaround", since SFML already receives/processes messages for gained/lost focus, adding a query function should be really trivial to do.

Member

MarioLiebisch commented Jan 7, 2014

No need for such a "workaround", since SFML already receives/processes messages for gained/lost focus, adding a query function should be really trivial to do.

@eXpl0it3r

This comment has been minimized.

Show comment
Hide comment
@eXpl0it3r

eXpl0it3r Jan 7, 2014

Member

No need for such a "workaround", since SFML already receives/processes messages for gained/lost focus, adding a query function should be really trivial to do.

But that is exactly the issue. In order to track the gain/lost events/messages, you need to know the initial state of the window. So in theory it should always have the focus, but apparently in some situations that's not the case. With the current SFML API it's impossible to test whether the window actually has the focus and it's also impossible to force the focus.

Member

eXpl0it3r commented Jan 7, 2014

No need for such a "workaround", since SFML already receives/processes messages for gained/lost focus, adding a query function should be really trivial to do.

But that is exactly the issue. In order to track the gain/lost events/messages, you need to know the initial state of the window. So in theory it should always have the focus, but apparently in some situations that's not the case. With the current SFML API it's impossible to test whether the window actually has the focus and it's also impossible to force the focus.

MarioLiebisch added a commit to MarioLiebisch/SFML that referenced this issue Jan 7, 2014

Added Window members to request and determine input focus
This relates to issue SFML#518.

So far this is broken for Unix/Mac, I'm going to extend the code later,
but wanted to commit this for (Windows) people to try it right now, since
handling is partially completely different based on platform.
@MarioLiebisch

This comment has been minimized.

Show comment
Hide comment
@MarioLiebisch

MarioLiebisch Jan 7, 2014

Member

Implemented this for Win32 so far (see commit above). Feel free to test run it. I'm going to add Unix code as soon as I can and will have a look at the Mac part as well, although I might need quite a bit of help there. :)

To test the focus, you can do something as simple as window.clear(window.hasFocus() ? sf::Color::White : sf::Color::Black);. Just note that sf::Window::requestFocus() is not guaranteed to work, e.g. under Windows programs aren't allowed to steal active input focus in most situations, so only the task bar button will start flashing (unless you change the defaults).

Member

MarioLiebisch commented Jan 7, 2014

Implemented this for Win32 so far (see commit above). Feel free to test run it. I'm going to add Unix code as soon as I can and will have a look at the Mac part as well, although I might need quite a bit of help there. :)

To test the focus, you can do something as simple as window.clear(window.hasFocus() ? sf::Color::White : sf::Color::Black);. Just note that sf::Window::requestFocus() is not guaranteed to work, e.g. under Windows programs aren't allowed to steal active input focus in most situations, so only the task bar button will start flashing (unless you change the defaults).

@Foaly

This comment has been minimized.

Show comment
Hide comment
@Foaly

Foaly Jan 8, 2014

Contributor

Ah man really... I already put a couple days research into this, so I would have liked to send this pull request!

Contributor

Foaly commented Jan 8, 2014

Ah man really... I already put a couple days research into this, so I would have liked to send this pull request!

@MarioLiebisch

This comment has been minimized.

Show comment
Hide comment
@MarioLiebisch

MarioLiebisch Jan 8, 2014

Member

It's no pull request so far, just one possible implementation. Feel free to help, if you want to. Don't mind who submits the final patch in the end.

Member

MarioLiebisch commented Jan 8, 2014

It's no pull request so far, just one possible implementation. Feel free to help, if you want to. Don't mind who submits the final patch in the end.

@Foaly

This comment has been minimized.

Show comment
Hide comment
@Foaly

Foaly Jan 12, 2014

Contributor

Yeah that was childish. Its about finding a fix, not who is doing it. Sorry about that.
I am going to start working on a the Linux part today.

Contributor

Foaly commented Jan 12, 2014

Yeah that was childish. Its about finding a fix, not who is doing it. Sorry about that.
I am going to start working on a the Linux part today.

@MarioLiebisch

This comment has been minimized.

Show comment
Hide comment
@MarioLiebisch

MarioLiebisch Jan 12, 2014

Member

No offense taken. :) Sounds gold.

Member

MarioLiebisch commented Jan 12, 2014

No offense taken. :) Sounds gold.

@Foaly

This comment has been minimized.

Show comment
Hide comment
@Foaly

Foaly Jan 12, 2014

Contributor

Ok I implemented the Linux part and removed the unnessesary book keeping of the focused state for windows. I also updated the documentation. (and removed some whitespace :P)
I collected everything in a pull request. The only thing that is missing is the Mac part. I doesn't seem to complicted, but I have no experience on Mac (also I can't test), but I'll see if I can find someone.

Contributor

Foaly commented Jan 12, 2014

Ok I implemented the Linux part and removed the unnessesary book keeping of the focused state for windows. I also updated the documentation. (and removed some whitespace :P)
I collected everything in a pull request. The only thing that is missing is the Mac part. I doesn't seem to complicted, but I have no experience on Mac (also I can't test), but I'll see if I can find someone.

@LaurentGomila

This comment has been minimized.

Show comment
Hide comment
@LaurentGomila

LaurentGomila May 20, 2014

Member

Implemented in #525.

Member

LaurentGomila commented May 20, 2014

Implemented in #525.

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