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