Add new function to get my focus node and apply it
[platform/core/multimedia/libmm-sound.git] / focus_server / mm_sound_mgr_focus.c
index 1aaef9d..9179833 100644 (file)
@@ -62,7 +62,7 @@ typedef struct {
        x_node->taken_##x_postfix[i].handle_id = x_hid; \
 } while (0)
 
-#define CONTINUE_IF_LIST_DATA_IS_NULL(x_node, x_list) \
+#define SET_NODE_FROM_LIST_DATA(x_node, x_list) \
        if (!((x_node) = (focus_node_t *)(x_list)->data)) \
                continue; \
 
@@ -308,7 +308,7 @@ static int _mm_sound_mgr_focus_do_watch_callback(focus_type_e focus_type, focus_
        }
 
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                if (node == my_node)
                        continue;
                if (!node->is_for_watch || !(node->status & focus_type))
@@ -501,7 +501,7 @@ int _mm_sound_mgr_focus_do_callback(focus_command_e command, focus_node_t *victi
                                GList *list = NULL;
                                focus_node_t *node = NULL;
                                for (list = g_focus_node_list; list != NULL; list = list->next) {
-                                       CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+                                       SET_NODE_FROM_LIST_DATA(node, list);
                                        if (node->taken_by_id[i].pid == victim_node->pid &&
                                                node->taken_by_id[i].handle_id == victim_node->handle_id) {
                                                UPDATE_FOCUS_TAKEN_INFO(backup, node, node->taken_by_id[i].pid, node->taken_by_id[i].handle_id);
@@ -613,6 +613,30 @@ static void _mm_sound_mgr_focus_fill_info_from_msg(focus_node_t *node, const _mm
        return;
 }
 
+static int get_my_focus_node(GList *glist, const _mm_sound_mgr_focus_param_t *param, focus_node_t **my_node)
+{
+       GList *list = NULL;
+       focus_node_t *node = NULL;
+
+       if (!param || !my_node)
+               return MM_ERROR_INVALID_ARGUMENT;
+
+       for (list = glist; list != NULL; list = list->next) {
+               SET_NODE_FROM_LIST_DATA(node, list);
+               CONTINUE_IF_NOT_MY_FOCUS_NODE(node, param);
+               break;
+       }
+
+       if (node == NULL) {
+               debug_error("node is null");
+               return MM_ERROR_INVALID_ARGUMENT;
+       }
+
+       *my_node = node;
+
+       return MM_ERROR_NONE;
+}
+
 int mm_sound_mgr_focus_create_node(const _mm_sound_mgr_focus_param_t *param)
 {
        int ret = MM_ERROR_NONE;
@@ -625,7 +649,7 @@ int mm_sound_mgr_focus_create_node(const _mm_sound_mgr_focus_param_t *param)
        MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_focus_node_list_mutex, MM_ERROR_SOUND_INTERNAL);
 
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                CONTINUE_IF_NOT_MY_FOCUS_NODE(node, param);
 
                debug_error("the node of pid[%d]/handle_id[%d] is already created", param->pid, param->handle_id);
@@ -677,19 +701,8 @@ int mm_sound_mgr_focus_destroy_node(const _mm_sound_mgr_focus_param_t *param)
 
        MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_focus_node_list_mutex, MM_ERROR_SOUND_INTERNAL);
 
-       for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
-               CONTINUE_IF_NOT_MY_FOCUS_NODE(node, param);
-
-               debug_log("found the node of pid[%d]/handle_id[%d]", param->pid, param->handle_id);
-               my_node = node;
-               break;
-       }
-       if (my_node == NULL) {
-               debug_error("could not find any node of pid[%d]/handle_id[%d]", param->pid, param->handle_id);
-               ret = MM_ERROR_INVALID_ARGUMENT;
+       if ((ret = get_my_focus_node(g_focus_node_list, param, &my_node)))
                goto FINISH;
-       }
 
        if (need_to_trigger) {
                bool need_to_trigger_watch_cb = true;
@@ -707,7 +720,7 @@ int mm_sound_mgr_focus_destroy_node(const _mm_sound_mgr_focus_param_t *param)
                MMSOUND_STRNCPY(new_param->ext_info, my_node->ext_info[i], MM_SOUND_NAME_NUM);
 
                for (list = g_focus_node_list; list != NULL; list = list->next) {
-                       CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+                       SET_NODE_FROM_LIST_DATA(node, list);
                        if (my_node == node || node->is_for_watch)
                                continue;
                        for (i = 0; i < NUM_OF_STREAM_IO_TYPE; i++) {
@@ -764,7 +777,7 @@ int mm_sound_mgr_focus_set_reacquisition(const _mm_sound_mgr_focus_param_t *para
 
        /* Find node to set reacquisition */
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                CONTINUE_IF_NOT_MY_FOCUS_NODE(node, param);
 
                if (node->reacquisition == param->reacquisition) {
@@ -784,7 +797,7 @@ int mm_sound_mgr_focus_set_reacquisition(const _mm_sound_mgr_focus_param_t *para
 
        if (!param->reacquisition) {
                for (list = g_focus_node_list; list != NULL; list = list->next) {
-                       CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+                       SET_NODE_FROM_LIST_DATA(node, list);
                        for (i = 0; i < NUM_OF_STREAM_IO_TYPE; i++) {
                                if (node->taken_by_id[i].pid == param->pid) {
                                        /* victim node : append my node's taken info to my victim node */
@@ -801,7 +814,7 @@ int mm_sound_mgr_focus_set_reacquisition(const _mm_sound_mgr_focus_param_t *para
                }
        } else {
                for (list = g_focus_node_list; list != NULL; list = list->next) {
-                       CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+                       SET_NODE_FROM_LIST_DATA(node, list);
                        for (i = 0; i < NUM_OF_STREAM_IO_TYPE; i++) {
                                /* rollback and reset backup info. */
                                if (node->taken_by_id[i].pid && (node->taken_by_id[i].pid == my_node->taken_backup[i].pid)) {
@@ -838,7 +851,7 @@ int mm_sound_mgr_focus_get_stream_type_of_acquired_focus(focus_type_e focus_type
 
        /* Find node to set reacquisition */
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                if (!node->is_for_watch && (node->status & focus_type)) {
                        int index = focus_type - 1;
 
@@ -888,27 +901,18 @@ int mm_sound_mgr_focus_request_acquire(const _mm_sound_mgr_focus_param_t *param)
        if (!param->is_in_thread)
                MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_focus_node_list_mutex, MM_ERROR_SOUND_INTERNAL);
 
-       for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
-               CONTINUE_IF_NOT_MY_FOCUS_NODE(node, param);
-
-               my_node = node;
-               if ((my_node->status > FOCUS_STATUS_DEACTIVATED) && (my_node->status & param->request_type)) {
-                       debug_error("focus status is already activated");
-                       ret = MM_ERROR_SOUND_INVALID_STATE;
-                       goto FINISH;
-               }
-       }
+       if ((ret = get_my_focus_node(g_focus_node_list, param, &my_node)))
+               goto FINISH;
 
-       if (my_node == NULL) {
-               debug_error("node is null");
-               ret = MM_ERROR_INVALID_ARGUMENT;
+       if ((my_node->status > FOCUS_STATUS_DEACTIVATED) && (my_node->status & param->request_type)) {
+               debug_error("focus status is already activated");
+               ret = MM_ERROR_SOUND_INVALID_STATE;
                goto FINISH;
        }
 
        /* check if the priority of any node is higher than its based on io direction */
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                if (my_node == node || node->is_for_watch)
                        continue;
                if (param->request_type == FOCUS_TYPE_BOTH || node->status == FOCUS_STATUS_ACTIVATED_BOTH ||
@@ -930,7 +934,7 @@ int mm_sound_mgr_focus_request_acquire(const _mm_sound_mgr_focus_param_t *param)
                _mm_sound_mgr_focus_param_t *param_s = (_mm_sound_mgr_focus_param_t *)param;
                MMSOUND_STRNCPY(param_s->stream_type, my_node->stream_type, MAX_STREAM_TYPE_LEN);
                for (list = g_focus_node_list; list != NULL; list = list->next) {
-                       CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+                       SET_NODE_FROM_LIST_DATA(node, list);
                        if (node == my_node || node->is_for_watch)
                                continue;
                        if (param_s->request_type == FOCUS_TYPE_BOTH || node->status == FOCUS_STATUS_ACTIVATED_BOTH ||
@@ -992,7 +996,6 @@ int mm_sound_mgr_focus_request_release(const _mm_sound_mgr_focus_param_t *param)
        focus_node_t *node = NULL;
        focus_node_t *my_node = NULL;
        bool need_to_trigger_watch_cb = true;
-       bool need_to_trigger_cb = true;
        int i = 0;
 
        debug_fenter();
@@ -1000,50 +1003,39 @@ int mm_sound_mgr_focus_request_release(const _mm_sound_mgr_focus_param_t *param)
        if (!param->is_in_thread)
                MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_focus_node_list_mutex, MM_ERROR_SOUND_INTERNAL);
 
-       for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
-               CONTINUE_IF_NOT_MY_FOCUS_NODE(node, param);
+       if ((ret = get_my_focus_node(g_focus_node_list, param, &my_node)))
+               goto FINISH;
 
-               my_node = node;
-               if (my_node->status == FOCUS_STATUS_DEACTIVATED) {
+       if (my_node->status == FOCUS_STATUS_DEACTIVATED) {
                        debug_error("focus status is already deactivated");
                        ret = MM_ERROR_SOUND_INVALID_STATE;
                        goto FINISH;
-               } else if ((my_node->status != FOCUS_STATUS_ACTIVATED_BOTH) && (my_node->status != (focus_status_e)param->request_type)) {
+       } else if ((my_node->status != FOCUS_STATUS_ACTIVATED_BOTH) && (my_node->status != (focus_status_e)param->request_type)) {
                        debug_error("request type is not matched with current focus type");
                        ret = MM_ERROR_SOUND_INVALID_STATE;
                        goto FINISH;
-               }
-               break;
        }
 
-       if (my_node == NULL) {
-               debug_error("node is null");
-               ret = MM_ERROR_INVALID_ARGUMENT;
-               goto FINISH;
-       }
-
-       if (need_to_trigger_cb) {
-               _mm_sound_mgr_focus_param_t *param_s = (_mm_sound_mgr_focus_param_t *)param;
-               MMSOUND_STRNCPY(param_s->stream_type, my_node->stream_type, MAX_STREAM_TYPE_LEN);
-               for (list = g_focus_node_list; list != NULL; list = list->next) {
-                       CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
-                       if (node == my_node || node->is_for_watch)
-                               continue;
-                       for (i = 0; i < NUM_OF_STREAM_IO_TYPE; i++) {
-                               if (param_s->request_type & (i+1)) {
-                                       if (node->taken_by_id[i].pid == param_s->pid &&
-                                               node->taken_by_id[i].handle_id == param_s->handle_id) {
-                                               /* do callback for resumption */
-                                               if ((ret = _mm_sound_mgr_focus_do_callback(FOCUS_COMMAND_ACQUIRE, node, param_s)))
-                                                       debug_error("Fail to _focus_do_callback for COMMAND ACQUIRE to node[%p], ret[0x%x]", node, ret);
-                                               if (!strncmp(my_node->stream_type, node->stream_type, MAX_STREAM_TYPE_LEN))
-                                                       need_to_trigger_watch_cb = false;
-                                       }
+       _mm_sound_mgr_focus_param_t *param_s = (_mm_sound_mgr_focus_param_t *)param;
+       MMSOUND_STRNCPY(param_s->stream_type, my_node->stream_type, MAX_STREAM_TYPE_LEN);
+       for (list = g_focus_node_list; list != NULL; list = list->next) {
+               SET_NODE_FROM_LIST_DATA(node, list);
+               if (node == my_node || node->is_for_watch)
+                       continue;
+               for (i = 0; i < NUM_OF_STREAM_IO_TYPE; i++) {
+                       if (param_s->request_type & (i+1)) {
+                               if (node->taken_by_id[i].pid == param_s->pid &&
+                                       node->taken_by_id[i].handle_id == param_s->handle_id) {
+                                       /* do callback for resumption */
+                                       if ((ret = _mm_sound_mgr_focus_do_callback(FOCUS_COMMAND_ACQUIRE, node, param_s)))
+                                               debug_error("Fail to _focus_do_callback for COMMAND ACQUIRE to node[%p], ret[0x%x]", node, ret);
+                                       if (!strncmp(my_node->stream_type, node->stream_type, MAX_STREAM_TYPE_LEN))
+                                               need_to_trigger_watch_cb = false;
                                }
                        }
                }
        }
+
        /* update status */
        my_node->status &= ~(param->request_type);
        /* remove ext info. */
@@ -1078,7 +1070,7 @@ int mm_sound_mgr_focus_add_watch_node(const _mm_sound_mgr_focus_param_t *param)
        MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_focus_node_list_mutex, MM_ERROR_SOUND_INTERNAL);
 
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                if ((node->pid == param->pid) && (node->handle_id == param->handle_id) && node->is_for_watch) {
                        debug_error("the node of pid[%d]/handle_id[%d] for watch focus is already created", param->pid, param->handle_id);
                        ret = MM_ERROR_INVALID_ARGUMENT;
@@ -1121,7 +1113,7 @@ int mm_sound_mgr_focus_remove_watch_node(const _mm_sound_mgr_focus_param_t *para
        MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_focus_node_list_mutex, MM_ERROR_SOUND_INTERNAL);
 
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                if ((node->pid == param->pid) && (node->handle_id == param->handle_id) && node->is_for_watch) {
                        debug_log("found the node of pid[%d]/handle_id[%d] for watch focus", param->pid, param->handle_id);
                        __clear_focus_pipe(node);
@@ -1157,20 +1149,8 @@ int mm_sound_mgr_focus_deliver(const _mm_sound_mgr_focus_param_t *param)
 
        MMSOUND_ENTER_CRITICAL_SECTION_WITH_RETURN(&g_focus_node_list_mutex, MM_ERROR_SOUND_INTERNAL);
 
-       for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(src_node, list);
-               if ((src_node->pid == param->pid) && (src_node->handle_id == param->handle_id)) {
-                       debug_log("SRC: found the node of pid[%d]/handle_id[%d] for watch focus",
-                                       param->pid, param->handle_id);
-                       break;
-               }
-       }
-       if (src_node == NULL) {
-               debug_error("could not find the source node of param, pid[%d]/handle_id[%d]",
-                                       param->pid, param->handle_id);
-               ret = MM_ERROR_INVALID_ARGUMENT;
+       if ((ret = get_my_focus_node(g_focus_node_list, param, &src_node)))
                goto FINISH;
-       }
 
        if (!(src_node->status & param->request_type)) {
                debug_error("invalid request type(0x%x), src_node->status(0x%x)",
@@ -1180,7 +1160,7 @@ int mm_sound_mgr_focus_deliver(const _mm_sound_mgr_focus_param_t *param)
        }
 
        for (list = g_focus_node_list; list != NULL; list = list->next) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(dst_node, list);
+               SET_NODE_FROM_LIST_DATA(dst_node, list);
                if ((dst_node->pid == param->pid) && (dst_node->handle_id == param->handle_id_dst)) {
                        debug_log("DST: found the destination node of param, pid[%d]/handle_id[%d]",
                                        param->pid, param->handle_id_dst);
@@ -1200,7 +1180,7 @@ int mm_sound_mgr_focus_deliver(const _mm_sound_mgr_focus_param_t *param)
                        continue;
 
                for (list = g_focus_node_list; list != NULL; list = list->next) {
-                       CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+                       SET_NODE_FROM_LIST_DATA(node, list);
                        if ((node->taken_by_id[i].pid == src_node->pid) &&
                                (node->taken_by_id[i].handle_id == src_node->handle_id)) {
                                debug_log("found the node pid[%d]/handle_id[%d] that focus was taken by this src node, update it",
@@ -1250,7 +1230,7 @@ int mm_sound_mgr_focus_emergent_exit_by_id(int id)
 
        list = g_focus_node_list;
        while (list) {
-               CONTINUE_IF_LIST_DATA_IS_NULL(node, list);
+               SET_NODE_FROM_LIST_DATA(node, list);
                if (node->handle_id != id) {
                        list = list->next;
                        continue;
@@ -1268,7 +1248,7 @@ int mm_sound_mgr_focus_emergent_exit_by_id(int id)
                        my_node = node;
                        /* update info of nodes that are lost their focus by the process exited */
                        for (list_s = g_focus_node_list; list_s != NULL; list_s = list_s->next) {
-                               CONTINUE_IF_LIST_DATA_IS_NULL(node, list_s);
+                               SET_NODE_FROM_LIST_DATA(node, list_s);
                                for (i = 0; i < NUM_OF_STREAM_IO_TYPE; i++) {
                                        if (node->taken_by_id[i].handle_id == id) {
                                                if (my_node->taken_by_id[i].handle_id)
@@ -1294,7 +1274,7 @@ int mm_sound_mgr_focus_emergent_exit_by_id(int id)
                        param_s.request_type = my_node->status;
                        MMSOUND_STRNCPY(param_s.stream_type, my_node->stream_type, MAX_STREAM_TYPE_LEN);
                        for (list_s = g_focus_node_list; list_s != NULL; list_s = list_s->next) {
-                               CONTINUE_IF_LIST_DATA_IS_NULL(node, list_s);
+                               SET_NODE_FROM_LIST_DATA(node, list_s);
                                if (my_node->handle_id == node->handle_id || node->is_for_watch)
                                        continue;
                                for (i = 0; i < NUM_OF_STREAM_IO_TYPE; i++) {