diff --git a/include/LCUI/cursor.h b/include/LCUI/cursor.h index 269196fa..07c3d154 100644 --- a/include/LCUI/cursor.h +++ b/include/LCUI/cursor.h @@ -1,7 +1,7 @@ /* *************************************************************************** * cursor.h -- mouse cursor operation set. * - * Copyright (C) 2012-2016 by Liu Chao + * Copyright (C) 2012-2017 by Liu Chao * * This file is part of the LCUI project, and may only be used, modified, and * distributed under the terms of the GPLv2. @@ -22,7 +22,7 @@ /* **************************************************************************** * cursor.h -- 鼠标游标的操作集 * - * 版权所有 (C) 2012-2016 归属于 刘超 + * 版权所有 (C) 2012-2017 归属于 刘超 * * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 * @@ -63,7 +63,7 @@ LCUI_API void LCUICursor_Show( void ); LCUI_API void LCUICursor_Hide( void ); /* 更新鼠标指针的位置 */ -LCUI_API void LCUICursor_UpdatePos( void ); +LCUI_API void LCUICursor_Update( void ); /* 设定游标的位置 */ LCUI_API void LCUICursor_SetPos( LCUI_Pos pos ); diff --git a/include/LCUI/display.h b/include/LCUI/display.h index e9656144..a154aa97 100644 --- a/include/LCUI/display.h +++ b/include/LCUI/display.h @@ -1,7 +1,7 @@ /* *************************************************************************** * display.h -- graphic display control * - * Copyright (C) 2012-2016 by Liu Chao + * Copyright (C) 2012-2017 by Liu Chao * * This file is part of the LCUI project, and may only be used, modified, and * distributed under the terms of the GPLv2. @@ -22,7 +22,7 @@ /* **************************************************************************** * display.h -- 图形显示控制 * - * 版权所有 (C) 2012-2016 归属于 刘超 + * 版权所有 (C) 2012-2017 归属于 刘超 * * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 * @@ -108,6 +108,15 @@ LCUI_API int LCUIDisplay_SetMode( int mode ); /** 获取屏幕显示模式 */ LCUI_API int LCUIDisplay_GetMode( void ); +/** 更新各种图形元素 */ +LCUI_API void LCUIDisplay_Update( void ); + +/** 渲染内容 */ +LCUI_API void LCUIDisplay_Render( void ); + +/** 呈现渲染后的内容 */ +LCUI_API void LCUIDisplay_Present( void ); + LCUI_API void LCUIDisplay_ShowRectBorder( void ); LCUI_API void LCUIDisplay_HideRectBorder( void ); diff --git a/include/LCUI/gui/widget_base.h b/include/LCUI/gui/widget_base.h index 72efaeae..b1882c0e 100755 --- a/include/LCUI/gui/widget_base.h +++ b/include/LCUI/gui/widget_base.h @@ -186,7 +186,6 @@ typedef struct LCUI_WidgetRec_ { LCUI_WidgetPrototypeC proto; /**< 原型 */ LCUI_BOOL enable_graph; /**< 是否启用位图缓存 */ LCUI_Graph graph; /**< 位图缓存 */ - LCUI_Mutex mutex; /**< 互斥锁 */ LCUI_EventTrigger trigger; /**< 事件触发器 */ LCUI_WidgetTaskBoxRec task; /**< 任务记录 */ LinkedList dirty_rects; /**< 记录无效区域(脏矩形) */ @@ -306,10 +305,6 @@ LCUI_API void Widget_Show( LCUI_Widget w ); LCUI_API void Widget_Hide( LCUI_Widget w ); -LCUI_API void Widget_Lock( LCUI_Widget w ); - -LCUI_API void Widget_Unlock( LCUI_Widget w ); - /** 为部件设置属性 */ LCUI_API int Widget_SetAttributeEx( LCUI_Widget w, const char *name, void *value, int value_type, void( *value_destructor )(void*) ); diff --git a/include/LCUI/gui/widget_task.h b/include/LCUI/gui/widget_task.h index d3e8a445..37a66311 100644 --- a/include/LCUI/gui/widget_task.h +++ b/include/LCUI/gui/widget_task.h @@ -60,13 +60,13 @@ LCUI_API void Widget_AddToTrash( LCUI_Widget w ); LCUI_API void Widget_AddTaskForChildren( LCUI_Widget widget, int task ); /** 初始化 LCUI 部件任务处理功能 */ -void LCUIWidget_InitTask( void ); +void LCUIWidget_InitTasks( void ); /** 销毁(释放) LCUI 部件任务处理功能的相关资源 */ -void LCUIWidget_ExitTask( void ); +void LCUIWidget_ExitTasks( void ); /** 处理一次当前积累的部件任务 */ -void LCUIWidget_StepTask( void ); +void LCUIWidget_Update( void ); LCUI_END_HEADER diff --git a/include/LCUI/main.h b/include/LCUI/main.h index 534a06b1..af2bc34e 100644 --- a/include/LCUI/main.h +++ b/include/LCUI/main.h @@ -123,7 +123,7 @@ typedef void(*LCUI_SysEventFunc)(LCUI_SysEvent, void*); /** LCUI 应用程序驱动接口,封装了各个平台下的应用程序相关功能支持接口 */ typedef struct LCUI_AppDriverRec_ { - void( *DispatchEvent )(void); + void ( *ProcessEvents )(void); LCUI_BOOL( *WaitEvent )(void); LCUI_BOOL( *PostTask )(LCUI_AppTask); int( *BindSysEvent )(int, LCUI_EventFunc, void*, void( *)(void*)); @@ -158,7 +158,8 @@ LCUI_API void *LCUI_GetAppData( void ); LCUI_API void LCUI_SetTaskAgent( LCUI_BOOL enabled ); -LCUI_API void LCUI_DispatchEvent( void ); +/** 处理当前所有事件 */ +LCUI_API void LCUI_ProcessEvents( void ); /** 添加任务 */ LCUI_API LCUI_BOOL LCUI_PostTask( LCUI_AppTask task ); @@ -170,13 +171,13 @@ LCUI_API void LCUI_DeleteTask( LCUI_AppTask task ); LCUI_API int LCUI_RunTask( LCUI_AppTask task ); /* 新建一个主循环 */ -LCUI_API LCUI_MainLoop LCUI_MainLoop_New( void ); +LCUI_API LCUI_MainLoop LCUIMainLoop_New( void ); /* 运行目标循环 */ -LCUI_API int LCUI_MainLoop_Run( LCUI_MainLoop loop ); +LCUI_API int LCUIMainLoop_Run( LCUI_MainLoop loop ); /* 标记目标主循环需要退出 */ -LCUI_API void LCUI_MainLoop_Quit( LCUI_MainLoop loop ); +LCUI_API void LCUIMainLoop_Quit( LCUI_MainLoop loop ); /* 检测LCUI是否活动 */ LCUI_API LCUI_BOOL LCUI_IsActive( void ); diff --git a/src/cursor.c b/src/cursor.c index 5e80b245..c63ab5d9 100644 --- a/src/cursor.c +++ b/src/cursor.c @@ -1,7 +1,7 @@ /* *************************************************************************** * cursor.c -- mouse cursor operation set. * - * Copyright (C) 2012-2016 by Liu Chao + * Copyright (C) 2012-2017 by Liu Chao * * This file is part of the LCUI project, and may only be used, modified, and * distributed under the terms of the GPLv2. @@ -22,7 +22,7 @@ /* **************************************************************************** * cursor.c -- 鼠标游标的操作集 * - * 版权所有 (C) 2012-2016 归属于 刘超 + * 版权所有 (C) 2012-2017 归属于 刘超 * * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 * @@ -169,7 +169,7 @@ void LCUICursor_Hide( void ) } /* 更新鼠标指针的位置 */ -void LCUICursor_UpdatePos( void ) +void LCUICursor_Update( void ) { if( cursor.pos.x == cursor.new_pos.x && cursor.pos.y == cursor.new_pos.y ) { diff --git a/src/display.c b/src/display.c index 6731440d..ff4e58b4 100644 --- a/src/display.c +++ b/src/display.c @@ -55,10 +55,12 @@ #define DEFAULT_HEIGHT 600 /** surface 记录 */ -typedef struct SurfaceRecord { +typedef struct SurfaceRecordRec_ { + LCUI_BOOL rendered; /**< 是否已渲染了新内容 */ + LinkedList rects; /**< 需重绘的区域列表 */ LCUI_Surface surface; /**< surface */ LCUI_Widget widget; /**< surface 所映射的 widget */ -} SurfaceRecord; +} SurfaceRecordRec, *SurfaceRecord; /** 图形显示功能的上下文数据 */ static struct DisplayContext { @@ -68,7 +70,6 @@ static struct DisplayContext { FrameControl fc_ctx; /**< 上下文句柄,用于画面更新时的帧数控制 */ LCUI_Thread thread; /**< 线程,负责画面更新工作 */ LinkedList surfaces; /**< surface 列表 */ - LCUI_Mutex mutex; LCUI_DisplayDriver driver; } display = { LCDM_DEFAULT, FALSE, FALSE, NULL }; @@ -95,27 +96,43 @@ static void DrawBorder( LCUI_PaintContext paint ) Graph_DrawHorizLine( &paint->canvas, color, 1, pos, end_x ); } -/** 更新各种图形元素的显示 */ -static void LCUIDisplay_Update(void) +void LCUIDisplay_Update( void ) { - LinkedList rlist; - SurfaceRecord *p_sr; - LinkedListNode *sn, *rn; - LCUI_PaintContext paint; - LinkedList_Init( &rlist ); + LinkedListNode *sn; + LCUICursor_Update(); + LCUIWidget_Update(); /* 遍历当前的 surface 记录列表 */ for( LinkedList_Each( sn, &display.surfaces ) ) { - p_sr = sn->data; - if( !p_sr->widget || !p_sr->surface - || !Surface_IsReady(p_sr->surface) ) { + SurfaceRecord record = sn->data; + LCUI_Surface surface = record->surface; + + if( !record->widget || !surface || + !Surface_IsReady( surface ) ) { continue; } - Surface_Update( p_sr->surface ); + Surface_Update( surface ); /* 收集无效区域记录 */ - Widget_ProcInvalidArea( p_sr->widget, &rlist ); + Widget_ProcInvalidArea( record->widget, &record->rects ); + } +} + +void LCUIDisplay_Render( void ) +{ + LinkedListNode *sn, *rn; + LCUI_PaintContext paint; + + /* 遍历当前的 surface 记录列表 */ + for( LinkedList_Each( sn, &display.surfaces ) ) { + SurfaceRecord record = sn->data; + LCUI_Surface surface = record->surface; + + if( !record->widget || !surface || + !Surface_IsReady( surface ) ) { + continue; + } /* 在 surface 上逐个重绘无效区域 */ - for( LinkedList_Each( rn, &rlist ) ) { - paint = Surface_BeginPaint( p_sr->surface, rn->data ); + for( LinkedList_Each( rn, &record->rects ) ) { + paint = Surface_BeginPaint( surface, rn->data ); if( !paint ) { continue; } @@ -123,33 +140,45 @@ static void LCUIDisplay_Update(void) p_sr->widget->type, paint->rect.x, paint->rect.y, paint->rect.width, paint->rect.height ); - Widget_Render( p_sr->widget, paint ); + Widget_Render( record->widget, paint ); if( display.show_rect_border ) { DrawBorder( paint ); } - Surface_EndPaint( p_sr->surface, paint ); + Surface_EndPaint( surface, paint ); + } + RectList_Clear( &record->rects ); + record->rendered = TRUE; + } +} + +void LCUIDisplay_Present( void ) +{ + LinkedListNode *sn; + for( LinkedList_Each( sn, &display.surfaces ) ) { + SurfaceRecord record = sn->data; + LCUI_Surface surface = record->surface; + if( !surface || !Surface_IsReady( surface ) ) { + continue; + } + if( record->rendered ) { + Surface_Present( surface ); } - if( rlist.length > 0 ) { - Surface_Present( p_sr->surface ); - } - RectList_Clear( &rlist ); } - RectList_Clear( &rlist ); } void LCUIDisplay_InvalidateArea( LCUI_Rect *rect ) { - + LCUI_Widget root = LCUIWidget_GetRoot(); + Widget_InvalidateArea( root, rect, SV_BORDER_BOX ); } static LCUI_Widget LCUIDisplay_GetBindWidget( LCUI_Surface surface ) { - SurfaceRecord *sr; LinkedListNode *node; for( LinkedList_Each( node, &display.surfaces ) ) { - sr = node->data; - if( sr && sr->surface == surface ) { - return sr->widget; + SurfaceRecord record = node->data; + if( record && record->surface == surface ) { + return record->widget; } } return NULL; @@ -157,12 +186,11 @@ static LCUI_Widget LCUIDisplay_GetBindWidget( LCUI_Surface surface ) static LCUI_Surface LCUIDisplay_GetBindSurface( LCUI_Widget widget ) { - SurfaceRecord *sr; LinkedListNode *node; for( LinkedList_Each( node, &display.surfaces ) ) { - sr = node->data; - if( sr && sr->widget == widget ) { - return sr->surface; + SurfaceRecord record = node->data; + if( record && record->widget == widget ) { + return record->surface; } } return NULL; @@ -184,42 +212,43 @@ LCUI_Surface LCUIDisplay_GetSurfaceOwner( LCUI_Widget w ) static void LCUIDisplay_BindSurface( LCUI_Widget widget ) { LCUI_RectF *rect; - SurfaceRecord *sr; + SurfaceRecord record; int width, height; if( LCUIDisplay_GetBindSurface(widget) ) { return; } rect = &widget->box.graph; - sr = NEW( SurfaceRecord, 1 ); - sr->surface = Surface_New(); - sr->widget = widget; - Surface_SetCaptionW( sr->surface, widget->title ); + record = NEW( SurfaceRecordRec, 1 ); + record->surface = Surface_New(); + record->widget = widget; + record->rendered = FALSE; + LinkedList_Init( &record->rects ); + Surface_SetCaptionW( record->surface, widget->title ); if( widget->style->sheet[key_top].is_valid && widget->style->sheet[key_left].is_valid ) { - Surface_Move( sr->surface, rect->x, rect->y ); + Surface_Move( record->surface, rect->x, rect->y ); } - width = (int)(rect->width + 0.5); - height = (int)(rect->height + 0.5); - Surface_Resize( sr->surface, width, height ); + width = roundi( rect->width ); + height = roundi( rect->height ); + Surface_Resize( record->surface, width, height ); if( widget->computed_style.visible ) { - Surface_Show( sr->surface ); + Surface_Show( record->surface ); } else { - Surface_Hide( sr->surface ); + Surface_Hide( record->surface ); } Widget_InvalidateArea( widget, NULL, SV_GRAPH_BOX ); - LinkedList_Append( &display.surfaces, sr ); + LinkedList_Append( &display.surfaces, record ); } /** 解除 widget 与 sruface 的绑定 */ static void LCUIDisplay_UnbindSurface( LCUI_Widget widget ) { - SurfaceRecord *sr; LinkedListNode *node; for( LinkedList_Each( node, &display.surfaces ) ) { - sr = node->data; - if( sr && sr->widget == widget ) { - Surface_Delete( sr->surface ); + SurfaceRecord record = node->data; + if( record && record->widget == widget ) { + Surface_Delete( record->surface ); LinkedList_DeleteNode( &display.surfaces, node ); break; } @@ -228,11 +257,10 @@ static void LCUIDisplay_UnbindSurface( LCUI_Widget widget ) static void LCUIDisplay_CleanSurfaces( void ) { - SurfaceRecord *sr; LinkedListNode *node; for( LinkedList_Each( node, &display.surfaces ) ) { - sr = node->data; - Surface_Delete( sr->surface ); + SurfaceRecord record = node->data; + Surface_Delete( record->surface ); LinkedList_DeleteNode( &display.surfaces, node ); } } @@ -300,7 +328,6 @@ int LCUIDisplay_SetMode( int mode ) { int ret; DEBUG_MSG("mode: %d\n", mode); - LCUIMutex_Lock( &display.mutex ); switch( mode ) { case LCDM_WINDOWED: ret = LCUIDisplay_Windowed(); @@ -313,7 +340,6 @@ int LCUIDisplay_SetMode( int mode ) ret = LCUIDisplay_FullScreen(); break; } - LCUIMutex_Unlock( &display.mutex ); return ret; } @@ -349,7 +375,6 @@ void LCUIDisplay_SetSize( int width, int height ) Widget_Resize( root, width, height ); } -/** 获取屏幕宽度 */ int LCUIDisplay_GetWidth( void ) { if( !display.is_working ) { @@ -362,7 +387,6 @@ int LCUIDisplay_GetWidth( void ) return display.driver->getWidth(); } -/** 获取屏幕高度 */ int LCUIDisplay_GetHeight( void ) { if( !display.is_working ) { @@ -375,21 +399,6 @@ int LCUIDisplay_GetHeight( void ) return display.driver->getHeight(); } -/** LCUI的图形显示处理线程 */ -static void LCUIDisplay_Thread( void *unused ) -{ - while( LCUI_IsActive() && display.is_working ) { - LCUICursor_UpdatePos(); /* 更新鼠标位置 */ - LCUIWidget_StepTask(); /* 处理所有部件任务 */ - LCUIMutex_Lock( &display.mutex ); - LCUIDisplay_Update(); - LCUIMutex_Unlock( &display.mutex ); - /* 让本帧停留一段时间 */ - FrameControl_Remain( display.fc_ctx ); - } - LCUIThread_Exit(NULL); -} - void Surface_Delete( LCUI_Surface surface ) { if( display.is_working ) { @@ -563,16 +572,15 @@ static void OnSurfaceEvent( LCUI_Widget w, LCUI_WidgetEvent e, void *arg ) /** 在 surface 主动产生无效区域并需要绘制的时候 */ static void OnPaint( LCUI_Event e, void *arg ) { - SurfaceRecord *sr; LinkedListNode *node; LCUI_DisplayEvent dpy_ev = arg; for( LinkedList_Each( node, &display.surfaces ) ) { - sr = node->data; - if( sr && sr->surface != dpy_ev->surface ) { + SurfaceRecord record = node->data; + if( record && record->surface != dpy_ev->surface ) { continue; } - Widget_InvalidateArea( sr->widget, &dpy_ev->paint.rect, - SV_GRAPH_BOX ); + Widget_InvalidateArea( record->widget, + &dpy_ev->paint.rect, SV_GRAPH_BOX ); } } @@ -606,15 +614,14 @@ int LCUI_InitDisplay( LCUI_DisplayDriver driver ) if( display.is_working ) { return -1; } - LOG("[display] init ...\n"); + LOG( "[display] init ...\n" ); display.mode = 0; root = LCUIWidget_GetRoot(); - LCUIMutex_Init( &display.mutex ); LinkedList_Init( &display.surfaces ); if( !driver ) { driver = LCUI_CreateDisplayDriver(); if( !driver ) { - LOG("[display] init failed\n"); + LOG( "[display] init failed\n" ); return -2; } } @@ -626,8 +633,8 @@ int LCUI_InitDisplay( LCUI_DisplayDriver driver ) FrameControl_SetMaxFPS( display.fc_ctx, MAX_FRAMES_PER_SEC ); Widget_BindEvent( root, "surface", OnSurfaceEvent, NULL, NULL ); LCUIDisplay_SetMode( LCDM_DEFAULT ); - LOG("[display] init ok, driver name: %s\n", display.driver->name); - return LCUIThread_Create( &display.thread, LCUIDisplay_Thread, NULL ); + LOG( "[display] init ok, driver name: %s\n", display.driver->name ); + return 0; } /** 停用图形输出模块 */ @@ -637,7 +644,6 @@ int LCUI_ExitDisplay( void ) return -1; } display.is_working = FALSE; - LCUIMutex_Destroy( &display.mutex ); FrameControl_Destroy( display.fc_ctx ); - return LCUIThread_Join( display.thread, NULL ); + return 0; } diff --git a/src/gui/widget/textedit.c b/src/gui/widget/textedit.c index 5aa53492..6173e326 100644 --- a/src/gui/widget/textedit.c +++ b/src/gui/widget/textedit.c @@ -452,7 +452,6 @@ void TextEdit_SetMultiline( LCUI_Widget widget, LCUI_BOOL is_true ) void TextEdit_ClearText( LCUI_Widget widget ) { LCUI_TextEdit edit; - Widget_Lock( widget ); edit = Widget_GetData( widget, self.prototype ); LCUIMutex_Lock( &edit->mutex ); TextLayer_ClearText( edit->layer ); @@ -461,7 +460,6 @@ void TextEdit_ClearText( LCUI_Widget widget ) Widget_AddTask( widget, WTT_USER ); LCUIMutex_Unlock( &edit->mutex ); Widget_InvalidateArea( widget, NULL, SV_PADDING_BOX ); - Widget_Unlock( widget ); } int TextEdit_GetTextW( LCUI_Widget w, int start, int max_len, wchar_t *buf ) @@ -519,14 +517,12 @@ int TextEdit_InsertTextW( LCUI_Widget w, const wchar_t *wstr ) int TextEdit_SetPlaceHolderW( LCUI_Widget w, const wchar_t *wstr ) { LCUI_TextEdit edit = Widget_GetData( w, self.prototype ); - Widget_Lock( w ); LCUIMutex_Lock( &edit->mutex ); TextLayer_ClearText( edit->layer_placeholder ); LCUIMutex_Unlock( &edit->mutex ); if( edit->is_placeholder_shown ) { Widget_InvalidateArea( w, NULL, SV_PADDING_BOX ); } - Widget_Unlock( w ); return TextEdit_AddTextToBuffer( w, wstr, TBAT_INSERT, TBOT_PLACEHOLDER ); } @@ -579,7 +575,6 @@ static void TextEdit_TextBackspace( LCUI_Widget widget, int n_ch ) { LCUI_TextEdit edit; LCUI_WidgetEventRec ev; - Widget_Lock( widget ); edit = Widget_GetData( widget, self.prototype ); LCUIMutex_Lock( &edit->mutex ); TextLayer_TextBackspace( edit->layer_source, n_ch ); @@ -590,7 +585,6 @@ static void TextEdit_TextBackspace( LCUI_Widget widget, int n_ch ) edit->tasks[TASK_UPDATE] = TRUE; Widget_AddTask( widget, WTT_USER ); LCUIMutex_Unlock( &edit->mutex ); - Widget_Unlock( widget ); ev.type = self.event_id; ev.cancel_bubble = TRUE; Widget_TriggerEvent( widget, &ev, NULL ); @@ -600,7 +594,6 @@ static void TextEdit_TextDelete(LCUI_Widget widget, int n_ch ) { LCUI_TextEdit edit; LCUI_WidgetEventRec ev; - Widget_Lock( widget ); edit = Widget_GetData( widget, self.prototype ); LCUIMutex_Lock( &edit->mutex ); TextLayer_TextDelete( edit->layer_source, n_ch ); @@ -611,7 +604,6 @@ static void TextEdit_TextDelete(LCUI_Widget widget, int n_ch ) edit->tasks[TASK_UPDATE] = TRUE; Widget_AddTask( widget, WTT_USER ); LCUIMutex_Unlock( &edit->mutex ); - Widget_Unlock( widget ); ev.type = self.event_id; ev.cancel_bubble = TRUE; Widget_TriggerEvent( widget, &ev, NULL ); diff --git a/src/gui/widget/textview.c b/src/gui/widget/textview.c index 8b57a987..2ad28615 100644 --- a/src/gui/widget/textview.c +++ b/src/gui/widget/textview.c @@ -499,7 +499,6 @@ int TextView_SetTextW( LCUI_Widget w, const wchar_t *text ) } else { wcscpy( newtext, text ); } - Widget_Lock( w ); txt = Widget_GetData( w, self.prototype ); if( txt->tasks[TASK_SET_TEXT].is_valid && txt->tasks[TASK_SET_TEXT].text ) { @@ -508,7 +507,6 @@ int TextView_SetTextW( LCUI_Widget w, const wchar_t *text ) txt->tasks[TASK_SET_TEXT].is_valid = TRUE; txt->tasks[TASK_SET_TEXT].text = newtext; Widget_AddTask( w, WTT_USER ); - Widget_Unlock( w ); return 0; } diff --git a/src/gui/widget_base.c b/src/gui/widget_base.c index 9d454fe5..9018c8f3 100755 --- a/src/gui/widget_base.c +++ b/src/gui/widget_base.c @@ -265,7 +265,6 @@ static void Widget_Init( LCUI_Widget widget ) LinkedList_Init( &widget->children ); LinkedList_Init( &widget->children_show ); LinkedList_Init( &widget->dirty_rects ); - LCUIMutex_Init( &widget->mutex ); Graph_Init( &widget->graph ); } @@ -332,7 +331,6 @@ void Widget_ExecDestroy( LCUI_Widget widget ) widget->attributes ? Dict_Release( widget->attributes ) : 0; widget->classes ? StrList_Destroy( widget->classes ) : 0; widget->status ? StrList_Destroy( widget->status ) : 0; - LCUIMutex_Destroy( &widget->mutex ); EventTrigger_Destroy( widget->trigger ); widget->trigger = NULL; free( widget ); @@ -1474,16 +1472,6 @@ void Widget_SetDisabled( LCUI_Widget w, LCUI_BOOL disabled ) } } -void Widget_Lock( LCUI_Widget w ) -{ - LCUIMutex_Lock( &w->mutex ); -} - -void Widget_Unlock( LCUI_Widget w ) -{ - LCUIMutex_Unlock( &w->mutex ); -} - int Widget_SetAttributeEx( LCUI_Widget w, const char *name, void *value, int value_type, void( *value_destructor )(void*) ) { @@ -1565,16 +1553,12 @@ LCUI_BOOL Widget_CheckPrototype( LCUI_Widget w, LCUI_WidgetPrototypeC proto ) /** 为部件添加一个类 */ int Widget_AddClass( LCUI_Widget w, const char *class_name ) { - Widget_Lock( w ); if( strshas( w->classes, class_name ) ) { - Widget_Unlock( w ); return 1; } if( strsadd( &w->classes, class_name ) <= 0 ) { - Widget_Unlock( w ); return 0; } - Widget_Unlock( w ); Widget_HandleChildrenStyleChange( w, 0, class_name ); Widget_UpdateStyle( w, TRUE ); return 1; @@ -1583,72 +1567,53 @@ int Widget_AddClass( LCUI_Widget w, const char *class_name ) /** 判断部件是否包含指定的类 */ LCUI_BOOL Widget_HasClass( LCUI_Widget w, const char *class_name ) { - Widget_Lock( w ); if( strshas( w->classes, class_name ) ) { - Widget_Unlock( w ); return TRUE; } - Widget_Unlock( w ); return FALSE; } /** 从部件中移除一个类 */ int Widget_RemoveClass( LCUI_Widget w, const char *class_name ) { - Widget_Lock( w ); if( strshas( w->classes, class_name ) ) { Widget_HandleChildrenStyleChange( w, 0, class_name ); strsdel( &w->classes, class_name ); Widget_UpdateStyle( w, TRUE ); - Widget_Unlock( w ); return 1; } - Widget_Unlock( w ); return 0; } -/** 为部件添加一个状态 */ int Widget_AddStatus( LCUI_Widget w, const char *status_name ) { - Widget_Lock( w ); if( strshas( w->status, status_name ) ) { - Widget_Unlock( w ); return 0; } if( strsadd( &w->status, status_name ) <= 0 ) { - Widget_Unlock( w ); return 0; } - Widget_Unlock( w ); Widget_HandleChildrenStyleChange( w, 1, status_name ); Widget_UpdateStyle( w, TRUE ); return 1; } -/** 判断部件是否包含指定的状态 */ LCUI_BOOL Widget_HasStatus( LCUI_Widget w, const char *status_name ) { - Widget_Lock( w ); if( strshas( w->status, status_name ) ) { - Widget_Unlock( w ); return TRUE; } - Widget_Unlock( w ); return FALSE; } -/** 从部件中移除一个状态 */ int Widget_RemoveStatus( LCUI_Widget w, const char *status_name ) { - Widget_Lock( w ); if( strshas( w->status, status_name ) ) { Widget_HandleChildrenStyleChange( w, 1, status_name ); strsdel( &w->status, status_name ); Widget_UpdateStyle( w, TRUE ); - Widget_Unlock( w ); return 1; } - Widget_Unlock( w ); return 0; } @@ -1859,7 +1824,7 @@ extern void LCUIWidget_AddTextEdit( void ); void LCUI_InitWidget( void ) { - LCUIWidget_InitTask(); + LCUIWidget_InitTasks(); LCUIWidget_InitEvent(); LCUIWidget_InitPrototype(); LCUIWidget_InitStyle(); diff --git a/src/gui/widget_paint.c b/src/gui/widget_paint.c index 37bf4453..27386714 100644 --- a/src/gui/widget_paint.c +++ b/src/gui/widget_paint.c @@ -158,7 +158,6 @@ static void Widget_OnPaint( LCUI_Widget w, LCUI_PaintContext paint ) LCUI_Rect box; LCUI_WidgetStyle *s; - Widget_Lock( w ); box.x = box.y = 0; s = &w->computed_style; box.width = w->box.graph.width; @@ -174,7 +173,6 @@ static void Widget_OnPaint( LCUI_Widget w, LCUI_PaintContext paint ) box.height = w->box.border.height; Graph_DrawBackground( paint, &box, &s->background ); Graph_DrawBorder( paint, &box, &s->border ); - Widget_Unlock( w ); if( w->proto && w->proto->paint ) { w->proto->paint( w, paint ); } diff --git a/src/gui/widget_style.c b/src/gui/widget_style.c index 35547c3c..70d0d197 100644 --- a/src/gui/widget_style.c +++ b/src/gui/widget_style.c @@ -72,7 +72,6 @@ LCUI_SelectorNode Widget_GetSelectorNode( LCUI_Widget w ) int i; ASSIGN( sn, LCUI_SelectorNode ); ZEROSET( sn, LCUI_SelectorNode ); - Widget_Lock( w ); if( w->id ) { sn->id = strdup( w->id ); } @@ -91,7 +90,6 @@ LCUI_SelectorNode Widget_GetSelectorNode( LCUI_Widget w ) w->status[i] ); } } - Widget_Unlock( w ); SelectorNode_Update( sn ); return sn; } diff --git a/src/gui/widget_task.c b/src/gui/widget_task.c index 075f375b..9ada4c76 100644 --- a/src/gui/widget_task.c +++ b/src/gui/widget_task.c @@ -141,14 +141,14 @@ static void MapTaskHandler(void) self.handlers[WTT_PROPS] = Widget_UpdateProps; } -void LCUIWidget_InitTask( void ) +void LCUIWidget_InitTasks( void ) { MapTaskHandler(); self.timeout = 0; LinkedList_Init( &self.trash ); } -void LCUIWidget_ExitTask( void ) +void LCUIWidget_ExitTasks( void ) { LinkedList_Clear( &self.trash, NULL ); } @@ -178,8 +178,8 @@ int Widget_UpdateEx( LCUI_Widget w, LCUI_BOOL has_timeout ) LCUI_BOOL *buffer; LinkedListNode *node, *next; - /* 如果该部件没有任务需要处理、或者被其它线程占用 */ - if( !w->task.for_self || LCUIMutex_TryLock( &w->mutex ) != 0 ) { + /* 如果该部件没有任务需要处理 */ + if( !w->task.for_self ) { goto proc_children_task; } w->task.for_self = FALSE; @@ -198,7 +198,6 @@ int Widget_UpdateEx( LCUI_Widget w, LCUI_BOOL has_timeout ) buffer[i] = FALSE; } } - LCUIMutex_Unlock( &w->mutex ); /* 如果部件还处于未准备完毕的状态 */ if( w->state < WSTATE_READY ) { w->state |= WSTATE_UPDATED; @@ -248,7 +247,7 @@ proc_children_task: return w->task.for_self || w->task.for_children; } -void LCUIWidget_StepTask( void ) +void LCUIWidget_Update( void ) { LCUI_Widget root; LinkedListNode *node; diff --git a/src/main.c b/src/main.c index 01ec81b9..e2455572 100644 --- a/src/main.c +++ b/src/main.c @@ -208,7 +208,7 @@ void LCUI_DestroyEvent( LCUI_SysEvent e ) /*--------------------------- system event ----------------------------*/ -void LCUI_DispatchEvent( void ) +LCUI_BOOL LCUI_ProcessTask( void ) { LCUI_AppTask task; LinkedListNode *node; @@ -222,15 +222,18 @@ void LCUI_DispatchEvent( void ) LCUI_DeleteTask( task ); free( task ); free( node ); - return; - } else { - LCUIMutex_Unlock( &MainApp.agent.mutex ); - } - if( MainApp.agent.state == STATE_RUNNING ) { - return; + return TRUE; } + LCUIMutex_Unlock( &MainApp.agent.mutex ); + return FALSE; +} + +void LCUI_ProcessEvents( void ) +{ + int i; + for( i = 0; LCUI_ProcessTask() && i < 100; ++i ); if( MainApp.driver_ready ) { - MainApp.driver->DispatchEvent(); + return MainApp.driver->ProcessEvents(); } } @@ -269,7 +272,7 @@ int LCUI_RunTask( LCUI_AppTask task ) } /* 新建一个主循环 */ -LCUI_MainLoop LCUI_MainLoop_New( void ) +LCUI_MainLoop LCUIMainLoop_New( void ) { LCUI_MainLoop loop; loop = NEW( LCUI_MainLoopRec, 1 ); @@ -279,11 +282,11 @@ LCUI_MainLoop LCUI_MainLoop_New( void ) } /** 运行目标主循环 */ -int LCUI_MainLoop_Run( LCUI_MainLoop loop ) +int LCUIMainLoop_Run( LCUI_MainLoop loop ) { LCUI_BOOL at_same_thread = FALSE; if( loop->state == STATE_RUNNING ) { - DEBUG_MSG("error: main-loop already running."); + DEBUG_MSG( "error: main-loop already running.\n" ); return -1; } loop->state = STATE_RUNNING; @@ -291,7 +294,7 @@ int LCUI_MainLoop_Run( LCUI_MainLoop loop ) if( MainApp.loop ) { at_same_thread = MainApp.loop->tid == loop->tid; } - DEBUG_MSG("at_same_thread: %d\n", at_same_thread); + DEBUG_MSG( "at_same_thread: %d\n", at_same_thread ); if( !at_same_thread ) { LCUIMutex_Lock( &MainApp.loop_mutex ); LinkedList_Insert( &MainApp.loops, 0, loop ); @@ -299,15 +302,18 @@ int LCUI_MainLoop_Run( LCUI_MainLoop loop ) } else { LinkedList_Insert( &MainApp.loops, 0, loop ); } - DEBUG_MSG("loop: %p, enter\n", loop); + DEBUG_MSG( "loop: %p, enter\n", loop ); MainApp.loop = loop; while( loop->state != STATE_EXITED ) { LCUI_WaitEvent(); - LCUI_DispatchEvent(); + LCUI_ProcessEvents(); + LCUIDisplay_Update(); + LCUIDisplay_Render(); + LCUIDisplay_Present(); /* 如果当前运行的主循环不是自己 */ while( MainApp.loop != loop ) { loop->state = STATE_PAUSED; - LCUICond_Wait( &MainApp.loop_changed, + LCUICond_Wait( &MainApp.loop_changed, &MainApp.loop_mutex ); } } @@ -319,13 +325,13 @@ int LCUI_MainLoop_Run( LCUI_MainLoop loop ) /* 改变当前运行的主循环 */ MainApp.loop = loop; } - DEBUG_MSG("loop: %p, exit\n", loop); + DEBUG_MSG( "loop: %p, exit\n", loop ); LCUICond_Broadcast( &MainApp.loop_changed ); return 0; } /** 标记目标主循环需要退出 */ -void LCUI_MainLoop_Quit( LCUI_MainLoop loop ) +void LCUIMainLoop_Quit( LCUI_MainLoop loop ) { loop->state = STATE_EXITED; } @@ -365,7 +371,7 @@ static void LCUI_ExitApp( void ) LinkedListNode *node; for( LinkedList_Each( node, &MainApp.loops ) ) { loop = node->data; - LCUI_MainLoop_Quit( loop ); + LCUIMainLoop_Quit( loop ); LCUIThread_Join( loop->tid, NULL ); } LCUIMutex_Destroy( &MainApp.loop_mutex ); @@ -495,8 +501,7 @@ static void LCUI_ShowCopyrightText(void) ); } -/** 检测LCUI是否活动 */ -LCUI_BOOL LCUI_IsActive(void) +LCUI_BOOL LCUI_IsActive( void ) { if( System.state == STATE_ACTIVE ) { return TRUE; @@ -504,8 +509,7 @@ LCUI_BOOL LCUI_IsActive(void) return FALSE; } -/** 检测当前是否在主线程上 */ -LCUI_BOOL LCUI_IsOnMainLoop(void) +LCUI_BOOL LCUI_IsOnMainLoop( void ) { if( !MainApp.loop ) { return FALSE; @@ -565,19 +569,14 @@ void LCUI_Quit( void ) LCUIApp_QuitAllMainLoop(); } -/* - * 功能:LCUI程序的主循环 - * 说明:每个LCUI程序都需要调用它,此函数会让程序执行LCUI分配的任务 - * */ int LCUI_Main( void ) { LCUI_MainLoop loop; - loop = LCUI_MainLoop_New(); - LCUI_MainLoop_Run( loop ); + loop = LCUIMainLoop_New(); + LCUIMainLoop_Run( loop ); return LCUI_Destroy(); } -/* 获取LCUI的版本 */ int LCUI_GetSelfVersion( char *out ) { return sprintf(out, "%s", LCUI_VERSION); diff --git a/src/platform/windows/windows_events.c b/src/platform/windows/windows_events.c index 386e695f..d2d66208 100644 --- a/src/platform/windows/windows_events.c +++ b/src/platform/windows/windows_events.c @@ -90,13 +90,15 @@ static LCUI_BOOL WIN_WaitEvent( void ) return WaitMessage(); } -static void WIN_DispatchEvent( void ) +static LCUI_BOOL WIN_DispatchEvent( void ) { MSG msg; if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); + return TRUE; } + return FALSE; } static int WIN_BindSysEvent( int event_id, LCUI_EventFunc func, @@ -170,7 +172,7 @@ LCUI_AppDriver LCUI_CreateWinAppDriver( void ) app->GetData = WIN_GetData; app->PostTask = WIN_PostTask; app->WaitEvent = WIN_WaitEvent; - app->DispatchEvent = WIN_DispatchEvent; + app->ProcessEvents = WIN_DispatchEvent; app->BindSysEvent = WIN_BindSysEvent; app->UnbindSysEvent = WIN_UnbindSysEvent; app->UnbindSysEvent2 = WIN_UnbindSysEvent2;