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://floralicense.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 struct _minictrl_sig_handle {
31 void (*callback) (void *data, DBusMessage *msg);
36 int _minictrl_viewer_req_message_send(void)
38 DBusConnection *connection = NULL;
39 DBusMessage *message = NULL;
42 int ret = MINICONTROL_ERROR_NONE;
44 dbus_error_init(&err);
45 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
47 ERR("Fail to dbus_bus_get : %s", err.message);
48 ret = MINICONTROL_ERROR_DBUS;
49 goto release_n_return;
52 message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
53 MINICTRL_DBUS_INTERFACE,
54 MINICTRL_DBUS_SIG_RUNNING_REQ);
56 ERR("fail to create dbus message");
57 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
58 goto release_n_return;
61 dbus_ret = dbus_connection_send(connection, message, NULL);
63 ERR("fail to send dbus viewer req message");
64 ret = MINICONTROL_ERROR_DBUS;
65 goto release_n_return;
68 dbus_connection_flush(connection);
71 dbus_error_free(&err);
74 dbus_message_unref(message);
77 dbus_connection_unref(connection);
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)
86 DBusConnection *connection = NULL;
87 DBusMessage *message = NULL;
90 int ret = MINICONTROL_ERROR_NONE;
93 ERR("sig_name is NULL, invaild parameter");
94 return MINICONTROL_ERROR_INVALID_PARAMETER;
98 ERR("svr_name is NULL, invaild parameter");
99 return MINICONTROL_ERROR_INVALID_PARAMETER;
102 dbus_error_init(&err);
103 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
105 ERR("Fail to dbus_bus_get : %s", err.message);
106 ret = MINICONTROL_ERROR_DBUS;
107 goto release_n_return;
110 message = dbus_message_new_signal(MINICTRL_DBUS_PATH,
111 MINICTRL_DBUS_INTERFACE,
115 ERR("fail to create dbus message");
116 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
117 goto release_n_return;
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,
127 ERR("fail to append name to dbus message : %s", svr_name);
128 ret = MINICONTROL_ERROR_OUT_OF_MEMORY;
129 goto release_n_return;
132 dbus_ret = dbus_connection_send(connection, message, NULL);
134 ERR("fail to send dbus message : %s", svr_name);
135 ret = MINICONTROL_ERROR_DBUS;
136 goto release_n_return;
139 dbus_connection_flush(connection);
140 INFO("[%s][%s] size-[%ux%u] priority[%u]",
141 sig_name, svr_name, witdh, height, priority);
144 dbus_error_free(&err);
147 dbus_message_unref(message);
150 dbus_connection_unref(connection);
155 static DBusHandlerResult _minictrl_signal_filter(DBusConnection *conn,
156 DBusMessage *msg, void *user_data)
158 minictrl_sig_handle *handle = NULL;
159 const char *interface;
164 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
168 dbus_error_init(&error);
170 interface = dbus_message_get_interface(msg);
172 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
174 if (strcmp(MINICTRL_DBUS_INTERFACE, interface))
175 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
177 ret = dbus_message_is_signal(msg, interface, handle->signal);
179 DBG("this msg is not signal");
180 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
183 if (handle->callback)
184 handle->callback(handle->user_data, msg);
186 return DBUS_HANDLER_RESULT_HANDLED;
190 minictrl_sig_handle *_minictrl_dbus_sig_handle_attach(const char *signal,
191 void (*callback) (void *data, DBusMessage *msg),
194 minictrl_sig_handle *handle = NULL;
196 DBusConnection *conn = NULL;
197 char rule[1024] = {'\0', };
200 ERR("signal is NULL");
209 handle = malloc(sizeof(minictrl_sig_handle));
211 ERR("fail to alloc handle");
215 dbus_error_init(&err);
216 conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
218 ERR("fail to get bus : %s", err.message);
222 dbus_connection_setup_with_g_main(conn, NULL);
224 "path='%s',type='signal',interface='%s',member='%s'",
226 MINICTRL_DBUS_INTERFACE,
229 dbus_bus_add_match(conn, rule, &err);
230 if (dbus_error_is_set(&err)) {
231 ERR("fail to dbus_bus_remove_match : %s",
236 if (dbus_connection_add_filter(conn, _minictrl_signal_filter,
237 handle, NULL) == FALSE) {
238 ERR("fail to dbus_connection_add_filter : %s",
243 dbus_connection_set_exit_on_disconnect(conn, FALSE);
246 handle->callback = callback;
247 handle->user_data = data;
248 handle->signal = strdup(signal);
250 INFO("success to attach signal[%s]-[%p, %p]", signal, callback, data);
259 dbus_error_free(&err);
262 dbus_connection_close(conn);
267 void _minictrl_dbus_sig_handle_dettach(minictrl_sig_handle *handle)
270 char rule[1024] = {'\0', };
273 ERR("handle is NULL");
277 dbus_error_init(&err);
279 dbus_connection_remove_filter(handle->conn,
280 _minictrl_signal_filter, handle);
283 "path='%s',type='signal',interface='%s',member='%s'",
285 MINICTRL_DBUS_INTERFACE,
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);
294 dbus_connection_close(handle->conn);
296 free(handle->signal);