[gnome-settings-daemon] power: Move sound loop to helpers



commit 30237695a77bd56fc967779ef4b8446a875e44f7
Author: Bastien Nocera <hadess hadess net>
Date:   Fri Jan 25 20:32:56 2013 +0100

    power: Move sound loop to helpers

 plugins/power/gpm-common.c        |   41 +++++++++++++++++++++++++++++++
 plugins/power/gpm-common.h        |    4 +++
 plugins/power/gsd-power-manager.c |   49 +++----------------------------------
 3 files changed, 49 insertions(+), 45 deletions(-)
---
diff --git a/plugins/power/gpm-common.c b/plugins/power/gpm-common.c
index 860d9e1..5c8a9f3 100644
--- a/plugins/power/gpm-common.c
+++ b/plugins/power/gpm-common.c
@@ -29,6 +29,7 @@
 #include <glib/gi18n.h>
 #include <gdk/gdkx.h>
 #include <X11/extensions/XTest.h>
+#include <canberra-gtk.h>
 
 #define GNOME_DESKTOP_USE_UNSTABLE_API
 #include <libgnome-desktop/gnome-rr.h>
@@ -38,6 +39,8 @@
 #include "gsd-backlight-linux.h"
 
 #define XSCREENSAVER_WATCHDOG_TIMEOUT           120 /* seconds */
+#define UPS_SOUND_LOOP_ID                        99
+#define GSD_POWER_MANAGER_CRITICAL_ALERT_TIMEOUT  5 /* seconds */
 
 /* take a discrete value with offset and convert to percentage */
 int
@@ -1670,3 +1673,41 @@ external_monitor_is_connected (GnomeRRScreen *screen)
 
         return FALSE;
 }
+
+static void
+play_sound (void)
+{
+        ca_context_play (ca_gtk_context_get (), UPS_SOUND_LOOP_ID,
+                         CA_PROP_EVENT_ID, "battery-caution",
+                         CA_PROP_EVENT_DESCRIPTION, _("Battery is critically low"), NULL);
+}
+
+static gboolean
+play_loop_timeout_cb (gpointer user_data)
+{
+        play_sound ();
+        return TRUE;
+}
+
+void
+play_loop_start (guint *id)
+{
+        if (*id != 0)
+                return;
+
+        *id = g_timeout_add_seconds (GSD_POWER_MANAGER_CRITICAL_ALERT_TIMEOUT,
+                                     (GSourceFunc) play_loop_timeout_cb,
+                                     NULL);
+        play_sound ();
+}
+
+void
+play_loop_stop (guint *id)
+{
+        if (*id == 0)
+                return;
+
+        ca_context_cancel (ca_gtk_context_get (), UPS_SOUND_LOOP_ID);
+        g_source_remove (*id);
+        *id = 0;
+}
diff --git a/plugins/power/gpm-common.h b/plugins/power/gpm-common.h
index b5f544c..6d321c2 100644
--- a/plugins/power/gpm-common.h
+++ b/plugins/power/gpm-common.h
@@ -71,6 +71,10 @@ int              backlight_set_abs                      (GnomeRRScreen *rr_scree
 /* RandR helpers */
 gboolean         external_monitor_is_connected          (GnomeRRScreen *screen);
 
+/* Sound helpers */
+void             play_loop_start                        (guint *id);
+void             play_loop_stop                         (guint *id);
+
 G_END_DECLS
 
 #endif  /* __GPMCOMMON_H */
diff --git a/plugins/power/gsd-power-manager.c b/plugins/power/gsd-power-manager.c
index bfe5334..a607720 100644
--- a/plugins/power/gsd-power-manager.c
+++ b/plugins/power/gsd-power-manager.c
@@ -76,7 +76,6 @@
 #define GSD_POWER_MANAGER_NOTIFY_TIMEOUT_SHORT          10 * 1000 /* ms */
 #define GSD_POWER_MANAGER_NOTIFY_TIMEOUT_LONG           30 * 1000 /* ms */
 
-#define GSD_POWER_MANAGER_CRITICAL_ALERT_TIMEOUT        5 /* seconds */
 #define GSD_POWER_MANAGER_RECALL_DELAY                  30 /* seconds */
 #define GSD_POWER_MANAGER_LID_CLOSE_SAFETY_TIMEOUT      30 /* seconds */
 
@@ -87,8 +86,6 @@
 /* Keep this in sync with gnome-shell */
 #define SCREENSAVER_FADE_TIME                           10 /* seconds */
 
-#define UPS_SOUND_LOOP_ID 99
-
 static const gchar introspection_xml[] =
 "<node>"
 "  <interface name='org.gnome.SettingsDaemon.Power'>"
@@ -254,44 +251,6 @@ gsd_power_manager_error_quark (void)
 }
 
 static void
