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
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[] = {
60 char *application_name;
63 pa_proplist *proplist;
68 pa_hook_slot *client_new_slot, *client_proplist_changed_slot;
71 static void rule_free(struct rule *r) {
74 pa_xfree(r->process_name);
75 pa_xfree(r->application_name);
76 pa_xfree(r->icon_name);
79 pa_proplist_free(r->proplist);
83 static int parse_properties(
92 struct rule *r = userdata;
95 if (!(n = pa_proplist_from_string(rvalue)))
99 pa_proplist_update(r->proplist, PA_UPDATE_MERGE, n);
107 static int parse_categories(
108 const char *filename,
116 struct rule *r = userdata;
117 const char *state = NULL;
120 while ((c = pa_split(rvalue, ";", &state))) {
122 if (pa_streq(c, "Game")) {
124 r->role = pa_xstrdup("game");
125 } else if (pa_streq(c, "Telephony")) {
127 r->role = pa_xstrdup("phone");
136 static int check_type(
137 const char *filename,
145 return pa_streq(rvalue, "Application") ? 0 : -1;
148 static int catch_all(
149 const char *filename,
160 static void update_rule(struct rule *r) {
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;
175 fn = pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP "%s.desktop", r->process_name);
177 if (stat(fn, &st) == 0)
180 DIR *desktopfiles_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)
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) {
201 closedir(desktopfiles_dir);
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 */
217 pa_log_debug("Found %s (which has been updated since we last checked).", fn);
219 pa_log_debug("Found %s.", fn);
222 r->mtime = st.st_mtime;
223 pa_xfree(r->application_name);
224 pa_xfree(r->icon_name);
226 r->application_name = r->icon_name = r->role = NULL;
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);
239 static void apply_rule(struct rule *r, pa_proplist *p) {
247 pa_proplist_update(p, PA_UPDATE_MERGE, r->proplist);
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) {
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);
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) {
270 while (pa_hashmap_size(cache) >= MAX_CACHE_SIZE) {
273 pa_assert_se(r = pa_hashmap_steal_first(cache));
278 static pa_hook_result_t process(struct userdata *u, pa_proplist *p) {
286 if (!(pn = pa_proplist_gets(p, PA_PROP_APPLICATION_PROCESS_BINARY)))
289 if (*pn == '.' || strchr(pn, '/'))
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) {
304 r = pa_xnew0(struct rule, 1);
305 r->process_name = pa_xstrdup(pn);
307 pa_hashmap_put(u->cache, r->process_name, r);
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);
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);
328 return process(u, client->proplist);
331 int pa__init(pa_module *m) {
332 pa_modargs *ma = NULL;
337 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
338 pa_log("Failed to parse module arguments");
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);
361 void pa__done(pa_module *m) {
366 if (!(u = m->userdata))
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);
377 while ((r = pa_hashmap_steal_first(u->cache)))
380 pa_hashmap_free(u->cache, NULL, NULL);