ConfigScriptEtc: wiimote_watch.2.3

File wiimote_watch.2.3, 3.8 kB (added by Djiboun, 5 years ago)

1. Gnome version. 2. Parse also user directory for config files

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 zenity 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#   gui elements (only allowed when a config file
19#   is specified on the commandline).
20# - Added --help usage screen.
21
22# Modified by Jean-Baptiste Cuenot
23# - Adapted for Gnome-based GUI elements.
24#   (Uses kdialog for KDE or zenity for Gnome:
25#   comment (or not) corresponding lines)
26# - Added user directory parsing for config files
27
28configParam=$1
29optionParam=$2
30
31GUIOffStr="-b" # "barebones"
32
33if [ "$optionParam" != "" -a "$optionParam" != "$GUIOffStr" -o "$configParam" = "--help" -o "$configParam" = "$GUIOffStr" ]; then
34        echo ""
35        echo "Wiimote Watch Script"
36        echo ""
37        echo "Usage:"
38        echo "  ./wiimote_watch.sh [[config] $GUIOffStr]"
39        echo ""
40        echo "config: Name of config file to use. If omitted,"
41        echo "        a menu will the allow user to"
42        echo "        select a config whenever a wiimote is detected."
43        echo ""
44        echo "$GUIOffStr:     Barebones. If config is specified, disables all"
45        echo "        GUI elements."
46        echo ""
47        exit 0
48fi
49
50declare -a myConfigFiles
51declare -a configList
52declare -a hctscan
53declare -a wiimote
54
55echo "started on `date`" >> /tmp/wiimote_watch.log
56
57if [ "$optionParam" != "$GUIOffStr" ]; then
58        # directory to look for config files in.
59        cd /usr/local/etc/cwiid/wminput
60
61        # add all files in the directory above to an array
62        for file in *
63        do
64                myConfigFiles[${#myConfigFiles[@]}]="$file"
65        done
66       
67        # user directory to look for config files in.
68        cd ~/.cwiid/wminput
69
70        # add all files in the directory above to an array
71        for file in *
72        do
73                myConfigFiles[${#myConfigFiles[@]}]="${file}"
74        done
75
76        # KDE : do some ugly stuff so kdialog behaves how we want
77        #for ((  i=0 ;  i <= ${#myConfigFiles[@]} * 2 - 2; i++  ))
78        #do
79        #       configList[i*2+1]=${myConfigFiles[i]}
80        #       configList[i*2]=${myConfigFiles[i]}
81        #done
82fi
83
84# main loop
85# look for a wiimote. When one is found, ask the user which config file to use then start wminput
86while true
87do
88        #kdialog --passivepopup "Put Wiimote in discoverable mode now (press 1+2)..." 5 & # KDE
89        zenity --info --text="Put Wiimote in discoverable mode now (press 1+2)..." # Gnome
90        hctscn=`/usr/bin/hcitool scan | grep Nintendo`
91       
92        if [ "`echo ${hctscn[@]}`" ]; then
93               
94                # parse  the output output of hcitool scan
95                n=0
96                for i in `echo ${hctscn} | tr '.' '\n'` ; do
97                        str=${str},${i}
98                        let n=$n+1
99                        wiimote[${n}]=${i}
100                done
101
102                if [ "$optionParam" != "$GUIOffStr" ]; then
103                        #kdialog --passivepopup "Wiimote Found at ${wiimote[1]}." 5 & # KDE
104                        zenity --info --text="Wiimote Found at ${wiimote[1]}." # Gnome
105                fi
106
107                if [ "$configParam" = "" ]; then
108                        #whichConfig=`kdialog --menu "Select Which config file to use:" ${configList[@]}` # KDE
109                        whichConfig=`zenity --list --column="Config files" ${myConfigFiles[@]}` # Gnome
110                        if [ "$whichConfig" = "" ]; then
111                                exit 0
112                        fi
113                else
114                        whichConfig=$configParam
115                fi
116                echo "Starting wminput for wiimote with blutooth address ${wiimote[1]} and configuration file $whichConfig" >> /tmp/wiimote_watch.log
117               
118                wminstatus="`/usr/local/bin/wminput -c $whichConfig ${wiimote[1]}`"
119               
120                echo "Wiimote disconnected." >> /tmp/wiimote_watch.log
121                if [ "$optionParam" != "$GUIOffStr" ]; then
122                        #kdialog --passivepopup "Wiimote was Disconnected." 5 & # KDE
123                        zenity --info --text="Wiimote was Disconnected." # Gnome
124                fi
125        fi
126        # My system acts up if I don't pause
127        # before doing an hcitool scan continually
128        # you might not need this. Comment it out if you don't.
129        sleep 10
130done
131
132exit 0