-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (110 loc) · 4.08 KB
/
Copy pathmain.cpp
File metadata and controls
134 lines (110 loc) · 4.08 KB
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
133
134
//
// main.cpp
// Awaken
//
// Created by Marcel Dierkes on 24.12.19.
// Copyright © 2019 Marcel Dierkes. All rights reserved.
//
#include <stdlib.h>
#include <print>
#include <chrono>
#include <dispatch/dispatch.h>
#include <Awaken/Awaken.hpp>
#include <cxxopts.hpp>
void RunAwaken(std::chrono::seconds timeout, bool preventDisplaySleep, bool preventSystemSleep, std::optional<float> minimumBatteryCapacity)
{
// __block
auto awaken = Awaken::Awaken("awaken command-line tool");
awaken.setPreventUserIdleSystemSleep(preventSystemSleep);
awaken.setPreventUserIdleDisplaySleep(preventDisplaySleep);
using namespace std::chrono_literals;
awaken.setTimeout(timeout);
if(const auto capacity = minimumBatteryCapacity)
{
awaken.setMinimumBatteryCapacity(*minimumBatteryCapacity);
awaken.setMinimumBatteryCapacityReachedHandler([](float capacity) {
std::println("Minimum battery capacity reached {:.0f}", capacity);
});
}
awaken.setTimeoutHandler([]{
exit(EXIT_SUCCESS);
});
awaken.run();
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// awaken.cancel();
// });
// ------------
dispatch_main();
}
cxxopts::ParseResult ParseArguments(int argc, char* argv[])
{
try
{
cxxopts::Options options { argv[0], "awaken - prevents your Mac from going to sleep." };
options.add_options()
("d,display-sleep", "prevent the display from idle sleeping", cxxopts::value<bool>()->default_value("false"))
("s,system-sleep", "prevent the system from idle sleeping", cxxopts::value<bool>()->default_value("true"))
("t,timeout", "timeout in seconds until the sleep assertion expires", cxxopts::value<int64_t>()->default_value("0"), "N")
("b,battery-level", "a minimum battery level on devices with a built-in battery that causes the sleep assertion to expire (e.g. 20 for <= 20% remaining battery). Values above 95 are unreliable and depend on the battery health.", cxxopts::value<uint8_t>()->default_value("0"), "N")
;
options.add_options("Help")
("h,help", "print this help")
("v,version", "print version information")
;
const auto result = options.parse(argc, argv);
if(result.count("version"))
{
std::println("Awaken {}", Awaken::Awaken::version());
exit(EXIT_SUCCESS);
}
if(result.count("help"))
{
std::println("{}", options.help());
exit(EXIT_SUCCESS);
}
return result;
}
catch (const cxxopts::exceptions::exception& e)
{
std::println("Failed parsing options: {}", e.what());
exit(EXIT_FAILURE);
}
}
int main(int argc, char **argv)
{
const auto result = ParseArguments(argc, argv);
std::chrono::seconds timeout { 0 };
bool preventDisplaySleep = false;
bool preventSystemSleep = false;
std::optional<float> minimumBatteryCapacity = std::nullopt;
if(result.count("display-sleep"))
{
preventDisplaySleep = true;
}
if(result.count("system-sleep"))
{
preventSystemSleep = true;
}
if(!result.count("display-sleep") && !result.count("system-sleep"))
{
// If no parameters are given, prevent system sleep
preventSystemSleep = true;
}
if(result.count("timeout"))
{
auto customTimeout = result["timeout"].as<int64_t>();
timeout = std::chrono::seconds { customTimeout };
}
if(result.count("battery-level"))
{
auto batteryLevel = result["battery-level"].as<uint8_t>();
if(batteryLevel > 100)
{
std::println("Unsupported battery percentage '{}' provided.", batteryLevel);
exit(EXIT_FAILURE);
}
minimumBatteryCapacity = static_cast<float>(batteryLevel);
}
RunAwaken(timeout, preventDisplaySleep, preventSystemSleep, minimumBatteryCapacity);
return EXIT_SUCCESS;
}