Merge remote branch 'remotes/origin/bug_135_dvdburn_ts_only' into experimental
[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/eerror.h>
13 #include <lib/base/init.h>
14 #include <lib/base/init_num.h>
15 #include <lib/gdi/gfbdc.h>
16 #include <lib/gdi/glcddc.h>
17 #include <lib/gdi/grc.h>
18 #ifdef WITH_SDL
19 #include <lib/gdi/sdl.h>
20 #endif
21 #include <lib/gdi/epng.h>
22 #include <lib/gdi/font.h>
23 #include <lib/gui/ebutton.h>
24 #include <lib/gui/elabel.h>
25 #include <lib/gui/elistboxcontent.h>
26 #include <lib/gui/ewidget.h>
27 #include <lib/gui/ewidgetdesktop.h>
28 #include <lib/gui/ewindow.h>
29 #include <lib/python/connections.h>
30 #include <lib/python/python.h>
31
32 #include "bsod.h"
33
34 #ifdef HAVE_GSTREAMER
35 #include <gst/gst.h>
36 #endif
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
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 #ifdef HAVE_GSTREAMER
139         gst_init(&argc, &argv);
140 #endif
141
142         // set pythonpath if unset
143         setenv("PYTHONPATH", LIBDIR "/enigma2/python", 0);
144         printf("PYTHONPATH: %s\n", getenv("PYTHONPATH"));
145         
146         bsodLogInit();
147
148         ePython python;
149         eMain main;
150
151 #if 1
152 #ifdef WITH_SDL
153         ePtr<gSDLDC> my_dc;
154         gSDLDC::getInstance(my_dc);
155 #else
156         ePtr<gFBDC> my_dc;
157         gFBDC::getInstance(my_dc);
158         
159         int double_buffer = my_dc->haveDoubleBuffering();
160 #endif
161
162         ePtr<gLCDDC> my_lcd_dc;
163         gLCDDC::getInstance(my_lcd_dc);
164
165
166                 /* ok, this is currently hardcoded for arabic. */
167                         /* some characters are wrong in the regular font, force them to use the replacement font */
168         for (int i = 0x60c; i <= 0x66d; ++i)
169                 eTextPara::forceReplacementGlyph(i);
170         eTextPara::forceReplacementGlyph(0xfdf2);
171         for (int i = 0xfe80; i < 0xff00; ++i)
172                 eTextPara::forceReplacementGlyph(i);
173
174         eWidgetDesktop dsk(eSize(720, 576));
175         eWidgetDesktop dsk_lcd(eSize(132, 64));
176         
177         dsk.setStyleID(0);
178         dsk_lcd.setStyleID(1);
179         
180 /*      if (double_buffer)
181         {
182                 eDebug(" - double buffering found, enable buffered graphics mode.");
183                 dsk.setCompositionMode(eWidgetDesktop::cmBuffered);
184         } */
185         
186         wdsk = &dsk;
187         lcddsk = &dsk_lcd;
188
189         dsk.setDC(my_dc);
190         dsk_lcd.setDC(my_lcd_dc);
191
192         ePtr<gPixmap> m_pm;
193         loadPNG(m_pm, DATADIR "/enigma2/skin_default/pal.png");
194         if (!m_pm)
195         {
196                 eFatal("pal.png not found!");
197         } else
198                 dsk.setPalette(*m_pm);
199
200         dsk.setBackgroundColor(gRGB(0,0,0,0xFF));
201 #endif
202
203                 /* redrawing is done in an idle-timer, so we have to set the context */
204         dsk.setRedrawTask(main);
205         dsk_lcd.setRedrawTask(main);
206         
207         
208         eDebug("Loading spinners...");
209         
210         {
211                 int i;
212 #define MAX_SPINNER 64
213                 ePtr<gPixmap> wait[MAX_SPINNER];
214                 for (i=0; i<MAX_SPINNER; ++i)
215                 {
216                         char filename[strlen(DATADIR) + 41];
217                         sprintf(filename, DATADIR "/enigma2/skin_default/spinner/wait%d.png", i + 1);
218                         loadPNG(wait[i], filename);
219                         
220                         if (!wait[i])
221                         {
222                                 if (!i)
223                                         eDebug("failed to load %s! (%m)", filename);
224                                 else
225                                         eDebug("found %d spinner!\n", i);
226                                 break;
227                         }
228                 }
229                 if (i)
230                         my_dc->setSpinner(eRect(ePoint(100, 100), wait[0]->size()), wait, i);
231                 else
232                         my_dc->setSpinner(eRect(100, 100, 0, 0), wait, 1);
233         }
234         
235         gRC::getInstance()->setSpinnerDC(my_dc);
236
237         eRCInput::getInstance()->keyEvent.connect(slot(keyEvent));
238         
239         printf("executing main\n");
240         
241         bsodCatchSignals();
242
243         setIoPrio(IOPRIO_CLASS_BE, 3);
244
245 //      python.execute("mytest", "__main__");
246         python.execFile("/usr/lib/enigma2/python/mytest.py");
247
248         extern void setFullsize(); // definend in lib/gui/evideo.cpp
249         setFullsize();
250
251         if (exit_code == 5) /* python crash */
252         {
253                 eDebug("(exit code 5)");
254                 bsodFatal(0);
255         }
256         
257         dsk.paint();
258         dsk_lcd.paint();
259
260         {
261                 gPainter p(my_lcd_dc);
262                 p.resetClip(eRect(0, 0, 132, 64));
263                 p.clear();
264         }
265
266         return exit_code;
267 }
268
269 eWidgetDesktop *getDesktop(int which)
270 {
271         return which ? lcddsk : wdsk;
272 }
273
274 eApplication *getApplication()
275 {
276         return eApp;
277 }
278
279 void runMainloop()
280 {
281         eApp->runLoop();
282 }
283
284 void quitMainloop(int exitCode)
285 {
286         FILE *f = fopen("/proc/stb/fp/was_timer_wakeup", "w");
287         if (f)
288         {
289                 fprintf(f, "%d", 0);
290                 fclose(f);
291         }
292         else
293         {
294                 int fd = open("/dev/dbox/fp0", O_WRONLY);
295                 if (fd >= 0)
296                 {
297                         if (ioctl(fd, 10 /*FP_CLEAR_WAKEUP_TIMER*/) < 0)
298                                 eDebug("FP_CLEAR_WAKEUP_TIMER failed (%m)");
299                         close(fd);
300                 }
301                 else
302                         eDebug("open /dev/dbox/fp0 for wakeup timer clear failed!(%m)");
303         }
304         exit_code = exitCode;
305         eApp->quit(0);
306 }
307
308 #include "version.h"
309
310 const char *getEnigmaVersionString()
311 {
312         std::string date =
313 #ifdef ENIGMA2_LAST_CHANGE_DATE
314                 ENIGMA2_LAST_CHANGE_DATE;
315 #else
316                 __DATE__;
317 #endif
318         std::string branch =
319 #ifdef ENIGMA2_BRANCH
320                 ENIGMA2_BRANCH;
321 #else
322                 "HEAD";
323 #endif
324         return std::string(date + '-' + branch).c_str();
325 }
326
327 #include <malloc.h>
328
329 void dump_malloc_stats(void)
330 {
331         struct mallinfo mi = mallinfo();
332         eDebug("MALLOC: %d total", mi.uordblks);
333 }