[hackers] [slstatus][PATCH] wifi: fixed disconnected wifi status on openbsd

2018-05-31 Thread Tobias Tschinkowitz
>From be0734685e9b5e0da906d06f08b027024c549c2f Mon Sep 17 00:00:00 2001
From: Tobias Tschinkowitz 
Date: Thu, 31 May 2018 11:53:28 +0200
Subject: [PATCH] wifi: fixed disconnected wifi status on openbsd

---
Hi!

if wifi is disconnected we cannot retrieve its ssid and signal strength.
So i added a check for empty bssid and we return 0 in that case.

Greetings,
Tobias

 components/wifi.c | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/components/wifi.c b/components/wifi.c
index 7815c86..3751ae3 100644
--- a/components/wifi.c
+++ b/components/wifi.c
@@ -109,6 +109,7 @@
{
struct ieee80211_bssid bssid;
int sockfd;
+   uint8_t zero_bssid[IEEE80211_ADDR_LEN];
 
memset(, 0, sizeof(bssid));
memset(nr, 0, sizeof(struct ieee80211_nodereq));
@@ -122,6 +123,12 @@
close(sockfd);
return 0;
}
+   memset(_bssid, 0, sizeof(zero_bssid));
+   if (memcmp(bssid.i_bssid, zero_bssid,
+   IEEE80211_ADDR_LEN) == 0) {
+   close(sockfd);
+   return 0;
+   }
strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
memcpy(>nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
-- 
2.16.2




Re: [hackers] [slstatus][PATCH] backlight: implemented openbsd support

2018-05-24 Thread Tobias Tschinkowitz
On Thu, May 24, 2018 at 02:55:28PM +0200, Aaron Marcher wrote:
> Hi,
> 
> > i have implemented basic support for the backlight function on OpenBSD.
> > The problem here is that /dev/ttyC0 permission is 600 (root:wheel) so a
> > user cannot read from the device without changing the permission or
> > running slstatus as root.
> 
> Running slstatus as root is no real option - however there are not that much
> alternatives to reading from /dev/ttyC0. Linking against xcb-xrandr is
> bloated and appearently doesn't work for every setup.
> A possible workaround would be to set the group permissions of /dev/ttyC0 in
> /etc/rc.local or crontab automatically - which is not perfect.
> The question that comes up to my mind is: Do we need the backlight
> component? I can literally "see" the current brightness of my screen.  Tell
> me what you think and we will come up with a sane solution.
> 
> 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
> 

Hi,

in my opinion, if nobody else comes up with a clever idea how to get the
brightness on openbsd we should just drop that component.
As you already said, the brightness can be seen obviously.

Greetings,
Tobias



[hackers] [slstatus][PATCH] ram: fixed int overflow on pagetok macro

2018-05-24 Thread Tobias Tschinkowitz
---
 components/ram.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/components/ram.c b/components/ram.c
index 0ac9753..0333b3b 100644
--- a/components/ram.c
+++ b/components/ram.c
@@ -75,7 +75,7 @@
#include 
 
#define LOG1024 10
-   #define pagetok(size, pageshift) ((size) << (pageshift - LOG1024))
+   #define pagetok(size, pageshift) (size_t)(size << (pageshift - LOG1024))
 
inline int
load_uvmexp(struct uvmexp *uvmexp)
-- 
2.16.2




Re: [hackers] [slstatus][PATCH] backlight: implemented openbsd support

2018-05-23 Thread Tobias Tschinkowitz
On Wed, May 23, 2018 at 04:30:41PM -0300, lucas wrote:
Lucas,
> Tobias,
> 
> > See my notes below the commit message of the patch.
> 
> I saw the note just after sending the email. My bad. Still, I think that
> slstatus shouldn't be run as root. It would be nice to find a workaround
> when launching X from xenodm.
> 

Absolutely, this is why i just noted that this is a proposal.
I hoped to get the discussion started on how we could work this out
(seems like it worked..)

> > Updated diff below.
> > 
> > diff --git a/components/backlight.c b/components/backlight.c
> > index 21e06a1..4234164 100644
> > --- a/components/backlight.c
> > +++ b/components/backlight.c
> > @@ -39,7 +39,7 @@
> > const char *
> > backlight_perc(const char *unused)
> > {
> > -   int fd, err;
> > +   int fd;
> > struct wsdisplay_param wsd_param = {
> > .param = WSDISPLAYIO_PARAM_BRIGHTNESS
> > };
> > @@ -48,11 +48,12 @@
> > warn("could not open /dev/ttyC0");
> > return NULL;
> > }
> > -   if ((err = ioctl(fd, WSDISPLAYIO_GETPARAM, _param)) < 0) {
> > +   if ((ioctl(fd, WSDISPLAYIO_GETPARAM, _param)) < 0) {
> 
> Haven't taken a look at the rest of the code, but are the extra () used
> everywhere?
> 

Nope, just blindly removed the err and forgot about the (). Removed
them, diff below..

> > warn("ioctl 'WSDISPLAYIO_GETPARAM' failed");
> > return NULL;
> > }
> > -   return bprintf("%d", wsd_param.curval * 100 / wsd_param.max);
> > +   return bprintf("%d", wsd_param.curval * 100 /
> > +  (wsd_param.max - wsd_param.min));
> 
> Better, but .curval is still between .min and .max, so you should substract
> .min from it to get the right percentage.
> 

Fixed in the diff below. Thanks again.

> > }
> >  
> >  #endif
> > 
> 

Greetings,
Tobias

diff --git a/components/backlight.c b/components/backlight.c
index 21e06a1..cc13aed 100644
--- a/components/backlight.c
+++ b/components/backlight.c
@@ -39,7 +39,7 @@
const char *
backlight_perc(const char *unused)
{
-   int fd, err;
+   int fd;
struct wsdisplay_param wsd_param = {
.param = WSDISPLAYIO_PARAM_BRIGHTNESS
};
@@ -48,11 +48,12 @@
warn("could not open /dev/ttyC0");
return NULL;
}
-   if ((err = ioctl(fd, WSDISPLAYIO_GETPARAM, _param)) < 0) {
+   if (ioctl(fd, WSDISPLAYIO_GETPARAM, _param) < 0) {
warn("ioctl 'WSDISPLAYIO_GETPARAM' failed");
return NULL;
}
-   return bprintf("%d", wsd_param.curval * 100 / wsd_param.max);
+   return bprintf("%d", (wsd_param.curval - wsd_param.min) * 100 /
+  (wsd_param.max - wsd_param.min));
}
 
 #endif



Re: [hackers] [slstatus][PATCH] backlight: implemented openbsd support

2018-05-23 Thread Tobias Tschinkowitz
On Wed, May 23, 2018 at 03:57:37PM -0300, lucas wrote:
> Hi Tobias,
> 
> Two notes below
> 
> > diff --git a/components/backlight.c b/components/backlight.c
> > index f9c4096..21e06a1 100644
> > --- a/components/backlight.c
> > +++ b/components/backlight.c
> > @@ -29,4 +29,30 @@
> >  
> > return bprintf("%d", cur * 100 / max);
> > }
> > +
> > +#elif defined(__OpenBSD__)
> > +   #include 
> > +   #include 
> > +   #include 
> > +   #include 
> > +
> > +   const char *
> > +   backlight_perc(const char *unused)
> > +   {
> > +   int fd, err;
> > +   struct wsdisplay_param wsd_param = {
> > +   .param = WSDISPLAYIO_PARAM_BRIGHTNESS
> > +   };
> > +
> > +   if ((fd = open("/dev/ttyC0", O_RDONLY)) < 0) {
> 
> This only works if you run startx from tty. If not, /dev/ttyC0 would be 0600
> owned by root.
> 

See my notes below the commit message of the patch.

> > +   warn("could not open /dev/ttyC0");
> > +   return NULL;
> > +   }
> > +   if ((err = ioctl(fd, WSDISPLAYIO_GETPARAM, _param)) < 0) {
> 
> Why storing the return value in err if it isn't used?
> 

Bad habit, removed it, see diff below.

> > +   warn("ioctl 'WSDISPLAYIO_GETPARAM' failed");
> > +   return NULL;
> > +   }
> > +   return bprintf("%d", wsd_param.curval * 100 / wsd_param.max);
> 
> wsd_param also includes a .min field. It should be properly scaled instead
> of just dividing by .max
> 

Fixed that, see diff below.

> > +   }
> > +
> >  #endif
> > diff --git a/config.def.h b/config.def.h
> > index 75debe5..3a0f838 100644
> > --- a/config.def.h
> > +++ b/config.def.h
> > @@ -14,6 +14,7 @@ static const char unknown_str[] = "n/a";
> >   *
> >   * backlight_perc   backlight percentagedevice name
> >   *  (intel_backlight)
> > + *  NULL on OpenBSD
> >   * battery_perc battery percentage  battery name (BAT0)
> >   *  NULL on OpenBSD
> >   * battery_statebattery charging state  battery name (BAT0)
> > -- 
> > 2.16.2
> > 
> > 
> 
> Cheers.
> 

