Initial version
[apps/native/rcc.git] / src / controller.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Jin Yoon <jinny.yoon@samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <Eina.h>
20
21 #include "log.h"
22 #include "controller.h"
23
24 struct controller_s {
25         Eina_List *event_cb_list;
26 };
27 static struct controller_s controller_info;
28
29 struct _controller_event_cb_info_s {
30         char *event_name;
31         controller_event_cb event_cb;
32         void *data;
33 };
34 typedef struct _controller_event_cb_info_s controller_event_cb_info_s;
35
36
37 int controller_register_event_cb(const char *event_name, controller_event_cb event_cb, void *data)
38 {
39         controller_event_cb_info_s *event_cb_info = NULL;
40
41         retv_if(!event_name, -1);
42         retv_if(!event_cb, -1);
43
44         event_cb_info = calloc(1, sizeof(controller_event_cb_info_s));
45         retv_if(!event_cb_info, -1);
46
47         event_cb_info->event_name = strdup(event_name);
48         goto_if(!event_cb_info->event_name, error);
49
50         event_cb_info->event_cb = event_cb;
51         event_cb_info->data = data;
52
53         controller_info.event_cb_list = eina_list_append(controller_info.event_cb_list, event_cb_info);
54
55         return 0;
56
57 error:
58         if (event_cb_info) free(event_cb_info);
59
60         return -1;
61 }
62
63 int controller_unregister_event_cb(const char *event_name, controller_event_cb event_cb)
64 {
65         controller_event_cb_info_s *event_cb_info = NULL;
66         const Eina_List *l = NULL;
67         const Eina_List *ln = NULL;
68
69         retv_if(!event_name, -1);
70         retv_if(!event_cb, -1);
71
72         EINA_LIST_FOREACH_SAFE(controller_info.event_cb_list, l, ln, event_cb_info) {
73                 if (event_cb_info->event_name 
74                         && strcmp(event_cb_info->event_name, event_name)
75                         && event_cb_info->event_cb == event_cb)
76                 {
77                         controller_info.event_cb_list = eina_list_remove(controller_info.event_cb_list, event_cb_info);
78                         break;
79                 }
80         }
81
82         return 0;
83 }
84
85 int controller_send_event(const char *event_name, void *event_info)
86 {
87         controller_event_cb_info_s *event_cb_info = NULL;
88         const Eina_List *l = NULL;
89         const Eina_List *ln = NULL;
90
91         retv_if(!event_name, -1);
92
93         EINA_LIST_FOREACH_SAFE(controller_info.event_cb_list, l, ln, event_cb_info) {
94                 if (event_cb_info->event_name 
95                         && strcmp(event_cb_info->event_name, event_name))
96                 {
97                         int ret = -1;
98                         ret = event_cb_info->event_cb(event_name, event_info, event_cb_info->data);
99                         if (ret < 0) _E("There were errors sending an event");
100                         break;
101                 }
102         }
103
104         return 0;
105 }