From f7f684828d8bf47aa2b12da3ff327ca8702c310d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Hern=C3=A1ndez=20Hern=C3=A1ndez?= Date: Sun, 27 Feb 2022 14:50:01 -0600 Subject: [PATCH] allow user to set path of the fifo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Leonardo Hernández Hernández --- README.md | 3 +- src/main.cpp | 79 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 143bce5..2b129f1 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ sudo ninja -C build install You must start somebar using dwl's `-s` flag, e.g. `dwl -s somebar`. -Somebar can be controlled by writing to `$XDG_RUNTIME_DIR/somebar-0`. +Somebar can be controlled by writing to `$XDG_RUNTIME_DIR/somebar-0` +or the path defined by `-s` argument. The following commands are supported: * `status TEXT`: Updates the status bar diff --git a/src/main.cpp b/src/main.cpp index 6198d8b..e3795e0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -229,32 +229,46 @@ void onReady() wl_display_roundtrip(display); // wait for xdg_output names before we read stdin } +bool createFifo(std::string path) +{ + auto result = mkfifo(path.c_str(), 0666); + if (result == 0) { + auto fd = open(path.c_str(), O_CLOEXEC | O_NONBLOCK | O_RDONLY); + if (fd < 0) { + diesys("open status fifo reader"); + } + statusFifoName = path; + statusFifoFd = fd; + + fd = open(path.c_str(), O_CLOEXEC | O_WRONLY); + if (fd < 0) { + diesys("open status fifo writer"); + } + statusFifoWriter = fd; + + pollfds.push_back({ + .fd = statusFifoFd, + .events = POLLIN, + }); + return true; + } else if (errno != EEXIST) { + diesys("mkfifo"); + } + + return false; +} + void setupStatusFifo() { + if (!statusFifoName.empty()) { + createFifo(statusFifoName); + return; + } + for (auto i=0; i<100; i++) { auto path = std::string{getenv("XDG_RUNTIME_DIR")} + "/somebar-" + std::to_string(i); - auto result = mkfifo(path.c_str(), 0666); - if (result == 0) { - auto fd = open(path.c_str(), O_CLOEXEC | O_NONBLOCK | O_RDONLY); - if (fd < 0) { - diesys("open status fifo reader"); - } - statusFifoName = path; - statusFifoFd = fd; - - fd = open(path.c_str(), O_CLOEXEC | O_WRONLY); - if (fd < 0) { - diesys("open status fifo writer"); - } - statusFifoWriter = fd; - - pollfds.push_back({ - .fd = statusFifoFd, - .events = POLLIN, - }); + if (createFifo(path)) { return; - } else if (errno != EEXIST) { - diesys("mkfifo"); } } } @@ -420,14 +434,18 @@ static const struct wl_registry_listener registry_listener = { int main(int argc, char* argv[]) { int opt; - while ((opt = getopt(argc, argv, "chv")) != -1) { + while ((opt = getopt(argc, argv, "chvs:")) != -1) { switch (opt) { + case 's': + statusFifoName = optarg; + break; case 'h': - printf("Usage: %s [-h] [-v] [-c command]\n", argv[0]); + printf("Usage: %s [-h] [-v] [-s path to the fifo] [-c command]\n", argv[0]); printf(" -h: Show this help\n"); printf(" -v: Show somebar version\n"); + printf(" -s: Change path to the fifo (default is \"$XDG_RUNTIME_DIR/somebar-0\")\n"); printf(" -c: Sends a command to sombar. See README for details.\n"); - printf("If any of these are specified, somebar exits after the action.\n"); + printf("If any of these are specified (except -s), somebar exits after the action.\n"); printf("Otherwise, somebar will display itself.\n"); exit(0); case 'v': @@ -437,9 +455,14 @@ int main(int argc, char* argv[]) if (optind >= argc) { die("Expected command"); } - auto path = std::string {getenv("XDG_RUNTIME_DIR")} + "/somebar-0"; - int fd = open(path.c_str(), O_WRONLY | O_CLOEXEC); - if (fd < 0) { + std::string path; + if (statusFifoName.empty()) { + path = std::string {getenv("XDG_RUNTIME_DIR")} + "/somebar-0"; + } else { + path = statusFifoName; + } + statusFifoWriter = open(path.c_str(), O_WRONLY | O_CLOEXEC); + if (statusFifoWriter < 0) { fprintf(stderr, "could not open %s: ", path.c_str()); perror(""); exit(1); @@ -450,7 +473,7 @@ int main(int argc, char* argv[]) str += argv[i]; } str += "\n"; - write(fd, str.c_str(), str.size()); + write(statusFifoWriter, str.c_str(), str.size()); exit(0); } }