2021-04-08 15:12:09 -07:00
|
|
|
|
using Espresso.Shell.Core;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.CommandLine;
|
|
|
|
|
|
using System.CommandLine.Invocation;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Espresso.Shell
|
|
|
|
|
|
{
|
|
|
|
|
|
class Program
|
|
|
|
|
|
{
|
|
|
|
|
|
private static Mutex mutex = null;
|
|
|
|
|
|
private const string appName = "Espresso";
|
|
|
|
|
|
|
|
|
|
|
|
static int Main(string[] args)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool instantiated;
|
|
|
|
|
|
mutex = new Mutex(true, appName, out instantiated);
|
|
|
|
|
|
|
|
|
|
|
|
if (!instantiated)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(appName + " is already running! Exiting the application.");
|
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
|
Environment.Exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Espresso - Computer Caffeination Engine");
|
|
|
|
|
|
|
2021-04-08 16:09:59 -07:00
|
|
|
|
var displayOption = new Option<bool>(
|
|
|
|
|
|
aliases: new[] { "--display-on", "-d" },
|
|
|
|
|
|
getDefaultValue: () => true,
|
|
|
|
|
|
description: "Determines whether the display should be kept awake.")
|
2021-04-08 15:12:09 -07:00
|
|
|
|
{
|
2021-04-08 16:09:59 -07:00
|
|
|
|
Argument = new Argument<bool>(() => false)
|
|
|
|
|
|
{
|
|
|
|
|
|
Arity = ArgumentArity.ZeroOrOne,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
displayOption.Required = false;
|
|
|
|
|
|
|
|
|
|
|
|
var timeOption = new Option<long>(
|
|
|
|
|
|
aliases: new[] { "--time-limit", "-t" },
|
2021-04-08 15:12:09 -07:00
|
|
|
|
getDefaultValue: () => 0,
|
|
|
|
|
|
description: "Determines the interval, in seconds, during which the computer is kept awake.")
|
2021-04-08 16:09:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
Argument = new Argument<long>(() => 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Arity = ArgumentArity.ExactlyOne,
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
timeOption.Required = false;
|
|
|
|
|
|
|
|
|
|
|
|
var rootCommand = new RootCommand
|
|
|
|
|
|
{
|
|
|
|
|
|
displayOption,
|
|
|
|
|
|
timeOption
|
2021-04-08 15:12:09 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
rootCommand.Description = appName;
|
|
|
|
|
|
|
|
|
|
|
|
rootCommand.Handler = CommandHandler.Create<bool, long>(HandleCommandLineArguments);
|
|
|
|
|
|
|
|
|
|
|
|
return rootCommand.InvokeAsync(args).Result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-08 16:09:59 -07:00
|
|
|
|
private static void HandleCommandLineArguments(bool displayOn, long timeLimit)
|
2021-04-08 15:12:09 -07:00
|
|
|
|
{
|
2021-04-08 16:09:59 -07:00
|
|
|
|
Console.WriteLine($"The value for --display-on is: {displayOn}");
|
|
|
|
|
|
Console.WriteLine($"The value for --time-limit is: {timeLimit}");
|
2021-04-08 15:12:09 -07:00
|
|
|
|
|
2021-04-08 16:09:59 -07:00
|
|
|
|
if (timeLimit <= 0)
|
2021-04-08 15:12:09 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Indefinite keep awake.
|
2021-04-08 16:09:59 -07:00
|
|
|
|
bool success = APIHelper.SetIndefiniteKeepAwake(displayOn);
|
2021-04-08 15:12:09 -07:00
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
2021-04-08 16:09:59 -07:00
|
|
|
|
Console.WriteLine($"Currently in indefinite keep awake. Display always on: {displayOn}");
|
2021-04-08 15:12:09 -07:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Could not set up the state to be indefinite keep awake.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-08 16:30:56 -07:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Timed keep-awake.
|
|
|
|
|
|
bool success = APIHelper.SetTimedKeepAwake(timeLimit, displayOn);
|
|
|
|
|
|
if (success)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"Finished execution of timed keep-awake.");
|
|
|
|
|
|
|
|
|
|
|
|
// Because the timed keep-awake execution completed, there is no reason for
|
|
|
|
|
|
// Espresso to stay alive - I will just shut down the application until it's
|
|
|
|
|
|
// launched again by the user.
|
|
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Could not set up the state to be timed keep awake.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-04-08 15:12:09 -07:00
|
|
|
|
|
|
|
|
|
|
new ManualResetEvent(false).WaitOne();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|