/
brightctl.c
executable file
·65 lines (57 loc) · 1.29 KB
/
brightctl.c
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
#include <stdio.h>
#include <string.h>
const char *CTL_FILE = "/sys/class/backlight/acpi_video0/brightness";
const char *MAX_FILE = "/sys/class/backlight/acpi_video0/max_brightness";
void show_help()
{
printf("Usage: brightctl up/down\n");
}
int cat(const char *file_path)
{
FILE *fl = fopen(file_path, "r");
int num;
fscanf(fl, "%d", &num);
fclose(fl);
return num;
}
void set_brightness(int num)
{
FILE *fl = fopen(CTL_FILE, "w");
fprintf(fl, "%d", num);
fclose(fl);
}
int main(int argc, char *argv[])
{
int bright = -1;
switch(argc)
{
case 2:
{
int max = cat(MAX_FILE);
int current = cat(CTL_FILE);
printf("brightness: max %d, current %d", max, current);
const char *method = argv[1];
if (strcmp(method, "up") == 0 && current < max)
{
bright = current + 1;
}
else if(strcmp(method, "down") == 0 && current > 0)
{
bright = current - 1;
}
break;
}
default:
{
show_help();
return -1;
}
}
if(bright > -1)
{
printf(", set %d", bright);
set_brightness(bright);
}
printf("\n");
return 0;
}