Initial upload of minicontrol(tagging)
[apps/home/minicontrol.git] / src / minicontrol-monitor.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *  http://www.tizenopensource.org/license
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <dbus/dbus.h>
19
20 #include "minicontrol-error.h"
21 #include "minicontrol-internal.h"
22 #include "minicontrol-monitor.h"
23 #include "minicontrol-log.h"
24
25 struct _minicontrol_monitor {
26         minictrl_sig_handle *start_sh;
27         minictrl_sig_handle *stop_sh;
28         minictrl_sig_handle *resize_sh;
29         minicontrol_monitor_cb callback;
30         void *user_data;
31 };
32
33 static struct _minicontrol_monitor *g_monitor_h;
34
35 static minicontrol_priority_e _int_to_priority(unsigned int value)
36 {
37         minicontrol_priority_e priority = MINICONTROL_PRIORITY_LOW;
38         switch (value) {
39         case MINICONTROL_PRIORITY_TOP:
40                 priority = MINICONTROL_PRIORITY_TOP;
41                 break;
42         case MINICONTROL_PRIORITY_MIDDLE:
43                 priority = MINICONTROL_PRIORITY_MIDDLE;
44                 break;
45         case MINICONTROL_PRIORITY_LOW:
46         default:
47                 priority = MINICONTROL_PRIORITY_LOW;
48                 break;
49         }
50         return priority;
51 }
52
53 static void _provider_start_cb(void *data, DBusMessage *msg)
54 {
55         DBusError err;
56         char *svr_name = NULL;
57         unsigned int w = 0;
58         unsigned int h = 0;
59         unsigned int pri = 0;
60         minicontrol_priority_e priority;
61         dbus_bool_t dbus_ret;
62
63         dbus_error_init(&err);
64
65         dbus_ret = dbus_message_get_args(msg, &err,
66                                 DBUS_TYPE_STRING, &svr_name,
67                                 DBUS_TYPE_UINT32, &w,
68                                 DBUS_TYPE_UINT32, &h,
69                                 DBUS_TYPE_UINT32, &pri,
70                                 DBUS_TYPE_INVALID);
71         if (!dbus_ret) {
72                 ERR("fail to get args : %s", err.message);
73                 dbus_error_free(&err);
74                 return;
75         }
76
77         priority = _int_to_priority(pri);
78
79         if (g_monitor_h->callback)
80                 g_monitor_h->callback(MINICONTROL_ACTION_START,
81                                 svr_name, w, h, priority,
82                                 g_monitor_h->user_data);
83
84         dbus_error_free(&err);
85 }
86
87 static void _provider_stop_cb(void *data, DBusMessage *msg)
88 {
89         DBusError err;
90         char *svr_name = NULL;
91         dbus_bool_t dbus_ret;
92
93         dbus_error_init(&err);
94
95         dbus_ret = dbus_message_get_args(msg, &err,
96                                 DBUS_TYPE_STRING, &svr_name,
97                                 DBUS_TYPE_INVALID);
98         if (!dbus_ret) {
99                 ERR("fail to get args : %s", err.message);
100                 dbus_error_free(&err);
101                 return;
102         }
103
104         if (g_monitor_h->callback)
105                 g_monitor_h->callback(MINICONTROL_ACTION_STOP,
106                                 svr_name, 0, 0, MINICONTROL_PRIORITY_LOW,
107                                 g_monitor_h->user_data);
108
109         dbus_error_free(&err);
110 }
111
112 static void _provider_resize_cb(void *data, DBusMessage *msg)
113 {
114         DBusError err;
115         char *svr_name = NULL;
116         unsigned int w = 0;
117         unsigned int h = 0;
118         unsigned int pri = 0;
119         minicontrol_priority_e priority;
120         dbus_bool_t dbus_ret;
121
122         dbus_error_init(&err);
123
124         dbus_ret = dbus_message_get_args(msg, &err,
125                                 DBUS_TYPE_STRING, &svr_name,
126                                 DBUS_TYPE_UINT32, &w,
127                                 DBUS_TYPE_UINT32, &h,
128                                 DBUS_TYPE_UINT32, &pri,
129                                 DBUS_TYPE_INVALID);
130         if (!dbus_ret) {
131                 ERR("fail to get args : %s", err.message);
132                 dbus_error_free(&err);
133                 return;
134         }
135
136         priority = _int_to_priority(pri);
137
138         if (g_monitor_h->callback)
139                 g_monitor_h->callback(MINICONTROL_ACTION_RESIZE,
140                                 svr_name, w, h, priority,
141                                 g_monitor_h->user_data);
142
143         dbus_error_free(&err);
144 }
145
146
147 EXPORT_API minicontrol_error_e minicontrol_monitor_start(
148                                 minicontrol_monitor_cb callback, void *data)
149 {
150         if (!callback)
151                 return MINICONTROL_ERROR_INVALID_PARAMETER;
152
153         if (!g_monitor_h) {
154                 minictrl_sig_handle *start_sh;
155                 minictrl_sig_handle *stop_sh;
156                 minictrl_sig_handle *resize_sh;
157                 struct _minicontrol_monitor *monitor_h;
158
159                 start_sh = _minictrl_dbus_sig_handle_attach(
160                                 MINICTRL_DBUS_SIG_START,
161                                 _provider_start_cb, NULL);
162                 if (!start_sh) {
163                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
164                                 MINICTRL_DBUS_SIG_START);
165                         return MINICONTROL_ERROR_DBUS;
166                 }
167
168                 stop_sh = _minictrl_dbus_sig_handle_attach(
169                                 MINICTRL_DBUS_SIG_STOP,
170                                 _provider_stop_cb, NULL);
171                 if (!start_sh) {
172                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
173                                 MINICTRL_DBUS_SIG_STOP);
174                         return MINICONTROL_ERROR_DBUS;
175                 }
176
177                 resize_sh = _minictrl_dbus_sig_handle_attach(
178                                 MINICTRL_DBUS_SIG_RESIZE,
179                                 _provider_resize_cb, NULL);
180                 if (!resize_sh) {
181                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
182                                 MINICTRL_DBUS_SIG_RESIZE);
183                         return MINICONTROL_ERROR_DBUS;
184                 }
185
186                 monitor_h = malloc(sizeof(struct _minicontrol_monitor));
187                 if (!monitor_h) {
188                         ERR("fail to alloc monitor_h");
189                         _minictrl_dbus_sig_handle_dettach(start_sh);
190                         _minictrl_dbus_sig_handle_dettach(stop_sh);
191                         _minictrl_dbus_sig_handle_dettach(resize_sh);
192                         return MINICONTROL_ERROR_OUT_OF_MEMORY;
193                 }
194
195                 monitor_h->start_sh = start_sh;
196                 monitor_h->stop_sh = stop_sh;
197                 monitor_h->resize_sh = resize_sh;
198                 g_monitor_h = monitor_h;
199         }
200
201         g_monitor_h->callback = callback;
202         g_monitor_h->user_data = data;
203         INFO("callback[%p], data[%p]", callback, data);
204
205         return _minictrl_viewer_req_message_send();
206 }
207
208 EXPORT_API minicontrol_error_e minicontrol_monitor_stop(void)
209 {
210         if (!g_monitor_h)
211                 return MINICONTROL_ERROR_NONE;
212
213         if (g_monitor_h->start_sh)
214                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->start_sh);
215
216         if (g_monitor_h->stop_sh)
217                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->stop_sh);
218
219         if (g_monitor_h->resize_sh)
220                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->resize_sh);
221
222         free(g_monitor_h);
223         g_monitor_h = NULL;
224
225         return MINICONTROL_ERROR_NONE;
226 }
227