Thanks for the notes lucas!
Updated diff below.

diff --git a/components/backlight.c b/components/backlight.c
index 21e06a1..4234164 100644
--- a/components/backlight.c
+++ b/components/backlight.c
@@ -39,7 +39,7 @@
const char *
backlight_perc(const char *unused)
{
-   int fd, err;
+   int fd;
struct wsdisplay_param wsd_param = {
.param = WSDISPLAYIO_PARAM_BRIGHTNESS
};
@@ -48,11 +48,12 @@
warn("could not open /dev/ttyC0");
return NULL;
}
-   if ((err = ioctl(fd, WSDISPLAYIO_GETPARAM, _param)) < 0) {
+   if ((ioctl(fd, WSDISPLAYIO_GETPARAM, _param)) < 0) {
warn("ioctl 'WSDISPLAYIO_GETPARAM' failed");
return NULL;
}
-   return bprintf("%d", wsd_param.curval * 100 / wsd_param.max);
+   return bprintf("%d", wsd_param.curval * 100 /
+  (wsd_param.max - wsd_param.min));
}
 
 #endif



[hackers] [slstatus][PATCH] backlight: implemented openbsd support

2018-05-23 Thread Tobias Tschinkowitz
implemented openbsd support for the backlight function.
this only works when running slstatus as superuser.

