Skip to content

Commit 7789f67

Browse files
authored
Merge pull request #43 from Aynshe/dev
Dev
2 parents dcb42c3 + 7bb95e4 commit 7789f67

3 files changed

Lines changed: 53 additions & 36 deletions

File tree

Batrun.cs

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void Main()
6666
return;
6767
}
6868

69-
Application.SetHighDpiMode(HighDpiMode.SystemAware);
69+
Application.SetHighDpiMode(HighDpiMode.DpiUnaware);
7070
Application.EnableVisualStyles();
7171
Application.SetCompatibleTextRenderingDefault(false);
7272

RetroBatService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public async Task Launch()
131131
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
132132
{
133133
FileName = _retrobatPath,
134+
Arguments = "--external-launcher",
134135
UseShellExecute = false
135136
};
136137
System.Diagnostics.Process.Start(startInfo);

0 commit comments

Comments
 (0)