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