Skip to content

Commit

Permalink
Added log message when frontend unexpectedly loses focus
Browse files Browse the repository at this point in the history
- hopefully this will help troubleshooting for people who are losing
  focus while running the frontend
- on Windows and Linux the process for the new foreground window will
  be reported in the log
  • Loading branch information
mickelson committed Aug 17, 2018
1 parent 1acef4a commit d00e37f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 7 deletions.
112 changes: 108 additions & 4 deletions src/fe_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <tlhelp32.h> // for CreateToolhelp32Snapshot()
#include <psapi.h> // for GetModuleFileNameEx()
#else
#include <sys/wait.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -968,10 +970,13 @@ bool run_program( const std::string &prog,
FeLog() << "Parameter word expansion failed. [" << args << "]." << std::endl;

// Provide some debugging output for what we are doing
FeDebug() << "execvp(): file='" << prog.c_str() << "', argv=";
for ( int r=0; we.we_wordv[r]; r++ )
FeDebug() << "[" << we.we_wordv[r] << "]";
FeDebug() << std::endl;
if ( callback == NULL )
{
FeDebug() << "execvp(): file='" << prog.c_str() << "', argv=";
for ( int r=0; we.we_wordv[r]; r++ )
FeDebug() << "[" << we.we_wordv[r] << "]";
FeDebug() << std::endl;
}

execvp( prog.c_str(), we.we_wordv );

Expand Down Expand Up @@ -1352,3 +1357,102 @@ bool get_console_stdin( std::string &str )

return ( !str.empty() );
}

std::string get_focus_process()
{
std::string retval( "Unknown" );

#if defined( SFML_SYSTEM_WINDOWS )
HWND focus_wnd = GetForegroundWindow();
if ( !focus_wnd )
return "None";

DWORD focus_pid( 0 );
GetWindowThreadProcessId( focus_wnd, &focus_pid );

HANDLE snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if ( snap == INVALID_HANDLE_VALUE )
{
FeLog() << "Could not get process snapshot." << std::endl;
return retval;
}

PROCESSENTRY32 pei;
pei.dwSize = sizeof( pei );
BOOL np = Process32First( snap, &pei );
while ( np )
{
if ( pei.th32ProcessID == focus_pid )
{
retval = pei.szExeFile;
break;
}
np = Process32Next( snap, &pei );
}

CloseHandle( snap );
retval += " (" + as_str( (int)focus_pid ) + ")";

#elif defined( USE_XLIB )

::Display *xdisp = XOpenDisplay( NULL );
Atom atom = XInternAtom( xdisp, "_NET_WM_PID", True );

Window wnd( 0 );
int revert;
XGetInputFocus( xdisp, &wnd, &revert );
if ( wnd == PointerRoot )
return "Pointer Root";
else if ( wnd == None )
return "None";

Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;
unsigned char *prop = NULL;

if ( XGetWindowProperty( xdisp,
wnd,
atom,
0,
1024,
False,
AnyPropertyType,
&actual_type,
&actual_format,
&nitems,
&bytes_after,
&prop ) != Success )
{
FeLog() << "Could not get window property." << std::endl;
return retval;
}

if ( !prop )
{
FeLog() << "Empty window property." << std::endl;
return retval;
}

int pid = prop[1] * 256;
pid += prop[0];
XFree( prop );

// Try to get the actual name for the process id
//
// Note: this is Linux specific
//
std::string fn = "/proc/" + as_str( pid ) + "/cmdline";
std::ifstream myfile( fn.c_str() );
if ( myfile.good() )
{
getline( myfile, retval );
myfile.close();
}

retval += " (" + as_str( pid ) + ")";

#endif

return retval;
}
3 changes: 3 additions & 0 deletions src/fe_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ bool line_to_setting_and_value( const std::string &line,
std::string &value,
const char *sep=FE_WHITESPACE );

// return the name of the process that currently has window focus
std::string get_focus_process();

//
// Non-blocking check for input on stdin
// return true if input found, false otherwise
Expand Down
22 changes: 20 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ int main(int argc, char *argv[])
soundsys.play_ambient();

FeBlend::load_default_shaders();

FeWindow window( feSettings );
window.initial_create();

Expand Down Expand Up @@ -162,7 +162,7 @@ int main(int argc, char *argv[])

soundsys.sound_event( FeInputMap::EventStartup );

bool redraw=true;
bool redraw=true, has_focus=true;
int guard_joyid=-1, guard_axis=-1;

// variables used to track movement when a key is held down
Expand Down Expand Up @@ -303,6 +303,8 @@ int main(int argc, char *argv[])

soundsys.sound_event( FeInputMap::EventGameReturn );
soundsys.play_ambient();

has_focus=true;
}

launch_game=false;
Expand Down Expand Up @@ -977,6 +979,22 @@ int main(int argc, char *argv[])
}
}

#if ( SFML_VERSION_INT >= FE_VERSION_INT( 2, 2, 0 ) )
//
// Log any unexpected loss of window focus
//
if ( has_focus )
{
if ( !window.hasFocus() )
{
has_focus = false;
FeLog() << " ! Unexpectedly lost focus to: " << get_focus_process() << std::endl;
}
}
else
has_focus = window.hasFocus();
#endif

if ( feVM.on_tick() )
redraw=true;

Expand Down
5 changes: 4 additions & 1 deletion src/scraper_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ std::string get_crc( const std::string &full_path,
return "";

correct_buff_for_format( buff, size, *itr );
return get_crc32( buff, size );
std::string retval = get_crc32( buff, size );
FeDebug() << "CRC: " << full_path << "=" << retval << std::endl;
return retval;
}
}

Expand Down Expand Up @@ -165,6 +167,7 @@ std::string get_crc( const std::string &full_path,

delete [] orig_buff_ptr;

FeDebug() << "CRC: " << full_path << "=" << retval << std::endl;
return retval;
}

Expand Down

0 comments on commit d00e37f

Please sign in to comment.