Skip to content

Commit

Permalink
Automatic addition of all current keyboards during installation (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhoekim authored Oct 17, 2024
1 parent d641fce commit 934cd35
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,21 @@ install:
mkdir --parents $(SERVICEPATH)
cp --force $(service) $(SERVICEPATH)
@echo ""

@echo "# Copying default configuration file to $(CONFIGPATH)/$(config)"
mkdir --parents $(CONFIGPATH)
-cp --no-clobber $(config) $(CONFIGPATH)
@echo ""

@echo "# Do you want to add all currently connected keyboards to your configuration? (y/N)"
@read -r answer; \
if [ "$$answer" = "y" ] || [ "$$answer" = "Y" ]; then \
echo ""; \
echo "Updating configuration..."; \
bash ./scripts/update_conf_with_all_keyboards.sh $(CONFIGPATH)/$(config); \
fi
@echo ""

@echo "# Enabling and starting the service"
systemctl --user daemon-reload
systemctl --user enable --now $(service)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ y - insert
1. Clone or download this repo
2. 'make' to build the application
3. 'make install' to install the application
4. To add all your current keyboards to the configuration, type 'y' when prompted.
5. Modify the config file (`~/.config/touchcursor/touchcursor.conf`) to your liking
6. Restart the service `systemctl --user restart touchcursor.service`

Expand Down
76 changes: 76 additions & 0 deletions scripts/update_conf_with_all_keyboards.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash

# Check if an argument is provided
if [ $# -eq 0 ]; then
echo "Error: No configuration file specified."
echo "Usage: $0 <path_to_config_file>"
exit 1
fi

CONFIG_FILE="$1"

# Set permissions to allow write for owner and read for others
chmod 644 "$CONFIG_FILE"

# Check if the config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: $CONFIG_FILE does not exist. Please create a basic configuration file first."
exit 1
fi

# Create a temporary file
TEMP_FILE=$(mktemp)

# Function to get all keyboard device names
get_keyboard_names() {
grep -E 'Name=|Handlers=|EV=' /proc/bus/input/devices |
grep -B2 'EV=120013' --no-group-separator |
grep 'Name=' |
cut -d= -f2- |
sed 's/"//g' |
sed 's/^[[:space:]]*//' |
sed 's/[[:space:]]*$//'
}

# Process the configuration file
{
device_section_found=false
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ $line == "[Device]" ]]; then
echo "$line"
# Insert all keyboard devices
while IFS= read -r device_name; do
echo "Name=\"$device_name\""
done < <(get_keyboard_names)

device_section_found=true
elif $device_section_found && [[ $line == Name=* ]]; then
# Skip original device configurations
continue
elif [[ $line == "["*"]" ]]; then
# Reset flag when a new section is found
device_section_found=false
echo "$line"
else
echo "$line"
fi
done < "$CONFIG_FILE"
} > "$TEMP_FILE"

# Check if the temporary file is not empty and is readable
if [ -s "$TEMP_FILE" ] && [ -r "$TEMP_FILE" ]; then
# Create a backup of the original file
cp "$CONFIG_FILE" "${CONFIG_FILE}.bak"

# Replace the original file with the modified content
mv "$TEMP_FILE" "$CONFIG_FILE"
chmod 644 "$CONFIG_FILE"

echo "Configuration ($CONFIG_FILE) has been updated."
echo "A backup of the original configuration has been saved as ${CONFIG_FILE}.bak"
echo "All keyboard devices have been added to the [Device] section."
else
echo "Error: Failed to update the configuration. The original file has not been modified."
rm "$TEMP_FILE"
exit 1
fi

0 comments on commit 934cd35

Please sign in to comment.