Difference between revisions of "Loader:CWiid Daemon Alternative"

From Wah!ki
Jump to navigation Jump to search
 
Line 1: Line 1:
 
<pre>
 
<pre>
#!/bin/bash
+
    #!/bin/bash
  
$Wiimote_Keymap = ir_wahcade
+
    wii_keymap="ir_wahcade"
 +
    file=$HOME/.wahcade/wiimote-mac
  
# wiimote-mac is the name of the file with the mac address
+
    declare -a bd_address
  
if [ -f wiimote-mac ]; then # if file exists
+
    # wiimote-mac is the name of the file with the mac address
    MAC_ADDRESS=$(cat wiimote-mac) # load mac address into the variable
+
    # search wiimotes with hcitool
 +
    zenity --info --title "Wiimote Pairing" --text "Getting Wiimote Address." --timeout 5 &
 +
    if [[ -f $file ]]; then # if file exists
 +
      IFS=$'\n' read -r -d '' -a bd_address < $file
 +
      while read -r bd_address; do
 +
          wminput -d -c "$wii_keymap" "$bd_address" &
 +
          printf '%s\n' "$bd_address" >> "$file" || break
 +
      done < <(comm -23 <(hcitool scan|awk '/Nintendo/{print $1}'|sort -u) <(sort -u "$file"))
 +
      else
 +
           
 +
      # Only Write to the configuration file if there is a Wiimote Address aquired
 +
      if [[ -n "$bd_address" ]]; then
 +
          printf '%s\n' "${bd_address[@]}" > $file
 +
      fi
 +
      #if no Wiimote is found, exit with an error
 +
      if test ${#bd_address[*]} -eq 0; then
 +
          zenity --info --title "Wiimote Pairing" --text "No wiimotes found." --timeout 5 &
 +
          exit 0
 +
      fi
 +
    fi
  
    if [ ! -z "$MAC_ADDRESS" ]; then # if variable not empty
 
        echo "We already have a mac address configured: $MAC_ADDRESS"
 
        # run commands here
 
  wminput -d -c $Wiimote_Keymap $MAC_ADDRESS
 
        exit
 
    fi
 
fi
 
  
# We only fall here if the mac address is not configured...
+
    # killing wminput -d instances
# Configure the mac address, assign it to $MAC_ADDRESS and then store it inside
+
    killall -q wminput
# the file
+
 
wminput -c $Wiimote_Keymap
+
    # start wminput for each macaddress
MAC_ADDRESS=$REPLY
+
    zenity --info --title "Wiimote Pairing" --text "Put the ${#bd_address[*]} Wiimotes in Discovery Mode." --timeout 5 &
echo $MAC_ADDRESS > wiimote-mac
+
    for m in "${bd_address[@]}"; do
 +
      wminput -d -c $wii_keymap $m &
 +
    done
 
</pre>
 
</pre>

Latest revision as of 07:34, 9 February 2011

    #!/bin/bash

    wii_keymap="ir_wahcade"
    file=$HOME/.wahcade/wiimote-mac

    declare -a bd_address

    # wiimote-mac is the name of the file with the mac address
    # search wiimotes with hcitool
    zenity --info --title "Wiimote Pairing" --text "Getting Wiimote Address." --timeout 5 &
    if [[ -f $file ]]; then # if file exists
       IFS=$'\n' read -r -d '' -a bd_address < $file
       while read -r bd_address; do
          wminput -d -c "$wii_keymap" "$bd_address" &
          printf '%s\n' "$bd_address" >> "$file" || break
       done < <(comm -23 <(hcitool scan|awk '/Nintendo/{print $1}'|sort -u) <(sort -u "$file"))
       else
             
       # Only Write to the configuration file if there is a Wiimote Address aquired
       if [[ -n "$bd_address" ]]; then
          printf '%s\n' "${bd_address[@]}" > $file
       fi
       #if no Wiimote is found, exit with an error
       if test ${#bd_address[*]} -eq 0; then
          zenity --info --title "Wiimote Pairing" --text "No wiimotes found." --timeout 5 &
          exit 0
       fi
    fi


    # killing wminput -d instances
    killall -q wminput

    # start wminput for each macaddress
    zenity --info --title "Wiimote Pairing" --text "Put the ${#bd_address[*]} Wiimotes in Discovery Mode." --timeout 5 &
    for m in "${bd_address[@]}"; do
       wminput -d -c $wii_keymap $m &
    done