Skip to content

Commit 9011bc5

Browse files
committed
Auto-generate udev-rules
Also fix cmake version warning
1 parent c850a50 commit 9011bc5

6 files changed

Lines changed: 67 additions & 95 deletions

File tree

CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 2.6.2)
1+
cmake_minimum_required(VERSION 2.8...3.19)
22
project(headsetcontrol)
33

44
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/")
@@ -115,7 +115,17 @@ install(TARGETS headsetcontrol DESTINATION bin)
115115

116116
# install udev files on linux
117117
if(UNIX AND NOT APPLE)
118-
install(DIRECTORY udev/ DESTINATION /etc/udev/rules.d)
118+
set (program_cmd headsetcontrol)
119+
set (program_arg "-u")
120+
set (program_output "/etc/udev/rules.d/70-headsets.rules")
121+
install( CODE
122+
"
123+
execute_process(COMMAND ${program_cmd} ${program_arg}
124+
OUTPUT_FILE ${program_output})
125+
126+
message(STATUS \"Installed udev rules to ${program_output}\")
127+
"
128+
)
119129
endif()
120130

121131

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ This will copy the binary to a folder globally accessible via path.
114114

115115
### Access without root
116116

117-
Also in Linux, you need udev rules if you don't want to start the application with root. Those rules reside in the udev folder of this repository. Typing `make install` on Linux copies them automatically to /etc/udev/rules.d/.
117+
Also in Linux, you need udev rules if you don't want to start the application with root. Those rules are generated via `headsetcontrol -u`. Typing `make install` on Linux generates and writes them automatically to /etc/udev/rules.d/.
118118

119119
You can reload udev configuration without reboot via `sudo udevadm control --reload-rules && sudo udevadm trigger`
120120

@@ -123,7 +123,7 @@ You can reload udev configuration without reboot via `sudo udevadm control --rel
123123
Type `headsetcontrol -h` to get all available options.\
124124
(Don't forget to prefix it with `./` when the application resides in the current folder)
125125

126-
Type `headsetcontrol -?` to get a list of supported capabilities for the currently detected headset
126+
Type `headsetcontrol -?` to get a list of supported capabilities for the currently detected headset.
127127

128128
`headsetcontrol -s 128` sets the sidetone to 128 (REAL loud). You can silence it with `0`. I recommend a loudness of 16.
129129

@@ -141,6 +141,8 @@ Following options don't work on all devices yet:
141141

142142
`headsetcontrol -m` retrieves the current chat-mix-dial level setting.
143143

144+
`headsetcontrol -u` Generates and outputs udev-rules for Linux.
145+
144146
### Third Party
145147

146148
The following additional software can be used to enable control via a GUI

src/device_registry.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ int get_device(struct device* device_found, uint16_t idVendor, uint16_t idProduc
5757
}
5858
return 1;
5959
}
60+
61+
int iterate_devices(int index, struct device** device_found)
62+
{
63+
if (index < NUMDEVICES) {
64+
*device_found = devicelist[index];
65+
return 0;
66+
} else {
67+
return -1;
68+
}
69+
}

src/device_registry.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ void init_devices();
1717
* @return 0 when a device was found, 1 otherwise
1818
*/
1919
int get_device(struct device* device_found, uint16_t idVendor, uint16_t idProduct);
20+
21+
/** @brief Gives back an device at the given index
22+
*
23+
* Caller must iterate from index 0 upwards until returned -1 to get all devices
24+
*
25+
* @param index Current index
26+
* @param device_found output parameter, pointer to device pointer
27+
* @return 0 when a device exists at the given index, -1 if not
28+
*/
29+
int iterate_devices(int index, struct device** device_found);

src/main.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,27 @@ static void print_capability(enum capabilities cap, char shortName, const char*
186186
}
187187
}
188188

189+
static void print_udevrules()
190+
{
191+
int i = 0;
192+
struct device* device_found;
193+
194+
printf("ACTION!=\"add|change\", GOTO=\"headset_end\"\n");
195+
printf("\n");
196+
197+
while (iterate_devices(i++, &device_found) == 0) {
198+
printf("# %s\n", device_found->device_name);
199+
200+
for (int i = 0; i < device_found->numIdProducts; i++)
201+
printf("KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", TAG+=\"uaccess\"\n",
202+
(unsigned int)device_found->idVendor, (unsigned int)device_found->idProductsSupported[i]);
203+
204+
printf("\n");
205+
}
206+
207+
printf("LABEL=\"headset_end\"\n");
208+
}
209+
189210
int main(int argc, char* argv[])
190211
{
191212
int c;
@@ -200,7 +221,10 @@ int main(int argc, char* argv[])
200221
long rotate_to_mute = -1;
201222
long print_capabilities = -1;
202223

203-
while ((c = getopt(argc, argv, "bchs:n:l:i:mv:r:?")) != -1) {
224+
// Init all information of supported devices
225+
init_devices();
226+
227+
while ((c = getopt(argc, argv, "bchs:n:l:i:mv:r:u?")) != -1) {
204228
switch (c) {
205229
case 'b':
206230
request_battery = 1;
@@ -256,6 +280,10 @@ int main(int argc, char* argv[])
256280
return 1;
257281
}
258282
break;
283+
case 'u':
284+
fprintf(stderr, "Outputting udev rules to stdout/console...\n\n");
285+
print_udevrules();
286+
return 0;
259287
case '?':
260288
print_capabilities = 1;
261289
break;
@@ -271,6 +299,8 @@ int main(int argc, char* argv[])
271299
printf(" -m\t\tRetrieves the current chat-mix-dial level setting\n");
272300
printf(" -v 0|1\tTurn voice prompts on or off (0 = off, 1 = on)\n");
273301
printf(" -r 0|1\tTurn rotate to mute feature on or off (0 = off, 1 = on)\n");
302+
printf("\n");
303+
printf(" -u\t\tOutputs udev rules to stdout/console\n");
274304

275305
printf("\n");
276306
return 0;
@@ -285,9 +315,6 @@ int main(int argc, char* argv[])
285315
printf("Non-option argument %s\n", argv[index]);
286316
}
287317

288-
// Init all information of supported devices
289-
init_devices();
290-
291318
// Look for a supported device
292319
int headset_available = find_device();
293320
if (headset_available != 0) {

udev/70-headsets.rules

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)