tizen 2.3.1 release
[apps/home/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
29 static struct _s_info {
30         int sound_step;
31         int sound_status;
32         int vibration_status;
33         Eina_Bool is_vibration;
34 } s_info = {
35         .sound_step = 0,
36         .sound_status = 0,
37         .vibration_status = 0,
38         .is_vibration = EINA_FALSE,
39 };
40
41 int volume_sound_step_get(void)
42 {
43         return s_info.sound_step;
44 }
45
46 int volume_sound_sound_status_get(void)
47 {
48         return s_info.sound_status;
49 }
50
51 int volume_sound_vibration_status_get(void)
52 {
53         return s_info.vibration_status;
54 }
55
56 Eina_Bool volume_sound_is_vibration_get(void)
57 {
58         return s_info.is_vibration;
59 }
60
61 void volume_sound_is_vibration_set(Eina_Bool val)
62 {
63         s_info.is_vibration = val;
64 }
65
66 void volume_sound_feedback_play(int feedback_type, int pattern)
67 {
68         _D("Feedback type : %d / pattern : %d", feedback_type, pattern);
69
70         if (FEEDBACK_ERROR_NONE != feedback_play_type(feedback_type, pattern))
71         {
72                 _E("Failed to play feedback");
73         }
74 }
75
76 void volume_sound_vib_play(void)
77 {
78         ret_if(s_info.is_vibration);
79
80         _D("Play Feedback : vibration");
81
82         if (FEEDBACK_ERROR_NONE != feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_VIBRATION_ON))
83         {
84                 _E("Failed to play feedback");
85         }
86
87         s_info.is_vibration = EINA_TRUE;
88 }
89
90 void volume_sound_play(void)
91 {
92         sound_type_e sound_type = volume_control_sound_type_at_show_get();
93         _D("sound type at show : %d", sound_type);
94
95         ret_if(sound_type == SOUND_TYPE_ALARM);
96         ret_if(sound_type == SOUND_TYPE_MEDIA);
97         ret_if(sound_type == SOUND_TYPE_VOICE);
98         ret_if(sound_type == SOUND_TYPE_NOTIFICATION);
99
100         _D("Play Feedback : sound");
101         if(FEEDBACK_ERROR_NONE != feedback_play(FEEDBACK_PATTERN_VOLUME_KEY)) {
102                 _E("Failed to play feedback");
103         }
104
105         return;
106 }
107
108 static int _setting_sound_enabled(void)
109 {
110         int snd_status = 0;
111
112         if(vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status) < 0)
113         {
114                 _E("Failed to get vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
115         }
116
117         if (snd_status == 1)
118         {
119                 return 1;
120         }
121
122         return 0;
123 }
124
125 //void _play_setting_sound(void)
126 void volume_sound_setting_sound_play(void)
127 {
128         int snd_enabled = _setting_sound_enabled();
129         _D("snd_enabled(%d)", snd_enabled);
130
131         if(snd_enabled == 1)
132         {
133                 //feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TOUCH_TAP);
134                 volume_sound_feedback_play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TOUCH_TAP);
135         }
136 }
137
138 int volume_sound_sound_manager_step_get(sound_type_e type)
139 {
140         int ret = -1, step = 0;
141
142         ret = sound_manager_get_max_volume(type, &step);
143         retvm_if(ret < 0, -1, "Failed to get step");
144
145         s_info.sound_step = step;
146
147         return step;
148 }
149
150 int volume_sound_sound_manager_volume_get(sound_type_e type)
151 {
152         int ret = -1, val = 0;
153         ret = sound_manager_get_volume(type, &val);
154         retvm_if(ret < 0, -1, "Failed to get volume");
155         return val;
156 }
157
158 //sound_type_e _get_volume_type
159 sound_type_e volume_sound_sound_manager_type_get(void)
160 {
161         _D(" Volume sound manager type get");
162         int ret = 0;
163         sound_type_e sound_type = -1;
164
165         ret = sound_manager_get_current_sound_type(&sound_type);
166         switch (ret) {
167         case SOUND_MANAGER_ERROR_NONE:
168                 break;
169         case SOUND_MANAGER_ERROR_NO_PLAYING_SOUND:
170                 sound_type = SOUND_TYPE_RINGTONE;
171                 break;
172         default:
173                 _E("Failed to get sound type : %d", ret);
174                 sound_type = SOUND_TYPE_RINGTONE;
175                 return sound_type;
176         }
177
178         if(sound_type == SOUND_TYPE_SYSTEM) {
179                 sound_type = SOUND_TYPE_RINGTONE;
180         }
181
182         return sound_type;
183 }
184
185 void _mm_func(sound_type_e type, unsigned int volume, void *data)
186 {
187         _D("type : %d, volume : %d", type, volume);
188         if(type == volume_control_sound_type_at_show_get()){
189                 _D("current show type : %d, volume : %d", type, volume);
190                 /* Need to check sound type & device in media type volume. */
191                 if(type == SOUND_TYPE_MEDIA){
192                         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
193                         _D("sound status : %d", sound);
194
195                         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
196                         _D("vibration : %d", vibration);
197                         volume_view_volume_icon_set(type, sound, vibration);
198                 }
199                 if(VOLUME_ERROR_OK != volume_view_slider_value_set(volume))
200                 {
201                         _E("Failed to set slider value");
202                 }
203         }
204 }
205
206 //int _init_mm_sound(void *data);
207 void volume_sound_mm_sound_init(void)
208 {
209         int ret = sound_manager_set_volume_changed_cb(_mm_func, NULL);
210         if (ret != SOUND_MANAGER_ERROR_NONE)
211         {
212                 _E("Failed to set volume changed event[%d]", ret);
213         }
214 }
215
216 //int _get_sound_level(sound_type_e type, int *val)
217 int volume_sound_level_get(sound_type_e type)
218 {
219         int val = -1;
220         if(sound_manager_get_volume(type, &val) < 0)
221         {
222                 _E("Failed to get Volume step");
223                 return -1;
224         }
225
226         return val;
227 }
228
229 //int _set_sound_level(sound_type_e type, int val)
230 int volume_sound_level_set(sound_type_e type, int val)
231 {
232         int ret = -1;
233         ret = sound_manager_set_volume(type, val);
234
235         return ret;
236 }
237
238 void volume_sound_change_set(int val)
239 {
240         sound_type_e sound_type = volume_control_sound_type_at_show_get();
241         _D("sound type at show : %d", sound_type);
242
243         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
244         _D("sound status : %d", sound);
245
246         if (val)
247         {
248                 if(sound_type != SOUND_TYPE_MEDIA)
249                 {
250                         if(!volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS))
251                         {
252                                 volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
253                                 volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
254                         }
255                 }
256                 if(volume_sound_level_set(sound_type, val) != 0)
257                 {
258                         volume_sound_level_set(sound_type, 9);
259                 }
260
261                 volume_sound_play();
262         }
263         else
264         {
265                 if(sound_type != SOUND_TYPE_MEDIA)
266                 {
267                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
268                 }
269                 volume_view_slider_value_set(0);
270                 volume_sound_level_set(sound_type, 0);
271
272                 if(sound_type == SOUND_TYPE_RINGTONE)
273                 {
274                         volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
275                         volume_sound_vib_play();
276                 }
277         }
278 }
279
280 static void _sound_status_changed_cb(keynode_t *key, void *data)
281 {
282         const char *keyname = vconf_keynode_get_name(key);
283         ret_if(!keyname);
284         _D("keyname : %s", keyname);
285
286         sound_type_e sound_type = volume_control_sound_type_at_show_get();
287         _D("sound type at show : %d", sound_type);
288
289         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
290         _D("sound status : %d", sound);
291         s_info.sound_status = sound;
292
293         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
294         _D("vibration : %d", vibration);
295         s_info.vibration_status = vibration;
296
297         if(sound == 0 && vibration == 1)
298         {
299                 _D("vibration mode");
300         } else if(sound == 1 && vibration == 0)
301         {
302                 _D("sound mode");
303                 s_info.is_vibration = EINA_FALSE;
304         } else if(sound == 0 && vibration == 0)
305         {
306                 _D("mute mode");
307                 s_info.is_vibration = EINA_FALSE;
308         }
309
310         volume_view_volume_icon_set(sound_type, sound, vibration);
311 }
312
313 void volume_sound_vconf_status_set(volume_vconf_type_e type, int val)
314 {
315         char *vconfkey = NULL;
316         switch(type)
317         {
318         case TYPE_VCONF_SOUND_STATUS:
319                 vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
320                 break;
321         case TYPE_VCONF_VIBRATION_STATUS:
322                 vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
323                 break;
324         default:
325                 _E("Failed to set vconfkey : Type error");
326                 return;
327         }
328
329         if(vconf_set_bool(vconfkey, val) < 0)
330         {
331                 _E("Failed to set vconfkey : %s", vconfkey);
332         }
333 }
334
335 int volume_sound_vconf_status_get(volume_vconf_type_e type)
336 {
337         int ret = -1;
338         char *vconfkey = NULL;
339
340         switch(type)
341         {
342         case TYPE_VCONF_SOUND_STATUS:
343                 vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
344                 break;
345         case TYPE_VCONF_VIBRATION_STATUS:
346                 vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
347                 break;
348         default:
349                 _E("Failed to get vconfkey : Type error");
350                 return -1;
351         }
352
353         if(vconf_get_bool(vconfkey, &ret) < 0)
354         {
355                 _E("Failed to get vconfkey : %s", vconfkey);
356                 return -1;
357         }
358
359         return ret;
360 }
361
362 void volume_sound_vconfkey_register(void)
363 {
364         if(vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
365         {
366                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
367         }
368
369         if(vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
370         {
371                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
372         }
373         _sound_status_changed_cb(NULL, NULL);
374 }
375
376 void volume_sound_vconfkey_unregister(void)
377 {
378         if(vconf_ignore_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb) < 0)
379         {
380                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
381         }
382
383         if(vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb) < 0)
384         {
385                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
386         }
387 }