panel: added sysfs support
PATCH v5 1/1 panel: added sysfs support
battery applet supports reading battery and supply information from sysfs entries, /sys/class/power_supply/.
[YOCTO #12904]
https://bugzilla.yoctoproject.org/show_bug.cgi?id=12904
---
V4: https://lists.yoctoproject.org/g/yocto-patches/message/789
--- applets/battery/Makefile.am | 4 + applets/battery/battery-acpi.c | 2 + applets/battery/battery-apm.c | 2 + applets/battery/battery-sysfs.c | 148 ++++++++++++++++++++++++++++++++ applets/battery/battery.c | 2 + applets/battery/battery.h | 3 +- configure.ac | 4 + 7 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 applets/battery/battery-sysfs.c
applets/battery/Makefile.am +4 -0
@@ -5,6 +5,10 @@ applet_LTLIBRARIES = libbattery.la libbattery_la_SOURCES = battery.c battery.h libbattery_la_CPPFLAGS = $(AM_CPPFLAGS) -DDATADIR=\"$(pkgdatadir)/battery/\" +if HAVE_SYSFS +libbattery_la_SOURCES += battery-sysfs.c +endif + if HAVE_LIBAPM libbattery_la_LIBADD = -lapm libbattery_la_SOURCES += battery-apm.c
applets/battery/battery-acpi.c +2 -0
@@ -14,6 +14,8 @@ global_t global;adapter_t *ac;int batt_state, ac_state; +void pm_cleanup (void ) { } +int pm_support (void ) {if (check_acpi_support () ==NOT_SUPPORTED ){
applets/battery/battery-apm.c +2 -0
@@ -9,6 +9,8 @@#include "battery.h" #include <apm.h> +void pm_cleanup (void ) { } +int pm_support (void ) {if (1 ==apm_exists ()) {
applets/battery/battery-sysfs.c new file +148 -0
148 lines not on this page · raw message
applets/battery/battery.c +2 -0
@@ -24,6 +24,8 @@ battery_applet_free (BatteryApplet *applet)g_source_remove (applet->timeout_id);g_slice_free (BatteryApplet, applet); + +pm_cleanup (); } static gboolean
applets/battery/battery.h +1 -2
@@ -9,8 +9,7 @@#ifndef MB_APPLET_BATTERY_H #define MB_APPLET_BATTERY_H -#include <string.h> - +void pm_cleanup (void );int pm_support (void );const char *pm_battery_icon (void );
configure.ac +4 -0
@@ -61,6 +61,9 @@ AC_ARG_WITH( ) case "$with_battery" in + "sysfs") enable_linux_sysfs=yes + enable_battery=yes + ;; "acpi") AC_CHECK_HEADERS(libacpi.h, enable_linux_acpi=yes, AC_MSG_FAILURE([You need to install libacpi])) enable_battery=yes ;;
@@ -75,6 +78,7 @@ esac AM_CONDITIONAL(HAVE_BATTERY, test x$enable_battery = xyes) AM_CONDITIONAL(HAVE_LIBAPM, test x$enable_linux_apm = xyes) AM_CONDITIONAL(HAVE_LIBACPI, test x$enable_linux_acpi = xyes) +AM_CONDITIONAL(HAVE_SYSFS, test x$enable_linux_sysfs = xyes) # glib-genmarshal GLIB_GENMARSHAL=`$PKG_CONFIG --variable=glib_genmarshal glib-2.0`
Signature · 1 line
2.43.0
Hi Ross and all,
As it has been almost a month since this new patch version submission, I would like to send a gentle reminder to bring this patch to the top of your review list. It's of course not urgent, but an ACK to confirm if it looks good or not would be highly appreciated.
Thanks,
Dixit
Ross is on holiday now. This work is massively appreciated, I'll make sure to poke him if needed :-)
Alex
Quoted 18 lines · Dixit Parmar via lists.yoctoproject.org
Hi Ross and all,
As it has been almost a month since this new patch version submission, I would like to send a gentle reminder to bring this patch to the top of your review list. It's of course not urgent, but an ACK to confirm if it looks good or not would be highly appreciated.
Thanks,
Dixit_._,_._,_
________________________________
Links:You receive all messages sent to this group.
View/Reply Online (#1924) | Reply to Group | Reply to Sender | Mute This Topic | New Topic Your Subscription | Contact Group Owner | Unsubscribe [[email protected]]
_._,_._,_
Quoted 5 lines · Alexander Kanavin
Ross is on holiday now. This work is massively appreciated, I'll make sure to poke him if needed :-)
Alex
Hi Alex,
No please, let him enjoy. Anyway I got some suggestions to go for v6. Thanks
Quoted 15 lines
Quoted 9 lines · Dixit Parmar via lists.yoctoproject.org
Hi Ross and all,
As it has been almost a month since this new patch version submission, I would like to send a gentle reminder to bring this patch to the top of your review list. It's of course not urgent, but an ACK to confirm if it looks good or not would be highly appreciated.
Thanks,
Dixit
Though I'm not Ross, added some notes inline. I gotta admit that haven't read the reviews for v1-v3, only for v4: if there is something that was discussed beforehand, and I'm just repeating it, just ignore that part.
Quoted 74 lines · Dixit Parmar via lists.yoctoproject.org
battery applet supports reading battery and supply information from sysfs entries,
/sys/class/power_supply/.[YOCTO #12904]
https://bugzilla.yoctoproject.org/show_bug.cgi?id=12904Signed-off-by Dixit Parmar <[email protected]>---
V4: https://lists.yoctoproject.org/g/yocto-patches/message/789--- applets/battery/Makefile.am | 4 + applets/battery/battery-acpi.c | 2 + applets/battery/battery-apm.c | 2 + applets/battery/battery-sysfs.c | 148 ++++++++++++++++++++++++++++++++ applets/battery/battery.c | 2 + applets/battery/battery.h | 3 +- configure.ac | 4 + 7 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 applets/battery/battery-sysfs.capplets/battery/Makefile.am +4
-0@@ -5,6 +5,10 @@ applet_LTLIBRARIES = libbattery.la libbattery_la_SOURCES = battery.c battery.h libbattery_la_CPPFLAGS = $(AM_CPPFLAGS) -DDATADIR=\"$(pkgdatadir)/battery/\" +if HAVE_SYSFS +libbattery_la_SOURCES += battery-sysfs.c +endif + if HAVE_LIBAPM libbattery_la_LIBADD = -lapm libbattery_la_SOURCES += battery-apm.capplets/battery/battery-acpi.c +2
-0@@ -14,6 +14,8 @@ global_t global;adapter_t *ac;int batt_state, ac_state; +void pm_cleanup (void ) { } +int pm_support (void ) {if (check_acpi_support () ==NOT_SUPPORTED ){applets/battery/battery-apm.c +2
-0@@ -9,6 +9,8 @@#include "battery.h" #include <apm.h> +void pm_cleanup (void ) { } +int pm_support (void ) {if (1 ==apm_exists ()) {applets/battery/battery-sysfs.c new file +7
-0@@ -0,0 +1,148 @@ +/* +* (C) Dixit Parmar +* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* Licensed under the GPL v2 or greater. +*/
applets/battery/battery-sysfs.c line 17
Quoted 10 lines
applets/battery/battery-sysfs.c new file +10
-0+ +#include "battery.h" +#include <glib.h> + +#define PROCFS_PS_DIR "/sys/class/power_supply" + +static gchar *batt_dev; +static gchar *ac_dev; +static guchar batt_exist =0 ; +static guchar ac_exist =0 ;
I'm not very well versed in gtk, so if anyone tells me that it is idiomatic, I will believe it, but: why are these guchar? The code doesn't seem to do anything char-like with them, they are used as bools. Why not guint, or gboolean?
Quoted 40 lines
applets/battery/battery-sysfs.c new file +40
-0+ +void pm_cleanup (void ) +{ +g_free (batt_dev); +g_free (ac_dev); +} + +int pm_support (void ) +{ +GDir *dir =g_dir_open (PROCFS_PS_DIR ,0 ,NULL ); +if (dir !=NULL ) { +const gchar *subdir_name; +while ((subdir_name =g_dir_read_name (dir)) !=NULL ) { +gchar *subdir_path =g_build_filename (PROCFS_PS_DIR , subdir_name,NULL ); +// Check if it's a directory +if (g_file_test (subdir_path,G_FILE_TEST_IS_DIR )) { +gchar *type_file_path =g_build_filename (subdir_path,"type" ,NULL ); +// Check if the "type" file exists +if (g_file_test (type_file_path,G_FILE_TEST_EXISTS )) { +gchar *file_content =NULL ; +gsize length; + +if (g_file_get_contents (type_file_path, &file_content, &length,NULL )) { +if (!batt_exist &&strncmp (file_content,"Battery" ,strlen ("Battery" )) ==0 ) { + batt_dev =g_malloc0 (strlen (subdir_name) +1 ); +strncpy (batt_dev, subdir_name,strlen (subdir_name)); + batt_exist =1 ; + } +if (!ac_exist &&strncmp (file_content,"Mains" ,strlen ("Mains" )) ==0 ) { + ac_dev =g_malloc0 (strlen (subdir_name) +1 ); +strncpy (ac_dev, subdir_name,strlen (subdir_name)); + ac_exist =1 ; + } +g_free (file_content); + } + } +g_free (type_file_path); + } +g_free (subdir_path); +if (batt_exist && ac_exist)
applets/battery/battery-sysfs.c line 67
Quoted 10 lines
applets/battery/battery-sysfs.c new file +10
-0+break ; + } +g_dir_close (dir); + } +return (int )batt_exist; +} + +static gboolean read_val_from_file (const gchar *dev,const gchar *f,gint64 *val) +{ +gboolean ret = TRUE;
Should this be not FALSE by default? If the next "if" branch is false, it stays TRUE, even though it fails to read anything.
Quoted 6 lines
applets/battery/battery-sysfs.c new file +6
-0+gchar *endptr; + +if (dev !=NULL && f !=NULL && val !=NULL ) { +gchar *file_path =g_build_filename (PROCFS_PS_DIR , dev, f,NULL ); +gchar *file_content; +gsize length;
applets/battery/battery-sysfs.c line 83
Quoted 10 lines
applets/battery/battery-sysfs.c new file +10
-0+ +if (g_file_get_contents (file_path, &file_content, &length,NULL )) { + *val =g_ascii_strtoll (file_content, &endptr,10 ); + }else { + ret = FALSE; + } + +g_free (file_content); +g_free (file_path); +return ret;
This return above seems to be superfluous in view of the next statement.
applets/battery/battery-sysfs.c line 92
Quoted 9 lines
applets/battery/battery-sysfs.c new file +9
-0+ } +return ret; +} + +const char *pm_battery_icon (void ) +{ +const char *icon; + +gint64 temp_val =0 ;
Space and tabs are mixed here. There are some other instances of this in this patch.
applets/battery/battery-sysfs.c line 93
Quoted 1 line
applets/battery/battery-sysfs.c new file +1
-0+gint64 ac_sts =0 ;
Nitpick: batt_present and batt_percentage are nicely spelled out. Could this be called also ac_status?
applets/battery/battery-sysfs.c line 102
Quoted 9 lines
applets/battery/battery-sysfs.c new file +9
-0+gint64 batt_present =0 ; +gint64 batt_percentage =0 ; + +if (batt_exist ==0 ) +return NULL ; + +/* Check battery presence */ +if (read_val_from_file (batt_dev,"present" , &temp_val)) + batt_present = temp_val;
Nitpick: I wonder if checking for "present" is the best way to determine if a battery is present. This file might not exist in all drivers, in which case the battery is considered to be present (according to sysfs docs). On the other hand, as a first version I consider it good enough, it can be iterated, when/if needed.
applets/battery/battery-sysfs.c line 113
Quoted 11 lines
applets/battery/battery-sysfs.c new file +11
-0+ +if (batt_present ==0 ) { +/* Battery is removed */ + icon ="ac-adapter.png" ; +return icon; + } + +/* Check Adaptor status*/ +if (read_val_from_file (ac_dev,"online" , &temp_val)) + ac_sts = temp_val; +
ac_exist isn't verified before trying to read. While I believe it shouldn't lead to anything bad, but it is checked later when the icon is displayed.
Quoted 84 lines
applets/battery/battery-sysfs.c new file +35
-0+/* Check battery percentage */ +if (read_val_from_file (batt_dev,"capacity" , &temp_val)) + batt_percentage = temp_val; + +if (ac_exist && ac_sts ==1 ) { +/* We're charging */ +if (batt_percentage <10 ) + icon ="battery-charging-000.png" ; +else if (batt_percentage <30 ) + icon ="battery-charging-020.png" ; +else if (batt_percentage <50 ) + icon ="battery-charging-040.png" ; +else if (batt_percentage <70 ) + icon ="battery-charging-060.png" ; +else if (batt_percentage <90 ) + icon ="battery-charging-080.png" ; +else + icon ="battery-charging-100.png" ; + }else { +if (batt_percentage <10 ) + icon ="battery-discharging-000.png" ; +else if (batt_percentage <30 ) + icon ="battery-discharging-020.png" ; +else if (batt_percentage <50 ) + icon ="battery-discharging-040.png" ; +else if (batt_percentage <70 ) + icon ="battery-discharging-060.png" ; +else if (batt_percentage <90 ) + icon ="battery-discharging-080.png" ; +else + icon ="battery-discharging-100.png" ; + } + +return icon; +}applets/battery/battery.c +2
-0@@ -24,6 +24,8 @@ battery_applet_free (BatteryApplet *applet)g_source_remove (applet->timeout_id);g_slice_free (BatteryApplet, applet); + +pm_cleanup (); } static gbooleanapplets/battery/battery.h +1
-2@@ -9,8 +9,7 @@#ifndef MB_APPLET_BATTERY_H #define MB_APPLET_BATTERY_H -#include <string.h> - +void pm_cleanup (void );int pm_support (void );const char *pm_battery_icon (void );configure.ac +4
-0@@ -61,6 +61,9 @@ AC_ARG_WITH( ) case "$with_battery" in + "sysfs") enable_linux_sysfs=yes + enable_battery=yes + ;; "acpi") AC_CHECK_HEADERS(libacpi.h, enable_linux_acpi=yes, AC_MSG_FAILURE([You need to install libacpi])) enable_battery=yes ;;@@ -75,6 +78,7 @@ esac AM_CONDITIONAL(HAVE_BATTERY, test x$enable_battery = xyes) AM_CONDITIONAL(HAVE_LIBAPM, test x$enable_linux_apm = xyes) AM_CONDITIONAL(HAVE_LIBACPI, test x$enable_linux_acpi = xyes) +AM_CONDITIONAL(HAVE_SYSFS, test x$enable_linux_sysfs = xyes) # glib-genmarshal GLIB_GENMARSHAL=`$PKG_CONFIG --variable=glib_genmarshal glib-2.0`
Quoted 5 lines
I'm not very well versed in gtk, so if anyone tells me that it is idiomatic, I will believe it, but: why are these guchar? The code doesn't seem to do anything char-like with them, they are used as bools. Why not guint, or gboolean?
Ideally it should be gboolean, I dont recall why I kept it guchar. I will shift to gboolean and see.
Quoted 9 lines
Quoted 6 lines
+static gboolean
read_val_from_file(const gchar *dev, const gchar *f, gint64 *val)
+{
+ gboolean ret = TRUE;Should this be not FALSE by default? If the next "if" branch is false, it stays TRUE, even though it fails to read anything.
Its rare that we'll land to that condition but yeah theoretically it should be false. Will revise this function.
Quoted 22 lines
Quoted 8 lines
+ gchar *endptr;
applets/battery/battery-sysfs.c new file +5
-0+ +if (dev !=NULL && f !=NULL && val !=NULL ) { +gchar *file_path =g_build_filename (PROCFS_PS_DIR , dev, f,NULL ); +gchar *file_content; +gsize length;applets/battery/battery-sysfs.c line 83
Quoted 10 lines
applets/battery/battery-sysfs.c new file +10
-0+ +if (g_file_get_contents (file_path, &file_content, &length,NULL )) { + *val =g_ascii_strtoll (file_content, &endptr,10 ); + }else { + ret = FALSE; + } + +g_free (file_content); +g_free (file_path); +return ret;This return above seems to be superfluous in view of the next statement.
Quoted 12 lines
applets/battery/battery-sysfs.c line 92
Quoted 9 lines
+ }
+ return ret;applets/battery/battery-sysfs.c new file +7
-0+} + +const char *pm_battery_icon (void ) +{ +const char *icon; + +gint64 temp_val =0 ;Space and tabs are mixed here. There are some other instances of this in this patch.
Yeah, I noticed that once I submitted, I wonder why I am not seeing this garbage locally in vim. Need to check.
Quoted 6 lines
Quoted 3 lines
+ gint64
ac_sts= 0;Nitpick:
batt_presentandbatt_percentageare nicely spelled out. Could this be called alsoac_status?
True, will make it uniform.
Quoted 17 lines
applets/battery/battery-sysfs.c line 102
Quoted 11 lines
+ gint64
batt_present= 0;
+ gint64batt_percentage= 0;applets/battery/battery-sysfs.c new file +7
-0+ +if (batt_exist ==0 ) +return NULL ; + +/* Check battery presence */ +if (read_val_from_file (batt_dev,"present" , &temp_val)) + batt_present = temp_val;Nitpick: I wonder if checking for "present" is the best way to determine if a battery is present. This file might not exist in all drivers, in which case the battery is considered to be present (according to sysfs docs). On the other hand, as a first version I consider it good enough, it can be iterated, when/if needed.
In the whatever drivers I have seen so far while implementing this, present is the base entry for any battery device in power subsystem.
Quoted 17 lines
applets/battery/battery-sysfs.c line 113
Quoted 13 lines
applets/battery/battery-sysfs.c new file +11
-0+ +if (batt_present ==0 ) { +/* Battery is removed */ + icon ="ac-adapter.png" ; +return icon; + } + +/* Check Adaptor status*/ +if (read_val_from_file (ac_dev,"online" , &temp_val)) + ac_sts = temp_val; +
ac_existisn't verified before trying to read. While I believe it shouldn't lead to anything bad, but it is checked later when the icon is displayed.
Will add the check.
Thanks for the review,
Dixit
One more note, I'm sorry sorry for forgetting this from the previous mail.
applets/battery/battery-sysfs.c line 69
Quoted 6 lines · Dixit Parmar via lists.yoctoproject.org
applets/battery/battery-sysfs.c new file +6
-0+ +static gboolean read_val_from_file (const gchar *dev,const gchar *f,gint64 *val) +{ +gboolean ret = TRUE; +gchar *endptr; +
The endptr variable is not used at all, I think it can be removed. According to the docs g_ascii_strtoll accepts NULL also instead of a pointer as the second argument.
Quoted 18 lines
applets/battery/battery-sysfs.c new file +17
-0+if (dev !=NULL && f !=NULL && val !=NULL ) { +gchar *file_path =g_build_filename (PROCFS_PS_DIR , dev, f,NULL ); +gchar *file_content; +gsize length; + +if (g_file_get_contents (file_path, &file_content, &length,NULL )) { + *val =g_ascii_strtoll (file_content, &endptr,10 ); + }else { + ret = FALSE; + } + +g_free (file_content); +g_free (file_path); +return ret; + } +return ret; +}
Quoted 6 lines · Gyorgy Sarvari
One more note, I'm sorry sorry for forgetting this from the previous mail.
On 7/16/25 05:17, Dixit Parmar via lists.yoctoproject.org wrote:
Quoted 11 lines
applets/battery/battery-sysfs.c line 69
Quoted 7 lines
(quoted diff) +2
-0+ +static gboolean read_val_from_file(const gchar *dev, const gchar *f,gint64 *val)
applets/battery/battery-sysfs.c new file +4
-0+{ +gboolean ret = TRUE; +gchar *endptr; +The endptr variable is not used at all, I think it can be removed. According to the docs
g_ascii_strtollaccepts NULL also instead of a pointer as the second argument.
Noted.