[hackers] [slstatus] battery: Move out struct as common code || Aaron Marcher

2018-05-18 Thread git
commit aac29e2bba1d34762971718c40004f37415d411e
Author: Aaron Marcher 
AuthorDate: Sat May 19 01:49:46 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sat May 19 01:49:46 2018 +0200

battery: Move out struct as common code

diff --git a/components/battery.c b/components/battery.c
index 3965e8f..b086a28 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -5,6 +5,14 @@
 
 #include "../util.h"
 
+static struct {
+   char *state;
+   char *symbol;
+} map[] = {
+   { "Charging","+" },
+   { "Discharging", "-" },
+};
+
 #if defined(__linux__)
#include 
 
@@ -22,13 +30,6 @@
const char *
battery_state(const char *bat)
{
-   struct {
-   char *state;
-   char *symbol;
-   } map[] = {
-   { "Charging","+" },
-   { "Discharging", "-" },
-   };
size_t i;
char path[PATH_MAX], state[12];
 
@@ -123,13 +124,6 @@
{
struct apm_power_info apm_info;
size_t i;
-   struct {
-   unsigned int state;
-   char *symbol;
-   } map[] = {
-   { APM_AC_ON,  "+" },
-   { APM_AC_OFF, "-" },
-   };
 
if (load_apm_power_info(_info)) {
for (i = 0; i < LEN(map); i++) {



[hackers] [slstatus] implemented openbsd netspeed functions || Tobias Tschinkowitz

2018-05-18 Thread git
commit 22175f0d5792f1d046b6b501c694a3afabc877ed
Author: Tobias Tschinkowitz 
AuthorDate: Sat May 19 01:29:20 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sat May 19 01:44:36 2018 +0200

implemented openbsd netspeed functions

implemented the netspeed functionality for openbsd.
furthermore the static keyword was removed of the interval variable in
config.def.h for usage as extern variable.

diff --git a/components/netspeeds.c b/components/netspeeds.c
index ef8bf93..9315fef 100644
--- a/components/netspeeds.c
+++ b/components/netspeeds.c
@@ -49,5 +49,63 @@
return fmt_scaled((txbytes - oldtxbytes) / interval * 1000);
}
 #elif defined(__OpenBSD__)
-   /* unimplemented */
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+
+   const char *
+   netspeed_rx(const char *interface)
+   {
+   struct ifaddrs *ifal, *ifa;
+   struct if_data *ifd;
+   static uint64_t oldrxbytes;
+   uint64_t rxbytes = 0;
+   const char *rxs;
+   extern const unsigned int interval;
+
+   if (getifaddrs() == -1) {
+   warn("getifaddrs failed");
+   return NULL;
+   }
+   for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
+   if (!strcmp(ifa->ifa_name, interface) &&
+  (ifd = (struct if_data *)ifa->ifa_data)) {
+   rxbytes += ifd->ifi_ibytes;
+   }
+   }
+   freeifaddrs(ifal);
+
+   rxs = oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
+ interval * 1000) : NULL;
+   return (oldrxbytes = rxbytes, rxs);
+   }
+
+   const char *
+   netspeed_tx(const char *interface)
+   {
+   struct ifaddrs *ifal, *ifa;
+   struct if_data *ifd;
+   static uint64_t oldtxbytes;
+   uint64_t txbytes = 0;
+   const char *txs;
+   extern const unsigned int interval;
+
+   if (getifaddrs() == -1) {
+   warn("getifaddrs failed");
+   return NULL;
+   }
+   for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
+   if (!strcmp(ifa->ifa_name, interface) &&
+  (ifd = (struct if_data *)ifa->ifa_data)) {
+   txbytes += ifd->ifi_obytes;
+   }
+   }
+   freeifaddrs(ifal);
+
+   txs = oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
+ interval * 1000) : NULL;
+   return (oldtxbytes = txbytes, txs);
+   }
 #endif
diff --git a/config.def.h b/config.def.h
index bf6aef9..49ea282 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,7 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 
 /* interval between updates (in ms) */
-static const unsigned int interval = 1000;
+const unsigned int interval = 1000;
 
 /* text to show if no value can be retrieved */
 static const char unknown_str[] = "n/a";



[hackers] [slstatus] uptime: Format function is static || Aaron Marcher

2018-05-18 Thread git
commit 0383146e5e616e81c13b23a127ecfa697feac7db
Author: Aaron Marcher 
AuthorDate: Sat May 19 01:55:31 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sat May 19 01:55:31 2018 +0200

uptime: Format function is static

diff --git a/components/uptime.c b/components/uptime.c
index 45e12db..f97809d 100644
--- a/components/uptime.c
+++ b/components/uptime.c
@@ -3,7 +3,7 @@
 
 #include "../util.h"
 
-const char *
+static const char *
 format(int uptime)
 {
int h, m;



[hackers] [slstatus][PATCH] implemented openbsd netspeed functions

2018-05-18 Thread Tobias Tschinkowitz
implemented the netspeed functionality for openbsd.
furthermore the static keyword was removed of the interval variable in
config.def.h for usage as extern variable.

---
 components/netspeeds.c | 60 +-
 config.def.h   |  2 +-
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/components/netspeeds.c b/components/netspeeds.c
index ef8bf93..9315fef 100644
--- a/components/netspeeds.c
+++ b/components/netspeeds.c
@@ -49,5 +49,63 @@
return fmt_scaled((txbytes - oldtxbytes) / interval * 1000);
}
 #elif defined(__OpenBSD__)
