allow user to set path of the fifo

Signed-off-by: Leonardo Hernández Hernández <leohdz172@protonmail.com>
This commit is contained in:
Leonardo Hernández Hernández 2022-02-27 14:50:01 -06:00 committed by Raphael Robatsch
parent ee2e709317
commit f7f684828d
2 changed files with 53 additions and 29 deletions

View file

@ -46,7 +46,8 @@ sudo ninja -C build install
You must start somebar using dwl's `-s` flag, e.g. `dwl -s somebar`. 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: The following commands are supported:
* `status TEXT`: Updates the status bar * `status TEXT`: Updates the status bar

View file

@ -229,10 +229,8 @@ void onReady()
wl_display_roundtrip(display); // wait for xdg_output names before we read stdin wl_display_roundtrip(display); // wait for xdg_output names before we read stdin
} }
void setupStatusFifo() bool createFifo(std::string path)
{ {
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); auto result = mkfifo(path.c_str(), 0666);
if (result == 0) { if (result == 0) {
auto fd = open(path.c_str(), O_CLOEXEC | O_NONBLOCK | O_RDONLY); auto fd = open(path.c_str(), O_CLOEXEC | O_NONBLOCK | O_RDONLY);
@ -252,10 +250,26 @@ void setupStatusFifo()
.fd = statusFifoFd, .fd = statusFifoFd,
.events = POLLIN, .events = POLLIN,
}); });
return; return true;
} else if (errno != EEXIST) { } else if (errno != EEXIST) {
diesys("mkfifo"); 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);
if (createFifo(path)) {
return;
}
} }
} }
@ -420,14 +434,18 @@ static const struct wl_registry_listener registry_listener = {
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int opt; int opt;
while ((opt = getopt(argc, argv, "chv")) != -1) { while ((opt = getopt(argc, argv, "chvs:")) != -1) {
switch (opt) { switch (opt) {
case 's':
statusFifoName = optarg;
break;
case 'h': 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(" -h: Show this help\n");
printf(" -v: Show somebar version\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(" -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"); printf("Otherwise, somebar will display itself.\n");
exit(0); exit(0);
case 'v': case 'v':
@ -437,9 +455,14 @@ int main(int argc, char* argv[])
if (optind >= argc) { if (optind >= argc) {
die("Expected command"); die("Expected command");
} }
auto path = std::string {getenv("XDG_RUNTIME_DIR")} + "/somebar-0"; std::string path;
int fd = open(path.c_str(), O_WRONLY | O_CLOEXEC); if (statusFifoName.empty()) {
if (fd < 0) { 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()); fprintf(stderr, "could not open %s: ", path.c_str());
perror(""); perror("");
exit(1); exit(1);
@ -450,7 +473,7 @@ int main(int argc, char* argv[])
str += argv[i]; str += argv[i];
} }
str += "\n"; str += "\n";
write(fd, str.c_str(), str.size()); write(statusFifoWriter, str.c_str(), str.size());
exit(0); exit(0);
} }
} }