implement configurable http user-agent (fixes #224)
[enigma2.git] / main / enigma.cpp
index f81a3c28f7ddf0a0f4ee870464599b8daa6c249b..b8554d582c1b72f6788002509a36ae3c6a4f698d 100644 (file)
@@ -1,42 +1,39 @@
-#undef WITH_SDL
-#ifdef WITH_SDL
-#error
-#define SDLDC
-#endif
+#include <unistd.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
 #include <libsig_comp.h>
 #include <libsig_comp.h>
+
+#include <lib/actions/action.h>
+#include <lib/driver/rc.h>
+#include <lib/base/ioprio.h>
 #include <lib/base/ebase.h>
 #include <lib/base/eerror.h>
 #include <lib/base/init.h>
 #include <lib/base/init_num.h>
 #include <lib/base/ebase.h>
 #include <lib/base/eerror.h>
 #include <lib/base/init.h>
 #include <lib/base/init_num.h>
-
-#include <unistd.h>
-
-#include <lib/gdi/grc.h>
 #include <lib/gdi/gfbdc.h>
 #include <lib/gdi/gfbdc.h>
+#include <lib/gdi/glcddc.h>
+#include <lib/gdi/grc.h>
 #ifdef WITH_SDL
 #ifdef WITH_SDL
-#error
 #include <lib/gdi/sdl.h>
 #endif
 #include <lib/gdi/sdl.h>
 #endif
-#include <lib/gdi/font.h> 
-
+#include <lib/gdi/epng.h>
+#include <lib/gdi/font.h>
+#include <lib/gui/ebutton.h>
+#include <lib/gui/elabel.h>
+#include <lib/gui/elistboxcontent.h>
 #include <lib/gui/ewidget.h>
 #include <lib/gui/ewidgetdesktop.h>
 #include <lib/gui/ewidget.h>
 #include <lib/gui/ewidgetdesktop.h>
-#include <lib/gui/elabel.h>
-#include <lib/gui/ebutton.h>
-
 #include <lib/gui/ewindow.h>
 #include <lib/gui/ewindow.h>
-
-#include <lib/python/python.h>
 #include <lib/python/connections.h>
 #include <lib/python/connections.h>
+#include <lib/python/python.h>
 
 
-#include <lib/gui/elistboxcontent.h>
-
-#include <lib/driver/rc.h>
-
-#include <lib/actions/action.h>
+#include "bsod.h"
 
 
-#include <lib/gdi/epng.h>
+#ifdef HAVE_GSTREAMER
+#include <gst/gst.h>
+#endif
 
 #ifdef OBJECT_DEBUG
 int object_total_remaining;
 
 #ifdef OBJECT_DEBUG
 int object_total_remaining;
@@ -47,51 +44,46 @@ void object_dump()
 }
 #endif
 
 }
 #endif
 
