]> git.lizzy.rs Git - bspwm.git/commitdiff
Add an option to *subscribe*: --fifo
authorBastien Dejean <nihilhill@gmail.com>
Mon, 24 Jul 2017 18:28:22 +0000 (20:28 +0200)
committerBastien Dejean <nihilhill@gmail.com>
Mon, 24 Jul 2017 18:28:22 +0000 (20:28 +0200)
doc/bspwm.1
doc/bspwm.1.asciidoc
src/helpers.c
src/helpers.h
src/messages.c
src/messages.h
src/subscribe.c
src/subscribe.h
src/types.h

index a06f995095b53241dd529f707b652445e2528910..b2e468074d321d0020d6794bf253c5bc72b321c9 100644 (file)
@@ -2,12 +2,12 @@
 .\"     Title: bspwm
 .\"    Author: [see the "Author" section]
 .\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
-.\"      Date: 07/23/2017
+.\"      Date: 07/24/2017
 .\"    Manual: Bspwm Manual
-.\"    Source: Bspwm 0.9.3-7-gc968f7a
+.\"    Source: Bspwm 0.9.3-9-g9ce48c4
 .\"  Language: English
 .\"
-.TH "BSPWM" "1" "07/23/2017" "Bspwm 0\&.9\&.3\-7\-gc968f7a" "Bspwm Manual"
+.TH "BSPWM" "1" "07/24/2017" "Bspwm 0\&.9\&.3\-9\-g9ce48c4" "Bspwm Manual"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
@@ -975,9 +975,9 @@ Get or set the value of <setting>\&.
 .PP
 subscribe [\fIOPTIONS\fR] (all|report|monitor|desktop|node|\&...)*
 .RS 4
-Continuously print status information\&. See the
+Continuously print events\&. See the
 \fBEVENTS\fR
-section for the detailed description of each event\&.
+section for the description of each event\&.
 .RE
 .RE
 .sp
@@ -989,6 +989,11 @@ section for the detailed description of each event\&.
 \fBOptions\fR
 .RS 4
 .PP
+\fB\-f\fR, \fB\-\-fifo\fR
+.RS 4
+Print a path to a FIFO from which events can be read and return\&.
+.RE
+.PP
 \fB\-c\fR, \fB\-\-count\fR \fICOUNT\fR
 .RS 4
 Stop the corresponding
index 9b0b451b29343ab05e8ef37b60e50be36c4a8ac1..420208bc00df255fac41be200946fca55a0f0195 100644 (file)
@@ -575,11 +575,14 @@ Subscribe
 General Syntax
 ^^^^^^^^^^^^^^
 subscribe ['OPTIONS'] (all|report|monitor|desktop|node|...)*::
-       Continuously print status information. See the *EVENTS* section for the detailed description of each event.
+       Continuously print events. See the *EVENTS* section for the description of each event.
 
 Options
 ^^^^^^^
 
+*-f*, *--fifo*::
+       Print a path to a FIFO from which events can be read and return.
+
 *-c*, *--count* 'COUNT'::
        Stop the corresponding *bspc* process after having received 'COUNT' events.
 
index 39d0a4907ed14798a0dfebdbdee99f30cbb9d3ff..9b6a7a58de5ef19804752142e1b4325c0d169ec9 100644 (file)
@@ -119,6 +119,24 @@ char *copy_string(char *str, size_t len)
        return cpy;
 }
 
+char *mktempfifo(const char *template)
+{
+       char *fifo_path = malloc(strlen(template)+1);
+       if (fifo_path == NULL) {
+               return NULL;
+       }
+       sprintf(fifo_path, "%s", template);
+       if (mktemp(fifo_path) == NULL) {
+               free(fifo_path);
+               return NULL;
+       }
+       if (mkfifo(fifo_path, 0666) == -1) {
+               free(fifo_path);
+               return NULL;
+       }
+       return fifo_path;
+}
+
 /* Adapted from i3wm */
 uint32_t get_color_pixel(const char *color)
 {
index 0f82c329df095fa6142d54647de426b2323369ab..ff776427e2c4f4375dc9ba7559dc918058141369 100644 (file)
@@ -79,6 +79,7 @@ void warn(char *fmt, ...);
 void err(char *fmt, ...);
 char *read_string(const char *file_path, size_t *tlen);
 char *copy_string(char *str, size_t len);
+char *mktempfifo(const char *template) ;
 uint32_t get_color_pixel(const char *color);
 bool is_hex_color(const char *color);
 
index 3bb7206191a2539143934829aa61393ab7fbc3ad..0994ad36816d7e440d41ed83d37303636353025f 100644 (file)
@@ -110,7 +110,7 @@ void process_message(char **args, int num, FILE *rsp)
 
        fflush(rsp);
 
-       if (ret != SUBSCRIBE_SUCCESS) {
+       if (ret != SUBSCRIBE_SOCKET) {
                fclose(rsp);
        }
 }
@@ -1214,6 +1214,8 @@ int cmd_subscribe(char **args, int num, FILE *rsp)
 {
        int field = 0;
        int count = -1;
+       FILE *stream = rsp;
+       char *fifo_path = NULL;
 
        if (num < 1) {
                field = SBSC_MASK_REPORT;
@@ -1230,6 +1232,14 @@ int cmd_subscribe(char **args, int num, FILE *rsp)
                                        fail(rsp, "subscribe %s: Invalid argument: '%s'.\n", *(args - 1), *args);
                                        return SUBSCRIBE_FAILURE;
                                }
+                       } else if (streq("-f", *args) || streq("--fifo", *args)) {
+                               fifo_path = mktempfifo(FIFO_TEMPLATE);
+                               if (fifo_path == NULL) {
+                                       fail(rsp, "subscribe %s: Can't create FIFO.\n", *(args - 1));
+                                       return SUBSCRIBE_FAILURE;
+                               }
+                               stream = NULL;
+                               fprintf(rsp, "%s\n", fifo_path);
                        } else if (parse_subscriber_mask(*args, &mask)) {
                                field |= mask;
                        } else {
@@ -1240,8 +1250,13 @@ int cmd_subscribe(char **args, int num, FILE *rsp)
                }
        }
 
