tstatus/bspwm.c

140 lines
3.1 KiB
C
Raw Normal View History

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