disable signal handler for VD dotnet application, cleanup will be done by external...
[platform/core/multimedia/libmm-sound.git] / mm_sound_client.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <poll.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <signal.h>
31
32 #include <pthread.h>
33 #include <semaphore.h>
34
35 #include <mm_error.h>
36 #include <mm_debug.h>
37
38 #include "include/mm_sound.h"
39 #include "include/mm_sound_client.h"
40 #include "include/mm_sound_proxy.h"
41 #include "include/mm_sound_common.h"
42 #include "include/mm_sound_device.h"
43 #include "include/mm_sound_stream.h"
44
45 #include <mm_session.h>
46 #include <mm_session_private.h>
47
48 #include <glib.h>
49 #if defined(__GSOURCE_CALLBACK__)
50 #include <sys/poll.h>
51 #endif
52
53 #define CLIENT_HANDLE_MAX 256
54
55 #define FOCUS_HANDLE_MAX 512
56 #define FOCUS_HANDLE_INIT_VAL -1
57 #define CONFIG_ENABLE_RETCB
58
59 #define VOLUME_TYPE_LEN 64
60
61 struct sigaction system_int_old_action;
62 struct sigaction system_abrt_old_action;
63 struct sigaction system_segv_old_action;
64 struct sigaction system_term_old_action;
65 struct sigaction system_sys_old_action;
66 struct sigaction system_xcpu_old_action;
67
68 struct callback_data {
69         void *user_cb;
70         void *user_data;
71         void *extra_data;
72         guint subs_id;
73 };
74
75 typedef struct _FocusSource {
76         GSource source;
77         GPollFD poll_fd;
78 } FocusSource;
79
80 #define GET_CB_DATA(_cb_data, _func, _userdata, _extradata) \
81         do { \
82                 _cb_data = (struct callback_data*) g_malloc0(sizeof(struct callback_data)); \
83                 _cb_data->user_cb = _func; \
84                 _cb_data->user_data = _userdata; \
85                 _cb_data->extra_data = _extradata; \
86         } while (0)
87
88 #ifdef USE_FOCUS
89 typedef struct {
90         int focus_tid;
91         int handle;
92         int focus_fd;
93         FocusSource *fsrc;
94         bool is_used;
95         bool auto_reacquire;
96         GMutex focus_lock;
97         GThread *focus_cb_thread;
98         GMainLoop *focus_loop;
99         mm_sound_focus_changed_cb focus_callback;
100         mm_sound_focus_changed_watch_cb watch_callback;
101         void* user_data;
102         bool unset_watch_callback_requested;
103
104         /* will be removed when the session concept is completely left out*/
105         bool is_for_session;
106         bool is_for_monitor;
107 } focus_sound_info_t;
108
109 typedef struct {
110         int pid;
111         int handle;
112         int type;
113         int state;
114         char stream_type[MAX_STREAM_TYPE_LEN];
115         char ext_info[MM_SOUND_NAME_NUM];
116         int option;
117 } focus_cb_data_lib;
118
119 typedef struct {
120         int watch_cb_id;
121         mm_sound_focus_session_interrupt_cb user_cb;
122         void* user_data;
123 } focus_session_interrupt_info_t;
124
125 typedef gboolean (*focus_callback_handler_t)(gpointer user_data);
126
127 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
128 focus_session_interrupt_info_t g_focus_session_interrupt_info = {-1, NULL, NULL};
129 static pthread_mutex_t g_index_mutex = PTHREAD_MUTEX_INITIALIZER;
130 static pthread_mutex_t g_event_mutex = PTHREAD_MUTEX_INITIALIZER;
131 guint g_focus_signal_handle;
132 guint g_idle_event_src;
133 #endif
134
135 gboolean g_need_emergent_exit = FALSE;
136
137 typedef struct {
138         /* handle to watch end of playing */
139         int watching_handle;
140         /* subscription id to unsubscribe when handle ended */
141         unsigned subs_id;
142 } play_sound_end_callback_data_t;
143
144 typedef struct _focus_idle_event {
145         focus_idle_event_type_e type;
146         int data;
147 } focus_idle_event_t;
148
149 void mm_sound_client_cleanup(void)
150 {
151         int ret = MM_ERROR_NONE;
152
153         if (g_need_emergent_exit) {
154                 ret = mm_sound_proxy_emergent_exit(getpid());
155                 if (ret == MM_ERROR_NONE)
156                         debug_msg("Success to emergnet_exit");
157                 else
158                         debug_error("Error occurred : 0x%x", ret);
159         }
160 }
161
162 void _system_signal_handler(int signo, siginfo_t *siginfo, void *context)
163 {
164         sigset_t old_mask, all_mask;
165
166         debug_warning("Got signal : signo(%d)", signo);
167
168         /* signal block */
169
170         sigfillset(&all_mask);
171         sigprocmask(SIG_BLOCK, &all_mask, &old_mask);
172
173         mm_sound_client_cleanup();
174
175         sigprocmask(SIG_SETMASK, &old_mask, NULL);
176         /* signal unblock */
177
178         switch (signo) {
179         case SIGINT:
180                 if (system_int_old_action.sa_sigaction)
181                         system_int_old_action.sa_sigaction(signo, siginfo, context);
182                 else
183                         sigaction(signo, &system_int_old_action, NULL);
184                 break;
185         case SIGABRT:
186                 if (system_abrt_old_action.sa_sigaction)
187                         system_abrt_old_action.sa_sigaction(signo, siginfo, context);
188                 else
189                         sigaction(signo, &system_abrt_old_action, NULL);
190                 break;
191         case SIGSEGV:
192                 if (system_segv_old_action.sa_sigaction)
193                         system_segv_old_action.sa_sigaction(signo, siginfo, context);
194                 else
195                         sigaction(signo, &system_segv_old_action, NULL);
196                 break;
197         case SIGTERM:
198                 if (system_term_old_action.sa_sigaction)
199                         system_term_old_action.sa_sigaction(signo, siginfo, context);
200                 else
201                         sigaction(signo, &system_term_old_action, NULL);
202                 break;
203         case SIGSYS:
204                 if (system_sys_old_action.sa_sigaction)
205                         system_sys_old_action.sa_sigaction(signo, siginfo, context);
206                 else
207                         sigaction(signo, &system_sys_old_action, NULL);
208                 break;
209         case SIGXCPU:
210                 if (system_xcpu_old_action.sa_sigaction)
211                         system_xcpu_old_action.sa_sigaction(signo, siginfo, context);
212                 else
213                         sigaction(signo, &system_xcpu_old_action, NULL);
214                 break;
215         default:
216                 break;
217         }
218
219         debug_warning("signal handling end");
220 }
221
222 #ifdef TIZEN_TV
223 static bool _is_dotnet_app(void)
224 {
225         char *is_dotnet = NULL;
226
227         is_dotnet = getenv("DOTNET_APP");
228         if (is_dotnet && atoi(is_dotnet) == 1)
229                 return true;
230         else
231                 return false;
232 }
233 #endif
234
235 int mm_sound_client_initialize(void)
236 {
237         int ret = MM_ERROR_NONE;
238         struct sigaction system_action;
239
240         debug_fenter();
241
242         mm_sound_proxy_initialize();
243         g_idle_event_src = 0;
244
245 #ifdef TIZEN_TV
246         if (_is_dotnet_app()) {
247                 debug_warning("no signal handler for dotnet!!");
248                 return ret;
249         }
250 #endif
251         system_action.sa_sigaction = _system_signal_handler;
252         system_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
253
254         sigemptyset(&system_action.sa_mask);
255
256         sigaction(SIGINT, &system_action, &system_int_old_action);
257         sigaction(SIGABRT, &system_action, &system_abrt_old_action);
258         sigaction(SIGSEGV, &system_action, &system_segv_old_action);
259         sigaction(SIGTERM, &system_action, &system_term_old_action);
260         sigaction(SIGSYS, &system_action, &system_sys_old_action);
261         sigaction(SIGXCPU, &system_action, &system_xcpu_old_action);
262
263         debug_fleave();
264         return ret;
265 }
266
267 int mm_sound_client_finalize(void)
268 {
269         int ret = MM_ERROR_NONE;
270
271         debug_fenter();
272
273         mm_sound_client_cleanup();
274
275         ret = mm_sound_proxy_finalize();
276
277 #ifdef USE_FOCUS
278         if (g_idle_event_src > 0) {
279                 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
280                 g_source_remove(g_idle_event_src);
281                 MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
282         }
283 #endif
284
285 #ifdef TIZEN_TV
286         if (_is_dotnet_app()) {
287                 debug_warning("no signal handler for dotnet!!");
288                 return ret;
289         }
290 #endif
291
292         sigaction(SIGINT, &system_int_old_action, NULL);
293         sigaction(SIGABRT, &system_abrt_old_action, NULL);
294         sigaction(SIGSEGV, &system_segv_old_action, NULL);
295         sigaction(SIGTERM, &system_term_old_action, NULL);
296         sigaction(SIGSYS, &system_sys_old_action, NULL);
297         sigaction(SIGXCPU, &system_xcpu_old_action, NULL);
298
299         debug_fleave();
300         return ret;
301 }
302
303 void mm_sound_convert_volume_type_to_stream_type(int volume_type, char *stream_type)
304 {
305         switch (volume_type) {
306         case VOLUME_TYPE_SYSTEM:
307                 MMSOUND_STRNCPY(stream_type, "system", MAX_STREAM_TYPE_LEN);
308                 break;
309         case VOLUME_TYPE_NOTIFICATION:
310                 MMSOUND_STRNCPY(stream_type, "notification", MAX_STREAM_TYPE_LEN);
311                 break;
312         case VOLUME_TYPE_ALARM:
313                 MMSOUND_STRNCPY(stream_type, "alarm", MAX_STREAM_TYPE_LEN);
314                 break;
315         case VOLUME_TYPE_RINGTONE:
316                 MMSOUND_STRNCPY(stream_type, "ringtone-voip", MAX_STREAM_TYPE_LEN);
317                 break;
318         case VOLUME_TYPE_MEDIA:
319                 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
320                 break;
321         case VOLUME_TYPE_CALL:
322                 MMSOUND_STRNCPY(stream_type, "call-voice", MAX_STREAM_TYPE_LEN);
323                 break;
324         case VOLUME_TYPE_VOIP:
325                 MMSOUND_STRNCPY(stream_type, "voip", MAX_STREAM_TYPE_LEN);
326                 break;
327         case VOLUME_TYPE_VOICE:
328                 MMSOUND_STRNCPY(stream_type, "voice-information", MAX_STREAM_TYPE_LEN);
329                 break;
330         default:
331                 MMSOUND_STRNCPY(stream_type, "media", MAX_STREAM_TYPE_LEN);
332                 break;
333         }
334
335         debug_msg("volume type (%d) converted to stream type (%s)", volume_type, stream_type);
336
337 }
338
339 /*****************************************************************************************
340                             DBUS SUPPORTED FUNCTIONS
341 ******************************************************************************************/
342 #ifdef USE_FOCUS
343 void _mm_sound_client_focus_signal_callback(mm_sound_signal_name_t signal, int value, void *user_data)
344 {
345         int ret = MM_ERROR_NONE;
346
347         debug_fenter();
348         debug_msg("focus signal received, value = %d", value);
349
350         if (value == 1) {
351                 ret = mm_sound_proxy_clear_focus(getpid());
352                 if (ret)
353                         debug_error("clear focus failed ret = 0x%x", ret);
354                 mm_sound_unsubscribe_signal(g_focus_signal_handle);
355                 g_focus_signal_handle = 0;
356         }
357 }
358 #endif
359
360 int mm_sound_client_play_tone(int number, int volume_config, double volume,
361                                                         int time, int *handle, bool enable_session)
362 {
363         int ret = MM_ERROR_NONE;
364 //       int instance = -1; /* instance is unique to communicate with server : client message queue filter type */
365         int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
366         char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
367
368          debug_fenter();
369
370         /* read session information */
371         int session_type = MM_SESSION_TYPE_MEDIA;
372         int session_options = 0;
373         int is_focus_registered = 0;
374
375         ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
376         if (ret) {
377                 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
378                 return MM_ERROR_POLICY_INTERNAL;
379         }
380
381         if (is_focus_registered)
382                 enable_session = false;
383
384         if (enable_session) {
385                 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
386                         debug_warning("Read Session Information failed. use default \"media\" type");
387                         session_type = MM_SESSION_TYPE_MEDIA;
388
389                         if (MM_ERROR_NONE != mm_session_init(session_type)) {
390                                 debug_critical("MMSessionInit() failed");
391                                 return MM_ERROR_POLICY_INTERNAL;
392                         }
393                 }
394         }
395
396          // instance = getpid();
397          //debug_log("pid for client ::: [%d]", instance);
398
399          /* Send msg */
400          debug_msg("Input number : %d", number);
401          /* Send req memory */
402
403         mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
404         ret = mm_sound_proxy_play_tone(number, time, volume, volume_config,
405                                         session_type, session_options, getpid(), enable_session, handle, stream_type, -1);
406 #ifdef USE_FOCUS
407         if (enable_session && !g_focus_signal_handle) {
408                 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
409                                                                                 _mm_sound_client_focus_signal_callback, NULL);
410                 if (ret) {
411                         debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
412                         return MM_ERROR_POLICY_INTERNAL;
413                 }
414         }
415 #endif
416
417         debug_fleave();
418         return ret;
419 }
420
421 int mm_sound_client_play_tone_with_stream_info(int tone, char *stream_type, int stream_id,
422                                                                                         double volume, int duration, int *handle)
423 {
424         int ret = MM_ERROR_NONE;
425
426         debug_fenter();
427
428         ret = mm_sound_proxy_play_tone_with_stream_info(getpid(), tone, stream_type, stream_id, volume, duration, handle);
429
430         debug_fleave();
431         return ret;
432 }
433
434 static void _mm_sound_stop_callback_wrapper_func(int ended_handle, void *userdata)
435 {
436         struct callback_data *cb_data = (struct callback_data*) userdata;
437         play_sound_end_callback_data_t *end_cb_data;
438
439         debug_log("ended_handle : %d", ended_handle);
440
441         if (cb_data == NULL) {
442                 debug_warning("stop callback data null");
443                 return;
444         }
445
446         end_cb_data = (play_sound_end_callback_data_t*) cb_data->extra_data;
447
448         if (ended_handle == end_cb_data->watching_handle) {
449                 debug_log("Interested playing handle end : %d", ended_handle);
450                 ((mm_sound_stop_callback_func)(cb_data->user_cb))(cb_data->user_data, ended_handle);
451                 if (mm_sound_proxy_remove_play_sound_end_callback(end_cb_data->subs_id) != MM_ERROR_NONE)
452                         debug_error("mm_sound_client_dbus_remove_play_file_end_callback failed");
453         } else {
454                 debug_log("Not interested playing handle : %d", ended_handle);
455         }
456 }
457
458 static void play_end_callback_data_free_func(void *data)
459 {
460         struct callback_data *cb_data = (struct callback_data*) data;
461
462         if (cb_data) {
463                 g_free(cb_data->extra_data);
464                 g_free(cb_data);
465         }
466 }
467
468 int mm_sound_client_play_sound(MMSoundPlayParam *param, int tone, int *handle)
469 {
470         int ret = MM_ERROR_NONE;
471         int session_type = MM_SESSION_TYPE_MEDIA;
472         int session_options = 0;
473         int is_focus_registered = 0;
474 //      int instance = -1;      /* instance is unique to communicate with server : client message queue filter type */
475         int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
476         char stream_type[MAX_STREAM_TYPE_LEN] = {0, };
477         struct callback_data *cb_data = NULL;
478         play_sound_end_callback_data_t *end_cb_data;
479
480         debug_fenter();
481
482         /* read session information */
483
484         ret = mm_sound_get_signal_value(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &is_focus_registered);
485         if (ret) {
486                 debug_error("mm_sound_get_signal_value failed [0x%x]", ret);
487                 return MM_ERROR_POLICY_INTERNAL;
488         }
489
490         if (is_focus_registered)
491                 param->skip_session = true;
492
493         if (param->skip_session == false) {
494                 if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options)) {
495                         debug_warning("Read MMSession Type failed. use default \"media\" type");
496                         session_type = MM_SESSION_TYPE_MEDIA;
497
498                         if (MM_ERROR_NONE != mm_session_init(session_type)) {
499                                 debug_critical("MMSessionInit() failed");
500                                 return MM_ERROR_POLICY_INTERNAL;
501                         }
502                 }
503         }
504
505         /* Send msg */
506         if ((param->mem_ptr && param->mem_size)) {
507                 // Play memory, deprecated
508                 return MM_ERROR_INVALID_ARGUMENT;
509         }
510
511         mm_sound_convert_volume_type_to_stream_type(volume_type, stream_type);
512         ret = mm_sound_proxy_play_sound(param->filename, tone, param->loop, param->volume, param->volume_config,
513                                          session_type, session_options, getpid(), param->skip_session, handle, stream_type, -1);
514         if (ret != MM_ERROR_NONE) {
515                 debug_error("Play Sound Failed");
516                 goto failed;
517         }
518         if (param->callback) {
519                 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
520                 end_cb_data->watching_handle = *handle;
521                 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
522
523                 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
524                                                                                                                 play_end_callback_data_free_func, &end_cb_data->subs_id);
525                 if (ret != MM_ERROR_NONE) {
526                         debug_error("Add callback for play sound(%d) Failed", *handle);
527                 }
528         }
529 #ifdef USE_FOCUS
530         if (!param->skip_session && !g_focus_signal_handle) {
531                 ret = mm_sound_subscribe_signal(MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS, &g_focus_signal_handle,
532                                                                                 _mm_sound_client_focus_signal_callback, NULL);
533                 if (ret) {
534                         debug_error("mm_sound_subscribe_signal failed [0x%x]", ret);
535                         return MM_ERROR_POLICY_INTERNAL;
536                 }
537         }
538 #endif
539
540 failed:
541
542         debug_fleave();
543         return ret;
544 }
545
546 int mm_sound_client_play_sound_with_stream_info(MMSoundPlayParam *param, int *handle, char* stream_type, int stream_id)
547 {
548         int ret = MM_ERROR_NONE;
549         struct callback_data *cb_data = NULL;
550         play_sound_end_callback_data_t *end_cb_data;
551
552         ret = mm_sound_proxy_play_sound_with_stream_info(param->filename, param->loop, param->volume,
553                                                                                                         getpid(), handle, stream_type, stream_id);
554         if (ret != MM_ERROR_NONE) {
555                 debug_error("Play Sound Failed");
556                 goto failed;
557         }
558         if (param->callback) {
559                 end_cb_data = (play_sound_end_callback_data_t *) g_malloc0(sizeof(play_sound_end_callback_data_t));
560                 end_cb_data->watching_handle = *handle;
561                 GET_CB_DATA(cb_data, param->callback, param->data, end_cb_data);
562
563                 ret = mm_sound_proxy_add_play_sound_end_callback(_mm_sound_stop_callback_wrapper_func, cb_data,
564                                                                                                                 play_end_callback_data_free_func, &end_cb_data->subs_id);
565                 if (ret != MM_ERROR_NONE) {
566                         debug_error("Add callback for play sound(%d) Failed", *handle);
567                 }
568         }
569
570 failed:
571
572         debug_fleave();
573         return ret;
574
575 }
576
577 int mm_sound_client_stop_sound(int handle)
578 {
579         int ret = MM_ERROR_NONE;
580         debug_fenter();
581
582         if (handle < 0 || handle > CLIENT_HANDLE_MAX) {
583                 ret = MM_ERROR_INVALID_ARGUMENT;
584                 return ret;
585         }
586
587         ret = mm_sound_proxy_stop_sound(handle);
588
589         debug_fleave();
590         return ret;
591 }
592
593 static int _mm_sound_client_device_list_dump(GList *device_list)
594 {
595         int ret = MM_ERROR_NONE;
596         GList *list = NULL;
597         mm_sound_device_t *device_node = NULL;
598         int count = 0;
599         if (!device_list) {
600                 debug_error("Device list NULL, cannot dump list");
601                 return MM_ERROR_SOUND_INTERNAL;
602         }
603
604         debug_log("======================== device list : start ==========================");
605         for (list = device_list; list != NULL; list = list->next) {
606                 device_node = (mm_sound_device_t *)list->data;
607                 if (device_node) {
608                         debug_log(" list idx[%d]: type[%17s], id[%02d], io_direction[%d], state[%d], name[%s]",
609                                                 count++, device_node->type, device_node->id, device_node->io_direction,
610                                                 device_node->state, device_node->name);
611                 }
612         }
613         debug_log("======================== device list : end ============================");
614
615         return ret;
616 }
617
618 int mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t *device_list)
619 {
620         int ret = MM_ERROR_NONE;
621         debug_fenter();
622
623         if (!device_list) {
624                 debug_error("Device list NULL");
625                 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
626                 goto failed;
627         }
628
629         if ((ret = mm_sound_proxy_get_current_connected_device_list(device_flags, &device_list->list)) != MM_ERROR_NONE) {
630                 debug_error("failed to get current connected device list with dbus, ret[0x%x]", ret);
631                 goto failed;
632         }
633         if (!device_list->list) {
634                 debug_error("Got device list null");
635                 ret = MM_ERROR_SOUND_NO_DATA;
636                 goto failed;
637         }
638         _mm_sound_client_device_list_dump(device_list->list);
639
640 failed:
641         debug_fleave();
642         return ret;
643 }
644
645 int mm_sound_client_get_device_by_id(int device_id, mm_sound_device_t **device)
646 {
647         int ret = MM_ERROR_NONE;
648
649         debug_fenter();
650
651         if ((ret = mm_sound_proxy_get_device_by_id(device_id, device)) != MM_ERROR_NONE)
652                 debug_error("failed to get device by id");
653
654         debug_fleave();
655
656         return ret;
657 }
658
659 static bool device_is_match_direction(int direction, int mask)
660 {
661         if (mask == DEVICE_IO_DIRECTION_FLAGS || mask == 0)
662                 return true;
663
664         if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_IN_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_IN))
665                 return true;
666         if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_OUT_FLAG) && (direction & MM_SOUND_DEVICE_IO_DIRECTION_OUT))
667                 return true;
668         if ((mask & MM_SOUND_DEVICE_IO_DIRECTION_BOTH_FLAG) && (direction == MM_SOUND_DEVICE_IO_DIRECTION_BOTH))
669                 return true;
670
671         return false;
672 }
673
674 static bool device_is_match_state(int state, int mask)
675 {
676         if (mask == DEVICE_STATE_FLAGS || mask == 0)
677                 return true;
678
679         if ((mask & MM_SOUND_DEVICE_STATE_DEACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_DEACTIVATED))
680                 return true;
681         if ((mask & MM_SOUND_DEVICE_STATE_ACTIVATED_FLAG) && (state == MM_SOUND_DEVICE_STATE_ACTIVATED))
682                 return true;
683
684         return false;
685 }
686
687 static bool device_is_match_type(const char *type, int mask)
688 {
689         bool is_builtin;
690         const char *builtin_prefix = "builtin";
691
692         if (mask == DEVICE_TYPE_FLAGS || mask == 0)
693                 return true;
694
695         is_builtin = !strncmp(type, builtin_prefix, strlen(builtin_prefix));
696
697         if ((mask & MM_SOUND_DEVICE_TYPE_INTERNAL_FLAG) && (is_builtin))
698                 return true;
699         if ((mask & MM_SOUND_DEVICE_TYPE_EXTERNAL_FLAG) && (!is_builtin))
700                 return true;
701
702         return false;
703 }
704
705 static bool device_is_match_with_mask(const char *type, int direction, int state, int mask)
706 {
707         if (mask == DEVICE_ALL_FLAG)
708                 return true;
709
710         return (device_is_match_direction(direction, mask & DEVICE_IO_DIRECTION_FLAGS) &&
711                         device_is_match_state(state, mask & DEVICE_STATE_FLAGS) &&
712                         device_is_match_type(type, mask & DEVICE_TYPE_FLAGS));
713 }
714
715 static int _fill_sound_device(mm_sound_device_t *device_h, int device_id, const char *device_type,
716                 int direction, int state, const char *name, int vendor_id, int product_id,
717                 int *stream_id, int stream_num)
718 {
719         int i;
720
721         if (stream_num > 0 && stream_id == NULL) {
722                 debug_error("stream_num is %d, but stream_id is NULL", stream_num);
723                 return -1;
724         }
725
726         if (stream_num > MAX_STREAM_ON_DEVICE) {
727                 debug_error("too many streams on this device");
728                 return -1;
729         }
730
731         device_h->id = device_id;
732         device_h->io_direction = direction;
733         device_h->state = state;
734         MMSOUND_STRNCPY(device_h->name, name, MAX_DEVICE_NAME_NUM);
735         MMSOUND_STRNCPY(device_h->type, device_type, MAX_DEVICE_TYPE_STR_LEN);
736         device_h->vendor_id = vendor_id;
737         device_h->product_id = product_id;
738
739         if (stream_num > 0) {
740                 device_h->stream_num = stream_num;
741                 debug_log("%d streams on this device", stream_num);
742                 for (i = 0; i < stream_num; i++) {
743                         debug_log("  stream_id : %d", stream_id[i]);
744                         device_h->stream_id[i] = stream_id[i];
745                 }
746         } else {
747                 device_h->stream_num = 0;
748                 debug_log("There is no stream on this device");
749         }
750
751         return 0;
752 }
753
754 static void _mm_sound_device_connected_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
755                                                         int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
756                                                         gboolean is_connected, void *userdata)
757 {
758         mm_sound_device_t device_h;
759         struct callback_data *cb_data = (struct callback_data*) userdata;
760         int device_flags;
761
762         debug_log("[Device %s] id(%d) type(%s) direction(%d) state(%d) name(%s) vendor-id(%04x) product-id(%04x)",
763                           is_connected ? "Connected" : "Disconnected", device_id, device_type, io_direction,
764                           state, name, vendor_id, product_id, is_connected);
765
766         if (cb_data == NULL) {
767                 debug_warning("device connected changed callback data null");
768                 return;
769         }
770
771         device_flags = (int) cb_data->extra_data;
772         if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
773                 return;
774
775         if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
776                                 vendor_id, product_id, stream_id, stream_num) < 0) {
777                 debug_error("Failed to fill sound device");
778                 return;
779         }
780
781         ((mm_sound_device_connected_cb)(cb_data->user_cb))(&device_h, is_connected, cb_data->user_data);
782 }
783
784 int mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func,
785                                                                                                 void* userdata, unsigned int *subs_id)
786 {
787         int ret = MM_ERROR_NONE;
788         struct callback_data *cb_data = NULL;
789
790         debug_fenter();
791
792         GET_CB_DATA(cb_data, func, userdata, (void*) device_flags);
793
794         ret = mm_sound_proxy_add_device_connected_callback(device_flags,
795                                                                                                         _mm_sound_device_connected_callback_wrapper_func,
796                                                                                                         cb_data, g_free, subs_id);
797         if (ret == MM_ERROR_NONE)
798                 g_need_emergent_exit = TRUE;
799
800         debug_fleave();
801         return ret;
802 }
803
804 int mm_sound_client_remove_device_connected_callback(unsigned int subs_id)
805 {
806         int ret = MM_ERROR_NONE;
807         debug_fenter();
808
809         ret = mm_sound_proxy_remove_device_connected_callback(subs_id);
810
811         debug_fleave();
812         return ret;
813 }
814
815 static void _mm_sound_device_info_changed_callback_wrapper_func(int device_id, const char *device_type, int io_direction,
816                                                         int state, const char *name, int vendor_id, int product_id, int *stream_id, int stream_num,
817                                                         int changed_device_info_type, void *userdata)
818 {
819         mm_sound_device_t device_h;
820         struct callback_data *cb_data = (struct callback_data*) userdata;
821         int device_flags;
822
823         debug_log("[Device Info Changed] id(%d) type(%s) direction(%d) state(%d) name(%s) "
824                         "vendor-id(%04x) product-id(%04x) changed_info_type(%d)",
825                         device_id, device_type, io_direction, state, name, vendor_id, product_id, changed_device_info_type);
826
827         if (cb_data == NULL) {
828                 debug_warning("device info changed callback data null");
829                 return;
830         }
831
832         device_flags = (int) cb_data->extra_data;
833         if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
834                 return;
835
836         if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
837                                 vendor_id, product_id, stream_id, stream_num) < 0) {
838                 debug_error("Failed to fill sound device");
839                 return;
840         }
841
842         ((mm_sound_device_info_changed_cb)(cb_data->user_cb))(&device_h, changed_device_info_type, cb_data->user_data);
843 }
844
845 int mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func,
846                                                                                                         void *userdata, unsigned int *subs_id)
847 {
848         int ret = MM_ERROR_NONE;
849         struct callback_data *cb_data = (struct callback_data*) userdata;
850
851         debug_fenter();
852
853         GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
854
855         ret = mm_sound_proxy_add_device_info_changed_callback(device_flags,
856                                                                                                                 _mm_sound_device_info_changed_callback_wrapper_func,
857                                                                                                                 cb_data, g_free, subs_id);
858
859         debug_fleave();
860         return ret;
861 }
862
863 int mm_sound_client_remove_device_info_changed_callback(unsigned int subs_id)
864 {
865         int ret = MM_ERROR_NONE;
866         debug_fenter();
867
868         ret =  mm_sound_proxy_remove_device_info_changed_callback(subs_id);
869
870         debug_fleave();
871         return ret;
872
873 }
874
875 static void _mm_sound_device_state_changed_callback_wrapper_func(int device_id, const char *device_type,
876                                                                 int io_direction, int state, const char *name, int vendor_id, int product_id,
877                                                                 int *stream_id, int stream_num, void *userdata)
878 {
879         mm_sound_device_t device_h;
880         struct callback_data *cb_data = (struct callback_data*) userdata;
881         int device_flags;
882
883         debug_log("[Device State Changed] id(%d) type(%s) direction(%d) state(%d) name(%s)"
884                         "vendor-id(%04x), product-id(%04x)",
885                         device_id, device_type, io_direction, state, name, vendor_id, product_id);
886
887         if (cb_data == NULL) {
888                 debug_warning("device state changed callback data null");
889                 return;
890         }
891
892         device_flags = (int) cb_data->extra_data;
893
894         if (!device_is_match_with_mask(device_type, io_direction, state, device_flags))
895                 return;
896
897         if (_fill_sound_device(&device_h, device_id, device_type, io_direction, state, name,
898                                 vendor_id, product_id, stream_id, stream_num) < 0) {
899                 debug_error("Failed to fill sound device");
900                 return;
901         }
902
903         ((mm_sound_device_state_changed_cb)(cb_data->user_cb))(&device_h, state, cb_data->user_data);
904 }
905
906 int mm_sound_client_add_device_state_changed_callback(int device_flags, mm_sound_device_state_changed_cb func,
907                                                                                                         void *userdata, unsigned int *id)
908 {
909         int ret = MM_ERROR_NONE;
910         struct callback_data *cb_data = (struct callback_data*) userdata;
911
912         debug_fenter();
913
914         GET_CB_DATA(cb_data, func, userdata, (void *) device_flags);
915
916         ret = mm_sound_proxy_add_device_state_changed_callback(device_flags,
917                                                                                                                 _mm_sound_device_state_changed_callback_wrapper_func,
918                                                                                                                 cb_data, g_free, id);
919
920         debug_fleave();
921         return ret;
922 }
923
924 int mm_sound_client_remove_device_state_changed_callback(unsigned int id)
925 {
926         int ret = MM_ERROR_NONE;
927         debug_fenter();
928
929         ret =  mm_sound_proxy_remove_device_state_changed_callback(id);
930
931         debug_fleave();
932         return ret;
933 }
934
935 int mm_sound_client_is_stream_on_device(int stream_id, int device_id, bool *is_on)
936 {
937         int ret = MM_ERROR_NONE;
938         debug_fenter();
939
940         if (!is_on) {
941                 debug_error("Invalid Parameter");
942                 ret = MM_ERROR_COMMON_INVALID_ARGUMENT;
943                 goto failed;
944         }
945
946         if ((ret = mm_sound_proxy_is_stream_on_device(stream_id, device_id, is_on)) != MM_ERROR_NONE) {
947                 debug_error("failed to query is stream on device, ret[0x%x]", ret);
948                 goto failed;
949         }
950
951 failed:
952         debug_fleave();
953         return ret;
954 }
955
956 int __convert_volume_type_to_str(int volume_type, char **volume_type_str)
957 {
958         int ret = MM_ERROR_NONE;
959
960         if (!volume_type_str) {
961                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
962         }
963
964         switch (volume_type) {
965         case VOLUME_TYPE_SYSTEM:
966                 *volume_type_str = "system";
967                 break;
968         case VOLUME_TYPE_NOTIFICATION:
969                 *volume_type_str = "notification";
970                 break;
971         case VOLUME_TYPE_ALARM:
972                 *volume_type_str = "alarm";
973                 break;
974         case VOLUME_TYPE_RINGTONE:
975                 *volume_type_str = "ringtone";
976                 break;
977         case VOLUME_TYPE_MEDIA:
978                 *volume_type_str = "media";
979                 break;
980         case VOLUME_TYPE_CALL:
981                 *volume_type_str = "call";
982                 break;
983         case VOLUME_TYPE_VOIP:
984                 *volume_type_str = "voip";
985                 break;
986         case VOLUME_TYPE_VOICE:
987                 *volume_type_str = "voice";
988                 break;
989         default:
990                 debug_error("unexpected volume type [%d]", volume_type);
991                 return MM_ERROR_SOUND_INTERNAL;
992         }
993         if (!strncmp(*volume_type_str, "", VOLUME_TYPE_LEN)) {
994                 debug_error("could not find the volume_type[%d] in this switch case statement", volume_type);
995                 ret = MM_ERROR_SOUND_INTERNAL;
996         } else {
997                 debug_log("volume_type[%s]", *volume_type_str);
998         }
999         return ret;
1000 }
1001
1002 static int __convert_volume_type_to_int(const char *volume_type_str, volume_type_t *volume_type)
1003 {
1004         int ret = MM_ERROR_NONE;
1005
1006         if (!volume_type || !volume_type_str) {
1007                 return MM_ERROR_COMMON_INVALID_ARGUMENT;
1008         }
1009
1010         if (!strncmp(volume_type_str, "system", VOLUME_TYPE_LEN)) {
1011                 *volume_type = VOLUME_TYPE_SYSTEM;
1012         } else if (!strncmp(volume_type_str, "notification", VOLUME_TYPE_LEN)) {
1013                 *volume_type = VOLUME_TYPE_NOTIFICATION;
1014         } else if (!strncmp(volume_type_str, "alarm", VOLUME_TYPE_LEN)) {
1015                 *volume_type = VOLUME_TYPE_ALARM;
1016         } else if (!strncmp(volume_type_str, "ringtone", VOLUME_TYPE_LEN)) {
1017                 *volume_type = VOLUME_TYPE_RINGTONE;
1018         } else if (!strncmp(volume_type_str, "media", VOLUME_TYPE_LEN)) {
1019                 *volume_type = VOLUME_TYPE_MEDIA;
1020         } else if (!strncmp(volume_type_str, "call", VOLUME_TYPE_LEN)) {
1021                 *volume_type = VOLUME_TYPE_CALL;
1022         } else if (!strncmp(volume_type_str, "voip", VOLUME_TYPE_LEN)) {
1023                 *volume_type = VOLUME_TYPE_VOIP;
1024         } else if (!strncmp(volume_type_str, "voice", VOLUME_TYPE_LEN)) {
1025                 *volume_type = VOLUME_TYPE_VOICE;
1026         } else {
1027                 debug_log("Invalid volume type : [%s]", volume_type_str);
1028                 ret = MM_ERROR_SOUND_INTERNAL;
1029         }
1030
1031         return ret;
1032 }
1033
1034 int mm_sound_client_set_volume_by_type(const int volume_type, const unsigned int volume_level)
1035 {
1036         int ret = MM_ERROR_NONE;
1037         char *type_str = NULL;
1038         debug_fenter();
1039
1040         if ((ret = __convert_volume_type_to_str(volume_type, &type_str)) != MM_ERROR_NONE) {
1041                 debug_error("volume type convert failed");
1042                 goto failed;
1043         }
1044
1045         ret = mm_sound_proxy_set_volume_by_type(type_str, volume_level);
1046
1047 failed:
1048         debug_fleave();
1049         return ret;
1050 }
1051
1052 static void _mm_sound_volume_changed_callback_wrapper_func(const char *direction, const char *volume_type_str,
1053                                                                                                                 int volume_level, void *userdata)
1054 {
1055         volume_type_t volume_type = 0;
1056         struct callback_data *cb_data = (struct callback_data *) userdata;
1057
1058         debug_log("direction : %s, volume_type : %s, volume_level : %d",
1059                         direction, volume_type_str, volume_level);
1060
1061         if (cb_data == NULL) {
1062                 debug_warning("volume changed callback data null");
1063                 return;
1064         }
1065
1066         if (__convert_volume_type_to_int(volume_type_str, &volume_type) != MM_ERROR_NONE) {
1067                 debug_error("volume type convert failed");
1068                 return;
1069         }
1070         debug_log("Call volume changed user cb, direction : %s, vol_type : %s(%d), level : %u",
1071                         direction, volume_type_str, volume_type, volume_level);
1072         ((mm_sound_volume_changed_cb)(cb_data->user_cb))(volume_type, volume_level, cb_data->user_data);
1073 }
1074
1075 int mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* userdata, unsigned int *subs_id)
1076 {
1077         int ret = MM_ERROR_NONE;
1078         struct callback_data *cb_data = NULL;
1079
1080         debug_fenter();
1081
1082         GET_CB_DATA(cb_data, func, userdata, NULL);
1083
1084         ret = mm_sound_proxy_add_volume_changed_callback(_mm_sound_volume_changed_callback_wrapper_func, cb_data, g_free, subs_id);
1085
1086         debug_fleave();
1087
1088         return ret;
1089 }
1090
1091 int mm_sound_client_remove_volume_changed_callback(unsigned int subs_id)
1092 {
1093         int ret = MM_ERROR_NONE;
1094         debug_fenter();
1095
1096         ret = mm_sound_proxy_remove_volume_changed_callback(subs_id);
1097
1098         debug_fleave();
1099         return ret;
1100 }
1101
1102 int mm_sound_client_set_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_parameters, const char *filter_group)
1103 {
1104         int ret = MM_ERROR_NONE;
1105         debug_fenter();
1106
1107         ret = mm_sound_proxy_set_filter_by_type(stream_type, filter_name, filter_parameters, filter_group);
1108
1109         debug_fleave();
1110         return ret;
1111 }
1112
1113 int mm_sound_client_unset_filter_by_type(const char *stream_type)
1114 {
1115         int ret = MM_ERROR_NONE;
1116         debug_fenter();
1117
1118         ret = mm_sound_proxy_unset_filter_by_type(stream_type);
1119
1120         debug_fleave();
1121         return ret;
1122 }
1123
1124 int mm_sound_client_control_filter_by_type(const char *stream_type, const char *filter_name, const char *filter_controls)
1125 {
1126         int ret = MM_ERROR_NONE;
1127         debug_fenter();
1128
1129         ret = mm_sound_proxy_control_filter_by_type(stream_type, filter_name, filter_controls);
1130
1131         debug_fleave();
1132         return ret;
1133 }
1134
1135 #ifdef USE_FOCUS
1136 static gboolean _interrupted_completed(gpointer *data)
1137 {
1138         if (!data) {
1139                 debug_error("data is null");
1140                 return false;
1141         }
1142         if (!g_focus_session_interrupt_info.user_cb) {
1143                 debug_error("user_cb is null");
1144                 free(data);
1145                 return false;
1146         }
1147
1148         debug_msg("invoke user_cb(%p)", g_focus_session_interrupt_info.user_cb);
1149         (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_RELEASED, (const char *)data, g_focus_session_interrupt_info.user_data);
1150         debug_msg("invoked");
1151
1152         free(data);
1153         return false;
1154 }
1155
1156 static void _session_interrupted_cb(int id, mm_sound_focus_type_e focus_type, mm_sound_focus_state_e state,
1157                                                                         const char *reason_for_change, const char *ext_info, void *user_data)
1158 {
1159         debug_msg("id(%d), focus_type(%d), state(%d), reason(%s)", id, focus_type, state, reason_for_change);
1160
1161         if (id != g_focus_session_interrupt_info.watch_cb_id) {
1162                 debug_error("id is not valid(param id:%d, g_focus_session_interrupt_watch_cb_id:%d)",
1163                                         id, g_focus_session_interrupt_info.watch_cb_id);
1164                 return;
1165         }
1166         if (!g_focus_session_interrupt_info.user_cb) {
1167                 debug_error("user callback is null");
1168                 return;
1169         }
1170
1171         debug_msg("  >>> invoking session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1172         if (state == FOCUS_IS_RELEASED)
1173                 (g_focus_session_interrupt_info.user_cb)(FOCUS_IS_ACQUIRED, reason_for_change, g_focus_session_interrupt_info.user_data);
1174         else {
1175                 debug_msg("INTERRUPTED COMPLETED case, append it to idle");
1176                 g_idle_add((GSourceFunc)_interrupted_completed, strdup(reason_for_change));
1177         }
1178         debug_msg("  <<< session interrupt callback finished");
1179 }
1180
1181 int mm_sound_client_set_session_interrupt_callback(mm_sound_focus_session_interrupt_cb callback, void* user_data)
1182 {
1183         int ret = MM_ERROR_NONE;
1184
1185         debug_fenter();
1186
1187         if (!callback)
1188                 return MM_ERROR_INVALID_ARGUMENT;
1189
1190         /* add internal focus watch callback */
1191         if (g_focus_session_interrupt_info.watch_cb_id == -1) {
1192                 if ((ret = mm_sound_client_set_focus_watch_callback(getpid(), FOCUS_FOR_BOTH, true, true, _session_interrupted_cb, NULL,
1193                                                                                                                         &g_focus_session_interrupt_info.watch_cb_id))) {
1194                         debug_error("failed to mm_sound_client_set_focus_watch_callback(), ret(0x%x)", ret);
1195                         return ret;
1196                 }
1197         }
1198         g_focus_session_interrupt_info.user_cb = callback;
1199         g_focus_session_interrupt_info.user_data = user_data;
1200
1201         debug_fleave();
1202         return ret;
1203 }
1204
1205 int mm_sound_client_unset_session_interrupt_callback(void)
1206 {
1207         int ret = MM_ERROR_NONE;
1208
1209         debug_fenter();
1210
1211         if (!g_focus_session_interrupt_info.user_cb || g_focus_session_interrupt_info.watch_cb_id == -1) {
1212                 debug_error("no callback to unset");
1213                 return MM_ERROR_SOUND_INTERNAL;
1214         }
1215
1216         /* remove internal focus watch callback */
1217         if ((ret = mm_sound_client_unset_focus_watch_callback(g_focus_session_interrupt_info.watch_cb_id))) {
1218                 debug_error("failed to mm_sound_client_unset_focus_watch_callback(), id(%d), ret(0x%x)",
1219                                         g_focus_session_interrupt_info.watch_cb_id, ret);
1220                 return ret;
1221         }
1222         g_focus_session_interrupt_info.watch_cb_id = -1;
1223         g_focus_session_interrupt_info.user_cb = NULL;
1224         g_focus_session_interrupt_info.user_data = NULL;
1225
1226         debug_fleave();
1227         return ret;
1228 }
1229
1230 static gpointer _focus_thread_func(gpointer data)
1231 {
1232         unsigned int thread_id = (unsigned int)pthread_self();
1233         GMainLoop *focus_loop = (GMainLoop*)data;
1234
1235         debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
1236         if (focus_loop)
1237                 g_main_loop_run(focus_loop);
1238         debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);
1239
1240         return NULL;
1241 }
1242
1243 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
1244 {
1245 #ifdef __DEBUG__
1246         debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout ? *timeout : -1);
1247 #endif
1248         return FALSE;
1249 }
1250
1251 static gboolean _focus_fd_check(GSource * source)
1252 {
1253         FocusSource* fsource = (FocusSource *)source;
1254
1255         if (!fsource) {
1256                 debug_error("GSource is null");
1257                 return FALSE;
1258         }
1259 #ifdef __DEBUG__
1260         debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
1261 #endif
1262         if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
1263                 return TRUE;
1264         else
1265                 return FALSE;
1266 }
1267
1268 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
1269 {
1270         debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
1271         return callback(user_data);
1272 }
1273
1274 static void _focus_fd_finalize(GSource *source)
1275 {
1276         debug_warning("### FINALIZE : %p", source);
1277 }
1278
1279 static int _focus_find_index_by_handle(int handle)
1280 {
1281         int i = 0;
1282         for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1283                 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
1284                         /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
1285                         return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1286                 }
1287         }
1288         return -1;
1289 }
1290
1291 static int _focus_watch_find_index_by_handle(int handle)
1292 {
1293         int i = 0;
1294         for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1295                 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
1296                         /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
1297                         return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
1298                 }
1299         }
1300         return -1;
1301 }
1302
1303 static gboolean _focus_callback_handler(gpointer user_data)
1304 {
1305         focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1306         GPollFD *poll_fd;
1307         int count;
1308         int tid = 0;
1309         focus_cb_data_lib cb_data;
1310
1311         debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1312
1313         if (!focus_handle) {
1314                 debug_error("focus_handle is null");
1315                 return G_SOURCE_CONTINUE;
1316         }
1317         poll_fd = &focus_handle->fsrc->poll_fd;
1318         debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1319
1320         memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1321
1322         if (poll_fd->revents & (POLLIN | POLLPRI)) {
1323                 int changed_state = -1;
1324
1325                 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1326                 if (count < 0) {
1327                         char str_error[256];
1328                         strerror_r(errno, str_error, sizeof(str_error));
1329                         debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1330                         return G_SOURCE_CONTINUE;
1331                 }
1332                 changed_state = cb_data.state;
1333
1334                 g_mutex_lock(&focus_handle->focus_lock);
1335
1336                 tid = focus_handle->focus_tid;
1337
1338                 if (changed_state != -1) {
1339                         debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1340                                         tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
1341                         if (focus_handle->focus_callback == NULL) {
1342                                         debug_error("focus callback is null..");
1343                                         g_mutex_unlock(&focus_handle->focus_lock);
1344                                         return G_SOURCE_CONTINUE;
1345                         }
1346                         debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
1347                         (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1348                                                                                 cb_data.option, cb_data.ext_info, focus_handle->user_data);
1349                         debug_msg("[CALLBACK END]");
1350                 }
1351 #ifdef CONFIG_ENABLE_RETCB
1352                 {
1353                         int rett = 0;
1354                         int tmpfd = -1;
1355                         unsigned int buf = 0;
1356                         char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_tid, cb_data.handle);
1357                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1358                         if (tmpfd < 0) {
1359                                 char str_error[256];
1360                                 strerror_r(errno, str_error, sizeof(str_error));
1361                                 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1362                                                         tid, tmpfd, filename2, errno, str_error);
1363                                 g_free(filename2);
1364                                 g_mutex_unlock(&focus_handle->focus_lock);
1365                                 return G_SOURCE_CONTINUE;
1366                         }
1367                         /* buf contains data as below,
1368                          * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
1369                         buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
1370                         rett = write(tmpfd, &buf, sizeof(buf));
1371                         close(tmpfd);
1372                         g_free(filename2);
1373                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1374                 }
1375 #endif
1376         }
1377
1378         g_mutex_unlock(&focus_handle->focus_lock);
1379
1380         debug_fleave();
1381
1382         return G_SOURCE_CONTINUE;
1383 }
1384
1385 static gboolean _focus_watch_callback_handler(gpointer user_data)
1386 {
1387         focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
1388         GPollFD *poll_fd;
1389         int count;
1390         int tid = 0;
1391         focus_cb_data_lib cb_data;
1392
1393         debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
1394
1395         if (!focus_handle) {
1396                 debug_error("focus_handle is null");
1397                 return G_SOURCE_CONTINUE;
1398         }
1399         poll_fd = &focus_handle->fsrc->poll_fd;
1400         debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
1401
1402         memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1403
1404         if (poll_fd->revents & (POLLIN | POLLPRI)) {
1405                 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
1406                 if (count < 0) {
1407                         char str_error[256];
1408                         strerror_r(errno, str_error, sizeof(str_error));
1409                         debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
1410                         return G_SOURCE_CONTINUE;
1411                 }
1412
1413                 if (!focus_handle->is_used) {
1414                         debug_warning("unsetting watch calllback has been already requested");
1415                         goto SKIP_CB_AND_RET;
1416                 }
1417
1418                 g_mutex_lock(&focus_handle->focus_lock);
1419
1420                 tid = focus_handle->focus_tid;
1421
1422                 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
1423                                 tid, cb_data.handle,  cb_data.type, cb_data.state, cb_data.stream_type);
1424
1425                 if (focus_handle->watch_callback == NULL) {
1426                         debug_warning("watch callback is null..");
1427                         goto SKIP_CB_AND_RET;
1428                 }
1429                 if (focus_handle->unset_watch_callback_requested == true) {
1430                         debug_warning("unset_watch_callback_requested..");
1431                         goto SKIP_CB_AND_RET;
1432                 }
1433
1434                 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
1435                 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
1436                                                                         cb_data.ext_info, focus_handle->user_data);
1437                 debug_msg("[CALLBACK END]");
1438
1439 SKIP_CB_AND_RET:
1440 #ifdef CONFIG_ENABLE_RETCB
1441                 {
1442                         int rett = 0;
1443                         int tmpfd = -1;
1444                         int buf = -1;
1445                         char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_tid, cb_data.handle);
1446                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1447                         if (tmpfd < 0) {
1448                                 char str_error[256];
1449                                 strerror_r(errno, str_error, sizeof(str_error));
1450                                 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
1451                                                         tid, tmpfd, filename2, errno, str_error);
1452                                 g_free(filename2);
1453                                 g_mutex_unlock(&focus_handle->focus_lock);
1454                                 return G_SOURCE_CONTINUE;
1455                         }
1456                         buf = cb_data.handle;
1457                         rett = write(tmpfd, &buf, sizeof(buf));
1458                         close(tmpfd);
1459                         g_free(filename2);
1460                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
1461                 }
1462 #endif
1463         }
1464
1465         if (focus_handle->is_used) {
1466                 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
1467                 g_mutex_unlock(&focus_handle->focus_lock);
1468         }
1469
1470         debug_fleave();
1471
1472         return G_SOURCE_CONTINUE;
1473 }
1474
1475 static void _focus_open_callback(int index, bool is_for_watching)
1476 {
1477         mode_t pre_mask;
1478         char *filename;
1479
1480         debug_fenter();
1481
1482         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1483                 debug_error("Invalid focus handle index [%d]", index);
1484                 return;
1485         }
1486
1487         if (is_for_watching) {
1488                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1489                                                                 g_focus_sound_handle[index].focus_tid,
1490                                                                 g_focus_sound_handle[index].handle);
1491         } else {
1492                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1493                                                                 g_focus_sound_handle[index].focus_tid,
1494                                                                 g_focus_sound_handle[index].handle);
1495         }
1496         pre_mask = umask(0);
1497         if (mknod(filename, S_IFIFO|0666, 0)) {
1498                 debug_error("mknod() failure, errno(%d)", errno);
1499         }
1500         umask(pre_mask);
1501         g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1502         if (g_focus_sound_handle[index].focus_fd == -1) {
1503                 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1504         } else {
1505                 debug_log("Open success : index(%d), filename(%s), fd(%d)",
1506                                 index, filename, g_focus_sound_handle[index].focus_fd);
1507         }
1508         g_free(filename);
1509         filename = NULL;
1510
1511 #ifdef CONFIG_ENABLE_RETCB
1512         char *filename2;
1513
1514         if (is_for_watching) {
1515                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1516                                                                         g_focus_sound_handle[index].focus_tid,
1517                                                                         g_focus_sound_handle[index].handle);
1518         } else {
1519                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1520                                                                         g_focus_sound_handle[index].focus_tid,
1521                                                                         g_focus_sound_handle[index].handle);
1522         }
1523         pre_mask = umask(0);
1524         if (mknod(filename2, S_IFIFO | 0666, 0)) {
1525                 debug_error("mknod() failure, errno(%d)", errno);
1526         }
1527         umask(pre_mask);
1528         g_free(filename2);
1529         filename2 = NULL;
1530 #endif
1531         debug_fleave();
1532
1533 }
1534
1535 void _focus_close_callback(int index, bool is_for_watching)
1536 {
1537         char *filename = NULL;
1538
1539         debug_fenter();
1540
1541         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1542                 debug_error("Invalid focus handle index [%d]", index);
1543                 return;
1544         }
1545
1546         if (g_focus_sound_handle[index].focus_fd < 0) {
1547                 debug_error("Close fail : index(%d)", index);
1548         } else {
1549                 close(g_focus_sound_handle[index].focus_fd);
1550                 debug_log("Close Success : index(%d)", index);
1551         }
1552
1553         if (is_for_watching) {
1554                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
1555                                                                 g_focus_sound_handle[index].focus_tid,
1556                                                                 g_focus_sound_handle[index].handle);
1557         } else {
1558                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
1559                                                                 g_focus_sound_handle[index].focus_tid,
1560                                                                 g_focus_sound_handle[index].handle);
1561         }
1562         if (remove(filename)) {
1563                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1564                                         filename, errno);
1565         }
1566         g_free(filename);
1567         filename = NULL;
1568
1569 #ifdef CONFIG_ENABLE_RETCB
1570         char *filename2;
1571
1572         if (is_for_watching) {
1573                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
1574                                                                         g_focus_sound_handle[index].focus_tid,
1575                                                                         g_focus_sound_handle[index].handle);
1576         } else {
1577                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr",
1578                                                                         g_focus_sound_handle[index].focus_tid,
1579                                                                         g_focus_sound_handle[index].handle);
1580         }
1581         if (remove(filename2)) {
1582                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
1583                                         filename2, errno);
1584         }
1585         g_free(filename2);
1586         filename2 = NULL;
1587
1588         debug_fleave();
1589 #endif
1590
1591 }
1592
1593 static GSourceFuncs event_funcs = {
1594         .prepare = _focus_fd_prepare,
1595         .check = _focus_fd_check,
1596         .dispatch = _focus_fd_dispatch,
1597         .finalize = _focus_fd_finalize,
1598 };
1599
1600 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
1601 {
1602         FocusSource *fsrc = NULL;
1603         GSource *src = NULL;
1604         guint fsrc_id = 0;
1605
1606         debug_fenter();
1607
1608         g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1609
1610         src = g_source_new(&event_funcs, sizeof(FocusSource));
1611         if (!src) {
1612                 debug_error("failed to g_source_new for focus source");
1613                 goto ERROR;
1614         }
1615
1616         fsrc = (FocusSource*) src;
1617
1618         fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
1619         fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
1620         g_source_add_poll(src, &fsrc->poll_fd);
1621
1622         g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
1623
1624         debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
1625                                 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
1626
1627         fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
1628         if (!fsrc_id) {
1629                 debug_error("failed to attach the source to context");
1630                 goto ERROR;
1631         }
1632         g_source_unref(src);
1633
1634         g_focus_sound_handle[index].fsrc = fsrc;
1635
1636         debug_fleave();
1637         return true;
1638
1639 ERROR:
1640         if (src)
1641                 g_source_unref(src);
1642
1643         return false;
1644 }
1645
1646 static bool _focus_remove_sound_callback(int index)
1647 {
1648         focus_sound_info_t *h = NULL;
1649
1650         debug_fenter();
1651
1652         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1653                 debug_error("Invalid focus handle index [%d]", index);
1654                 return false;
1655         }
1656
1657         h = &g_focus_sound_handle[index];
1658         if (h->fsrc) {
1659                 g_source_destroy((GSource *)h->fsrc);
1660                 h->fsrc = NULL;
1661         }
1662
1663         h->focus_callback = NULL;
1664         h->watch_callback = NULL;
1665
1666         g_mutex_clear(&h->focus_lock);
1667
1668         debug_fleave();
1669
1670         return true;
1671 }
1672
1673 static void _focus_add_callback(int index, bool is_for_watching)
1674 {
1675         debug_fenter();
1676
1677         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1678                 debug_error("Invalid focus handle index [%d]", index);
1679                 return;
1680         }
1681
1682         if (!is_for_watching) {
1683                 if (!_focus_add_sound_callback(index, _focus_callback_handler))
1684                         debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
1685         } else { // need to check if it's necessary
1686                 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
1687                         debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
1688         }
1689         debug_fleave();
1690 }
1691
1692 static void _focus_remove_callback(int index)
1693 {
1694         debug_fenter();
1695         if (!_focus_remove_sound_callback(index))
1696                 debug_error("failed to __focus_remove_sound_callback()");
1697         debug_fleave();
1698 }
1699
1700 static void _focus_init_callback(int index, bool is_for_watching)
1701 {
1702         debug_fenter();
1703         _focus_open_callback(index, is_for_watching);
1704         _focus_add_callback(index, is_for_watching);
1705         debug_fleave();
1706 }
1707
1708 static void _focus_deinit_callback(int index, bool is_for_watching)
1709 {
1710         debug_fenter();
1711         _focus_remove_callback(index);
1712         _focus_close_callback(index, is_for_watching);
1713         debug_fleave();
1714 }
1715
1716 #define INTERVAL_MS 20
1717 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
1718 {
1719         int reduced_time_ms = timeout_ms;
1720         if (!focus_loop || timeout_ms < 0) {
1721                 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
1722                 return MM_ERROR_INVALID_ARGUMENT;
1723         }
1724
1725         do {
1726                 if (g_main_loop_is_running(focus_loop))
1727                         return MM_ERROR_NONE;
1728
1729                 usleep(INTERVAL_MS * 1000);
1730                 if (reduced_time_ms < timeout_ms)
1731                         debug_warning("reduced_time_ms(%d)", reduced_time_ms);
1732         } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
1733
1734         debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
1735
1736         return MM_ERROR_SOUND_INTERNAL;
1737 }
1738
1739 #define LOOP_RUNNING_WAIT_TIME_MS 200
1740 static int _focus_init_context(int index)
1741 {
1742         int ret = MM_ERROR_NONE;
1743         GMainContext *focus_context;
1744
1745         debug_fenter();
1746
1747         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1748                 debug_error("index(%d) is not valid", index);
1749                 return MM_ERROR_INVALID_ARGUMENT;
1750         }
1751
1752         focus_context = g_main_context_new();
1753         g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
1754         g_main_context_unref(focus_context);
1755         if (g_focus_sound_handle[index].focus_loop == NULL) {
1756                 debug_error("could not create mainloop..");
1757                 goto ERROR;
1758         }
1759
1760         g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
1761                                                                         _focus_thread_func,
1762                                                                         g_focus_sound_handle[index].focus_loop);
1763         if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
1764                 debug_error("could not create thread..");
1765                 goto ERROR;
1766         }
1767
1768         debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
1769                                 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1770
1771         if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
1772                 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
1773                 goto ERROR;
1774         }
1775
1776         debug_fleave();
1777
1778         return MM_ERROR_NONE;
1779
1780 ERROR:
1781         if (g_focus_sound_handle[index].focus_loop) {
1782                 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1783                 g_focus_sound_handle[index].focus_loop = NULL;
1784         }
1785         return MM_ERROR_SOUND_INTERNAL;
1786 }
1787
1788 static void _focus_deinit_context(int index)
1789 {
1790         debug_fenter();
1791
1792         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
1793                 debug_error("index(%d) is not valid", index);
1794                 return;
1795         }
1796
1797         if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
1798                 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
1799                                 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
1800                 return;
1801         }
1802
1803         g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
1804         g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
1805         debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
1806                         g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
1807         g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
1808         g_focus_sound_handle[index].focus_loop = NULL;
1809         g_focus_sound_handle[index].focus_cb_thread = NULL;
1810
1811         debug_fleave();
1812 }
1813
1814 int mm_sound_client_get_unique_id(int *id)
1815 {
1816         int ret = MM_ERROR_NONE;
1817
1818         debug_fenter();
1819
1820         if (!id)
1821                 ret = MM_ERROR_INVALID_ARGUMENT;
1822         else
1823                 ret = mm_sound_proxy_get_unique_id(id);
1824
1825         debug_fleave();
1826
1827         return ret;
1828 }
1829
1830 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1831 {
1832         int ret = MM_ERROR_NONE;
1833         int i = 0;
1834
1835         if (!mine || !result)
1836                 ret = MM_ERROR_INVALID_ARGUMENT;
1837         else {
1838                 *result = false;
1839                 for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
1840                         if (!g_focus_sound_handle[i].is_used)
1841                                 continue;
1842                         if (g_focus_sound_handle[i].focus_cb_thread == mine) {
1843                                 *result = true;
1844                                 break;
1845                         }
1846                 }
1847         }
1848
1849         return ret;
1850 }
1851
1852 int mm_sound_client_register_focus(int id, int pid, const char *stream_type, bool is_for_session,
1853                                                                 mm_sound_focus_changed_cb callback, void* user_data)
1854 {
1855         int ret = MM_ERROR_NONE;
1856         int instance;
1857         int index = 0;
1858
1859         debug_fenter();
1860         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1861
1862         instance = pid;
1863
1864         for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1865                 if (g_focus_sound_handle[index].is_used == false) {
1866                         g_focus_sound_handle[index].is_used = true;
1867                         break;
1868                 }
1869         }
1870
1871         g_focus_sound_handle[index].focus_tid = instance;
1872         g_focus_sound_handle[index].handle = id;
1873         g_focus_sound_handle[index].focus_callback = callback;
1874         g_focus_sound_handle[index].user_data = user_data;
1875         g_focus_sound_handle[index].is_for_session = is_for_session;
1876         g_focus_sound_handle[index].auto_reacquire = true;
1877
1878         ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1879         if (ret == MM_ERROR_NONE) {
1880                 debug_msg("Success to register focus");
1881                 g_need_emergent_exit = TRUE;
1882                 if (_focus_init_context(index)) {
1883                         ret = MM_ERROR_SOUND_INTERNAL;
1884                         goto cleanup;
1885                 }
1886         } else {
1887                 debug_error("Error occurred : 0x%x", ret);
1888                 goto cleanup;
1889         }
1890
1891         _focus_init_callback(index, false);
1892
1893 cleanup:
1894         if (ret) {
1895                 g_focus_sound_handle[index].is_used = false;
1896         }
1897
1898         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1899         debug_fleave();
1900
1901         return ret;
1902 }
1903
1904 int mm_sound_client_unregister_focus(int id)
1905 {
1906         int ret = MM_ERROR_NONE;
1907         int instance;
1908         int index = -1;
1909
1910         debug_fenter();
1911         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1912
1913         index = _focus_find_index_by_handle(id);
1914         if (index == -1) {
1915                 debug_error("Could not find index");
1916                 ret = MM_ERROR_INVALID_ARGUMENT;
1917                 goto cleanup;
1918         }
1919         instance = g_focus_sound_handle[index].focus_tid;
1920
1921         if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1922                 debug_warning("maybe focus_callback is being called, try one more time..");
1923                 usleep(2500000); // 2.5 sec
1924                 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1925                         debug_msg("finally got focus_lock");
1926                 }
1927         }
1928
1929         ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1930         if (ret == MM_ERROR_NONE)
1931                 debug_msg("Success to unregister focus");
1932         else
1933                 debug_error("Error occurred : 0x%x", ret);
1934
1935         g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1936
1937         _focus_deinit_callback(index, false);
1938         g_focus_sound_handle[index].focus_fd = 0;
1939         g_focus_sound_handle[index].focus_tid = 0;
1940         g_focus_sound_handle[index].handle = 0;
1941         g_focus_sound_handle[index].is_used = false;
1942         _focus_deinit_context(index);
1943
1944
1945 cleanup:
1946         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1947         debug_fleave();
1948         return ret;
1949 }
1950
1951 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition, bool is_for_session)
1952 {
1953         int ret = MM_ERROR_NONE;
1954         int instance;
1955         int index = -1;
1956         bool result;
1957
1958         debug_fenter();
1959
1960         /* Since the muse server which uses this library for multiple handles executes requests from clients serially,
1961          * we can skip locking/unlocking the mutex for this case. */
1962         if (!is_for_session)
1963                 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1964
1965         index = _focus_find_index_by_handle(id);
1966         if (index == -1) {
1967                 debug_error("Could not find index");
1968                 ret = MM_ERROR_INVALID_ARGUMENT;
1969                 goto cleanup;
1970         }
1971         instance = g_focus_sound_handle[index].focus_tid;
1972
1973         ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1974         if (ret) {
1975                 debug_error("mm_sound_client_is_focus_cb_thread failed");
1976                 goto cleanup;
1977         } else if (!result) {
1978                 ret = mm_sound_proxy_set_focus_reacquisition(instance, id, reacquisition, is_for_session);
1979                 if (ret == MM_ERROR_NONE) {
1980                         debug_msg("Success to set focus reacquisition to [%d]", reacquisition);
1981                 } else {
1982                         debug_error("Error occurred : 0x%x", ret);
1983                         goto cleanup;
1984                 }
1985         } else {
1986                 debug_warning("Inside the focus cb thread, set focus reacquisition to [%d]", reacquisition);
1987         }
1988
1989         g_focus_sound_handle[index].auto_reacquire = reacquisition;
1990         debug_msg("set focus reacquisition(%d) for id(%d)", reacquisition, id);
1991
1992 cleanup:
1993         if (!is_for_session)
1994                 MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1995         debug_fleave();
1996         return ret;
1997 }
1998
1999 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
2000 {
2001         int ret = MM_ERROR_NONE;
2002         int index = -1;
2003
2004         debug_fenter();
2005
2006         if (!reacquisition) {
2007                 debug_error("Invalid parameter");
2008                 return MM_ERROR_INVALID_ARGUMENT;
2009         }
2010
2011         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2012
2013         index = _focus_find_index_by_handle(id);
2014         if (index == -1) {
2015                 debug_error("Could not find index");
2016                 ret = MM_ERROR_INVALID_ARGUMENT;
2017                 goto cleanup;
2018         }
2019
2020         *reacquisition = g_focus_sound_handle[index].auto_reacquire;
2021         debug_msg("get focus reacquisition(%d) for id(%d)", *reacquisition, id);
2022
2023 cleanup:
2024         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2025         debug_fleave();
2026         return ret;
2027 }
2028
2029 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, int *option, char **ext_info)
2030 {
2031         int ret = MM_ERROR_NONE;
2032         char *stream_type_str = NULL;
2033         char *ext_info_str = NULL;
2034
2035         debug_fenter();
2036
2037         ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, &stream_type_str, option, &ext_info_str);
2038         if (ret == MM_ERROR_NONE) {
2039                 debug_msg("Success to get stream type of acquired focus, stream_type(%s), ext_info(%s)",
2040                                 stream_type_str, ext_info_str);
2041                 *stream_type = strdup(stream_type_str);
2042                 *ext_info = strdup(ext_info_str);
2043                 g_free(stream_type_str);
2044                 g_free(ext_info_str);
2045         } else {
2046                 debug_error("Error occurred : 0x%x", ret);
2047         }
2048
2049         debug_fleave();
2050         return ret;
2051 }
2052
2053 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2054 {
2055         int ret = MM_ERROR_NONE;
2056         int instance;
2057         int index = -1;
2058
2059         debug_fenter();
2060         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2061
2062         index = _focus_find_index_by_handle(id);
2063         if (index == -1) {
2064                 debug_error("Could not find index");
2065                 ret = MM_ERROR_INVALID_ARGUMENT;
2066                 goto cleanup;
2067         }
2068         instance = g_focus_sound_handle[index].focus_tid;
2069
2070         ret = mm_sound_proxy_acquire_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2071         if (ret == MM_ERROR_NONE)
2072                 debug_msg("Success to acquire focus");
2073         else
2074                 debug_error("Error occurred : 0x%x", ret);
2075
2076 cleanup:
2077         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2078         debug_fleave();
2079         return ret;
2080 }
2081
2082 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, int option, const char *ext_info)
2083 {
2084         int ret = MM_ERROR_NONE;
2085         int instance;
2086         int index = -1;
2087
2088         debug_fenter();
2089         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2090
2091         index = _focus_find_index_by_handle(id);
2092         if (index == -1) {
2093                 debug_error("Could not find index");
2094                 ret = MM_ERROR_INVALID_ARGUMENT;
2095                 goto cleanup;
2096         }
2097         instance = g_focus_sound_handle[index].focus_tid;
2098
2099         ret = mm_sound_proxy_release_focus(instance, id, type, option, ext_info, g_focus_sound_handle[index].is_for_session);
2100         if (ret == MM_ERROR_NONE)
2101                 debug_msg("Success to release focus");
2102         else
2103                 debug_error("Error occurred : 0x%x", ret);
2104
2105 cleanup:
2106         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2107         debug_fleave();
2108         return ret;
2109 }
2110
2111 int mm_sound_client_update_stream_focus_status(int id, unsigned int status)
2112 {
2113         int ret = MM_ERROR_NONE;
2114         debug_fenter();
2115
2116         if ((ret = mm_sound_proxy_update_stream_focus_status(id, status)) != MM_ERROR_NONE)
2117                 debug_error("failed to update stream focus status, ret[0x%x]", ret);
2118
2119         debug_fleave();
2120         return ret;
2121 }
2122
2123 int mm_sound_client_deliver_focus(int pid, int src_id, int dst_id, mm_sound_focus_type_e focus_type)
2124 {
2125         int ret = MM_ERROR_NONE;
2126         debug_fenter();
2127
2128         if ((ret = mm_sound_proxy_deliver_focus(pid, src_id, dst_id, focus_type)) != MM_ERROR_NONE)
2129                 debug_error("failed to deliver focus, ret[0x%x]", ret);
2130
2131         debug_fleave();
2132         return ret;
2133 }
2134
2135 int mm_sound_client_set_focus_watch_callback(int pid, mm_sound_focus_type_e focus_type, bool is_for_session, bool is_for_monitor,
2136                                                                                         mm_sound_focus_changed_watch_cb callback, void* user_data, int *id)
2137 {
2138         int ret = MM_ERROR_NONE;
2139         int instance;
2140         int index = 0;
2141
2142         debug_fenter();
2143
2144         if (!id)
2145                 return MM_ERROR_INVALID_ARGUMENT;
2146
2147         //pthread_mutex_lock(&g_thread_mutex2);
2148
2149         instance = pid;
2150
2151         ret = mm_sound_proxy_get_unique_id(id);
2152         if (ret)
2153                 return ret;
2154
2155         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2156
2157         for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
2158                 if (g_focus_sound_handle[index].is_used == false) {
2159                         g_focus_sound_handle[index].is_used = true;
2160                         break;
2161                 }
2162         }
2163
2164         g_focus_sound_handle[index].focus_tid = instance;
2165         g_focus_sound_handle[index].handle = *id;
2166         g_focus_sound_handle[index].watch_callback = callback;
2167         g_focus_sound_handle[index].user_data = user_data;
2168         g_focus_sound_handle[index].is_for_session = is_for_session;
2169         g_focus_sound_handle[index].is_for_monitor = is_for_monitor;
2170         g_focus_sound_handle[index].unset_watch_callback_requested = false;
2171
2172         ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type,
2173                                                                                                 is_for_session, is_for_monitor, callback, user_data);
2174
2175         if (ret == MM_ERROR_NONE) {
2176                 debug_msg("Success to watch focus");
2177                 g_need_emergent_exit = TRUE;
2178                 if (_focus_init_context(index)) {
2179                         ret = MM_ERROR_SOUND_INTERNAL;
2180                         goto cleanup;
2181                 }
2182         } else {
2183                 debug_error("Error occurred : 0x%x", ret);
2184                 goto cleanup;
2185         }
2186
2187         _focus_init_callback(index, true);
2188
2189 cleanup:
2190         if (ret) {
2191                 g_focus_sound_handle[index].is_used = false;
2192         }
2193
2194         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2195         debug_fleave();
2196         return ret;
2197 }
2198
2199 int mm_sound_client_request_unset_focus_watch_callback(int id)
2200 {
2201         int ret = MM_ERROR_NONE;
2202         int index = -1;
2203
2204         debug_fenter();
2205         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2206
2207         index = _focus_watch_find_index_by_handle(id);
2208         if (index == -1) {
2209                 debug_error("Could not find index");
2210                 ret = MM_ERROR_INVALID_ARGUMENT;
2211                 goto cleanup;
2212         }
2213         g_focus_sound_handle[index].unset_watch_callback_requested = true;
2214
2215 cleanup:
2216         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2217         debug_fleave();
2218         return ret;
2219 }
2220
2221 int mm_sound_client_unset_focus_watch_callback(int id)
2222 {
2223         int ret = MM_ERROR_NONE;
2224         int index = -1;
2225
2226         debug_fenter();
2227         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
2228
2229         index = _focus_watch_find_index_by_handle(id);
2230         if (index == -1) {
2231                 debug_error("Could not find index");
2232                 ret = MM_ERROR_INVALID_ARGUMENT;
2233                 goto cleanup;
2234         }
2235
2236         g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
2237
2238         g_focus_sound_handle[index].is_used = false;
2239
2240         ret = mm_sound_proxy_unset_focus_watch_callback(g_focus_sound_handle[index].focus_tid,
2241                                                                                                         g_focus_sound_handle[index].handle,
2242                                                                                                         g_focus_sound_handle[index].is_for_session);
2243
2244         if (ret == MM_ERROR_NONE)
2245                 debug_msg("Success to unwatch focus");
2246         else
2247                 debug_error("Error occurred : 0x%x", ret);
2248
2249
2250         g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
2251
2252         _focus_deinit_callback(index, true);
2253         g_focus_sound_handle[index].focus_fd = 0;
2254         g_focus_sound_handle[index].focus_tid = 0;
2255         g_focus_sound_handle[index].handle = 0;
2256         _focus_deinit_context(index);
2257
2258 cleanup:
2259         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
2260         debug_fleave();
2261         return ret;
2262 }
2263
2264 static gboolean _idle_event_callback(void *data)
2265 {
2266         focus_idle_event_t *idle_event_data = (focus_idle_event_t*)data;
2267         int ret = MM_ERROR_NONE;
2268
2269         if (data == NULL) {
2270                 debug_error("data is null");
2271                 return FALSE;
2272         }
2273
2274         debug_msg("idle_event_data(%p): type(%d), data(%d)",
2275                 idle_event_data, idle_event_data->type, idle_event_data->data);
2276
2277         switch (idle_event_data->type) {
2278         case IDLE_EVENT_TYPE_UNSET_FOCUS_WATCH_CB:
2279                 if ((ret = mm_sound_client_unset_focus_watch_callback(idle_event_data->data)))
2280                         debug_error("Could not unset focus watch callback, id(%d), ret = %x", idle_event_data->data, ret);
2281                 break;
2282         case IDLE_EVENT_TYPE_UNREGISTER_FOCUS:
2283                 if ((ret = mm_sound_client_unregister_focus(idle_event_data->data)))
2284                         debug_error("Could not unregister focus, id(%d), ret = %x", idle_event_data->data, ret);
2285                 break;
2286         default:
2287                 debug_warning("invalid type(%d)", idle_event_data->type);
2288                 break;
2289         }
2290
2291         g_free(idle_event_data);
2292
2293         g_idle_event_src = 0;
2294
2295         MMSOUND_LEAVE_CRITICAL_SECTION(&g_event_mutex);
2296
2297         return FALSE;
2298 }
2299
2300 int mm_sound_client_execute_focus_func_in_main_context(focus_idle_event_type_e type, int data)
2301 {
2302         focus_idle_event_t *idle_event_data = NULL;
2303
2304         if (IDLE_EVENT_TYPE_MAX < type)
2305                 return MM_ERROR_INVALID_ARGUMENT;
2306
2307         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_event_mutex, MM_ERROR_SOUND_INTERNAL);
2308
2309         idle_event_data = g_new0(focus_idle_event_t, 1);
2310         idle_event_data->type = type;
2311         idle_event_data->data = data;
2312
2313         g_idle_event_src = g_idle_add_full(G_PRIORITY_HIGH,
2314                                 (GSourceFunc)_idle_event_callback,
2315                                 (gpointer)idle_event_data,
2316                                 NULL);
2317
2318         return MM_ERROR_NONE;
2319 }
2320 #endif
2321
2322
2323 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
2324 {
2325         int ret = MM_ERROR_NONE;
2326
2327         debug_fenter();
2328
2329         ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
2330
2331         debug_fleave();
2332         return ret;
2333 }
2334
2335 int mm_sound_client_remove_test_callback(unsigned int subs_id)
2336 {
2337         int ret = MM_ERROR_NONE;
2338         debug_fenter();
2339
2340         ret = mm_sound_proxy_remove_test_callback(subs_id);
2341
2342         debug_fleave();
2343         return ret;
2344 }
2345
2346
2347 int mm_sound_client_test(int a, int b, int* getv)
2348 {
2349         int ret = MM_ERROR_NONE;
2350
2351         debug_fenter();
2352
2353         ret = mm_sound_proxy_test(a, b, getv);
2354         debug_log("%d * %d -> result : %d", a, b, *getv);
2355
2356         debug_fleave();
2357
2358         return ret;
2359 }