]> git.sur5r.net Git - sysbacklight/blob - sysbacklight.c
Initialize all variables
[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     SET,
11     INC,
12     DEC
13 };
14
15 struct sysfs_class_device *find_brightness_device(void)
16 {
17     struct sysfs_class *cls;
18     struct dlist *backlights;
19
20     cls = sysfs_open_class("backlight");
21     if(!cls)
22     {
23         perror("Could not open backlight class");
24         return NULL;
25     }
26
27     backlights = sysfs_get_class_devices(cls);
28     if(!backlights)
29     {
30         perror("Could not enumerate backlight devices");
31         return NULL;
32     }
33
34     struct sysfs_class_device *iter=NULL;
35     struct sysfs_attribute *attr=NULL;
36
37     dlist_for_each_data(backlights, iter, struct sysfs_class_device)
38     {
39         if((attr=sysfs_get_classdev_attr(iter, "type")))
40         {
41             if(!strncmp(attr->value, "raw", 3))
42                 return iter;
43         }
44     }
45
46     return NULL;
47 }
48
49 bool set_brightness(struct sysfs_class_device *dev, enum OP op, double value)
50 {
51     struct sysfs_attribute *attr;
52     double brightness=0;
53     double max_brightness=0;
54
55     if((attr=sysfs_get_classdev_attr(dev, "max_brightness")))
56     {
57         max_brightness = strtod(attr->value, NULL);
58     }
59
60     if((attr=sysfs_get_classdev_attr(dev, "brightness")))
61     {
62         brightness = strtod(attr->value, NULL);
63     }
64
65     if(max_brightness < 1 || isnan(max_brightness))
66     {
67         fprintf(stderr, "Failed to acquire maximum brightness.");
68         return false;
69     }
70
71     switch(op)
72     {
73         case SET:
74             brightness = (max_brightness / 100) * value;
75             break;
76
77         case INC:
78             brightness += (max_brightness / 100) * value;
79             break;
80
81         case DEC:
82             brightness -= (max_brightness / 100) * value;
83             break;
84     }
85
86     if(brightness > max_brightness)
87         return true;
88     if(brightness < 0)
89         return true;
90
91     int len;
92     char valbuf[32];
93     len=snprintf(valbuf, 32, "%.0f", brightness);
94     if(sysfs_write_attribute(attr, valbuf, len) != 0)
95     {
96         perror("Failed to write brightness attribute");
97         return false;
98     }
99
100     return true;
101 }
102
103 int main(int argc, char **argv)
104 {
105     struct sysfs_class_device *dev;
106
107     enum OP op=INC;
108     double value=0;
109
110     dev = find_brightness_device();
111     if(!dev)
112     {
113         fprintf(stderr, "Could not find backlight device of type raw!\n");
114         return 1;
115     }
116
117     if(argc >= 3)
118     {
119         switch(argv[1][0])
120         {
121             case '=':
122                 op = SET;
123                 break;
124
125             case '+':
126                 op = INC;
127                 break;
128
129             case '-':
130                 op = DEC;
131                 break;
132
133             default:
134                 fprintf(stderr, "Unknown operation '%c'!\n", argv[1][0]);
135                 return 2;
136         }
137
138         value = strtod(argv[2], NULL);
139         if(value < 0 || isnan(value))
140         {
141             fprintf(stderr, "Invalid brightness value\n");
142             return 3;
143         }
144     }
145
146
147     if(!set_brightness(dev, op, value))
148     {
149         fprintf(stderr, "Could not set brightness!\n");
150         return 4;
151     }
152
153     return 0;
154 }
155