]> code.delx.au - pulseaudio/blob - src/modules/module-augment-properties.c
Fix up according to Coding Style
[pulseaudio] / src / modules / module-augment-properties.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/stat.h>
27 #include <dirent.h>
28 #include <time.h>
29
30 #include <pulse/xmalloc.h>
31 #include <pulse/volume.h>
32 #include <pulse/channelmap.h>
33
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/modargs.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/client.h>
40 #include <pulsecore/conf-parser.h>
41
42 #include "module-augment-properties-symdef.h"
43
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("Augment the property sets of streams with additional static information");
46 PA_MODULE_VERSION(PACKAGE_VERSION);
47 PA_MODULE_LOAD_ONCE(TRUE);
48
49 #define STAT_INTERVAL 30
50 #define MAX_CACHE_SIZE 50
51
52 static const char* const valid_modargs[] = {
53 NULL
54 };
55
56 struct rule {
57 time_t timestamp;
58 pa_bool_t good;
59 time_t mtime;
60 char *process_name;
61 char *application_name;
62 char *icon_name;
63 char *role;
64 pa_proplist *proplist;
65 };
66
67 struct userdata {
68 pa_hashmap *cache;
69 pa_hook_slot *client_new_slot, *client_proplist_changed_slot;
70 };
71
72 static void rule_free(struct rule *r) {
73 pa_assert(r);
74
75 pa_xfree(r->process_name);
76 pa_xfree(r->application_name);
77 pa_xfree(r->icon_name);
78 pa_xfree(r->role);
79 if (r->proplist)
80 pa_proplist_free(r->proplist);
81 pa_xfree(r);
82 }
83
84 static int parse_properties(
85 const char *filename,
86 unsigned line,
87 const char *section,
88 const char *lvalue,
89 const char *rvalue,
90 void *data,
91 void *userdata) {
92
93 struct rule *r = userdata;
94 pa_proplist *n;
95
96 if (!(n = pa_proplist_from_string(rvalue)))
97 return -1;
98
99 if (r->proplist) {
100 pa_proplist_update(r->proplist, PA_UPDATE_MERGE, n);
101 pa_proplist_free(n);
102 } else
103 r->proplist = n;
104
105 return 0;
106 }
107
108 static int parse_categories(
109 const char *filename,
110 unsigned line,
111 const char *section,
112 const char *lvalue,
113 const char *rvalue,
114 void *data,
115 void *userdata) {
116
117 struct rule *r = userdata;
118 const char *state = NULL;
119 char *c;
120
121 while ((c = pa_split(rvalue, ";", &state))) {
122
123 if (pa_streq(c, "Game")) {
124 pa_xfree(r->role);
125 r->role = pa_xstrdup("game");
126 } else if (pa_streq(c, "Telephony")) {
127 pa_xfree(r->role);
128 r->role = pa_xstrdup("phone");
129 }
130
131 pa_xfree(c);
132 }
133
134 return 0;
135 }
136
137 static int check_type(
138 const char *filename,
139 unsigned line,
140 const char *section,
141 const char *lvalue,
142 const char *rvalue,
143 void *data,
144 void *userdata) {
145
146 return pa_streq(rvalue, "Application") ? 0 : -1;
147 }
148
149 static int catch_all(
150 const char *filename,
151 unsigned line,
152 const char *section,
153 const char *lvalue,
154 const char *rvalue,
155 void *data,
156 void *userdata) {
157
158 return 0;
159 }
160
161 static void update_rule(struct rule *r) {
162 char *fn;
163 struct stat st;
164 static pa_config_item table[] = {
165 { "Name", pa_config_parse_string, NULL, "Desktop Entry" },
166 { "Icon", pa_config_parse_string, NULL, "Desktop Entry" },
167 { "Type", check_type, NULL, "Desktop Entry" },
168 { "X-PulseAudio-Properties", parse_properties, NULL, "Desktop Entry" },
169 { "Categories", parse_categories, NULL, "Desktop Entry" },
170 { NULL, catch_all, NULL, NULL },
171 { NULL, NULL, NULL, NULL },
172 };
173 pa_bool_t found = FALSE;
174
175 pa_assert(r);
176 fn = pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP "%s.desktop", r->process_name);
177
178 if (stat(fn, &st) == 0)
179 found = TRUE;
180 else {
181 DIR *desktopfiles_dir;
182 struct dirent *dir;
183
184 #ifdef DT_DIR
185 /* Let's try a more aggressive search, but only one level */
186 if ((desktopfiles_dir = opendir(DESKTOPFILEDIR))) {
187 while ((dir = readdir(desktopfiles_dir))) {
188 if (dir->d_type != DT_DIR
189 || strcmp(dir->d_name, ".") == 0
190 || strcmp(dir->d_name, "..") == 0)
191 continue;
192
193 pa_xfree(fn);
194 fn = pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP "%s" PA_PATH_SEP "%s.desktop", dir->d_name, r->process_name);
195
196 if (stat(fn, &st) == 0) {
197 found = TRUE;
198 break;
199 }
200 }
201 closedir(desktopfiles_dir);
202 }
203 #endif
204 }
205 if (!found) {
206 r->good = FALSE;
207 pa_xfree(fn);
208 return;
209 }
210
211 if (r->good) {
212 if (st.st_mtime == r->mtime) {
213 /* Theoretically the filename could have changed, but if so
214 having the same mtime is very unlikely so not worth tracking it in r */
215 pa_xfree(fn);
216 return;
217 }
218 pa_log_debug("Found %s (which has been updated since we last checked).", fn);
219 } else
220 pa_log_debug("Found %s.", fn);
221
222 r->good = TRUE;
223 r->mtime = st.st_mtime;
224 pa_xfree(r->application_name);
225 pa_xfree(r->icon_name);
226 pa_xfree(r->role);
227 r->application_name = r->icon_name = r->role = NULL;
228 if (r->proplist)
229 pa_proplist_clear(r->proplist);
230
231 table[0].data = &r->application_name;
232 table[1].data = &r->icon_name;
233
234 if (pa_config_parse(fn, NULL, table, r) < 0)
235 pa_log_warn("Failed to parse .desktop file %s.", fn);
236
237 pa_xfree(fn);
238 }
239
240 static void apply_rule(struct rule *r, pa_proplist *p) {
241 pa_assert(r);
242 pa_assert(p);
243
244 if (!r->good)
245 return;
246
247 if (r->proplist)
248 pa_proplist_update(p, PA_UPDATE_MERGE, r->proplist);
249
250 if (r->icon_name)
251 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME))
252 pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, r->icon_name);
253
254 if (r->application_name) {
255 const char *t;
256
257 t = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME);
258
259 if (!t || pa_streq(t, r->process_name))
260 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, r->application_name);
261 }
262
263 if (r->role)
264 if (!pa_proplist_contains(p, PA_PROP_MEDIA_ROLE))
265 pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, r->role);
266 }
267
268 static void make_room(pa_hashmap *cache) {
269 pa_assert(cache);
270
271 while (pa_hashmap_size(cache) >= MAX_CACHE_SIZE) {
272 struct rule *r;
273
274 pa_assert_se(r = pa_hashmap_steal_first(cache));
275 rule_free(r);
276 }
277 }
278
279 static pa_hook_result_t process(struct userdata *u, pa_proplist *p) {
280 struct rule *r;
281 time_t now;
282 const char *pn;
283
284 pa_assert(u);
285 pa_assert(p);
286
287 if (!(pn = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
288 return PA_HOOK_OK;
289
290 if (*pn == '.' || strchr(pn, '/'))
291 return PA_HOOK_OK;
292
293 time(&now);
294
295 pa_log_debug("Looking for .desktop file for %s", pn);
296
297 if ((r = pa_hashmap_get(u->cache, pn))) {
298 if (now-r->timestamp > STAT_INTERVAL) {
299 r->timestamp = now;
300 update_rule(r);
301 }
302 } else {
303 make_room(u->cache);
304
305 r = pa_xnew0(struct rule, 1);
306 r->process_name = pa_xstrdup(pn);
307 r->timestamp = now;
308 pa_hashmap_put(u->cache, r->process_name, r);
309 update_rule(r);
310 }
311
312 apply_rule(r, p);
313 return PA_HOOK_OK;
314 }
315
316 static pa_hook_result_t client_new_cb(pa_core *core, pa_client_new_data *data, struct userdata *u) {
317 pa_core_assert_ref(core);
318 pa_assert(data);
319 pa_assert(u);
320
321 return process(u, data->proplist);
322 }
323
324 static pa_hook_result_t client_proplist_changed_cb(pa_core *core, pa_client *client, struct userdata *u) {
325 pa_core_assert_ref(core);
326 pa_assert(client);
327 pa_assert(u);
328
329 return process(u, client->proplist);
330 }
331
332 int pa__init(pa_module *m) {
333 pa_modargs *ma = NULL;
334 struct userdata *u;
335
336 pa_assert(m);
337
338 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
339 pa_log("Failed to parse module arguments");
340 goto fail;
341 }
342
343 m->userdata = u = pa_xnew(struct userdata, 1);
344
345 u->cache = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
346 u->client_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CLIENT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) client_new_cb, u);
347 u->client_proplist_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_CLIENT_PROPLIST_CHANGED], PA_HOOK_EARLY, (pa_hook_cb_t) client_proplist_changed_cb, u);
348
349 pa_modargs_free(ma);
350
351 return 0;
352
353 fail:
354 pa__done(m);
355
356 if (ma)
357 pa_modargs_free(ma);
358
359 return -1;
360 }
361
362 void pa__done(pa_module *m) {
363 struct userdata* u;
364
365 pa_assert(m);
366
367 if (!(u = m->userdata))
368 return;
369
370 if (u->client_new_slot)
371 pa_hook_slot_free(u->client_new_slot);
372 if (u->client_proplist_changed_slot)
373 pa_hook_slot_free(u->client_proplist_changed_slot);
374
375 if (u->cache) {
376 struct rule *r;
377
378 while ((r = pa_hashmap_steal_first(u->cache)))
379 rule_free(r);
380
381 pa_hashmap_free(u->cache, NULL, NULL);
382 }
383
384 pa_xfree(u);
385 }