fixes for support more than one CI
[enigma2.git] / lib / dvb_ci / dvbci_ui.cpp
1 #include <lib/dvb_ci/dvbci_ui.h>
2 #include <lib/dvb_ci/dvbci.h>
3
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <sys/ioctl.h>
7
8 #include <string>
9
10 #include <lib/base/init.h>
11 #include <lib/base/init_num.h>
12 #include <lib/base/eerror.h>
13
14 eDVBCI_UI *eDVBCI_UI::instance;
15
16 eDVBCI_UI::eDVBCI_UI()
17 {
18         ASSERT(!instance);
19         instance = this;
20         for(int i=0;i<MAX_SLOTS;++i)
21         {
22                 slotdata[i].mmiScreen=NULL;
23                 slotdata[i].mmiScreenReady=0;
24                 slotdata[i].mmiTuplePos=0;
25                 slotdata[i].state=0;
26         }
27 }
28
29 eDVBCI_UI::~eDVBCI_UI()
30 {
31         for(int i=0;i<MAX_SLOTS;++i)
32         {
33                 if (slotdata[i].mmiScreen)
34                         Py_DECREF(slotdata[i].mmiScreen);
35         }
36 }
37
38 eDVBCI_UI *eDVBCI_UI::getInstance()
39 {
40         return instance;
41 }
42
43 int eDVBCI_UI::getState(int slot)
44 {
45         if (slot < MAX_SLOTS)
46                 return slotdata[slot].state;
47         return 0;
48 }
49
50 void eDVBCI_UI::setState(int slot, int newState)
51 {
52         if (slot < MAX_SLOTS)
53                 slotdata[slot].state = newState;
54 }
55
56 std::string eDVBCI_UI::getAppName(int slot)
57 {
58         if (slot < MAX_SLOTS)
59                 return slotdata[slot].appName;
60         return "";
61 }
62
63 void eDVBCI_UI::setAppName(int slot, const char *name)
64 {
65         if (slot < MAX_SLOTS)
66                 slotdata[slot].appName = name;
67 }
68
69 void eDVBCI_UI::setInit(int slot)
70 {
71         eDVBCIInterfaces::getInstance()->initialize(slot);
72 }
73
74 void eDVBCI_UI::setReset(int slot)
75 {
76         eDVBCIInterfaces::getInstance()->reset(slot);
77 }
78
79 int eDVBCI_UI::startMMI(int slot)
80 {
81         eDVBCIInterfaces::getInstance()->startMMI(slot);
82         return 0;
83 }
84
85 int eDVBCI_UI::stopMMI(int slot)
86 {
87         eDVBCIInterfaces::getInstance()->stopMMI(slot);
88         return 0;
89 }
90
91 int eDVBCI_UI::answerMenu(int slot, int answer)
92 {
93         eDVBCIInterfaces::getInstance()->answerText(slot, answer);
94         return 0;
95 }
96
97 int eDVBCI_UI::answerEnq(int slot, char *value)
98 {
99         eDVBCIInterfaces::getInstance()->answerEnq(slot, value);
100         return 0;
101 }
102
103 int eDVBCI_UI::cancelEnq(int slot)
104 {
105         eDVBCIInterfaces::getInstance()->cancelEnq(slot);
106         return 0;
107 }
108
109 int eDVBCI_UI::availableMMI(int slot)
110 {
111         if (slot < MAX_SLOTS)
112                 return slotdata[slot].mmiScreenReady;
113         return false;
114 }
115
116 int eDVBCI_UI::mmiScreenEnq(int slot, int blind, int answerLen, char *text)
117 {
118         if (slot >= MAX_SLOTS)
119                 return 0;
120
121         slot_ui_data &data = slotdata[slot];
122
123         data.mmiScreenReady = 0;
124
125         if (data.mmiScreen)
126                 Py_DECREF(data.mmiScreen);
127         data.mmiScreen = PyList_New(2);
128
129         PyObject *tuple = PyTuple_New(1);
130         PyTuple_SET_ITEM(tuple, 0, PyString_FromString("ENQ"));
131         PyList_SET_ITEM(data.mmiScreen, 0, tuple);
132
133         tuple = PyTuple_New(4);
134         PyTuple_SET_ITEM(tuple, 0, PyString_FromString("PIN"));
135         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(answerLen));
136         PyTuple_SET_ITEM(tuple, 2, PyString_FromString(text));
137         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(blind));
138
139         PyList_SET_ITEM(data.mmiScreen, 1, tuple);
140
141         data.mmiScreenReady = 1;
142
143         return 0;
144 }
145
146 int eDVBCI_UI::mmiScreenBegin(int slot, int listmenu)
147 {
148         if (slot >= MAX_SLOTS)
149                 return 0;
150
151         printf("eDVBCI_UI::mmiScreenBegin\n");
152
153         slot_ui_data &data = slotdata[slot];
154
155         data.mmiScreenReady = 0;
156
157         if (data.mmiScreen)
158                 Py_DECREF(data.mmiScreen);
159
160         data.mmiScreen = PyList_New(1);
161
162         PyObject *tuple = PyTuple_New(1);
163         if (listmenu == 0)                              //menu
164                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("MENU"));
165         else    //list
166                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("LIST"));
167
168         PyList_SET_ITEM(data.mmiScreen, 0, tuple);
169
170         data.mmiTuplePos = 1;
171
172         return 0;
173 }
174
175 int eDVBCI_UI::mmiScreenAddText(int slot, int type, char *value)
176 {
177         if (slot >= MAX_SLOTS)
178                 return 0;
179
180         eDebug("eDVBCI_UI::mmiScreenAddText(%s)",value ? value : "");
181
182         slot_ui_data &data = slotdata[slot];
183
184         PyObject *tuple = PyTuple_New(3);
185
186         if (type == 0)                                  //title
187                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("TITLE"));
188         else if (type == 1)                             //subtitle
189                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("SUBTITLE"));
190         else if (type == 2)                             //bottom
191                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("BOTTOM"));
192         else
193                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("TEXT"));
194
195         eDebug("addText %s with id %d", value, type);
196
197         PyTuple_SET_ITEM(tuple, 1, PyString_FromString(value));
198
199         if (type > 2)
200                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(type-2));
201         else
202                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(-1));
203
204         PyList_Append(data.mmiScreen, tuple);
205         Py_DECREF(tuple);
206
207         return 0;
208 }
209
210 int eDVBCI_UI::mmiScreenFinish(int slot)
211 {
212         if (slot < MAX_SLOTS)
213         {
214                 printf("eDVBCI_UI::mmiScreenFinish\n");
215                 slotdata[slot].mmiScreenReady = 1;
216         }
217         return 0;
218 }
219
220 int eDVBCI_UI::getMMIState(int slot)
221 {
222         return eDVBCIInterfaces::getInstance()->getMMIState(slot);
223 }
224
225 PyObject *eDVBCI_UI::getMMIScreen(int slot)
226 {
227         if (slot < MAX_SLOTS)
228         {
229                 slot_ui_data &data = slotdata[slot];
230                 if (data.mmiScreenReady)
231                 {
232                         data.mmiScreenReady = 0;
233                         Py_INCREF(data.mmiScreen);
234                         return data.mmiScreen;
235                 }
236         }
237         Py_INCREF(Py_None);
238         return Py_None;
239 }
240
241 //FIXME: correct "run/startlevel"
242 eAutoInitP0<eDVBCI_UI> init_dvbciui(eAutoInitNumbers::rc, "DVB-CI UI");