/** * Test timers - function to be called in main application loop * @param timers Timers instance */ void timers_ctest(Timers timers) { time_t now = time(NULL); ll_loop(timers, timer) { switch (timer->type) { case TM_TIMEOUT: timer->currentTimeout -= (now - timers->lastTest); if (timer->currentTimeout <= 0) { if (timer->callback != NULL && timer->callback(timer)) { timer->currentTimeout = timer->setTimeout; } else { timers_remove(timer); } } break; case TM_AT: if (timer->currentTimeout <= now) { if (timer->callback != NULL) { timer->callback(timer); } timers_remove(timer); } break; default: printError("timers", "Unknown timer type (%d).", timer->type); break; } } timers->lastTest = now; } // timers_ctest
void timers_script_dispose(int script_uid) { int i; i=0; while (i<js.count.timer) { if (js.timers[i].attach.script_uid==script_uid) { timers_remove(i); if (js.count.timer==0) break; } else { i++; } } }
void timers_clear() { while (timers_get_count() > 0) { timers_remove(0); } }
void timers_remove_selected() { if(selected<0){ return; } timers_remove(selected); }
void timers_run(void) { int i,ntimer; timer_type *timer; // only run current timers // don't run new timers created inside these timer calls ntimer=js.count.timer; // run timers for (i=0;i<ntimer;i++) { timer=&js.timers[i]; // skip timers in dispose if (timer->mode==timer_mode_dispose) continue; // time to fire? timer->count--; if (timer->count>0) continue; // fire timer switch (timer->mode) { case timer_mode_single: scripts_post_event_console(&timer->attach,sd_event_wait,0,timer->user_id); timer->mode=timer_mode_dispose; // auto-dispose waits break; case timer_mode_chain: scripts_chain_console(&timer->attach,timer->chain_func_name); timer->mode=timer_mode_dispose; // auto-dispose chains break; case timer_mode_repeat: scripts_post_event_console(&timer->attach,sd_event_timer,0,timer->user_id); break; } // repeat timer timer->count=timer->freq; } // remove timers i=0; while (i<js.count.timer) { timer=&js.timers[i]; if (timer->mode==timer_mode_dispose) { timers_remove(i); if (js.count.timer==0) break; } else { i++; } } }