-void dumpRegion(const gRegion &region)
-{
-       fprintf(stderr, "extends: %d %d -> %d %d (%d rects)\n", 
-               region.extends.left(), region.extends.top(),
-               region.extends.right(), region.extends.bottom(), region.rects.size());
-#if 0
-       for (int y=0; y<region.extends.bottom(); ++y)
-       {
-               for (int x=0; x<region.extends.right(); ++x)
-               {
-                       unsigned char res = ' ';
-                       for (unsigned int i=0; i < region.rects.size(); ++i)
-                               if (region.rects[i].contains(ePoint(x, y)))
-                                       res = '0' + i;
-                       fprintf(stderr, "%c", res);
-               }
-               fprintf(stderr, "\n");
-       }
-#endif
-
-}
-
-eWidgetDesktop *wdsk;
-
-// typedef struct _object PyObject;
-
-void print(int i)
-{
-       printf("C++ says: it's a %d!!!\n", i);
-}
+static eWidgetDesktop *wdsk, *lcddsk;
 
 
-PSignal1<void,int> keyPressed;
+static int prev_ascii_code;
 
 
-PSignal1<void,int> &keyPressedSignal()
+int getPrevAsciiCode()
 {
 {
-       return keyPressed;
+       int ret = prev_ascii_code;
+       prev_ascii_code = 0;
+       return ret;
 }
 
 void keyEvent(const eRCKey &key)
 {
 }
 
 void keyEvent(const eRCKey &key)
 {
+       static eRCKey last(0, 0, 0);
+       static int num_repeat;
+
        ePtr<eActionMap> ptr;
        eActionMap::getInstance(ptr);
        ePtr<eActionMap> ptr;
        eActionMap::getInstance(ptr);
-       ptr->keyPressed(0, key.code, key.flags);
-//     if (!key.flags)
-//             keyPressed(key.code);
+
+       if ((key.code == last.code) && (key.producer == last.producer) && key.flags & eRCKey::flagRepeat)
+               num_repeat++;
+       else
+       {
+               num_repeat = 0;
+               last = key;
+       }
+
+       if (num_repeat == 4)
+       {
+               ptr->keyPressed(key.producer->getIdentifier(), key.code, eRCKey::flagLong);
+               num_repeat++;
+       }
+
+       if (key.flags & eRCKey::flagAscii)
+       {
+               prev_ascii_code = key.code;
+               ptr->keyPressed(key.producer->getIdentifier(), 510 /* faked KEY_ASCII */, 0);
+       }
+       else
+               ptr->keyPressed(key.producer->getIdentifier(), key.code, key.flags);
 }
 
 /************************************************/
 }
 
 /************************************************/
@@ -106,81 +98,102 @@ void keyEvent(const eRCKey &key)
 class eMain: public eApplication, public Object
 {
        eInit init;
 class eMain: public eApplication, public Object
 {
        eInit init;
-       
-       ePtr<eDVBResourceManager> m_mgr;
+
        ePtr<eDVBDB> m_dvbdb;
        ePtr<eDVBDB> m_dvbdb;
+       ePtr<eDVBResourceManager> m_mgr;
        ePtr<eDVBLocalTimeHandler> m_locale_time_handler;
        ePtr<eDVBLocalTimeHandler> m_locale_time_handler;
-       ePtr<eComponentScan> m_scan;
        ePtr<eEPGCache> m_epgcache;
 
 public:
        eMain()
        {
                init.setRunlevel(eAutoInitNumbers::main);
        ePtr<eEPGCache> m_epgcache;
 
 public:
        eMain()
        {
                init.setRunlevel(eAutoInitNumbers::main);
-
-                               /* TODO: put into init */
+               /* TODO: put into init */
                m_dvbdb = new eDVBDB();
                m_mgr = new eDVBResourceManager();
                m_locale_time_handler = new eDVBLocalTimeHandler();
                m_epgcache = new eEPGCache();
                m_mgr->setChannelList(m_dvbdb);
                m_dvbdb = new eDVBDB();
                m_mgr = new eDVBResourceManager();
                m_locale_time_handler = new eDVBLocalTimeHandler();
                m_epgcache = new eEPGCache();
                m_mgr->setChannelList(m_dvbdb);
-               
-//             m_scan = new eComponentScan();
-//             m_scan->start();
-
        }
        
        ~eMain()
        {
        }
        
        ~eMain()
        {
-               m_scan = 0;
+               m_dvbdb->saveServicelist();
+               m_mgr->releaseCachedChannel();
        }
 };
 
        }
 };
 
