[hackers] [slstatus] battery: Minor coding style fixes || Aaron Marcher

2018-05-20 Thread git
commit 103945e7bb8d3a4198cf175b8f6be09fe53ab9e1
Author: Aaron Marcher 
AuthorDate: Mon May 21 00:36:59 2018 +0200
Commit: Aaron Marcher 
CommitDate: Mon May 21 00:38:13 2018 +0200

battery: Minor coding style fixes

- Line length
- Spacing and indentation
- No explicit checks for NULL

diff --git a/components/battery.c b/components/battery.c
index ff06f40..6105a37 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -9,19 +9,22 @@
#include 
#include 
 
-   #define CHARGE_NOW"/sys/class/power_supply/%s/charge_now"
-   #define ENERGY_NOW"/sys/class/power_supply/%s/energy_now"
-   #define CURRENT_NOW   "/sys/class/power_supply/%s/current_now"
-   #define POWER_NOW "/sys/class/power_supply/%s/power_now"
+   #define CHARGE_NOW "/sys/class/power_supply/%s/charge_now"
+   #define ENERGY_NOW "/sys/class/power_supply/%s/energy_now"
+   #define CURRENT_NOW "/sys/class/power_supply/%s/current_now"
+   #define POWER_NOW "/sys/class/power_supply/%s/power_now"
 
