Merge pull request #4394 from aldelaro5/fix-focus-detection-linux

Fix window focus detection on Linux (rebase from #3843)
This commit is contained in:
Anthony 2016-10-29 00:35:18 -05:00 committed by GitHub
commit 41563c55cd
5 changed files with 22 additions and 68 deletions

View File

@ -504,18 +504,25 @@ void CFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
// Events // Events
void CFrame::OnActive(wxActivateEvent& event) void CFrame::OnActive(wxActivateEvent& event)
{ {
m_bRendererHasFocus = (event.GetActive() && event.GetEventObject() == m_RenderFrame);
if (Core::GetState() == Core::CORE_RUN || Core::GetState() == Core::CORE_PAUSE) if (Core::GetState() == Core::CORE_RUN || Core::GetState() == Core::CORE_PAUSE)
{ {
if (event.GetActive() && event.GetEventObject() == m_RenderFrame) if (m_bRendererHasFocus)
{ {
if (SConfig::GetInstance().bRenderToMain) if (SConfig::GetInstance().bRenderToMain)
m_RenderParent->SetFocus(); m_RenderParent->SetFocus();
if (SConfig::GetInstance().m_PauseOnFocusLost && Core::GetState() == Core::CORE_PAUSE)
DoPause();
if (SConfig::GetInstance().bHideCursor && Core::GetState() == Core::CORE_RUN) if (SConfig::GetInstance().bHideCursor && Core::GetState() == Core::CORE_RUN)
m_RenderParent->SetCursor(wxCURSOR_BLANK); m_RenderParent->SetCursor(wxCURSOR_BLANK);
} }
else else
{ {
if (SConfig::GetInstance().m_PauseOnFocusLost && Core::GetState() == Core::CORE_RUN)
DoPause();
if (SConfig::GetInstance().bHideCursor) if (SConfig::GetInstance().bHideCursor)
m_RenderParent->SetCursor(wxNullCursor); m_RenderParent->SetCursor(wxNullCursor);
} }
@ -769,38 +776,7 @@ bool CFrame::RendererHasFocus()
{ {
if (m_RenderParent == nullptr) if (m_RenderParent == nullptr)
return false; return false;
#ifdef _WIN32 return m_bRendererHasFocus;
HWND window = GetForegroundWindow();
if (window == nullptr)
return false;
if (m_RenderFrame->GetHWND() == window)
return true;
#else
wxWindow* window = wxWindow::FindFocus();
if (window == nullptr)
return false;
// Why these different cases?
if (m_RenderParent == window || m_RenderParent == window->GetParent() ||
m_RenderParent->GetParent() == window->GetParent())
{
return true;
}
#endif
return false;
}
bool CFrame::UIHasFocus()
{
// UIHasFocus should return true any time any one of our UI
// windows has the focus, including any dialogs or other windows.
//
// wxWindow::FindFocus() returns the current wxWindow which has
// focus. If it's not one of our windows, then it will return
// null.
wxWindow* focusWindow = wxWindow::FindFocus();
return (focusWindow != nullptr);
} }
void CFrame::OnGameListCtrlItemActivated(wxListEvent& WXUNUSED(event)) void CFrame::OnGameListCtrlItemActivated(wxListEvent& WXUNUSED(event))
@ -1139,35 +1115,6 @@ void CFrame::OnMouse(wxMouseEvent& event)
event.Skip(); event.Skip();
} }
void CFrame::OnFocusChange(wxFocusEvent& event)
{
if (SConfig::GetInstance().m_PauseOnFocusLost && Core::IsRunningAndStarted())
{
if (RendererHasFocus())
{
if (Core::GetState() == Core::CORE_PAUSE)
{
Core::SetState(Core::CORE_RUN);
if (SConfig::GetInstance().bHideCursor)
m_RenderParent->SetCursor(wxCURSOR_BLANK);
}
}
else
{
if (Core::GetState() == Core::CORE_RUN)
{
Core::SetState(Core::CORE_PAUSE);
if (SConfig::GetInstance().bHideCursor)
m_RenderParent->SetCursor(wxNullCursor);
Core::UpdateTitle();
}
}
UpdateGUI();
}
event.Skip();
}
void CFrame::DoFullscreen(bool enable_fullscreen) void CFrame::DoFullscreen(bool enable_fullscreen)
{ {
if (g_Config.bExclusiveMode && Core::GetState() == Core::CORE_PAUSE) if (g_Config.bExclusiveMode && Core::GetState() == Core::CORE_PAUSE)

View File

@ -101,7 +101,6 @@ public:
void OnRenderParentClose(wxCloseEvent& event); void OnRenderParentClose(wxCloseEvent& event);
void OnRenderParentMove(wxMoveEvent& event); void OnRenderParentMove(wxMoveEvent& event);
bool RendererHasFocus(); bool RendererHasFocus();
bool UIHasFocus();
bool RendererIsFullscreen(); bool RendererIsFullscreen();
void DoFullscreen(bool bF); void DoFullscreen(bool bF);
void ToggleDisplayMode(bool bFullscreen); void ToggleDisplayMode(bool bFullscreen);
@ -158,6 +157,7 @@ private:
bool m_bNoDocking = false; bool m_bNoDocking = false;
bool m_bGameLoading = false; bool m_bGameLoading = false;
bool m_bClosing = false; bool m_bClosing = false;
bool m_bRendererHasFocus = false;
bool m_confirmStop = false; bool m_confirmStop = false;
bool m_tried_graceful_shutdown = false; bool m_tried_graceful_shutdown = false;
int m_saveSlot = 1; int m_saveSlot = 1;
@ -306,8 +306,6 @@ private:
void OnKeyDown(wxKeyEvent& event); // Keyboard void OnKeyDown(wxKeyEvent& event); // Keyboard
void OnMouse(wxMouseEvent& event); // Mouse void OnMouse(wxMouseEvent& event); // Mouse
void OnFocusChange(wxFocusEvent& event);
void OnHostMessage(wxCommandEvent& event); void OnHostMessage(wxCommandEvent& event);
void OnMemcard(wxCommandEvent& event); // Misc void OnMemcard(wxCommandEvent& event); // Misc

View File

@ -735,8 +735,6 @@ void CFrame::StartGame(const std::string& filename)
wxTheApp->Bind(wxEVT_MIDDLE_DOWN, &CFrame::OnMouse, this); wxTheApp->Bind(wxEVT_MIDDLE_DOWN, &CFrame::OnMouse, this);
wxTheApp->Bind(wxEVT_MIDDLE_UP, &CFrame::OnMouse, this); wxTheApp->Bind(wxEVT_MIDDLE_UP, &CFrame::OnMouse, this);
wxTheApp->Bind(wxEVT_MOTION, &CFrame::OnMouse, this); wxTheApp->Bind(wxEVT_MOTION, &CFrame::OnMouse, this);
wxTheApp->Bind(wxEVT_SET_FOCUS, &CFrame::OnFocusChange, this);
wxTheApp->Bind(wxEVT_KILL_FOCUS, &CFrame::OnFocusChange, this);
m_RenderParent->Bind(wxEVT_SIZE, &CFrame::OnRenderParentResize, this); m_RenderParent->Bind(wxEVT_SIZE, &CFrame::OnRenderParentResize, this);
} }
} }
@ -928,6 +926,8 @@ void CFrame::OnStopped()
m_RenderFrame->SetWindowStyle(m_RenderFrame->GetWindowStyle() & ~wxSTAY_ON_TOP); m_RenderFrame->SetWindowStyle(m_RenderFrame->GetWindowStyle() & ~wxSTAY_ON_TOP);
} }
m_RenderParent = nullptr; m_RenderParent = nullptr;
m_bRendererHasFocus = false;
m_RenderFrame = nullptr;
// Clean framerate indications from the status bar. // Clean framerate indications from the status bar.
GetStatusBar()->SetStatusText(" ", 0); GetStatusBar()->SetStatusText(" ", 0);