-   /* unimplemented */
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+
+   const char *
+   netspeed_rx(const char *interface)
+   {
+   struct ifaddrs *ifal, *ifa;
+   struct if_data *ifd;
+   static uint64_t oldrxbytes;
+   uint64_t rxbytes = 0;
+   const char *rxs;
+   extern const unsigned int interval;
+
+   if (getifaddrs() == -1) {
+   warn("getifaddrs failed");
+   return NULL;
+   }
+   for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
+   if (!strcmp(ifa->ifa_name, interface) &&
+  (ifd = (struct if_data *)ifa->ifa_data)) {
+   rxbytes += ifd->ifi_ibytes;
+   }
+   }
+   freeifaddrs(ifal);
+
+   rxs = oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
+ interval * 1000) : NULL;
+   return (oldrxbytes = rxbytes, rxs);
+   }
+
+   const char *
+   netspeed_tx(const char *interface)
+   {
+   struct ifaddrs *ifal, *ifa;
+   struct if_data *ifd;
+   static uint64_t oldtxbytes;
+   uint64_t txbytes = 0;
+   const char *txs;
+   extern const unsigned int interval;
+
+   if (getifaddrs() == -1) {
+   warn("getifaddrs failed");
+   return NULL;
+   }
+   for (ifa = ifal; ifa; ifa = ifa->ifa_next) {
+   if (!strcmp(ifa->ifa_name, interface) &&
+  (ifd = (struct if_data *)ifa->ifa_data)) {
+   txbytes += ifd->ifi_obytes;
+   }
+   }
+   freeifaddrs(ifal);
+
+   txs = oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
+ interval * 1000) : NULL;
+   return (oldtxbytes = txbytes, txs);
+   }
 #endif
diff --git a/config.def.h b/config.def.h
index bf6aef9..49ea282 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,7 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 
 /* interval between updates (in ms) */
-static const unsigned int interval = 1000;
+const unsigned int interval = 1000;
 
 /* text to show if no value can be retrieved */
 static const char unknown_str[] = "n/a";
-- 
2.16.2




[hackers] [slstatus] Add netspeeds to config.def.h || Aaron Marcher

2018-05-18 Thread git
commit 176d8ff87418a22240a0b079d7dac593d103e984
Author: Aaron Marcher 
AuthorDate: Sat May 19 00:40:59 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sat May 19 00:40:59 2018 +0200

Add netspeeds to config.def.h

diff --git a/config.def.h b/config.def.h
index 495f44f..bf6aef9 100644
--- a/config.def.h
+++ b/config.def.h
@@ -34,6 +34,8 @@ static const char unknown_str[] = "n/a";
  * keyboard_indicators  caps/num lock indicatorsNULL
  * load_avg load averageformat string
  *  (%.2f %.2f %.2f)
+ * netspeed_rx  receive network speed   interface name (wlan0)
+ * netspeed_tx  transfer network speed  interface name (wlan0)
  * num_filesnumber of files in a directory  path
  *  (/home/foo/Inbox/cur)
  * ram_free free memory in GB   NULL



[hackers] [slstatus] Add network speed functions || Aaron Marcher

2018-05-18 Thread git
commit 0d05b3d13d42f8bebc74bb5f6da7e5e10c0e9933
Author: Aaron Marcher 
AuthorDate: Sat May 19 00:31:33 2018 +0200
Commit: Aaron Marcher 
CommitDate: Sat May 19 00:31:33 2018 +0200

Add network speed functions

diff --git a/Makefile b/Makefile
index b9d7695..0e925cc 100644
--- a/Makefile
+++ b/Makefile
@@ -16,6 +16,7 @@ COM =\
components/kernel_release\
components/keyboard_indicators\
components/load_avg\
+   components/netspeeds\
components/num_files\
components/ram\
components/run_command\
diff --git a/README b/README
index ec6a5a3..c278599 100644
--- a/README
+++ b/README
@@ -19,6 +19,7 @@ Features
 - Kernel version
 - Keyboard indicators
 - Load average
+- Network speeds (RX and TX)
 - Number of files in a directory (hint: Maildir)
 - Memory status (free memory, percentage, total memory and used memory)
 - Swap status (free swap, percentage, total swap and used swap)
