use event source manager to implement main loop

This commit is contained in:
xianjimli 2019-12-29 07:51:41 +08:00
parent f42a5992d9
commit fb163dfa48
6 changed files with 66 additions and 4 deletions

View File

@ -1,4 +1,7 @@
# 最新动态 # 最新动态
* 2019/12/29
* 使用event\_source\_manager实现主循环。
* 2019/12/27 * 2019/12/27
* 增加str\_from\_wstr\_with\_len * 增加str\_from\_wstr\_with\_len
* 增加tk\_utf8\_from\_utf16\_ex * 增加tk\_utf8\_from\_utf16\_ex

View File

@ -37,7 +37,6 @@ ret_t main_loop_quit(main_loop_t* l) {
if (l->quit != NULL) { if (l->quit != NULL) {
l->quit(l); l->quit(l);
l->app_quited = TRUE;
} }
return RET_OK; return RET_OK;
@ -100,6 +99,8 @@ ret_t main_loop_sleep_default(main_loop_t* l) {
} }
ret_t main_loop_sleep(main_loop_t* l) { ret_t main_loop_sleep(main_loop_t* l) {
return_value_if_fail(l != NULL, RET_BAD_PARAMS);
if (l->sleep != NULL) { if (l->sleep != NULL) {
return l->sleep(l); return l->sleep(l);
} }
@ -108,9 +109,35 @@ ret_t main_loop_sleep(main_loop_t* l) {
} }
ret_t main_loop_step(main_loop_t* l) { ret_t main_loop_step(main_loop_t* l) {
return_value_if_fail(l != NULL, RET_BAD_PARAMS);
if (l->step != NULL) { if (l->step != NULL) {
return l->step(l); return l->step(l);
} }
return RET_OK; return RET_OK;
} }
event_source_manager_t* main_loop_get_event_source_manager(main_loop_t* l) {
return_value_if_fail(l != NULL, NULL);
if (l->get_event_source_manager != NULL) {
return l->get_event_source_manager(l);
}
return NULL;
}
ret_t main_loop_add_event_source(main_loop_t* l, event_source_t* source) {
event_source_manager_t* m = main_loop_get_event_source_manager(l);
return_value_if_fail(m != NULL && source != NULL, RET_BAD_PARAMS);
return event_source_manager_add(m, source);
}
ret_t main_loop_remove_event_source(main_loop_t* l, event_source_t* source) {
event_source_manager_t* m = main_loop_get_event_source_manager(l);
return_value_if_fail(m != NULL && source != NULL, RET_BAD_PARAMS);
return event_source_manager_remove(m, source);
}

View File

@ -24,6 +24,7 @@
#include "base/widget.h" #include "base/widget.h"
#include "base/event_queue.h" #include "base/event_queue.h"
#include "tkc/event_source_manager.h"
BEGIN_C_DECLS BEGIN_C_DECLS
@ -33,6 +34,7 @@ typedef struct _main_loop_t main_loop_t;
typedef ret_t (*main_loop_run_t)(main_loop_t* l); typedef ret_t (*main_loop_run_t)(main_loop_t* l);
typedef ret_t (*main_loop_quit_t)(main_loop_t* l); typedef ret_t (*main_loop_quit_t)(main_loop_t* l);
typedef ret_t (*main_loop_queue_event_t)(main_loop_t* l, const event_queue_req_t* e); typedef ret_t (*main_loop_queue_event_t)(main_loop_t* l, const event_queue_req_t* e);
typedef event_source_manager_t* (*main_loop_get_event_source_manager_t)(main_loop_t* l);
typedef ret_t (*main_loop_wakeup_t)(main_loop_t* l); typedef ret_t (*main_loop_wakeup_t)(main_loop_t* l);
typedef ret_t (*main_loop_step_t)(main_loop_t* l); typedef ret_t (*main_loop_step_t)(main_loop_t* l);
typedef ret_t (*main_loop_sleep_t)(main_loop_t* l); typedef ret_t (*main_loop_sleep_t)(main_loop_t* l);
@ -45,6 +47,7 @@ struct _main_loop_t {
main_loop_sleep_t sleep; main_loop_sleep_t sleep;
main_loop_wakeup_t wakeup; main_loop_wakeup_t wakeup;
main_loop_queue_event_t queue_event; main_loop_queue_event_t queue_event;
main_loop_get_event_source_manager_t get_event_source_manager;
main_loop_destroy_t destroy; main_loop_destroy_t destroy;
bool_t running; bool_t running;
@ -67,6 +70,11 @@ ret_t main_loop_destroy(main_loop_t* l);
ret_t main_loop_step(main_loop_t* l); ret_t main_loop_step(main_loop_t* l);
ret_t main_loop_sleep(main_loop_t* l); ret_t main_loop_sleep(main_loop_t* l);
/*event_source*/
event_source_manager_t* main_loop_get_event_source_manager(main_loop_t* l);
ret_t main_loop_add_event_source(main_loop_t* l, event_source_t* source);
ret_t main_loop_remove_event_source(main_loop_t* l, event_source_t* source);
END_C_DECLS END_C_DECLS
#endif /*TK_MAIN_LOOP_H*/ #endif /*TK_MAIN_LOOP_H*/

View File

@ -202,7 +202,7 @@ static ret_t main_loop_sdl2_dispatch_window_event(main_loop_simple_t* loop, SDL_
e = event_init(EVT_NATIVE_WINDOW_DESTROY, NULL); e = event_init(EVT_NATIVE_WINDOW_DESTROY, NULL);
window_manager_dispatch_native_window_event(l->wm, &e, win); window_manager_dispatch_native_window_event(l->wm, &e, win);
main_loop_quit(&(loop->base)); tk_quit();
} }
} break; } break;
#if SDL_VERSION_ATLEAST(2, 0, 5) #if SDL_VERSION_ATLEAST(2, 0, 5)

