int CCApplication::run()
{
    PVRFrameEnableControlWindow(false);
	CCLog("--- INTO CCApplication::run \n");

    // Main message loop:
    
    // Initialize instance and cocos2d.
    if (! initInstance() || ! applicationDidFinishLaunching())
    {
        return 0;
    }
    
    SDL_Joystick *joystick = SDL_JoystickOpen(0);
	
    CCEGLView& mainWnd = CCEGLView::sharedOpenGLView();
    mainWnd.centerWindow();
   CCLog(" --- BEFORE EVENT LOOP\n");
	 SDL_Event Event;
	 while (1) {
        bool gotEvent;
        
        // without the inner while loop, input performance gets really laggy    	        
		while ( SDL_PollEvent(&Event) ) {  
//		  CCLog("--- GOT NEW EVENT .... \n");
		 switch (Event.type) {
                case SDL_KEYDOWN:
                    switch (Event.key.keysym.sym) {
                        case PDLK_GESTURE_BACK: /* also maps to ESC */
                            if (PDL_GetPDKVersion() >= 200) {
                                // standard behavior is to minimize to a card when you perform a back
                                // gesture at the top level of the app
                                PDL_Minimize();
                            }
                            break;
                            case PDLK_GESTURE_DISMISS_KEYBOARD:{
                                CCDirector::sharedDirector()->getOpenGLView()->setIMEKeyboardState(false);
                            }
                            break;
                            case SDLK_BACKSPACE: {
                                CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();
                            }
                            break;
                        default:
                        {
                            CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(SDL_GetKeyName(Event.key.keysym.sym), 1); 
                        }
                        break;
                    }
                    break;
                case SDL_MOUSEBUTTONDOWN:
//					CCLog(" --- GOT NEW EVENT (SDL_MOUSEBUTTONDOWN).... \n");
					mainWnd.WindowProc(SDL_MOUSEBUTTONDOWN,Event);
					break;
				case SDL_MOUSEBUTTONUP:
//					CCLog("--- GOT NEW EVENT (SDL_MOUSEBUTTONUP).... \n");
					mainWnd.WindowProc(SDL_MOUSEBUTTONUP,Event);
					break;
				case SDL_MOUSEMOTION:
					mainWnd.WindowProc(SDL_MOUSEMOTION,Event);
					break;
				case SDL_QUIT:
                    // We exit anytime we get a request to quit the app
                    // all shutdown code is registered via atexit() so this is clean.
                    exit(0);
                    break;
                case SDL_JOYAXISMOTION:
                	CCAccelerometer::sharedAccelerometer()->update(joystick, time(NULL));
                	break;
                case SDL_ACTIVEEVENT:
                      switch(Event.active.gain)
                      {
                        case 1:
                            {
                                applicationWillEnterForeground();
                                break;
                            }
                        default:
                        {
                            applicationDidEnterBackground();
                            break;
                        }
                      }
                    break;
                default:
                    break;
            }
        }   
		   // Get current time tick.
            // If it's the time to draw next frame, draw it, else sleep a while.
            CCDirector::sharedDirector()->mainLoop();
     }

    return (int) 0;
}
Boolean CCXApplication::EventHandler(EventType * pEvent)
{
    Boolean bHandled = FALSE;
    switch (pEvent->eType)
    {
    case EVENT_AppLoad:
        if (! applicationDidFinishLaunching())
        {
            CCScheduler::purgeSharedScheduler();
            SendStopEvent();
        }
#ifdef _TRANZDA_VM_
        QueryPerformanceFrequency(&s_nFreq);
        QueryPerformanceCounter(&s_nLast);
#else
        s_nLast = getTimeOfDayMicroSecond();
#endif
        bHandled = TRUE;
        break;

    case EVENT_AppStopNotify:

        break;
    case EVENT_AppActiveNotify:
        if (pEvent->sParam1 == 0)
        {
            if (!m_bInBackground)
            {
                applicationDidEnterBackground();
                m_bInBackground = true;
            }

            if (CCDirector::sharedDirector()->isPaused())
            {
               StopMainLoop();
            }
            CfgTurnOnBackLight();
            EnableKeyLock();
        }
        else if (pEvent->sParam1 > 0)
        {
            if (m_bInBackground)
            {
                applicationWillEnterForeground();
                m_bInBackground = false;
            }

            StartMainLoop();
            
            CfgTurnOnBackLightDelay(0x7fffffff);
            // if KeyLock disactived, disable it.
            if (! CfgKeyLock_GetActive())
            {
                DisableKeyLock();
            }
        }
        break;
    }

    if (! bHandled)
    {
        bHandled = TApplication::EventHandler(pEvent);
    }
    return bHandled;
}