Skip to content

Commit

Permalink
Initial/preliminary refactor for thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
HalosGhost committed Apr 15, 2018
1 parent 67a39a4 commit a86b3b6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ZSHDIR ?= $(DESTDIR)$(PREFIX)/share/zsh
BSHDIR ?= $(DESTDIR)$(PREFIX)/share/bash-completions

CC = clang
CFLAGS ?= -g -O2 -fPIE -pie -flto -D_FORTIFY_SOURCE=2 -fstack-protector-strong --param=ssp-buffer-size=1 -Weverything -Werror -std=c11 -fsanitize=undefined -fsanitize-trap=undefined -Wl,-z,relro,-z,now
CFLAGS ?= -g -ggdb -O2 -fPIE -pie -flto -D_FORTIFY_SOURCE=2 -fstack-protector-strong --param=ssp-buffer-size=1 -Weverything -Werror -std=c11 -fsanitize=undefined -fsanitize-trap=undefined -Wl,-z,relro,-z,now
CFLAGS += -Wno-disabled-macro-expansion

.PHONY: all clean clang-analyze cov-build install uninstall
Expand Down
44 changes: 32 additions & 12 deletions src/enlighten.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,31 @@ bl_get (const char * path) {
return bness;
}

struct brightness_cmd
bl_cmd_parse (const char * string) {

char sign [] = { 0, 0 }, perc [] = { 0, 0 };
signed bness = 0;

sscanf(string, "%1[+-]", sign);
if ( sscanf(string, "%d", &bness) != 1 ) {
sign[0] = '!';
}
sscanf(string, "%*d%1[%]", perc);

return (struct brightness_cmd ){ .bness = bness
, .sign = sign[0]
, .perc = perc[0] == '%' };
}

unsigned
bl_calc (signed bness, bool sign, bool perc, unsigned cur, unsigned max) {
bl_calc (struct brightness_cmd cmd, unsigned cur, unsigned max) {

if ( perc ) {
bness = bness * (signed )max / 100;
if ( cmd.perc ) {
cmd.bness = cmd.bness * (signed )max / 100;
}

return (unsigned )(bness + sign * (signed )cur);
return (unsigned )(cmd.bness + (cmd.sign != '!') * (signed )cur);
}

void
Expand Down Expand Up @@ -69,11 +86,11 @@ main (signed argc, const char * argv []) {
dev = dev ? dev : D_DEV;

const char * thresh_top = 0;
thresh_top = getenv("BACKLIGHT_THRESHOLD_TOP");
thresh_top = getenv("BACKLIGHT_THRESHOLD_MAX");
thresh_top = thresh_top ? thresh_top : THRESH_TOP;

const char * thresh_bot = 0;
thresh_bot = getenv("BACKLIGHT_THRESHOLD_BOT");
thresh_bot = getenv("BACKLIGHT_THRESHOLD_MIN");
thresh_bot = thresh_bot ? thresh_bot : THRESH_BOT;

size_t blen = 33 + strlen(dev);
Expand Down Expand Up @@ -110,18 +127,21 @@ main (signed argc, const char * argv []) {
}
}

char sign [] = { 0, 0 }, perc [] = { 0, 0 };
signed bness = 0;
sscanf(argv[1], "%1[+-]", sign);
if ( sscanf(argv[1], "%d", &bness) != 1 ) {
struct brightness_cmd cmd = bl_cmd_parse(argv[1]);
if ( cmd.sign == '!' ) {
fputs(USAGE_STR, stderr);
status = EXIT_FAILURE;
goto cleanup;
}

sscanf(argv[1], "%*d%1[%]", perc);
unsigned ciel = bl_calc(bl_cmd_parse(thresh_top), 0, max),
floo = bl_calc(bl_cmd_parse(thresh_bot), 0, max);

unsigned nbness = bl_calc(cmd, cur, max);

nbness = nbness > ciel ? ciel :
nbness < floo ? floo : nbness;

unsigned nbness = bl_calc(bness, sign[0], perc[0] == '%', cur, max);
bl_set(bpath, nbness);

cleanup:
Expand Down
16 changes: 12 additions & 4 deletions src/enlighten.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,26 @@ static const char USAGE_STR [] =
" if + or - is specified, increment or\n"
" decrement accordingly\n\n"
" if % is specified, treat int as a\n"
" percentage of the max brightness\n\n"
"set the BACKLIGHT_DEVICE environment variable\n"
"to specify a device name at runtime\n";
" percentage of the max brightness\n"
"\nsee " PROGNAME "(1) for more usage information\n"
;

struct brightness_cmd {
signed bness;
signed sign: 16, perc: 16;
};

void
bl_set (const char *, unsigned);

unsigned
bl_get (const char *);

struct brightness_cmd
bl_cmd_parse (const char *);

unsigned
bl_calc (signed, bool, bool, unsigned, unsigned);
bl_calc (struct brightness_cmd, unsigned, unsigned);

void
bl_list (const char *);

0 comments on commit a86b3b6

Please sign in to comment.