Added option to not write to restore file and fixed a lot of restore file issues

This commit is contained in:
rexy712 2018-12-16 14:27:58 -08:00
parent 3c2e3f35a0
commit 87b7dd2503
5 changed files with 137 additions and 139 deletions

View File

@ -29,6 +29,9 @@
#define OP_VERSION 254 #define OP_VERSION 254
#define OP_USAGE 255 #define OP_USAGE 255
#define ARG_FLAG_NONE 0
#define ARG_FLAG_NO_SAVE 1
struct cmd_arg{ struct cmd_arg{
const char* lopt; const char* lopt;
const char* sopt; const char* sopt;
@ -48,7 +51,11 @@ struct arg_values{
//NULL means all devices //NULL means all devices
const char* device; const char* device;
//What value to put in the backlight file //starting brightness
int act_start;
//value to write in backlight file
int act_delta;
//output percentage
float delta; float delta;
//How many seconds to transition //How many seconds to transition
@ -60,6 +67,7 @@ struct arg_values{
int operation; int operation;
int num_values; int num_values;
}; };
int flags;
}; };

View File

@ -24,8 +24,8 @@
#include "common.h" #include "common.h"
#include "cmd.h" #include "cmd.h"
void save_restore_file(struct string_array* devices); void save_restore_file(struct string_array* devices, struct arg_values* args);
int restore_to_delta(struct arg_values* curr, RJP_value** root, int* try_restore); int restore_to_delta(struct arg_values* curr);
void prep_restore(struct arg_values* a); void prep_restore(struct arg_values* a);
RJP_value* find_matching_json_device(const char* name, RJP_value* root); RJP_value* find_matching_json_device(const char* name, RJP_value* root);
RJP_value* read_restore_file(const char* file); RJP_value* read_restore_file(const char* file);

View File

@ -58,6 +58,9 @@
#define RESTORE_LONG_OPT "--restore" #define RESTORE_LONG_OPT "--restore"
#define RESTORE_SHORT_OPT "-R" #define RESTORE_SHORT_OPT "-R"
#define RESTORE_XBACK_OPT NO_OPT #define RESTORE_XBACK_OPT NO_OPT
#define NO_SAVE_LONG_OPT "--no-save"
#define NO_SAVE_SHORT_OPT "-N"
#define NO_SAVE_XBACK_OPT NO_OPT
#define DEVICE_DESC "select which device to control" #define DEVICE_DESC "select which device to control"
#define FADE_DESC "change brightness over time interval" #define FADE_DESC "change brightness over time interval"
@ -70,6 +73,7 @@
#define INC_DESC "increase backlight device by specified value" #define INC_DESC "increase backlight device by specified value"
#define DEC_DESC "decrease backlight device by specified value" #define DEC_DESC "decrease backlight device by specified value"
#define RESTORE_DESC "reassign previously saved device values" #define RESTORE_DESC "reassign previously saved device values"
#define NO_SAVE_DESC "do not write any data to the restore file"
static inline int strcmp_handle_null(const char* one, const char* two){ static inline int strcmp_handle_null(const char* one, const char* two){
if(!one) if(!one)
@ -102,13 +106,14 @@ struct cmd_arg rexbacklight_args[] = {
OPTION(LIST), OPTION(LIST),
#ifdef ENABLE_RESTORE_FILE #ifdef ENABLE_RESTORE_FILE
OPTION(RESTORE), OPTION(RESTORE),
OPTION(NO_SAVE),
#endif #endif
OPTION(HELP), OPTION(HELP),
OPTION(VERSION) OPTION(VERSION)
}; };
int rexbacklight_args_length = sizeof(rexbacklight_args) / sizeof(rexbacklight_args[0]); int rexbacklight_args_length = sizeof(rexbacklight_args) / sizeof(rexbacklight_args[0]);
\
//Clean up a cmd_arg struct //Clean up a cmd_arg struct
void free_cmd_args(struct arg_values* a){ void free_cmd_args(struct arg_values* a){
if(!a->next) if(!a->next)
@ -135,7 +140,7 @@ void free_cmd_args(struct arg_values* a){
}while(0); }while(0);
struct arg_values init_arg_values(void){ struct arg_values init_arg_values(void){
return (struct arg_values){.next = NULL, .device = NULL, .delta = 0, .fade_duration = 0, .fade_steps = 20, .operation = 0}; return (struct arg_values){.next = NULL, .device = NULL, .delta = 0, .fade_duration = 0, .fade_steps = 20, .operation = 0, .flags = 0};
} }
//Convert command line arguments to flags //Convert command line arguments to flags
@ -191,14 +196,14 @@ struct arg_values process_cmd_args(int argc, char** argv){
} }
else if(CHECK_OPTION(LIST, argv[i])){ else if(CHECK_OPTION(LIST, argv[i])){
free_cmd_args(&ret); curr->operation = OP_LIST;
ret.operation = OP_LIST;
ret.next = NULL;
return ret;
} }
#ifdef ENABLE_RESTORE_FILE #ifdef ENABLE_RESTORE_FILE
else if(CHECK_OPTION(RESTORE, argv[i])){ else if(CHECK_OPTION(RESTORE, argv[i])){
ret.operation = OP_RESTORE; curr->operation = OP_RESTORE;
}
else if(CHECK_OPTION(NO_SAVE, argv[i])){
curr->flags |= ARG_FLAG_NO_SAVE;
} }
#endif #endif
else if(!strcmp(argv[i], "max")){ else if(!strcmp(argv[i], "max")){
@ -290,7 +295,9 @@ struct arg_values process_cmd_args(int argc, char** argv){
curr->operation = ret.operation; curr->operation = ret.operation;
if(!curr->delta) if(!curr->delta)
curr->delta = ret.delta; curr->delta = ret.delta;
}
if(curr->flags == ARG_FLAG_NONE){
curr->flags = ret.flags;
} }
} }
} }

