comparison cagou/core/cagou_main.py @ 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 ff1efdeff53f
children 145c29b5f2b5
comparison
equal deleted inserted replaced
258:d29be7e348ca 259:4601793b0dee
74 from plyer import notification 74 from plyer import notification
75 except ImportError: 75 except ImportError:
76 notification = None 76 notification = None
77 log.warning(_(u"Can't import plyer, some features disabled")) 77 log.warning(_(u"Can't import plyer, some features disabled"))
78 78
79 # we want white background by default
80 Window.clearcolor = (1, 1, 1, 1)
81
82
83 if kivy_utils.platform == "android": 79 if kivy_utils.platform == "android":
80 import socket
81
82
84 # FIXME: move to separate android module 83 # FIXME: move to separate android module
85 # sys.platform is "linux" on android by default 84 # sys.platform is "linux" on android by default
86 # so we change it to allow backend to detect android 85 # so we change it to allow backend to detect android
87 sys.platform = "android" 86 sys.platform = "android"
88 import mmap
89 C.PLUGIN_EXT = 'pyo' 87 C.PLUGIN_EXT = 'pyo'
88 SOCKET_DIR = "/data/data/org.salutatoi.cagou/"
89 SOCKET_FILE = ".socket"
90 STATE_RUNNING = "running"
91 STATE_PAUSED = "paused"
92 STATE_STOPPED = "stopped"
93
94
95 # we want white background by default
96 Window.clearcolor = (1, 1, 1, 1)
90 97
91 98
92 class NotifsIcon(IconButton): 99 class NotifsIcon(IconButton):
93 notifs = properties.ListProperty() 100 notifs = properties.ListProperty()
94 101
314 @param *args: additional arguments used in format 321 @param *args: additional arguments used in format
315 @param **kwargs: additional keyword arguments used in format 322 @param **kwargs: additional keyword arguments used in format
316 """ 323 """
317 return os.path.expanduser(path).format(*args, media=self.host.media_dir, **kwargs) 324 return os.path.expanduser(path).format(*args, media=self.host.media_dir, **kwargs)
318 325
319 def on_start(self): 326 def initFrontendState(self):
327 """Init state to handle paused/stopped/running on mobile OSes"""
320 if sys.platform == "android": 328 if sys.platform == "android":
321 # XXX: we use memory map instead of bridge because if we 329 # XXX: we use a separated socket instead of bridge because if we
322 # try to call a bridge method in on_pause method, the call data 330 # try to call a bridge method in on_pause method, the call data
323 # is not written before the actual pause 331 # is not written before the actual pause
324 # we create a memory map on .cagou_status file with a 1 byte status 332 s = self._frontend_status_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
325 # status is: 333 s.connect(os.path.join(SOCKET_DIR, SOCKET_FILE))
326 # R => running 334 s.sendall(STATE_RUNNING)
327 # P => paused
328 # S => stopped
329 self._first_pause = True
330 self.cagou_status_fd = open('.cagou_status', 'wb+')
331 self.cagou_status_fd.write('R')
332 self.cagou_status_fd.flush()
333 self.cagou_status = mmap.mmap(self.cagou_status_fd.fileno(), 1,
334 prot=mmap.PROT_WRITE)
335 335
336 def on_pause(self): 336 def on_pause(self):
337 self.cagou_status[0] = 'P' 337 self._frontend_status_socket.sendall(STATE_PAUSED)
338 return True 338 return True
339 339
340 def on_resume(self): 340 def on_resume(self):
341 self.cagou_status[0] = 'R' 341 self._frontend_status_socket.sendall(STATE_RUNNING)
342 342
343 def on_stop(self): 343 def on_stop(self):
344 if sys.platform == "android": 344 if sys.platform == "android":
345 self.cagou_status[0] = 'S' 345 self._frontend_status_socket.sendall(STATE_STOPPED)
346 self.cagou_status.flush() 346 self._frontend_status_socket.close()
347 self.cagou_status_fd.close()
348 347
349 def key_input(self, window, key, scancode, codepoint, modifier): 348 def key_input(self, window, key, scancode, codepoint, modifier):
350 if key == 27: 349 if key == 27:
351 # we disable [esc] handling, because default action is to quit app 350 # we disable [esc] handling, because default action is to quit app
352 return True 351 return True
465 self.version = version 464 self.version = version
466 465
467 def onBackendReady(self): 466 def onBackendReady(self):
468 self.app.showWidget() 467 self.app.showWidget()
469 self.bridge.getVersion(callback=self._getVersionCb) 468 self.bridge.getVersion(callback=self._getVersionCb)
469 self.app.initFrontendState()
470 self.postInit() 470 self.postInit()
471 471
472 def postInit(self, dummy=None): 472 def postInit(self, dummy=None):
473 # FIXME: resize seem to bug on android, so we use below_target for now 473 # FIXME: resize seem to bug on android, so we use below_target for now
474 self.app.root_window.softinput_mode = "below_target" 474 self.app.root_window.softinput_mode = "below_target"