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