4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <sys/types.h>
36 #include <mm_message.h>
38 #include "include/mm_sound_private.h"
39 #include "include/mm_sound.h"
40 #include "include/mm_sound_utils.h"
41 #include "include/mm_sound_client.h"
42 #include "include/mm_sound_pa_client.h"
43 #include "include/mm_ipc.h"
44 #include "include/mm_sound_common.h"
47 #define VOLUME_MAX_MULTIMEDIA 16
48 #define VOLUME_MAX_BASIC 8
49 #define VOLUME_MAX_SINGLE 1
52 #define MASTER_VOLUME_MAX 100
53 #define MASTER_VOLUME_MIN 0
57 volume_callback_fn func;
62 volume_cb_param g_volume_param[VOLUME_TYPE_MAX];
64 static pthread_mutex_t g_volume_mutex = PTHREAD_MUTEX_INITIALIZER;
68 static GList *g_subscribe_cb_list = NULL;
69 static pthread_mutex_t g_subscribe_cb_list_mutex = PTHREAD_MUTEX_INITIALIZER;
71 #define MM_SOUND_DBUS_BUS_NAME_PREPIX "org.tizen.MMSound"
72 #define MM_SOUND_DBUS_OBJECT_PATH "/org/tizen/MMSound"
73 #define MM_SOUND_DBUS_INTERFACE "org.tizen.mmsound"
75 GDBusConnection *g_dbus_conn_mmsound;
77 int g_dbus_signal_values[MM_SOUND_SIGNAL_MAX] = {0,};
79 const char* dbus_signal_name_str[] = {
80 "ReleaseInternalFocus",
83 typedef struct _subscribe_cb {
84 mm_sound_signal_name_t signal_type;
85 mm_sound_signal_callback callback;
90 static char* _get_volume_str (volume_type_t type)
92 static const char *volume_type_str[VOLUME_TYPE_MAX] =
93 { "SYSTEM", "NOTIFICATION", "ALARM", "RINGTONE", "MEDIA", "CALL", "VOIP", "VOICE", "FIXED"};
95 return (type >= VOLUME_TYPE_SYSTEM && type < VOLUME_TYPE_MAX)? volume_type_str[type] : "Unknown";
98 static int _validate_volume(volume_type_t type, int value)
105 case VOLUME_TYPE_CALL:
106 case VOLUME_TYPE_VOIP:
107 if (value >= VOLUME_MAX_BASIC) {
111 case VOLUME_TYPE_SYSTEM:
112 case VOLUME_TYPE_MEDIA:
113 case VOLUME_TYPE_ALARM:
114 case VOLUME_TYPE_NOTIFICATION:
115 case VOLUME_TYPE_RINGTONE:
116 case VOLUME_TYPE_VOICE:
117 if (value >= VOLUME_MAX_MULTIMEDIA) {
128 static void _volume_changed_cb(keynode_t* node, void* data)
130 volume_cb_param* param = (volume_cb_param*) data;
132 debug_msg("%s changed callback called\n",vconf_keynode_get_name(node));
134 MMSOUND_ENTER_CRITICAL_SECTION( &g_volume_mutex )
136 if(param && (param->func != NULL)) {
137 debug_log("function 0x%x\n", param->func);
138 ((volume_callback_fn)param->func)(param->data);
141 MMSOUND_LEAVE_CRITICAL_SECTION( &g_volume_mutex )
145 int mm_sound_volume_add_callback(volume_type_t type, volume_callback_fn func, void* user_data)
147 debug_msg("type = (%d)%15s, func = %p, user_data = %p", type, _get_volume_str(type), func, user_data);
149 /* Check input param */
150 if (type < 0 || type >= VOLUME_TYPE_MAX) {
151 debug_error("invalid argument\n");
152 return MM_ERROR_INVALID_ARGUMENT;
155 debug_warning("callback function is null\n");
156 return MM_ERROR_INVALID_ARGUMENT;
159 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN( &g_volume_mutex, MM_ERROR_SOUND_INTERNAL );
161 g_volume_param[type].func = func;
162 g_volume_param[type].data = user_data;
163 g_volume_param[type].type = type;
165 MMSOUND_LEAVE_CRITICAL_SECTION( &g_volume_mutex );
167 return mm_sound_util_volume_add_callback(type, _volume_changed_cb, (void*)&g_volume_param[type]);
171 int mm_sound_volume_remove_callback(volume_type_t type)
173 debug_msg("type = (%d)%s", type, _get_volume_str(type));
175 if(type < 0 || type >=VOLUME_TYPE_MAX) {
176 debug_error("invalid argument\n");
177 return MM_ERROR_INVALID_ARGUMENT;
180 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN( &g_volume_mutex, MM_ERROR_SOUND_INTERNAL );
182 g_volume_param[type].func = NULL;
183 g_volume_param[type].data = NULL;
184 g_volume_param[type].type = type;
186 MMSOUND_LEAVE_CRITICAL_SECTION( &g_volume_mutex );
188 return mm_sound_util_volume_remove_callback(type, _volume_changed_cb);
192 int mm_sound_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data)
194 int ret = MM_ERROR_NONE;
197 debug_error("argument is not valid\n");
198 return MM_ERROR_INVALID_ARGUMENT;
201 ret = mm_sound_client_add_volume_changed_callback(func, user_data);
203 debug_error("Can not add volume changed callback, ret = %x\n", ret);
210 int mm_sound_remove_volume_changed_callback(void)
212 int ret = MM_ERROR_NONE;
214 ret = mm_sound_client_remove_volume_changed_callback();
216 debug_error("Can not remove volume changed callback, ret = %x\n", ret);
223 int mm_sound_volume_get_step(volume_type_t type, int *step)
225 return MM_ERROR_SOUND_NOT_SUPPORTED_OPERATION;
229 int mm_sound_volume_set_value(volume_type_t volume_type, const unsigned int volume_level)
231 int ret = MM_ERROR_NONE;
233 debug_msg("type = (%d)%s, value = %d", volume_type, _get_volume_str(volume_type), volume_level);
235 /* Check input param */
236 if (0 > _validate_volume(volume_type, (int)volume_level)) {
237 debug_error("invalid volume type %d, value %u\n", volume_type, volume_level);
238 return MM_ERROR_INVALID_ARGUMENT;
241 ret = mm_sound_util_volume_set_value_by_type(volume_type, volume_level);
242 if (ret == MM_ERROR_NONE) {
243 /* update shared memory value */
244 if(MM_ERROR_NONE != mm_sound_client_set_volume_by_type(volume_type, volume_level)) {
245 debug_error("Can not set volume to shared memory 0x%x\n", ret);
253 int mm_sound_volume_get_value(volume_type_t type, unsigned int *value)
255 int ret = MM_ERROR_NONE;
257 /* Check input param */
259 debug_error("invalid argument\n");
260 return MM_ERROR_INVALID_ARGUMENT;
262 if (type < 0 || type >= VOLUME_TYPE_MAX) {
263 debug_error("invalid volume type value %d\n", type);
264 return MM_ERROR_INVALID_ARGUMENT;
267 ret = mm_sound_util_volume_get_value_by_type(type, value);
269 debug_msg("returned %s = %d", _get_volume_str(type), *value);
274 int mm_sound_volume_primary_type_set(volume_type_t type)
277 int ret = MM_ERROR_NONE;
279 /* Check input param */
280 if(type < VOLUME_TYPE_UNKNOWN || type >= VOLUME_TYPE_MAX) {
281 debug_error("invalid argument\n");
282 return MM_ERROR_INVALID_ARGUMENT;
285 if (vconf_set_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, type)) {
286 debug_error("could not set vconf for RIMARY_VOLUME_TYPE\n");
287 ret = MM_ERROR_SOUND_INTERNAL;
289 debug_msg("set primary volume type forcibly %d(%s)", type, _get_volume_str(type));
296 int mm_sound_volume_primary_type_get(volume_type_t *type)
298 int ret = MM_ERROR_NONE;
299 int voltype = VOLUME_TYPE_RINGTONE;
301 /* Check input param */
303 debug_error("invalid argument\n");
304 return MM_ERROR_INVALID_ARGUMENT;
307 /* check force set */
308 if (vconf_get_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, &voltype)) {
309 debug_error("could not get vconf for PRIMARY_VOLUME_TYPE\n");
310 ret = MM_ERROR_SOUND_INTERNAL;
312 debug_msg("get primary volume type %d(%s)", voltype, _get_volume_str(voltype));
319 /* it will be removed */
321 int mm_sound_volume_primary_type_clear(void)
324 int ret = MM_ERROR_NONE;
326 if (vconf_set_int(VCONFKEY_SOUND_PRIMARY_VOLUME_TYPE, -1)) {
327 debug_error("could not reset vconf for PRIMARY_VOLUME_TYPE\n");
328 ret = MM_ERROR_SOUND_INTERNAL;
330 debug_msg("clear primary volume type forcibly %d(%s)", -1, "none");
336 ///////////////////////////////////
337 //// MMSOUND PLAY APIs
338 ///////////////////////////////////
339 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)
341 param->filename = filename;
342 param->volume = 0; //volume value dose not effect anymore
343 param->callback = callback;
346 param->volume_config = volume_config;
347 param->priority = priority;
348 param->handle_route = handle_route;
352 int mm_sound_play_loud_solo_sound_no_restore(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
354 MMSoundPlayParam param = { 0, };
356 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, HANDLE_PRIORITY_SOLO, MM_SOUND_HANDLE_ROUTE_SPEAKER_NO_RESTORE);
357 return mm_sound_play_sound_ex(¶m, handle);
361 int mm_sound_play_loud_solo_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
363 MMSoundPlayParam param = { 0, };
365 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, HANDLE_PRIORITY_SOLO, MM_SOUND_HANDLE_ROUTE_SPEAKER);
366 return mm_sound_play_sound_ex(¶m, handle);
370 int mm_sound_play_solo_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
372 MMSoundPlayParam param = { 0, };
374 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, HANDLE_PRIORITY_SOLO, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
375 return mm_sound_play_sound_ex(¶m, handle);
379 int mm_sound_play_sound_without_session(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
381 MMSoundPlayParam param = { 0, };
383 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, HANDLE_PRIORITY_SOLO, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
384 param.skip_session = true;
385 return mm_sound_play_sound_ex(¶m, handle);
389 int mm_sound_play_sound(const char *filename, int volume_config, mm_sound_stop_callback_func callback, void *data, int *handle)
391 MMSoundPlayParam param = { 0, };
393 _mm_sound_fill_play_param(¶m, filename, volume_config, callback, data, HANDLE_PRIORITY_NORMAL, MM_SOUND_HANDLE_ROUTE_USING_CURRENT);
394 return mm_sound_play_sound_ex(¶m, handle);
398 int mm_sound_play_sound_ex(MMSoundPlayParam *param, int *handle)
403 /* Check input param */
405 debug_error("param is null\n");
406 return MM_ERROR_INVALID_ARGUMENT;
409 volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(param->volume_config);
411 if (param->filename == NULL) {
412 debug_error("filename is NULL\n");
413 return MM_ERROR_SOUND_FILE_NOT_FOUND;
415 if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
416 debug_error("Volume type is invalid %d\n", volume_type);
417 return MM_ERROR_INVALID_ARGUMENT;
420 debug_warning ("play sound : priority=[%d], handle_route=[%d]\n", param->priority, param->handle_route);
423 err = mm_sound_client_play_sound(param, 0, &lhandle);
425 debug_error("Failed to play sound\n");
429 /* Set handle to return */
433 debug_critical("The sound hadle cannot be get [%d]\n", lhandle);
436 debug_warning ("success : handle=[%p]\n", handle);
438 return MM_ERROR_NONE;
442 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)
444 MMSoundPlayParam param = { 0, };
447 param.filename = filename;
448 param.volume = 0; //volume value dose not effect anymore
449 param.callback = callback;
452 param.priority = HANDLE_PRIORITY_NORMAL;
453 param.handle_route = MM_SOUND_HANDLE_ROUTE_USING_CURRENT;
455 err = mm_sound_client_play_sound_with_stream_info(¶m, handle, stream_type, stream_id);
457 debug_error("Failed to play sound\n");
461 debug_warning ("success : handle=[%p]\n", handle);
463 return MM_ERROR_NONE;
469 int mm_sound_stop_sound(int handle)
473 debug_warning ("enter : handle=[%p]\n", handle);
475 err = mm_sound_client_stop_sound(handle);
477 debug_error("Fail to stop sound\n");
480 debug_warning ("success : handle=[%p]\n", handle);
482 return MM_ERROR_NONE;
485 ///////////////////////////////////
486 //// MMSOUND TONE APIs
487 ///////////////////////////////////
489 int mm_sound_play_tone_ex (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle, bool enable_session)
492 int err = MM_ERROR_NONE;
493 int volume_type = MM_SOUND_VOLUME_CONFIG_TYPE(volume_config);
497 /* Check input param */
499 debug_error("number is invalid %d\n", duration);
500 return MM_ERROR_INVALID_ARGUMENT;
502 if (num < MM_SOUND_TONE_DTMF_0 || num >= MM_SOUND_TONE_NUM) {
503 debug_error("TONE Value is invalid %d\n", num);
504 return MM_ERROR_INVALID_ARGUMENT;
506 if (volume_type < 0 || volume_type >= VOLUME_TYPE_MAX) {
507 debug_error("Volume type is invalid %d\n", volume_type);
508 return MM_ERROR_INVALID_ARGUMENT;
510 if (volume < 0.0 || volume > 1.0) {
511 debug_error("Volume Value is invalid %d\n", volume);
512 return MM_ERROR_INVALID_ARGUMENT;
516 debug_msg("Call MMSoundClientPlayTone\n");
517 err = mm_sound_client_play_tone(num, volume_config, volume, duration, &lhandle, enable_session);
519 debug_error("Failed to play sound\n");
523 /* Set handle to return */
527 debug_critical("The sound handle cannot be get [%d]\n", lhandle);
530 return MM_ERROR_NONE;
534 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)
537 int err = MM_ERROR_NONE;
539 err = mm_sound_client_play_tone_with_stream_info(tone, stream_type, stream_id, volume, duration, handle);
541 debug_error("Failed to play sound\n");
551 int mm_sound_play_tone (MMSoundTone_t num, int volume_config, const double volume, const int duration, int *handle)
553 return mm_sound_play_tone_ex (num, volume_config, volume, duration, handle, true);
556 ///////////////////////////////////
557 //// MMSOUND ROUTING APIs
558 ///////////////////////////////////
561 int mm_sound_route_get_a2dp_status (bool *connected, char **bt_name)
563 int ret = MM_ERROR_NONE;
565 if (connected == NULL || bt_name == NULL) {
566 debug_error ("argument is not valid\n");
567 return MM_ERROR_INVALID_ARGUMENT;
570 ret = mm_sound_client_is_bt_a2dp_on (connected, bt_name);
571 debug_msg ("connected=[%d] bt_name[%s]\n", *connected, *bt_name);
573 debug_error("MMSoundClientIsBtA2dpOn() Failed\n");
581 int mm_sound_is_route_available(mm_sound_route route, bool *is_available)
583 int ret = MM_ERROR_NONE;
585 debug_warning ("enter : route=[%x], is_available=[%p]\n", route, is_available);
587 if (!mm_sound_util_is_route_valid(route)) {
588 debug_error("route is invalid %d\n", route);
589 return MM_ERROR_INVALID_ARGUMENT;
592 debug_error("is_available is invalid\n");
593 return MM_ERROR_INVALID_ARGUMENT;
596 ret = mm_sound_client_is_route_available(route, is_available);
598 debug_error("Can not check given route is available, ret = %x\n", ret);
600 debug_warning ("success : route=[%x], available=[%d]\n", route, *is_available);
607 int mm_sound_foreach_available_route_cb(mm_sound_available_route_cb available_route_cb, void *user_data)
609 int ret = MM_ERROR_NONE;
611 if (!available_route_cb) {
612 debug_error("available_route_cb is invalid\n");
613 return MM_ERROR_INVALID_ARGUMENT;
616 ret = mm_sound_client_foreach_available_route_cb(available_route_cb, user_data);
618 debug_error("Can not set foreach available route callback, ret = %x\n", ret);
625 int mm_sound_set_active_route(mm_sound_route route)
627 int ret = MM_ERROR_NONE;
629 debug_warning ("enter : route=[%x]\n", route);
630 if (!mm_sound_util_is_route_valid(route)) {
631 debug_error("route is invalid %d\n", route);
632 return MM_ERROR_INVALID_ARGUMENT;
635 ret = mm_sound_client_set_active_route(route, true);
637 debug_error("Can not set active route, ret = %x\n", ret);
639 debug_warning ("success : route=[%x]\n", route);
646 int mm_sound_set_active_route_auto(void)
648 int ret = MM_ERROR_NONE;
650 ret = mm_sound_client_set_active_route_auto();
652 debug_error("fail to set active route auto, ret = %x\n", ret);
654 debug_msg ("success !!\n");
661 int mm_sound_set_active_route_without_broadcast(mm_sound_route route)
663 int ret = MM_ERROR_NONE;
665 debug_warning ("enter : route=[%x]\n", route);
666 if (!mm_sound_util_is_route_valid(route)) {
667 debug_error("route is invalid %d\n", route);
668 return MM_ERROR_INVALID_ARGUMENT;
671 ret = mm_sound_client_set_active_route(route, false);
673 debug_error("Can not set active route, ret = %x\n", ret);
675 debug_warning ("success : route=[%x]\n", route);
682 int mm_sound_get_active_device(mm_sound_device_in *device_in, mm_sound_device_out *device_out)
684 int ret = MM_ERROR_NONE;
686 if (device_in == NULL || device_out == NULL) {
687 debug_error("argument is not valid\n");
688 return MM_ERROR_INVALID_ARGUMENT;
691 ret = mm_sound_client_get_active_device(device_in, device_out);
693 debug_error("Can not add active device callback, ret = %x\n", ret);
695 debug_msg ("success : in=[%x], out=[%x]\n", *device_in, *device_out);
702 int mm_sound_get_audio_path(mm_sound_device_in *device_in, mm_sound_device_out *device_out)
704 int ret = MM_ERROR_NONE;
706 if (device_in == NULL || device_out == NULL) {
707 debug_error("argument is not valid\n");
708 return MM_ERROR_INVALID_ARGUMENT;
711 ret = mm_sound_client_get_audio_path(device_in, device_out);
713 debug_error("Can not add active device callback, ret = %x\n", ret);
715 debug_msg ("success : in=[%x], out=[%x]\n", *device_in, *device_out);
722 int mm_sound_add_active_device_changed_callback(const char *name, mm_sound_active_device_changed_cb func, void *user_data)
724 int ret = MM_ERROR_NONE;
726 debug_warning ("enter %s\n", name);
728 debug_error("argument is not valid\n");
729 return MM_ERROR_INVALID_ARGUMENT;
732 ret = mm_sound_client_add_active_device_changed_callback(name, func, user_data);
734 debug_error("Can not add active device changed callback, ret = %x\n", ret);
741 int mm_sound_remove_active_device_changed_callback(const char *name)
743 int ret = MM_ERROR_NONE;
745 debug_warning ("enter name %s \n", name);
746 ret = mm_sound_client_remove_active_device_changed_callback(name);
748 debug_error("Can not remove active device changed callback, ret = %x\n", ret);
755 int mm_sound_add_available_route_changed_callback(mm_sound_available_route_changed_cb func, void *user_data)
757 int ret = MM_ERROR_NONE;
760 debug_error("argument is not valid\n");
761 return MM_ERROR_INVALID_ARGUMENT;
764 ret = mm_sound_client_add_available_route_changed_callback(func, user_data);
766 debug_error("Can not add available route changed callback, ret = %x\n", ret);
773 int mm_sound_remove_available_route_changed_callback(void)
775 int ret = MM_ERROR_NONE;
777 ret = mm_sound_client_remove_available_route_changed_callback();
779 debug_error("Can not remove available route changed callback, ret = %x\n", ret);
786 int mm_sound_set_sound_path_for_active_device(mm_sound_device_out device_out, mm_sound_device_in device_in)
788 int ret = MM_ERROR_NONE;
790 ret = mm_sound_client_set_sound_path_for_active_device(device_out, device_in);
792 debug_error("Can not mm sound set sound path for active device, ret = %x\n", ret);
800 int mm_sound_test(int a, int b, int* getv)
802 int ret = MM_ERROR_NONE;
804 debug_log("mm_sound_test enter");
806 debug_error("argu null");
807 return MM_ERROR_INVALID_ARGUMENT;
809 ret = mm_sound_client_test(a, b, getv);
811 debug_error("Can not mm sound test, ret = %x\n", ret);
813 debug_log("mm_sound_test leave");
819 int mm_sound_add_test_callback(mm_sound_test_cb func, void *user_data)
821 int ret = MM_ERROR_NONE;
823 debug_log("mm_sound_add_test_callback enter");
825 debug_error("argument is not valid\n");
826 return MM_ERROR_INVALID_ARGUMENT;
829 ret = mm_sound_client_add_test_callback(func, user_data);
831 debug_error("Can not add test callback, ret = %x\n", ret);
833 debug_log("mm_sound_add_test_callback leave");
839 int mm_sound_remove_test_callback(void)
841 int ret = MM_ERROR_NONE;
843 debug_log("mm_sound_remove_test_callback enter");
844 ret = mm_sound_client_remove_test_callback();
846 debug_error("Can not remove test callback, ret = %x\n", ret);
848 debug_log("mm_sound_remove_test_callback leave");
853 static void signal_callback(GDBusConnection *conn,
854 const gchar *sender_name,
855 const gchar *object_path,
856 const gchar *interface_name,
857 const gchar *signal_name,
858 GVariant *parameters,
862 const GVariantType* value_type;
864 debug_msg ("sender : %s, object : %s, interface : %s, signal : %s",
865 sender_name, object_path, interface_name, signal_name);
866 if(g_variant_is_of_type(parameters, G_VARIANT_TYPE("(i)"))) {
867 g_variant_get(parameters, "(i)",&value);
868 debug_msg(" - value : %d\n", value);
869 _dbus_signal_callback (signal_name, value, user_data);
871 value_type = g_variant_get_type(parameters);
872 debug_warning("signal type is %s", value_type);
876 int _convert_signal_name_str_to_enum (const char *name_str, mm_sound_signal_name_t *name_enum) {
877 int ret = MM_ERROR_NONE;
879 if (!name_str || !name_enum)
880 return MM_ERROR_INVALID_ARGUMENT;
882 if (!strncmp(name_str, "ReleaseInternalFocus", strlen("ReleaseInternalFocus"))) {
883 *name_enum = MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS;
885 ret = MM_ERROR_INVALID_ARGUMENT;
886 LOGE("not supported signal name(%s), err(0x%08x)", name_str, ret);
891 void _dbus_signal_callback (const char *signal_name, int value, void *user_data)
893 int ret = MM_ERROR_NONE;
894 mm_sound_signal_name_t signal;
895 subscribe_cb_t *subscribe_cb = (subscribe_cb_t*)user_data;
902 ret = _convert_signal_name_str_to_enum(signal_name, &signal);
906 debug_msg ("signal_name[%s], value[%d], user_data[0x%x]\n", signal_name, value, user_data);
908 if (subscribe_cb->signal_type == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
909 /* trigger the signal callback when it comes from the same process */
910 if (getpid() == ((value & 0xFFFF0000) >> 16)) {
911 subscribe_cb->callback(signal, (value & 0x0000FFFF), subscribe_cb->user_data);
914 subscribe_cb->callback(signal, value, subscribe_cb->user_data);
923 int mm_sound_subscribe_signal(mm_sound_signal_name_t signal, unsigned int *subscribe_id, mm_sound_signal_callback callback, void *user_data)
925 int ret = MM_ERROR_NONE;
928 subscribe_cb_t *subscribe_cb = NULL;
932 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
934 if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX || !subscribe_id) {
935 debug_error ("invalid argument, signal(%d), subscribe_id(0x%p)", signal, subscribe_id);
936 ret = MM_ERROR_INVALID_ARGUMENT;
940 subscribe_cb = malloc(sizeof(subscribe_cb_t));
942 ret = MM_ERROR_SOUND_INTERNAL;
945 memset(subscribe_cb, 0, sizeof(subscribe_cb_t));
949 g_dbus_conn_mmsound = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
950 if (!g_dbus_conn_mmsound && err) {
951 debug_error ("g_bus_get_sync() error (%s) ", err->message);
953 ret = MM_ERROR_SOUND_INTERNAL;
957 subscribe_cb->signal_type = signal;
958 subscribe_cb->callback = callback;
959 subscribe_cb->user_data = user_data;
961 *subscribe_id = g_dbus_connection_signal_subscribe(g_dbus_conn_mmsound,
962 NULL, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal], MM_SOUND_DBUS_OBJECT_PATH, NULL, 0,
963 signal_callback, subscribe_cb, NULL);
964 if (*subscribe_id == 0) {
965 debug_error ("g_dbus_connection_signal_subscribe() error (%d)", *subscribe_id);
966 ret = MM_ERROR_SOUND_INTERNAL;
970 subscribe_cb->id = *subscribe_id;
972 g_subscribe_cb_list = g_list_append(g_subscribe_cb_list, subscribe_cb);
973 if (g_subscribe_cb_list) {
974 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);
976 debug_error("g_list_append failed\n");
977 ret = MM_ERROR_SOUND_INTERNAL;
981 MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
988 g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, *subscribe_id);
989 g_object_unref(g_dbus_conn_mmsound);
995 MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
1001 void mm_sound_unsubscribe_signal(unsigned int subscribe_id)
1004 subscribe_cb_t *subscribe_cb = NULL;
1008 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
1010 if (g_dbus_conn_mmsound && subscribe_id) {
1011 g_dbus_connection_signal_unsubscribe(g_dbus_conn_mmsound, subscribe_id);
1012 g_object_unref(g_dbus_conn_mmsound);
1013 for (list = g_subscribe_cb_list; list != NULL; list = list->next) {
1014 subscribe_cb = (subscribe_cb_t *)list->data;
1015 if (subscribe_cb && (subscribe_cb->id == subscribe_id)) {
1016 g_subscribe_cb_list = g_list_remove(g_subscribe_cb_list, subscribe_cb);
1017 debug_log("subscribe_cb(0x%x) is removed\n", subscribe_cb);
1018 free (subscribe_cb);
1023 MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
1029 int mm_sound_send_signal(mm_sound_signal_name_t signal, int value)
1031 int ret = MM_ERROR_NONE;
1033 GDBusConnection *conn = NULL;
1034 gboolean dbus_ret = TRUE;
1038 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
1040 if (signal < 0 || signal >= MM_SOUND_SIGNAL_MAX) {
1041 debug_error ("invalid argument, signal(%d)", signal);
1042 ret = MM_ERROR_INVALID_ARGUMENT;
1046 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1048 debug_error ("g_bus_get_sync() error (%s)", err->message);
1049 ret = MM_ERROR_SOUND_INTERNAL;
1053 g_dbus_signal_values[signal] = value;
1054 if (signal == MM_SOUND_SIGNAL_RELEASE_INTERNAL_FOCUS) {
1055 /* trigger the signal callback when it comes from the same process */
1056 value |= ((int)getpid() << 16);
1058 dbus_ret = g_dbus_connection_emit_signal (conn,
1059 NULL, MM_SOUND_DBUS_OBJECT_PATH, MM_SOUND_DBUS_INTERFACE, dbus_signal_name_str[signal],
1060 g_variant_new ("(i)", value),
1062 if (!dbus_ret && err) {
1063 debug_error ("g_dbus_connection_emit_signal() error (%s)", err->message);
1064 ret = MM_ERROR_SOUND_INTERNAL;
1068 dbus_ret = g_dbus_connection_flush_sync(conn, NULL, &err);
1069 if (!dbus_ret && err) {
1070 debug_error ("g_dbus_connection_flush_sync() error (%s)", err->message);
1071 ret = MM_ERROR_SOUND_INTERNAL;
1075 g_object_unref(conn);
1076 debug_msg ("sending signal[%s], value[%d] success", dbus_signal_name_str[signal], value);
1078 MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
1088 g_object_unref(conn);
1090 MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
1096 int mm_sound_get_signal_value(mm_sound_signal_name_t signal, int *value)
1098 int ret = MM_ERROR_NONE;
1102 MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_subscribe_cb_list_mutex, MM_ERROR_SOUND_INTERNAL);
1104 *value = g_dbus_signal_values[signal];
1106 MMSOUND_LEAVE_CRITICAL_SECTION(&g_subscribe_cb_list_mutex);
1113 __attribute__ ((constructor))
1114 static void _mm_sound_initialize(void)
1116 mm_sound_client_initialize();
1120 __attribute__ ((destructor))
1121 static void _mm_sound_finalize(void)
1123 mm_sound_client_finalize();