Skip to content

Instantly share code, notes, and snippets.

@bigdavedev
Last active November 2, 2016 19:47
Show Gist options
  • Save bigdavedev/7b2a398d700660703add837e5f08ee89 to your computer and use it in GitHub Desktop.
Save bigdavedev/7b2a398d700660703add837e5f08ee89 to your computer and use it in GitHub Desktop.
Simple toy program to set the brightness on my laptop. Mostly for gaining a little exposure to the filesystem library
#include <experimental/filesystem>
#include <fstream>
#include <string>
namespace fs = std::experimental::filesystem;
using namespace std::string_literals;
fs::path const get_backlight_dir(std::string const& path)
{
auto const iterator = fs::directory_iterator{fs::path{path}};
return iterator->path();
}
int const get_max_brightness(fs::path const& backlight_dir)
{
auto const max_backlight_path = backlight_dir / "max_brightness"s;
std::ifstream max_brightness_file{max_backlight_path};
int max_brightness{};
max_brightness_file >> max_brightness;
return max_brightness;
}
void set_brightness(fs::path const& backlight_dir, int const level)
{
std::ofstream{backlight_dir / "brightness"s} << std::to_string(level);
}
int main(int argc, char* argv[])
{
auto const percentage = [argc, argv] {
return (argc == 2) ? std::min(std::stod(argv[1]) / 100.0, 1.0) : 1.0;
}();
auto const backlight_dir = get_backlight_dir("/sys/class/backlight/"s);
auto const max_brightness = get_max_brightness(backlight_dir);
set_brightness(backlight_dir, static_cast<int>(max_brightness * percentage));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment