[RecordTimer] fix repeat timer issue.
[vuplus_dvbapp] / 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 void quitMainloop(int exitCode)
126 {
127         FILE *f = fopen("/proc/stb/fp/was_timer_wakeup", "w");
128         if (f)
129         {
130                 fprintf(f, "%d", 0);
131                 fclose(f);
132         }
133         else
134         {
135                 int fd = open("/dev/dbox/fp0", O_WRONLY);
136                 if (fd >= 0)
137                 {
138                         if (ioctl(fd, 10 /*FP_CLEAR_WAKEUP_TIMER*/) < 0)
139                                 eDebug("FP_CLEAR_WAKEUP_TIMER failed (%m)");
140                         close(fd);
141                 }
142                 else
143                         eDebug("open /dev/dbox/fp0 for wakeup timer clear failed!(%m)");
144         }
145         exit_code = exitCode;
146         eApp->quit(0);
147 }
148
149 static void sigterm_handler(int num)
150 {
151         quitMainloop(128 + num);
152 }
153
154 void setSigTermHandler()
155 {
156         struct sigaction act;
157
158         act.sa_handler = sigterm_handler;
159         act.sa_flags = SA_RESTART;
160
161         if (sigemptyset(&act.sa_mask) == -1)
162                 perror("sigemptyset");
163         if (sigaction(SIGTERM, &act, 0) == -1)
164                 perror("SIGTERM");
165 }
166
167 int main(int argc, char **argv)
168 {
169 #ifdef MEMLEAK_CHECK
170         atexit(DumpUnfreed);
171 #endif
172
173 #ifdef OBJECT_DEBUG
174         atexit(object_dump);
175 #endif
176
177         gst_init(&argc, &argv);
178
179         // set pythonpath if unset
180         setenv("PYTHONPATH", eEnv::resolve("${libdir}/enigma2/python").c_str(), 0);
181         printf("PYTHONPATH: %s\n", getenv("PYTHONPATH"));
182         
183         bsodLogInit();
184
185         ePython python;
186         eMain main;
187
188 #if 1
189         ePtr<gMainDC> my_dc;
190         gMainDC::getInstance(my_dc);
191         
192         //int double_buffer = my_dc->haveDoubleBuffering();
193
194         ePtr<gLCDDC> my_lcd_dc;
195         gLCDDC::getInstance(my_lcd_dc);
196
197
198                 /* ok, this is currently hardcoded for arabic. */
199                         /* some characters are wrong in the regular font, force them to use the replacement font */
200         for (int i = 0x60c; i <= 0x66d; ++i)
201                 eTextPara::forceReplacementGlyph(i);
202         eTextPara::forceReplacementGlyph(0xfdf2);
203         for (int i = 0xfe80; i < 0xff00; ++i)
204                 eTextPara::forceReplacementGlyph(i);
205
206         eWidgetDesktop dsk(eSize(720, 576));
207         eWidgetDesktop dsk_lcd(my_lcd_dc->size());
208
209         dsk.setStyleID(0);
210         dsk_lcd.setStyleID(my_lcd_dc->size().width() == 96 ? 2 : 1);
211
212 /*      if (double_buffer)
213         {
214                 eDebug(" - double buffering found, enable buffered graphics mode.");
215                 dsk.setCompositionMode(eWidgetDesktop::cmBuffered);
216         } */
217         
218         wdsk = &dsk;
219         lcddsk = &dsk_lcd;
220
221         dsk.setDC(my_dc);
222         dsk_lcd.setDC(my_lcd_dc);
223
224         ePtr<gPixmap> m_pm;
225         loadPNG(m_pm, eEnv::resolve("${datadir}/enigma2/skin_default/pal.png").c_str());
226         if (!m_pm)
227         {
228                 eFatal("pal.png not found!");
229         } else
230                 dsk.setPalette(*m_pm);
231
232         dsk.setBackgroundColor(gRGB(0,0,0,0xFF));
233 #endif
234
235                 /* redrawing is done in an idle-timer, so we have to set the context */
236         dsk.setRedrawTask(main);
237         dsk_lcd.setRedrawTask(main);
238         
239         
240         eDebug("Loading spinners...");
241         
242         {
243                 int i;
244 #define MAX_SPINNER 64
245                 ePtr<gPixmap> wait[MAX_SPINNER];
246                 for (i=0; i<MAX_SPINNER; ++i)
247                 {
248                         char filename[64];
249                         std::string rfilename;
250                         snprintf(filename, sizeof(filename), "${datadir}/enigma2/skin_default/spinner/wait%d.png", i + 1);
251                         rfilename = eEnv::resolve(filename);
252                         loadPNG(wait[i], rfilename.c_str());
253                         
254                         if (!wait[i])
255                         {
256                                 if (!i)
257                                         eDebug("failed to load %s! (%m)", rfilename.c_str());
258                                 else
259                                         eDebug("found %d spinner!\n", i);
260                                 break;
261                         }
262                 }
263                 if (i)
264                         my_dc->setSpinner(eRect(ePoint(100, 100), wait[0]->size()), wait, i);
265                 else
266                         my_dc->setSpinner(eRect(100, 100, 0, 0), wait, 1);
267         }
268         
269         gRC::getInstance()->setSpinnerDC(my_dc);
270
271         eRCInput::getInstance()->keyEvent.connect(slot(keyEvent));
272         
273         printf("executing main\n");
274         
275         bsodCatchSignals();
276
277         setSigTermHandler();
278
279         setIoPrio(IOPRIO_CLASS_BE, 3);
280
281 //      python.execute("mytest", "__main__");
282         python.execFile(eEnv::resolve("${libdir}/enigma2/python/mytest.py").c_str());
283
284         extern void setFullsize(); // definend in lib/gui/evideo.cpp
285         setFullsize();
286
287         if (exit_code == 5) /* python crash */
288         {
289                 eDebug("(exit code 5)");
290                 bsodFatal(0);
291         }
292         
293         dsk.paint();
294         dsk_lcd.paint();
295
296         {
297                 gPainter p(my_lcd_dc);
298                 p.resetClip(eRect(ePoint(0, 0), my_lcd_dc->size()));
299                 p.clear();
300                 p.flush();
301         }
302
303         return exit_code;
304 }
305
306 eWidgetDesktop *getDesktop(int which)
307 {
308         return which ? lcddsk : wdsk;
309 }
310
311 eApplication *getApplication()
312 {
313         return eApp;
314 }
315
316 void runMainloop()
317 {
318         setSigTermHandler();
319         eApp->runLoop();
320 }
321
322 const char *getEnigmaVersionString()
323 {
324         std::string date = enigma2_date;
325         std::string branch = enigma2_branch;
326         return std::string(date + '-' + branch).c_str();
327 }
328
329 #include <malloc.h>
330
331 void dump_malloc_stats(void)
332 {
333         struct mallinfo mi = mallinfo();
334         eDebug("MALLOC: %d total", mi.uordblks);
335 }
336
337 #ifdef USE_LIBVUGLES2
338 #include <vuplus_gles.h>
339
340 void setAnimation_current(int a)
341 {
342         gles_set_animation_func(a);
343 }
344
345 void setAnimation_speed(int speed)
346 {
347         gles_set_animation_speed(speed);
348 }
349
350 void setAnimation_current_listbox(int a)
351 {
352         gles_set_animation_listbox_func(a);
353 }
354 #else
355 void setAnimation_current(int a) {}
356 void setAnimation_speed(int speed) {}
357 void setAnimation_current_listbox(int a) {}
358 #endif