Skip to content

Commit

Permalink
Install and run as user instead of root
Browse files Browse the repository at this point in the history
  • Loading branch information
neildavis committed Dec 10, 2022
1 parent 74d0053 commit 7782a3d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
28 changes: 14 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ ARGS ?=
CORE_DIR := src
CONFIG_DIR := config
SYSTEMD_CONFIG_DIR := $(CONFIG_DIR)/etc/systemd/system

SYSTEMD_CONFIG_FILE := $(TARGET).service

SYSTEM_SYSTEMD_CONFIG_DIR := /etc/systemd/system
INSTALL_DIR := /opt/$(TARGET)
USER_SYSTEMD_CONFIG_DIR := /home/$(USER)/.config/systemd/user
INSTALL_DIR := /home/$(USER)/.local/opt/$(TARGET)
VENV_PATH := $(INSTALL_DIR)/env

install:
mkdir -p $(INSTALL_DIR)
mkdir -p $(USER_SYSTEMD_CONFIG_DIR)
python3 -m venv $(VENV_PATH)
$(VENV_PATH)/bin/pip install evdev pyalsaaudio
cp $(CORE_DIR)/$(TARGET_PY) $(INSTALL_DIR)/$(TARGET_PY)
cp $(SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE) $(SYSTEM_SYSTEMD_CONFIG_DIR)/
sed -i 's|%VENV_PATH%|$(VENV_PATH)|g' $(SYSTEM_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE)
sed -i 's|%TARGET_PY%|$(INSTALL_DIR)/$(TARGET_PY)|g' $(SYSTEM_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE)
sed -i 's|%ARGS%|$(ARGS)|g' $(SYSTEM_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE)
systemctl daemon-reload
systemctl enable $(TARGET)
cp $(SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE) $(USER_SYSTEMD_CONFIG_DIR)/
sed -i 's|%VENV_PATH%|$(VENV_PATH)|g' $(USER_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE)
sed -i 's|%TARGET_PY%|$(INSTALL_DIR)/$(TARGET_PY)|g' $(USER_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE)
sed -i 's|%ARGS%|$(ARGS)|g' $(USER_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE)
systemctl --user daemon-reload
systemctl --user enable $(TARGET)

uninstall:
systemctl stop $(TARGET) 2> /dev/null || true
systemctl --user stop $(TARGET) 2> /dev/null || true
killall $(TARGET) 2> /dev/null || true
systemctl disable $(TARGET) 2> /dev/null || true
rm $(SYSTEM_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE) 2> /dev/null || true
systemctl --user disable $(TARGET) 2> /dev/null || true
rm $(USER_SYSTEMD_CONFIG_DIR)/$(SYSTEMD_CONFIG_FILE) 2> /dev/null || true
rm -rf $(INSTALL_DIR) 2> /dev/null || true

start:
systemctl start $(TARGET)
systemctl --user start $(TARGET)

stop:
systemctl stop $(TARGET)
systemctl --user stop $(TARGET)

