14627f4b691a3cd392a8ccbe7ad66067e48cd3c2
[openblackhole/openblackhole-enigma2.git] / main / enigma.cpp
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/ioctl.h>
6 #include <libsig_comp.h>
7
8 #include <lib/actions/action.h>
9 #include <lib/driver/rc.h>
10 #include <lib/base/ioprio.h>
11 #include <lib/base/ebase.h>
12 #include <lib/base/eenv.h>
13 #include <lib/base/eerror.h>
14 #include <lib/base/init.h>
15 #include <lib/base/init_num.h>
16 #include <lib/gdi/gmaindc.h>
17 #include <lib/gdi/glcddc.h>
18 #include <lib/gdi/grc.h>
19 #include <lib/gdi/epng.h>
20 #include <lib/gdi/font.h>
21 #include <lib/gui/ebutton.h>
22 #include <lib/gui/elabel.h>
23 #include <lib/gui/elistboxcontent.h>
24 #include <lib/gui/ewidget.h>
25 #include <lib/gui/ewidgetdesktop.h>
26 #include <lib/gui/ewindow.h>
27 #include <lib/gui/evideo.h>
28 #include <lib/python/connections.h>
29 #include <lib/python/python.h>
30 #include <lib/python/pythonconfig.h>
31
32 #include "bsod.h"
33 #include "version_info.h"
34
35 #include <gst/gst.h>
36
37 #ifdef OBJECT_DEBUG
38 int object_total_remaining;
39
40 void object_dump()
41 {
42         printf("%d items left\n", object_total_remaining);
43 }
44 #endif
45
46 static eWidgetDesktop *wdsk, *lcddsk;
47
48 static int prev_ascii_code;
49
50 int getPrevAsciiCode()
51 {
52         int ret = prev_ascii_code;
53         prev_ascii_code = 0;
54         return ret;
55 }
56
57 void keyEvent(const eRCKey &key)
58 {
59         static eRCKey last(0, 0, 0);
60         static int num_repeat;
61
62         ePtr<eActionMap> ptr;
63         eActionMap::getInstance(ptr);
64
65         if ((key.code == last.code) && (key.producer == last.producer) && key.flags & eRCKey::flagRepeat)
66                 num_repeat++;
67         else
68         {
69                 num_repeat = 0;
70                 last = key;
71         }
72
73         if (num_repeat == 4)
74         {
75                 ptr->keyPressed(key.producer->getIdentifier(), key.code, eRCKey::flagLong);
76                 num_repeat++;
77         }
78
79         if (key.flags & eRCKey::flagAscii)
80         {
81                 prev_ascii_code = key.code;
82                 ptr->keyPressed(key.producer->getIdentifier(), 510 /* faked KEY_ASCII */, 0);
83         }
84         else
85                 ptr->keyPressed(key.producer->getIdentifier(), key.code, key.flags);
86 }
87
88 /************************************************/
89 #include <unistd.h>
90 #include <lib/components/scan.h>
91 #include <lib/dvb/idvb.h>
92 #include <lib/dvb/dvb.h>
93 #include <lib/dvb/db.h>
94 #include <lib/dvb/dvbtime.h>
95 #include <lib/dvb/epgcache.h>
96
97 class eMain: public eApplication, public Object
98 {
99         eInit init;
100         ePythonConfigQuery config;
101
102         ePtr<eDVBDB> m_dvbdb;
103         ePtr<eDVBResourceManager> m_mgr;
104         ePtr<eDVBLocalTimeHandler> m_locale_time_handler;
105         ePtr<eEPGCache> m_epgcache;
106
107 public:
108         eMain()
109         {
110                 init.setRunlevel(eAutoInitNumbers::main);
111                 /* TODO: put into init */
112                 m_dvbdb = new eDVBDB();
113                 m_mgr = new eDVBResourceManager();
114                 m_locale_time_handler = new eDVBLocalTimeHandler();
115                 m_epgcache = new eEPGCache();
116                 m_mgr->setChannelList(m_dvbdb);
117         }
118
119         ~eMain()
120         {
121                 m_dvbdb->saveServicelist();
122                 m_mgr->releaseCachedChannel();
123         }
124 };
125
126 int exit_code;
127
128 int main(int argc, char **argv)
129 {
130 #ifdef MEMLEAK_CHECK
131         atexit(DumpUnfreed);
132 #endif
133
134 #ifdef OBJECT_DEBUG
135         atexit(object_dump);
136 #endif
137
138         gst_init(&argc, &argv);
139
140         // set pythonpath if unset
141         setenv("PYTHONPATH", eEnv::resolve("${libdir}/enigma2/python").c_str(), 0);
142         printf("PYTHONPATH: %s\n", getenv("PYTHONPATH"));
143
144         bsodLogInit();
145
146         ePython python;
147         eMain main;
148
149 #if 1
150         ePtr<gMainDC> my_dc;
151         gMainDC::getInstance(my_dc);
152
153         //int double_buffer = my_dc->haveDoubleBuffering();
154
155         ePtr<gLCDDC> my_lcd_dc;
156         gLCDDC::getInstance(my_lcd_dc);
157
158
159                 /* ok, this is currently hardcoded for arabic. */
160                         /* some characters are wrong in the regular font, force them to use the replacement font */
161         for (int i = 0x60c; i <= 0x66d; ++i)
162                 eTextPara::forceReplacementGlyph(i);
163         eTextPara::forceReplacementGlyph(0xfdf2);
164         for (int i = 0xfe80; i < 0xff00; ++i)
165                 eTextPara::forceReplacementGlyph(i);
166
167         eWidgetDesktop dsk(my_dc->size());
168         eWidgetDesktop dsk_lcd(my_lcd_dc->size());
169
170         dsk.setStyleID(0);
171         dsk_lcd.setStyleID(my_lcd_dc->size().width() == 96 ? 2 : 1);
172
173 /*      if (double_buffer)
174         {
175                 eDebug(" - double buffering found, enable buffered graphics mode.");
176                 dsk.setCompositionMode(eWidgetDesktop::cmBuffered);
177         } */
178
179         wdsk = &dsk;
180         lcddsk = &dsk_lcd;
181
182         dsk.setDC(my_dc);
183         dsk_lcd.setDC(my_lcd_dc);
184
185         dsk.setBackgroundColor(gRGB(0,0,0,0xFF));
186 #endif
187
188                 /* redrawing is done in an idle-timer, so we have to set the context */
189         dsk.setRedrawTask(main);
190         dsk_lcd.setRedrawTask(main);
191
192
193         eDebug("Loading spinners...");
194
195         {
196                 int i;
197 #define MAX_SPINNER 64
198                 ePtr<gPixmap> wait[MAX_SPINNER];
199                 for (i=0; i<MAX_SPINNER; ++i)
200                 {
201                         char filename[64];
202                         std::string rfilename;
203                         snprintf(filename, sizeof(filename), "${datadir}/enigma2/skin_default/spinner/wait%d.png", i + 1);
204                         rfilename = eEnv::resolve(filename);
205                         loadPNG(wait[i], rfilename.c_str());
206
207                         if (!wait[i])
208                         {
209                                 if (!i)
210                                         eDebug("failed to load %s! (%m)", rfilename.c_str());
211                                 else
212                                         eDebug("found %d spinner!\n", i);
213                                 break;
214                         }
215                 }
216                 if (i)
217                         my_dc->setSpinner(eRect(ePoint(100, 100), wait[0]->size()), wait, i);
218                 else
219                         my_dc->setSpinner(eRect(100, 100, 0, 0), wait, 1);
220         }
221
222         gRC::getInstance()->setSpinnerDC(my_dc);
223
224         eRCInput::getInstance()->keyEvent.connect(slot(keyEvent));
225
226         printf("executing main\n");
227
228         bsodCatchSignals();
229
230         setIoPrio(IOPRIO_CLASS_BE, 3);
231
232         /* start at full size */
233         eVideoWidget::setFullsize(true);
234
235 //      python.execute("mytest", "__main__");
236         python.execFile(eEnv::resolve("${libdir}/enigma2/python/mytest.py").c_str());
237
238         /* restore both decoders to full size */
239         eVideoWidget::setFullsize(true);
240
241         if (exit_code == 5) /* python crash */
242         {
243                 eDebug("(exit code 5)");
244                 bsodFatal(0);
245         }
246
247         dsk.paint();
248         dsk_lcd.paint();
249
250         {
251                 gPainter p(my_lcd_dc);
252                 p.resetClip(eRect(ePoint(0, 0), my_lcd_dc->size()));
253                 p.clear();
254                 p.flush();
255         }
256
257         return exit_code;
258 }
259
260 eWidgetDesktop *getDesktop(int which)
261 {
262         return which ? lcddsk : wdsk;
263 }
264
265 eApplication *getApplication()
266 {
267         return eApp;
268 }
269
270 void quitMainloop(int exitCode)
271 {
272         FILE *f = fopen("/proc/stb/fp/was_timer_wakeup", "w");
273         if (f)
274         {
275                 fprintf(f, "%d", 0);
276                 fclose(f);
277         }
278         else
279         {
280                 int fd = open("/dev/dbox/fp0", O_WRONLY);
281                 if (fd >= 0)
282                 {
283                         if (ioctl(fd, 10 /*FP_CLEAR_WAKEUP_TIMER*/) < 0)
284                                 eDebug("FP_CLEAR_WAKEUP_TIMER failed (%m)");
285                         close(fd);
286                 }
287                 else
288                         eDebug("open /dev/dbox/fp0 for wakeup timer clear failed!(%m)");
289         }
290         exit_code = exitCode;
291         eApp->quit(0);
292 }
293
294 static void sigterm_handler(int num)
295 {
296         quitMainloop(128 + num);
297 }
298
299 void runMainloop()
300 {
301         struct sigaction act;
302
303         act.sa_handler = sigterm_handler;
304         act.sa_flags = SA_RESTART;
305
306         if (sigemptyset(&act.sa_mask) == -1)
307                 perror("sigemptyset");
308         if (sigaction(SIGTERM, &act, 0) == -1)
309                 perror("SIGTERM");
310
311         eApp->runLoop();
312 }
313
314 const char *getEnigmaVersionString()
315 {
316         return enigma2_version;
317 }
318
319 const char *getBoxType()
320 {
321         return BOXTYPE;
322 }
323
324 #include <malloc.h>
325
326 void dump_malloc_stats(void)
327 {
328         struct mallinfo mi = mallinfo();
329         eDebug("MALLOC: %d total", mi.uordblks);
330 }