From bc4a9bbd3fbbaf7bcde14c05eb067726197e65bb Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Sun, 16 Oct 2016 12:53:19 +0200 Subject: [PATCH 2/2] UpKbdBacklight: Try the new current_brightness attr and watch for POLL_PRI New kernels have a current_brightness attribute which can be polled to detect hardware initiated brightness changes, such as done by e.g. hardwired keyboard backlight brightness control hotkeys. --- src/up-kbd-backlight.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/up-kbd-backlight.c b/src/up-kbd-backlight.c index 4813aca..83aa11f 100644 --- a/src/up-kbd-backlight.c +++ b/src/up-kbd-backlight.c @@ -45,6 +45,7 @@ struct UpKbdBacklightPrivate gint fd; gint brightness; gint max_brightness; + GIOChannel *channel; }; G_DEFINE_TYPE (UpKbdBacklight, up_kbd_backlight, UP_TYPE_EXPORTED_KBD_BACKLIGHT_SKELETON) @@ -207,6 +208,28 @@ up_kbd_backlight_class_init (UpKbdBacklightClass *klass) } /** + * up_kbd_backlight_event_io: + **/ +static gboolean +up_kbd_backlight_event_io (GIOChannel *channel, GIOCondition condition, gpointer data) +{ + UpKbdBacklight *kbd_backlight = (UpKbdBacklight*) data; + gint brightness; + + if (!(condition & G_IO_PRI)) + return FALSE; + + brightness = up_kbd_backlight_brightness_read (kbd_backlight); + if (brightness >= 0 && brightness != kbd_backlight->priv->brightness) { + kbd_backlight->priv->brightness = brightness; + up_exported_kbd_backlight_emit_brightness_changed ( + UP_EXPORTED_KBD_BACKLIGHT (kbd_backlight), brightness); + } + + return TRUE; +} + +/** * up_kbd_backlight_find: **/ static gboolean @@ -263,9 +286,21 @@ up_kbd_backlight_find (UpKbdBacklight *kbd_backlight) goto out; } - /* open the brightness file for read and write operations */ - path_now = g_build_filename (dir_path, "brightness", NULL); + /* + * open the brightness file for read and write operations, first try + * the new current_brightness sysfs attr, which supports poll. + */ + path_now = g_build_filename (dir_path, "current_brightness", NULL); kbd_backlight->priv->fd = open (path_now, O_RDWR); + if (kbd_backlight->priv->fd >= 0) { + kbd_backlight->priv->channel = g_io_channel_unix_new (kbd_backlight->priv->fd); + g_io_add_watch (kbd_backlight->priv->channel, G_IO_PRI, up_kbd_backlight_event_io, kbd_backlight); + } else { + /* Try again with "brightness" */ + g_free (path_now); + path_now = g_build_filename (dir_path, "brightness", NULL); + kbd_backlight->priv->fd = open (path_now, O_RDWR); + } /* read brightness and check if it has an acceptable value */ brightness = up_kbd_backlight_brightness_read (kbd_backlight); @@ -316,6 +351,11 @@ up_kbd_backlight_finalize (GObject *object) kbd_backlight = UP_KBD_BACKLIGHT (object); kbd_backlight->priv = UP_KBD_BACKLIGHT_GET_PRIVATE (kbd_backlight); + if (kbd_backlight->priv->channel) { + g_io_channel_shutdown (kbd_backlight->priv->channel, FALSE, NULL); + g_io_channel_unref (kbd_backlight->priv->channel); + } + /* close file */ if (kbd_backlight->priv->fd >= 0) close (kbd_backlight->priv->fd); -- 2.9.3