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