tstatus/bspwm.c

140 lines
3.1 KiB
C

/* see bspc.h for copyright and distribution information */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <xcb/xcb.h>
#include <poll.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include "module.h"
#include "bspwm.h"
__attribute__((noreturn))
void err(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
char *send_msg_to_bspwm(char *args[], int count) {
int sock_fd;
struct sockaddr_un sock_address;
char msg[BUFSIZ];
static char rsp[BUFSIZ];
sock_address.sun_family = AF_UNIX;
char *sp;
if((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
err("Failed to create the socket.\n");
}
sp = getenv(SOCKET_ENV_VAR);
if(sp != NULL) {
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s", sp);
} else {
char *host = NULL;
int dn = 0, sn = 0;
if(xcb_parse_display(NULL, &host, &dn, &sn) != 0) {
snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), SOCKET_PATH_TPL, host, dn, sn);
}
free(host);
}
if(connect(sock_fd, (struct sockaddr *) &sock_address, sizeof(sock_address)) == -1) {
err("Failed to connect to the socket.\n");
}
int msg_len = 0;
for(int offset = 0, rem = sizeof(msg), n = 0; count > 0 && rem > 0; offset += n, rem -= n, count--, args++) {
n = snprintf(msg + offset, rem, "%s%c", *args, 0);
msg_len += n;
}
if(send(sock_fd, msg, msg_len, 0) == -1) {
err("Failed to send the data.\n");
}
int nb;
struct pollfd fds[] = {
{sock_fd, POLLIN, 0},
{STDOUT_FILENO, POLLHUP, 0},
};
while(poll(fds, 2, -1) > 0) {
if(fds[0].revents & POLLIN) {
if((nb = recv(sock_fd, rsp, sizeof(rsp)-1, 0)) > 0) {
rsp[nb] = '\0';
return rsp;
} else {
break;
}
}
if(fds[1].revents & (POLLERR | POLLHUP)) {
break;
}
}
close(sock_fd);
return NULL;
}
int bspwm_update(struct module *module) {
char current[2];
char *occupied[] = {"query", "-D", "-d", ".occupied", "--names"};
char *focused[] = {"query", "-D", "-d", ".focused", "--names"};
char *result;
result = send_msg_to_bspwm(occupied, LENGTH(occupied));
if(!result) {
printf("error: sending message to bspwm failed!\n");
return 1;
}
memcpy(&module->buffer, result, MODULE_BUFFER_LEN);
result = send_msg_to_bspwm(focused, LENGTH(focused));
if(!result) {
printf("error: sending message to bspwm failed!\n");
return 1;
}
memcpy(&current, result, 2);
current[1] = '\0';
for(int i = 0; i < MODULE_BUFFER_LEN; i++) {
if(module->buffer[i] == '\0' && i) {
if(*current != '\0' && i <= MODULE_BUFFER_LEN - 2) {
module->buffer[i - 1] = ' ';
module->buffer[i] = *current;
module->buffer[i + 1] = '<';
module->buffer[i + 2] = '\0';
} else module->buffer[i - 1] = '\0';
break;
}
if(isdigit(module->buffer[i]) && *current == module->buffer[i]) {
if(i <= MODULE_BUFFER_LEN - 2) module->buffer[i + 1] = '<';
*current = '\0';
}
if(module->buffer[i] == '\n')
module->buffer[i] = ' ';
}
return 0;
}