View File

@ -84,6 +84,7 @@ bool DolphinApp::OnInit()
Bind(wxEVT_QUERY_END_SESSION, &DolphinApp::OnEndSession, this); Bind(wxEVT_QUERY_END_SESSION, &DolphinApp::OnEndSession, this);
Bind(wxEVT_END_SESSION, &DolphinApp::OnEndSession, this); Bind(wxEVT_END_SESSION, &DolphinApp::OnEndSession, this);
Bind(wxEVT_IDLE, &DolphinApp::OnIdle, this); Bind(wxEVT_IDLE, &DolphinApp::OnIdle, this);
Bind(wxEVT_ACTIVATE_APP, &DolphinApp::OnActivate, this);
// Register message box and translation handlers // Register message box and translation handlers
RegisterMsgAlertHandler(&wxMsgAlert); RegisterMsgAlertHandler(&wxMsgAlert);
@ -256,6 +257,11 @@ void DolphinApp::AfterInit()
} }
} }
void DolphinApp::OnActivate(wxActivateEvent& ev)
{
m_is_active = ev.GetActive();
}
void DolphinApp::InitLanguageSupport() void DolphinApp::InitLanguageSupport()
{ {
std::string language_code; std::string language_code;
@ -500,7 +506,7 @@ void Host_SetWiiMoteConnectionState(int _State)
bool Host_UIHasFocus() bool Host_UIHasFocus()
{ {
return main_frame->UIHasFocus(); return wxGetApp().IsActiveThreadsafe();
} }
bool Host_RendererHasFocus() bool Host_RendererHasFocus()

View File

@ -16,6 +16,7 @@ extern CFrame* main_frame;
class DolphinApp : public wxApp class DolphinApp : public wxApp
{ {
public: public:
bool IsActiveThreadsafe() const { return m_is_active; }
CFrame* GetCFrame(); CFrame* GetCFrame();
private: private:
@ -33,10 +34,12 @@ private:
void OnEndSession(wxCloseEvent& event); void OnEndSession(wxCloseEvent& event);
void InitLanguageSupport(); void InitLanguageSupport();
void AfterInit(); void AfterInit();
void OnActivate(wxActivateEvent& ev);
void OnIdle(wxIdleEvent&); void OnIdle(wxIdleEvent&);
bool m_batch_mode = false; bool m_batch_mode = false;
bool m_confirm_stop = false; bool m_confirm_stop = false;
bool m_is_active = true;
bool m_load_file = false; bool m_load_file = false;
bool m_play_movie = false; bool m_play_movie = false;
bool m_use_debugger = false; bool m_use_debugger = false;