dbus: make bus_flags_to_events() and friends generally useful
diff --git a/src/dbus-common.c b/src/dbus-common.c
index 5db077b..c2650fd 100644
--- a/src/dbus-common.c
+++ b/src/dbus-common.c
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <dbus/dbus.h>
 #include <string.h>
+#include <sys/epoll.h>
 
 #include "log.h"
 #include "dbus-common.h"
@@ -698,3 +699,38 @@
 
         return NULL;
 }
+
+uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
+        unsigned flags;
+        uint32_t events = 0;
+
+        assert(bus_watch);
+
+        /* no watch flags for disabled watches */
+        if (!dbus_watch_get_enabled(bus_watch))
+                return 0;
+
+        flags = dbus_watch_get_flags(bus_watch);
+
+        if (flags & DBUS_WATCH_READABLE)
+                events |= EPOLLIN;
+        if (flags & DBUS_WATCH_WRITABLE)
+                events |= EPOLLOUT;
+
+        return events | EPOLLHUP | EPOLLERR;
+}
+
+unsigned bus_events_to_flags(uint32_t events) {
+        unsigned flags = 0;
+
+        if (events & EPOLLIN)
+                flags |= DBUS_WATCH_READABLE;
+        if (events & EPOLLOUT)
+                flags |= DBUS_WATCH_WRITABLE;
+        if (events & EPOLLHUP)
+                flags |= DBUS_WATCH_HANGUP;
+        if (events & EPOLLERR)
+                flags |= DBUS_WATCH_ERROR;
+
+        return flags;
+}