Skip to content

Commit

Permalink
app: move entry point to Program
Browse files Browse the repository at this point in the history
  • Loading branch information
nomi-san committed Jun 5, 2024
1 parent 2251328 commit f93b305
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 92 deletions.
90 changes: 22 additions & 68 deletions app/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;
using System.Linq;
using System.Windows.Interop;
Expand All @@ -15,93 +13,49 @@ namespace ParsecVDisplay
{
public partial class App : Application
{
const string ID = "QpHOX8IBUHBznGtqk9xm1";
public const string NAME = "ParsecVDisplay";
public const string VERSION = "0.45.2";
public const string GITHUB_REPO = "nomi-san/parsec-vdd";

public static bool Silent { get; private set; }

public static string[] Languages => LanguageDicts.Keys.ToArray();
static Dictionary<string, ResourceDictionary> LanguageDicts;

protected override void OnStartup(StartupEventArgs e)
{
if (e.Args.Length >= 2 && e.Args[0] == "-custom")
{
var modes = Display.ParseModes(e.Args[1]);
ParsecVDD.SetCustomDisplayModes(modes);

if (e.Args.Length >= 3)
{
if (Enum.TryParse<ParsecVDD.ParentGPU>(e.Args[2], true, out var kind))
{
ParsecVDD.SetParentGPU(kind);
}
}

Shutdown();
return;
}

Silent = e.Args.Contains("-silent");

var signal = new EventWaitHandle(false,
EventResetMode.AutoReset, ID, out var isOwned);

if (!isOwned)
{
signal.Set();
Shutdown();
return;
}

base.OnStartup(e);
LoadLanguages();

string error = null;
var status = ParsecVDD.QueryStatus();

if (status != Device.Status.OK)
{
if (status == Device.Status.RESTART_REQUIRED)
{
MessageBox.Show(GetTranslation("t_msg_must_restart_pc"),
NAME, MessageBoxButton.OK, MessageBoxImage.Warning);
}
else if (status == Device.Status.DISABLED)
{
MessageBox.Show(GetTranslation("t_msg_driver_is_disabled", ParsecVDD.ADAPTER),
NAME, MessageBoxButton.OK, MessageBoxImage.Warning);
}
else if (status == Device.Status.NOT_INSTALLED)
{
MessageBox.Show(GetTranslation("t_msg_please_install_driver"),
NAME, MessageBoxButton.OK, MessageBoxImage.Warning);
}
else
switch (status)
{
MessageBox.Show(GetTranslation("t_msg_driver_status_not_ok", status),
NAME, MessageBoxButton.OK, MessageBoxImage.Warning);
case Device.Status.RESTART_REQUIRED:
error = GetTranslation("t_msg_must_restart_pc");
break;
case Device.Status.DISABLED:
error =GetTranslation("t_msg_driver_is_disabled", ParsecVDD.ADAPTER);
break;
case Device.Status.NOT_INSTALLED:
error = GetTranslation("t_msg_please_install_driver");
break;
default:
error = GetTranslation("t_msg_driver_status_not_ok", status);
break;
}

Shutdown();
return;
}

if (ParsecVDD.Init() == false)
else if (ParsecVDD.Init() != true)
{
MessageBox.Show(GetTranslation("t_msg_failed_to_obtain_handle"),
NAME, MessageBoxButton.OK, MessageBoxImage.Warning);

Shutdown();
return;
error = GetTranslation("t_msg_failed_to_obtain_handle");
}

Task.Run(() =>
if (error != null)
{
while (signal.WaitOne())
{
Tray.ShowApp();
}
});
MessageBox.Show(error, Program.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
Shutdown();
}

// Disable GPU to prevent flickering when adding display
RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
Expand Down
4 changes: 2 additions & 2 deletions app/Components/CustomPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void ApplyChanges(object sender, EventArgs e)
if (width < 0 || width > 7680 || height < 0 || height > 4320 || hz < 0)
{
MessageBox.Show(App.GetTranslation("t_msg_custom_invalid_slot", i / 3 + 1),
App.NAME, MessageBoxButton.OK, MessageBoxImage.Warning);
Program.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
else
Expand All @@ -66,7 +66,7 @@ private void ApplyChanges(object sender, EventArgs e)
if (Helper.RunAdminTask(args) == false)
{
MessageBox.Show(App.GetTranslation("t_msg_custom_access_denied"),
App.NAME, MessageBoxButton.OK, MessageBoxImage.Warning);
Program.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ParsecVDisplay
{
internal static class Config
{
static string REG_PATH => $"HKEY_CURRENT_USER\\SOFTWARE\\{App.NAME}";
static string REG_PATH => $"HKEY_CURRENT_USER\\SOFTWARE\\{Program.AppName}";
static string REG_STARTUP_PATH => @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

public static string Language
Expand Down Expand Up @@ -70,7 +70,7 @@ public static bool RunOnStartup
{
using (var key = Registry.CurrentUser.OpenSubKey(REG_STARTUP_PATH, false))
{
return key.GetValue(App.NAME) != null;
return key.GetValue(Program.AppName) != null;
}
}
set
Expand All @@ -80,11 +80,11 @@ public static bool RunOnStartup
if (value)
{
var exePath = Assembly.GetExecutingAssembly().Location;
key.SetValue(App.NAME, $"\"{exePath}\" -silent", RegistryValueKind.String);
key.SetValue(Program.AppName, $"\"{exePath}\" -silent", RegistryValueKind.String);
}
else
{
key.DeleteValue(App.NAME, false);
key.DeleteValue(Program.AppName, false);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="{x:Static local:App.NAME}" Click="QueryStatus">
<MenuItem Header="{x:Static local:Program.AppName}" Click="QueryStatus">
<MenuItem.Icon>
<Image Source="./Resources/icon.ico" />
</MenuItem.Icon>
Expand Down
12 changes: 6 additions & 6 deletions app/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
xAppName.Content += $" v{App.VERSION}";
xAppName.Content += $" v{Program.AppVersion}";

// prevent frame history
xFrame.Navigating += (_, e) => { e.Cancel = e.NavigationMode != NavigationMode.New; };
Expand Down Expand Up @@ -177,14 +177,14 @@ private void QueryStatus(object sender, EventArgs e)

MessageBox.Show(this, $"Parsec Virtual Display v{version}\n" +
$"{App.GetTranslation("t_msg_driver_status")}: {status}",
App.NAME, MessageBoxButton.OK, MessageBoxImage.Information);
Program.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
}

private void ExitApp(object sender, EventArgs e)
{
if (ParsecVDD.DisplayCount > 0)
if (MessageBox.Show(this, App.GetTranslation("t_msg_prompt_leave_all"),
App.NAME, MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes)
Program.AppName, MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes)
return;

Tray.Uninit();
Expand All @@ -194,7 +194,7 @@ private void ExitApp(object sender, EventArgs e)
private void OpenRepoLink(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
Helper.OpenLink($"https://github.com/{App.GITHUB_REPO}");
Helper.OpenLink($"https://github.com/{Program.GitHubRepo}");
}

private async void CheckUpdate(object sender, RoutedEventArgs e)
Expand All @@ -210,7 +210,7 @@ private async void CheckUpdate(object sender, RoutedEventArgs e)
if (!string.IsNullOrEmpty(newVersion))
{
var ret = MessageBox.Show(this, App.GetTranslation("t_msg_update_available", newVersion),
App.NAME, MessageBoxButton.YesNo, MessageBoxImage.Question);
Program.AppName, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (ret == MessageBoxResult.Yes)
{
Helper.OpenLink(Updater.DOWNLOAD_URL);
Expand All @@ -219,7 +219,7 @@ private async void CheckUpdate(object sender, RoutedEventArgs e)
else if (sender != null)
{
MessageBox.Show(this, App.GetTranslation("t_msg_up_to_date"),
App.NAME, MessageBoxButton.OK, MessageBoxImage.Information);
Program.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
}

if (menuItem != null)
Expand Down
2 changes: 1 addition & 1 deletion app/ParsecVDD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static async void UpdateRoutine(CancellationToken token)

if ((sw.ElapsedMilliseconds - start) < 100)
{
await Task.Delay(80);
await Task.Delay(50);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/ParsecVDisplay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<ApplicationManifest>Properties\app.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup>
<StartupObject>ParsecVDisplay.App</StartupObject>
<StartupObject>ParsecVDisplay.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -87,6 +87,7 @@
<Compile Include="Device.cs" />
<Compile Include="Display.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Program.cs" />
<Compile Include="Tray.cs" />
<Compile Include="ParsecVDD.cs" />
<Compile Include="Updater.cs" />
Expand Down
58 changes: 58 additions & 0 deletions app/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ParsecVDisplay
{
public static class Program
{
public const string AppId = "QpHOX8IBUHBznGtqk9xm1";
public const string AppName = "ParsecVDisplay";
public const string AppVersion = "0.45.2";
public const string GitHubRepo = "nomi-san/parsec-vdd";

[STAThread]
static int Main(string[] args)
{
if (args.Length >= 2 && args[0] == "-custom")
{
var modes = Display.ParseModes(args[1]);
ParsecVDD.SetCustomDisplayModes(modes);

if (args.Length >= 3)
{
if (Enum.TryParse<ParsecVDD.ParentGPU>(args[2], true, out var kind))
{
ParsecVDD.SetParentGPU(kind);
}
}

return 0;
}

bool isOwned = false;
var signal = new EventWaitHandle(false,
EventResetMode.AutoReset, AppId, out isOwned);

if (isOwned)
{
Task.Run(() =>
{
while (signal.WaitOne())
{
Tray.ShowApp();
}
});

App.Main();
}
else
{
signal.Set();
signal.Dispose();
}

return 0;
}
}
}
10 changes: 5 additions & 5 deletions app/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle(ParsecVDisplay.App.NAME)]
[assembly: AssemblyTitle(ParsecVDisplay.Program.AppName)]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct(ParsecVDisplay.App.NAME)]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyProduct(ParsecVDisplay.Program.AppName)]
[assembly: AssemblyCopyright("Copyright (c) 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion(ParsecVDisplay.App.VERSION)]
[assembly: AssemblyFileVersion(ParsecVDisplay.App.VERSION)]
[assembly: AssemblyVersion(ParsecVDisplay.Program.AppVersion)]
[assembly: AssemblyFileVersion(ParsecVDisplay.Program.AppVersion)]
8 changes: 4 additions & 4 deletions app/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace ParsecVDisplay
{
internal static class Updater
{
public static string DOWNLOAD_URL => $"https://github.com/{App.GITHUB_REPO}/releases/latest";
static string GH_API_URL => $"https://api.github.com/repos/{App.GITHUB_REPO}/releases/latest";
public static string DOWNLOAD_URL => $"https://github.com/{Program.GitHubRepo}/releases/latest";
static string GH_API_URL => $"https://api.github.com/repos/{Program.GitHubRepo}/releases/latest";

static Updater()
{
Expand All @@ -22,7 +22,7 @@ public static Task<string> CheckUpdate()
{
return Task.Run(async () =>
{
var localVersion = new Version(App.VERSION);
var localVersion = new Version(Program.AppVersion);
var remoteVersion = await FetchLatestVersion();
if (remoteVersion.CompareTo(localVersion) > 0)
Expand Down Expand Up @@ -59,7 +59,7 @@ static async Task<Version> FetchLatestVersion()
{
}

return new Version(App.VERSION);
return new Version(Program.AppVersion);
}

static async Task<string> DownloadString(string url)
Expand Down

0 comments on commit f93b305

Please sign in to comment.