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