- added option to set the deinterlacer mode (newest driver needed)
[enigma2-plugins.git] / autoresolution / src / plugin.py
1 from Screens.Screen import Screen
2 from Screens.Setup import SetupSummary
3 from Components.ConfigList import ConfigList, ConfigListScreen
4 from Components.config import config, getConfigListEntry, ConfigSelection, ConfigSubsection, ConfigYesNo
5 from Components.ServiceEventTracker import ServiceEventTracker
6 from Components.ActionMap import ActionMap
7 from Components.Label import Label
8 from Components.Pixmap import Pixmap
9 from enigma import iPlayableService, iServiceInformation, eTimer
10 from ServiceReference import ServiceReference
11 from Plugins.Plugin import PluginDescriptor
12 import string
13 # depends on Videomode Plugin
14 from Plugins.SystemPlugins.Videomode.VideoHardware import video_hw
15
16 session = [ ]
17
18 preferedmodes = [mode[0] for mode in video_hw.getModeList(config.av.videoport.value)]
19 default = config.av.videomode[config.av.videoport.value].value
20 config.plugins.autoresolution = ConfigSubsection()
21 config.plugins.autoresolution.enable = ConfigYesNo(default = False)
22 config.plugins.autoresolution.i1080 = ConfigSelection(default = default, choices = preferedmodes)
23 #config.plugins.autoresolution.p1080 = ConfigSelection(default = default, choices = preferedmodes)
24 config.plugins.autoresolution.i576 = ConfigSelection(default = default, choices = preferedmodes)
25 config.plugins.autoresolution.p576 = ConfigSelection(default = default, choices = preferedmodes)
26 #config.plugins.autoresolution.i720 = ConfigSelection(default = default, choices = preferedmodes)
27 config.plugins.autoresolution.p720 = ConfigSelection(default = default, choices = preferedmodes)
28 config.plugins.autoresolution.i480 = ConfigSelection(default = default, choices = preferedmodes)
29 config.plugins.autoresolution.p480 = ConfigSelection(default = default, choices = preferedmodes)
30 config.plugins.autoresolution.showinfo = ConfigYesNo(default = True)
31 config.plugins.autoresolution.testmode = ConfigYesNo(default = False)
32 config.plugins.autoresolution.deinterlacer = ConfigSelection(default = "auto", choices =
33                 [("auto", _("auto")), ("off", _("off")), ("on", _("on"))])
34 def setDeinterlacer(configElement):
35         mode = config.plugins.autoresolution.deinterlacer.value
36         print "[AutoRes] switch deinterlacer mode to %s" % mode
37         f = open('/proc/stb/vmpeg/deinterlace' , "w")
38         f.write("%s\n" % mode)
39         f.close()
40 config.plugins.autoresolution.deinterlacer.addNotifier(setDeinterlacer)
41
42 def build_switchdic():
43         dic = { '1080i': config.plugins.autoresolution.i1080.value, \
44 #               '1080p': config.plugins.autoresolution.p1080.value, \
45                 '576i':config.plugins.autoresolution.i576.value, \
46                 '576p':config.plugins.autoresolution.p576.value, \
47                 '480i':config.plugins.autoresolution.i480.value, \
48                 '480p':config.plugins.autoresolution.p480.value, \
49 #               '720i':config.plugins.autoresolution.i720.value, \
50                 '720p':config.plugins.autoresolution.p720.value}
51         return dic
52 switchdic = build_switchdic()
53
54
55 class AutoResSetupMenu(Screen, ConfigListScreen):
56         def __init__(self, session):
57                 Screen.__init__(self, session)
58                 self.skinName = "Setup"
59                 self.setup_title = "Autoresolution Videomode Setup"
60
61                 self.onChangedEntry = [ ]
62                 self.list = [ ]
63                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changedEntry)
64
65                 self["actions"] = ActionMap(["SetupActions"], 
66                         {
67                                 "cancel": self.keyCancel,
68                                 "save": self.apply,
69                         }, -2)
70
71                 self["title"] = Label(_("Autoresolution Settings"))
72
73                 self["oktext"] = Label(_("OK"))
74                 self["canceltext"] = Label(_("Cancel"))
75
76                 self["ok"] = Pixmap()
77                 self["cancel"] = Pixmap()
78                 
79                 self.createSetup()
80                 
81         def createSetup(self):
82                 self.list = []
83                 self.list.append(getConfigListEntry(_("Enable Autoresolution"), config.plugins.autoresolution.enable))
84                 if config.plugins.autoresolution.enable.value:
85                         self.list.append(getConfigListEntry(_("%s Content" % "1080i"), config.plugins.autoresolution.i1080))
86 #                       self.list.append(getConfigListEntry(_("%s Content" % "1080p"), config.plugins.autoresolution.p1080))
87 #                       self.list.append(getConfigListEntry(_("%s Content" % "720i"), config.plugins.autoresolution.i720))
88                         self.list.append(getConfigListEntry(_("720p Content"), config.plugins.autoresolution.p720))
89                         self.list.append(getConfigListEntry(_("576i Content"), config.plugins.autoresolution.i576))
90                         self.list.append(getConfigListEntry(_("576p Content"), config.plugins.autoresolution.p576))
91                         self.list.append(getConfigListEntry(_("480p Content"), config.plugins.autoresolution.p480))
92                         self.list.append(getConfigListEntry(_("480i Content"), config.plugins.autoresolution.i480))
93                         self.list.append(getConfigListEntry(_("Show Info Screen"), config.plugins.autoresolution.showinfo))
94                         self.list.append(getConfigListEntry(_("Running in Testmode"), config.plugins.autoresolution.testmode))
95                 self.list.append(getConfigListEntry(_("Deinterlacer Mode"), config.plugins.autoresolution.deinterlacer))
96                 self["config"].list = self.list
97                 self["config"].setList(self.list)
98         
99         def apply(self):
100                 for x in self["config"].list:
101                         x[1].save()
102                         global switchdic
103                         switchdic = build_switchdic()
104                 self.close()
105         
106         def keyLeft(self):
107                 ConfigListScreen.keyLeft(self)
108                 self.createSetup()
109
110         def keyRight(self):
111                 ConfigListScreen.keyRight(self)
112                 self.createSetup()
113         
114         # for summary:
115         def changedEntry(self):
116                 for x in self.onChangedEntry:
117                         x()
118
119         def getCurrentEntry(self):
120                 return self["config"].getCurrent()[0]
121
122         def getCurrentValue(self):
123                 return str(self["config"].getCurrent()[1].getText())
124
125         def createSummary(self):
126                 return SetupSummary
127         
128
129 class AutoRes(Screen):
130         def __init__(self, session):
131                 Screen.__init__(self, session)
132                 self.__event_tracker = ServiceEventTracker(screen = self, eventmap =
133                         {
134                                 iPlayableService.evVideoSizeChanged: self.__evVideoSizeChanged,
135                                 iPlayableService.evVideoProgressiveChanged: self.__evVideoProgressiveChanged,
136                         })
137                 
138                 self.height, self.progressive = '','i'
139                 
140         def __evVideoSizeChanged(self):
141                 print "[AutoRes] got event evVideoSizeChanged"
142                 if config.plugins.autoresolution.enable.value:
143                         service = self.session.nav.getCurrentService()
144                         info = service and service.info()
145                         height = info and info.getInfo(iServiceInformation.sVideoHeight)
146                         if height != self.height:
147                                 self.height = height
148                                 self.changeVideomode()
149         
150         def __evVideoProgressiveChanged(self):
151                 print "[AutoRes] got event evVideoProgressiveChanged"
152                 prog = 'i'
153                 if config.plugins.autoresolution.enable.value:
154                         service = self.session.nav.getCurrentService()
155                         info = service and service.info()
156                         progressive = info and info.getInfo(iServiceInformation.sProgressive)
157                         if progressive == 1:
158                                 prog = 'p'
159                         if prog != self.progressive:
160                                 self.progressive = prog
161                                 self.changeVideomode()
162                                                
163         def changeVideomode(self):
164                 content_mode = "%s%s" %(self.height, self.progressive)
165                 print "[AutoRes] new content is %s%s" %(self.height, self.progressive)
166                 if switchdic.has_key(content_mode):
167                         if switchdic[content_mode] in preferedmodes:
168                                 port = config.av.videoport.value
169                                 mode = switchdic[content_mode]
170                                 rate = config.av.videorate[mode].value
171                                 print "[AutoRes] switching to %s %s %s" % (port, mode, rate)
172                                 if config.plugins.autoresolution.showinfo.value:
173                                         resolutionlabel["restxt"].setText("%s %s %s" % (port, mode, rate))
174                                         resolutionlabel.show()
175                 
176                                 video_hw.setMode(port, mode, rate)
177                                                 
178                                 if config.plugins.autoresolution.testmode.value:
179                                         from Screens.MessageBox import MessageBox
180                                         self.session.openWithCallback(
181                                                         self.confirm, MessageBox, _("Autoresolution Plugin Testmode:\nIs %s %s %s videomode ok?" % ((port, mode, rate))), MessageBox.TYPE_YESNO, timeout = 15, default = False)
182                         else:
183                                 print '[AutoRes] TV dont support %s' % switchdic[self.height]
184
185         def confirm(self, confirmed):
186                 if not confirmed:
187                         port = config.av.videoport.value
188                         mode = config.av.videomode[port].value
189                         rate = config.av.videorate[mode].value
190                         if config.plugins.autoresolution.showinfo.value:
191                                 resolutionlabel["restxt"].setText("%s %s %s" % (port, mode, rate))
192                                 resolutionlabel.show()
193                         video_hw.setMode(port, mode, rate)
194
195
196 class ResolutionLabel(Screen):
197         skin = """
198                 <screen position="50,30" size="140,20" flags="wfNoBorder" >
199                         <widget name="restxt" position="0,0" size="140,20" font="Regular;18" />
200                 </screen>"""
201         def __init__(self, session):
202                 Screen.__init__(self, session)
203                 
204                 from Components.Label import Label
205                 self["restxt"] = Label()
206                 
207                 self.hideTimer = eTimer()
208                 self.hideTimer.callback.append(self.hide)
209                 
210                 self.onShow.append(self.hide_me)
211         
212         def hide_me(self):
213                 self.hideTimer.start(config.usage.infobar_timeout.index * 1000, True)
214
215         
216 def autostart(reason, **kwargs):
217         global session, resolutionlabel
218         if "session" in kwargs:
219                 session = kwargs["session"]
220                 resolutionlabel = session.instantiateDialog(ResolutionLabel)
221                 AutoRes(session)
222
223 def startSetup(menuid):
224         if menuid != "system": 
225                 return [ ]
226         return [(_("Autoresolution") + "...", autoresSetup, "autores_setup", 45)]
227
228 def autoresSetup(session, **kwargs):
229         session.open(AutoResSetupMenu)
230
231 def Plugins(path, **kwargs):
232         return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], fnc = autostart), \
233                 PluginDescriptor(name=_("Autoresolution"), description=_("Autoresolution Switch"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup) ]