static const char *
-   pick(const char *bat, const char *f1, const char *f2, char *path, 
size_t length)
+   pick(const char *bat, const char *f1, const char *f2, char *path,
+size_t length)
{
-   if (esnprintf(path, length, f1, bat) > 0 && access(path, R_OK) 
== 0) {
+   if (esnprintf(path, length, f1, bat) > 0 &&
+   access(path, R_OK) == 0) {
return f1;
}
 
-   if (esnprintf(path, length, f2, bat) > 0 && access(path, R_OK) 
== 0) {
+   if (esnprintf(path, length, f2, bat) > 0 &&
+   access(path, R_OK) == 0) {
return f2;
}
 
@@ -92,13 +95,14 @@
return NULL;
}
 
-   if (pick(bat, CHARGE_NOW, ENERGY_NOW, path, sizeof (path)) == 
NULL ||
+   if (!pick(bat, CHARGE_NOW, ENERGY_NOW, path, sizeof(path)) ||
pscanf(path, "%d", _now) < 0) {
return NULL;
}
 
if (!strcmp(state, "Discharging")) {
-   if (pick(bat, CURRENT_NOW, POWER_NOW, path, sizeof 
(path)) == NULL ||
+   if (!pick(bat, CURRENT_NOW, POWER_NOW, path,
+ sizeof(path)) ||
pscanf(path, "%d", _now) < 0) {
return NULL;
}
@@ -182,7 +186,8 @@
 
if (load_apm_power_info(_info)) {
if (apm_info.ac_state != APM_AC_ON) {
-   return bprintf("%uh %02um", 
apm_info.minutes_left / 60,
+   return bprintf("%uh %02um",
+  apm_info.minutes_left / 60,
   apm_info.minutes_left % 60);
} else {
return "";



[hackers] [slstatus] battery_remaining: check for division by zero || Aaron Marcher

2018-05-20 Thread git
commit aa8654795da19a8d517faf1b84cd4dc4ac75f6e7
Author: Aaron Marcher 
AuthorDate: Mon May 21 00:42:06 2018 +0200
Commit: Aaron Marcher 
CommitDate: Mon May 21 00:42:06 2018 +0200

battery_remaining: check for division by zero

diff --git a/components/battery.c b/components/battery.c
index fa525be..8bfe42a 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -107,6 +107,10 @@
return NULL;
}
 
+   if (current_now == 0) {
+   return NULL;
+   }
+
timeleft = (double)charge_now / (double)current_now;
h = timeleft;
m = (timeleft - (double)h) * 60;



[hackers] [slstatus] battery_remaining: Change float to double || Aaron Marcher

2018-05-20 Thread git
commit a546d4b585234f66b82e048c2d7cb228b9e7fea4
Author: Aaron Marcher 
AuthorDate: Mon May 21 00:41:03 2018 +0200
Commit: Aaron Marcher 
CommitDate: Mon May 21 00:41:03 2018 +0200

battery_remaining: Change float to double

diff --git a/components/battery.c b/components/battery.c
index 6105a37..fa525be 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -83,7 +83,7 @@
battery_remaining(const char *bat)
{
int charge_now, current_now, m, h;
-   float timeleft;
+   double timeleft;
char path[PATH_MAX], state[12];
 
if (esnprintf(path, sizeof(path),
@@ -107,9 +107,9 @@
return NULL;
}
 
-   timeleft = (float)charge_now / (float)current_now;
+   timeleft = (double)charge_now / (double)current_now;
h = timeleft;
-   m = (timeleft - (float)h) * 60;
+   m = (timeleft - (double)h) * 60;
 
return bprintf("%dh %dm", h, m);
}



[hackers] [slstatus] Increase readability for load_uvmexp return || Aaron Marcher

2018-05-20 Thread git
commit bae576cd221ef8f30195d6927cf935a797ac5a02
Author: Aaron Marcher 
AuthorDate: Mon May 21 00:29:54 2018 +0200
Commit: Aaron Marcher 
CommitDate: Mon May 21 00:29:54 2018 +0200

Increase readability for load_uvmexp return

diff --git a/components/ram.c b/components/ram.c
index f653156..cba22a1 100644
--- a/components/ram.c
+++ b/components/ram.c
@@ -84,7 +84,11 @@
 
size = sizeof(*uvmexp);
 
-   return sysctl(uvmexp_mib, 2, uvmexp, , NULL, 0) >= 0 ? 1 : 
0;
+   if (sysctl(uvmexp_mib, 2, uvmexp, , NULL, 0) >= 0) {
+   return 1;
+   }
+
+   return 0;
}
 
const char *



[hackers] [slstatus] Remove units from numbers || Aaron Marcher

2018-05-20 Thread git
commit ec5c35ec9f23254f09e734d0c1880559774dfa52
Author: Aaron Marcher 
AuthorDate: Mon May 21 00:16:54 2018 +0200
Commit: Aaron Marcher 
CommitDate: Mon May 21 00:16:54 2018 +0200

Remove units from numbers

This is a first step to decouple formatting from information because of
two reasons:

 1. The components should only gather and return the values by design
 2. Fine grained user control should be a focus

Scaling will be implemented in a different way in a later commit.

diff --git a/components/battery.c b/components/battery.c
index 5d7a385..ff06f40 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -43,7 +43,7 @@
return NULL;
}
 
-   return bprintf("%d%%", perc);
+   return bprintf("%d", perc);
}
 
const char *
@@ -144,7 +144,7 @@
struct apm_power_info apm_info;
 
if (load_apm_power_info(_info)) {
-   return bprintf("%d%%", apm_info.battery_life);
+   return bprintf("%d", apm_info.battery_life);
}
 
return NULL;
diff --git a/components/cpu.c b/components/cpu.c
index 53af71d..3fadb36 100644
--- a/components/cpu.c
+++ b/components/cpu.c
@@ -20,7 +20,7 @@
return NULL;
}
 
-   return fmt_human_10(freq * 1000, "Hz");
+   return fmt_human_10(freq * 1000);
}
 
const char *
@@ -39,7 +39,7 @@
return NULL;
}
 
-   return bprintf("%d%%", (int)(100 *
+   return bprintf("%d", (int)(100 *
   ((b[0] + b[1] + b[2] + b[5] + b[6]) -
(a[0] + a[1] + a[2] + a[5] + a[6])) /
   ((b[0] + b[1] + b[2] + b[3] + b[4] + b[5] + 
b[6]) -
@@ -67,7 +67,7 @@
return NULL;
}
 
-   return fmt_human_10((size_t)freq * 1000 * 1000, "Hz");
+   return fmt_human_10((size_t)freq * 1000 * 1000);
}
 
const char *
@@ -92,7 +92,7 @@
return NULL;
}
 
-   return bprintf("%d%%", 100 *
+   return bprintf("%d", 100 *
   ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + 
a[CP_INTR]) -
(b[CP_USER] + b[CP_NICE] + b[CP_SYS] + 
b[CP_INTR])) /
   ((a[CP_USER] + a[CP_NICE] + a[CP_SYS] + 
a[CP_INTR] +
diff --git a/components/disk.c b/components/disk.c
index 998ad47..cf3c099 100644
--- a/components/disk.c
+++ b/components/disk.c
@@ -16,7 +16,7 @@ disk_free(const char *mnt)
return NULL;
}
 
-   return fmt_human_2(fs.f_frsize * fs.f_bavail, "B");
+   return fmt_human_2(fs.f_frsize * fs.f_bavail);
 }
 
 const char *
@@ -29,7 +29,7 @@ disk_perc(const char *mnt)
return NULL;
}
 
-   return bprintf("%d%%", (int)(100 *
+   return bprintf("%d", (int)(100 *
   (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks;
 }
 
@@ -43,7 +43,7 @@ disk_total(const char *mnt)
return NULL;
}
 
-   return fmt_human_2(fs.f_frsize * fs.f_blocks, "B");
+   return fmt_human_2(fs.f_frsize * fs.f_blocks);
 }
 
 const char *
@@ -56,5 +56,5 @@ disk_used(const char *mnt)
return NULL;
}
 
-   return fmt_human_2(fs.f_frsize * (fs.f_blocks - fs.f_bfree), "B");
+   return fmt_human_2(fs.f_frsize * (fs.f_blocks - fs.f_bfree));
 }
diff --git a/components/netspeeds.c b/components/netspeeds.c
index 14b7799..76557e0 100644
--- a/components/netspeeds.c
+++ b/components/netspeeds.c
@@ -29,8 +29,7 @@
return NULL;
}
 
-   return fmt_human_2((rxbytes - oldrxbytes) *
-  1000 / interval, "B/s");
+   return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
}
 
const char *
@@ -55,8 +54,7 @@
return NULL;
}
 
