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 <ludovico.denittis@collabora.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/src/core/linux/SDL_dbus.c b/src/core/linux/SDL_dbus.c
index ecfbe6f..58d3fd3 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);
}