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