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