-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisable-mouse-wakeup.sh
More file actions
134 lines (114 loc) · 4.32 KB
/
disable-mouse-wakeup.sh
File metadata and controls
134 lines (114 loc) · 4.32 KB
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
#!/bin/bash
# Disable Mouse Wake-up Script
# Prevents mice from waking laptop while preserving keyboard wake-up
set -e
UDEV_RULE_FILE="/etc/udev/rules.d/90-disable-mouse-wakeup.rules"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Check if running as root
if [[ $EUID -eq 0 ]]; then
echo -e "${RED}Error: Don't run this script as root. It will use sudo when needed.${NC}"
exit 1
fi
# Check if sudo is available
if ! command -v sudo &> /dev/null; then
echo -e "${RED}Error: sudo is required but not found.${NC}"
exit 1
fi
echo " Disable Mouse Wake-up Script"
echo
# Find connected mice
echo "Scanning for USB mice..."
mice_found=()
while IFS= read -r line; do
if [[ $line =~ Bus\ ([0-9]+)\ Device\ ([0-9]+):\ ID\ ([0-9a-f]{4}):([0-9a-f]{4})\ (.+) ]]; then
vendor_id="${BASH_REMATCH[3]}"
product_id="${BASH_REMATCH[4]}"
device_name="${BASH_REMATCH[5]}"
# Check if it's likely a mouse (common mouse vendors/names)
if [[ $device_name =~ [Mm]ouse ]] || \
[[ $vendor_id == "046d" ]] || \
[[ $vendor_id == "1bcf" ]] || \
[[ $vendor_id == "258a" ]] || \
[[ $vendor_id == "093a" ]] || \
[[ $device_name =~ [Ww]ireless.*[Dd]ongle ]] || \
[[ $device_name =~ [Rr]eceiver ]]; then
mice_found+=("$vendor_id:$product_id $device_name")
echo -e "${GREEN}Found mouse:${NC} $device_name (${vendor_id}:${product_id})"
fi
fi
done < <(lsusb)
if [[ ${#mice_found[@]} -eq 0 ]]; then
echo -e "${YELLOW}No USB mice detected.${NC}"
echo "Note: Built-in trackpads are usually handled differently."
exit 0
fi
echo
echo "This script will:"
echo "1. Immediately disable wake-up for currently connected mice"
echo "2. Create a udev rule to automatically disable wake-up for these mice when plugged in"
echo "3. Preserve keyboard wake-up functionality"
echo
read -p "Continue? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Cancelled."
exit 0
fi
# Disable wake-up for currently connected mice
echo
echo "Disabling wake-up for currently connected mice..."
for device_path in /sys/bus/usb/devices/*/power/wakeup; do
if [[ -f "$device_path" ]]; then
device_dir=$(dirname "$device_path")
if [[ -f "$device_dir/idVendor" && -f "$device_dir/idProduct" ]]; then
vendor=$(cat "$device_dir/idVendor")
product=$(cat "$device_dir/idProduct")
# Check if this device is one of our detected mice
for mouse in "${mice_found[@]}"; do
if [[ $mouse =~ ^${vendor}:${product} ]]; then
current_wakeup=$(cat "$device_path")
if [[ $current_wakeup == "enabled" ]]; then
echo "Disabling wake-up for $vendor:$product"
echo 'disabled' | sudo tee "$device_path" > /dev/null
fi
fi
done
fi
fi
done
# Create udev rule
echo
echo "Creating persistent udev rule..."
if [[ -f "$UDEV_RULE_FILE" ]]; then
echo -e "${YELLOW}Warning: $UDEV_RULE_FILE already exists. Backing up to ${UDEV_RULE_FILE}.backup${NC}"
sudo cp "$UDEV_RULE_FILE" "${UDEV_RULE_FILE}.backup"
fi
# Generate udev rule content
udev_content="# Disable mouse wake-up - generated by disable-mouse-wakeup.sh"$'\n'
udev_content+="# This prevents mice from waking the system while preserving keyboard wake-up"$'\n'$'\n'
for mouse in "${mice_found[@]}"; do
if [[ $mouse =~ ^([0-9a-f]{4}):([0-9a-f]{4})\ (.+) ]]; then
vendor_id="${BASH_REMATCH[1]}"
product_id="${BASH_REMATCH[2]}"
device_name="${BASH_REMATCH[3]}"
udev_content+="# $device_name"$'\n'
udev_content+="SUBSYSTEM==\"usb\", ATTR{idVendor}==\"$vendor_id\", ATTR{idProduct}==\"$product_id\", ATTR{power/wakeup}=\"disabled\""$'\n'$'\n'
fi
done
echo "$udev_content" | sudo tee "$UDEV_RULE_FILE" > /dev/null
# Reload udev rules
echo "Reloading udev rules..."
sudo udevadm control --reload-rules
echo
echo -e "${GREEN} Success!${NC}"
echo
echo "Changes made:"
echo "- Disabled wake-up for currently connected mice"
echo "- Created $UDEV_RULE_FILE for persistence"
echo
echo "Your mice will no longer wake the laptop, but keyboards still will."
echo "To undo: sudo rm $UDEV_RULE_FILE && sudo udevadm control --reload-rules"