Rebase code with tizen 2.3 with following additional changes
[platform/core/multimedia/libmm-sound.git] / common / mm_sound_utils.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <errno.h>
29
30 #include <vconf.h>
31 #include <vconf-keys.h>
32 #include <mm_types.h>
33 #include <mm_error.h>
34 #include <mm_debug.h>
35 #include "../include/mm_sound_private.h"
36 #include "../include/mm_sound.h"
37 #include "../include/mm_sound_common.h"
38 #include "../include/mm_sound_utils.h"
39
40 static mm_sound_route g_valid_route[] = {
41                 MM_SOUND_ROUTE_OUT_SPEAKER, MM_SOUND_ROUTE_OUT_RECEIVER, MM_SOUND_ROUTE_OUT_WIRED_ACCESSORY, MM_SOUND_ROUTE_OUT_BLUETOOTH_SCO, MM_SOUND_ROUTE_OUT_BLUETOOTH_A2DP,
42                 MM_SOUND_ROUTE_OUT_DOCK, MM_SOUND_ROUTE_OUT_HDMI, MM_SOUND_ROUTE_OUT_MIRRORING, MM_SOUND_ROUTE_OUT_USB_AUDIO, MM_SOUND_ROUTE_OUT_MULTIMEDIA_DOCK,
43                 MM_SOUND_ROUTE_IN_MIC, MM_SOUND_ROUTE_IN_WIRED_ACCESSORY, MM_SOUND_ROUTE_IN_MIC_OUT_RECEIVER,
44                 MM_SOUND_ROUTE_IN_MIC_OUT_SPEAKER, MM_SOUND_ROUTE_IN_MIC_OUT_HEADPHONE,
45                 MM_SOUND_ROUTE_INOUT_HEADSET, MM_SOUND_ROUTE_INOUT_BLUETOOTH
46 };
47
48 #define MM_SOUND_DEFAULT_VOLUME_SYSTEM                  9
49 #define MM_SOUND_DEFAULT_VOLUME_NOTIFICATION    11
50 #define MM_SOUND_DEFAULT_VOLUME_ALARAM                  7
51 #define MM_SOUND_DEFAULT_VOLUME_RINGTONE                11
52 #define MM_SOUND_DEFAULT_VOLUME_MEDIA                   7
53 #define MM_SOUND_DEFAULT_VOLUME_CALL                    4
54 #define MM_SOUND_DEFAULT_VOLUME_VOIP                    4
55 #define MM_SOUND_DEFAULT_VOLUME_VOICE                   7
56 #define MM_SOUND_DEFAULT_VOLUME_ANDROID                 0
57
58 static char *g_volume_vconf[VOLUME_TYPE_MAX] = {
59         VCONF_KEY_VOLUME_TYPE_SYSTEM,           /* VOLUME_TYPE_SYSTEM */
60         VCONF_KEY_VOLUME_TYPE_NOTIFICATION,     /* VOLUME_TYPE_NOTIFICATION */
61         VCONF_KEY_VOLUME_TYPE_ALARM,            /* VOLUME_TYPE_ALARM */
62         VCONF_KEY_VOLUME_TYPE_RINGTONE,         /* VOLUME_TYPE_RINGTONE */
63         VCONF_KEY_VOLUME_TYPE_MEDIA,            /* VOLUME_TYPE_MEDIA */
64         VCONF_KEY_VOLUME_TYPE_CALL,                     /* VOLUME_TYPE_CALL */
65         VCONF_KEY_VOLUME_TYPE_VOIP,                     /* VOLUME_TYPE_VOIP */
66         VCONF_KEY_VOLUME_TYPE_VOICE,            /* VOLUME_TYPE_VOICE */
67         VCONF_KEY_VOLUME_TYPE_ANDROID           /* VOLUME_TYPE_FIXED */
68 };
69 static char *g_volume_str[VOLUME_TYPE_MAX] = {
70         "SYSTEM",
71         "NOTIFICATION",
72         "ALARM",
73         "RINGTONE",
74         "MEDIA",
75         "CALL",
76         "VOIP",
77         "VOICE",
78         "FIXED",
79 };
80
81 EXPORT_API
82 int _mm_sound_get_valid_route_list(mm_sound_route **route_list)
83 {
84         *route_list = g_valid_route;
85
86         return (int)(sizeof(g_valid_route) / sizeof(mm_sound_route));
87 }
88
89 EXPORT_API
90 bool _mm_sound_is_route_valid(mm_sound_route route)
91 {
92         mm_sound_route *route_list = 0;
93         int route_index = 0;
94         int route_list_count = 0;
95
96         route_list_count = _mm_sound_get_valid_route_list(&route_list);
97         for (route_index = 0; route_index < route_list_count; route_index++) {
98                 if (route_list[route_index] == route)
99                         return 1;
100         }
101
102         return 0;
103 }
104
105 EXPORT_API
106 void _mm_sound_get_devices_from_route(mm_sound_route route, mm_sound_device_in *device_in, mm_sound_device_out *device_out)
107 {
108         if (device_in && device_out) {
109                 *device_in = route & 0x00FF;
110                 *device_out = route & 0xFFF00;
111         }
112 }
113
114 EXPORT_API
115 bool _mm_sound_check_hibernation (const char *path)
116 {
117         int fd = -1;
118         if (path == NULL) {
119                 debug_error ("Path is null\n");
120                 return false;
121         }
122
123         fd = open (path, O_RDONLY | O_CREAT, 0644);
124         if (fd != -1) {
125                 debug_log ("Open [%s] success!!\n", path);
126         } else {
127                 debug_error ("Can't create [%s] with errno [%d]\n", path, errno);
128                 return false;
129         }
130
131         close (fd);
132         return true;
133 }
134
135 EXPORT_API
136 int _mm_sound_volume_add_callback(volume_type_t type, void *func, void* user_data)
137 {
138         if (vconf_notify_key_changed(g_volume_vconf[type], func, user_data)) {
139                 debug_error ("vconf_notify_key_changed failed..\n");
140                 return MM_ERROR_SOUND_INTERNAL;
141         }
142
143         return MM_ERROR_NONE;
144 }
145
146 EXPORT_API
147 int _mm_sound_volume_remove_callback(volume_type_t type, void *func)
148 {
149         if (vconf_ignore_key_changed(g_volume_vconf[type], func)) {
150                 debug_error ("vconf_ignore_key_changed failed..\n");
151                 return MM_ERROR_SOUND_INTERNAL;
152         }
153
154         return MM_ERROR_NONE;
155 }
156
157 EXPORT_API
158 int _mm_sound_muteall_add_callback(void *func)
159 {
160         if (vconf_notify_key_changed(VCONF_KEY_MUTE_ALL, func, NULL)) {
161                 debug_error ("vconf_notify_key_changed failed..\n");
162                 return MM_ERROR_SOUND_INTERNAL;
163         }
164
165         return MM_ERROR_NONE;
166 }
167
168 EXPORT_API
169 int _mm_sound_muteall_remove_callback(void *func)
170 {
171         if (vconf_ignore_key_changed(VCONF_KEY_MUTE_ALL, func)) {
172                 debug_error ("vconf_ignore_key_changed failed..\n");
173                 return MM_ERROR_SOUND_INTERNAL;
174         }
175
176         return MM_ERROR_NONE;
177 }
178
179 EXPORT_API
180 int _mm_sound_volume_get_value_by_type(volume_type_t type, unsigned int *value)
181 {
182         int ret = MM_ERROR_NONE;
183         int vconf_value = 0;
184
185         /* Get volume value from VCONF */
186         if (vconf_get_int(g_volume_vconf[type], &vconf_value)) {
187                 debug_error ("vconf_get_int(%s) failed..\n", g_volume_vconf[type]);
188                 return MM_ERROR_SOUND_INTERNAL;
189         }
190
191         *value = vconf_value;
192         if (ret == MM_ERROR_NONE)
193                 debug_log("volume_get_value %s %d",  g_volume_str[type], *value);
194
195         return ret;
196 }
197
198 EXPORT_API
199 int _mm_sound_volume_set_value_by_type(volume_type_t type, unsigned int value)
200 {
201         int ret = MM_ERROR_NONE;
202         int vconf_value = 0;
203
204         vconf_value = value;
205         debug_log("volume_set_value %s %d",  g_volume_str[type], value);
206
207         /* Set volume value to VCONF */
208         if ((ret = vconf_set_int(g_volume_vconf[type], vconf_value)) != 0) {
209                 debug_error ("vconf_set_int(%s) failed..ret[%d]\n", g_volume_vconf[type], ret);
210                 if (ret == -EPERM || ret == -EACCES)
211                         return MM_ERROR_SOUND_PERMISSION_DENIED;
212                 else
213                         return MM_ERROR_SOUND_INTERNAL;
214         }
215         return ret;
216 }
217
218 EXPORT_API
219 int _mm_sound_volume_set_balance(float balance)
220 {
221         /* Set balance value to VCONF */
222         if (vconf_set_dbl(VCONF_KEY_VOLUME_BALANCE, balance)) {
223                 debug_error ("vconf_set_dbl(%s) failed..\n", VCONF_KEY_VOLUME_BALANCE);
224                 return MM_ERROR_SOUND_INTERNAL;
225         }
226
227         return MM_ERROR_NONE;
228 }
229
230 EXPORT_API
231 int _mm_sound_volume_get_balance(float *balance)
232 {
233         double balance_value = 0;
234
235         /* Get balance value from VCONF */
236         if (vconf_get_dbl(VCONF_KEY_VOLUME_BALANCE, &balance_value)) {
237                 debug_error ("vconf_get_int(%s) failed..\n", VCONF_KEY_VOLUME_BALANCE);
238                 return MM_ERROR_SOUND_INTERNAL;
239         }
240
241         *balance = balance_value;
242         debug_log("balance get value [%s]=[%f]", VCONF_KEY_VOLUME_BALANCE, *balance);
243
244         return MM_ERROR_NONE;
245 }
246
247 EXPORT_API
248 int _mm_sound_set_muteall(int muteall)
249 {
250         /* Set muteall value to VCONF */
251         if (vconf_set_int(VCONF_KEY_MUTE_ALL, muteall)) {
252                 debug_error ("vconf_set_int(%s) failed..\n", VCONF_KEY_MUTE_ALL);
253                 return MM_ERROR_SOUND_INTERNAL;
254         }
255
256         return MM_ERROR_NONE;
257 }
258
259 EXPORT_API
260 int _mm_sound_get_muteall(int *muteall)
261 {
262         int muteall_value = 0;
263
264         /* Get muteall value from VCONF */
265         if (vconf_get_int(VCONF_KEY_MUTE_ALL, &muteall_value)) {
266                 debug_error ("vconf_get_int(%s) failed..\n", VCONF_KEY_MUTE_ALL);
267                 return MM_ERROR_SOUND_INTERNAL;
268         }
269
270         *muteall = muteall_value;
271         debug_log("muteall get value [%s]=[%d]", VCONF_KEY_MUTE_ALL, *muteall);
272
273         return MM_ERROR_NONE;
274 }
275
276 EXPORT_API
277 int __mm_sound_set_stereo_to_mono(int ismono)
278 {
279         /* Set ismono value to VCONF */
280         if (vconf_set_int(VCONF_KEY_MONO_AUDIO, ismono)) {
281                 debug_error ("vconf_set_int(%s) failed..\n", VCONF_KEY_MONO_AUDIO);
282                 return MM_ERROR_SOUND_INTERNAL;
283         }
284
285         return MM_ERROR_NONE;
286 }
287
288 EXPORT_API
289 int __mm_sound_get_stereo_to_mono(int *ismono)
290 {
291         int ismono_value = 0;
292
293         /* Get ismono value from VCONF */
294         if (vconf_get_int(VCONF_KEY_MONO_AUDIO, &ismono_value)) {
295                 debug_error ("vconf_get_int(%s) failed..\n", VCONF_KEY_MONO_AUDIO);
296                 return MM_ERROR_SOUND_INTERNAL;
297         }
298
299         *ismono = ismono_value;
300         debug_log("ismono get value [%s]=[%d]", VCONF_KEY_MONO_AUDIO, *ismono);
301
302         return MM_ERROR_NONE;
303 }
304
305 EXPORT_API
306 int _mm_sound_get_earjack_type (int *type)
307 {
308         int earjack_status = 0;
309
310         if (type == NULL) {
311                 debug_error ("invalid parameter!!!");
312                 return MM_ERROR_INVALID_ARGUMENT;
313         }
314
315         /* Get actual vconf value */
316         vconf_get_int(VCONFKEY_SYSMAN_EARJACK, &earjack_status);
317         debug_msg ("[%s] get status=[%d]\n", VCONFKEY_SYSMAN_EARJACK, earjack_status);
318
319         *type = (earjack_status >= 0)? earjack_status : VCONFKEY_SYSMAN_EARJACK_REMOVED;
320
321         return MM_ERROR_NONE;
322 }
323
324 EXPORT_API
325 int _mm_sound_get_dock_type (int *type)
326 {
327         int dock_status = 0;
328
329         if (type == NULL) {
330                 debug_error ("invalid parameter!!!");
331                 return MM_ERROR_INVALID_ARGUMENT;
332         }
333
334         /* Get actual vconf value */
335         vconf_get_int(VCONFKEY_SYSMAN_CRADLE_STATUS, &dock_status);
336         debug_msg ("[%s] get dock status=[%d]\n", VCONFKEY_SYSMAN_CRADLE_STATUS, dock_status);
337
338         *type = dock_status;
339
340         return MM_ERROR_NONE;
341 }
342
343 EXPORT_API
344 bool _mm_sound_is_recording (void)
345 {
346         int capture_status = 0;
347         bool result = false;
348
349     /* FIXME */
350         return result;
351 }
352
353 EXPORT_API
354 bool _mm_sound_is_mute_policy (void)
355 {
356         int setting_sound_status = true;
357
358         /* If sound is mute mode, force ringtone/notification path to headset */
359         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &setting_sound_status);
360         debug_log ("[%s] setting_sound_status=%d\n", VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, setting_sound_status);
361
362         return !setting_sound_status;
363 }