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