scripting: support for zone based routing in application classes
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / murphy-config.c
1 /*
2  * module-murphy-ivi -- PulseAudio module for providing audio routing support
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU Lesser General Public License,
7  * version 2.1, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St - Fifth Floor, Boston,
17  * MA 02110-1301 USA.
18  *
19  */
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <pulsecore/pulsecore-config.h>
26
27 #include "murphy-config.h"
28 #include "node.h"
29 #include "router.h"
30 #include "volume.h"
31 #include "scripting.h"
32
33 typedef struct {
34     mir_direction          type;
35     const char            *name;
36     mir_rtgroup_accept_t   accept;
37     mir_rtgroup_compare_t  compare;
38 } rtgroup_def;
39
40 typedef struct {
41     mir_node_type  class;
42     mir_direction  type;
43     const char    *rtgroup;
44 } classmap_def;
45
46 typedef struct {
47     char          *id;
48     mir_node_type  type;
49 } typemap_def;
50
51 typedef struct {
52     mir_node_type  class;
53     int            priority;
54 } prior_def;
55
56
57
58 static rtgroup_def  rtgroups[] = {
59     {mir_input,
60      "phone",
61      mir_router_phone_accept,
62      mir_router_phone_compare
63     },
64
65     {mir_output,
66      "default",
67      mir_router_default_accept,
68      mir_router_default_compare
69     },
70
71     {mir_output,
72      "phone",
73      mir_router_phone_accept,
74      mir_router_phone_compare
75     },
76
77     {0,NULL,NULL,NULL}
78 };
79
80 static classmap_def classmap[] = {
81     {mir_phone    , mir_input , "phone"  },
82
83     {mir_radio    , mir_output, "default"},
84     {mir_player   , mir_output, "default"},
85     {mir_navigator, mir_output, "default"},
86     {mir_game     , mir_output, "default"},
87     {mir_browser  , mir_output, "default"},
88     {mir_phone    , mir_output, "phone"  },
89     {mir_event    , mir_output, "default"},
90     {mir_node_type_unknown, mir_direction_unknown, NULL}
91 };
92
93 static typemap_def rolemap[] = {
94     {"video"    , mir_player    },
95     {"music"    , mir_player    },
96     {"game"     , mir_game      },
97     {"event"    , mir_event     },
98     {"navigator", mir_navigator },
99     {"phone"    , mir_phone     },
100     {"carkit"   , mir_phone     },
101     {"animation", mir_browser   },
102     {"test"     , mir_player    },
103     {"ringtone" , mir_alert     },
104     {"alarm"    , mir_alert     },
105     {"camera"   , mir_camera    },
106     {"system"   , mir_system    },
107     {NULL, mir_node_type_unknown}
108 };
109
110 static typemap_def binmap[] = {
111     {"rhytmbox"    , mir_player },
112     {"firefox"     , mir_browser},
113     {"chrome"      , mir_browser},
114     {"sound-juicer", mir_player },
115     {NULL, mir_node_type_unknown}
116 };
117
118 static prior_def priormap[] = {
119     {mir_radio    , 1},
120     {mir_player   , 1},
121     {mir_navigator, 2},
122     {mir_game     , 3},
123     {mir_browser  , 1},
124     {mir_phone    , 4},
125     {mir_event    , 5},
126     {mir_node_type_unknown, 0}
127 };
128
129 static double speedvol;
130 static double supprvol = -20.0;
131 static int exception_classes[] = {mir_phone, mir_navigator};
132 static mir_volume_suppress_arg suppress = {
133     &supprvol, {DIM(exception_classes), exception_classes}
134 };
135
136
137 static pa_bool_t use_default_configuration(struct userdata *);
138
139 #if 0
140 static pa_bool_t parse_config_file(struct userdata *, FILE *);
141 #endif
142
143 pa_mir_config *pa_mir_config_init(struct userdata *u)
144 {
145     pa_mir_config *config;
146
147     pa_assert(u);
148
149     config = pa_xnew0(pa_mir_config, 1);
150
151     return config;
152 }
153
154 void pa_mir_config_done(struct userdata *u)
155 {
156     pa_mir_config *config;
157
158     if (u && (config = u->config)) {
159         pa_xfree(config);
160         u->config = NULL;
161     }
162 }
163
164
165 pa_bool_t pa_mir_config_parse_file(struct userdata *u, const char *path)
166 {
167     pa_module *module;
168     pa_mir_config *config;
169     int success;
170     char buf[4096];
171
172     pa_assert(u);
173     pa_assert_se((module = u->module));
174     pa_assert_se((config = u->config));
175
176     if (!path)
177         success = FALSE;
178     else {
179         pa_log_info("%s: configuration file is '%s'", module->name, path);
180         success =  pa_scripting_dofile(u, path);
181     }
182
183     if (!success) {
184         pa_log_info("%s: builtin default configuration applies", module->name);
185         success = use_default_configuration(u);
186     }
187
188     pa_nodeset_print_maps(u, buf, sizeof(buf));
189     pa_log_debug(buf);
190
191     return success;
192 }
193
194 static pa_bool_t use_default_configuration(struct userdata *u)
195 {
196     rtgroup_def  *r;
197     classmap_def *c;
198     typemap_def  *t;
199     prior_def    *p;
200
201     pa_assert(u);
202
203     for (r = rtgroups;  r->name;   r++)
204         mir_router_create_rtgroup(u, r->type, r->name, r->accept, r->compare);
205
206     for (c = classmap;  c->rtgroup;  c++)
207         mir_router_assign_class_to_rtgroup(u, c->class, c->type, c->rtgroup);
208
209     for (t = rolemap; t->id; t++)
210         pa_nodeset_add_role(u, t->id, t->type, NULL);
211
212     for (t = binmap; t->id; t++)
213         pa_nodeset_add_binary(u, t->id, t->type, NULL);
214
215     for (p = priormap;  p->class;  p++)
216         mir_router_assign_class_priority(u, p->class, p->priority);
217
218
219     mir_volume_add_generic_limit(u, mir_volume_correction, &speedvol);
220
221     mir_volume_add_class_limit(u, mir_phone,mir_volume_suppress, &suppress);
222     mir_volume_add_class_limit(u, mir_navigator,mir_volume_suppress,&suppress);
223
224
225     return TRUE;
226 }
227
228 /*
229  * Local Variables:
230  * c-basic-offset: 4
231  * indent-tabs-mode: nil
232  * End:
233  *
234  */