/** * 释放定时器 * 当不需要定时器时,可以使用该函数释放定时器占用的资源 * @param timer_id * 需要释放的定时器的标识符 * @return * 正常返回0,指定ID的定时器不存在则返回-1. * */ LCUI_API int LCUITimer_Free( int timer_id ) { int i, total; timer_data *timer; LCUISleeper_BreakSleep( &timer_sleeper ); Queue_Lock( &global_timer_list ); total = Queue_GetTotal( &global_timer_list ); for(i=0; i<total; ++i) { timer = (timer_data*)Queue_Get( &global_timer_list, i ); if( !timer ) { continue; } if( timer->id == timer_id ) { DEBUG_MSG("delete timer: %d, n_ms: %d\n", timer->id, timer->total_ms); Queue_Delete( &global_timer_list, i ); break; } } Queue_Unlock( &global_timer_list ); if( i < total ) { LCUISleeper_BreakSleep( &timer_sleeper ); return 0; } return -1; }
/** 暂停数据帧的更新 */ void FrameControl_Pause( FrameCtrlCtx *ctx, LCUI_BOOL need_pause ) { if( ctx->state == FRAME_CTRL_STATE_RUN && need_pause ) { LCUISleeper_BreakSleep( &ctx->wait_pause ); ctx->state = FRAME_CTRL_STATE_PAUSE; } else if( ctx->state == FRAME_CTRL_STATE_PAUSE && !need_pause ){ LCUISleeper_BreakSleep( &ctx->wait_continue ); ctx->state = FRAME_CTRL_STATE_RUN; } }
/* 向消息队列中投递消息 */ int Game_PostMsg( GameMsg *msg ) { Queue_Lock( &game_msg_queue ); if( !Queue_Add( &game_msg_queue, msg ) ) { Queue_Unlock( &game_msg_queue ); return -1; } Queue_Unlock( &game_msg_queue ); return LCUISleeper_BreakSleep( &msg_sleeper ); }
/* * 功能:发送任务给程序,使这个程序进行指定任务 * 说明:LCUI_Task结构体中的成员变量 id,保存的是目标程序的id */ LCUI_API int AppTasks_Add( LCUI_Task *task ) { LCUI_App *app; app = LCUIApp_Find( task->id ); if( !app ) { return -1; } Queue_Lock( &app->tasks ); if(Queue_Add( &app->tasks, task ) < 0 ) { Queue_Unlock( &app->tasks ); return -2; } LCUISleeper_BreakSleep( &app->mainloop_sleeper ); Queue_Unlock( &app->tasks ); return 0; }
/** * 继续定时器的倒计时 * @param timer_id * 目标定时器的标识符 * @return * 正常返回0,指定ID的定时器不存在则返回-1. * */ LCUI_API int LCUITimer_Continue( int timer_id ) { timer_data *timer; LCUISleeper_BreakSleep( &timer_sleeper ); Queue_Lock( &global_timer_list ); timer = TimerList_Find( timer_id ); if( timer ) { /* 计算处于暂停状态的时长 */ timer->pause_ms += (long int)LCUI_GetTicks( timer->pause_time ); timer->state = STATE_RUN; Queue_Unlock( &global_timer_list ); return 0; } Queue_Unlock( &global_timer_list ); return -1; }
/** * 暂停定时器的倒计时 * 一般用于往复定时的定时器 * @param timer_id * 目标定时器的标识符 * @return * 正常返回0,指定ID的定时器不存在则返回-1. * */ LCUI_API int LCUITimer_Pause( int timer_id ) { timer_data *timer; LCUISleeper_BreakSleep( &timer_sleeper ); Queue_Lock( &global_timer_list ); timer = TimerList_Find( timer_id ); if( timer ) { /* 记录暂停时的时间 */ timer->pause_time = LCUI_GetTickCount(); timer->state = STATE_PAUSE; Queue_Unlock( &global_timer_list ); return 0; } Queue_Unlock( &global_timer_list ); return -1; }
/** * 重设定时器的等待时间 * @param timer_id * 需要释放的定时器的标识符 * @param n_ms * 等待的时间,单位为毫秒 * @return * 正常返回0,指定ID的定时器不存在则返回-1. * */ LCUI_API int LCUITimer_Reset( int timer_id, long int n_ms ) { timer_data *timer; LCUISleeper_BreakSleep( &timer_sleeper ); Queue_Lock( &global_timer_list ); timer = TimerList_Find( timer_id ); if( timer ) { timer->start_time = LCUI_GetTickCount(); timer->pause_ms = 0; timer->total_ms = n_ms; TimerList_UpdateTimerPos( &global_timer_list, timer ); Queue_Unlock( &global_timer_list ); return 0; } Queue_Unlock( &global_timer_list ); return -1; }
/* * 功能:使用自定义方式添加程序任务 * 用法示例: * 在函数的各参数与队列中的函数及各参数不重复时,添加它 * AppTasks_CustomAdd(ADD_MODE_NOT_REPEAT | AND_ARG_F | AND_ARG_S, task); * 只要函数和参数1不重复则添加 * AppTasks_CustomAdd(ADD_MODE_NOT_REPEAT | AND_ARG_F, task); * 要函数不重复则添加 * AppTasks_CustomAdd(ADD_MODE_NOT_REPEAT, task); * 添加新的,不管是否有重复的 * AppTasks_CustomAdd(ADD_MODE_ADD_NEW, task); * 有相同函数则覆盖,没有则新增 * AppTasks_CustomAdd(ADD_MODE_REPLACE, task); * */ LCUI_API int AppTasks_CustomAdd( int mode, LCUI_Task *task ) { int ret; LCUI_App *app; if( task->id == (LCUI_ID)0 ) { app = LCUIApp_GetSelf(); } else { app = LCUIApp_Find( task->id ); } if( !app ) { return -1; } ret = Tasks_CustomAdd( &app->tasks, mode, task ); if( ret == 0 ) { LCUISleeper_BreakSleep( &app->mainloop_sleeper ); } return ret; }
/** * 设置定时器 * 定时器的作用是让一个任务在经过指定时间后才执行 * @param n_ms * 等待的时间,单位为毫秒 * @param callback_func * 用于响应定时器的回调函数 * @param reuse * 指示该定时器是否重复使用,如果要用于循环定时处理某些 * 任务,可将它置为 TRUE,否则置于 FALSE。 * @return * 该定时器的标识符 * */ LCUI_API int LCUITimer_Set( long int n_ms, void (*callback_func)(void*), void *arg, LCUI_BOOL reuse ) { int n; int64_t time_left; timer_data timer, *p_timer; static int id = 100; /* 打断定时器睡眠者的睡眠 */ LCUISleeper_BreakSleep( &timer_sleeper ); Queue_Lock( &global_timer_list ); n = Queue_GetTotal( &global_timer_list ); while(n--) { p_timer = (timer_data*)Queue_Get( &global_timer_list, n ); if( !p_timer ) { continue; } time_left = LCUI_GetTicks( p_timer->start_time ); time_left -= p_timer->pause_ms; time_left = p_timer->total_ms - time_left; if( time_left <= n_ms ) { break; } } timer.id = ++id; timer.app_id = LCUIApp_GetSelfID(); timer.state = STATE_RUN; timer.reuse = reuse; timer.total_ms = n_ms; timer.pause_ms = 0; timer.start_time = LCUI_GetTickCount(); timer.callback_func = callback_func; timer.arg = arg; Queue_Insert( &global_timer_list, n+1, &timer ); Queue_Unlock( &global_timer_list ); DEBUG_MSG("set timer, id: %d, total_ms: %d,app_id: %lu\n", timer.id, timer.total_ms, timer.app_id); return timer.id; }