From 641899d4733b3f2d33e35a6c39902c006d8b7782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Desgualdo=20Pereira?= Date: Wed, 22 Oct 2025 16:27:08 -0300 Subject: [PATCH 1/4] Add an option to retrieve the current focused window title from dwlb --- dwlb.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dwlb.c b/dwlb.c index faab2d9..1572cd7 100644 --- a/dwlb.c +++ b/dwlb.c @@ -208,6 +208,8 @@ static uint32_t height, textpadding, buffer_scale; static bool run_display; +static char *last_logged_title = NULL; + #include "config.h" static void @@ -1219,6 +1221,7 @@ read_stdin(void) if (!(bar->window_title = strdup(wordend))) EDIE("strdup"); bar->redraw = true; + last_logged_title = strdup(wordend); } else if (!strcmp(wordbeg, "selmon")) { ADVANCE(); if ((val = atoi(wordbeg)) != bar->sel) { @@ -1543,6 +1546,12 @@ read_socket(void) if (!bar->hidden) hide_bar(bar); } + } else if (!strcmp(wordbeg, "printinfo")) { + FILE *f = fopen("/tmp/dwlb_info", "w"); + if (f) { + fprintf(f, "%s", last_logged_title ? last_logged_title : "(none)"); + fclose(f); + } } else if (!strcmp(wordbeg, "toggle-visibility")) { if (all) { wl_list_for_each(bar, &bar_list, link) @@ -1738,6 +1747,11 @@ main(int argc, char **argv) DIE("Option -hide requires an argument"); client_send_command(&sock_address, argv[i], "hide", NULL, target_socket); return 0; + } else if (!strcmp(argv[i], "-printinfo")) { + if (++i >= argc) + DIE("Option -printinfo requires an argument"); + client_send_command(&sock_address, argv[i], "printinfo", NULL, target_socket); + return 0; } else if (!strcmp(argv[i], "-toggle-visibility")) { if (++i >= argc) DIE("Option -toggle requires an argument"); From e0bf2d04832d4de76c784c32ef53c3434e244b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Desgualdo=20Pereira?= Date: Sun, 26 Oct 2025 08:19:28 -0300 Subject: [PATCH 2/4] Improved the -printinfo function, not it prints the title of the focused window. Also updated the help to inform user of the option --- dwlb.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dwlb.c b/dwlb.c index 1572cd7..3e19285 100644 --- a/dwlb.c +++ b/dwlb.c @@ -111,6 +111,7 @@ " -set-top [OUTPUT] draw bar at the top\n" \ " -set-bottom [OUTPUT] draw bar at the bottom\n" \ " -toggle-location [OUTPUT] toggle bar location\n" \ + " -printinfo [OUTPUT] print the title of focused window\n" \ "Other\n" \ " -v get version information\n" \ " -h view this help text\n" @@ -1751,6 +1752,27 @@ main(int argc, char **argv) if (++i >= argc) DIE("Option -printinfo requires an argument"); client_send_command(&sock_address, argv[i], "printinfo", NULL, target_socket); + int wait_for_file_ready(const char *path, int timeout_ms) { + struct stat st; + int waited = 0; + while (waited < timeout_ms) { + if (stat(path, &st) == 0 && st.st_size > 0) + return 0; // file exists and not empty + usleep(10); // sleep .01 ms + waited += 1; + } + return -1; // timeout + } + if (wait_for_file_ready("/tmp/dwlb_info", 8) == 0) { // wait up to 8ms + FILE *f = fopen("/tmp/dwlb_info", "r"); + int c; + while ((c = fgetc(f)) != EOF) + putchar(c); + fclose(f); + } else { + fprintf(stderr, "Timed out waiting for info\n"); + } + remove("/tmp/dwlb_info"); return 0; } else if (!strcmp(argv[i], "-toggle-visibility")) { if (++i >= argc) From bdfee2104ff43428e79ebfd4b35a074c06aa4ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Desgualdo=20Pereira?= Date: Sun, 26 Oct 2025 10:12:32 -0300 Subject: [PATCH 3/4] update the function to use the socket instead of a temp file --- dwlb.c | 56 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/dwlb.c b/dwlb.c index 3e19285..8359f20 100644 --- a/dwlb.c +++ b/dwlb.c @@ -1455,11 +1455,14 @@ read_socket(void) if ((cli_fd = accept(sock_fd, NULL, 0)) == -1) EDIE("accept"); ssize_t len = recv(cli_fd, sockbuf, sizeof sockbuf - 1, 0); - if (len == -1) + if (len == -1) { + close(cli_fd); EDIE("recv"); - close(cli_fd); - if (len == 0) + } + if (len == 0) { + close(cli_fd); return; + } sockbuf[len] = '\0'; char *wordbeg, *wordend; @@ -1489,6 +1492,7 @@ read_socket(void) } if (!all && !bar) + close(cli_fd); return; ADVANCE(); @@ -1548,10 +1552,10 @@ read_socket(void) hide_bar(bar); } } else if (!strcmp(wordbeg, "printinfo")) { - FILE *f = fopen("/tmp/dwlb_info", "w"); - if (f) { - fprintf(f, "%s", last_logged_title ? last_logged_title : "(none)"); - fclose(f); + const char *info = last_logged_title ? last_logged_title : "(none)"; + ssize_t len = strlen(info); + if (send(cli_fd, info, len, 0) != len) { + perror("send"); } } else if (!strcmp(wordbeg, "toggle-visibility")) { if (all) { @@ -1599,6 +1603,7 @@ read_socket(void) else set_bottom(bar); } + close(cli_fd); } } @@ -1751,28 +1756,23 @@ main(int argc, char **argv) } else if (!strcmp(argv[i], "-printinfo")) { if (++i >= argc) DIE("Option -printinfo requires an argument"); - client_send_command(&sock_address, argv[i], "printinfo", NULL, target_socket); - int wait_for_file_ready(const char *path, int timeout_ms) { - struct stat st; - int waited = 0; - while (waited < timeout_ms) { - if (stat(path, &st) == 0 && st.st_size > 0) - return 0; // file exists and not empty - usleep(10); // sleep .01 ms - waited += 1; - } - return -1; // timeout + int sockfd; + if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) + EDIE("socket"); + snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s/%s", socketdir, target_socket ? target_socket : "dwlb-0"); + if (connect(sockfd, (struct sockaddr *)&sock_address, sizeof(sock_address)) == -1) + EDIE("connect"); + char cmd[256]; + snprintf(cmd, sizeof(cmd), "%s printinfo", argv[i]); + if (send(sockfd, cmd, strlen(cmd), 0) == -1) + EDIE("send"); + char buf[1024]; + ssize_t len = recv(sockfd, buf, sizeof(buf) - 1, 0); + if (len > 0) { + buf[len] = '\0'; + printf("%s\n", buf); } - if (wait_for_file_ready("/tmp/dwlb_info", 8) == 0) { // wait up to 8ms - FILE *f = fopen("/tmp/dwlb_info", "r"); - int c; - while ((c = fgetc(f)) != EOF) - putchar(c); - fclose(f); - } else { - fprintf(stderr, "Timed out waiting for info\n"); - } - remove("/tmp/dwlb_info"); + close(sockfd); return 0; } else if (!strcmp(argv[i], "-toggle-visibility")) { if (++i >= argc) From 1149930689ecdd3b19c63d07b3782b30714f2be8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Desgualdo=20Pereira?= Date: Sun, 26 Oct 2025 10:42:24 -0300 Subject: [PATCH 4/4] fix hanging when tag is empty --- dwlb.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/dwlb.c b/dwlb.c index 8359f20..e7580f1 100644 --- a/dwlb.c +++ b/dwlb.c @@ -209,8 +209,6 @@ static uint32_t height, textpadding, buffer_scale; static bool run_display; -static char *last_logged_title = NULL; - #include "config.h" static void @@ -1222,7 +1220,6 @@ read_stdin(void) if (!(bar->window_title = strdup(wordend))) EDIE("strdup"); bar->redraw = true; - last_logged_title = strdup(wordend); } else if (!strcmp(wordbeg, "selmon")) { ADVANCE(); if ((val = atoi(wordbeg)) != bar->sel) { @@ -1552,11 +1549,10 @@ read_socket(void) hide_bar(bar); } } else if (!strcmp(wordbeg, "printinfo")) { - const char *info = last_logged_title ? last_logged_title : "(none)"; - ssize_t len = strlen(info); - if (send(cli_fd, info, len, 0) != len) { + const char *info = (bar->window_title && bar->window_title[0] != '\0') ? bar->window_title : "(none)"; + if (send(cli_fd, info, strlen(info), 0) == -1) { perror("send"); - } + } } else if (!strcmp(wordbeg, "toggle-visibility")) { if (all) { wl_list_for_each(bar, &bar_list, link)