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)
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)