ConfigScriptEtc: wiimote_watch.2.sh

File wiimote_watch.2.sh, 2.9 kB (added by Abscissa, 3 years ago)

Enhanced version of nickishappy's wiimote_watch.sh script: 1. Optionally allows a config file to be specified on the command line instead of through the menu that normally appears when a wiimote is found (this also eliminates the problem I had where I needed to hit 1+2 twice: once to get to the config menu, and again after choosing a config). 2. KDE-based UI can be optinally disabled (but only if a config file is specified on the command line). 3. Usage instructions are displayed with param --help

Line 
1#!/bin/bash
2
3# This script was written by Nick Wilbanks
4# and is released under the GPL v2
5# it was inspired by wiimote_connect.rb
6# written by Heavybell.
7# Some things aren't done properly
8# so use at your own risk!
9
10# requires kdialog to function properly
11# (unless you specify a config on the commandline
12# and use the -b barebones flag)
13
14# Modified by Nick Sabalausky:
15# - Allow a config file to be specified
16#   on the command line.
17# - Added -b barebones flag to disable kde-based
18#   menu and notifications (only allowed when
19#   a config file is specified on the commandline).
20
21configParam=$1
22optionParam=$2
23
24kdeOffStr="-b" #"barebones"
25
26if [ "$optionParam" != "" -a "$optionParam" != "$kdeOffStr" -o "$configParam" = "--help" -o "$configParam" = "$kdeOffStr" ]; then
27        echo ""
28        echo "Wiimote Watch Script"
29        echo ""
30        echo "Usage:"
31        echo "  ./wiimote_watch.sh [[config] -b]"
32        echo ""
33        echo "config: Name of config file to use. If omitted,"
34        echo "        a KDE-based menu will the allow user to"
35        echo "        select a config whenever a wiimote is detected."
36        echo ""
37        echo "-b:     Barebones. If config is specified, disables all"
38        echo "        KDE-based gui elements."
39        echo ""
40        exit 0
41fi
42
43declare -a myConfigFiles
44declare -a configList
45declare -a hctscan
46declare -a wiimote
47
48echo "started on `date`" >> /tmp/wiimote_watch.log
49
50if [ "$optionParam" != "$kdeOffStr" ]; then
51        # directory to look for config files in.
52        cd /usr/local/etc/cwiid/wminput
53
54        # add all files in the directory above to an array
55        for file in *
56        do
57                myConfigFiles[${#myConfigFiles[@]}]="$file"
58        done
59
60        # do some ugly stuff so kdialog behaves how we want
61        for ((  i=0 ;  i <= ${#myConfigFiles[@]} * 2 - 2; i++  ))
62        do
63                configList[i*2+1]=${myConfigFiles[i]}
64                configList[i*2]=${myConfigFiles[i]}
65        done
66fi
67
68# main loop
69# look for a wiimote. When one is found, ask the user which config file to use then start wminput
70while true
71do
72        hctscn=`/usr/bin/hcitool scan | grep Nintendo`
73        if [ "`echo ${hctscn[@]}`" ]; then
74                # parse  the output output of hcitool scan
75                n=0
76                for i in `echo ${hctscn} | tr '.' '\n'` ; do
77                        str=${str},${i}
78                        let n=$n+1
79                        wiimote[${n}]=${i}
80                done
81
82                if [ "$optionParam" != "$kdeOffStr" ]; then
83                        kdialog --passivepopup "Wiimote Found at ${wiimote[1]}." 5 &
84                fi
85
86                if [ "$configParam" = "" ]; then
87                        whichConfig=`kdialog --menu "Select Which config file to use:" ${configList[@]}`
88                else
89                        whichConfig=$configParam
90                fi
91                echo "Starting wminput for wiimote with blutooth address ${wiimote[1]} and configuration file $whichConfig" >> /tmp/wiimote_watch.log
92                wminstatus="`/usr/local/bin/wminput -c $whichConfig ${wiimote[1]}`"
93
94                if [ "$optionParam" != "$kdeOffStr" ]; then
95                        echo "Wiimote disconnected." >> /tmp/wiimote_watch.log
96                        kdialog --passivepopup "Wiimote was Disconnected." 5 &
97        fi
98        fi
99        # My system acts up if I don't pause
100        # before doing an hcitool scan continually
101        # you might not need this. Comment it out if you don't.
102        sleep 10
103done
104
105exit 0