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