From dbfa9b9d12bb0c6969b326ea2c39bd2a1a24dc49 Mon Sep 17 00:00:00 2001 From: rexy712 Date: Thu, 6 Dec 2018 13:37:01 -0800 Subject: [PATCH] Made restoration files optional --- CMakeLists.txt | 35 +++++++++++++++++++++++------------ include/common.h | 2 -- include/config.h.in | 2 ++ src/cmd.c | 13 ++++++++++--- src/common.c | 1 + src/restore.c | 7 ++++--- src/rexbacklight.c | 45 +++++++++++++++++++++++++++++++++------------ 7 files changed, 73 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index affbc50..a3beb62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,10 +6,6 @@ project(rexbacklight) set(rexbacklight_VERSION_MAJOR 1) set(rexbacklight_VERSION_MINOR 3) set(INCLUDE_PATH ${CMAKE_SOURCE_DIR}/include) -configure_file( - "${INCLUDE_PATH}/config.h.in" - "${INCLUDE_PATH}/config.h" -) #set project include directory include_directories("${INCLUDE_PATH}") @@ -17,6 +13,7 @@ include_directories("${INCLUDE_PATH}") #setup cmake options option(BUILD_REXLEDCTL "Build led control program" ON) option(BUILD_REXBACKLIGHT "Build backlight control program" ON) +option(ENABLE_RESTORE_FILE "Enable backlight restoration from generated save file" ON) option(XBACKLIGHT_COMPAT "Use xbacklight style options (eg -get -inc -dec)" OFF) CMAKE_DEPENDENT_OPTION(INSTALL_UDEV_LED_RULE "Install the udev rule to allow users of video group to control led devices" ON "BUILD_REXLEDCTL" OFF) @@ -30,19 +27,31 @@ if(XBACKLIGHT_COMPAT) endif() #locate rjp library requirements -find_library(RJP_LIB rjp) -find_path(RJP_HEADER_DIR rjp.h) +if(ENABLE_RESTORE_FILE) + find_library(RJP_LIB rjp) + find_path(RJP_HEADER_DIR rjp.h) + #temporary library (no actual library generated) + add_library(common_srcs OBJECT src/cmd.c src/common.c src/restore.c) + set(enable_RESTORE_FILE "#define ENABLE_RESTORE_FILE") +else() + add_library(common_srcs OBJECT src/cmd.c src/common.c) + set(enable_RESTORE_FILE "") +endif() -#temporary library (no actual library generated) -add_library(common_srcs OBJECT src/cmd.c src/common.c src/restore.c) +configure_file( + "${INCLUDE_PATH}/config.h.in" + "${INCLUDE_PATH}/config.h" +) if(BUILD_REXLEDCTL) add_executable (rexledctl src/rexbacklight.c) add_dependencies(rexledctl common_srcs) #force common_srcs to be built first target_compile_definitions(rexledctl PRIVATE REXLEDCTL) #define REXLEDCTL in C files - target_link_libraries(rexledctl PRIVATE "${RJP_LIB}") #link with rjp + if(ENABLE_RESTORE_FILE) + target_link_libraries(rexledctl PRIVATE "${RJP_LIB}") #link with rjp + target_include_directories(rexledctl PUBLIC "${RJP_HEADER_DIR}") #include rjp.h directory + endif() target_link_libraries(rexledctl PRIVATE $) #link with the common_srcs "library" - target_include_directories(rexledctl PUBLIC "${RJP_HEADER_DIR}") #include rjp.h directory install(TARGETS rexledctl RUNTIME DESTINATION bin) if(INSTALL_UDEV_LED_RULE) install(FILES ${CMAKE_SOURCE_DIR}/rules/91-leds.rules DESTINATION ${UDEV_DIR}) @@ -52,9 +61,11 @@ if(BUILD_REXBACKLIGHT) add_executable (rexbacklight src/rexbacklight.c) add_dependencies(rexledctl common_srcs) target_compile_definitions(rexbacklight PRIVATE REXBACKLIGHT) - target_link_libraries(rexbacklight PRIVATE "${RJP_LIB}") + if(ENABLE_RESTORE_FILE) + target_link_libraries(rexbacklight PRIVATE "${RJP_LIB}") + target_include_directories(rexbacklight PUBLIC "${RJP_HEADER_DIR}") + endif() target_link_libraries(rexbacklight PRIVATE $) - target_include_directories(rexbacklight PUBLIC "${RJP_HEADER_DIR}") install(TARGETS rexbacklight RUNTIME DESTINATION bin) if(INSTALL_UDEV_BACKLIGHT_RULE) install(FILES ${CMAKE_SOURCE_DIR}/rules/91-backlight.rules DESTINATION ${UDEV_DIR}) diff --git a/include/common.h b/include/common.h index ec2af52..63ffd70 100644 --- a/include/common.h +++ b/include/common.h @@ -16,8 +16,6 @@ along with this program, If not, see . */ -#include - #ifndef REXBACKLIGHT_COMMON_H #define REXBACKLIGHT_COMMON_H diff --git a/include/config.h.in b/include/config.h.in index 1f88fb3..4cc19e0 100644 --- a/include/config.h.in +++ b/include/config.h.in @@ -22,5 +22,7 @@ #define REXBACKLIGHT_VERSION_MAJOR @rexbacklight_VERSION_MAJOR@ #define REXBACKLIGHT_VERSION_MINOR @rexbacklight_VERSION_MINOR@ +@enable_RESTORE_FILE@ + #endif diff --git a/src/cmd.c b/src/cmd.c index aa9c43b..5ffeee7 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -16,12 +16,13 @@ along with this program. If not, see . */ -#include -#include -#include +#include //fprintf +#include //strcmp +#include //malloc, free, strtol, atof #include "cmd.h" #include "common.h" +#include "config.h" #define NO_OPT NULL #define GET_LONG_OPT "--get" @@ -94,7 +95,9 @@ struct cmd_arg rexbacklight_args[] = { {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} }; @@ -108,7 +111,9 @@ struct cmd_arg rexbacklight_args[] = { {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} }; @@ -202,9 +207,11 @@ struct arg_values process_cmd_args(int argc, char** argv){ ret.next = NULL; return ret; } +#ifdef ENABLE_RESTORE_FILE else if(CHECK_OPTION(RESTORE, argv[i])){ ret.operation = OP_RESTORE; } +#endif else if(!strcmp(argv[i], "max")){ curr->operation = OP_SET; curr->delta = 100; diff --git a/src/common.c b/src/common.c index 97e4cee..de3fc51 100644 --- a/src/common.c +++ b/src/common.c @@ -22,6 +22,7 @@ #include "globals.h" #include //printf +#include //exit int return_value = RETVAL_SUCCESS; diff --git a/src/restore.c b/src/restore.c index 37a6bf4..32c2fa7 100644 --- a/src/restore.c +++ b/src/restore.c @@ -16,12 +16,13 @@ along with this program. If not, see . */ -#include -#include //strcmp +#include //printf +#include //strcmp, strlen #include -#include //struct passwd +#include //getpwnam #include //getpwnam #include //chdir +#include //malloc, free #include "restore.h" #include "common.h" diff --git a/src/rexbacklight.c b/src/rexbacklight.c index 76f0e78..1f0f31c 100644 --- a/src/rexbacklight.c +++ b/src/rexbacklight.c @@ -16,22 +16,24 @@ along with this program. If not, see . */ -//Needed for usleep to work -#define _DEFAULT_SOURCE - -#include -#include -#include -#include -#include -#include -#include -#include +#include //fprintf +#include //malloc, free +#include //strlen, strcpy +#include //chdir +#include //opendir +#include //opendir +#include //gettimeofday +#include //nanosleep #include "common.h" #include "cmd.h" -#include "restore.h" #include "globals.h" +#include "config.h" + +#ifdef ENABLE_RESTORE_FILE +#include +#include "restore.h" +#endif //name of the program being run so that we print the correct name in the usage @@ -240,6 +242,7 @@ int individual_device_op(struct arg_values* curr){ return return_value; } +#ifdef ENABLE_RESTORE_FILE //If devices were specified, this function will run void individual_device_loop(struct arg_values* a){ struct arg_values* curr; @@ -258,6 +261,18 @@ void individual_device_loop(struct arg_values* a){ } rjp_free_value(root); } +#else +//If devices were specified, this function will run +void individual_device_loop(struct arg_values* a){ + struct arg_values* curr; + int try_restore = 1; + for(curr = a->next;curr;curr = curr->next){ + return_value = individual_device_op(curr); + if(return_value == RETVAL_INTERNAL_ERROR) + return; + } +} +#endif //If no devices were specified, this function will run @@ -280,7 +295,9 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args) return; } } +#ifdef ENABLE_RESTORE_FILE save_restore_file(device_names); +#endif break; case OP_LIST: do_list(device_names); @@ -300,9 +317,11 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args) } } break; +#ifdef ENABLE_RESTORE_FILE case OP_RESTORE:; all_restore(); break; +#endif default: fprintf(stderr, "Internal processing error!\n"); break; @@ -365,7 +384,9 @@ int main(int argc, char** argv){ //Run selected operation if(args.next){ individual_device_loop(&args); +#ifdef ENABLE_RESTORE_FILE save_restore_file(&device_names); +#endif }else{ all_device_loop(&device_names, &args); }