]> git.lizzy.rs Git - bspwm.git/blob - subscribe.c
Mention the default reference
[bspwm.git] / subscribe.c
1 /* Copyright (c) 2012, Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  *    list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  *    this list of conditions and the following disclaimer in the documentation
11  *    and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <stdarg.h>
29 #include "bspwm.h"
30 #include "desktop.h"
31 #include "settings.h"
32 #include "subscribe.h"
33
34 subscriber_list_t *make_subscriber_list(FILE *stream, int field)
35 {
36         subscriber_list_t *sb = malloc(sizeof(subscriber_list_t));
37         sb->prev = sb->next = NULL;
38         sb->stream = stream;
39         sb->field = field;
40         return sb;
41 }
42
43 void remove_subscriber(subscriber_list_t *sb)
44 {
45         if (sb == NULL) {
46                 return;
47         }
48         subscriber_list_t *a = sb->prev;
49         subscriber_list_t *b = sb->next;
50         if (a != NULL) {
51                 a->next = b;
52         }
53         if (b != NULL) {
54                 b->prev = a;
55         }
56         if (sb == subscribe_head) {
57                 subscribe_head = b;
58         }
59         if (sb == subscribe_tail) {
60                 subscribe_tail = a;
61         }
62         fclose(sb->stream);
63         free(sb);
64 }
65
66 void add_subscriber(FILE *stream, int field)
67 {
68         subscriber_list_t *sb = make_subscriber_list(stream, field);
69         if (subscribe_head == NULL) {
70                 subscribe_head = subscribe_tail = sb;
71         } else {
72                 subscribe_tail->next = sb;
73                 sb->prev = subscribe_tail;
74                 subscribe_tail = sb;
75         }
76         if (sb->field & SBSC_MASK_REPORT) {
77                 print_report(sb->stream);
78         }
79 }
80
81 int print_report(FILE *stream)
82 {
83         fprintf(stream, "%s", status_prefix);
84         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
85                 fprintf(stream, "%c%s", (mon == m ? 'M' : 'm'), m->name);
86                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
87                         char c = (is_urgent(d) ? 'u' : (d->root == NULL ? 'f' : 'o'));
88                         if (m->desk == d) {
89                                 c = toupper(c);
90                         }
91                         fprintf(stream, ":%c%s", c, d->name);
92                 }
93                 if (m->desk != NULL) {
94                         fprintf(stream, ":L%c", LAYOUT_CHR(m->desk->layout));
95                         if (m->desk->focus != NULL) {
96                                 node_t *n = m->desk->focus;
97                                 if (n->client != NULL) {
98                                         fprintf(stream, ":T%c", STATE_CHR(n->client->state));
99                                 } else {
100                                         fprintf(stream, ":T@");
101                                 }
102                                 int i = 0;
103                                 char flags[4];
104                                 if (n->sticky) {
105                                         flags[i++] = 'S';
106                                 }
107                                 if (n->private) {
108                                         flags[i++] = 'P';
109                                 }
110                                 if (n->locked) {
111                                         flags[i++] = 'L';
112                                 }
113                                 flags[i] = '\0';
114                                 fprintf(stream, ":G%s", flags);
115                         }
116                 }
117                 if (m != mon_tail) {
118                         fprintf(stream, "%s", ":");
119                 }
120         }
121         fprintf(stream, "%s", "\n");
122         return fflush(stream);
123 }
124
125 void put_status(subscriber_mask_t mask, ...)
126 {
127         subscriber_list_t *sb = subscribe_head;
128         int ret;
129         while (sb != NULL) {
130                 subscriber_list_t *next = sb->next;
131                 if (sb->field & mask) {
132                         if (mask == SBSC_MASK_REPORT) {
133                                 ret = print_report(sb->stream);
134                         } else {
135                                 char *fmt;
136                                 va_list args;
137                                 va_start(args, mask);
138                                 fmt = va_arg(args, char *);
139                                 vfprintf(sb->stream, fmt, args);
140                                 va_end(args);
141                                 ret = fflush(sb->stream);
142                         }
143                         if (ret != 0) {
144                                 remove_subscriber(sb);
145                         }
146                 }
147                 sb = next;
148         }
149 }