Skip to content

Commit 74586a9

Browse files
committed
Bluetooth (Linux): improve performance
1. ignore unpaired devices 2. ignore disconnected devices when `showDisconnected == false`
1 parent c5d4b20 commit 74586a9

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

src/detection/bluetooth/bluetooth_linux.c

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ array [ //root
4545
]
4646
*/
4747

48-
static void detectBluetoothValue(FFDBusData* dbus, DBusMessageIter* iter, FFBluetoothResult* device)
48+
static bool detectBluetoothValue(FFDBusData* dbus, DBusMessageIter* iter, FFBluetoothResult* device)
4949
{
5050
if(dbus->lib->ffdbus_message_iter_get_arg_type(iter) != DBUS_TYPE_DICT_ENTRY)
51-
return;
51+
return true;
5252

5353
DBusMessageIter dictIter;
5454
dbus->lib->ffdbus_message_iter_recurse(iter, &dictIter);
5555

5656
if(dbus->lib->ffdbus_message_iter_get_arg_type(&dictIter) != DBUS_TYPE_STRING)
57-
return;
57+
return true;
5858

5959
const char* deviceProperty;
6060
dbus->lib->ffdbus_message_iter_get_basic(&dictIter, &deviceProperty);
@@ -75,6 +75,13 @@ static void detectBluetoothValue(FFDBusData* dbus, DBusMessageIter* iter, FFBlue
7575
}
7676
else if(ffStrEquals(deviceProperty, "Connected"))
7777
ffDBusGetBool(dbus, &dictIter, &device->connected);
78+
else if(ffStrEquals(deviceProperty, "Paired"))
79+
{
80+
bool paired = true;
81+
ffDBusGetBool(dbus, &dictIter, &paired);
82+
if (!paired) return false;
83+
}
84+
return true;
7885
}
7986

8087
static void detectBluetoothProperty(FFDBusData* dbus, DBusMessageIter* iter, FFBluetoothResult* device)
@@ -104,32 +111,37 @@ static void detectBluetoothProperty(FFDBusData* dbus, DBusMessageIter* iter, FFB
104111

105112
do
106113
{
107-
detectBluetoothValue(dbus, &arrayIter, device);
114+
bool shouldContinue = detectBluetoothValue(dbus, &arrayIter, device);
115+
if (!shouldContinue)
116+
{
117+
ffStrbufClear(&device->name);
118+
break;
119+
}
108120
} while (dbus->lib->ffdbus_message_iter_next(&arrayIter));
109121
}
110122

111-
static void detectBluetoothObject(FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter)
123+
static FFBluetoothResult* detectBluetoothObject(FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter)
112124
{
113125
if(dbus->lib->ffdbus_message_iter_get_arg_type(iter) != DBUS_TYPE_DICT_ENTRY)
114-
return;
126+
return NULL;
115127

116128
DBusMessageIter dictIter;
117129
dbus->lib->ffdbus_message_iter_recurse(iter, &dictIter);
118130

119131
if(dbus->lib->ffdbus_message_iter_get_arg_type(&dictIter) != DBUS_TYPE_OBJECT_PATH)
120-
return;
132+
return NULL;
121133

122134
const char* objectPath;
123135
dbus->lib->ffdbus_message_iter_get_basic(&dictIter, &objectPath);
124136

125137
// We don't want adapter objects
126138
if(!ffStrContains(objectPath, "/dev_"))
127-
return;
139+
return NULL;
128140

129141
dbus->lib->ffdbus_message_iter_next(&dictIter);
130142

131143
if(dbus->lib->ffdbus_message_iter_get_arg_type(&dictIter) != DBUS_TYPE_ARRAY)
132-
return;
144+
return NULL;
133145

134146
DBusMessageIter arrayIter;
135147
dbus->lib->ffdbus_message_iter_recurse(&dictIter, &arrayIter);
@@ -146,16 +158,10 @@ static void detectBluetoothObject(FFlist* devices, FFDBusData* dbus, DBusMessage
146158
detectBluetoothProperty(dbus, &arrayIter, device);
147159
} while (dbus->lib->ffdbus_message_iter_next(&arrayIter));
148160

149-
if(device->name.length == 0)
150-
{
151-
ffStrbufDestroy(&device->name);
152-
ffStrbufDestroy(&device->address);
153-
ffStrbufDestroy(&device->type);
154-
--devices->length;
155-
}
161+
return device;
156162
}
157163

158-
static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter)
164+
static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIter* iter, int32_t connectedCount)
159165
{
160166
if(dbus->lib->ffdbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
161167
return;
@@ -165,11 +171,25 @@ static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIt
165171

166172
do
167173
{
168-
detectBluetoothObject(devices, dbus, &arrayIter);
174+
FFBluetoothResult* device = detectBluetoothObject(devices, dbus, &arrayIter);
175+
176+
if (device)
177+
{
178+
if(device->name.length == 0 || (connectedCount > 0 && !device->connected))
179+
{
180+
ffStrbufDestroy(&device->name);
181+
ffStrbufDestroy(&device->address);
182+
ffStrbufDestroy(&device->type);
183+
--devices->length;
184+
}
185+
186+
if (device->connected && --connectedCount == 0)
187+
break;
188+
}
169189
} while (dbus->lib->ffdbus_message_iter_next(&arrayIter));
170190
}
171191

172-
static const char* detectBluetooth(FFlist* devices)
192+
static const char* detectBluetooth(FFlist* devices, int32_t connectedCount)
173193
{
174194
FFDBusData dbus;
175195
const char* error = ffDBusLoadData(DBUS_BUS_SYSTEM, &dbus);
@@ -187,37 +207,43 @@ static const char* detectBluetooth(FFlist* devices)
187207
return "Failed to get root iterator of GetManagedObjects";
188208
}
189209

190-
detectBluetoothRoot(devices, &dbus, &rootIter);
210+
detectBluetoothRoot(devices, &dbus, &rootIter, connectedCount);
191211

192212
dbus.lib->ffdbus_message_unref(managedObjects);
193213
return NULL;
194214
}
195215

196-
static bool hasConnectedDevices(void)
216+
static uint32_t connectedDevices(void)
197217
{
198218
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/bluetooth");
199219
if(dirp == NULL)
200-
return false;
220+
return 0;
201221

222+
uint32_t result = 0;
202223
struct dirent* entry;
203224
while ((entry = readdir(dirp)) != NULL)
204225
{
205-
if (strchr(entry->d_name, ':') != NULL) // ignore connected devices
206-
return true;
226+
if (strchr(entry->d_name, ':') != NULL)
227+
++result;
207228
}
208229

209-
return false;
230+
return result;
210231
}
211232

212233
#endif
213234

214235
const char* ffDetectBluetooth(FFBluetoothOptions* options, FF_MAYBE_UNUSED FFlist* devices /* FFBluetoothResult */)
215236
{
216237
#ifdef FF_HAVE_DBUS
217-
if (!options->showDisconnected && !hasConnectedDevices())
218-
return NULL;
219-
220-
return detectBluetooth(devices);
238+
int32_t connectedCount = -1;
239+
if (!options->showDisconnected)
240+
{
241+
connectedCount = (int32_t) connectedDevices();
242+
if (connectedCount == 0)
243+
return NULL;
244+
}
245+
246+
return detectBluetooth(devices, connectedCount);
221247
#else
222248
return "Fastfetch was compiled without DBus support";
223249
#endif

0 commit comments

Comments
 (0)