aboutsummaryrefslogtreecommitdiff
path: root/src/wakeup-timer-powerd.cpp
blob: e66c94ce35699ce559f754bf3506a5f16fac8ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * Copyright 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include <datetime/clock.h>
#include <datetime/wakeup-timer-powerd.h>

#include <notifications/dbus-shared.h> // BUS_POWERD_NAME

#include <gio/gio.h>

#include <memory> // std::shared_ptr

namespace ayatana {
namespace indicator {
namespace datetime {

/***
****
***/

class PowerdWakeupTimer::Impl
{
public:

    Impl(const std::shared_ptr<Clock>& clock):
        m_clock(clock),
        m_cancellable(g_cancellable_new())
    {
        g_bus_get(G_BUS_TYPE_SYSTEM, m_cancellable, on_bus_ready, this);
    }

    ~Impl()
    {
        clear_current_cookie();

        g_cancellable_cancel(m_cancellable);
        g_clear_object(&m_cancellable);

        if (m_sub_id)
            g_dbus_connection_signal_unsubscribe(m_bus.get(), m_sub_id);

        if (m_watch_tag)
            g_bus_unwatch_name(m_watch_tag);
    }

    void set_wakeup_time(const DateTime& d)
    {
        m_wakeup_time = d;
        update_cookie();
    }

    core::Signal<>& timeout() { return m_timeout; }

private:

    void emit_timeout() { return m_timeout(); }

    static void on_bus_ready(GObject      * /*unused*/,
                             GAsyncResult * res,
                             gpointer       gself)
    {
        GError * error;
        GDBusConnection * bus;

        error = nullptr;
        bus = g_bus_get_finish(res, &error);
        if (bus == nullptr)
        {
            if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                g_warning("%s Couldn't get system bus: %s", G_STRLOC, error->message);
        }
        else
        {
            static_cast<Impl*>(gself)->init_bus(bus);
        }

        g_clear_object(&bus);
        g_clear_error(&error);
    }

    void init_bus(GDBusConnection* connection)
    {
        m_bus.reset(G_DBUS_CONNECTION(g_object_ref(G_OBJECT(connection))),
                    [](GDBusConnection *c){g_object_unref(G_OBJECT(c));});

        m_sub_id = g_dbus_connection_signal_subscribe(m_bus.get(),
                                                      BUS_POWERD_NAME,
                                                      BUS_POWERD_INTERFACE,
                                                      "Wakeup",
                                                      BUS_POWERD_PATH,
                                                      nullptr,
                                                      G_DBUS_SIGNAL_FLAGS_NONE,
                                                      on_wakeup_signal,
                                                      this, // userdata
                                                      nullptr); // userdata free

        m_watch_tag = g_bus_watch_name_on_connection(m_bus.get(),
                                                     BUS_POWERD_NAME,
                                                     G_BUS_NAME_WATCHER_FLAGS_NONE,
                                                     on_name_appeared_static,
                                                     nullptr, // name-vanished,
                                                     this, // userdata
                                                     nullptr); // userdata free
    }

    static void
    on_wakeup_signal(GDBusConnection * /*connection*/,
                     const gchar     * sender_name,
                     const gchar     * /*object_path*/,
                     const gchar     * /*interface_name*/,
                     const gchar     * /*signal_name*/,
                     GVariant        * /*parameters*/,
                     gpointer          gself)
    {
        g_debug("%s %s broadcast a hw wakeup signal", G_STRLOC, sender_name);
        static_cast<Impl*>(gself)->emit_timeout();
    }

    static void
    on_name_appeared_static(GDBusConnection * /*connection*/,
                            const gchar     * name,
                            const gchar     * name_owner,
                            gpointer          gself)
    {
        g_debug("%s %s owns %s now; let's ask for a new cookie", G_STRLOC, name, name_owner);
        static_cast<Impl*>(gself)->update_cookie();
    }

    /***
    ****  requestWakeup
    ***/

