update the function to use the socket instead of a temp file

This commit is contained in:
André Desgualdo Pereira 2025-10-26 10:12:32 -03:00
parent e0bf2d0483
commit bdfee2104f

56
dwlb.c
View File

@ -1455,11 +1455,14 @@ read_socket(void)
if ((cli_fd = accept(sock_fd, NULL, 0)) == -1) if ((cli_fd = accept(sock_fd, NULL, 0)) == -1)
EDIE("accept"); EDIE("accept");
ssize_t len = recv(cli_fd, sockbuf, sizeof sockbuf - 1, 0); ssize_t len = recv(cli_fd, sockbuf, sizeof sockbuf - 1, 0);
if (len == -1) if (len == -1) {
close(cli_fd);
EDIE("recv"); EDIE("recv");
close(cli_fd); }
if (len == 0) if (len == 0) {
close(cli_fd);
return; return;
}
sockbuf[len] = '\0'; sockbuf[len] = '\0';
char *wordbeg, *wordend; char *wordbeg, *wordend;
@ -1489,6 +1492,7 @@ read_socket(void)
} }
if (!all && !bar) if (!all && !bar)
close(cli_fd);
return; return;
ADVANCE(); ADVANCE();
@ -1548,10 +1552,10 @@ read_socket(void)
hide_bar(bar); hide_bar(bar);
} }
} else if (!strcmp(wordbeg, "printinfo")) { } else if (!strcmp(wordbeg, "printinfo")) {
FILE *f = fopen("/tmp/dwlb_info", "w"); const char *info = last_logged_title ? last_logged_title : "(none)";
if (f) { ssize_t len = strlen(info);
fprintf(f, "%s", last_logged_title ? last_logged_title : "(none)"); if (send(cli_fd, info, len, 0) != len) {
fclose(f); perror("send");
} }
} else if (!strcmp(wordbeg, "toggle-visibility")) { } else if (!strcmp(wordbeg, "toggle-visibility")) {
if (all) { if (all) {
@ -1599,6 +1603,7 @@ read_socket(void)
else else
set_bottom(bar); set_bottom(bar);
} }
close(cli_fd);
} }
} }
@ -1751,28 +1756,23 @@ main(int argc, char **argv)
} else if (!strcmp(argv[i], "-printinfo")) { } else if (!strcmp(argv[i], "-printinfo")) {
if (++i >= argc) if (++i >= argc)
DIE("Option -printinfo requires an argument"); DIE("Option -printinfo requires an argument");
client_send_command(&sock_address, argv[i], "printinfo", NULL, target_socket); int sockfd;
int wait_for_file_ready(const char *path, int timeout_ms) { if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
struct stat st; EDIE("socket");
int waited = 0; snprintf(sock_address.sun_path, sizeof(sock_address.sun_path), "%s/%s", socketdir, target_socket ? target_socket : "dwlb-0");
while (waited < timeout_ms) { if (connect(sockfd, (struct sockaddr *)&sock_address, sizeof(sock_address)) == -1)
if (stat(path, &st) == 0 && st.st_size > 0) EDIE("connect");
return 0; // file exists and not empty char cmd[256];
usleep(10); // sleep .01 ms snprintf(cmd, sizeof(cmd), "%s printinfo", argv[i]);
waited += 1; if (send(sockfd, cmd, strlen(cmd), 0) == -1)
} EDIE("send");
return -1; // timeout 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 close(sockfd);
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; return 0;
} else if (!strcmp(argv[i], "-toggle-visibility")) { } else if (!strcmp(argv[i], "-toggle-visibility")) {
if (++i >= argc) if (++i >= argc)