diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 78 |
1 files changed, 66 insertions, 12 deletions
@@ -20,6 +20,10 @@ #include <time.h> #include <errno.h> +#include "config.h" + +#define CONF "/etc/luminous.conf" + struct props { int lvl; @@ -32,6 +36,17 @@ struct props { }; +void props_new(struct props* p) { + p->lvl = -1; + p->step = -1; + p->rate = -1; + p->max = -1; + p->cur = -1; + p->filebrightness[0] = '\0'; + p->filemax_bright[0] = '\0'; +} + + /** * fgeti: * @path @@ -183,7 +198,7 @@ void usage() { * returns: Success (0), failure (-1), or incomplete (-2) */ int parseargs(struct props* out, int argc, char* argv[]) { - int i = 0; + int i; strcpy(out->filebrightness, "/sys/class/backlight/intel_backlight/brightness"); @@ -194,11 +209,7 @@ int parseargs(struct props* out, int argc, char* argv[]) { out->cur = fgeti(out->filebrightness); out->max = fgeti(out->filemax_bright); - // Some defaults - out->rate = 10; - out->step = 8; - - for(;i < argc; i++) { + for(i = 0; i < argc; i++) { if(strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--rate") == 0) { i++; out->rate = atoi(argv[i]); @@ -221,6 +232,14 @@ int parseargs(struct props* out, int argc, char* argv[]) { } } + // 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) { out->lvl = out->max; @@ -231,6 +250,38 @@ int parseargs(struct props* out, int argc, char* argv[]) { } +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 + + if(config_new(&c, confpath) == 0) { + config_load(&c); + } else { + // Config file not found, return -2 code + return -2; + } + + // If the config specifies a "level" key, set it to the props object + 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); + + // Don't forget to clean up! + config_free(&c); + + return 0; +} + + /** * Ye olde main */ @@ -238,16 +289,19 @@ int main(int argc, char* argv[]) { int r = 0; struct props p; + props_new(&p); + + // Load configs into properties struct + config_to_props(CONF, &p); + + // Parse cli arguments (overriding any configs if specified at runtime) if(parseargs(&p, argc, argv) != 0) return 1; - if(argc == 1) { - printf("%d\n", p.cur); - return 0; - } - // Determine if we should fade up or fade down - if(p.lvl > p.cur) { + 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); |