Tizen 2.0 Release
[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://floralicense.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         minictrl_sig_handle *request_sh;
30         minicontrol_monitor_cb callback;
31         void *user_data;
32 };
33
34 static struct _minicontrol_monitor *g_monitor_h;
35
36 static minicontrol_priority_e _int_to_priority(unsigned int value)
37 {
38         minicontrol_priority_e priority = MINICONTROL_PRIORITY_LOW;
39         switch (value) {
40         case MINICONTROL_PRIORITY_TOP:
41                 priority = MINICONTROL_PRIORITY_TOP;
42                 break;
43         case MINICONTROL_PRIORITY_MIDDLE:
44                 priority = MINICONTROL_PRIORITY_MIDDLE;
45                 break;
46         case MINICONTROL_PRIORITY_LOW:
47         default:
48                 priority = MINICONTROL_PRIORITY_LOW;
49                 break;
50         }
51         return priority;
52 }
53
54 static void _provider_start_cb(void *data, DBusMessage *msg)
55 {
56         DBusError err;
57         char *svr_name = NULL;
58         unsigned int w = 0;
59         unsigned int h = 0;
60         unsigned int pri = 0;
61         minicontrol_priority_e priority;
62         dbus_bool_t dbus_ret;
63
64         dbus_error_init(&err);
65
66         dbus_ret = dbus_message_get_args(msg, &err,
67                                 DBUS_TYPE_STRING, &svr_name,
68                                 DBUS_TYPE_UINT32, &w,
69                                 DBUS_TYPE_UINT32, &h,
70                                 DBUS_TYPE_UINT32, &pri,
71                                 DBUS_TYPE_INVALID);
72         if (!dbus_ret) {
73                 ERR("fail to get args : %s", err.message);
74                 dbus_error_free(&err);
75                 return;
76         }
77
78         priority = _int_to_priority(pri);
79
80         if (g_monitor_h->callback)
81                 g_monitor_h->callback(MINICONTROL_ACTION_START,
82                                 svr_name, w, h, priority,
83                                 g_monitor_h->user_data);
84
85         dbus_error_free(&err);
86 }
87
88 static void _provider_stop_cb(void *data, DBusMessage *msg)
89 {
90         DBusError err;
91         char *svr_name = NULL;
92         dbus_bool_t dbus_ret;
93
94         dbus_error_init(&err);
95
96         dbus_ret = dbus_message_get_args(msg, &err,
97                                 DBUS_TYPE_STRING, &svr_name,
98                                 DBUS_TYPE_INVALID);
99         if (!dbus_ret) {
100                 ERR("fail to get args : %s", err.message);
101                 dbus_error_free(&err);
102                 return;
103         }
104
105         if (g_monitor_h->callback)
106                 g_monitor_h->callback(MINICONTROL_ACTION_STOP,
107                                 svr_name, 0, 0, MINICONTROL_PRIORITY_LOW,
108                                 g_monitor_h->user_data);
109
110         dbus_error_free(&err);
111 }
112
113 static void _provider_resize_cb(void *data, DBusMessage *msg)
114 {
115         DBusError err;
116         char *svr_name = NULL;
117         unsigned int w = 0;
118         unsigned int h = 0;
119         unsigned int pri = 0;
120         minicontrol_priority_e priority;
121         dbus_bool_t dbus_ret;
122
123         dbus_error_init(&err);
124
125         dbus_ret = dbus_message_get_args(msg, &err,
126                                 DBUS_TYPE_STRING, &svr_name,
127                                 DBUS_TYPE_UINT32, &w,
128                                 DBUS_TYPE_UINT32, &h,
129                                 DBUS_TYPE_UINT32, &pri,
130                                 DBUS_TYPE_INVALID);
131         if (!dbus_ret) {
132                 ERR("fail to get args : %s", err.message);
133                 dbus_error_free(&err);
134                 return;
135         }
136
137         priority = _int_to_priority(pri);
138
139         if (g_monitor_h->callback)
140                 g_monitor_h->callback(MINICONTROL_ACTION_RESIZE,
141                                 svr_name, w, h, priority,
142                                 g_monitor_h->user_data);
143
144         dbus_error_free(&err);
145 }
146
147 static void _provider_request_cb(void *data, DBusMessage *msg)
148 {
149         DBusError err;
150         char *svr_name = NULL;
151         unsigned int w = 0;
152         unsigned int h = 0;
153         unsigned int pri = 0;
154         minicontrol_priority_e priority;
155         dbus_bool_t dbus_ret;
156
157         dbus_error_init(&err);
158
159         dbus_ret = dbus_message_get_args(msg, &err,
160                                 DBUS_TYPE_STRING, &svr_name,
161                                 DBUS_TYPE_UINT32, &w,
162                                 DBUS_TYPE_UINT32, &h,
163                                 DBUS_TYPE_UINT32, &pri,
164                                 DBUS_TYPE_INVALID);
165         if (!dbus_ret) {
166                 ERR("fail to get args : %s", err.message);
167                 dbus_error_free(&err);
168                 return;
169         }
170
171         priority = _int_to_priority(pri);
172
173         if (g_monitor_h->callback)
174                 g_monitor_h->callback(MINICONTROL_ACTION_REQUEST,
175                                 svr_name, w, h, priority,
176                                 g_monitor_h->user_data);
177
178         dbus_error_free(&err);
179 }
180
181 EXPORT_API minicontrol_error_e minicontrol_monitor_start(
182                                 minicontrol_monitor_cb callback, void *data)
183 {
184         if (!callback)
185                 return MINICONTROL_ERROR_INVALID_PARAMETER;
186
187         if (!g_monitor_h) {
188                 minictrl_sig_handle *start_sh;
189                 minictrl_sig_handle *stop_sh;
190                 minictrl_sig_handle *resize_sh;
191                 minictrl_sig_handle *request_sh;
192                 struct _minicontrol_monitor *monitor_h;
193
194                 start_sh = _minictrl_dbus_sig_handle_attach(
195                                 MINICTRL_DBUS_SIG_START,
196                                 _provider_start_cb, NULL);
197                 if (!start_sh) {
198                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
199                                 MINICTRL_DBUS_SIG_START);
200                         return MINICONTROL_ERROR_DBUS;
201                 }
202
203                 stop_sh = _minictrl_dbus_sig_handle_attach(
204                                 MINICTRL_DBUS_SIG_STOP,
205                                 _provider_stop_cb, NULL);
206                 if (!stop_sh) {
207                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
208                                 MINICTRL_DBUS_SIG_STOP);
209                         return MINICONTROL_ERROR_DBUS;
210                 }
211
212                 resize_sh = _minictrl_dbus_sig_handle_attach(
213                                 MINICTRL_DBUS_SIG_RESIZE,
214                                 _provider_resize_cb, NULL);
215                 if (!resize_sh) {
216                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
217                                 MINICTRL_DBUS_SIG_RESIZE);
218                         return MINICONTROL_ERROR_DBUS;
219                 }
220
221                 request_sh = _minictrl_dbus_sig_handle_attach(
222                                 MINICTRL_DBUS_SIG_REQUEST,
223                                 _provider_request_cb, NULL);
224                 if (!request_sh) {
225                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
226                                         MINICTRL_DBUS_SIG_REQUEST);
227                         return MINICONTROL_ERROR_DBUS;
228                 }
229
230                 monitor_h = malloc(sizeof(struct _minicontrol_monitor));
231                 if (!monitor_h) {
232                         ERR("fail to alloc monitor_h");
233                         _minictrl_dbus_sig_handle_dettach(start_sh);
234                         _minictrl_dbus_sig_handle_dettach(stop_sh);
235                         _minictrl_dbus_sig_handle_dettach(resize_sh);
236                         _minictrl_dbus_sig_handle_dettach(request_sh);
237                         return MINICONTROL_ERROR_OUT_OF_MEMORY;
238                 }
239
240                 monitor_h->start_sh = start_sh;
241                 monitor_h->stop_sh = stop_sh;
242                 monitor_h->resize_sh = resize_sh;
243                 monitor_h->request_sh = request_sh;
244                 g_monitor_h = monitor_h;
245         }
246
247         g_monitor_h->callback = callback;
248         g_monitor_h->user_data = data;
249         INFO("callback[%p], data[%p]", callback, data);
250
251         return _minictrl_viewer_req_message_send();
252 }
253
254 EXPORT_API minicontrol_error_e minicontrol_monitor_stop(void)
255 {
256         if (!g_monitor_h)
257                 return MINICONTROL_ERROR_NONE;
258
259         if (g_monitor_h->start_sh)
260                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->start_sh);
261
262         if (g_monitor_h->stop_sh)
263                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->stop_sh);
264
265         if (g_monitor_h->resize_sh)
266                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->resize_sh);
267
268         if (g_monitor_h->request_sh)
269                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->request_sh);
270
271         free(g_monitor_h);
272         g_monitor_h = NULL;
273
274         return MINICONTROL_ERROR_NONE;
275 }
276