From: Jakob Haufe Date: Sat, 21 Oct 2017 17:05:08 +0000 (+0200) Subject: Add support for querying brightness X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=4496ba54429d3f8b639a6385e75b646df9c64be4;p=sysbacklight Add support for querying brightness --- diff --git a/sysbacklight.c b/sysbacklight.c index fe0affd..c9e2cd7 100644 --- a/sysbacklight.c +++ b/sysbacklight.c @@ -7,6 +7,7 @@ #include enum OP { + QUERY, SET, INC, DEC @@ -46,6 +47,24 @@ struct sysfs_class_device *find_brightness_device(void) return NULL; } +double get_brightness(struct sysfs_class_device *dev) +{ + struct sysfs_attribute *attr; + double brightness=0; + double max_brightness=0; + + if((attr=sysfs_get_classdev_attr(dev, "max_brightness"))) + { + max_brightness = strtod(attr->value, NULL); + } + if((attr=sysfs_get_classdev_attr(dev, "brightness"))) + { + brightness = strtod(attr->value, NULL); + } + + return (brightness / max_brightness) * 100; +} + bool set_brightness(struct sysfs_class_device *dev, enum OP op, double value) { struct sysfs_attribute *attr; @@ -81,6 +100,10 @@ bool set_brightness(struct sysfs_class_device *dev, enum OP op, double value) case DEC: brightness -= (max_brightness / 100) * value; break; + + default: + // This is not supposed to happen + return false; } if(brightness > max_brightness) @@ -104,7 +127,7 @@ int main(int argc, char **argv) { struct sysfs_class_device *dev; - enum OP op=INC; + enum OP op=QUERY; double value=0; dev = find_brightness_device(); @@ -114,10 +137,14 @@ int main(int argc, char **argv) return 1; } - if(argc >= 3) + if(argc >= 2) { switch(argv[1][0]) { + case '?': + op = QUERY; + break; + case '=': op = SET; break; @@ -135,19 +162,35 @@ int main(int argc, char **argv) return 2; } - value = strtod(argv[2], NULL); + if(op != QUERY) + { + if(argc < 3) + { + fprintf(stderr, "Operation '%c' needs a value argument!\n", argv[1][0]); + return 2; + } + value = strtod(argv[2], NULL); + } + if(value < 0 || isnan(value)) { fprintf(stderr, "Invalid brightness value\n"); - return 3; + return 2; } } - - if(!set_brightness(dev, op, value)) + if(op==QUERY) + { + value = get_brightness(dev); + printf("%.0f\n", value); + } + else { - fprintf(stderr, "Could not set brightness!\n"); - return 4; + if(!set_brightness(dev, op, value)) + { + fprintf(stderr, "Could not set brightness!\n"); + return 3; + } } return 0;