gui: Output "xxx tag not set" for unset tags rather than empty strings.
[paraslash.git] / grab_client.c
1 /*
2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /**
8 * \file grab_client.c Functions for grabbing the stream at any position
9 * in a filter chain.
10 *
11 * \sa filter_chain filter_chain_info filter.
12 */
13
14 #include <regex.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17
18 #include "para.h"
19 #include "list.h"
20 #include "sched.h"
21 #include "ggo.h"
22 #include "filter.h"
23 #include "grab_client.h"
24 #include "audiod.h"
25 #include "error.h"
26 #include "string.h"
27 #include "fd.h"
28
29 /** Grab clients that are not yet attached to a filter node. */
30 struct list_head inactive_grab_client_list;
31
32 static int max_num_filters(void)
33 {
34 int i, ret = 0;
35
36 for (i = 0; audio_formats[i]; i++) {
37 PARA_INFO_LOG("%s filter chain length: %d\n", audio_formats[i],
38 num_filters(i));
39 ret = PARA_MAX(ret, num_filters(i));
40 }
41 PARA_INFO_LOG("maximal filter chain length: %d\n", ret);
42 return ret;
43 }
44
45 static struct filter_node *find_filter_node(int format, int filternum)
46 {
47 int i;
48
49 FOR_EACH_SLOT(i) {
50 struct slot_info *s = &slot[i];
51 if (s->format < 0 || !s->fc)
52 continue;
53 if (format >= 0 && s->format != format)
54 continue;
55 if (num_filters(i) <= filternum)
56 continue;
57 /* success */
58 return s->fc->filter_nodes + filternum;
59 }
60 return NULL;
61 }
62
63 static int gc_write(char *buf, size_t len, struct filter_callback *fcb)
64 {
65 struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
66 size_t written = 0;
67
68 while (written < len) {
69 int ret = write_ok(gc->fd);
70 if (ret < 0)
71 goto err;
72 if (ret == 0) { /* fd not ready */
73 if (gc->mode == GM_PEDANTIC)
74 goto err;
75 if (gc->mode == GM_SLOPPY)
76 return 1;
77 }
78 ret = write(gc->fd, buf + written, len - written);
79 if (ret < 0) {
80 if (errno != EAGAIN && errno != EINTR)
81 goto err;
82 if (gc->mode == GM_PEDANTIC)
83 goto err;
84 if (gc->mode == GM_SLOPPY)
85 return 1;
86 } else
87 written += ret;
88 }
89 return 1;
90 err:
91 gc->error = -E_GC_WRITE;
92 return -E_GC_WRITE;
93 }
94
95 static void add_inactive_gc(struct grab_client *gc)
96 {
97 PARA_INFO_LOG("adding grab client %p (fd %d) to inactive list\n",
98 gc, gc->fd);
99 para_list_add(&gc->node, &inactive_grab_client_list);
100 }
101
102 static void gc_close(struct filter_callback *fcb)
103 {
104 struct grab_client *gc = container_of(fcb, struct grab_client, fcb);
105
106 if ((gc->flags & GF_ONE_SHOT) || gc->error < 0) {
107 PARA_INFO_LOG("closing fd %d (grab client %p)\n", gc->fd, gc);
108 close(gc->fd);
109 free(gc);
110 return;
111 }
112 add_inactive_gc(gc);
113 }
114
115 /**
116 * Move a grab client from the inactive list to a filter node.
117 *
118 * \param gc The grab client to activate.
119 * \param fn The filter node \a gc gets attached to.
120 *
121 * \sa filter_node::callbacks, inactive_grab_client_list.
122 */
123 void activate_grab_client(struct grab_client *gc, struct filter_node *fn)
124 {
125 PARA_INFO_LOG("activating %p (fd %d, filter node: %p)\n", gc, gc->fd, fn);
126 list_del(&gc->node);
127 para_list_add(&gc->fcb.node, &fn->callbacks);
128 }
129
130 /**
131 * Activate inactive grab clients if possible.
132 *
133 * \param audio_format_num The number of the audio format of the new audio file.
134 * \param fc The filter chain containing the activated filters.
135 *
136 * This is called from audiod.c when the current audio file changes. It loops
137 * over all inactive grab clients and checks each grab client's configuration
138 * to determine if the client in question wishes to grab the new stream. If
139 * yes, this grab client is moved from the inactive grab client list to an
140 * appropriate filter_node.
141 *
142 * \sa filter_chain_info::filters, inactive_grab_client_list,
143 * activate_grab_client.
144 */
145 void activate_inactive_grab_clients(int audio_format_num,
146 struct filter_chain *fc)
147 {
148 struct grab_client *gc, *tmp;
149 struct filter_node *fn;
150
151 list_for_each_entry_safe(gc, tmp, &inactive_grab_client_list, node) {
152 if (gc->audio_format_num >= 0 && gc->audio_format_num !=
153 audio_format_num)
154 continue;
155 if (gc->filter_num >= num_filters(audio_format_num))
156 continue;
157 fn = fc->filter_nodes + gc->filter_num;
158 activate_grab_client(gc, fn);
159 }
160 }
161
162 static int check_gc_args(int argc, char **argv, struct grab_client *gc)
163 {
164 int i, ret;
165
166 gc->audio_format_num = -1; /* default: grab any audio format */
167 for (i = 1; i < argc; i++) {
168 const char *arg = argv[i];
169 if (arg[0] != '-')
170 break;
171 if (!strcmp(arg, "--")) {
172 i++;
173 break;
174 }
175 if (!strncmp(arg, "-n=", 3)) {
176 ret = para_atoi32(arg + 3, &gc->filter_num);
177 if (ret < 0)
178 return ret;
179 if (gc->filter_num < 0)
180 return -E_BAD_GC_FILTER_NUM;
181 if (gc->filter_num >= max_num_filters())
182 return -E_BAD_GC_FILTER_NUM;
183 continue;
184 }
185 if (!strncmp(arg, "-m", 2)) {
186 if (*(arg + 3))
187 return -E_GC_SYNTAX;
188 switch(*(arg + 2)) {
189 case 's':
190 gc->mode = GM_SLOPPY;
191 continue;
192 case 'a':
193 gc->mode = GM_AGGRESSIVE;
194 continue;
195 case 'p':
196 gc->mode = GM_PEDANTIC;
197 continue;
198 default:
199 return -E_GC_SYNTAX;
200 }
201 }
202 if (!strcmp(arg, "-i")) {
203 gc->flags |= GF_INPUT_GRAB;
204 continue;
205 }
206 if (!strcmp(arg, "-o")) {
207 gc->flags |= GF_ONE_SHOT;
208 continue;
209 }
210 if (!strncmp(arg, "-f=", 3)) {
211 ret = get_audio_format_num(arg + 3);
212 if (ret < 0)
213 return ret;
214 gc->audio_format_num = ret;
215 continue;
216 }
217 return -E_GC_SYNTAX;
218 }
219 if (i != argc)
220 return -E_GC_SYNTAX;
221 return 1;
222 }
223
224 /**
225 * Check the command line options and allocate a grab_client structure.
226 *
227 * \param fd The file descriptor of the client.
228 * \param argc Argument count.
229 * \param argv Argument vector.
230 *
231 * If the command line options given by \a argc and \a argv are valid.
232 * allocate a struct grab_client and initialize it with this valid
233 * configuration. Moreover, add the new grab client to the inactive list.
234 *
235 * \return Standard.
236 *
237 * \sa grab_client, inactive_grab_client_list, activate_grab_client,
238 * filter_node::callbacks.
239 */
240 int grab_client_new(int fd, int argc, char **argv)
241 {
242 int ret;
243 struct grab_client *gc = para_calloc(sizeof(struct grab_client));
244 struct filter_node *fn;
245
246 ret = check_gc_args(argc, argv, gc);
247 if (ret < 0)
248 goto err_out;
249 if (gc->flags & GF_INPUT_GRAB)
250 gc->fcb.input_cb = gc_write;
251 else
252 gc->fcb.output_cb = gc_write;
253 gc->fd = fd;
254 gc->fcb.close = gc_close;
255 fn = find_filter_node(gc->audio_format_num, gc->filter_num);
256 if (fn)
257 para_list_add(&gc->fcb.node, &fn->callbacks);
258 else
259 add_inactive_gc(gc);
260 return 1;
261 err_out:
262 free(gc);
263 return ret;
264 }
265
266 /**
267 * Initialize the grabbing subsystem.
268 *
269 * This has to be called once during startup before any other function from
270 * grab_client.c may be used. It initializes \a inactive_grab_client_list.
271 */
272 void init_grabbing(void)
273 {
274 PARA_INFO_LOG("grab init\n");
275 INIT_LIST_HEAD(&inactive_grab_client_list);
276 }