Skip to content

Commit

Permalink
Add option to focus game window on injection (#4)
Browse files Browse the repository at this point in the history
Some games don't play audio or render at reduced frame rate when
their window is not in focus. Switching active window when VR session is
started is a hassle.

This commit adds an option to automatically focus game window post
injection making UEVR experience more streamlined.
  • Loading branch information
keton authored Jan 7, 2024
1 parent a0da74c commit 4673c5a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions UEVR/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@
<ToggleButton x:Name="m_nullifyVRPluginsCheckbox" HorizontalAlignment="Left"/>
<TextBlock TextWrapping="Wrap" Text="Nullify VR Plugins"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<ToggleButton x:Name="m_focusGameOnInjectionCheckbox" HorizontalAlignment="Left" IsChecked="True"/>
<TextBlock TextWrapping="Wrap" Text="Focus game on injection"/>
</StackPanel>
</StackPanel>
<TextBlock x:Name="m_connectionStatus" TextWrapping="Wrap" Text="Unknown status"/>
</StackPanel>
Expand Down
15 changes: 15 additions & 0 deletions UEVR/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public class ValueTemplateSelector : DataTemplateSelector {
}
}
}

public partial class MainWindow : Window {
// variables
// process list
Expand All @@ -173,6 +174,9 @@ public partial class MainWindow : Window {
private string? m_commandLineAttachExe = null;
private bool m_ignoreFutureVDWarnings = false;

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

public MainWindow() {
InitializeComponent();

Expand Down Expand Up @@ -204,6 +208,7 @@ private void MainWindow_Loaded(object sender, RoutedEventArgs e) {
m_openxrRadio.IsChecked = m_mainWindowSettings.OpenXRRadio;
m_nullifyVRPluginsCheckbox.IsChecked = m_mainWindowSettings.NullifyVRPluginsCheckbox;
m_ignoreFutureVDWarnings = m_mainWindowSettings.IgnoreFutureVDWarnings;
m_focusGameOnInjectionCheckbox.IsChecked = m_mainWindowSettings.FocusGameOnInjection;

m_updateTimer.Tick += (sender, e) => Dispatcher.Invoke(MainWindow_Update);
m_updateTimer.Start();
Expand Down Expand Up @@ -351,6 +356,10 @@ private void Update_InjectStatus() {
m_lastAutoInjectTime = now;
m_commandLineAttachExe = null; // no need anymore.
FillProcessList();
if (m_focusGameOnInjectionCheckbox.IsChecked == true)
{
SwitchToThisWindow(process.MainWindowHandle, true);
}
}
}
}
Expand Down Expand Up @@ -574,6 +583,7 @@ private void MainWindow_Closing(object sender, System.ComponentModel.CancelEvent
m_mainWindowSettings.OpenVRRadio = m_openvrRadio.IsChecked == true;
m_mainWindowSettings.NullifyVRPluginsCheckbox = m_nullifyVRPluginsCheckbox.IsChecked == true;
m_mainWindowSettings.IgnoreFutureVDWarnings = m_ignoreFutureVDWarnings;
m_mainWindowSettings.FocusGameOnInjection = m_focusGameOnInjectionCheckbox.IsChecked == true;

m_mainWindowSettings.Save();
}
Expand Down Expand Up @@ -1040,6 +1050,11 @@ private void Inject_Clicked(object sender, RoutedEventArgs e) {

Injector.InjectDll(process.Id, "UEVRBackend.dll");
}

if (m_focusGameOnInjectionCheckbox.IsChecked == true)
{
SwitchToThisWindow(process.MainWindowHandle, true);
}
}

private string GenerateProcessName(Process p) {
Expand Down
8 changes: 8 additions & 0 deletions UEVR/MainWindowSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@ public bool IgnoreFutureVDWarnings {
get { return (bool)this["IgnoreFutureVDWarnings"]; }
set { this["IgnoreFutureVDWarnings"] = value; }
}

[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("true")]
public bool FocusGameOnInjection
{
get { return (bool)this["FocusGameOnInjection"]; }
set { this["FocusGameOnInjection"] = value; }
}
}
}

0 comments on commit 4673c5a

Please sign in to comment.