From b348af16e28756d3d4cf1d7fa1d4cfa035b44b22 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 24 Apr 2008 17:45:30 +0200 Subject: [PATCH] [dbus] first support Signed-off-by: Julien Danjou --- Makefile.am | 7 +-- awesome.c | 7 +++ configure.ac | 2 + dbus.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ dbus.h | 32 +++++++++++ 5 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 dbus.c create mode 100644 dbus.h diff --git a/Makefile.am b/Makefile.am index b41692fd..3962b615 100644 --- a/Makefile.am +++ b/Makefile.am @@ -115,7 +115,7 @@ AM_CPPFLAGS = $(pangocairo_CFLAGS) $(confuse_CFLAGS) $(AWESOME_CFLAGS) \ $(xcb_CFLAGS) $(xcb_event_CFLAGS) \ $(xcb_randr_CFLAGS) $(xcb_xinerama_CFLAGS) $(xcb_shape_CFLAGS) \ $(xcb_aux_CFLAGS) $(xcb_atom_CFLAGS) $(xcb_keysyms_CFLAGS) \ - $(xcb_icccm_CFLAGS) + $(xcb_icccm_CFLAGS) $(dbus_CFLAGS) bin_PROGRAMS += awesome awesome_SOURCES = \ @@ -145,12 +145,13 @@ awesome_SOURCES = \ rules.c rules.h \ mouse.c mouse.h \ widget.c widget.h \ - ewmh.c ewmh.h + ewmh.c ewmh.h \ + dbus.c dbus.h awesome_SOURCES += $(LAYOUTS) awesome_SOURCES += $(WIDGETS) awesome_LDADD = $(pangocairo_LIBS) $(confuse_LIBS) $(xcb_LIBS) $(xcb_event_LIBS) \ $(xcb_randr_LIBS) $(xcb_xinerama_LIBS) $(xcb_shape_LIBS) $(xcb_aux_LIBS) \ - $(xcb_atom_LIBS) $(xcb_keysyms_LIBS) $(xcb_icccm_LIBS) \ + $(xcb_atom_LIBS) $(xcb_keysyms_LIBS) $(xcb_icccm_LIBS) $(dbus_LIBS)\ $(imlib2_LIBS) $(GTK_LIBS) bin_PROGRAMS += awesome-client diff --git a/awesome.c b/awesome.c index 9020bf83..3dfbd773 100644 --- a/awesome.c +++ b/awesome.c @@ -53,6 +53,7 @@ #include "focus.h" #include "ewmh.h" #include "tag.h" +#include "dbus.h" #include "common/socket.h" #include "common/util.h" #include "common/version.h" @@ -491,6 +492,8 @@ main(int argc, char **argv) else warn("error binding UNIX domain socket: %s\n", strerror(errno)); } + + a_dbus_init(); /* register function for signals */ signal(SIGINT, &exit_on_signal); @@ -531,6 +534,9 @@ main(int argc, char **argv) statusbar_refresh(); layout_refresh(); } + + /* a_dbus_process_requests(); */ + /* two level XPending: * we need to first check we have XEvent to handle * and if so, we handle them all in a round. @@ -559,6 +565,7 @@ main(int argc, char **argv) xcb_aux_sync(globalconf.connection); } + a_dbus_cleanup(); if(csfd > 0 && close(csfd)) warn("error closing UNIX domain socket: %s\n", strerror(errno)); diff --git a/configure.ac b/configure.ac index 11b4b04e..27eb3de3 100644 --- a/configure.ac +++ b/configure.ac @@ -113,6 +113,8 @@ PKG_CHECK_MODULES([pangocairo], [pangocairo],, [AC_MSG_ERROR([awesome requires pangocairo.])]) PKG_CHECK_MODULES([confuse], [libconfuse >= 2.6],, [AC_MSG_ERROR([awesome requires libconfuse >= 2.6.])]) +PKG_CHECK_MODULES([dbus], [dbus-1],, + [AC_MSG_ERROR([awesome requires dbus-1.])]) AC_ARG_WITH([imlib2], AS_HELP_STRING([--with-imlib2], [Build with Imlib2 (default: disabled)])) if test "x$with_imlib2" == "xyes"; then diff --git a/dbus.c b/dbus.c new file mode 100644 index 00000000..41524993 --- /dev/null +++ b/dbus.c @@ -0,0 +1,149 @@ +/* + * dbus.c - awesome dbus support + * + * Copyright © 2008 Julien Danjou + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include + +#include "dbus.h" +#include "common/util.h" + +static DBusError err; +static DBusConnection *conn = NULL; + +static void +a_dbus_process_widget_tell(DBusMessage *req) +{ + int ret; + char *arg, *path; + DBusMessageIter iter; + + if(!dbus_message_iter_init(req, &iter)) + { + warn("message has no argument: %s\n", err.message); + dbus_error_free(&err); + return; + } + else if(DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&iter)) + { + warn("argument must be a string"); + dbus_error_free(&err); + return; + } + else + dbus_message_iter_get_basic(&iter, &arg); + + p_delete(&path); +} + +void +a_dbus_process_requests(void) +{ + DBusMessage *msg; + int nmsg = 0; + + if(!conn && !a_dbus_init()) + return; + + while(true) + { + dbus_connection_read_write(conn, 0); + + if(!(msg = dbus_connection_pop_message(conn))) + break; + + + if(dbus_message_is_method_call(msg, "org.awesome.statusbar.widget", "tell")) + a_dbus_process_widget_tell(msg); + else if(dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected")) + { + a_dbus_cleanup(); + dbus_message_unref(msg); + return; + } + + dbus_message_unref(msg); + + nmsg++; + } + + if(nmsg) + dbus_connection_flush(conn); +} + +bool +a_dbus_init(void) +{ + bool ret; + + dbus_error_init(&err); + + conn = dbus_bus_get(DBUS_BUS_SESSION, &err); + if(dbus_error_is_set(&err)) + { + warn("DBus system bus connection failed: %s\n", err.message); + + conn = NULL; + + dbus_error_free(&err); + + return false; + } + + dbus_connection_set_exit_on_disconnect(conn, FALSE); + + ret = dbus_bus_request_name(conn, "org.awesome", 0, &err); + + if(dbus_error_is_set(&err)) + { + warn("failed to request DBus name: %s\n", err.message); + + a_dbus_cleanup(); + + return false; + } + + if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) + { + warn("not primary DBus name owner\n"); + + a_dbus_cleanup(); + + return false; + } + + return true; +} + +void +a_dbus_cleanup(void) +{ + if(!conn) + return; + + dbus_error_free(&err); + + /* This is a shared connection owned by libdbus + * Do not close it, only unref + */ + dbus_connection_unref(conn); + conn = NULL; +} + +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/dbus.h b/dbus.h new file mode 100644 index 00000000..ad317812 --- /dev/null +++ b/dbus.h @@ -0,0 +1,32 @@ +/* + * dbus.h - dbus management header + * + * Copyright © 2008 Julien Danjou + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#ifndef AWESOME_DBUS_H +#define AWESOME_DBUS_H + +#include + +void a_dbus_process_requests(void); +bool a_dbus_init(void); +void a_dbus_cleanup(void); + +#endif +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80