View File

@ -22,6 +22,10 @@
#include "tkc/time_now.h" #include "tkc/time_now.h"
#include "main_loop/main_loop_simple.h" #include "main_loop/main_loop_simple.h"
#include "tkc/event_source_idle.h"
#include "tkc/event_source_timer.h"
#include "tkc/event_source_manager_default.h"
static ret_t main_loop_simple_queue_event(main_loop_t* l, const event_queue_req_t* r) { static ret_t main_loop_simple_queue_event(main_loop_t* l, const event_queue_req_t* r) {
ret_t ret = RET_FAIL; ret_t ret = RET_FAIL;
main_loop_simple_t* loop = (main_loop_simple_t*)l; main_loop_simple_t* loop = (main_loop_simple_t*)l;
@ -164,10 +168,9 @@ static ret_t main_loop_dispatch_input(main_loop_simple_t* loop) {
static ret_t main_loop_simple_step(main_loop_t* l) { static ret_t main_loop_simple_step(main_loop_t* l) {
main_loop_simple_t* loop = (main_loop_simple_t*)l; main_loop_simple_t* loop = (main_loop_simple_t*)l;
timer_dispatch();
main_loop_dispatch_input(loop); main_loop_dispatch_input(loop);
main_loop_dispatch_events(loop); main_loop_dispatch_events(loop);
idle_dispatch(); event_source_manager_dispatch(loop->event_source_manager);
window_manager_paint(loop->base.wm); window_manager_paint(loop->base.wm);
@ -186,7 +189,15 @@ static ret_t main_loop_simple_run(main_loop_t* l) {
return RET_OK; return RET_OK;
} }
static event_source_manager_t* main_loop_simple_get_event_source_manager(main_loop_t* l) {
main_loop_simple_t* loop = (main_loop_simple_t*)l;
return loop->event_source_manager;
}
main_loop_simple_t* main_loop_simple_init(int w, int h) { main_loop_simple_t* main_loop_simple_init(int w, int h) {
event_source_t* idle_source = NULL;
event_source_t* timer_source = NULL;
static main_loop_simple_t s_main_loop_simple; static main_loop_simple_t s_main_loop_simple;
main_loop_simple_t* loop = &s_main_loop_simple; main_loop_simple_t* loop = &s_main_loop_simple;
@ -206,15 +217,27 @@ main_loop_simple_t* main_loop_simple_init(int w, int h) {
loop->base.run = main_loop_simple_run; loop->base.run = main_loop_simple_run;
loop->base.step = main_loop_simple_step; loop->base.step = main_loop_simple_step;
loop->base.queue_event = main_loop_simple_queue_event; loop->base.queue_event = main_loop_simple_queue_event;
loop->base.get_event_source_manager = main_loop_simple_get_event_source_manager;
window_manager_post_init(loop->base.wm, w, h); window_manager_post_init(loop->base.wm, w, h);
main_loop_set((main_loop_t*)loop); main_loop_set((main_loop_t*)loop);
loop->event_source_manager = event_source_manager_default_create();
idle_source = event_source_idle_create(idle_manager());
timer_source = event_source_timer_create(timer_manager());
event_source_manager_add(loop->event_source_manager, idle_source);
event_source_manager_add(loop->event_source_manager, timer_source);
OBJECT_UNREF(idle_source);
OBJECT_UNREF(timer_source);
return loop; return loop;
} }
ret_t main_loop_simple_reset(main_loop_simple_t* loop) { ret_t main_loop_simple_reset(main_loop_simple_t* loop) {
return_value_if_fail(loop != NULL, RET_BAD_PARAMS); return_value_if_fail(loop != NULL, RET_BAD_PARAMS);
event_source_manager_destroy(loop->event_source_manager);
event_queue_destroy(loop->queue); event_queue_destroy(loop->queue);
tk_mutex_destroy(loop->mutex); tk_mutex_destroy(loop->mutex);

View File

@ -53,6 +53,7 @@ struct _main_loop_simple_t {
void* user2; void* user2;
void* user3; void* user3;
void* user4; void* user4;
event_source_manager_t* event_source_manager;
main_loop_dispatch_input_t dispatch_input; main_loop_dispatch_input_t dispatch_input;
}; };