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