Made integer on command line work without '=' and removed excess strcmp

This commit is contained in:
rexy712 2018-01-20 14:29:48 -08:00
parent a3c32e5e7f
commit dda74af557
3 changed files with 64 additions and 56 deletions

View File

@ -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

View File

@ -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);
else if(argv[i][0] == '='){
curr->operation = OP_SET;
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 = argv[i];
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] == '='){
switch(arg->operation){
case OP_SET:
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)){
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;
}

View File

@ -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