/* 新建一个主循环 */ LCUI_API LCUI_MainLoop* LCUI_MainLoop_New( void ) { LCUI_MainLoop *loop; if( !init_mainloop_queue ) { LCUI_MainLoopQueue_Init(); init_mainloop_queue = TRUE; } loop = LCUI_MainLoop_GetAvailable(); if( loop == NULL ) { loop = (LCUI_MainLoop*)malloc(sizeof(LCUI_MainLoop)); if( !loop ) { return NULL; } } loop->app_id = LCUIApp_GetSelfID(); loop->quit = FALSE; loop->level = Queue_GetTotal( &mainloop_queue ); loop->running = FALSE; Queue_AddPointer( &mainloop_queue, loop ); /* 重新对主循环队列进行排序 */ LCUI_MainLoopQueue_Sort(); return loop; }
/** * 设置定时器 * 定时器的作用是让一个任务在经过指定时间后才执行 * @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; }
Frames_AddFunc( LCUI_Frames *des, void (*func)(LCUI_Graph*, void*), void *arg ) /* * 功能:为动画关联回调函数 * 说明:关联回调函数后,动画每更新一帧都会调用这个函数 * */ { LCUI_Func func_data; if( !des ) { return -1; } func_data.func = func; func_data.id = LCUIApp_GetSelfID(); func_data.arg[0] = NULL; func_data.arg[1] = arg; /* 如果不将该标志置为FALSE,会是确定的值,导致在执行任务后的销毁参数时出现段错误 */ func_data.destroy_arg[0] = FALSE; func_data.destroy_arg[1] = FALSE; Queue_Add(&des->func_data, &func_data); return 0; }