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