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