Skip to content

Commit

Permalink
Auto-generate udev-rules
Browse files Browse the repository at this point in the history
Also fix cmake version warning
  • Loading branch information
Sapd committed May 13, 2021
1 parent c850a50 commit 9011bc5
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 95 deletions.
14 changes: 12 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.6.2)
cmake_minimum_required(VERSION 2.8...3.19)
project(headsetcontrol)

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

# install udev files on linux
if(UNIX AND NOT APPLE)
install(DIRECTORY udev/ DESTINATION /etc/udev/rules.d)
set (program_cmd headsetcontrol)
set (program_arg "-u")
set (program_output "/etc/udev/rules.d/70-headsets.rules")
install( CODE
"
execute_process(COMMAND ${program_cmd} ${program_arg}
OUTPUT_FILE ${program_output})
message(STATUS \"Installed udev rules to ${program_output}\")
"
)
endif()


Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ This will copy the binary to a folder globally accessible via path.

### Access without root

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/.
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/.

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

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

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

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

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

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

`headsetcontrol -u` Generates and outputs udev-rules for Linux.

### Third Party

The following additional software can be used to enable control via a GUI
Expand Down
10 changes: 10 additions & 0 deletions src/device_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ int get_device(struct device* device_found, uint16_t idVendor, uint16_t idProduc
}
return 1;
}

int iterate_devices(int index, struct device** device_found)
{
if (index < NUMDEVICES) {
*device_found = devicelist[index];
return 0;
} else {
return -1;
}
}
10 changes: 10 additions & 0 deletions src/device_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ void init_devices();
* @return 0 when a device was found, 1 otherwise
*/
int get_device(struct device* device_found, uint16_t idVendor, uint16_t idProduct);

/** @brief Gives back an device at the given index
*
* Caller must iterate from index 0 upwards until returned -1 to get all devices
*
* @param index Current index
* @param device_found output parameter, pointer to device pointer
* @return 0 when a device exists at the given index, -1 if not
*/
int iterate_devices(int index, struct device** device_found);
35 changes: 31 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ static void print_capability(enum capabilities cap, char shortName, const char*
}
}

static void print_udevrules()
{
int i = 0;
struct device* device_found;

printf("ACTION!=\"add|change\", GOTO=\"headset_end\"\n");
printf("\n");

while (iterate_devices(i++, &device_found) == 0) {
printf("# %s\n", device_found->device_name);

for (int i = 0; i < device_found->numIdProducts; i++)
printf("KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", TAG+=\"uaccess\"\n",
(unsigned int)device_found->idVendor, (unsigned int)device_found->idProductsSupported[i]);

printf("\n");
}

printf("LABEL=\"headset_end\"\n");
}

int main(int argc, char* argv[])
{
int c;
Expand All @@ -200,7 +221,10 @@ int main(int argc, char* argv[])
long rotate_to_mute = -1;
long print_capabilities = -1;

while ((c = getopt(argc, argv, "bchs:n:l:i:mv:r:?")) != -1) {
// Init all information of supported devices
init_devices();

while ((c = getopt(argc, argv, "bchs:n:l:i:mv:r:u?")) != -1) {
switch (c) {
case 'b':
request_battery = 1;
Expand Down Expand Up @@ -256,6 +280,10 @@ int main(int argc, char* argv[])
return 1;
}
break;
case 'u':
fprintf(stderr, "Outputting udev rules to stdout/console...\n\n");
print_udevrules();
return 0;
case '?':
print_capabilities = 1;
break;
Expand All @@ -271,6 +299,8 @@ int main(int argc, char* argv[])
printf(" -m\t\tRetrieves the current chat-mix-dial level setting\n");
printf(" -v 0|1\tTurn voice prompts on or off (0 = off, 1 = on)\n");
printf(" -r 0|1\tTurn rotate to mute feature on or off (0 = off, 1 = on)\n");
printf("\n");
printf(" -u\t\tOutputs udev rules to stdout/console\n");

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

// Init all information of supported devices
init_devices();

// Look for a supported device
int headset_available = find_device();
if (headset_available != 0) {
Expand Down
87 changes: 0 additions & 87 deletions udev/70-headsets.rules

This file was deleted.

0 comments on commit 9011bc5

Please sign in to comment.