[hackers] [slstatus] battery_perc: Port to OpenBSD || Aaron Marcher

2018-05-01 Thread git
commit 41deabf2ec1ad5f10851421cfbaf17023e7de05f
Author: Aaron Marcher 
AuthorDate: Tue May 1 20:45:29 2018 +0200
Commit: Aaron Marcher 
CommitDate: Tue May 1 20:45:29 2018 +0200

battery_perc: Port to OpenBSD

diff --git a/components/battery.c b/components/battery.c
index 04dfa47..327d576 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -88,4 +88,41 @@
 
return bprintf("%d", apm_info.battery_life);
}
+
+   const char *
+   battery_state(const char *bat)
+   {
+   int fd;
+   size_t i;
+   struct apm_power_info apm_info;
+   struct {
+   unsigned int state;
+   char *symbol;
+   } map[] = {
+   { APM_AC_ON,  "+" },
+   { APM_AC_OFF, "-" },
+   { APM_AC_UNKNOWN, "/" },
+   };
+
+   fd = open("/dev/apm", O_RDONLY);
+   if (fd < 0) {
+   fprintf(stderr, "open '/dev/apm': %s\n", 
strerror(errno));
+   return NULL;
+   }
+
+   if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
+   fprintf(stderr, "ioctl 'APM_IOC_GETPOWER': %s\n",
+   strerror(errno));
+   close(fd);
+   return NULL;
+   }
+   close(fd);
+
+   for (i = 0; i < LEN(map); i++) {
+   if (map[i].state == apm_info.ac_state) {
+   break;
+   }
+   }
+   return (i == LEN(map)) ? "?" : map[i].symbol;
+   }
 #endif



Re: [hackers] [slstatus] battery_perc: Port to OpenBSD. || Aaron Marcher

2018-03-20 Thread Quentin Rameau
> +#elif __OpenBSD__
> +#elif __OpenBSD__

Also, for more robustness you should use

#elif defined(__OpenBSD__)

You shouldn't test its evaluated expanded value, but only if it is
defined or not.

(then I'd replace matching #ifdef with #if defined(), but I guess
that's more a matter of taste)



Re: [hackers] [slstatus] battery_perc: Port to OpenBSD. || Aaron Marcher

2018-03-20 Thread Quentin Rameau
> Hello,
> 
> > If you're going to do this for every module for every OS you're
> > planning on supporting, this is a bad start imho.  
> 
> Well, i will only support Linux and OpenBSD - and almost half of the 
> modules is POSIX-compliant anyway.
> 
> > What would you think about rather separating objects to their OS
> > directory and chose which to build/link at make time?  
> 
> Will adding an additional separation layer or even directory
> structure with duplicate code actually improve the readbility of the
> program? Performance-wise it is the same.

Yes.

Now, if you only have a couple of those, maybe it's not worthwhile
indeed. Just keep this in mind if/when you reach a point where there
are too much of those.



Re: [hackers] [slstatus] battery_perc: Port to OpenBSD. || Aaron Marcher

2018-03-19 Thread Aaron Marcher

Hello,


If you're going to do this for every module for every OS you're
planning on supporting, this is a bad start imho.


Well, i will only support Linux and OpenBSD - and almost half of the 
modules is POSIX-compliant anyway.



What would you think about rather separating objects to their OS
directory and chose which to build/link at make time?


Will adding an additional separation layer or even directory structure 
with duplicate code actually improve the readbility of the program? 
Performance-wise it is the same.


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



Re: [hackers] [slstatus] battery_perc: Port to OpenBSD. || Aaron Marcher

2018-03-19 Thread Quentin Rameau
Hello Aaron,

> battery_perc: Port to OpenBSD.

> +#elif __OpenBSD__
> +#ifdef __linux__
> +#elif __OpenBSD__
> +#endif
> +#ifdef __linux__

If you're going to do this for every module for every OS you're
planning on supporting, this is a bad start imho.

What would you think about rather separating objects to their OS
directory and chose which to build/link at make time?




[hackers] [slstatus] battery_perc: Port to OpenBSD. || Aaron Marcher

2018-03-19 Thread git
commit 7e3f80c1a39e16973f3c9235d3f9d85df0d1b995
Author: Aaron Marcher 
AuthorDate: Mon Mar 19 18:44:52 2018 +0100
Commit: Aaron Marcher 
CommitDate: Mon Mar 19 18:46:52 2018 +0100

battery_perc: Port to OpenBSD.

In OpenBSD battery percentage gets fetched using apm now.

diff --git a/README b/README
index f14a3b8..ddd295b 100644
--- a/README
+++ b/README
@@ -69,6 +69,6 @@ The following functions are not portable at the moment:
 - cpu_{freq,perc,iowait}
 - entropy
 - swap_{free,perc,total,used}
-- battery_{perc,power,state}
+- battery_{power,state}
 - temp
 - vol_perc
diff --git a/components/battery.c b/components/battery.c
index 52ad343..4314e81 100644
--- a/components/battery.c
+++ b/components/battery.c
@@ -1,22 +1,50 @@
 /* See LICENSE file for copyright and license details. */
+#include 
+#include 
 #ifdef __linux__
 #include 
-#include 
 #include 
+#elif __OpenBSD__
+#include 
+#include 
+#include 
+#include 
+#endif
 
 #include "../util.h"
 
 const char *
 battery_perc(const char *bat)
 {
+#ifdef __linux__
int perc;
char path[PATH_MAX];
 
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, 
"/capacity");
return (pscanf(path, "%i", &perc) == 1) ?
   bprintf("%d", perc) : NULL;
+#elif __OpenBSD__
+   struct apm_power_info apm_info;
+   int fd;
+
+   fd = open("/dev/apm", O_RDONLY);
+   if (fd < 0) {
+   warn("Failed to open file /dev/apm");
+   return NULL;
+   }
+
+   if (ioctl(fd, APM_IOC_GETPOWER, &apm_info) < 0) {
+   warn("Failed to get battery info");
+   close(fd);
+   return NULL;
+   }
+   close(fd);
+
+   return bprintf("%d", apm_info.battery_life);
+#endif
 }
 
+#ifdef __linux__
 const char *
 battery_power(const char *bat)
 {