commit - 75f8ae7f8e35a50d328695d0271b352a78a130cc
commit + 66bd0b1c1064405df759d617cd7c068f69acd4fa
blob - 0669e0183e4a20ddb9377c556be4e1c761f6449e
blob + 6ac39fd8390e60c02019037096662ff2a57c78c1
--- include/statbar.h
+++ include/statbar.h
#define UNKNOWN "\ueb32"
#define CPUTEMP "\uf4bc"
#define FANSPEED "\uefa7"
+#define WIFI "\uf1eb"
+#define ETHERNET "\uef09"
/* Clock */
extern char clock_string[26];
extern char **get_mail_path_ptr(void);
extern void close_mail(void);
+/* Network */
+extern char network_string[5];
+
+extern bool init_network_socket(int *fd);
+extern void read_network_socket(int fd);
+extern void get_initial_network_state(void);
+extern void set_ethernet_device(char *dev);
+extern void set_wifi_device(char *dev);
+
blob - 96c322c42d324b68f0900677018b1d20da12bde5
blob + 165126fb42caef1112dddf77ff6d848e4fe38ed4
--- modules/Makefile
+++ modules/Makefile
LIB= statbar_modules
-SRCS= clock.c battery.c volume.c cputemp.c fanspeed.c weather.c mail.c
+SRCS= clock.c battery.c volume.c cputemp.c fanspeed.c weather.c mail.c network.c
MAN=
CFLAGS += -I../include -I/usr/local/include
blob - /dev/null
blob + 32ecda3cfc75f04bb7e246ac462680711f03d230 (mode 644)
--- /dev/null
+++ modules/network.c
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <unistd.h>
+#include <net/if.h>
+#include <net/route.h>
+#include <sys/socket.h>
+#include <sys/sysctl.h>
+
+#include "statbar.h"
+
+char network_string[5];
+
+static char ethernet_device[IF_NAMESIZE];
+static char wifi_device[IF_NAMESIZE];
+
+bool
+init_network_socket(int *fd)
+{
+ *fd = socket(AF_ROUTE, SOCK_RAW, 0);
+ if (*fd < 0)
+ {
+ perror("socket");
+
+ return false;
+ }
+
+ return true;
+}
+
+void
+read_network_socket(int fd)
+{
+ char buf[1024];
+ char ifname[IF_NAMESIZE];
+ struct rt_msghdr *rtm;
+ struct if_msghdr *ifm;
+ ssize_t n;
+
+ n = read(fd, buf, sizeof(buf));
+ if (n < 0)
+ {
+ perror("read");
+
+ return;
+ }
+
+ rtm = (struct rt_msghdr *)buf;
+ if (rtm->rtm_type != RTM_IFINFO) return;
+
+ ifm = (struct if_msghdr *)buf;
+ if (if_indextoname(ifm->ifm_index, ifname) == NULL)
+ {
+ (void)fprintf(stderr, "No name assosiated with index %u\n", ifm->ifm_index);
+
+ return;
+ }
+ if (!LINK_STATE_IS_UP(ifm->ifm_data.ifi_link_state))
+ {
+ network_string[0] = '\0';
+
+ return;
+ }
+ if (strcmp(ifname, ethernet_device) == 0) (void)strcpy(network_string, ETHERNET);
+ else if (strcmp(ifname, wifi_device) == 0) (void)strcpy(network_string, WIFI);
+}
+
+void
+get_initial_network_state(void)
+{
+ int mib[6];
+ char ifname[IF_NAMESIZE];
+ char *buf;
+ char *next;
+ char *end;
+ size_t len;
+ struct rt_msghdr *rtm;
+ struct if_msghdr *ifm;
+
+ mib[0] = CTL_NET;
+ mib[1] = AF_ROUTE;
+ mib[2] = 0;
+ mib[3] = 0;
+ mib[4] = NET_RT_IFLIST;
+ mib[5] = 0;
+
+ if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
+ {
+ perror("sysctl");
+
+ return;
+ }
+ buf = malloc(len);
+ if (buf == NULL)
+ {
+ perror("malloc");
+
+ return;
+ }
+ if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)
+ {
+ perror("sysctl");
+ free(buf);
+
+ return;
+ }
+
+ end = buf + len;
+ for (next = buf; next < end; next += ((struct rt_msghdr *)next)->rtm_msglen)
+ {
+ rtm = (struct rt_msghdr *)next;
+ if (rtm->rtm_type != RTM_IFINFO) continue;
+
+ ifm = (struct if_msghdr *)next;
+ if (if_indextoname(ifm->ifm_index, ifname) == NULL)
+ {
+ (void)fprintf(stderr, "No name assosiated with index %u\n", ifm->ifm_index);
+
+ continue;
+ }
+ if (!LINK_STATE_IS_UP(ifm->ifm_data.ifi_link_state))
+ {
+ network_string[0] = '\0';
+
+ continue;
+ }
+ if (strcmp(ifname, wifi_device) == 0) (void)strcpy(network_string, WIFI);
+ if (strcmp(ifname, ethernet_device) == 0) (void)strcpy(network_string, ETHERNET);
+ }
+
+ free(buf);
+}
+
+void set_ethernet_device(char *dev)
+{
+ (void)strlcpy(ethernet_device, dev, IF_NAMESIZE);
+}
+
+void set_wifi_device(char *dev)
+{
+ (void)strlcpy(wifi_device, dev, IF_NAMESIZE);
+}
+
blob - c83ffa882820285f002677666ee34df2cdf10c11
blob + 87f55d1a04d52dcae9fa214c9a70008fde5a41da
--- src/main.c
+++ src/main.c
continue;
}
+ if (strncmp(line, "ethernet device", strlen("ethernet device")) == 0)
+ {
+ delim++;
+ while (*delim == ' ') delim++;
+ set_ethernet_device(delim);
+ continue;
+ }
+ if (strncmp(line, "wifi device", strlen("wifi device")) == 0)
+ {
+ delim++;
+ while (*delim == ' ') delim++;
+ set_wifi_device(delim);
+ continue;
+ }
/* 10 minute max is arbitrary */
delim++;
Window root;
char statbar_text[MAX_STATBAR_LEN];
bool dirty = true;
+ struct pollfd *network_pfd = NULL;
struct pollfd *weather_pfd = NULL;
char wpfd_buf;
bool apm_open = false;
bool hdl_open = false;
+ bool network_open = false;
struct timespec now;
struct timespec clocks[CLOCKS_COUNT];
struct timespec *next_event;
struct timespec fanspeed_interval = { .tv_sec = 3 };
struct timespec weather_interval = { .tv_sec = 3600 };
struct pollfd *pfd;
- int nfds = 0;
+ int nfds = 1; /* Start with 1 to guarantee space for the network socket */
int i;
(void)setvbuf(stdout, NULL, _IOLBF, 0);
hdl_open = init_volume(&nfds);
get_cputemp();
get_fanspeed();
+ get_initial_network_state();
if (weather_loc_valid) nfds++;
pfd = malloc(nfds * sizeof(struct pollfd));
perror("malloc");
goto cleanup;
}
+ network_pfd = &pfd[nfds - 1];
+ network_open = init_network_socket(&network_pfd->fd);
+ network_pfd->events = POLLIN;
if (weather_loc_valid)
{
- weather_pfd = &pfd[nfds - 1];
+ weather_pfd = &pfd[nfds - 2];
weather_pfd->fd = weather_pipe[0];
get_weather();
}
{
if (hdl_open)
process_volume_events(pfd, &hdl_open);
+ if (network_pfd->revents & POLLIN)
+ read_network_socket(network_pfd->fd);
if (weather_pfd && (weather_pfd->revents & POLLIN))
(void)read(weather_pipe[0], &wpfd_buf, 1);
if (dirty)
{
- (void)snprintf(statbar_text, MAX_STATBAR_LEN, "%s | %s | %s | %s | %s | %s | %s",
+ (void)snprintf(statbar_text, MAX_STATBAR_LEN, "%s%s | %s | %s | %s | %s | %s | %s",
mail_string,
+ network_string,
weather_string,
fanspeed_string,
cputemp_string,
(void)puts("Closing statbar");
if (apm_open) close_battery();
if (hdl_open) close_volume();
+ if (network_open) (void)close(network_pfd->fd);
if (weather_loc_valid) close_weather();
if (mail_path_valid) close_mail();
(void)XCloseDisplay(display);