From 4ae7d9cd14b3c80b7e45e27d1d74f01497a0ec01 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 26 Jan 2014 09:55:39 +0100 Subject: implement --set MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/adjbacklight.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'src/adjbacklight.c') diff --git a/src/adjbacklight.c b/src/adjbacklight.c index 4bf50ac..289401c 100644 --- a/src/adjbacklight.c +++ b/src/adjbacklight.c @@ -99,6 +99,14 @@ static void adjust(int cols, const char* device); */ static float getbrightness(const char* device); +/** + * Sets the current backlight setting on a device + * + * @param device The device from which to get backlight + * @param adjustment The adjustment to make + */ +static void setbrightness(const char* device, const char* adjustment); + /** * Read a file * @@ -395,6 +403,8 @@ int main(int argc, char** argv) nbrightness++; } } + else if (set) + setbrightness(device, set); else adjust(cols, device); } @@ -422,6 +432,8 @@ int main(int argc, char** argv) nbrightness++; } } + else if (set) + setbrightness(device, set); else adjust(cols, device); } @@ -597,6 +609,87 @@ static float getbrightness(const char* device) +/** + * Sets the current backlight setting on a device + * + * @param device The device from which to get backlight + * @param adjustment The adjustment to make + */ +static void setbrightness(const char* device, const char* adjustment) +{ + int min, max, cur, i, adj; + size_t lendir; + char* dir = alloca(PATH_MAX * sizeof(char)); + char* buf = alloca(256); + int act = 0, integer = 0, decimal = 0, p = 0, d = 0; + + *dir = 0; + dir = strcat(dir, "/sys/class/backlight/"); + dir = strcat(dir, device); + dir = strcat(dir, "/"); + lendir = strlen(dir); + + /* Get brightness parameters */ + min = 0; + if (readfile(buf, strcat(dir, "max_brightness"))) + return; + max = atoi(unnl(buf)); + *(dir + lendir) = 0; + if (readfile(buf, strcat(dir, "brightness"))) + return; + cur = atoi(unnl(buf)); + + if (max <= min) + return; /* what the buck */ + + /* Read -/+/= head */ + if (*adjustment == '-') + act = -1; + else if (*adjustment == '+') + act = 1; + else if (*adjustment != '=') + adjustment--; + adjustment++; + + /* Parse numerical part */ + for (; *adjustment && (*adjustment != '%'); adjustment++) + if (*adjustment == '.') + d = 1; + else if (d) + { + if ((d * 10 < 0) || (decimal * 10 + 9 < 0)) /* stop if the precision is too high */ + continue; + d *= 10; + decimal *= 10; + decimal += (*adjustment) - '0'; + } + else + { + integer *= 10; + integer -= (*adjustment) - '0'; + } + + /* Count number of p:s */ + while (*adjustment++) + p++; + + /* Calculate value to send */ + if (p == 0) + adj = (int)((double)decimal / (double)d + 0.5d) - integer; + else if (p == 1) + adj = (int)(((double)decimal / (double)d - (double)integer) * (double)(max - min)); + else + adj = (int)(((double)decimal / (double)d - (double)integer) * (double)cur); + adj = (act & 1) * cur + (act | 1) * adj; + if (adj < min) adj = min; + if (adj > max) adj = max; + + /* Send value */ + writefile(buf, adj, dir); +} + + + /** * Read a file * -- cgit v1.2.3-70-g09d2