[Sugar-devel] [PATCH Sugar] Inhibit power suspend while playing text to speech - OLPC #11830

godiard at sugarlabs.org godiard at sugarlabs.org
Thu May 10 10:14:14 EDT 2012


From: Gonzalo Odiard <godiard at gmail.com>

To avoid stoping playing the text when the xo go to sleep.
This patch creates a file in /var/run/powerd-inhibit-suspend/
and remove it when finish. Is the same technique used in activities
(the code is copied from Distance activity) but we need decide
if is the right thing to do in sugar.

Signed-off-by: Gonzalo Odiard <gonzalo at laptop.org>

---------------------------------------------------

v2: Remove ohmd code
v3: Implemented changes according to James review.
---
 src/jarabe/model/speech.py |   43 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/src/jarabe/model/speech.py b/src/jarabe/model/speech.py
index 1cb0ad4..533fec2 100644
--- a/src/jarabe/model/speech.py
+++ b/src/jarabe/model/speech.py
@@ -30,6 +30,10 @@ DEFAULT_RATE = 0
 
 _speech_manager = None
 
+# directory exists if powerd is running.  create a file here,
+# named after our pid, to inhibit suspend.
+POWERD_INHIBIT_DIR = '/var/run/powerd-inhibit-suspend'
+
 
 class SpeechManager(gobject.GObject):
 
@@ -137,8 +141,10 @@ class _GstSpeechPlayer(gobject.GObject):
     def __init__(self):
         gobject.GObject.__init__(self)
         self._pipeline = None
+        self.using_powerd = self._verify_powerd_directory()
 
     def restart_sound_device(self):
+        self._inhibit_suspend()
         if self._pipeline is None:
             logging.debug('Trying to restart not initialized sound device')
             return
@@ -147,6 +153,7 @@ class _GstSpeechPlayer(gobject.GObject):
         self.emit('play')
 
     def pause_sound_device(self):
+        self._allow_suspend()
         if self._pipeline is None:
             return
 
@@ -154,6 +161,7 @@ class _GstSpeechPlayer(gobject.GObject):
         self.emit('pause')
 
     def stop_sound_device(self):
+        self._allow_suspend()
         if self._pipeline is None:
             return
 
@@ -172,12 +180,9 @@ class _GstSpeechPlayer(gobject.GObject):
         bus.connect('message', self.__pipe_message_cb)
 
     def __pipe_message_cb(self, bus, message):
-        if message.type == gst.MESSAGE_EOS:
-            self._pipeline.set_state(gst.STATE_NULL)
-            self.emit('stop')
-        elif message.type == gst.MESSAGE_ERROR:
-            self._pipeline.set_state(gst.STATE_NULL)
-            self.emit('stop')
+        if message.type == gst.MESSAGE_EOS or \
+                message.type == gst.MESSAGE_ERROR:
+            self.stop_sound_device()
 
     def speak(self, pitch, rate, voice_name, text):
         # TODO workaround for http://bugs.sugarlabs.org/ticket/1801
@@ -223,6 +228,32 @@ class _GstSpeechPlayer(gobject.GObject):
                 locale, best)
         return best
 
+    def _verify_powerd_directory(self):
+        using_powerd = os.access(POWERD_INHIBIT_DIR, os.W_OK)
+        logging.debug("using_powerd: %d", using_powerd)
+        return using_powerd
+
+    def _inhibit_suspend(self):
+        if self.using_powerd:
+            flag_file_name = self._powerd_flag_name()
+            try:
+                file(flag_file_name, 'w').write('')
+                logging.debug("inhibit_suspend file is %s", flag_file_name)
+            except IOError:
+                pass
+
+    def _allow_suspend(self):
+        if self.using_powerd:
+            flag_file_name = self._powerd_flag_name()
+            try:
+                os.unlink(flag_file_name)
+                logging.debug("allow_suspend unlinking %s", flag_file_name)
+            except IOError:
+                pass
+
+    def _powerd_flag_name(self):
+        return POWERD_INHIBIT_DIR + "/%u" % os.getpid()
+
 
 def get_speech_manager():
     global _speech_manager
-- 
1.7.10.1



More information about the Sugar-devel mailing list