Fix SVACE defects (remove dead code)
[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 = (unsigned int)((0x0000ffff & cb_data.handle) |(g_focus_sound_handle[focus_index].auto_reacquire << 16));
1058                                 rett = write(tmpfd, &buf, sizeof(buf));
1059                                 close(tmpfd);
1060                                 g_free(filename2);
1061                                 debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1062 #endif
1063         }
1064
1065         g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1066
1067         return TRUE;
1068 }
1069
1070 static gboolean _focus_watch_callback_handler(gpointer d)
1071 {
1072         GPollFD *data = (GPollFD*)d;
1073         int count;
1074         int tid = 0;
1075         int focus_index = 0;
1076         focus_cb_data_lib cb_data;
1077
1078         debug_fenter();
1079
1080         memset(&cb_data, 0, sizeof(focus_cb_data_lib));
1081
1082         if (!data) {
1083                 debug_error("GPollFd is null");
1084                 return FALSE;
1085         }
1086         if (data->revents & (POLLIN | POLLPRI)) {
1087                 count = read(data->fd, &cb_data, sizeof(cb_data));
1088                 if (count < 0){
1089                         char str_error[256];
1090                         strerror_r(errno, str_error, sizeof(str_error));
1091                         debug_error("GpollFD read fail, errno=%d(%s)",errno, str_error);
1092                         return FALSE;
1093                 }
1094
1095                 focus_index = _focus_find_index_by_handle(cb_data.handle);
1096                 if (focus_index == -1) {
1097                         debug_error("Could not find index");
1098                         return FALSE;
1099                 }
1100
1101                 debug_error("lock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1102                 g_mutex_lock(&g_focus_sound_handle[focus_index].focus_lock);
1103
1104                 tid = g_focus_sound_handle[focus_index].focus_tid;
1105
1106                 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);
1107
1108                 if (g_focus_sound_handle[focus_index].watch_callback == NULL) {
1109                         debug_msg("callback is null..");
1110                 } else {
1111                         debug_msg("[CALLBACK(%p) START]",g_focus_sound_handle[focus_index].watch_callback);
1112                         (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);
1113                         debug_msg("[CALLBACK END]");
1114                         if (g_focus_session_interrupt_info.user_cb) {
1115                                 debug_error("sending session interrupt callback(%p)", g_focus_session_interrupt_info.user_cb);
1116                                 (g_focus_session_interrupt_info.user_cb)(cb_data.state, cb_data.stream_type, true, g_focus_session_interrupt_info.user_data);
1117                         }
1118                 }
1119
1120 #ifdef CONFIG_ENABLE_RETCB
1121                         int rett = 0;
1122                         int tmpfd = -1;
1123                         int buf = -1;
1124                         char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[focus_index].focus_tid, cb_data.handle);
1125                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
1126                         if (tmpfd < 0) {
1127                                 char str_error[256];
1128                                 strerror_r(errno, str_error, sizeof(str_error));
1129                                 debug_error("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)\n", tid, tmpfd, filename2, errno, str_error);
1130                                 g_free(filename2);
1131                                 g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1132                                 return FALSE;
1133                         }
1134                         buf = cb_data.handle;
1135                         rett = write(tmpfd, &buf, sizeof(buf));
1136                         close(tmpfd);
1137                         g_free(filename2);
1138                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)\n", tid, rett);
1139
1140 #endif
1141
1142         }
1143
1144         debug_error("unlock focus_lock = %p", &g_focus_sound_handle[focus_index].focus_lock);
1145         g_mutex_unlock(&g_focus_sound_handle[focus_index].focus_lock);
1146
1147         debug_fleave();
1148
1149
1150         return TRUE;
1151 }
1152
1153 static void _focus_open_callback(int index, bool is_for_watching)
1154 {
1155         mode_t pre_mask;
1156         char *filename;
1157
1158         debug_fenter();
1159
1160         if (is_for_watching) {
1161                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1162         } else {
1163                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1164         }
1165         pre_mask = umask(0);
1166         if (mknod(filename, S_IFIFO|0666, 0)) {
1167                 debug_error("mknod() failure, errno(%d)", errno);
1168         }
1169         umask(pre_mask);
1170         g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
1171         if (g_focus_sound_handle[index].focus_fd == -1) {
1172                 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
1173         } else {
1174                 debug_log("Open sucess : index(%d), filename(%s), fd(%d)", index, filename, g_focus_sound_handle[index].focus_fd);
1175         }
1176         g_free(filename);
1177         filename = NULL;
1178
1179 #ifdef CONFIG_ENABLE_RETCB
1180         char *filename2;
1181
1182         if (is_for_watching) {
1183                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1184         } else {
1185                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1186         }
1187         pre_mask = umask(0);
1188         if (mknod(filename2, S_IFIFO | 0666, 0)) {
1189                 debug_error("mknod() failure, errno(%d)", errno);
1190         }
1191         umask(pre_mask);
1192         g_free(filename2);
1193         filename2 = NULL;
1194 #endif
1195         debug_fleave();
1196
1197 }
1198
1199 void _focus_close_callback(int index, bool is_for_watching)
1200 {
1201         char *filename = NULL;
1202
1203         debug_fenter();
1204
1205         if (g_focus_sound_handle[index].focus_fd < 0) {
1206                 debug_error("Close fail : index(%d)", index);
1207         } else {
1208                 close(g_focus_sound_handle[index].focus_fd);
1209                 debug_log("Close Sucess : index(%d)", index);
1210         }
1211
1212         if (is_for_watching) {
1213                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1214         } else {
1215                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1216         }
1217         if (remove(filename)) {
1218                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename, errno);
1219         }
1220         g_free(filename);
1221         filename = NULL;
1222
1223 #ifdef CONFIG_ENABLE_RETCB
1224         char *filename2;
1225
1226         if (is_for_watching) {
1227                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1228         } else {
1229                 filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", g_focus_sound_handle[index].focus_tid, g_focus_sound_handle[index].handle);
1230         }
1231         if (remove(filename2)) {
1232                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)", filename2, errno);
1233         }
1234         g_free(filename2);
1235         filename2 = NULL;
1236
1237         debug_fleave();
1238 #endif
1239
1240 }
1241
1242 static bool _focus_add_sound_callback(int index, int fd, gushort events, focus_gLoopPollHandler_t p_gloop_poll_handler )
1243 {
1244         GSource* g_src = NULL;
1245         GSourceFuncs *g_src_funcs = NULL;               /* handler function */
1246         guint g_src_id = 0;
1247         GPollFD *g_poll_fd = NULL;                      /* file descriptor */
1248
1249         debug_fenter();
1250
1251         g_mutex_init(&g_focus_sound_handle[index].focus_lock);
1252
1253         /* 1. make GSource Object */
1254         g_src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
1255         if (!g_src_funcs) {
1256                 debug_error("failed to g_malloc for g_src_funcs");
1257                 goto ERROR;
1258         }
1259
1260         g_src_funcs->prepare = _focus_fd_prepare;
1261         g_src_funcs->check = _focus_fd_check;
1262         g_src_funcs->dispatch = _focus_fd_dispatch;
1263         g_src_funcs->finalize = NULL;
1264         g_src = g_source_new(g_src_funcs, sizeof(GSource));
1265         if (!g_src) {
1266                 debug_error("failed to g_source_new for g_src");
1267                 goto ERROR;
1268         }
1269
1270         /* 2. add file description which used in g_loop() */
1271         g_poll_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
1272         if (!g_poll_fd) {
1273                 debug_error("failed to g_malloc for g_poll_fd");
1274                 goto ERROR;
1275         }
1276         g_poll_fd->fd = fd;
1277         g_poll_fd->events = events;
1278
1279         /* 3. combine g_source object and file descriptor */
1280         g_source_add_poll(g_src, g_poll_fd);
1281         g_src_id = g_source_attach(g_src, g_main_loop_get_context(g_focus_loop));
1282         if (!g_src_id) {
1283                 debug_error("failed to attach the source to context");
1284                 goto ERROR;
1285         }
1286
1287         /* 4. set callback */
1288         g_source_set_callback(g_src, p_gloop_poll_handler,(gpointer)g_poll_fd, NULL);
1289
1290         debug_log("g_malloc : g_src_funcs(%p), g_poll_fd(%p)", g_src_funcs, g_poll_fd);
1291
1292         /* 5. store to global handle */
1293         g_focus_sound_handle[index].focus_src = g_src;
1294         g_focus_sound_handle[index].g_src_funcs = g_src_funcs;
1295         g_focus_sound_handle[index].g_poll_fd = g_poll_fd;
1296
1297         debug_fleave();
1298         return true;
1299
1300 ERROR:
1301         if (g_src_funcs)
1302                 g_free(g_src_funcs);
1303         if (g_poll_fd)
1304                 g_free(g_poll_fd);
1305         if (g_src)
1306                 g_source_unref(g_src);
1307
1308         return false;
1309 }
1310
1311 static bool _focus_remove_sound_callback(int index, gushort events)
1312 {
1313         bool ret = true;
1314
1315         debug_fenter();
1316
1317         g_mutex_clear(&g_focus_sound_handle[index].focus_lock);
1318
1319         GSourceFuncs *g_src_funcs = g_focus_sound_handle[index].g_src_funcs;
1320         GPollFD *g_poll_fd = g_focus_sound_handle[index].g_poll_fd;     /* store file descriptor */
1321         if (!g_poll_fd) {
1322                 debug_error("g_poll_fd is null..");
1323                 ret = false;
1324                 goto RELEASE;
1325         }
1326         g_poll_fd->fd = g_focus_sound_handle[index].focus_fd;
1327         g_poll_fd->events = events;
1328
1329         if (!g_focus_sound_handle[index].focus_src) {
1330                 debug_error("g_focus_sound_handle[%d].focus_src is null..", index);
1331                 ret = false;
1332                 goto RELEASE;
1333         }
1334         g_source_remove_poll(g_focus_sound_handle[index].focus_src, g_poll_fd);
1335         debug_log("g_source_remove_poll : fd(%d), event(%x)", g_poll_fd->fd, g_poll_fd->events);
1336
1337 RELEASE:
1338         if (g_focus_sound_handle[index].focus_src)
1339                 g_source_destroy(g_focus_sound_handle[index].focus_src);
1340
1341         debug_log("g_free : g_src_funcs(%x), g_poll_fd(%x)", g_src_funcs, g_poll_fd);
1342         if (g_src_funcs) {
1343                 g_free(g_src_funcs);
1344                 g_src_funcs = NULL;
1345         }
1346         if (g_poll_fd) {
1347                 g_free(g_poll_fd);
1348                 g_poll_fd = NULL;
1349         }
1350         if (g_focus_sound_handle[index].focus_src)
1351                 g_source_unref(g_focus_sound_handle[index].focus_src);
1352
1353         g_focus_sound_handle[index].g_src_funcs = NULL;
1354         g_focus_sound_handle[index].g_poll_fd = NULL;
1355         g_focus_sound_handle[index].focus_src = NULL;
1356         g_focus_sound_handle[index].focus_callback = NULL;
1357         g_focus_sound_handle[index].watch_callback = NULL;
1358
1359         debug_fleave();
1360         return ret;
1361 }
1362
1363
1364 static void _focus_add_callback(int index, bool is_for_watching)
1365 {
1366         debug_fenter();
1367         if (!is_for_watching) {
1368                 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_callback_handler)) {
1369                         debug_error("failed to _focus_add_sound_callback()");
1370                         //return false;
1371                 }
1372         } else { // need to check if it's necessary
1373                 if (!_focus_add_sound_callback(index, g_focus_sound_handle[index].focus_fd, (gushort)POLLIN | POLLPRI, _focus_watch_callback_handler)) {
1374                         debug_error("failed to _focus_add_sound_callback()");
1375                         //return false;
1376                 }
1377         }
1378         debug_fleave();
1379 }
1380
1381 static void _focus_remove_callback(int index)
1382 {
1383         debug_fenter();
1384         if (!_focus_remove_sound_callback(index, (gushort)POLLIN | POLLPRI)) {
1385                 debug_error("failed to __focus_remove_sound_callback()");
1386                 //return false;
1387         }
1388         debug_fleave();
1389 }
1390
1391 static void _focus_init_callback(int index, bool is_for_watching)
1392 {
1393         debug_fenter();
1394         _focus_open_callback(index, is_for_watching);
1395         _focus_add_callback(index, is_for_watching);
1396         debug_fleave();
1397 }
1398
1399 static void _focus_destroy_callback(int index, bool is_for_watching)
1400 {
1401         debug_fenter();
1402         _focus_remove_callback(index);
1403         _focus_close_callback(index, is_for_watching);
1404         debug_fleave();
1405 }
1406
1407 int mm_sound_client_get_unique_id(int *id)
1408 {
1409         int ret = MM_ERROR_NONE;
1410
1411         debug_fenter();
1412
1413         if (!id)
1414                 ret = MM_ERROR_INVALID_ARGUMENT;
1415         else
1416                 ret = mm_sound_proxy_get_unique_id(id);
1417
1418         debug_fleave();
1419
1420         return ret;
1421 }
1422
1423 int mm_sound_client_is_focus_cb_thread(GThread *mine, bool *result)
1424 {
1425         int ret = MM_ERROR_NONE;
1426
1427         if (!mine || !result)
1428                 ret = MM_ERROR_INVALID_ARGUMENT;
1429         else {
1430                 if (mine == g_focus_thread)
1431                         *result = true;
1432                 else
1433                         *result = false;
1434         }
1435
1436         return ret;
1437 }
1438
1439 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)
1440 {
1441         int ret = MM_ERROR_NONE;
1442         int instance;
1443         int index = 0;
1444
1445         debug_fenter();
1446         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1447
1448         instance = pid;
1449
1450         for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1451                 if (g_focus_sound_handle[index].is_used == false) {
1452                         g_focus_sound_handle[index].is_used = true;
1453                         break;
1454                 }
1455         }
1456
1457         g_focus_sound_handle[index].focus_tid = instance;
1458         g_focus_sound_handle[index].handle = id;
1459         g_focus_sound_handle[index].focus_callback = callback;
1460         g_focus_sound_handle[index].user_data = user_data;
1461         g_focus_sound_handle[index].is_for_session = is_for_session;
1462         g_focus_sound_handle[index].auto_reacquire = true;
1463
1464         ret = mm_sound_proxy_register_focus(id, pid, stream_type, callback, is_for_session, user_data);
1465
1466         if (ret == MM_ERROR_NONE) {
1467                 debug_msg("[Client] Success to register focus\n");
1468                 g_need_emergent_exit = TRUE;
1469                 if (!g_focus_thread) {
1470                         GMainContext* focus_context = g_main_context_new ();
1471                         g_focus_loop = g_main_loop_new (focus_context, FALSE);
1472                         g_main_context_unref(focus_context);
1473                         g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1474                         if (g_focus_thread == NULL) {
1475                                 debug_error ("could not create thread..");
1476                                 g_main_loop_unref(g_focus_loop);
1477                                 g_focus_sound_handle[index].is_used = false;
1478                                 ret = MM_ERROR_SOUND_INTERNAL;
1479                                 goto cleanup;
1480                         }
1481                 }
1482         } else {
1483                 debug_error("[Client] Error occurred : 0x%x \n",ret);
1484                 g_focus_sound_handle[index].is_used = false;
1485                 goto cleanup;
1486         }
1487
1488         _focus_init_callback(index, false);
1489
1490 cleanup:
1491         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1492         debug_fleave();
1493
1494         return ret;
1495 }
1496
1497 int mm_sound_client_unregister_focus(int id)
1498 {
1499         int ret = MM_ERROR_NONE;
1500         int instance;
1501         int index = -1;
1502
1503         debug_fenter();
1504         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1505
1506         index = _focus_find_index_by_handle(id);
1507         if (index == -1) {
1508                 debug_error("Could not find index");
1509                 ret = MM_ERROR_INVALID_ARGUMENT;
1510                 goto cleanup;
1511         }
1512         instance = g_focus_sound_handle[index].focus_tid;
1513
1514         if (!g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1515                 debug_warning("maybe focus_callback is being called, try one more time..");
1516                 usleep(2500000); // 2.5 sec
1517                 if (g_mutex_trylock(&g_focus_sound_handle[index].focus_lock)) {
1518                         debug_msg("finally got focus_lock");
1519                 }
1520         }
1521
1522         ret = mm_sound_proxy_unregister_focus(instance, id, g_focus_sound_handle[index].is_for_session);
1523
1524         if (ret == MM_ERROR_NONE)
1525                 debug_msg("[Client] Success to unregister focus\n");
1526         else
1527                 debug_error("[Client] Error occurred : 0x%x \n",ret);
1528
1529         g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1530
1531         _focus_destroy_callback(index, false);
1532         g_focus_sound_handle[index].focus_fd = 0;
1533         g_focus_sound_handle[index].focus_tid = 0;
1534         g_focus_sound_handle[index].handle = 0;
1535         g_focus_sound_handle[index].is_used = false;
1536 cleanup:
1537         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1538         debug_fleave();
1539         return ret;
1540 }
1541
1542 int mm_sound_client_set_focus_reacquisition(int id, bool reacquisition)
1543 {
1544         int ret = MM_ERROR_NONE;
1545         int instance;
1546         int index = -1;
1547         bool result;
1548
1549         debug_fenter();
1550         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1551
1552         index = _focus_find_index_by_handle(id);
1553         if (index == -1) {
1554                 debug_error("Could not find index");
1555                 ret = MM_ERROR_INVALID_ARGUMENT;
1556                 goto cleanup;
1557         }
1558         instance = g_focus_sound_handle[index].focus_tid;
1559
1560         ret = mm_sound_client_is_focus_cb_thread(g_thread_self(), &result);
1561         if (ret) {
1562                 debug_error("[Client] mm_sound_client_is_focus_cb_thread failed");
1563                 goto cleanup;
1564         } else if (!result) {
1565                 ret = mm_sound_proxy_set_foucs_reacquisition(instance, id, reacquisition);
1566                 if (ret == MM_ERROR_NONE) {
1567                         debug_msg("[Client] Success to set focus reacquisition\n");
1568                 } else {
1569                         debug_error("[Client] Error occurred : 0x%x \n",ret);
1570                         goto cleanup;
1571                 }
1572         } else {
1573                 debug_warning("[Client] Inside the focus cb thread, bypassing dbus method call");
1574         }
1575
1576         g_focus_sound_handle[index].auto_reacquire = reacquisition;
1577
1578 cleanup:
1579         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1580         debug_fleave();
1581         return ret;
1582 }
1583
1584 int mm_sound_client_get_focus_reacquisition(int id, bool *reacquisition)
1585 {
1586         int ret = MM_ERROR_NONE;
1587         int index = -1;
1588
1589         debug_fenter();
1590
1591         if (!reacquisition) {
1592                 debug_error("Invalid parameter");
1593                 return MM_ERROR_INVALID_ARGUMENT;
1594         }
1595
1596         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1597
1598         index = _focus_find_index_by_handle(id);
1599         if (index == -1) {
1600                 debug_error("Could not find index");
1601                 ret = MM_ERROR_INVALID_ARGUMENT;
1602                 goto cleanup;
1603         }
1604
1605         *reacquisition = g_focus_sound_handle[index].auto_reacquire;
1606
1607 cleanup:
1608         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1609         debug_fleave();
1610         return ret;
1611 }
1612
1613 int mm_sound_client_get_acquired_focus_stream_type(int focus_type, char **stream_type, char **additional_info)
1614 {
1615         int ret = MM_ERROR_NONE;
1616
1617         debug_fenter();
1618
1619         ret = mm_sound_proxy_get_acquired_focus_stream_type(focus_type, stream_type, additional_info);
1620         if (ret == MM_ERROR_NONE)
1621                 debug_msg("[Client] Success to get stream type of acquired focus, stream_type(%s), additional_info(%s)\n", *stream_type, *additional_info);
1622         else
1623                 debug_error("[Client] Error occurred : 0x%x \n",ret);
1624
1625         debug_fleave();
1626         return ret;
1627 }
1628
1629 int mm_sound_client_acquire_focus(int id, mm_sound_focus_type_e type, const char *option)
1630 {
1631         int ret = MM_ERROR_NONE;
1632         int instance;
1633         int index = -1;
1634
1635         debug_fenter();
1636         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1637
1638         index = _focus_find_index_by_handle(id);
1639         if (index == -1) {
1640                 debug_error("Could not find index");
1641                 ret = MM_ERROR_INVALID_ARGUMENT;
1642                 goto cleanup;
1643         }
1644         instance = g_focus_sound_handle[index].focus_tid;
1645
1646         ret = mm_sound_proxy_acquire_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1647
1648         if (ret == MM_ERROR_NONE)
1649                 debug_msg("[Client] Success to acquire focus\n");
1650         else
1651                 debug_error("[Client] Error occurred : 0x%x \n",ret);
1652
1653 cleanup:
1654         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1655         debug_fleave();
1656         return ret;
1657 }
1658
1659 int mm_sound_client_release_focus(int id, mm_sound_focus_type_e type, const char *option)
1660 {
1661         int ret = MM_ERROR_NONE;
1662         int instance;
1663         int index = -1;
1664
1665         debug_fenter();
1666         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1667
1668         index = _focus_find_index_by_handle(id);
1669         if (index == -1) {
1670                 debug_error("Could not find index");
1671                 ret = MM_ERROR_INVALID_ARGUMENT;
1672                 goto cleanup;
1673         }
1674         instance = g_focus_sound_handle[index].focus_tid;
1675
1676         ret = mm_sound_proxy_release_focus(instance, id, type, option, g_focus_sound_handle[index].is_for_session);
1677
1678         if (ret == MM_ERROR_NONE)
1679                 debug_msg("[Client] Success to release focus\n");
1680         else
1681                 debug_error("[Client] Error occurred : 0x%x \n",ret);
1682
1683 cleanup:
1684         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1685         debug_fleave();
1686         return ret;
1687 }
1688
1689 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)
1690 {
1691         int ret = MM_ERROR_NONE;
1692         int instance;
1693         int index = 0;
1694
1695         debug_fenter();
1696
1697         if (!id)
1698                 return MM_ERROR_INVALID_ARGUMENT;
1699
1700         //pthread_mutex_lock(&g_thread_mutex2);
1701
1702         instance = pid;
1703
1704         ret = mm_sound_proxy_get_unique_id(id);
1705         if (ret)
1706                 return ret;
1707
1708         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1709
1710         for (index = 0; index < FOCUS_HANDLE_MAX - 1; index++) {
1711                 if (g_focus_sound_handle[index].is_used == false) {
1712                         g_focus_sound_handle[index].is_used = true;
1713                         break;
1714                 }
1715         }
1716
1717         g_focus_sound_handle[index].focus_tid = instance;
1718         g_focus_sound_handle[index].handle = *id;
1719         g_focus_sound_handle[index].watch_callback = callback;
1720         g_focus_sound_handle[index].user_data = user_data;
1721         g_focus_sound_handle[index].is_for_session = is_for_session;
1722
1723         ret = mm_sound_proxy_set_focus_watch_callback(pid, g_focus_sound_handle[index].handle, focus_type, callback, is_for_session, user_data);
1724
1725         if (ret == MM_ERROR_NONE) {
1726                 debug_msg("[Client] Success to watch focus");
1727                 g_need_emergent_exit = TRUE;
1728                 if (!g_focus_thread) {
1729                         GMainContext* focus_context = g_main_context_new ();
1730                         g_focus_loop = g_main_loop_new (focus_context, FALSE);
1731                         g_main_context_unref(focus_context);
1732                         g_focus_thread = g_thread_new("focus-callback-thread", _focus_thread_func, NULL);
1733                         if (g_focus_thread == NULL) {
1734                                 debug_error ("could not create thread..");
1735                                 g_main_loop_unref(g_focus_loop);
1736                                 ret = MM_ERROR_SOUND_INTERNAL;
1737                                 goto cleanup;
1738                         }
1739                 }
1740         } else {
1741                 debug_error("[Client] Error occurred : 0x%x",ret);
1742                 goto cleanup;
1743         }
1744
1745         _focus_init_callback(index, true);
1746
1747 cleanup:
1748
1749         if (ret) {
1750                 g_focus_sound_handle[index].is_used = false;
1751         }
1752
1753         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1754         debug_fleave();
1755         return ret;
1756 }
1757
1758 int mm_sound_client_unset_focus_watch_callback(int id)
1759 {
1760         int ret = MM_ERROR_NONE;
1761         int index = -1;
1762         debug_fenter();
1763         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_index_mutex, MM_ERROR_SOUND_INTERNAL);
1764
1765         index = _focus_find_index_by_handle(id);
1766         if (index == -1) {
1767                 debug_error("Could not find index");
1768                 ret = MM_ERROR_INVALID_ARGUMENT;
1769                 goto cleanup;
1770         }
1771
1772         g_mutex_lock(&g_focus_sound_handle[index].focus_lock);
1773
1774         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);
1775
1776         if (ret == MM_ERROR_NONE)
1777                 debug_msg("[Client] Success to unwatch focus\n");
1778         else
1779                 debug_error("[Client] Error occurred : 0x%x \n",ret);
1780
1781
1782         g_mutex_unlock(&g_focus_sound_handle[index].focus_lock);
1783
1784         _focus_destroy_callback(index, true);
1785         g_focus_sound_handle[index].focus_fd = 0;
1786         g_focus_sound_handle[index].focus_tid = 0;
1787         g_focus_sound_handle[index].handle = 0;
1788         g_focus_sound_handle[index].is_used = false;
1789 cleanup:
1790         MMSOUND_LEAVE_CRITICAL_SECTION(&g_index_mutex);
1791         debug_fleave();
1792         return ret;
1793 }
1794 #endif
1795
1796
1797 int mm_sound_client_add_test_callback(mm_sound_test_cb func, void* user_data, unsigned int *subs_id)
1798 {
1799         int ret = MM_ERROR_NONE;
1800
1801         debug_fenter();
1802
1803         ret = mm_sound_proxy_add_test_callback(func, user_data, g_free, subs_id);
1804
1805         debug_fleave();
1806         return ret;
1807 }
1808
1809 int mm_sound_client_remove_test_callback(unsigned int subs_id)
1810 {
1811         int ret = MM_ERROR_NONE;
1812         debug_fenter();
1813
1814         ret = mm_sound_proxy_remove_test_callback(subs_id);
1815
1816         debug_fleave();
1817         return ret;
1818 }
1819
1820
1821 int mm_sound_client_test(int a, int b, int* getv)
1822 {
1823         int ret = MM_ERROR_NONE;
1824
1825         debug_fenter();
1826
1827         ret = mm_sound_proxy_test(a, b, getv);
1828         debug_log("%d * %d -> result : %d", a, b, *getv);
1829
1830         debug_fleave();
1831
1832         return ret;
1833 }