-/************************************************/
-
+int exit_code;
 
 int main(int argc, char **argv)
 {
 
 int main(int argc, char **argv)
 {
+#ifdef MEMLEAK_CHECK
+       atexit(DumpUnfreed);
+#endif
+
 #ifdef OBJECT_DEBUG
        atexit(object_dump);
 #endif
 
 #ifdef OBJECT_DEBUG
        atexit(object_dump);
 #endif
 
+#ifdef HAVE_GSTREAMER
+       gst_init(&argc, &argv);
+#endif
+
+       // set pythonpath if unset
+       setenv("PYTHONPATH", LIBDIR "/enigma2/python", 0);
+       printf("PYTHONPATH: %s\n", getenv("PYTHONPATH"));
+       
+       bsodLogInit();
 
        ePython python;
        eMain main;
 
        ePython python;
        eMain main;
-       
-       int double_buffer = 0;
 
 #if 1
 
 #if 1
-#ifdef SDLDC
+#ifdef WITH_SDL
        ePtr<gSDLDC> my_dc;
        gSDLDC::getInstance(my_dc);
 #else
        ePtr<gFBDC> my_dc;
        gFBDC::getInstance(my_dc);
        
        ePtr<gSDLDC> my_dc;
        gSDLDC::getInstance(my_dc);
 #else
        ePtr<gFBDC> my_dc;
        gFBDC::getInstance(my_dc);
        
-       double_buffer = my_dc->haveDoubleBuffering();
+       int double_buffer = my_dc->haveDoubleBuffering();
 #endif
 
 #endif
 
-       fontRenderClass::getInstance()->AddFont(FONTDIR "/arial.ttf", "Arial", 100);
+       ePtr<gLCDDC> my_lcd_dc;
+       gLCDDC::getInstance(my_lcd_dc);
+
+
+               /* ok, this is currently hardcoded for arabic. */
+                       /* some characters are wrong in the regular font, force them to use the replacement font */
+       for (int i = 0x60c; i <= 0x66d; ++i)
+               eTextPara::forceReplacementGlyph(i);
+       eTextPara::forceReplacementGlyph(0xfdf2);
+       for (int i = 0xfe80; i < 0xff00; ++i)
+               eTextPara::forceReplacementGlyph(i);
 
        eWidgetDesktop dsk(eSize(720, 576));
 
        eWidgetDesktop dsk(eSize(720, 576));
-       
-       if (double_buffer)
+       eWidgetDesktop dsk_lcd(my_lcd_dc->size());
+
+       dsk.setStyleID(0);
+       dsk_lcd.setStyleID(my_lcd_dc->size().width() == 96 ? 2 : 1);
+
+/*     if (double_buffer)
        {
                eDebug(" - double buffering found, enable buffered graphics mode.");
                dsk.setCompositionMode(eWidgetDesktop::cmBuffered);
        {
                eDebug(" - double buffering found, enable buffered graphics mode.");
                dsk.setCompositionMode(eWidgetDesktop::cmBuffered);
-       }
+       } */
        
        wdsk = &dsk;
        
        wdsk = &dsk;
+       lcddsk = &dsk_lcd;
 
        dsk.setDC(my_dc);
 
        dsk.setDC(my_dc);
+       dsk_lcd.setDC(my_lcd_dc);
 
        ePtr<gPixmap> m_pm;
 
        ePtr<gPixmap> m_pm;
-       loadPNG(m_pm, "data/pal.png");
+       loadPNG(m_pm, DATADIR "/enigma2/skin_default/pal.png");
        if (!m_pm)
        {
        if (!m_pm)
        {
-               eFatal("hi ghost, please copy pal.png into your ./data, thanks!");
+               eFatal("pal.png not found!");
        } else
                dsk.setPalette(*m_pm);
 
        } else
                dsk.setPalette(*m_pm);
 
@@ -189,29 +202,132 @@ int main(int argc, char **argv)
 
                /* redrawing is done in an idle-timer, so we have to set the context */
        dsk.setRedrawTask(main);
 
                /* redrawing is done in an idle-timer, so we have to set the context */
        dsk.setRedrawTask(main);
+       dsk_lcd.setRedrawTask(main);
+       
+       
+       eDebug("Loading spinners...");
+       
+       {
+               int i;
+#define MAX_SPINNER 64
+               ePtr<gPixmap> wait[MAX_SPINNER];
+               for (i=0; i<MAX_SPINNER; ++i)
+               {
+                       char filename[strlen(DATADIR) + 41];
+                       sprintf(filename, DATADIR "/enigma2/skin_default/spinner/wait%d.png", i + 1);
+                       loadPNG(wait[i], filename);
+                       
+                       if (!wait[i])
+                       {
+                               if (!i)
+                                       eDebug("failed to load %s! (%m)", filename);
+                               else
+                                       eDebug("found %d spinner!\n", i);
+                               break;
+                       }
+               }
+               if (i)
+                       my_dc->setSpinner(eRect(ePoint(100, 100), wait[0]->size()), wait, i);
+               else
+                       my_dc->setSpinner(eRect(100, 100, 0, 0), wait, 1);
+       }
        
        
+       gRC::getInstance()->setSpinnerDC(my_dc);
+
        eRCInput::getInstance()->keyEvent.connect(slot(keyEvent));
        
        printf("executing main\n");
        eRCInput::getInstance()->keyEvent.connect(slot(keyEvent));
        
        printf("executing main\n");
+       
+       bsodCatchSignals();
+
+       setIoPrio(IOPRIO_CLASS_BE, 3);
 
 
-       python.execute("mytest", "__main__");
+//     python.execute("mytest", "__main__");
+       python.execFile("/usr/lib/enigma2/python/mytest.py");
 
 
-//     eApp->exec();
+       extern void setFullsize(); // definend in lib/gui/evideo.cpp
+       setFullsize();
 
 
-       return 0;
+       if (exit_code == 5) /* python crash */
+       {
+               eDebug("(exit code 5)");
+               bsodFatal(0);
+       }
+       
+       dsk.paint();
+       dsk_lcd.paint();
+
+       {
+               gPainter p(my_lcd_dc);
+               p.resetClip(eRect(0, 0, 132, 64));
+               p.clear();
+       }
+
+       return exit_code;
+}
+
+eWidgetDesktop *getDesktop(int which)
+{
+       return which ? lcddsk : wdsk;
 }
 
 }
 
