aboutsummaryrefslogtreecommitdiff
path: root/misc/cwiid/rc.cwiid.new
blob: 3efb8e43349deb6b91153e713cdd257e988d2b13 (plain)
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
#!/bin/sh

# RC script for cwiid's wminput daemon, part of the SBo cwiid package.

# Where's the config file for this script? This is the only variable that can't
# be overridden in the config file.
RC_CONF=/etc/rc.d/rc.cwiid.conf

####
# Override this stuff in $RC_CONF if you see a need to:

# Default config file for wminput instances
WMINPUT_DEFAULT_CONF=default

# full path to wminput binary:
WMINPUT_BIN=/usr/bin/wminput

# probably nobody will ever need to increase this:
MAX_WIIMOTES=9

####

start_cwiid() {
	local cwiid_conf
	local cwiid_addr
	local cmd
	local daemons

	echo "Starting cwiid wminput daemon(s)..."

	# Guarantee the uinput module gets loaded, don't complain if not
	# (it may not be modular in the running kernel)
	/sbin/modprobe uinput &>/dev/null

	# WIIMOTE is a bash array, will be populated by config file or default conf
	unset WIIMOTE

	if [ -r "$RC_CONF" ]; then
		source $RC_CONF
	else
		echo -e "$RC_CONF missing, using defaults:\\n\\tWIIMOTE[0]='auto'\\n\\tWIIMOTE_CONF[0]='$WMINPUT_DEFAULT_CONF'" 1>&2
		WIIMOTE[0]=auto
	fi

	i=0
	daemons=0
	while [ $i -le $MAX_WIIMOTES ]; do
		if [ -n "${WIIMOTE[$i]}" ]; then
			cwiid_conf="${WIIMOTE_CONF[$i]:-$WMINPUT_DEFAULT_CONF}"
			cwiid_addr=${WIIMOTE[$i]}

			if [ "$cwiid_addr" = "auto" ]; then
				cwiid_addr=""
			fi

			cmd="$WMINPUT_BIN -d -c $cwiid_conf $cwiid_addr"
			echo "  Starting daemon: $cmd"
			$cmd &

			daemons=$((daemons+1))
		fi

		i=$((i+1))
	done
	echo "cwiid startup done, $daemons daemons started"
}

stop_cwiid() {
	echo -n "Stopping cwiid wminput daemon(s)..."
	killall wminput &>/dev/null
	sleep 2
	killall -9 wminput &>/dev/null
	echo "done"
}

case "$1" in
	start) start_cwiid ;;
	stop) stop_cwiid ;;
	restart)
		stop_cwiid
		sleep 1
		start_cwiid
		;;
	*)
		echo "Usage: $0 start|stop|restart" 1>&2
		exit 1
		;;
esac
exit 0