2 * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dbus/dbus.h>
20 #include "minicontrol-error.h"
21 #include "minicontrol-internal.h"
22 #include "minicontrol-monitor.h"
23 #include "minicontrol-log.h"
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;
36 static struct _minicontrol_monitor *g_monitor_h;
38 static minicontrol_priority_e _int_to_priority(unsigned int value)
40 minicontrol_priority_e priority = MINICONTROL_PRIORITY_LOW;
42 case MINICONTROL_PRIORITY_TOP:
43 priority = MINICONTROL_PRIORITY_TOP;
45 case MINICONTROL_PRIORITY_MIDDLE:
46 priority = MINICONTROL_PRIORITY_MIDDLE;
48 case MINICONTROL_PRIORITY_LOW:
50 priority = MINICONTROL_PRIORITY_LOW;
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)
64 char *raw_data = NULL;
67 if (msg != NULL && name != NULL &&
68 w != NULL && h != NULL && pri != NULL &&
70 dbus_error_init(&err);
71 dbus_ret = dbus_message_get_args(msg, &err,
72 DBUS_TYPE_STRING, name,
75 DBUS_TYPE_UINT32, pri,
76 DBUS_TYPE_STRING, &raw_data,
77 DBUS_TYPE_UINT32, &raw_data_len,
80 ERR("fail to get args : %s", err.message);
82 ERR("Failed to get a service name, give up");
83 dbus_error_free(&err);
84 return MINICONTROL_ERROR_DBUS;
88 if (raw_data != NULL && raw_data_len > 0) {
89 _minictrl_handler_get_handler_from_raw_data(handler, raw_data, raw_data_len);
91 ERR("failed to get handler from DBUS message");
93 dbus_error_free(&err);
96 return MINICONTROL_ERROR_NONE;
99 static void _provider_start_cb(void *data, DBusMessage *msg)
101 char *svr_name = NULL;
104 unsigned int pri = 0;
105 minicontrol_h handler = NULL;
106 minicontrol_priority_e priority;
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");
113 priority = _int_to_priority(pri);
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);
120 if (g_monitor_h->callback_with_handler) {
121 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_START,
122 svr_name, w, h, priority,
124 g_monitor_h->user_data);
127 if (handler != NULL) {
128 _minictrl_handler_destroy(handler);
132 static void _provider_realized_cb(void *data, DBusMessage *msg)
134 char *svr_name = NULL;
137 unsigned int pri = 0;
138 minicontrol_h handler = NULL;
139 minicontrol_priority_e priority;
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");
146 priority = _int_to_priority(pri);
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);
153 if (g_monitor_h->callback_with_handler) {
154 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_REALIZE,
155 svr_name, w, h, priority,
157 g_monitor_h->user_data);
160 if (handler != NULL) {
161 _minictrl_handler_destroy(handler);
165 static void _provider_stop_cb(void *data, DBusMessage *msg)
167 char *svr_name = NULL;
170 unsigned int pri = 0;
171 minicontrol_h handler = NULL;
172 minicontrol_priority_e priority;
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");
179 priority = _int_to_priority(pri);
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);
186 if (g_monitor_h->callback_with_handler) {
187 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_STOP,
188 svr_name, w, h, priority,
190 g_monitor_h->user_data);
193 if (handler != NULL) {
194 _minictrl_handler_destroy(handler);
198 static void _provider_resize_cb(void *data, DBusMessage *msg)
200 char *svr_name = NULL;
203 unsigned int pri = 0;
204 minicontrol_h handler = NULL;
205 minicontrol_priority_e priority;
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");
212 priority = _int_to_priority(pri);
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);
219 if (g_monitor_h->callback_with_handler) {
220 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_RESIZE,
221 svr_name, w, h, priority,
223 g_monitor_h->user_data);
226 if (handler != NULL) {
227 _minictrl_handler_destroy(handler);
231 static void _provider_request_cb(void *data, DBusMessage *msg)
233 char *svr_name = NULL;
236 unsigned int pri = 0;
237 minicontrol_h handler = NULL;
238 minicontrol_priority_e priority;
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");
245 priority = _int_to_priority(pri);
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);
252 if (g_monitor_h->callback_with_handler) {
253 g_monitor_h->callback_with_handler(MINICONTROL_ACTION_REQUEST,
254 svr_name, w, h, priority,
256 g_monitor_h->user_data);
259 if (handler != NULL) {
260 _minictrl_handler_destroy(handler);
264 EXPORT_API minicontrol_error_e minicontrol_monitor_start(
265 minicontrol_monitor_cb callback, void *data)
268 return MINICONTROL_ERROR_INVALID_PARAMETER;
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;
278 start_sh = _minictrl_dbus_sig_handle_attach(
279 MINICTRL_DBUS_SIG_START,
280 _provider_start_cb, NULL);
282 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
283 MINICTRL_DBUS_SIG_START);
284 return MINICONTROL_ERROR_DBUS;
287 realize_sh = _minictrl_dbus_sig_handle_attach(
288 MINICTRL_DBUS_SIG_REALIZE,
289 _provider_realized_cb, NULL);
291 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
292 MINICTRL_DBUS_SIG_REALIZE);
293 return MINICONTROL_ERROR_DBUS;
296 stop_sh = _minictrl_dbus_sig_handle_attach(
297 MINICTRL_DBUS_SIG_STOP,
298 _provider_stop_cb, NULL);
300 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
301 MINICTRL_DBUS_SIG_STOP);
302 return MINICONTROL_ERROR_DBUS;
305 resize_sh = _minictrl_dbus_sig_handle_attach(
306 MINICTRL_DBUS_SIG_RESIZE,
307 _provider_resize_cb, NULL);
309 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
310 MINICTRL_DBUS_SIG_RESIZE);
311 return MINICONTROL_ERROR_DBUS;
314 request_sh = _minictrl_dbus_sig_handle_attach(
315 MINICTRL_DBUS_SIG_REQUEST,
316 _provider_request_cb, NULL);
318 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
319 MINICTRL_DBUS_SIG_REQUEST);
320 return MINICONTROL_ERROR_DBUS;
323 monitor_h = calloc(1, sizeof(struct _minicontrol_monitor));
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;
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;
344 g_monitor_h->callback = callback;
345 g_monitor_h->user_data = data;
346 INFO("callback[%p], data[%p]", callback, data);
348 return _minictrl_viewer_req_message_send();
351 EXPORT_API minicontrol_error_e minicontrol_monitor_start_with_handler(
352 minicontrol_monitor_with_handler_cb callback, void *data)
355 return MINICONTROL_ERROR_INVALID_PARAMETER;
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;
365 start_sh = _minictrl_dbus_sig_handle_attach(
366 MINICTRL_DBUS_SIG_START,
367 _provider_start_cb, NULL);
369 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
370 MINICTRL_DBUS_SIG_START);
371 return MINICONTROL_ERROR_DBUS;
374 realize_sh = _minictrl_dbus_sig_handle_attach(
375 MINICTRL_DBUS_SIG_REALIZE,
376 _provider_realized_cb, NULL);
378 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
379 MINICTRL_DBUS_SIG_REALIZE);
380 return MINICONTROL_ERROR_DBUS;
383 stop_sh = _minictrl_dbus_sig_handle_attach(
384 MINICTRL_DBUS_SIG_STOP,
385 _provider_stop_cb, NULL);
387 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
388 MINICTRL_DBUS_SIG_STOP);
389 return MINICONTROL_ERROR_DBUS;
392 resize_sh = _minictrl_dbus_sig_handle_attach(
393 MINICTRL_DBUS_SIG_RESIZE,
394 _provider_resize_cb, NULL);
396 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
397 MINICTRL_DBUS_SIG_RESIZE);
398 return MINICONTROL_ERROR_DBUS;
401 request_sh = _minictrl_dbus_sig_handle_attach(
402 MINICTRL_DBUS_SIG_REQUEST,
403 _provider_request_cb, NULL);
405 ERR("fail to _minictrl_dbus_sig_handle_attach - %s",
406 MINICTRL_DBUS_SIG_REQUEST);
407 return MINICONTROL_ERROR_DBUS;
410 monitor_h = calloc(1, sizeof(struct _minicontrol_monitor));
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;
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;
431 g_monitor_h->callback_with_handler = callback;
432 g_monitor_h->user_data = data;
433 INFO("callback[%p], data[%p]", callback, data);
435 return _minictrl_viewer_req_message_send();
438 EXPORT_API minicontrol_error_e minicontrol_monitor_stop(void)
441 return MINICONTROL_ERROR_NONE;
443 if (g_monitor_h->start_sh)
444 _minictrl_dbus_sig_handle_dettach(g_monitor_h->start_sh);
446 if (g_monitor_h->realize_sh)
447 _minictrl_dbus_sig_handle_dettach(g_monitor_h->realize_sh);
449 if (g_monitor_h->stop_sh)
450 _minictrl_dbus_sig_handle_dettach(g_monitor_h->stop_sh);
452 if (g_monitor_h->resize_sh)
453 _minictrl_dbus_sig_handle_dettach(g_monitor_h->resize_sh);
455 if (g_monitor_h->request_sh)
456 _minictrl_dbus_sig_handle_dettach(g_monitor_h->request_sh);
461 return MINICONTROL_ERROR_NONE;