diff mbox series

[matchbox-panel-2,v2] panel: added sysfs support

Message ID 20241022171035.460059-1-dixitparmar19@gmail.com
State New
Headers show
Series [matchbox-panel-2,v2] panel: added sysfs support | expand

Commit Message

Dixit Parmar Oct. 22, 2024, 5:10 p.m. UTC
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

Signed-off-by: Dixit Parmar <dixitparmar19@gmail.com>
---
 applets/battery/Makefile.am     |   4 +
 applets/battery/battery-sysfs.c | 155 ++++++++++++++++++++++++++++++++
 applets/battery/battery.h       |   5 +-
 configure.ac                    |   4 +
 4 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 applets/battery/battery-sysfs.c

Comments

Alexander Kanavin Oct. 23, 2024, 9:37 a.m. UTC | #1
Hello Dixit,

This looks fine, except for one point.

On Tue, 22 Oct 2024 at 19:11, Dixit Parmar via lists.yoctoproject.org
<dixitparmar19=gmail.com@lists.yoctoproject.org> wrote:
> +++ b/applets/battery/battery-sysfs.c
> @@ -0,0 +1,155 @@
> +/*
> + * (C) 2008 Intel.
> + *
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + *
> + * Licensed under the GPL v2 or greater.
> + */
> +
> +#include "battery.h"

The origin and copyright of this new file needs to be explained. Have
you written it yourself or was it copied partly or wholly from a
different place? If so, why does it have an Intel copyright dating to
2008?

If you've written it yourself, was it done as part of your work
duties, and if so, should the copyright be assigned to your employer?

Alex
Dixit Parmar Oct. 23, 2024, 10:12 a.m. UTC | #2
Hi Alex,
Thank you for review. from what I understood from your comment,
The file[battery-sysfs.c] was created solely by me as an individual contributor, out of my work duties with my employer.
The file header(along with copyright details) is copied straight away from the same codebase's existing file: https://git.yoctoproject.org/matchbox-panel-2/tree/applets/battery/battery-acpi.c. ( https://git.yoctoproject.org/matchbox-panel-2/tree/applets/battery/battery-acpi.c ) I am not good with the licensing terms, so I am not exactly sure what the correct one should be. Pls guide me for correct header.
Thanks,
Dixit
Alexander Kanavin Oct. 23, 2024, 10:24 a.m. UTC | #3
On Wed, 23 Oct 2024 at 12:12, Dixit Parmar via lists.yoctoproject.org
<dixitparmar19=gmail.com@lists.yoctoproject.org> wrote:
> Thank you for review. from what I understood from your comment,
> The file[battery-sysfs.c] was created solely by me as an individual contributor, out of my work duties with my employer.
> The file header(along with copyright details) is copied straight away from the same codebase's existing file: https://git.yoctoproject.org/matchbox-panel-2/tree/applets/battery/battery-acpi.c. I am not good with the licensing terms, so I am not exactly sure what the correct one should be. Pls guide me for correct header.

I think
(C) Dixit Parmar
is fine. No need for the year.

Alex
Alexander Kanavin Oct. 23, 2024, 10:25 a.m. UTC | #4
On Wed, 23 Oct 2024 at 12:24, Alexander Kanavin <alex.kanavin@gmail.com> wrote:
> > Thank you for review. from what I understood from your comment,
> > The file[battery-sysfs.c] was created solely by me as an individual contributor, out of my work duties with my employer.
> > The file header(along with copyright details) is copied straight away from the same codebase's existing file: https://git.yoctoproject.org/matchbox-panel-2/tree/applets/battery/battery-acpi.c. I am not good with the licensing terms, so I am not exactly sure what the correct one should be. Pls guide me for correct header.
>
> I think
> (C) Dixit Parmar
> is fine. No need for the year.

And the rest of the header should be as it is.

Alex
Dixit Parmar Oct. 23, 2024, 10:46 a.m. UTC | #5
Alex, Okay, thanks. If you have some examples handy, can you show them (mentioning the personal copyrights)?
Alexander Kanavin Oct. 23, 2024, 1:09 p.m. UTC | #6
On Wed, 23 Oct 2024 at 12:46, Dixit Parmar via lists.yoctoproject.org
<dixitparmar19=gmail.com@lists.yoctoproject.org> wrote:
>
> Alex, Okay, thanks. If you have some examples handy, can you show them (mentioning the personal copyrights)?

You can find them anywhere. For example in Linux kernel:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/arm/kernel/irq.c

The important thing is that it must say who holds the copyright, and
what the license is (in the form of spdx identifier). If you add to an
existing project, the license should follow the rest of the project.

Alex
Dixit Parmar Oct. 24, 2024, 11:10 a.m. UTC | #7
Thanks Alex, I have pushed v3 patch with the suggested modification.
Alexander Kanavin Oct. 24, 2024, 11:16 a.m. UTC | #8
Thanks, v3 looks good. We don't really have maintainers for these
items, but hopefully Ross can pick it up and push to repo.

Alex

