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