summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c114
1 files changed, 42 insertions, 72 deletions
diff --git a/src/main.c b/src/main.c
index 5a790e2..0e67623 100644
--- a/src/main.c
+++ b/src/main.c
@@ -30,13 +30,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
};
@@ -66,7 +66,7 @@ void version_print(FILE* fd) {
/**
* 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.
@@ -88,18 +88,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);
@@ -107,72 +110,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;
}
@@ -322,10 +294,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) {

Generated by cgit