From c0e6113e0b2f831529923c59b598ef67f28c5f70 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 19 Nov 2008 02:21:11 +0100 Subject: [PATCH] dbus: add support for return values Signed-off-by: Julien Danjou --- dbus.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- hooks.c | 2 ++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/dbus.c b/dbus.c index aa12e5a5f..4de5a7be2 100644 --- a/dbus.c +++ b/dbus.c @@ -224,7 +224,68 @@ a_dbus_process_request(DBusMessage *msg) if(dbus_message_iter_init(msg, &iter)) nargs += a_dbus_message_iter(&iter); - luaA_dofunction(globalconf.L, globalconf.hooks.dbus, nargs, 0); + if(dbus_message_get_no_reply(msg)) + luaA_dofunction(globalconf.L, globalconf.hooks.dbus, nargs, 0); + else + { + int n = lua_gettop(globalconf.L) - nargs; + luaA_dofunction(globalconf.L, globalconf.hooks.dbus, nargs, LUA_MULTRET); + n -= lua_gettop(globalconf.L); + + DBusMessage *reply = dbus_message_new_method_return(msg); + + dbus_message_iter_init_append(reply, &iter); + + /* i is negative */ + for(int i = n; i < 0; i += 2) + { + /* i is the type name, i+1 the value */ + size_t len; + const char *type = lua_tolstring(globalconf.L, i, &len); + + if(!type || len != 1) + break; + + switch(*type) + { + case DBUS_TYPE_BOOLEAN: + { + bool b = lua_toboolean(globalconf.L, i + 1); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b); + } + break; +#define DBUS_MSG_RETURN_HANDLE_TYPE_STRING(dbustype) \ + case dbustype: \ + if((s = lua_tostring(globalconf.L, i + 1))) \ + dbus_message_iter_append_basic(&iter, dbustype, &s); \ + break; + DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_STRING) + DBUS_MSG_RETURN_HANDLE_TYPE_STRING(DBUS_TYPE_BYTE) +#undef DBUS_MSG_RETURN_HANDLE_TYPE_STRING +#define DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(type, dbustype) \ + case dbustype: \ + { \ + type num = lua_tonumber(globalconf.L, i + 1); \ + dbus_message_iter_append_basic(&iter, dbustype, &num); \ + } \ + break; + DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int16_t, DBUS_TYPE_INT16) + DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint16_t, DBUS_TYPE_UINT16) + DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int32_t, DBUS_TYPE_INT32) + DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint32_t, DBUS_TYPE_UINT32) + DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(int64_t, DBUS_TYPE_INT64) + DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(uint64_t, DBUS_TYPE_UINT64) + DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER(double, DBUS_TYPE_DOUBLE) +#undef DBUS_MSG_RETURN_HANDLE_TYPE_NUMBER + } + + lua_remove(globalconf.L, i); + lua_remove(globalconf.L, i + 1); + } + + dbus_connection_send(dbus_connection, reply, NULL); + dbus_message_unref(reply); + } } static void diff --git a/hooks.c b/hooks.c index d76b7516e..7af235ede 100644 --- a/hooks.c +++ b/hooks.c @@ -180,6 +180,8 @@ luaA_hooks_timer(lua_State *L) * receive: signal, method_call, method_return or error. * The second argument is the path. * The other arguments are a variable list of arguments. + * The function can return values using pair of type, value. + * For example: return "s", "hello", "i", 32 * \param L The Lua VM state. * \return The number of elements pushed on stack. * \luastack