View File

@ -66,7 +66,6 @@ RJP_value* read_restore_file(const char* file){
int i; int i;
FILE* fp = fopen(file, "r"); FILE* fp = fopen(file, "r");
if(!fp){ if(!fp){
fprintf(stderr, "Could not restore! No restore file found!\n");
return NULL; return NULL;
} }
@ -83,6 +82,16 @@ RJP_value* read_restore_file(const char* file){
return root; return root;
} }
static RJP_value* restore_file_handle(void){
static RJP_value* rf = NULL;
if(!rf){
char* f = restore_file();
rf = read_restore_file(f);
free(f);
}
return rf;
}
RJP_value* find_matching_json_device(const char* name, RJP_value* root){ RJP_value* find_matching_json_device(const char* name, RJP_value* root){
for(RJP_value* curr = rjp_get_member(root);curr;curr = rjp_next_member(curr)){ for(RJP_value* curr = rjp_get_member(root);curr;curr = rjp_next_member(curr)){
if(!strcmp(rjp_member_name(curr), name)){ if(!strcmp(rjp_member_name(curr), name)){
@ -93,17 +102,13 @@ RJP_value* find_matching_json_device(const char* name, RJP_value* root){
} }
//convert a restoration operation to a set operation //convert a restoration operation to a set operation
int restore_to_delta(struct arg_values* curr, RJP_value** root, int* try_restore){ int restore_to_delta(struct arg_values* curr){
if(!*root && *try_restore){ RJP_value* root = restore_file_handle();
char* tmp = restore_file(); if(!root){
*root = read_restore_file(tmp); fprintf(stderr, "Could not restore! No restore file found!\n");
free(tmp); return 0;
if(!*root){
*try_restore = 0;
return 0;
}
} }
RJP_value* match = find_matching_json_device(curr->device, *root); RJP_value* match = find_matching_json_device(curr->device, root);
if(!match){ if(!match){
fprintf(stderr, "No matching device '%s' in restore file!\n", curr->device); fprintf(stderr, "No matching device '%s' in restore file!\n", curr->device);
return 0; return 0;
@ -113,43 +118,38 @@ int restore_to_delta(struct arg_values* curr, RJP_value** root, int* try_restore
return 1; return 1;
} }
void prep_restore(struct arg_values* a){ void prep_restore(struct arg_values* a){
RJP_value* root = 0;
int try_restore = 1;
for(struct arg_values* curr = a;curr;curr = curr->next){ for(struct arg_values* curr = a;curr;curr = curr->next){
restore_to_delta(curr, &root, &try_restore); restore_to_delta(curr);
} }
rjp_free_value(root);
} }
void save_restore_file(struct string_array* devices){ void save_restore_file(struct string_array* devices, struct arg_values* args){
RJP_value* root = rjp_init_json(); RJP_value* rf = restore_file_handle();
for(size_t i = 0;i < devices->size;++i){ if(!rf)
if(chdir(devices->list[i])){ rf = rjp_init_json();
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), devices->list[i]); for(RJP_value* mem = rjp_get_member(rf);mem;mem = rjp_next_member(mem)){
return_value = RETVAL_INVALID_DIR; for(struct arg_values* curr = args->next, *prev = args;curr;prev = curr, curr = curr->next){
continue; if(curr->operation != OP_SET || curr->flags & ARG_FLAG_NO_SAVE){
} prev->next = curr->next;
size_t esc_name_len = rjp_escape_strlen(devices->list[i]); free(curr);
char* esc_str = rjp_alloc(esc_name_len + 1); curr = prev;
rjp_escape_strcpy(esc_str, devices->list[i]); continue;
}
float curr = get_brightness(brightness_file()); if(!strcmp(rjp_member_name(mem), curr->device)){
float max = get_brightness(max_brightness_file()); rjp_set_value(mem, rjp_dfloat(curr->delta));
if(return_value == RETVAL_INVALID_FILE && (!curr || !max)){ prev->next = curr->next;
rjp_add_member(root, 0, esc_str, 0, rjp_null()); free(curr);
}else{ break;
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); for(struct arg_values* curr = args->next;curr;curr = curr->next){
if(curr->operation == OP_SET)
rjp_add_member(rf, curr->device, 0, rjp_dfloat(curr->delta));
}
free_cmd_args(args);
char* tmp = rjp_to_json(rf);
char* rfil = restore_file(); char* rfil = restore_file();
FILE* restf = fopen(rfil, "w"); FILE* restf = fopen(rfil, "w");
if(!restf){ if(!restf){
@ -160,7 +160,7 @@ void save_restore_file(struct string_array* devices){
} }
free(rfil); free(rfil);
rjp_free(tmp); rjp_free(tmp);
rjp_free_value(root); rjp_free_value(rf);
} }

View File

@ -163,14 +163,14 @@ void sleep_for(double time){
} }
//update brightness incrementally over requested millisecond time interval //update brightness incrementally over requested millisecond time interval
void fade_out(struct arg_values* arg, int iv, int fv){ void fade_out(struct arg_values* arg){
FILE* fd; FILE* fd;
double start, end; double start, end;
double tdelta = 0; //amount of time that has passed in ms double tdelta = 0; //amount of time that has passed in ms
float value = iv; //current value to write to file float value = arg->act_start; //current value to write to file
double step_delta = (double)arg->fade_duration / arg->fade_steps; double step_delta = (double)arg->fade_duration / arg->fade_steps;
double step_inc = step_delta; double step_inc = step_delta;
float brdelta = (float)(fv - iv) / arg->fade_steps; //amount brightness needs to change per iteration float brdelta = (float)(arg->act_delta - arg->act_start) / arg->fade_steps; //amount brightness needs to change per iteration
while(arg->fade_duration > tdelta){ while(arg->fade_duration > tdelta){
//write //write
@ -205,106 +205,89 @@ void fade_out(struct arg_values* arg, int iv, int fv){
return_value = RETVAL_INVALID_FILE; return_value = RETVAL_INVALID_FILE;
return; return;
} }
fprintf(fd, "%d", fv); fprintf(fd, "%d", arg->act_delta);
fclose(fd); fclose(fd);
if(chdir(device_dir())){
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
return_value = RETVAL_INVALID_DIR;
return;
}
} }
//Write value to device files int prep_offset(struct arg_values* args){
int do_assignment(struct arg_values* args){ if(chdir(args->device)){
for(;args;args = args->next){ io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), args->device);
if(chdir(args->device)){ return RETVAL_INVALID_DIR;
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), args->device);
return_value = RETVAL_INVALID_DIR;
continue;
}
int start = get_brightness(brightness_file());
int out = process_op(args, 0, start, get_brightness(max_brightness_file()));
fade_out(args, start, out);
if(chdir(device_dir())){
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
return RETVAL_INVALID_DIR;
}
} }
return return_value; args->act_start = get_brightness(brightness_file());
float act_max = get_brightness(max_brightness_file());
args->act_delta = process_op(args, 0, args->act_start, act_max);
float start_pec = (args->act_start / act_max) * 100.0;
if(args->operation == OP_INC){
args->delta = start_pec + args->delta;
}else if(args->operation == OP_DEC){
args->delta = start_pec - args->delta;
}
if(args->delta > 100)
args->delta = 100;
else if(args->delta < 0)
args->delta = 0;
args->operation = OP_SET;
//chdir back in do_assignment
return 0;
} }
//Run get operation //Run get operation
int do_get(struct arg_values* args){ int do_get(struct arg_values* args){
for(;args;args = args->next){ float curr, max;
float curr, max; if(chdir(args->device)){
if(chdir(args->device)){ io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), args->device);
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), args->device); return RETVAL_INVALID_DIR;
return_value = RETVAL_INVALID_DIR; }
continue; curr = get_brightness(brightness_file());
} max = get_brightness(max_brightness_file());
curr = get_brightness(brightness_file()); if(return_value == RETVAL_INVALID_FILE && (!curr || !max)){
max = get_brightness(max_brightness_file()); fprintf(stdout, "%s", args->device);
if(return_value == RETVAL_INVALID_FILE && (!curr || !max)){ }else{
fprintf(stdout, "%s", args->device); fprintf(stdout, "%-25s %.2f\n", args->device, curr / max * 100);
}else{ }
fprintf(stdout, "%-25s %.2f\n", args->device, curr / max * 100); if(chdir(device_dir())){
} io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
if(chdir(device_dir())){ return RETVAL_INVALID_DIR;
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
return RETVAL_INVALID_DIR;
}
} }
return return_value; return return_value;
} }
//Run list operation //Run list operation
void do_list(struct arg_values* args){ void do_list(struct arg_values* args){
for(;args;args = args->next){ printf("%s\n", args->device);
printf("%s\n", args->device);
}
}
//move next arg in src list to next arg in dest list
void extract_next_arg(struct arg_values* dest, struct arg_values* src){
dest->next = src->next;
src->next = src->next->next;
dest->next->next = NULL;
}
//remove all args in src which have the specified operation and append them to dest
struct arg_values* extract_operation(int operation, struct arg_values* dest, struct arg_values* src){
struct arg_values* curr_dest = dest;
dest->next = NULL;
for(struct arg_values* curr_src = src;curr_src->next;){
if(curr_src->next->operation & operation){
extract_next_arg(curr_dest, curr_src);
curr_dest = curr_dest->next;
}else{
curr_src = curr_src->next;
}
}
return curr_dest;
} }
//Run requested operation on a per device basis //Run requested operation on a per device basis
void run_device_operations(struct arg_values* a){ void run_device_operations(struct arg_values* a){
struct arg_values* curr; for(a = a->next;a;a = a->next){
struct arg_values current_op_args = {0}; switch(a->operation){
case OP_LIST:
//list do_list(a);
extract_operation(OP_LIST, &current_op_args, a); break;
do_list(current_op_args.next); case OP_GET:
free_cmd_args(&current_op_args); return_value = do_get(a);
break;
//get
extract_operation(OP_GET, &current_op_args, a);
return_value = do_get(current_op_args.next);
free_cmd_args(&current_op_args);
//assignment
#ifdef ENABLE_RESTORE_FILE #ifdef ENABLE_RESTORE_FILE
curr = extract_operation(OP_RESTORE, &current_op_args, a); case OP_RESTORE:
prep_restore(current_op_args.next); if(!restore_to_delta(a)){
continue;
}
#endif #endif
extract_operation(OP_SET | OP_INC | OP_DEC, curr, a); case OP_INC:
return_value = do_assignment(current_op_args.next); case OP_DEC:
free_cmd_args(&current_op_args); case OP_SET:
if(prep_offset(a))
break;
fade_out(a);
break;
};
}
} }
//If a global operation is requested, run this to fill arg with all devices, each with the global operation //If a global operation is requested, run this to fill arg with all devices, each with the global operation
@ -317,6 +300,7 @@ void populate_args(struct arg_values* arg, struct string_array* devices){
curr->next->fade_duration = arg->fade_duration; curr->next->fade_duration = arg->fade_duration;
curr->next->fade_steps = arg->fade_steps; curr->next->fade_steps = arg->fade_steps;
curr->next->operation = arg->operation; curr->next->operation = arg->operation;
curr->next->flags = arg->flags;
curr = curr->next; curr = curr->next;
} }
curr->next = NULL; curr->next = NULL;
@ -378,10 +362,9 @@ int main(int argc, char** argv){
} }
run_device_operations(&args); run_device_operations(&args);
#ifdef ENABLE_RESTORE_FILE #ifdef ENABLE_RESTORE_FILE
save_restore_file(&device_names); save_restore_file(&device_names, &args); //performs cleanup on args
#endif #endif
free_string_array(&device_names); free_string_array(&device_names);
free_cmd_args(&args);
return return_value; return return_value;
} }