dedicated dumb client to run bspc query -D -d .occupied --names

This commit is contained in:
randomuser 2022-09-16 13:10:59 -05:00
parent 0fca3dba56
commit fb2a937f02
1 changed files with 16 additions and 26 deletions

42
bspc.c
View File

@ -29,23 +29,17 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <poll.h>
#include <sys/un.h> #include <sys/un.h>
#include <poll.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h> #include <ctype.h>
#include <stdbool.h>
/* prototypes */ /* prototypes */
void warn(char *fmt, ...); void warn(char *fmt, ...);
@ -218,16 +212,12 @@ bool is_hex_color(const char *color)
return true; return true;
} }
int main(int argc, char *argv[]) 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], rsp[BUFSIZ];
if (argc < 2) {
err("No arguments given.\n");
}
sock_address.sun_family = AF_UNIX; sock_address.sun_family = AF_UNIX;
char *sp; char *sp;
@ -251,11 +241,10 @@ int main(int argc, char *argv[])
err("Failed to connect to the socket.\n"); err("Failed to connect to the socket.\n");
} }
argc--, argv++;
int msg_len = 0; int msg_len = 0;
for (int offset = 0, rem = sizeof(msg), n = 0; argc > 0 && rem > 0; offset += n, rem -= n, argc--, argv++) { 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", *argv, 0); n = snprintf(msg + offset, rem, "%s%c", *args, 0);
msg_len += n; msg_len += n;
} }
@ -274,14 +263,7 @@ int main(int argc, char *argv[])
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';
if (rsp[0] == FAILURE_MESSAGE[0]) { return strdup(rsp);
ret = EXIT_FAILURE;
fprintf(stderr, "%s", rsp + 1);
fflush(stderr);
} else {
fprintf(stdout, "%s", rsp);
fflush(stdout);
}
} else { } else {
break; break;
} }
@ -292,5 +274,13 @@ int main(int argc, char *argv[])
} }
close(sock_fd); close(sock_fd);
return ret; return NULL;
}
int main(void) {
char *text[] = {"query", "-D", "-d", ".occupied", "--names"};
int count = 5;
char *hi = send_msg_to_bspwm(text, count);
printf("%s", hi);
} }