From 470b34cd89b32d67affe8ce99aea7e5f254a21f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Thu, 19 May 2016 04:33:06 +0200 Subject: [PATCH] UpKbdBacklight: don't cache the brightness level, always read it from sysnode When GetBrightness is called it's better to fetch the current value in the sysnode since many devices doesn't support the emission of brightness changes to userland, and this could make settings daemons to handle this value incorrectly. Added static up_kbd_backlight_brightness_read that is now called also in initialization, it returns -1 on errors, and use it everywhere we need to read or check the current brightness. https://bugs.freedesktop.org/show_bug.cgi?id=95457 --- src/up-kbd-backlight.c | 74 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/src/up-kbd-backlight.c b/src/up-kbd-backlight.c index cfa1dd3..1417e60 100644 --- a/src/up-kbd-backlight.c +++ b/src/up-kbd-backlight.c @@ -43,13 +43,47 @@ static void up_kbd_backlight_finalize (GObject *object); struct UpKbdBacklightPrivate { gint fd; - gint brightness; gint max_brightness; }; G_DEFINE_TYPE (UpKbdBacklight, up_kbd_backlight, UP_TYPE_EXPORTED_KBD_BACKLIGHT_SKELETON) /** + * up_kbd_backlight_brightness_read: + **/ +static gint +up_kbd_backlight_brightness_read (UpKbdBacklight *kbd_backlight) +{ + gchar buf[16]; + gchar *end = NULL; + ssize_t len; + gint64 brightness = -1; + + if (kbd_backlight->priv->fd < 0) { + g_warning ("cannot read kbd_backlight as file not open"); + goto out; + } + + lseek (kbd_backlight->priv->fd, 0, SEEK_SET); + len = read (kbd_backlight->priv->fd, buf, G_N_ELEMENTS (buf)); + + if (len > 0) { + buf[MIN(len, (int) G_N_ELEMENTS (buf) - 1)] = '\0'; + brightness = g_ascii_strtoll (buf, &end, 10); + + if (brightness < 0 || + brightness > kbd_backlight->priv->max_brightness || + (brightness == 0 && end == buf)) { + brightness = -1; + g_warning ("failed to convert brightness: %s", buf); + } + } + +out: + return brightness; +} + +/** * up_kbd_backlight_brightness_write: **/ static gboolean @@ -58,6 +92,7 @@ up_kbd_backlight_brightness_write (UpKbdBacklight *kbd_backlight, gint value) gchar *text = NULL; gint retval; gint length; + gint brightness; gboolean ret = TRUE; /* write new values to backlight */ @@ -84,9 +119,8 @@ up_kbd_backlight_brightness_write (UpKbdBacklight *kbd_backlight, gint value) } /* emit signal */ - kbd_backlight->priv->brightness = value; up_exported_kbd_backlight_emit_brightness_changed (UP_EXPORTED_KBD_BACKLIGHT (kbd_backlight), - kbd_backlight->priv->brightness); + value); out: g_free (text); @@ -103,8 +137,19 @@ up_kbd_backlight_get_brightness (UpExportedKbdBacklight *skeleton, GDBusMethodInvocation *invocation, UpKbdBacklight *kbd_backlight) { - up_exported_kbd_backlight_complete_get_brightness (skeleton, invocation, - kbd_backlight->priv->brightness); + gint brightness; + + brightness = up_kbd_backlight_brightness_read (kbd_backlight); + + if (brightness >= 0) { + up_exported_kbd_backlight_complete_get_brightness (skeleton, invocation, + brightness); + } else { + g_dbus_method_invocation_return_error (invocation, + UP_DAEMON_ERROR, UP_DAEMON_ERROR_GENERAL, + "error reading brightness"); + } + return TRUE; } @@ -216,23 +261,12 @@ up_kbd_backlight_find (UpKbdBacklight *kbd_backlight) goto out; } - /* read brightness */ + /* open the brightness file for read and write operations */ path_now = g_build_filename (dir_path, "brightness", NULL); - ret = g_file_get_contents (path_now, &buf_now, NULL, &error); - if (!ret) { - g_warning ("failed to get brightness: %s", error->message); - g_error_free (error); - goto out; - } - kbd_backlight->priv->brightness = g_ascii_strtoull (buf_now, &end, 10); - if (kbd_backlight->priv->brightness == 0 && end == buf_now) { - g_warning ("failed to convert brightness: %s", buf_now); - goto out; - } - - /* open the file for writing */ kbd_backlight->priv->fd = open (path_now, O_RDWR); - if (kbd_backlight->priv->fd < 0) + + /* read brightness and check if it has an acceptable value */ + if (up_kbd_backlight_brightness_read (kbd_backlight) < 0) goto out; /* success */ -- 1.9.1