Tizen 2.1 base
[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 <avsys-audio.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_WIRED_ACCESSORY, MM_SOUND_ROUTE_OUT_BLUETOOTH,
42                 MM_SOUND_ROUTE_OUT_DOCK, MM_SOUND_ROUTE_OUT_HDMI, MM_SOUND_ROUTE_OUT_WFD, MM_SOUND_ROUTE_OUT_USB_AUDIO,
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                  5
49 #define MM_SOUND_DEFAULT_VOLUME_NOTIFICATION    7
50 #define MM_SOUND_DEFAULT_VOLUME_ALARAM                  6
51 #define MM_SOUND_DEFAULT_VOLUME_RINGTONE                13
52 #define MM_SOUND_DEFAULT_VOLUME_MEDIA                   7
53 #define MM_SOUND_DEFAULT_VOLUME_CALL                    7
54 #define MM_SOUND_DEFAULT_VOLUME_VOIP                    7
55 #define MM_SOUND_DEFAULT_VOLUME_ANDROID                 0
56 #define MM_SOUND_DEFAULT_VOLUME_JAVA                    11
57 static int g_default_volume[VOLUME_TYPE_MAX] = {
58         MM_SOUND_DEFAULT_VOLUME_SYSTEM,
59         MM_SOUND_DEFAULT_VOLUME_NOTIFICATION,
60         MM_SOUND_DEFAULT_VOLUME_ALARAM,
61         MM_SOUND_DEFAULT_VOLUME_RINGTONE,
62         MM_SOUND_DEFAULT_VOLUME_MEDIA,
63         MM_SOUND_DEFAULT_VOLUME_CALL,
64         MM_SOUND_DEFAULT_VOLUME_VOIP,
65         MM_SOUND_DEFAULT_VOLUME_ANDROID,
66         MM_SOUND_DEFAULT_VOLUME_JAVA,
67 };
68 static char *g_volume_vconf[VOLUME_TYPE_MAX] = {
69         VCONF_KEY_VOLUME_TYPE_SYSTEM,           /* VOLUME_TYPE_SYSTEM */
70         VCONF_KEY_VOLUME_TYPE_NOTIFICATION,     /* VOLUME_TYPE_NOTIFICATION */
71         VCONF_KEY_VOLUME_TYPE_ALARM,            /* VOLUME_TYPE_ALARM */
72         VCONF_KEY_VOLUME_TYPE_RINGTONE,         /* VOLUME_TYPE_RINGTONE */
73         VCONF_KEY_VOLUME_TYPE_MEDIA,            /* VOLUME_TYPE_MEDIA */
74         VCONF_KEY_VOLUME_TYPE_CALL,                     /* VOLUME_TYPE_CALL */
75         VCONF_KEY_VOLUME_TYPE_VOIP,                     /* VOLUME_TYPE_VOIP */
76         VCONF_KEY_VOLUME_TYPE_ANDROID,          /* VOLUME_TYPE_FIXED */
77         VCONF_KEY_VOLUME_TYPE_JAVA                      /* VOLUME_TYPE_EXT_JAVA */
78 };
79 static char *g_volume_str[VOLUME_TYPE_MAX] = {
80         "SYSTEM",
81         "NOTIFICATION",
82         "ALARM",
83         "RINGTONE",
84         "MEDIA",
85         "CALL",
86         "VOIP",
87         "FIXED",
88         "JAVA",
89 };
90
91 EXPORT_API
92 int _mm_sound_get_valid_route_list(mm_sound_route **route_list)
93 {
94         *route_list = g_valid_route;
95
96         return (int)(sizeof(g_valid_route) / sizeof(mm_sound_route));
97 }
98
99 EXPORT_API
100 bool _mm_sound_is_route_valid(mm_sound_route route)
101 {
102         mm_sound_route *route_list = 0;
103         int route_index = 0;
104         int route_list_count = 0;
105
106         route_list_count = _mm_sound_get_valid_route_list(&route_list);
107         for (route_index = 0; route_index < route_list_count; route_index++) {
108                 if (route_list[route_index] == route)
109                         return 1;
110         }
111
112         return 0;
113 }
114
115 EXPORT_API
116 void _mm_sound_get_devices_from_route(mm_sound_route route, mm_sound_device_in *device_in, mm_sound_device_out *device_out)
117 {
118         if (device_in && device_out) {
119                 *device_in = route & 0x00FF;
120                 *device_out = route & 0xFFF00;
121         }
122 }
123
124 EXPORT_API
125 bool _mm_sound_check_hibernation (const char *path)
126 {
127         int fd = -1;
128         if (path == NULL) {
129                 debug_error ("Path is null\n");
130                 return false;
131         }
132
133         fd = open (path, O_RDONLY | O_CREAT, 0644);
134         if (fd != -1) {
135                 debug_log ("Open [%s] success!!\n", path);
136         } else {
137                 debug_error ("Can't create [%s] with errno [%d]\n", path, errno);
138                 return false;
139         }
140
141         close (fd);
142         return true;
143 }
144
145 EXPORT_API
146 int _mm_sound_volume_add_callback(volume_type_t type, void *func, void* user_data)
147 {
148         if (vconf_notify_key_changed(g_volume_vconf[type], func, user_data)) {
149                 debug_error ("vconf_notify_key_changed failed..\n");
150                 return MM_ERROR_SOUND_INTERNAL;
151         }
152
153         return MM_ERROR_NONE;
154 }
155
156 EXPORT_API
157 int _mm_sound_volume_remove_callback(volume_type_t type, void *func)
158 {
159         if (vconf_ignore_key_changed(g_volume_vconf[type], func)) {
160                 debug_error ("vconf_ignore_key_changed failed..\n");
161                 return MM_ERROR_SOUND_INTERNAL;
162         }
163
164         return MM_ERROR_NONE;
165 }
166
167 EXPORT_API
168 int _mm_sound_volume_get_value_by_type(volume_type_t type, unsigned int *value)
169 {
170         int vconf_value = 0;
171
172         /* Get volume value from VCONF */
173         if (vconf_get_int(g_volume_vconf[type], &vconf_value)) {
174                 debug_error ("vconf_get_int(%s) failed..\n", g_volume_vconf[type]);
175                 return MM_ERROR_SOUND_INTERNAL;
176         }
177
178         *value = vconf_value;
179         debug_log("volume_get_value %s %d",  g_volume_str[type], *value);
180
181         return MM_ERROR_NONE;
182 }
183
184 EXPORT_API
185 int _mm_sound_volume_set_value_by_type(volume_type_t type, unsigned int value)
186 {
187         int vconf_value = 0;
188
189         vconf_value = value;
190         debug_log("volume_set_value %s %d",  g_volume_str[type], value);
191
192         /* Set volume value to VCONF */
193         if (vconf_set_int(g_volume_vconf[type], vconf_value)) {
194                 debug_error ("vconf_set_int(%s) failed..\n", g_volume_vconf[type]);
195                 return MM_ERROR_SOUND_INTERNAL;
196         }
197
198         return MM_ERROR_NONE;
199 }
200
201 EXPORT_API
202 int _mm_sound_volume_get_values_on_bootup(int *values)
203 {
204         int i, vconf_value = 0;
205
206         for (i = 0; i < VOLUME_TYPE_MAX; i++) {
207                 if (!vconf_get_int(g_volume_vconf[i], &vconf_value)) {
208                         values[i] = vconf_value;
209                         debug_log("volume_get_value %s %d", g_volume_str[i], values[i]);
210                 } else {
211                         debug_error ("vconf_get_int(%s) failed..\n", g_volume_vconf[i]);
212                         values[i] = g_default_volume[i];
213                         vconf_value = values[i];
214                         if (vconf_set_int(g_volume_vconf[i], vconf_value)) {
215                                 debug_error ("vconf_set_int(%s) failed..\n", g_volume_vconf[i]);
216                         }
217                 }
218         }
219
220         return MM_ERROR_NONE;
221 }
222