From 42a99e775b14abcba56af5fb7d44cdd72a574b38 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 8 Nov 2018 16:53:27 -0600 Subject: [PATCH] Allow launching on startup. If this is used to play GIF animations on the pad, being able to have it launch on startup is useful. This is disabled by default and there's no UI to enable it yet. --- smx-config/App.config | 10 +++ smx-config/App.xaml.cs | 12 ++++ smx-config/Helpers.cs | 73 ++++++++++++++++++++-- smx-config/Properties/Settings.Designer.cs | 12 ++++ smx-config/Properties/Settings.settings | 8 ++- 5 files changed, 109 insertions(+), 6 deletions(-) diff --git a/smx-config/App.config b/smx-config/App.config index 89464e6..fba0990 100644 --- a/smx-config/App.config +++ b/smx-config/App.config @@ -1,8 +1,18 @@  + +
+ + + + + False + + + diff --git a/smx-config/App.xaml.cs b/smx-config/App.xaml.cs index 6c3ad6d..fdf1bd2 100644 --- a/smx-config/App.xaml.cs +++ b/smx-config/App.xaml.cs @@ -24,6 +24,18 @@ namespace smx_config { base.OnStartup(e); + // If we're being launched on startup, but the LaunchOnStartup setting is false, + // then the user turned off auto-launching but we're still being launched for some + // reason (eg. a renamed launch shortcut that we couldn't find to remove). As + // a safety so we don't launch when the user doesn't want us to, just exit in this + // case. + if(Helpers.LaunchedOnStartup() && !LaunchOnStartup.Enable) + { + Shutdown(); + return; + } + + LaunchOnStartup.Enable = true; if(!SMX.SMX.DLLExists()) { MessageBox.Show("SMXConfig encountered an unexpected error.\n\nSMX.dll couldn't be found:\n\n" + Helpers.GetLastWin32ErrorString(), "SMXConfig"); diff --git a/smx-config/Helpers.cs b/smx-config/Helpers.cs index 3e4060a..1a54393 100644 --- a/smx-config/Helpers.cs +++ b/smx-config/Helpers.cs @@ -75,17 +75,29 @@ namespace smx_config static class Helpers { - // Return true if we're in debug mode. - public static bool GetDebug() + // Return true if arg is in the commandline. + public static bool HasCommandlineArgument(string arg) { - foreach(string arg in Environment.GetCommandLineArgs()) + foreach(string s in Environment.GetCommandLineArgs()) { - if(arg == "-d") + if(s == arg) return true; } return false; } + // Return true if we're in debug mode. + public static bool GetDebug() + { + return HasCommandlineArgument("-d"); + } + + // Return true if we were launched on startup. + public static bool LaunchedOnStartup() + { + return HasCommandlineArgument("-s"); + } + // Return the last Win32 error as a string. public static string GetLastWin32ErrorString() { @@ -332,6 +344,19 @@ namespace smx_config string error; SMX.SMX.LightsAnimation_Load(gif, pad, type, out error); } + + // Create a .lnk. + public static void CreateShortcut(string outputFile, string targetPath, string arguments) + { + Type shellType = Type.GetTypeFromProgID("WScript.Shell"); + dynamic shell = Activator.CreateInstance(shellType); + dynamic shortcut = shell.CreateShortcut(outputFile); + + shortcut.TargetPath = targetPath; + shortcut.Arguments = arguments; + shortcut.WindowStyle = 0; + shortcut.Save(); + } } // This class just makes it easier to assemble binary command packets. @@ -368,6 +393,46 @@ namespace smx_config private LinkedList parts = new LinkedList(); }; + // Manage launching on startup. + static class LaunchOnStartup + { + public static string GetLaunchShortcutFilename() + { + string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup); + return startupFolder + "/StepManiaX.lnk"; + } + + // Enable or disable launching on startup. + public static bool Enable + { + get { + return Properties.Settings.Default.LaunchOnStartup; + } + + set { + // Remember whether we want to be launched on startup. This is used as a sanity + // check in case we're not able to remove our launch shortcut. + Properties.Settings.Default.LaunchOnStartup = value; + Properties.Settings.Default.Save(); + + string shortcutFilename = GetLaunchShortcutFilename(); + if(value) + { + string filename = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; + Helpers.CreateShortcut(shortcutFilename, filename, "-s"); + } else { + + try { + System.IO.File.Delete(shortcutFilename); + } catch { + // If there's an error deleting the shortcut (most likely it doesn't exist), + // don't do anything. + } + } + } + } + }; + // When enabled, periodically set all lights to the current auto-lighting color. This // is enabled while manipulating the step color slider. class ShowAutoLightsColor diff --git a/smx-config/Properties/Settings.Designer.cs b/smx-config/Properties/Settings.Designer.cs index de27821..6a30787 100644 --- a/smx-config/Properties/Settings.Designer.cs +++ b/smx-config/Properties/Settings.Designer.cs @@ -22,5 +22,17 @@ namespace smx_config.Properties { return defaultInstance; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool LaunchOnStartup { + get { + return ((bool)(this["LaunchOnStartup"])); + } + set { + this["LaunchOnStartup"] = value; + } + } } } diff --git a/smx-config/Properties/Settings.settings b/smx-config/Properties/Settings.settings index 8e615f2..2d633bc 100644 --- a/smx-config/Properties/Settings.settings +++ b/smx-config/Properties/Settings.settings @@ -1,5 +1,9 @@  - + - + + + False + + \ No newline at end of file