---
Hi Guys,

i have implemented basic support for the backlight function on OpenBSD.
The problem here is that /dev/ttyC0 permission is 600 (root:wheel) so a
user cannot read from the device without changing the permission or
running slstatus as root.

Anyway, this is just an proposal. Hopefully there is 
another solution to solve this. I know that we could also do this by
linking against xcb-xrandr (like xbacklight does) but i think we should
not do this for now.

Greetings,
Tobias

 components/backlight.c | 26 ++
 config.def.h   |  1 +
 2 files changed, 27 insertions(+)

diff --git a/components/backlight.c b/components/backlight.c
index f9c4096..21e06a1 100644
--- a/components/backlight.c
+++ b/components/backlight.c
@@ -29,4 +29,30 @@
 
return bprintf("%d", cur * 100 / max);
}
+
+#elif defined(__OpenBSD__)
+   #include 
+   #include 
+   #include 
+   #include 
+
+   const char *
+   backlight_perc(const char *unused)
+   {
+   int fd, err;
+   struct wsdisplay_param wsd_param = {
+   .param = WSDISPLAYIO_PARAM_BRIGHTNESS
+   };
+
+   if ((fd = open("/dev/ttyC0", O_RDONLY)) < 0) {
+   warn("could not open /dev/ttyC0");
+   return NULL;
+   }
+   if ((err = ioctl(fd, WSDISPLAYIO_GETPARAM, _param)) < 0) {
+   warn("ioctl 'WSDISPLAYIO_GETPARAM' failed");
+   return NULL;
+   }
+   return bprintf("%d", wsd_param.curval * 100 / wsd_param.max);
+   }
+
 #endif
diff --git a/config.def.h b/config.def.h
index 75debe5..3a0f838 100644
--- a/config.def.h
+++ b/config.def.h
@@ -14,6 +14,7 @@ static const char unknown_str[] = "n/a";
  *
  * backlight_perc   backlight percentagedevice name
  *  (intel_backlight)
+ *  NULL on OpenBSD
  * battery_perc battery percentage  battery name (BAT0)
  *  NULL on OpenBSD
  * battery_statebattery charging state  battery name (BAT0)
-- 
2.16.2




