-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkpoint.sh
288 lines (196 loc) · 5.68 KB
/
tkpoint.sh
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/bash
#
# Zenity Trackpoint Configuration Tool
#
# nvz < enveezee@gmail.com >
rule_file="/etc/udev/rules.d/70-ztrackpoint.rules"
settings=(
"bind_mode"
"draghys"
"drift_time"
"ext_dev"
"inertia"
"jenks"
"mindrag"
"press_to_select"
"rate"
"reach"
"resetafter"
"resolution"
"resync_time"
"sensitivity"
"skipback"
"speed"
"thresh"
"upthresh"
"ztime"
)
title="Trackpoint Configuration Tool"
# Show Settings Form
config_form () {
# Load current settings values into the entry array
for setting in ${settings[*]}; do
# Load current value for setting
value=$(cat "$sysfs_path/$setting")
# Save setting name to array
entry+=("$setting")
# Save setting value to array
entry+=("$value")
done
# Create config form
output=$(zenity --list --title "$title"\
--text "You can edit values and select settings to write."\
--editable --print-column=ALL --multiple --width=300 --height=680\
--column "Setting" --column "Value" "${entry[@]}")
# Exit if the user hit cancel
if [[ $? == 1 ]]; then exit 0; fi
# Parse output of list dialog into an array
IFS="|" read -ra entry < <(echo "$output")
# Check for selection of settings to modify
if (( ${#entry[*]} < 2 )); then
# Error on no selection
no_selection
# Restart form
config_form
fi
}
# No selection was made
no_selection () {
# Output error to console
echo "E: No selection was made!"
# Output error to GUI
zenity --info --title "Error" --width 300\
--text "No selection was made!"
}
# Select trackpoint device
select_trackpoint () {
# List available trackpoints for selection
output=$(zenity --list --title "$title"\
--text "Select trackpoint to configure."\
--print-column=ALL --width=600 --height=300\
--column "Name" --column "Sysfs Path" "${trackpoint[@]}")
# Exit if user presses cancel
if [[ $? == 1 ]]; then exit 0; fi
# Parse output of list dialog into array
IFS="|" read -ra trackpoint < <(echo "$output")
# Check to see if user made a selection
if (( ${#trackpoint[*]} < 2 )); then
# Error on no selection
no_selection
# Restart form
select_trackpoint
fi
# Set the name of the selected trackpoint
name="${trackpoint[0]}"
# Set the sysfs path of the selected trackpoint
sysfs_path="${trackpoint[1]}"
}
# Detect available trackpoints
trackpoint_detect () {
# Iterate over each mouse in /dev/input
for mouse in /dev/input/mouse*; do
# Get udev information of this mouse
mouse_info=$(udevadm info -n "$mouse")
# Check if this mouse is a INPUT_POINTINGSTICK
if [[ "$mouse_info" =~ "POINTINGSTICK=1" ]]; then
# Get the udev path of this mouse
path=$(echo "$mouse_info" | grep -E "^P: " | cut -b 4-)
# Set the sysfs path of this mouse's settings
sysfs_path="/sys$path/device/device"
# Set the name of this mouse
name=$(cat "/sys$path/device/name")
# Add the sysfs path and name of this mouse to array
trackpoint+=("$name")
trackpoint+=("$sysfs_path")
# Output to console that a trackpoint was detected
echo "$name detected at $mouse"
fi
done
# See if we had found any trackpoints
if (( ${#trackpoint[*]} > 1 )); then
# Set count of number of detected trackpoints
trackpoints=$((${#trackpoint[*]}/2))
# Output to console how many trackpoints were detected
echo "$trackpoints trackpoints detected"
# Test to see if we found more than one trackpoint
if (( trackpoints >= 2 )); then
select_trackpoint
fi
else
# If no trackpoints, error to console and GUI and exit.
echo "E: No trackpoints were detected."
zenity --info --title "Error" --width=200\
--text "No trackpoints were detected"
exit 1
fi
}
# Write settings to sysfs/udev
write_settings () {
# Generate a tempfile name
temp_file=$(tempfile)
# Create temp file as a bash script
echo "#!/bin/bash" > $temp_file
# Set excutable permissions on temp file
chmod +x $temp_file
# Number of settings to write
n=$((${#entry[*]}/2))
# Prompt to write config
zenity --question --title "$title" --width=400 --height=100\
--text "Write $n settings to udev rule for persistent setting?"
# If yes was selected
if [[ $? == 0 ]]; then
# Then write a udev rule
udev_rule=1
# The idea here was to use sed or such to modify existing rules
# however this is not implemented and the file will merely be
# overwritten as of right now, with any settings selected.
rules="ACTION==\"add\", SUBSYSTEM=\"input\", ATTR{name}==\"$name\""
# If no was selected
else
# Then don't write udev rule
udev_rule=0
fi
# Iterate over each setting in selection
for (( c=0; c<n; c++ )); do
# Setting to modify
setting=${entry[$c]}
# Value to write
value=${entry[(($c+1))]}
# This check verifies user did not change left col to invalid entry
if [[ "${settings[*]}" =~ $setting ]]; then
cat <<- EOF >> $temp_file
# Output to console what we're modifying
echo "Setting $setting ..."
# Add command to modify value to a temp file
echo "$value" > "$sysfs_path/$setting"
EOF
# If we are writing a rule
if (( udev_rule == 1 )); then
# Append the setting to the rules
rules+=", ATTR{device/$setting}=\"$value\""
fi
fi
done
# if we wrote a rule, then write it to file and reload the rules
if (( udev_rule == 1 )); then
# Write heredoc to a temp file
cat <<- EOF >> $temp_file
# Output to console that we're writing udev rules
echo "Writing udev rules ..."
# Create the rules file
echo "$rules" > "$rule_file"
# Reload the udev rules
udevadm control --reload-rules
EOF
fi
# Use polkit to execute the temp file as root to apply changes
pkexec $temp_file
# Cleanup temp file
rm $temp_file
}
# Detect trackpoints
trackpoint_detect
# Display config UI
config_form
# Write settings to sysfs/udev
write_settings