From dda74af557da0788833361955346cd1152da3ff8 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Sat, 20 Jan 2018 14:29:48 -0800 Subject: [PATCH] Made integer on command line work without '=' and removed excess strcmp --- include/cmd.h | 6 ++- src/cmd.c | 106 ++++++++++++++++++++++++--------------------- src/rexbacklight.c | 8 ++-- 3 files changed, 64 insertions(+), 56 deletions(-) diff --git a/include/cmd.h b/include/cmd.h index 1e860a8..f63f1de 100644 --- a/include/cmd.h +++ b/include/cmd.h @@ -19,6 +19,8 @@ #ifndef RECBACKLIGHT_CMD_H #define REXBACKLIGHT_CMD_H +#define OP_INC 1 +#define OP_DEC 2 #define OP_SET 4 #define OP_LIST 8 #define OP_GET 128 @@ -52,7 +54,7 @@ struct arg_values{ const char* device; //What value to put in the backlight file - char* delta; + float delta; //How many seconds to transition int fade_duration; @@ -64,6 +66,6 @@ struct arg_values{ void free_cmd_args(struct arg_values* a); struct arg_values process_cmd_args(int argc, char** argv); -int process_op(char* arg, float min, float current, float max); +int process_op(struct arg_values* arg, float min, float current, float max); #endif diff --git a/src/cmd.c b/src/cmd.c index 54ce680..fb9f45e 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -95,26 +95,46 @@ struct arg_values process_cmd_args(int argc, char** argv){ else if(!strcmp(argv[i], "max")){ curr->operation = OP_SET; - curr->delta = argv[i]; + curr->delta = 100; } else if(!strcmp(argv[i], "min")){ curr->operation = OP_SET; - curr->delta = argv[i]; + curr->delta = 0.1; } else if(!strcmp(argv[i], "off")){ curr->operation = OP_SET; - curr->delta = argv[i]; + curr->delta = 0; } - //If we get a '-' followed by not a number, it's not a known option - else if(argv[i][1] < '0' || argv[i][1] > '9'){ - UNRECOGNIZED_OPTION(RETVAL_UNRECOGNIZED_OPTION); - } - - //If we get a '-' followed by a number, it's a decrement request - else{ + else if(argv[i][0] == '='){ curr->operation = OP_SET; - curr->delta = argv[i]; + curr->delta = atof(argv[i] + 1); + } + else if(argv[i][0] == '+'){ + curr->operation = OP_INC; + curr->delta = atof(argv[i] + 1); + } + else if(argv[i][0] == '-'){ + //If we get a '-' followed by not a number, it's not a known option + if(argv[i][1] < '0' || argv[i][1] > '9'){ + UNRECOGNIZED_OPTION(RETVAL_UNRECOGNIZED_OPTION); + + //If we get a '-' followed by a number, it's a decrement request + }else{ + curr->operation = OP_DEC; + curr->delta = atof(argv[i] + 1); + } + } + + else{ + for(int j = 0; j < 3;j++){ + if(argv[i][j] == '\0') + break; + if(argv[i][j] < '0' || argv[i][j] > '9') + UNRECOGNIZED_OPTION(RETVAL_UNRECOGNIZED_OPTION); + } + curr->operation = OP_SET; + curr->delta = atof(argv[i]); } } @@ -149,47 +169,33 @@ struct arg_values process_cmd_args(int argc, char** argv){ #undef UNRECOGNIZED_OPTION //Process an operation -int process_op(char* arg, float min, float current, float max){ +int process_op(struct arg_values* arg, float min, float current, float max){ //Amount to inc/dec - int delta = delta = max * atof(arg + 1) / 100.0; + int delta = max * arg->delta / 100.0; - //Increment - if(arg[0] == '+'){ - delta = delta + current; - if(delta >= max) - return max; - else{ - return delta; - } - - //Decrement - }else if(arg[0] == '-'){ - delta = current - delta; - if(delta <= min) - return min; - else - return delta; - - //Directly set - }else if(arg[0] == '='){ - if(delta >= max) - return max; - else if(delta <= min) - return min; - else - return delta; - - //Set to minimum plus one (so backlight is actually on!) - }else if(!strcmp("min", arg)){ - return min + 1; - - //Turn off backlight - }else if(!strcmp("off", arg)){ - return min; - - //Set to maximum - }else if(!strcmp("max", arg)){ - return max; + switch(arg->operation){ + case OP_SET: + if(delta >= max) + return max; + else if(delta <= min) + return min; + else + return delta; + case OP_INC: + delta += current; + if(delta >= max) + return max; + else + return delta; + case OP_DEC: + delta = current - delta; + if(delta <= min) + return min; + else + return delta; + default: + fprintf(stderr, "Internal error, please fix for me ;)\n"); + exit(-99); } return current; } diff --git a/src/rexbacklight.c b/src/rexbacklight.c index b9ba1e5..b1bd551 100644 --- a/src/rexbacklight.c +++ b/src/rexbacklight.c @@ -104,8 +104,8 @@ float get_brightness(const char* file){ } //Write value to backlight files -void write_delta(char* delta){ - int out = process_op(delta, 0, get_brightness(backlight_file), get_brightness(max_backlight_file)); +void write_delta(struct arg_values* arg){ + int out = process_op(arg, 0, get_brightness(backlight_file), get_brightness(max_backlight_file)); FILE* bright = fopen(backlight_file, "w+"); if(!bright){ fprintf(stderr, "Unable to open brightness file \"%s%s\" for writing!\n", backlight_dir, backlight_file); @@ -170,7 +170,7 @@ int main(int argc, char** argv){ fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, curr->device); continue; } - write_delta(curr->delta); + write_delta(curr); } //Otherise, apply delta to all backlights }else{ @@ -179,7 +179,7 @@ int main(int argc, char** argv){ fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names.list[i]); continue; } - write_delta(args.delta); + write_delta(&args); } } //Return to start directory