-   return fmt_human_2((txbytes - oldtxbytes) *
-  1000 / interval, "B/s");
+   return fmt_human_2((txbytes - oldtxbytes) * 1000 / interval);
}
 #elif defined(__OpenBSD__)
#include 
@@ -97,8 +95,7 @@
return NULL;
}
 
-   return fmt_human_2((rxbytes - oldrxbytes) *
-  1000 / interval, "B/s");
+   return fmt_human_2((rxbytes - oldrxbytes) * 1000 / interval);
}
 
const char *
@@ -133,7 +130,6 @@
return NULL;
}
 
-   return fmt_human_2((txbytes - oldtxbytes) *
-  1000 / interval, "B/s");
+   return fmt_human_2((txbytes - 

[hackers] [slstatus] swap_perc: check for division by zero on obsd too || Aaron Marcher

2018-05-20 Thread git
commit 4bd234c7ef7224117a6e50eda4ce3184338daab1
Author: Aaron Marcher 
AuthorDate: Mon May 21 00:02:33 2018 +0200
Commit: Aaron Marcher 
CommitDate: Mon May 21 00:02:33 2018 +0200

swap_perc: check for division by zero on obsd too

diff --git a/components/swap.c b/components/swap.c
index 465ffd4..f5db667 100644
--- a/components/swap.c
+++ b/components/swap.c
@@ -188,6 +188,10 @@
 
getstats(, );
 
+   if (total == 0) {
+   return NULL;
+   }
+
return bprintf("%d%%", 100 * used / total);
}
 



[hackers] [slstatus] swap_perc: check for division by zero || Aaron Marcher

2018-05-20 Thread git
commit 806815778fe75f13b569a1b5fe6d6d74a50c89ff
Author: Aaron Marcher 
AuthorDate: Sun May 20 23:53:26 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sun May 20 23:53:26 2018 +0200

swap_perc: check for division by zero

diff --git a/components/swap.c b/components/swap.c
index c005691..465ffd4 100644
--- a/components/swap.c
+++ b/components/swap.c
@@ -76,6 +76,10 @@
}
sscanf(match, "SwapFree: %ld kB\n", );
 
+   if (total == 0) {
+   return NULL;
+   }
+
return bprintf("%d%%", 100 * (total - free - cached) / total);
}
 



Re: [hackers] slstatus keymap patch idea

2018-05-20 Thread Michael Buch
Cool, I’ll see if i can put something together for linux.

Regards,

Michael

On Sun, 20 May 2018 at 22:42, Aaron Marcher  wrote:

