From 8edb07b3feb84dfffe7ded07bf70e4c12edb6b7e Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Tue, 19 Feb 2013 15:09:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0delay.c=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=EF=BC=8C=E5=B0=81=E8=A3=85=E4=BA=86sleep=E3=80=81usleep?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/LCUI/LCUI.h | 10 +++++--- include/LCUI/LCUI_Input.h | 9 ++----- include/LCUI/LCUI_Misc.h | 6 +++++ src/LCUI_Queue.c | 1 - src/LCUI_Widget.c | 3 +-- src/input/keyboard.c | 46 +++++++++++++++++------------------ src/input/mouse.c | 2 +- src/kernel/device.c | 12 ++++----- src/kernel/event.c | 10 ++++---- src/kernel/main.c | 22 +++-------------- src/kernel/timer.c | 15 ++++++------ src/misc/Makefile.am | 2 +- src/misc/Makefile.in | 6 +++-- src/misc/catch_screen.c | 4 +-- src/misc/delay.c | 27 ++++++++++++++++++++ src/output/graph_display.c | 50 ++++---------------------------------- src/widget/activebox.c | 3 +-- src/widget/progressbar.c | 8 +++--- 18 files changed, 103 insertions(+), 133 deletions(-) create mode 100644 src/misc/delay.c diff --git a/include/LCUI/LCUI.h b/include/LCUI/LCUI.h index 69fa8242..98ede4fe 100644 --- a/include/LCUI/LCUI.h +++ b/include/LCUI/LCUI.h @@ -54,12 +54,16 @@ #include LC_CONFIG_H -#ifdef WIN32 +#if defined(WIN32) || defined(_WIN32) #define LCUI_BUILD_IN_WIN32 #define LCUI_THREAD_WIN32 #define LCUI_VIDEO_DRIVER_WIN32 #undef LCUI_THREAD_PTHREAD #undef LCUI_VIDEO_DRIVER_FRAMEBUFFER +#else +#define LCUI_BUILD_IN_LINUX +#define LCUI_KEYBOARD_DRIVER_LINUX +#define LCUI_MOUSE_DRIVER_LINUX #endif @@ -367,8 +371,8 @@ typedef enum { } ALIGN_TYPE; /*---------------------------------------------*/ -#define MAX_APP_IDLE_TIME 50000 -#define MAX_LCUI_IDLE_TIME 50000 +#define MAX_APP_IDLE_TIME 50 +#define MAX_LCUI_IDLE_TIME 50 #define nobuff_printf(format, ...) \ { \ diff --git a/include/LCUI/LCUI_Input.h b/include/LCUI/LCUI_Input.h index caaf96f6..2790e685 100755 --- a/include/LCUI/LCUI_Input.h +++ b/include/LCUI/LCUI_Input.h @@ -149,13 +149,8 @@ BOOL LCUIKeyboard_Hit( void ); /* 功能:获取被按下的按键的键值 */ int LCUIKeyboard_Get( void ); -int Find_Pressed_Key(int key); -/* - * 功能:检测指定键值的按键是否处于按下状态 - * 返回值: - * 1 存在 - * 0 不存在 - **/ +/* 检测指定键值的按键是否处于按下状态 */ +BOOL KeyHit( int key_code ); /* 初始化键盘输入模块 */ void LCUIModule_Keyboard_Init( void ); diff --git a/include/LCUI/LCUI_Misc.h b/include/LCUI/LCUI_Misc.h index 3a23a22b..70ace466 100755 --- a/include/LCUI/LCUI_Misc.h +++ b/include/LCUI/LCUI_Misc.h @@ -44,6 +44,12 @@ LCUI_BEGIN_HEADER +/* 秒级延时 */ +void LCUI_Sleep( unsigned int s ); + +/* 毫秒级延时 */ +void LCUI_MSleep( unsigned int ms ); + /******************************** Misc *********************************/ int Check_Option(int result, int option); /* 功能:检测result是否包含option */ diff --git a/src/LCUI_Queue.c b/src/LCUI_Queue.c index 6e7dda22..4d8edcf0 100644 --- a/src/LCUI_Queue.c +++ b/src/LCUI_Queue.c @@ -66,7 +66,6 @@ #include LC_GRAPH_H #include LC_WIDGET_H #include LC_ERROR_H -#include /************************ LCUI_Queue **********************************/ /* 为队列设定互斥锁,使之只能被一个线程使用 */ diff --git a/src/LCUI_Widget.c b/src/LCUI_Widget.c index a4ccc236..e4564d90 100644 --- a/src/LCUI_Widget.c +++ b/src/LCUI_Widget.c @@ -50,7 +50,6 @@ #include LC_CURSOR_H #include LC_INPUT_H -#include #include #include @@ -2211,7 +2210,7 @@ void Move_Widget_To_Pos(LCUI_Widget *widget, LCUI_Pos des_pos, int speed) x += w; y += h; Widget_Move(widget, Pos(x, y)); - usleep(10000);/* 停顿0.01秒 */ + LCUI_MSleep(10);/* 停顿0.01秒 */ } } diff --git a/src/input/keyboard.c b/src/input/keyboard.c index 52c899f7..64f960b5 100644 --- a/src/input/keyboard.c +++ b/src/input/keyboard.c @@ -41,15 +41,20 @@ #include #include LC_LCUI_H + +#ifdef LCUI_KEYBOARD_DRIVER_LINUX + +#define __NEED_CATCHSCREEN__ +#ifdef __NEED_CATCHSCREEN__ #include LC_GRAPH_H #include LC_DRAW_H #include LC_DISPLAY_H +#endif #include #include #include - static struct termios tm;//, tm_old; static int fd = STDIN_FILENO; @@ -131,38 +136,25 @@ int LCUIKeyboard_Get( void ) return c; } -int Find_Pressed_Key(int key) -/* - * 功能:检测指定键值的按键是否处于按下状态 - * 返回值: - * 1 存在 - * 0 不存在 - **/ +/* 检测指定键值的按键是否处于按下状态 */ +BOOL KeyHit( int key_code ) { - int t; + int *t; int i, total; total = Queue_Get_Total(&LCUI_Sys.press_key); for(i=0; ibutton)) { + && !KeyHit(event->button)) { return 1; } return 0; diff --git a/src/kernel/device.c b/src/kernel/device.c index 64661e39..c09e57b6 100644 --- a/src/kernel/device.c +++ b/src/kernel/device.c @@ -1,9 +1,7 @@ #include #include LC_LCUI_H -#include /*----------------------------- Device -------------------------------*/ - /* 设备列表初始化 */ static void dev_list_init( LCUI_Queue *dev_list ) @@ -39,7 +37,7 @@ proc_dev_list ( void *arg ) { LCUI_Queue *dev_list; dev_func_data *data_ptr; - int total, i, result, sleep_time = 1000; + int total, i, result, sleep_time = 1; dev_list = (LCUI_Queue *)arg; while( LCUI_Active() ) { @@ -53,11 +51,11 @@ proc_dev_list ( void *arg ) result += data_ptr->proc_func(); } if( result > 0 ) { - sleep_time = 1000; + sleep_time = 1; } else { - usleep( sleep_time ); - if( sleep_time < 100000 ) { - sleep_time += 1000; + LCUI_MSleep( sleep_time ); + if( sleep_time < 100 ) { + sleep_time += 1; } } } diff --git a/src/kernel/event.c b/src/kernel/event.c index 44bb11bb..d21cf62e 100644 --- a/src/kernel/event.c +++ b/src/kernel/event.c @@ -110,11 +110,11 @@ LCUI_DispatchUserEvent( LCUI_Event *event ) static void LCUI_EventLoop( void *unused ) { LCUI_Event event; - int delay_time = 1500; + int delay_time = 1; while( active ) { if( LCUI_PollEvent( &event ) ) { - delay_time = 1500; + delay_time = 1; switch( event.type ) { case LCUI_KEYDOWN: case LCUI_KEYUP: @@ -128,10 +128,10 @@ static void LCUI_EventLoop( void *unused ) break; } } else { - if( delay_time <= 15000 ) { - delay_time += 1500; + if( delay_time <= 15 ) { + delay_time += 1; } - usleep( delay_time ); + LCUI_MSleep( delay_time ); } } _LCUIThread_Exit( NULL ); diff --git a/src/kernel/main.c b/src/kernel/main.c index 9c7314c4..c8c4dd8c 100644 --- a/src/kernel/main.c +++ b/src/kernel/main.c @@ -10,22 +10,8 @@ #include LC_FONT_H #include LC_WIDGET_H -#include -#include - LCUI_System LCUI_Sys; -static clock_t start_time; -void count_time() -{ - start_time = clock(); -} - -void end_count_time() -{ - printf("%ldms\n",clock()-start_time); -} - /************************* App Management *****************************/ /* 根据程序的ID,获取指向程序数据结构的指针 */ LCUI_App *LCUIApp_Find( LCUI_ID id ) @@ -311,7 +297,7 @@ LCUIApp_RunTask( LCUI_App *app ) int LCUI_MainLoop_Run( LCUI_MainLoop *loop ) { LCUI_App *app; - int idle_time = 1500; + int idle_time = 1; app = LCUIApp_GetSelf(); if( !app ) { @@ -322,12 +308,12 @@ int LCUI_MainLoop_Run( LCUI_MainLoop *loop ) loop->running = TRUE; while( !loop->quit ) { if( LCUIApp_HaveTask(app) ) { - idle_time = 1500; + idle_time = 1; LCUIApp_RunTask( app ); } else { - usleep (idle_time); + LCUI_MSleep (idle_time); if (idle_time < MAX_APP_IDLE_TIME) { - idle_time += 1500; + idle_time += 1; } } } diff --git a/src/kernel/timer.c b/src/kernel/timer.c index fd1dc426..8c71605d 100644 --- a/src/kernel/timer.c +++ b/src/kernel/timer.c @@ -1,6 +1,5 @@ #include #include LC_LCUI_H -#include /*----------------------------- Timer --------------------------------*/ /*----------------------------- Private ------------------------------*/ @@ -87,7 +86,7 @@ timer_list_update( LCUI_Queue *timer_list ) return NULL; } if(timer->cur_ms > 0) { - usleep( timer->cur_ms*1000 ); + LCUI_MSleep( timer->cur_ms ); } /* 减少列表中所有定时器的剩余等待时间 */ timer_list_sub( timer_list, timer->cur_ms ); @@ -102,7 +101,7 @@ static BOOL timer_thread_active = TRUE; static void timer_list_process( void *arg ) { - int sleep_time = 1000; + int sleep_time = 1; LCUI_Func func_data; LCUI_Queue *timer_list; timer_data *timer; @@ -112,18 +111,18 @@ timer_list_process( void *arg ) func_data.arg[1] = NULL; while( !LCUI_Active() ) { - usleep(10000); + LCUI_MSleep(10); } while( LCUI_Active() || timer_thread_active ) { timer = timer_list_update( timer_list ); if( !timer ) { - usleep( sleep_time ); - if(sleep_time < 500000) { - sleep_time += 1000; + LCUI_MSleep( sleep_time ); + if(sleep_time < 500) { + sleep_time += 1; } continue; } - sleep_time = 1000; + sleep_time = 1; func_data.id = timer->app_id; func_data.func = timer->callback_func; /* 添加该任务至指定程序的任务队列,添加模式是覆盖 */ diff --git a/src/misc/Makefile.am b/src/misc/Makefile.am index 1fc64d44..823e49be 100644 --- a/src/misc/Makefile.am +++ b/src/misc/Makefile.am @@ -1,5 +1,5 @@ AUTOMAKE_OPTIONS=foreign INCLUDES = -I$(abs_top_srcdir)/include noinst_LTLIBRARIES = libmisc.la -libmisc_la_SOURCES = catch_screen.c rect.c string.c misc.c +libmisc_la_SOURCES = catch_screen.c rect.c string.c misc.c delay.c diff --git a/src/misc/Makefile.in b/src/misc/Makefile.in index f0f5fcff..97527fb4 100644 --- a/src/misc/Makefile.in +++ b/src/misc/Makefile.in @@ -47,7 +47,8 @@ CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libmisc_la_LIBADD = -am_libmisc_la_OBJECTS = catch_screen.lo rect.lo string.lo misc.lo +am_libmisc_la_OBJECTS = catch_screen.lo rect.lo string.lo misc.lo \ + delay.lo libmisc_la_OBJECTS = $(am_libmisc_la_OBJECTS) AM_V_lt = $(am__v_lt_$(V)) am__v_lt_ = $(am__v_lt_$(AM_DEFAULT_VERBOSITY)) @@ -210,7 +211,7 @@ top_srcdir = @top_srcdir@ AUTOMAKE_OPTIONS = foreign INCLUDES = -I$(abs_top_srcdir)/include noinst_LTLIBRARIES = libmisc.la -libmisc_la_SOURCES = catch_screen.c rect.c string.c misc.c +libmisc_la_SOURCES = catch_screen.c rect.c string.c misc.c delay.c all: all-am .SUFFIXES: @@ -264,6 +265,7 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/catch_screen.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/delay.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/misc.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rect.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/string.Plo@am__quote@ diff --git a/src/misc/catch_screen.c b/src/misc/catch_screen.c index 037450a2..ead0daee 100644 --- a/src/misc/catch_screen.c +++ b/src/misc/catch_screen.c @@ -46,8 +46,6 @@ #include LC_DRAW_H #include LC_DISPLAY_H -#include - void Catch_Screen_Graph_By_Cache(LCUI_Rect area, LCUI_Graph *out) /* * 功能:通过内存中的图像数据,捕获将在屏幕上显示的图像 @@ -131,7 +129,7 @@ static void catch() Catch_Screen_Graph_By_FB( area, &graph ); write_png( filename, &graph ); - usleep(35000); + LCUI_MSleep(35); } LCUIThread_Exit(NULL); } diff --git a/src/misc/delay.c b/src/misc/delay.c new file mode 100644 index 00000000..efbdcc56 --- /dev/null +++ b/src/misc/delay.c @@ -0,0 +1,27 @@ +#include +#include LC_LCUI_H + +#ifdef LCUI_BUILD_IN_WIN32 +#include +#else +#include +#endif +/* 秒级延时 */ +void LCUI_Sleep( unsigned int s ) +{ +#ifdef LCUI_BUILD_IN_WIN32 + Sleep(s*1000); +#else + sleep(s); +#endif +} + +/* 毫秒级延时 */ +void LCUI_MSleep( unsigned int ms ) +{ +#ifdef LCUI_BUILD_IN_WIN32 + Sleep(ms); +#else + usleep(ms*1000); +#endif +} diff --git a/src/output/graph_display.c b/src/output/graph_display.c index 9ce6a82e..5e4fd522 100644 --- a/src/output/graph_display.c +++ b/src/output/graph_display.c @@ -48,8 +48,6 @@ #include LC_WIDGET_H #include LC_CURSOR_H -#include - int Get_Screen_Width () /* * 功能:获取屏幕宽度 @@ -80,7 +78,6 @@ LCUI_Size Get_Screen_Size( void ) return LCUI_Sys.screen.size; } -//static int debug_count = 0; int Add_Screen_Refresh_Area (LCUI_Rect rect) /* 功能:在整个屏幕内添加需要刷新的区域 */ { @@ -88,14 +85,7 @@ int Add_Screen_Refresh_Area (LCUI_Rect rect) if (rect.width <= 0 || rect.height <= 0) { return -1; } - rect = Get_Valid_Area(Get_Screen_Size(), rect); - //if( rect.width == 320 && rect.height == 240 ) { - //++debug_count; - //} - //if( debug_count >= 20 ) { - // abort(); - //} - + rect = Get_Valid_Area(Get_Screen_Size(), rect); Queue_Lock( &LCUI_Sys.invalid_area ); ret = RectQueue_Add ( &LCUI_Sys.invalid_area, rect ); /* 队列使用结束,解开锁 */ @@ -166,27 +156,6 @@ Handle_Screen_Update() Graph_Free(&graph); } -//#define need_autoquit -#ifdef need_autoquit -static int auto_flag = 0; -static void *autoquit() -/* 在超时后,会自动终止程序,用于调试 */ -{ - LCUI_ID time = 0; - while(time <5000000) { - if(auto_flag == 0) { - usleep(10000); - time += 10000; - } else { - auto_flag = 0; - time = 0; - } - } - exit(-1); -} -#endif - - static void Handle_Refresh_Area( void ) /* @@ -218,28 +187,19 @@ static void Handle_Area_Update () /* 功能:进行屏幕内容更新 */ { -#ifdef need_autoquit - LCUI_Thread t; - LCUIThread_Create(&t, NULL, autoquit, NULL); -#endif int timer_id; /* 添加个定时器,每隔1秒刷新FPS计数 */ timer_id = set_timer( 1000, refresh_fps_count, TRUE ); //_DEBUG_MSG("enter\n"); while(LCUI_Active()) { - Handle_AllWidgetUpdate();/* 处理所有部件更新 */ - usleep(5000);/* 停顿一段时间,让程序主循环处理任务 */ + Handle_AllWidgetUpdate(); /* 处理所有部件更新 */ + LCUI_MSleep(5); /* 停顿一段时间,让程序主循环处理任务 */ Handle_Refresh_Area(); /* 处理需要刷新的区域 */ - Handle_Screen_Update();/* 处理屏幕更新 */ -#ifdef need_autoquit - auto_flag = 1; -#endif - /* 累计当前更新的帧数 */ - ++fps_count; + Handle_Screen_Update(); /* 处理屏幕更新 */ + ++fps_count; /* 累计当前更新的帧数 */ } /* 释放定时器 */ free_timer( timer_id ); - //_DEBUG_MSG("exit\n"); LCUIThread_Exit(NULL); } diff --git a/src/widget/activebox.c b/src/widget/activebox.c index 21c1bf13..3c5b18bc 100644 --- a/src/widget/activebox.c +++ b/src/widget/activebox.c @@ -40,7 +40,6 @@ * ****************************************************************************/ //#define DEBUG #include -#include #include LC_LCUI_H #include LC_WIDGET_H #include LC_MISC_H @@ -410,7 +409,7 @@ Process_Frames() LCUI_Frames *frames; while(!LCUI_Active()) { - usleep(10000); + LCUI_MSleep(10); } frames = FramesStream_Update( &sleep_time ); reset_timer( __timer_id, sleep_time ); diff --git a/src/widget/progressbar.c b/src/widget/progressbar.c index 13eb6486..11f418d4 100644 --- a/src/widget/progressbar.c +++ b/src/widget/progressbar.c @@ -47,8 +47,6 @@ #include LC_PICBOX_H #include LC_RES_H -#include - /* 功能:获取进度条上闪光图形所在的PictureBox部件 */ static LCUI_Widget * ProgressBar_GetMoveingLight(LCUI_Widget *widget) @@ -79,7 +77,7 @@ ProgressBar_Init(LCUI_Widget *widget) Graph_Init(&pb->fore_graph); Graph_Init(&pb->flash_image); pb->img_move_speed = 200; - pb->sleep_time = 100000; + pb->sleep_time = 100; pb->max_value = 1; pb->value = 0; @@ -204,9 +202,9 @@ ProgressBar_ProcMoveingLight(void *arg) x<=flash->parent->size.w; x+=(pb->img_move_speed/20.0+0.5)) { Widget_Move(flash, Pos(x , flash->pos.y)); - usleep(50000); + LCUI_MSleep(50); } - usleep(pb->sleep_time); + LCUI_MSleep(pb->sleep_time); } LCUIThread_Exit(NULL); }