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