-       add_subscriber(rsp, field, count);
-       return SUBSCRIBE_SUCCESS;
+       add_subscriber(stream, fifo_path, field, count);
+
+       if (stream != NULL) {
+               return SUBSCRIBE_SOCKET;
+       } else {
+               return SUBSCRIBE_FIFO;
+       }
 }
 
 void cmd_quit(char **args, int num, FILE *rsp)
index a80ff714b99c0b9c26173ac1684910ed32a73439..e6a54ffbbbcae8bfef26af436b11441fd0b4409a 100644 (file)
@@ -29,7 +29,8 @@
 #include "subscribe.h"
 
 enum {
-       SUBSCRIBE_SUCCESS,
+       SUBSCRIBE_SOCKET,
+       SUBSCRIBE_FIFO,
        SUBSCRIBE_FAILURE
 };
 
index 4a8c961eeb6a757ca551eb285c4135c428a6bf4e..5e8037c386760e0049900bddc6af4643c26583c2 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <ctype.h>
 #include <stdarg.h>
 #include "bspwm.h"
 #include "settings.h"
 #include "subscribe.h"
 
-subscriber_list_t *make_subscriber_list(FILE *stream, int field, int count)
+subscriber_list_t *make_subscriber_list(FILE *stream, char *fifo_path, int field, int count)
 {
        subscriber_list_t *sb = calloc(1, sizeof(subscriber_list_t));
        sb->prev = sb->next = NULL;
        sb->stream = stream;
+       sb->fifo_path = fifo_path;
        sb->field = field;
        sb->count = count;
        return sb;
@@ -61,12 +63,14 @@ void remove_subscriber(subscriber_list_t *sb)
                subscribe_tail = a;
        }
        fclose(sb->stream);
+       unlink(sb->fifo_path);
+       free(sb->fifo_path);
        free(sb);
 }
 
-void add_subscriber(FILE *stream, int field, int count)
+void add_subscriber(FILE *stream, char* fifo_path, int field, int count)
 {
-       subscriber_list_t *sb = make_subscriber_list(stream, field, count);
+       subscriber_list_t *sb = make_subscriber_list(stream, fifo_path, field, count);
        if (subscribe_head == NULL) {
                subscribe_head = subscribe_tail = sb;
        } else {
@@ -133,6 +137,9 @@ void put_status(subscriber_mask_t mask, ...)
                        if (sb->count > 0) {
                                sb->count--;
                        }
+                       if (sb->stream == NULL) {
+                               sb->stream = fopen(sb->fifo_path, "w");
+                       }
                        if (mask == SBSC_MASK_REPORT) {
                                ret = print_report(sb->stream);
                        } else {
index 26771be0e38a2deff670a4ef243453fadcc114a8..167e9d3537860db4b84d6c9a334c7638317a022b 100644 (file)
@@ -25,6 +25,8 @@
 #ifndef BSPWM_SUBSCRIBE_H
 #define BSPWM_SUBSCRIBE_H
 
+#define FIFO_TEMPLATE  "/tmp/bspwm_fifo.XXXXX"
+
 typedef enum {
        SBSC_MASK_REPORT = 1 << 0,
        SBSC_MASK_MONITOR_ADD = 1 << 1,
@@ -60,9 +62,9 @@ typedef enum {
        SBSC_MASK_ALL = (1 << 28) - 1
 } subscriber_mask_t;
 
-subscriber_list_t *make_subscriber_list(FILE *stream, int field, int count);
+subscriber_list_t *make_subscriber_list(FILE *stream, char *fifo_path, int field, int count);
 void remove_subscriber(subscriber_list_t *sb);
-void add_subscriber(FILE *stream, int field, int count);
+void add_subscriber(FILE *stream, char* fifo_path, int field, int count);
 int print_report(FILE *stream);
 void put_status(subscriber_mask_t mask, ...);
 
index 3321c0838bb3eb93a4d4c6169b879a848f75ace0..6fa56c9ec97e60f3e16346fb6c7dea2d01b58a9d 100644 (file)
@@ -303,6 +303,7 @@ typedef struct subscriber_list_t subscriber_list_t;
 struct subscriber_list_t {
        int fd;
        FILE *stream;
+       char* fifo_path;
        int field;
        int count;
        subscriber_list_t *prev;