Initialize Tizen 2.3
[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.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.tizenopensource.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 #define PROC_DBUS_OBJECT        "/Org/Tizen/ResourceD/Process"
30 #define PROC_DBUS_INTERFACE     "org.tizen.resourced.process"
31 #define PROC_DBUS_METHOD        "ProcExclude"
32 #define PROC_DBUS_EXCLUDE       "exclude"
33 #define PROC_DBUS_INCLUDE       "include"
34
35 struct _minictrl_sig_handle {
36         DBusConnection *conn;
37         void (*callback) (void *data, DBusMessage *msg);
38         void *user_data;
39         char *signal;
40 };
41
42 int _minictrl_viewer_req_message_send(void)
43 {
44         DBusConnection *connection = NULL;
45         DBusMessage *message = NULL;
46         DBusError err;
47         dbus_bool_t dbus_ret;
48         int ret = MINICONTROL_ERROR_NONE;
49
50         dbus_error_init(&err);
51         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
52         if (!connection) {
53                 ERR("Fail to dbus_bus_get : %s", err.message);
54                 ret = MINICONTROL_ERROR_DBUS;
55                 goto release_n_return;
56         }
57
58         message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
59                                 MINICTRL_DBUS_INTERFACE,
60                                 MINICTRL_DBUS_SIG_RUNNING_REQ);
61         if (!message) {
62                 ERR("fail to create dbus message");
63                 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
64                 goto release_n_return;
65         }
66
67         dbus_ret = dbus_connection_send(connection, message, NULL);
68         if (!dbus_ret) {
69                 ERR("fail to send dbus viewer req message");
70                 ret = MINICONTROL_ERROR_DBUS;
71                 goto release_n_return;
72         }
73
74         dbus_connection_flush(connection);
75
76 release_n_return:
77         dbus_error_free(&err);
78
79         if (message)
80                 dbus_message_unref(message);
81
82         if (connection)
83                 dbus_connection_unref(connection);
84
85         return ret;
86 }
87
88 int _minictrl_provider_proc_send(int type)
89 {
90         DBusError err;
91         DBusConnection* conn = NULL;
92         DBusMessage* msg = NULL;
93         int ret = -1;
94         int pid = getpid();
95         dbus_uint32_t serial = 0;
96         char *typestr;
97         if (type == MINICONTROL_DBUS_PROC_EXCLUDE)
98                 typestr = PROC_DBUS_EXCLUDE;
99         else if  (type == MINICONTROL_DBUS_PROC_INCLUDE)
100                 typestr = PROC_DBUS_INCLUDE;
101         else {
102                 ERR("Check unsupported type : %d", type);
103                 return ret;
104         }
105         DBG("_minictrl_provider_proc_send : %d, %d", pid, type);
106         dbus_error_init(&err);
107         conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
108         if (!conn) {
109                 ERR("Fail to dbus_bus_get : %s", err.message);
110                 ret = MINICONTROL_ERROR_DBUS;
111                 goto release_n_return;
112         }
113         msg = dbus_message_new_signal(PROC_DBUS_OBJECT, // object name of the signal
114                             PROC_DBUS_INTERFACE, // interface name of the signal
115                             PROC_DBUS_METHOD); // name of the signal
116         if (!msg) {
117                 ERR("ERR Could not create DBus Message");
118                 goto release_n_return;
119         }
120         ret = dbus_message_append_args(msg,
121                             DBUS_TYPE_STRING, &typestr,
122                             DBUS_TYPE_INT32, &pid,
123                             DBUS_TYPE_INVALID);
124         if (!dbus_connection_send(conn, msg, &serial))
125                 ERR("ERR send DBus Message");
126         dbus_connection_flush(conn);
127 release_n_return:
128         dbus_error_free(&err);
129
130         if (msg)
131                 dbus_message_unref(msg);
132
133         if (conn)
134                 dbus_connection_unref(conn);
135
136         return ret;
137
138 }
139
140 int _minictrl_provider_message_send(const char *sig_name, const char *svr_name,
141                                 unsigned int witdh, unsigned int height,
142                                 minicontrol_priority_e priority)
143 {
144         DBusConnection *connection = NULL;
145         DBusMessage *message = NULL;
146         DBusError err;
147         dbus_bool_t dbus_ret;
148         int ret = MINICONTROL_ERROR_NONE;
149
150         if (!sig_name) {
151                 ERR("sig_name is NULL, invaild parameter");
152                 return MINICONTROL_ERROR_INVALID_PARAMETER;
153         }
154
155         if (!svr_name) {
156                 ERR("svr_name is NULL, invaild parameter");
157                 return MINICONTROL_ERROR_INVALID_PARAMETER;
158         }
159
160         dbus_error_init(&err);
161         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
162         if (!connection) {
163                 ERR("Fail to dbus_bus_get : %s", err.message);
164                 ret = MINICONTROL_ERROR_DBUS;
165                 goto release_n_return;
166         }
167
168         message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
169                                 MINICTRL_DBUS_INTERFACE,
170                                 sig_name);
171
172         if (!message) {
173                 ERR("fail to create dbus message");
174                 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
175                 goto release_n_return;
176         }
177
178         dbus_ret = dbus_message_append_args(message,
179                         DBUS_TYPE_STRING, &svr_name,
180                         DBUS_TYPE_UINT32, &witdh,
181                         DBUS_TYPE_UINT32, &height,
182                         DBUS_TYPE_UINT32, &priority,
183                         DBUS_TYPE_INVALID);
184         if (!dbus_ret) {
185                 ERR("fail to append name to dbus message : %s", svr_name);
186                 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
187                 goto release_n_return;
188         }
189
190         dbus_ret = dbus_connection_send(connection, message, NULL);
191         if (!dbus_ret) {
192                 ERR("fail to send dbus message : %s", svr_name);
193                 ret = MINICONTROL_ERROR_DBUS;
194                 goto release_n_return;
195         }
196
197         dbus_connection_flush(connection);
198
199 release_n_return:
200         dbus_error_free(&err);
201
202         if (message)
203                 dbus_message_unref(message);
204
205         if (connection)
206                 dbus_connection_unref(connection);
207
208         return ret;
209 }
210
211 static DBusHandlerResult _minictrl_signal_filter(DBusConnection *conn,
212                 DBusMessage *msg, void *user_data)
213 {
214         minictrl_sig_handle *handle = NULL;
215         const char *interface;
216         DBusError error;
217         dbus_bool_t ret;
218
219         if (!user_data)
220                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
221
222         handle = user_data;
223
224         dbus_error_init(&error);
225
226         interface = dbus_message_get_interface(msg);
227         if (!interface)
228                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
229
230         if (strcmp(MINICTRL_DBUS_INTERFACE, interface))
231                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
232
233         ret = dbus_message_is_signal(msg, interface, handle->signal);
234         if (!ret) {
235                 DBG("this msg is not signal");
236                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
237         }
238
239         if (handle->callback)
240                 handle->callback(handle->user_data, msg);
241
242         return DBUS_HANDLER_RESULT_HANDLED;
243 }
244
245
246 minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
247                                 void (*callback) (void *data, DBusMessage *msg),
248                                 void *data)
249 {
250         minictrl_sig_handle *handle = NULL;
251         DBusError err;
252         DBusConnection *conn = NULL;
253         char rule[1024] = {'\0', };
254
255         if (!signal) {
256                 ERR("signal is NULL");
257                 return NULL;
258         }
259
260         if (!callback) {
261                 ERR("call is NULL");
262                 return NULL;
263         }
264
265         handle = malloc(sizeof(minictrl_sig_handle));
266         if (!handle) {
267                 ERR("fail to alloc handle");
268                 return NULL;
269         }
270
271         dbus_error_init(&err);
272         conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
273         if (!conn) {
274                 ERR("fail to get bus : %s", err.message);
275                 goto error_n_return;
276         }
277
278         dbus_connection_setup_with_g_main(conn, NULL);
279         snprintf(rule, 1024,
280                 "path='%s',type='signal',interface='%s',member='%s'",
281                 MINICTRL_DBUS_PATH,
282                 MINICTRL_DBUS_INTERFACE,
283                 signal);
284
285         dbus_bus_add_match(conn, rule, &err);
286         if (dbus_error_is_set(&err)) {
287                 ERR("fail to dbus_bus_remove_match : %s",
288                                 err.message);
289                 goto error_n_return;
290         }
291
292         if (dbus_connection_add_filter(conn, _minictrl_signal_filter,
293                                         handle, NULL) == FALSE) {
294                 ERR("fail to dbus_connection_add_filter : %s",
295                                 err.message);
296                 goto error_n_return;
297         }
298
299         dbus_connection_set_exit_on_disconnect(conn, FALSE);
300
301         handle->conn = conn;
302         handle->callback = callback;
303         handle->user_data = data;
304         handle->signal = strdup(signal);
305
306         INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);
307
308         return handle;
309
310
311 error_n_return:
312         if (handle)
313                 free(handle);
314
315         dbus_error_free(&err);
316
317         if (conn)
318                 dbus_connection_close(conn);
319
320         return NULL;
321 }
322
323 void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
324 {
325         DBusError err;
326         char rule[1024] = {'\0', };
327
328         if (!handle) {
329                 ERR("handle is NULL");
330                 return;
331         }
332
333         dbus_error_init(&err);
334
335         dbus_connection_remove_filter(handle->conn,
336                         _minictrl_signal_filter, handle);
337
338         snprintf(rule, 1024,
339                 "path='%s',type='signal',interface='%s',member='%s'",
340                 MINICTRL_DBUS_PATH,
341                 MINICTRL_DBUS_INTERFACE,
342                 handle->signal);
343
344         dbus_bus_remove_match(handle->conn, rule, &err);
345         if (dbus_error_is_set(&err)) {
346                 ERR("fail to dbus_bus_remove_match : %s", err.message);
347                 dbus_error_free(&err);
348         }
349
350         dbus_connection_close(handle->conn);
351
352         free(handle->signal);
353         free(handle);
354
355         return;
356 }
357