lirc: fix logic behind mute buttons
[profile/ivi/pulseaudio-panda.git] / 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     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     if (r->proplist)
76         pa_proplist_free(r->proplist);
77     pa_xfree(r);
78 }
79
80 static int parse_properties(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
81     struct rule *r = userdata;
82     pa_proplist *n;
83
84     if (!(n = pa_proplist_from_string(rvalue)))
85         return -1;
86
87     if (r->proplist) {
88         pa_proplist_update(r->proplist, PA_UPDATE_MERGE, n);
89         pa_proplist_free(n);
90     } else
91         r->proplist = n;
92
93     return 0;
94 }
95
96 static int check_type(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
97     return pa_streq(rvalue, "Application") ? 0 : -1;
98 }
99
100 static int catch_all(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata) {
101     return 0;
102 }
103
104 static void update_rule(struct rule *r) {
105     char *fn;
106     struct stat st;
107     static pa_config_item table[] = {
108         { "Name", pa_config_parse_string,              NULL, "Desktop Entry" },
109         { "Icon", pa_config_parse_string,              NULL, "Desktop Entry" },
110         { "Type", check_type,                          NULL, "Desktop Entry" },
111         { "X-PulseAudio-Properties", parse_properties, NULL, "Desktop Entry" },
112         { NULL,  catch_all, NULL, NULL },
113         { NULL, NULL, NULL, NULL },
114     };
115
116     pa_assert(r);
117     fn = pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP "%s.desktop", r->process_name);
118
119     if (stat(fn, &st) < 0) {
120         r->good = FALSE;
121         pa_xfree(fn);
122         return;
123     }
124
125     if (r->good && st.st_mtime == r->mtime) {
126         pa_xfree(fn);
127         return;
128     }
129
130     r->good = TRUE;
131     r->mtime = st.st_mtime;
132     pa_xfree(r->application_name);
133     pa_xfree(r->icon_name);
134     r->application_name = r->icon_name = NULL;
135     if (r->proplist)
136         pa_proplist_clear(r->proplist);
137
138     table[0].data = &r->application_name;
139     table[1].data = &r->icon_name;
140
141     if (pa_config_parse(fn, NULL, table, r) < 0)
142         pa_log_warn("Failed to parse .desktop file %s.", fn);
143
144     pa_xfree(fn);
145 }
146
147 static void apply_rule(struct rule *r, pa_proplist *p) {
148     pa_assert(r);
149     pa_assert(p);
150
151     if (!r->good)
152         return;
153
154     if (r->icon_name)
155         if (!pa_proplist_contains(p, PA_PROP_APPLICATION_ICON_NAME))
156             pa_proplist_sets(p, PA_PROP_APPLICATION_ICON_NAME, r->icon_name);
157
158     if (r->application_name) {
159         const char *t;
160
161         t = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME);
162
163         if (!t || pa_streq(t, r->process_name))
164             pa_proplist_sets(p, PA_PROP_APPLICATION_NAME, r->application_name);
165     }
166
167     if (r->proplist)
168         pa_proplist_update(p, PA_UPDATE_MERGE, r->proplist);
169 }
170
171 static void make_room(pa_hashmap *cache) {
172     pa_assert(cache);
173
174     while (pa_hashmap_size(cache) >= MAX_CACHE_SIZE) {
175         struct rule *r;
176
177         pa_assert_se(r = pa_hashmap_steal_first(cache));
178         rule_free(r);
179     }
180 }
181
182 static pa_hook_result_t process(struct userdata *u, pa_proplist *p) {
183     struct rule *r;
184     time_t now;
185     const char *pn;
186
187     pa_assert(u);
188     pa_assert(p);
189
190     if (!(pn = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
191         return PA_HOOK_OK;
192
193     if (*pn == '.' || strchr(pn, '/'))
194         return PA_HOOK_OK;
195
196     time(&now);
197
198     pa_log_debug("Looking for .desktop file for %s", pn);
199
200     if ((r = pa_hashmap_get(u->cache, pn))) {
201         if (now-r->timestamp > STAT_INTERVAL) {
202             r->timestamp = now;
203             update_rule(r);
204         }
205     } else {
206         make_room(u->cache);
207
208         r = pa_xnew0(struct rule, 1);
209         r->process_name = pa_xstrdup(pn);
210         r->timestamp = now;
211         pa_hashmap_put(u->cache, r->process_name, r);
212         update_rule(r);
213     }
214
215     apply_rule(r, p);
216     return PA_HOOK_OK;
217 }
218
219 static pa_hook_result_t client_new_cb(pa_core *core, pa_client_new_data *data, struct userdata *u) {
220     pa_core_assert_ref(core);
221     pa_assert(data);
222     pa_assert(u);
223
224     return process(u, data->proplist);
225 }
226
227 static pa_hook_result_t client_proplist_changed_cb(pa_core *core, pa_client *client, struct userdata *u) {
228     pa_core_assert_ref(core);
229     pa_assert(client);
230     pa_assert(u);
231
232     return process(u, client->proplist);
233 }
234
235 int pa__init(pa_module *m) {
236     pa_modargs *ma = NULL;
237     struct userdata *u;
238
239     pa_assert(m);
240
241     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
242         pa_log("Failed to parse module arguments");
243         goto fail;
244     }
245
246     m->userdata = u = pa_xnew(struct userdata, 1);
247
248     u->cache = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
249     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);
250     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);
251
252     pa_modargs_free(ma);
253
254     return 0;
255
256 fail:
257     pa__done(m);
258
259     if (ma)
260         pa_modargs_free(ma);
261
262     return  -1;
263 }
264
265 void pa__done(pa_module *m) {
266     struct userdata* u;
267
268     pa_assert(m);
269
270     if (!(u = m->userdata))
271         return;
272
273     if (u->client_new_slot)
274         pa_hook_slot_free(u->client_new_slot);
275     if (u->client_proplist_changed_slot)
276         pa_hook_slot_free(u->client_proplist_changed_slot);
277
278     if (u->cache) {
279         struct rule *r;
280
281         while ((r = pa_hashmap_steal_first(u->cache)))
282             rule_free(r);
283
284         pa_hashmap_free(u->cache, NULL, NULL);
285     }
286
287     pa_xfree(u);
288 }