-
Notifications
You must be signed in to change notification settings - Fork 0
/
backlightctl.cpp
74 lines (54 loc) · 2 KB
/
backlightctl.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
#include <boost/program_options.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <streambuf>
#include <sstream>
#include <vector>
namespace {
std::string read_file_contents(const std::string &filename) {
std::ifstream file(filename);
return { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() };
}
template<typename T>
void write_file_contents(const std::string &filename, const T &contents) {
std::ofstream file(filename);
file << contents;
}
} /* namespace <anonymous> */
int main(int argc, char *argv[]) {
namespace po = boost::program_options;
const std::string dirname = "/sys/class/backlight/intel_backlight";
unsigned int max_brightness;
unsigned int brightness;
double current_ratio;
int delta;
po::options_description desc("Options");
desc.add_options()
("inc,i", po::value<std::vector<unsigned int>>()->composing()
->notifier([&delta](const auto &vs) { for(auto v: vs) delta += v; }), "increase brightness by percentage")
("dec,d", po::value<std::vector<unsigned int>>()->composing()
->notifier([&delta](const auto &vs) { for(auto v: vs) delta -= v; }), "decrease brightness by percentage")
("print,p", "print the brightness")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
{
std::istringstream brstr(read_file_contents(dirname + "/brightness"));
brstr >> brightness;
}
{
std::istringstream mbrstr(read_file_contents(dirname + "/max_brightness"));
mbrstr >> max_brightness;
}
current_ratio = static_cast<double>(brightness) / max_brightness;
double new_ratio = current_ratio + static_cast<double>(delta) / 100;
if(new_ratio < 0.0) new_ratio = 0.0;
else if(new_ratio > 1.0) new_ratio = 1.0;
brightness = static_cast<int>(new_ratio * max_brightness);
write_file_contents(dirname + "/brightness", brightness);
if(vm.find("print") != vm.end()) {
std::cout << "brightness: " << brightness << '/' << max_brightness << " (" << static_cast<unsigned int>(new_ratio * 100) << "%)\n";
}
}