-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAwake.cpp
132 lines (105 loc) · 4.99 KB
/
Awake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
////////////////////////////////////////////////////////////////////////////////////
/// Awake ///
/// by Noah Everett ///
/// ///
/// Awake is an application to keep your Mac awake while in clamshell mode. ///
/// For more information, please visit https://github.com/Noah-Everett/Awake. ///
////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <time.h>
#include "CmdLnArgParser.hpp"
#include "Messenger.hpp"
using std::cout;
using std::string;
using std::vector;
using Base::CmdLnArgParser::parser;
using Base::Messenger::Verbosity::fatal_int;
using Base::Messenger::Verbosity::info_int;
using Base::Messenger::messenger_c;
// Function templates
string exec( string cmd );
bool isCharging();
// Constant messages
const string k_welcome = " ----------------------- \n"
"| Awake by Noah Everett |\n"
" ----------------------- \n ";
const string k_help = "Program Usage (<> denotes required arguments, [] denotes optional arguments):\n"
" awake [-h -> display usage statement]\n"
" [-p <password> -> system password for sudo]\n"
"Other Information\n"
" You must use either the -p <password> or set the PASSWORD environmental variable\n"
" If you encounter \"zsh: segmentation fault awake\", make sure the PASSWORD environmental variable is set.";
// Global console messenger
namespace Base::Messenger {
messenger messenger_c( cout, k_welcome, LOCATION );
}
// Possible arguments
vector< string > optsWArg_req = {};
vector< string > optsWArg_opt = { "-p" };
vector< string > optsNArg_req = {};
vector< string > optsNArg_opt = { "-h", "--help" };
// User given arguments
string password;
int main( int argc, char** argv )
{
///////////////////////////////////////////////////////////////////////////////
/// Command Line Arguments ///
///////////////////////////////////////////////////////////////////////////////
parser parser( argc, argv, optsWArg_req, optsWArg_opt, optsNArg_req, optsNArg_opt );
if( parser.hasOpt( "-h" ) || parser.hasOpt( "--help" ) ) {
messenger_c( fatal_int, LOCATION ) << '\n' << k_help;
return 0;
}
if( parser.hasOpt( "-p" ) )
password = parser.getArg_string( "-p" );
else
password = getenv( "PASSWORD" );
///////////////////////////////////////////////////////////////////////////////
/// Main Method ///
///////////////////////////////////////////////////////////////////////////////
messenger_c( info_int, LOCATION ) << "Turning sleep off with: `sudo pmset -b sleep 0 && "
"sudo pmset -a hibernatemode 0 && sudo pmset -a disablesleep 1`";
exec( "echo " + password + " | sudo -S pmset -a sleep 0 2>/dev/null && sudo pmset -a hibernatemode 0 2>/dev/null && "
"sudo pmset -a disablesleep 1 2>/dev/null" );
messenger_c( info_int, LOCATION ) << "Program will terminate when computer stops sharging.";
time_t start, cur;
time( &start );
time( &cur );
while( isCharging() && int( cur - start ) < 300 ) {
time( &cur );
}
messenger_c( info_int, LOCATION ) << "Charging stopped.";
messenger_c( info_int, LOCATION ) << "Turning sleep on with: `sudo pmset -b sleep 1 && "
"sudo pmset -a hibernatemode 3 && "
"sudo pmset -a disablesleep 0`";
exec( "echo " + password + " | sudo -S pmset -a sleep 1 2>/dev/null && "
"sudo pmset -a hibernatemode 3 2>/dev/null && "
"sudo pmset -a disablesleep 0 2>/dev/null" );
///////////////////////////////////////////////////////////////////////////////
/// End Program ///
///////////////////////////////////////////////////////////////////////////////
messenger_c( info_int, LOCATION ) << "Program ran successfully. Bye bye!";
return 0;
}
// Function declarations
string exec( string cmd ) {
char buffer[ 256 ];
string out = "";
FILE* pipe = popen( cmd.c_str(), "r" );
if( !pipe )
return "\0";
while( !feof( pipe ) )
if( fgets( buffer, 256, pipe ) != NULL )
out += buffer;
pclose( pipe );
return out;
}
bool isCharging() {
string out = exec( "echo " + password + "| sudo -S pmset -g ps 2>/dev/null" );
if( out.find( "discharging" ) != string::npos )
return false;
return true;
}