adds provider name to the events, duplicate data but keeps the event field consistent
[enigma2-plugins.git] / multirc / src / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ConfigList import ConfigListScreen
5 from Components.ActionMap import ActionMap
6 from Components.Label import Label
7 from Components.config import *
8 from os import path
9
10 CONFIGS = [("1", _("layer 1")),
11            ("2", _("layer 2")),
12            ("4", _("layer 3")),
13            ("8", _("layer 4")),
14            ("f", _("any RC"))]
15
16 CONFIGS1 = [("100", _("DM8000-RC layer 1")),
17              ("200", _("DM8000-RC layer 2")),
18              ("400", _("DM8000-RC layer 3")),
19              ("800", _("DM8000-RC layer 4"))]
20
21 # config file for IR mask, default is DM7025 or later, fallback to DM500/600
22 # new-gen boxes also support DM8000 RCs via mask1
23 MASK = "/proc/stb/ir/rc/mask0"
24 if path.exists(MASK):
25         MASK1 = "/proc/stb/ir/rc/mask1"
26         CONFIGS += CONFIGS1
27 else:
28         MASK = "/proc/stb/ir/rc/mask"
29
30 config.plugins.MultiRC = ConfigSubsection()
31 config.plugins.MultiRC.mask = ConfigSelection(choices = CONFIGS, default = "f")
32
33 class MultiRCSetup(ConfigListScreen, Screen):
34         skin = """
35                 <screen position="170,150" size="420,280" title="Multi RemoteControl" >
36                 <widget name="config" position="10,10" size="400,40" scrollbarMode="showOnDemand" />
37                 <widget name="warning" position="10,50" size="400,220" font="Regular;20" halign="center"/>
38                 </screen>"""
39
40         # most of the following is black magic copied from other plugins.
41         # e2 devs should really make some best practices or wrapper for this!
42         def __init__(self, session, args = None):
43                 Screen.__init__(self, session)
44                 self.list = [getConfigListEntry(_("Listen on Remote Control"), config.plugins.MultiRC.mask)]
45                 ConfigListScreen.__init__(self, self.list)
46                 self["warning"] = Label(_("""WARNING!
47 After changing this and pressing <Ok>, your Remote Control might stop working!
48
49 Information about re-configuring the RC is available at http://www.dream-multimedia-tv.de/board/thread.php?threadid=5613"""))
50                 self["config"].list = self.list
51                 self["config"].setList(self.list)
52                 self["setupActions"] = ActionMap(["SetupActions"],
53                 {
54                         "save": self.ask_save,
55                         "cancel": self.cancel,
56                         "ok": self.ask_save,
57                 }, -2)
58
59         def ask_save(self):
60                 if not set_mask():
61                         self.session.open(MessageBox, text = _("Error writing to %s!") % MASK,
62                                 type = MessageBox.TYPE_WARNING)
63                         return
64                 # mask value 0xf allows all RCs, no need to verify
65                 if config.plugins.MultiRC.mask.value == "f":
66                         self.confirm_save(True)
67                 else:
68                         self.session.openWithCallback(self.confirm_save, MessageBox,
69                                 _("Is the RC still working?"), MessageBox.TYPE_YESNO,
70                                 timeout = 20, default = False)
71
72         def confirm_save(self, confirmed):
73                 if confirmed:
74                         for x in self["config"].list:
75                                 x[1].save()
76                         set_mask()
77                         self.close()
78                 else:
79                         # input failed, reset to any RC
80                         set_mask("f")
81
82         def cancel(self):
83                 for x in self["config"].list:
84                         x[1].cancel()
85                 set_mask()
86                 self.close()
87
88 def set_mask(mask=None):
89         if not mask:
90                 mask = config.plugins.MultiRC.mask.value
91         try:
92                 # for two-mask boxes, we have to separate
93                 # old-rc values (1..8) to mask0 and new-rc
94                 # values (100..800) to mask1. the special
95                 # case "any RC" (f) must be set to both.
96                 if MASK1:
97                         if mask == "f":
98                                 mask1 = "f00"
99                         elif int(mask, 16) >= 0x100:
100                                 mask1 = mask
101                                 mask = "0"
102                         else:
103                                 mask1 = "0"
104                         print "mask1", mask1
105                         f = open(MASK1, "w")
106                         f.write(mask1)
107                         f.close()
108
109                 print "mask0", mask
110                 f = open(MASK, "w")
111                 f.write(mask)
112                 f.close()
113         except Exception, e:
114                 return False
115         return True
116
117 def multirc_setup(session, **kwargs):
118         session.open(MultiRCSetup)
119
120 def multirc_autostart(reason, **kwargs):
121         # on startup, set correct remote mask
122         if reason == 0:
123                 set_mask()
124                 pass
125
126 def Plugins(**kwargs):
127         return [PluginDescriptor(name="Multi RemoteControl",
128                                 description=_("Multi Dreambox RC layer setup"),
129                                 where=PluginDescriptor.WHERE_PLUGINMENU,
130                                 fnc = multirc_setup),
131
132                 PluginDescriptor(where=PluginDescriptor.WHERE_AUTOSTART,
133                                 fnc = multirc_autostart)]
134
135