[App/xbacklight] Incorrect behaviour when using -dec and then -inc with the same argument
Submitted by Nicola Lamacchia
Assigned to Xorg Project Team
Description
Created attachment 86859 Fix the bug
On my Samsung NC10 I experience the following:
$ xrandr --verbose
...
BACKLIGHT: 8 (0x00000008) range: (0,8)
Backlight: 8 (0x00000008) range: (0,8)
...
$ xbacklight -dec 10 # this works
$ xrandr --verbose
...
BACKLIGHT: 7 (0x00000007) range: (0,8)
Backlight: 7 (0x00000007) range: (0,8)
...
$ xbacklight -inc 10 # this doesn't work, nothing happens
$ xrandr --verbose
...
BACKLIGHT: 7 (0x00000007) range: (0,8)
Backlight: 7 (0x00000007) range: (0,8)
...
I've had a look to the code (xbacklight.c commit a4c00b26):
[...line 263]
double cur, new, step;
double min, max;
double set;
[...line 287]
set = value * (max - min) / 100;
switch (op) {
[...line 292]
case Inc:
new = cur + set;
break;
case Dec:
new = cur - set;
break;
[...line 308]
cur = new;
[...line 311]
backlight_set (conn, output, (long) cur);
[...]
Doing some math as C does:
# suppose
value = 15
max = 8
min = 0
cur = 7
set = value * (max - min) / 100 = 15 * (8 - 0) / 100 = 1.2
# decrease by 15%
op = Dec => new = 7 - 1.2 = 5.8
as per line 311 -> (long) cur => 5.8 -> 5 (toward zero)
now: cur = 5 (initial cur - 2)
# then increase by 15%
op = Inc => new = 5 + 1.2 = 6.2
Toward zero => 6.8 -> 6
now: cur = 6 (initial cur - 1)
'set' should be rounded at line 287 to avoid this inconsistent behaviour. I've attached a possible solution.
Attachment 86859, "Fix the bug":
0001-Add-missing-type-cast.patch
Version: git