support for 'long keypresses' ('l' in keymap.xml flags)
[openblackhole/openblackhole-enigma2.git] / main / enigma.cpp
1 #ifdef WITH_SDL
2 #define SDLDC
3 #endif
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <libsig_comp.h>
8
9 #include <lib/actions/action.h>
10 #include <lib/driver/rc.h>
11 #include <lib/base/ioprio.h>
12 #include <lib/base/ebase.h>
13 #include <lib/base/eerror.h>
14 #include <lib/base/init.h>
15 #include <lib/base/init_num.h>
16 #include <lib/gdi/gfbdc.h>
17 #include <lib/gdi/glcddc.h>
18 #include <lib/gdi/grc.h>
19 #ifdef WITH_SDL
20 #include <lib/gdi/sdl.h>
21 #endif
22 #include <lib/gdi/epng.h>
23 #include <lib/gdi/font.h> 
24 #include <lib/gui/ebutton.h>
25 #include <lib/gui/elabel.h>
26 #include <lib/gui/elistboxcontent.h>
27 #include <lib/gui/ewidget.h>
28 #include <lib/gui/ewidgetdesktop.h>
29 #include <lib/gui/ewindow.h>
30 #include <lib/python/connections.h>
31 #include <lib/python/python.h>
32
33 #include "bsod.h" 
34
35 #ifdef HAVE_GSTREAMER
36 #include <gst/gst.h>
37 #endif
38
39 #ifdef OBJECT_DEBUG
40 int object_total_remaining;
41
42 void object_dump()
43 {
44         printf("%d items left\n", object_total_remaining);
45 }
46 #endif
47
48 static eWidgetDesktop *wdsk, *lcddsk;
49
50 static int prev_ascii_code;
51
52 int getPrevAsciiCode()
53 {
54         int ret = prev_ascii_code;
55         prev_ascii_code = 0;
56         return ret;
57 }
58
59 void keyEvent(const eRCKey &key)
60 {
61         static eRCKey last(0, 0, 0);
62         static int num_repeat;
63
64         ePtr<eActionMap> ptr;
65         eActionMap::getInstance(ptr);
66
67         if ((key.code == last.code) && (key.producer == last.producer) && key.flags & eRCKey::flagRepeat)
68                 num_repeat++;
69         else
70         {
71                 num_repeat = 0;
72                 last = key;
73         }
74
75         if (num_repeat == 4)
76         {
77                 ptr->keyPressed(key.producer->getIdentifier(), key.code, eRCKey::flagLong);
78                 num_repeat++;
79         }
80
81         if (key.flags & eRCKey::flagAscii)
82         {
83                 prev_ascii_code = key.code;
84                 ptr->keyPressed(key.producer->getIdentifier(), 510 /* faked KEY_ASCII */, 0);
85         }
86         else
87                 ptr->keyPressed(key.producer->getIdentifier(), key.code, key.flags);
88 }
89
90 /************************************************/
91 #include <unistd.h>
92 #include <lib/components/scan.h>
93 #include <lib/dvb/idvb.h>
94 #include <lib/dvb/dvb.h>
95 #include <lib/dvb/db.h>
96 #include <lib/dvb/dvbtime.h>
97 #include <lib/dvb/epgcache.h>
98
99 class eMain: public eApplication, public Object
100 {
101         eInit init;
102         
103         ePtr<eDVBResourceManager> m_mgr;
104         ePtr<eDVBDB> m_dvbdb;
105         ePtr<eDVBLocalTimeHandler> m_locale_time_handler;
106         ePtr<eComponentScan> m_scan;
107         ePtr<eEPGCache> m_epgcache;
108
109 public:
110         eMain()
111         {
112                 init.setRunlevel(eAutoInitNumbers::main);
113
114                                 /* TODO: put into init */
115                 m_dvbdb = new eDVBDB();
116                 m_mgr = new eDVBResourceManager();
117                 m_locale_time_handler = new eDVBLocalTimeHandler();
118                 m_epgcache = new eEPGCache();
119                 m_mgr->setChannelList(m_dvbdb);
120         }
121         
122         ~eMain()
123         {
124                 m_dvbdb->saveServicelist();
125                 m_scan = 0;
126         }
127 };
128
129 int exit_code;
130
131 int main(int argc, char **argv)
132 {
133 #ifdef MEMLEAK_CHECK
134         atexit(DumpUnfreed);
135 #endif
136
137 #ifdef OBJECT_DEBUG
138         atexit(object_dump);
139 #endif
140
141 #ifdef HAVE_GSTREAMER
142         gst_init(&argc, &argv);
143 #endif
144
145         // set pythonpath if unset
146         setenv("PYTHONPATH", LIBDIR "/enigma2/python", 0);
147         printf("PYTHONPATH: %s\n", getenv("PYTHONPATH"));
148         
149         bsodLogInit();
150
151         ePython python;
152         eMain main;
153         
154 #if 1
155 #ifdef SDLDC
156         ePtr<gSDLDC> my_dc;
157         gSDLDC::getInstance(my_dc);
158 #else
159         ePtr<gFBDC> my_dc;
160         gFBDC::getInstance(my_dc);
161         
162         int double_buffer = my_dc->haveDoubleBuffering();
163 #endif
164
165         ePtr<gLCDDC> my_lcd_dc;
166         gLCDDC::getInstance(my_lcd_dc);
167
168
169                 /* ok, this is currently hardcoded for arabic. */
170                         /* some characters are wrong in the regular font, force them to use the replacement font */
171         for (int i = 0x60c; i <= 0x66d; ++i)
172                 eTextPara::forceReplacementGlyph(i);
173         eTextPara::forceReplacementGlyph(0xfdf2);
174         for (int i = 0xfe80; i < 0xff00; ++i)
175                 eTextPara::forceReplacementGlyph(i);
176
177         eWidgetDesktop dsk(eSize(720, 576));
178         eWidgetDesktop dsk_lcd(eSize(132, 64));
179         
180         dsk.setStyleID(0);
181         dsk_lcd.setStyleID(1);
182         
183 /*      if (double_buffer)
184         {
185                 eDebug(" - double buffering found, enable buffered graphics mode.");
186                 dsk.setCompositionMode(eWidgetDesktop::cmBuffered);
187         } */
188         
189         wdsk = &dsk;
190         lcddsk = &dsk_lcd;
191
192         dsk.setDC(my_dc);
193         dsk_lcd.setDC(my_lcd_dc);
194
195         ePtr<gPixmap> m_pm;
196         loadPNG(m_pm, DATADIR "/enigma2/pal.png");
197         if (!m_pm)
198         {
199                 eFatal("pal.png not found!");
200         } else
201                 dsk.setPalette(*m_pm);
202
203         dsk.setBackgroundColor(gRGB(0,0,0,0xFF));
204 #endif
205
206                 /* redrawing is done in an idle-timer, so we have to set the context */
207         dsk.setRedrawTask(main);
208         dsk_lcd.setRedrawTask(main);
209         
210         
211         eDebug("Loading spinners...");
212         
213         {
214                 int i;
215                 ePtr<gPixmap> wait[4];
216                 for (i=0; i<4; ++i)
217                 {
218                         char filename[strlen(DATADIR) + 20];
219                         sprintf(filename, DATADIR "/enigma2/wait%d.png", i + 1);
220                         if (loadPNG(wait[i], filename))
221                         {
222                                 eDebug("failed to load %s! (%m)", filename);
223                                 continue;
224                         }
225                 }
226                 my_dc->setSpinner(eRect(100, 100, 105, 105), wait, 4);
227         }
228         
229         gRC::getInstance()->setSpinnerDC(my_dc);
230
231         eRCInput::getInstance()->keyEvent.connect(slot(keyEvent));
232         
233         printf("executing main\n");
234         
235         bsodCatchSignals();
236
237         setIoPrio(IOPRIO_CLASS_BE, 3);
238
239         python.execute("mytest", "__main__");
240         
241         if (exit_code == 5) /* python crash */
242                 bsodFatal();
243         
244         dsk.paint();
245         dsk_lcd.paint();
246
247         {
248                 gPainter p(my_lcd_dc);
249                 p.resetClip(eRect(0, 0, 132, 64));
250                 p.clear();
251                 p.flush();
252         }
253
254         return exit_code;
255 }
256
257 eWidgetDesktop *getDesktop(int which)
258 {
259         return which ? lcddsk : wdsk;
260 }
261
262 eApplication *getApplication()
263 {
264         return eApp;
265 }
266
267 void runMainloop()
268 {
269         eApp->runLoop();
270 }
271
272 void quitMainloop(int exitCode)
273 {
274         exit_code = exitCode;
275         eApp->quit(0);
276 }