sync with private git. updated the license and the boilerplates
[apps/home/quickpanel.git] / daemon / media.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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://floralicense.org
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 #include <stdio.h>
17 #include <vconf.h>
18 #include "common.h"
19 #include "quickpanel-ui.h"
20
21 static player_h g_sound_player;
22 static Ecore_Timer *g_sound_player_timer;
23
24 static void _quickpanel_player_free_job_cb(void *data)
25 {
26         player_h sound_player = data;
27         player_state_e state = PLAYER_STATE_NONE;
28
29         retif(sound_player == NULL, , "invalid parameter");
30
31         if (player_get_state(sound_player, &state) == PLAYER_ERROR_NONE) {
32
33                 INFO("the state of sound player %d", state);
34
35                 if (state == PLAYER_STATE_PLAYING) {
36                         player_stop(sound_player);
37                         player_unprepare(sound_player);
38                 }
39                 if (state == PLAYER_STATE_READY) {
40                         player_unprepare(sound_player);
41                 }
42         }
43         player_destroy(sound_player);
44 }
45
46 static void _quickpanel_player_free(player_h *sound_player)
47 {
48         retif(sound_player == NULL, , "invalid parameter");
49         retif(*sound_player == NULL, , "invalid parameter");
50
51         ecore_job_add(_quickpanel_player_free_job_cb, *sound_player);
52         *sound_player = NULL;
53 }
54
55 static void
56 _quickpanel_player_del_timeout_timer(void)
57 {
58         if (g_sound_player_timer) {
59                 ecore_timer_del(g_sound_player_timer);
60                 g_sound_player_timer = NULL;
61         }
62 }
63
64 static Eina_Bool _quickpanel_player_timeout_cb(void *data)
65 {
66         g_sound_player_timer = NULL;
67
68         retif(data == NULL, ECORE_CALLBACK_CANCEL, "invalid parameter");
69         player_h *sound_player = data;
70
71         _quickpanel_player_free(sound_player);
72         g_sound_player_timer = NULL;
73
74         return ECORE_CALLBACK_CANCEL;
75 }
76
77 static void
78 _quickpanel_player_completed_cb(void *user_data)
79 {
80         retif(user_data == NULL, , "invalid parameter");
81         player_h *sound_player = user_data;
82
83         _quickpanel_player_del_timeout_timer();
84         _quickpanel_player_free(sound_player);
85 }
86
87 static void
88 _quickpanel_player_interrupted_cb(player_interrupted_code_e code, void *user_data)
89 {
90         retif(user_data == NULL, , "invalid parameter");
91         player_h *sound_player = user_data;
92
93         _quickpanel_player_del_timeout_timer();
94         _quickpanel_player_free(sound_player);
95 }
96
97 static void
98 _quickpanel_player_error_cb(int error_code, void *user_data)
99 {
100         retif(user_data == NULL, , "invalid parameter");
101         player_h *sound_player = user_data;
102
103         _quickpanel_player_del_timeout_timer();
104         _quickpanel_player_free(sound_player);
105 }
106
107 HAPI void quickpanel_player_play(sound_type_e sound_type, const char *sound_file)
108 {
109         player_h *sound_player = &g_sound_player;
110
111         int ret = PLAYER_ERROR_NONE;
112         player_state_e state = PLAYER_STATE_NONE;
113
114         _quickpanel_player_del_timeout_timer();
115
116         if (*sound_player != NULL) {
117                 _quickpanel_player_free(sound_player);
118         }
119
120         ret = player_create(sound_player);
121         if (ret != PLAYER_ERROR_NONE) {
122                 ERR("creating the player handle failed[%d]", ret);
123                 player_destroy(*sound_player);
124         }
125
126         ret = player_set_sound_type(*sound_player, sound_type);
127         if (ret != PLAYER_ERROR_NONE) {
128                 ERR("player_set_sound_type() ERR: %x!!!!", ret);
129                 _quickpanel_player_free(sound_player);
130                 return ;
131         }
132
133         player_get_state(*sound_player, &state);
134         if (state > PLAYER_STATE_READY) {
135                 _quickpanel_player_free(sound_player);
136                 return;
137         }
138
139         ret = player_set_uri(*sound_player, sound_file);
140         if (ret != PLAYER_ERROR_NONE) {
141                 ERR("set attribute---profile_uri[%d]", ret);
142                 _quickpanel_player_free(sound_player);
143                 return;
144         }
145
146         ret = player_prepare(*sound_player);
147         if (ret != PLAYER_ERROR_NONE) {
148                 ERR("realizing the player handle failed[%d]", ret);
149                 _quickpanel_player_free(sound_player);
150                 return;
151         }
152
153         player_get_state(*sound_player, &state);
154         if (state != PLAYER_STATE_READY) {
155                 ERR("state of player is invalid %d", state);
156                 _quickpanel_player_free(sound_player);
157                 return;
158         }
159
160         /* register callback */
161         ret = player_set_completed_cb(*sound_player, _quickpanel_player_completed_cb, sound_player);
162         if (ret != PLAYER_ERROR_NONE) {
163                 ERR("player_set_completed_cb() ERR: %x!!!!", ret);
164                 _quickpanel_player_free(sound_player);
165                 return;
166         }
167
168         ret = player_set_interrupted_cb(*sound_player, _quickpanel_player_interrupted_cb, sound_player);
169         if (ret != PLAYER_ERROR_NONE) {
170                 _quickpanel_player_free(sound_player);
171                 return;
172         }
173
174         ret = player_set_error_cb(*sound_player, _quickpanel_player_error_cb, sound_player);
175         if (ret != PLAYER_ERROR_NONE) {
176                 _quickpanel_player_free(sound_player);
177                 return;
178         }
179
180         ret = player_start(*sound_player);
181         if (ret != PLAYER_ERROR_NONE) { /* if directly return retor.. */
182                 ERR("player_start [%d]", ret);
183                 _quickpanel_player_free(sound_player);
184                 return;
185         }
186
187         g_sound_player_timer = ecore_timer_add(QP_PLAY_DURATION_LIMIT,
188                         _quickpanel_player_timeout_cb, sound_player);
189 }
190
191 HAPI void quickpanel_player_stop(void)
192 {
193         _quickpanel_player_del_timeout_timer();
194
195         if (g_sound_player != NULL) {
196                 _quickpanel_player_free(&g_sound_player);
197         }
198 }
199
200 HAPI int quickpanel_is_sound_enabled(void)
201 {
202         int snd_status = 0;
203
204 #ifdef VCONFKEY_SETAPPL_ACCESSIBILITY_TURN_OFF_ALL_SOUNDS
205         int snd_disabled_status = 0;
206
207         vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TURN_OFF_ALL_SOUNDS, &snd_disabled_status);
208         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status);
209
210         if (snd_disabled_status == 0 && snd_status == 1) {
211                 return 1;
212         }
213 #else
214         vconf_get_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, &snd_status);
215
216         if (snd_status == 1) {
217                 return 1;
218         }
219 #endif
220
221         return 0;
222 }
223
224 HAPI int quickpanel_is_vib_enabled(void)
225 {
226         int vib_status = 0;
227
228         vconf_get_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, &vib_status);
229
230         if (vib_status == 1)
231                 return 1;
232         else
233                 return 0;
234 }
235
236 HAPI void quickpanel_play_feedback(void)
237 {
238         int snd_enabled = quickpanel_is_sound_enabled();
239         int vib_enabled = quickpanel_is_vib_enabled();
240
241         if (snd_enabled == 1) {
242                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TOUCH_TAP);
243         } else  if (vib_enabled == 1) {
244                 feedback_play_type(FEEDBACK_TYPE_VIBRATION, FEEDBACK_PATTERN_TOUCH_TAP);
245         }
246 }
247
248 HAPI int quickpanel_set_mute_toggle(void)
249 {
250         int ret = -1;
251
252         if (quickpanel_is_sound_enabled() == 1 ||
253                         quickpanel_is_vib_enabled() == 1) {
254                 ret = vconf_set_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, 0);
255                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
256
257                 ret = vconf_set_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, 0);
258                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
259
260                 return 0;
261         } else {
262                 ret = vconf_set_bool(VCONFKEY_SETAPPL_SOUND_STATUS_BOOL, 1);
263                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_SOUND_STATUS_BOOL");
264
265                 ret = vconf_set_bool(VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL, 0);
266                 msgif(ret != 0, "failed to set VCONFKEY_SETAPPL_VIBRATION_STATUS_BOOL");
267
268                 return 1;
269         }
270 }