kolunmi / dwlb

Feature-Complete Bar for DWL
Other
64 stars 21 forks source link

[PATCH] Sending commands to specific instance #28

Closed DhruvaSambrani closed 12 months ago

DhruvaSambrani commented 12 months ago

DWM has the extrabar patch which allows you to have a bar at the top and bottom. Is it possible to do the same in DWMB too?

I tried to just start two dwlb instances, but the "client" sends the messages to all sockets. It should be trivial enough to add a param to send to a specific instance.

DhruvaSambrani commented 12 months ago
diff --git a/dwlb.c b/dwlb.c
index f512caa..ca59b0e 100644
--- a/dwlb.c
+++ b/dwlb.c
@@ -1565,7 +1565,7 @@ event_loop(void)

 static void
 client_send_command(struct sockaddr_un *sock_address, const char *output,
-           const char *cmd, const char *data)
+           const char *cmd, const char *data, const char *target_socket)
 {
    DIR *dir;
    if (!(dir = opendir(socketdir)))
@@ -1584,20 +1584,22 @@ client_send_command(struct sockaddr_un *sock_address, const char *output,
    /* Send data to all dwlb instances */
    while ((de = readdir(dir))) {
        if (!strncmp(de->d_name, "dwlb-", 5)) {
-           if (newfd && (sock_fd = socket(AF_UNIX, SOCK_STREAM, 1)) == -1)
-               EDIE("socket");
-           snprintf(sock_address->sun_path, sizeof sock_address->sun_path, "%s/%s", socketdir, de->d_name);
-           if (connect(sock_fd, (struct sockaddr *) sock_address, sizeof(*sock_address)) == -1) {
-               newfd = false;
-               continue;
+           if (!target_socket || !strncmp(de -> d_name, target_socket, 6)){
+               if (newfd && (sock_fd = socket(AF_UNIX, SOCK_STREAM, 1)) == -1)
+                   EDIE("socket");
+               snprintf(sock_address->sun_path, sizeof sock_address->sun_path, "%s/%s", socketdir, de->d_name);
+               if (connect(sock_fd, (struct sockaddr *) sock_address, sizeof(*sock_address)) == -1) {
+                   newfd = false;
+                   continue;
+               }
+               if (send(sock_fd, sockbuf, len, 0) == -1)
+                   fprintf(stderr, "Could not send status data to '%s'\n", sock_address->sun_path);
+               close(sock_fd);
+               newfd = true;
            }
-           if (send(sock_fd, sockbuf, len, 0) == -1)
-               fprintf(stderr, "Could not send status data to '%s'\n", sock_address->sun_path);
-           close(sock_fd);
-           newfd = true;
        }
    }
-           
+
    closedir(dir);
 }

@@ -1626,11 +1628,20 @@ main(int argc, char **argv)
    sock_address.sun_family = AF_UNIX;

    /* Parse options */
-   for (int i = 1; i < argc; i++) {
+   char *target_socket = NULL;
+   int i = 1;
+   if (argc > 1 && !strcmp(argv[1], "-target-socket")) {
+       if (2 >= argc) {
+           DIE("Option -socket requires an argument");
+       }
+       target_socket = argv[2];
+       i += 2;
+   }
+   for (; i < argc; i++) {
        if (!strcmp(argv[i], "-status")) {
            if (++i + 1 >= argc)
                DIE("Option -status requires two arguments");
-           client_send_command(&sock_address, argv[i], "status", argv[i + 1]);
+           client_send_command(&sock_address, argv[i], "status", argv[i + 1], target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-status-stdin")) {
            if (++i >= argc)
@@ -1638,44 +1649,44 @@ main(int argc, char **argv)
            char *status = malloc(TEXT_MAX * sizeof(char));
            while (fgets(status, TEXT_MAX-1, stdin)) {
                status[strlen(status)-1] = '\0';
-               client_send_command(&sock_address, argv[i], "status", status);
+               client_send_command(&sock_address, argv[i], "status", status, target_socket);
            }
            free(status);
            return 0;
        } else if (!strcmp(argv[i], "-title")) {
            if (++i + 1 >= argc)
                DIE("Option -title requires two arguments");
-           client_send_command(&sock_address, argv[i], "title", argv[i + 1]);
+           client_send_command(&sock_address, argv[i], "title", argv[i + 1], target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-show")) {
            if (++i >= argc)
                DIE("Option -show requires an argument");
-           client_send_command(&sock_address, argv[i], "show", NULL);
+           client_send_command(&sock_address, argv[i], "show", NULL, target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-hide")) {
            if (++i >= argc)
                DIE("Option -hide requires an argument");
-           client_send_command(&sock_address, argv[i], "hide", NULL);
+           client_send_command(&sock_address, argv[i], "hide", NULL, target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-toggle-visibility")) {
            if (++i >= argc)
                DIE("Option -toggle requires an argument");
-           client_send_command(&sock_address, argv[i], "toggle-visibility", NULL);
+           client_send_command(&sock_address, argv[i], "toggle-visibility", NULL, target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-set-top")) {
            if (++i >= argc)
                DIE("Option -set-top requires an argument");
-           client_send_command(&sock_address, argv[i], "set-top", NULL);
+           client_send_command(&sock_address, argv[i], "set-top", NULL, target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-set-bottom")) {
            if (++i >= argc)
                DIE("Option -set-bottom requires an argument");
-           client_send_command(&sock_address, argv[i], "set-bottom", NULL);
+           client_send_command(&sock_address, argv[i], "set-bottom", NULL, target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-toggle-location")) {
            if (++i >= argc)
                DIE("Option -toggle-location requires an argument");
-           client_send_command(&sock_address, argv[i], "toggle-location", NULL);
+           client_send_command(&sock_address, argv[i], "toggle-location", NULL, target_socket);
            return 0;
        } else if (!strcmp(argv[i], "-ipc")) {
            ipc = true;
DhruvaSambrani commented 12 months ago

Above patch only allows socket to be the first parameter. Currently sockets cannot be named at creation, only messages can be sent to the sockets. We should probably warn? documentation needs to be added still.

I can do all that and send in a PR if you approve

DhruvaSambrani commented 12 months ago

Additionally, we can add the following patch to allow for 0 tags

diff --git a/dwlb.c b/dwlb.c
index ca59b0e..e5f1d50 100644
--- a/dwlb.c
+++ b/dwlb.c
@@ -1765,10 +1765,10 @@ main(int argc, char **argv)
            if (parse_color(argv[i], &urgent_bg_color) == -1)
                DIE("malformed color string");
        } else if (!strcmp(argv[i], "-tags")) {
-           if (++i + 1 >= argc)
-               DIE("Option -tags requires at least two arguments");
+           if (++i >= argc)
+               DIE("Option -tags requires at least one argument");
            int v;
-           if ((v = atoi(argv[i])) <= 0 || i + v >= argc)
+           if ((v = atoi(argv[i])) < 0 || i + v >= argc)
                DIE("-tags: invalid arguments");
            if (tags) {
                for (uint32_t j = 0; j < tags_l; j++)
kolunmi commented 12 months ago

Hi @DhruvaSambrani ! Looks good to me. Go ahead and make two pull requests for the two features please.