]> git.sur5r.net Git - sysbacklight/blob - sysbacklight.c
c9e2cd73fc3260d4eec7c48cae8eeaa7cc21afe9
[sysbacklight] / sysbacklight.c
1 #include <math.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <libsysfs.h>
8
9 enum OP {
10     QUERY,
11     SET,
12     INC,
13     DEC
14 };
15
16 struct sysfs_class_device *find_brightness_device(void)
17 {
18     struct sysfs_class *cls;
19     struct dlist *backlights;
20
21     cls = sysfs_open_class("backlight");
22     if(!cls)
23     {
24         perror("Could not open backlight class");
25         return NULL;
26     }
27
28     backlights = sysfs_get_class_devices(cls);
29     if(!backlights)
30     {
31         perror("Could not enumerate backlight devices");
32         return NULL;
33     }
34
35     struct sysfs_class_device *iter=NULL;
36     struct sysfs_attribute *attr=NULL;
37
38     dlist_for_each_data(backlights, iter, struct sysfs_class_device)
39     {
40         if((attr=sysfs_get_classdev_attr(iter, "type")))
41         {
42             if(!strncmp(attr->value, "raw", 3))
43                 return iter;
44         }
45     }
46
47     return NULL;
48 }
49
50 double get_brightness(struct sysfs_class_device *dev)
51 {
52     struct sysfs_attribute *attr;
53     double brightness=0;
54     double max_brightness=0;
55
56     if((attr=sysfs_get_classdev_attr(dev, "max_brightness")))
57     {
58         max_brightness = strtod(attr->value, NULL);
59     }
60     if((attr=sysfs_get_classdev_attr(dev, "brightness")))
61     {
62         brightness = strtod(attr->value, NULL);
63     }
64
65     return (brightness / max_brightness) * 100;
66 }
67
68 bool set_brightness(struct sysfs_class_device *dev, enum OP op, double value)
69 {
70     struct sysfs_attribute *attr;
71     double brightness=0;
72     double max_brightness=0;
73
74     if((attr=sysfs_get_classdev_attr(dev, "max_brightness")))
75     {
76         max_brightness = strtod(attr->value, NULL);
77     }
78
79     if((attr=sysfs_get_classdev_attr(dev, "brightness")))
80     {
81         brightness = strtod(attr->value, NULL);
82     }
83
84     if(max_brightness < 1 || isnan(max_brightness))
85     {
86         fprintf(stderr, "Failed to acquire maximum brightness.");
87         return false;
88     }
89
90     switch(op)
91     {
92         case SET:
93             brightness = (max_brightness / 100) * value;
94             break;
95
96         case INC:
97             brightness += (max_brightness / 100) * value;
98             break;
99
100         case DEC:
101             brightness -= (max_brightness / 100) * value;
102             break;
103
104         default:
105             // This is not supposed to happen
106             return false;
107     }
108
109     if(brightness > max_brightness)
110         return true;
111     if(brightness < 0)
112         return true;
113
114     int len;
115     char valbuf[32];
116     len=snprintf(valbuf, 32, "%.0f", brightness);
117     if(sysfs_write_attribute(attr, valbuf, len) != 0)
118     {
119         perror("Failed to write brightness attribute");
120         return false;
121     }
122
123     return true;
124 }
125
126 int main(int argc, char **argv)
127 {
128     struct sysfs_class_device *dev;
129
130     enum OP op=QUERY;
131     double value=0;
132
133     dev = find_brightness_device();
134     if(!dev)
135     {
136         fprintf(stderr, "Could not find backlight device of type raw!\n");
137         return 1;
138     }
139
140     if(argc >= 2)
141     {
142         switch(argv[1][0])
143         {
144             case '?':
145                 op = QUERY;
146                 break;
147
148             case '=':
149                 op = SET;
150                 break;
151
152             case '+':
153                 op = INC;
154                 break;
155
156             case '-':
157                 op = DEC;
158                 break;
159
160             default:
161                 fprintf(stderr, "Unknown operation '%c'!\n", argv[1][0]);
162                 return 2;
163         }
164
165         if(op != QUERY)
166         {
167             if(argc < 3)
168             {
169                 fprintf(stderr, "Operation '%c' needs a value argument!\n", argv[1][0]);
170                 return 2;
171             }
172             value = strtod(argv[2], NULL);
173         }
174
175         if(value < 0 || isnan(value))
176         {
177             fprintf(stderr, "Invalid brightness value\n");
178             return 2;
179         }
180     }
181
182     if(op==QUERY)
183     {
184         value = get_brightness(dev);
185         printf("%.0f\n", value);
186     }
187     else
188     {
189         if(!set_brightness(dev, op, value))
190         {
191             fprintf(stderr, "Could not set brightness!\n");
192             return 3;
193         }
194     }
195
196     return 0;
197 }
198