Add keygrab privilege & fix tzsh region
[apps/native/volume-app.git] / src / bt.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 #include <bluetooth.h>
23 #include <bluetooth_internal.h>
24 #include <bluetooth_extension.h>
25
26 #include "main.h"
27 #include "_util_log.h"
28 #include "view.h"
29 #include "control.h"
30 #include "sound.h"
31 #include "timer.h"
32
33 static void _bt_display_bt_volume_view(sound_type_e sound_type, int sound, int vibration, bool bt_opened);
34 static void _bt_volume_changed_cb(int volume, void *user_data);
35 static void _bt_state_changed_cb(int result, bool opened, void *user_data);
36 static int _bt_register_changed_cb(void);
37 static int _bt_unregister_changed_cb(void);
38
39 int bt_get_bt_volume(void)
40 {
41         int ret = BT_ERROR_NONE;
42         int bt_vol = 0;
43
44         ret = bt_ag_get_speaker_gain(&bt_vol);
45         if (ret != BT_ERROR_NONE)
46         {
47                 _E("bt_ag_get_speaker_gain Failed");
48                 return -1;
49         }
50         _D("bt vol: %d", bt_vol);
51         return bt_vol;
52 }
53
54 void bt_init_sco(void)
55 {
56         _D("SCO volume initialize");
57         if (BT_ERROR_NONE != bt_initialize())
58                 _E("BT initialize failed");
59
60         if (_bt_register_changed_cb() != BT_ERROR_NONE)
61                 _E("volume bt register changed cb failed");
62 }
63
64 void bt_deinit_sco(void)
65 {
66         _D("SCO volume Deinitialize");
67         if (_bt_unregister_changed_cb() != BT_ERROR_NONE)
68                 _E("volume bt Unregister changed cb failed");
69
70         if (BT_ERROR_NONE != bt_deinitialize())
71                 _E("BT Deinitialize failed");
72 }
73
74 static void _bt_display_bt_volume_view(sound_type_e sound_type, int sound, int vibration, bool bt_opened)
75 {
76         if (VOLUME_ERROR_OK != volume_view_window_show(sound_type))
77                 _E("Failed to show volume window");
78
79         volume_view_volume_icon_set(sound_type, sound, vibration, bt_opened);
80 }
81
82 static void _bt_volume_changed_cb(int volume, void *user_data)
83 {
84         bool bt_opened = false;
85         int error = 0;
86         int status = 0;
87         int sound = 0;
88         int lock = 1;
89         int vibration = 0;
90         sound_type_e sound_type = 0;
91         _D("BT VOLUME is changed");
92
93         status = volume_control_check_status(&lock, &sound_type);
94         _D("status: %d, lock: %d, sound type : %d", status, lock, sound_type);
95
96         sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
97         _D("sound status : %d", sound);
98
99         error = bt_ag_is_sco_opened(&bt_opened);
100         if (error != BT_ERROR_NONE)
101                 _E("bt_ag_is_sco_opened return [%d]", error);
102         _D("BT state %d", bt_opened);
103
104         if (bt_opened == true && sound_type == SOUND_TYPE_CALL)
105         {
106                 _bt_display_bt_volume_view(sound_type, sound, vibration, bt_opened);
107                 _D("BT SCO volume level is : %d", volume);
108                 if (VOLUME_ERROR_OK != volume_view_slider_value_set(volume))
109                         _E("Failed to set slider value");
110
111                 volume_timer_add(3.0, TYPE_TIMER_BT);
112         }
113 }
114
115 static void _bt_state_changed_cb(int result, bool opened, void *user_data)
116 {
117         _D("SCO opened [%s]", opened ? "YES" : "NO");
118 }
119
120 static int _bt_register_changed_cb(void)
121 {
122         int ret = 0;
123
124         ret = bt_audio_initialize();
125         if (ret != BT_ERROR_NONE)
126         {
127                 _E("bt audio initialize failed");
128                 return -1;
129         }
130
131         ret = bt_ag_set_speaker_gain_changed_cb(_bt_volume_changed_cb, NULL);
132         if (ret != BT_ERROR_NONE)
133         {
134                 _E("register bt volume changed callback failed");
135                 return -1;
136         }
137
138         ret = bt_ag_set_sco_state_changed_cb(_bt_state_changed_cb, NULL);
139         if (ret != BT_ERROR_NONE)
140                 _E("register bt state changed callback failed");
141
142         return 0;
143 }
144
145 static int _bt_unregister_changed_cb(void)
146 {
147         int ret = 0;
148
149         ret = bt_audio_deinitialize();
150         if (ret != BT_ERROR_NONE) {
151                 _E("bt audio initialize failed");
152                 return -1;
153         }
154
155         ret = bt_ag_unset_speaker_gain_changed_cb();
156         if (ret != BT_ERROR_NONE) {
157                 _E("register bt volume changed callback failed");
158                 return -1;
159         }
160
161         ret = bt_ag_unset_sco_state_changed_cb();
162         if (ret != BT_ERROR_NONE)
163                 _E("register bt state changed callback failed");
164
165         return 0;
166 }
167