Remove SMACK rule file(.rule) according three domain model
[apps/core/preloaded/video-player.git] / src / mp-video-sound-path-ctrl.c
1 /*
2  * Copyright (c) [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/license/
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 #include <app.h>
18 #include <player.h>
19 #include <sound_manager.h>
20 #include <Ecore_Evas.h>
21 #include <Elementary.h>
22
23 #include "mp-util.h"
24 #include "mp-video-log.h"
25 #include "video-player.h"
26 #include "mp-video-type-define.h"
27 #include "mp-video-value-define.h"
28 #include "mp-video-string-define.h"
29 #include "mp-video-sound-path-ctrl.h"
30
31 #define MP_VIDEO_SOUND_PATH_NUM                 10
32 #define MP_VIDEO_SOUND_PATH_SPEAKER             "Speaker"
33 #define MP_VIDEO_SOUND_PATH_EARJACK             "Headphones"
34
35 static Evas_Object *pVideoSoundPathPopUpHandle = NULL;
36 //static Evas_Object *pVideoSoundPathpPopupWindow = NULL;
37 static Evas_Object *pGroupRadio = NULL;
38 static Evas_Object *pGenList = NULL;
39
40 static char **szSoundPathItems = NULL;
41 static bool bIsConnectBluetooth = FALSE;
42 static bool bIsConnectEarjack = FALSE;
43
44 void MpVideoSoundpathDeleteRadioButton(void)
45 {
46         VideoLogInfo("");
47
48         if (pGroupRadio) {
49                 evas_object_del(pGroupRadio);
50                 pGroupRadio = NULL;
51         }
52 }
53
54 void MpVideoSoundpathDeletePopupHandle(void)
55 {
56         VideoLogInfo("");
57
58         int nCount = 0;
59
60         if(szSoundPathItems)
61         {
62                 for(nCount = 0; nCount < MP_VIDEO_SOUND_PATH_NUM; nCount++)
63                 {
64                         if(szSoundPathItems[nCount])
65                         {
66                                 free(szSoundPathItems[nCount]);
67                                 szSoundPathItems[nCount] = NULL;
68                         }
69                 }
70                 free(szSoundPathItems);
71                 szSoundPathItems = NULL;
72         }
73
74         MpVideoSoundpathDeleteRadioButton();
75
76         if (pGenList) {
77                 evas_object_del(pGenList);
78                 pGenList = NULL;
79         }
80
81         if (pVideoSoundPathPopUpHandle) {
82                 evas_object_del(pVideoSoundPathPopUpHandle);
83                 pVideoSoundPathPopUpHandle = NULL;
84         }
85
86         MpUtilSetAlphaOfMainWindow(TRUE);
87
88         /*
89         if (pVideoSoundPathpPopupWindow) {
90                 evas_object_del(pVideoSoundPathpPopupWindow);
91                 pVideoSoundPathpPopupWindow = NULL;
92         }
93         */
94 }
95
96 static char *MpVideoSoundpathGetLabelOfGenlistItemCb(const void *pUserData,
97                                                      Evas_Object *pObject,
98                                                      const char *pPart)
99 {
100         VideoLogInfo("");
101
102         int nIndex = (int)pUserData;
103
104         if (!strcmp(pPart, "elm.text")) {
105                 VideoLogInfo("Index : %d", nIndex);
106                 return strdup(szSoundPathItems[nIndex]);
107         }
108
109         return NULL;
110 }
111
112 static Evas_Object *MpVideoSoundpathGetIconOfGenlistItemCb(const void
113                                                            *pUserData,
114                                                            Evas_Object *pObject,
115                                                            const char *pPart)
116 {
117         VideoLogInfo("");
118
119         int nIndex = (int)pUserData;
120         Evas_Object *pTmpRadio = NULL;
121
122         if (!strcmp(pPart, "elm.icon")) {
123                 VideoLogInfo("Index : %d", nIndex);
124                 pTmpRadio = elm_radio_add(pObject);
125                 elm_radio_state_value_set(pTmpRadio, nIndex);
126                 evas_object_show(pTmpRadio);
127                 elm_radio_group_add(pTmpRadio, pGroupRadio);
128         }
129
130         return pTmpRadio;
131 }
132
133 static void MpVideoSoundpathSelectSoundPathItemCb(void *pUserData, Evas_Object *pObject, void *pEventInfo)
134 {
135         if(pUserData == NULL)
136         {
137                 VideoLogInfo("[ERR]No have pUserData");
138                 return;
139         }
140
141         VideoLogInfo("");
142
143         VideoAppData *pAppData = (VideoAppData *)pUserData;
144
145         int nIndex = 0;
146         Elm_Object_Item *pItem = (Elm_Object_Item *) pEventInfo;
147
148         if(pItem)
149         {
150                 nIndex = (int)elm_object_item_data_get(pItem);
151                 VideoLogInfo("Select sound path : %s", szSoundPathItems[nIndex]);
152
153                 if(!strcmp(szSoundPathItems[nIndex], MP_VIDEO_SOUND_PATH_SPEAKER))
154                 {
155                         int nRet = 0;
156                         nRet = sound_manager_set_active_route(SOUND_ROUTE_OUT_SPEAKER);
157
158                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
159                 }
160                 else if (!strcmp(szSoundPathItems[nIndex], MP_VIDEO_SOUND_PATH_EARJACK))
161                 {
162                         int nRet = sound_manager_set_active_route(SOUND_ROUTE_OUT_WIRED_ACCESSORY);
163                         if(nRet != SOUND_MANAGER_ERROR_NONE)
164                         {
165                                 VideoLogInfo("[ERR] set_system_policy(0x%x)", nRet);
166                         }
167                         else
168                         {
169                                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_EARJACK);
170                         }
171                 }
172                 else
173                 {
174                         //int nRet = sound_manager_set_route_policy(SOUND_ROUTE_DEFAULT);
175                         int nRet = sound_manager_set_active_route(SOUND_ROUTE_OUT_BLUETOOTH);
176                         if(nRet != SOUND_MANAGER_ERROR_NONE)
177                         {
178                                 VideoLogInfo("[ERR] set_system_policy(0x%x)", nRet);
179                         }
180                         else
181                         {
182                                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_BLUETOOTH);
183                         }
184                 }
185         }
186
187         MpVideoSoundpathDeletePopupHandle();
188 }
189
190 static void MpVideoSoundpathPopupCb(void *pUserData, Evas_Object *pObject,
191                                     void *pEventInfo)
192 {
193         VideoLogInfo("");
194
195         if (pUserData == NULL) {
196                 VideoLogInfo("[ERR]No have pUserData");
197                 return;
198         }
199
200         MpVideoSoundpathDeletePopupHandle();
201 }
202
203 static void MpVideoSoundpathCurrentState()
204 {
205         VideoLogInfo("");
206
207         bool bBluetoothConnect = 0;
208         char *szBluetoothName = NULL;
209         bIsConnectEarjack = FALSE;
210         bIsConnectBluetooth = FALSE;
211
212         /* Speaker */
213         strncpy(szSoundPathItems[MP_SOUND_PATH_SPEAKER], MP_VIDEO_SOUND_PATH_SPEAKER, STR_LEN_MAX - 1);
214
215         /*  Check and Add earjack sound path. */
216         if(MpUtilIsConnectEarjack())
217         {
218                 VideoLogInfo("Connect earjack.");
219                 bIsConnectEarjack = TRUE;
220                 strncpy(szSoundPathItems[MP_SOUND_PATH_EARJACK], MP_VIDEO_SOUND_PATH_EARJACK, STR_LEN_MAX - 1);
221         }
222         else
223         {
224                 VideoLogInfo("No connect earjack.");
225         }
226
227         /*  Add bluetooth stereo sound path. */
228         if(SOUND_MANAGER_ERROR_NONE != sound_manager_get_a2dp_status(&bBluetoothConnect, &szBluetoothName))
229         {
230                 VideoLogInfo("[ERR] Fail to check bluetooth stereo sound path.");
231         }
232         else
233         {
234                 if(bBluetoothConnect)
235                 {
236                         if(szBluetoothName)
237                         {
238                                 VideoLogInfo("Connect szBluetoothName : %s", szBluetoothName);
239                                 bIsConnectBluetooth = TRUE;
240                                 strncpy(szSoundPathItems[MP_SOUND_PATH_BLUETOOTH], szBluetoothName, STR_LEN_MAX - 1);
241                                 free(szBluetoothName);
242                                 szBluetoothName = NULL;
243                         }
244                 }
245                 else
246                 {
247                         if(szBluetoothName)
248                         {
249                                 free(szBluetoothName);
250                                 szBluetoothName = NULL;
251                         }
252                         VideoLogInfo("Unactivate Bluetooth stereo A2DP.");
253                 }
254         }
255
256 }
257
258 void MpVideoSoundpathSetRadioButton(void)
259 {
260         VideoLogInfo("");
261
262         sound_device_in_e in;
263         sound_device_out_e out;
264
265         sound_manager_get_active_device(&in, &out);
266
267         switch(out)
268         {
269         case SOUND_DEVICE_OUT_SPEAKER:
270                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
271                 break;
272
273         case SOUND_DEVICE_OUT_WIRED_ACCESSORY:
274                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_EARJACK);
275                 break;
276
277         case SOUND_DEVICE_OUT_BT_A2DP:
278                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_BLUETOOTH);
279                 break;
280
281         default:
282                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
283                 break;
284         }
285
286 /*
287         bool bBluetoothConnect = 0;
288         char *szBluetoothName = NULL;
289         sound_route_policy_e nPolicy = 0;
290
291         if(sound_manager_get_route_policy(&nPolicy) != SOUND_MANAGER_ERROR_NONE)
292         {
293                 VideoLogInfo("[ERR] Fail to get sound path route policy.");
294                 return;
295         }
296
297         if(nPolicy == SOUND_ROUTE_DEFAULT)
298         {
299                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_DEFAULT");
300
301                 if(SOUND_MANAGER_ERROR_NONE != sound_manager_get_a2dp_status(&bBluetoothConnect, &szBluetoothName))
302                 {
303                         VideoLogInfo("[ERR] Fail to check bluetooth stereo sound path.");
304                 }
305
306                 if(bBluetoothConnect)
307                 {
308                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_BLUETOOTH);
309                 }
310                 else if(MpUtilIsConnectEarjack())
311                 {
312                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_EARJACK);
313                 }
314                 else
315                 {
316                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
317                 }
318         }
319         else if(nPolicy == SOUND_ROUTE_IGNORE_A2DP)
320         {
321                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP");
322
323                 if(MpUtilIsConnectEarjack())
324                 {
325                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_EARJACK);
326                 }
327                 else
328                 {
329                         elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
330                 }
331         }
332         else
333         {
334                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_HANDSET_ONLY");
335
336                 elm_radio_value_set(pGroupRadio, MP_SOUND_PATH_SPEAKER);
337         }
338 */
339 }
340
341 /*
342  * External function
343  */
344 bool MpVideoSoundpathShow(void *pUserData)
345 {
346         if (pUserData == NULL) {
347                 VideoLogInfo("No have popup message or pAppData is null.");
348                 return FALSE;
349         }
350
351         VideoLogInfo("");
352
353         MpVideoSoundpathIsConnectOtherSoundpath();
354
355         if (!bIsConnectBluetooth && !bIsConnectEarjack) {
356                 VideoLogInfo("Soundpath is only speaker.");
357                 return FALSE;
358         }
359
360         Evas_Object *pBox = NULL;
361         Evas_Object *pButton = NULL;
362         VideoAppData *pAppData = (VideoAppData *)pUserData;
363
364         MpVideoSoundpathDeletePopupHandle();
365
366         MpUtilSetAlphaOfMainWindow(FALSE);
367
368         /*  Popup handle */
369         //pVideoSoundPathpPopupWindow = (Evas_Object *)MpUtilCreateWindow("soundpath_popup_window");
370         //pVideoSoundPathPopUpHandle = elm_popup_add(pVideoSoundPathpPopupWindow);
371         pVideoSoundPathPopUpHandle = elm_popup_add(pAppData->pMainWindow);
372
373
374         szSoundPathItems = (char **)calloc(1, sizeof(char *) * MP_VIDEO_SOUND_PATH_NUM);
375         int idx = 0;
376         for(idx = 0; idx < MP_VIDEO_SOUND_PATH_NUM; idx++)
377         {
378                 szSoundPathItems[idx] = (char *)calloc(1, STR_LEN_MAX * sizeof(char));
379         }
380
381         MpVideoSoundpathCurrentState();
382
383         elm_object_style_set(pVideoSoundPathPopUpHandle, "menustyle");
384         elm_object_part_text_set(pVideoSoundPathPopUpHandle, "title,text", MP_VIDEO_SOUND_PATH);
385         evas_object_size_hint_weight_set(pVideoSoundPathPopUpHandle, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
386
387         pButton = elm_button_add(pVideoSoundPathPopUpHandle);
388         elm_object_style_set (pButton, "popup_button/default");
389         elm_object_text_set(pButton, dgettext(MP_SYS_STRING, MP_COM_BUTTON_CANCEL));
390         elm_object_part_content_set(pVideoSoundPathPopUpHandle, "button1", pButton);
391         evas_object_smart_callback_add(pButton, "clicked", MpVideoSoundpathPopupCb, (void*)pAppData);
392
393         /*  Set item of Genlist. */
394         Elm_Genlist_Item_Class *st_Itc = NULL;
395         st_Itc = elm_genlist_item_class_new();
396         st_Itc->item_style = "1text.1icon.3";
397         st_Itc->func.text_get = (void *)MpVideoSoundpathGetLabelOfGenlistItemCb;
398         st_Itc->func.content_get = (void *)MpVideoSoundpathGetIconOfGenlistItemCb;
399         st_Itc->func.state_get = NULL;
400         st_Itc->func.del = NULL;
401
402         /* Create genlist handle */
403         pGenList = elm_genlist_add(pVideoSoundPathPopUpHandle);
404         evas_object_size_hint_weight_set(pGenList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
405         evas_object_size_hint_align_set(pGenList, EVAS_HINT_FILL, EVAS_HINT_FILL);
406
407         //MpUtilRotateWindow(MpUtilGetRotationState(), pVideoSoundPathpPopupWindow);
408
409         pGroupRadio = elm_radio_add(pGenList);
410
411         if (bIsConnectBluetooth) {
412                 elm_genlist_item_append(pGenList, st_Itc, (void *)MP_SOUND_PATH_BLUETOOTH, NULL,
413                                         ELM_GENLIST_ITEM_NONE,
414                                         MpVideoSoundpathSelectSoundPathItemCb,
415                                         pAppData);
416         }
417
418         if (bIsConnectEarjack) {
419                 elm_genlist_item_append(pGenList, st_Itc, (void *)MP_SOUND_PATH_EARJACK, NULL,
420                                         ELM_GENLIST_ITEM_NONE,
421                                         MpVideoSoundpathSelectSoundPathItemCb,
422                                         pAppData);
423         }
424
425         elm_genlist_item_append(pGenList, st_Itc, (void *)MP_SOUND_PATH_SPEAKER, NULL,
426                                         ELM_GENLIST_ITEM_NONE,
427                                         MpVideoSoundpathSelectSoundPathItemCb,
428                                         pAppData);
429
430         elm_genlist_item_class_free(st_Itc);
431
432         MpVideoSoundpathSetRadioButton();
433
434         pBox = elm_box_add(pVideoSoundPathPopUpHandle);
435         if(bIsConnectBluetooth && bIsConnectEarjack)
436         {
437                 evas_object_size_hint_min_set(pBox, 614 * elm_config_scale_get(), 338 * elm_config_scale_get());
438         }
439         else if(bIsConnectBluetooth || bIsConnectEarjack)
440         {
441                 evas_object_size_hint_min_set(pBox, 614 * elm_config_scale_get(), 225 * elm_config_scale_get());
442         }
443         else
444         {
445                 evas_object_size_hint_min_set(pBox, 614 * elm_config_scale_get(), 451 * elm_config_scale_get());
446         }
447
448         elm_box_pack_end(pBox, pGenList);
449         elm_object_content_set(pVideoSoundPathPopUpHandle, pBox);
450
451         evas_object_show(pGenList);
452         evas_object_show(pVideoSoundPathPopUpHandle);
453
454         return TRUE;
455 }
456
457 void MpVideoSoundpathHide(void)
458 {
459         VideoLogInfo("");
460
461         MpVideoSoundpathDeletePopupHandle();
462 }
463
464 void MpVideoSoundpathRotatePopupWindow(int nRotateMode)
465 {
466         /*
467         if (!pVideoSoundPathpPopupWindow) {
468                 VideoLogInfo("No exist genlist popup window handle.");
469                 return;
470         }
471
472         VideoLogInfo("");
473
474         MpUtilRotateWindow(nRotateMode, pVideoSoundPathpPopupWindow);
475         */
476
477         return;
478 }
479
480 bool MpVideoSoundpathIsConnectOtherSoundpath(void)
481 {
482         VideoLogInfo("");
483
484         bool bBluetoothConnect = 0;
485         char *szBluetoothName = NULL;
486
487         bIsConnectEarjack = FALSE;
488         bIsConnectBluetooth = FALSE;
489
490         if(MpUtilIsConnectEarjack())
491         {
492                 VideoLogInfo("Connect earjack.");
493                 bIsConnectEarjack= TRUE;
494                 return TRUE;
495         }
496         sound_manager_get_a2dp_status(&bBluetoothConnect, &szBluetoothName);
497
498         if(bBluetoothConnect)
499         {
500                 if(szBluetoothName) {
501                         free(szBluetoothName);
502                         szBluetoothName = NULL;
503                 }
504
505                 VideoLogInfo("Connect bluetooth.");
506                 bIsConnectBluetooth = TRUE;
507                 return TRUE;
508         }
509
510         if(szBluetoothName) {
511                 free(szBluetoothName);
512                 szBluetoothName = NULL;
513         }
514
515         VideoLogInfo("Only spaker.");
516         return FALSE;
517 }
518
519 int MpVideoSoundpathGetCurrConnectedSoundpath(void)
520 {
521         VideoLogInfo("");
522
523         sound_device_in_e in;
524         sound_device_out_e out;
525         MpSoundPathType nType = MP_SOUND_PATH_SPEAKER;
526
527         sound_manager_get_active_device(&in, &out);
528
529         switch(out)
530         {
531         case SOUND_DEVICE_OUT_SPEAKER:
532                 nType =  MP_SOUND_PATH_SPEAKER;
533                 break;
534
535         case SOUND_DEVICE_OUT_WIRED_ACCESSORY:
536                 nType =  MP_SOUND_PATH_EARJACK;
537                 break;
538
539         case SOUND_DEVICE_OUT_BT_A2DP:
540                 nType =  MP_SOUND_PATH_BLUETOOTH;
541                 break;
542
543         default:
544                 nType =  MP_SOUND_PATH_SPEAKER;
545                 break;
546         }
547
548         return nType;
549
550 /*
551         bool bBluetoothConnect = 0;
552         char *szBluetoothName = NULL;
553         sound_route_policy_e nPolicy = 0;
554
555         if(sound_manager_get_route_policy(&nPolicy) != SOUND_MANAGER_ERROR_NONE)
556         {
557                 VideoLogInfo("[ERR] Fail to get sound path route policy.");
558                 return MP_SOUND_PATH_SPEAKER;
559         }
560
561         if(nPolicy == SOUND_ROUTE_DEFAULT)
562         {
563                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_DEFAULT");
564
565                 if(SOUND_MANAGER_ERROR_NONE != sound_manager_get_a2dp_status(&bBluetoothConnect, &szBluetoothName))
566                 {
567                         VideoLogInfo("[ERR] Fail to check bluetooth stereo sound path.");
568                 }
569
570                 if(bBluetoothConnect)
571                 {
572                         return MP_SOUND_PATH_BLUETOOTH;
573                 }
574                 else if(MpUtilIsConnectEarjack())
575                 {
576                         return MP_SOUND_PATH_EARJACK;
577                 }
578                 else
579                 {
580                         return MP_SOUND_PATH_SPEAKER;
581                 }
582         }
583         else if(nPolicy == SOUND_ROUTE_IGNORE_A2DP)
584         {
585                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_IGNORE_A2DP");
586
587                 if(MpUtilIsConnectEarjack())
588                 {
589                         return MP_SOUND_PATH_EARJACK;
590                 }
591                 else
592                 {
593                         return MP_SOUND_PATH_SPEAKER;
594                 }
595         }
596         else
597         {
598                 VideoLogInfo("SYSTEM_AUDIO_ROUTE_POLICY_HANDSET_ONLY");
599
600                 return MP_SOUND_PATH_SPEAKER;
601         }
602
603         return MP_SOUND_PATH_SPEAKER;
604 */
605 }