diff --git a/components/netspeeds.c b/components/netspeeds.c
new file mode 100644
index 000..ef8bf93
--- /dev/null
+++ b/components/netspeeds.c
@@ -0,0 +1,53 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+
+#include "../util.h"
+
+#if defined(__linux__)
+   const char *
+   netspeed_rx(const char *interface)
+   {
+   static int valid;
+   static unsigned long long rxbytes;
+   unsigned long oldrxbytes;
+   extern const unsigned int interval;
+   char path[PATH_MAX];
+
+   oldrxbytes = rxbytes;
+   snprintf(path, sizeof(path), 
"/sys/class/net/%s/statistics/rx_bytes", interface);
+   if (pscanf(path, "%llu", ) != 1) {
+   return NULL;
+   }
+   if (!valid) {
+   valid = 1;
+   return NULL;
+   }
+
+   return fmt_scaled((rxbytes - oldrxbytes) / interval * 1000);
+   }
+
+   const char *
+   netspeed_tx(const char *interface)
+   {
+   static int valid;
+   static unsigned long long txbytes;
+   unsigned long oldtxbytes;
+   extern const unsigned int interval;
+   char path[PATH_MAX];
+
+   oldtxbytes = txbytes;
+   snprintf(path, sizeof(path), 
"/sys/class/net/%s/statistics/tx_bytes", interface);
+   if (pscanf(path, "%llu", ) != 1) {
+   return NULL;
+   }
+   if (!valid) {
+   valid = 1;
+   return NULL;
+   }
+
+   return fmt_scaled((txbytes - oldtxbytes) / interval * 1000);
+   }
+#elif defined(__OpenBSD__)
+   /* unimplemented */
+#endif
diff --git a/slstatus.h b/slstatus.h
index abe28d3..8bd8bb5 100644
--- a/slstatus.h
+++ b/slstatus.h
@@ -37,6 +37,10 @@ const char *keyboard_indicators(void);
 /* load_avg */
 const char *load_avg(const char *);
 
+/* netspeeds */
+const char *netspeed_rx(const char *);
+const char *netspeed_tx(const char *);
+
 /* num_files */
 const char *num_files(const char *);
 



[hackers] [slstatus] Implement fmt_scaled for ram_* functions || Aaron Marcher

2018-05-18 Thread git
commit d0c68989cd9f42f2b79da3a2ebe9d4765b28bfc1
Author: Aaron Marcher 
AuthorDate: Fri May 18 23:38:59 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 23:38:59 2018 +0200

Implement fmt_scaled for ram_* functions

diff --git a/components/ram.c b/components/ram.c
index f451601..8f96b04 100644
--- a/components/ram.c
+++ b/components/ram.c
@@ -14,7 +14,7 @@
   "MemFree: %ld kB\n"
   "MemAvailable: %ld kB\n",
   , , ) == 3) ?
-  bprintf("%f", (float)free / 1024 / 1024) : NULL;
+  fmt_scaled(free * 1024) : NULL;
}
 
const char *
@@ -39,7 +39,7 @@
long total;
 
return (pscanf("/proc/meminfo", "MemTotal: %ld kB\n", ) 
== 1) ?
-  bprintf("%f", (float)total / 1024 / 1024) : NULL;
+  fmt_scaled(total * 1024) : NULL;
}
 
const char *
@@ -53,9 +53,7 @@
   "MemAvailable: %ld kB\nBuffers: %ld kB\n"
   "Cached: %ld kB\n",
   , , , , ) == 
5) ?
-  bprintf("%f", (float)(total - free - buffers - cached) /
-  1024 / 1024) :
-  NULL;
+  fmt_scaled((total - free - buffers - cached) * 1024) : 
NULL;
}
 #elif defined(__OpenBSD__)
#include 
@@ -81,13 +79,11 @@
ram_free(void)
{
struct uvmexp uvmexp;
-   float free;
int free_pages;
 
if (load_uvmexp()) {
free_pages = uvmexp.npages - uvmexp.active;
-   free = (float)(pagetok(free_pages, uvmexp.pageshift)) / 
1024 / 1024;
-   return bprintf("%f", free);
+   return fmt_scaled(pagetok(free_pages, uvmexp.pageshift) 
* 1024);
}
 
return NULL;
@@ -111,11 +107,9 @@
ram_total(void)
{
struct uvmexp uvmexp;
-   float total;
 
if (load_uvmexp()) {
-   total = (float)(pagetok(uvmexp.npages, 
uvmexp.pageshift)) / 1024 / 1024;
-   return bprintf("%f", total);
+   return fmt_scaled(pagetok(uvmexp.npages, 
uvmexp.pageshift) * 1024);
}
 
return NULL;
@@ -125,11 +119,9 @@
ram_used(void)
{
struct uvmexp uvmexp;
-   float used;
 
if (load_uvmexp()) {
-   used = (float)(pagetok(uvmexp.active, 
uvmexp.pageshift)) / 1024 / 1024;
-   return bprintf("%f", used);
+   return fmt_scaled(pagetok(uvmexp.active, 
uvmexp.pageshift) * 1024);
}
 
return NULL;



[hackers] [slstatus] Fix ram_free for Linux || Aaron Marcher

2018-05-18 Thread git
commit 49d1e5fae2e4957abcf0f1056b3e8df8d695094c
Author: Aaron Marcher 
AuthorDate: Fri May 18 23:32:00 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 23:32:00 2018 +0200

Fix ram_free for Linux

diff --git a/components/ram.c b/components/ram.c
index 57081d2..f451601 100644
--- a/components/ram.c
+++ b/components/ram.c
@@ -9,7 +9,11 @@
{
long free;
 
-   return (pscanf("/proc/meminfo", "MemFree: %ld kB\n", ) == 
1) ?
+   return (pscanf("/proc/meminfo",
+  "MemTotal: %ld kB\n"
+  "MemFree: %ld kB\n"
+  "MemAvailable: %ld kB\n",
+  , , ) == 3) ?
   bprintf("%f", (float)free / 1024 / 1024) : NULL;
}
 



[hackers] [slstatus] Implement fmt_scaled for swap_* functions || Aaron Marcher

2018-05-18 Thread git
commit 39ee31c2425d3ff79f88808cabf44c305d6849ab
Author: Aaron Marcher 
AuthorDate: Fri May 18 23:19:18 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 23:19:18 2018 +0200

Implement fmt_scaled for swap_* functions

diff --git a/components/swap.c b/components/swap.c
index 234e7d1..73d24df 100644
--- a/components/swap.c
+++ b/components/swap.c
@@ -48,7 +48,7 @@
}
sscanf(match, "SwapFree: %ld kB\n", );
 
-   return bprintf("%f", (float)free / 1024 / 1024);
+   return fmt_scaled(free * 1024);
}
 
const char *
@@ -94,7 +94,7 @@
}
sscanf(match, "SwapTotal: %ld kB\n", );
 