    void update_cookie()
    {
        if (!m_bus)
            return;

        // if we've already got a cookie, clear it
        clear_current_cookie();
        g_warn_if_fail(m_cookie.empty());

        // get a new cookie, if necessary
        if (m_wakeup_time.is_set())
        {
            g_debug("%s calling %s::requestWakeup(%s)",
                    G_STRLOC, BUS_POWERD_NAME,
                    m_wakeup_time.format("%F %T").c_str());

            auto args = g_variant_new("(st)",
                                      GETTEXT_PACKAGE,
                                      uint64_t(m_wakeup_time.to_unix()));

            g_dbus_connection_call(m_bus.get(),
                                   BUS_POWERD_NAME,
                                   BUS_POWERD_PATH,
                                   BUS_POWERD_INTERFACE,
                                   "requestWakeup", // method_name
                                   args,
                                   G_VARIANT_TYPE("(s)"), // reply_type
                                   G_DBUS_CALL_FLAGS_NONE,
                                   -1, // use default timeout
                                   m_cancellable,
                                   on_request_wakeup_done,
                                   this);
        }
    }

    static void on_request_wakeup_done(GObject      * o,
                                       GAsyncResult * res,
                                       gpointer       gself)
    {
        GError * error;
        GVariant * ret;

        error = nullptr;
        ret = g_dbus_connection_call_finish(G_DBUS_CONNECTION(o), res, &error);
        if (ret == nullptr)
        {
            /* powerd isn't on the desktop, but we don't need hardware wakeups there
               anyway... so no need to warn on SERVICE_UNKNOWN */
            if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
                !g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN))
            {
                g_warning("%s Could not set hardware wakeup: %s", G_STRLOC, error->message);
            }
        }
        else
        {
            const char* s = nullptr;
            g_variant_get(ret, "(&s)", &s);
            g_debug("%s %s::requestWakeup() sent cookie %s",
                    G_STRLOC, BUS_POWERD_NAME, s);

            auto& cookie = static_cast<Impl*>(gself)->m_cookie;
            if (s != nullptr)
                cookie = s;
            else
                cookie.clear();
        }

        // cleanup
        g_clear_pointer(&ret, g_variant_unref);
        g_clear_error(&error);
    }

    /***
    ****  clearWakeup
    ***/

    void clear_current_cookie()
    {
        if (!m_cookie.empty())
        {
            g_debug("%s calling %s::clearWakeup(%s)",
                    G_STRLOC, BUS_POWERD_NAME, m_cookie.c_str());

            g_dbus_connection_call(m_bus.get(),
                                   BUS_POWERD_NAME,
                                   BUS_POWERD_PATH,
                                   BUS_POWERD_INTERFACE,
                                   "clearWakeup", // method_name
                                   g_variant_new("(s)", m_cookie.c_str()),
                                   nullptr, // no response type
                                   G_DBUS_CALL_FLAGS_NONE,
                                   -1, // use default timeout
                                   nullptr, // cancellable
                                   on_clear_wakeup_done,
                                   nullptr);
            m_cookie.clear();
        }
    }

    // this is only here to log errors
    static void on_clear_wakeup_done(GObject      * o,
                                     GAsyncResult * res,
                                     gpointer       /*unused*/)
    {
        GError * error;
        GVariant * ret;

        error = nullptr;
        ret = g_dbus_connection_call_finish(G_DBUS_CONNECTION(o), res, &error);
        if (!ret && !g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
            g_warning("%s Couldn't clear hardware wakeup: %s", G_STRLOC, error->message);

        // cleanup
        g_clear_pointer(&ret, g_variant_unref);
        g_clear_error(&error);
    }

    /***
    ****
    ***/

    core::Signal<> m_timeout;
    const std::shared_ptr<Clock>& m_clock;
    DateTime m_wakeup_time;

    std::shared_ptr<GDBusConnection> m_bus;
    GCancellable * m_cancellable = nullptr;
    std::string m_cookie;
    guint m_watch_tag = 0;
    guint m_sub_id = 0;
};

/***
****
***/

PowerdWakeupTimer::PowerdWakeupTimer(const std::shared_ptr<Clock>& clock):
    p(new Impl(clock))
{
}

PowerdWakeupTimer::~PowerdWakeupTimer()
{
}

void PowerdWakeupTimer::set_wakeup_time(const DateTime& d)
{
    p->set_wakeup_time(d);
}

core::Signal<>& PowerdWakeupTimer::timeout()
{
    return p->timeout();
}

/***
****
***/

} // namespace datetime
} // namespace indicator
} // namespace ayatana