Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions brightnessctl.1
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ Specify device class.
Print version and exit.
.RE

.sp
\fB\\-\-frac\fP
.RS 4
Output percentages as fractional values.
.RE


.SH "OPERATIONS"

Expand Down
49 changes: 34 additions & 15 deletions brightnessctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -101,6 +104,7 @@ struct params {
bool percentage;
bool save;
bool restore;
bool frac;
float exponent;
};

Expand All @@ -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,}
};

Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Copy link
Contributor

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 use strtof

Copy link
Owner Author

@Hummer12007 Hummer12007 Dec 15, 2024

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)

if (errno || buf == str)
return false;
val->val = labs(n) % LONG_MAX;
while ((c = *(buf++))) switch(c) {
case '+':
val->sign = PLUS;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at #51 (comment) wouldn't the --frac incompatible with the -m option ? should this be documented or handled in some way ?

Copy link
Owner Author

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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\
Expand Down