2008-04-24 17:45:30 +02:00
|
|
|
/*
|
|
|
|
* dbus.c - awesome dbus support
|
|
|
|
*
|
|
|
|
* Copyright © 2008 Julien Danjou <julien@danjou.info>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2008-05-30 21:20:23 +02:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-06-16 23:48:07 +02:00
|
|
|
#ifdef WITH_DBUS
|
2008-04-24 17:45:30 +02:00
|
|
|
|
2008-06-16 23:32:20 +02:00
|
|
|
#include <ev.h>
|
2008-04-24 17:45:30 +02:00
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
|
|
|
#include "dbus.h"
|
2008-04-30 18:44:23 +02:00
|
|
|
#include "widget.h"
|
2008-07-08 10:54:57 +02:00
|
|
|
#include "client.h"
|
2008-04-24 17:45:30 +02:00
|
|
|
|
2008-05-24 08:59:27 +02:00
|
|
|
extern awesome_t globalconf;
|
2008-04-30 18:44:23 +02:00
|
|
|
|
2008-04-24 17:45:30 +02:00
|
|
|
static DBusError err;
|
2008-04-30 16:47:38 +02:00
|
|
|
static DBusConnection *dbus_connection = NULL;
|
2008-06-16 23:32:20 +02:00
|
|
|
ev_io dbusio = { .fd = -1 };
|
2008-04-24 17:45:30 +02:00
|
|
|
|
2008-07-08 10:54:57 +02:00
|
|
|
/** Check a dbus object path format and its number of element.
|
|
|
|
* \param path The path.
|
|
|
|
* \param nelem The number of element it should have.
|
|
|
|
* \return true if the path is ok, false otherwise.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
a_dbus_path_check(char **path, int nelem)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; path[i]; i++);
|
|
|
|
if(i != nelem)
|
|
|
|
return false;
|
|
|
|
return (!a_strcmp(path[0], "org") && !a_strcmp(path[1], "awesome"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-07-08 14:39:50 +02:00
|
|
|
a_dbus_process_request_do(DBusMessage *msg)
|
2008-07-08 10:54:57 +02:00
|
|
|
{
|
2008-07-08 14:39:50 +02:00
|
|
|
int i;
|
2008-07-08 10:54:57 +02:00
|
|
|
DBusMessageIter iter;
|
2008-07-08 14:39:50 +02:00
|
|
|
char **path, *cmd;
|
2008-07-08 10:54:57 +02:00
|
|
|
|
|
|
|
if(!dbus_message_get_path_decomposed(msg, &path))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* path is:
|
2008-07-08 14:39:50 +02:00
|
|
|
* /org/awesome */
|
|
|
|
if(!a_dbus_path_check(path, 2))
|
2008-07-08 10:54:57 +02:00
|
|
|
goto bailout;
|
|
|
|
|
|
|
|
if(!dbus_message_iter_init(msg, &iter))
|
|
|
|
{
|
|
|
|
dbus_error_free(&err);
|
2008-07-08 14:39:50 +02:00
|
|
|
goto bailout;
|
2008-07-08 10:54:57 +02:00
|
|
|
}
|
2008-07-08 14:39:50 +02:00
|
|
|
else if(DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&iter))
|
2008-07-08 10:54:57 +02:00
|
|
|
{
|
2008-07-08 14:39:50 +02:00
|
|
|
dbus_error_free(&err);
|
|
|
|
goto bailout;
|
2008-07-08 10:54:57 +02:00
|
|
|
}
|
2008-07-08 14:39:50 +02:00
|
|
|
else
|
|
|
|
dbus_message_iter_get_basic(&iter, &cmd);
|
2008-07-08 10:54:57 +02:00
|
|
|
|
2008-07-08 14:39:50 +02:00
|
|
|
luaA_dostring(globalconf.L, cmd);
|
2008-07-08 10:54:57 +02:00
|
|
|
|
|
|
|
bailout:
|
|
|
|
for(i = 0; path[i]; i++)
|
|
|
|
p_delete(&path[i]);
|
|
|
|
p_delete(&path);
|
|
|
|
}
|
|
|
|
|
2008-06-16 23:32:20 +02:00
|
|
|
static void
|
|
|
|
a_dbus_process_requests(EV_P_ ev_io *w, int revents)
|
2008-04-24 17:45:30 +02:00
|
|
|
{
|
|
|
|
DBusMessage *msg;
|
|
|
|
int nmsg = 0;
|
|
|
|
|
2008-06-16 23:32:20 +02:00
|
|
|
if(!dbus_connection && !a_dbus_init())
|
2008-04-24 17:45:30 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
while(true)
|
|
|
|
{
|
2008-04-30 16:47:38 +02:00
|
|
|
dbus_connection_read_write(dbus_connection, 0);
|
2008-04-24 17:45:30 +02:00
|
|
|
|
2008-04-30 16:47:38 +02:00
|
|
|
if(!(msg = dbus_connection_pop_message(dbus_connection)))
|
2008-04-24 17:45:30 +02:00
|
|
|
break;
|
|
|
|
|
2008-06-25 17:47:51 +02:00
|
|
|
if(dbus_message_is_signal(msg, DBUS_INTERFACE_LOCAL, "Disconnected"))
|
2008-04-24 17:45:30 +02:00
|
|
|
{
|
|
|
|
a_dbus_cleanup();
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
return;
|
|
|
|
}
|
2008-07-08 14:39:50 +02:00
|
|
|
else if(dbus_message_is_method_call(msg, "org.awesome", "do"))
|
|
|
|
a_dbus_process_request_do(msg);
|
2008-04-24 17:45:30 +02:00
|
|
|
|
|
|
|
dbus_message_unref(msg);
|
|
|
|
|
|
|
|
nmsg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(nmsg)
|
2008-04-30 16:47:38 +02:00
|
|
|
dbus_connection_flush(dbus_connection);
|
2008-04-24 17:45:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2008-06-16 23:32:20 +02:00
|
|
|
a_dbus_init(void)
|
2008-04-24 17:45:30 +02:00
|
|
|
{
|
|
|
|
bool ret;
|
2008-06-16 23:32:20 +02:00
|
|
|
int fd;
|
2008-04-24 17:45:30 +02:00
|
|
|
|
|
|
|
dbus_error_init(&err);
|
|
|
|
|
2008-04-30 16:47:38 +02:00
|
|
|
dbus_connection = dbus_bus_get(DBUS_BUS_SESSION, &err);
|
2008-04-24 17:45:30 +02:00
|
|
|
if(dbus_error_is_set(&err))
|
|
|
|
{
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("DBus system bus connection failed: %s", err.message);
|
2008-04-30 16:47:38 +02:00
|
|
|
dbus_connection = NULL;
|
2008-04-24 17:45:30 +02:00
|
|
|
dbus_error_free(&err);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-30 16:47:38 +02:00
|
|
|
dbus_connection_set_exit_on_disconnect(dbus_connection, FALSE);
|
2008-04-24 17:45:30 +02:00
|
|
|
|
2008-04-30 16:47:38 +02:00
|
|
|
ret = dbus_bus_request_name(dbus_connection, "org.awesome", 0, &err);
|
2008-04-24 17:45:30 +02:00
|
|
|
|
|
|
|
if(dbus_error_is_set(&err))
|
|
|
|
{
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("failed to request DBus name: %s", err.message);
|
2008-04-24 17:45:30 +02:00
|
|
|
a_dbus_cleanup();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
|
|
|
|
{
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("not primary DBus name owner");
|
2008-04-24 17:45:30 +02:00
|
|
|
a_dbus_cleanup();
|
2008-04-30 17:59:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
2008-04-24 17:45:30 +02:00
|
|
|
|
2008-06-16 23:32:20 +02:00
|
|
|
if(!dbus_connection_get_unix_fd(dbus_connection, &fd))
|
2008-04-30 17:59:42 +02:00
|
|
|
{
|
2008-05-23 22:53:59 +02:00
|
|
|
warn("cannot get DBus connection file descriptor");
|
2008-04-30 17:59:42 +02:00
|
|
|
a_dbus_cleanup();
|
2008-04-24 17:45:30 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-06-16 23:32:20 +02:00
|
|
|
ev_io_init(&dbusio, a_dbus_process_requests, fd, EV_READ);
|
|
|
|
ev_io_start(EV_DEFAULT_UC_ &dbusio);
|
|
|
|
ev_unref(EV_DEFAULT_UC);
|
2008-04-24 17:45:30 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
a_dbus_cleanup(void)
|
|
|
|
{
|
2008-04-30 16:47:38 +02:00
|
|
|
if(!dbus_connection)
|
2008-04-24 17:45:30 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
dbus_error_free(&err);
|
|
|
|
|
2008-06-16 23:32:20 +02:00
|
|
|
if (dbusio.fd >= 0) {
|
|
|
|
ev_ref(EV_DEFAULT_UC);
|
|
|
|
ev_io_stop(EV_DEFAULT_UC_ &dbusio);
|
|
|
|
dbusio.fd = -1;
|
|
|
|
}
|
|
|
|
|
2008-04-24 17:45:30 +02:00
|
|
|
/* This is a shared connection owned by libdbus
|
|
|
|
* Do not close it, only unref
|
|
|
|
*/
|
2008-04-30 16:47:38 +02:00
|
|
|
dbus_connection_unref(dbus_connection);
|
|
|
|
dbus_connection = NULL;
|
2008-04-24 17:45:30 +02:00
|
|
|
}
|
|
|
|
|
2008-05-30 21:20:23 +02:00
|
|
|
#else /* HAVE_DBUS */
|
|
|
|
|
|
|
|
#include "dbus.h"
|
|
|
|
|
|
|
|
bool
|
2008-06-16 23:32:20 +02:00
|
|
|
a_dbus_init(void)
|
2008-05-30 21:20:23 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
a_dbus_cleanup(void)
|
|
|
|
{
|
|
|
|
/* empty */
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2008-04-24 17:45:30 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|