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