@@ -1153,55 +1153,71 @@ private void HandleESSystemSelect()
11531153 }
11541154 }
11551155
1156- private void FocusEmulationStation ( )
1156+ private async void FocusEmulationStation ( )
11571157 {
1158- try
1158+ // Use the robust looping focus logic to ensure ES comes to the foreground, especially after the loading video.
1159+ int focusDuration = config . ReadInt ( "Focus" , "FocusDuration" , 10000 ) ; // Try for 10 seconds
1160+ int focusInterval = config . ReadInt ( "Focus" , "FocusInterval" , 500 ) ;
1161+ int elapsedTime = 0 ;
1162+
1163+ logger . LogInfo ( $ "Starting robust focus sequence for EmulationStation (Duration: { focusDuration } ms, Interval: { focusInterval } ms)") ;
1164+
1165+ while ( elapsedTime < focusDuration )
11591166 {
1160- Process [ ] processes = Process . GetProcessesByName ( "EmulationStation" ) ;
1161- if ( processes . Length > 0 && ! processes [ 0 ] . HasExited )
1167+ try
11621168 {
1163- IntPtr hWnd = processes [ 0 ] . MainWindowHandle ;
1169+ var process = Process . GetProcessesByName ( "emulationstation" ) . FirstOrDefault ( ) ;
1170+ IntPtr hWnd = process ? . MainWindowHandle ?? IntPtr . Zero ;
1171+
11641172 if ( hWnd != IntPtr . Zero )
11651173 {
1166- // Allow focus change for this process
1167- NativeMethods . AllowSetForegroundWindow ( processes [ 0 ] . Id ) ;
1168-
1169- // Get thread IDs
1170- uint foregroundThread = NativeMethods . GetWindowThreadProcessId (
1171- NativeMethods . GetForegroundWindow ( ) , IntPtr . Zero ) ;
1172- uint appThread = NativeMethods . GetCurrentThreadId ( ) ;
1173-
1174- // Attach threads for focus
1175- bool threadAttached = false ;
1176- if ( foregroundThread != appThread )
1174+ if ( process != null )
11771175 {
1178- threadAttached = NativeMethods . AttachThreadInput ( foregroundThread , appThread , true ) ;
1179- }
1176+ NativeMethods . AllowSetForegroundWindow ( process . Id ) ;
1177+ uint foregroundThread = NativeMethods . GetWindowThreadProcessId ( NativeMethods . GetForegroundWindow ( ) , IntPtr . Zero ) ;
1178+ uint appThread = NativeMethods . GetCurrentThreadId ( ) ;
1179+ bool threadAttached = false ;
11801180
1181- try
1182- {
1183- NativeMethods . ShowWindow ( hWnd , NativeMethods . SW_RESTORE ) ;
1184- NativeMethods . ShowWindow ( hWnd , NativeMethods . SW_SHOW ) ;
1185- NativeMethods . BringWindowToTop ( hWnd ) ;
1186- NativeMethods . SetForegroundWindow ( hWnd ) ;
1181+ if ( foregroundThread != appThread )
1182+ {
1183+ threadAttached = NativeMethods . AttachThreadInput ( foregroundThread , appThread , true ) ;
1184+ }
11871185
1188- logger . LogInfo ( "Focus forcefully applied to EmulationStation" ) ;
1189- }
1190- finally
1191- {
1192- // Detach threads if necessary
1193- if ( threadAttached )
1186+ try
11941187 {
1195- NativeMethods . AttachThreadInput ( foregroundThread , appThread , false ) ;
1188+ NativeMethods . ShowWindow ( hWnd , NativeMethods . SW_RESTORE ) ;
1189+ NativeMethods . ShowWindow ( hWnd , NativeMethods . SW_SHOW ) ;
1190+ NativeMethods . BringWindowToTop ( hWnd ) ;
1191+ NativeMethods . SetForegroundWindow ( hWnd ) ;
1192+ logger . LogInfo ( $ "Focus applied to EmulationStation (attempt { elapsedTime / focusInterval + 1 } )") ;
1193+
1194+ // If the window is now in the foreground, we can stop trying.
1195+ if ( NativeMethods . GetForegroundWindow ( ) == hWnd )
1196+ {
1197+ logger . LogInfo ( "EmulationStation is now in the foreground. Stopping focus loop." ) ;
1198+ return ;
1199+ }
1200+ }
1201+ finally
1202+ {
1203+ if ( threadAttached )
1204+ {
1205+ NativeMethods . AttachThreadInput ( foregroundThread , appThread , false ) ;
1206+ }
11961207 }
11971208 }
11981209 }
11991210 }
1211+ catch ( Exception ex )
1212+ {
1213+ logger . LogError ( "Error setting focus to EmulationStation" , ex ) ;
1214+ }
1215+
1216+ await Task . Delay ( focusInterval ) ;
1217+ elapsedTime += focusInterval ;
12001218 }
1201- catch ( Exception ex )
1202- {
1203- logger . LogError ( $ "Error focusing EmulationStation: { ex . Message } ") ;
1204- }
1219+
1220+ logger . LogWarning ( "End of robust focus sequence. EmulationStation may not be in the foreground." ) ;
12051221 }
12061222
12071223
0 commit comments