gui: Make curses_active a bool.
[paraslash.git] / grab_client.c
1 /*
2  * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file grab_client.c Functions for grabbing the audio stream. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "filter.h"
18 #include "grab_client.h"
19 #include "audiod.h"
20 #include "error.h"
21 #include "string.h"
22 #include "fd.h"
23
24 /**
25  * How to handle blocking writes for the grab client fds.
26  */
27 enum grab_mode {
28         /** Ignore the data and do not write. */
29         GM_SLOPPY,
30         /** Write anyway (default). */
31         GM_AGGRESSIVE,
32         /** Close fd if write would block. */
33         GM_PEDANTIC,
34 };
35
36 /** Flags specified as arguments to the grab command. */
37 enum grab_flags {
38         /** Stop grabbing if audio file changes. */
39         GF_ONE_SHOT = 1,
40 };
41
42 /** Describes one active grab client. */
43 struct grab_client {
44         /* The value of the -p option. */
45         char *parent;
46         /* The value of the -n option. */
47         char *name;
48         /** The file descriptor to send the grabbed stream to. */
49         int fd;
50         /** See \ref grab_mode. */
51         enum grab_mode mode;
52         /** Flags given at the command line. */
53         enum grab_flags flags;
54         /** The point of the grab client's node in the buffer tree. */
55         struct btr_node *btrn;
56         /* The task of this grab client. */
57         struct task task;
58         /** Belongs to either the active or the inactive list. */
59         struct list_head node;
60 };
61
62 /* Grab clients that are attached to a btr node. */
63 static INITIALIZED_LIST_HEAD(active_grab_client_list);
64 /* Grab clients that are not currently attached any btr node. */
65 static INITIALIZED_LIST_HEAD(inactive_grab_client_list);
66
67 static int gc_write(struct grab_client *gc, char *buf, size_t len)
68 {
69         int ret = write_ok(gc->fd);
70
71         if (ret < 0)
72                 goto err;
73         if (ret == 0) { /* fd not ready */
74                 if (gc->mode == GM_PEDANTIC)
75                         goto err;
76                 if (gc->mode == GM_SLOPPY)
77                         return len;
78         }
79         ret = xwrite(gc->fd, buf, len);
80         if (ret < 0)
81                 goto err;
82         if (ret > 0)
83                 return ret;
84         if (ret == 0) {
85                 if (gc->mode == GM_PEDANTIC)
86                         goto err;
87                 if (gc->mode == GM_SLOPPY)
88                         return len;
89         }
90         return 0;
91 err:
92         return -E_GC_WRITE;
93 }
94
95 static void gc_pre_select(struct sched *s, struct task *t)
96 {
97         struct grab_client *gc = container_of(t, struct grab_client, task);
98         int ret = btr_node_status(gc->btrn, 0, BTR_NT_LEAF);
99
100         if (ret == 0)
101                 return;
102         if (ret < 0)
103                 sched_min_delay(s);
104         para_fd_set(gc->fd, &s->wfds, &s->max_fileno);
105 }
106
107 /*
108  * We need this forward declaration as post_select() needs
109  * activate_grab_client and vice versa.
110  */
111 static int gc_post_select(struct sched *s, struct task *t);
112
113 /**
114  * Move a grab client to the active list and start it.
115  *
116  * \param gc The grab client to activate.
117  */
118 static void gc_activate(struct grab_client *gc, struct sched *s)
119 {
120         struct btr_node *root = audiod_get_btr_root(), *parent;
121         char *name = gc->name? gc->name : "grab";
122
123         if (!root)
124                 return;
125         parent = btr_search_node(gc->parent, root);
126         if (!parent)
127                 return;
128         PARA_INFO_LOG("activating fd %d\n", gc->fd);
129         list_move(&gc->node, &active_grab_client_list);
130         gc->btrn = btr_new_node(&(struct btr_node_description)
131                 EMBRACE(.name = name, .parent = parent));
132         gc->task.pre_select = gc_pre_select;
133         gc->task.post_select = gc_post_select;
134         snprintf(gc->task.status, sizeof(gc->task.status) - 1, "%s", name);
135         gc->task.status[sizeof(gc->task.status) - 1] = '\0';
136         gc->task.error = 0;
137         register_task(s, &gc->task);
138 }
139
140 /**
141  * Activate inactive grab clients if possible.
142  *
143  * \param s Needed to schedule the grab client task.
144  *
145  * This is called from audiod.c when the current audio file changes. It loops
146  * over all inactive grab clients and checks each grab client's configuration
147  * to determine if the client in question wishes to grab the new stream.  If
148  * yes, this grab client is moved from the inactive to the active grab client list.
149  *
150  * This function also garbage collects all grab clients whose tasks have been
151  * unscheduled.
152  */
153 void activate_grab_clients(struct sched *s)
154 {
155         struct grab_client *gc, *tmp;
156
157         list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
158                 if (gc->fd < 0) {
159                         list_del(&gc->node);
160                         free(gc);
161                         continue;
162                 }
163                 gc_activate(gc, s);
164         }
165 }
166
167 static int gc_close(struct grab_client *gc, int err)
168 {
169         btr_remove_node(&gc->btrn);
170         PARA_INFO_LOG("closing gc: %s\n", para_strerror(-err));
171         list_move(&gc->node, &inactive_grab_client_list);
172         if (err == -E_GC_WRITE || (gc->flags & GF_ONE_SHOT)) {
173                 /*
174                  * We must not free the gc structure here as it contains ->task
175                  * which is still used because this function is called from
176                  * post_select().
177                  */
178                 close(gc->fd);
179                 gc->fd = -1;
180                 free(gc->parent);
181                 free(gc->name);
182                 return 1;
183         }
184         return 0;
185 }
186
187 static int gc_post_select(__a_unused struct sched *s, struct task *t)
188 {
189         struct grab_client *gc = container_of(t, struct grab_client, task);
190         struct btr_node *btrn = gc->btrn;
191         int ret;
192         size_t sz;
193         char *buf;
194
195         ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
196         if (ret == 0)
197                 return 0;
198         if (ret < 0)
199                 goto err;
200         sz = btr_next_buffer(btrn, &buf);
201         assert(sz != 0);
202         ret = gc_write(gc, buf, sz);
203         if (ret < 0)
204                 goto err;
205         if (ret > 0)
206                 btr_consume(btrn, ret);
207         return 0;
208 err:
209         gc_close(gc, ret);
210         return ret;
211 }
212
213 static int gc_check_args(int argc, char **argv, struct grab_client *gc)
214 {
215         int i;
216
217         for (i = 1; i < argc; i++) {
218                 const char *arg = argv[i];
219                 if (arg[0] != '-')
220                         break;
221                 if (!strcmp(arg, "--")) {
222                         i++;
223                         break;
224                 }
225                 if (!strncmp(arg, "-m", 2)) {
226                         if (*(arg + 3))
227                                 return -E_GC_SYNTAX;
228                         switch(*(arg + 2)) {
229                         case 's':
230                                 gc->mode = GM_SLOPPY;
231                                 continue;
232                         case 'a':
233                                 gc->mode = GM_AGGRESSIVE;
234                                 continue;
235                         case 'p':
236                                 gc->mode = GM_PEDANTIC;
237                                 continue;
238                         default:
239                                 return -E_GC_SYNTAX;
240                         }
241                 }
242                 if (!strcmp(arg, "-o")) {
243                         gc->flags |= GF_ONE_SHOT;
244                         continue;
245                 }
246                 if (!strncmp(arg, "-p=", 3)) {
247                         gc->parent = para_strdup(arg + 3);
248                         continue;
249                 }
250                 if (!strncmp(arg, "-n=", 3)) {
251                         gc->name = para_strdup(arg + 3);
252                         continue;
253                 }
254                 return -E_GC_SYNTAX;
255         }
256         if (i != argc)
257                 return -E_GC_SYNTAX;
258         return 1;
259 }
260
261 /**
262  * Check the command line options and allocate a grab_client structure.
263  *
264  * \param fd The file descriptor of the client.
265  * \param argc Argument count.
266  * \param argv Argument vector.
267  * \param s The scheduler to register the grab client task to.
268  *
269  * If the command line options given by \a argc and \a argv are valid.
270  * allocate a struct grab_client and initialize it with this valid
271  * configuration.
272  *
273  * If the new grab client can be added to an existing buffer tree, activate it.
274  * Otherwise, add it to the inactive list for later activation.
275  *
276  * \return Standard.
277  */
278 int grab_client_new(int fd, int argc, char **argv, struct sched *s)
279 {
280         int ret;
281         struct grab_client *gc = para_calloc(sizeof(struct grab_client));
282
283         ret = gc_check_args(argc, argv, gc);
284         if (ret < 0)
285                 goto err_out;
286         gc->fd = fd;
287         para_list_add(&gc->node, &inactive_grab_client_list);
288         gc_activate(gc, s);
289         return 1;
290 err_out:
291         free(gc);
292         return ret;
293 }