-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_bright.c
104 lines (83 loc) · 2.49 KB
/
keyboard_bright.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
/**
* keyboard_bright
* Copyright (C) 2010 Kenny Parnell <[email protected]>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef EXISTS_XOSD
#include <xosd.h>
#endif
#define CTL_FILE "/sys/devices/platform/applesmc.768/leds/smc::kbd_backlight/brightness"
#define MAX 255
#define MIN 0
#define STEP 17
int usage(char *argv[]) {
printf("usage: %s up|down|max|min\n\n", argv[0]);
return EXIT_FAILURE;
}
int get_current() {
int val;
FILE *ctl_file;
ctl_file = fopen(CTL_FILE, "r");
fscanf(ctl_file, "%i", &val);
fclose(ctl_file);
return val;
}
void set_current(int val) {
if (val > MAX) val = MAX;
if (val < MIN) val = MIN;
printf("Value: %i", val);
FILE *ctl_file;
ctl_file = fopen(CTL_FILE, "w");
fprintf(ctl_file, "%i", val);
fclose(ctl_file);
// Display OSD
#ifdef XOSD_H
xosd *osd;
osd = xosd_create(3);
if (osd == NULL) return;
int percentage = (int)(100.0 * val / MAX);
xosd_set_pos(osd, XOSD_bottom);
xosd_set_align(osd, XOSD_center);
xosd_set_timeout(osd, 1);
xosd_set_shadow_offset(osd, 1);
xosd_display(osd, 0, XOSD_string, "Brightness");
xosd_display(osd, 2, XOSD_percentage, percentage);
xosd_wait_until_no_display(osd);
xosd_destroy(osd);
#endif
}
int main(int argc, char *argv[]) {
// Make sure we have required arg
if (argc != 2) return usage(argv);
// fetch the current value
int val = get_current();
// Increment/Decrement the current value
if (strcmp(argv[1], "up") == 0) {
val += STEP;
} else if (strcmp(argv[1], "down") == 0) {
val -= STEP;
} else if (strcmp(argv[1], "min") == 0) {
val = MIN;
} else if (strcmp(argv[1], "max") == 0) {
val = MAX;
} else {
return usage(argv);
}
// write the new value
set_current(val);
return EXIT_SUCCESS;
}