Made integer on command line work without '=' and removed excess strcmp
This commit is contained in:
parent
a3c32e5e7f
commit
dda74af557
@ -19,6 +19,8 @@
|
|||||||
#ifndef RECBACKLIGHT_CMD_H
|
#ifndef RECBACKLIGHT_CMD_H
|
||||||
#define REXBACKLIGHT_CMD_H
|
#define REXBACKLIGHT_CMD_H
|
||||||
|
|
||||||
|
#define OP_INC 1
|
||||||
|
#define OP_DEC 2
|
||||||
#define OP_SET 4
|
#define OP_SET 4
|
||||||
#define OP_LIST 8
|
#define OP_LIST 8
|
||||||
#define OP_GET 128
|
#define OP_GET 128
|
||||||
@ -52,7 +54,7 @@ struct arg_values{
|
|||||||
const char* device;
|
const char* device;
|
||||||
|
|
||||||
//What value to put in the backlight file
|
//What value to put in the backlight file
|
||||||
char* delta;
|
float delta;
|
||||||
|
|
||||||
//How many seconds to transition
|
//How many seconds to transition
|
||||||
int fade_duration;
|
int fade_duration;
|
||||||
@ -64,6 +66,6 @@ struct arg_values{
|
|||||||
|
|
||||||
void free_cmd_args(struct arg_values* a);
|
void free_cmd_args(struct arg_values* a);
|
||||||
struct arg_values process_cmd_args(int argc, char** argv);
|
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
|
#endif
|
||||||
|
|||||||
84
src/cmd.c
84
src/cmd.c
@ -95,26 +95,46 @@ struct arg_values process_cmd_args(int argc, char** argv){
|
|||||||
|
|
||||||
else if(!strcmp(argv[i], "max")){
|
else if(!strcmp(argv[i], "max")){
|
||||||
curr->operation = OP_SET;
|
curr->operation = OP_SET;
|
||||||
curr->delta = argv[i];
|
curr->delta = 100;
|
||||||
}
|
}
|
||||||
else if(!strcmp(argv[i], "min")){
|
else if(!strcmp(argv[i], "min")){
|
||||||
curr->operation = OP_SET;
|
curr->operation = OP_SET;
|
||||||
curr->delta = argv[i];
|
curr->delta = 0.1;
|
||||||
}
|
}
|
||||||
else if(!strcmp(argv[i], "off")){
|
else if(!strcmp(argv[i], "off")){
|
||||||
curr->operation = OP_SET;
|
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][0] == '='){
|
||||||
else if(argv[i][1] < '0' || argv[i][1] > '9'){
|
curr->operation = OP_SET;
|
||||||
UNRECOGNIZED_OPTION(RETVAL_UNRECOGNIZED_OPTION);
|
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
|
//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{
|
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->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
|
#undef UNRECOGNIZED_OPTION
|
||||||
|
|
||||||
//Process an operation
|
//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
|
//Amount to inc/dec
|
||||||
int delta = delta = max * atof(arg + 1) / 100.0;
|
int delta = max * arg->delta / 100.0;
|
||||||
|
|
||||||
//Increment
|
switch(arg->operation){
|
||||||
if(arg[0] == '+'){
|
case OP_SET:
|
||||||
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)
|
if(delta >= max)
|
||||||
return max;
|
return max;
|
||||||
else if(delta <= min)
|
else if(delta <= min)
|
||||||
return min;
|
return min;
|
||||||
else
|
else
|
||||||
return delta;
|
return delta;
|
||||||
|
case OP_INC:
|
||||||
//Set to minimum plus one (so backlight is actually on!)
|
delta += current;
|
||||||
}else if(!strcmp("min", arg)){
|
if(delta >= max)
|
||||||
return min + 1;
|
|
||||||
|
|
||||||
//Turn off backlight
|
|
||||||
}else if(!strcmp("off", arg)){
|
|
||||||
return min;
|
|
||||||
|
|
||||||
//Set to maximum
|
|
||||||
}else if(!strcmp("max", arg)){
|
|
||||||
return 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;
|
return current;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -104,8 +104,8 @@ float get_brightness(const char* file){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Write value to backlight files
|
//Write value to backlight files
|
||||||
void write_delta(char* delta){
|
void write_delta(struct arg_values* arg){
|
||||||
int out = process_op(delta, 0, get_brightness(backlight_file), get_brightness(max_backlight_file));
|
int out = process_op(arg, 0, get_brightness(backlight_file), get_brightness(max_backlight_file));
|
||||||
FILE* bright = fopen(backlight_file, "w+");
|
FILE* bright = fopen(backlight_file, "w+");
|
||||||
if(!bright){
|
if(!bright){
|
||||||
fprintf(stderr, "Unable to open brightness file \"%s%s\" for writing!\n", backlight_dir, backlight_file);
|
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);
|
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, curr->device);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
write_delta(curr->delta);
|
write_delta(curr);
|
||||||
}
|
}
|
||||||
//Otherise, apply delta to all backlights
|
//Otherise, apply delta to all backlights
|
||||||
}else{
|
}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]);
|
fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names.list[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
write_delta(args.delta);
|
write_delta(&args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Return to start directory
|
//Return to start directory
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user