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

godiard at sugarlabs.org godiard at sugarlabs.org
Wed May 9 13:39:02 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
---
 src/jarabe/model/speech.py |   37 +++++++++++++++++++++++++++++++------
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git a/src/jarabe/model/speech.py b/src/jarabe/model/speech.py
index 1cb0ad4..f07171c 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_running()
 
     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,26 @@ class _GstSpeechPlayer(gobject.GObject):
                 locale, best)
         return best
 
+    def _verify_powerd_running(self):
+        self.using_powerd = os.access(POWERD_INHIBIT_DIR, os.W_OK)
+        logging.debug("using_powerd: %d", self.using_powerd)
+        return self.using_powerd
+
+    def _inhibit_suspend(self):
+        if self.using_powerd:
+            flag_file_name = POWERD_INHIBIT_DIR + "/%u" % os.getpid()
+            if not os.path.exists(flag_file_name):
+                fd = open(flag_file_name, 'w')
+                logging.debug("inhibit_suspend file is %s" % flag_file_name)
+                fd.close()
+
+    def _allow_suspend(self):
+        if self.using_powerd:
+            flag_file_name = POWERD_INHIBIT_DIR + "/%u" % os.getpid()
+            if os.path.exists(flag_file_name):
+                os.unlink(flag_file_name)
+            logging.debug("allow_suspend unlinking %s" % flag_file_name)
+
 
 def get_speech_manager():
     global _speech_manager
-- 
1.7.10.1



More information about the Sugar-devel mailing list