Diff between 42d1c2f0ba790e8a2050d5fcf8b45e0c563776ef and 8c19cbee79fb00580386083d405bec948c7217f0

Changed Files

File Additions Deletions Status
client/main.c +56 -37 modified

Full Patch

diff --git a/client/main.c b/client/main.c
index 6514be8..be785c5 100644
--- a/client/main.c
+++ b/client/main.c
@@ -321,6 +321,60 @@ static gboolean service_is_child(GDBusProxy *service)
 	return FALSE;
 }
 
+static void set_default_device(GDBusProxy *proxy, const char *attribute)
+{
+	char *desc = NULL;
+	DBusMessageIter iter;
+	const char *path;
+
+	default_dev = proxy;
+
+	if (proxy == NULL) {
+		default_attr = NULL;
+		goto done;
+	}
+
+	if (!g_dbus_proxy_get_property(proxy, "Alias", &iter)) {
+		if (!g_dbus_proxy_get_property(proxy, "Address", &iter))
+			goto done;
+	}
+
+	path = g_dbus_proxy_get_path(proxy);
+
+	dbus_message_iter_get_basic(&iter, &desc);
+	desc = g_strdup_printf(COLOR_BLUE "[%s%s%s]" COLOR_OFF "# ", desc,
+				attribute ? ":" : "",
+				attribute ? attribute + strlen(path) : "");
+
+done:
+	rl_set_prompt(desc ? desc : PROMPT_ON);
+	printf("\r");
+	rl_on_new_line();
+	rl_redisplay();
+	g_free(desc);
+}
+
+static void device_added(GDBusProxy *proxy)
+{
+	DBusMessageIter iter;
+
+	dev_list = g_list_append(dev_list, proxy);
+
+	print_device(proxy, COLORED_NEW);
+
+	if (default_dev)
+		return;
+
+	if (g_dbus_proxy_get_property(proxy, "Connected", &iter)) {
+		dbus_bool_t connected;
+
+		dbus_message_iter_get_basic(&iter, &connected);
+
+		if (connected)
+			set_default_device(proxy, NULL);
+	}
+}
+
 static void proxy_added(GDBusProxy *proxy, void *user_data)
 {
 	const char *interface;
@@ -328,11 +382,9 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 	interface = g_dbus_proxy_get_interface(proxy);
 
 	if (!strcmp(interface, "org.bluez.Device1")) {
-		if (device_is_child(proxy, default_ctrl) == TRUE) {
-			dev_list = g_list_append(dev_list, proxy);
+		if (device_is_child(proxy, default_ctrl) == TRUE)
+			device_added(proxy);
 
-			print_device(proxy, COLORED_NEW);
-		}
 	} else if (!strcmp(interface, "org.bluez.Adapter1")) {
 		ctrl_list = g_list_append(ctrl_list, proxy);
 
@@ -360,39 +412,6 @@ static void proxy_added(GDBusProxy *proxy, void *user_data)
 	}
 }
 
-static void set_default_device(GDBusProxy *proxy, const char *attribute)
-{
-	char *desc = NULL;
-	DBusMessageIter iter;
-	const char *path;
-
-	default_dev = proxy;
-
-	if (proxy == NULL) {
-		default_attr = NULL;
-		goto done;
-	}
-
-	if (!g_dbus_proxy_get_property(proxy, "Alias", &iter)) {
-		if (!g_dbus_proxy_get_property(proxy, "Address", &iter))
-			goto done;
-	}
-
-	path = g_dbus_proxy_get_path(proxy);
-
-	dbus_message_iter_get_basic(&iter, &desc);
-	desc = g_strdup_printf(COLOR_BLUE "[%s%s%s]" COLOR_OFF "# ", desc,
-				attribute ? ":" : "",
-				attribute ? attribute + strlen(path) : "");
-
-done:
-	rl_set_prompt(desc ? desc : PROMPT_ON);
-	printf("\r");
-	rl_on_new_line();
-	rl_redisplay();
-	g_free(desc);
-}
-
 static void set_default_attribute(GDBusProxy *proxy)
 {
 	const char *path;