From 559d185dd7d51e56fbd8246970ef520d3edd18ae Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 30 Jul 2014 16:01:03 -0500 Subject: initial draft of haptic feedback when alarms play --- src/haptic.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/haptic.cpp (limited to 'src/haptic.cpp') diff --git a/src/haptic.cpp b/src/haptic.cpp new file mode 100644 index 0000000..c5a20df --- /dev/null +++ b/src/haptic.cpp @@ -0,0 +1,92 @@ +/* + * 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 . + * + * Authors: + * Charles Kerr + */ + +#include + +#include + +#include + +namespace unity { +namespace indicator { +namespace notifications { + +/*** +**** +***/ + +class Haptic::Impl +{ +public: + + Impl(const Mode& mode): + m_mode(mode), + m_sensor(ua_sensors_haptic_new()) + { + if (m_sensor == nullptr) + g_warning ("Haptic device unavailable"); + else + m_tag = g_timeout_add_seconds (1, on_timeout, this); + } + + ~Impl() + { + if (m_tag) + g_source_remove(m_tag); + } + +private: + + static gboolean on_timeout (gpointer gself) + { + static_cast(gself)->vibrate_now(); + return G_SOURCE_CONTINUE; + } + + void vibrate_now() + { + const uint32_t msec = 1500; + ua_sensors_haptic_vibrate_once (m_sensor, msec); + } + + const Mode m_mode; + UASensorsHaptic * m_sensor = nullptr; + guint m_tag = 0; +}; + +/*** +**** +***/ + +Haptic::Haptic(const Mode& mode): + impl(new Impl (mode)) +{ +} + +Haptic::~Haptic() +{ +} + +/*** +**** +***/ + +} // namespace datetime +} // namespace indicator +} // namespace unity -- cgit v1.2.3 From e4b663ebd5be78bd9fb9802e54b050a7b2a984bf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 30 Jul 2014 16:34:02 -0500 Subject: in haptic.cpp, make sure to enable the sensor by calling ua_sensors_haptic_enable() --- src/haptic.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/haptic.cpp') diff --git a/src/haptic.cpp b/src/haptic.cpp index c5a20df..663e919 100644 --- a/src/haptic.cpp +++ b/src/haptic.cpp @@ -40,13 +40,21 @@ public: m_sensor(ua_sensors_haptic_new()) { if (m_sensor == nullptr) + { g_warning ("Haptic device unavailable"); + } else - m_tag = g_timeout_add_seconds (1, on_timeout, this); + { + ua_sensors_haptic_enable(m_sensor); + m_tag = g_timeout_add_seconds (2, on_timeout, this); + } } ~Impl() { + if (m_sensor != nullptr) + ua_sensors_haptic_enable(m_sensor); + if (m_tag) g_source_remove(m_tag); } @@ -61,8 +69,8 @@ private: void vibrate_now() { - const uint32_t msec = 1500; - ua_sensors_haptic_vibrate_once (m_sensor, msec); + const uint32_t msec = 1000; + (void) ua_sensors_haptic_vibrate_once (m_sensor, msec); } const Mode m_mode; -- cgit v1.2.3 From 5809e107390f59ad11565e8b8644c191f8430dd3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Jul 2014 11:44:16 -0500 Subject: in haptic.cpp, start vibrating immediately --- src/haptic.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/haptic.cpp') diff --git a/src/haptic.cpp b/src/haptic.cpp index 663e919..c7517d1 100644 --- a/src/haptic.cpp +++ b/src/haptic.cpp @@ -47,6 +47,7 @@ public: { ua_sensors_haptic_enable(m_sensor); m_tag = g_timeout_add_seconds (2, on_timeout, this); + on_timeout (this); } } -- cgit v1.2.3 From ad789d53f4c4bb059eb52c97c53d720c7ccb7a3e Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Jul 2014 11:59:12 -0500 Subject: in Haptic::Impl::~Impl(), call ua_sensors_haptic_disable(). h/t Antti --- src/haptic.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/haptic.cpp') diff --git a/src/haptic.cpp b/src/haptic.cpp index c7517d1..637c6f5 100644 --- a/src/haptic.cpp +++ b/src/haptic.cpp @@ -54,7 +54,7 @@ public: ~Impl() { if (m_sensor != nullptr) - ua_sensors_haptic_enable(m_sensor); + ua_sensors_haptic_disable(m_sensor); if (m_tag) g_source_remove(m_tag); -- cgit v1.2.3 From 0c8faf27ea83c3996c68b0ab02c0b4ed824b4129 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Jul 2014 12:01:04 -0500 Subject: in haptic.cpp, better comments --- src/haptic.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/haptic.cpp') diff --git a/src/haptic.cpp b/src/haptic.cpp index 637c6f5..90c75f7 100644 --- a/src/haptic.cpp +++ b/src/haptic.cpp @@ -45,6 +45,9 @@ public: } else { + /* We only support one vibrate mode for now: an on/off pulse at + one-second intervals. So, set a timer to go off every 2 seconds + that vibrates for one second. */ ua_sensors_haptic_enable(m_sensor); m_tag = g_timeout_add_seconds (2, on_timeout, this); on_timeout (this); -- cgit v1.2.3 From af1b645de9b8116ead5ad72583f51afe28350818 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Jul 2014 15:22:47 -0500 Subject: drop the ubuntu-application-api middleman and call usensorsd directly: ua_sensors_haptic_new() crashes on desktop and ua_sensors_haptic_vibrate_once() makes blocking dbus calls. --- CMakeLists.txt | 3 +- debian/control | 1 - include/notifications/dbus-shared.h | 4 ++ src/haptic.cpp | 80 ++++++++++++++++++++++++++----------- 4 files changed, 62 insertions(+), 26 deletions(-) (limited to 'src/haptic.cpp') diff --git a/CMakeLists.txt b/CMakeLists.txt index 3bc7e81..9b4987e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,8 +41,7 @@ pkg_check_modules (SERVICE_DEPS REQUIRED gstreamer-1.0>=1.2 libnotify>=0.7.6 url-dispatcher-1>=1 - properties-cpp>=0.0.1 - ubuntu-platform-api>=2.2.0) + properties-cpp>=0.0.1) include_directories (SYSTEM ${SERVICE_DEPS_INCLUDE_DIRS}) CHECK_INCLUDE_FILE(ubuntu/hardware/alarm.h HAVE_UBUNTU_HW_ALARM_H) diff --git a/debian/control b/debian/control index 15b981a..9e7133e 100644 --- a/debian/control +++ b/debian/control @@ -24,7 +24,6 @@ Build-Depends: cmake, libproperties-cpp-dev, libubuntu-platform-hardware-api-headers [armhf i386 amd64], libubuntu-platform-hardware-api-dev [armhf i386 amd64], - libubuntu-application-api-dev [armhf i386 amd64], libdbustest1-dev, locales, Standards-Version: 3.9.3 diff --git a/include/notifications/dbus-shared.h b/include/notifications/dbus-shared.h index 7738cb7..af714e7 100644 --- a/include/notifications/dbus-shared.h +++ b/include/notifications/dbus-shared.h @@ -29,4 +29,8 @@ #define BUS_POWERD_PATH "/com/canonical/powerd" #define BUS_POWERD_INTERFACE "com.canonical.powerd" +#define BUS_HAPTIC_NAME "com.canonical.usensord" +#define BUS_HAPTIC_PATH "/com/canonical/usensord/haptic" +#define BUS_HAPTIC_INTERFACE "com.canonical.usensord.haptic" + #endif /* INDICATOR_NOTIFICATIONS_DBUS_SHARED_H */ diff --git a/src/haptic.cpp b/src/haptic.cpp index 90c75f7..2b1ee21 100644 --- a/src/haptic.cpp +++ b/src/haptic.cpp @@ -17,11 +17,10 @@ * Charles Kerr */ +#include #include -#include - -#include +#include namespace unity { namespace indicator { @@ -37,34 +36,58 @@ public: Impl(const Mode& mode): m_mode(mode), - m_sensor(ua_sensors_haptic_new()) + m_cancellable(g_cancellable_new()) { - if (m_sensor == nullptr) - { - g_warning ("Haptic device unavailable"); - } - else - { - /* We only support one vibrate mode for now: an on/off pulse at - one-second intervals. So, set a timer to go off every 2 seconds - that vibrates for one second. */ - ua_sensors_haptic_enable(m_sensor); - m_tag = g_timeout_add_seconds (2, on_timeout, this); - on_timeout (this); - } + g_bus_get (G_BUS_TYPE_SESSION, m_cancellable, on_bus_ready, this); } ~Impl() { - if (m_sensor != nullptr) - ua_sensors_haptic_disable(m_sensor); - if (m_tag) g_source_remove(m_tag); + + g_cancellable_cancel (m_cancellable); + g_object_unref (m_cancellable); + + g_clear_object (&m_bus); } private: + static void on_bus_ready (GObject*, GAsyncResult* res, gpointer gself) + { + GError * error; + GDBusConnection * bus; + + error = nullptr; + bus = g_bus_get_finish (res, &error); + if (error != nullptr) + { + if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning ("Unable to get bus: %s", error->message); + + g_error_free (error); + } + else if (bus != nullptr) + { + auto self = static_cast(gself); + + self->m_bus = G_DBUS_CONNECTION (g_object_ref (bus)); + self->start_vibrating(); + + g_object_unref (bus); + } + } + + void start_vibrating() + { + /* We only support one vibrate mode for now: an on/off pulse at + one-second intervals. So set a looping 2-second timer that asks + the phone to vibrate for one second. */ + m_tag = g_timeout_add_seconds (2, on_timeout, this); + on_timeout (this); + } + static gboolean on_timeout (gpointer gself) { static_cast(gself)->vibrate_now(); @@ -73,12 +96,23 @@ private: void vibrate_now() { - const uint32_t msec = 1000; - (void) ua_sensors_haptic_vibrate_once (m_sensor, msec); + g_dbus_connection_call (m_bus, + BUS_HAPTIC_NAME, + BUS_HAPTIC_PATH, + BUS_HAPTIC_INTERFACE, + "Vibrate", + g_variant_new("(u)", 1000u), + nullptr, + G_DBUS_CALL_FLAGS_NONE, + -1, + m_cancellable, + nullptr, + nullptr); } const Mode m_mode; - UASensorsHaptic * m_sensor = nullptr; + GCancellable * m_cancellable = nullptr; + GDBusConnection * m_bus = nullptr; guint m_tag = 0; }; -- cgit v1.2.3 From 43e0fb826aa3a5acfd8937438989231804b4c400 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Jul 2014 16:25:26 -0500 Subject: in haptic.cpp, better support for vibration patterns --- src/haptic.cpp | 57 ++++++++++++++++++++++++++++++++++++++++------------- tests/test-snap.cpp | 8 ++++---- 2 files changed, 47 insertions(+), 18 deletions(-) (limited to 'src/haptic.cpp') diff --git a/src/haptic.cpp b/src/haptic.cpp index 2b1ee21..a27c334 100644 --- a/src/haptic.cpp +++ b/src/haptic.cpp @@ -22,6 +22,8 @@ #include +#include + namespace unity { namespace indicator { namespace notifications { @@ -81,38 +83,65 @@ private: void start_vibrating() { - /* We only support one vibrate mode for now: an on/off pulse at - one-second intervals. So set a looping 2-second timer that asks - the phone to vibrate for one second. */ - m_tag = g_timeout_add_seconds (2, on_timeout, this); + g_return_if_fail (m_tag == 0); + + switch (m_mode) + { + case MODE_PULSE: // the only mode currently supported... :) + // one second on, one second off. + m_vibrate_pattern_msec = std::vector({1000, 1000}); + break; + } + + // Set up a loop so that the pattern keeps repeating. + // NB: VibratePattern takes a repeat arg, but we avoid it because + // there's no way to cancel a pattern once it's started... + // The phone would keep vibrating long after the alarm was dismissed! + // So we stick to a short pattern and handle the repeat loop manually. + guint interval_msec = 0; + for (const auto& msec : m_vibrate_pattern_msec) + interval_msec += msec; + m_tag = g_timeout_add (interval_msec, on_timeout, this); on_timeout (this); } static gboolean on_timeout (gpointer gself) { - static_cast(gself)->vibrate_now(); - return G_SOURCE_CONTINUE; - } - - void vibrate_now() - { - g_dbus_connection_call (m_bus, + auto self = static_cast(gself); + + // build the pattern array of uint32s + GVariantBuilder builder; + g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); + for (const auto& msec : self->m_vibrate_pattern_msec) + g_variant_builder_add_value (&builder, g_variant_new_uint32(msec)); + auto pattern_array = g_variant_builder_end (&builder); + + // build the argument list + g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE); + g_variant_builder_add_value (&builder, pattern_array); + g_variant_builder_add_value (&builder, g_variant_new_uint32(1u)); + auto args = g_variant_builder_end (&builder); + + g_dbus_connection_call (self->m_bus, BUS_HAPTIC_NAME, BUS_HAPTIC_PATH, BUS_HAPTIC_INTERFACE, - "Vibrate", - g_variant_new("(u)", 1000u), + "VibratePattern", + args, nullptr, G_DBUS_CALL_FLAGS_NONE, -1, - m_cancellable, + self->m_cancellable, nullptr, nullptr); + + return G_SOURCE_CONTINUE; } const Mode m_mode; GCancellable * m_cancellable = nullptr; GDBusConnection * m_bus = nullptr; + std::vector m_vibrate_pattern_msec; guint m_tag = 0; }; diff --git a/tests/test-snap.cpp b/tests/test-snap.cpp index 018d6f9..06e0a80 100644 --- a/tests/test-snap.cpp +++ b/tests/test-snap.cpp @@ -56,7 +56,7 @@ private: protected: - static constexpr char const * HAPTIC_METHOD_VIBRATE {"Vibrate"}; + static constexpr char const * HAPTIC_METHOD_VIBRATE_PATTERN {"VibratePattern"}; static constexpr int SCREEN_COOKIE {8675309}; static constexpr char const * SCREEN_METHOD_KEEP_DISPLAY_ON {"keepDisplayOn"}; @@ -253,8 +253,8 @@ protected: dbus_test_dbus_mock_object_add_method(haptic_mock, haptic_obj, - HAPTIC_METHOD_VIBRATE, - G_VARIANT_TYPE("(u)"), + HAPTIC_METHOD_VIBRATE_PATTERN, + G_VARIANT_TYPE("(auu)"), nullptr, "", &error); @@ -419,7 +419,7 @@ TEST_F(SnapFixture, InhibitSleep) // confirm that haptic feedback got called EXPECT_TRUE (dbus_test_dbus_mock_object_check_method_call (haptic_mock, haptic_obj, - HAPTIC_METHOD_VIBRATE, + HAPTIC_METHOD_VIBRATE_PATTERN, nullptr, &error)); -- cgit v1.2.3 From d129112ad9ce04fad979af95b5b5cd4fd87f5b2b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 31 Jul 2014 19:52:42 -0500 Subject: in Haptic, make the looping logic easier to read. --- include/notifications/haptic.h | 2 +- src/haptic.cpp | 51 ++++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 25 deletions(-) (limited to 'src/haptic.cpp') diff --git a/include/notifications/haptic.h b/include/notifications/haptic.h index d82d1c9..bfb5679 100644 --- a/include/notifications/haptic.h +++ b/include/notifications/haptic.h @@ -31,7 +31,7 @@ namespace notifications { ***/ /** - * A class that forces the screen display on and inhibits sleep + * Tries to emit haptic feedback to match the user-specified mode. */ class Haptic { diff --git a/src/haptic.cpp b/src/haptic.cpp index a27c334..54a7d49 100644 --- a/src/haptic.cpp +++ b/src/haptic.cpp @@ -22,6 +22,7 @@ #include +#include #include namespace unity { @@ -88,60 +89,62 @@ private: switch (m_mode) { case MODE_PULSE: // the only mode currently supported... :) + // one second on, one second off. - m_vibrate_pattern_msec = std::vector({1000, 1000}); + m_pattern = std::vector({1000u, 1000u}); break; } - // Set up a loop so that the pattern keeps repeating. - // NB: VibratePattern takes a repeat arg, but we avoid it because - // there's no way to cancel a pattern once it's started... - // The phone would keep vibrating long after the alarm was dismissed! - // So we stick to a short pattern and handle the repeat loop manually. - guint interval_msec = 0; - for (const auto& msec : m_vibrate_pattern_msec) - interval_msec += msec; - m_tag = g_timeout_add (interval_msec, on_timeout, this); - on_timeout (this); + // Set up a loop to keep repeating the pattern + auto msec = std::accumulate(m_pattern.begin(), m_pattern.end(), 0u); + m_tag = g_timeout_add(msec, call_vibrate_pattern_static, this); + call_vibrate_pattern(); } - static gboolean on_timeout (gpointer gself) + static gboolean call_vibrate_pattern_static (gpointer gself) { - auto self = static_cast(gself); + static_cast(gself)->call_vibrate_pattern(); + return G_SOURCE_CONTINUE; + } - // build the pattern array of uint32s + void call_vibrate_pattern() + { + // build the vibrate pattern GVariantBuilder builder; g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY); - for (const auto& msec : self->m_vibrate_pattern_msec) + for (const auto& msec : m_pattern) g_variant_builder_add_value (&builder, g_variant_new_uint32(msec)); auto pattern_array = g_variant_builder_end (&builder); - // build the argument list + /* Use a repeat_count of 1 here because we handle looping ourselves. + NB: VibratePattern could do it for us, but doesn't let us cancel + a running loop -- we could keep vibrating even after "this" was + destructed */ + auto repeat_count = g_variant_new_uint32 (1u); + g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE); g_variant_builder_add_value (&builder, pattern_array); - g_variant_builder_add_value (&builder, g_variant_new_uint32(1u)); - auto args = g_variant_builder_end (&builder); + g_variant_builder_add_value (&builder, repeat_count); + auto vibrate_pattern_args = g_variant_builder_end (&builder); - g_dbus_connection_call (self->m_bus, + g_dbus_connection_call (m_bus, BUS_HAPTIC_NAME, BUS_HAPTIC_PATH, BUS_HAPTIC_INTERFACE, "VibratePattern", - args, + vibrate_pattern_args, nullptr, G_DBUS_CALL_FLAGS_NONE, -1, - self->m_cancellable, + m_cancellable, nullptr, nullptr); - - return G_SOURCE_CONTINUE; } const Mode m_mode; GCancellable * m_cancellable = nullptr; GDBusConnection * m_bus = nullptr; - std::vector m_vibrate_pattern_msec; + std::vector m_pattern; guint m_tag = 0; }; -- cgit v1.2.3