diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 114 |
1 files changed, 42 insertions, 72 deletions
@@ -26,13 +26,13 @@ struct props { - int lvl; - int step; - int rate; - int max; - int cur; - char filebrightness[512]; - char filemax_bright[512]; + 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 + char filebrightness[512]; // Path to the sysfs ent for brightness + char filemax_bright[512]; // Path to the sysfs ent for max brightness }; @@ -49,7 +49,7 @@ void props_new(struct props* p) { /** * fgeti: - * @path + * @path Path to the file from which to read the integer * * Reads integer from the specified file. If the file does not contain only an * integer, returns -1. @@ -71,18 +71,21 @@ int fgeti(char* path) { /** - * fadeup: + * fade: * @props Props struct containing runtime parameters * - * Fades brightness down, incremented by props->step every props->rate - * milliseconds + * Fades brightness up or down, based on positive (fade up) or negative value + * (fade down) contained in `step` property. Rate of fade is controled by + * `props->rate`. * * returns: Success (0) or failure (-1) */ -int fadeup(struct props* props) { +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; sec = props->rate / 1000; msec = props->rate - (sec * 1000); @@ -90,72 +93,41 @@ int fadeup(struct props* props) { tim.tv_sec = sec; tim.tv_nsec = 1000000 * msec; - while(props->cur < props->lvl) { - nanosleep(&tim, &tim2); + // 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; + } - FILE* fd = fopen(props->filebrightness, "w"); - if(! fd) { - if(errno == EACCES) { - fprintf(stderr, "Permission denied: %s\n", props->filebrightness); - } else { - fprintf(stderr, "Could not open file %s\n", props->filebrightness); - } - return -1; + FILE* fd = fopen(props->filebrightness, "w"); + if(! fd) { + if(errno == EACCES) { + fprintf(stderr, "Permission denied: %s\n", props->filebrightness); + } else { + fprintf(stderr, "Could not open file %s\n", props->filebrightness); } - - // Write the new brightness to the brightness file - fprintf(fd, "%d\n", props->cur); - fclose(fd); - - if(props->cur >= props->max) - break; - props->cur += props->step; + return -1; } - return 0; -} - - -/** - * fadedown: - * @props Props struct containing runtime parameters - * - * Fades brightness down, decremented by props->step every props->rate - * milliseconds - * - * returns: Success (0) or failure (-1) - */ -int fadedown(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) - - sec = props->rate / 1000; - msec = props->rate - (sec * 1000); - - tim.tv_sec = sec; - tim.tv_nsec = 1000000 * msec; - while(props->cur > props->lvl) { + while(opcount > 0) { nanosleep(&tim, &tim2); - FILE* fd = fopen(props->filebrightness, "w"); - if(! fd) { - if(errno == EACCES) { - fprintf(stderr, "Permission denied: %s\n", props->filebrightness); - } else { - fprintf(stderr, "Could not open file %s\n", props->filebrightness); - } - return -1; - } - // Write the new brightness to the brightness file fprintf(fd, "%d\n", props->cur); - fclose(fd); - if(props->cur <= 0) - break; - props->cur -= props->step; + // Flush the toil...er...buffer! + fflush(fd); + + props->cur += props->step * direction; + opcount--; } + + fclose(fd); return 0; } @@ -301,10 +273,8 @@ int main(int argc, char* argv[]) { // Determine if we should fade up or fade down if(p.lvl < 0) { printf("%d\n", p.cur); - } else if(p.lvl > p.cur) { - r = fadeup(&p); - } else if (p.lvl < p.cur) { - r = fadedown(&p); + } else { + r = fade(&p); } if(r < 0) { |