Mercurial > backlight
changeset 15:282ec7c30c04
backlight: now supports more commands
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 11 Oct 2019 12:58:48 +0200 |
parents | df8639b5b248 |
children | 0d923f0155dd |
files | backlight.1 backlight.c |
diffstat | 2 files changed, 110 insertions(+), 48 deletions(-) [+] |
line wrap: on
line diff
--- a/backlight.1 Wed Oct 09 16:54:50 2019 +0200 +++ b/backlight.1 Fri Oct 11 12:58:48 2019 +0200 @@ -23,9 +23,14 @@ .\" SYNOPSIS .Sh SYNOPSIS .Nm -.Ar down +.Ar decrease +.Nm +.Ar get .Nm -.Ar up +.Ar increase +.Nm +.Ar set +.Ar percentage .\" DESCRIPTION .Sh DESCRIPTION The @@ -33,11 +38,19 @@ tool adjust the brightness on your screen using direct access to ACPI video extension. .Pp -It understand the two commands -.Ar down -and -.Ar up -which decrease or increase the brightness level respectively. +The following commands are available: +.Bl -tag -width 10n +.It Cm decrease +Decrease to the next lower level. +.It Cm get +Get the current level scaled to a percentage range. +.It Cm increase +Increase to the next upper level. +.It Cm set +Set brightness to the +.Ar percentage +value. +.El .\" AUTHORS .Sh AUTHORS .Nm
--- a/backlight.c Wed Oct 09 16:54:50 2019 +0200 +++ b/backlight.c Fri Oct 11 12:58:48 2019 +0200 @@ -22,17 +22,35 @@ #include <stdlib.h> #include <string.h> -enum action { - None, - Up, - Down -}; +/* + * Functions to implement for your system: + * + * All of these functions are allowed to exit on failures. + */ + +/* Increase to the nearest upper level */ +static void +increase(void); + +/* Decrease to the nearest lower level */ +static void +decrease(void); + +/* Set the level explicitly to the nearest value */ +static void +set(unsigned int); + +/* Get the current value */ +static unsigned int +get(void); static void usage(const char *name) { - fprintf(stderr, "usage: %s down\n", name); - fprintf(stderr, " %s up\n", name); + fprintf(stderr, "usage: %s decrease\n", name); + fprintf(stderr, " %s get\n", name); + fprintf(stderr, " %s increase\n", name); + fprintf(stderr, " %s set percentage\n", name); exit(1); } @@ -117,7 +135,7 @@ #define SYS_PATH "/sys/class/backlight" -static int +static unsigned int read_int(int dfd, const char *file) { char buf[32] = {0}; @@ -134,7 +152,7 @@ } static int -write_int(int dfd, const char *file, int value) +write_int(int dfd, const char *file, unsigned int value) { char buf[32] = {0}; int length; @@ -191,34 +209,66 @@ closedir(dirp); close(dfd); + if (adaptorfd < 0) + die("could not find card adaptor\n"); + return adaptorfd; } -static int -set(int type) +static unsigned int +downscale(int dfd, unsigned int value) { - int current, max; - int dfd; + return value * 100 / read_int(dfd, "max_brightness"); +} + +static unsigned int +upscale(int dfd, unsigned int value) +{ + return value * read_int(dfd, "max_brightness") / 100; +} - if ((dfd = find_adaptor()) < 0) - die("could not find card adaptor\n"); +static void +set(unsigned int percent) +{ + int dfd = find_adaptor(); + unsigned int value = upscale(dfd, percent); - current = read_int(dfd, "actual_brightness"); - max = read_int(dfd, "max_brightness"); + write_int(dfd, "brightness", value); +} + +static void +increase(void) +{ + unsigned int value = get(); - if (type == Up) { - current += max / 100; + if (value + 2 > 100) + value = 100; + else + value += 2; + + set(value); +} + +static void +decrease(void) +{ + unsigned int value = get(); - if (current > max) - return max; - } else { - current -= max / 100; + if ((int)value - 2 < 0) + value = 0; + else + value -= 2; + + set(value); +} - if (current < 0) - return 0; - } +static unsigned int +get(void) +{ + int dfd = find_adaptor(); + int value = read_int(dfd, "brightness"); - return write_int(dfd, "brightness", current); + return downscale(dfd, value); } #else @@ -230,8 +280,6 @@ (void)type; die("backlight is not supported on this system"); - - return -1; } #endif @@ -241,20 +289,21 @@ int main(int argc, char *argv[]) { - int st, type = None; - - if (argc < 2) - usage("backlight"); + -- argc; + ++ argv; - if (strcmp(argv[1], "up") == 0) - type = Up; - else if (strcmp(argv[1], "down") == 0) - type = Down; - else + if (argc < 1) usage(argv[0]); - if ((st = set(type)) < 0) - return 1; + if (strcmp(argv[0], "get") == 0) + printf("%d\n", get()); + else if (strcmp(argv[0], "set") == 0) { + if (argc != 2) + usage(argv[0]); - printf("switching to %d\n", st); + set(atoi(argv[1])); + } else if (strcmp(argv[0], "increase") == 0) + increase(); + else if (strcmp(argv[0], "decrease") == 0) + decrease(); }