-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
124 lines (103 loc) · 3.15 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#define CURRENT_BRIGHTNESS_FILE "/sys/class/backlight/intel_backlight/brightness"
#define MAX_BRIGHTNESS_FILE "/sys/class/backlight/intel_backlight/max_brightness"
#define BUFFER_SIZE 10
#define NB_STEP 20
char buffer[BUFFER_SIZE];
int maxBrightness() {
FILE* max_brightness_file = fopen(MAX_BRIGHTNESS_FILE, "r");
if(max_brightness_file == NULL) {
perror("Cannot open brightness file: ");
return -1;
}
size_t nb_read_bytes = fread(buffer, 1, BUFFER_SIZE - 1, max_brightness_file);
fclose(max_brightness_file);
if(nb_read_bytes > 0) {
return atoi(buffer);
} else {
perror("Cannot read max brightness file: ");
return -1;
}
}
void printUsage() {
printf("Usage : backlight_mod {++ or --}\n");
}
int main(int argc, char* argv[]) {
FILE* current_brightness_file;
size_t nb_bytes;
int current_brightness, new_brightness, step_brightness, max_brightness;
bool increment, verbose = false;
if(argc != 2 && argc != 3) {
printUsage(argv[0]);
exit(EXIT_SUCCESS);
} else {
if(strncmp(argv[1], "++", 2) == 0) {
increment = true;
}
else if(strncmp(argv[1], "--", 2) == 0) {
increment = false;
}
else {
printUsage();
exit(EXIT_SUCCESS);
}
if(argc == 3) {
if(strncmp(argv[2], "-v", 2) == 0) {
verbose = true;
} else {
printUsage(argv[0]);
exit(EXIT_SUCCESS);
}
}
}
if(seteuid(0)) {
perror("Unable to use seteuid(0): ");
exit(EXIT_FAILURE);
}
memset(buffer, '\0', BUFFER_SIZE);
max_brightness = maxBrightness();
if (max_brightness < 1) {
exit(EXIT_FAILURE);
}
current_brightness_file = fopen(CURRENT_BRIGHTNESS_FILE, "a+");
if(current_brightness_file == NULL) {
perror("Cannot open brightness file: ");
exit(EXIT_FAILURE);
}
nb_bytes = fread(buffer, 1, BUFFER_SIZE - 1, current_brightness_file);
if(nb_bytes > 0) {
current_brightness = atoi(buffer);
} else {
perror("Cannot read brightness file: ");
fclose(current_brightness_file);
exit(EXIT_FAILURE);
}
step_brightness = current_brightness * NB_STEP / 100 + 1;
if(increment) {
new_brightness = current_brightness + step_brightness;
} else {
new_brightness = current_brightness - step_brightness;
}
if(new_brightness > max_brightness) {
new_brightness = max_brightness;
} else if (new_brightness <= 0) {
new_brightness = 1;
}
memset(buffer, '\0', BUFFER_SIZE);
sprintf(buffer, "%d", new_brightness);
nb_bytes = fwrite(buffer, strlen(buffer), 1, current_brightness_file);
if(nb_bytes == 0) {
perror("Cannot write brightness file: ");
fclose(current_brightness_file);
exit(EXIT_FAILURE);
}
if(verbose) {
printf("Brightness: %d ==> %d\n", current_brightness, new_brightness);
}
fclose(current_brightness_file);
exit(EXIT_SUCCESS);
}