[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




[hackers] [slstatus][PATCH] netspeeds: added error condition for openbsd

2018-05-19 Thread Tobias Tschinkowitz
implemented additional error condition for openbsd netstat in case the 
interface could not be found in the interface list or if_data is not
readable.

---
 components/netspeeds.c | 14 --
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/components/netspeeds.c b/components/netspeeds.c
index 9315fef..c846a2c 100644
--- a/components/netspeeds.c
+++ b/components/netspeeds.c
@@ -64,6 +64,7 @@
uint64_t rxbytes = 0;
const char *rxs;
extern const unsigned int interval;
+   char if_ok = 0;
 
if (getifaddrs() == -1) {
warn("getifaddrs failed");
@@ -72,10 +73,14 @@
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;
+   rxbytes += ifd->ifi_ibytes, if_ok = 1;
}
}
freeifaddrs(ifal);
+   if (!if_ok) {
+   warn("reading 'if_data' failed");
+   return NULL;
+   }
 
rxs = oldrxbytes ? fmt_scaled((rxbytes - oldrxbytes) /
  interval * 1000) : NULL;
@@ -91,6 +96,7 @@
uint64_t txbytes = 0;
const char *txs;
extern const unsigned int interval;
+   char if_ok = 0;
 
if (getifaddrs() == -1) {
warn("getifaddrs failed");
@@ -99,10 +105,14 @@
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;
+   txbytes += ifd->ifi_obytes, if_ok = 1;
}
}
freeifaddrs(ifal);
+   if (!if_ok) {
+   warn("reading 'if_data' failed");
+   return NULL;
+   }
 
txs = oldtxbytes ? fmt_scaled((txbytes - oldtxbytes) /
  interval * 1000) : NULL;
-- 
2.16.2




[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] [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




[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][PATCH] suggestion: add user defined format callback

2018-05-17 Thread Tobias Tschinkowitz
Hi!

This is just a suggestion, so not really something to commit right away.
What do you think about a callback function where the user could define
more precisely how formatting is done (like adding symbols for
percentages etc.)

I just prepared a little patch to show what i mean.

Greetings,
Tobias

---
 config.def.h | 16 +---
 slstatus.c   | 15 ---
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/config.def.h b/config.def.h
index 9dcd5d6..b92fbac 100644
--- a/config.def.h
+++ b/config.def.h
@@ -6,6 +6,15 @@ static const unsigned int interval = 1000;
 /* text to show if no value can be retrieved */
 static const char unknown_str[] = "n/a";
 
+/* example callback function for custom formatting */
+void
+wifi_perc_cb(const char *raw_val, char *res, int res_len)
+{
+   char *s[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "█" };
+   int perc = atoi(raw_val);
+   snprintf(res, res_len, "%s %s ", raw_val, s[((8 * perc) / 100)]);
+}
+
 /* maximum output string length */
 #define MAXLEN 2048
 
@@ -54,6 +63,7 @@ static const char unknown_str[] = "n/a";
  * wifi_essid   WiFi ESSID  interface name (wlan0)
  */
 static const struct arg args[] = {
-   /* function format  argument */
-   { datetime, "%s",   "%F %T" },
-};
+   /* function format  format_cb   argument */
+   { wifi_perc,NULL,   wifi_perc_cb,   "iwn0"  },
+   { datetime, "%s",   NULL,   "%F %T" },
+};
diff --git a/slstatus.c b/slstatus.c
index e8d367b..614d703 100644
--- a/slstatus.c
+++ b/slstatus.c
@@ -14,6 +14,7 @@
 struct arg {
const char *(*func)();
const char *fmt;
+   void (*fmt_cb)(const char *, char *, int);
const char *args;
 };
 
