disable preferred_modes stuff for now, implement hotplug (but disable it)
[enigma2.git] / lib / python / Plugins / SystemPlugins / Videomode / plugin.py
1 from Screens.Screen import Screen
2 from Plugins.Plugin import PluginDescriptor
3
4 from enigma import eTimer
5
6 from Components.ActionMap import ActionMap
7 from Components.Label import Label
8 from Components.Pixmap import Pixmap
9 from Screens.MessageBox import MessageBox
10 from Screens.Setup import SetupSummary
11 from Components.ConfigList import ConfigListScreen
12 from Components.config import getConfigListEntry, config
13 from VideoWizard import VideoWizard
14 from Components.config import config
15
16 from VideoHardware import video_hw
17
18 class VideoSetup(Screen, ConfigListScreen):
19         def __init__(self, session, hw):
20                 Screen.__init__(self, session)
21                 self.skinName = "Setup"
22                 self.setup_title = "Videomode Setup"
23                 self.hw = hw
24                 self.onChangedEntry = [ ]
25
26                 # handle hotplug by re-creating setup
27                 self.onShow.append(self.startHotplug)
28                 self.onHide.append(self.stopHotplug)
29
30                 self.list = [ ]
31                 ConfigListScreen.__init__(self, self.list, session = session, on_change = self.changedEntry)
32
33                 self["actions"] = ActionMap(["SetupActions"], 
34                         {
35                                 "cancel": self.keyCancel,
36                                 "save": self.apply,
37                         }, -2)
38
39                 self["title"] = Label(_("Video-Setup"))
40
41                 self["oktext"] = Label(_("OK"))
42                 self["canceltext"] = Label(_("Cancel"))
43                 self["ok"] = Pixmap()
44                 self["cancel"] = Pixmap()
45
46                 self.createSetup()
47                 self.grabLastGoodMode()
48
49         def startHotplug(self):
50                 self.hw.on_hotplug.append(self.createSetup)
51
52         def stopHotplug(self):
53                 self.hw.on_hotplug.remove(self.createSetup)
54
55         def createSetup(self):
56                 self.list = [ ]
57                 self.list.append(getConfigListEntry(_("Output Type"), config.av.videoport))
58
59                 # if we have modes for this port:
60                 if config.av.videoport.value in config.av.videomode:
61                         # add mode- and rate-selection:
62                         self.list.append(getConfigListEntry(_("Mode"), config.av.videomode[config.av.videoport.value]))
63                         self.list.append(getConfigListEntry(_("Rate"), config.av.videorate[config.av.videomode[config.av.videoport.value].value]))
64
65 #               if config.av.videoport.value == "DVI":
66 #                       self.list.append(getConfigListEntry(_("Allow Unsupported Modes"), config.av.edid_override))
67
68                 self["config"].list = self.list
69                 self["config"].l.setList(self.list)
70
71         def keyLeft(self):
72                 ConfigListScreen.keyLeft(self)
73                 self.createSetup()
74
75         def keyRight(self):
76                 ConfigListScreen.keyRight(self)
77                 self.createSetup()
78
79         def confirm(self, confirmed):
80                 if not confirmed:
81                         self.hw.setMode(*self.last_good)
82                 else:
83                         self.keySave()
84
85         def grabLastGoodMode(self):
86                 port = config.av.videoport.value
87                 mode = config.av.videomode[port].value
88                 rate = config.av.videorate[mode].value
89                 self.last_good = (port, mode, rate)
90
91         def apply(self):
92                 port = config.av.videoport.value
93                 mode = config.av.videomode[port].value
94                 rate = config.av.videorate[mode].value
95                 if (port, mode, rate) != self.last_good or True:
96                         self.hw.setMode(port, mode, rate)
97                         self.session.openWithCallback(self.confirm, MessageBox, "Is this videomode ok?", MessageBox.TYPE_YESNO, timeout = 20, default = False)
98                 else:
99                         self.keySave()
100
101         # for summary:
102         def changedEntry(self):
103                 for x in self.onChangedEntry:
104                         x()
105
106         def getCurrentEntry(self):
107                 return self["config"].getCurrent()[0]
108
109         def getCurrentValue(self):
110                 return str(self["config"].getCurrent()[1].getText())
111
112         def createSummary(self):
113                 return SetupSummary
114
115 class VideomodeHotplug:
116         def __init__(self, hw):
117                 self.hw = hw
118
119         def start(self):
120                 self.hw.on_hotplug.append(self.hotplug)
121
122         def stop(self):
123                 self.hw.on_hotplug.remove(self.hotplug)
124
125         def hotplug(self, what):
126                 print "hotplug detected on port '%s'" % (what)
127                 port = config.av.videoport.value
128                 mode = config.av.videomode[port].value
129                 rate = config.av.videorate[mode].value
130
131                 if not self.hw.isModeAvailable(port, mode, rate):
132                         print "mode %s/%s/%s went away!" % (port, mode, rate)
133                         modelist = self.hw.getModeList(port)
134                         if not len(modelist):
135                                 print "sorry, no other mode is available (unplug?). Doing nothing."
136                                 return
137                         mode = modelist[0][0]
138                         rate = modelist[0][1]
139                         print "setting %s/%s/%s" % (port, mode, rate)
140                         self.hw.setMode(port, mode, rate)
141
142 hotplug = None
143
144 def startHotplug():
145         global hotplug, video_hw
146         hotplug = VideomodeHotplug(video_hw)
147         hotplug.start()
148
149 def stopHotplug():
150         global hotplug
151         hotplug.stop()
152
153
154 def autostart(reason, session = None, **kwargs):
155         if session is not None:
156                 global my_global_session
157                 my_global_session = session
158                 return
159
160         if reason == 0:
161                 startHotplug()
162         elif reason == 1:
163                 stopHotplug()
164
165 def videoSetupMain(session, **kwargs):
166         session.open(VideoSetup, video_hw)
167
168 def startSetup(menuid):
169         if menuid != "system": 
170                 return [ ]
171
172         return [(_("Video Setup"), videoSetupMain, "video_setup", None)]
173
174 def Plugins(**kwargs):
175         list = [
176 #               PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc = autostart),
177                 PluginDescriptor(name=_("Video Setup"), description=_("Advanced Video Setup"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup) 
178         ]
179         if config.misc.firstrun.value:
180                 list.append(PluginDescriptor(name=_("Video Wizard"), where = PluginDescriptor.WHERE_WIZARD, fnc=VideoWizard))
181         return list