-   return bprintf("%f", (float)total / 1024 / 1024);
+   return fmt_scaled(total * 1024);
}
 
const char *
@@ -122,7 +122,7 @@
}
sscanf(match, "SwapFree: %ld kB\n", );
 
-   return bprintf("%f", (float)(total - free - cached) / 1024 / 
1024);
+   return fmt_scaled((total - free - cached) * 1024);
}
 #elif defined(__OpenBSD__)
#include 
@@ -174,7 +174,7 @@
 
getstats(, );
 
-   return bprintf("%f", (float)(total - used) / 1024 / 1024);
+   return fmt_scaled((total - used) * 1024);
}
 
const char *
@@ -194,7 +194,7 @@
 
getstats(, );
 
-   return bprintf("%f", (float)total / 1024 / 1024);
+   return fmt_scaled(total * 1024);
}
 
const char *
@@ -204,6 +204,6 @@
 
getstats(, );
 
-   return bprintf("%f", (float)used / 1024 / 1024);
+   return fmt_scaled(used * 1024);
}
 #endif



[hackers] [slstatus] Implement fmt_scaled for disk_* functions || Aaron Marcher

2018-05-18 Thread git
commit b3e56066ed5349c6fd0afe1ac7cf5035e16a8a2f
Author: Aaron Marcher 
AuthorDate: Fri May 18 23:14:55 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 23:14:55 2018 +0200

Implement fmt_scaled for disk_* functions

diff --git a/components/disk.c b/components/disk.c
index f4031ea..00deea2 100644
--- a/components/disk.c
+++ b/components/disk.c
@@ -16,8 +16,7 @@ disk_free(const char *mnt)
return NULL;
}
 
-   return bprintf("%f",
-  (float)fs.f_frsize * (float)fs.f_bavail / 1024 / 1024 / 
1024);
+   return fmt_scaled(fs.f_frsize * fs.f_bavail);
 }
 
 const char *
@@ -44,8 +43,7 @@ disk_total(const char *mnt)
return NULL;
}
 
-   return bprintf("%f",
-  (float)fs.f_frsize * (float)fs.f_blocks / 1024 / 1024 / 
1024);
+   return fmt_scaled(fs.f_frsize * fs.f_blocks);
 }
 
 const char *
@@ -58,7 +56,5 @@ disk_used(const char *mnt)
return NULL;
}
 
-   return bprintf("%f",
-  (float)fs.f_frsize * ((float)fs.f_blocks -
-  (float)fs.f_bfree) / 1024 / 1024 / 1024);
+   return fmt_scaled(fs.f_frsize * (fs.f_blocks - fs.f_bfree));
 }



[hackers] [slstatus] Add fmt_scaled util function || Aaron Marcher

2018-05-18 Thread git
commit 35219d39caa5ab5e0b1c9e6d4a1cbb29a388842e
Author: Aaron Marcher 
AuthorDate: Fri May 18 23:14:10 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 23:14:10 2018 +0200

Add fmt_scaled util function

diff --git a/util.c b/util.c
index 08f14ff..a7576b4 100644
--- a/util.c
+++ b/util.c
@@ -65,6 +65,22 @@ bprintf(const char *fmt, ...)
return buf;
 }
 
+const char *
+fmt_scaled(size_t bytes)
+{
+   unsigned int i;
+   float scaled;
+   const char *units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
+   "ZiB", "YiB" };
+
+   scaled = bytes;
+   for (i = 0; i < LEN(units) && scaled >= 1024; i++) {
+   scaled /= 1024.0;
+   }
+
+   return bprintf("%.1f%s", scaled, units[i]);
+}
+
 int
 pscanf(const char *path, const char *fmt, ...)
 {
diff --git a/util.h b/util.h
index a73b103..bd05574 100644
--- a/util.h
+++ b/util.h
@@ -9,4 +9,5 @@ void warn(const char *, ...);
 void die(const char *, ...);
 
 const char *bprintf(const char *fmt, ...);
+const char *fmt_scaled(size_t);
 int pscanf(const char *path, const char *fmt, ...);



[hackers] [slstatus] Use %d instead of accidentally %i || Aaron Marcher

2018-05-18 Thread git
commit f3178199848911c8fb51b30e3993ae47120fcc6c
Author: Aaron Marcher 
AuthorDate: Fri May 18 19:58:51 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 19:58:51 2018 +0200

Use %d instead of accidentally %i

diff --git a/components/battery.c b/components/battery.c
index c7c402a..3965e8f 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -16,7 +16,7 @@
 
snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
 bat, "/capacity");
