4 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
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 <sensor_log.h>
21 #include "sensor_event_listener.h"
22 #include "dbus_listener.h"
24 #define HANDLE_GERROR(Err) \
27 _E("GError: %s", Err->message); \
33 #define ROTATION_DBUS_DEST "org.tizen.system.coord"
34 #define ROTATION_DBUS_OBJ_PATH "/Org/Tizen/System/Coord/Rotation"
35 #define ROTATION_DBUS_IFACE "org.tizen.system.coord.rotation"
36 #define ROTATION_DBUS_SIGNAL "Changed"
37 #define ROTATION_DBUS_METHOD "Degree"
39 static void rotation_signal_cb(GDBusConnection *conn, const gchar *sender,
40 const gchar *obj_path, const gchar *iface, const gchar *signal_name,
41 GVariant *param, gpointer user_data)
44 g_variant_get(param, "(i)", &state);
45 sensor_event_listener::get_instance().set_display_rotation(state);
48 static void rotation_read_cb(GObject *source_object, GAsyncResult *res, gpointer user_data)
51 GDBusConnection *conn = G_DBUS_CONNECTION(source_object);
52 GVariant *result = g_dbus_connection_call_finish(conn, res, &error);
54 ret_if(result == NULL);
57 g_variant_get(result, "(i)", &state);
58 g_variant_unref(result);
59 sensor_event_listener::get_instance().set_display_rotation(state);
62 dbus_listener::dbus_listener()
65 #ifndef GLIB_VERSION_2_36
70 dbus_listener::~dbus_listener()
75 void dbus_listener::init(void)
77 static dbus_listener listener;
78 static bool done = false;
84 void dbus_listener::connect(void)
88 gchar *addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &gerr);
90 retm_if(addr == NULL, "Getting address failed");
92 g_dbus_connection_new_for_address(addr,
93 (GDBusConnectionFlags)(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION),
94 NULL, NULL, on_connection_ready, this);
98 void dbus_listener::on_connection_ready(GObject *source_object, GAsyncResult *res, gpointer user_data)
101 dbus_listener *listener = static_cast<dbus_listener*>(user_data);
103 GDBusConnection *conn = g_dbus_connection_new_finish(res, &gerr);
106 retm_if(conn == NULL, "Connection failed");
107 _D("Dbus connection established: %s", g_dbus_connection_get_unique_name(conn));
109 listener->m_connection = conn;
110 listener->get_current_state();
111 listener->subscribe();
114 void dbus_listener::disconnect(void)
116 ret_if(!m_connection);
117 g_dbus_connection_close_sync(m_connection, NULL, NULL);
118 g_object_unref(m_connection);
121 void dbus_listener::subscribe(void)
123 /* Diplay rotation */
124 g_dbus_connection_signal_subscribe(m_connection,
125 ROTATION_DBUS_DEST, ROTATION_DBUS_IFACE, ROTATION_DBUS_SIGNAL, ROTATION_DBUS_OBJ_PATH,
126 NULL, G_DBUS_SIGNAL_FLAGS_NONE, (GDBusSignalCallback)rotation_signal_cb, NULL, NULL);
129 void dbus_listener::get_current_state(void)
131 /* Display rotation */
132 g_dbus_connection_call(m_connection,
133 ROTATION_DBUS_DEST, ROTATION_DBUS_OBJ_PATH, ROTATION_DBUS_IFACE, ROTATION_DBUS_METHOD,
134 NULL, NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, (GAsyncReadyCallback)rotation_read_cb, NULL);