2021-04-08 15:12:09 -07:00
using Espresso.Shell.Core ;
using System ;
using System.CommandLine ;
using System.CommandLine.Invocation ;
2021-04-20 07:54:03 -07:00
using System.IO ;
2021-04-08 15:12:09 -07:00
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-20 07:54:03 -07:00
var configOption = new Option < string > (
aliases : new [ ] { "--config" , "-c" } ,
getDefaultValue : ( ) = > string . Empty ,
description : "Pointer to a PowerToys configuration file that the tool will be watching for changes. All other options are disregarded if config is used." )
{
Argument = new Argument < string > ( ( ) = > string . Empty )
{
Arity = ArgumentArity . ZeroOrOne ,
} ,
} ;
configOption . Required = false ;
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
{
2021-04-20 07:54:03 -07:00
configOption ,
2021-04-08 16:09:59 -07:00
displayOption ,
timeOption
2021-04-08 15:12:09 -07:00
} ;
rootCommand . Description = appName ;
2021-04-20 07:54:03 -07:00
rootCommand . Handler = CommandHandler . Create < string , bool , long > ( HandleCommandLineArguments ) ;
2021-04-08 15:12:09 -07:00
return rootCommand . InvokeAsync ( args ) . Result ;
}
2021-04-20 07:54:03 -07:00
private static void HandleCommandLineArguments ( string config , 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-20 07:54:03 -07:00
if ( ! string . IsNullOrWhiteSpace ( config ) )
2021-04-08 15:12:09 -07:00
{
2021-04-20 07:54:03 -07:00
// Configuration file is used, therefore we disregard any other command-line parameter
// and instead watch for changes in the file.
FileSystemWatcher watcher = new FileSystemWatcher
2021-04-08 15:12:09 -07:00
{
2021-04-20 07:54:03 -07:00
Path = Path . GetDirectoryName ( config ) ,
EnableRaisingEvents = true ,
NotifyFilter = NotifyFilters . LastWrite ,
Filter = Path . GetFileName ( config )
} ;
watcher . Changed + = new FileSystemEventHandler ( HandleEspressoConfigChange ) ;
2021-04-08 15:12:09 -07:00
}
2021-04-08 16:30:56 -07:00
else
{
2021-04-20 07:54:03 -07:00
if ( timeLimit < = 0 )
2021-04-08 16:30:56 -07:00
{
2021-04-20 07:54:03 -07:00
// Indefinite keep awake.
bool success = APIHelper . SetIndefiniteKeepAwake ( displayOn ) ;
if ( success )
{
Console . WriteLine ( $"Currently in indefinite keep awake. Display always on: {displayOn}" ) ;
}
else
{
Console . WriteLine ( "Could not set up the state to be indefinite keep awake." ) ;
}
2021-04-08 16:30:56 -07:00
}
else
{
2021-04-20 07:54:03 -07:00
// 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 16:30:56 -07:00
}
}
2021-04-08 15:12:09 -07:00
new ManualResetEvent ( false ) . WaitOne ( ) ;
}
2021-04-20 07:54:03 -07:00
private static void HandleEspressoConfigChange ( object sender , FileSystemEventArgs e )
{
Console . WriteLine ( "Reached test!" ) ;
}
2021-04-08 15:12:09 -07:00
}
}