-   return (pscanf(path, "%i", ) == 1) ? bprintf("%d", perc) : 
NULL;
+   return (pscanf(path, "%d", ) == 1) ? bprintf("%d", perc) : 
NULL;
}
 
const char *
@@ -62,12 +62,12 @@
if (!strcmp(state, "Discharging")) {
snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
 bat, "/charge_now");
-   if (pscanf(path, "%i", _now) != 1) {
+   if (pscanf(path, "%d", _now) != 1) {
return NULL;
}
snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
 bat, "/current_now");
-   if (pscanf(path, "%i", _now) != 1) {
+   if (pscanf(path, "%d", _now) != 1) {
return NULL;
}
 
diff --git a/components/cpu.c b/components/cpu.c
index b9b3924..60a7991 100644
--- a/components/cpu.c
+++ b/components/cpu.c
@@ -12,7 +12,7 @@
int freq;
 
return 
(pscanf("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
-  "%i", ) == 1) ?
+  "%d", ) == 1) ?
   bprintf("%d", (freq + 500) / 1000) : NULL;
}
 



[hackers] [slstatus] Port battery_remaining to Linux || Aaron Marcher

2018-05-18 Thread git
commit f170028527bdccead9a99797b45011d5e76100f1
Author: Aaron Marcher 
AuthorDate: Fri May 18 17:25:09 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 17:25:09 2018 +0200

Port battery_remaining to Linux

Additionally unify the format of battery_state and uptime

diff --git a/README b/README
index 4cb8ca4..ec6a5a3 100644
--- a/README
+++ b/README
@@ -6,7 +6,7 @@ slstatus is a suckless status monitor for window managers that 
use WM_NAME
 
 Features
 
-- Battery percentage/state
+- Battery percentage/state/time left
 - CPU usage
 - CPU frequency
 - Custom shell commands
diff --git a/components/battery.c b/components/battery.c
index b05c171..c7c402a 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -49,8 +49,36 @@
const char *
battery_remaining(const char *bat)
{
-   /* TODO: Implement */
-   return NULL;
+   int charge_now, current_now, m, h;
+   float timeleft;
+   char path[PATH_MAX], state[12];
+
+   snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
+bat, "/status");
+   if (pscanf(path, "%12s", state) != 1) {
+   return NULL;
+   }
+
+   if (!strcmp(state, "Discharging")) {
+   snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
+bat, "/charge_now");
+   if (pscanf(path, "%i", _now) != 1) {
+   return NULL;
+   }
+   snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
+bat, "/current_now");
+   if (pscanf(path, "%i", _now) != 1) {
+   return NULL;
+   }
+
+   timeleft = (float)charge_now / (float)current_now;
+   h = timeleft;
+   m = (timeleft - (float)h) * 60;
+
+   return bprintf("%dh %dm", h, m);
+   }
+
+   return "";
}
 #elif defined(__OpenBSD__)
#include 
@@ -122,7 +150,7 @@
 
if (load_apm_power_info(_info)) {
if (apm_info.ac_state != APM_AC_ON) {
-   return bprintf("%u:%02u", apm_info.minutes_left 
/ 60,
+   return bprintf("%uh %02um", 
apm_info.minutes_left / 60,
   apm_info.minutes_left % 60);
} else {
return "";



[hackers] [slstatus] Fix memory leak || Aaron Marcher

2018-05-18 Thread git
commit b04ca3ef45decced916dd43370d43a10f21e2991
Author: Aaron Marcher 
AuthorDate: Fri May 18 16:55:37 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 16:55:37 2018 +0200

Fix memory leak

diff --git a/components/battery.c b/components/battery.c
index 84b2c11..b05c171 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -125,7 +125,7 @@
return bprintf("%u:%02u", apm_info.minutes_left 
/ 60,
   apm_info.minutes_left % 60);
} else {
-   return strdup("");
+   return "";
}
}
 



[hackers] [slstatus] Fix memory leak || Aaron Marcher

2018-05-18 Thread git
commit 9be2be7197d9ba51abaeb98675fc81676a72b1df
Author: Aaron Marcher 
AuthorDate: Fri May 18 16:52:10 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 16:52:10 2018 +0200

Fix memory leak

diff --git a/components/battery.c b/components/battery.c
index 84b2c11..b0cd97b 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -49,8 +49,32 @@
const char *
battery_remaining(const char *bat)
{
-   /* TODO: Implement */
-   return NULL;
+   int charge_now, current_now;
+   char path[PATH_MAX], state[12];
+
+   snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
+bat, "/status");
+   if (pscanf(path, "%12s", state) != 1) {
+   return NULL;
+   }
+
+   if (!strcmp(state, "Discharging")) {
+   snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
+bat, "/charge_now");
+   if (pscanf(path, "%i", charge_now) != 1) {
+   return NULL;
+   }
+   snprintf(path, sizeof(path), "%s%s%s", 
"/sys/class/power_supply/",
+bat, "/current_now");
+   if (pscanf(path, "%i", current_now) != 1) {
+   return NULL;
+   }
+
+   return bprintf("%d")
+   }
+
+   return (pscanf(path, "%i", ) == 1) ? bprintf("%d", perc) : 
NULL;
+
}
 #elif defined(__OpenBSD__)