-play_sound (void)
-{
-        ca_context_play (ca_gtk_context_get (), UPS_SOUND_LOOP_ID,
-                         CA_PROP_EVENT_ID, "battery-caution",
-                         CA_PROP_EVENT_DESCRIPTION, _("Battery is critically low"), NULL);
-}
-
-static gboolean
-play_loop_timeout_cb (gpointer user_data)
-{
-        play_sound ();
-        return TRUE;
-}
-
-static void
-play_loop_stop (GsdPowerManager *manager)
-{
-        if (manager->priv->critical_alert_timeout_id == 0)
-                return;
-
-        ca_context_cancel (ca_gtk_context_get (), UPS_SOUND_LOOP_ID);
-        g_source_remove (manager->priv->critical_alert_timeout_id);
-        manager->priv->critical_alert_timeout_id = 0;
-}
-
-static void
-play_loop_start (GsdPowerManager *manager)
-{
-        if (manager->priv->critical_alert_timeout_id != 0)
-                return;
-
-        manager->priv->critical_alert_timeout_id = g_timeout_add_seconds (GSD_POWER_MANAGER_CRITICAL_ALERT_TIMEOUT,
-                                                                          (GSourceFunc) play_loop_timeout_cb,
-                                                                          NULL);
-        play_sound ();
-}
-
-static void
 notify_close_if_showing (NotifyNotification *notification)
 {
         if (notification == NULL)
@@ -1296,7 +1255,7 @@ manager_critical_action_do (GsdPowerManager *manager,
         GsdPowerActionType action_type;
 
         /* stop playing the alert as it's too late to do anything now */
-        play_loop_stop (manager);
+        play_loop_stop (&manager->priv->critical_alert_timeout_id);
 
         action_type = manager_critical_action_get (manager, is_ups);
         do_power_action_type (manager, action_type);
@@ -1647,7 +1606,7 @@ engine_charge_critical (GsdPowerManager *manager, UpDevice *device)
         case UP_DEVICE_KIND_BATTERY:
         case UP_DEVICE_KIND_UPS:
                 g_debug ("critical charge level reached, starting sound loop");
-                play_loop_start (manager);
+                play_loop_start (&manager->priv->critical_alert_timeout_id);
                 break;
 
         default:
@@ -2313,7 +2272,7 @@ up_client_changed_cb (UpClient *client, GsdPowerManager *manager)
 
         if (!up_client_get_on_battery (client)) {
             /* if we are playing a critical charge sound loop on AC, stop it */
-            play_loop_stop (manager);
+            play_loop_stop (&manager->priv->critical_alert_timeout_id);
             notify_close_if_showing (manager->priv->notification_low);
             main_battery_or_ups_low_changed (manager, FALSE);
         }
@@ -3554,7 +3513,7 @@ gsd_power_manager_stop (GsdPowerManager *manager)
         g_clear_object (&manager->priv->session_presence_proxy);
         g_clear_object (&manager->priv->screensaver_proxy);
 
-        play_loop_stop (manager);
+        play_loop_stop (&manager->priv->critical_alert_timeout_id);
 
         g_clear_object (&manager->priv->idle_monitor);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]