-
Notifications
You must be signed in to change notification settings - Fork 52
Support fractional values #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,7 +80,10 @@ enum delta_type { DIRECT, DELTA }; | |
enum sign { PLUS, MINUS }; | ||
|
||
struct value { | ||
unsigned long val; | ||
union { | ||
unsigned long val; | ||
float percentage; | ||
}; | ||
enum value_type v_type; | ||
enum delta_type d_type; | ||
enum sign sign; | ||
|
@@ -101,6 +104,7 @@ struct params { | |
bool percentage; | ||
bool save; | ||
bool restore; | ||
bool frac; | ||
float exponent; | ||
}; | ||
|
||
|
@@ -120,6 +124,7 @@ static const struct option options[] = { | |
{"restore", no_argument, NULL, 'r'}, | ||
{"save", no_argument, NULL, 's'}, | ||
{"version", no_argument, NULL, 'V'}, | ||
{"frac", no_argument, NULL, 0}, | ||
{NULL,} | ||
}; | ||
|
||
|
@@ -189,6 +194,9 @@ int main(int argc, char **argv) { | |
printf("%s\n", VERSION); | ||
exit(0); | ||
break; | ||
case 0: | ||
p.frac = true; | ||
break; | ||
default: | ||
phelp++; | ||
} | ||
|
@@ -283,7 +291,7 @@ int apply_operation(struct device *dev, enum operation operation, struct value * | |
return 0; | ||
case GET: | ||
if (p.percentage) | ||
fprintf(stdout, "%d\n", (int) val_to_percent(dev->curr_brightness, dev, true)); | ||
fprintf(stdout, "%.4g\n", val_to_percent(dev->curr_brightness, dev, !p.frac)); | ||
else | ||
fprintf(stdout, "%u\n", dev->curr_brightness); | ||
return 0; | ||
|
@@ -311,7 +319,7 @@ int apply_operation(struct device *dev, enum operation operation, struct value * | |
} | ||
|
||
bool parse_value(struct value *val, char *str) { | ||
long n; | ||
double n; | ||
char c; | ||
char *buf; | ||
errno = 0; | ||
|
@@ -325,10 +333,9 @@ bool parse_value(struct value *val, char *str) { | |
val->d_type = DELTA; | ||
str++; | ||
} | ||
n = strtol(str, &buf, 10); | ||
n = strtod(str, &buf); | ||
if (errno || buf == str) | ||
return false; | ||
val->val = labs(n) % LONG_MAX; | ||
while ((c = *(buf++))) switch(c) { | ||
case '+': | ||
val->sign = PLUS; | ||
|
@@ -342,6 +349,11 @@ bool parse_value(struct value *val, char *str) { | |
val->v_type = RELATIVE; | ||
break; | ||
} | ||
if (val->v_type == RELATIVE) { | ||
val->percentage = n; | ||
} else { | ||
val->val = labs((long) n) % LONG_MAX; | ||
} | ||
return true; | ||
} | ||
|
||
|
@@ -374,33 +386,39 @@ unsigned long percent_to_val(float percent, struct device *d) { | |
} | ||
|
||
void print_device(struct device *dev) { | ||
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"; | ||
char *format = p.mach ? "%s,%s,%d,%.4g%%,%d\n" : | ||
"Device '%s' of class '%s':\n\tCurrent brightness: %d (%.4g%%)\n\tMax brightness: %d\n\n"; | ||
Comment on lines
+389
to
+390
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at #51 (comment) wouldn't the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nah, I checked, we don't setlocale, so it always uses the dot |
||
fprintf(stdout, format, | ||
dev->id, dev->class, | ||
dev->curr_brightness, | ||
(int) val_to_percent(dev->curr_brightness, dev, true), | ||
val_to_percent(dev->curr_brightness, dev, !p.frac), | ||
dev->max_brightness); | ||
} | ||
|
||
unsigned int calc_value(struct device *d, struct value *val) { | ||
long new = d->curr_brightness; | ||
if (val->d_type == DIRECT) { | ||
new = val->v_type == ABSOLUTE ? val->val : percent_to_val(val->val, d); | ||
new = val->v_type == ABSOLUTE ? val->val : percent_to_val(val->percentage, d); | ||
goto apply; | ||
} | ||
long mod = val->val; | ||
if (val->sign == MINUS) | ||
mod *= -1; | ||
int sign_mod = val->sign == MINUS ? -1 : 1; | ||
long mod; | ||
if (val->v_type == RELATIVE) { | ||
mod = percent_to_val(val_to_percent(d->curr_brightness, d, false) + mod, d) - d->curr_brightness; | ||
if (val->val != 0 && mod == 0) | ||
float cur_pct = val_to_percent(d->curr_brightness, d, false); | ||
float cur_rnd = nearbyintf(cur_pct); | ||
if (mod == nearbyintf(mod) && cur_pct != cur_rnd && percent_to_val(cur_pct, d) == percent_to_val(cur_rnd, d)) | ||
cur_pct = cur_rnd; | ||
|
||
mod = percent_to_val(val_to_percent(d->curr_brightness, d, false) + val->percentage * sign_mod, d) - d->curr_brightness; | ||
if (val->percentage != 0 && mod == 0) | ||
mod = val->sign == PLUS ? 1 : -1; | ||
} else { | ||
mod = val->val * sign_mod; | ||
} | ||
new += mod; | ||
apply: | ||
if (p.min.v_type == RELATIVE) { | ||
p.min.val = percent_to_val(p.min.val, d); | ||
p.min.val = percent_to_val(p.min.percentage, d); | ||
p.min.v_type = ABSOLUTE; | ||
} | ||
if (new < (long)p.min.val) | ||
|
@@ -730,6 +748,7 @@ Options:\n\ | |
-d, --device=DEVICE \tspecify device name (can be a wildcard).\n\ | ||
-c, --class=CLASS \tspecify device class.\n\ | ||
-V, --version \tprint version and exit.\n\ | ||
--frac \tenable fractional percentage output.\n\ | ||
\n\ | ||
Operations:\n\ | ||
i, info \tget device info.\n\ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I would either convert all the float handling to
double
or usestrtof
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I conciously made it double, in case some display maker decides that their brightness scale goes to higher than 16.777.216 (it's 15360 on my laptop, which freaked me out when I noticed it first)