From 38f8d2fdce472da12b2e4d2c3f1acf449c4ec649 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Mon, 16 Jun 2008 23:32:20 +0200 Subject: [PATCH] Plug dbus in the event loop. Signed-off-by: Pierre Habouzit --- awesome.c | 13 ++++--------- dbus.c | 31 ++++++++++++++++++------------- dbus.h | 3 +-- 3 files changed, 23 insertions(+), 24 deletions(-) diff --git a/awesome.c b/awesome.c index b8699db6..ce987c5f 100644 --- a/awesome.c +++ b/awesome.c @@ -36,6 +36,7 @@ #include #include +#include #include #include @@ -261,7 +262,7 @@ main(int argc, char **argv) { char buf[1024]; const char *confpath = NULL; - int r, xfd, csfd, dbusfd, i, screen_nbr, opt; + int r, xfd, csfd, i, screen_nbr, opt; ssize_t cmdlen = 1; const xcb_query_extension_reply_t *shape_query, *randr_query; fd_set rd; @@ -474,8 +475,7 @@ main(int argc, char **argv) warn("error binding UNIX domain socket: %s", strerror(errno)); } - if(!a_dbus_init(&dbusfd)) - dbusfd = -1; + a_dbus_init(); /* register function for signals */ signal(SIGINT, &exit_on_signal); @@ -492,8 +492,6 @@ main(int argc, char **argv) FD_ZERO(&rd); if(csfd >= 0) FD_SET(csfd, &rd); - if(dbusfd >= 0) - FD_SET(dbusfd, &rd); FD_SET(xfd, &rd); if(timerisset(&globalconf.timer)) { @@ -509,7 +507,7 @@ main(int argc, char **argv) else timersub(&hook_nextrun, &now, &select_timeout); } - if(select(MAX(MAX(xfd, csfd), dbusfd) + 1, &rd, + if(select(MAX(xfd, csfd) + 1, &rd, NULL, NULL, timerisset(&globalconf.timer) ? &select_timeout : NULL) == -1) { @@ -533,9 +531,6 @@ main(int argc, char **argv) luaA_docmd(buf); } - if(dbusfd >= 0 && FD_ISSET(dbusfd, &rd)) - a_dbus_process_requests(&dbusfd); - /* Two level polling: * We need to first check we have an event to handle * and if so, we handle them all in a round. diff --git a/dbus.c b/dbus.c index 96f42402..3fb38370 100644 --- a/dbus.c +++ b/dbus.c @@ -22,6 +22,7 @@ #ifdef WITH_DBUS +#include #include #include "dbus.h" @@ -32,6 +33,7 @@ extern awesome_t globalconf; static DBusError err; static DBusConnection *dbus_connection = NULL; +ev_io dbusio = { .fd = -1 }; /** Check a dbus object path format and its number of element. * \param path The path. @@ -98,13 +100,13 @@ a_dbus_process_widget_set(DBusMessage *req) p_delete(&path); } -void -a_dbus_process_requests(int *fd) +static void +a_dbus_process_requests(EV_P_ ev_io *w, int revents) { DBusMessage *msg; int nmsg = 0; - if(!dbus_connection && !a_dbus_init(fd)) + if(!dbus_connection && !a_dbus_init()) return; while(true) @@ -134,9 +136,10 @@ a_dbus_process_requests(int *fd) } bool -a_dbus_init(int *fd) +a_dbus_init(void) { bool ret; + int fd; dbus_error_init(&err); @@ -167,13 +170,16 @@ a_dbus_init(int *fd) return false; } - if(!dbus_connection_get_unix_fd(dbus_connection, fd)) + if(!dbus_connection_get_unix_fd(dbus_connection, &fd)) { warn("cannot get DBus connection file descriptor"); a_dbus_cleanup(); return false; } + ev_io_init(&dbusio, a_dbus_process_requests, fd, EV_READ); + ev_io_start(EV_DEFAULT_UC_ &dbusio); + ev_unref(EV_DEFAULT_UC); return true; } @@ -185,6 +191,12 @@ a_dbus_cleanup(void) dbus_error_free(&err); + if (dbusio.fd >= 0) { + ev_ref(EV_DEFAULT_UC); + ev_io_stop(EV_DEFAULT_UC_ &dbusio); + dbusio.fd = -1; + } + /* This is a shared connection owned by libdbus * Do not close it, only unref */ @@ -196,16 +208,9 @@ a_dbus_cleanup(void) #include "dbus.h" -void -a_dbus_process_requests(int *fd __attribute__((unused))) -{ - /* empty */ -} - bool -a_dbus_init(int *fd) +a_dbus_init(void) { - *fd = -1; return false; } diff --git a/dbus.h b/dbus.h index ce4a0b55..d826ddcb 100644 --- a/dbus.h +++ b/dbus.h @@ -24,8 +24,7 @@ #include -void a_dbus_process_requests(int *); -bool a_dbus_init(int *); +bool a_dbus_init(void); void a_dbus_cleanup(void); #endif