2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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>
19 #include <dbus/dbus-glib-lowlevel.h>
21 #include "minicontrol-error.h"
22 #include "minicontrol-type.h"
23 #include "minicontrol-internal.h"
24 #include "minicontrol-log.h"
26 #define MINICTRL_DBUS_PATH "/org/tizen/minicontrol"
27 #define MINICTRL_DBUS_INTERFACE "org.tizen.minicontrol.signal"
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"
35 struct _minictrl_sig_handle {
37 void (*callback) (void *data, DBusMessage *msg);
42 int _minictrl_viewer_req_message_send(void)
44 DBusConnection *connection = NULL;
45 DBusMessage *message = NULL;
48 int ret = MINICONTROL_ERROR_NONE;
50 dbus_error_init(&err);
51 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
53 ERR("Fail to dbus_bus_get : %s", err.message);
54 ret = MINICONTROL_ERROR_DBUS;
55 goto release_n_return;
58 message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
59 MINICTRL_DBUS_INTERFACE,
60 MINICTRL_DBUS_SIG_RUNNING_REQ);
62 ERR("fail to create dbus message");
63 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
64 goto release_n_return;
67 dbus_ret = dbus_connection_send(connection, message, NULL);
69 ERR("fail to send dbus viewer req message");
70 ret = MINICONTROL_ERROR_DBUS;
71 goto release_n_return;
74 dbus_connection_flush(connection);
77 dbus_error_free(&err);
80 dbus_message_unref(message);
83 dbus_connection_unref(connection);
88 int _minictrl_provider_proc_send(int type)
91 DBusConnection* conn = NULL;
92 DBusMessage* msg = NULL;
95 dbus_uint32_t serial = 0;
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;
102 ERR("Check unsupported type : %d", type);
105 DBG("_minictrl_provider_proc_send : %d, %d", pid, type);
106 dbus_error_init(&err);
107 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
109 ERR("Fail to dbus_bus_get : %s", err.message);
110 ret = MINICONTROL_ERROR_DBUS;
111 goto release_n_return;
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
117 ERR("ERR Could not create DBus Message");
118 goto release_n_return;
120 ret = dbus_message_append_args(msg,
121 DBUS_TYPE_STRING, &typestr,
122 DBUS_TYPE_INT32, &pid,
124 if (!dbus_connection_send(conn, msg, &serial))
125 ERR("ERR send DBus Message");
126 dbus_connection_flush(conn);
128 dbus_error_free(&err);
131 dbus_message_unref(msg);
134 dbus_connection_unref(conn);
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)
144 DBusConnection *connection = NULL;
145 DBusMessage *message = NULL;
147 dbus_bool_t dbus_ret;
148 int ret = MINICONTROL_ERROR_NONE;
151 ERR("sig_name is NULL, invaild parameter");
152 return MINICONTROL_ERROR_INVALID_PARAMETER;
156 ERR("svr_name is NULL, invaild parameter");
157 return MINICONTROL_ERROR_INVALID_PARAMETER;
160 dbus_error_init(&err);
161 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
163 ERR("Fail to dbus_bus_get : %s", err.message);
164 ret = MINICONTROL_ERROR_DBUS;
165 goto release_n_return;
168 message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
169 MINICTRL_DBUS_INTERFACE,
173 ERR("fail to create dbus message");
174 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
175 goto release_n_return;
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,
185 ERR("fail to append name to dbus message : %s", svr_name);
186 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
187 goto release_n_return;
190 dbus_ret = dbus_connection_send(connection, message, NULL);
192 ERR("fail to send dbus message : %s", svr_name);
193 ret = MINICONTROL_ERROR_DBUS;
194 goto release_n_return;
197 dbus_connection_flush(connection);
200 dbus_error_free(&err);
203 dbus_message_unref(message);
206 dbus_connection_unref(connection);
211 static DBusHandlerResult _minictrl_signal_filter(DBusConnection *conn,
212 DBusMessage *msg, void *user_data)
214 minictrl_sig_handle *handle = NULL;
215 const char *interface;
220 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
224 dbus_error_init(&error);
226 interface = dbus_message_get_interface(msg);
228 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
230 if (strcmp(MINICTRL_DBUS_INTERFACE, interface))
231 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
233 ret = dbus_message_is_signal(msg, interface, handle->signal);
235 DBG("this msg is not signal");
236 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
239 if (handle->callback)
240 handle->callback(handle->user_data, msg);
242 return DBUS_HANDLER_RESULT_HANDLED;
246 minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
247 void (*callback) (void *data, DBusMessage *msg),
250 minictrl_sig_handle *handle = NULL;
252 DBusConnection *conn = NULL;
253 char rule[1024] = {'\0', };
256 ERR("signal is NULL");
265 handle = malloc(sizeof(minictrl_sig_handle));
267 ERR("fail to alloc handle");
271 dbus_error_init(&err);
272 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
274 ERR("fail to get bus : %s", err.message);
278 dbus_connection_setup_with_g_main(conn, NULL);
280 "path='%s',type='signal',interface='%s',member='%s'",
282 MINICTRL_DBUS_INTERFACE,
285 dbus_bus_add_match(conn, rule, &err);
286 if (dbus_error_is_set(&err)) {
287 ERR("fail to dbus_bus_remove_match : %s",
292 if (dbus_connection_add_filter(conn, _minictrl_signal_filter,
293 handle, NULL) == FALSE) {
294 ERR("fail to dbus_connection_add_filter : %s",
299 dbus_connection_set_exit_on_disconnect(conn, FALSE);
302 handle->callback = callback;
303 handle->user_data = data;
304 handle->signal = strdup(signal);
306 INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);
315 dbus_error_free(&err);
318 dbus_connection_close(conn);
323 void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
326 char rule[1024] = {'\0', };
329 ERR("handle is NULL");
333 dbus_error_init(&err);
335 dbus_connection_remove_filter(handle->conn,
336 _minictrl_signal_filter, handle);
339 "path='%s',type='signal',interface='%s',member='%s'",
341 MINICTRL_DBUS_INTERFACE,
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);
350 dbus_connection_close(handle->conn);
352 free(handle->signal);