@@ -88,9 +89,17 @@ main(int argc, char *argv[])
for (i = len = 0; i < LEN(args); i++) {
const char * res = args[i].func(args[i].args);
res = (res == NULL) ? unknown_str : res;
-   len += snprintf(status + len, sizeof(status) - len,
-   args[i].fmt, res);
-
+   if (args[i].fmt_cb) {
+   char cb_res[64];
+   args[i].fmt_cb(res, cb_res, 64);
+   strlcat(status + len, cb_res,
+   sizeof(status) - len);
+   len += strlen(cb_res);
+   } else {
+   len += snprintf(status + len,
+   sizeof(status) - len,
+   args[i].fmt, res);
+   }
if (len >= sizeof(status)) {
status[sizeof(status) - 1] = '\0';
}
-- 
2.16.2




[hackers] [slstatus][PATCH] added comment for temp function (openbsd)

2018-05-17 Thread Tobias Tschinkowitz
---
 config.def.h | 1 +
 1 file changed, 1 insertion(+)

diff --git config.def.h config.def.h
index ef64b16..00796c6 100644
--- config.def.h
+++ config.def.h
@@ -43,6 +43,7 @@ static const char unknown_str[] = "n/a";
  * swap_usedused swap in GB NULL
  * temp temperature in degree celsius   sensor file
  *  
(/sys/class/thermal/...)
+ *  NULL on OpenBSD
  * uid  UID of current user NULL
  * uptime   system uptime   NULL
  * username username of current userNULL
-- 
2.16.2




[hackers] [slstatus][patch] corrected calculations for disk space

2018-05-17 Thread Tobias Tschinkowitz
Hi Aaron,

i checked the complete functionality now for openbsd and had just some
different values for my disk space (compared it to df(1)).
The rest is working fine!

Greets,
Tobias

---
 components/disk.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git components/disk.c components/disk.c
index 8112981..bf69875 100644
--- components/disk.c
+++ components/disk.c
@@ -17,7 +17,7 @@ disk_free(const char *mnt)
}
 
return bprintf("%f",
-  (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 
1024);
+  (float)fs.f_frsize * (float)fs.f_bavail / 1024 / 1024 / 
1024);
 }
 
 const char *
@@ -31,7 +31,7 @@ disk_perc(const char *mnt)
}
 
return bprintf("%d", (int)(100 *
-  (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks;
+  (1.0f - ((float)fs.f_bavail / (float)fs.f_blocks;
 }
 
 const char *
@@ -45,7 +45,7 @@ disk_total(const char *mnt)
}
 
return bprintf("%f",
-  (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 
1024);
+  (float)fs.f_frsize * (float)fs.f_blocks / 1024 / 1024 / 
1024);
 }
 
 const char *
@@ -59,6 +59,6 @@ disk_used(const char *mnt)
}
 
return bprintf("%f",
-  (float)fs.f_bsize * ((float)fs.f_blocks -
+  (float)fs.f_frsize * ((float)fs.f_blocks -
   (float)fs.f_bfree) / 1024 / 1024 / 1024);
 }
-- 
2.16.2




[hackers] [slstatus][patch] added wifi functionality for openbsd

2018-05-16 Thread Tobias Tschinkowitz
Heyho,

just added wifi functionality for openbsd (display ESSID and signal
strength percentage)

Greets,
Tobias

---
 components/wifi.c | 76 ++-
 1 file changed, 75 insertions(+), 1 deletion(-)

diff --git components/wifi.c components/wifi.c
index 24dca36..edbcc01 100644
--- components/wifi.c
+++ components/wifi.c
@@ -93,5 +93,79 @@
return id;
}
 #elif defined(__OpenBSD__)
-   /* unimplemented */
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+   #include 
+
+   #include "../util.h"
+
+   static int
+   load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
+   {
+   struct ieee80211_bssid bssid;
+   int sockfd;
+   memset(, 0, sizeof(bssid);
+   memset(nr, 0, sizeof(struct ieee80211_nodereq));
+   if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
+   fprintf(stderr, "socket 'AF_INET': %s\n",
+   strerror(errno));
+   return 0;
+   }
+   strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
+   if ((ioctl(sockfd, SIOCG80211BSSID, )) == -1) {
+   fprintf(stderr, "ioctl 'SIOCG80211BSSID': %s\n",
+   strerror(errno));
+   close(sockfd);
+   return 0;
+   }
+   strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
+   memmove(>nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
+   if ((ioctl(sockfd, SIOCG80211NODE, nr)) == -1 && nr->nr_rssi) {
+   fprintf(stderr, "ioctl 'SIOCG80211NODE': %s\n",
+   strerror(errno));
+   close(sockfd);
+   return 0;
+   }
+   return close(sockfd), 1;
+
+   }
+
+   const char *
+   wifi_perc(const char *iface)
+   {
+   struct ieee80211_nodereq nr;
+   int q;
+
+   if (load_ieee80211_nodereq(iface, )) {
+   if (nr.nr_max_rssi)
+   q = IEEE80211_NODEREQ_RSSI();
+   else
+   q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= 
-100 ? 0 :
+   (2 * (nr.nr_rssi + 100)));
+   return bprintf("%d", q);
+   }
+   return NULL;
+   }
+
+   const char *
+   wifi_essid(const char *iface)
+   {
+   struct ieee80211_nodereq nr;
+
+   if (load_ieee80211_nodereq(iface, )) {
+   return bprintf("%s", nr.nr_nwid);
+   }
+   return NULL;
+   }
+
 #endif
