From ed17b5eb3e86d018e51565198df33f0ea25ed28c Mon Sep 17 00:00:00 2001 From: Enzo Matsumiya Date: Fri, 24 May 2019 17:17:52 -0300 Subject: [PATCH] Limit to max_brightness, some clean up, add some TODOs - Although I don't know the behaviour if value goes above max_brightness, doesn't hurt to check. - Changed some variable names for better understanding :P - Add some TODOs to README and code --- README.md | 12 +++++++++--- twbl.cpp | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d3b44b7..1353d27 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,13 @@ Had some trouble getting xbacklight to work on openSUSE Tumbleweed, so I wrote t simple program that should work around it. Requires root, a workaround can be found at [stackoverflow](https://unix.stackexchange.com/a/248495). -The value of the `fname` variable might need to be changed to the correct path -to your backlight brightness file. +The values of the `fname` and `max_fname` variables might need to be changed to the correct path +to your backlight brightness files. ### Compilation: ``` -g++ -std=c++11 twbl.cpp +g++ -std=c++11 -o twbl twbl.cpp ``` ### Usage (i3 config): @@ -19,3 +19,9 @@ g++ -std=c++11 twbl.cpp bindsym XF86MonBrightnessUp exec sudo /home/dzejrou/bin/twbl +100 # increase screen brightness bindsym XF86MonBrightnessDown exec sudo /home/dzejrou/bin/twbl -100 # decrease screen brightness ``` + +### TODO + +- Create a Makefile +- Find a way to not need sudo +- (RFC) Maybe change granularity of increment/decrement (e.g. "./twbl +1" makes no difference at all, up until 1000) diff --git a/twbl.cpp b/twbl.cpp index e4b8084..b74dc35 100644 --- a/twbl.cpp +++ b/twbl.cpp @@ -7,37 +7,59 @@ int main(int argc, char** argv) { /** * Edit here with the proper path to your backlight. + * TODO: save these strings in a file maybe? */ std::string fname{"/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/brightness"}; + std::string max_fname{"/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/max_brightness"}; if (argc < 2) { - std::cerr << "Usage: " << argv[0] << " {,+,-}N\n"; + std::cerr << "Usage: " << argv[0] << " {,+,-}N\n\n"; return 1; } - std::istringstream iss{argv[1]}; + std::istringstream read_val{argv[1]}; int val{}; - if (!(iss >> val)) + if (!(read_val >> val)) { std::cerr << "Argument " << argv[1] << " is not a valid number.\n"; return 1; } - std::ifstream ifs{fname}; + std::ifstream read_current{fname}; + int current_value{}; - int curr{}; - if (!(ifs >> curr)) + if (!(read_current >> current_value)) { std::cerr << "Could not read current brightness value.\n"; return 1; } - ifs.close(); - val += curr; - std::ofstream ofs{fname}; - if (!(ofs << std::to_string(val))) + read_current.close(); + + std::ifstream read_max{max_fname}; + int max_value{}; + + if (!(read_max >> max_value)) + { + std::cerr << "Could not read max brightness value.\n"; + return 1; + } + + read_max.close(); + + if ((current_value + val) > max_value) + { + std::cerr << "Invalid input value: " << val << " is greater than max value " << max_value; + return 1; + } + + val += current_value; + + std::ofstream write_value{fname}; + + if (!(write_value << std::to_string(val))) { std::cerr << "Could not write new brightness value.\n"; return 1;