Example #1
0
        public static StartUpParams Parse(IEnumerable <string> args)
        {
            var settings = new StartUpParams();

            foreach (var arg in args)
            {
                switch (arg.ToLowerInvariant())
                {
                case RunningFromScheduledTaskParam:
                {
                    settings.RunningFromScheduledTask = true;
                    break;
                }

                case ActivateChromeFontFixParam:
                {
                    settings.ActivateChromeFontFix = true;
                    break;
                }

                case DeactivateChromeFontFixParam:
                {
                    settings.DeactivateChromeFontFix = true;
                    break;
                }
                }
            }
            return(settings);
        }
Example #2
0
        static void Main(string[] args)
        {
            var currentDomain = AppDomain.CurrentDomain;

            // Handler for unhandled exceptions.
            currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
            // Handler for exceptions in threads behind forms.
            Application.ThreadException += GlobalThreadExceptionHandler;

            var startUpParams = StartUpParams.Parse(args);

            if (startUpParams.ActivateChromeFontFix || startUpParams.DeactivateChromeFontFix)
            {
                Utils.InstallChromeFix(startUpParams.ActivateChromeFontFix);
                return;
            }

            string mutexId = $"Global\\{typeof(MainForm).GUID}";
            bool   mutexCreated;
            var    mutex = new Mutex(true, mutexId, out mutexCreated);

            try
            {
                if (!mutexCreated)
                {
                    MessageBox.Show("Only one instance of this program can be active.", "ColorControl");
                }
                else
                {
                    try
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new MainForm(startUpParams));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error while initializing application: " + ex.ToLogString(Environment.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                if (mutexCreated)
                {
                    mutex.Dispose();
                }
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var currentDomain = AppDomain.CurrentDomain;

            // Handler for unhandled exceptions.
            currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
            // Handler for exceptions in threads behind forms.
            Application.ThreadException += GlobalThreadExceptionHandler;

            DataDir = Utils.GetDataPath();
            InitLogger();

            LoadConfig();

            if (Config.UseGdiScaling)
            {
                Application.SetHighDpiMode(HighDpiMode.DpiUnawareGdiScaled);
            }

            var startUpParams = StartUpParams.Parse(args);

            AppContext = new AppContext(Config, startUpParams);

            var existingProcess = Utils.GetProcessByName("ColorControl");

            try
            {
                if (HandleStartupParams(startUpParams, existingProcess))
                {
                    return;
                }
            }
            finally
            {
                Utils.CloseConsole();
            }

            string mutexId = $"Global\\{typeof(MainForm).GUID}";
            var    mutex   = new Mutex(true, mutexId, out var mutexCreated);

            try
            {
                if (!mutexCreated)
                {
                    if (existingProcess != null && existingProcess.Threads.Count > 0)
                    {
                        var thread = existingProcess.Threads[0];
                        NativeMethods.EnumThreadWindows((uint)thread.Id, EnumThreadWindows, IntPtr.Zero);

                        return;
                    }

                    MessageBox.Show("Only one instance of this program can be active.", "ColorControl");
                }
                else
                {
                    try
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        Application.Run(new MainForm(AppContext));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error while initializing application: " + ex.ToLogString(Environment.StackTrace), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                if (mutexCreated)
                {
                    mutex.Dispose();
                }
            }
        }
Example #4
0
        private static bool HandleStartupParams(StartUpParams startUpParams, Process existingProcess)
        {
            if (startUpParams.ActivateChromeFontFix || startUpParams.DeactivateChromeFontFix)
            {
                Utils.InstallChromeFix(startUpParams.ActivateChromeFontFix);
                return(true);
            }
            if (startUpParams.EnableAutoStart || startUpParams.DisableAutoStart)
            {
                Utils.RegisterTask(TS_TASKNAME, startUpParams.EnableAutoStart);
                return(true);
            }

            var useConsole = startUpParams.NoGui || existingProcess != null;

            if (!useConsole)
            {
                return(false);
            }

            startUpParams.NoGui = true;

            var result = false;

            if (startUpParams.ExecuteHelp)
            {
                Utils.OpenConsole();

                Console.WriteLine("\nColorControl CLI");
                Console.WriteLine("Syntax  : ColorControl command options");
                Console.WriteLine("Commands:");
                Console.WriteLine("--nvpreset  <preset name or id>: execute NVIDIA-preset");
                Console.WriteLine("--amdpreset <preset name or id>: execute AMD-preset");
                Console.WriteLine("--lgpreset  <preset name>      : execute LG-preset");
                Console.WriteLine("--help                         : displays this help info");
                Console.WriteLine("Options:");
                Console.WriteLine("--nogui: starts command from the command line and will not open GUI (is forced when GUI is already running)");

                result = true;
            }

            if (startUpParams.ExecuteLgPreset)
            {
                Utils.OpenConsole();

                Console.WriteLine($"Executing LG-preset '{startUpParams.LgPresetName}'...");
                var task = LgService.ExecutePresetAsync(startUpParams.LgPresetName);

                var taskResult = Utils.WaitForTask(task);

                if (taskResult)
                {
                    Console.WriteLine("Done.");
                }

                result = true;
            }

            if (startUpParams.ExecuteNvidiaPreset)
            {
                Utils.OpenConsole();

                Console.WriteLine($"Executing NVIDIA-preset '{startUpParams.NvidiaPresetIdOrName}'...");
                var taskResult = NvService.ExecutePresetAsync(startUpParams.NvidiaPresetIdOrName);

                if (taskResult)
                {
                    Console.WriteLine("Done.");
                }

                result = true;
            }

            if (startUpParams.ExecuteAmdPreset)
            {
                Utils.OpenConsole();

                Console.WriteLine($"Executing AMD-preset '{startUpParams.AmdPresetIdOrName}'...");
                var taskResult = AmdService.ExecutePresetAsync(startUpParams.AmdPresetIdOrName);

                if (taskResult)
                {
                    Console.WriteLine("Done.");
                }

                result = true;
            }

            return(result);
        }
Example #5
0
        public static StartUpParams Parse(IEnumerable <string> args)
        {
            var settings       = new StartUpParams();
            var parseNameParam = string.Empty;

            foreach (var arg in args)
            {
                if (!string.IsNullOrEmpty(parseNameParam))
                {
                    switch (parseNameParam)
                    {
                    case ExecuteNvidiaPresetParam:
                    {
                        settings.NvidiaPresetIdOrName = arg;
                        break;
                    }

                    case ExecuteAmdPresetParam:
                    {
                        settings.AmdPresetIdOrName = arg;
                        break;
                    }

                    case ExecuteLgPresetParam:
                    {
                        settings.LgPresetName = arg;
                        break;
                    }
                    }

                    parseNameParam = string.Empty;
                    continue;
                }

                switch (arg.ToLowerInvariant())
                {
                case RunningFromScheduledTaskParam:
                {
                    settings.RunningFromScheduledTask = true;
                    break;
                }

                case ActivateChromeFontFixParam:
                {
                    settings.ActivateChromeFontFix = true;
                    break;
                }

                case DeactivateChromeFontFixParam:
                {
                    settings.DeactivateChromeFontFix = true;
                    break;
                }

                case ExecuteHelpParam:
                {
                    settings.ExecuteHelp = true;
                    settings.NoGui       = true;
                    break;
                }

                case ExecuteNvidiaPresetParam:
                {
                    settings.ExecuteNvidiaPreset = true;
                    parseNameParam = ExecuteNvidiaPresetParam;
                    break;
                }

                case ExecuteAmdPresetParam:
                {
                    settings.ExecuteAmdPreset = true;
                    parseNameParam            = ExecuteAmdPresetParam;
                    break;
                }

                case ExecuteLgPresetParam:
                {
                    settings.ExecuteLgPreset = true;
                    parseNameParam           = ExecuteLgPresetParam;
                    break;
                }

                case NoGuiParam:
                {
                    settings.NoGui = true;
                    break;
                }

                case EnableAutoStartParam:
                {
                    settings.EnableAutoStart = true;
                    break;
                }

                case DisableAutoStartParam:
                {
                    settings.DisableAutoStart = true;
                    break;
                }
                }
            }
            return(settings);
        }