Tizen 2.4 SDK Rev6 Release
[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-viewer.h"
24 #include "minicontrol-log.h"
25
26 struct _minicontrol_monitor {
27         minictrl_sig_handle *event_sh;
28         minicontrol_monitor_cb callback;
29         void *user_data;
30 };
31
32 static struct _minicontrol_monitor *g_monitor_h = NULL;
33
34 static minicontrol_priority_e _int_to_priority(unsigned int value)
35 {
36         minicontrol_priority_e priority = MINICONTROL_PRIORITY_LOW;
37         switch (value) {
38         case MINICONTROL_PRIORITY_TOP:
39                 priority = MINICONTROL_PRIORITY_TOP;
40                 break;
41         case MINICONTROL_PRIORITY_MIDDLE:
42                 priority = MINICONTROL_PRIORITY_MIDDLE;
43                 break;
44         case MINICONTROL_PRIORITY_LOW:
45         default:
46                 priority = MINICONTROL_PRIORITY_LOW;
47                 break;
48         }
49         return priority;
50 }
51
52 static void _sig_to_viewer_handler_cb(minicontrol_event_e event, const char *minicontrol_name, bundle *event_arg, void *data)
53 {
54         minicontrol_action_e action;
55         int width = 0;
56         int height = 0;
57         int priority_from_signal = 0;
58         minicontrol_priority_e priority = 0;
59         size_t n_size;
60
61         switch(event) {
62         case MINICONTROL_EVENT_START:
63                 action = MINICONTROL_ACTION_START;
64                 break;
65
66         case MINICONTROL_EVENT_STOP:
67                 action = MINICONTROL_ACTION_STOP;
68                 break;
69
70         case MINICONTROL_EVENT_RESIZE:
71                 action = MINICONTROL_ACTION_RESIZE;
72                 break;
73
74         case MINICONTROL_EVENT_REQUEST_HIDE:
75         case MINICONTROL_EVENT_REQUEST_ANGLE:
76                 action = MINICONTROL_ACTION_REQUEST;
77                 break;
78
79         default:
80                 WARN("Not supported event [%d]", event);
81                 action = event;
82                 break;
83         }
84
85         if (action == MINICONTROL_ACTION_START || action == MINICONTROL_ACTION_RESIZE || action == MINICONTROL_ACTION_REQUEST) {
86                 bundle_get_byte(event_arg, "width", (void*)&width, &n_size);
87                 bundle_get_byte(event_arg, "height", (void*)&height, &n_size);
88                 bundle_get_byte(event_arg, "priority", (void*)&priority_from_signal, &n_size);
89                 priority = _int_to_priority(priority_from_signal);
90         }
91         else {
92                 priority = MINICONTROL_PRIORITY_LOW;
93         }
94
95         g_monitor_h->callback(action, minicontrol_name, width, height, priority, g_monitor_h->user_data);
96 }
97
98 EXPORT_API minicontrol_error_e minicontrol_monitor_start(minicontrol_monitor_cb callback, void *data)
99 {
100         if (!callback)
101                 return MINICONTROL_ERROR_INVALID_PARAMETER;
102
103         INFO("callback[%p], data[%p]", callback, data);
104
105         if (g_monitor_h) {
106                 ERR("Already started");
107                 return MINICONTROL_ERROR_UNKNOWN;
108         }
109
110         g_monitor_h = malloc(sizeof(struct _minicontrol_monitor));
111         if (g_monitor_h == NULL) {
112                 ERR("fail to alloc monitor_h");
113                 return MINICONTROL_ERROR_OUT_OF_MEMORY;
114         }
115
116         minicontrol_viewer_set_event_cb(_sig_to_viewer_handler_cb, data);
117
118         g_monitor_h->callback = callback;
119         g_monitor_h->user_data = data;
120
121         return _minictrl_viewer_req_message_send();
122 }
123
124 EXPORT_API minicontrol_error_e minicontrol_monitor_stop(void)
125 {
126         if (!g_monitor_h)
127                 return MINICONTROL_ERROR_NONE;
128
129         minicontrol_viewer_unset_event_cb();
130
131         free(g_monitor_h);
132         g_monitor_h = NULL;
133
134         return MINICONTROL_ERROR_NONE;
135 }