Merge 2.4
[apps/native/volume-app.git] / src / sound.c
1 /*
2  * Copyright (c) 2009-2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <appcore-common.h>
19 #include <vconf.h>
20 #include <vconf-keys.h>
21 #include <feedback.h>
22
23 #include "main.h"
24 #include "_util_log.h"
25 #include "view.h"
26 #include "control.h"
27 #include "sound.h"
28 #include "bt.h"
29
30 struct _sound_s_info {
31         int sound_step;
32         int sound_status;
33         int vibration_status;
34         Eina_Bool is_vibration;
35 };
36
37 static struct _sound_s_info sound_info = {
38         .sound_step = 0,
39         .sound_status = 0,
40         .vibration_status = 0,
41         .is_vibration = EINA_FALSE,
42 };
43
44 static void _sound_status_changed_cb(keynode_t *key, void *data);
45 static int _setting_sound_enabled(void);
46
47 int volume_sound_step_get(void)
48 {
49         return sound_info.sound_step;
50 }
51
52 int volume_sound_sound_status_get(void)
53 {
54         return sound_info.sound_status;
55 }
56
57 int volume_sound_vibration_status_get(void)
58 {
59         return sound_info.vibration_status;
60 }
61
62 Eina_Bool volume_sound_is_vibration_get(void)
63 {
64         return sound_info.is_vibration;
65 }
66
67 void volume_sound_is_vibration_set(Eina_Bool val)
68 {
69         sound_info.is_vibration = val;
70 }
71
72 void volume_sound_feedback_play(int feedback_type, int pattern)
73 {
74         _D("Feedback type : %d / pattern : %d", feedback_type, pattern);
75
76         if (FEEDBACK_ERROR_NONE != feedback_play_type(feedback_type, pattern))
77                 _E("Failed to play feedback");
78 }
79
80 void volume_sound_vib_play(void)
81 {
82         ret_if(sound_info.is_vibration);
83
84         _D("Play Feedback : vibration");
85
86         //if (FEEDBACK_ERROR_NONE != feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_VIBRATION_ON))
87         //      _E("Failed to play feedback");
88
89         sound_info.is_vibration = EINA_TRUE;
90 }
91
92 void volume_sound_play(void)
93 {
94         sound_type_e sound_type = volume_control_get_sound_type_at_show();
95         _D("sound type at show : %d", sound_type);
96
97         ret_if(sound_type == SOUND_TYPE_ALARM);
98         ret_if(sound_type == SOUND_TYPE_MEDIA);
99         ret_if(sound_type == SOUND_TYPE_VOICE);
100         ret_if(sound_type == SOUND_TYPE_NOTIFICATION);
101
102         _D("Play Feedback : sound");
103         if(FEEDBACK_ERROR_NONE != feedback_play(FEEDBACK_PATTERN_VOLUME_KEY)) {
104                 _E("Failed to play feedback");
105         }
106
107         return;
108 }
109
110 void volume_sound_setting_sound_play(void)
111 {
112         int snd_enabled = _setting_sound_enabled();
113         _D("snd_enabled(%d)", snd_enabled);
114
115         if(snd_enabled == 1)
116                 volume_sound_feedback_play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TOUCH_TAP);
117 }
118
119 int volume_sound_sound_manager_step_get(sound_type_e type)
120 {
121         _D("volume sound manager step get");
122         int ret = -1, step = 0;
123
124         ret = sound_manager_get_max_volume(type, &step);
125         retvm_if(ret < 0, -1, "Failed to get step");
126
127         sound_info.sound_step = step;
128
129         return step;
130 }
131
132 int volume_sound_sound_manager_volume_get(sound_type_e type)
133 {
134         int ret = -1, val = 0;
135         ret = sound_manager_get_volume(type, &val);
136         retvm_if(ret < 0, -1, "Failed to get volume");
137         return val;
138 }
139
140 sound_type_e volume_sound_sound_manager_type_get(void)
141 {
142         _D(" Volume sound manager type get");
143         int ret = 0;
144         sound_type_e sound_type = -1;
145
146         ret = sound_manager_get_current_sound_type(&sound_type);
147         _D("ret: %d, sound type: %d", ret, sound_type);
148         switch (ret) {
149         case SOUND_MANAGER_ERROR_NONE:
150                 _D("Error none");
151                 break;
152         case SOUND_MANAGER_ERROR_NO_PLAYING_SOUND:
153                 _D("NO playing sound");
154                 sound_type = SOUND_TYPE_RINGTONE;
155                 break;
156         default:
157                 _E("Failed to get sound type : %d", ret);
158                 sound_type = SOUND_TYPE_RINGTONE;
159                 return sound_type;
160         }
161
162         if(sound_type == SOUND_TYPE_SYSTEM)
163                 sound_type = SOUND_TYPE_RINGTONE;
164
165         return sound_type;
166 }
167
168 void _mm_func(sound_type_e type, unsigned int volume, void *data)
169 {
170         bool bt_opened = false;
171
172         _D("mm func is called type : %d, volume : %d", type, volume);
173         if (type == volume_control_get_sound_type_at_show()) {
174                 _D("current show type : %d, volume : %d", type, volume);
175                 /* Need to check sound type & device in media type volume. */
176                 if (type == SOUND_TYPE_MEDIA) {
177                         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
178                         _D("sound status : %d", sound);
179
180                         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
181                         _D("vibration : %d", vibration);
182                         volume_view_volume_icon_set(type, sound, vibration, bt_opened);
183                 }
184                 if (VOLUME_ERROR_OK != volume_view_slider_value_set(volume))
185                         _E("Failed to set slider value");
186         }
187 }
188
189 void volume_sound_mm_sound_init(void)
190 {
191         _D("MM sound Init");
192         int ret = sound_manager_set_volume_changed_cb(_mm_func, NULL);
193         if (ret != SOUND_MANAGER_ERROR_NONE)
194                 _E("Failed to set volume changed event[%d]", ret);
195 }
196
197 int volume_sound_level_get(sound_type_e type)
198 {
199         int val = -1;
200         if (sound_manager_get_volume(type, &val) < 0) {
201                 _E("Failed to get Volume step");
202                 return -1;
203         }
204
205         return val;
206 }
207
208 int volume_sound_level_set(sound_type_e type, int val)
209 {
210         int ret = -1;
211         ret = sound_manager_set_volume(type, val);
212
213         return ret;
214 }
215
216 void volume_sound_change_set(int val)
217 {
218         sound_type_e sound_type = volume_control_get_sound_type_at_show();
219         _D("sound type at show : %d", sound_type);
220
221         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
222         _D("sound status : %d", sound);
223
224         if (val) {
225                 if (sound_type != SOUND_TYPE_MEDIA) {
226                         if (!volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS)) {
227                                 volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
228                                 volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
229                         }
230                 }
231                 if (volume_sound_level_set(sound_type, val) != 0)
232                         volume_sound_level_set(sound_type, 9);
233
234                 volume_sound_play();
235         } else {
236                 if (sound_type != SOUND_TYPE_MEDIA)
237                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
238                 volume_view_slider_value_set(0);
239                 volume_sound_level_set(sound_type, 0);
240
241                 if (sound_type == SOUND_TYPE_RINGTONE) {
242                         volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
243                         volume_sound_vib_play();
244                 }
245         }
246 }
247
248 void volume_sound_vconf_status_set(volume_vconf_type_e type, int val)
249 {
250         char *vconfkey = NULL;
251         switch (type)
252         {
253         case TYPE_VCONF_SOUND_STATUS:
254                 vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
255                 break;
256         case TYPE_VCONF_VIBRATION_STATUS:
257                 vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
258                 break;
259         default:
260                 _E("Failed to set vconfkey : Type error");
261                 return;
262         }
263
264         if (vconf_set_bool(vconfkey, val) < 0)
265                 _E("Failed to set vconfkey : %s", vconfkey);
266 }
267
268 int volume_sound_vconf_status_get(volume_vconf_type_e type)
269 {
270         int ret = -1;
271         char *vconfkey = NULL;
272
273         switch (type)
274         {
275         case TYPE_VCONF_SOUND_STATUS:
276                 vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
277                 break;
278         case TYPE_VCONF_VIBRATION_STATUS:
279                 vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
280                 break;
281         default:
282                 _E("Failed to get vconfkey : Type error");
283                 return -1;
284         }
285
286         if (vconf_get_bool(vconfkey, &ret) < 0) {
287                 _E("Failed to get vconfkey : %s", vconfkey);
288                 return -1;
289         }
290
291         return ret;
292 }
293
294 void volume_sound_vconfkey_register(void)
295 {
296         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
297                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
298
299         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
300                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
301         _sound_status_changed_cb(NULL, NULL);
302 }
303
304 void volume_sound_vconfkey_unregister(void)
305 {
306         if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb) < 0)
307                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
308
309         if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb) < 0)
310                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
311 }
312
313 static int _setting_sound_enabled(void)
314 {
315         int snd_status = 0;
316
317         if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status) < 0)
318                 _E("Failed to get vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
319
320         if (snd_status == 1)
321                 return 1;
322
323         return 0;
324 }
325
326 static void _sound_status_changed_cb(keynode_t *key, void *data)
327 {
328         bool bt_opened = false;
329         const char *keyname = vconf_keynode_get_name(key);
330         ret_if(!keyname);
331         _D("keyname : %s", keyname);
332
333         sound_type_e sound_type = volume_control_get_sound_type_at_show();
334         _D("sound type at show : %d", sound_type);
335
336         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
337         _D("sound status : %d", sound);
338         sound_info.sound_status = sound;
339
340         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
341         _D("vibration : %d", vibration);
342         sound_info.vibration_status = vibration;
343
344         if (sound == 0 && vibration == 1)
345                 _D("vibration mode");
346         else if (sound == 1 && vibration == 0) {
347                 _D("sound mode");
348                 sound_info.is_vibration = EINA_FALSE;
349         } else if (sound == 0 && vibration == 0) {
350                 _D("mute mode");
351                 sound_info.is_vibration = EINA_FALSE;
352         }
353
354         volume_view_volume_icon_set(sound_type, sound, vibration, bt_opened);
355 }
356