@@ -45,16 +45,16 @@ array [ //root
45
45
]
46
46
*/
47
47
48
- static void detectBluetoothValue (FFDBusData * dbus , DBusMessageIter * iter , FFBluetoothResult * device )
48
+ static bool detectBluetoothValue (FFDBusData * dbus , DBusMessageIter * iter , FFBluetoothResult * device )
49
49
{
50
50
if (dbus -> lib -> ffdbus_message_iter_get_arg_type (iter ) != DBUS_TYPE_DICT_ENTRY )
51
- return ;
51
+ return true ;
52
52
53
53
DBusMessageIter dictIter ;
54
54
dbus -> lib -> ffdbus_message_iter_recurse (iter , & dictIter );
55
55
56
56
if (dbus -> lib -> ffdbus_message_iter_get_arg_type (& dictIter ) != DBUS_TYPE_STRING )
57
- return ;
57
+ return true ;
58
58
59
59
const char * deviceProperty ;
60
60
dbus -> lib -> ffdbus_message_iter_get_basic (& dictIter , & deviceProperty );
@@ -75,6 +75,13 @@ static void detectBluetoothValue(FFDBusData* dbus, DBusMessageIter* iter, FFBlue
75
75
}
76
76
else if (ffStrEquals (deviceProperty , "Connected" ))
77
77
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;
78
85
}
79
86
80
87
static void detectBluetoothProperty (FFDBusData * dbus , DBusMessageIter * iter , FFBluetoothResult * device )
@@ -104,32 +111,37 @@ static void detectBluetoothProperty(FFDBusData* dbus, DBusMessageIter* iter, FFB
104
111
105
112
do
106
113
{
107
- detectBluetoothValue (dbus , & arrayIter , device );
114
+ bool shouldContinue = detectBluetoothValue (dbus , & arrayIter , device );
115
+ if (!shouldContinue )
116
+ {
117
+ ffStrbufClear (& device -> name );
118
+ break ;
119
+ }
108
120
} while (dbus -> lib -> ffdbus_message_iter_next (& arrayIter ));
109
121
}
110
122
111
- static void detectBluetoothObject (FFlist * devices , FFDBusData * dbus , DBusMessageIter * iter )
123
+ static FFBluetoothResult * detectBluetoothObject (FFlist * devices , FFDBusData * dbus , DBusMessageIter * iter )
112
124
{
113
125
if (dbus -> lib -> ffdbus_message_iter_get_arg_type (iter ) != DBUS_TYPE_DICT_ENTRY )
114
- return ;
126
+ return NULL ;
115
127
116
128
DBusMessageIter dictIter ;
117
129
dbus -> lib -> ffdbus_message_iter_recurse (iter , & dictIter );
118
130
119
131
if (dbus -> lib -> ffdbus_message_iter_get_arg_type (& dictIter ) != DBUS_TYPE_OBJECT_PATH )
120
- return ;
132
+ return NULL ;
121
133
122
134
const char * objectPath ;
123
135
dbus -> lib -> ffdbus_message_iter_get_basic (& dictIter , & objectPath );
124
136
125
137
// We don't want adapter objects
126
138
if (!ffStrContains (objectPath , "/dev_" ))
127
- return ;
139
+ return NULL ;
128
140
129
141
dbus -> lib -> ffdbus_message_iter_next (& dictIter );
130
142
131
143
if (dbus -> lib -> ffdbus_message_iter_get_arg_type (& dictIter ) != DBUS_TYPE_ARRAY )
132
- return ;
144
+ return NULL ;
133
145
134
146
DBusMessageIter arrayIter ;
135
147
dbus -> lib -> ffdbus_message_iter_recurse (& dictIter , & arrayIter );
@@ -146,16 +158,10 @@ static void detectBluetoothObject(FFlist* devices, FFDBusData* dbus, DBusMessage
146
158
detectBluetoothProperty (dbus , & arrayIter , device );
147
159
} while (dbus -> lib -> ffdbus_message_iter_next (& arrayIter ));
148
160
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 ;
156
162
}
157
163
158
- static void detectBluetoothRoot (FFlist * devices , FFDBusData * dbus , DBusMessageIter * iter )
164
+ static void detectBluetoothRoot (FFlist * devices , FFDBusData * dbus , DBusMessageIter * iter , int32_t connectedCount )
159
165
{
160
166
if (dbus -> lib -> ffdbus_message_iter_get_arg_type (iter ) != DBUS_TYPE_ARRAY )
161
167
return ;
@@ -165,11 +171,25 @@ static void detectBluetoothRoot(FFlist* devices, FFDBusData* dbus, DBusMessageIt
165
171
166
172
do
167
173
{
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
+ }
169
189
} while (dbus -> lib -> ffdbus_message_iter_next (& arrayIter ));
170
190
}
171
191
172
- static const char * detectBluetooth (FFlist * devices )
192
+ static const char * detectBluetooth (FFlist * devices , int32_t connectedCount )
173
193
{
174
194
FFDBusData dbus ;
175
195
const char * error = ffDBusLoadData (DBUS_BUS_SYSTEM , & dbus );
@@ -187,37 +207,43 @@ static const char* detectBluetooth(FFlist* devices)
187
207
return "Failed to get root iterator of GetManagedObjects" ;
188
208
}
189
209
190
- detectBluetoothRoot (devices , & dbus , & rootIter );
210
+ detectBluetoothRoot (devices , & dbus , & rootIter , connectedCount );
191
211
192
212
dbus .lib -> ffdbus_message_unref (managedObjects );
193
213
return NULL ;
194
214
}
195
215
196
- static bool hasConnectedDevices (void )
216
+ static uint32_t connectedDevices (void )
197
217
{
198
218
FF_AUTO_CLOSE_DIR DIR * dirp = opendir ("/sys/class/bluetooth" );
199
219
if (dirp == NULL )
200
- return false ;
220
+ return 0 ;
201
221
222
+ uint32_t result = 0 ;
202
223
struct dirent * entry ;
203
224
while ((entry = readdir (dirp )) != NULL )
204
225
{
205
- if (strchr (entry -> d_name , ':' ) != NULL ) // ignore connected devices
206
- return true ;
226
+ if (strchr (entry -> d_name , ':' ) != NULL )
227
+ ++ result ;
207
228
}
208
229
209
- return false ;
230
+ return result ;
210
231
}
211
232
212
233
#endif
213
234
214
235
const char * ffDetectBluetooth (FFBluetoothOptions * options , FF_MAYBE_UNUSED FFlist * devices /* FFBluetoothResult */ )
215
236
{
216
237
#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 );
221
247
#else
222
248
return "Fastfetch was compiled without DBus support" ;
223
249
#endif
0 commit comments