add bspwm module

This commit is contained in:
randomuser 2022-09-16 14:32:59 -05:00
parent fb2a937f02
commit c1e5b28402
2 changed files with 45 additions and 6 deletions

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
bspc:
cc bspc.c -o bspc -lxcb
clean:
rm bspc

46
bspc.c
View File

@ -129,7 +129,7 @@ char *copy_string(char *str, size_t len)
{ {
char *cpy = calloc(1, ((len+1) * sizeof(char))); char *cpy = calloc(1, ((len+1) * sizeof(char)));
if (cpy == NULL) { if (cpy == NULL) {
perror("Copy string: calloc"); perror("Copy string: callow");
return NULL; return NULL;
} }
strncpy(cpy, str, len); strncpy(cpy, str, len);
@ -216,7 +216,8 @@ char *send_msg_to_bspwm(char *args[], int count)
{ {
int sock_fd; int sock_fd;
struct sockaddr_un sock_address; struct sockaddr_un sock_address;
char msg[BUFSIZ], rsp[BUFSIZ]; char msg[BUFSIZ];
static char rsp[BUFSIZ];
sock_address.sun_family = AF_UNIX; sock_address.sun_family = AF_UNIX;
char *sp; char *sp;
@ -263,7 +264,7 @@ char *send_msg_to_bspwm(char *args[], int count)
if (fds[0].revents & POLLIN) { if (fds[0].revents & POLLIN) {
if ((nb = recv(sock_fd, rsp, sizeof(rsp)-1, 0)) > 0) { if ((nb = recv(sock_fd, rsp, sizeof(rsp)-1, 0)) > 0) {
rsp[nb] = '\0'; rsp[nb] = '\0';
return strdup(rsp); return rsp;
} else { } else {
break; break;
} }
@ -278,9 +279,42 @@ char *send_msg_to_bspwm(char *args[], int count)
} }
int main(void) { int main(void) {
char *text[] = {"query", "-D", "-d", ".occupied", "--names"}; char finalbuffer[64], currentdesktop[2];
char *occupied[] = {"query", "-D", "-d", ".occupied", "--names"};
char *focused[] = {"query", "-D", "-d", ".focused", "--names"};
char *result;
char current;
int count = 5; int count = 5;
char *hi = send_msg_to_bspwm(text, count); result = send_msg_to_bspwm(occupied, count);
printf("%s", hi); if(!result) {
printf("error: sending message to bspwm failed!\n");
return 1;
}
memcpy(&finalbuffer, result, 64);
result = send_msg_to_bspwm(focused, count);
if(!result) {
printf("error: sending message to bspwm failed!\n");
return 1;
}
memcpy(&currentdesktop, result, 2);
*(currentdesktop + 1) = '\0';
result = finalbuffer;
for(int i = 0; i < 64; i++) {
current = *(finalbuffer + i);
if(current == '\0') break;
if(isdigit(current) && *currentdesktop == current)
*(finalbuffer + i + 1) = '<';
if(current == '\n')
current = ' ';
}
printf("%s", finalbuffer);
return 0;
} }