changeset 259:4601793b0dee

core: use new unix socket mechanism to set frontend state
author Goffi <goffi@goffi.org>
date Mon, 11 Mar 2019 08:39:43 +0100
parents d29be7e348ca
children 145c29b5f2b5
files cagou/core/cagou_main.py
diffstat 1 files changed, 22 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Wed Feb 06 09:19:17 2019 +0100
+++ b/cagou/core/cagou_main.py	Mon Mar 11 08:39:43 2019 +0100
@@ -76,17 +76,24 @@
     notification = None
     log.warning(_(u"Can't import plyer, some features disabled"))
 
-# we want white background by default
-Window.clearcolor = (1, 1, 1, 1)
+if kivy_utils.platform == "android":
+    import socket
 
 
-if kivy_utils.platform == "android":
     # FIXME: move to separate android module
     # sys.platform is "linux" on android by default
     # so we change it to allow backend to detect android
     sys.platform = "android"
-    import mmap
     C.PLUGIN_EXT = 'pyo'
+    SOCKET_DIR = "/data/data/org.salutatoi.cagou/"
+    SOCKET_FILE = ".socket"
+    STATE_RUNNING = "running"
+    STATE_PAUSED = "paused"
+    STATE_STOPPED = "stopped"
+
+
+# we want white background by default
+Window.clearcolor = (1, 1, 1, 1)
 
 
 class NotifsIcon(IconButton):
@@ -316,35 +323,27 @@
         """
         return os.path.expanduser(path).format(*args, media=self.host.media_dir, **kwargs)
 
-    def on_start(self):
+    def initFrontendState(self):
+        """Init state to handle paused/stopped/running on mobile OSes"""
         if sys.platform == "android":
-            # XXX: we use memory map instead of bridge because if we
+            # XXX: we use a separated socket instead of bridge because if we
             #      try to call a bridge method in on_pause method, the call data
             #      is not written before the actual pause
-            # we create a memory map on .cagou_status file with a 1 byte status
-            # status is:
-            # R => running
-            # P => paused
-            # S => stopped
-            self._first_pause = True
-            self.cagou_status_fd = open('.cagou_status', 'wb+')
-            self.cagou_status_fd.write('R')
-            self.cagou_status_fd.flush()
-            self.cagou_status = mmap.mmap(self.cagou_status_fd.fileno(), 1,
-                                          prot=mmap.PROT_WRITE)
+            s = self._frontend_status_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+            s.connect(os.path.join(SOCKET_DIR, SOCKET_FILE))
+            s.sendall(STATE_RUNNING)
 
     def on_pause(self):
-        self.cagou_status[0] = 'P'
+        self._frontend_status_socket.sendall(STATE_PAUSED)
         return True
 
     def on_resume(self):
-        self.cagou_status[0] = 'R'
+        self._frontend_status_socket.sendall(STATE_RUNNING)
 
     def on_stop(self):
         if sys.platform == "android":
-            self.cagou_status[0] = 'S'
-            self.cagou_status.flush()
-            self.cagou_status_fd.close()
+            self._frontend_status_socket.sendall(STATE_STOPPED)
+            self._frontend_status_socket.close()
 
     def key_input(self, window, key, scancode, codepoint, modifier):
         if key == 27:
@@ -467,6 +466,7 @@
     def onBackendReady(self):
         self.app.showWidget()
         self.bridge.getVersion(callback=self._getVersionCb)
+        self.app.initFrontendState()
         self.postInit()
 
     def postInit(self, dummy=None):