## Saturday, December 5, 2009

### Using sockets to run VLC in the background or across computers Part 2

This is the "vlc.do" script. This is the main way to interact with vlc once you created a socket.
I also created a few other small "shortcut" scripts that are wrappers to vlc.do to make common tasks easier (such as adding files, pausing, etc)

Lets set up the defaults for options we will be using later
 #!/usr/bin/env sh cmd_to_tell=""; x_mode=""; optdir=~/.vlc_extra; 
In vlc_create_sock we put the location of the socket into $optdir/socket. Here we retrieve that location. Keep in mind that we don't look into$optdir/sockets/ directly. I did this because of cleanup problems (the socket file was not always removed when exiting)
 socket="$(cat$optdir/socket_name)"; 
We have three options:
-c "command"  This is the command we will be passing to vlc. Try "help" to see them all.
 -o dir The optdir. If you want to work on a remote computer or another user's account try changing this.
-x The script uses Xdialog to notify you of what actions took place. This is useful where you map a function to a key on your keyboard and you want visual notification that something took place.
 while getopts c:o:x option do case "$option" in 'c') cmd_to_tell="$OPTARG";; 'o') optdir="$OPTARG";; 'x') x_mode="on";; esac done  Here we check to see if you actually told us anything to do - a simple sanity check.  if [ -z "$cmd_to_tell" ] then if [ -n "$x_mode" ] then Xdialog --icon ~/bin/icons/warning.xpm --infobox "Missing a cmd!" 0 0 2000; else echo "Your missing a command..." >/dev/stderr; fi return 1; fi  Here I check to see if the socket actually exists. If it does not exist then I assume VLC is not running and I warn you via Xdialog (if -x is set).  ## if no opts give entire line not just cmd_to_tell if [ -e "$socket" ] then 
Here is the crucial part. nc or netcat has a -U option which lets you write to sockets. VLC will react to any command supplied to it via this file.
 echo "$cmd_to_tell"|nc -U$socket if [ -n "$x_mode" ] then Xdialog --title "VLC Remote control" --backtitle "I told vlc to " --no-buttons --icon ~/bin/icons/media-cdrom.xpm --infobox "$cmd_to_tell" 0 0 3000 fi else if [ -n "\$x_mode" ] then Xdialog --icon ~/bin/icons/warning.xpm --infobox "VLC not running!" 0 0 2000; fi return 1; fi