fix enigma2 all systemresources (high systemload) since previous CI changes
[vuplus_dvbapp] / 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         {
47                 if (eDVBCIInterfaces::getInstance()->getSlotState(slot) == eDVBCISlot::stateInvalid)
48                         eDVBCIInterfaces::getInstance()->reset(slot);
49                 return slotdata[slot].state;
50         }
51         return 0;
52 }
53
54 void eDVBCI_UI::setState(int slot, int newState)
55 {
56         if (slot < MAX_SLOTS)
57         {
58                 slotdata[slot].state = newState;
59                 /*emit*/ ciStateChanged(slot);
60         }
61 }
62
63 std::string eDVBCI_UI::getAppName(int slot)
64 {
65         if (slot < MAX_SLOTS)
66                 return slotdata[slot].appName;
67         return "";
68 }
69
70 void eDVBCI_UI::setAppName(int slot, const char *name)
71 {
72         if (slot < MAX_SLOTS)
73                 slotdata[slot].appName = name;
74 }
75
76 void eDVBCI_UI::setInit(int slot)
77 {
78         eDVBCIInterfaces::getInstance()->initialize(slot);
79 }
80
81 void eDVBCI_UI::setReset(int slot)
82 {
83         eDVBCIInterfaces::getInstance()->reset(slot);
84 }
85
86 int eDVBCI_UI::startMMI(int slot)
87 {
88         eDVBCIInterfaces::getInstance()->startMMI(slot);
89         return 0;
90 }
91
92 int eDVBCI_UI::stopMMI(int slot)
93 {
94         eDVBCIInterfaces::getInstance()->stopMMI(slot);
95         return 0;
96 }
97
98 int eDVBCI_UI::answerMenu(int slot, int answer)
99 {
100         eDVBCIInterfaces::getInstance()->answerText(slot, answer);
101         return 0;
102 }
103
104 int eDVBCI_UI::answerEnq(int slot, char *value)
105 {
106         eDVBCIInterfaces::getInstance()->answerEnq(slot, value);
107         return 0;
108 }
109
110 int eDVBCI_UI::cancelEnq(int slot)
111 {
112         eDVBCIInterfaces::getInstance()->cancelEnq(slot);
113         return 0;
114 }
115
116 int eDVBCI_UI::availableMMI(int slot)
117 {
118         if (slot < MAX_SLOTS)
119                 return slotdata[slot].mmiScreenReady;
120         return false;
121 }
122
123 int eDVBCI_UI::mmiScreenClose(int slot, int timeout)
124 {
125         if (slot >= MAX_SLOTS)
126                 return 0;
127
128         slot_ui_data &data = slotdata[slot];
129
130         data.mmiScreenReady = 0;
131
132         if (data.mmiScreen)
133                 Py_DECREF(data.mmiScreen);
134         data.mmiScreen = PyList_New(1);
135
136         PyObject *tuple = PyTuple_New(2);
137         PyTuple_SET_ITEM(tuple, 0, PyString_FromString("CLOSE"));
138         PyTuple_SET_ITEM(tuple, 1, PyLong_FromLong(timeout));
139         PyList_SET_ITEM(data.mmiScreen, 0, tuple);
140         data.mmiScreenReady = 1;
141         /*emit*/ ciStateChanged(slot);
142         return 0;
143 }
144
145 int eDVBCI_UI::mmiScreenEnq(int slot, int blind, int answerLen, char *text)
146 {
147         if (slot >= MAX_SLOTS)
148                 return 0;
149
150         slot_ui_data &data = slotdata[slot];
151
152         data.mmiScreenReady = 0;
153
154         if (data.mmiScreen)
155                 Py_DECREF(data.mmiScreen);
156         data.mmiScreen = PyList_New(2);
157
158         PyObject *tuple = PyTuple_New(1);
159         PyTuple_SET_ITEM(tuple, 0, PyString_FromString("ENQ"));
160         PyList_SET_ITEM(data.mmiScreen, 0, tuple);
161
162         tuple = PyTuple_New(4);
163         PyTuple_SET_ITEM(tuple, 0, PyString_FromString("PIN"));
164         PyTuple_SET_ITEM(tuple, 1, PyInt_FromLong(answerLen));
165         PyTuple_SET_ITEM(tuple, 2, PyString_FromString(text));
166         PyTuple_SET_ITEM(tuple, 3, PyInt_FromLong(blind));
167
168         PyList_SET_ITEM(data.mmiScreen, 1, tuple);
169
170         data.mmiScreenReady = 1;
171
172         /*emit*/ ciStateChanged(slot);
173
174         return 0;
175 }
176
177 int eDVBCI_UI::mmiScreenBegin(int slot, int listmenu)
178 {
179         if (slot >= MAX_SLOTS)
180                 return 0;
181
182         printf("eDVBCI_UI::mmiScreenBegin\n");
183
184         slot_ui_data &data = slotdata[slot];
185
186         data.mmiScreenReady = 0;
187
188         if (data.mmiScreen)
189                 Py_DECREF(data.mmiScreen);
190
191         data.mmiScreen = PyList_New(1);
192
193         PyObject *tuple = PyTuple_New(1);
194         if (listmenu == 0)                              //menu
195                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("MENU"));
196         else    //list
197                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("LIST"));
198
199         PyList_SET_ITEM(data.mmiScreen, 0, tuple);
200
201         data.mmiTuplePos = 1;
202
203         return 0;
204 }
205
206 int eDVBCI_UI::mmiScreenAddText(int slot, int type, char *value)
207 {
208         if (slot >= MAX_SLOTS)
209                 return 0;
210
211         eDebug("eDVBCI_UI::mmiScreenAddText(%s)",value ? value : "");
212
213         slot_ui_data &data = slotdata[slot];
214
215         PyObject *tuple = PyTuple_New(3);
216
217         if (type == 0)                                  //title
218                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("TITLE"));
219         else if (type == 1)                             //subtitle
220                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("SUBTITLE"));
221         else if (type == 2)                             //bottom
222                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("BOTTOM"));
223         else
224                 PyTuple_SET_ITEM(tuple, 0, PyString_FromString("TEXT"));
225
226         eDebug("addText %s with id %d", value, type);
227
228         PyTuple_SET_ITEM(tuple, 1, PyString_FromString(value));
229
230         if (type > 2)
231                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(type-2));
232         else
233                 PyTuple_SET_ITEM(tuple, 2, PyInt_FromLong(-1));
234
235         PyList_Append(data.mmiScreen, tuple);
236         Py_DECREF(tuple);
237
238         return 0;
239 }
240
241 int eDVBCI_UI::mmiScreenFinish(int slot)
242 {
243         if (slot < MAX_SLOTS)
244         {
245                 printf("eDVBCI_UI::mmiScreenFinish\n");
246                 slotdata[slot].mmiScreenReady = 1;
247                 /*emit*/ ciStateChanged(slot);
248         }
249         return 0;
250 }
251
252 void eDVBCI_UI::mmiSessionDestroyed(int slot)
253 {
254         /*emit*/ ciStateChanged(slot);
255 }
256
257 int eDVBCI_UI::getMMIState(int slot)
258 {
259         return eDVBCIInterfaces::getInstance()->getMMIState(slot);
260 }
261
262 PyObject *eDVBCI_UI::getMMIScreen(int slot)
263 {
264         if (slot < MAX_SLOTS)
265         {
266                 slot_ui_data &data = slotdata[slot];
267                 if (data.mmiScreenReady)
268                 {
269                         data.mmiScreenReady = 0;
270                         Py_INCREF(data.mmiScreen);
271                         return data.mmiScreen;
272                 }
273         }
274         Py_INCREF(Py_None);
275         return Py_None;
276 }
277
278 //FIXME: correct "run/startlevel"
279 eAutoInitP0<eDVBCI_UI> init_dvbciui(eAutoInitNumbers::rc, "DVB-CI UI");