Lines 1-433
Link Here
|
1 |
#!/bin/sh |
|
|
2 |
|
3 |
# PROVIDE: wacom |
4 |
# REQUIRE: FILESYSTEMS |
5 |
# |
6 |
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf |
7 |
# to enable this service: |
8 |
# |
9 |
# wacom_enable (bool): Set to NO by default. |
10 |
# Set it to YES to enable wacom. |
11 |
# wacom_types (list): Set to "stylus eraser cursor pad touch" by |
12 |
# default. A list of wacom profiles to set up. |
13 |
# wacom_porttype (enum): Set to "%%PORTTYPE%%" by default. |
14 |
# Set this to usb or serial. |
15 |
# |
16 |
# This script additionally offers setup and cleanup to configure Xorg |
17 |
# for use of the driver or remove the configuration settings. |
18 |
# The input devices are only added to the first ServerLayout section |
19 |
# and also only removed once. |
20 |
# In USB mode the file /boot/loader.conf is also adjusted. |
21 |
# |
22 |
|
23 |
. /etc/rc.subr |
24 |
|
25 |
name="wacom" |
26 |
rcvar=wacom_enable |
27 |
|
28 |
extra_commands="setup cleanup" |
29 |
setup_cmd=do_setup |
30 |
start_cmd=do_start |
31 |
stop_cmd=do_stop |
32 |
cleanup_cmd=do_cleanup |
33 |
|
34 |
# |
35 |
# Outputs the location of the Xorg configuration file. |
36 |
# Returns 1 if no file could be found. |
37 |
# |
38 |
get_xorg_conf() { |
39 |
local config_locations config |
40 |
|
41 |
# Possible Xorg configuration file locations, taken from the |
42 |
# xorg.conf(5) manual page. |
43 |
config_locations=" |
44 |
/etc/X11/$XORGCONFIG |
45 |
%%PREFIX%%/etc/X11/$XORGCONFIG |
46 |
/etc/X11/xorg.conf-4 |
47 |
/etc/X11/xorg.conf |
48 |
/etc/xorg.conf |
49 |
%%PREFIX%%/etc/X11/xorg.conf.$HOST |
50 |
%%PREFIX%%/etc/X11/xorg.conf-4 |
51 |
%%PREFIX%%/etc/X11/xorg.conf |
52 |
%%PREFIX%%/lib/X11/xorg.conf.$HOST |
53 |
%%PREFIX%%/lib/X11/xorg.conf-4 |
54 |
%%PREFIX%%/lib/X11/xorg.conf |
55 |
NONE |
56 |
" |
57 |
|
58 |
# Find the first matching config file. |
59 |
for config in $config_locations; { |
60 |
test -f "$config" && break |
61 |
} |
62 |
|
63 |
if [ "$config" = "NONE" ]; then |
64 |
echo "No Xorg configuration has been found." 1>&2 |
65 |
return 1 |
66 |
fi |
67 |
|
68 |
echo "$config" |
69 |
return 0 |
70 |
} |
71 |
|
72 |
# |
73 |
# Returns the line number of the first line matching the extended regular |
74 |
# expression $2 in the file $1. |
75 |
# |
76 |
# @param $1 |
77 |
# The file to get the line number from. |
78 |
# @param $2 |
79 |
# An extend regular expression. |
80 |
# @stdout |
81 |
# The line number of the first line matching $2. |
82 |
# |
83 |
get_first() { |
84 |
local result IFS |
85 |
IFS=' |
86 |
' |
87 |
result="$(/usr/bin/grep -Ein "$2" "$1")" |
88 |
result="${result%%:*}" |
89 |
|
90 |
# No match. |
91 |
if [ -z "$result" ]; then |
92 |
return 1 |
93 |
fi |
94 |
|
95 |
echo "$result" |
96 |
return 0 |
97 |
} |
98 |
|
99 |
# |
100 |
# Returns the line numbers of lines matching the extended regular |
101 |
# expression $2 in the file $1. |
102 |
# |
103 |
# @param $1 |
104 |
# The file to get the line numbers from. |
105 |
# @param $2 |
106 |
# An extend regular expression. |
107 |
# @stdout |
108 |
# The line numbers of lines matching $2. |
109 |
# |
110 |
get_all() { |
111 |
local entry result IFS |
112 |
IFS=' |
113 |
' |
114 |
|
115 |
result="$(/usr/bin/grep -Ein "$2" "$1")" |
116 |
|
117 |
# No match. |
118 |
if [ -z "$result" ]; then |
119 |
return 1 |
120 |
fi |
121 |
|
122 |
for entry in $result; { |
123 |
echo "${entry%%:*}" |
124 |
} |
125 |
} |
126 |
|
127 |
# |
128 |
# Returns the line number of the line before line number $2 that matches the |
129 |
# extended regular expression $3 in the file $1. |
130 |
# |
131 |
# @param $1 |
132 |
# The file to get the line number from. |
133 |
# @param $2 |
134 |
# The line before which the expression should match. |
135 |
# @param $3 |
136 |
# An extend regular expression. |
137 |
# @stdout |
138 |
# The line number of the first line before line $2 matching $3. |
139 |
# |
140 |
get_before() { |
141 |
local result IFS line length |
142 |
IFS=' |
143 |
' |
144 |
|
145 |
# The length is necessary to make sure the check is run once |
146 |
# again for the last match. |
147 |
length="$(/usr/bin/wc -l "$1")" |
148 |
length=${length% *} |
149 |
|
150 |
result=-1 |
151 |
for line in $(/usr/bin/grep -Ein "$3" "$1") $(($length + 1)); { |
152 |
line="${line%%:*}" |
153 |
|
154 |
# We have passed the line to look for. |
155 |
if [ $line -ge $2 ]; then |
156 |
|
157 |
# No match before this line. |
158 |
if [ $result -lt 0 ]; then |
159 |
return 1 |
160 |
fi |
161 |
|
162 |
echo "$result" |
163 |
return 0 |
164 |
fi |
165 |
|
166 |
result=$line |
167 |
} |
168 |
|
169 |
# No result. |
170 |
return 1 |
171 |
} |
172 |
|
173 |
# |
174 |
# Returns the line number of the line behind line number $2 that matches the |
175 |
# extended regular expression $3 in the file $1. |
176 |
# |
177 |
# @param $1 |
178 |
# The file to get the line number from. |
179 |
# @param $2 |
180 |
# The line behind which the expression should match. |
181 |
# @param $3 |
182 |
# An extend regular expression. |
183 |
# @stdout |
184 |
# The line number of the first line behind line $2 matching $3. |
185 |
# |
186 |
get_behind() { |
187 |
local IFS line |
188 |
IFS=' |
189 |
' |
190 |
|
191 |
for line in $(/usr/bin/grep -Ein "$3" "$1"); { |
192 |
line="${line%%:*}" |
193 |
|
194 |
# We have passed the line to look for. |
195 |
if [ $line -gt $2 ]; then |
196 |
echo "$line" |
197 |
return 0 |
198 |
fi |
199 |
} |
200 |
|
201 |
# No result. |
202 |
return 1 |
203 |
} |
204 |
|
205 |
# |
206 |
# Inserts a line at the end of a section of an Xorg configuration file. |
207 |
# The insertion is done on all matching sections! |
208 |
# |
209 |
section_insert_line() { |
210 |
local file section insert sections begin end length |
211 |
file="$1" |
212 |
section="$2" |
213 |
insert="$3" |
214 |
|
215 |
# Find the beginning of the section. |
216 |
# Start with the last section to avoid moving sections around |
217 |
# before something is inserted into them. |
218 |
sections="$(get_all "$file" \ |
219 |
"^[[:space:]]*Section[[:space:]]+\"$section\"" \ |
220 |
| /usr/bin/sort -nr |
221 |
)" |
222 |
|
223 |
if [ -z "$sections" ]; then |
224 |
echo "Identifying section $section has failed." 1>&2 |
225 |
return 1 |
226 |
fi |
227 |
|
228 |
for begin in $sections; { |
229 |
|
230 |
# Find the end of the section. |
231 |
end="$(get_behind "$file" "$begin" "^[[:space:]]*EndSection")" |
232 |
|
233 |
if [ -z "$end" ]; then |
234 |
echo "The section $section($begin) is not closed." 1>&2 |
235 |
return 2 |
236 |
fi |
237 |
|
238 |
# Determine the length of the configuration file. |
239 |
length="$(/usr/bin/wc -l "$file")" |
240 |
length=${length% *} |
241 |
|
242 |
# Insert the line. |
243 |
/bin/cp "$file" "$file.$$" |
244 |
|
245 |
/usr/bin/head -n$(($end - 1)) "$file.$$" > "$file" |
246 |
echo "$insert" >> "$file" |
247 |
/usr/bin/tail -n$(($length - $end + 1)) "$file.$$" >> "$file" |
248 |
|
249 |
/bin/rm "$file.$$" |
250 |
} |
251 |
} |
252 |
|
253 |
# |
254 |
# Adds the necessary lines to the Xorg configuration. |
255 |
# |
256 |
do_setup() { |
257 |
local config ident status |
258 |
|
259 |
# Get the Xorg configuration file. |
260 |
config="$(get_xorg_conf)" |
261 |
status=$? |
262 |
test $status -ne 0 && return $status |
263 |
|
264 |
echo "Setting up configuration in $config." |
265 |
|
266 |
# Add all the necessary sections. |
267 |
for ident in $(eval "echo \${${name}_types}"); { |
268 |
if /usr/bin/grep -Eqi "^[[:space:]]*Identifier[[:space:]]+\"$ident\"" "$config"; then |
269 |
echo "Skipping $ident, because it already exists in $config." |
270 |
continue |
271 |
fi |
272 |
|
273 |
echo "Inserting $ident." |
274 |
|
275 |
echo "Section \"InputDevice\" |
276 |
Driver \"wacom\" |
277 |
Identifier \"$ident\" |
278 |
Option \"Type\" \"$ident\"" >> "$config" |
279 |
|
280 |
if [ "$(eval "echo \${${name}_porttype}")" = "usb" ]; then |
281 |
echo " Option \"Device\" \"/dev/event0\" |
282 |
Option \"USB\" \"on\"" >> "$config" |
283 |
else |
284 |
echo " Option \"Device\" \"/dev/ttyd0\" |
285 |
Option \"ForceDevice\" \"ISDV4\"" >> "$config" |
286 |
fi |
287 |
|
288 |
echo "EndSection |
289 |
" >> "$config" |
290 |
|
291 |
section_insert_line "$config" "ServerLayout" " InputDevice \"$ident\" \"SendCoreEvents\"" |
292 |
} |
293 |
|
294 |
if [ "$(eval "echo \${${name}_porttype}")" = "usb" ]; then |
295 |
if ! /usr/bin/grep -qx 'uwacom_load="YES"' \ |
296 |
/boot/loader.conf; then |
297 |
echo "Setting up /boot/loader.conf" |
298 |
/bin/cp /boot/loader.conf /boot/loader.conf.orig |
299 |
echo 'uwacom_load="YES"' >> /boot/loader.conf |
300 |
fi |
301 |
fi |
302 |
} |
303 |
|
304 |
# |
305 |
# Removes all wacom stuff from the Xorg configuration file. Beware, |
306 |
# this also affects handwritten content. |
307 |
# |
308 |
do_cleanup() { |
309 |
local config status driver i begin last_begin end length ident IFS |
310 |
|
311 |
# Get the Xorg configuration file. |
312 |
config="$(get_xorg_conf)" |
313 |
status=$? |
314 |
test $status -ne 0 && return $status |
315 |
|
316 |
echo "Removing wacom entries from $config." |
317 |
|
318 |
IFS=' |
319 |
' |
320 |
while true; do |
321 |
# Find a wacom section. |
322 |
driver="$(get_first "$config" \ |
323 |
"^[[:space:]]*Driver[[:space:]]+\"wacom\"" \ |
324 |
)" |
325 |
|
326 |
# Not a numeric, no more wacom sections left. |
327 |
test -z "$driver" && break |
328 |
|
329 |
# Find the beginning of the section. |
330 |
begin="$(get_before "$config" "$driver" \ |
331 |
"^[[:space:]]*Section[[:space:]]+\"InputDevice\"" \ |
332 |
)" |
333 |
|
334 |
if [ -z "$begin" ]; then |
335 |
echo "Beginning of wacom driver section" \ |
336 |
"could not be found!" 1>&2 |
337 |
return 1 |
338 |
fi |
339 |
|
340 |
# Find the end of the section. |
341 |
end="$(get_behind "$config" "$driver" \ |
342 |
"^[[:space:]]*EndSection" \ |
343 |
)" |
344 |
|
345 |
if [ -z "$end" ]; then |
346 |
echo "The Wacom driver section is" \ |
347 |
"not closed properly." 1>&2 |
348 |
return 2 |
349 |
fi |
350 |
|
351 |
# Determine the length of the configuration file. |
352 |
length="$(/usr/bin/wc -l "$config")" |
353 |
length=${length% *} |
354 |
|
355 |
# Adjust the end by trailing newlines. |
356 |
while [ $end -lt $length -a \ |
357 |
-z "$(/usr/bin/tail -n$(($length - $end)) "$config" \ |
358 |
| /usr/bin/head -n1)" ]; do |
359 |
end=$(($end + 1)) |
360 |
done |
361 |
|
362 |
# Remove the section. |
363 |
/bin/cp "$config" "$config.$$" |
364 |
|
365 |
/usr/bin/head -n$(($begin - 1)) "$config.$$" > "$config" |
366 |
/usr/bin/tail -n$(($length - $end)) "$config.$$" >> "$config" |
367 |
|
368 |
/bin/rm "$config.$$" |
369 |
done |
370 |
IFS=' |
371 |
' |
372 |
|
373 |
echo "Cleaning up ServerLayout section." |
374 |
|
375 |
for ident in $(eval "echo \${${name}_types}"); { |
376 |
while true; do |
377 |
driver="$(get_first "$config" \ |
378 |
"[[:space:]]*InputDevice[[:space:]]+\"$ident\"" \ |
379 |
)" |
380 |
|
381 |
# There is no line matching this driver. |
382 |
test -z "$driver" && break |
383 |
|
384 |
# Determine the length of the configuration file. |
385 |
length="$(/usr/bin/wc -l "$config")" |
386 |
length=${length% *} |
387 |
|
388 |
# Remove the section. |
389 |
/bin/cp "$config" "$config.$$" |
390 |
|
391 |
/usr/bin/head -n$(($driver - 1)) "$config.$$" > "$config" |
392 |
/usr/bin/tail -n$(($length - $driver)) "$config.$$" >> "$config" |
393 |
|
394 |
/bin/rm "$config.$$" |
395 |
done |
396 |
} |
397 |
|
398 |
if [ "$(eval "echo \${${name}_porttype}")" = "usb" ]; then |
399 |
if /usr/bin/grep -qx 'uwacom_load="YES"' \ |
400 |
/boot/loader.conf; then |
401 |
echo "Cleaning up /boot/loader.conf" |
402 |
/bin/cp /boot/loader.conf /boot/loader.conf.orig |
403 |
/usr/bin/grep -xv 'uwacom_load="YES"' \ |
404 |
/boot/loader.conf.orig > /boot/loader.conf |
405 |
fi |
406 |
fi |
407 |
|
408 |
} |
409 |
|
410 |
do_start() { |
411 |
if /sbin/kldstat | /usr/bin/grep -q uwacom; then |
412 |
return 0 |
413 |
fi |
414 |
echo "Starting ${name}." |
415 |
/sbin/kldload %%KMODDIR%%/uwacom.ko |
416 |
} |
417 |
|
418 |
do_stop() { |
419 |
if ! /sbin/kldstat | /usr/bin/grep -q uwacom; then |
420 |
echo "${name} is not running." |
421 |
return 0 |
422 |
fi |
423 |
echo "Stopping ${name}." |
424 |
/sbin/kldunload %%KMODDIR%%/uwacom.ko |
425 |
} |
426 |
|
427 |
load_rc_config $name |
428 |
|
429 |
eval ": \${${name}_enable=\"NO\"}" |
430 |
eval ": \${${name}_types=\"stylus eraser cursor pad touch\"}" |
431 |
eval ": \${${name}_porttype=\"%%PORTTYPE%%\"}" |
432 |
|
433 |
run_rc_command "$1" |