OSD3DSetup: Add sanity check
[openblackhole/openblackhole-enigma2.git] / lib / python / Plugins / SystemPlugins / OSD3DSetup / plugin.py
1 from Screens.Screen import Screen
2 from Screens.ChannelSelection import FLAG_IS_DEDICATED_3D
3 from Components.ConfigList import ConfigListScreen
4 from Components.ServiceEventTracker import ServiceEventTracker
5 from Components.SystemInfo import SystemInfo
6 from Components.config import config, ConfigSubsection, ConfigInteger, ConfigSelection, ConfigSlider, getConfigListEntry
7 from enigma import iPlayableService, iServiceInformation, eServiceCenter, eServiceReference, eDVBDB
8
9 modelist = {"off": _("Off"), "auto": _("Auto"), "sidebyside": _("Side by side"), "topandbottom": _("Top and bottom")}
10
11 config.plugins.OSD3DSetup = ConfigSubsection()
12 config.plugins.OSD3DSetup.mode = ConfigSelection(choices = modelist, default = "auto")
13 config.plugins.OSD3DSetup.znorm = ConfigInteger(default = 0)
14
15 class OSD3DSetupScreen(Screen, ConfigListScreen):
16         skin = """
17         <screen position="c-200,c-100" size="400,200" title="OSD 3D setup">
18                 <widget name="config" position="c-175,c-75" size="350,150" />
19                 <ePixmap pixmap="skin_default/buttons/green.png" position="c-145,e-45" zPosition="0" size="140,40" alphatest="on" />
20                 <ePixmap pixmap="skin_default/buttons/red.png" position="c+5,e-45" zPosition="0" size="140,40" alphatest="on" />
21                 <widget name="ok" position="c-145,e-45" size="140,40" valign="center" halign="center" zPosition="1" font="Regular;20" transparent="1" backgroundColor="green" />
22                 <widget name="cancel" position="c+5,e-45" size="140,40" valign="center" halign="center" zPosition="1" font="Regular;20" transparent="1" backgroundColor="red" />
23         </screen>"""
24
25         def __init__(self, session):
26                 self.skin = OSD3DSetupScreen.skin
27                 Screen.__init__(self, session)
28
29                 from Components.ActionMap import ActionMap
30                 from Components.Button import Button
31
32                 self["ok"] = Button(_("OK"))
33                 self["cancel"] = Button(_("Cancel"))
34
35                 self["actions"] = ActionMap(["SetupActions", "ColorActions", "MenuActions"],
36                 {
37                         "ok": self.keyGo,
38                         "save": self.keyGo,
39                         "cancel": self.keyCancel,
40                         "green": self.keyGo,
41                         "red": self.keyCancel,
42                         "menu": self.closeRecursive,
43                 }, -2)
44
45                 self.list = []
46                 ConfigListScreen.__init__(self, self.list, session = self.session)
47
48                 mode = config.plugins.OSD3DSetup.mode.value
49                 znorm = config.plugins.OSD3DSetup.znorm.value
50
51                 self.mode = ConfigSelection(choices = modelist, default = mode)
52                 self.znorm = ConfigSlider(default = znorm + 50, increment = 1, limits = (0, 100))
53                 self.list.append(getConfigListEntry(_("3d mode"), self.mode))
54                 self.list.append(getConfigListEntry(_("Depth"), self.znorm))
55                 self["config"].list = self.list
56                 self["config"].l.setList(self.list)
57
58         def keyLeft(self):
59                 ConfigListScreen.keyLeft(self)
60                 self.setPreviewSettings()
61
62         def keyRight(self):
63                 ConfigListScreen.keyRight(self)
64                 self.setPreviewSettings()
65
66         def setPreviewSettings(self):
67                 applySettings(self.mode.value, int(self.znorm.value) - 50)
68
69         def keyGo(self):
70                 config.plugins.OSD3DSetup.mode.value = self.mode.value
71                 config.plugins.OSD3DSetup.znorm.value = int(self.znorm.value) - 50
72                 config.plugins.OSD3DSetup.save()
73                 self.close()
74
75         def keyCancel(self):
76                 applySettings()
77                 self.close()
78
79 previous = None
80 isDedicated3D = False
81
82 def applySettings(mode=config.plugins.OSD3DSetup.mode.value, znorm=int(config.plugins.OSD3DSetup.znorm.value)):
83         global previous, isDedicated3D
84         mode = isDedicated3D and mode == "auto" and "sidebyside" or mode
85         mode == "3dmode" in SystemInfo["3DMode"] and mode or mode == 'sidebyside' and 'sbs' or mode == 'topandbottom' and 'tab' or 'off'
86         if previous != (mode, znorm):
87                 try:
88                         open(SystemInfo["3DMode"], "w").write(mode)
89                         open(SystemInfo["3DZNorm"], "w").write('%d' % znorm)
90                         previous = (mode, znorm)
91                 except:
92                         return
93
94 class auto3D(Screen):
95         def __init__(self, session):
96                 Screen.__init__(self, session)
97                 self.session = session
98                 self.__event_tracker = ServiceEventTracker(screen = self, eventmap =
99                         {
100                                 iPlayableService.evStart: self.__evStart
101                         })
102
103         def checkIfDedicated3D(self):
104                         service = self.session.nav.getCurrentlyPlayingServiceReference()
105                         servicepath = service and service.getPath()
106                         if servicepath and servicepath.startswith("/"):
107                                 if service.toString().startswith("1:"):
108                                         info = eServiceCenter.getInstance().info(service)
109                                         service = info and info.getInfoString(service, iServiceInformation.sServiceref)
110                                         return service and eDVBDB.getInstance().getFlag(eServiceReference(service)) & FLAG_IS_DEDICATED_3D == FLAG_IS_DEDICATED_3D and "sidebyside"
111                                 else:
112                                         return ".3d." in servicepath.lower() and "sidebyside" or ".tab." in servicepath.lower() and "topandbottom"
113                         service = self.session.nav.getCurrentService()
114                         info = service and service.info()
115                         return info and info.getInfo(iServiceInformation.sIsDedicated3D) == 1 and "sidebyside"
116
117         def __evStart(self):
118                 if config.plugins.OSD3DSetup.mode.value == "auto":
119                         global isDedicated3D
120                         isDedicated3D = self.checkIfDedicated3D()
121                         if isDedicated3D:
122                                 applySettings(isDedicated3D)
123                         else:
124                                 applySettings()
125
126 def main(session, **kwargs):
127         session.open(OSD3DSetupScreen)
128
129 def startSetup(menuid):
130         if menuid != "system":
131                 return [ ]
132         return [(_("OSD 3D setup"), main, "auto_3d_setup", 0)]
133
134 def autostart(reason, **kwargs):
135         "session" in kwargs and kwargs["session"].open(auto3D)
136
137 def Plugins(**kwargs):
138         if SystemInfo["3DMode"]:
139                 from Plugins.Plugin import PluginDescriptor
140                 return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], fnc = autostart),
141                         PluginDescriptor(name = "OSD 3D setup", description = _("Adjust 3D settings"), where = PluginDescriptor.WHERE_MENU, fnc = startSetup)]
142         return []