diff --git a/README.md b/README.md index 9cbf23bd..f37ee474 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # The LCUI Project +![LCUI Logo](http://lcui.org/static/images/lcui-project-logo.png) + [![Join the chat at https://gitter.im/lc-soft/LCUI](https://badges.gitter.im/lc-soft/LCUI.svg)](https://gitter.im/lc-soft/LCUI?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/lc-soft/LCUI.png?branch=master)](https://travis-ci.org/lc-soft/LCUI) - -![LCUI Logo](http://lcui.org/static/images/lcui-project-logo.png) +[![Downloads](https://img.shields.io/sourceforge/dt/lc-ui.svg)](https://sourceforge.net/projects/lc-ui) +[![License](https://img.shields.io/badge/license-GPLv2-blue.svg)](http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) ## Description diff --git a/include/LCUI/main.h b/include/LCUI/main.h index b056eefa..e77a2e48 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( *PumbEvents )(void); + void( *DispatchEvent )(void); LCUI_BOOL( *WaitEvent )(void); LCUI_BOOL( *PostTask )(LCUI_AppTask); int( *BindSysEvent )(int, LCUI_EventFunc, void*, void( *)(void*)); @@ -158,6 +158,8 @@ LCUI_API void LCUI_PumbEvents( void ); LCUI_API void *LCUI_GetAppData( void ); +LCUI_API void LCUI_SetTaskAgent( LCUI_BOOL enabled ); + /** 添加任务 */ LCUI_API LCUI_BOOL LCUI_PostTask( LCUI_AppTask task ); diff --git a/include/LCUI/platform/linux/linux_display.h b/include/LCUI/platform/linux/linux_display.h index d28407fc..83c2f19e 100644 --- a/include/LCUI/platform/linux/linux_display.h +++ b/include/LCUI/platform/linux/linux_display.h @@ -1,6 +1,9 @@ #ifndef LCUI_LINUX_DISPLAY_H #define LCUI_LINUX_DISPLAY_H +#include +#include + int LCUI_InitLinuxDisplay( LCUI_DisplayDriver driver ); int LCUI_ExitLinuxDisplay( void ); diff --git a/include/LCUI/platform/linux/linux_events.h b/include/LCUI/platform/linux/linux_events.h index 374e4926..8a29efce 100644 --- a/include/LCUI/platform/linux/linux_events.h +++ b/include/LCUI/platform/linux/linux_events.h @@ -1,6 +1,7 @@ #ifndef LCUI_LINUX_EVENTS_H #define LCUI_LINUX_EVENTS_H +#include int LCUI_InitLinuxApp( LCUI_AppDriver app ); diff --git a/include/LCUI/platform/linux/linux_x11display.h b/include/LCUI/platform/linux/linux_x11display.h index 8b137891..d0d32d07 100644 --- a/include/LCUI/platform/linux/linux_x11display.h +++ b/include/LCUI/platform/linux/linux_x11display.h @@ -1 +1,9 @@ +#ifndef LCUI_LINUX_X11_DISPLAY_H +#define LCUI_LINUX_X11_DISPLAY_H + +int LCUI_InitLinuxX11Display( LCUI_DisplayDriver driver ); + +int LCUI_ExitLinuxX11Display( void ); + +#endif \ No newline at end of file diff --git a/include/LCUI/platform/linux/linux_x11events.h b/include/LCUI/platform/linux/linux_x11events.h index 8b137891..6aad4ab3 100644 --- a/include/LCUI/platform/linux/linux_x11events.h +++ b/include/LCUI/platform/linux/linux_x11events.h @@ -1 +1,25 @@ +#ifndef LCUI_LINUX_X11_EVENTS_H +#define LCUI_LINUX_X11_EVENTS_H +#include +#include +#include +#include + +typedef struct LCUI_X11AppDriverRec_ { + int screen; + Display *display; + Window win_root; + Window win_main; + Colormap cmap; + Atom wm_lcui; + LCUI_EventTrigger trigger; +} LCUI_X11AppDriverRec, *LCUI_X11AppDriver; + +void LCUI_SetLinuxX11MainWindow( Window win ); + +void LCUI_PreInitLinuxX11App( void *data ); + +int LCUI_InitLinuxX11App( LCUI_AppDriver app ); + +#endif diff --git a/src/display.c b/src/display.c index 49523aa8..c2000ef9 100644 --- a/src/display.c +++ b/src/display.c @@ -51,6 +51,9 @@ #include #include LCUI_DISPLAY_H +#define DEFAULT_WIDTH 800 +#define DEFAULT_HEIGHT 600 + /** surface 记录 */ typedef struct SurfaceRecord { LCUI_Surface surface; /**< surface */ @@ -113,6 +116,9 @@ static void LCUIDisplay_Update(void) /* 在 surface 上逐个重绘无效区域 */ LinkedList_ForEach( rn, &rlist ) { paint = Surface_BeginPaint( p_sr->surface, rn->data ); + if( !paint ) { + continue; + } DEBUG_MSG( "[%s]: render rect: (%d,%d,%d,%d)\n", p_sr->widget->type, paint->rect.left, paint->rect.top, paint->rect.w, paint->rect.h ); @@ -230,9 +236,10 @@ static int LCUIDisplay_Windowed( void ) { LCUI_Widget root = LCUIWidget_GetRoot(); switch( display.mode ) { - case LCDM_FULLSCREEN: case LCDM_WINDOWED: return 0; + case LCDM_FULLSCREEN: + break; case LCDM_SEAMLESS: default: LCUIDisplay_CleanSurfaces(); @@ -240,7 +247,7 @@ static int LCUIDisplay_Windowed( void ) break; } Widget_Show( root ); - Widget_Resize( root, LCUIDisplay_GetWidth(), LCUIDisplay_GetHeight() ); + Widget_Resize( root, DEFAULT_WIDTH, DEFAULT_HEIGHT ); display.mode = LCDM_WINDOWED; return 0; } @@ -504,7 +511,6 @@ static void OnSurfaceEvent( LCUI_Widget w, LCUI_WidgetEvent e, void *arg ) e_type = *((int*)&arg); root = LCUIWidget_GetRoot(); surface = LCUIDisplay_GetBindSurface( e->target ); - DEBUG_MSG("tip, widget: %s, e_type = %d\n", w->type, e_type); if( display.mode == LCDM_SEAMLESS ) { if( !surface && e_type != WET_ADD ) { return; @@ -605,7 +611,7 @@ int LCUI_InitDisplay( void ) FrameControl_SetMaxFPS( display.fc_ctx, MAX_FRAMES_PER_SEC ); Widget_BindEvent( root, "surface", OnSurfaceEvent, NULL, NULL ); LCUIDisplay_SetMode( LCDM_DEFAULT ); - printf("[display] init ok.\n"); + printf("[display] init ok, driver name: %s\n", display.driver.name); return LCUIThread_Create( &display.thread, LCUIDisplay_Thread, NULL ); } diff --git a/src/font/charset.c b/src/font/charset.c index 4ca34d8c..3f51d3f5 100755 --- a/src/font/charset.c +++ b/src/font/charset.c @@ -1,7 +1,7 @@ /* *************************************************************************** * charset.c -- The charset opreation set. * -* Copyright (C) 2012-2016 by Liu Chao +* Copyright (C) 2015-2016 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 @@ /* **************************************************************************** * charset.c -- 字符集的相关操作函数 * -* 版权所有 (C) 2013-2016 归属于 刘超 +* 版权所有 (C) 2015-2016 归属于 刘超 * * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 * @@ -39,7 +39,7 @@ #include #include -#include +#include #include #include @@ -48,6 +48,25 @@ #define MAX_SAVE_NUM 20 +#ifdef LCUI_BUILD_IN_LINUX +static int SetLocaleByEncoding( int encoding ) +{ + const char *locale; + switch( encoding ) { + case ENCODING_ANSI: + locale = NULL; + break; + case ENCODING_UTF8: + locale = "zh_CN.UTF-8"; + break; + default: return -1; + } + if( !setlocale( LC_CTYPE, locale ) ) { + return -1; + } + return 0; +} +#else /** 将UTF-8字符串解码成 Unicode 字符串 */ static int DecodeFromUTF8( wchar_t *wstr, int max_len, const char *str ) { @@ -107,9 +126,12 @@ static int DecodeFromUTF8( wchar_t *wstr, int max_len, const char *str ) return len; } +#endif + int LCUI_DecodeString( wchar_t *wstr, const char *str, int max_len, int encoding ) { +#ifdef LCUI_BUILD_IN_WIN32 // 暂时不处理其它编码方式 switch( encoding ) { case ENCODING_ANSI: @@ -117,6 +139,12 @@ int LCUI_DecodeString( wchar_t *wstr, const char *str, default: break; } return DecodeFromUTF8( wstr, max_len, str ); +#else + if( SetLocaleByEncoding( encoding ) != 0 ) { + return -1; + } + return mbstowcs( wstr, str, max_len ); +#endif } #ifdef LCUI_BUILD_IN_WIN32 @@ -137,6 +165,9 @@ int LCUI_EncodeString( char *str, const wchar_t *wstr, } return encode( cp, wstr, str, max_len ); #else - return -1; + if( SetLocaleByEncoding( encoding ) != 0 ) { + return -1; + } + return wcstombs( str, wstr, max_len ); #endif } diff --git a/src/gui/widget_event.c b/src/gui/widget_event.c index a652f7d1..d75a55e4 100644 --- a/src/gui/widget_event.c +++ b/src/gui/widget_event.c @@ -37,6 +37,7 @@ * 没有,请查看:. * ***************************************************************************/ //#define DEBUG +#include #include #include #include @@ -496,6 +497,7 @@ static int Widget_TriggerEventEx( LCUI_Widget widget, LCUI_WidgetEvent e, return Widget_PostEvent( widget->parent, e, data, destroy_data ); } + return 0; } if( !widget->parent || e->cancel_bubble ) { return -1; @@ -893,7 +895,6 @@ int Widget_PostSurfaceEvent( LCUI_Widget w, int event_type ) } e.target = w; e.type = WET_SURFACE; - DEBUG_MSG("widget: %s, post event: %d\n", w->type, event_type ); return Widget_PostEvent( root, &e, *((int**)n), NULL ); } diff --git a/src/main.c b/src/main.c index c533780a..26974970 100644 --- a/src/main.c +++ b/src/main.c @@ -81,27 +81,26 @@ static struct LCUI_System { int state; /**< 状态 */ int mode; /**< LCUI的运行模式 */ LCUI_BOOL is_inited; /**< 标志,指示LCUI是否初始化过 */ - unsigned long int main_tid; /**< 主线程ID */ - struct { LCUI_EventTrigger trigger; /**< 系统事件容器 */ LCUI_Mutex mutex; /**< 互斥锁 */ } event; - - int exit_code; /**< 退出码 */ - void (*func_atexit)(void); /**< 在LCUI退出时调用的函数 */ } System; /** LCUI 应用程序数据 */ static struct LCUI_App { - LinkedList tasks; /**< 程序的任务队列 */ - LCUI_Mutex tasks_mutex; /**< 任务记录互斥锁 */ LCUI_Mutex loop_mutex; /**< 互斥锁,确保一次只允许一个线程跑主循环 */ LCUI_Cond loop_changed; /**< 条件变量,用于指示当前运行的主循环是否改变 */ LCUI_MainLoop loop; /**< 当前运行的主循环 */ LinkedList loops; /**< 主循环列表 */ LCUI_AppDriverRec driver; /**< 程序事件驱动支持 */ + struct LCUI_AppTaskAgent { + int state; /**< 状态 */ + LinkedList tasks; /**< 任务队列 */ + LCUI_Mutex mutex; /**< 互斥锁 */ + LCUI_Cond cond; /**< 条件变量 */ + } agent; } MainApp; /*-------------------------- system event ---------------------------*/ @@ -201,10 +200,14 @@ void LCUI_DestroyEvent( LCUI_SysEvent e ) LCUI_BOOL LCUI_PostTask( LCUI_AppTask task ) { - LCUI_AppTask buff; - buff = NEW( LCUI_AppTaskRec, 1 ); - *buff = *task; - return MainApp.driver.PostTask( buff ); + LCUI_AppTask newtask; + newtask = NEW( LCUI_AppTaskRec, 1 ); + *newtask = *task; + LCUIMutex_Lock( &MainApp.agent.mutex ); + LinkedList_Append( &MainApp.agent.tasks, newtask ); + LCUICond_Signal( &MainApp.agent.cond ); + LCUIMutex_Unlock( &MainApp.agent.mutex ); + return MainApp.driver.PostTask( newtask ); } void LCUI_DeleteTask( LCUI_AppTask task ) @@ -294,14 +297,31 @@ static void LCUIApp_Init(void) LCUICond_Init( &MainApp.loop_changed ); LCUIMutex_Init( &MainApp.loop_mutex ); LinkedList_Init( &MainApp.loops ); - LCUIMutex_Init( &MainApp.tasks_mutex ); - LinkedList_Init( &MainApp.tasks ); + LCUIMutex_Init( &MainApp.agent.mutex ); + LCUICond_Init( &MainApp.agent.cond ); + LinkedList_Init( &MainApp.agent.tasks ); LCUI_InitApp( &MainApp.driver ); + MainApp.agent.state = STATE_RUNNING; } LCUI_BOOL LCUI_WaitEvent( void ) { - return MainApp.driver.WaitEvent(); + if( MainApp.agent.tasks.length > 0 ) { + return TRUE; + } + if( MainApp.agent.state != STATE_RUNNING ) { + return MainApp.driver.WaitEvent(); + } + LCUIMutex_Lock( &MainApp.agent.mutex ); + while( MainApp.agent.state == STATE_RUNNING ) { + if( MainApp.agent.tasks.length > 0 ) { + LCUIMutex_Unlock( &MainApp.agent.mutex ); + return TRUE; + } + LCUICond_Wait( &MainApp.agent.cond, &MainApp.agent.mutex ); + } + LCUIMutex_Unlock( &MainApp.agent.mutex ); + return FALSE; } int LCUI_BindSysEvent( int event_id, LCUI_EventFunc func, @@ -315,9 +335,35 @@ int LCUI_UnbindSysEvent( int event_id, LCUI_EventFunc func ) return MainApp.driver.UnbindSysEvent( event_id, func ); } +static void LCUI_DispatchEvent( void ) +{ + LCUI_AppTask task; + LinkedListNode *node; + LCUIMutex_Lock( &MainApp.agent.mutex ); + node = LinkedList_GetNode( &MainApp.agent.tasks, 0 ); + if( node ) { + task = node->data; + LinkedList_Unlink( &MainApp.agent.tasks, node ); + LCUIMutex_Unlock( &MainApp.agent.mutex ); + LCUI_RunTask( task ); + LCUI_DeleteTask( task ); + free( task ); + free( node ); + return; + } else { + LCUIMutex_Unlock( &MainApp.agent.mutex ); + } + if( MainApp.agent.state == STATE_RUNNING ) { + return; + } + MainApp.driver.DispatchEvent(); +} + void LCUI_PumbEvents( void ) { - MainApp.driver.PumbEvents(); + while( LCUI_WaitEvent() ) { + LCUI_DispatchEvent(); + } } void *LCUI_GetAppData( void ) @@ -325,6 +371,18 @@ void *LCUI_GetAppData( void ) return MainApp.driver.GetData(); } +void LCUI_SetTaskAgent( LCUI_BOOL enabled ) +{ + LCUIMutex_Lock( &MainApp.agent.mutex ); + if( enabled ) { + MainApp.agent.state = STATE_RUNNING; + } else { + MainApp.agent.state = STATE_PAUSED; + } + LCUICond_Signal( &MainApp.agent.cond ); + LCUIMutex_Unlock( &MainApp.agent.mutex ); +} + /** 退出所有主循环 */ static void LCUIApp_QuitAllMainLoop(void) { @@ -338,6 +396,12 @@ static void LCUIApp_QuitAllMainLoop(void) } } +static void OnDeleteTask( void *arg ) +{ + LCUI_DeleteTask( arg ); + free( arg ); +} + /* 销毁程序占用的资源 */ static void LCUIApp_Destroy(void) { @@ -351,8 +415,10 @@ static void LCUIApp_Destroy(void) /* 开始清理 */ LCUIMutex_Destroy( &MainApp.loop_mutex ); LCUICond_Destroy( &MainApp.loop_changed ); - LCUIMutex_Destroy( &MainApp.tasks_mutex ); + LCUIMutex_Destroy( &MainApp.agent.mutex ); + LCUICond_Destroy( &MainApp.agent.cond ); LinkedList_Clear( &MainApp.loops, free ); + LinkedList_Clear( &MainApp.agent.tasks, OnDeleteTask ); } /** 打印LCUI的信息 */ @@ -429,8 +495,6 @@ int LCUI_Init(void) return -1; } System.is_inited = TRUE; - System.func_atexit = NULL; - System.exit_code = 0; System.state = STATE_ACTIVE; System.main_tid = LCUIThread_SelfID(); LCUI_ShowCopyrightText(); @@ -463,7 +527,7 @@ static int LCUI_Destroy( void ) LCUI_ExitTimer(); LCUI_ExitDisplay(); LCUIApp_Destroy(); - return System.exit_code; + return 0; } void LCUI_Quit(void) @@ -472,12 +536,6 @@ void LCUI_Quit(void) LCUIApp_QuitAllMainLoop(); } -void LCUI_Exit( int exit_code ) -{ - System.exit_code = exit_code; - LCUI_Quit(); -} - /* * 功能:LCUI程序的主循环 * 说明:每个LCUI程序都需要调用它,此函数会让程序执行LCUI分配的任务 diff --git a/src/platform/linux/linux_display.c b/src/platform/linux/linux_display.c index 7b2b7998..5d97fc5f 100644 --- a/src/platform/linux/linux_display.c +++ b/src/platform/linux/linux_display.c @@ -4,9 +4,15 @@ #include #include #include +#include LCUI_EVENTS_H +#include LCUI_DISPLAY_H int LCUI_InitLinuxDisplay( LCUI_DisplayDriver driver ) { + LCUI_BOOL is_x11_mode = TRUE; + if( is_x11_mode ) { + return LCUI_InitLinuxX11Display( driver ); + } return -1; } diff --git a/src/platform/linux/linux_events.c b/src/platform/linux/linux_events.c index 8c90705a..6092f322 100644 --- a/src/platform/linux/linux_events.c +++ b/src/platform/linux/linux_events.c @@ -13,6 +13,10 @@ void LCUI_PreInitLinuxApp( void *data ) int LCUI_InitLinuxApp( LCUI_AppDriver app ) { + LCUI_BOOL is_x11_mode = TRUE; + if( is_x11_mode ) { + return LCUI_InitLinuxX11App( app ); + } return -1; } #endif diff --git a/src/platform/linux/linux_x11display.c b/src/platform/linux/linux_x11display.c index 8b137891..1ab5e396 100644 --- a/src/platform/linux/linux_x11display.c +++ b/src/platform/linux/linux_x11display.c @@ -1 +1,465 @@ +/* *************************************************************************** + * linux_x11display.c -- surface support for linux xwindow. + * + * Copyright (C) 2016 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. + * + * (GPLv2 is abbreviation of GNU General Public License Version 2) + * + * By continuing to use, modify, or distribute this file you indicate that you + * have read the license and understand and accept it fully. + * + * The LCUI project is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GPL v2 for more details. + * + * You should have received a copy of the GPLv2 along with this file. It is + * usually in the LICENSE.TXT file, If not, see . + * ****************************************************************************/ +/* **************************************************************************** + * linux_x11display.c -- linux 平台的图形显示功能支持,基于 xwindow。 + * + * 版权所有 (C) 2016 归属于 刘超 + * + * 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。 + * + * (GPLv2 是 GNU通用公共许可证第二版 的英文缩写) + * + * 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。 + * + * LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特 + * 定用途的隐含担保,详情请参照GPLv2许可协议。 + * + * 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果 + * 没有,请查看:. + * ****************************************************************************/ + +//#define DEBUG +#include +#include +#include +#define LCUI_SURFACE_C +#ifdef LCUI_BUILD_IN_LINUX +#include +#include +#include +#include +#include LCUI_DISPLAY_H +#include LCUI_EVENTS_H + +#define MIN_WIDTH 50 +#define MIN_HEIGHT 50 + +enum SurfaceTaskType { + TASK_CREATE, + TASK_MOVE, + TASK_RESIZE, + TASK_SHOW, + TASK_SET_CAPTION, + TASK_PRESENT, + TASK_DELETE, + TASK_TOTAL_NUM +}; + +typedef struct LCUI_SurfaceTaskRec_ { + int type; + union { + struct { + int x, y; + }; + struct { + int width, height; + }; + LCUI_BOOL show; + struct { + char *caption; + size_t caption_len; + }; + }; +} LCUI_SurfaceTaskRec, *LCUI_SurfaceTask; + +typedef struct LCUI_SurfaceRec_ { + int mode; + int width; + int height; + GC gc; + Window window; + XImage *ximage; + LCUI_BOOL is_ready; + LCUI_Graph fb; + LinkedList rects; + LinkedListNode node; +} LCUI_SurfaceRec; + +static struct X11_Display { + XVisualInfo vinfo; + LCUI_BOOL is_inited; + LinkedList surfaces; + LCUI_X11AppDriver app; + LCUI_EventTrigger trigger; +} x11; + +static void ReleaseSurfaceTask( void *arg ) +{ + LCUI_SurfaceTask task = arg; + if( task->type == TASK_SET_CAPTION ) { + free( task->caption ); + task->caption = NULL; + } +} + +static LCUI_Surface GetSurfaceByWindow( Window win ) +{ + LinkedListNode *node; + LinkedList_ForEach( node, &x11.surfaces ) { + if( ((LCUI_Surface)node->data)->window == win ) { + return node->data; + } + } + return NULL; +} + +static void X11Surface_OnResize( LCUI_Surface s, int width, int height ) +{ + int depth; + XGCValues gcv; + Visual *visual; + if( s->ximage ) { + XDestroyImage( s->ximage ); + s->ximage = NULL; + } + if( s->gc ) { + XFreeGC( x11.app->display, s->gc ); + s->gc = NULL; + } + Graph_Init( &s->fb ); + s->width = width; + s->height = height; + depth = DefaultDepth( x11.app->display, x11.app->screen ); + switch( depth ) { + case 32: + case 24: + s->fb.color_type = COLOR_TYPE_ARGB; + break; + default: + printf("[x11display] unsupport depth: %d.\n", depth); + break; + } + Graph_Create( &s->fb, width, height ); + visual = DefaultVisual( x11.app->display, x11.app->screen ); + s->ximage = XCreateImage( x11.app->display, visual, depth, ZPixmap, + 0, (char *)(s->fb.bytes), + width, height, 32, 0 ); + if( !s->ximage ) { + Graph_Free( &s->fb ); + printf("[x11display] create XImage faild.\n"); + return; + } + gcv.graphics_exposures = False; + s->gc = XCreateGC( x11.app->display, s->window, + GCGraphicsExposures, &gcv ); + if( !s->gc ) { + printf("[x11display] create graphics context faild.\n"); + return; + } +} + +static void X11Surface_OnCreate( LCUI_Surface s ) +{ + unsigned long bdcolor = BlackPixel(x11.app->display, x11.app->screen); + unsigned long bgcolor = WhitePixel(x11.app->display, x11.app->screen); + s->window = XCreateSimpleWindow( x11.app->display, x11.app->win_root, + 0, 100, MIN_WIDTH, MIN_HEIGHT, 1, + bdcolor, bgcolor ); + LinkedList_Init( &s->rects ); + X11Surface_OnResize( s, MIN_WIDTH, MIN_HEIGHT ); + s->is_ready = TRUE; + LCUI_SetLinuxX11MainWindow( s->window ); +} + +static void X11Surface_OnTask( LCUI_Surface surface, LCUI_SurfaceTask task ) +{ + Window win = surface->window; + Display *dpy = x11.app->display; + + switch( task->type ) { + case TASK_CREATE: + X11Surface_OnCreate( surface ); + break; + case TASK_RESIZE: { + int w = task->width , h = task->height; + w = MIN_WIDTH > w ? MIN_WIDTH: w; + h = MIN_HEIGHT > h ? MIN_HEIGHT: h; + X11Surface_OnResize( surface, w, h ); + XResizeWindow( dpy, win, w, h ); + break; + } + case TASK_MOVE: + XMoveWindow( dpy, win, task->x, task->y ); + break; + case TASK_SHOW: + if( task->show ) { + XMapWindow( dpy, win ); + } else { + XUnmapWindow( dpy, win ); + } + break; + case TASK_SET_CAPTION: { + XTextProperty name; + name.value = (unsigned char*)task->caption; + name.encoding = XA_STRING; + name.format = 8 * sizeof(char); + name.nitems = task->caption_len; + XSetWMName( dpy, win, &name ); + break; + } + case TASK_PRESENT: { + LinkedListNode *node; + LinkedList_ForEach( node, &surface->rects ) { + LCUI_Rect *rect = node->data; + XPutImage( x11.app->display, surface->window, surface->gc, + surface->ximage, 0, 0, rect->x, rect->y, + rect->width, rect->height); + } + LinkedList_Clear( &surface->rects, free ); + break; + } + case TASK_DELETE: + default: break; + } +} + +static void X11Surface_SendTask( LCUI_Surface surface, LCUI_SurfaceTask task ) +{ + LCUI_AppTaskRec apptask; + LCUI_SurfaceTask newtask; + newtask = NEW( LCUI_SurfaceTaskRec, 1 ); + *newtask = *task; + apptask.arg[0] = surface; + apptask.arg[1] = newtask; + apptask.destroy_arg[0] = NULL; + apptask.destroy_arg[1] = ReleaseSurfaceTask; + apptask.func = (LCUI_AppTaskFunc)X11Surface_OnTask; + LCUI_PostTask( &apptask ); +} + +static void X11Surface_Delete( LCUI_Surface surface ) +{ + LCUI_SurfaceTaskRec task; + task.type = TASK_DELETE; + LinkedList_Unlink( &x11.surfaces, &surface->node ); + X11Surface_SendTask( surface, &task ); +} + +static LCUI_Surface X11Surface_New( void ) +{ + LCUI_Surface surface; + LCUI_SurfaceTaskRec task; + task.type = TASK_CREATE; + surface = NEW( LCUI_SurfaceRec, 1 ); + surface->is_ready = FALSE; + surface->node.data = surface; + surface->gc = NULL; + surface->ximage = NULL; + Graph_Init( &surface->fb ); + surface->fb.color_type = COLOR_TYPE_ARGB; + LinkedList_AppendNode( &x11.surfaces, &surface->node ); + X11Surface_SendTask( surface, &task ); + return surface; +} + +static LCUI_BOOL X11Surface_IsReady( LCUI_Surface surface ) +{ + return surface->is_ready; +} + +static void X11Surface_Move( LCUI_Surface surface, int x, int y ) +{ + LCUI_SurfaceTaskRec task; + task.type = TASK_MOVE; + task.x = x; + task.y = y; + X11Surface_SendTask( surface, &task ); +} + +static void X11Surface_Resize( LCUI_Surface surface, int width, int height ) +{ + LCUI_SurfaceTaskRec task; + task.type = TASK_RESIZE; + task.width = width; + task.height = height; + X11Surface_SendTask( surface, &task ); +} + +static void X11Surface_Show( LCUI_Surface surface ) +{ + LCUI_SurfaceTaskRec task; + task.type = TASK_SHOW; + task.show = TRUE; + X11Surface_SendTask( surface, &task ); +} + +static void X11Surface_Hide( LCUI_Surface surface ) +{ + LCUI_SurfaceTaskRec task; + task.type = TASK_SHOW; + task.show = FALSE; + X11Surface_SendTask( surface, &task ); +} + +static void X11Surface_SetCaptionW( LCUI_Surface surface, const wchar_t *wstr ) +{ + int len; + LCUI_SurfaceTaskRec task; + + if( wstr ) { + char *caption; + len = LCUI_EncodeString( NULL, wstr, 0, ENCODING_UTF8 ) + 1; + caption = malloc( sizeof(char) * len ); + if( !caption ) { + return; + } + LCUI_EncodeString( caption, wstr, len, ENCODING_UTF8 ); + task.caption = caption; + task.caption_len = len - 1; + } else { + task.caption = NULL; + task.caption_len = 0; + } + task.type = TASK_SET_CAPTION; + X11Surface_SendTask( surface, &task ); +} + +void X11Surface_SetOpacity( LCUI_Surface surface, float opacity ) +{ + +} + +/** 设置 Surface 的渲染模式 */ +static void X11Surface_SetRenderMode( LCUI_Surface surface, int mode ) +{ + surface->mode = mode; +} + +static LCUI_PaintContext X11Surface_BeginPaint( LCUI_Surface surface, + LCUI_Rect *rect ) +{ + LCUI_PaintContext paint; + paint = malloc(sizeof(LCUI_PaintContextRec)); + paint->rect = *rect; + paint->with_alpha = FALSE; + Graph_Init( &paint->canvas ); + LCUIRect_ValidateArea( &paint->rect, surface->width, surface->height ); + Graph_Quote( &paint->canvas, &surface->fb, &paint->rect ); + Graph_FillRect( &paint->canvas, RGB( 255, 255, 255 ), NULL, TRUE ); + return paint; +} + +static void X11Surface_EndPaint( LCUI_Surface surface, + LCUI_PaintContext paint ) +{ + LCUI_Rect *r; + r = NEW( LCUI_Rect, 1 ); + *r = paint->rect; + LinkedList_Append( &surface->rects, r ); + free( paint ); +} + +/** 将帧缓存中的数据呈现至Surface的窗口内 */ +static void X11Surface_Present( LCUI_Surface surface ) +{ + + LCUI_SurfaceTaskRec task; + task.type = TASK_PRESENT; + X11Surface_SendTask( surface, &task ); +} + +/** 更新 surface,应用缓存的变更 */ +static void X11Surface_Update( LCUI_Surface surface ) +{ + return; +} + +static int WinDisplay_BindEvent( int event_id, LCUI_EventFunc func, + void *data, void (*destroy_data)(void*) ) +{ + return EventTrigger_Bind( x11.trigger, event_id, func, + data, destroy_data ); +} + +static void* X11Surface_GetHandle( LCUI_Surface s ) +{ + return NULL; +} + +static int X11Display_GetWidth( void ) +{ + Screen *s = DefaultScreenOfDisplay( x11.app->display ); + return XWidthOfScreen( s ); +} + +static int X11Display_GetHeight( void ) +{ + Screen *s = DefaultScreenOfDisplay( x11.app->display ); + return XHeightOfScreen( s ); +} + +static void OnExpose( LCUI_Event e, void *arg ) +{ + LCUI_Rect rect; + XEvent *ev = arg; + LCUI_Surface surface; + LCUI_DisplayEventRec dpy_ev; + rect.x = ev->xexpose.x; + rect.y = ev->xexpose.y; + rect.width = ev->xexpose.width; + rect.height = ev->xexpose.height; + surface = GetSurfaceByWindow( ev->xexpose.window ); + if( !surface ) { + return; + } + dpy_ev.type = DET_PAINT; + dpy_ev.surface = surface; + dpy_ev.paint.rect = rect; + EventTrigger_Trigger( x11.trigger, DET_PAINT, &dpy_ev ); +} + +int LCUI_InitLinuxX11Display( LCUI_DisplayDriver driver ) +{ + strcpy( driver->name, "x11" ); + x11.app = LCUI_GetAppData(); + driver->getWidth = X11Display_GetWidth; + driver->getHeight = X11Display_GetHeight; + driver->new = X11Surface_New; + driver->delete = X11Surface_Delete; + driver->isReady = X11Surface_IsReady; + driver->show = X11Surface_Show; + driver->hide = X11Surface_Hide; + driver->move = X11Surface_Move; + driver->resize = X11Surface_Resize; + driver->update = X11Surface_Update; + driver->present = X11Surface_Present; + driver->setCaptionW = X11Surface_SetCaptionW; + driver->setRenderMode = X11Surface_SetRenderMode; + driver->setOpacity = X11Surface_SetOpacity; + driver->getHandle = X11Surface_GetHandle; + driver->beginPaint = X11Surface_BeginPaint; + driver->endPaint = X11Surface_EndPaint; + driver->bindEvent = WinDisplay_BindEvent; + LinkedList_Init( &x11.surfaces ); + LCUI_BindSysEvent( Expose, OnExpose, NULL, NULL ); + x11.trigger = EventTrigger(); + x11.is_inited = TRUE; + return 0; +} + +int LCUI_ExitLinuxX11Display( void ) +{ + // ... + return 0; +} + +#endif diff --git a/src/platform/linux/linux_x11events.c b/src/platform/linux/linux_x11events.c index 59542060..0822715a 100644 --- a/src/platform/linux/linux_x11events.c +++ b/src/platform/linux/linux_x11events.c @@ -1,24 +1,32 @@ + +#include +#include #include -#if defined(LCUI_BUILD_IN_LINUX) -#include -#include -#include +#ifdef LCUI_BUILD_IN_LINUX #include #include #include #include LCUI_EVENTS_H -static struct X11Driver { - int screen; - Display *display; - Window rootwin; - Colormap cmap; - LCUI_EventTrigger trigger; -} x11; +static LCUI_X11AppDriverRec x11; + +void LCUI_SetLinuxX11MainWindow( Window win ) +{ + x11.win_main = win; + XSelectInput( x11.display, win, ExposureMask | KeyPressMask ); + LCUI_SetTaskAgent( FALSE ); +} static LCUI_BOOL X11_PostTask( LCUI_AppTask task ) { - return FALSE; + XEvent ev; + memset( &ev, 0, sizeof (ev) ); + ev.xclient.type = ClientMessage; + ev.xclient.window = x11.win_main; + ev.xclient.format = 32; + ev.xclient.message_type = x11.wm_lcui; + XSendEvent(x11.display, x11.win_main, FALSE, NoEventMask, &ev); + return TRUE; } static LCUI_BOOL X11_WaitEvent( void ) @@ -39,9 +47,16 @@ static LCUI_BOOL X11_WaitEvent( void ) return FALSE; } -static void X11_PumpEvents( void ) +static void X11_DispatchEvent( void ) { - + XEvent xevent; + XNextEvent( x11.display, &xevent ); + DEBUG_MSG("%d\n", xevent.type); + switch( xevent.type ) { + case ClientMessage: + default: break; + } + EventTrigger_Trigger( x11.trigger, xevent.type, &xevent ); } static int X11_BindSysEvent( int event_id, LCUI_EventFunc func, @@ -62,7 +77,7 @@ static int X11_UnbindSysEvent2( int handler_id ) static void *X11_GetData( void ) { - return NULL; + return &x11; } void LCUI_PreInitLinuxX11App( void *data ) @@ -77,10 +92,12 @@ int LCUI_InitLinuxX11App( LCUI_AppDriver app ) return -1; } x11.screen = DefaultScreen( x11.display ); - x11.rootwin = RootWindow( x11.display, x11.screen ); + x11.win_root = RootWindow( x11.display, x11.screen ); x11.cmap = DefaultColormap( x11.display, x11.screen ); + x11.wm_lcui = XInternAtom( x11.display, "WM_LCUI", FALSE ); + XSetWMProtocols( x11.display, x11.win_root, &x11.wm_lcui, 1 ); app->WaitEvent = X11_WaitEvent; - app->PumbEvents = X11_PumpEvents; + app->DispatchEvent = X11_DispatchEvent; app->PostTask = X11_PostTask; app->BindSysEvent = X11_BindSysEvent; app->UnbindSysEvent = X11_UnbindSysEvent; @@ -89,4 +106,5 @@ int LCUI_InitLinuxX11App( LCUI_AppDriver app ) x11.trigger = EventTrigger(); return 0; } + #endif