backlight/main.c
Владимир 7d8ba1a72a modified: Makefile
modified:   config.h
	modified:   main.c
2024-11-09 12:52:59 +05:00

103 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
//
int get_backlight(const char* file)
{
FILE *fd_m;
if((fd_m = fopen(file, "r")) == NULL)
{
printf("Error open %s\n", file);
return 1;
}
char buf[10];
fscanf(fd_m, "%s", buf);
if(fd_m != NULL)
fclose(fd_m);
return atoi(buf);
}
//
int set_backlight(const char* file, int value)
{
FILE *fd;
if((fd = fopen(file, "w")) == NULL)
{
printf("Error open %s\n", file);
return 2;
}
printf("%d --> %s\n", value, file);
int r = fprintf(fd, "%d", value);
if(fd != NULL)
fclose(fd);
return r;
}
//
int min_max(int value, int min, int max)
{
if(value < min)
value = min;
if(value > max)
value = max;
return value;
}
//
int change(char* arg, char direction)
{
float max = (float)get_backlight(max_file);
float current_value = (float)get_backlight(target_file);
int proc = atoi(arg);
float one_proc = max/100.0;
int new_value = current_value + direction*proc*one_proc;
set_backlight(target_file, min_max(new_value, 0, max));
return 0;
}
//
int main(int argc, char** argv)
{
if(argc == 3)
{
if(strcmp(argv[1], "set") == 0)
{
float max = (float)get_backlight(max_file);
float new = (float)atoi(argv[2]);
int value = (int)(max*new/100.0);
set_backlight(target_file, min_max(value, 0, max));
}
if(strcmp(argv[1], "dec") == 0)
change(argv[2], -1);
if(strcmp(argv[1], "inc") == 0)
change(argv[2], 1);
}
if(argc == 2)
{
if(strcmp(argv[1], "get") == 0)
printf("%d/%d\n", get_backlight(target_file), get_backlight(max_file));
if(strcmp(argv[1], "-h") == 0)
printf("%s\n", help);
if(strcmp(argv[1], "--help") == 0)
printf("%s\n", help);
}
return 0;
}