Fix build warnings
[platform/core/multimedia/libmm-sound.git] / mm_sound.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 <memory.h>
24 #include <unistd.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29
30 #include <sys/stat.h>
31 #include <errno.h>
32
33 #include <vconf.h>
34 #include <mm_types.h>
35 #include <mm_error.h>
36 #include <mm_message.h>
37 #include <mm_session_private.h>
38 #include <mm_debug.h>
39 #include "include/mm_sound_private.h"
40 #include "include/mm_sound.h"
41 #include "include/mm_sound_utils.h"
42 #include "include/mm_sound_client.h"
43 #include "include/mm_sound_pa_client.h"
44 #include "include/mm_ipc.h"
45 #include "include/mm_sound_common.h"
46
47
48 #define VOLUME_MAX_MULTIMEDIA   16
49 #define VOLUME_MAX_BASIC                8
50 #define VOLUME_MAX_SINGLE               1
51
52
53 #define MASTER_VOLUME_MAX 100
54 #define MASTER_VOLUME_MIN 0
55
56 #include <gio/gio.h>
57
58 static GList *g_subscribe_cb_list = NULL;
59 static pthread_mutex_t g_subscribe_cb_list_mutex = PTHREAD_MUTEX_INITIALIZER;
60
61 #define MM_SOUND_DBUS_BUS_NAME_PREPIX  "org.tizen.MMSound"
62 #define MM_SOUND_DBUS_OBJECT_PATH  "/org/tizen/MMSound"
63 #define MM_SOUND_DBUS_INTERFACE    "org.tizen.mmsound"
64
65 GDBusConnection *g_dbus_conn_mmsound;
66
67 int g_dbus_signal_values[MM_SOUND_SIGNAL_MAX] = {0,};
68
69 const char* dbus_signal_name_str[] = {
70         "ReleaseInternalFocus",
71 };
72
73 typedef struct _subscribe_cb {
74         mm_sound_signal_name_t signal_type;
75         mm_sound_signal_callback callback;
76         void *user_data;
77         unsigned int id;
78         int client_pid;
79 } subscribe_cb_t;
80
81 static const char* _get_volume_str (volume_type_t type)
82 {
83         static const char *volume_type_str[VOLUME_TYPE_MAX] =
84                 { "SYSTEM", "NOTIFICATION", "ALARM", "RINGTONE", "MEDIA", "CALL", "VOIP", "VOICE", "FIXED"};
85
86         return (type >= VOLUME_TYPE_SYSTEM && type < VOLUME_TYPE_MAX)? volume_type_str[type] : "Unknown";
87 }
88
89 static int _validate_volume(volume_type_t type, int value)
90 {
91         if (value < 0)
92                 return -1;
93
94         switch (type)
95         {
96         case VOLUME_TYPE_CALL:
97         case VOLUME_TYPE_VOIP:
98                 if (value >= VOLUME_MAX_BASIC) {
99                         return -1;
100                 }
101                 break;
102         case VOLUME_TYPE_SYSTEM:
103         case VOLUME_TYPE_MEDIA:
104         case VOLUME_TYPE_ALARM:
105         case VOLUME_TYPE_NOTIFICATION:
106         case VOLUME_TYPE_RINGTONE:
107         case VOLUME_TYPE_VOICE:
108                 if (value >= VOLUME_MAX_MULTIMEDIA) {
109                         return -1;
110                 }
111                 break;
112         default:
113                 return -1;
114                 break;
115         }
116         return 0;
117 }
118
119 EXPORT_API
120 int mm_sound_volume_remove_callback(volume_type_t type)
121 {
122         /* FIXME : Will be removed */
123         return MM_ERROR_NOT_SUPPORT_API;
124 }
125
126 EXPORT_API
127 int mm_sound_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data, unsigned int *subs_id)
128 {
129         int ret = MM_ERROR_NONE;
130
131         if (func == NULL || subs_id == NULL) {
132                 debug_error("argument is not valid\n");
133                 return MM_ERROR_INVALID_ARGUMENT;
134         }
135
136         ret = mm_sound_client_add_volume_changed_callback(func, user_data, subs_id);
137         if (ret < 0) {
138                 debug_error("Can not add volume changed callback, ret = %x\n", ret);
139         }
140
141         return ret;
142 }
143
144 EXPORT_API
145 int mm_sound_remove_volume_changed_callback(unsigned int subs_id)
146 {
147         int ret = MM_ERROR_NONE;
148
149         ret = mm_sound_client_remove_volume_changed_callback(subs_id);
150         if (ret < 0) {
151                 debug_error("Can not remove volume changed callback, ret = %x\n", ret);
152         }
153
154         return ret;
155 }
156
157 EXPORT_API
158 int mm_sound_volume_set_value(volume_type_t volume_type, const unsigned int volume_level)
159 {
160         int ret = MM_ERROR_NONE;
161
162         debug_msg("type = (%d)%s, value = %d", volume_type, _get_volume_str(volume_type), volume_level);
163
164         /* Check input param */
165         if (0 > _validate_volume(volume_type, (int)volume_level)) {
166                 debug_error("invalid volume type %d, value %u\n", volume_type, volume_level);
167                 return MM_ERROR_INVALID_ARGUMENT;
168         }
169
170         ret = mm_sound_util_volume_set_value_by_type(volume_type, volume_level);
171         if (ret == MM_ERROR_NONE) {
172                 /* update shared memory value */
173                 if(MM_ERROR_NONE != mm_sound_client_set_volume_by_type(volume_type, volume_level)) {
174                         debug_error("Can not set volume to shared memory 0x%x\n", ret);
175                 }
176         }
177
178         return ret;
179 }
180
181 EXPORT_API
182 int mm_sound_volume_get_value(volume_type_t type, unsigned int *value)
183 {
184         int ret = MM_ERROR_NONE;
185
186         /* Check input param */
187         if (value == NULL) {
188                 debug_error("invalid argument\n");
189                 return MM_ERROR_INVALID_ARGUMENT;
190         }
191         if (type < 0 || type >= VOLUME_TYPE_MAX) {
192                 debug_error("invalid volume type value %d\n", type);
193                 return MM_ERROR_INVALID_ARGUMENT;
194         }
195
196         ret = mm_sound_util_volume_get_value_by_type(type, value);
197
198         debug_msg("returned %s = %d", _get_volume_str(type), *value);
199         return ret;
200 }
201
202 EXPORT_API
203 int mm_sound_volume_primary_type_set(volume_type_t type)
204 {
205         int ret = MM_ERROR_NONE;
206
207         /* Check input param */
208         if(type < VOLUME_TYPE_UNKNOWN || type >= VOLUME_TYPE_MAX) {
209                 debug_error("invalid argument\n");
210                 return MM_ERROR_INVALID_ARGUMENT;
211         }
212
213         if (vconf_set_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, type)) {
214                 debug_error("could not set vconf for RIMARY_VOLUME_TYPE\n");
215                 ret = MM_ERROR_SOUND_INTERNAL;
216         } else {
217                 debug_msg("set primary volume type forcibly %d(%s)", type, _get_volume_str(type));
218         }
219
220         return ret;
221 }
222
223 EXPORT_API
224 int mm_sound_volume_primary_type_get(volume_type_t *type)
225 {
226         int ret = MM_ERROR_NONE;
227         int voltype = VOLUME_TYPE_RINGTONE;
228
229         /* Check input param */
230         if(type == NULL) {
231                 debug_error("invalid argument\n");
232                 return MM_ERROR_INVALID_ARGUMENT;
233         }
234
235         /* check force set */
236         if (vconf_get_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, &voltype)) {
237                 debug_error("could not get vconf for PRIMARY_VOLUME_TYPE\n");
238                 ret = MM_ERROR_SOUND_INTERNAL;
239         } else {
240                 debug_msg("get primary volume type %d(%s)", voltype, _get_volume_str(voltype));
241                 *type = voltype;
242         }
243
244         return ret;
245 }
246
247 ///////////////////////////////////
248 ////     MMSOUND PLAY APIs
249 ///////////////////////////////////
250 static inline void _mm_sound_fill_play_param(MMSoundPlayParam *param, const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int priority, int handle_route)
251 {
252         param->filename = filename;
253         param->volume = 0; //volume value dose not effect anymore
254         param->callback = callback;
255         param->data = data;
256         param->loop = 1;
257         param->volume_config = volume_config;
258         param->priority = priority;
259         param->handle_route = handle_route;
260 }
261
262 EXPORT_API
263 int mm_sound_play_loud_solo_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
264 {
265         MMSoundPlayParam param = { 0, };
266
267     /* FIXME : this function will be deleted */
268         _mm_sound_fill_play_param(&param, filename, volume_config, callback, data, HANDLE_PRIORITY_NORMAL, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
269         return mm_sound_play_sound_ex(&param, handle);
270 }
271
272 EXPORT_API
273 int mm_sound_play_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
274 {
275         MMSoundPlayParam param = { 0, };
276
277         _mm_sound_fill_play_param(&param, filename, volume_config, callback, data, HANDLE_PRIORITY_NORMAL, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
278         return mm_sound_play_sound_ex(&param, handle);
279 }
280
281 EXPORT_API
282 int mm_sound_play_sound_ex(MMSoundPlayParam *param, int *handle)
283 {
284         int err;
285         int lhandle = -1;
286         int volume_type = 0;
287         /* Check input param */
288         if (param == NULL) {
289                 debug_error("param is null\n");
290                 return MM_ERROR_INVALID_ARGUMENT;
291         }
292
293         volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
294
295         if (param->filename == NULL) {
296                 debug_error("filename is NULL\n");
297                 return MM_ERROR_SOUND_FILE_NOT_FOUND;
298         }
299         if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
300                 debug_error("Volume type is invalid %d\n", volume_type);
301                 return MM_ERROR_INVALID_ARGUMENT;
302         }
303
304         debug_warning ("play sound : priority=[%d], handle_route=[%d]\n", param->priority, param->handle_route);
305
306         /* Play sound */
307         err = mm_sound_client_play_sound(param, 0, &lhandle);
308         if (err < 0) {
309                 debug_error("Failed to play sound\n");
310                 return err;
311         }
312
313         /* Set handle to return */
314         if (handle) {
315                 *handle = lhandle;
316         } else {
317                 debug_critical("The sound hadle cannot be get [%d]\n", lhandle);
318         }
319
320         debug_warning ("success : handle=[%p]\n", handle);
321
322         return MM_ERROR_NONE;
323 }
324
325 EXPORT_API
326 int mm_sound_play_sound_with_stream_info(const char *filename, char *stream_type, int stream_id, mm_sound_stop_callback_func callback, void *data, int *handle)
327 {
328         MMSoundPlayParam param = { 0, };
329         int err;
330
331         param.filename = filename;
332         param.volume = 0; //volume value dose not effect anymore
333         param.callback = callback;
334         param.data = data;
335         param.loop = 1;
336         param.priority = HANDLE_PRIORITY_NORMAL;
337         param.handle_route = MM_SOUND_HANDLE_ROUTE_USING_CURRENT;
338
339         err = mm_sound_client_play_sound_with_stream_info(&param, handle, stream_type, stream_id);
340         if (err < 0) {
341                 debug_error("Failed to play sound\n");
342                 return err;
343         }
344
345         debug_warning ("success : handle=[%p]\n", handle);
346
347         return MM_ERROR_NONE;
348
349 }
350
351
352 EXPORT_API
353 int mm_sound_stop_sound(int handle)
354 {
355         int err;
356
357         debug_warning ("enter : handle=[%p]\n", handle);
358         /* Stop sound */
359         err = mm_sound_client_stop_sound(handle);
360         if (err < 0) {
361                 debug_error("Fail to stop sound\n");
362                 return err;
363         }
364         debug_warning ("success : handle=[%p]\n", handle);
365
366         return MM_ERROR_NONE;
367 }
368
369 ///////////////////////////////////
370 ////     MMSOUND TONE APIs
371 ///////////////////////////////////
372 EXPORT_API
373 int mm_sound_play_tone_ex (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle, bool enable_session)
374 {
375         int lhandle = -1;
376         int err = MM_ERROR_NONE;
377         int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
378
379         debug_fenter();
380
381         /* Check input param */
382         if (duration < -1) {
383                 debug_error("number is invalid %d\n", duration);
384                 return MM_ERROR_INVALID_ARGUMENT;
385         }
386         if (num < MM_SOUND_TONE_DTMF_0 || num >= MM_SOUND_TONE_NUM) {
387                 debug_error("TONE Value is invalid %d\n", num);
388                 return MM_ERROR_INVALID_ARGUMENT;
389         }
390         if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
391                 debug_error("Volume type is invalid %d\n", volume_type);
392                 return MM_ERROR_INVALID_ARGUMENT;
393         }
394         if (volume < 0.0 || volume > 1.0) {
395                 debug_error("Volume Value is invalid %d\n", volume);
396                 return MM_ERROR_INVALID_ARGUMENT;
397         }
398
399         /* Play tone */
400         debug_msg("Call MMSoundClientPlayTone\n");
401         err = mm_sound_client_play_tone(num, volume_config, volume, duration, &lhandle, enable_session);
402         if (err < 0) {
403                 debug_error("Failed to play sound\n");
404                 return err;
405         }
406
407         /* Set handle to return */
408         if (handle)
409                 *handle = lhandle;
410         else
411                 debug_critical("The sound handle cannot be get [%d]\n", lhandle);
412
413         debug_fleave();
414         return MM_ERROR_NONE;
415 }
416
417 EXPORT_API
418 int mm_sound_play_tone_with_stream_info(MMSoundTone_t tone, char *stream_type, int stream_id, const double volume, const int duration, int *handle)
419 {
420
421         int err = MM_ERROR_NONE;
422
423         err = mm_sound_client_play_tone_with_stream_info(tone, stream_type, stream_id, volume, duration, handle);
424         if (err <0) {
425                 debug_error("Failed to play sound\n");
426                 return err;
427         }
428
429         return err;
430
431 }
432
433
434 EXPORT_API
435 int mm_sound_play_tone (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle)
436 {
437         return mm_sound_play_tone_ex (num, volume_config, volume, duration, handle, true);
438 }
439
440 ///////////////////////////////////
441 ////     MMSOUND ROUTING APIs
442 ///////////////////////////////////
443
444 EXPORT_API
445 int mm_sound_test(int a, int b, int* getv)
446 {
447         int ret = MM_ERROR_NONE;
448
449         debug_log("mm_sound_test enter");
450         if (!getv) {
451                 debug_error("argu null");
452                 return MM_ERROR_INVALID_ARGUMENT;
453         }
454         ret = mm_sound_client_test(a, b, getv);
455         if (ret < 0) {
456                 debug_error("Can not mm sound test, ret = %x\n", ret);
457         }
458         debug_log("mm_sound_test leave");
459
460         return ret;
461 }
462
463 EXPORT_API
464 int mm_sound_add_test_callback(mm_sound_test_cb func, void *user_data, unsigned int *subs_id)
465 {
466         int ret = MM_ERROR_NONE;
467
468         debug_log("mm_sound_add_test_callback enter");
469         if (!func || !subs_id) {
470                 debug_error("argument is not valid\n");
471                 return MM_ERROR_INVALID_ARGUMENT;
472         }
473
474         ret = mm_sound_client_add_test_callback(func, user_data, subs_id);
475         if (ret < 0) {
476                 debug_error("Can not add test callback, ret = %x\n", ret);
477         }
478         debug_log("mm_sound_add_test_callback leave");
479
480         return ret;
481 }
482
483 EXPORT_API
484 int mm_sound_remove_test_callback(unsigned int subs_id)
485 {
486         int ret = MM_ERROR_NONE;
487
488         debug_log("mm_sound_remove_test_callback enter");
489         ret = mm_sound_client_remove_test_callback(subs_id);
490         if (ret < 0) {
491                 debug_error("Can not remove test callback, ret = %x\n", ret);
492         }
493         debug_log("mm_sound_remove_test_callback leave");
494
495         return ret;
496 }
497
498 static int _convert_signal_name_str_to_enum (const char *name_str, mm_sound_signal_name_t *name_enum) {
499         int ret = MM_ERROR_NONE;
500
501         if (!name_str || !name_enum)
502                 return MM_ERROR_INVALID_ARGUMENT;
503
504         if (!strncmp(name_str, "ReleaseInternalFocus", strlen("ReleaseInternalFocus"))) {
505                 *name_enum = MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS;
506         } else {
507                 ret = MM_ERROR_INVALID_ARGUMENT;
508                 LOGE("not supported signal name(%s), err(0x%08x)", name_str, ret);
509         }
510         return ret;
511 }
512
513 static void _dbus_signal_callback (const char *signal_name, int value, void *user_data)
514 {
515         int ret = MM_ERROR_NONE;
516         mm_sound_signal_name_t signal;
517         subscribe_cb_t *subscribe_cb = (subscribe_cb_t*)user_data;
518
519         debug_fenter();
520
521         if (!subscribe_cb)
522                 return;
523
524         ret = _convert_signal_name_str_to_enum(signal_name, &signal);
525         if (ret)
526                 return;
527
528         debug_msg ("signal_name[%s], value[%d], user_data[0x%x]\n", signal_name, value, user_data);
529
530         if (subscribe_cb->signal_type == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
531                 /* Trigger the signal callback when it comes from the same process.
532                 * In this case, the second integer argument is consist of
533                 * |<-- pid (16bits) -->|<-- value (16bits) -->|,
534                 * FYI, #define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000).
535                 * In case of daemon usage, it uses the client_pid of subscribe_cb. */
536                 if ((subscribe_cb->client_pid ? subscribe_cb->client_pid : getpid()) == (value >> 16)) {
537                         subscribe_cb->callback(signal, (value & 0x0000FFFF), subscribe_cb->user_data);
538                 }
539         } else {
540                 subscribe_cb->callback(signal, value, subscribe_cb->user_data);
541         }
542
543         debug_fleave();
544
545         return;
546 }
547
548 static void signal_callback(GDBusConnection *conn,
549                                                            const gchar *sender_name,
550                                                            const gchar *object_path,
551                                                            const gchar *interface_name,
552                                                            const gchar *signal_name,
553                                                            GVariant *parameters,
554                                                            gpointer user_data)
555 {
556         int value=0;
557         const GVariantType* value_type;
558
559         debug_msg ("sender : %s, object : %s, interface : %s, signal : %s",
560                         sender_name, object_path, interface_name, signal_name);
561         if (g_variant_is_of_type(parameters, G_VARIANT_TYPE("(i)"))) {
562                 g_variant_get(parameters, "(i)",&value);
563                 debug_msg(" - value : %d\n", value);
564                 _dbus_signal_callback(signal_name, value, user_data);
565         } else  {
566                 value_type = g_variant_get_type(parameters);
567                 debug_warning("signal type is %s", value_type);
568         }
569 }
570
571 EXPORT_API
572 int mm_sound_subscribe_signal(mm_sound_signal_name_t signal, unsigned int *subscribe_id, mm_sound_signal_callback callback, void *user_data)
573 {
574         int ret = MM_ERROR_NONE;
575         GError *err = NULL;
576
577         subscribe_cb_t *subscribe_cb = NULL;
578
579         debug_fenter();
580
581         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
582
583         if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX || !subscribe_id) {
584                 debug_error ("invalid argument, signal(%d), subscribe_id(0x%p)", signal, subscribe_id);
585                 ret = MM_ERROR_INVALID_ARGUMENT;
586                 goto error;
587         }
588
589         subscribe_cb = malloc(sizeof(subscribe_cb_t));
590         if (!subscribe_cb) {
591                 ret = MM_ERROR_SOUND_INTERNAL;
592                 goto error;
593         }
594         memset(subscribe_cb, 0, sizeof(subscribe_cb_t));
595
596         g_dbus_conn_mmsound = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
597         if (!g_dbus_conn_mmsound && err) {
598                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
599                 g_error_free (err);
600                 ret = MM_ERROR_SOUND_INTERNAL;
601                 goto error;
602         }
603
604         subscribe_cb->signal_type = signal;
605         subscribe_cb->callback = callback;
606         subscribe_cb->user_data = user_data;
607
608         *subscribe_id = g_dbus_connection_signal_subscribe(g_dbus_conn_mmsound,
609                         NULL, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal], MM_SOUND_DBUS_OBJECT_PATH, NULL, 0,
610                         signal_callback, subscribe_cb, NULL);
611         if (*subscribe_id == 0) {
612                 debug_error ("g_dbus_connection_signal_subscribe() error (%d)", *subscribe_id);
613                 ret = MM_ERROR_SOUND_INTERNAL;
614                 goto sig_error;
615         }
616
617         subscribe_cb->id = *subscribe_id;
618
619         g_subscribe_cb_list = g_list_append(g_subscribe_cb_list, subscribe_cb);
620         if (g_subscribe_cb_list) {
621                 debug_log("new subscribe_cb(0x%x)[user_callback(0x%x), subscribe_id(%u)] is added\n", subscribe_cb, subscribe_cb->callback, subscribe_cb->id);
622         } else {
623                 debug_error("g_list_append failed\n");
624                 ret = MM_ERROR_SOUND_INTERNAL;
625                 goto error;
626         }
627
628         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
629
630         debug_fleave();
631
632         return ret;
633
634 sig_error:
635         g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, *subscribe_id);
636         g_object_unref(g_dbus_conn_mmsound);
637
638 error:
639         if (subscribe_cb)
640                 free (subscribe_cb);
641
642         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
643
644         return ret;
645 }
646
647 EXPORT_API
648 int mm_sound_subscribe_signal_for_daemon(mm_sound_signal_name_t signal, int client_pid, unsigned int *subscribe_id, mm_sound_signal_callback callback, void *user_data)
649 {
650         int ret = MM_ERROR_NONE;
651         GError *err = NULL;
652
653         subscribe_cb_t *subscribe_cb = NULL;
654
655         debug_fenter();
656
657         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
658
659         if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX || !client_pid || !subscribe_id) {
660                 debug_error ("invalid argument, signal(%d), client_pid(%d), subscribe_id(0x%p)", signal, client_pid, subscribe_id);
661                 ret = MM_ERROR_INVALID_ARGUMENT;
662                 goto error;
663         }
664
665         subscribe_cb = malloc(sizeof(subscribe_cb_t));
666         if (!subscribe_cb) {
667                 ret = MM_ERROR_SOUND_INTERNAL;
668                 goto error;
669         }
670         memset(subscribe_cb, 0, sizeof(subscribe_cb_t));
671
672         g_dbus_conn_mmsound = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
673         if (!g_dbus_conn_mmsound && err) {
674                 debug_error ("g_bus_get_sync() error (%s) ", err->message);
675                 g_error_free (err);
676                 ret = MM_ERROR_SOUND_INTERNAL;
677                 goto error;
678         }
679
680         subscribe_cb->signal_type = signal;
681         subscribe_cb->callback = callback;
682         subscribe_cb->user_data = user_data;
683         subscribe_cb->client_pid = client_pid;
684
685         *subscribe_id = g_dbus_connection_signal_subscribe(g_dbus_conn_mmsound,
686                         NULL, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal], MM_SOUND_DBUS_OBJECT_PATH, NULL, 0,
687                         signal_callback, subscribe_cb, NULL);
688         if (*subscribe_id == 0) {
689                 debug_error ("g_dbus_connection_signal_subscribe() error (%d)", *subscribe_id);
690                 ret = MM_ERROR_SOUND_INTERNAL;
691                 goto sig_error;
692         }
693
694         subscribe_cb->id = *subscribe_id;
695
696         g_subscribe_cb_list = g_list_append(g_subscribe_cb_list, subscribe_cb);
697         if (g_subscribe_cb_list) {
698                 debug_log("new subscribe_cb(0x%x)[user_callback(0x%x), subscribe_id(%u)] is added\n", subscribe_cb, subscribe_cb->callback, subscribe_cb->id);
699         } else {
700                 debug_error("g_list_append failed\n");
701                 ret = MM_ERROR_SOUND_INTERNAL;
702                 goto error;
703         }
704
705         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
706
707         debug_fleave();
708
709         return ret;
710
711 sig_error:
712         g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, *subscribe_id);
713         g_object_unref(g_dbus_conn_mmsound);
714
715 error:
716         if (subscribe_cb)
717                 free (subscribe_cb);
718
719         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
720
721         return ret;
722 }
723
724 EXPORT_API
725 void mm_sound_unsubscribe_signal(unsigned int subscribe_id)
726 {
727         GList *list = NULL;
728         subscribe_cb_t *subscribe_cb = NULL;
729
730         debug_fenter();
731
732         MMSOUND_ENTER_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
733
734         if (g_dbus_conn_mmsound && subscribe_id) {
735                 g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, subscribe_id);
736                 g_object_unref(g_dbus_conn_mmsound);
737                 for (list = g_subscribe_cb_list; list != NULL; list = list->next) {
738                         subscribe_cb = (subscribe_cb_t *)list->data;
739                         if (subscribe_cb && (subscribe_cb->id == subscribe_id)) {
740                                 g_subscribe_cb_list = g_list_remove(g_subscribe_cb_list, subscribe_cb);
741                                 debug_log("subscribe_cb(0x%x) is removed\n", subscribe_cb);
742                                 free (subscribe_cb);
743                         }
744                 }
745         }
746
747         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
748
749         debug_fleave();
750 }
751
752 EXPORT_API
753 int mm_sound_send_signal(mm_sound_signal_name_t signal, int value)
754 {
755         int ret = MM_ERROR_NONE;
756         GError *err = NULL;
757         GDBusConnection *conn = NULL;
758         gboolean dbus_ret = TRUE;
759
760         debug_fenter();
761
762         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
763
764         if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX) {
765                 debug_error ("invalid argument, signal(%d)", signal);
766                 ret = MM_ERROR_INVALID_ARGUMENT;
767                 goto error;
768         }
769
770         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
771         if (!conn && err) {
772                 debug_error ("g_bus_get_sync() error (%s)", err->message);
773                 ret = MM_ERROR_SOUND_INTERNAL;
774                 goto error;
775         }
776
777         g_dbus_signal_values[signal] = value;
778         if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
779                 /* Trigger the signal callback when it comes from the same process.
780                 * |<-- pid (16bits) -->|<-- value (16bits) -->|,
781                 * FYI, #define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000). */
782                 value |= ((int)getpid() << 16);
783                 if ((_mm_session_util_write_information((int)getpid(), MM_SESSION_TYPE_REPLACED_BY_STREAM, 0)))
784                         debug_error ("failed to _mm_session_util_write_information for MM_SESSION_TYPE_REPLACED_BY_STREAM");
785         }
786         dbus_ret = g_dbus_connection_emit_signal (conn,
787                                 NULL, MM_SOUND_DBUS_OBJECT_PATH, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal],
788                                 g_variant_new ("(i)", value),
789                                 &err);
790         if (!dbus_ret && err) {
791                 debug_error ("g_dbus_connection_emit_signal() error (%s)", err->message);
792                 ret = MM_ERROR_SOUND_INTERNAL;
793                 goto error;
794         }
795
796         dbus_ret = g_dbus_connection_flush_sync(conn, NULL, &err);
797         if (!dbus_ret && err) {
798                 debug_error ("g_dbus_connection_flush_sync() error (%s)", err->message);
799                 ret = MM_ERROR_SOUND_INTERNAL;
800                 goto error;
801         }
802
803         g_object_unref(conn);
804         debug_msg ("sending signal[%s], value[%d] success", dbus_signal_name_str[signal], value);
805
806         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
807
808         debug_fleave();
809
810         return ret;
811
812 error:
813         if (err)
814                 g_error_free (err);
815         if (conn)
816                 g_object_unref(conn);
817
818         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
819
820         return ret;
821 }
822
823 EXPORT_API
824 int mm_sound_get_signal_value(mm_sound_signal_name_t signal, int *value)
825 {
826         int ret = MM_ERROR_NONE;
827
828         debug_fenter();
829
830         MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
831
832         *value = g_dbus_signal_values[signal];
833
834         MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
835
836         debug_fleave();
837
838         return ret;
839 }
840
841 __attribute__ ((constructor))
842 static void _mm_sound_initialize(void)
843 {
844         mm_sound_client_initialize();
845         /* Will be Fixed */
846 }
847
848 __attribute__ ((destructor))
849 static void _mm_sound_finalize(void)
850 {
851         mm_sound_client_finalize();
852 }
853
854