> Michael,
>
> > I’ve been using slstatus for a while and am really enjoying it.
>
> Thanks!
>
> > Was thinking of adding a patch to the current slstatus for a keymap
> > indicator component. Is it worth adding this feature now or later
> > after the release as a custom patch?
>
> I thought about the same thing a few days ago and I like the idea of
> adding it natively to slstatus. Adding it before the release is fine.
>
> > I guess it could be replicated by a run_command using setxkbmap but
> > would be nice as a separate component out of the box.
>
> Implementing it using Xlib is more elegant.
>
> Cheers!
> Aaron
>
> --
> Web: https://drkhsh.at/ or http://drkhsh5rv6pnahas.onion/
> Gopher: gopher://drkhsh.at or gopher://drkhsh5rv6pnahas.onion
> GPG: 0x7A65E38D55BE96FE
> Fingerprint: 4688 907C 8720 3318 0D9F AFDE 7A65 E38D 55BE 96FE
>
>


Re: [hackers] slstatus keymap patch idea

2018-05-20 Thread Aaron Marcher

Michael,


I’ve been using slstatus for a while and am really enjoying it.


Thanks!


Was thinking of adding a patch to the current slstatus for a keymap
indicator component. Is it worth adding this feature now or later
after the release as a custom patch?


I thought about the same thing a few days ago and I like the idea of 
adding it natively to slstatus. Adding it before the release is fine.


I guess it could be replicated by a run_command using setxkbmap but 
would be nice as a separate component out of the box.


Implementing it using Xlib is more elegant.

Cheers!
Aaron

--
Web: https://drkhsh.at/ or http://drkhsh5rv6pnahas.onion/
Gopher: gopher://drkhsh.at or gopher://drkhsh5rv6pnahas.onion
GPG: 0x7A65E38D55BE96FE
Fingerprint: 4688 907C 8720 3318 0D9F AFDE 7A65 E38D 55BE 96FE



[hackers] slstatus keymap patch idea

2018-05-20 Thread Michael Buch
Hey,

I’ve been using slstatus for a while and am really enjoying it. One thing I
sometimes struggle with is keeping track of keyboard layouts.

Was thinking of adding a patch to the current slstatus for a keymap
indicator component. Is it worth adding this feature now or later after the
release as a custom patch?

I guess it could be replicated by a run_command using setxkbmap but would
be nice as a separate component out of the box.

Regards,

Michael


Re: [hackers] [PATCH slstatus] Add basic backlight percentage support

2018-05-20 Thread Aaron Marcher

David,


Done and done.


do you send an updated patch with the check for != 1 for pscanf and the 
division by zero here or should I add it after applying?


Good catch, what do you recommend if max is 0? Resetting to 100? Or to 
return NULL to indicate an error?


I am not quite sure but if the max is 0 we should return NULL as 
something is weird.


Cheers!
Aaron

--
Web: https://drkhsh.at/ or http://drkhsh5rv6pnahas.onion/
Gopher: gopher://drkhsh.at or gopher://drkhsh5rv6pnahas.onion
GPG: 0x7A65E38D55BE96FE
Fingerprint: 4688 907C 8720 3318 0D9F AFDE 7A65 E38D 55BE 96FE



[hackers] [slstatus] Add David Demelier to LICENSE || Aaron Marcher

2018-05-20 Thread git
commit d8ce4e694217bc69e95372f705d023e71dd70115
Author: Aaron Marcher 
AuthorDate: Sun May 20 22:16:39 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sun May 20 22:16:39 2018 +0200

Add David Demelier to LICENSE

diff --git a/LICENSE b/LICENSE
index 99b88ed..bd6b13b 100644
--- a/LICENSE
+++ b/LICENSE
@@ -15,6 +15,7 @@ Copyright 2017-2018 Laslo Hunhold 
 Copyright 2018 Darron Anderson 
 Copyright 2018 Josuah Demangeon 
 Copyright 2018 Tobias Tschinkowitz 
+Copyright 2018 David Demelier 
 
 Permission to use, copy, modify, and/or distribute this software for any
 purpose with or without fee is hereby granted, provided that the above



Re: [hackers] [PATCH slstatus] Support energy_now/power_now in battery_remaining

2018-05-20 Thread Aaron Marcher

David,

thanks for the patch. I applied it.

Cheers!
Aaron

--
Web: https://drkhsh.at/ or http://drkhsh5rv6pnahas.onion/
Gopher: gopher://drkhsh.at or gopher://drkhsh5rv6pnahas.onion
GPG: 0x7A65E38D55BE96FE
Fingerprint: 4688 907C 8720 3318 0D9F AFDE 7A65 E38D 55BE 96FE



