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
66
67
68
69
70
71
72
73
74
75
76
|
// By Tom Smeding (2016)
// modified for new laptop (2017)
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#define INCREMENT 100
__attribute__((noreturn))
__attribute__((format (printf, 1, 2)))
void die(const char *format,...){
va_list ap;
va_start(ap,format);
vprintf(format,ap);
va_end(ap);
putchar('\n');
exit(1);
}
int get_max_brightness(void){
FILE *f=fopen("/sys/class/backlight/intel_backlight/max_brightness","r");
if(!f)die("Could not open max_brightness file");
int n;
fscanf(f,"%d",&n);
fclose(f);
return n;
}
int get_brightness(void){
FILE *f=fopen("/sys/class/backlight/intel_backlight/brightness","r");
if(!f)die("Could not open brightness file");
int n;
fscanf(f,"%d",&n);
fclose(f);
return n;
}
void set_brightness(int n){
FILE *f=fopen("/sys/class/backlight/intel_backlight/brightness","w");
if(!f)die("Could not open brightness file");
fprintf(f,"%d",n);
fflush(f);
fclose(f);
}
int main(int argc,char **argv){
uid_t uid=geteuid();
if(uid!=0){
printf("brightness will only work with root.\n");
return 1;
}
if(argc==1){
printf("max: %d\n",get_max_brightness());
printf("current: %d\n",get_brightness());
} else if(argc==2){
int n;
if(strcmp(argv[1],"-")==0){
n=get_brightness()-INCREMENT;
if(n<0)n=0;
} else if(strcmp(argv[1],"+")==0){
n=get_brightness()+INCREMENT;
int mx=get_max_brightness();
if(n>mx)n=mx;
} else {
char *endp;
n=strtol(argv[1],&endp,10);
if(argv[1][0]=='\0'||*endp!='\0')die("Invalid number '%s'",argv[1]);
}
set_brightness(n);
}
}
|