writing to restore file

This commit is contained in:
rexy712 2018-12-01 10:10:40 -08:00
parent 904abe3294
commit 38793bf575

View File

@ -227,7 +227,7 @@ void do_get(const char* device){
if(return_value == RETVAL_INVALID_FILE && (!curr || !max)){
fprintf(stdout, "%s", device);
}else{
fprintf(stdout, "%-25s %.2f\n", device, get_brightness(brightness_file) / get_brightness(max_brightness_file) * 100);
fprintf(stdout, "%-25s %.2f\n", device, curr / max * 100);
}
}
@ -319,11 +319,11 @@ void individual_device_loop(struct arg_values* a){
}
return_value = individual_device_op(curr);
if(return_value == RETVAL_INTERNAL_ERROR){
rjp_free(root);
rjp_free_value(root);
return;
}
}
rjp_free(root);
rjp_free_value(root);
}
void process_restore_file(void){
@ -339,7 +339,7 @@ void process_restore_file(void){
tmp.delta = rjp_value_dfloat(curr);
individual_device_op(&tmp);
}
rjp_free(root);
rjp_free_value(root);
}
//If no devices were specified, this function will run
@ -390,6 +390,45 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args)
}
}
void save_restore_file(struct string_array* devices){
RJP_value* root = rjp_init_json();
for(size_t i = 0;i < devices->size;++i){
if(chdir(devices->list[i])){
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir, devices->list[i]);
return_value = RETVAL_INVALID_DIR;
continue;
}
size_t esc_name_len = rjp_escape_strlen(devices->list[i]);
char* esc_str = rjp_alloc(esc_name_len + 1);
rjp_escape_strcpy(esc_str, devices->list[i]);
float curr = get_brightness(brightness_file);
float max = get_brightness(max_brightness_file);
if(return_value == RETVAL_INVALID_FILE && (!curr || !max)){
rjp_add_member(root, 0, esc_str, 0, rjp_null());
}else{
rjp_add_member(root, 0, esc_str, 0, rjp_dfloat(curr/max*100));
}
if(chdir(device_dir)){
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir);
return_value = RETVAL_INVALID_DIR;
rjp_free_value(root);
free(esc_str);
return;
}
}
char* tmp = rjp_to_json(root);
FILE* restf = fopen(restore_file, "w");
if(!restf){
io_error(IO_ERROR_OPEN, IO_ERROR_FILE, restore_file);
}else{
fprintf(restf, "//File generated by rexbacklight\n%s\n", tmp);
fclose(restf);
}
rjp_free(tmp);
rjp_free_value(root);
}
int main(int argc, char** argv){
@ -450,6 +489,7 @@ int main(int argc, char** argv){
}else{
all_device_loop(&device_names, &args);
}
save_restore_file(&device_names);
CLEANUP();
return return_value;