Initialize

This commit is contained in:
Kaan Barmore-Genç 2023-04-26 20:20:26 -04:00
commit 2d3f54a919
Signed by: kaan
GPG key ID: B2E280771CD62FCF
4 changed files with 121 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
backlight-control

4
Makefile Normal file
View file

@ -0,0 +1,4 @@
CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic -O2
backlight-control: main.c
$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)

19
README.md Normal file
View file

@ -0,0 +1,19 @@
A basic tool to increase and decrease intel backlight percent.
## Build
You'll need `make` and some C compiler installed.
```
make
```
That's it.
## Usage
Copy the `backlight-control` file to `/usr/local/bin/` or somewhere similar.
Then run `backlight-control` to get the current brightness as a percentage, or
`backlight-control +1` to increase brightness by 1 percent, or
`backlight-control -1` to decrease brightness by 1 percent. Or higher numbers
for higher percentages at a time.

97
main.c Normal file
View file

@ -0,0 +1,97 @@
#include <stdio.h>
#include <stdlib.h>
void print_usage(char *argv[])
{
fprintf(stderr, "Usage: %s <value>\n"
"\n"
"value:\n"
"\t +percent - increase brightness by percent\n"
"\t -percent - decrease brightness by percent\n",
argv[0]);
}
int read_file(const char *path)
{
FILE *file = fopen(path, "r");
int value;
int read = fscanf(file, "%d", &value);
fclose(file);
if (read != 1)
{
printf("Error reading %s\n", path);
exit(1);
}
return value;
}
int max_brightness()
{
return read_file("/sys/class/backlight/intel_backlight/max_brightness");
}
int brightness()
{
return read_file("/sys/class/backlight/intel_backlight/brightness");
}
int brightness_percent()
{
return (brightness() * 100) / max_brightness();
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("%d\n", brightness_percent());
return 0;
}
if (argc > 3)
{
print_usage(argv);
exit(2);
}
int positive = 0;
if (argv[1][0] == '-')
{
positive = 0;
}
else if (argv[1][0] == '+')
{
positive = 1;
}
else
{
print_usage(argv);
exit(2);
}
int percent;
char *number_str = argv[1] + 1;
int read = sscanf(number_str, "%d", &percent);
if (read != 1)
{
print_usage(argv);
exit(3);
}
int current = brightness();
int max = max_brightness();
int change = (max * percent) / 100 * (positive ? 1 : -1);
FILE *file = fopen("/sys/class/backlight/intel_backlight/brightness", "w");
int set_to = current + change;
// clamp
if (set_to > max)
{
set_to = max;
}
if (set_to < 0)
{
set_to = 1;
}
fprintf(file, "%d", set_to);
}