Merge branch 'master' of https://github.com/opendreambox/enigma2-plugins
[enigma2-plugins.git] / multirc / src / plugin.py
1 # encoding: utf-8
2 from Plugins.Plugin import PluginDescriptor
3 from Screens.Screen import Screen
4 from Screens.MessageBox import MessageBox
5 from Components.ConfigList import ConfigListScreen
6 from Components.ActionMap import ActionMap
7 from Components.Label import Label
8 from Components.config import *
9 from os import path
10 from enigma import getDesktop
11
12 CONFIGS = [("1", _("layer 1")),
13            ("2", _("layer 2")),
14            ("4", _("layer 3")),
15            ("8", _("layer 4")),
16            ("f", _("any RC"))]
17
18 CONFIGS1 = [("100", _("DM8000-RC layer 1")),
19              ("200", _("DM8000-RC layer 2")),
20              ("400", _("DM8000-RC layer 3")),
21              ("800", _("DM8000-RC layer 4"))]
22
23 CONFIGS2 = [("10000", _("Illuminated RC layer 1")),
24             ("20000", _("Illuminated RC layer 2")),
25             ("40000", _("Illuminated RC layer 3")),
26             ("80000", _("Illuminated RC layer 4"))]
27
28 # config file for IR mask, default is DM7025 or later, fallback to DM500/600
29 # new-gen boxes also support DM8000 RCs via mask1
30 MASK = "/proc/stb/ir/rc/mask0"
31 if path.exists(MASK):
32         MASK1 = "/proc/stb/ir/rc/mask1"
33         CONFIGS += CONFIGS1
34         MASK2 = "/proc/stb/ir/rc/mask2"
35         # check for mask2 (RCU, only on newer kernels)
36         if path.exists(MASK2):
37                 CONFIGS += CONFIGS2
38         else:
39                 MASK2 = None
40 else:
41         MASK = "/proc/stb/ir/rc/mask"
42
43 config.plugins.MultiRC = ConfigSubsection()
44 config.plugins.MultiRC.mask = ConfigSelection(choices = CONFIGS, default = "f")
45
46 class MultiRCSetup(ConfigListScreen, Screen):
47         height = getDesktop(0).size().height()
48         if height == 2160:
49                 skin = """
50                         <screen position="center,center" size="1500,840" title="Multi RemoteControl" >
51                         <widget name="config" position="30,30" size="1440,120" seperation="720" scrollbarMode="showOnDemand" />
52                         <widget name="warning" position="30,150" size="1440,660" font="Regular;60" halign="center"/>
53                         </screen>"""
54         elif height == 1080:
55                 skin = """
56                         <screen position="center,center" size="750,420" title="Multi RemoteControl" >
57                         <widget name="config" position="15,15" size="720,60" seperation="360" scrollbarMode="showOnDemand" />
58                         <widget name="warning" position="15,75" size="720,330" font="Regular;30" halign="center"/>
59                         </screen>"""
60         else:
61                 skin = """
62                         <screen position="center,center" size="500,280" title="Multi RemoteControl" >
63                         <widget name="config" position="10,10" size="480,40" seperation="240" scrollbarMode="showOnDemand" />
64                         <widget name="warning" position="10,50" size="480,220" font="Regular;20" halign="center"/>
65                         </screen>"""
66                         
67         # most of the following is black magic copied from other plugins.
68         # e2 devs should really make some best practices or wrapper for this!
69         def __init__(self, session, args = None):
70                 Screen.__init__(self, session)
71                 self.list = [getConfigListEntry(_("Listen on Remote Control"), config.plugins.MultiRC.mask)]
72                 ConfigListScreen.__init__(self, self.list)
73                 self["warning"] = Label(_("""WARNING!
74 After changing this and pressing <Ok>, your Remote Control might stop working!
75
76 Information about re-configuring the RC is available at https://dreambox.de/board/thread.php?threadid=5613"""))
77                 self["config"].list = self.list
78                 self["config"].setList(self.list)
79                 self["setupActions"] = ActionMap(["SetupActions"],
80                 {
81                         "save": self.ask_save,
82                         "cancel": self.cancel,
83                         "ok": self.ask_save,
84                 }, -2)
85
86         def ask_save(self):
87                 if not set_mask():
88                         self.session.open(MessageBox, text = _("Error writing to %s!") % MASK,
89                                 type = MessageBox.TYPE_WARNING)
90                         return
91                 # mask value 0xf allows all RCs, no need to verify
92                 if config.plugins.MultiRC.mask.value == "f":
93                         self.confirm_save(True)
94                 else:
95                         self.session.openWithCallback(self.confirm_save, MessageBox,
96                                 _("Is the RC still working?"), MessageBox.TYPE_YESNO,
97                                 timeout = 20, default = False)
98
99         def confirm_save(self, confirmed):
100                 if confirmed:
101                         for x in self["config"].list:
102                                 x[1].save()
103                         set_mask()
104                         self.close()
105                 else:
106                         # input failed, reset to any RC
107                         set_mask("f")
108
109         def cancel(self):
110                 for x in self["config"].list:
111                         x[1].cancel()
112                 set_mask()
113                 self.close()
114
115 def write_mask(fname, value):
116         print "MultiRC:", fname, value
117         f = open(fname, "w")
118         f.write(value)
119         f.close()
120
121 def set_mask(mask=None):
122         if not mask:
123                 mask = config.plugins.MultiRC.mask.value
124         try:
125                 # we have to separate old-rc values (1..8) to mask0,
126                 # DM8000-RC values (100..800) to mask1 and Illuminated RC
127                 # values (10000..80000) to mask2. The special
128                 # case "any RC" (f) must be set to all three.
129                 if mask == "f":
130                         mask = "0f0f0f"
131                 v = "000000" + mask
132                 mask0 = v[-2:]          # last two digits
133                 mask1 = v[-4:-2] + "00" # four digits (WTF?)
134                 mask2 = v[-6:-4]        # first two, if available
135
136                 write_mask(MASK, mask0)
137                 if MASK1:
138                         write_mask(MASK1, mask1)
139                 if MASK2:
140                         write_mask(MASK2, mask2)
141
142         except Exception, e:
143                 print "MultiRC failed:", e
144                 return False
145         return True
146
147 def multirc_setup(session, **kwargs):
148         session.open(MultiRCSetup)
149
150 def multirc_autostart(reason, **kwargs):
151         # on startup, set correct remote mask
152         if reason == 0:
153                 set_mask()
154                 pass
155
156 def Plugins(**kwargs):
157         return [PluginDescriptor(name="Multi RemoteControl",
158                                 description=_("Multi Dreambox RC layer setup"),
159                                 where=PluginDescriptor.WHERE_PLUGINMENU,
160                                 fnc = multirc_setup),
161
162                 PluginDescriptor(where=PluginDescriptor.WHERE_AUTOSTART,
163                                 fnc = multirc_autostart)]