/*
 * ehci_work is called from some interrupts, timers, and so on.
 * it calls driver completion functions, after dropping ehci->lock.
 */
static void ehci_work (struct ehci_hcd *ehci)
{
	timer_action_done (ehci, TIMER_IO_WATCHDOG);

	/* another CPU may drop ehci->lock during a schedule scan while
	 * it reports urb completions.  this flag guards against bogus
	 * attempts at re-entrant schedule scanning.
	 */
	if (ehci->scanning)
		return;
	ehci->scanning = 1;
	scan_async (ehci);
	if (ehci->next_uframe != -1)
		scan_periodic (ehci);
	ehci->scanning = 0;

	/* the IO watchdog guards against hardware or driver bugs that
	 * misplace IRQs, and should let us run completely without IRQs.
	 * such lossage has been observed on both VT6202 and VT8235.
	 */
	if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
			(ehci->async->qh_next.ptr != NULL ||
			 ehci->periodic_sched != 0))
		timer_action (ehci, TIMER_IO_WATCHDOG);
}
/**
 * This is the main entry point of a native application that is using
 * android_native_app_glue.  It runs in its own thread, with its own
 * event loop for receiving input events and doing other things.
 */
void android_main(struct android_app* state)
{
    int times = 0;
    struct engine engine;

    // Make sure glue isn't stripped.

    app_dummy();


    memset(&engine, 0, sizeof(engine));

    state->userData = &engine;

    state->onAppCmd = engine_handle_cmd;

    state->onInputEvent = engine_handle_input;

    engine.app = state;

    // loop waiting for stuff to do.


    while (1) {

        // Read all pending events.

        int ident;

        int events;
        
        struct android_poll_source* source;


        while ((ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) {
            // Process this event.

            if (source != NULL) {

                source->process(state, source);

            }


            // Check if we are exiting.

            if (state->destroyRequested != 0) {

                engine_term_display(&engine);

                return;

            }

        }

        times++;
        if (times == 10) {
            times = 0;
            timer_action(engine.context);
        }
        engine_draw_frame(&engine);

    }

}