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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
int
libgamepad_open_superdevice(struct libgamepad_superdevice *devicep, const char *syspath)
{
struct libgamepad_subdevice *subdev;
int dirfd = -1, dirfd2, dirfd3;
DIR *dir = NULL, *dir2 = NULL;
struct dirent *f, *f2;
enum libgamepad_type type;
size_t first = 0, len;
void *new;
int saved_errno;
devicep->ndevices = 0;
devicep->devices = NULL;
devicep->nleds = 0;
devicep->leds = NULL;
devicep->npower_supplies = 0;
devicep->power_supplies = NULL;
devicep->syspath = strdup(syspath);
if (!devicep->syspath)
return -1;
dirfd = open(syspath, O_DIRECTORY);
if (dirfd < 0)
goto fail;
dirfd2 = openat(dirfd, "input", O_DIRECTORY);
if (dirfd2 < 0)
goto fail;
dir = fdopendir(dirfd2);
if (!dir)
goto fail;
while (errno = 0, (f = readdir(dir))) {
if (!strncmp(f->d_name, "input", 5) && isdigit(f->d_name[5])) {
dirfd3 = openat(dirfd2, f->d_name, O_DIRECTORY);
if (dirfd3 < 0)
goto fail;
dir2 = fdopendir(dirfd3);
if (!dir2)
goto fail;
type = 0;
while (errno = 0, (f2 = readdir(dir2))) {
if (!strncmp(f2->d_name, "event", 5) && isdigit(f2->d_name[5])) {
new = realloc(devicep->devices, (devicep->ndevices + 1) * sizeof(*devicep->devices));
if (!new)
goto fail;
devicep->devices = new;
len = sizeof("/dev/input/") + strlen(f2->d_name);
subdev = malloc(offsetof(struct libgamepad_subdevice, path) + len);
if (!subdev)
goto fail;
stpcpy(stpcpy(subdev->path, "/dev/input/"), f2->d_name);
devicep->devices[devicep->ndevices++] = subdev;
} else if (!strncmp(f2->d_name, "js", 2) && isdigit(f2->d_name[2])) {
type |= LIBGAMEPAD_GAMEPAD;
} else if (!strncmp(f2->d_name, "mouse", 5) && isdigit(f2->d_name[5])) {
type |= LIBGAMEPAD_MOUSE;
}
}
while (first < devicep->ndevices)
devicep->devices[first++]->type = type;
closedir(dir2);
dir2 = NULL;
}
}
if (errno)
goto fail;
closedir(dir);
dir = NULL;
dirfd2 = openat(dirfd, "leds", O_DIRECTORY);
if (dirfd2 >= 0) {
dir = fdopendir(dirfd2);
if (!dir)
goto fail;
len = strlen(syspath) + sizeof("/leds/");
while (errno = 0, (f = readdir(dir))) {
if (f->d_name[0] != '.') {
new = realloc(devicep->leds, (devicep->nleds + 1) * sizeof(*devicep->leds));
if (!new)
goto fail;
devicep->leds = new;
devicep->leds[devicep->nleds] = malloc(len + strlen(f->d_name));
if (!devicep->leds[devicep->nleds])
goto fail;
stpcpy(stpcpy(stpcpy(devicep->leds[devicep->nleds], syspath), "/leds/"), f->d_name);
devicep->nleds += 1;
}
}
if (errno)
goto fail;
closedir(dir);
dir = NULL;
}
dirfd2 = openat(dirfd, "power_supply", O_DIRECTORY);
if (dirfd2 >= 0) {
dir = fdopendir(dirfd2);
if (!dir)
goto fail;
len = strlen(syspath) + sizeof("/power_supply/");
while (errno = 0, (f = readdir(dir))) {
if (f->d_name[0] != '.') {
new = realloc(devicep->power_supplies,
(devicep->npower_supplies + 1) * sizeof(*devicep->power_supplies));
if (!new)
goto fail;
devicep->power_supplies = new;
devicep->power_supplies[devicep->npower_supplies] = malloc(len + strlen(f->d_name));
if (!devicep->power_supplies[devicep->npower_supplies])
goto fail;
stpcpy(stpcpy(stpcpy(devicep->power_supplies[devicep->npower_supplies], syspath),
"/power_supply/"), f->d_name);
devicep->npower_supplies += 1;
}
}
if (errno)
goto fail;
closedir(dir);
dir = NULL;
}
close(dirfd);
return 0;
fail:
saved_errno = errno;
if (dir2)
closedir(dir2);
if (dir)
closedir(dir);
if (dirfd >= 0)
close(dirfd);
libgamepad_close_superdevice(devicep);
devicep->syspath = NULL;
devicep->ndevices = 0;
devicep->devices = NULL;
devicep->nleds = 0;
devicep->leds = NULL;
devicep->npower_supplies = 0;
devicep->power_supplies = NULL;
errno = saved_errno;
return -1;
}
|