summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--status.c172
1 files changed, 85 insertions, 87 deletions
diff --git a/status.c b/status.c
index 7df88af..935b933 100644
--- a/status.c
+++ b/status.c
@@ -1,18 +1,18 @@
#include <arpa/inet.h>
+#include <assert.h>
#include <errno.h>
#include <ifaddrs.h>
#include <linux/if.h>
#include <linux/wireless.h>
+#include <mqueue.h>
#include <netinet/in.h>
+#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
-#include <mqueue.h>
-#include <signal.h>
-#include <assert.h>
#include <time.h>
#include <unistd.h>
@@ -28,7 +28,7 @@
#define MAX(a, b) ((a) > (b) ? (a) : (b))
/* Type definitions */
-typedef int (update_func_t)(char*, char*);
+typedef int(update_func_t)(char*, char*);
/* Data structures */
struct element {
@@ -64,8 +64,6 @@ struct interface_status {
char name[IFNAMSIZ + 1];
};
-
-
/* Prototypes */
int element_date(char* buf, char* fmt);
struct battery_status get_battery_status(const char* buf);
@@ -96,24 +94,32 @@ static struct element statusbar[] = {
/*{update_func_t, {minutes, seconds}, {0}, {0}},*/
[ELEMENT_INVALID] = {NULL, NULL, {0}, {0}, {0}},
-#define ELEMENT(name, arg, minutes, seconds) \
- [ELEMENT_##name] = {element_##name, arg, {(minutes * 60) + ((int)seconds), (long)(seconds * 1000000) % 1000000}, {0}, {0}},
+#define ELEMENT(name, arg, minutes, seconds) \
+ [ELEMENT_##name] = { \
+ element_##name, \
+ arg, \
+ {(minutes * 60) + ((int)seconds), (long)(seconds * 1000000) % 1000000}, \
+ {0}, \
+ {0}},
#include "config.def.h"
#undef ELEMENT
};
-static void cleanup(void) {
+static void
+cleanup(void) {
if (mq_unlink("/status") == -1) {
fprintf(stderr, "Failed to unlink mq: %s\n", strerror(errno));
}
}
// "fail fast strcmp", neat to find inequalities between strings
-static int ffast_strcmp(char* a, const char *const b) {
+static int
+ffast_strcmp(char* a, const char* const b) {
int i = 0;
while (a[i] == b[i]) {
- if (a[i] == '\0') break;
+ if (a[i] == '\0')
+ break;
i++;
}
@@ -122,11 +128,11 @@ static int ffast_strcmp(char* a, const char *const b) {
/* Functions */
int
element_date(char* buf, char* fmt) {
- //if (!ffast_strcmp(fmt, "NULL")) fmt = "%Y-%m-%d %H:%M";
- if (fmt == NULL) fmt = "%Y-%m-%d %H:%M";
+ if (fmt == NULL)
+ fmt = "%Y-%m-%d %H:%M";
- time_t now = time(NULL);
- struct tm *tm = localtime(&now);
+ time_t now = time(NULL);
+ struct tm* tm = localtime(&now);
strftime(buf, ELEMENT_STRBUF_SZ, fmt, tm);
return 0;
@@ -134,7 +140,7 @@ element_date(char* buf, char* fmt) {
struct battery_status
get_battery_status(const char* buf) {
- const char path_prefix[] = "/sys/class/power_supply/";
+ const char path_prefix[] = "/sys/class/power_supply/";
struct battery_status status = {.status = bat_unknown, .charge = -1};
char charge_path[128];
@@ -168,7 +174,7 @@ get_battery_status(const char* buf) {
strcat(status_path, buf);
strcat(status_path, "/status");
- bat_charge = fopen(charge_path, "r");
+ bat_charge = fopen(charge_path, "r");
if (!bat_charge) {
fprintf(stderr, "%d: \"%s\" %s\n", errno, charge_path, strerror(errno));
return status;
@@ -187,8 +193,8 @@ get_battery_status(const char* buf) {
fclose(bat_charge);
fclose(bat_capacity);
- charge = atoi(charge_str);
- capacity = atoi(capacity_str);
+ charge = atoi(charge_str);
+ capacity = atoi(capacity_str);
status.charge = (float)charge / capacity;
// Get battery status
@@ -211,11 +217,9 @@ get_battery_status(const char* buf) {
if (!strcmp(status_str, "Charging")) {
status.status = bat_charging;
- }
- else if (!strcmp(status_str, "Discharging")) {
+ } else if (!strcmp(status_str, "Discharging")) {
status.status = bat_discharging;
- }
- else if (!strcmp(status_str, "Not charging")) {
+ } else if (!strcmp(status_str, "Not charging")) {
status.status = bat_not_charging;
}
@@ -227,8 +231,9 @@ element_battery(char* buf, char* bat) {
struct battery_status s = get_battery_status(bat);
int batlvl = (int)(s.charge * 100.f) / 10;
- if (s.status == bat_charging) batlvl = 11;
- char* batlvl_icon = battery_level_icon[batlvl];
+ if (s.status == bat_charging)
+ batlvl = 11;
+ char* batlvl_icon = battery_level_icon[batlvl];
snprintf(buf, ELEMENT_STRBUF_SZ, "%s %.1f%%", batlvl_icon, 100.f * s.charge);
return 0;
@@ -277,14 +282,12 @@ get_essid(char* if_name, char* dst) {
// May need to open a new socket, rebind, or something in-between to get both
// wireless capabilities and essid??
-
-
// Test if we have wireless on this interface
- //if (ioctl(sock, SIOCGIWNAME, &wreq) < 0) {
+ // if (ioctl(sock, SIOCGIWNAME, &wreq) < 0) {
// // protocol stored in wreq.u.name
// close(sock);
// return;
- //}
+ // }
if (ioctl(sock, SIOCGIWESSID, &wreq) < 0) {
dst[0] = '\0';
@@ -293,13 +296,15 @@ get_essid(char* if_name, char* dst) {
close(sock);
}
-int element_wifi(char* buf, char* link_name) {
+int
+element_wifi(char* buf, char* link_name) {
struct interface_status if_status;
struct ifaddrs* if_addr;
struct ifaddrs* ifa;
memset(&if_status, 0, sizeof(struct interface_status));
- const int nlen = strlen(link_name) > IFNAMSIZ + 1 ? IFNAMSIZ : strlen(link_name);
+ const int nlen =
+ strlen(link_name) > IFNAMSIZ + 1 ? IFNAMSIZ : strlen(link_name);
strncpy(if_status.name, link_name, nlen);
getifaddrs(&if_addr);
@@ -308,41 +313,43 @@ int element_wifi(char* buf, char* link_name) {
size_t i = 0;
const char* name = ifa->ifa_name;
- if (ffast_strcmp(if_status.name, name)) continue;
+ if (ffast_strcmp(if_status.name, name))
+ continue;
if (ifa->ifa_flags & IFF_LOWER_UP) {
if_status.up = true;
if (ifa->ifa_addr != NULL) {
- const int family = ifa->ifa_addr->sa_family;
+ const int family = ifa->ifa_addr->sa_family;
- size_t strsize = 0;
- char* dst = NULL;
+ size_t strsize = 0;
+ char* dst = NULL;
- if (family == AF_INET) {
- strsize = INET_ADDRSTRLEN;
- dst = if_status.address.ip4;
- } else if (family == AF_INET6) {
- strsize = INET6_ADDRSTRLEN;
- dst = if_status.address.ip6;
- } else if (family == AF_PACKET) {
- continue;
- } else
- /* In this case, there's probably something horribly wrong */
- continue;
+ if (family == AF_INET) {
+ strsize = INET_ADDRSTRLEN;
+ dst = if_status.address.ip4;
+ } else if (family == AF_INET6) {
+ strsize = INET6_ADDRSTRLEN;
+ dst = if_status.address.ip6;
+ } else if (family == AF_PACKET) {
+ continue;
+ } else
+ /* In this case, there's probably something horribly wrong */
+ continue;
- /* Get the IP address */
- inet_ntop(family, (void*)&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr,
- dst, strsize);
+ /* Get the IP address */
+ inet_ntop(family,
+ (void*)&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, dst,
+ strsize);
}
}
-
}
freeifaddrs(if_addr);
// print interface
- if (!if_status.up) return 0;
+ if (!if_status.up)
+ return 0;
get_essid(if_status.name, if_status.ssid);
@@ -353,15 +360,7 @@ int element_wifi(char* buf, char* link_name) {
strcat(buf, if_status.ssid);
strcat(buf, ")");
}
- //if (if_status.address.ip4[0] != 0 || if_status.address.ip6[0] != 0) {
- // strcat(buf, " ");
- // if (if_status.address.ip4[0] != 0) {
- // strcat(buf, "⁴");
- // }
- // if (if_status.address.ip6[0] != 0) {
- // strcat(buf, "⁶");
- // }
- //}
+
return 0;
}
@@ -436,10 +435,10 @@ update_element_thread(union sigval sv) {
// further notifications before emptying the queue.
// Makes you wonder if you should've used a named pipe + signals instead :)
struct sigevent sev = (struct sigevent){
- .sigev_notify = SIGEV_THREAD,
- .sigev_notify_function = update_element_thread,
+ .sigev_notify = SIGEV_THREAD,
+ .sigev_notify_function = update_element_thread,
.sigev_notify_attributes = NULL,
- .sigev_value = sv,
+ .sigev_value = sv,
};
if (mq_notify(mq_data.mqfd, &sev) == -1) {
@@ -447,9 +446,11 @@ update_element_thread(union sigval sv) {
}
// Empty queue
- while (mq_receive(mq_data.mqfd, (char*)&msg, sizeof(struct message_t), NULL) != -1) {
+ while (mq_receive(mq_data.mqfd, (char*)&msg, sizeof(struct message_t),
+ NULL) != -1) {
fprintf(stderr, "%d on %d\n", msg.action, msg.element);
- if (msg.action != update || msg.element <= ELEMENT_INVALID || msg.element > ELEMENT_MAX) {
+ if (msg.action != update || msg.element <= ELEMENT_INVALID ||
+ msg.element > ELEMENT_MAX) {
fprintf(stderr, "Invalid action/element\n");
continue;
}
@@ -469,7 +470,8 @@ update_element_thread(union sigval sv) {
/* Check if this element needs to be refreshed next, again */
next_fire = time_add(now, e->fire_interval);
- if (time_lt(next_fire, ((struct mq_data*)(sv.sival_ptr))->next_update)) {
+ if (time_lt(next_fire,
+ ((struct mq_data*)(sv.sival_ptr))->next_update)) {
((struct mq_data*)(sv.sival_ptr))->next_update = next_fire;
}
}
@@ -501,7 +503,8 @@ update_element_thread(union sigval sv) {
}
}
-static void update_statusbuffer(char* buf) {
+static void
+update_statusbuffer(char* buf) {
static const int num_elems = sizeof(statusbar) / sizeof(statusbar[0]);
/* Copy the statusbar buffers into the final buffer */
@@ -525,31 +528,30 @@ static void update_statusbuffer(char* buf) {
int
main(void) {
char buf[STATUS_STRBUF_SZ];
- //struct message_t *ipc_message;
//// ("/status", max(argv[identifier], XDG_SEAT, DISPLAY, WAYLAND_DISPLAY))
- const char* queue_identifier = "/status";
+ const char* queue_identifier = "/status";
const struct timespec one_minute = {60, 0};
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
struct mq_attr attr = {
- .mq_flags = 0, // ignored for mq_open
- .mq_maxmsg = 10, // must be [1;10], default value can be read from /proc/sys/fs/mqueue/msg_default
+ .mq_flags = 0, // ignored for mq_open
+ .mq_maxmsg = 10, // must be [1;10], default value can be read from
+ // /proc/sys/fs/mqueue/msg_default
.mq_msgsize = sizeof(struct message_t),
.mq_curmsgs = 0, // ignored for mq_open
};
- //printf("Attempting setting msg-size to %ld\n", sizeof(struct message_t));
-
struct mq_data mq_data;
- mq_data.dstbuffer = buf;
- mq_data.next_update = time_add(now, one_minute);;
+ mq_data.dstbuffer = buf;
+ mq_data.next_update = time_add(now, one_minute);
+ ;
mq_data.mqfd = mq_open(queue_identifier,
- O_RDONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NONBLOCK,
- // 600
- S_IRUSR | S_IWUSR,
- &attr); // O_NONBLOCK
+ O_RDONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NONBLOCK,
+ // 600
+ S_IRUSR | S_IWUSR,
+ &attr); // O_NONBLOCK
if (mq_data.mqfd == -1) {
fprintf(stderr, "Failed to open mq: %s\n", strerror(errno));
// Make this check a flag
@@ -561,10 +563,10 @@ main(void) {
fprintf(stderr, "Unlinked %s\n", queue_identifier);
// Retry
- mq_data.mqfd = mq_open(queue_identifier,
- O_RDONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NONBLOCK,
- S_IRUSR | S_IWUSR,
- &attr);
+ mq_data.mqfd =
+ mq_open(queue_identifier,
+ O_RDONLY | O_CREAT | O_EXCL | O_CLOEXEC | O_NONBLOCK,
+ S_IRUSR | S_IWUSR, &attr);
if (mq_data.mqfd == -1) {
fprintf(stderr, "Failed to open mq: %s\n", strerror(errno));
exit(EXIT_FAILURE);
@@ -581,8 +583,6 @@ main(void) {
// empty the queue
update_element_thread((union sigval){.sival_ptr = &mq_data});
-
-
const int num_elems = sizeof(statusbar) / sizeof(statusbar[0]);
while (true) {
@@ -629,8 +629,6 @@ main(void) {
update_statusbuffer(buf);
struct timespec sleep_for = time_sub(mq_data.next_update, now);
- // why the fuck are we adding 500 nanoseconds here
- //sleep_for = time_add(sleep_for, (struct timespec){0, 500});
/* Replace NULL to get "remaining time", in case we got
* interrupted / signaled */