Cleaned up option management in cmd.c and moved a restore operation from rexbacklight.c to restore.c
This commit is contained in:
parent
eab07e49cf
commit
00062612f5
@ -26,6 +26,7 @@
|
|||||||
|
|
||||||
void save_restore_file(struct string_array* devices);
|
void save_restore_file(struct string_array* devices);
|
||||||
int restore_to_delta(struct arg_values* curr, RJP_value** root, int* try_restore);
|
int restore_to_delta(struct arg_values* curr, RJP_value** root, int* try_restore);
|
||||||
|
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);
|
||||||
|
|
||||||
|
|||||||
61
src/cmd.c
61
src/cmd.c
@ -71,7 +71,7 @@
|
|||||||
#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"
|
||||||
|
|
||||||
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)
|
||||||
return 1;
|
return 1;
|
||||||
if(!two)
|
if(!two)
|
||||||
@ -79,46 +79,34 @@ int strcmp_handle_null(const char* one, const char* two){
|
|||||||
return strcmp(one, two);
|
return strcmp(one, two);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define CHECK_SHORT_OPTION(opt, arg) (!strcmp(opt##_SHORT_OPT, arg))
|
#define CHECK_SHORT_OPTION(opt, arg) (!strcmp_handle_null(opt##_SHORT_OPT, arg))
|
||||||
#define CHECK_LONG_OPTION(opt, arg) (!strcmp(opt##_LONG_OPT, arg))
|
#define CHECK_LONG_OPTION(opt, arg) (!strcmp_handle_null(opt##_LONG_OPT, arg))
|
||||||
#define CHECK_XBACK_OPTION(opt, arg) (!strcmp(opt##_XBACK_OPT, arg))
|
#define CHECK_XBACK_OPTION(opt, arg) (!strcmp_handle_null(opt##_XBACK_OPT, arg))
|
||||||
|
|
||||||
#ifdef XBACKLIGHT_COMPAT_OPTIONS
|
#ifdef XBACKLIGHT_COMPAT_OPTIONS
|
||||||
|
#define OPTION(x) {x##_LONG_OPT, x##_SHORT_OPT, x##_XBACK_OPT, x##_DESC}
|
||||||
#define CHECK_OPTION(opt, arg) (!strcmp_handle_null(opt##_LONG_OPT, arg) || !strcmp_handle_null(opt##_SHORT_OPT, arg) || !strcmp_handle_null(opt##_XBACK_OPT, arg))
|
#define CHECK_OPTION(opt, arg) (CHECK_LONG_OPTION(opt, arg) || CHECK_SHORT_OPTION(opt, arg) || CHECK_XBACK_OPTION(opt, arg))
|
||||||
struct cmd_arg rexbacklight_args[] = {
|
|
||||||
{DEVICE_LONG_OPT, DEVICE_SHORT_OPT, DEVICE_XBACK_OPT, DEVICE_DESC},
|
|
||||||
{NO_OPT, NO_OPT, SET_XBACK_OPT, SET_DESC},
|
|
||||||
{NO_OPT, NO_OPT, INC_XBACK_OPT, INC_DESC},
|
|
||||||
{NO_OPT, NO_OPT, DEC_XBACK_OPT, DEC_DESC},
|
|
||||||
{FADE_LONG_OPT, FADE_SHORT_OPT, FADE_XBACK_OPT, FADE_DESC},
|
|
||||||
{STEPS_LONG_OPT, STEPS_SHORT_OPT, STEPS_XBACK_OPT, STEPS_DESC},
|
|
||||||
{GET_LONG_OPT, GET_SHORT_OPT, GET_XBACK_OPT, GET_DESC},
|
|
||||||
{LIST_LONG_OPT, LIST_SHORT_OPT, LIST_XBACK_OPT, LIST_DESC},
|
|
||||||
#ifdef ENABLE_RESTORE_FILE
|
|
||||||
{RESTORE_LONG_OPT, RESTORE_SHORT_OPT, RESTORE_XBACK_OPT, RESTORE_DESC},
|
|
||||||
#endif
|
|
||||||
{HELP_LONG_OPT, HELP_SHORT_OPT, HELP_XBACK_OPT, HELP_DESC},
|
|
||||||
{VERSION_LONG_OPT, NO_OPT, VERSION_XBACK_OPT, VERSION_DESC}
|
|
||||||
};
|
|
||||||
|
|
||||||
#else //XBACKLIGHT_COMPAT_OPTIONS
|
#else //XBACKLIGHT_COMPAT_OPTIONS
|
||||||
|
#define OPTION(x) {x##_LONG_OPT, x##_SHORT_OPT, x##_DESC}
|
||||||
#define CHECK_OPTION(opt, arg) (!strcmp_handle_null(opt##_LONG_OPT, arg) || !strcmp_handle_null(opt##_SHORT_OPT, arg))
|
#define CHECK_OPTION(opt, arg) (CHECK_LONG_OPTION(opt, arg) || CHECK_SHORT_OPTION(opt, arg))
|
||||||
struct cmd_arg rexbacklight_args[] = {
|
|
||||||
{DEVICE_LONG_OPT, DEVICE_SHORT_OPT, DEVICE_DESC},
|
|
||||||
{FADE_LONG_OPT, FADE_SHORT_OPT, FADE_DESC},
|
|
||||||
{STEPS_LONG_OPT, STEPS_SHORT_OPT, STEPS_DESC},
|
|
||||||
{GET_LONG_OPT, GET_SHORT_OPT, GET_DESC},
|
|
||||||
{LIST_LONG_OPT, LIST_SHORT_OPT, LIST_DESC},
|
|
||||||
#ifdef ENABLE_RESTORE_FILE
|
|
||||||
{RESTORE_LONG_OPT, RESTORE_SHORT_OPT, RESTORE_DESC},
|
|
||||||
#endif
|
|
||||||
{HELP_LONG_OPT, HELP_SHORT_OPT, HELP_DESC},
|
|
||||||
{VERSION_LONG_OPT, NO_OPT, VERSION_DESC}
|
|
||||||
};
|
|
||||||
#endif //XBACKLIGHT_COMPAT_OPTIONS
|
#endif //XBACKLIGHT_COMPAT_OPTIONS
|
||||||
|
|
||||||
|
struct cmd_arg rexbacklight_args[] = {
|
||||||
|
OPTION(DEVICE),
|
||||||
|
OPTION(SET),
|
||||||
|
OPTION(INC),
|
||||||
|
OPTION(DEC),
|
||||||
|
OPTION(FADE),
|
||||||
|
OPTION(STEPS),
|
||||||
|
OPTION(GET),
|
||||||
|
OPTION(LIST),
|
||||||
|
#ifdef ENABLE_RESTORE_FILE
|
||||||
|
OPTION(RESTORE),
|
||||||
|
#endif
|
||||||
|
OPTION(HELP),
|
||||||
|
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
|
||||||
@ -312,6 +300,7 @@ struct arg_values process_cmd_args(int argc, char** argv){
|
|||||||
}
|
}
|
||||||
#undef CHECK_OPTION
|
#undef CHECK_OPTION
|
||||||
#undef UNRECOGNIZED_OPTION
|
#undef UNRECOGNIZED_OPTION
|
||||||
|
#undef OPTION
|
||||||
|
|
||||||
//Process an operation
|
//Process an operation
|
||||||
int process_op(struct arg_values* arg, float min, float current, float max){
|
int process_op(struct arg_values* arg, float min, float current, float max){
|
||||||
|
|||||||
15
src/common.c
15
src/common.c
@ -35,6 +35,9 @@ void io_error_2(const char* error, const char* type, const char* name, const cha
|
|||||||
void io_error_3(const char* error, const char* type, const char* name, const char* name2, const char* name3){
|
void io_error_3(const char* error, const char* type, const char* name, const char* name2, const char* name3){
|
||||||
fprintf(stderr, "Unable to %s %s '%s%s%s'!\n", error, type, name, name2, name3);
|
fprintf(stderr, "Unable to %s %s '%s%s%s'!\n", error, type, name, name2, name3);
|
||||||
}
|
}
|
||||||
|
void mem_error(void){
|
||||||
|
fprintf(stderr, "Failed to allocate memory! Unable to continue!\n");
|
||||||
|
}
|
||||||
|
|
||||||
//Clean up a string array
|
//Clean up a string array
|
||||||
void free_string_array(struct string_array* s){
|
void free_string_array(struct string_array* s){
|
||||||
@ -44,10 +47,6 @@ void free_string_array(struct string_array* s){
|
|||||||
free(s->list);
|
free(s->list);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mem_error(void){
|
|
||||||
fprintf(stderr, "Failed to allocate memory! Unable to continue!\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
_Noreturn void version(void){
|
_Noreturn void version(void){
|
||||||
printf("%s version %s\n", executable_name(), REXBACKLIGHT_VERSION);
|
printf("%s version %s\n", executable_name(), REXBACKLIGHT_VERSION);
|
||||||
exit(return_value);
|
exit(return_value);
|
||||||
@ -76,11 +75,15 @@ _Noreturn void usage(int exit_val){
|
|||||||
if(rexbacklight_args[i].xopt){
|
if(rexbacklight_args[i].xopt){
|
||||||
if(printed)
|
if(printed)
|
||||||
printf("|");
|
printf("|");
|
||||||
|
else
|
||||||
|
printed = 1;
|
||||||
printf("%s", rexbacklight_args[i].xopt);
|
printf("%s", rexbacklight_args[i].xopt);
|
||||||
}
|
}
|
||||||
#endif //XBACKLIGHT_COMPAT_OPTIONS
|
#endif //XBACKLIGHT_COMPAT_OPTIONS
|
||||||
printf("\n");
|
if(printed){
|
||||||
printf(" %s\n", rexbacklight_args[i].desc);
|
printf("\n");
|
||||||
|
printf(" %s\n", rexbacklight_args[i].desc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("Arguments:\n");
|
printf("Arguments:\n");
|
||||||
|
|||||||
@ -112,6 +112,14 @@ int restore_to_delta(struct arg_values* curr, RJP_value** root, int* try_restore
|
|||||||
curr->delta = rjp_value_dfloat(match);
|
curr->delta = rjp_value_dfloat(match);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
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){
|
||||||
|
restore_to_delta(curr, &root, &try_restore);
|
||||||
|
}
|
||||||
|
rjp_free_value(root);
|
||||||
|
}
|
||||||
|
|
||||||
void save_restore_file(struct string_array* devices){
|
void save_restore_file(struct string_array* devices){
|
||||||
RJP_value* root = rjp_init_json();
|
RJP_value* root = rjp_init_json();
|
||||||
|
|||||||
@ -31,30 +31,38 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#ifdef ENABLE_RESTORE_FILE
|
#ifdef ENABLE_RESTORE_FILE
|
||||||
#include <rjp.h>
|
|
||||||
#include "restore.h"
|
#include "restore.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
#ifdef REXBACKLIGHT
|
#ifdef REXBACKLIGHT
|
||||||
|
|
||||||
//This is where backlight devices can be found in sysfs
|
//This is where backlight devices can be found in sysfs
|
||||||
const char* device_dir(void){return "/sys/class/backlight/";}
|
const char* device_dir(void){return "/sys/class/backlight/";}
|
||||||
//name of the program being run so that we print the correct name in the usage
|
//name of the program being run so that we print the correct name in the usage
|
||||||
const char* executable_name(void){return "rexbacklight";}
|
const char* executable_name(void){return "rexbacklight";}
|
||||||
//location of the restore file in the home directory
|
//location of the restore file in the home directory
|
||||||
const char* restore_file_suffix(void){return "/.config/rexbacklight.json";}
|
const char* restore_file_suffix(void){return "/.config/rexbacklight.json";}
|
||||||
|
|
||||||
#elif defined(REXLEDCTL)
|
#elif defined(REXLEDCTL)
|
||||||
|
|
||||||
//This is where led devices can be found in sysfs
|
//This is where led devices can be found in sysfs
|
||||||
const char* device_dir(void){return "/sys/class/leds/";}
|
const char* device_dir(void){return "/sys/class/leds/";}
|
||||||
const char* executable_name(void){return "rexledctl";}
|
const char* executable_name(void){return "rexledctl";}
|
||||||
const char* restore_file_suffix(void){return "/.config/rexledctl.json";}
|
const char* restore_file_suffix(void){return "/.config/rexledctl.json";}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
#error "UNDEFINED PROGRAM NAME!"
|
#error "UNDEFINED PROGRAM NAME!"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char* brightness_file(void){return "brightness";}
|
const char* brightness_file(void){return "brightness";}
|
||||||
const char* max_brightness_file(void){return "max_brightness";}
|
const char* max_brightness_file(void){return "max_brightness";}
|
||||||
|
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
//Get a list of led/backlight devices in sysfs
|
//Get a list of led/backlight devices in sysfs
|
||||||
struct string_array get_device_sources(void){
|
struct string_array get_device_sources(void){
|
||||||
DIR* fd;
|
DIR* fd;
|
||||||
@ -80,7 +88,7 @@ struct string_array get_device_sources(void){
|
|||||||
for(arr.size = 0;(dir = readdir(fd));){
|
for(arr.size = 0;(dir = readdir(fd));){
|
||||||
int slen;
|
int slen;
|
||||||
//ignore '.' and '..'
|
//ignore '.' and '..'
|
||||||
if(!strncmp(dir->d_name, ".", 1) || !strncmp(dir->d_name, "..", 2)){
|
if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")){
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,6 +146,7 @@ float get_brightness(const char* file){
|
|||||||
fclose(bright);
|
fclose(bright);
|
||||||
return atof(buff);
|
return atof(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
//return milliseconds since last boot
|
//return milliseconds since last boot
|
||||||
double get_time(void){
|
double get_time(void){
|
||||||
struct timespec t;
|
struct timespec t;
|
||||||
@ -257,20 +266,6 @@ void extract_next_arg(struct arg_values* dest, struct arg_values* src){
|
|||||||
dest->next->next = NULL;
|
dest->next->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ENABLE_RESTORE_FILE
|
|
||||||
//convert all restore operations to set operations
|
|
||||||
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){
|
|
||||||
restore_to_delta(curr, &root, &try_restore);
|
|
||||||
}
|
|
||||||
rjp_free_value(root);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
#define prep_restore(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//remove all args in src which have the specified operation and append them to dest
|
//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* extract_operation(int operation, struct arg_values* dest, struct arg_values* src){
|
||||||
struct arg_values* curr_dest = dest;
|
struct arg_values* curr_dest = dest;
|
||||||
@ -286,7 +281,7 @@ struct arg_values* extract_operation(int operation, struct arg_values* dest, str
|
|||||||
return curr_dest;
|
return curr_dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
//If devices were specified, this function will run
|
//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;
|
struct arg_values* curr;
|
||||||
struct arg_values current_op_args = {0};
|
struct arg_values current_op_args = {0};
|
||||||
@ -302,8 +297,10 @@ void run_device_operations(struct arg_values* a){
|
|||||||
free_cmd_args(¤t_op_args);
|
free_cmd_args(¤t_op_args);
|
||||||
|
|
||||||
//assignment
|
//assignment
|
||||||
|
#ifdef ENABLE_RESTORE_FILE
|
||||||
curr = extract_operation(OP_RESTORE, ¤t_op_args, a);
|
curr = extract_operation(OP_RESTORE, ¤t_op_args, a);
|
||||||
prep_restore(current_op_args.next);
|
prep_restore(current_op_args.next);
|
||||||
|
#endif
|
||||||
extract_operation(OP_SET | OP_INC | OP_DEC, curr, a);
|
extract_operation(OP_SET | OP_INC | OP_DEC, curr, a);
|
||||||
return_value = do_assignment(current_op_args.next);
|
return_value = do_assignment(current_op_args.next);
|
||||||
free_cmd_args(¤t_op_args);
|
free_cmd_args(¤t_op_args);
|
||||||
@ -344,7 +341,7 @@ int main(int argc, char** argv){
|
|||||||
//If there are no led/backlights, we can't do anything
|
//If there are no led/backlights, we can't do anything
|
||||||
if(device_names.size == 0){
|
if(device_names.size == 0){
|
||||||
#ifdef REXBACKLIGHT
|
#ifdef REXBACKLIGHT
|
||||||
fprintf(stderr, "No backlights devices found!\n");
|
fprintf(stderr, "No backlight devices found!\n");
|
||||||
#elif defined(REXLEDCTL)
|
#elif defined(REXLEDCTL)
|
||||||
fprintf(stderr, "No led devices found!\n");
|
fprintf(stderr, "No led devices found!\n");
|
||||||
#endif
|
#endif
|
||||||
@ -375,13 +372,13 @@ int main(int argc, char** argv){
|
|||||||
return RETVAL_INVALID_DIR;
|
return RETVAL_INVALID_DIR;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Run selected operation
|
//on a global operation, populate list with all devices
|
||||||
if(!args.next){
|
if(!args.next){
|
||||||
populate_args(&args, &device_names);
|
populate_args(&args, &device_names);
|
||||||
}
|
}
|
||||||
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);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
free_string_array(&device_names);
|
free_string_array(&device_names);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user