forked from lc-soft/LCUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
556 lines (513 loc) · 13.6 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/* ***************************************************************************
* main.c -- The main functions for the LCUI normal work
*
* Copyright (C) 2012-2017 by Liu Chao <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
* ***************************************************************************/
/* ****************************************************************************
* main.c -- 使LCUI能够正常工作的相关主要函数
*
* 版权所有 (C) 2012-2017 归属于 刘超 <[email protected]>
*
* 这个文件是LCUI项目的一部分,并且只可以根据GPLv2许可协议来使用、更改和发布。
*
* (GPLv2 是 GNU通用公共许可证第二版 的英文缩写)
*
* 继续使用、修改或发布本文件,表明您已经阅读并完全理解和接受这个许可协议。
*
* LCUI 项目是基于使用目的而加以散布的,但不负任何担保责任,甚至没有适销性或特
* 定用途的隐含担保,详情请参照GPLv2许可协议。
*
* 您应已收到附随于本文件的GPLv2许可协议的副本,它通常在LICENSE.TXT文件中,如果
* 没有,请查看:<http://www.gnu.org/licenses/>.
* ***************************************************************************/
//#define DEBUG
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <LCUI_Build.h>
#include <LCUI/LCUI.h>
#include <LCUI/thread.h>
#include <LCUI/timer.h>
#include <LCUI/cursor.h>
#include <LCUI/font.h>
#include <LCUI/input.h>
#include <LCUI/display.h>
#include <LCUI/ime.h>
#include <LCUI/platform.h>
#include LCUI_EVENTS_H
#include LCUI_MOUSE_H
#include LCUI_KEYBOARD_H
#include LCUI_DISPLAY_H
#define STATE_ACTIVE 1
#define STATE_KILLED 0
/** 一秒内的最大更新帧数 */
#define MAX_FRAMES_PER_SEC 100
/** 主循环的状态 */
enum MainLoopState {
STATE_PAUSED,
STATE_RUNNING,
STATE_EXITED
};
typedef struct SysEventHandlerRec_ {
LCUI_SysEventFunc func;
void *data;
void (*destroy_data)(void*);
} SysEventHandlerRec, *SysEventHandler;
typedef struct SysEventPackRec_ {
LCUI_SysEvent event;
void *arg;
} SysEventPackRec, *SysEventPack;
/** LCUI 系统相关数据 */
static struct LCUI_System {
LCUI_BOOL is_inited; /**< 标志,指示LCUI是否初始化过 */
int state; /**< 状态 */
int mode; /**< LCUI的运行模式 */
unsigned long int main_tid; /**< 主线程ID */
struct {
LCUI_EventTrigger trigger; /**< 系统事件容器 */
LCUI_Mutex mutex; /**< 互斥锁 */
} event;
} System;
/** LCUI 应用程序数据 */
static struct LCUI_App {
LCUI_Mutex loop_mutex; /**< 互斥锁,确保一次只允许一个线程跑主循环 */
LCUI_Cond loop_changed; /**< 条件变量,用于指示当前运行的主循环是否改变 */
LCUI_MainLoop loop; /**< 当前运行的主循环 */
LinkedList loops; /**< 主循环列表 */
StepTimer timer; /**< 渲染循环计数器 */
LCUI_AppDriver driver; /**< 程序事件驱动支持 */
LCUI_BOOL driver_ready; /**< 事件驱动支持是否已经准备就绪 */
LinkedList tasks; /**< 任务队列 */
LCUI_Mutex tasks_mutex; /**< 互斥锁 */
LCUI_Cond tasks_cond; /**< 条件变量 */
} MainApp;
/*-------------------------- system event <START> ---------------------------*/
/** 初始化事件模块 */
static void LCUI_InitEvent( void )
{
LCUIMutex_Init( &System.event.mutex );
System.event.trigger = EventTrigger();
}
/** 停用事件模块并进行清理 */
static void LCUI_ExitEvent( void )
{
if( !System.is_inited ) {
return;
}
LCUIMutex_Destroy( &System.event.mutex );
EventTrigger_Destroy( System.event.trigger );
System.event.trigger = NULL;
}
static void OnEvent( LCUI_Event e, void *arg )
{
SysEventHandler handler = e->data;
SysEventPack pack = arg;
pack->event->type = e->type;
pack->event->data = handler->data;
handler->func( pack->event, pack->arg );
}
static void DestroySysEventHandler( void *arg )
{
SysEventHandler handler = arg;
if( handler->data && handler->destroy_data ) {
handler->destroy_data( handler->data );
}
handler->data = NULL;
free( arg );
}
int LCUI_BindEvent( int id, LCUI_SysEventFunc func, void *data,
void (*destroy_data)(void*) )
{
int ret;
SysEventHandler handler;
if( !System.is_inited ) {
return -1;
}
handler = NEW( SysEventHandlerRec, 1 );
handler->func = func;
handler->data = data;
handler->destroy_data = destroy_data;
LCUIMutex_Lock( &System.event.mutex );
ret = EventTrigger_Bind( System.event.trigger, id, OnEvent,
handler, DestroySysEventHandler );
LCUIMutex_Unlock( &System.event.mutex );
return ret;
}
int LCUI_UnbindEvent( int handler_id )
{
int ret;
if( !System.is_inited ) {
return -1;
}
LCUIMutex_Lock( &System.event.mutex );
ret = EventTrigger_Unbind2( System.event.trigger, handler_id );
LCUIMutex_Unlock( &System.event.mutex );
return ret;
}
int LCUI_TriggerEvent( LCUI_SysEvent e, void *arg )
{
int ret;
SysEventPackRec pack;
pack.arg = arg;
pack.event = e;
LCUIMutex_Lock( &System.event.mutex );
ret = EventTrigger_Trigger( System.event.trigger, e->type, &pack );
LCUIMutex_Unlock( &System.event.mutex );
return ret;
}
int LCUI_CreateTouchEvent( LCUI_SysEvent e,
LCUI_TouchPoint points, int n_points )
{
e->type = LCUI_TOUCH;
e->touch.n_points = n_points;
e->touch.points = NEW( LCUI_TouchPointRec, n_points );
if( !e->touch.points ) {
return -ENOMEM;
}
for( n_points -= 1; n_points >= 0; --n_points ) {
e->touch.points[n_points] = points[n_points];
}
return 0;
}
void LCUI_DestroyEvent( LCUI_SysEvent e )
{
switch( e->type ) {
case LCUI_TOUCH:
if( e->touch.points ) {
free( e->touch.points );
}
e->touch.points = NULL;
e->touch.n_points = 0;
break;
case LCUI_TEXTINPUT:
if( e->text.text ) {
free( e->text.text );
}
e->text.text = NULL;
e->text.length = 0;
break;
}
e->type = LCUI_NONE;
}
/*--------------------------- system event <END> ----------------------------*/
LCUI_BOOL LCUI_ProcessTask( void )
{
LCUI_AppTask task;
LinkedListNode *node;
LCUIMutex_Lock( &MainApp.tasks_mutex );
node = LinkedList_GetNode( &MainApp.tasks, 0 );
if( node ) {
task = node->data;
LinkedList_Unlink( &MainApp.tasks, node );
LCUIMutex_Unlock( &MainApp.tasks_mutex );
LCUI_RunTask( task );
LCUI_DeleteTask( task );
free( task );
free( node );
return TRUE;
}
LCUIMutex_Unlock( &MainApp.tasks_mutex );
return FALSE;
}
void LCUI_ProcessEvents( void )
{
int i;
for( i = 0; LCUI_ProcessTask() && i < 100; ++i );
if( MainApp.driver_ready ) {
MainApp.driver->ProcessEvents();
}
}
LCUI_BOOL LCUI_PostTask( LCUI_AppTask task )
{
LCUI_AppTask newtask;
newtask = NEW( LCUI_AppTaskRec, 1 );
*newtask = *task;
LCUIMutex_Lock( &MainApp.tasks_mutex );
LinkedList_Append( &MainApp.tasks, newtask );
LCUICond_Signal( &MainApp.tasks_cond );
LCUIMutex_Unlock( &MainApp.tasks_mutex );
return TRUE;
}
void LCUI_DeleteTask( LCUI_AppTask task )
{
if( task->destroy_arg[0] && task->arg[0] ) {
task->destroy_arg[0]( task->arg[0] );
}
if( task->destroy_arg[1] && task->arg[1] ) {
task->destroy_arg[1]( task->arg[1] );
}
}
int LCUI_RunTask( LCUI_AppTask task )
{
if( task && task->func ) {
task->func( task->arg[0], task->arg[1] );
return 0;
}
return -1;
}
/* 新建一个主循环 */
LCUI_MainLoop LCUIMainLoop_New( void )
{
LCUI_MainLoop loop;
loop = NEW( LCUI_MainLoopRec, 1 );
loop->state = STATE_PAUSED;
loop->tid = 0;
return 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.\n" );
return -1;
}
loop->state = STATE_RUNNING;
loop->tid = LCUIThread_SelfID();
if( MainApp.loop ) {
at_same_thread = MainApp.loop->tid == loop->tid;
}
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 );
LCUIMutex_Unlock( &MainApp.loop_mutex );
} else {
LinkedList_Insert( &MainApp.loops, 0, loop );
}
DEBUG_MSG( "loop: %p, enter\n", loop );
MainApp.loop = loop;
while( loop->state != STATE_EXITED ) {
LCUI_ProcessEvents();
LCUIDisplay_Update();
LCUIDisplay_Render();
LCUIDisplay_Present();
StepTimer_Remain( MainApp.timer );
/* 如果当前运行的主循环不是自己 */
while( MainApp.loop != loop ) {
loop->state = STATE_PAUSED;
LCUICond_Wait( &MainApp.loop_changed,
&MainApp.loop_mutex );
}
}
loop->state = STATE_EXITED;
LinkedList_Delete( &MainApp.loops, 0 );
/* 获取处于列表表头的主循环 */
loop = LinkedList_Get( &MainApp.loops, 0 );
if( loop ) {
/* 改变当前运行的主循环 */
MainApp.loop = loop;
}
DEBUG_MSG( "loop: %p, exit\n", loop );
LCUICond_Broadcast( &MainApp.loop_changed );
return 0;
}
/** 标记目标主循环需要退出 */
void LCUIMainLoop_Quit( LCUI_MainLoop loop )
{
loop->state = STATE_EXITED;
}
void LCUI_InitApp( LCUI_AppDriver app )
{
if( MainApp.driver_ready ) {
return;
}
MainApp.driver_ready = FALSE;
MainApp.timer = StepTimer_Create();
LCUICond_Init( &MainApp.loop_changed );
LCUIMutex_Init( &MainApp.loop_mutex );
LinkedList_Init( &MainApp.loops );
LCUIMutex_Init( &MainApp.tasks_mutex );
LCUICond_Init( &MainApp.tasks_cond );
LinkedList_Init( &MainApp.tasks );
StepTimer_SetFrameLimit( MainApp.timer, MAX_FRAMES_PER_SEC );
if( !app ) {
app = LCUI_CreateAppDriver();
if( !app ) {
return;
}
}
MainApp.driver = app;
MainApp.driver_ready = TRUE;
}
static void OnDeleteTask( void *arg )
{
LCUI_DeleteTask( arg );
free( arg );
}
static void LCUI_ExitApp( void )
{
LCUI_MainLoop loop;
LinkedListNode *node;
for( LinkedList_Each( node, &MainApp.loops ) ) {
loop = node->data;
LCUIMainLoop_Quit( loop );
LCUIThread_Join( loop->tid, NULL );
}
StepTimer_Destroy( MainApp.timer );
LCUIMutex_Destroy( &MainApp.loop_mutex );
LCUICond_Destroy( &MainApp.loop_changed );
LCUIMutex_Destroy( &MainApp.tasks_mutex );
LCUICond_Destroy( &MainApp.tasks_cond );
LinkedList_Clear( &MainApp.loops, free );
LinkedList_Clear( &MainApp.tasks, OnDeleteTask );
if( MainApp.driver_ready ) {
LCUI_DestroyAppDriver( MainApp.driver );
}
MainApp.driver_ready = FALSE;
}
int LCUI_BindSysEvent( int event_id, LCUI_EventFunc func,
void *data, void( *destroy_data )(void*) )
{
if( MainApp.driver_ready ) {
return MainApp.driver->BindSysEvent( event_id, func,
data, destroy_data );
}
return -1;
}
int LCUI_UnbindSysEvent( int event_id, LCUI_EventFunc func )
{
if( MainApp.driver_ready ) {
return MainApp.driver->UnbindSysEvent( event_id, func );
}
return -1;
}
void *LCUI_GetAppData( void )
{
if( MainApp.driver_ready ) {
return MainApp.driver->GetData();
}
return NULL;
}
/** 退出所有主循环 */
static void LCUIApp_QuitAllMainLoop( void )
{
LCUI_MainLoop loop;
LinkedListNode *node;
for( LinkedList_Each( node, &MainApp.loops ) ) {
loop = node->data;
if( loop ) {
loop->state = STATE_EXITED;
}
}
}
/** 打印LCUI的信息 */
static void LCUI_ShowCopyrightText(void)
{
Logger_Log(
"LCUI (LC's UI) version "LCUI_VERSION"\n"
#ifdef _MSC_VER
"Build tool: "
#if (_MSC_VER > 1900)
"MS VC++ (higher version)"
#elif (_MSC_VER == 1900)
"MS VC++ 14.0 (VisualStudio 2015)"
#elif (_MSC_VER == 1800)
"MS VC++ 12.0 (VisualStudio 2013)"
#elif (_MSC_VER == 1700)
"MS VC++ 11.0 (VisualStudio 2012)"
#elif (_MSC_VER == 1600)
"MS VC++ 10.0 (VisualStudio 2010)"
#else
"MS VC++ (older version)"
#endif
"\n"
#endif
"Build at "__DATE__" - "__TIME__"\n"
"Copyright (C) 2012-2017 Liu Chao <[email protected]>.\n"
"This is free software, licensed under GPLv2. \n"
"See source distribution for detailed copyright notices.\n"
"To learn more, visit http://www.lcui.org.\n\n"
);
}
LCUI_BOOL LCUI_IsActive( void )
{
if( System.state == STATE_ACTIVE ) {
return TRUE;
}
return FALSE;
}
LCUI_BOOL LCUI_IsOnMainLoop( void )
{
if( !MainApp.loop ) {
return FALSE;
}
return (MainApp.loop->tid == LCUIThread_SelfID());
}
void LCUI_InitBase( void )
{
if( System.is_inited ) {
return;
}
System.is_inited = TRUE;
System.state = STATE_ACTIVE;
System.main_tid = LCUIThread_SelfID();
LCUI_ShowCopyrightText();
/* 初始化各个模块 */
LCUI_InitEvent();
LCUI_InitFont();
LCUI_InitTimer();
LCUI_InitKeyboard();
LCUI_InitWidget();
LCUI_InitMetrics();
}
void LCUI_Init( void )
{
LCUI_InitBase();
LCUI_InitApp( NULL );
LCUI_InitMouseDriver();
LCUI_InitKeyboardDriver();
LCUI_InitDisplay( NULL );
LCUI_InitCursor();
LCUI_InitIME();
}
int LCUI_Destroy( void )
{
LCUI_SysEventRec e;
e.type = LCUI_QUIT;
LCUI_TriggerEvent( &e, NULL );
System.state = STATE_KILLED;
LCUI_ExitIME();
LCUI_ExitKeyboard();
LCUI_ExitCursor();
LCUI_ExitWidget();
LCUI_ExitFont();
LCUI_ExitTimer();
LCUI_ExitDisplay();
LCUI_ExitEvent();
LCUI_ExitMetrics();
LCUI_ExitApp();
return 0;
}
void LCUI_Quit( void )
{
System.state = STATE_KILLED;
LCUIApp_QuitAllMainLoop();
}
int LCUI_Main( void )
{
LCUI_MainLoop loop;
loop = LCUIMainLoop_New();
LCUIMainLoop_Run( loop );
return LCUI_Destroy();
}
int LCUI_GetSelfVersion( char *out )
{
return sprintf(out, "%s", LCUI_VERSION);
}