screensaver: add proper standby handling
[enigma2-plugins.git] / screensaver / src / plugin.py
1 from enigma import iPlayableService, iServiceInformation
2 from Components.config import config
3 from Plugins.Plugin import PluginDescriptor
4 from Tools.Log import Log
5
6 from PhotoScreensaver import PhotoScreensaver
7 from ScreensaverSetup import ScreensaverSetup
8
9 class ScreenSaverHandler(object):
10         def __init__(self):
11                 self.session = None
12
13         def start(self, session):
14                 self.session = session
15                 self._screenSaver = None
16                 self.session.screensaver = self
17                 self.session.nav.event.append(self._onEvent)
18                 self._enabled = True
19                 config.misc.standbyCounter.addNotifier(self._onStandby, initial_call=False)
20
21         def _onStandby(self, *args):
22                 if not self._enabled:
23                         return
24                 self.disable()
25                 from Screens.Standby import inStandby
26                 inStandby.onClose.append(self.enable)
27
28         def _onStandbyLeft(self):
29                 self.enable()
30
31         def enable(self):
32                 self._enabled = True
33                 self._onEvent(iPlayableService.evPlay)
34
35         def disable(self):
36                 self._enabled = False
37                 self._onEvent(-1) #event doesn't matter when disabled
38
39         def _instantiateSaver(self):
40                 if not self._screenSaver:
41                         self._screenSaver = self.session.instantiateDialog(PhotoScreensaver, zPosition=4000)
42
43         def _onEvent(self, evt):
44                 self._instantiateSaver()
45                 if not (self._enabled and config.plugins.screensaver.enabled.value):
46                         self._screenSaver.enabled = False
47                         return
48                 if evt == iPlayableService.evPlay:
49                         Log.i("play event, checking for video")
50                         service = self.session.nav.getCurrentService()
51                         info = service and service.info()
52                         if not info:
53                                 return
54                         width = info.getInfo(iServiceInformation.sVideoWidth)
55                         Log.d(width)
56                         self._screenSaver.enabled = width <= 0
57                 elif evt in (iPlayableService.evStopped, iPlayableService.evPause):
58                         self._screenSaver.enabled = True
59
60 screenSaverHandler = ScreenSaverHandler()
61
62 def runSetup(session, *args):
63         session.open(ScreensaverSetup)
64
65 def menu(menuid, *args):
66         if menuid == "osd_video_audio":
67                 return [(_("Screensaver"), runSetup, "screensaver", 100)]
68         return []
69
70 def infobar(session):
71         screenSaverHandler.start(session)
72
73 def main(session, **kwargs):
74         session.open(PhotoScreensaver)
75
76 def Plugins(**kwargs):
77         return [
78                 PluginDescriptor(name=_("Screensaver"), description=_("Screensaver using random photos"), where = PluginDescriptor.WHERE_MENU, fnc=menu),
79                 PluginDescriptor(name=_("Screensaver"), description=_("Screensaver using random photos"), where = PluginDescriptor.WHERE_INFOBAR, fnc=infobar),
80                 PluginDescriptor(name=_("Screensaver"), description=_("Screensaver using random photos"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)
81         ]