From 047eccda8160cfbdddb231e388ec0e47432065ec Mon Sep 17 00:00:00 2001 From: Mykyta Holubakha Date: Fri, 2 Aug 2019 15:18:04 +0300 Subject: Documented new changes Rewritten some contributed code --- brightnessctl.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'brightnessctl.c') diff --git a/brightnessctl.c b/brightnessctl.c index ab87ef3..f46b2bd 100644 --- a/brightnessctl.c +++ b/brightnessctl.c @@ -305,46 +305,43 @@ void list_devices(struct device **devs) { print_device(dev); } -float val_to_percent(long val,struct device *d) { - if (val < 0) return 0; - return powf(powf(100,p.exponent)*val/d->max_brightness,1.0f/p.exponent); +float val_to_percent(long val, struct device *d) { + if (val < 0) + return 0; + return powf(powf(100, p.exponent) * val / d->max_brightness, 1.0f / p.exponent); } -unsigned long percent_to_val(float percent,struct device *d) { - float r = (powf(percent,p.exponent)*d->max_brightness)*(powf(100,-p.exponent)); - return r < 0 ? 0 : (unsigned long)r; +unsigned long percent_to_val(float percent, struct device *d) { + float r = (powf(percent, p.exponent) * d->max_brightness) * powf(100, -p.exponent); + return (unsigned long) r; } void print_device(struct device *dev) { - char *format = p.mach ? "%s,%s,%d,%d%%,%d\n": + char *format = p.mach ? "%s,%s,%d,%d%%,%d\n" : "Device '%s' of class '%s':\n\tCurrent brightness: %d (%d%%)\n\tMax brightness: %d\n\n"; fprintf(stdout, format, dev->id, dev->class, dev->curr_brightness, - (int) val_to_percent(dev->curr_brightness,dev), + (int) val_to_percent(dev->curr_brightness, dev), dev->max_brightness); } void apply_value(struct device *d, struct value *val) { - long new; + long new = d->curr_brightness; if (val->d_type == DIRECT) { - if(val->v_type == ABSOLUTE) - new = val->val > d->max_brightness ? d->max_brightness : val->val; - else - new = percent_to_val(val->val,d); - } else {//DELTA - if(val->v_type == ABSOLUTE) { - new = d->curr_brightness + (val->sign == PLUS ? val->val : -val->val); - } else { - new = percent_to_val(val_to_percent(d->curr_brightness,d) + ((long)(val->sign == PLUS ? val->val : -val->val)),d); - if(new == d->curr_brightness) { - if(val->sign == PLUS) - new += 1; - else - new -= 1; - } - } + new = val->v_type == ABSOLUTE ? val->val : percent_to_val(val->val, d); + goto apply; + } + long mod = val->val; + if (val->sign == MINUS) + mod *= -1; + if (val->v_type == RELATIVE) { + mod = percent_to_val(val_to_percent(d->curr_brightness, d) + mod, d) - d->curr_brightness; + if (val->val != 0 && mod == 0) + mod = val->sign == PLUS ? 1 : -1; } + new += mod; +apply: if (new < p.min) new = p.min; if (new < 0) @@ -598,9 +595,7 @@ Options:\n\ -p, --pretend\t\t\tdo not perform write operations.\n\ -m, --machine-readable\tproduce machine-readable output.\n\ -n, --min-value\t\tset minimum brightness, defaults to 1.\n\ - -e, --exponent\t\tchange percent value mapping to polynominal.\n\ - \t\t\t%%=x^k*max*100^-k, k defaults to 4.\n\ - \t\t\tTry changing the value until the changes \"feel\" equal.\n\ + -e, --exponent[=K]\t\tchanges percentage curve to exponential.\n\ -s, --save\t\t\tsave previous state in a temporary file.\n\ -r, --restore\t\t\trestore previous saved state.\n\ -h, --help\t\t\tprint this help.\n\ -- cgit v1.2.3