Bug 98404

Summary: UpKbdBacklight: Emit BrightnessChanged on brightness changes
Product: upower Reporter: Hans de Goede <jwrdegoede>
Component: generalAssignee: Richard Hughes <richard>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: medium CC: bugzilla
Version: unspecified   
Hardware: Other   
OS: All   
Whiteboard:
i915 platform: i915 features:
Attachments: [PATCH 1/2] UpKbdBacklight: Emit BrightnessChanged when brightness is changed on GetBrightness
[PATCH 2/2] UpKbdBacklight: Watch for POLL_PRI on the brightness fd
UpKbdBacklight: Emit BrightnessChanged when brightness is changed on GetBrightness
UpKbdBacklight: Try the new current_brightness attr and watch for POLL_PRI
[PATCH 1/3] UpKbdBacklight: Add new BrightnessChangedWithSource signal
[PATCH 2/3] UpKbdBacklight: up_kbd_backlight_brightness_read: pass in fd
[PATCH 3/3] UpKbdBacklight: Try the new brightness_hw_changed attr and watch for POLL_PRI
UpKbdBacklight: Add new BrightnessChangedWithSource signal
UpKbdBacklight: Allow reading from alternate fd in _brightness_read()
UpKbdBacklight: Send notifications about hardware brightness changes
UpKbdBacklight: Add new BrightnessChangedWithSource signal
UpKbdBacklight: Allow reading from alternate fd in _brightness_read()
UpKbdBacklight: Send notifications about hardware brightness changes

Description Hans de Goede 2016-10-24 07:27:39 UTC
Created attachment 127509 [details] [review]
[PATCH 1/2] UpKbdBacklight: Emit BrightnessChanged when brightness is changed on GetBrightness

Hi,

Here are 2 patches to make upower emit BrightnessChanged on brightness changes, note the second patch relies on the kernel implementing poll support for the brightness sysfs attribute. I've submitted a patch for this upstream here: http://www.spinics.net/lists/platform-driver-x86/msg09612.html

This is v2, v1 has received a somewhat favorable review (some changes were needed but no blockers)
so I expect this to go upstream, but you should probably wait with merging the 2nd patch till this is at least acked upstream. Note that without the kernel changes / on older kernels the poll() will simply never wakeup so this is safe to do on older kernels.

FYI: I've also implemented actually waking up the poll when a hardwired hotkey changes the keyboard backlight brightness for dell laptops:
http://www.spinics.net/lists/platform-driver-x86/msg09615.html
http://www.spinics.net/lists/platform-driver-x86/msg09613.html

And I plan to also add support for this for Thinkpads.

Regards,

Hans
Comment 1 Hans de Goede 2016-10-24 07:28:04 UTC
Created attachment 127510 [details] [review]
[PATCH 2/2] UpKbdBacklight: Watch for POLL_PRI on the brightness fd
Comment 2 Bastien Nocera 2016-10-24 10:48:17 UTC
Comment on attachment 127509 [details] [review]
[PATCH 1/2] UpKbdBacklight: Emit BrightnessChanged when brightness is changed on GetBrightness

Review of attachment 127509 [details] [review]:
-----------------------------------------------------------------