#include 
@@ -125,7 +149,7 @@
return bprintf("%u:%02u", apm_info.minutes_left 
/ 60,
   apm_info.minutes_left % 60);
} else {
-   return strdup("");
+   return "";
}
}
 



[hackers] [slstatus] battery: fixed remaining time on connected AC || Tobias Tschinkowitz

2018-05-18 Thread git
commit 367f8a8c44fc9dc05a0f7a2c1500111ca97f669c
Author: Tobias Tschinkowitz 
AuthorDate: Fri May 18 16:33:51 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 16:35:32 2018 +0200

battery: fixed remaining time on connected AC

when an AC is connected apm_info shows a non-valid value for remaining
minutes. it was decided that in that case the function should return an
empty string.

diff --git a/components/battery.c b/components/battery.c
index 53d94b5..84b2c11 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -121,8 +121,12 @@
struct apm_power_info apm_info;
 
if (load_apm_power_info(_info)) {
-   return bprintf("%u:%02u", apm_info.minutes_left / 60,
-  apm_info.minutes_left % 60);
+   if (apm_info.ac_state != APM_AC_ON) {
+   return bprintf("%u:%02u", apm_info.minutes_left 
/ 60,
+  apm_info.minutes_left % 60);
+   } else {
+   return strdup("");
+   }
}
 
return NULL;



[hackers] [slstat_new][PATCH] battery: fixed remaining time on connected AC

2018-05-18 Thread Tobias Tschinkowitz
when an AC is connected apm_info shows a non-valid value for remaining
minutes. it was decided that in that case the function should return an
empty string.

---
 components/battery.c | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/components/battery.c b/components/battery.c
index 53d94b5..84b2c11 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -121,8 +121,12 @@
struct apm_power_info apm_info;
 
if (load_apm_power_info(_info)) {
-   return bprintf("%u:%02u", apm_info.minutes_left / 60,
-  apm_info.minutes_left % 60);
+   if (apm_info.ac_state != APM_AC_ON) {
+   return bprintf("%u:%02u", apm_info.minutes_left 
/ 60,
+  apm_info.minutes_left % 60);
+   } else {
+   return strdup("");
+   }
}
 
return NULL;
-- 
2.16.2




Re: [hackers] [slstatus][PATCH] added battery_remaining function

2018-05-18 Thread Aaron Marcher

Tobias,


What do you think?


Thank you very much for your patch. Good work! Applied it. I will work 
now on the Linux function.
Also thank you for moving the common code to load_apm_power_info(). It 
is a lot simpler now. I hope it is OK for you if I modified your commit 
message to explain that too, as the patch by itself is not that 
readbale.


Regards,
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][PATCH] added battery_remaining function

2018-05-18 Thread Tobias Tschinkowitz
implementation of a battery_remaining function which returns the
remaining battery time in HH:MM format. linux function still needs
implementation.

---
Hi!

I added a function which shows the remaining battery time. As i dont
have a linux machine at the moment i would like to leave the
implementation for this function to someone else.

What do you think?

Greets,
Tobias

 components/battery.c | 70 +---
 config.def.h |  2 ++
 slstatus.h   |  1 +
 3 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/components/battery.c b/components/battery.c
index 807a7e6..152777e 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -45,40 +45,56 @@
}
return (i == LEN(map)) ? "?" : map[i].symbol;
}
+
+   const char *
+   battery_remaining(const char *bat)
+   {
+   /* TODO: Implement */
+   return NULL;
+   }
 #elif defined(__OpenBSD__)
#include 
#include 
#include 
#include 
 
