From 9ea15fd22437c0e1ab4eb2959d88358a3f63cfd0 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Tue, 17 Jun 2008 00:30:53 +0200 Subject: [PATCH] And migrate the rest of the loop to the libev. Signed-off-by: Pierre Habouzit --- awesome.c | 147 ++++++++++++++++++++++++++-------------------------- lua.c | 13 +++-- lua.h | 2 + statusbar.c | 1 - structs.h | 5 +- 5 files changed, 88 insertions(+), 80 deletions(-) diff --git a/awesome.c b/awesome.c index c571db78..44c55c15 100644 --- a/awesome.c +++ b/awesome.c @@ -22,20 +22,12 @@ #define _GNU_SOURCE #include -#include #include #include #include -#include -#include -#include -#include -#include #include -#include #include - #include #include #include @@ -61,8 +53,6 @@ #include "common/xutil.h" #include "config.h" -bool running = true; - awesome_t globalconf; typedef struct @@ -168,6 +158,45 @@ scan(void) p_delete(&root_wins); } +static void +a_xcb_check_cb(EV_P_ ev_check *w, int revents) +{ + xcb_generic_event_t *ev; + + while((ev = xcb_poll_for_event(globalconf.connection))) + { + do + { + xcb_handle_event(globalconf.evenths, ev); + /* need to resync */ + xcb_aux_sync(globalconf.connection); + p_delete(&ev); + } while((ev = xcb_poll_for_event(globalconf.connection))); + + layout_refresh(); + statusbar_refresh(); + + /* need to resync */ + xcb_aux_sync(globalconf.connection); + } + layout_refresh(); + statusbar_refresh(); + xcb_aux_sync(globalconf.connection); +} + +static void +a_xcb_io_cb(EV_P_ ev_io *w, int revents) +{ + /* Two level polling: + * We need to first check we have an event to handle + * and if so, we handle them all in a round. + * Then when we have refresh()'ed stuff so maybe new XEvent + * are available and select() won't tell us, so let's check + * with xcb_poll_for_event() again. + */ + /* empty */ +} + /** Startup Error handler to check if another window manager * is already running. * \param data Additional optional parameters data @@ -186,9 +215,9 @@ xerrorstart(void * data __attribute__ ((unused)), * \param sig the signal received, unused */ static void -exit_on_signal(int sig __attribute__ ((unused))) +exit_on_signal(EV_P_ ev_signal *w, int revents) { - running = false; + ev_unloop(EV_A_ 1); } /** \brief awesome xerror function @@ -262,10 +291,7 @@ main(int argc, char **argv) int xfd, i, screen_nbr, opt; ssize_t cmdlen = 1; const xcb_query_extension_reply_t *shape_query, *randr_query; - fd_set rd; - xcb_generic_event_t *ev; client_t *c; - struct timeval select_timeout, hook_lastrun = { 0, 0 }, now, hook_nextrun; static struct option long_options[] = { {"help", 0, NULL, 'h'}, @@ -274,6 +300,13 @@ main(int argc, char **argv) {NULL, 0, NULL, 0} }; + /* event loop watchers */ + ev_io xio = { .fd = -1 }; + ev_check xcheck; + ev_signal sigint; + ev_signal sigterm; + ev_signal sighup; + /* clear the globalconf structure */ p_clear(&globalconf, 1); globalconf.keygrabber = LUA_REFNIL; @@ -312,6 +345,19 @@ main(int argc, char **argv) /* Text won't be printed correctly otherwise */ setlocale(LC_CTYPE, ""); + globalconf.loop = ev_default_loop(0); + ev_timer_init(&globalconf.timer, &luaA_on_timer, 0., 0.); + + /* register function for signals */ + ev_signal_init(&sigint, exit_on_signal, SIGINT); + ev_signal_init(&sigterm, &exit_on_signal, SIGTERM); + ev_signal_init(&sighup, &exit_on_signal, SIGHUP); + ev_signal_start(globalconf.loop, &sigint); + ev_signal_start(globalconf.loop, &sigterm); + ev_signal_start(globalconf.loop, &sighup); + ev_unref(globalconf.loop); + ev_unref(globalconf.loop); + ev_unref(globalconf.loop); /* X stuff */ globalconf.connection = xcb_connect(NULL, &globalconf.default_screen); @@ -320,6 +366,12 @@ main(int argc, char **argv) /* Get the file descriptor corresponding to the X connection */ xfd = xcb_get_file_descriptor(globalconf.connection); + ev_io_init(&xio, &a_xcb_io_cb, xfd, EV_READ); + ev_io_start(globalconf.loop, &xio); + ev_unref(globalconf.loop); + ev_check_init(&xcheck, &a_xcb_check_cb); + ev_check_start(globalconf.loop, &xcheck); + ev_unref(globalconf.loop); /* Allocate a handler which will holds all errors and events */ globalconf.evenths = xcb_alloc_event_handlers(globalconf.connection); @@ -457,71 +509,18 @@ main(int argc, char **argv) luaA_cs_init(); a_dbus_init(); - /* register function for signals */ - signal(SIGINT, &exit_on_signal); - signal(SIGTERM, &exit_on_signal); - signal(SIGHUP, &exit_on_signal); - /* refresh everything before waiting events */ layout_refresh(); statusbar_refresh(); /* main event loop */ - while(running) - { - FD_ZERO(&rd); - FD_SET(xfd, &rd); - if(timerisset(&globalconf.timer)) - { - gettimeofday(&now, NULL); - timeradd(&hook_lastrun, &globalconf.timer, &hook_nextrun); - /* Need to do 2 tests, <= does not work with timercmp() */ - if(timercmp(&hook_nextrun, &now, <) || (timercmp(&hook_nextrun, &now, ==))) - { - gettimeofday(&hook_lastrun, NULL); - luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0); - select_timeout = globalconf.timer; - } - else - timersub(&hook_nextrun, &now, &select_timeout); - } - if(select(xfd + 1, &rd, - NULL, NULL, - timerisset(&globalconf.timer) ? &select_timeout : NULL) == -1) - { - if(errno == EINTR) - continue; - eprint("select failed"); - } - - /* Two level polling: - * We need to first check we have an event to handle - * and if so, we handle them all in a round. - * Then when we have refresh()'ed stuff so maybe new XEvent - * are available and select() won't tell us, so let's check - * with xcb_poll_for_event() again. - */ - while((ev = xcb_poll_for_event(globalconf.connection))) - { - do - { - xcb_handle_event(globalconf.evenths, ev); - /* need to resync */ - xcb_aux_sync(globalconf.connection); - p_delete(&ev); - } while((ev = xcb_poll_for_event(globalconf.connection))); - - layout_refresh(); - statusbar_refresh(); - - /* need to resync */ - xcb_aux_sync(globalconf.connection); - } - layout_refresh(); - statusbar_refresh(); - xcb_aux_sync(globalconf.connection); - } + ev_loop(globalconf.loop, 0); + /* cleanup event loop */ + ev_ref(globalconf.loop); + ev_check_stop(globalconf.loop, &xcheck); + ev_ref(globalconf.loop); + ev_io_stop(globalconf.loop, &xio); a_dbus_cleanup(); luaA_cs_cleanup(); diff --git a/lua.c b/lua.c index 1b4eb168..ff351d48 100644 --- a/lua.c +++ b/lua.c @@ -46,7 +46,6 @@ extern awesome_t globalconf; -extern bool running; extern const name_func_link_t FloatingPlacementList[]; extern const struct luaL_reg awesome_keygrabber_lib[]; @@ -108,7 +107,7 @@ luaA_floating_placement_set(lua_State *L) static int luaA_quit(lua_State *L __attribute__ ((unused))) { - running = false; + ev_unloop(globalconf.loop, 1); return 0; } @@ -378,8 +377,7 @@ luaA_hooks_urgent(lua_State *L) static int luaA_hooks_timer(lua_State *L) { - globalconf.timer.tv_usec = 0; - globalconf.timer.tv_sec = luaL_checknumber(L, 1); + globalconf.timer.repeat = luaL_checknumber(L, 1); if(lua_gettop(L) == 2 && !lua_isnil(L, 2)) { @@ -389,6 +387,7 @@ luaA_hooks_timer(lua_State *L) globalconf.hooks.timer = luaL_ref(L, LUA_REGISTRYINDEX); } + ev_timer_again(globalconf.loop, &globalconf.timer); return 0; } @@ -649,3 +648,9 @@ luaA_cs_cleanup(void) p_delete(&addr); csio.fd = -1; } + +void +luaA_on_timer(EV_P_ ev_timer *w, int revents) +{ + luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0); +} diff --git a/lua.h b/lua.h index c420100a..aeb534c7 100644 --- a/lua.h +++ b/lua.h @@ -22,6 +22,7 @@ #ifndef AWESOME_LUA_H #define AWESOME_LUA_H +#include #include #include @@ -173,6 +174,7 @@ bool luaA_parserc(const char *); void luaA_pushpointer(void *, awesome_type_t); void luaA_cs_init(void); void luaA_cs_cleanup(void); +void luaA_on_timer(EV_P_ ev_timer *w, int revents); #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/statusbar.c b/statusbar.c index 6047fc4c..02d36e84 100644 --- a/statusbar.c +++ b/statusbar.c @@ -29,7 +29,6 @@ #include "window.h" extern awesome_t globalconf; -extern bool running; /** Draw a statusbar. * \param statusbar The statusbar to draw. diff --git a/structs.h b/structs.h index 12128547..0857fa2a 100644 --- a/structs.h +++ b/structs.h @@ -23,6 +23,7 @@ #define AWESOME_STRUCTS_H #include +#include #include "lua.h" #include "layout.h" @@ -461,8 +462,10 @@ struct awesome_t /** Command to run on time */ luaA_function timer; } hooks; + /** The event loop */ + struct ev_loop *loop; /** The timeout after which we need to stop select() */ - struct timeval timer; + struct ev_timer timer; /** The key grabber function */ luaA_function keygrabber; };