2
* Copyright 2014 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include <notifications/awake.h>
21
#include <notifications/dbus-shared.h>
29
namespace notifications {
39
Impl(const std::string& app_name):
41
m_cancellable(g_cancellable_new())
43
g_bus_get(G_BUS_TYPE_SYSTEM, m_cancellable, on_system_bus_ready, this);
48
g_cancellable_cancel (m_cancellable);
49
g_object_unref (m_cancellable);
51
if (m_system_bus != nullptr)
55
g_object_unref (m_system_bus);
61
static void on_system_bus_ready (GObject *,
66
GDBusConnection * system_bus;
69
system_bus = g_bus_get_finish (res, &error);
72
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
73
g_warning ("Unable to get bus: %s", error->message);
77
else if (system_bus != nullptr)
79
auto self = static_cast<Impl*>(gself);
81
self->m_system_bus = G_DBUS_CONNECTION (g_object_ref (system_bus));
83
// ask powerd to keep the system awake
84
static constexpr int32_t POWERD_SYS_STATE_ACTIVE = 1;
85
g_dbus_connection_call (system_bus,
90
g_variant_new("(si)", self->m_app_name.c_str(), POWERD_SYS_STATE_ACTIVE),
91
G_VARIANT_TYPE("(s)"),
92
G_DBUS_CALL_FLAGS_NONE,
95
on_force_awake_response,
98
// ask unity-system-compositor to turn on the screen
99
g_dbus_connection_call (system_bus,
102
BUS_SCREEN_INTERFACE,
105
G_VARIANT_TYPE("(i)"),
106
G_DBUS_CALL_FLAGS_NONE,
109
on_force_screen_response,
112
g_object_unref (system_bus);
116
static void on_force_awake_response (GObject * connection,
124
args = g_dbus_connection_call_finish (G_DBUS_CONNECTION(connection),
127
if (error != nullptr)
129
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
130
!g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN))
132
g_warning ("Unable to inhibit sleep: %s", error->message);
135
g_error_free (error);
139
auto self = static_cast<Impl*>(gself);
141
g_clear_pointer (&self->m_awake_cookie, g_free);
142
g_variant_get (args, "(s)", &self->m_awake_cookie);
143
g_debug ("m_awake_cookie is now '%s'", self->m_awake_cookie);
145
g_variant_unref (args);
149
static void on_force_screen_response (GObject * connection,
157
args = g_dbus_connection_call_finish (G_DBUS_CONNECTION(connection),
160
if (error != nullptr)
162
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
163
!g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN))
165
g_warning ("Unable to turn on the screen: %s", error->message);
168
g_error_free (error);
172
auto self = static_cast<Impl*>(gself);
174
self->m_screen_cookie = NO_SCREEN_COOKIE;
175
g_variant_get (args, "(i)", &self->m_screen_cookie);
176
g_debug ("m_screen_cookie is now '%d'", self->m_screen_cookie);
178
g_variant_unref (args);
182
void unforce_awake ()
184
g_return_if_fail (G_IS_DBUS_CONNECTION(m_system_bus));
186
if (m_awake_cookie != nullptr)
188
g_dbus_connection_call (m_system_bus,
191
BUS_POWERD_INTERFACE,
193
g_variant_new("(s)", m_awake_cookie),
195
G_DBUS_CALL_FLAGS_NONE,
201
g_clear_pointer (&m_awake_cookie, g_free);
205
void unforce_screen ()
207
g_return_if_fail (G_IS_DBUS_CONNECTION(m_system_bus));
209
if (m_screen_cookie != NO_SCREEN_COOKIE)
211
g_dbus_connection_call (m_system_bus,
214
BUS_SCREEN_INTERFACE,
215
"removeDisplayOnRequest",
216
g_variant_new("(i)", m_screen_cookie),
218
G_DBUS_CALL_FLAGS_NONE,
224
m_screen_cookie = NO_SCREEN_COOKIE;
228
const std::string m_app_name;
229
GCancellable * m_cancellable = nullptr;
230
GDBusConnection * m_system_bus = nullptr;
231
char * m_awake_cookie = nullptr;
232
int32_t m_screen_cookie = NO_SCREEN_COOKIE;
234
static constexpr int32_t NO_SCREEN_COOKIE { std::numeric_limits<int32_t>::min() };
241
Awake::Awake(const std::string& app_name):
242
impl(new Impl (app_name))
254
} // namespace datetime
255
} // namespace indicator