diff --git a/src/rexbacklight.c b/src/rexbacklight.c index b6345c1..e4ab761 100644 --- a/src/rexbacklight.c +++ b/src/rexbacklight.c @@ -104,7 +104,7 @@ float get_brightness(const char* file){ } //Write value to backlight files -void write_delta(struct arg_values* arg){ +void do_assignment(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){ @@ -114,8 +114,61 @@ void write_delta(struct arg_values* arg){ fprintf(bright, "%d", out); fclose(bright); } +void do_get(const char* device){ + fprintf(stdout, "%s: %f\n", device, get_brightness(backlight_file)); +} +void do_list(struct string_array* names){ + for(int i = 0;i < names->size;i++) + fprintf(stdout, "%s\n", names->list[i]); +} + +void individual_device_loop(struct arg_values* a){ + for(struct arg_values* curr = a->next;curr;curr = curr->next){ + if(chdir(curr->device)){ + fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, curr->device); + continue; + } + switch(curr->operation){ + case OP_SET: + case OP_INC: + case OP_DEC: + do_assignment(curr); + break; + case OP_GET: + do_get(curr->device); + break; + } + } +} +void all_device_loop(struct string_array* backlight_names, struct arg_values* args){ + switch(args->operation){ + case OP_SET: + case OP_INC: + case OP_DEC: + for(int i = 0;i < backlight_names->size;i++){ + if(chdir(backlight_names->list[i])){ + fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names->list[i]); + continue; + } + do_assignment(args); + } + break; + case OP_LIST: + do_list(backlight_names); + break; + case OP_GET: + for(int i = 0;i < backlight_names->size;i++){ + if(chdir(backlight_names->list[i])){ + fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names->list[i]); + continue; + } + fprintf(stdout, "%s: %f\n", backlight_names->list[i], get_brightness(backlight_file)); + } + break; + } +} + -//argv[1] shall be [+-=]|min|max|off int main(int argc, char** argv){ struct arg_values args; //A linked list of devices and the requested settings. @@ -136,6 +189,7 @@ int main(int argc, char** argv){ return RETVAL_INVALID_DEVICE; } + //Make sure all explicit devices actually exist for(struct arg_values* curr = args.next;curr;curr = curr->next){ for(int i = 0;i < backlight_names.size;i++){ @@ -149,13 +203,16 @@ int main(int argc, char** argv){ continue_outer:; } + //save our starting directory so we can change to each backlight directory (makes logic easier) char* starting_dir = getcwd(NULL, 0); + //Redefine CLEANUP to include freeing of the starting_dir string #undef CLEANUP #define CLEANUP() do{free_string_array(&backlight_names);free_cmd_args(&args);free(starting_dir);}while(0) + //Change to the base directory for all sysfs backlights if(chdir(backlight_dir)){ fprintf(stderr, "Unable to read backlight sysfs directory!\n"); @@ -163,54 +220,15 @@ int main(int argc, char** argv){ return RETVAL_INVALID_DIR; } - //If args.next is not NULL, then 1+ devices were specified. Only apply change to those + + //Run selected operation if(args.next){ - for(struct arg_values* curr = args.next;curr;curr = curr->next){ - if(chdir(curr->device)){ - fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, curr->device); - continue; - } - switch(curr->operation){ - case OP_SET: - case OP_INC: - case OP_DEC: - write_delta(curr); - break; - case OP_GET: - fprintf(stdout, "%s: %f\n", curr->device, get_brightness(backlight_file)); - break; - } - } - //Otherise, apply delta to all backlights + individual_device_loop(&args); }else{ - switch(args.operation){ - case OP_SET: - case OP_INC: - case OP_DEC: - for(int i = 0;i < backlight_names.size;i++){ - if(chdir(backlight_names.list[i])){ - fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names.list[i]); - continue; - } - write_delta(&args); - } - break; - case OP_LIST: - for(int i = 0;i < backlight_names.size;i++){ - fprintf(stdout, "%s\n", backlight_names.list[i]); - } - break; - case OP_GET: - for(int i = 0;i < backlight_names.size;i++){ - if(chdir(backlight_names.list[i])){ - fprintf(stderr, "Unable to open backlight directory \"%s%s\"!\n", backlight_dir, backlight_names.list[i]); - continue; - } - fprintf(stdout, "%s: %f\n", backlight_names.list[i], get_brightness(backlight_file)); - } - break; - } + all_device_loop(&backlight_names, &args); } + + //Return to start directory if(chdir(starting_dir)){ CLEANUP();