summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2017-12-31 15:22:48 -0700
committerAaron Ball <nullspoon@oper.io>2017-12-31 15:42:36 -0700
commitf95b9f6f6c7f83dcb9aef2b4191ace55dcb47789 (patch)
tree079c452426ad3a522e848512bcd2e185e4961b11
parent67009321e9aa39035eade6321a633c4111909b65 (diff)
downloadluminous-f95b9f6f6c7f83dcb9aef2b4191ace55dcb47789.tar.gz
luminous-f95b9f6f6c7f83dcb9aef2b4191ace55dcb47789.tar.xz
Integrated new config code into main
Now we check the config path (/etc/luminous.conf) for a config file. If one is present, its contents are parsed and loaded into the properties object. This occurs before parsing command line arguments, so all config values can be overridden by the user at runtime. The config file currently supports "level", "rate", and "step" values. It is delimited by the '=' char and can handle any spacing between key, delimiter, and value. We also handle no config file being present as well as no command line arguments being specified by using sane defaults hardcoded into the program. Added props constructor to ensure state of a freshly allocated props struct.
-rw-r--r--Makefile13
-rw-r--r--sample.conf3
-rw-r--r--src/main.c78
3 files changed, 79 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index b390334..15186c0 100644
--- a/Makefile
+++ b/Makefile
@@ -3,15 +3,22 @@ CCOPTS=-Wall
BINDIR=/usr/bin
INITDIR=/etc/rc.d
+CONFDIR=/etc/
all:
- cc $(CCOPTS) $(DBG) src/main.c -o luminous
if [ ! -d obj ]; then mkdir obj; fi
cc $(CCOPTS) $(DBG) src/config.c -c -o obj/config.o
+ cc $(CCOPTS) $(DBG) obj/*.o src/main.c -o luminous
install:
- install -D luminous ${DESTDIR}/${BINDIR}/luminous
- install -D init.sh ${DESTDIR}/${INITDIR}/luminous
+ install -D luminous ${DESTDIR}/${BINDIR}/luminous
+ install -D init.sh ${DESTDIR}/${INITDIR}/luminous
+ install -D sample.conf ${DESTDIR}/${CONFDIR}/luminous.conf
debug:
make DBG=-g
+
+leak-test:
+ make debug
+ valgrind --error-exitcode=1 luminous -l 800 && printf "\n\n--- No leaks found!\n\n"
+ valgrind --error-exitcode=1 luminous -l 150 && printf "\n\n--- No leaks found!\n\n"
diff --git a/sample.conf b/sample.conf
new file mode 100644
index 0000000..da257f9
--- /dev/null
+++ b/sample.conf
@@ -0,0 +1,3 @@
+level = 150
+rate = 20
+step = 12
diff --git a/src/main.c b/src/main.c
index be7bd29..f3ab73a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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);

Generated by cgit