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 */
218 pa_log_debug("Found %s.", fn
);
221 r
->mtime
= st
.st_mtime
;
222 pa_xfree(r
->application_name
);
223 pa_xfree(r
->icon_name
);
225 r
->application_name
= r
->icon_name
= r
->role
= NULL
;
227 pa_proplist_clear(r
->proplist
);
229 table
[0].data
= &r
->application_name
;
230 table
[1].data
= &r
->icon_name
;
232 if (pa_config_parse(fn
, NULL
, table
, r
) < 0)
233 pa_log_warn("Failed to parse .desktop file %s.", fn
);
238 static void apply_rule(struct rule
*r
, pa_proplist
*p
) {
246 pa_proplist_update(p
, PA_UPDATE_MERGE
, r
->proplist
);
249 if (!pa_proplist_contains(p
, PA_PROP_APPLICATION_ICON_NAME
))
250 pa_proplist_sets(p
, PA_PROP_APPLICATION_ICON_NAME
, r
->icon_name
);
252 if (r
->application_name
) {
255 t
= pa_proplist_gets(p
, PA_PROP_APPLICATION_NAME
);
257 if (!t
|| pa_streq(t
, r
->process_name
))
258 pa_proplist_sets(p
, PA_PROP_APPLICATION_NAME
, r
->application_name
);
262 if (!pa_proplist_contains(p
, PA_PROP_MEDIA_ROLE
))
263 pa_proplist_sets(p
, PA_PROP_MEDIA_ROLE
, r
->role
);
266 static void make_room(pa_hashmap
*cache
) {
269 while (pa_hashmap_size(cache
) >= MAX_CACHE_SIZE
) {
272 pa_assert_se(r
= pa_hashmap_steal_first(cache
));
277 static pa_hook_result_t
process(struct userdata
*u
, pa_proplist
*p
) {
285 if (!(pn
= pa_proplist_gets(p
, PA_PROP_APPLICATION_PROCESS_BINARY
)))
288 if (*pn
== '.' || strchr(pn
, '/'))
293 pa_log_debug("Looking for .desktop file for %s", pn
);
295 if ((r
= pa_hashmap_get(u
->cache
, pn
))) {
296 if (now
-r
->timestamp
> STAT_INTERVAL
) {
303 r
= pa_xnew0(struct rule
, 1);
304 r
->process_name
= pa_xstrdup(pn
);
306 pa_hashmap_put(u
->cache
, r
->process_name
, r
);
314 static pa_hook_result_t
client_new_cb(pa_core
*core
, pa_client_new_data
*data
, struct userdata
*u
) {
315 pa_core_assert_ref(core
);
319 return process(u
, data
->proplist
);
322 static pa_hook_result_t
client_proplist_changed_cb(pa_core
*core
, pa_client
*client
, struct userdata
*u
) {
323 pa_core_assert_ref(core
);
327 return process(u
, client
->proplist
);
330 int pa__init(pa_module
*m
) {
331 pa_modargs
*ma
= NULL
;
336 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
337 pa_log("Failed to parse module arguments");
341 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
343 u
->cache
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
344 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
);
345 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
);
360 void pa__done(pa_module
*m
) {
365 if (!(u
= m
->userdata
))
368 if (u
->client_new_slot
)
369 pa_hook_slot_free(u
->client_new_slot
);
370 if (u
->client_proplist_changed_slot
)
371 pa_hook_slot_free(u
->client_proplist_changed_slot
);
376 while ((r
= pa_hashmap_steal_first(u
->cache
)))
379 pa_hashmap_free(u
->cache
, NULL
, NULL
);