Skip to content

Commit dafca86

Browse files
committed
Cleanup SDL_UDEV_GetProductSerial()
1 parent 54f129f commit dafca86

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

src/core/linux/SDL_udev.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#include "SDL_evdev_capabilities.h"
3737
#include "../unix/SDL_poll.h"
3838

39-
#define SDL_UDEV_FALLBACK_LIBS "libudev.so.1", "libudev.so.0"
39+
#define SDL_UDEV_FALLBACK_LIBS "libudev.so.1", "libudev.so.0"
4040

4141
static const char *SDL_UDEV_LIBS[] = { SDL_UDEV_FALLBACK_LIBS };
4242

@@ -247,22 +247,19 @@ bool SDL_UDEV_GetProductInfo(const char *device_path, struct input_id *inpid, in
247247
return false;
248248
}
249249

250-
if (stat(device_path, &statbuf) == -1) {
250+
if (stat(device_path, &statbuf) < 0) {
251251
return false;
252252
}
253253

254254
if (S_ISBLK(statbuf.st_mode)) {
255255
type = 'b';
256-
}
257-
else if (S_ISCHR(statbuf.st_mode)) {
256+
} else if (S_ISCHR(statbuf.st_mode)) {
258257
type = 'c';
259-
}
260-
else {
258+
} else {
261259
return false;
262260
}
263261

264262
dev = _this->syms.udev_device_new_from_devnum(_this->udev, type, statbuf.st_rdev);
265-
266263
if (!dev) {
267264
return false;
268265
}
@@ -302,41 +299,43 @@ bool SDL_UDEV_GetProductInfo(const char *device_path, struct input_id *inpid, in
302299
return true;
303300
}
304301

305-
bool SDL_UDEV_GetProductSerial(const char *device_path, const char **serial)
302+
char *SDL_UDEV_GetProductSerial(const char *device_path)
306303
{
307304
struct stat statbuf;
308305
char type;
309306
struct udev_device *dev;
310307
const char *val;
308+
char *result = NULL;
311309

312310
if (!_this) {
313-
return false;
311+
return NULL;
314312
}
315313

316314
if (stat(device_path, &statbuf) < 0) {
317-
return false;
315+
return NULL;
318316
}
319317

320318
if (S_ISBLK(statbuf.st_mode)) {
321319
type = 'b';
322320
} else if (S_ISCHR(statbuf.st_mode)) {
323321
type = 'c';
324322
} else {
325-
return false;
323+
return NULL;
326324
}
327325

328326
dev = _this->syms.udev_device_new_from_devnum(_this->udev, type, statbuf.st_rdev);
329327
if (!dev) {
330-
return false;
328+
return NULL;
331329
}
332330

333331
val = _this->syms.udev_device_get_property_value(dev, "ID_SERIAL_SHORT");
334-
if (val) {
335-
*serial = val;
336-
return true;
332+
if (val && *val) {
333+
result = SDL_strdup(val);
337334
}
338335

339-
return false;
336+
_this->syms.udev_device_unref(dev);
337+
338+
return result;
340339
}
341340

342341
void SDL_UDEV_UnloadLibrary(void)

src/core/linux/SDL_udev.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ extern bool SDL_UDEV_LoadLibrary(void);
105105
extern void SDL_UDEV_Poll(void);
106106
extern bool SDL_UDEV_Scan(void);
107107
extern bool SDL_UDEV_GetProductInfo(const char *device_path, struct input_id *inpid, int *class, char **driver);
108-
extern bool SDL_UDEV_GetProductSerial(const char *device_path, const char **serial);
108+
extern char *SDL_UDEV_GetProductSerial(const char *device_path);
109109
extern bool SDL_UDEV_AddCallback(SDL_UDEV_Callback cb);
110110
extern void SDL_UDEV_DelCallback(SDL_UDEV_Callback cb);
111111
extern const SDL_UDEV_Symbols *SDL_UDEV_GetUdevSyms(void);

src/joystick/linux/SDL_sysjoystick.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,10 +1609,7 @@ static bool LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
16091609
}
16101610

16111611
#ifdef SDL_USE_LIBUDEV
1612-
const char *serial = NULL;
1613-
if (SDL_UDEV_GetProductSerial(item->path, &serial)) {
1614-
joystick->serial = SDL_strdup(serial);
1615-
}
1612+
joystick->serial = SDL_UDEV_GetProductSerial(item->path);
16161613
#endif
16171614

16181615
// mark joystick as fresh and ready

0 commit comments

Comments
 (0)