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