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