50f96097936d7c09684538362e7307abfd5942ff
[apps/home/minicontrol.git] / src / minicontrol-monitor.c
1 /*
2  * Copyright (c)  2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 *realize_sh;
28         minictrl_sig_handle *stop_sh;
29         minictrl_sig_handle *resize_sh;
30         minictrl_sig_handle *request_sh;
31         minicontrol_monitor_cb callback;
32         minicontrol_monitor_with_handler_cb callback_with_handler;
33         void *user_data;
34 };
35
36 static struct _minicontrol_monitor *g_monitor_h;
37
38 static minicontrol_priority_e _int_to_priority(unsigned int value)
39 {
40         minicontrol_priority_e priority = MINICONTROL_PRIORITY_LOW;
41         switch (value) {
42         case MINICONTROL_PRIORITY_TOP:
43                 priority = MINICONTROL_PRIORITY_TOP;
44                 break;
45         case MINICONTROL_PRIORITY_MIDDLE:
46                 priority = MINICONTROL_PRIORITY_MIDDLE;
47                 break;
48         case MINICONTROL_PRIORITY_LOW:
49         default:
50                 priority = MINICONTROL_PRIORITY_LOW;
51                 break;
52         }
53         return priority;
54 }
55
56 static int _provider_get_data_from_dbus_message(
57         DBusMessage *msg, char **name,
58         unsigned int *w, unsigned int *h, unsigned int *pri,
59         minicontrol_h *handler)
60 {
61
62         DBusError err;
63         dbus_bool_t dbus_ret;
64         char *raw_data = NULL;
65         int raw_data_len = 0;
66
67         if (msg != NULL && name != NULL && 
68                 w != NULL && h != NULL && pri != NULL && 
69                 handler != NULL) {
70                 dbus_error_init(&err);
71                 dbus_ret = dbus_message_get_args(msg, &err,
72                                         DBUS_TYPE_STRING, name,
73                                         DBUS_TYPE_UINT32, w,
74                                         DBUS_TYPE_UINT32, h,
75                                         DBUS_TYPE_UINT32, pri,
76                                         DBUS_TYPE_STRING, &raw_data,
77                                         DBUS_TYPE_UINT32, &raw_data_len,
78                                         DBUS_TYPE_INVALID);
79                 if (!dbus_ret) {
80                         ERR("fail to get args : %s", err.message);
81                         if (*name == NULL) {
82                                 ERR("Failed to get a service name, give up");
83                                 dbus_error_free(&err);
84                                 return MINICONTROL_ERROR_DBUS;
85                         }
86                 }
87
88                 if (raw_data != NULL && raw_data_len > 0) {
89                         _minictrl_handler_get_handler_from_raw_data(handler, raw_data, raw_data_len);
90                 } else {
91                         ERR("failed to get handler from DBUS message");
92                 }
93                 dbus_error_free(&err);
94         }
95
96         return MINICONTROL_ERROR_NONE;
97 }
98
99 static void _provider_start_cb(void *data, DBusMessage *msg)
100 {
101         char *svr_name = NULL;
102         unsigned int w = 0;
103         unsigned int h = 0;
104         unsigned int pri = 0;
105         minicontrol_h handler = NULL;
106         minicontrol_priority_e priority;
107
108         if (_provider_get_data_from_dbus_message(msg, &svr_name, &w, &h, &pri, &handler) != MINICONTROL_ERROR_NONE) {
109                 ERR("fail to data from DBUS message");
110                 return ;
111         }
112
113         priority = _int_to_priority(pri);
114
115         if (g_monitor_h->callback) {
116                 g_monitor_h->callback(MINICONTROL_ACTION_START,
117                                 svr_name, w, h, priority,
118                                 g_monitor_h->user_data);
119         }
120         if (g_monitor_h->callback_with_handler) {
121                 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_START,
122                                 svr_name, w, h, priority,
123                                 handler,
124                                 g_monitor_h->user_data);
125         }
126
127         if (handler != NULL) {
128                 _minictrl_handler_destroy(handler);
129         }
130 }
131
132 static void _provider_realized_cb(void *data, DBusMessage *msg)
133 {
134         char *svr_name = NULL;
135         unsigned int w = 0;
136         unsigned int h = 0;
137         unsigned int pri = 0;
138         minicontrol_h handler = NULL;
139         minicontrol_priority_e priority;
140
141         if (_provider_get_data_from_dbus_message(msg, &svr_name, &w, &h, &pri, &handler) != MINICONTROL_ERROR_NONE) {
142                 ERR("fail to data from DBUS message");
143                 return ;
144         }
145
146         priority = _int_to_priority(pri);
147
148         if (g_monitor_h->callback) {
149                 g_monitor_h->callback(MINICONTROL_ACTION_REALIZE,
150                                 svr_name, w, h, priority,
151                                 g_monitor_h->user_data);
152         }
153         if (g_monitor_h->callback_with_handler) {
154                 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_REALIZE,
155                                 svr_name, w, h, priority,
156                                 handler,
157                                 g_monitor_h->user_data);
158         }
159
160         if (handler != NULL) {
161                 _minictrl_handler_destroy(handler);
162         }
163 }
164
165 static void _provider_stop_cb(void *data, DBusMessage *msg)
166 {
167         char *svr_name = NULL;
168         unsigned int w = 0;
169         unsigned int h = 0;
170         unsigned int pri = 0;
171         minicontrol_h handler = NULL;
172         minicontrol_priority_e priority;
173
174         if (_provider_get_data_from_dbus_message(msg, &svr_name, &w, &h, &pri, &handler) != MINICONTROL_ERROR_NONE) {
175                 ERR("fail to data from DBUS message");
176                 return ;
177         }
178
179         priority = _int_to_priority(pri);
180
181         if (g_monitor_h->callback) {
182                 g_monitor_h->callback(MINICONTROL_ACTION_STOP,
183                                 svr_name, w, h, priority,
184                                 g_monitor_h->user_data);
185         }
186         if (g_monitor_h->callback_with_handler) {
187                 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_STOP,
188                                 svr_name, w, h, priority,
189                                 handler,
190                                 g_monitor_h->user_data);
191         }
192
193         if (handler != NULL) {
194                 _minictrl_handler_destroy(handler);
195         }
196 }
197
198 static void _provider_resize_cb(void *data, DBusMessage *msg)
199 {
200         char *svr_name = NULL;
201         unsigned int w = 0;
202         unsigned int h = 0;
203         unsigned int pri = 0;
204         minicontrol_h handler = NULL;
205         minicontrol_priority_e priority;
206
207         if (_provider_get_data_from_dbus_message(msg, &svr_name, &w, &h, &pri, &handler) != MINICONTROL_ERROR_NONE) {
208                 ERR("fail to data from DBUS message");
209                 return ;
210         }
211
212         priority = _int_to_priority(pri);
213
214         if (g_monitor_h->callback) {
215                 g_monitor_h->callback(MINICONTROL_ACTION_RESIZE,
216                                 svr_name, w, h, priority,
217                                 g_monitor_h->user_data);
218         }
219         if (g_monitor_h->callback_with_handler) {
220                 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_RESIZE,
221                                 svr_name, w, h, priority,
222                                 handler,
223                                 g_monitor_h->user_data);
224         }
225
226         if (handler != NULL) {
227                 _minictrl_handler_destroy(handler);
228         }
229 }
230
231 static void _provider_request_cb(void *data, DBusMessage *msg)
232 {
233         char *svr_name = NULL;
234         unsigned int w = 0;
235         unsigned int h = 0;
236         unsigned int pri = 0;
237         minicontrol_h handler = NULL;
238         minicontrol_priority_e priority;
239
240         if (_provider_get_data_from_dbus_message(msg, &svr_name, &w, &h, &pri, &handler) != MINICONTROL_ERROR_NONE) {
241                 ERR("fail to data from DBUS message");
242                 return ;
243         }
244
245         priority = _int_to_priority(pri);
246
247         if (g_monitor_h->callback) {
248                 g_monitor_h->callback(MINICONTROL_ACTION_REQUEST,
249                                 svr_name, w, h, priority,
250                                 g_monitor_h->user_data);
251         }
252         if (g_monitor_h->callback_with_handler) {
253                 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_REQUEST,
254                                 svr_name, w, h, priority,
255                                 handler,
256                                 g_monitor_h->user_data);
257         }
258
259         if (handler != NULL) {
260                 _minictrl_handler_destroy(handler);
261         }
262 }
263
264 EXPORT_API minicontrol_error_e minicontrol_monitor_start(
265                                 minicontrol_monitor_cb callback, void *data)
266 {
267         if (!callback)
268                 return MINICONTROL_ERROR_INVALID_PARAMETER;
269
270         if (!g_monitor_h) {
271                 minictrl_sig_handle *start_sh;
272                 minictrl_sig_handle *realize_sh;
273                 minictrl_sig_handle *stop_sh;
274                 minictrl_sig_handle *resize_sh;
275                 minictrl_sig_handle *request_sh;
276                 struct _minicontrol_monitor *monitor_h;
277
278                 start_sh = _minictrl_dbus_sig_handle_attach(
279                                 MINICTRL_DBUS_SIG_START,
280                                 _provider_start_cb, NULL);
281                 if (!start_sh) {
282                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
283                                 MINICTRL_DBUS_SIG_START);
284                         return MINICONTROL_ERROR_DBUS;
285                 }
286
287                 realize_sh = _minictrl_dbus_sig_handle_attach(
288                                 MINICTRL_DBUS_SIG_REALIZE,
289                                 _provider_realized_cb, NULL);
290                 if (!realize_sh) {
291                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
292                                 MINICTRL_DBUS_SIG_REALIZE);
293                         return MINICONTROL_ERROR_DBUS;
294                 }
295
296                 stop_sh = _minictrl_dbus_sig_handle_attach(
297                                 MINICTRL_DBUS_SIG_STOP,
298                                 _provider_stop_cb, NULL);
299                 if (!stop_sh) {
300                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
301                                 MINICTRL_DBUS_SIG_STOP);
302                         return MINICONTROL_ERROR_DBUS;
303                 }
304
305                 resize_sh = _minictrl_dbus_sig_handle_attach(
306                                 MINICTRL_DBUS_SIG_RESIZE,
307                                 _provider_resize_cb, NULL);
308                 if (!resize_sh) {
309                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
310                                 MINICTRL_DBUS_SIG_RESIZE);
311                         return MINICONTROL_ERROR_DBUS;
312                 }
313
314                 request_sh = _minictrl_dbus_sig_handle_attach(
315                                 MINICTRL_DBUS_SIG_REQUEST,
316                                 _provider_request_cb, NULL);
317                 if (!request_sh) {
318                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
319                                         MINICTRL_DBUS_SIG_REQUEST);
320                         return MINICONTROL_ERROR_DBUS;
321                 }
322
323                 monitor_h = calloc(1, sizeof(struct _minicontrol_monitor));
324                 if (!monitor_h) {
325                         ERR("fail to alloc monitor_h");
326                         _minictrl_dbus_sig_handle_dettach(start_sh);
327                         _minictrl_dbus_sig_handle_dettach(realize_sh);
328                         _minictrl_dbus_sig_handle_dettach(stop_sh);
329                         _minictrl_dbus_sig_handle_dettach(resize_sh);
330                         _minictrl_dbus_sig_handle_dettach(request_sh);
331                         return MINICONTROL_ERROR_OUT_OF_MEMORY;
332                 }
333
334                 monitor_h->start_sh = start_sh;
335                 monitor_h->realize_sh = realize_sh;
336                 monitor_h->stop_sh = stop_sh;
337                 monitor_h->resize_sh = resize_sh;
338                 monitor_h->request_sh = request_sh;
339                 monitor_h->callback = NULL;
340                 monitor_h->callback_with_handler = NULL;
341                 g_monitor_h = monitor_h;
342         }
343
344         g_monitor_h->callback = callback;
345         g_monitor_h->user_data = data;
346         INFO("callback[%p], data[%p]", callback, data);
347
348         return _minictrl_viewer_req_message_send();
349 }
350
351 EXPORT_API minicontrol_error_e minicontrol_monitor_start_with_handler(
352                                 minicontrol_monitor_with_handler_cb callback, void *data)
353 {
354         if (!callback)
355                 return MINICONTROL_ERROR_INVALID_PARAMETER;
356
357         if (!g_monitor_h) {
358                 minictrl_sig_handle *start_sh;
359                 minictrl_sig_handle *realize_sh;
360                 minictrl_sig_handle *stop_sh;
361                 minictrl_sig_handle *resize_sh;
362                 minictrl_sig_handle *request_sh;
363                 struct _minicontrol_monitor *monitor_h;
364
365                 start_sh = _minictrl_dbus_sig_handle_attach(
366                                 MINICTRL_DBUS_SIG_START,
367                                 _provider_start_cb, NULL);
368                 if (!start_sh) {
369                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
370                                 MINICTRL_DBUS_SIG_START);
371                         return MINICONTROL_ERROR_DBUS;
372                 }
373
374                 realize_sh = _minictrl_dbus_sig_handle_attach(
375                                 MINICTRL_DBUS_SIG_REALIZE,
376                                 _provider_realized_cb, NULL);
377                 if (!realize_sh) {
378                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
379                                 MINICTRL_DBUS_SIG_REALIZE);
380                         return MINICONTROL_ERROR_DBUS;
381                 }
382
383                 stop_sh = _minictrl_dbus_sig_handle_attach(
384                                 MINICTRL_DBUS_SIG_STOP,
385                                 _provider_stop_cb, NULL);
386                 if (!stop_sh) {
387                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
388                                 MINICTRL_DBUS_SIG_STOP);
389                         return MINICONTROL_ERROR_DBUS;
390                 }
391
392                 resize_sh = _minictrl_dbus_sig_handle_attach(
393                                 MINICTRL_DBUS_SIG_RESIZE,
394                                 _provider_resize_cb, NULL);
395                 if (!resize_sh) {
396                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
397                                 MINICTRL_DBUS_SIG_RESIZE);
398                         return MINICONTROL_ERROR_DBUS;
399                 }
400
401                 request_sh = _minictrl_dbus_sig_handle_attach(
402                                 MINICTRL_DBUS_SIG_REQUEST,
403                                 _provider_request_cb, NULL);
404                 if (!request_sh) {
405                         ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
406                                         MINICTRL_DBUS_SIG_REQUEST);
407                         return MINICONTROL_ERROR_DBUS;
408                 }
409
410                 monitor_h = calloc(1, sizeof(struct _minicontrol_monitor));
411                 if (!monitor_h) {
412                         ERR("fail to alloc monitor_h");
413                         _minictrl_dbus_sig_handle_dettach(start_sh);
414                         _minictrl_dbus_sig_handle_dettach(realize_sh);
415                         _minictrl_dbus_sig_handle_dettach(stop_sh);
416                         _minictrl_dbus_sig_handle_dettach(resize_sh);
417                         _minictrl_dbus_sig_handle_dettach(request_sh);
418                         return MINICONTROL_ERROR_OUT_OF_MEMORY;
419                 }
420
421                 monitor_h->start_sh = start_sh;
422                 monitor_h->realize_sh = realize_sh;
423                 monitor_h->stop_sh = stop_sh;
424                 monitor_h->resize_sh = resize_sh;
425                 monitor_h->request_sh = request_sh;
426                 monitor_h->callback = NULL;
427                 monitor_h->callback_with_handler = NULL;
428                 g_monitor_h = monitor_h;
429         }
430
431         g_monitor_h->callback_with_handler = callback;
432         g_monitor_h->user_data = data;
433         INFO("callback[%p], data[%p]", callback, data);
434
435         return _minictrl_viewer_req_message_send();
436 }
437
438 EXPORT_API minicontrol_error_e minicontrol_monitor_stop(void)
439 {
440         if (!g_monitor_h)
441                 return MINICONTROL_ERROR_NONE;
442
443         if (g_monitor_h->start_sh)
444                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->start_sh);
445
446         if (g_monitor_h->realize_sh)
447                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->realize_sh);
448
449         if (g_monitor_h->stop_sh)
450                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->stop_sh);
451
452         if (g_monitor_h->resize_sh)
453                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->resize_sh);
454
455         if (g_monitor_h->request_sh)
456                 _minictrl_dbus_sig_handle_dettach(g_monitor_h->request_sh);
457
458         free(g_monitor_h);
459         g_monitor_h = NULL;
460
461         return MINICONTROL_ERROR_NONE;
462 }
463