summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2020-07-22 13:52:55 -0600
committerAaron Ball <nullspoon@oper.io>2020-07-22 13:52:55 -0600
commit10928c842c613099bd4770d55df81b0057e8f5ac (patch)
tree9d4eae468e9f0f977c89d618dcc7784f750a6174
parent38457b9c50bdee9ebd7e045d13375acd4808f73a (diff)
parent3254f7091245b3f0f6ccfb71e839276ce3238c26 (diff)
downloadluminous-10928c842c613099bd4770d55df81b0057e8f5ac.tar.gz
luminous-10928c842c613099bd4770d55df81b0057e8f5ac.tar.xz
Merge branch 'support_toggle'
-rw-r--r--src/main.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
index 9ee733f..d78a40d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -22,6 +22,7 @@
#include "config.h"
#define CONF "/etc/luminous.conf"
+#define STATE "/tmp/luminous.state"
// For string macros
#define xstr(s) str(s)
@@ -85,6 +86,25 @@ int fgeti(char* path) {
/**
+ * write_state:
+ * Writes the current brightness level to a text file.
+ *
+ * @path Path to the file to contain the current brightness
+ */
+int write_state(char* path, int cur) {
+ FILE* fd;
+ fd = fopen(path, "w");
+
+ if(!fd)
+ return errno;
+
+ fprintf(fd, "%d", cur);
+ fclose(fd);
+ return 0;
+}
+
+
+/**
* fade:
* @props Props struct containing runtime parameters
*
@@ -141,6 +161,9 @@ int fade(struct props* props) {
props->cur += step * direction;
steps--;
}
+ // The step loop got us very close to the destination brightness, just finish
+ // things off since the math rarely works evenly.
+ fprintf(fd, "%d\n", props->lvl);
fclose(fd);
return 0;
}
@@ -200,6 +223,21 @@ int parseargs(struct props* out, int argc, char* argv[]) {
i++;
out->duration = atoi(argv[i]);
+ } else if(strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--toggle") == 0) {
+ FILE* fd = fopen(STATE, "r");
+ // If somehow we are at zero and there is no statefile, set brightness to
+ // half max.
+ if(out->cur < out->max * .02) {
+ // If 0, we increase brightness which means reading from statefile
+ if(!fd)
+ write_state(STATE, out->lvl = out->max / 2);
+ else
+ out->lvl = fgeti(STATE);
+ } else {
+ write_state(STATE, out->cur);
+ out->lvl = 0;
+ }
+
} else if(strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--level") == 0) {
i++;
// Account for relative and absolute increments and decrements

Generated by cgit