-   const char *
-   battery_perc(const char *unused)
+   static int
+   load_apm_power_info(struct apm_power_info *apm_info)
{
-   struct apm_power_info apm_info;
int fd;
 
fd = open("/dev/apm", O_RDONLY);
if (fd < 0) {
warn("open '/dev/apm':");
-   return NULL;
+   return 0;
}
 
-   if (ioctl(fd, APM_IOC_GETPOWER, _info) < 0) {
+   memset(apm_info, 0, sizeof(struct apm_power_info));
+   if (ioctl(fd, APM_IOC_GETPOWER, apm_info) < 0) {
warn("ioctl 'APM_IOC_GETPOWER':");
close(fd);
-   return NULL;
+   return 0;
+   }
+   return close(fd), 1;
+   }
+
+   const char *
+   battery_perc(const char *unused)
+   {
+   struct apm_power_info apm_info;
+
+   if (load_apm_power_info(_info)) {
+   return bprintf("%d", apm_info.battery_life);
}
-   close(fd);
 
-   return bprintf("%d", apm_info.battery_life);
+   return NULL;
}
 
const char *
battery_state(const char *unused)
{
-   int fd;
-   size_t i;
struct apm_power_info apm_info;
+   size_t i;
struct {
unsigned int state;
char *symbol;
@@ -87,24 +103,28 @@
{ APM_AC_OFF, "-" },
};
 
-   fd = open("/dev/apm", O_RDONLY);
-   if (fd < 0) {
-   warn("open '/dev/apm':");
-   return NULL;
+   if (load_apm_power_info(_info)) {
+   for (i = 0; i < LEN(map); i++) {
+   if (map[i].state == apm_info.ac_state) {
+   break;
+   }
+   }
+   return (i == LEN(map)) ? "?" : map[i].symbol;
}
 
-   if (ioctl(fd, APM_IOC_GETPOWER, _info) < 0) {
-   warn("ioctl 'APM_IOC_GETPOWER':");
-   close(fd);
-   return NULL;
-   }
-   close(fd);
+   return NULL;
+   }
 
-   for (i = 0; i < LEN(map); i++) {
-   if (map[i].state == apm_info.ac_state) {
-   break;
-   }
+   const char *
+   battery_remaining(const char *unused)
+   {
+   struct apm_power_info apm_info;
+
+   if (load_apm_power_info(_info)) {
+   return bprintf("%u:%02u", apm_info.minutes_left / 60,
+  apm_info.minutes_left % 60);
}
-   return (i == LEN(map)) ? "?" : map[i].symbol;
+
+   return NULL;
}
 #endif
diff --git a/config.def.h b/config.def.h
index 9dcd5d6..82a5df5 100644
--- a/config.def.h
+++ b/config.def.h
@@ -16,6 +16,8 @@ static const char unknown_str[] = "n/a";
  *  NULL on OpenBSD
  * battery_statebattery charging state  battery name (BAT0)
  *  NULL on OpenBSD
+ * battery_remaining   battery remaining HH:MM battery name (BAT0)
+ * NULL on OpenBSD
  * cpu_perc cpu usage in percentNULL
  * cpu_freq cpu frequency in MHzNULL
  * datetime date and time   format string (%F %T)
diff --git a/slstatus.h b/slstatus.h
index 6a25209..abe28d3 100644
--- 

[hackers] [slstatus] Audit slstatus.c || Laslo Hunhold

2018-05-18 Thread git
commit a4fe8c97414f07dd8b891e0d325dd2733195151d
Author: Laslo Hunhold 
AuthorDate: Fri May 18 10:07:50 2018 +0200
Commit: Aaron Marcher 
CommitDate: Fri May 18 10:08:03 2018 +0200

Audit slstatus.c

 1) Remove setlocale() (locales are harmful and any 'issues' shall
be fixed in different ways that are expected).
 2) Disable buffering on stdout with setbuf() rather than flushing
it each time.
 3) Make error messages more consistent.
 4) Add error checks where applicable.
 5) Make code a bit more readable where res is assigned.
 6) Use XFlush() rather than XSync() (we don't need to wait for the
XServer to react, which could lead to long hangs on our side).

diff --git a/slstatus.c b/slstatus.c
index 35bdcd6..7ff323f 100644
--- a/slstatus.c
+++ b/slstatus.c
@@ -1,6 +1,5 @@
 /* See LICENSE file for copyright and license details. */
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -56,6 +55,7 @@ main(int argc, char *argv[])
size_t i, len;
int sflag, ret;
char status[MAXLEN];
+   const char *res;
 
sflag = 0;
ARGBEGIN {
@@ -70,25 +70,31 @@ main(int argc, char *argv[])
usage();
}
 
-   setlocale(LC_ALL, "");
-
memset(, 0, sizeof(act));
act.sa_handler = terminate;
sigaction(SIGINT,  , NULL);
sigaction(SIGTERM, , NULL);
 
+   if (sflag) {
+   setbuf(stdout, NULL);
+   }
+
if (!sflag && !(dpy = XOpenDisplay(NULL))) {
-   fprintf(stderr, "Cannot open display");
+   fprintf(stderr, "XOpenDisplay: Failed to open display\n");
return 1;
}
 
while (!done) {
-   clock_gettime(CLOCK_MONOTONIC, );
+   if (clock_gettime(CLOCK_MONOTONIC, ) < 0) {
+   fprintf(stderr, "clock_gettime: %s\n", strerror(errno));
+   return 1;
+   }
 
status[0] = '\0';
for (i = len = 0; i < LEN(args); i++) {
-   const char * res = args[i].func(args[i].args);
-   res = (res == NULL) ? unknown_str : res;
+   if (!(res = args[i].func(args[i].args))) {
+   res = unknown_str;
+   }
if ((ret = snprintf(status + len, sizeof(status) - len,
args[i].fmt, res)) < 0) {
fprintf(stderr, "snprintf: %s\n",
@@ -103,14 +109,21 @@ main(int argc, char *argv[])
 
if (sflag) {
printf("%s\n", status);
-   fflush(stdout);
} else {
-   XStoreName(dpy, DefaultRootWindow(dpy), status);
-   XSync(dpy, False);
+   if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 
0) {
+   fprintf(stderr,
+   "XStoreName: Allocation failed\n");
+   return 1;
+   }
+   XFlush(dpy);
}
 
if (!done) {
-   clock_gettime(CLOCK_MONOTONIC, );
+   if (clock_gettime(CLOCK_MONOTONIC, ) < 0) {
+   fprintf(stderr, "clock_gettime: %s\n",
+   strerror(errno));
+   return 1;
+   }
difftimespec(, , );
 
intspec.tv_sec = interval / 1000;
@@ -118,14 +131,23 @@ main(int argc, char *argv[])
difftimespec(, , );
 
if (wait.tv_sec >= 0) {
-   nanosleep(, NULL);
+   if (nanosleep(, NULL) < 0 &&
+   errno != EINTR) {
+   fprintf(stderr, "nanosleep: %s\n",
+   strerror(errno));
+   return 1;
+   }
}
}
}
 
if (!sflag) {
XStoreName(dpy, DefaultRootWindow(dpy), NULL);
-   XCloseDisplay(dpy);
+   if (XCloseDisplay(dpy) < 0) {
+   fprintf(stderr,
+   "XCloseDisplay: Failed to close display\n");
+   return 1;
+   }
}
 
return 0;



Re: [hackers] [slstatus] [PATCH V-2] Audit slstatus.c

2018-05-18 Thread Laslo Hunhold
On Fri, 18 May 2018 00:09:59 +0200
Laslo Hunhold  wrote:

> 

This is an updated version, as I forgot to remove locale.h and wanted
to explain the XFlush()-change a bit more.

With best regards

Laslo Hunhold

-- 
Laslo Hunhold 
>From 3055e0270a2a32da86551beaa1cea092a3d2a4bb Mon Sep 17 00:00:00 2001
From: Laslo Hunhold 
Date: Fri, 18 May 2018 00:04:02 +0200
Subject: [PATCH] Audit slstatus.c

 1) Remove setlocale() (locales are harmful and any 'issues' shall
be fixed in different ways that are expected).
 2) Disable buffering on stdout with setbuf() rather than flushing