-eWidgetDesktop *getDesktop()
+eApplication *getApplication()
 {
 {
-       return wdsk;
+       return eApp;
 }
 
 void runMainloop()
 {
 }
 
 void runMainloop()
 {
-       eApp->exec();
+       eApp->runLoop();
 }
 
 }
 
-void quitMainloop()
+void quitMainloop(int exitCode)
 {
 {
+       FILE *f = fopen("/proc/stb/fp/was_timer_wakeup", "w");
+       if (f)
+       {
+               fprintf(f, "%d", 0);
+               fclose(f);
+       }
+       else
+       {
+               int fd = open("/dev/dbox/fp0", O_WRONLY);
+               if (fd >= 0)
+               {
+                       if (ioctl(fd, 10 /*FP_CLEAR_WAKEUP_TIMER*/) < 0)
+                               eDebug("FP_CLEAR_WAKEUP_TIMER failed (%m)");
+                       close(fd);
+               }
+               else
+                       eDebug("open /dev/dbox/fp0 for wakeup timer clear failed!(%m)");
+       }
+       exit_code = exitCode;
        eApp->quit(0);
 }
        eApp->quit(0);
 }
+
+#include "version.h"
+
+const char *getEnigmaVersionString()
+{
+       std::string date =
+#ifdef ENIGMA2_LAST_CHANGE_DATE
+               ENIGMA2_LAST_CHANGE_DATE;
+#else
+               __DATE__;
+#endif
+       std::string branch =
+#ifdef ENIGMA2_BRANCH
+               ENIGMA2_BRANCH;
+#else
+               "HEAD";
+#endif
+       return std::string(date + '-' + branch).c_str();
+}
+
+#include <malloc.h>
+
+void dump_malloc_stats(void)
+{
+       struct mallinfo mi = mallinfo();
+       eDebug("MALLOC: %d total", mi.uordblks);
+}