Tizen release 1.0
[apps/core/preloaded/video-player.git] / src / mp-video-sound-path-ctrl.c
1 /*
2  * Copyright   2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * This file is part of org.tizen.video-player
5  * Written by  JongDong Lee <jongdong.lee@samsung.com>, ChangSun Lee <cs78.lee@samsung.com>
6  *
7  * PROPRIETARY/CONFIDENTIAL
8  *
9  * This software is the confidential and proprietary information of SAMSUNG ELECTRONICS ("Confidential Information").
10  * You shall not disclose such Confidential Information and shall
11  * use it only in accordance with the terms of the license agreement you entered into with SAMSUNG ELECTRONICS.
12  * SAMSUNG make no representations or warranties about the suitability
13  * of the software, either express or implied, including but not
14  * limited to the implied warranties of merchantability, fitness for a particular purpose, or non-infringement.
15  * SAMSUNG shall not be liable for any damages suffered by licensee as
16  * a result of using, modifying or distributing this software or its derivatives.
17  *
18  */
19
20
21 #include <mm_sound.h>
22 #include <Ecore_Evas.h>
23 #include <Elementary.h>
24 #include <appcore-efl.h>
25 #include <appcore-common.h>
26
27 #include "mp-util.h"
28 #include "mp-video-log.h"
29 #include "video-player.h"
30 #include "mp-video-type-define.h"
31 #include "mp-video-value-define.h"
32 #include "mp-video-string-define.h"
33 #include "mp-video-sound-path-ctrl.h"
34
35 #define MP_VIDEO_SOUND_PATH_NUM                 10
36 #define MP_VIDEO_SOUND_PATH_SPEAKER             "Speaker"
37 #define MP_VIDEO_SOUND_PATH_EARJACK             "Headphones"
38
39 static Evas_Object *pVideoSoundPathPopUpHandle = NULL;
40 static Evas_Object *pGroupRadio = NULL;
41 static Evas_Object *pGenList = NULL;
42
43 static Elm_Genlist_Item_Class st_Itc;
44 static char szSoundPathItems[MP_VIDEO_SOUND_PATH_NUM][STR_LEN_MAX];
45 static bool bIsConnectBluetooth = FALSE;
46 static bool bIsConnectEarjack = FALSE;
47
48 void MpVideoSoundpathDeleteRadioButton(void)
49 {
50         VideoLogInfo("");
51
52         if (pGroupRadio) {
53                 evas_object_del(pGroupRadio);
54                 pGroupRadio = NULL;
55         }
56 }
57
58 void MpVideoSoundpathDeletePopupHandle(void)
59 {
60         VideoLogInfo("");
61
62         int nCount = 0;
63
64         for (nCount = 0; nCount < MP_VIDEO_SOUND_PATH_NUM; nCount++) {
65                 memset(&szSoundPathItems[nCount][0], 0,
66                        sizeof(char) * STR_LEN_MAX);
67         }
68
69         MpVideoSoundpathDeleteRadioButton();
70
71         memset(&st_Itc, 0, sizeof(Elm_Genlist_Item_Class));
72
73         if (pGenList) {
74                 evas_object_del(pGenList);
75                 pGenList = NULL;
76         }
77
78         if (pVideoSoundPathPopUpHandle) {
79                 evas_object_del(pVideoSoundPathPopUpHandle);
80                 pVideoSoundPathPopUpHandle = NULL;
81         }
82
83  }
84
85 static char *MpVideoSoundpathGetLabelOfGenlistItemCb(const void *pUserData,
86                                                      Evas_Object *pObject,
87                                                      const char *pPart)
88 {
89         VideoLogInfo("");
90
91         int nIndex = (int)pUserData;
92
93         if (!strcmp(pPart, "elm.text")) {
94                 VideoLogInfo("Index : %d", nIndex);
95                 return strdup(&szSoundPathItems[nIndex][0]);
96         }
97
98         return NULL;
99 }
100
101 static Evas_Object *MpVideoSoundpathGetIconOfGenlistItemCb(const void
102                                                            *pUserData,
103                                                            Evas_Object *pObject,
104                                                            const char *pPart)
105 {
106         VideoLogInfo("");
107
108         int nIndex = (int)pUserData;
109         Evas_Object *pTmpRadio = NULL;
110
111         if (!strcmp(pPart, "elm.icon")) {
112                 VideoLogInfo("Index : %d", nIndex);
113                 pTmpRadio = elm_radio_add(pObject);
114                 elm_radio_state_value_set(pTmpRadio, nIndex);
115                 evas_object_show(pTmpRadio);
116                 elm_radio_group_add(pTmpRadio, pGroupRadio);
117         }
118
119         return pTmpRadio;
120 }
121
122 static void MpVideoSoundpathSelectSoundPathItemCb(void *pUserData,
123                                                   Evas_Object *pObject,
124                                                   void *pEventInfo)
125 {
126         if (pUserData == NULL) {
127                 VideoLogInfo("[ERR]No have pUserData");
128                 return;
129         }
130
131         VideoLogInfo("");
132
133         int nIndex = 0;
134         Elm_Object_Item *pItem = (Elm_Object_Item *) pEventInfo;
135
136         if (pItem) {
137                 nIndex = (int)elm_object_item_data_get(pItem);
138                 VideoLogInfo("Select sound path : %s", szSoundPathItems[nIndex]);
139
140                 if (!strcmp(szSoundPathItems[nIndex], MP_VIDEO_SOUND_PATH_SPEAKER)) {
141                         int nRet = 0;
142
143                         if (MpUtilIsConnectEarjack()) {
144                                 nRet = mm_sound_route_set_system_policy(SYSTEM_AUDIO_ROUTE_POLICY_HANDSET_ONLY);
145                         } else {
146                                 nRet = mm_sound_route_set_system_policy(SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP);
147                         }
148
149                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
150                 } else if (!strcmp(szSoundPathItems[nIndex], MP_VIDEO_SOUND_PATH_EARJACK)) {
151                         int nRet = mm_sound_route_set_system_policy(SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP);
152                         if (nRet != MM_ERROR_NONE) {
153                                 VideoLogInfo("[ERR] mm_sound_route_set_system_policy(0x%x)",  nRet);
154                         } else {
155                                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_EARJACK);
156                         }
157                 } else {
158                         int nRet = mm_sound_route_set_system_policy(SYSTEM_AUDIO_ROUTE_POLICY_DEFAULT);
159                         if (nRet != MM_ERROR_NONE) {
160                                 VideoLogInfo("[ERR] mm_sound_route_set_system_policy(0x%x)", nRet);
161                         } else {
162                                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_BLUETOOTH);
163                         }
164                 }
165         }
166
167         MpVideoSoundpathDeletePopupHandle();
168 }
169
170 static void MpVideoSoundpathPopupCb(void *pUserData, Evas_Object *pObject,
171                                     void *pEventInfo)
172 {
173         VideoLogInfo("");
174
175         if (pUserData == NULL) {
176                 VideoLogInfo("[ERR]No have pUserData");
177                 return;
178         }
179
180         MpVideoSoundpathDeletePopupHandle();
181 }
182
183 void MpVideoSoundpathCurrentState(Evas_Object *pParent)
184 {
185         VideoLogInfo("");
186
187         int nBluetoothConnect = 0;
188         char *szBluetoothName = NULL;
189         bIsConnectEarjack = FALSE;
190         bIsConnectBluetooth = FALSE;
191
192         /* Speaker */
193         strncpy(&szSoundPathItems[MP_SOUND_PATH_SPEAKER][0], MP_VIDEO_SOUND_PATH_SPEAKER, STR_LEN_MAX - 1);
194
195         /*  Check and Add earjack sound path. */
196         if (MpUtilIsConnectEarjack()) {
197                 VideoLogInfo("Connect earjack.");
198                 bIsConnectEarjack = TRUE;
199                 strncpy(&szSoundPathItems[MP_SOUND_PATH_EARJACK][0], MP_VIDEO_SOUND_PATH_EARJACK, STR_LEN_MAX - 1);
200         } else {
201                 VideoLogInfo("No connect earjack.");
202         }
203
204         /*  Add bluetooth stereo sound path. */
205         if (MM_ERROR_NONE != mm_sound_route_get_a2dp_status(&nBluetoothConnect, &szBluetoothName)) {
206                 VideoLogInfo("[ERR] Fail to check bluetooth stereo sound path.");
207         } else {
208                 if (nBluetoothConnect) {
209                         VideoLogInfo("Connect szBluetoothName : %s", szBluetoothName);
210                         bIsConnectBluetooth = TRUE;
211                         strncpy(&szSoundPathItems[MP_SOUND_PATH_BLUETOOTH][0], szBluetoothName, STR_LEN_MAX - 1);
212                 } else {
213                         VideoLogInfo("Unactivate Bluetooth stereo A2DP.");
214                 }
215         }
216 }
217
218 void MpVideoSoundpathSetRadioButton(void)
219 {
220         VideoLogInfo("");
221
222         int nBluetoothConnect = 0;
223         char *szBluetoothName = NULL;
224         system_audio_route_t nPolicy = 0;
225
226         mm_sound_route_get_system_policy(&nPolicy);
227
228         if (nPolicy == SYSTEM_AUDIO_ROUTE_POLICY_DEFAULT) {
229                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_DEFAULT");
230
231                 if (MM_ERROR_NONE != mm_sound_route_get_a2dp_status(&nBluetoothConnect, &szBluetoothName)) {
232                         VideoLogInfo("[ERR] Fail to check bluetooth stereo sound path.");
233                 }
234
235                 if (nBluetoothConnect) {
236                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_BLUETOOTH);
237                 } else if (MpUtilIsConnectEarjack()) {
238                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_EARJACK);
239                 } else {
240                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
241                 }
242         } else if (nPolicy == SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP) {
243                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP");
244
245                 if (MpUtilIsConnectEarjack()) {
246                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_EARJACK);
247                 } else {
248                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
249                 }
250         } else {
251                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_HANDSET_ONLY");
252
253                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
254         }
255 }
256
257 /*
258  * External function
259  */
260 bool MpVideoSoundpathShow(void *pUserData)
261 {
262         if (pUserData == NULL) {
263                 VideoLogInfo("No have popup message or pAppData is null.");
264                 return FALSE;
265         }
266
267         VideoLogInfo("");
268
269         MpVideoSoundpathIsConnectOtherSoundpath();
270
271         if (!bIsConnectBluetooth && !bIsConnectEarjack) {
272                 VideoLogInfo("Soundpath is only speaker.");
273                 return FALSE;
274         }
275
276         Evas_Object *pBox = NULL;
277         Evas_Object *pButton = NULL;
278         VideoAppData *pAppData = (VideoAppData *)pUserData;
279
280         MpVideoSoundpathDeletePopupHandle();
281
282         pVideoSoundPathPopUpHandle = elm_popup_add(pAppData->pMainWindow);
283
284         MpVideoSoundpathCurrentState(pVideoSoundPathPopUpHandle);
285
286         elm_object_style_set(pVideoSoundPathPopUpHandle, "min_menustyle");
287         elm_object_part_text_set(pVideoSoundPathPopUpHandle, "title,text", MP_VIDEO_SOUND_PATH);
288         evas_object_size_hint_weight_set(pVideoSoundPathPopUpHandle, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
289
290         pButton = elm_button_add(pVideoSoundPathPopUpHandle);
291         elm_object_text_set(pButton, dgettext(MP_SYS_STRING, MP_COM_BUTTON_CANCEL));
292         elm_object_part_content_set(pVideoSoundPathPopUpHandle, "button1", pButton);
293         evas_object_smart_callback_add(pButton, "clicked", MpVideoSoundpathPopupCb, (void*)pAppData);
294
295         /*  Set item of Genlist. */
296         st_Itc.item_style = "1text.1icon.3";
297         st_Itc.func.text_get = (void *)MpVideoSoundpathGetLabelOfGenlistItemCb;
298         st_Itc.func.content_get = (void *)MpVideoSoundpathGetIconOfGenlistItemCb;
299         st_Itc.func.state_get = NULL;
300         st_Itc.func.del = NULL;
301
302         /* Create genlist handle */
303         pGenList = elm_genlist_add(pVideoSoundPathPopUpHandle);
304         evas_object_size_hint_weight_set(pGenList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
305         evas_object_size_hint_align_set(pGenList, EVAS_HINT_FILL, EVAS_HINT_FILL);
306
307         pGroupRadio = elm_radio_add(pGenList);
308
309         if (bIsConnectBluetooth) {
310                 elm_genlist_item_append(pGenList, &st_Itc, (void *)MP_SOUND_PATH_BLUETOOTH, NULL,
311                                         ELM_GENLIST_ITEM_NONE,
312                                         MpVideoSoundpathSelectSoundPathItemCb,
313                                         pAppData);
314         }
315
316         if (bIsConnectEarjack) {
317                 elm_genlist_item_append(pGenList, &st_Itc, (void *)MP_SOUND_PATH_EARJACK, NULL,
318                                         ELM_GENLIST_ITEM_NONE,
319                                         MpVideoSoundpathSelectSoundPathItemCb,
320                                         pAppData);
321         }
322
323         elm_genlist_item_append(pGenList, &st_Itc, (void *)MP_SOUND_PATH_SPEAKER, NULL,
324                                         ELM_GENLIST_ITEM_NONE,
325                                         MpVideoSoundpathSelectSoundPathItemCb,
326                                         pAppData);
327
328         MpVideoSoundpathSetRadioButton();
329
330         pBox = elm_box_add(pVideoSoundPathPopUpHandle);
331         if (bIsConnectBluetooth && bIsConnectEarjack) {
332                 evas_object_size_hint_min_set(pBox, 614 * elm_config_scale_get(), 338 * elm_config_scale_get());
333         } else if(bIsConnectBluetooth || bIsConnectEarjack) {
334                 evas_object_size_hint_min_set(pBox, 614 * elm_config_scale_get(), 225 * elm_config_scale_get());
335         } else {
336                 evas_object_size_hint_min_set(pBox, 614 * elm_config_scale_get(), 451 * elm_config_scale_get());
337         }
338         elm_box_pack_end(pBox, pGenList);
339         elm_object_content_set(pVideoSoundPathPopUpHandle, pBox);
340
341         evas_object_show(pGenList);
342         evas_object_show(pVideoSoundPathPopUpHandle);
343
344         return TRUE;
345 }
346
347 void MpVideoSoundpathHide(void)
348 {
349         VideoLogInfo("");
350
351         MpVideoSoundpathDeletePopupHandle();
352 }
353
354 bool MpVideoSoundpathIsConnectOtherSoundpath(void)
355 {
356         VideoLogInfo("");
357
358         int nBluetoothConnect = 0;
359         char *szBluetoothName = NULL;
360
361         bIsConnectEarjack = FALSE;
362         bIsConnectBluetooth = FALSE;
363
364         if (MpUtilIsConnectEarjack()) {
365                 VideoLogInfo("Connect earjack.");
366                 bIsConnectEarjack= TRUE;
367                 return TRUE;
368         }
369
370         mm_sound_route_get_a2dp_status(&nBluetoothConnect, &szBluetoothName);
371         if (nBluetoothConnect) {
372                 VideoLogInfo("Connect bluetooth.");
373                 bIsConnectBluetooth = TRUE;
374                 return TRUE;
375         }
376
377         VideoLogInfo("Only spaker.");
378         return FALSE;
379 }
380
381 int MpVideoSoundpathGetCurrConnectedSoundpath(void)
382 {
383         VideoLogInfo("");
384
385         int nBluetoothConnect = 0;
386         char *szBluetoothName = NULL;
387         system_audio_route_t nPolicy = 0;
388
389         mm_sound_route_get_system_policy(&nPolicy);
390
391         if (nPolicy == SYSTEM_AUDIO_ROUTE_POLICY_DEFAULT) {
392                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_DEFAULT");
393
394                 if (MM_ERROR_NONE != mm_sound_route_get_a2dp_status(&nBluetoothConnect, &szBluetoothName)) {
395                         VideoLogInfo("[ERR] Fail to check bluetooth stereo sound path.");
396                 }
397
398                 if (nBluetoothConnect) {
399                         return MP_SOUND_PATH_BLUETOOTH;
400                 } else if (MpUtilIsConnectEarjack()) {
401                         return MP_SOUND_PATH_EARJACK;
402                 } else {
403                         return MP_SOUND_PATH_SPEAKER;
404                 }
405         } else if (nPolicy == SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP) {
406                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP");
407
408                 if (MpUtilIsConnectEarjack()) {
409                         return MP_SOUND_PATH_EARJACK;
410                 } else {
411                         return MP_SOUND_PATH_SPEAKER;
412                 }
413         } else {
414                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_HANDSET_ONLY");
415
416                 return MP_SOUND_PATH_SPEAKER;
417         }
418 }