Updated the license and boilerplate
[apps/home/minicontrol.git] / src / minicontrol-internal.c
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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 #include <dbus/dbus-glib-lowlevel.h>
20
21 #include "minicontrol-error.h"
22 #include "minicontrol-type.h"
23 #include "minicontrol-internal.h"
24 #include "minicontrol-log.h"
25
26 #define MINICTRL_DBUS_PATH "/org/tizen/minicontrol"
27 #define MINICTRL_DBUS_INTERFACE "org.tizen.minicontrol.signal"
28
29 struct _minictrl_sig_handle {
30         DBusConnection *conn;
31         void (*callback) (void *data, DBusMessage *msg);
32         void *user_data;
33         char *signal;
34 };
35
36 int _minictrl_viewer_req_message_send(void)
37 {
38         DBusConnection *connection = NULL;
39         DBusMessage *message = NULL;
40         DBusError err;
41         dbus_bool_t dbus_ret;
42         int ret = MINICONTROL_ERROR_NONE;
43
44         dbus_error_init(&err);
45         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
46         if (!connection) {
47                 ERR("Fail to dbus_bus_get : %s", err.message);
48                 ret = MINICONTROL_ERROR_DBUS;
49                 goto release_n_return;
50         }
51
52         message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
53                                 MINICTRL_DBUS_INTERFACE,
54                                 MINICTRL_DBUS_SIG_RUNNING_REQ);
55         if (!message) {
56                 ERR("fail to create dbus message");
57                 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
58                 goto release_n_return;
59         }
60
61         dbus_ret = dbus_connection_send(connection, message, NULL);
62         if (!dbus_ret) {
63                 ERR("fail to send dbus viewer req message");
64                 ret = MINICONTROL_ERROR_DBUS;
65                 goto release_n_return;
66         }
67
68         dbus_connection_flush(connection);
69
70 release_n_return:
71         dbus_error_free(&err);
72
73         if (message)
74                 dbus_message_unref(message);
75
76         if (connection)
77                 dbus_connection_unref(connection);
78
79         return ret;
80 }
81
82 int _minictrl_provider_message_send(const char *sig_name, const char *svr_name,
83                                 unsigned int witdh, unsigned int height,
84                                 minicontrol_priority_e priority)
85 {
86         DBusConnection *connection = NULL;
87         DBusMessage *message = NULL;
88         DBusError err;
89         dbus_bool_t dbus_ret;
90         int ret = MINICONTROL_ERROR_NONE;
91
92         if (!sig_name) {
93                 ERR("sig_name is NULL, invaild parameter");
94                 return MINICONTROL_ERROR_INVALID_PARAMETER;
95         }
96
97         if (!svr_name) {
98                 ERR("svr_name is NULL, invaild parameter");
99                 return MINICONTROL_ERROR_INVALID_PARAMETER;
100         }
101
102         dbus_error_init(&err);
103         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
104         if (!connection) {
105                 ERR("Fail to dbus_bus_get : %s", err.message);
106                 ret = MINICONTROL_ERROR_DBUS;
107                 goto release_n_return;
108         }
109
110         message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
111                                 MINICTRL_DBUS_INTERFACE,
112                                 sig_name);
113
114         if (!message) {
115                 ERR("fail to create dbus message");
116                 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
117                 goto release_n_return;
118         }
119
120         dbus_ret = dbus_message_append_args(message,
121                         DBUS_TYPE_STRING, &svr_name,
122                         DBUS_TYPE_UINT32, &witdh,
123                         DBUS_TYPE_UINT32, &height,
124                         DBUS_TYPE_UINT32, &priority,
125                         DBUS_TYPE_INVALID);
126         if (!dbus_ret) {
127                 ERR("fail to append name to dbus message : %s", svr_name);
128                 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
129                 goto release_n_return;
130         }
131
132         dbus_ret = dbus_connection_send(connection, message, NULL);
133         if (!dbus_ret) {
134                 ERR("fail to send dbus message : %s", svr_name);
135                 ret = MINICONTROL_ERROR_DBUS;
136                 goto release_n_return;
137         }
138
139         dbus_connection_flush(connection);
140         INFO("[%s][%s] size-[%ux%u] priority[%u]",
141                 sig_name, svr_name, witdh, height, priority);
142
143 release_n_return:
144         dbus_error_free(&err);
145
146         if (message)
147                 dbus_message_unref(message);
148
149         if (connection)
150                 dbus_connection_unref(connection);
151
152         return ret;
153 }
154
155 static DBusHandlerResult _minictrl_signal_filter(DBusConnection *conn,
156                 DBusMessage *msg, void *user_data)
157 {
158         minictrl_sig_handle *handle = NULL;
159         const char *interface;
160         DBusError error;
161         dbus_bool_t ret;
162
163         if (!user_data)
164                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
165
166         handle = user_data;
167
168         dbus_error_init(&error);
169
170         interface = dbus_message_get_interface(msg);
171         if (!interface)
172                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
173
174         if (strcmp(MINICTRL_DBUS_INTERFACE, interface))
175                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
176
177         ret = dbus_message_is_signal(msg, interface, handle->signal);
178         if (!ret) {
179                 DBG("this msg is not signal");
180                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
181         }
182
183         if (handle->callback)
184                 handle->callback(handle->user_data, msg);
185
186         return DBUS_HANDLER_RESULT_HANDLED;
187 }
188
189
190 minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
191                                 void (*callback) (void *data, DBusMessage *msg),
192                                 void *data)
193 {
194         minictrl_sig_handle *handle = NULL;
195         DBusError err;
196         DBusConnection *conn = NULL;
197         char rule[1024] = {'\0', };
198
199         if (!signal) {
200                 ERR("signal is NULL");
201                 return NULL;
202         }
203
204         if (!callback) {
205                 ERR("call is NULL");
206                 return NULL;
207         }
208
209         handle = malloc(sizeof(minictrl_sig_handle));
210         if (!handle) {
211                 ERR("fail to alloc handle");
212                 return NULL;
213         }
214
215         dbus_error_init(&err);
216         conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
217         if (!conn) {
218                 ERR("fail to get bus : %s", err.message);
219                 goto error_n_return;
220         }
221
222         dbus_connection_setup_with_g_main(conn, NULL);
223         snprintf(rule, 1024,
224                 "path='%s',type='signal',interface='%s',member='%s'",
225                 MINICTRL_DBUS_PATH,
226                 MINICTRL_DBUS_INTERFACE,
227                 signal);
228
229         dbus_bus_add_match(conn, rule, &err);
230         if (dbus_error_is_set(&err)) {
231                 ERR("fail to dbus_bus_remove_match : %s",
232                                 err.message);
233                 goto error_n_return;
234         }
235
236         if (dbus_connection_add_filter(conn, _minictrl_signal_filter,
237                                         handle, NULL) == FALSE) {
238                 ERR("fail to dbus_connection_add_filter : %s",
239                                 err.message);
240                 goto error_n_return;
241         }
242
243         dbus_connection_set_exit_on_disconnect(conn, FALSE);
244
245         handle->conn = conn;
246         handle->callback = callback;
247         handle->user_data = data;
248         handle->signal = strdup(signal);
249
250         INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);
251
252         return handle;
253
254
255 error_n_return:
256         if (handle)
257                 free(handle);
258
259         dbus_error_free(&err);
260
261         if (conn)
262                 dbus_connection_close(conn);
263
264         return NULL;
265 }
266
267 void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
268 {
269         DBusError err;
270         char rule[1024] = {'\0', };
271
272         if (!handle) {
273                 ERR("handle is NULL");
274                 return;
275         }
276
277         dbus_error_init(&err);
278
279         dbus_connection_remove_filter(handle->conn,
280                         _minictrl_signal_filter, handle);
281
282         snprintf(rule, 1024,
283                 "path='%s',type='signal',interface='%s',member='%s'",
284                 MINICTRL_DBUS_PATH,
285                 MINICTRL_DBUS_INTERFACE,
286                 handle->signal);
287
288         dbus_bus_remove_match(handle->conn, rule, &err);
289         if (dbus_error_is_set(&err)) {
290                 ERR("fail to dbus_bus_remove_match : %s", err.message);
291                 dbus_error_free(&err);
292         }
293
294         dbus_connection_close(handle->conn);
295
296         free(handle->signal);
297         free(handle);
298
299         return;
300 }
301