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
30 #include <pulse/xmalloc.h>
31 #include <pulse/volume.h>
32 #include <pulse/channelmap.h>
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/modargs.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/client.h>
40 #include <pulsecore/conf-parser.h>
42 #include "module-augment-properties-symdef.h"
44 PA_MODULE_AUTHOR("Lennart Poettering");
45 PA_MODULE_DESCRIPTION("Augment the property sets of streams with additional static information");
46 PA_MODULE_VERSION(PACKAGE_VERSION
);
47 PA_MODULE_LOAD_ONCE(TRUE
);
49 #define STAT_INTERVAL 30
50 #define MAX_CACHE_SIZE 50
52 static const char* const valid_modargs
[] = {
61 char *application_name
;
64 pa_proplist
*proplist
;
69 pa_hook_slot
*client_new_slot
, *client_proplist_changed_slot
;
72 static void rule_free(struct rule
*r
) {
75 pa_xfree(r
->process_name
);
76 pa_xfree(r
->application_name
);
77 pa_xfree(r
->icon_name
);
80 pa_proplist_free(r
->proplist
);
84 static int parse_properties(
93 struct rule
*r
= userdata
;
96 if (!(n
= pa_proplist_from_string(rvalue
)))
100 pa_proplist_update(r
->proplist
, PA_UPDATE_MERGE
, n
);
108 static int parse_categories(
109 const char *filename
,
117 struct rule
*r
= userdata
;
118 const char *state
= NULL
;
121 while ((c
= pa_split(rvalue
, ";", &state
))) {
123 if (pa_streq(c
, "Game")) {
125 r
->role
= pa_xstrdup("game");
126 } else if (pa_streq(c
, "Telephony")) {
128 r
->role
= pa_xstrdup("phone");
137 static int check_type(
138 const char *filename
,
146 return pa_streq(rvalue
, "Application") ? 0 : -1;
149 static int catch_all(
150 const char *filename
,
161 static void update_rule(struct rule
*r
) {
164 static pa_config_item table
[] = {
165 { "Name", pa_config_parse_string
, NULL
, "Desktop Entry" },
166 { "Icon", pa_config_parse_string
, NULL
, "Desktop Entry" },
167 { "Type", check_type
, NULL
, "Desktop Entry" },
168 { "X-PulseAudio-Properties", parse_properties
, NULL
, "Desktop Entry" },
169 { "Categories", parse_categories
, NULL
, "Desktop Entry" },
170 { NULL
, catch_all
, NULL
, NULL
},
171 { NULL
, NULL
, NULL
, NULL
},
173 pa_bool_t found
= FALSE
;
176 fn
= pa_sprintf_malloc(DESKTOPFILEDIR PA_PATH_SEP
"%s.desktop", r
->process_name
);
178 if (stat(fn
, &st
) == 0)
181 DIR *desktopfiles_dir
;
185 /* Let's try a more aggressive search, but only one level */
186 if ((desktopfiles_dir
= opendir(DESKTOPFILEDIR
))) {
187 while ((dir
= readdir(desktopfiles_dir
))) {
188 if (dir
->d_type
!= DT_DIR
189 || strcmp(dir
->d_name
, ".") == 0
190 || strcmp(dir
->d_name
, "..") == 0)
194 fn
= pa_sprintf_malloc(DESKTOPFILEDIR
195 PA_PATH_SEP
"%s" PA_PATH_SEP
"%s.desktop",
196 dir
->d_name
, r
->process_name
);
198 if (stat(fn
, &st
) == 0) {
203 closedir(desktopfiles_dir
);
214 if (st
.st_mtime
== r
->mtime
) {
215 /* Theoretically the filename could have changed, but if so
216 having the same mtime is very unlikely so not worth tracking it in r */
220 pa_log_debug("Found %s (which has been updated since we last checked).", fn
);
222 pa_log_debug("Found %s.", fn
);
225 r
->mtime
= st
.st_mtime
;
226 pa_xfree(r
->application_name
);
227 pa_xfree(r
->icon_name
);
229 r
->application_name
= r
->icon_name
= r
->role
= NULL
;
231 pa_proplist_clear(r
->proplist
);
233 table
[0].data
= &r
->application_name
;
234 table
[1].data
= &r
->icon_name
;
236 if (pa_config_parse(fn
, NULL
, table
, r
) < 0)
237 pa_log_warn("Failed to parse .desktop file %s.", fn
);
242 static void apply_rule(struct rule
*r
, pa_proplist
*p
) {
250 pa_proplist_update(p
, PA_UPDATE_MERGE
, r
->proplist
);
253 if (!pa_proplist_contains(p
, PA_PROP_APPLICATION_ICON_NAME
))
254 pa_proplist_sets(p
, PA_PROP_APPLICATION_ICON_NAME
, r
->icon_name
);
256 if (r
->application_name
) {
259 t
= pa_proplist_gets(p
, PA_PROP_APPLICATION_NAME
);
261 if (!t
|| pa_streq(t
, r
->process_name
))
262 pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, r
->application_name
);
266 if (!pa_proplist_contains(p
, PA_PROP_MEDIA_ROLE
))
267 pa_proplist_sets(p
, PA_PROP_MEDIA_ROLE
, r
->role
);
270 static void make_room(pa_hashmap
*cache
) {
273 while (pa_hashmap_size(cache
) >= MAX_CACHE_SIZE
) {
276 pa_assert_se(r
= pa_hashmap_steal_first(cache
));
281 static pa_hook_result_t
process(struct userdata
*u
, pa_proplist
*p
) {
289 if (!(pn
= pa_proplist_gets(p
, PA_PROP_APPLICATION_PROCESS_BINARY
)))
292 if (*pn
== '.' || strchr(pn
, '/'))
297 pa_log_debug("Looking for .desktop file for %s", pn
);
299 if ((r
= pa_hashmap_get(u
->cache
, pn
))) {
300 if (now
-r
->timestamp
> STAT_INTERVAL
) {
307 r
= pa_xnew0(struct rule
, 1);
308 r
->process_name
= pa_xstrdup(pn
);
310 pa_hashmap_put(u
->cache
, r
->process_name
, r
);
318 static pa_hook_result_t
client_new_cb(pa_core
*core
, pa_client_new_data
*data
, struct userdata
*u
) {
319 pa_core_assert_ref(core
);
323 return process(u
, data
->proplist
);
326 static pa_hook_result_t
client_proplist_changed_cb(pa_core
*core
, pa_client
*client
, struct userdata
*u
) {
327 pa_core_assert_ref(core
);
331 return process(u
, client
->proplist
);
334 int pa__init(pa_module
*m
) {
335 pa_modargs
*ma
= NULL
;
340 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
341 pa_log("Failed to parse module arguments");
345 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
347 u
->cache
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
348 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
);
349 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
);
364 void pa__done(pa_module
*m
) {
369 if (!(u
= m
->userdata
))
372 if (u
->client_new_slot
)
373 pa_hook_slot_free(u
->client_new_slot
);
374 if (u
->client_proplist_changed_slot
)
375 pa_hook_slot_free(u
->client_proplist_changed_slot
);
380 while ((r
= pa_hashmap_steal_first(u
->cache
)))
383 pa_hashmap_free(u
->cache
, NULL
, NULL
);