Fix coverity issue 1149129
[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         sound_info.is_vibration = EINA_TRUE;
92 }
93
94 void volume_sound_play(void)
95 {
96         sound_type_e sound_type = volume_control_get_sound_type_at_show();
97         _D("sound type at show : %d", sound_type);
98
99         ret_if(sound_type == SOUND_TYPE_ALARM);
100         ret_if(sound_type == SOUND_TYPE_MEDIA);
101         ret_if(sound_type == SOUND_TYPE_VOICE);
102         ret_if(sound_type == SOUND_TYPE_NOTIFICATION);
103
104         _D("Play Feedback : sound");
105         if (FEEDBACK_ERROR_NONE != feedback_play(FEEDBACK_PATTERN_VOLUME_KEY))
106                 _E("Failed to play feedback");
107
108         return;
109 }
110
111 void volume_sound_setting_sound_play(void)
112 {
113         int snd_enabled = _setting_sound_enabled();
114         _D("snd_enabled(%d)", snd_enabled);
115
116         if (snd_enabled == 1)
117                 volume_sound_feedback_play(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP);
118 }
119
120 int volume_sound_sound_manager_step_get(sound_type_e type)
121 {
122         _D("volume sound manager step get");
123         int ret = -1, step = 0;
124
125         ret = sound_manager_get_max_volume(type, &step);
126         retvm_if(ret < 0, -1, "Failed to get step");
127
128         sound_info.sound_step = step;
129
130         return step;
131 }
132
133 int volume_sound_sound_manager_volume_get(sound_type_e type)
134 {
135         int ret = -1, val = 0;
136         ret = sound_manager_get_volume(type, &val);
137         retvm_if(ret < 0, -1, "Failed to get volume");
138         return val;
139 }
140
141 sound_type_e volume_sound_sound_manager_type_get(void)
142 {
143         _D(" Volume sound manager type get");
144         int ret = 0;
145         sound_type_e sound_type = -1;
146
147         ret = sound_manager_get_current_sound_type(&sound_type);
148         _D("ret: %d, sound type: %d", ret, sound_type);
149         switch (ret) {
150         case SOUND_MANAGER_ERROR_NONE:
151                 _D("Error none");
152                 break;
153         case SOUND_MANAGER_ERROR_NO_PLAYING_SOUND:
154                 _D("NO playing sound");
155                 sound_type = SOUND_TYPE_RINGTONE;
156                 break;
157         default:
158                 _E("Failed to get sound type : %d", ret);
159                 sound_type = SOUND_TYPE_RINGTONE;
160                 break;
161         }
162
163         return sound_type;
164 }
165
166 void _mm_func(sound_type_e type, unsigned int volume, void *data)
167 {
168         bool bt_opened = false;
169
170         _D("mm func is called type : %d, volume : %d", type, volume);
171         if (type == volume_control_get_sound_type_at_show()) {
172                 _D("current show type : %d, volume : %d", type, volume);
173                 /* Need to check sound type & device in media type volume. */
174                 if (type == SOUND_TYPE_MEDIA) {
175                         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
176                         _D("sound status : %d", sound);
177
178                         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
179                         _D("vibration : %d", vibration);
180                         volume_view_volume_icon_set(type, sound, vibration, bt_opened);
181                 }
182                 if (VOLUME_ERROR_OK != volume_view_slider_value_set(volume))
183                         _E("Failed to set slider value");
184         }
185 }
186
187 void volume_sound_mm_sound_init(void)
188 {
189         _D("MM sound Init");
190         int ret = sound_manager_add_volume_changed_cb(_mm_func, NULL, &sound_info.sound_changed_callback_id);
191         if (ret != SOUND_MANAGER_ERROR_NONE)
192                 _E("Failed to set volume changed event[%d]", ret);
193 }
194
195 int volume_sound_level_get(sound_type_e type)
196 {
197         int val = -1;
198         if (sound_manager_get_volume(type, &val) < 0) {
199                 _E("Failed to get Volume step");
200                 return -1;
201         }
202
203         return val;
204 }
205
206 int volume_sound_level_set(sound_type_e type, int val)
207 {
208         int ret = -1;
209         ret = sound_manager_set_volume(type, val);
210
211         return ret;
212 }
213
214 void volume_sound_change_set(int val)
215 {
216         sound_type_e sound_type = volume_control_get_sound_type_at_show();
217         _D("sound type at show : %d", sound_type);
218
219         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
220         _D("sound status : %d", sound);
221
222         if (val) {
223                 if (sound_type != SOUND_TYPE_MEDIA) {
224                         if (!volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS)) {
225                                 volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 1);
226                                 volume_sound_vconf_status_set(TYPE_VCONF_VIBRATION_STATUS, 0);
227                         }
228                 }
229                 if (volume_sound_level_set(sound_type, val) != 0)
230                         volume_sound_level_set(sound_type, 9);
231
232                 volume_sound_play();
233         } else {
234                 if (sound_type != SOUND_TYPE_MEDIA)
235                         volume_sound_vconf_status_set(TYPE_VCONF_SOUND_STATUS, 0);
236
237                 if (VOLUME_ERROR_OK != volume_view_slider_value_set(0))
238                         _E("Failed to set slider value");
239
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         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         case TYPE_VCONF_SOUND_STATUS:
275                 vconfkey = VCONFKEY_SETAPPL_SOUND_STATUS_BOOL;
276                 break;
277         case TYPE_VCONF_VIBRATION_STATUS:
278                 vconfkey = VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL;
279                 break;
280         default:
281                 _E("Failed to get vconfkey : Type error");
282                 return -1;
283         }
284
285         if (vconf_get_bool(vconfkey, &ret) < 0) {
286                 _E("Failed to get vconfkey : %s", vconfkey);
287                 return -1;
288         }
289
290         return ret;
291 }
292
293 void volume_sound_vconfkey_register(void)
294 {
295         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
296                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
297
298         if (vconf_notify_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb, NULL) < 0)
299                 _E("Failed to notify vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
300         _sound_status_changed_cb(NULL, NULL);
301 }
302
303 void volume_sound_vconfkey_unregister(void)
304 {
305         if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, _sound_status_changed_cb) < 0)
306                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
307
308         if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, _sound_status_changed_cb) < 0)
309                 _E("Failed to ignore vconfkey : VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
310 }
311
312 static int _setting_sound_enabled(void)
313 {
314         int snd_status = 0;
315
316         if (vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status) < 0)
317                 _E("Failed to get vconfkey : VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
318
319         if (snd_status == 1)
320                 return 1;
321
322         return 0;
323 }
324
325 static void _sound_status_changed_cb(keynode_t *key, void *data)
326 {
327         bool bt_opened = false;
328         const char *keyname = vconf_keynode_get_name(key);
329         ret_if(!keyname);
330         _D("keyname : %s", keyname);
331
332         sound_type_e sound_type = volume_control_get_sound_type_at_show();
333         _D("sound type at show : %d", sound_type);
334
335         int sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
336         _D("sound status : %d", sound);
337         sound_info.sound_status = sound;
338
339         int vibration = volume_sound_vconf_status_get(TYPE_VCONF_VIBRATION_STATUS);
340         _D("vibration : %d", vibration);
341         sound_info.vibration_status = vibration;
342
343         if (sound == 0 && vibration == 1)
344                 _D("vibration mode");
345         else if (sound == 1 && vibration == 0) {
346                 _D("sound mode");
347                 sound_info.is_vibration = EINA_FALSE;
348         } else if (sound == 0 && vibration == 0) {
349                 _D("mute mode");
350                 sound_info.is_vibration = EINA_FALSE;
351         }
352
353         volume_view_volume_icon_set(sound_type, sound, vibration, bt_opened);
354 }
355