3f2bb999200d0e5cd568f46fa73cbc228613a7e4
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / RemoteControlCode / plugin.py
1 from Screens.Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import config, getConfigListEntry, ConfigSubsection, ConfigSelection, ConfigInteger
4 from Components.ActionMap import ActionMap
5 from Screens.MessageBox import MessageBox
6 from Components.Sources.StaticText import StaticText
7 from Plugins.Plugin import PluginDescriptor
8 from Tools.Directories import fileExists
9 from enigma import eTimer, quitMainloop
10
11 def getModel():
12         ret = None
13         if fileExists("/proc/stb/info/vumodel"):
14                 vumodel = open("/proc/stb/info/vumodel")
15                 info=vumodel.read().strip()
16                 vumodel.close()
17                 ret = info
18
19         return ret
20
21 def getRcuDefaultType():
22         if getModel() in ["uno4kse", "zero4k", "duo4k"]:
23                 return "new"
24         return "legacy"
25
26 config.plugins.remotecontrolcode = ConfigSubsection()
27 config.plugins.remotecontrolcode.systemcode = ConfigSelection(default = "2", choices = 
28         [ ("1", "1 "), ("2", "2 "), ("3", "3 "), ("4", "4 ") ] )
29 config.plugins.remotecontrolcode.replytimeout = ConfigInteger(default = 30, limits = (15,9999))
30 config.plugins.remotecontrolcode.rcuType = ConfigSelection(default = getRcuDefaultType(), choices = 
31         [ ("legacy", "Legacy Vu+ Universal RCU"), ("new", "New Vu+ DarkBrown/Bluetooth RCU") ] )
32
33 class RemoteControlCodeInit:
34         def __init__(self):
35                 self.setSystemCode(int(config.plugins.remotecontrolcode.systemcode.value))
36
37         def setSystemCode(self, type = 2):
38                 if not fileExists("/proc/stb/fp/remote_code"):
39                         return -1
40                 print "<RemoteControlCode> Write Remote Control Code : %d" % type
41                 f = open("/proc/stb/fp/remote_code", "w")
42                 f.write("%d" % type)
43                 f.close()
44                 return 0
45
46         def checkModelSupport(self):
47                 ret = None
48                 model = getModel()
49                 if model not in ["duo", "solo"]:
50                         ret = True
51
52                 return ret
53
54 class RemoteControlCode(Screen,ConfigListScreen,RemoteControlCodeInit):
55         skin =  """
56                 <screen position="center,center" size="400,250" title="Remote Control System Code Setting" >
57                         <ePixmap pixmap="skin_default/buttons/red.png" position="30,10" size="140,40" alphatest="on" />
58                         <ePixmap pixmap="skin_default/buttons/green.png" position="230,10" size="140,40" alphatest="on" />
59
60                         <widget source="key_red" render="Label" position="30,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
61                         <widget source="key_green" render="Label" position="230,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
62
63                         <widget name="config" zPosition="2" position="5,70" size="380,180" scrollbarMode="showOnDemand" transparent="1" />
64                 </screen>
65                 """
66
67         def __init__(self,session):
68                 Screen.__init__(self,session)
69                 self.session = session
70                 self["shortcuts"] = ActionMap(["ShortcutActions", "SetupActions" ],
71                 {
72                         "ok": self.keySave,
73                         "cancel": self.keyCancel,
74                         "red": self.keyCancel,
75                         "green": self.keySave,
76                 }, -2)
77                 self.list = []
78                 ConfigListScreen.__init__(self, self.list,session = self.session)
79                 self["key_red"] = StaticText(_("Cancel"))
80                 self["key_green"] = StaticText(_("Ok"))
81                 self.replytext_1 ="The remote control code will be reset to previous setting, set your R/C's code and select 'keep'"
82                 self.replytext_2 ="\n\n<Code set manual>"
83                 self.replytext_2 +="\n1. Press Digit 2 and Digit 7 simultaneously for 3 seconds. After 3 seconds LED turns on. "
84                 self.replytext_2 +="\n2. Press the <HELP> key. LED is blinked and turns on."
85                 self.replytext_2 +="\n3. Enter a 4 digit code(ex. code 2 is '0002')"
86
87                 self.replytext_newrcu_2 ="\n\n<Code set manual>"
88                 self.replytext_newrcu_2 +="\n1. Press <OK> and <STB> simultaneously for 3 seconds. After 3 seconds LED turns on. "
89                 self.replytext_newrcu_2 +="\n2. Enter a 5 digit code(ex. code 2 is '00002')"
90                 self.replytext_newrcu_2 +="\n3. Press the <OK> key. LED is blinked and turns on."
91
92                 self.createSetup()
93                 self.onLayoutFinish.append(self.checkModel)
94                 self.checkModelTimer = eTimer()
95                 self.checkModelTimer.callback.append(self.invalidmodel)
96
97         def checkModel(self):
98                 if not self.checkModelSupport():
99                         self.checkModelTimer.start(1000,True)
100
101         def invalidmodel(self):
102                         self.session.openWithCallback(self.close, MessageBox, _("This Plugin doesn't support on SOLO/DUO"), MessageBox.TYPE_ERROR)
103
104         def createSetup(self):
105                 self.list = []
106                 self.rcuTypeEntry = getConfigListEntry(_("Remote Control Type"), config.plugins.remotecontrolcode.rcuType)
107                 self.rcsctype = getConfigListEntry(_("Remote Control System Code"), config.plugins.remotecontrolcode.systemcode)
108                 self.replytimeout = getConfigListEntry(_("Reply timeout"), config.plugins.remotecontrolcode.replytimeout)
109                 self.list.append( self.rcuTypeEntry )
110                 self.list.append( self.rcsctype )
111                 self.list.append( self.replytimeout )
112                 self["config"].list = self.list
113                 self["config"].l.setList(self.list)
114
115         def keySave(self):
116                 print "<RemoteControlCode> Selected System Code : ",config.plugins.remotecontrolcode.systemcode.value
117                 ret = self.setSystemCode(int(config.plugins.remotecontrolcode.systemcode.value))
118                 if ret == -1:
119                         self.restoreCode()
120                         self.session.openWithCallback(self.close, MessageBox, _("FILE NOT EXIST : /proc/stb/fp/remote_code"), MessageBox.TYPE_ERROR)
121                 else:
122                         timeout = config.plugins.remotecontrolcode.replytimeout.value
123                         text1 = self.replytext_1
124                         text2 = self.replytext_2
125                         if config.plugins.remotecontrolcode.rcuType.value == "new":
126                                 text2 = self.replytext_newrcu_2
127
128                         self.session.openWithCallback(self.MessageBoxConfirmCodeCallback, MessageBoxConfirmCode, text1, text2, MessageBox.TYPE_YESNO, timeout = timeout, default = False)
129
130         def restoreCode(self):
131                 for x in self["config"].list:
132                         x[1].cancel()
133
134         def MessageBoxConfirmCodeCallback(self,ret):
135                 if ret:
136                         self.saveAll()
137                         self.session.openWithCallback(self.restartCallback, MessageBox, _("GUI restart now, press 'OK' button."), MessageBox.TYPE_INFO)
138                 else:
139                         self.restoreCode()
140                         self.setSystemCode(int(config.plugins.remotecontrolcode.systemcode.value))
141
142         def restartCallback(self,result):
143                 quitMainloop(3)
144
145 class MessageBoxConfirmCode(MessageBox):
146         skin =  """
147                 <screen position="center,center" size="620,10" title="Message">
148                         <widget name="text" position="65,8" size="420,0" font="Regular;20" />
149                         <widget name="ErrorPixmap" pixmap="skin_default/icons/input_error.png" position="5,5" size="53,53" alphatest="blend" />
150                         <widget name="QuestionPixmap" pixmap="skin_default/icons/input_question.png" position="5,5" size="53,53" alphatest="blend" />
151                         <widget name="InfoPixmap" pixmap="skin_default/icons/input_info.png" position="5,5" size="53,53" alphatest="blend" />
152                         <widget name="list" position="100,100" size="380,375" transparent="1" />
153                         <applet type="onLayoutFinish">
154 # this should be factored out into some helper code, but currently demonstrates applets.
155 from enigma import eSize, ePoint
156
157 orgwidth  = self.instance.size().width()
158 orgheight = self.instance.size().height()
159 orgpos    = self.instance.position()
160 textsize  = self[&quot;text&quot;].getSize()
161
162 # y size still must be fixed in font stuff...
163 textsize = (textsize[0] + 50, textsize[1] + 50)
164 offset = 0
165 if self.type == self.TYPE_YESNO:
166         offset = 60
167 wsizex = textsize[0] + 60
168 wsizey = textsize[1] + offset
169 if (280 &gt; wsizex):
170         wsizex = 280
171 wsize = (wsizex, wsizey)
172
173 # resize
174 self.instance.resize(eSize(*wsize))
175
176 # resize label
177 self[&quot;text&quot;].instance.resize(eSize(*textsize))
178
179 # move list
180 listsize = (wsizex, 50)
181 self[&quot;list&quot;].instance.move(ePoint(0, textsize[1]))
182 self[&quot;list&quot;].instance.resize(eSize(*listsize))
183
184 # center window
185 newwidth = wsize[0]
186 newheight = wsize[1]
187 window_posx = orgpos.x() + (orgwidth - newwidth)/2
188 window_posy = orgpos.y() + (orgheight - newheight)/2
189 if (150 &gt; window_posy):
190         window_posy = 150
191 self.instance.move(ePoint(window_posx, window_posy))
192                         </applet>
193                 </screen>
194                 """
195
196         def __init__(self, session, replytext_1="", replytext_2="", type = MessageBox.TYPE_YESNO, timeout = -1, close_on_any_key = False, default = True, enable_input = True, msgBoxID = None):
197                 self.replytext_1 = replytext_1
198                 self.replytext_2 = replytext_2
199                 MessageBox.__init__(self,session,self.replytext_1 + "\n" + self.replytext_2,type,timeout,close_on_any_key,default,enable_input,msgBoxID)
200                 if type == MessageBox.TYPE_YESNO:
201                         self.list = [ (_("Keep"), 0), (_("Restore"), 1) ]
202                         self["list"].setList(self.list)
203
204         def timerTick(self):
205                 if self.execing:
206                         self.timeout -= 1
207                         self["text"].setText(self.replytext_1 + " in %d seconds."%self.timeout + self.replytext_2)
208                         if self.timeout == 0:
209                                 self.timer.stop()
210                                 self.timerRunning = False
211                                 self.timeoutCallback()
212
213         def move(self, direction):
214                 if self.close_on_any_key:
215                         self.close(True)
216                 self["list"].instance.moveSelection(direction)
217                 if self.list:
218                         self["selectedChoice"].setText(self["list"].getCurrent()[0])
219 #               self.stopTimer()
220
221         def timeoutCallback(self):
222                 self.close(False)
223
224 def main(session, **kwargs):
225         session.open(RemoteControlCode)
226
227 def Plugins(**kwargs):
228         return [PluginDescriptor(name=_("RemoteControlCode"), description="setup Remote Control System Code Type", where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = True, fnc=main)]
229
230 remotecontrolcodeinit = RemoteControlCodeInit()