Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
42 changes: 32 additions & 10 deletions twbl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whitespace in this PR does not match the original, tabs vs spaces.


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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline at the end of the error message.

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;
Expand Down