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