From a3ce700dedab201e52d2bcc20124d1502b078e7e Mon Sep 17 00:00:00 2001 From: Ludovico de Nittis Date: Thu, 25 Aug 2022 16:18:45 +0200 Subject: [PATCH] dbus: Add generic internal function to send messages with reply SDL_DBus_CallWithBasicReply() allows us to send a DBus message and get its result, if it is a basic type, e.g. integer or string. With this function we avoid duplicating code. Signed-off-by: Ludovico de Nittis --- src/core/linux/SDL_dbus.c | 40 ++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/core/linux/SDL_dbus.c b/src/core/linux/SDL_dbus.c index ecfbe6f4e..58d3fd311 100644 --- a/src/core/linux/SDL_dbus.c +++ b/src/core/linux/SDL_dbus.c @@ -287,6 +287,32 @@ SDL_DBus_CallVoidMethodInternal(DBusConnection *conn, const char *node, const ch return retval; } +static SDL_bool +SDL_DBus_CallWithBasicReply(DBusConnection *conn, DBusMessage *msg, const int expectedtype, void *result) +{ + SDL_bool retval = SDL_FALSE; + + DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL); + if (reply) { + DBusMessageIter iter, actual_iter; + dbus.message_iter_init(reply, &iter); + if (dbus.message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) { + dbus.message_iter_recurse(&iter, &actual_iter); + } else { + actual_iter = iter; + } + + if (dbus.message_iter_get_arg_type(&actual_iter) == expectedtype) { + dbus.message_iter_get_basic(&actual_iter, result); + retval = SDL_TRUE; + } + + dbus.message_unref(reply); + } + + return retval; +} + SDL_bool SDL_DBus_CallVoidMethodOnConnection(DBusConnection *conn, const char *node, const char *path, const char *interface, const char *method, ...) { @@ -318,19 +344,7 @@ SDL_DBus_QueryPropertyOnConnection(DBusConnection *conn, const char *node, const DBusMessage *msg = dbus.message_new_method_call(node, path, "org.freedesktop.DBus.Properties", "Get"); if (msg) { if (dbus.message_append_args(msg, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, &property, DBUS_TYPE_INVALID)) { - DBusMessage *reply = dbus.connection_send_with_reply_and_block(conn, msg, 300, NULL); - if (reply) { - DBusMessageIter iter, sub; - dbus.message_iter_init(reply, &iter); - if (dbus.message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) { - dbus.message_iter_recurse(&iter, &sub); - if (dbus.message_iter_get_arg_type(&sub) == expectedtype) { - dbus.message_iter_get_basic(&sub, result); - retval = SDL_TRUE; - } - } - dbus.message_unref(reply); - } + retval = SDL_DBus_CallWithBasicReply(conn, msg, expectedtype, result); } dbus.message_unref(msg); }