On Thu, 24 Oct 2024 at 13:10, Dixit Parmar via lists.yoctoproject.org
<dixitparmar19=gmail.com@lists.yoctoproject.org> wrote:
>
> Thanks Alex, I have pushed v3 patch with the suggested modification.
> _._,_._,_
> ________________________________
> Links:
>
> You receive all messages sent to this group.
>
> View/Reply Online (#741) | Reply To Group | Reply To Sender | Mute This Topic | New Topic
> Your Subscription | Contact Group Owner | Unsubscribe [alex.kanavin@gmail.com]
>
> _._,_._,_
diff mbox series

Patch

diff --git a/applets/battery/Makefile.am b/applets/battery/Makefile.am
index 9b02240..1b93a0b 100644
--- a/applets/battery/Makefile.am
+++ b/applets/battery/Makefile.am
@@ -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
diff --git a/applets/battery/battery-sysfs.c b/applets/battery/battery-sysfs.c
new file mode 100644
index 0000000..523b652
--- /dev/null
+++ b/applets/battery/battery-sysfs.c
@@ -0,0 +1,155 @@ 
+/*
+ * (C) 2008 Intel.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Licensed under the GPL v2 or greater.
+ */
+
+#include "battery.h"
+
+#define PROCFS_PS_DIR "/sys/class/power_supply"
+
+static char batt_dev[128] = "";
+static char ac_dev[128] = "";
+static char batt_exist = 0;
+static char ac_exist = 0;
+
+int read_file_content(const char *file_path, char *buff, size_t len)
+{
+	FILE *fd = NULL;
+	int ret = 0;
+
+	fd = fopen(file_path, "r");
+	char *line = NULL;
+	if(fd != NULL)
+	{
+		ret = getline(&line, &len, fd);
+		if(ret > 0)
+		{
+			memcpy(buff, line, ret);
+		}
+		free(line);
+		fclose(fd);
+	}
+	return ret;
+}
+
+int read_file(const char *dev, const char *f, char *buff, size_t len)
+{
+	char file_path[256] = "";
+	printf("%s/%s\n", dev, f);
+
+	if(dev != NULL && f != NULL && buff != NULL)
+	{
+		memset(buff, 0, len);
+		snprintf(file_path, sizeof(file_path), "%s/%s/%s", PROCFS_PS_DIR, dev, f);
+		return read_file_content(file_path, buff, len);
+	}
+	return -1;
+}
+
+int pm_support(void)
+{
+	DIR *dir;
+	struct dirent *in_file;
+	dir = opendir(PROCFS_PS_DIR);
+	char buff[128];
+	int ret;
+
+	if(dir != NULL)
+	{
+		while(in_file = readdir(dir))
+		{
+			if(in_file->d_name[0] == '.') /* Skip . & .. directory */
+				continue;
+			if(in_file->d_type == DT_DIR || in_file->d_type == DT_LNK)
+			{
+				ret = read_file(in_file->d_name, "type", buff, sizeof(buff));
+				if(ret > 0)
+				{
+					if(!batt_exist && strncmp(buff, "Battery", strlen("Battery")) == 0)
+					{
+						strncpy(batt_dev, in_file->d_name, strlen(in_file->d_name));
+						batt_exist = 1;
+					}
+					if(!ac_exist && strncmp(buff, "Mains", strlen("Mains")) == 0)
+					{
+						strncpy(ac_dev, in_file->d_name, strlen(in_file->d_name));
+						ac_exist = 1;
+					}
+				}
+			}
+			if(batt_exist && ac_exist)
+				break;
+		}
+	}
+	return (int)batt_exist;
+}
+
+const char* pm_battery_icon(void)
+{
+	const char *icon;
+	char buff[128] = "";
+	int ret = 0;
+	int ac_sts = 0;
+	int batt_present = 0;
+	int batt_percentage = 0;
+
+	if(batt_exist == 0)
+		return NULL;
+
+	/* Check battery presence */
+	ret = read_file(batt_dev, "present", buff, sizeof(buff));
+	if(ret > 0)
+		batt_present = atoi(buff);
+	if(batt_present == 0)
+	{
+		/* Battery is removed */
+		icon = "ac-adapter.png";
+		return icon;
+	}
+
+	/* Check Adaptor status*/
+	ret = read_file(ac_dev, "online", buff, sizeof(buff));
+	if(ret > 0)
+		ac_sts = atoi(buff);
+
+	/* Check battery percentage */
+	ret = read_file(batt_dev, "capacity", buff, sizeof(buff));
+	if(ret > 0)
+		batt_percentage = atoi(buff);
+
+	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;
+}
diff --git a/applets/battery/battery.h b/applets/battery/battery.h
index cf23431..5c60744 100644
--- a/applets/battery/battery.h
+++ b/applets/battery/battery.h
@@ -6,8 +6,11 @@ 
 
 #ifndef MB_APPLET_BATTERY_H
 #define MB_APPLET_BATTERY_H
-
+#include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <dirent.h>
 
 int pm_support(void);
 const char* pm_battery_icon(void);
diff --git a/configure.ac b/configure.ac
index d6c57d8..7038212 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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`