augment-properties: Fix debug messages and statement bracketing.
[pulseaudio-mirror.git] / src / modules / module-augment-properties.c
blob30df69d8173ee4534dbf75a057652acfd509082a
1 /***
2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <sys/stat.h>
27 #include <dirent.h>
29 #include <pulse/xmalloc.h>
30 #include <pulse/volume.h>
31 #include <pulse/channelmap.h>
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>
41 #include "module-augment-properties-symdef.h"
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);
48 #define STAT_INTERVAL 30
49 #define MAX_CACHE_SIZE 50
51 static const char* const valid_modargs[] = {
52 NULL
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;
66 struct userdata {
67 pa_hashmap *cache;
68 pa_hook_slot *client_new_slot, *client_proplist_changed_slot;
71 static void rule_free(struct rule *r) {
72 pa_assert(r);
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);
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) {
92 struct rule *r = userdata;
93 pa_proplist *n;
95 if (!(n = pa_proplist_from_string(rvalue)))
96 return -1;
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;
104 return 0;
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) {
116 struct rule *r = userdata;
117 const char *state = NULL;
118 char *c;
120 while ((c = pa_split(rvalue, ";", &state))) {
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");
130 pa_xfree(c);
133 return 0;
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) {
145 return pa_streq(rvalue, "Application") ? 0 : -1;
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) {
157 return 0;
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 },
172 pa_bool_t found = FALSE;
174 pa_assert(r);
175 fn = pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP "%s.desktop", r->process_name);
177 if (stat(fn, &st) == 0)
178 found = TRUE;
179 else {
180 DIR *desktopfiles_dir;
181 struct dirent *dir;
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;
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);
196 if (stat(fn, &st) == 0) {
197 found = TRUE;
198 break;
201 closedir(desktopfiles_dir);
204 if (!found) {
205 r->good = FALSE;
206 pa_xfree(fn);
207 return;
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;
217 pa_log_debug("Found %s (which has been updated since we last checked).", fn);
218 } else
219 pa_log_debug("Found %s.", fn);
221 r->good = TRUE;
222 r->mtime = st.st_mtime;
223 pa_xfree(r->application_name);
224 pa_xfree(r->icon_name);
225 pa_xfree(r->role);
226 r->application_name = r->icon_name = r->role = NULL;
227 if (r->proplist)
228 pa_proplist_clear(r->proplist);
230 table[0].data = &r->application_name;
231 table[1].data = &r->icon_name;
233 if (pa_config_parse(fn, NULL, table, r) < 0)
234 pa_log_warn("Failed to parse .desktop file %s.", fn);
236 pa_xfree(fn);
239 static void apply_rule(struct rule *r, pa_proplist *p) {
240 pa_assert(r);
241 pa_assert(p);
243 if (!r->good)
244 return;
246 if (r->proplist)
247 pa_proplist_update(p, PA_UPDATE_MERGE, r->proplist);
249 if (r->icon_name)
250 if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME))
251 pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, r->icon_name);
253 if (r->application_name) {
254 const char *t;
256 t = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME);
258 if (!t || pa_streq(t, r->process_name))
259 pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, r->application_name);
262 if (r->role)
263 if (!pa_proplist_contains(p, PA_PROP_MEDIA_ROLE))
264 pa_proplist_sets(p, PA_PROP_MEDIA_ROLE, r->role);
267 static void make_room(pa_hashmap *cache) {
268 pa_assert(cache);
270 while (pa_hashmap_size(cache) >= MAX_CACHE_SIZE) {
271 struct rule *r;
273 pa_assert_se(r = pa_hashmap_steal_first(cache));
274 rule_free(r);
278 static pa_hook_result_t process(struct userdata *u, pa_proplist *p) {
279 struct rule *r;
280 time_t now;
281 const char *pn;
283 pa_assert(u);
284 pa_assert(p);
286 if (!(pn = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
287 return PA_HOOK_OK;
289 if (*pn == '.' || strchr(pn, '/'))
290 return PA_HOOK_OK;
292 time(&now);
294 pa_log_debug("Looking for .desktop file for %s", pn);
296 if ((r = pa_hashmap_get(u->cache, pn))) {
297 if (now-r->timestamp > STAT_INTERVAL) {
298 r->timestamp = now;
299 update_rule(r);
301 } else {
302 make_room(u->cache);
304 r = pa_xnew0(struct rule, 1);
305 r->process_name = pa_xstrdup(pn);
306 r->timestamp = now;
307 pa_hashmap_put(u->cache, r->process_name, r);
308 update_rule(r);
311 apply_rule(r, p);
312 return PA_HOOK_OK;
315 static pa_hook_result_t client_new_cb(pa_core *core, pa_client_new_data *data, struct userdata *u) {
316 pa_core_assert_ref(core);
317 pa_assert(data);
318 pa_assert(u);
320 return process(u, data->proplist);
323 static pa_hook_result_t client_proplist_changed_cb(pa_core *core, pa_client *client, struct userdata *u) {
324 pa_core_assert_ref(core);
325 pa_assert(client);
326 pa_assert(u);
328 return process(u, client->proplist);
331 int pa__init(pa_module *m) {
332 pa_modargs *ma = NULL;
333 struct userdata *u;
335 pa_assert(m);
337 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
338 pa_log("Failed to parse module arguments");
339 goto fail;
342 m->userdata = u = pa_xnew(struct userdata, 1);
344 u->cache = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
345 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);
346 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 pa_modargs_free(ma);
350 return 0;
352 fail:
353 pa__done(m);
355 if (ma)
356 pa_modargs_free(ma);
358 return -1;
361 void pa__done(pa_module *m) {
362 struct userdata* u;
364 pa_assert(m);
366 if (!(u = m->userdata))
367 return;
369 if (u->client_new_slot)
370 pa_hook_slot_free(u->client_new_slot);
371 if (u->client_proplist_changed_slot)
372 pa_hook_slot_free(u->client_proplist_changed_slot);
374 if (u->cache) {
375 struct rule *r;
377 while ((r = pa_hashmap_steal_first(u->cache)))
378 rule_free(r);
380 pa_hashmap_free(u->cache, NULL, NULL);
383 pa_xfree(u);