diff options
author | Aaron Ball <nullspoon@oper.io> | 2020-07-22 13:35:41 -0600 |
---|---|---|
committer | Aaron Ball <nullspoon@oper.io> | 2020-07-22 13:35:41 -0600 |
commit | 800421ef0b9ae8039c55e6abbee0b2bf3ec00fa1 (patch) | |
tree | a2ef543e31ddf16ccda38bf5a9607e30ff3c8c58 | |
parent | 110b5ac379f220301d27c80d519e5893b3dc2b3b (diff) | |
download | luminous-800421ef0b9ae8039c55e6abbee0b2bf3ec00fa1.tar.gz luminous-800421ef0b9ae8039c55e6abbee0b2bf3ec00fa1.tar.xz |
Replace support for step/rate with durationsupport_duration
This replaces the rate and step configuration swith the single duration
configuration. This automatically calculates step size by dividing max
brightness into intervals of 1/300th. On systems where this operation
yields a less than 1 number, 1 is assumed.
This also removes the rate configuration by automatically setting it to
the duration divided by the step count (step count being step size
divided into distance).
-rw-r--r-- | sample.conf | 3 | ||||
-rw-r--r-- | src/main.c | 142 |
2 files changed, 53 insertions, 92 deletions
diff --git a/sample.conf b/sample.conf index da257f9..08d2604 100644 --- a/sample.conf +++ b/sample.conf @@ -1,3 +1,2 @@ level = 150 -rate = 20 -step = 12 +duration = 500 @@ -15,9 +15,7 @@ * along with noteless. If not, see <http://www.gnu.org/licenses/>. */ #include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> +#include <unistd.h> #include <errno.h> #include <sys/file.h> @@ -31,22 +29,20 @@ struct props { - int lvl; // Destination brightness - int step; // Step size - int rate; // Time in milliseconds between steps - int max; // Maximum brightness of screen device - int cur; // Current brightness prior to fade operation + int lvl; // Destination brightness + int max; // Maximum brightness of screen device + int cur; // Current brightness prior to fade operation + int duration; // Duration to fade through char filebrightness[512]; // Path to the sysfs ent for brightness char filemax_bright[512]; // Path to the sysfs ent for max brightness }; void props_new(struct props* p) { - p->lvl = -1; - p->step = -1; - p->rate = -1; - p->max = -1; - p->cur = -1; + p->lvl = 200; + p->max = -1; + p->cur = -1; + p->duration = 500; p->filebrightness[0] = '\0'; p->filemax_bright[0] = '\0'; } @@ -99,29 +95,15 @@ int fgeti(char* path) { * returns: Success (0) or failure (-1) */ int fade(struct props* props) { - struct timespec tim, tim2; - int sec; // Number of seconds to sleep - int msec; // Number of milliseconds to sleep (less than 1 second) - int direction = 0; // Direction of fade. Positive for up, negative for down - int opcount = 0; + int direction = 0; // Direction of fade. 1 for up, -1 for down + int step = props->max / 300; // Calculate what 1/300 of max is for step + int rate = 0; // Duration / step count + int steps = 0; // How many steps to cover the distance - sec = props->rate / 1000; - msec = props->rate - (sec * 1000); - - tim.tv_sec = sec; - tim.tv_nsec = 1000000 * msec; - - // Determine direction - if(props->lvl > props->cur) { - // Dest brightness is higher than current - direction = 1; - opcount = (props->lvl - props->cur) / props->step; - } else if(props->lvl < props->cur) { - // Dest brightness is lower than current - direction = -1; - opcount = (props->cur - props->lvl) / props->step; - } + if(step < 1) + step = 1; + // Make sure we can open the file FILE* fd = fopen(props->filebrightness, "w"); if(! fd) { if(errno == EACCES) { @@ -132,28 +114,33 @@ int fade(struct props* props) { return -1; } - // Lock the file descriptor so we can't have conflicting brightness change - // operations + // Determine direction + if(props->lvl > props->cur) { + direction = 1; + steps = (props->lvl - props->cur) / step; + } else if(props->lvl < props->cur) { + direction = -1; + steps = (props->cur - props->lvl) / step; + } + if(steps == 0) + steps = 1; // Ensure no divide by zero operations happen + rate = (props->duration / steps) * 1000; + + // Lock the file descriptor so we can't have conflicting write operations if(flock(fileno(fd), LOCK_EX | LOCK_NB) != 0) { fprintf(stderr, "Could not obtain brightness file lock\n"); fclose(fd); return -1; } - while(opcount > 0) { - nanosleep(&tim, &tim2); - + while(steps > 0) { + usleep(rate); // Write the new brightness to the brightness file fprintf(fd, "%d\n", props->cur); - - // Flush the toil...er...buffer! fflush(fd); - - props->cur += props->step * direction; - opcount--; + props->cur += step * direction; + steps--; } - - // Do not need to explicitly release the file lock fclose(fd); return 0; } @@ -176,11 +163,10 @@ void usage() { " luminous --rate 10 --step 8 --level [+-]400\n" "\n" "Arguments:\n" -" -h,--help Print this helptext\n" -" -s,--step Fade step (default: 6)\n" -" -r,--rate Time in milliseconds between fade steps (default: 10)\n" -" -l,--level Brightness level to change to. Supports absolute values and\n" -" relative values prefixed with + and -.\n" +" -h,--help Print this helptext\n" +" -d,--duration Duration of the fade (default: 500 ms)\n" +" -l,--level Brightness level to change to. Supports absolute values and\n" +" relative values prefixed with + and -.\n" " -V,--version Print program version" "\n\n" ); @@ -210,12 +196,10 @@ int parseargs(struct props* out, int argc, char* argv[]) { out->max = fgeti(out->filemax_bright); for(i = 0; i < argc; i++) { - if(strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--rate") == 0) { + if(strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--duration") == 0) { i++; - out->rate = atoi(argv[i]); - } else if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--step") == 0) { - i++; - out->step = atoi(argv[i]); + out->duration = atoi(argv[i]); + } else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--level") == 0) { i++; // Account for relative and absolute increments and decrements @@ -226,61 +210,44 @@ int parseargs(struct props* out, int argc, char* argv[]) { } else { out->lvl = atoi(argv[i]); } + } else if(strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "--version") == 0) { version_print(stdout); return -2; + } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { usage(); return -2; } } - // Some defaults, if no values are set yet - if(out->rate < 0) - out->rate = 20; - if(out->step < 0) - out->step = 12; - if(out->lvl == -1) - out->lvl = 200; - // Ensure min and max thresholds are respected - if(out->lvl > out->max) { + if(out->lvl > out->max) out->lvl = out->max; - } else if(out->lvl < 0) { + else if(out->lvl < 0) out->lvl = 0; - } return 0; } int config_to_props(char* confpath, struct props* p) { struct config c; - char lvlbuf[32]; // Buffer to store config value for "level" if found - char ratebuf[32]; // Buffer to store config value for "rate" if found - char stepbuf[32]; // Buffer to store config value for "step" if found + char lvlbuf[32]; // Buffer to store config value for "level" if found + char durationbuf[32]; // Buffer to store config value for "duration" if found - if(config_new(&c, confpath) == 0) { + if(config_new(&c, confpath) == 0) config_load(&c); - } else { - // Config file not found, return -2 code + else return -2; - } - // If the config specifies a "level" key, set it to the props object + // Check for and load specific configs if(config_get(&c, "level", lvlbuf) == 0) p->lvl = atoi(lvlbuf); - - // If the config specifies a "step" key, set it to the props object - if(config_get(&c, "step", stepbuf) == 0) - p->step = atoi(stepbuf); - - // If the config specifies a "rate" key, set it to the props object - if(config_get(&c, "rate", ratebuf) == 0) - p->rate = atoi(ratebuf); + if(config_get(&c, "duration", lvlbuf) == 0) + p->duration = atoi(durationbuf); // Don't forget to clean up! config_free(&c); - return 0; } @@ -301,12 +268,7 @@ int main(int argc, char* argv[]) { if(parseargs(&p, argc, argv) != 0) return 1; - // Determine if we should fade up or fade down - if(p.lvl < 0) { - printf("%d\n", p.cur); - } else { - r = fade(&p); - } + r = fade(&p); if(r < 0) { fprintf(stderr, "ERROR: Could not open brightness file\n"); |