2010-01-18, 13:06
I'm posting this up here for the benefit of other people who have family members (wives, etc.) that would like to use a XBMC-ified AppleTV - without the family member who originally set it up having to be there all the time
Disclaimer: I'm probably not going to answer questions about this. If the information I provide isn't enough for you to get things done, then this tutorial isn't for you.
1.) Install vim for AppleTV. Either copy it from an Intel Mac's /usr/bin or grab it from http://wiki.awkwardtv.org/wiki/Vim . Make sure to shove it in /usr/bin on the AppleTV, chmod +x it, and link 'vi' to it while you're there (ln -s /usr/bin/vim /usr/bin/vi).
2.) While ssh'd into the AppleTV, run: sudo visudo (ah, now the need for vim is explained - this will fail without it). Use it to add the following line to the end:
What It Does: This line enables XBMC / scripts to run the same shutdown command plugins like NitoTV use to halt your AppleTV, without having to type in an admin password / authentication.
3.) Put the following script into a new file: /Users/frontrow/Library/Application Support/XBMC/shutdown.py
IMPORTANT NOTE: If you're not familiar with python, whitespace (like indentation) is IMPORTANT / critical to your script actually working. Please don't type the above text, just copy and paste it - then edit it later if necessary.
What It Does: This script does three nifty tricks. Firstly, the os.fork stuff at the bottom is an old python trick to create an orphaned session. Why is this useful? It lets us spawn a process that, even if XBMC quits, it will continue to run. Secondly, the os.system stuff at the top is another python trick for running bash commands from within python. And lastly, the payload of os.system is (in plain English): wait 2 seconds, eject the disk likely connected to the USB jack, wait 1 second, then halt/shutdown the AppleTV (still need to power it off, mind you, once it halts).
4.) Modify the functionality of the Shutdown button in the default AppleTV skin, located in this file: /Users/frontrow/Applications/XBMC.app/Contents/Resources/XBMC/skin/PM3.HD/720p/DialogButtonMenu.xml
The line you want to remove looks like this:
You want to replace it with TWO lines:
What It Does: This change replaces the Shutdown button's normal action (which is XBMC.Shutdown - which on AppleTV is the same as XBMC.Quit) with a line that calls our new python script and then calls XBMC.Quit (yes, you can stack commands like this - did you know?).
------------------
Summary Explanation:
I wrote things this way because I was having a horrible, horrible time trying to get System.Exec to work correctly on the AppleTV. In addition, I wanted to quit XBMC cleanly - and still somehow trigger the shutdown - yet if I tried to do that through System.Exec, I never would have gotten XBMC to quit cleanly because it'd still be trying to shutdown first.
This script is a cheat and a trick - by changing the .xml so that it gets called when someone clicks "Shutdown", it spawns an orphaned process that basically just runs the shell commands I'd want it to run: wait a bit (while XBMC quits), eject the disk if it's there (regardless of the name), wait a second more, then shutdown.
On my system, using this method, pressing the button quits XBMC, cleanly unmounts the USB drive I have attached, then shuts down the AppleTV. Total shutdown is usually within 10 seconds of pressing the button (the AppleTV takes a while to shut down because it's trying to kill that orphaned process we create - this is ok). So I tell my wife to just wait until the blue screen appears / AppleTV picture goes away - then she can turn off the power strip that the AppleTV is connected to.
------------------
Hope this helps and encourages others to tell System.Exec to take a flying leap off a cliff and to try using python to solve some of your peskier problems
Disclaimer: I'm probably not going to answer questions about this. If the information I provide isn't enough for you to get things done, then this tutorial isn't for you.
1.) Install vim for AppleTV. Either copy it from an Intel Mac's /usr/bin or grab it from http://wiki.awkwardtv.org/wiki/Vim . Make sure to shove it in /usr/bin on the AppleTV, chmod +x it, and link 'vi' to it while you're there (ln -s /usr/bin/vim /usr/bin/vi).
2.) While ssh'd into the AppleTV, run: sudo visudo (ah, now the need for vim is explained - this will fail without it). Use it to add the following line to the end:
Code:
frontrow ALL=NOPASSWD: /sbin/shutdown -h now
What It Does: This line enables XBMC / scripts to run the same shutdown command plugins like NitoTV use to halt your AppleTV, without having to type in an admin password / authentication.
3.) Put the following script into a new file: /Users/frontrow/Library/Application Support/XBMC/shutdown.py
Code:
import os
def main():
os.system("sleep 2; /usr/sbin/diskutil eject /dev/disk1; sleep 1; /usr/bin/sudo /sbin/shutdown -h now")
pid = os.fork()
if pid == 0:
os.setsid()
pid = os.fork()
if pid == 0:
main()
IMPORTANT NOTE: If you're not familiar with python, whitespace (like indentation) is IMPORTANT / critical to your script actually working. Please don't type the above text, just copy and paste it - then edit it later if necessary.
What It Does: This script does three nifty tricks. Firstly, the os.fork stuff at the bottom is an old python trick to create an orphaned session. Why is this useful? It lets us spawn a process that, even if XBMC quits, it will continue to run. Secondly, the os.system stuff at the top is another python trick for running bash commands from within python. And lastly, the payload of os.system is (in plain English): wait 2 seconds, eject the disk likely connected to the USB jack, wait 1 second, then halt/shutdown the AppleTV (still need to power it off, mind you, once it halts).
4.) Modify the functionality of the Shutdown button in the default AppleTV skin, located in this file: /Users/frontrow/Applications/XBMC.app/Contents/Resources/XBMC/skin/PM3.HD/720p/DialogButtonMenu.xml
The line you want to remove looks like this:
Code:
<onclick>XBMC.Shutdown()</onclick>
You want to replace it with TWO lines:
Code:
<onclick>XBMC.RunScript(/Users/frontrow/Library/Application Support/XBMC/shutdown.py)</onclick>
<onclick>XBMC.Quit()</onclick>
What It Does: This change replaces the Shutdown button's normal action (which is XBMC.Shutdown - which on AppleTV is the same as XBMC.Quit) with a line that calls our new python script and then calls XBMC.Quit (yes, you can stack commands like this - did you know?).
------------------
Summary Explanation:
I wrote things this way because I was having a horrible, horrible time trying to get System.Exec to work correctly on the AppleTV. In addition, I wanted to quit XBMC cleanly - and still somehow trigger the shutdown - yet if I tried to do that through System.Exec, I never would have gotten XBMC to quit cleanly because it'd still be trying to shutdown first.
This script is a cheat and a trick - by changing the .xml so that it gets called when someone clicks "Shutdown", it spawns an orphaned process that basically just runs the shell commands I'd want it to run: wait a bit (while XBMC quits), eject the disk if it's there (regardless of the name), wait a second more, then shutdown.
On my system, using this method, pressing the button quits XBMC, cleanly unmounts the USB drive I have attached, then shuts down the AppleTV. Total shutdown is usually within 10 seconds of pressing the button (the AppleTV takes a while to shut down because it's trying to kill that orphaned process we create - this is ok). So I tell my wife to just wait until the blue screen appears / AppleTV picture goes away - then she can turn off the power strip that the AppleTV is connected to.
------------------
Hope this helps and encourages others to tell System.Exec to take a flying leap off a cliff and to try using python to solve some of your peskier problems