::: src/up-kbd-backlight.c
@@ +142,5 @@
>  		up_exported_kbd_backlight_complete_get_brightness (skeleton, invocation,
>  								   brightness);
> +		if (brightness != kbd_backlight->priv->brightness) {
> +			kbd_backlight->priv->brightness = brightness;
> +			up_exported_kbd_backlight_emit_brightness_changed (

One line, we have wide screens.

@@ +268,4 @@
>  	kbd_backlight->priv->fd = open (path_now, O_RDWR);
>  
>  	/* read brightness and check if it has an acceptable value */
> +	kbd_backlight->priv->brightness = up_kbd_backlight_brightness_read (kbd_backlight);

Is it such a good idea to save errors? I'd save it in a temp variable, and only set this value if there were no errors.
Comment 3 Bastien Nocera 2016-10-24 10:49:22 UTC
Comment on attachment 127510 [details] [review]
[PATCH 2/2] UpKbdBacklight: Watch for POLL_PRI on the brightness fd

Review of attachment 127510 [details] [review]:
-----------------------------------------------------------------

::: src/up-kbd-backlight.c
@@ +211,5 @@
>  /**
> + * up_kbd_backlight_event_io:
> + **/
> +static gboolean
> +up_kbd_backlight_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)

Please check the condition, and make sure to free it when exiting.
Comment 4 Bastien Nocera 2016-10-24 10:50:20 UTC
Comment on attachment 127509 [details] [review]
[PATCH 1/2] UpKbdBacklight: Emit BrightnessChanged when brightness is changed on GetBrightness

Review of attachment 127509 [details] [review]:
-----------------------------------------------------------------

> Since commit 793642bfb7("pKbdBacklight: don't cache the brightness level,

Typo in commit subject.
Comment 5 Bastien Nocera 2016-10-24 11:00:33 UTC
Please also remove the SOB from the commit messages.
Comment 6 Hans de Goede 2016-11-17 22:29:18 UTC
Hi,

Thanks for the review, I've prepared a new version of both patches addressing the review comments.

As for the second patch, I've also changed it to match some changes on the kernel side. Allowing poll on the regular brightness attribute was causing high CPU-load in some embedded systems. So there now is a new kernel patch-set which instead adds a current_brightness sysfs file, which is pollable and will only be available on some LEDs (with some triggers) to avoid the CPU load issues.

Please review, but wait a bit with merging the second patch until it is clear the kernel patches are really final (I'll add another comment at that time).

Regards,

Hans
Comment 7 Hans de Goede 2016-11-17 22:29:52 UTC
Created attachment 128051 [details] [review]
UpKbdBacklight: Emit BrightnessChanged when brightness is changed on GetBrightness
Comment 8 Hans de Goede 2016-11-17 22:30:42 UTC
Created attachment 128052 [details] [review]
UpKbdBacklight: Try the new current_brightness attr and watch for POLL_PRI
Comment 9 Bastien Nocera 2016-11-18 11:54:45 UTC
Comment on attachment 128051 [details] [review]
UpKbdBacklight: Emit BrightnessChanged when brightness is changed on GetBrightness

Review of attachment 128051 [details] [review]:
-----------------------------------------------------------------

> re-gets the actual brightness

gets the current brightness again

And s/upower/UPower/

Looks fine otherwise
Comment 10 Bastien Nocera 2016-11-18 12:00:45 UTC
Comment on attachment 128052 [details] [review]
UpKbdBacklight: Try the new current_brightness attr and watch for POLL_PRI

Review of attachment 128052 [details] [review]:
-----------------------------------------------------------------

> New kernels have a current_brightness attribute which can be polled to

State the version, or the commit and link to it, "new" will mean something else in 6 months time.

Looks fine otherwise.

::: src/up-kbd-backlight.c
@@ +212,5 @@
> + **/
> +static gboolean
> +up_kbd_backlight_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
> +{
> +	UpKbdBacklight *kbd_backlight = (UpKbdBacklight*) data;

No need for the cast from a gpointer.

@@ +213,5 @@
> +static gboolean
> +up_kbd_backlight_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
> +{
> +	UpKbdBacklight *kbd_backlight = (UpKbdBacklight*) data;
> +	gint brightness;

Use "int" please (the rest of the file doesn't, but we'll move away little by little).

@@ +286,4 @@
>  		goto out;
>  	}
>  
> +	/*

You can put this 4-line comment on two-lines, we're not following kernel-style comments (even that has one too much I think).
Comment 11 Bastien Nocera 2016-11-18 12:01:33 UTC
Do those patches avoid the double-set when pressing a button, or feedback loops with the brightness slider in the Power panel?
Comment 12 Hans de Goede 2016-11-19 10:31:44 UTC
Hi,

(In reply to Bastien Nocera from comment #10)
> Comment on attachment 128052 [details] [review] [review]
> UpKbdBacklight: Try the new current_brightness attr and watch for POLL_PRI
> 
> Review of attachment 128052 [details] [review] [review]:
> -----------------------------------------------------------------
> 
> > New kernels have a current_brightness attribute which can be polled to
> 
> State the version, or the commit and link to it, "new" will mean something
> else in 6 months time.

Ack, will do so once the kernel bits are actually merged upstream (or are in -next at least).

> Looks fine otherwise.
> 
> ::: src/up-kbd-backlight.c
> @@ +212,5 @@
> > + **/
> > +static gboolean
> > +up_kbd_backlight_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
> > +{
> > +	UpKbdBacklight *kbd_backlight = (UpKbdBacklight*) data;
> 
> No need for the cast from a gpointer.
> 
> @@ +213,5 @@
> > +static gboolean
> > +up_kbd_backlight_event_io (GIOChannel *channel, GIOCondition condition, gpointer data)
> > +{
> > +	UpKbdBacklight *kbd_backlight = (UpKbdBacklight*) data;
> > +	gint brightness;
> 
> Use "int" please (the rest of the file doesn't, but we'll move away little
> by little).

Ok.

> 
> @@ +286,4 @@
> >  		goto out;
> >  	}
> >  
> > +	/*
> 
> You can put this 4-line comment on two-lines, we're not following
> kernel-style comments (even that has one too much I think).

Ok.

(In reply to Bastien Nocera from comment #11)
> Do those patches avoid the double-set when pressing a button

There is no double set when pressing a button, the buttons on the laptops I'm looking at do not generate a keypress atm. I'm adding poll() support to the LED sysfs interface so that userspace can still detect changes done through the hotkey, even though the hardware/firmware already changes the brightness
autonomously. Generating a keypress in that case would be wrong, hence the poll() support instead.

So in a way yes, these avoid the double-set by not emitting a keypress event and instead notifying userspace of the brightness change directly through the LED sysfs API.

> or feedback loops with the brightness slider in the Power panel?

Feedback loops are avoided by up_kbd_backlight_event_io() checking that the brightness it reads on a poll() wake-up is different then the previous brightness.

Regards,

Hans
Comment 13 Hans de Goede 2017-02-09 16:09:39 UTC
Created attachment 129440 [details] [review]
[PATCH 1/3] UpKbdBacklight: Add new BrightnessChangedWithSource signal

Hi,

Here is a new set of the patches using the new brightness_hw_changed sysfs attribute which upstream eventually settled on (it is in linux-next heading for 4.11).

This also introduces a new BrightnessChangedWithSource signal so that listeners can distinguish between changes caused by SetBrightness getting called and changed detected through the new brightness_hw_changed sysfs attribute.

Regards,

Hans
Comment 14 Hans de Goede 2017-02-09 16:09:59 UTC
Created attachment 129441 [details] [review]
[PATCH 2/3] UpKbdBacklight: up_kbd_backlight_brightness_read: pass in fd
Comment 15 Hans de Goede 2017-02-09 16:10:34 UTC
Created attachment 129442 [details] [review]
[PATCH 3/3] UpKbdBacklight: Try the new brightness_hw_changed attr and watch for POLL_PRI
Comment 16 Bastien Nocera 2017-02-15 15:44:29 UTC
Comment on attachment 129440 [details] [review]
[PATCH 1/3] UpKbdBacklight: Add new BrightnessChangedWithSource signal

Review of attachment 129440 [details] [review]:
-----------------------------------------------------------------

::: dbus/org.freedesktop.UPower.KbdBacklight.xml
@@ +106,5 @@
> +      </arg>
> +      <arg name="source" direction="out" type="s">
> +        <doc:doc>
> +          <doc:summary>
> +            Source of the keyboard backlight brightness change.

Needs to list the possible values, if they are known, eg. :

Source of the keyboard backlight brightness change, such as "XXX" for something or other events, and "XXX" for something else entirely.

@@ +114,5 @@
> +      <doc:doc>
> +        <doc:description>
> +          <doc:para>
> +	    The keyboard backlight brightness level has changed including
> +	    information of the source of the change.

including information about the source of the change.
Comment 17 Bastien Nocera 2017-02-15 15:47:12 UTC
Comment on attachment 129441 [details] [review]
[PATCH 2/3] UpKbdBacklight: up_kbd_backlight_brightness_read: pass in fd

Review of attachment 129441 [details] [review]:
-----------------------------------------------------------------

Looks good, but the commit message isn't quite up to scratch. I can handle that when I'm committing though.
Comment 18 Bastien Nocera 2017-02-15 15:53:00 UTC
Comment on attachment 129442 [details] [review]
[PATCH 3/3] UpKbdBacklight: Try the new brightness_hw_changed attr and watch for POLL_PRI

Review of attachment 129442 [details] [review]:
-----------------------------------------------------------------

> Try the new brightness_hw_changed attr and watch for POLL_PRI

Send notifications about hardware brightness changes

::: src/up-kbd-backlight.c
@@ +47,2 @@
>  	gint			 max_brightness;
> +	GIOChannel		*channel;

A better name would be nice, such as "channel_hw_changed", and put next to fd_hw_changed in the struct.
Comment 19 Bastien Nocera 2017-02-15 16:20:26 UTC
Created attachment 129640 [details] [review]
UpKbdBacklight: Add new BrightnessChangedWithSource signal

There are multiple possible causes for the keyboard brightness to change,
e.g. SetBrightness may get called, or the brightness may get changed
through a hotkey which is handled in firmware.
Comment 20 Bastien Nocera 2017-02-15 16:20:30 UTC
Created attachment 129641 [details] [review]
UpKbdBacklight: Allow reading from alternate fd in _brightness_read()

Pass the fd to use to up_kbd_backlight_brightness_read() so that it can
be used with multiple fds. This will be used to support the
"brightness_hw_changed" sysfs attribute.
Comment 21 Bastien Nocera 2017-02-15 16:20:33 UTC
Created attachment 129642 [details] [review]
UpKbdBacklight: Send notifications about hardware brightness changes

Backlights, starting with kernel 4.11 [1] can have a
"brightness_hw_changed" sysfs attribute,  which can be polled to detect
hardware initiated brightness changes, such as ones done through
firmware-handled keyboard backlight hotkeys, or firmware controlled
changes (eg. turn off backlight on low battery).

[1] https://patchwork.kernel.org/patch/9544111/
Comment 22 Bastien Nocera 2017-02-15 16:34:48 UTC
Created attachment 129644 [details] [review]
UpKbdBacklight: Add new BrightnessChangedWithSource signal

There are multiple possible causes for the keyboard brightness to change,
e.g. SetBrightness may get called, or the brightness may get changed
through a hotkey which is handled in firmware.
Comment 23 Bastien Nocera 2017-02-15 16:34:51 UTC
Created attachment 129645 [details] [review]
UpKbdBacklight: Allow reading from alternate fd in _brightness_read()

Pass the fd to use to up_kbd_backlight_brightness_read() so that it can
be used with multiple fds. This will be used to support the
"brightness_hw_changed" sysfs attribute.
Comment 24 Bastien Nocera 2017-02-15 16:34:55 UTC
Created attachment 129646 [details] [review]
UpKbdBacklight: Send notifications about hardware brightness changes

Backlights, starting with kernel 4.11 [1] can have a
"brightness_hw_changed" sysfs attribute,  which can be polled to detect
hardware initiated brightness changes, such as ones done through
firmware-handled keyboard backlight hotkeys, or firmware controlled
changes (eg. turn off backlight on low battery).

[1] https://patchwork.kernel.org/patch/9544111/
Comment 25 Bastien Nocera 2017-02-15 16:36:06 UTC
Attachment 129644 [details] pushed as 112814f - UpKbdBacklight: Add new BrightnessChangedWithSource signal
Attachment 129645 [details] pushed as d28ee53 - UpKbdBacklight: Allow reading from alternate fd in _brightness_read()
Attachment 129646 [details] pushed as 0e256ec - UpKbdBacklight: Send notifications about hardware brightness changes

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.