summaryrefslogtreecommitdiff
path: root/status.c
diff options
context:
space:
mode:
author0scar <qgt268@alumni.ku.dk>2024-12-27 13:48:52 +0000
committer0scar <qgt268@alumni.ku.dk>2024-12-27 13:48:52 +0000
commita5f734afc52d2a46e3938e581dd1bb94bd87f0bd (patch)
treeb986f4172132c56a687295fe3a52636817ae3581 /status.c
parent15efcd8b6e9a26ec07edc15b21259da7e1895a1d (diff)
Add link info & battery icons
Diffstat (limited to 'status.c')
-rw-r--r--status.c124
1 files changed, 113 insertions, 11 deletions
diff --git a/status.c b/status.c
index 75ee1a6..814f94d 100644
--- a/status.c
+++ b/status.c
@@ -1,15 +1,21 @@
-#include <stdlib.h>
-#include <unistd.h>
-#include <stdio.h>
+#include <errno.h>
+#include <ifaddrs.h>
+#include <linux/wireless.h>
+//#include <net/if.h>
#include <stdbool.h>
#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include <time.h>
#include <sys/time.h>
-#include <errno.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
#define ELEMENT_SEPERATOR " "
+
#define STATUS_STRBUF_SZ 512
#define ELEMENT_STRBUF_SZ 64
@@ -31,6 +37,7 @@ void date(char* buf) {
strftime(buf, ELEMENT_STRBUF_SZ, "%F %R", &tm);
}
+
enum battery_status_charge {
bat_unknown,
bat_not_charging,
@@ -43,6 +50,20 @@ struct battery_status {
float charge;
};
+char* battery_level_icon[] = {
+ "\uf579",
+ "\uf57a",
+ "\uf57b",
+ "\uf57c",
+ "\uf57d",
+ "\uf57e",
+ "\uf57f",
+ "\uf580",
+ "\uf581",
+ "\uf578",
+ "\uf578",
+};
+
struct battery_status get_battery_status(const char* buf) {
const char path_prefix[] = "/sys/class/power_supply/";
@@ -93,17 +114,97 @@ struct battery_status get_battery_status(const char* buf) {
void get_battery1_status(char* buf) {
struct battery_status s = get_battery_status("BAT1");
- snprintf(buf, ELEMENT_STRBUF_SZ, "\uf581 %.1f%%", 100.f * s.charge);
+ int batlvl = (int)(s.charge*100.f) / 10;
+ char* batlvl_icon = battery_level_icon[batlvl];
+
+ snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge);
}
+
void get_battery0_status(char* buf) {
struct battery_status s = get_battery_status("BAT0");
- snprintf(buf, ELEMENT_STRBUF_SZ, "\uf581 %.1f%%", 100.f * s.charge);
+ int batlvl = (int)(s.charge*100.f) / 10;
+ char* batlvl_icon = battery_level_icon[batlvl];
+
+ snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge);
+}
+
+/* todo, remake this to enumerate all possible batteries */
+void get_all_bat_status(char* buf) {
+ struct battery_status s = get_battery_status("BAT0");
+ struct battery_status t = get_battery_status("BAT1");
+
+ s.charge = (s.charge + t.charge) / 2.f;
+
+ int batlvl = (int)(s.charge*100.f) / 10;
+ char* batlvl_icon = battery_level_icon[batlvl];
+
+ //snprintf(buf, ELEMENT_STRBUF_SZ, "%.1f%%", 100.f * s.charge);
+ snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge);
+}
+
+
+void get_net_link_status(char* buf) {
+ struct ifaddrs *if_addr;
+ size_t n = 0;
+
+ getifaddrs(&if_addr);
+
+
+ for (struct ifaddrs *ifa = if_addr; ifa != NULL; ifa = ifa->ifa_next) {
+ // Ignore loopback interface flag
+ if (ifa->ifa_flags & IFF_LOOPBACK) continue;
+
+ // Ignore interfaces without an address
+ if (ifa->ifa_addr == NULL) continue;
+
+ // Ignore everythings not an IPv4 link
+ // (AF_PACKET might be usefull if you want to display tx)
+ if (ifa->ifa_addr->sa_family != AF_INET) continue;
+
+ if (ifa->ifa_flags & IFF_UP) {
+
+ size_t l = strlen(ifa->ifa_name);
+ struct iwreq wreq;
+ int sock;
+ char id[IW_ESSID_MAX_SIZE+1];
+
+ memset(id, 0, IW_ESSID_MAX_SIZE+1);
+ memset(&wreq, 0, sizeof(struct iwreq));
+ sock = socket(AF_INET, SOCK_DGRAM, 0);
+ setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifa->ifa_name, l);
+
+ strncpy(wreq.ifr_name, ifa->ifa_name, l);
+
+ wreq.u.essid.pointer = id;
+ wreq.u.essid.length = IW_ESSID_MAX_SIZE;
+ ioctl(sock, SIOCGIWESSID, &wreq);
+
+
+ strncpy(buf + n, ifa->ifa_name, l);
+
+ size_t ssid_len = strlen(id);
+ if (ssid_len > 0) {
+ buf[n + l] = ' ';
+ buf[n + l + 1] = '(';
+ strncpy(buf + n + l + 2, id, ssid_len);
+ l += ssid_len;
+ buf[n + l + 2] = ')';
+ }
+
+ strcat(buf + n + l, " ");
+
+ n += l + 2;
+ }
+ }
}
static struct element statusbar[] = {
- {get_battery0_status, {60,0}, 0},
- {get_battery1_status, {60,0}, 0},
+
+ //{get_battery0_status, {60,0}, 0},
+ //{get_battery1_status, {60,0}, 0},
+ {get_net_link_status, {5,0}, 0},
+ {get_all_bat_status, {60,0}, 0},
{date, {2,500000}, 0},
};
@@ -156,9 +257,10 @@ int main() {
if (i != num_elems - 1) strcat(buf, ELEMENT_SEPERATOR);
}
- strcat(buf, "%");
+ //strcat(buf, "\0");
- puts(buf);
+ fputs(buf, stdout);
+ fflush(stdout);
timersub(&next_update, &now, &next_update);
usleep(next_update.tv_sec*1000*1000+next_update.tv_usec);