From a36c6528b10cb5b5d48fbf30a941443f738c9dd1 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 1 Apr 2018 23:23:34 +0200 Subject: Terminal inside Lua window manager --- src/lib/libkogata/mainloop.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/lib/libkogata/mainloop.c') diff --git a/src/lib/libkogata/mainloop.c b/src/lib/libkogata/mainloop.c index c1758a4..75fb252 100644 --- a/src/lib/libkogata/mainloop.c +++ b/src/lib/libkogata/mainloop.c @@ -7,6 +7,9 @@ mainloop_fd_t *mainloop_fds = 0; bool mainloop_fds_change = false; bool mainloop_must_exit = false; +idle_callback_t mainloop_idle_cb = 0; +void* mainloop_idle_cb_data = 0; + void mainloop_add_fd(mainloop_fd_t* fd) { mainloop_fds_change = true; @@ -53,12 +56,19 @@ bool mainloop_nonblocking_write(mainloop_fd_t *fd, void* buf, size_t size, bool return false; } +void mainloop_when_idle(idle_callback_t cb, void* data) { + mainloop_idle_cb = cb; + mainloop_idle_cb_data = data; +} + void mainloop_run() { sel_fd_t *sel_arg = 0; int nfds = 0; mainloop_fds_change = true; mainloop_must_exit = false; + + bool after_idle_step = false; while(!mainloop_must_exit) { if (mainloop_fds_change) { nfds = 0; @@ -91,11 +101,12 @@ void mainloop_run() { // ---- Do the select /*dbg_printf("(mainloop) begin select\n");*/ - bool ok = sc_select(sel_arg, nfds, -1); + bool ok = sc_select(sel_arg, nfds, (after_idle_step || mainloop_idle_cb == 0 ? -1 : 0)); if (!ok) { - dbg_printf("(mainloop) Failed to select.\n"); - free(sel_arg); - return; + // nothing happenned + if (mainloop_idle_cb != 0) mainloop_idle_cb(mainloop_idle_cb_data); + after_idle_step = true; + continue; } /*dbg_printf("(mainloop) end select\n");*/ @@ -131,6 +142,7 @@ void mainloop_run() { } i++; } + after_idle_step = false; } } } -- cgit v1.2.3