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