1
0
mirror of https://github.com/Dejvino/pinephone-toolkit.git synced 2024-11-21 14:12:35 +00:00

Add percentage backlight

This commit is contained in:
Dejvino 2021-01-03 11:28:39 +01:00
parent 51b6fa81ab
commit 0deaf8473a

View File

@ -2,17 +2,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdbool.h>
static char *argv0 = "./backlight"; static char *argv0 = "./backlight";
static char *backlight_file = "/sys/devices/platform/backlight/backlight/backlight/brightness"; static char *backlight_file = "/sys/devices/platform/backlight/backlight/backlight/brightness";
static char *max_backlight_file = "/sys/devices/platform/backlight/backlight/backlight/max_brightness";
int max_brightness = 10;
void print_usage() void print_usage()
{ {
printf("usage: %s COMMAND [VALUE]\n", argv0); printf("usage: %s COMMAND [VALUE]\n", argv0);
printf(" COMMAND = {get, set}\n"); printf(" COMMAND = {get, set}\n");
printf(" get ... prints current backlight level\n"); printf(" get ... prints current raw backlight level\n");
printf(" set ... sets backlight level to VALUE\n"); printf(" get_percent ... prints current percentage backlight level\n");
printf(" VALUE = [0..10] ... backlight amount\n"); printf(" set ... sets backlight level to raw VALUE_RAW\n");
printf(" set_percent ... sets backlight level to percentage VALUE_PCT\n");
printf(" VALUE_RAW = [0..%d]\n", max_brightness);
printf(" VALUE_PCT = [0..100]\n");
} }
void print_usage_and_fail() void print_usage_and_fail()
@ -21,7 +28,19 @@ void print_usage_and_fail()
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void command_get() void read_max_brightness()
{
FILE *fp = fopen(max_backlight_file, "r");
if (fp == NULL) {
fprintf(stderr, "Cannot open file '%s' for reading. Using the default max brightness '%d'.\n",
max_backlight_file, max_brightness);
return;
}
fscanf(fp, "%d", &max_brightness);
fclose(fp);
}
void command_get(bool percent)
{ {
FILE *fp = fopen(backlight_file, "r"); FILE *fp = fopen(backlight_file, "r");
if (fp == NULL) { if (fp == NULL) {
@ -30,13 +49,17 @@ void command_get()
} }
int buffer_length = 255; int buffer_length = 255;
char buffer[buffer_length]; char buffer[buffer_length];
while (fgets(buffer, buffer_length, fp)) { fgets(buffer, buffer_length, fp);
if (percent) {
int value = atoi(buffer);
printf("%d\n", value * 100 / max_brightness);
} else {
printf("%s", buffer); printf("%s", buffer);
} }
fclose(fp); fclose(fp);
} }
void command_set(int value) void command_set(int value, bool percent)
{ {
if (setuid(0)) { if (setuid(0)) {
fprintf(stderr, "Cannot set root permissions via setuid(0).\n"); fprintf(stderr, "Cannot set root permissions via setuid(0).\n");
@ -48,12 +71,14 @@ void command_set(int value)
fprintf(stderr, "Cannot open file '%s' for writing.\n", backlight_file); fprintf(stderr, "Cannot open file '%s' for writing.\n", backlight_file);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fprintf(fp, "%d\n", value); int raw = percent ? (value * max_brightness / 100) : value;
fprintf(fp, "%d\n", raw);
fclose(fp); fclose(fp);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
read_max_brightness();
if (argc < 1) { if (argc < 1) {
print_usage_and_fail(); print_usage_and_fail();
} }
@ -63,15 +88,24 @@ int main(int argc, char *argv[])
} }
char *command = argv[1]; char *command = argv[1];
if ((strcmp(command, "get") == 0) && (argc == 2)) { if ((strcmp(command, "get") == 0) && (argc == 2)) {
command_get(); command_get(false);
} else if ((strcmp(command, "get_percent") == 0) && (argc == 2)) {
command_get(true);
} else if ((strcmp(command, "set") == 0) && (argc == 3)) { } else if ((strcmp(command, "set") == 0) && (argc == 3)) {
int value = atoi(argv[2]); int value = atoi(argv[2]);
if (value < 0 || value > 10) { if (value < 0 || value > max_brightness) {
print_usage_and_fail(); print_usage_and_fail();
} }
command_set(value); command_set(value, false);
} else if ((strcmp(command, "set_percent") == 0) && (argc == 3)) {
int value = atoi(argv[2]);
if (value < 0 || value > 100) {
print_usage_and_fail();
}
command_set(value, true);
} else { } else {
print_usage_and_fail(); print_usage_and_fail();
} }
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }