Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Operations:

Valid values:
specific value Example: 500
percentage value Example: 50%
percentage value Example: 50.5%
specific delta Example: 50- or +10
percentage delta Example: 50%- or +10%
```
4 changes: 2 additions & 2 deletions brightnessctl.1
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ as a value or a delta from the current value. For example:
Sets brightness to 500.
.P
.RE
\fBbrightnessctl set 50%\fR
\fBbrightnessctl set 50.75%\fR
.RS "4"
Sets brightness to 50% of the maximum.
Sets brightness to 50.75% of the maximum.
.P
.RE
\fBbrightnessctl set 50-\fR
Expand Down
38 changes: 22 additions & 16 deletions brightnessctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ enum delta_type { DIRECT, DELTA };
enum sign { PLUS, MINUS };

struct value {
unsigned long val;
float val;
enum value_type v_type;
enum delta_type d_type;
enum sign sign;
Expand Down Expand Up @@ -276,7 +276,7 @@ int apply_operation(struct device *dev, enum operation operation, struct value *
}

bool parse_value(struct value *val, char *str) {
long n;
float n;
char c;
char *buf;
errno = 0;
Expand All @@ -290,10 +290,10 @@ bool parse_value(struct value *val, char *str) {
val->d_type = DELTA;
str++;
}
n = strtol(str, &buf, 10);
n = strtof(str, &buf);
if (errno || buf == str)
return false;
val->val = labs(n) % LONG_MAX;
val->val = fabsf(n);
while ((c = *(buf++))) switch(c) {
case '+':
val->sign = PLUS;
Expand Down Expand Up @@ -326,25 +326,31 @@ void list_devices(struct device **devs) {
print_device(dev);
}

float val_to_percent(float val, struct device *d, bool rnd) {
float val_to_percent(float val, struct device *d) {
if (val < 0)
return 0;
float ret = powf(val / d->max_brightness, 1.0f / p.exponent) * 100;
return rnd ? roundf(ret) : ret;
return powf(val / d->max_brightness, 1.0f / p.exponent) * 100;
}

unsigned long percent_to_val(float percent, struct device *d) {
return roundf(powf(percent / 100, p.exponent) * d->max_brightness);
}

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";
fprintf(stdout, format,
dev->id, dev->class,
dev->curr_brightness,
(int) val_to_percent(dev->curr_brightness, dev, true),
dev->max_brightness);
float brightness = val_to_percent(dev->curr_brightness, dev);
if (p.mach)
fprintf(stdout, "%s,%s,%d,%d%%,%d,%f%%\n",
dev->id, dev->class,
dev->curr_brightness,
(int) roundf(brightness),
dev->max_brightness,
(double) brightness);
else
fprintf(stdout, "Device '%s' of class '%s':\n\tCurrent brightness: %d (%.02f%%)\n\tMax brightness: %d\n\n",
dev->id, dev->class,
dev->curr_brightness,
(double) brightness,
dev->max_brightness);
}

unsigned int calc_value(struct device *d, struct value *val) {
Expand All @@ -357,7 +363,7 @@ unsigned int calc_value(struct device *d, struct value *val) {
if (val->sign == MINUS)
mod *= -1;
if (val->v_type == RELATIVE) {
mod = percent_to_val(val_to_percent(d->curr_brightness, d, false) + mod, d) - d->curr_brightness;
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;
}
Expand Down Expand Up @@ -670,7 +676,7 @@ Operations:\n\
\n\
Valid values:\n\
specific value \tExample: 500\n\
percentage value \tExample: 50%%\n\
percentage value \tExample: 50.75%%\n\
specific delta \tExample: 50- or +10\n\
percentage delta \tExample: 50%%- or +10%%\n\
\n");
Expand Down