[hackers] [slstatus] Support energy_now/power_now in battery_remaining || David Demelier

2018-05-20 Thread git
commit f386a03a20ca3de30f761dba52d9d7196e3544e4
Author: David Demelier 
AuthorDate: Sun May 20 09:42:16 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sun May 20 22:12:44 2018 +0200

Support energy_now/power_now in battery_remaining

On some laptops (mostly thinkpads), the remaining time may be
expressed in µWh using energy_now and power_now files rather than µAh
for charge_now and current_now.

Add pick function to conditionally select appropriate one.

diff --git a/components/battery.c b/components/battery.c
index 5031608..5d7a385 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -7,6 +7,26 @@
 
 #if defined(__linux__)
#include 
+   #include 
+
+   #define CHARGE_NOW"/sys/class/power_supply/%s/charge_now"
+   #define ENERGY_NOW"/sys/class/power_supply/%s/energy_now"
+   #define CURRENT_NOW   "/sys/class/power_supply/%s/current_now"
+   #define POWER_NOW "/sys/class/power_supply/%s/power_now"
+
+   static const char *
+   pick(const char *bat, const char *f1, const char *f2, char *path, 
size_t length)
+   {
+   if (esnprintf(path, length, f1, bat) > 0 && access(path, R_OK) 
== 0) {
+   return f1;
+   }
+
+   if (esnprintf(path, length, f2, bat) > 0 && access(path, R_OK) 
== 0) {
+   return f2;
+   }
+
+   return NULL;
+   }
 
const char *
battery_perc(const char *bat)
@@ -72,21 +92,14 @@
return NULL;
}
 
