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-11-19 02:22:05 +01:00
|
|
|
|
2008-05-30 21:20:23 +02:00
|
|
|
#include "config.h"
|
2008-11-19 02:22:05 +01:00
|
|
|
#include "dbus.h"
|
|
|
|
#include "client.h"
|
2008-05-30 21:20:23 +02:00
|
|
|
|
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>
|
2008-08-27 10:00:10 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2008-04-24 17:45:30 +02:00
|
|
|
|
2008-04-30 18:44:23 +02:00
|
|
|
#include "widget.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-11-19 02:22:05 +01:00
|
|
|
static int
|
|
|
|
a_dbus_message_iter(DBusMessageIter *iter)
|
2008-07-08 10:54:57 +02:00
|
|
|
{
|
2008-11-19 02:22:05 +01:00
|
|
|
int nargs = 0;
|
2008-07-08 10:54:57 +02:00
|
|
|
|
2008-11-19 02:22:05 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
switch(dbus_message_iter_get_arg_type(iter))
|
|
|
|
{
|
|
|
|
case DBUS_TYPE_INVALID:
|
|
|
|
break;
|
|
|
|
case DBUS_TYPE_VARIANT:
|
|
|
|
{
|
|
|
|
DBusMessageIter subiter;
|
|
|
|
dbus_message_iter_recurse(iter, &subiter);
|
|
|
|
a_dbus_message_iter(&subiter);
|
|
|
|
}
|
|
|
|
nargs++;
|
|
|
|
break;
|
|
|
|
case DBUS_TYPE_DICT_ENTRY:
|
|
|
|
{
|
|
|
|
DBusMessageIter subiter;
|
|
|
|
|
|
|
|
/* initialize a sub iterator */
|
|
|
|
dbus_message_iter_recurse(iter, &subiter);
|
|
|
|
/* create a new table to store the dict */
|
|
|
|
a_dbus_message_iter(&subiter);
|
|
|
|
}
|
|
|
|
nargs++;
|
|
|
|
break;
|
|
|
|
case DBUS_TYPE_ARRAY:
|
|
|
|
{
|
|
|
|
int array_type = dbus_message_iter_get_element_type(iter);
|
|
|
|
|
|
|
|
/* create a new table to store all the value */
|
|
|
|
lua_newtable(globalconf.L);
|
|
|
|
|
|
|
|
if(dbus_type_is_fixed(array_type))
|
|
|
|
switch(array_type)
|
|
|
|
{
|
|
|
|
int datalen;
|
|
|
|
#define DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(type, dbustype) \
|
|
|
|
case dbustype: \
|
|
|
|
{ \
|
|
|
|
type *data; \
|
|
|
|
dbus_message_iter_get_fixed_array(iter, &data, &datalen); \
|
|
|
|
for(int i = 0; i < datalen; i++) \
|
|
|
|
{ \
|
|
|
|
lua_pushnumber(globalconf.L, data[i]); \
|
|
|
|
lua_rawseti(globalconf.L, -2, i + 1); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
break;
|
|
|
|
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
|
|
|
|
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
|
|
|
|
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
|
|
|
|
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
|
|
|
|
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
|
|
|
|
DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
|
|
|
|
#undef DBUS_MSG_HANDLE_ARRAY_TYPE_NUMBER
|
|
|
|
}
|
|
|
|
else if(array_type == DBUS_TYPE_DICT_ENTRY)
|
|
|
|
{
|
|
|
|
DBusMessageIter subiter;
|
|
|
|
|
|
|
|
/* initialize a sub iterator */
|
|
|
|
dbus_message_iter_recurse(iter, &subiter);
|
|
|
|
/* get the keys and the values
|
|
|
|
* n is the number of entry in * dict */
|
|
|
|
int n = a_dbus_message_iter(&subiter);
|
|
|
|
|
|
|
|
for(int i = 0; i < n; i ++)
|
|
|
|
lua_rawset(globalconf.L, - (n * 2) - 1 + i * 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBusMessageIter subiter;
|
|
|
|
|
|
|
|
/* prepare to dig into the array*/
|
|
|
|
dbus_message_iter_recurse(iter, &subiter);
|
|
|
|
|
|
|
|
/* now iterate over every element of the array */
|
|
|
|
int n = a_dbus_message_iter(&subiter);
|
|
|
|
|
|
|
|
for(int i = n; i > 0; i--)
|
|
|
|
{
|
|
|
|
lua_rawseti(globalconf.L, - i - 1, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nargs++;
|
|
|
|
break;
|
|
|
|
case DBUS_TYPE_BOOLEAN:
|
|
|
|
{
|
|
|
|
bool b;
|
|
|
|
dbus_message_iter_get_basic(iter, &b);
|
|
|
|
lua_pushboolean(globalconf.L, b);
|
|
|
|
}
|
|
|
|
nargs++;
|
|
|
|
break;
|
|
|
|
case DBUS_TYPE_BYTE:
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
dbus_message_iter_get_basic(iter, &c);
|
|
|
|
lua_pushlstring(globalconf.L, &c, 1);
|
|
|
|
}
|
|
|
|
nargs++;
|
|
|
|
break;
|
|
|
|
#define DBUS_MSG_HANDLE_TYPE_NUMBER(type, dbustype) \
|
|
|
|
case dbustype: \
|
|
|
|
{ \
|
|
|
|
type ui; \
|
|
|
|
dbus_message_iter_get_basic(iter, &ui); \
|
|
|
|
lua_pushnumber(globalconf.L, ui); \
|
|
|
|
} \
|
|
|
|
nargs++; \
|
|
|
|
break;
|
|
|
|
DBUS_MSG_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16)
|
|
|
|
DBUS_MSG_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16)
|
|
|
|
DBUS_MSG_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32)
|
|
|
|
DBUS_MSG_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32)
|
|
|
|
DBUS_MSG_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64)
|
|
|
|
DBUS_MSG_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64)
|
|
|
|
#undef DBUS_MSG_HANDLE_TYPE_NUMBER
|
|
|
|
case DBUS_TYPE_STRING:
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
dbus_message_iter_get_basic(iter, &s);
|
|
|
|
lua_pushstring(globalconf.L, s);
|
|
|
|
}
|
|
|
|
nargs++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while(dbus_message_iter_next(iter));
|
|
|
|
|
|
|
|
return nargs;
|
2008-07-08 10:54:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2008-11-19 02:22:05 +01:00
|
|
|
a_dbus_process_request(DBusMessage *msg)
|
2008-07-08 10:54:57 +02:00
|
|
|
{
|
2008-11-19 02:22:05 +01:00
|
|
|
if(globalconf.hooks.dbus == LUA_REFNIL)
|
2008-07-08 10:54:57 +02:00
|
|
|
return;
|
|
|
|
|
2008-11-19 02:22:05 +01:00
|
|
|
lua_newtable(globalconf.L);
|
2008-07-08 10:54:57 +02:00
|
|
|
|
2008-11-19 02:22:05 +01:00
|
|
|
switch(dbus_message_get_type(msg))
|
2008-07-08 10:54:57 +02:00
|
|
|
{
|
2008-11-19 02:22:05 +01:00
|
|
|
case DBUS_MESSAGE_TYPE_SIGNAL:
|
|
|
|
lua_pushliteral(globalconf.L, "signal");
|
|
|
|
break;
|
|
|
|
case DBUS_MESSAGE_TYPE_METHOD_CALL:
|
|
|
|
lua_pushliteral(globalconf.L, "method_call");
|
|
|
|
break;
|
|
|
|
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
|
|
|
|
lua_pushliteral(globalconf.L, "method_return");
|
|
|
|
break;
|
|
|
|
case DBUS_MESSAGE_TYPE_ERROR:
|
|
|
|
lua_pushliteral(globalconf.L, "error");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_pushliteral(globalconf.L, "unknown");
|
|
|
|
break;
|
2008-07-08 10:54:57 +02:00
|
|
|
}
|
|
|
|
|
2008-11-19 02:22:05 +01:00
|
|
|
lua_setfield(globalconf.L, -2, "type");
|
2008-07-08 10:54:57 +02:00
|
|
|
|
2008-11-19 02:22:05 +01:00
|
|
|
const char *s = dbus_message_get_interface(msg);
|
|
|
|
lua_pushstring(globalconf.L, NONULL(s));
|
|
|
|
lua_setfield(globalconf.L, -2, "interface");
|
|
|
|
|
|
|
|
s = dbus_message_get_path(msg);
|
|
|
|
lua_pushstring(globalconf.L, NONULL(s));
|
|
|
|
lua_setfield(globalconf.L, -2, "path");
|
|
|
|
|
|
|
|
s = dbus_message_get_member(msg);
|
|
|
|
lua_pushstring(globalconf.L, NONULL(s));
|
|
|
|
lua_setfield(globalconf.L, -2, "member");
|
|
|
|
|
|
|
|
/* + 1 for the table above */
|
|
|
|
DBusMessageIter iter;
|
|
|
|
int nargs = 1;
|
|
|
|
|
|
|
|
if(dbus_message_iter_init(msg, &iter))
|
|
|
|
nargs += a_dbus_message_iter(&iter);
|
|
|
|
|
|
|
|
luaA_dofunction(globalconf.L, globalconf.hooks.dbus, nargs, 0);
|
2008-07-08 10:54:57 +02:00
|
|
|
}
|
|
|
|
|
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-11-19 02:22:05 +01:00
|
|
|
else
|
|
|
|
a_dbus_process_request(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
|
|
|
}
|
|
|
|
|
2008-11-18 16:31:27 +01:00
|
|
|
static bool
|
|
|
|
a_dbus_request_name(const char *name)
|
|
|
|
{
|
|
|
|
int ret = dbus_bus_request_name(dbus_connection, name, 0, &err);
|
|
|
|
|
|
|
|
if(dbus_error_is_set(&err))
|
|
|
|
{
|
|
|
|
warn("failed to request D-Bus name: %s", err.message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
|
|
|
|
return true;
|
|
|
|
case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
|
|
|
|
warn("already primary D-Bus name owner for %s", name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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-11-18 15:20:50 +01:00
|
|
|
warn("D-Bus 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-11-18 16:31:27 +01:00
|
|
|
a_dbus_request_name("org.awesome");
|
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-11-18 15:20:50 +01:00
|
|
|
warn("cannot get D-Bus 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-08-27 10:00:10 +02:00
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
|
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-11-18 16:31:27 +01:00
|
|
|
/** Register a D-Bus name to receive message from.
|
|
|
|
* \param L The Lua VM state.
|
|
|
|
* \return The number of elements pushed on stack.
|
|
|
|
* \luastack
|
|
|
|
* \lparam A string with the name of the D-Bus name to register. Note that
|
|
|
|
* org.awesome is registered by default.
|
|
|
|
* \lreturn True if everything worked fine, false otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
luaA_dbus_request_name(lua_State *L)
|
|
|
|
{
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
|
|
|
lua_pushboolean(L, a_dbus_request_name(name));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct luaL_reg awesome_dbus_lib[] =
|
|
|
|
{
|
|
|
|
{ "request_name", luaA_dbus_request_name },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2008-05-30 21:20:23 +02:00
|
|
|
#else /* HAVE_DBUS */
|
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
|
|
|
|
2008-11-18 16:31:27 +01:00
|
|
|
static int
|
|
|
|
luaA_donothing(lua_State *L)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct luaL_reg awesome_dbus_lib[] =
|
|
|
|
{
|
|
|
|
{ "request_name", luaA_donothing },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2008-05-30 21:20:23 +02:00
|
|
|
#endif
|
2008-04-24 17:45:30 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80
|