Update volume icon and modify sound_type logic
[apps/native/volume-app.git] / src / sound.c
1 /*
2  * Copyright (c) 2009-2015 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         int sound_changed_callback_id;
35         Eina_Bool is_vibration;
36 };
37
38 static struct _sound_s_info sound_info = {
39         .sound_step = 0,
40         .sound_status = 0,
41         .vibration_status = 0,
42         .sound_changed_callback_id = 1,
43         .is_vibration = EINA_FALSE,
44 };
45
46 static void _sound_status_changed_cb(keynode_t *key, void *data);
47 static int _setting_sound_enabled(void);
48
49 int volume_sound_step_get(void)
50 {
51         return sound_info.sound_step;
52 }
53
54 int volume_sound_sound_status_get(void)
55 {
56         return sound_info.sound_status;
57 }
58
59 int volume_sound_vibration_status_get(void)
60 {
61         return sound_info.vibration_status;
62 }
63
64 Eina_Bool volume_sound_is_vibration_get(void)
65 {
66         return sound_info.is_vibration;
67 }
68
69 void volume_sound_is_vibration_set(Eina_Bool val)
70 {
71         sound_info.is_vibration = val;
72 }
73
74 void volume_sound_feedback_play(int feedback_type, int pattern)
75 {
76         _D("Feedback type : %d / pattern : %d", feedback_type, pattern);
77
78         if (FEEDBACK_ERROR_NONE != feedback_play_type(feedback_type, pattern))
79                 _E("Failed to play feedback");
80 }
81
82 void volume_sound_vib_play(void)
83 {
84         ret_if(sound_info.is_vibration);
85
86         _D("Play Feedback : vibration");
87
88         if (FEEDBACK_ERROR_NONE != feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_VIBRATION_ON)) {
89                 _E("Failed to play feedback");
90         }
91
92         sound_info.is_vibration = EINA_TRUE;
93 }
94
95 void volume_sound_play(void)
96 {
97         sound_type_e sound_type = volume_control_get_sound_type_at_show();
98         _D("sound type at show : %d", sound_type);
99
100         ret_if(sound_type == SOUND_TYPE_ALARM);
101         ret_if(sound_type == SOUND_TYPE_MEDIA);
102         ret_if(sound_type == SOUND_TYPE_VOICE);
103         ret_if(sound_type == SOUND_TYPE_NOTIFICATION);
104
105         _D("Play Feedback : sound");
106         if(FEEDBACK_ERROR_NONE != feedback_play(FEEDBACK_PATTERN_VOLUME_KEY)) {
107                 _E("Failed to play feedback");
108         }
109
110         return;
111 }
112
113 void volume_sound_setting_sound_play(void)
114 {
115         int snd_enabled = _setting_sound_enabled();
116         _D("snd_enabled(%d)", snd_enabled);
117
118         if(snd_enabled == 1)
119                 volume_sound_feedback_play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP);
120 }
121
122 int volume_sound_sound_manager_step_get(sound_type_e type)
123 {
124         _D("volume sound manager step get");
125         int ret = -1, step = 0;
126
127         ret = sound_manager_get_max_volume(type, &step);
128         retvm_if(ret < 0, -1, "Failed to get step");
129
130         sound_info.sound_step = step;
131
132         return step;
133 }
134
135 int volume_sound_sound_manager_volume_get(sound_type_e type)
136 {
137         int ret = -1, val = 0;
138         ret = sound_manager_get_volume(type, &val);
139         retvm_if(ret < 0, -1, "Failed to get volume");
140         return val;
141 }
142
143 sound_type_e volume_sound_sound_manager_type_get(void)
144 {
145         _D(" Volume sound manager type get");
146         int ret = 0;
147         sound_type_e sound_type = -1;
148
149         ret = sound_manager_get_current_sound_type(&sound_type);
150         _D("ret: %d, sound type: %d", ret, sound_type);
151         switch (ret) {
152         case SOUND_MANAGER_ERROR_NONE:
153                 _D("Error none");
154                 break;
155         case SOUND_MANAGER_ERROR_NO_PLAYING_SOUND:
156                 _D("NO playing sound");
157                 sound_type = SOUND_TYPE_RINGTONE;
158                 break;
159         default:
160                 _E("Failed to get sound type : %d", ret);
161                 sound_type = SOUND_TYPE_RINGTONE;
162                 break;
163         }
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_add_volume_changed_cb(_mm_func, NULL, &sound_info.sound_changed_callback_id);
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                 }
239                 volume_view_slider_value_set(0);
240                 volume_sound_level_set(sound_type, 0);
241
242                 if (sound_type == SOUND_TYPE_RINGTONE) {
243                         volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 1);
244                         volume_sound_vib_play();
245                 }
246         }
247 }
248
249 void volume_sound_vconf_status_set(volume_vconf_type_e type, int val)
250 {
251         char *vconfkey = NULL;
252         switch (type)
253         {
254         case TYPE_VCONF_SOUND_STATUS:
255                 vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
256                 break;
257         case TYPE_VCONF_VIBRATION_STATUS:
258                 vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
259                 break;
260         default:
261                 _E("Failed to set vconfkey : Type error");
262                 return;
263         }
264
265         if (vconf_set_bool(vconfkey, val) < 0)
266                 _E("Failed to set vconfkey : %s", vconfkey);
267 }
268
269 int volume_sound_vconf_status_get(volume_vconf_type_e type)
270 {
271         int ret = -1;
272         char *vconfkey = NULL;
273
274         switch (type)
275         {
276         case TYPE_VCONF_SOUND_STATUS:
277                 vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
278                 break;
279         case TYPE_VCONF_VIBRATION_STATUS:
280                 vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
281                 break;
282         default:
283                 _E("Failed to get vconfkey : Type error");
284                 return -1;
285         }
286
287         if (vconf_get_bool(vconfkey, &ret) < 0) {
288                 _E("Failed to get vconfkey : %s", vconfkey);
289                 return -1;
290         }
291
292         return ret;
293 }
294
295 void volume_sound_vconfkey_register(void)
296 {
297         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
298                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
299
300         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
301                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
302         _sound_status_changed_cb(NULL, NULL);
303 }
304
305 void volume_sound_vconfkey_unregister(void)
306 {
307         if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb) < 0)
308                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
309
310         if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb) < 0)
311                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
312 }
313
314 static int _setting_sound_enabled(void)
315 {
316         int snd_status = 0;
317
318         if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status) < 0)
319                 _E("Failed to get vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
320
321         if (snd_status == 1)
322                 return 1;
323
324         return 0;
325 }
326
327 static void _sound_status_changed_cb(keynode_t *key, void *data)
328 {
329         bool bt_opened = false;
330         const char *keyname = vconf_keynode_get_name(key);
331         ret_if(!keyname);
332         _D("keyname : %s", keyname);
333
334         sound_type_e sound_type = volume_control_get_sound_type_at_show();
335         _D("sound type at show : %d", sound_type);
336
337         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
338         _D("sound status : %d", sound);
339         sound_info.sound_status = sound;
340
341         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
342         _D("vibration : %d", vibration);
343         sound_info.vibration_status = vibration;
344
345         if (sound == 0 && vibration == 1)
346                 _D("vibration mode");
347         else if (sound == 1 && vibration == 0) {
348                 _D("sound mode");
349                 sound_info.is_vibration = EINA_FALSE;
350         } else if (sound == 0 && vibration == 0) {
351                 _D("mute mode");
352                 sound_info.is_vibration = EINA_FALSE;
353         }
354
355         volume_view_volume_icon_set(sound_type, sound, vibration, bt_opened);
356 }
357