+   if (pick(bat, CHARGE_NOW, ENERGY_NOW, path, sizeof (path)) == 
NULL ||
+   pscanf(path, "%d", _now) < 0) {
+   return NULL;
+   }
+
if (!strcmp(state, "Discharging")) {
-   if (esnprintf(path, sizeof(path),
-  "/sys/class/power_supply/%s/charge_now",
- bat) < 0) {
-   return NULL;
-   }
-   if (pscanf(path, "%d", _now) != 1) {
-   return NULL;
-   }
-   if (esnprintf(path, sizeof(path),
- "/sys/class/power_supply/%s/current_now",
- bat) < 0) {
-   return NULL;
-   }
-   if (pscanf(path, "%d", _now) != 1) {
+   if (pick(bat, CURRENT_NOW, POWER_NOW, path, sizeof 
(path)) == NULL ||
+   pscanf(path, "%d", _now) < 0) {
return NULL;
}
 



Re: [hackers] [slstatus][PATCH] ip: fixed memory leak

2018-05-20 Thread Aaron Marcher

Tobias,

thank you very much!

Cheers!
Aaron

--
Web: https://drkhsh.at/ or http://drkhsh5rv6pnahas.onion/
Gopher: gopher://drkhsh.at or gopher://drkhsh5rv6pnahas.onion
GPG: 0x7A65E38D55BE96FE
Fingerprint: 4688 907C 8720 3318 0D9F AFDE 7A65 E38D 55BE 96FE



[hackers] [slstatus] ip: fixed memory leak || Tobias Tschinkowitz

2018-05-20 Thread git
commit 1cd8a7b5109311e03fe9de2372c7adc483445db7
Author: Tobias Tschinkowitz 
AuthorDate: Sun May 20 15:53:48 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sun May 20 22:06:54 2018 +0200

ip: fixed memory leak

free the interface list before returning from the function

diff --git a/components/ip.c b/components/ip.c
index 0b6293e..468dc84 100644
--- a/components/ip.c
+++ b/components/ip.c
@@ -31,6 +31,7 @@ ip(const char *iface, unsigned short sa_family)
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (!strcmp(ifa->ifa_name, iface) &&
(ifa->ifa_addr->sa_family == sa_family)) {
+   freeifaddrs(ifaddr);
if (s != 0) {
warn("getnameinfo: %s", gai_strerror(s));
return NULL;



Re: [hackers] [PATCH slstatus] Add basic backlight percentage support

2018-05-20 Thread David Demelier
On Sun, May 20, 2018 at 03:18:14PM +0200, Hiltjo Posthuma wrote:
> On Sun, May 20, 2018 at 02:21:36PM +0200, David Demelier wrote:
> > +const char *
> > +backlight_perc(const char *card)
> > +{
> > +   char path[PATH_MAX];
> > +   int max, cur;
> > +
> > +   if (esnprintf(path, sizeof (path), BRIGHTNESS_MAX, card) < 0 ||
> > +   pscanf(path, "%d", ) < 0) {
> > +   return NULL;
> > +   }
> > +
> 
> Check for pscanf != 1 here.

Done and done.

> Maybe check for division by zero (max) here.

Good catch, what do you recommend if max is 0? Resetting to 100? Or to return
NULL to indicate an error?

Kind regards,

-- 
David



[hackers] [slstatus][PATCH] ip: fixed memory leak

2018-05-20 Thread Tobias Tschinkowitz
free the interface list before returning from the function 

---
 components/ip.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/components/ip.c b/components/ip.c
index 0b6293e..468dc84 100644
--- a/components/ip.c
+++ b/components/ip.c
@@ -31,6 +31,7 @@ ip(const char *iface, unsigned short sa_family)
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (!strcmp(ifa->ifa_name, iface) &&
(ifa->ifa_addr->sa_family == sa_family)) {
+   freeifaddrs(ifaddr);
if (s != 0) {
warn("getnameinfo: %s", gai_strerror(s));
return NULL;
-- 
2.16.2




Re: [hackers] [PATCH slstatus] Add basic backlight percentage support

2018-05-20 Thread Hiltjo Posthuma
On Sun, May 20, 2018 at 02:21:36PM +0200, David Demelier wrote:
> At the moment linux only, but will add support for FreeBSD and OpenBSD
> as well.
> ---
>  Makefile   |  1 +
>  components/backlight.c | 33 +
>  slstatus.h |  3 +++
>  3 files changed, 37 insertions(+)
>  create mode 100644 components/backlight.c
> 
> diff --git a/Makefile b/Makefile
> index 0e925cc..8e18969 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -6,6 +6,7 @@ include config.mk
>  
>  REQ = util
>  COM =\
> + components/backlight\
>   components/battery\
>   components/cpu\
>   components/datetime\
> diff --git a/components/backlight.c b/components/backlight.c
> new file mode 100644
> index 000..05c12aa
> --- /dev/null
> +++ b/components/backlight.c
> @@ -0,0 +1,33 @@
> +/* See LICENSE file for copyright and license details. */
> +
> +#include 
> +
> +#include "../util.h"
> +
> +#if defined(__linux__)
> +
> +#include 
> +
> +#define BRIGHTNESS_MAX "/sys/class/backlight/%s/max_brightness"
> +#define BRIGHTNESS_CUR "/sys/class/backlight/%s/brightness"
> +
> +const char *
> +backlight_perc(const char *card)
> +{
> + char path[PATH_MAX];
> + int max, cur;
> +
> + if (esnprintf(path, sizeof (path), BRIGHTNESS_MAX, card) < 0 ||
> + pscanf(path, "%d", ) < 0) {
> + return NULL;
> + }
> +

Check for pscanf != 1 here.

> + if (esnprintf(path, sizeof (path), BRIGHTNESS_CUR, card) < 0 ||
> + pscanf(path, "%d", ) < 0) {
> + return NULL;
> + }
> +

Check for pscanf != 1 here.

> + return bprintf("%d%%", cur * 100 / max);
> +}
> +

Maybe check for division by zero (max) here.

> +#endif
> diff --git a/slstatus.h b/slstatus.h
> index 8bd8bb5..a18b881 100644
> --- a/slstatus.h
> +++ b/slstatus.h
> @@ -1,5 +1,8 @@
>  /* See LICENSE file for copyright and license details. */
>  
> +/* backlight */
> +const char *backlight_perc(const char *);
> +
>  /* battery */
>  const char *battery_perc(const char *);
>  const char *battery_state(const char *);
> -- 
> 2.17.0
> 
> 

-- 
Kind regards,
Hiltjo



[hackers] [PATCH slstatus] Add basic backlight percentage support

2018-05-20 Thread David Demelier
At the moment linux only, but will add support for FreeBSD and OpenBSD
as well.
---
 Makefile   |  1 +
 components/backlight.c | 33 +
 slstatus.h |  3 +++
 3 files changed, 37 insertions(+)
 create mode 100644 components/backlight.c

diff --git a/Makefile b/Makefile
index 0e925cc..8e18969 100644
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,7 @@ include config.mk
 
 REQ = util
 COM =\
+   components/backlight\
components/battery\
components/cpu\
components/datetime\
diff --git a/components/backlight.c b/components/backlight.c
new file mode 100644
index 000..05c12aa
--- /dev/null
+++ b/components/backlight.c
@@ -0,0 +1,33 @@
+/* See LICENSE file for copyright and license details. */
+
+#include 
+
+#include "../util.h"
+
+#if defined(__linux__)
+
+#include 
+
+#define BRIGHTNESS_MAX "/sys/class/backlight/%s/max_brightness"
+#define BRIGHTNESS_CUR "/sys/class/backlight/%s/brightness"
+
+const char *
+backlight_perc(const char *card)
+{
+   char path[PATH_MAX];
+   int max, cur;
+
+   if (esnprintf(path, sizeof (path), BRIGHTNESS_MAX, card) < 0 ||
+   pscanf(path, "%d", ) < 0) {
+   return NULL;
+   }
+
+   if (esnprintf(path, sizeof (path), BRIGHTNESS_CUR, card) < 0 ||
+   pscanf(path, "%d", ) < 0) {
+   return NULL;
+   }
+
+   return bprintf("%d%%", cur * 100 / max);
+}
+
+#endif
diff --git a/slstatus.h b/slstatus.h
index 8bd8bb5..a18b881 100644
--- a/slstatus.h
+++ b/slstatus.h
@@ -1,5 +1,8 @@
 /* See LICENSE file for copyright and license details. */
 
+/* backlight */
+const char *backlight_perc(const char *);
+
 /* battery */
 const char *battery_perc(const char *);
 const char *battery_state(const char *);
-- 
2.17.0




[hackers] [PATCH slstatus] Support energy_now/power_now in battery_remaining

2018-05-20 Thread David Demelier
On some laptops (mostly thinkpads), the remaining time may be
expressed in µWh using energy_now and power_now files rather than µAh
for charge_now and current_now.

Add pick function to conditionally select appropriate one.
---
 components/battery.c | 41 +++--
 1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/components/battery.c b/components/battery.c
index 5031608..5d7a385 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -7,6 +7,26 @@
 
 #if defined(__linux__)
#include 
+   #include 
+
+   #define CHARGE_NOW"/sys/class/power_supply/%s/charge_now"
+   #define ENERGY_NOW"/sys/class/power_supply/%s/energy_now"
+   #define CURRENT_NOW   "/sys/class/power_supply/%s/current_now"
+   #define POWER_NOW "/sys/class/power_supply/%s/power_now"
+
+   static const char *
+   pick(const char *bat, const char *f1, const char *f2, char *path, 
size_t length)
+   {
+   if (esnprintf(path, length, f1, bat) > 0 && access(path, R_OK) 
== 0) {
+   return f1;
+   }
+
+   if (esnprintf(path, length, f2, bat) > 0 && access(path, R_OK) 
== 0) {
+   return f2;
+   }
+
+   return NULL;
+   }
 
const char *
battery_perc(const char *bat)
@@ -72,21 +92,14 @@
return NULL;
}
 
+   if (pick(bat, CHARGE_NOW, ENERGY_NOW, path, sizeof (path)) == 
NULL ||
+   pscanf(path, "%d", _now) < 0) {
+   return NULL;
+   }
+
if (!strcmp(state, "Discharging")) {
-   if (esnprintf(path, sizeof(path),
-  "/sys/class/power_supply/%s/charge_now",
- bat) < 0) {
-   return NULL;
-   }
-   if (pscanf(path, "%d", _now) != 1) {
-   return NULL;
-   }
-   if (esnprintf(path, sizeof(path),
- "/sys/class/power_supply/%s/current_now",
- bat) < 0) {
-   return NULL;
-   }
-   if (pscanf(path, "%d", _now) != 1) {
+   if (pick(bat, CURRENT_NOW, POWER_NOW, path, sizeof 
(path)) == NULL ||
+   pscanf(path, "%d", _now) < 0) {
return NULL;
}
 
-- 
2.17.0