Moved restore file logic into its own file
This commit is contained in:
parent
82369b4d67
commit
abb03dcd40
@ -34,7 +34,7 @@ find_library(RJP_LIB rjp)
|
|||||||
find_path(RJP_HEADER_DIR rjp.h)
|
find_path(RJP_HEADER_DIR rjp.h)
|
||||||
|
|
||||||
#temporary library (no actual library generated)
|
#temporary library (no actual library generated)
|
||||||
add_library(common_srcs OBJECT src/cmd.c src/common.c)
|
add_library(common_srcs OBJECT src/cmd.c src/common.c src/restore.c)
|
||||||
|
|
||||||
if(BUILD_REXLEDCTL)
|
if(BUILD_REXLEDCTL)
|
||||||
add_executable (rexledctl src/rexbacklight.c)
|
add_executable (rexledctl src/rexbacklight.c)
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef RECBACKLIGHT_CMD_H
|
#ifndef REXBACKLIGHT_CMD_H
|
||||||
#define REXBACKLIGHT_CMD_H
|
#define REXBACKLIGHT_CMD_H
|
||||||
|
|
||||||
#define OP_INC 1
|
#define OP_INC 1
|
||||||
|
|||||||
@ -30,11 +30,26 @@
|
|||||||
#define RETVAL_INTERNAL_ERROR 7
|
#define RETVAL_INTERNAL_ERROR 7
|
||||||
#define RETVAL_SUCCESS EXIT_SUCCESS
|
#define RETVAL_SUCCESS EXIT_SUCCESS
|
||||||
|
|
||||||
|
#define IO_ERROR_DIR "directory"
|
||||||
|
#define IO_ERROR_FILE "file"
|
||||||
|
#define IO_ERROR_OPEN "open"
|
||||||
|
#define IO_ERROR_READ "read"
|
||||||
|
#define IO_ERROR_WRITE "write to"
|
||||||
|
|
||||||
|
void io_error(const char* error, const char* type, const char* name);
|
||||||
|
void io_error_2(const char* error, const char* type, const char* name, const char* name2);
|
||||||
|
void io_error_3(const char* error, const char* type, const char* name, const char* name2, const char* name3);
|
||||||
|
|
||||||
|
|
||||||
extern int return_value;
|
extern int return_value;
|
||||||
|
|
||||||
|
struct string_array{
|
||||||
|
char** list;
|
||||||
|
int size;
|
||||||
|
};
|
||||||
|
|
||||||
void mem_error(void);
|
void mem_error(void);
|
||||||
_Noreturn void version(void);
|
_Noreturn void version(void);
|
||||||
_Noreturn void usage(int exit_val);
|
_Noreturn void usage(int exit_val);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
28
include/globals.h
Normal file
28
include/globals.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
/**
|
||||||
|
rexbacklight
|
||||||
|
Copyright (C) 2018 rexy712
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GLOBALS_H
|
||||||
|
#define GLOBALS_H
|
||||||
|
|
||||||
|
const char* device_dir(void);
|
||||||
|
const char* executable_name(void);
|
||||||
|
const char* restore_file(void);
|
||||||
|
const char* brightness_file(void);
|
||||||
|
const char* max_brightness_file(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
36
include/restore.h
Normal file
36
include/restore.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
rexbacklight
|
||||||
|
Copyright (C) 2018 rexy712
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef RESTORE_H
|
||||||
|
#define RESTORE_H
|
||||||
|
|
||||||
|
#include <rjp.h>
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
#include "cmd.h"
|
||||||
|
|
||||||
|
void save_restore_file(struct string_array* devices);
|
||||||
|
void process_restore_file(void);
|
||||||
|
int prep_restore(struct arg_values* curr, RJP_value** root, int* try_restore);
|
||||||
|
RJP_value* find_matching_json_device(const char* name, RJP_value* root);
|
||||||
|
RJP_value* read_restore_file(const char* file);
|
||||||
|
char* get_home_folder();
|
||||||
|
char* get_home_restore_file();
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
27
include/rexbacklight.h
Normal file
27
include/rexbacklight.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
rexbacklight
|
||||||
|
Copyright (C) 2018 rexy712
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef REXBACKLIGHT_H
|
||||||
|
#define REXBACKLIGHT_H
|
||||||
|
|
||||||
|
#include "cmd.h"
|
||||||
|
|
||||||
|
float get_brightness(const char* file);
|
||||||
|
int individual_device_op(struct arg_values* curr);
|
||||||
|
|
||||||
|
#endif
|
||||||
25
src/common.c
25
src/common.c
@ -19,27 +19,36 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
|
#include "globals.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h> //printf
|
||||||
|
|
||||||
int return_value = RETVAL_SUCCESS;
|
int return_value = RETVAL_SUCCESS;
|
||||||
|
|
||||||
|
void io_error(const char* error, const char* type, const char* name){
|
||||||
|
fprintf(stderr, "Unable to %s %s '%s'!\n", error, type, name);
|
||||||
|
}
|
||||||
|
void io_error_2(const char* error, const char* type, const char* name, const char* name2){
|
||||||
|
fprintf(stderr, "Unable to %s %s '%s%s'!\n", error, type, name, name2);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void mem_error(void){
|
void mem_error(void){
|
||||||
fprintf(stderr, "Failed to allocate memory! Unable to continue!\n");
|
fprintf(stderr, "Failed to allocate memory! Unable to continue!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
//name of the program being run so that we print the correct name in the usage
|
|
||||||
extern const char* executable_name;
|
|
||||||
|
|
||||||
_Noreturn void version(void){
|
_Noreturn void version(void){
|
||||||
printf("%s version %d.%d\n", executable_name, REXBACKLIGHT_VERSION_MAJOR, REXBACKLIGHT_VERSION_MINOR);
|
printf("%s version %d.%d\n", executable_name(), REXBACKLIGHT_VERSION_MAJOR, REXBACKLIGHT_VERSION_MINOR);
|
||||||
exit(return_value);
|
exit(return_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
_Noreturn void usage(int exit_val){
|
_Noreturn void usage(int exit_val){
|
||||||
int i;
|
int i;
|
||||||
printf("%s version %d.%d\n\n", executable_name, REXBACKLIGHT_VERSION_MAJOR, REXBACKLIGHT_VERSION_MINOR);
|
printf("%s version %d.%d\n\n", executable_name(), REXBACKLIGHT_VERSION_MAJOR, REXBACKLIGHT_VERSION_MINOR);
|
||||||
printf("Usage: %s [argument] [options] [argument]\n\n", executable_name);
|
printf("Usage: %s [argument] [options] [argument]\n\n", executable_name());
|
||||||
|
|
||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
for(i = 0;i < rexbacklight_args_length;++i){
|
for(i = 0;i < rexbacklight_args_length;++i){
|
||||||
@ -74,7 +83,7 @@ _Noreturn void usage(int exit_val){
|
|||||||
printf(" max\n");
|
printf(" max\n");
|
||||||
printf(" min\n");
|
printf(" min\n");
|
||||||
|
|
||||||
printf("\n%s Copyright (C) 2018 rexy712\n", executable_name);
|
printf("\n%s Copyright (C) 2018 rexy712\n", executable_name());
|
||||||
printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
|
printf("This program comes with ABSOLUTELY NO WARRANTY.\n");
|
||||||
printf("This is free software, and you are welcome to redistribute it\n");
|
printf("This is free software, and you are welcome to redistribute it\n");
|
||||||
printf("under certain conditions; see the GNU GPLv3 for details.\n");
|
printf("under certain conditions; see the GNU GPLv3 for details.\n");
|
||||||
|
|||||||
136
src/restore.c
Normal file
136
src/restore.c
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
/**
|
||||||
|
rexbacklight
|
||||||
|
Copyright (C) 2018 rexy712
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h> //stat, mkdir
|
||||||
|
#include <sys/stat.h> //stat, mkdir
|
||||||
|
#include <unistd.h> //stat, mkdir
|
||||||
|
#include <string.h> //strcmp
|
||||||
|
#include <rjp.h>
|
||||||
|
|
||||||
|
#include "restore.h"
|
||||||
|
#include "common.h"
|
||||||
|
#include "cmd.h"
|
||||||
|
#include "globals.h"
|
||||||
|
#include "rexbacklight.h"
|
||||||
|
|
||||||
|
RJP_value* read_restore_file(const char* file){
|
||||||
|
size_t filesize;
|
||||||
|
char* file_contents;
|
||||||
|
int i;
|
||||||
|
FILE* fp = fopen(file, "r");
|
||||||
|
if(!fp){
|
||||||
|
fprintf(stderr, "Could not restore! No restore file found!\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
filesize = ftell(fp);
|
||||||
|
fseek(fp, 0, SEEK_SET);
|
||||||
|
|
||||||
|
file_contents = malloc(filesize+1);
|
||||||
|
i = fread(file_contents, filesize, 1, fp);
|
||||||
|
fclose(fp);
|
||||||
|
file_contents[filesize] = 0;
|
||||||
|
RJP_value* root = rjp_parse(file_contents);
|
||||||
|
free(file_contents);
|
||||||
|
return 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)){
|
||||||
|
if(!strcmp(rjp_member_name(curr), name)){
|
||||||
|
return curr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
int prep_restore(struct arg_values* curr, RJP_value** root, int* try_restore){
|
||||||
|
if(!*root && *try_restore){
|
||||||
|
*root = read_restore_file(restore_file());
|
||||||
|
if(!*root){
|
||||||
|
*try_restore = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
RJP_value* match = find_matching_json_device(curr->device, *root);
|
||||||
|
if(!match){
|
||||||
|
fprintf(stderr, "No matching device '%s' in restore file!\n", curr->device);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
curr->operation = OP_SET;
|
||||||
|
curr->delta = rjp_value_dfloat(match);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
void process_restore_file(void){
|
||||||
|
RJP_value* root = read_restore_file(restore_file());
|
||||||
|
if(!root)
|
||||||
|
return;
|
||||||
|
struct arg_values tmp = {.next = NULL,
|
||||||
|
.fade_duration = 0,
|
||||||
|
.fade_steps = 1,
|
||||||
|
.operation = OP_SET};
|
||||||
|
for(RJP_value* curr = rjp_get_member(root);curr;curr = rjp_next_member(curr)){
|
||||||
|
tmp.device = rjp_member_name(curr);
|
||||||
|
tmp.delta = rjp_value_dfloat(curr);
|
||||||
|
individual_device_op(&tmp);
|
||||||
|
}
|
||||||
|
rjp_free_value(root);
|
||||||
|
}
|
||||||
|
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 %s\n%s\n", executable_name(), tmp);
|
||||||
|
fclose(restf);
|
||||||
|
}
|
||||||
|
rjp_free(tmp);
|
||||||
|
rjp_free_value(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -30,46 +30,26 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "cmd.h"
|
#include "cmd.h"
|
||||||
|
#include "restore.h"
|
||||||
|
#include "globals.h"
|
||||||
|
|
||||||
//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
|
||||||
|
|
||||||
#ifdef REXBACKLIGHT
|
#ifdef REXBACKLIGHT
|
||||||
//This is where backlight devices can be found in sysfs
|
//This is where backlight devices can be found in sysfs
|
||||||
static const char* device_dir = "/sys/class/backlight/";
|
const char* device_dir(void){return "/sys/class/backlight/";}
|
||||||
const char* executable_name = "rexbacklight";
|
const char* executable_name(void){return "rexbacklight";}
|
||||||
static const char* restore_file = "/var/tmp/rexbacklight.json";
|
const char* restore_file(void){return "/var/tmp/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
|
||||||
static const char* device_dir = "/sys/class/leds";
|
const char* device_dir(void){return "/sys/class/leds/";}
|
||||||
const char* executable_name = "rexledctl";
|
const char* executable_name(void){return "rexledctl";}
|
||||||
static const char* restore_file = "/var/tmp/rexledctl.json";
|
const char* restore_file(void){return "/var/tmp/rexledctl.json";}
|
||||||
#else
|
#else
|
||||||
#error "UNDEFINED PROGRAM NAME!"
|
#error "UNDEFINED PROGRAM NAME!"
|
||||||
#endif
|
#endif
|
||||||
static const char* brightness_file = "brightness";
|
const char* brightness_file(void){return "brightness";}
|
||||||
static const char* max_brightness_file = "max_brightness";
|
const char* max_brightness_file(void){return "max_brightness";}
|
||||||
|
|
||||||
//Used to store a dynamic array of dynamic strings and number of strings
|
|
||||||
struct string_array{
|
|
||||||
char** list;
|
|
||||||
int size;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define IO_ERROR_DIR "directory"
|
|
||||||
#define IO_ERROR_FILE "file"
|
|
||||||
#define IO_ERROR_OPEN "open"
|
|
||||||
#define IO_ERROR_READ "read"
|
|
||||||
#define IO_ERROR_WRITE "write to"
|
|
||||||
|
|
||||||
void io_error(const char* error, const char* type, const char* name){
|
|
||||||
fprintf(stderr, "Unable to %s %s '%s'!\n", error, type, name);
|
|
||||||
}
|
|
||||||
void io_error_2(const char* error, const char* type, const char* name, const char* name2){
|
|
||||||
fprintf(stderr, "Unable to %s %s '%s%s'!\n", error, type, name, name2);
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
//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){
|
||||||
@ -86,9 +66,9 @@ struct string_array get_device_sources(void){
|
|||||||
struct string_array arr = {0};
|
struct string_array arr = {0};
|
||||||
int list_size = 8;
|
int list_size = 8;
|
||||||
|
|
||||||
fd = opendir(device_dir);
|
fd = opendir(device_dir());
|
||||||
if(!fd){
|
if(!fd){
|
||||||
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir);
|
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
|
||||||
return_value = RETVAL_INVALID_DIR;
|
return_value = RETVAL_INVALID_DIR;
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
@ -174,9 +154,9 @@ void fade_out(const char* device_name, int iv, int fv, int ms_duration, int step
|
|||||||
while(ms_duration > tdelta){
|
while(ms_duration > tdelta){
|
||||||
//write
|
//write
|
||||||
gettimeofday(&start, NULL);
|
gettimeofday(&start, NULL);
|
||||||
fd = fopen(brightness_file, "w+");
|
fd = fopen(brightness_file(), "w+");
|
||||||
if(!fd){
|
if(!fd){
|
||||||
io_error_3(IO_ERROR_OPEN, IO_ERROR_FILE, device_dir, device_name, brightness_file);
|
io_error_3(IO_ERROR_OPEN, IO_ERROR_FILE, device_dir(), device_name, brightness_file());
|
||||||
return_value = RETVAL_INVALID_FILE;
|
return_value = RETVAL_INVALID_FILE;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -201,9 +181,9 @@ void fade_out(const char* device_name, int iv, int fv, int ms_duration, int step
|
|||||||
//calc end of next step
|
//calc end of next step
|
||||||
step_delta += step_inc;
|
step_delta += step_inc;
|
||||||
}
|
}
|
||||||
fd = fopen(brightness_file, "w+");
|
fd = fopen(brightness_file(), "w+");
|
||||||
if(!fd){
|
if(!fd){
|
||||||
io_error_3(IO_ERROR_OPEN, IO_ERROR_FILE, device_dir, device_name, brightness_file);
|
io_error_3(IO_ERROR_OPEN, IO_ERROR_FILE, device_dir(), device_name, brightness_file());
|
||||||
return_value = RETVAL_INVALID_FILE;
|
return_value = RETVAL_INVALID_FILE;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -214,16 +194,16 @@ void fade_out(const char* device_name, int iv, int fv, int ms_duration, int step
|
|||||||
|
|
||||||
//Write value to device files
|
//Write value to device files
|
||||||
void do_assignment(struct arg_values* arg, const char* device){
|
void do_assignment(struct arg_values* arg, const char* device){
|
||||||
int start = get_brightness(brightness_file);
|
int start = get_brightness(brightness_file());
|
||||||
int out = process_op(arg, 0, start, get_brightness(max_brightness_file));
|
int out = process_op(arg, 0, start, get_brightness(max_brightness_file()));
|
||||||
fade_out(device, start, out, arg->fade_duration, arg->fade_steps);
|
fade_out(device, start, out, arg->fade_duration, arg->fade_steps);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Run get operation
|
//Run get operation
|
||||||
void do_get(const char* device){
|
void do_get(const char* device){
|
||||||
float curr, max;
|
float curr, max;
|
||||||
curr = get_brightness(brightness_file);
|
curr = get_brightness(brightness_file());
|
||||||
max = get_brightness(max_brightness_file);
|
max = get_brightness(max_brightness_file());
|
||||||
if(return_value == RETVAL_INVALID_FILE && (!curr || !max)){
|
if(return_value == RETVAL_INVALID_FILE && (!curr || !max)){
|
||||||
fprintf(stdout, "%s", device);
|
fprintf(stdout, "%s", device);
|
||||||
}else{
|
}else{
|
||||||
@ -238,31 +218,9 @@ void do_list(struct string_array* names){
|
|||||||
fprintf(stdout, "%s\n", names->list[i]);
|
fprintf(stdout, "%s\n", names->list[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
RJP_value* read_restore_file(const char* file){
|
|
||||||
size_t filesize;
|
|
||||||
char* file_contents;
|
|
||||||
int i;
|
|
||||||
FILE* fp = fopen(file, "r");
|
|
||||||
if(!fp){
|
|
||||||
fprintf(stderr, "Could not restore! No restore file found!\n");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
fseek(fp, 0, SEEK_END);
|
|
||||||
filesize = ftell(fp);
|
|
||||||
fseek(fp, 0, SEEK_SET);
|
|
||||||
|
|
||||||
file_contents = malloc(filesize+1);
|
|
||||||
i = fread(file_contents, filesize, 1, fp);
|
|
||||||
fclose(fp);
|
|
||||||
file_contents[filesize] = 0;
|
|
||||||
RJP_value* root = rjp_parse(file_contents);
|
|
||||||
free(file_contents);
|
|
||||||
return root;
|
|
||||||
}
|
|
||||||
int individual_device_op(struct arg_values* curr){
|
int individual_device_op(struct arg_values* curr){
|
||||||
if(chdir(curr->device)){
|
if(chdir(curr->device)){
|
||||||
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir, curr->device);
|
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), curr->device);
|
||||||
return RETVAL_INVALID_DIR;
|
return RETVAL_INVALID_DIR;
|
||||||
}
|
}
|
||||||
switch(curr->operation){
|
switch(curr->operation){
|
||||||
@ -275,38 +233,13 @@ int individual_device_op(struct arg_values* curr){
|
|||||||
do_get(curr->device);
|
do_get(curr->device);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(chdir(device_dir)){
|
if(chdir(device_dir())){
|
||||||
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir);
|
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
|
||||||
return RETVAL_INTERNAL_ERROR;
|
return RETVAL_INTERNAL_ERROR;
|
||||||
}
|
}
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)){
|
|
||||||
if(!strcmp(rjp_member_name(curr), name)){
|
|
||||||
return curr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
int prep_restore(struct arg_values* curr, RJP_value** root, int* try_restore){
|
|
||||||
if(!*root && *try_restore){
|
|
||||||
*root = read_restore_file(restore_file);
|
|
||||||
if(!*root){
|
|
||||||
*try_restore = 0;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RJP_value* match = find_matching_json_device(curr->device, *root);
|
|
||||||
if(!match){
|
|
||||||
fprintf(stderr, "No matching device '%s' in restore file!\n", curr->device);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
curr->operation = OP_SET;
|
|
||||||
curr->delta = rjp_value_dfloat(match);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
//If devices were specified, this function will run
|
//If devices were specified, this function will run
|
||||||
void individual_device_loop(struct arg_values* a){
|
void individual_device_loop(struct arg_values* a){
|
||||||
struct arg_values* curr;
|
struct arg_values* curr;
|
||||||
@ -326,21 +259,6 @@ void individual_device_loop(struct arg_values* a){
|
|||||||
rjp_free_value(root);
|
rjp_free_value(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_restore_file(void){
|
|
||||||
RJP_value* root = read_restore_file(restore_file);
|
|
||||||
if(!root)
|
|
||||||
return;
|
|
||||||
struct arg_values tmp = {.next = NULL,
|
|
||||||
.fade_duration = 0,
|
|
||||||
.fade_steps = 1,
|
|
||||||
.operation = OP_SET};
|
|
||||||
for(RJP_value* curr = rjp_get_member(root);curr;curr = rjp_next_member(curr)){
|
|
||||||
tmp.device = rjp_member_name(curr);
|
|
||||||
tmp.delta = rjp_value_dfloat(curr);
|
|
||||||
individual_device_op(&tmp);
|
|
||||||
}
|
|
||||||
rjp_free_value(root);
|
|
||||||
}
|
|
||||||
|
|
||||||
//If no devices were specified, this function will run
|
//If no devices were specified, this function will run
|
||||||
void all_device_loop(struct string_array* device_names, struct arg_values* args){
|
void all_device_loop(struct string_array* device_names, struct arg_values* args){
|
||||||
@ -351,13 +269,13 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args)
|
|||||||
case OP_DEC:
|
case OP_DEC:
|
||||||
for(i = 0;i < device_names->size;++i){
|
for(i = 0;i < device_names->size;++i){
|
||||||
if(chdir(device_names->list[i])){
|
if(chdir(device_names->list[i])){
|
||||||
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir, device_names->list[i]);
|
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), device_names->list[i]);
|
||||||
return_value = RETVAL_INVALID_DIR;
|
return_value = RETVAL_INVALID_DIR;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
do_assignment(args, device_names->list[i]);
|
do_assignment(args, device_names->list[i]);
|
||||||
if(chdir(device_dir)){
|
if(chdir(device_dir())){
|
||||||
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir);
|
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
|
||||||
return_value = RETVAL_INVALID_DIR;
|
return_value = RETVAL_INVALID_DIR;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -369,13 +287,13 @@ void all_device_loop(struct string_array* device_names, struct arg_values* args)
|
|||||||
case OP_GET:
|
case OP_GET:
|
||||||
for(i = 0;i < device_names->size;++i){
|
for(i = 0;i < device_names->size;++i){
|
||||||
if(chdir(device_names->list[i])){
|
if(chdir(device_names->list[i])){
|
||||||
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir, device_names->list[i]);
|
io_error_2(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir(), device_names->list[i]);
|
||||||
return_value = RETVAL_INVALID_DIR;
|
return_value = RETVAL_INVALID_DIR;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
do_get(device_names->list[i]);
|
do_get(device_names->list[i]);
|
||||||
if(chdir(device_dir)){
|
if(chdir(device_dir())){
|
||||||
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir);
|
io_error(IO_ERROR_OPEN, IO_ERROR_DIR, device_dir());
|
||||||
return_value = RETVAL_INVALID_DIR;
|
return_value = RETVAL_INVALID_DIR;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -390,46 +308,6 @@ 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 %s\n%s\n", executable_name, tmp);
|
|
||||||
fclose(restf);
|
|
||||||
}
|
|
||||||
rjp_free(tmp);
|
|
||||||
rjp_free_value(root);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
|
|
||||||
struct arg_values args; //A linked list of devices and the requested settings.
|
struct arg_values args; //A linked list of devices and the requested settings.
|
||||||
@ -477,8 +355,8 @@ int main(int argc, char** argv){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Change to the base directory for all sysfs leds/backlights
|
//Change to the base directory for all sysfs leds/backlights
|
||||||
if(chdir(device_dir)){
|
if(chdir(device_dir())){
|
||||||
io_error(IO_ERROR_READ, IO_ERROR_DIR, device_dir);
|
io_error(IO_ERROR_READ, IO_ERROR_DIR, device_dir());
|
||||||
CLEANUP();
|
CLEANUP();
|
||||||
return RETVAL_INVALID_DIR;
|
return RETVAL_INVALID_DIR;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user