54 changes: 28 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ In my case I was using a Raspberry Pi in CLI boot mode with the [ALSA SoftVol](h

The daemon installs as a [systemd](https://www.linux.com/training-tutorials/understanding-and-using-systemd/) service.

## Usage ##

### Prerequisites ###
## Prerequisites ##

### Python envronment ###

Since the daemon runs using [Python](https://www.python.org/) we need to setup the Python environment. In particular we require Python version 3, not the deprecated Python 2. Most modern Linux distros include Python3 by default, but the commands below will install it if it's missing.

Expand All @@ -32,14 +33,24 @@ Along with the base Python language & runtime support, we will need a few more t
sudo apt install python3 python3-pip python3-venv
```

### Running manually ###
### User group requirements ###

Add your user to the `input` group if necessary. This is required to allow the daemon to receive Linux evdev input events from USB HID devices.

```shell
sudo usermod -a -G input $USER
```

You will need to log out and log in again (or reboot) for this change to take effect. Again, this only has to be done once.

## Running manually ##

You can run daemon manually in a terminal using the steps below.
However most of the time you will want the daemon to install and start automatically instead of starting it manually like this. In that case, skip the rest of this section and see the following sections below including:

* ['Identifying ALSA cards and mixers'](#Identifying-ALSA-cards-and-mixers)
* ['Identifying USB HID input devices](#Identifying-USB-HID-input-devices)
* ['Installation'](#installation])
* ['Identifying ALSA cards and mixers'](#identifying-alsa-cards-and-mixers)
* ['Identifying USB HID input devices](#identifying-usb-hid-input-devices)
* ['Installing'](#installing)
But back to running manually in a terminal:
Expand All @@ -60,21 +71,8 @@ But back to running manually in a terminal:
```

Again, this only has to be done once.
3. Add your user to the `input` group if necessary. This is required to allow the daemon to receive Linux evdev input events from USB HID devices. First check to see if your user is already in the `input` group:

```shell
groups
```

This command will display all of the groups your user is a member of. if you see `input` listed in the results you're all set. If not, you need to add your user to the `input` group using this command:
```shell
sudo usermod -a -G input $USER
```

You will need to log out and log in again (or reboot) for this change to take effect. Again, this only has to be done once.
4. Finally, run the daemon. Without any args it will try to find the best ALSA mixer device and USB HID device automatically. These, and other options can be specified manually on the command line with the `-h` flag. See the help for details:
3. Finally, run the daemon. Without any args it will try to find the best ALSA mixer device and USB HID device automatically. These, and other options can be specified manually on the command line with the `-h` flag. See the help for details:

```monospace
python3 src/alsa_vol_from_usb_hid.py -h
Expand Down Expand Up @@ -104,7 +102,7 @@ But back to running manually in a terminal:
Press CTRL+C to exit.
### Identifying ALSA cards and mixers ###
## Identifying ALSA cards and mixers ##
Without any `-d` argument, the daemon will attempt to use the '`default`' ALSA device.
This is often a 'virtual device' with a 'Master' mixer control and exactly what you want!
Expand Down Expand Up @@ -167,7 +165,7 @@ i.e. '`Headphone`' which is the name of the ALSA mixer control you use with `-c`
python3 src/alsa_vol_from_usb_hid.py -d hw:0 -c Headphone
```
### Identifying USB HID input devices ###
## Identifying USB HID input devices ##
Without any `-i` argument, the daemon will attempt to find a suitable input device automatically.
It does this by iterating all available USB input devices which identify themselves as
Expand Down Expand Up @@ -201,22 +199,26 @@ lrwxrwxrwx 1 root root 6 Dec 8 11:08 usb-Logitech_Logitech_Dual_Action_D4BEAFFB
If I wanted to use the Apple keyboard I would try to use `/dev/input/event3` or `/dev/input/event4`
and see which works. (Spoiler: it's `event4`)
### Installation ###
## Installing ##
Before installing, make sure you have
[added your user to the ```input``` group](#user-group-requirements)
if necessary, as described above.
To install with automatic ALSA device/control & USB HID device selection
```sh
sudo make install
make install
```
To install specifying any additional command line args as described above, pass `ARGS` to make. e.g:
```shell
sudo make install ARGS="-d hw:0 -c Headphone -v 5 -l warning"
make install ARGS="-d hw:0 -c Headphone -v 5 -l warning"
```
To uninstall:
```shell
sudo make uninstall
make uninstall
```
1 change: 0 additions & 1 deletion config/etc/systemd/system/alsa_vol_from_usb_hid.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Description=ALSA Volume Control from USB HID Consumer Control

[Service]
Type=simple
User=pi
ExecStart=%VENV_PATH%/bin/python3 %TARGET_PY% %ARGS%
Restart=on-failure

Expand Down

0 comments on commit 7782a3d

Please sign in to comment.