diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2015-03-25 15:47:22 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2015-03-25 15:47:22 -0500 |
commit | 4bf70c7afae44d4005d25105690304b4e9e361f3 (patch) | |
tree | cf35ba35345305a0934326202e8a4b8e6ec69208 | |
parent | 9e29a4fdb99b722c43af1268312db68fff17fc60 (diff) | |
download | ayatana-indicator-datetime-4bf70c7afae44d4005d25105690304b4e9e361f3.tar.gz ayatana-indicator-datetime-4bf70c7afae44d4005d25105690304b4e9e361f3.tar.bz2 ayatana-indicator-datetime-4bf70c7afae44d4005d25105690304b4e9e361f3.zip |
the previous commit made the Awake API too complicated... simplifying.
-rw-r--r-- | include/notifications/awake.h | 8 | ||||
-rw-r--r-- | src/awake.cpp | 176 | ||||
-rw-r--r-- | src/snap.cpp | 26 |
3 files changed, 87 insertions, 123 deletions
diff --git a/include/notifications/awake.h b/include/notifications/awake.h index 9c9d434..ad04cca 100644 --- a/include/notifications/awake.h +++ b/include/notifications/awake.h @@ -36,15 +36,9 @@ namespace notifications { class Awake { public: - explicit Awake(const std::string& app_name); + explicit Awake(const std::string& app_name, unsigned int display_on_seconds); ~Awake(); - /** Whether or not the display is forced on */ - bool display_forced() const; - - /** Set the display force on/off */ - void set_display_forced(bool forced); - private: class Impl; std::unique_ptr<Impl> impl; diff --git a/src/awake.cpp b/src/awake.cpp index fda4099..a37d185 100644 --- a/src/awake.cpp +++ b/src/awake.cpp @@ -22,7 +22,7 @@ #include <gio/gio.h> -#include <set> +#include <limits> namespace unity { namespace indicator { @@ -36,9 +36,10 @@ class Awake::Impl { public: - Impl(const std::string& app_name): + Impl(const std::string& app_name, unsigned int display_on_seconds): m_app_name(app_name), - m_cancellable(g_cancellable_new()) + m_cancellable(g_cancellable_new()), + m_display_on_seconds(display_on_seconds) { g_bus_get(G_BUS_TYPE_SYSTEM, m_cancellable, on_system_bus_ready, this); } @@ -48,27 +49,20 @@ public: g_cancellable_cancel (m_cancellable); g_object_unref (m_cancellable); + if (m_display_on_timer) + { + g_source_remove (m_display_on_timer); + m_display_on_timer = 0; + } + if (m_system_bus != nullptr) { unforce_awake (); - unforce_screen (); + remove_display_on_request (); g_object_unref (m_system_bus); } } - bool display_forced() const - { - return !m_screen_cookies.empty(); - } - - void set_display_forced(bool force) - { - if (force) - force_screen(); - else if (!force) - unforce_screen(); - } - private: static void on_system_bus_ready (GObject *, @@ -108,7 +102,19 @@ private: on_force_awake_response, self); - self->force_screen(); + // ask unity-system-compositor to turn on the screen + g_dbus_connection_call (system_bus, + BUS_SCREEN_NAME, + BUS_SCREEN_PATH, + BUS_SCREEN_INTERFACE, + "keepDisplayOn", + nullptr, + G_VARIANT_TYPE("(i)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + self->m_cancellable, + on_keep_display_on_response, + self); g_object_unref (system_bus); } @@ -147,56 +153,9 @@ private: } } - void unforce_awake () - { - g_return_if_fail (G_IS_DBUS_CONNECTION(m_system_bus)); - - if (m_awake_cookie != nullptr) - { - g_dbus_connection_call (m_system_bus, - BUS_POWERD_NAME, - BUS_POWERD_PATH, - BUS_POWERD_INTERFACE, - "clearSysState", - g_variant_new("(s)", m_awake_cookie), - nullptr, - G_DBUS_CALL_FLAGS_NONE, - -1, - nullptr, - nullptr, - nullptr); - - g_clear_pointer (&m_awake_cookie, g_free); - } - } - - /*** - **** FORCE DISPLAY ON - ***/ - - void force_screen() - { - g_return_if_fail (G_IS_DBUS_CONNECTION(m_system_bus)); - g_return_if_fail (m_screen_cookies.empty()); - - // ask unity-system-compositor to turn on the screen - g_dbus_connection_call (m_system_bus, - BUS_SCREEN_NAME, - BUS_SCREEN_PATH, - BUS_SCREEN_INTERFACE, - "keepDisplayOn", - nullptr, - G_VARIANT_TYPE("(i)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - m_cancellable, - on_force_screen_response, - this); - } - - static void on_force_screen_response (GObject * connection, - GAsyncResult * res, - gpointer gself) + static void on_keep_display_on_response (GObject * connection, + GAsyncResult * res, + gpointer gself) { GError * error; GVariant * args; @@ -219,54 +178,92 @@ private: { auto self = static_cast<Impl*>(gself); - gint32 cookie = 0; - g_variant_get (args, "(i)", &cookie); + self->m_display_on_cookie = NO_DISPLAY_ON_COOKIE; + g_variant_get (args, "(i)", &self->m_display_on_cookie); + g_debug ("m_display_on_cookie is now '%d'", self->m_display_on_cookie); + + self->m_display_on_timer = g_timeout_add_seconds (self->m_display_on_seconds, + on_display_on_timer, + gself); + g_variant_unref (args); - g_debug ("got screen_cookie '%d'", (int)cookie); - self->m_screen_cookies.insert (cookie); } } - void unforce_screen () + static gboolean on_display_on_timer (gpointer gself) + { + auto self = static_cast<Impl*>(gself); + + self->m_display_on_timer = 0; + self->remove_display_on_request(); + + return G_SOURCE_REMOVE; + } + + + void unforce_awake () { g_return_if_fail (G_IS_DBUS_CONNECTION(m_system_bus)); - for (auto cookie : m_screen_cookies) + if (m_awake_cookie != nullptr) + { + g_dbus_connection_call (m_system_bus, + BUS_POWERD_NAME, + BUS_POWERD_PATH, + BUS_POWERD_INTERFACE, + "clearSysState", + g_variant_new("(s)", m_awake_cookie), + nullptr, + G_DBUS_CALL_FLAGS_NONE, + -1, + nullptr, + nullptr, + nullptr); + + g_clear_pointer (&m_awake_cookie, g_free); + } + } + + void remove_display_on_request () + { + g_return_if_fail (G_IS_DBUS_CONNECTION(m_system_bus)); + + if (m_display_on_cookie != NO_DISPLAY_ON_COOKIE) { g_dbus_connection_call (m_system_bus, BUS_SCREEN_NAME, BUS_SCREEN_PATH, BUS_SCREEN_INTERFACE, "removeDisplayOnRequest", - g_variant_new("(i)", cookie), + g_variant_new("(i)", m_display_on_cookie), nullptr, G_DBUS_CALL_FLAGS_NONE, -1, nullptr, nullptr, nullptr); - } - m_screen_cookies.clear(); + m_display_on_cookie = NO_DISPLAY_ON_COOKIE; + } } const std::string m_app_name; GCancellable * m_cancellable = nullptr; GDBusConnection * m_system_bus = nullptr; char * m_awake_cookie = nullptr; + int32_t m_display_on_cookie = NO_DISPLAY_ON_COOKIE; + const guint m_display_on_seconds; + guint m_display_on_timer; - // In general there will only be one cookie in this container... - // holding N cookies is a safeguard in case the client calls force_screen() - // while there's aleady a pending keepDisplayOn call on the bus. - std::set<int32_t> m_screen_cookies; + static constexpr int32_t NO_DISPLAY_ON_COOKIE { std::numeric_limits<int32_t>::min() }; }; /*** **** ***/ -Awake::Awake(const std::string& app_name): - impl(new Impl (app_name)) +Awake::Awake(const std::string& app_name, unsigned int display_on_seconds): + impl(new Impl (app_name, display_on_seconds)) { } @@ -274,19 +271,6 @@ Awake::~Awake() { } -bool -Awake::display_forced() const -{ - return impl->display_forced(); -} - -void -Awake::set_display_forced(bool forced) -{ - impl->set_display_forced(forced); -} - - /*** **** ***/ diff --git a/src/snap.cpp b/src/snap.cpp index 80f200d..4329eca 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -88,9 +88,11 @@ public: intervention and shouldn't loop the sound. */ const bool interactive = appointment.is_ubuntu_alarm() && m_engine->supports_actions(); - // Force the system to stay awake. - // In a clean world this would be a shared_ptr, but we use it as a GSourceFunc arg... - auto awake = new uin::Awake(m_engine->app_name()); + // keep the screen on for the first part of the alarm; + // keep the system awake for the duration of the alarm + constexpr unsigned int display_on_seconds = 60; + auto awake = std::make_shared<uin::Awake>(m_engine->app_name(), + display_on_seconds); // calendar events are muted in silent mode; alarm clocks never are std::shared_ptr<uin::Sound> sound; @@ -138,31 +140,15 @@ public: b.add_action ("snooze", _("Snooze")); } - // Don't keep the screen on forever. - // If the alarm keeps going on and on, release our screen-on lock - // after awhile to prevent unneccesary battery drain - constexpr int screen_awake_seconds { 60 }; - auto awake_timeout_func = [](gpointer a){ - static_cast<uin::Awake*>(a)->set_display_forced(false); - return G_SOURCE_REMOVE; - }; - auto awake_timeout_tag = g_timeout_add_seconds (screen_awake_seconds, - awake_timeout_func, - awake); - // add 'sound', 'haptic', and 'awake' objects to the capture so // they stay alive until the closed callback is called; i.e., // for the lifespan of the notficiation - b.set_closed_callback([appointment, snooze, ok, sound, haptic, - awake, awake_timeout_tag] + b.set_closed_callback([appointment, snooze, ok, sound, awake, haptic] (const std::string& action){ if (action == "snooze") snooze(appointment); else ok(appointment); - - g_source_remove(awake_timeout_tag); - delete awake; }); const auto key = m_engine->show(b); |