Skip to content

Commit

Permalink
Read bspwm events from bspc process
Browse files Browse the repository at this point in the history
  • Loading branch information
alexozer committed Nov 4, 2018
1 parent 9731383 commit cd37bd7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
68 changes: 62 additions & 6 deletions dapper.cpp
@@ -1,17 +1,56 @@
// Sockets code largely stolen from bspwm

#include <cstdio>
#include <iostream>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>

static const char *SOCKET_PATH = "/tmp/dapper.socket";

static volatile bool running = false;

#define MAX(A, B) ((A) > (B) ? (A) : (B))

void err(std::string msg) {
std::cerr << msg << std::endl;
std::exit(1);
}

void sig_handler(int sig) {
if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM) {
running = false;
}
}

int main() {
// Create fd for `bspc subscribe` process's stdout

int pipe_fds[2];
if (pipe(pipe_fds) == -1) {
err("Failed to create pipe");
}

int pipe_read = pipe_fds[0], pipe_write = pipe_fds[1];

int pid = fork();
if (pid == 0) {
dup2(pipe_write, STDOUT_FILENO);
const char *const args[] = {"bspc", "subscribe", "node", NULL};
execvp("bspc", (char *const *)args);
} else {
close(pipe_write);
}

signal(SIGINT, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGPIPE, SIG_IGN);

// Create fd for dapper's communication socket

struct sockaddr_un sock_address;
sock_address.sun_family = AF_UNIX;
std::snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s",
Expand All @@ -33,27 +72,44 @@ int main() {
err("Couldn't listen to the socket");
}

bool running = true;
int max_fd = sock_fd;
char msg[1024];
// Loop over input fds

running = true;
int max_fd = MAX(sock_fd, pipe_read);
const size_t BUF_SIZE = 1024;
char msg[BUF_SIZE];

while (running) {
fd_set descriptors;
FD_ZERO(&descriptors);
FD_SET(sock_fd, &descriptors);
FD_SET(pipe_read, &descriptors);

if (select(max_fd + 1, &descriptors, NULL, NULL, NULL) > 0) {
if (FD_ISSET(pipe_read, &descriptors)) {
int n = read(pipe_read, msg, BUF_SIZE);
if (n > 0) {
std::string message(msg, n);

std::cout << message << std::endl;
}
}

if (FD_ISSET(sock_fd, &descriptors)) {
int cli_fd = accept(sock_fd, NULL, 0);
if (cli_fd > 0) {
int n = recv(cli_fd, msg, sizeof(msg), 0);
int n = recv(cli_fd, msg, BUF_SIZE, 0);
if (n > 0) {
msg[n] = '\0';
std::string message(msg, n);

std::cout << std::string(msg) << std::endl;
std::cout << message << std::endl;
}
}
}
}
}

close(sock_fd);
unlink(SOCKET_PATH);
close(pipe_read);
}
16 changes: 2 additions & 14 deletions dapperc.cpp
@@ -1,4 +1,4 @@
// Sockets code largely stolen from bspc
// Sockets code largely stolen from bspwm

#include <cstdio>
#include <iostream>
Expand Down Expand Up @@ -46,22 +46,10 @@ int main(int argc, char *argv[]) {
std::string arg_str = arg_buf.str();
size_t msg_size = arg_str.size() * sizeof(arg_str[0]);

// Sent string is NOT null-terminated
if (send(sock_fd, arg_str.c_str(), msg_size, 0) == -1) {
err("Failed to send the data");
}

// struct pollfd poll_fd = {sock_fd, POLLIN, 0};

// struct pollfd fds[] = {
// {sock_fd, POLLIN, 0},
// {STDOUT_FILENO, POLLHUP, 0},
// };

// while (poll(fds, 2, -1) > 0) {
// if (fds[1].revents & (POLLERR | POLLHUP)) {
// break;
// }
// }

close(sock_fd);
}

0 comments on commit cd37bd7

Please sign in to comment.