it each time.
 3) Make error messages more consistent.
 4) Add error checks where applicable.
 5) Make code a bit more readable where res is assigned.
 6) Use XFlush() rather than XSync() (we don't need to wait for the
XServer to react, which could lead to long hangs on our side).
---
 slstatus.c | 48 +++-
 1 file changed, 35 insertions(+), 13 deletions(-)

diff --git a/slstatus.c b/slstatus.c
index 35bdcd6..7ff323f 100644
--- a/slstatus.c
+++ b/slstatus.c
@@ -1,6 +1,5 @@
 /* See LICENSE file for copyright and license details. */
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -56,6 +55,7 @@ main(int argc, char *argv[])
 	size_t i, len;
 	int sflag, ret;
 	char status[MAXLEN];
+	const char *res;
 
 	sflag = 0;
 	ARGBEGIN {
@@ -70,25 +70,31 @@ main(int argc, char *argv[])
 		usage();
 	}
 
-	setlocale(LC_ALL, "");
-
 	memset(, 0, sizeof(act));
 	act.sa_handler = terminate;
 	sigaction(SIGINT,  , NULL);
 	sigaction(SIGTERM, , NULL);
 
+	if (sflag) {
+		setbuf(stdout, NULL);
+	}
+
 	if (!sflag && !(dpy = XOpenDisplay(NULL))) {
-		fprintf(stderr, "Cannot open display");
+		fprintf(stderr, "XOpenDisplay: Failed to open display\n");
 		return 1;
 	}
 
 	while (!done) {
-		clock_gettime(CLOCK_MONOTONIC, );
+		if (clock_gettime(CLOCK_MONOTONIC, ) < 0) {
+			fprintf(stderr, "clock_gettime: %s\n", strerror(errno));
+			return 1;
+		}
 
 		status[0] = '\0';
 		for (i = len = 0; i < LEN(args); i++) {
-			const char * res = args[i].func(args[i].args);
-			res = (res == NULL) ? unknown_str : res;
+			if (!(res = args[i].func(args[i].args))) {
+res = unknown_str;
+			}
 			if ((ret = snprintf(status + len, sizeof(status) - len,
 			args[i].fmt, res)) < 0) {
 fprintf(stderr, "snprintf: %s\n",
@@ -103,14 +109,21 @@ main(int argc, char *argv[])
 
 		if (sflag) {
 			printf("%s\n", status);
-			fflush(stdout);
 		} else {
-			XStoreName(dpy, DefaultRootWindow(dpy), status);
-			XSync(dpy, False);
+			if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) {
+fprintf(stderr,
+"XStoreName: Allocation failed\n");
+return 1;
+			}
+			XFlush(dpy);
 		}
 
 		if (!done) {
-			clock_gettime(CLOCK_MONOTONIC, );
+			if (clock_gettime(CLOCK_MONOTONIC, ) < 0) {
+fprintf(stderr, "clock_gettime: %s\n",
+strerror(errno));
+return 1;
+			}
 			difftimespec(, , );
 
 			intspec.tv_sec = interval / 1000;
@@ -118,14 +131,23 @@ main(int argc, char *argv[])
 			difftimespec(, , );
 
 			if (wait.tv_sec >= 0) {
-nanosleep(, NULL);
+if (nanosleep(, NULL) < 0 &&
+errno != EINTR) {
+	fprintf(stderr, "nanosleep: %s\n",
+	strerror(errno));
+	return 1;
+}
 			}
 		}
 	}
 
 	if (!sflag) {
 		XStoreName(dpy, DefaultRootWindow(dpy), NULL);
-		XCloseDisplay(dpy);
+		if (XCloseDisplay(dpy) < 0) {
+			fprintf(stderr,
+			"XCloseDisplay: Failed to close display\n");
+			return 1;
+		}
 	}
 
 	return 0;
-- 
2.17.0