new detection-support in auto-3d-option:
[enigma2-plugins.git] / 3dsettings / src / plugin.py
1 #
2 #  3D Settings E2-Plugin
3 #
4 #  Coded by TheDOC and Dr.Best (c) 2011
5 #  Support: www.dreambox-tools.info
6 #
7 #  This plugin is licensed under the Creative Commons 
8 #  Attribution-NonCommercial-ShareAlike 3.0 Unported 
9 #  License. To view a copy of this license, visit
10 #  http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative
11 #  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
12 #
13 #  Alternatively, this plugin may be distributed and executed on hardware which
14 #  is licensed by Dream Multimedia GmbH.
15
16 #  This plugin is NOT free software. It is open source, you are allowed to
17 #  modify it (if you keep the license), but it may not be commercially 
18 #  distributed other than under the conditions noted above.
19 #
20
21 from Components.ActionMap import ActionMap
22 from Components.config import config, ConfigSubsection, ConfigYesNo, getConfigListEntry,\
23         ConfigSlider, ConfigSelection
24 from Components.ConfigList import ConfigListScreen
25 from Components.Sources.StaticText import StaticText
26 from Components.PluginComponent import plugins
27 from Components.ServiceEventTracker import ServiceEventTracker
28 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
29 from Plugins.Plugin import PluginDescriptor
30 from Screens.Screen import Screen
31 from enigma import iPlayableService, iServiceInformation, eServiceCenter, eServiceReference
32 from ServiceReference import ServiceReference
33 from os.path import basename as os_basename
34
35 # for localized messages
36 from . import _
37
38 def setZOffset(configElement):
39         open("/proc/stb/fb/primary/zoffset", "w").write(str(configElement.value))
40
41 config.plugins.threed = ConfigSubsection()
42 config.plugins.threed.showSBSmenu = ConfigYesNo(default = False)
43 config.plugins.threed.showTBmenu = ConfigYesNo(default = False)
44 config.plugins.threed.zoffset = ConfigSlider(default = 0, increment = 1, limits = [0, 10])
45 config.plugins.threed.zoffset.addNotifier(setZOffset)
46 config.plugins.threed.autothreed = ConfigSelection(default="0", choices = [("0", _("off")),("1", _("on with side by side")),("2", _("on with top/bottom"))])
47
48 THREE_D_OFF = 0
49 THREE_D_SIDE_BY_SIDE = 1
50 THREE_D_TOP_BOTTOM = 2
51
52 modes = {       THREE_D_OFF: "off",
53                         THREE_D_SIDE_BY_SIDE: "sbs",
54                         THREE_D_TOP_BOTTOM: "tab" }
55 reversemodes = dict((value, key) for key, value in modes.iteritems())
56
57 def switchmode(mode):
58         if mode in modes.keys():
59                 print "[3D Settings] switching to mode ", mode
60                 open("/proc/stb/fb/primary/3d", "w").write(modes[mode])
61                 AutoThreeD.instance.setLastMode(mode)
62                 
63 def getmode():
64         mode = reversemodes.get(open("/proc/stb/fb/primary/3d", "r").read().strip(), None)
65         return mode
66
67 def switchsbs(session, **kwargs):
68         switchmode(THREE_D_SIDE_BY_SIDE)
69         
70 def switchtb(session, **kwargs):
71         switchmode(THREE_D_TOP_BOTTOM)
72
73 def switchoff(session, **kwargs):
74         switchmode(THREE_D_OFF)
75
76 class AutoThreeD(Screen):
77         instance = None
78         def __init__(self, session):
79                 self.session = session
80                 Screen.__init__(self, session)
81                 self.__event_tracker = ServiceEventTracker(screen = self, eventmap =
82                         {
83                                 iPlayableService.evUpdatedInfo: self.__evUpdatedInfo,
84                                 iPlayableService.evStart: self.__evStart
85                         })
86                 self.newService = False
87                 self.lastmode = getmode()
88                 assert not AutoThreeD.instance, "only one AutoThreeD instance is allowed!"
89                 AutoThreeD.instance = self # set instance
90
91         def __evStart(self):
92                 self.newService = True
93
94         def __evUpdatedInfo(self):
95                 if self.newService and config.plugins.threed.autothreed.value != "0" and self.session.nav.getCurrentlyPlayingServiceReference():
96                         self.newService = False
97                         ref = self.session.nav.getCurrentService() 
98                         serviceRef = self.session.nav.getCurrentlyPlayingServiceReference()
99                         spath = serviceRef.getPath()
100                         if spath:
101                                 if spath[0] == '/':
102                                         serviceHandler = eServiceCenter.getInstance()
103                                         r = eServiceReference(ref.info().getInfoString(iServiceInformation.sServiceref))
104                                         info = serviceHandler.info(r)
105                                         if info:
106                                                 name = ServiceReference(info.getInfoString(r, iServiceInformation.sServiceref)).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')
107                                         else:
108                                                 name = os_basename(spath) # filename
109                                 else:
110                                         name = serviceRef.getName() # partnerbox servicename
111                         else:
112                                 name =  ServiceReference(ref.info().getInfoString(iServiceInformation.sServiceref)).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', '')
113                         if "3d" in name.lower():
114                                 if config.plugins.threed.autothreed.value == "1":
115                                         mode = THREE_D_SIDE_BY_SIDE
116                                 else:
117                                         mode = THREE_D_TOP_BOTTOM
118                         else:
119                                 mode = THREE_D_OFF
120                         if self.lastmode != mode:
121                                 switchmode(mode)
122
123         def setLastMode(self, mode):
124                 self.lastmode = mode
125
126 class ThreeDSettings(Screen, ConfigListScreen):
127         skin = """
128                 <screen position="center,center" size="570,420" title="3D settings" >
129                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
130                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
131                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
132                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
133                         <widget source="red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
134                         <widget source="green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
135                         <widget source="yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
136                         <widget source="blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
137                         
138                         <widget name="config" position="10,50" size="550,320" scrollbarMode="showOnDemand" />
139                 </screen>"""
140
141         def __init__(self, session, args = None):
142                 Screen.__init__(self, session)
143
144                 self["red"] = StaticText(_("Cancel"))
145                 self["green"] = StaticText(_("OK"))
146                 self["yellow"] = StaticText("")
147                 self["blue"] = StaticText("")
148                 self.updateButtons()
149                 
150                 self.list = []
151                 ConfigListScreen.__init__(self, self.list, session = self.session)
152                 self.createSetup()
153                 
154                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"], 
155                 {
156                         "ok": self.save,
157                         "cancel": self.cancel,
158                         "red": self.cancel,
159                         "green": self.save,
160                         "yellow": self.sideBySide,
161                         "blue": self.topBottom,
162                         
163                 }, -1)
164                 
165         def updateButtons(self):
166                 currentmode = getmode()
167                 if currentmode == THREE_D_OFF:
168                         self["yellow"].setText(_("Side by side"))
169                         self["blue"].setText(_("Top/Bottom"))
170                 elif currentmode == THREE_D_SIDE_BY_SIDE:
171                         self["yellow"].setText(_("2D mode"))
172                         self["blue"].setText("")
173                 elif currentmode == THREE_D_TOP_BOTTOM:
174                         self["blue"].setText(_("2D mode"))
175                         self["yellow"].setText("")
176                 
177         def createSetup(self):
178                 self.list = []
179                 self.list.append(getConfigListEntry(_("Show side by side option in extension menu"), config.plugins.threed.showSBSmenu))
180                 self.list.append(getConfigListEntry(_("Show top/bottom option in extension menu"), config.plugins.threed.showTBmenu))
181                 self.list.append(getConfigListEntry(_("Switch OSD automatically"), config.plugins.threed.autothreed))
182                 currentmode = getmode()
183                 if currentmode in [THREE_D_SIDE_BY_SIDE, THREE_D_TOP_BOTTOM]:
184                         self.list.append(getConfigListEntry(_("Offset"), config.plugins.threed.zoffset))
185                 self["config"].list = self.list
186                 self["config"].l.setList(self.list)
187         
188         def cancel(self):
189                 config.plugins.threed.zoffset.save()
190                 self.keyCancel()
191         
192         def save(self):
193                 self.saveAll()
194                 config.plugins.threed.zoffset.save()
195                 plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
196                 self.close()
197         
198         def sideBySide(self):
199                 currentmode = getmode()
200                 if currentmode == THREE_D_OFF:
201                         switchmode(THREE_D_SIDE_BY_SIDE)
202                 elif currentmode == THREE_D_SIDE_BY_SIDE:
203                         switchmode(THREE_D_OFF)
204                 self.updateButtons()
205                 self.createSetup()
206         
207         def topBottom(self):
208                 currentmode = getmode()
209                 if currentmode == THREE_D_OFF:
210                         switchmode(THREE_D_TOP_BOTTOM)
211                 elif currentmode == THREE_D_TOP_BOTTOM:
212                         switchmode(THREE_D_OFF)
213                 self.updateButtons()
214                 self.createSetup()
215                 
216 def opensettings(session, **kwargs):
217         session.open(ThreeDSettings)
218
219 def settings(menuid, **kwargs):
220         if menuid != "system":
221                 return []
222         return [(_("3D settings"), opensettings, "3d_settings", 10)]
223
224 def autostart(session, **kwargs):
225         AutoThreeD(session)
226
227 def Plugins(**kwargs):
228         pluginlist = []
229         if config.plugins.threed.showSBSmenu.value:
230                 pluginlist.append(PluginDescriptor(name = _("3D: Enable side by side menu"), description = _("3D: Enable side by side menu"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = switchsbs, needsRestart = False))
231         if config.plugins.threed.showTBmenu.value:
232                 pluginlist.append(PluginDescriptor(name = _("3D: Enable top/bottom menu"), description = _("3D: Enable top/bottom menu"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = switchtb, needsRestart = False))
233         if config.plugins.threed.showSBSmenu.value or config.plugins.threed.showTBmenu.value:
234                 pluginlist.append(PluginDescriptor(name = _("3D: disable 3D menu"), description = _("3D: 2D menu"), where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = switchoff, needsRestart = False))
235         pluginlist.append(PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], fnc=autostart))
236         pluginlist.append(PluginDescriptor(name = _("3D settings"), description = _("Change 3D settings"), icon = "plugin.png", where = PluginDescriptor.WHERE_MENU, fnc = settings))
237         return pluginlist