Add focus auto reacquirement option setting api
[platform/core/multimedia/libmm-sound.git] / mm_sound_focus.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sangchul Lee <sc11.lee@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <vconf.h>
26
27 #include <mm_debug.h>
28
29 #include "include/mm_sound.h"
30 #include "include/mm_sound_client.h"
31 #include "include/mm_sound_focus.h"
32 #include "focus_server/include/mm_sound_mgr_focus.h"
33
34 #define RETURN_ERROR_IF_FOCUS_CB_THREAD(x_thread) \
35 { \
36         int ret = MM_ERROR_NONE; \
37         bool result = false; \
38         ret = mm_sound_client_is_focus_cb_thread(x_thread, &result); \
39         if (ret) \
40                 return ret; \
41         else if (result) { \
42                 debug_error("it might be called in the thread of focus callback, it is not allowed\n"); \
43                 return MM_ERROR_SOUND_INVALID_OPERATION; \
44         } \
45 } \
46
47 EXPORT_API
48 int mm_sound_focus_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void *user_data)
49 {
50         int ret = MM_ERROR_NONE;
51         debug_fenter();
52
53         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
54
55         if (!callback)
56                 return MM_ERROR_INVALID_ARGUMENT;
57
58         ret = mm_sound_client_set_session_interrupt_callback (callback, user_data);
59
60         debug_fleave();
61
62         return ret;
63 }
64
65 EXPORT_API
66 int mm_sound_focus_unset_session_interrupt_callback(void)
67 {
68         int ret = MM_ERROR_NONE;
69         debug_fenter();
70
71         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
72
73         ret = mm_sound_client_unset_session_interrupt_callback ();
74         if (ret) {
75                 debug_error("Failed to mm_sound_client_unset_session_interrupt_callback(), ret[0x%x]\n", ret);
76         }
77
78         debug_fleave();
79
80         return ret;
81 }
82
83 EXPORT_API
84 int mm_sound_focus_get_id(int *id)
85 {
86         int ret = MM_ERROR_NONE;
87
88         debug_fenter();
89
90         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
91
92         ret = mm_sound_client_get_unique_id(id);
93         if (ret) {
94                 debug_error("Failed to mm_sound_client_get_unique_id(), ret[0x%x]\n", ret);
95         }
96
97         debug_fleave();
98
99         return ret;
100 }
101
102 EXPORT_API
103 int mm_sound_focus_is_cb_thread(bool *result)
104 {
105         int ret = MM_ERROR_NONE;
106
107         debug_fenter();
108
109         ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), result);
110         if (!ret) {
111                 if (*result)
112                         debug_error("it might be called in the thread of focus callback, it is not allowed\n");
113         }
114
115         debug_fleave();
116
117         return ret;
118 }
119
120 EXPORT_API
121 int mm_sound_register_focus(int id, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
122 {
123         int ret = MM_ERROR_NONE;
124
125         debug_fenter();
126
127         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
128
129         if (id < 0 || callback == NULL) {
130                 debug_error("argument is not valid\n");
131                 return MM_ERROR_INVALID_ARGUMENT;
132         }
133
134         ret = mm_sound_client_register_focus(id, getpid(), stream_type, callback, false, user_data);
135         if (ret) {
136                 debug_error("Could not register focus, ret[0x%x]\n", ret);
137         }
138
139         debug_fleave();
140
141         return ret;
142 }
143
144 EXPORT_API
145 int mm_sound_register_focus_for_session(int id, int pid, const char *stream_type, mm_sound_focus_changed_cb callback, void *user_data)
146 {
147         int ret = MM_ERROR_NONE;
148
149         debug_fenter();
150
151         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
152
153         if (id < 0 || callback == NULL) {
154                 debug_error("argument is not valid\n");
155                 return MM_ERROR_INVALID_ARGUMENT;
156         }
157
158         ret = mm_sound_client_register_focus(id, pid, stream_type, callback, true, user_data);
159         if (ret) {
160                 debug_error("Could not register focus for session, ret[0x%x]\n", ret);
161         }
162
163         debug_fleave();
164
165         return ret;
166 }
167
168 EXPORT_API
169 int mm_sound_unregister_focus(int id)
170 {
171         int ret = MM_ERROR_NONE;
172
173         debug_fenter();
174
175         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
176
177         if (id < 0) {
178                 debug_error("argument is not valid\n");
179                 return MM_ERROR_INVALID_ARGUMENT;
180         }
181
182         ret = mm_sound_client_unregister_focus(id);
183         if (ret) {
184                 debug_error("Could not unregister focus, ret = %x\n", ret);
185         }
186
187         debug_fleave();
188
189         return ret;
190 }
191
192 EXPORT_API
193 int mm_sound_set_focus_reacquisition(int id, bool reacquisition)
194 {
195         int ret = MM_ERROR_NONE;
196
197         debug_fenter();
198
199         if (id < 0) {
200                 debug_error("argument is not valid\n");
201                 return MM_ERROR_INVALID_ARGUMENT;
202         }
203
204         ret = mm_sound_client_set_focus_reacquisition(id, reacquisition);
205
206         if (ret) {
207                 debug_error("Could not set focus reacquisition, ret[0x%x]\n", ret);
208         }
209
210         debug_fleave();
211
212         return ret;
213 }
214
215 EXPORT_API
216 int mm_sound_get_focus_reacquisition(int id, bool *reacquisition)
217 {
218         int ret = MM_ERROR_NONE;
219
220         debug_fenter();
221
222         if (id < 0 || !reacquisition) {
223                 debug_error("argument is not valid\n");
224                 return MM_ERROR_INVALID_ARGUMENT;
225         }
226
227         ret = mm_sound_client_get_focus_reacquisition(id, reacquisition);
228
229         if (ret) {
230                 debug_error("Could not get focus reacquisition, ret[0x%x]\n", ret);
231         }
232
233         debug_fleave();
234
235         return ret;
236 }
237
238 EXPORT_API
239 int mm_sound_acquire_focus(int id, mm_sound_focus_type_e focus_type, const char *additional_info)
240 {
241         int ret = MM_ERROR_NONE;
242
243         debug_fenter();
244
245         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
246
247         if (id < 0) {
248                 debug_error("argument is not valid\n");
249                 return MM_ERROR_INVALID_ARGUMENT;
250         }
251         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
252                 debug_error("argument is not valid\n");
253                 return MM_ERROR_INVALID_ARGUMENT;
254         }
255
256         ret = mm_sound_client_acquire_focus(id, focus_type, additional_info);
257         if (ret) {
258                 debug_error("Could not acquire focus, ret[0x%x]\n", ret);
259         }
260
261         debug_fleave();
262
263         return ret;
264 }
265
266 EXPORT_API
267 int mm_sound_release_focus(int id, mm_sound_focus_type_e focus_type, const char *additional_info)
268 {
269         int ret = MM_ERROR_NONE;
270
271         debug_fenter();
272
273         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
274
275         if (id < 0) {
276                 debug_error("argument is not valid\n");
277                 return MM_ERROR_INVALID_ARGUMENT;
278         }
279         if (focus_type < FOCUS_FOR_PLAYBACK || focus_type > FOCUS_FOR_BOTH) {
280                 debug_error("argument is not valid\n");
281                 return MM_ERROR_INVALID_ARGUMENT;
282         }
283
284         ret = mm_sound_client_release_focus(id, focus_type, additional_info);
285         if (ret) {
286                 debug_error("Could not release focus, ret[0x%x]\n", ret);
287         }
288
289         debug_fleave();
290
291         return ret;
292 }
293
294 EXPORT_API
295 int mm_sound_set_focus_watch_callback(mm_sound_focus_type_e focus_type, mm_sound_focus_changed_watch_cb callback, void *user_data, int *id)
296 {
297         int ret = MM_ERROR_NONE;
298
299         debug_fenter();
300
301         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
302
303         if (callback == NULL || id == NULL) {
304                 debug_error("argument is not valid\n");
305                 return MM_ERROR_INVALID_ARGUMENT;
306         }
307         ret = mm_sound_client_set_focus_watch_callback(getpid(), focus_type, callback, false, user_data, id);
308         if (ret) {
309                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
310         }
311
312         debug_fleave();
313
314         return ret;
315 }
316
317 EXPORT_API
318 int mm_sound_set_focus_watch_callback_for_session(int pid, mm_sound_focus_type_e focus_type, mm_sound_focus_changed_watch_cb callback, void *user_data, int *id)
319 {
320         int ret = MM_ERROR_NONE;
321
322         debug_fenter();
323
324         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
325
326         if (callback == NULL || id == NULL) {
327                 debug_error("argument is not valid\n");
328                 return MM_ERROR_INVALID_ARGUMENT;
329         }
330         ret = mm_sound_client_set_focus_watch_callback(pid, focus_type, callback, true, user_data, id);
331         if (ret) {
332                 debug_error("Could not set focus watch callback, ret[0x%x]\n", ret);
333         }
334
335         debug_fleave();
336
337         return ret;
338 }
339
340 EXPORT_API
341 int mm_sound_unset_focus_watch_callback(int id)
342 {
343         int ret = MM_ERROR_NONE;
344
345         debug_fenter();
346
347         RETURN_ERROR_IF_FOCUS_CB_THREAD(g_thread_self());
348
349         ret = mm_sound_client_unset_focus_watch_callback(id);
350         if (ret) {
351                 debug_error("Could not unset focus watch callback, id(%d), ret = %x\n", id, ret);
352         }
353
354         debug_fleave();
355
356         return ret;
357 }