-- 
2.16.2




[hackers] [slstatus][patch] corrected calculation for swap on openbsd

2018-05-16 Thread Tobias Tschinkowitz
Hi again!

The swapctl(2) function fills the swapent struct with 512KB blocks.
As we want to display in GB, i just modified the calculation for this to
get the expected output.

Greetings,
Tobias

---
 components/swap.c | 12 +---
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git components/swap.c components/swap.c
index 031b713..caa4788 100644
--- components/swap.c
+++ components/swap.c
@@ -131,8 +131,6 @@
#include 
#include 
 
-   #define dbtoqb(b) dbtob((int64_t)(b))
-
static void
getstats(int *total, int *used)
{
@@ -162,8 +160,8 @@
*used = 0;
 
for (i = 0; i < rnswap; i++) {
-   *total += dbtoqb(sep->se_nblks);
-   *used += dbtoqb(sep->se_inuse);
+   *total += sep->se_nblks >> 1;
+   *used += sep->se_inuse >> 1;
}
 
free(fsep);
@@ -176,7 +174,7 @@
 
getstats(, );
 
-   return bprintf("%f", (float)(total - used) / 1024 / 1024 / 
1024);
+   return bprintf("%f", (float)(total - used) / 1024 / 1024);
}
 
const char *
@@ -196,7 +194,7 @@
 
getstats(, );
 
-   return bprintf("%f", (float)total / 1024 / 1024 / 1024);
+   return bprintf("%f", (float)total / 1024 / 1024);
}
 
const char *
@@ -206,6 +204,6 @@
 
getstats(, );
 
-   return bprintf("%f", (float)used / 1024 / 1024 / 1024);
+   return bprintf("%f", (float)used / 1024 / 1024);
}
 #endif
-- 
2.16.2



[hackers] [slstatus][patch] consistent calculation of ram_* on openbsd

2018-05-16 Thread Tobias Tschinkowitz
Just to add a little more consistency in the calculation...

Greets,
Tobias

---
 components/ram.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git components/ram.c components/ram.c
index 1178837..57081d2 100644
--- components/ram.c
+++ components/ram.c
@@ -60,6 +60,7 @@
#include 
 
#define LOG1024 10
+   #define pagetok(size, pageshift) ((size) << (pageshift - LOG1024))
 
inline int
load_uvmexp(struct uvmexp *uvmexp)
@@ -81,7 +82,7 @@
 
if (load_uvmexp()) {
free_pages = uvmexp.npages - uvmexp.active;
-   free = (float)(free_pages << (uvmexp.pageshift - 
LOG1024)) / 1024 / 1024; 
+   free = (float)(pagetok(free_pages, uvmexp.pageshift)) / 
1024 / 1024;
return bprintf("%f", free);
}
 
@@ -109,7 +110,7 @@
float total;
 
if (load_uvmexp()) {
-   total = (float)(uvmexp.npages << (uvmexp.pageshift - 
LOG1024)) / 1024 / 1024; 
+   total = (float)(pagetok(uvmexp.npages, 
uvmexp.pageshift)) / 1024 / 1024;
return bprintf("%f", total);
}
 
@@ -123,8 +124,7 @@
float used;
 
if (load_uvmexp()) {
-   used = (double) (uvmexp.active * uvmexp.pagesize) /
-  1024 / 1024 / 1024;
+   used = (float)(pagetok(uvmexp.active, 
uvmexp.pageshift)) / 1024 / 1024;
return bprintf("%f", used);
}
 
-- 
2.16.2