4 * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #include "popup-common.h"
21 #include <dd-display.h>
24 #define DBUS_REPLY_TIMEOUT (-1)
27 static E_DBus_Connection *edbus_conn = NULL;
29 /* Terminate window */
30 static Eina_Bool exit_idler_cb(void *data)
33 return ECORE_CALLBACK_CANCEL;
36 void window_terminate(void)
38 if (ecore_idler_add(exit_idler_cb, NULL))
45 void play_feedback(int type, int pattern)
49 ret = feedback_initialize();
50 if (ret != FEEDBACK_ERROR_NONE) {
51 _E("Cannot initialize feedback");
56 case FEEDBACK_TYPE_SOUND:
57 case FEEDBACK_TYPE_VIBRATION:
58 ret = feedback_play_type(type, pattern);
60 case FEEDBACK_TYPE_NONE:
61 ret = feedback_play(pattern);
64 _E("Play type is unknown");
67 if (ret != FEEDBACK_ERROR_NONE)
68 _E("Cannot play feedback: %d", pattern);
70 ret = feedback_deinitialize();
71 if (ret != FEEDBACK_ERROR_NONE)
72 _E("Cannot deinitialize feedback");
75 void init_multi_feedback(void)
79 ret = feedback_initialize();
80 if (ret != FEEDBACK_ERROR_NONE)
81 _E("Cannot initialize feedback");
84 void play_multi_feedback(int pattern)
88 ret = feedback_play(pattern);
89 if (ret != FEEDBACK_ERROR_NONE)
90 _E("Cannot play feedback: %d", pattern);
93 void stop_multi_feedback(void)
97 ret = feedback_deinitialize();
98 if (ret != FEEDBACK_ERROR_NONE)
99 _E("Cannot deinitialize feedback");
102 static void *thread_feedback(void* data)
104 long type = (long)data;
106 type = FEEDBACK_PATTERN_LOWBATT; /* Warning */
107 play_feedback(FEEDBACK_TYPE_NONE, type);
111 static void *thread_display(void* data)
113 if (display_change_state(LCD_NORMAL) < 0)
114 _E("FAIL: display_change_state()");
118 static void start_thread(void *(*operation)(void *), void *data)
123 ret = pthread_create(&th, NULL, operation, data);
125 _E("Failed to create pthread(%d)", ret);
132 void change_display_state(void)
134 start_thread(thread_display, NULL);
137 void notify_feedback(long pattern)
139 start_thread(thread_feedback, (void *)pattern);
143 int set_dbus_connection(void)
151 while (e_dbus_init() == 0) {
152 if (retry++ >= RETRY_MAX)
156 edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
158 _E("Failed to get dbus bus");
166 E_DBus_Connection *get_dbus_connection(void)
171 void unset_dbus_connection(void)
174 e_dbus_connection_close(edbus_conn);
180 static int append_variant(DBusMessageIter *iter, const char *sig, char *param[])
189 for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) {
192 iValue = atoi(param[i]);
193 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &iValue);
196 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, ¶m[i]);
205 int broadcast_dbus_signal(const char *path, const char *interface,
206 const char *name, const char *sig, char *param[])
208 E_DBus_Connection *conn = NULL;
210 DBusMessageIter iter;
214 if (!path || !interface || !name)
217 conn = get_dbus_connection();
219 _E("Failed to get dbus connection");
223 msg = dbus_message_new_signal(path, interface, name);
225 _E("FAIL: dbus_message_new_signal()");
229 dbus_message_iter_init_append(msg, &iter);
230 ret = append_variant(&iter, sig, param);
232 _E("append_variant error(%d)", ret);
236 pc = e_dbus_message_send(conn, msg, NULL, -1, NULL);
238 _E("FAIL: e_dbus_message_send()");
246 dbus_message_unref(msg);
250 int popup_dbus_method_sync(const char *dest, const char *path,
251 const char *interface, const char *method,
252 const char *sig, char *param[])
254 DBusConnection *conn = NULL;
255 DBusMessage *msg = NULL;
256 DBusMessageIter iter;
257 DBusMessage *reply = NULL;
261 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
263 _E("dbus_bus_get error");
267 msg = dbus_message_new_method_call(dest, path, interface, method);
269 _E("dbus_message_new_method_call(%s:%s-%s)",
270 path, interface, method);
275 dbus_message_iter_init_append(msg, &iter);
276 ret = append_variant(&iter, sig, param);
278 _E("append_variant error(%d)", ret);
282 dbus_error_init(&err);
284 reply = dbus_connection_send_with_reply_and_block(conn,
285 msg, DBUS_REPLY_TIMEOUT, &err);
287 _E("dbus_connection_send error(%s:%s)", err.name, err.message);
288 dbus_error_free(&err);
293 ret = dbus_message_get_args(reply, &err,
294 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
296 _E("no message : [%s:%s]", err.name, err.message);
297 dbus_error_free(&err);
306 dbus_message_unref(msg);
308 dbus_message_unref(reply);
310 dbus_connection_unref(conn);
314 void unregister_dbus_signal_handler(E_DBus_Signal_Handler *handler)
316 E_DBus_Connection *conn;
321 conn = get_dbus_connection();
323 _E("Failed to get dbus connection");
327 e_dbus_signal_handler_del(conn, handler);
330 int register_dbus_signal_handler(
331 E_DBus_Signal_Handler **handler,
335 void (*signal_cb)(void *data, DBusMessage *msg),
338 E_DBus_Connection *conn;
339 E_DBus_Signal_Handler *h;
341 if (!handler || !path || !iface || !name || !signal_cb)
344 conn = get_dbus_connection();
346 _E("Failed to get dbus connection");
350 h = e_dbus_signal_handler_add(conn, NULL,
351 path, iface, name, signal_cb, data);
353 return -ECONNREFUSED;