One kind of annoying/obnoxious thing, about having an HTPC, is that it's still a PC. No matter which OS, there will be times where you will need to restart the in focus application, or even restart the computer. I run my HTPC, without a keyboard or mouse, and I control it with my iPhone. Yes I can, and do use VNC applications on the iPhone, but it's unbelievably clunky, to do, when I all I am trying to do is stop, and start an application, or restart the computer. There are also "wireless" keyboard and mouses, as well as applications that offer the same functionality on the iPhone, but it still feels like a PC, which it is, but it's not simple enough.
These are the main applications I run on a regular basis on my HTPC:
Boxee
iTunes
Simplify Media
uTorrent
Among other applications, obviously. Boxee is still a beta application, it does crash, although infrequently, and mostly because of it's workarounds for things like Hulu, and Netflix. I still love it, but sometimes I have to restart it, to make things start working again, or after it crashes. Now my options are to open my laptop, connect via vnc, and click the dock icon for boxee, yes this works, but what a pain. Second I could click the VNC app on my iPhone, and click the boxee application. It may sound simple, but both solutions for a 1920x1080 pixel display over wifi are clunky.
My solution, run a webserver, that executes commands, to start, stop, restart given applications.
Okay stop, what? You want to run a web server on your HTPC, to open applications? Really?
Yes, Really! Here's why...
I can make a web app function and look like an iphone app, and be quick, and simple to use. With a couple taps, I am in business. No fumbling for my laptop, or pinching the screen on my iPhone, to tap on exactly the right place on the dock to start an application.
Even more, I can do special things, like control specific apps via applescript, like Pandora. Yes, I can make a web app, that looks like an iphone app, that functions as a pandora remote. Now this is kind of pointless, since I can just use the boxee remote to control pandora inside of boxee (which is what I do), but the possibility exists.
One nice capability, is to bring an app into focus, so I can for instance, check the status of that fair use file I am downloading. ;) This can done via applescript.
Here is the recipe:
MAMP - Yes OSX includes it's own webserver already, but I like MAMP, and it runs a newer version of apache and PHP, which OS X is not running, plus it's free, so why not.
iPhone App Template - It's a little old, but it works well, and it looks and feels like an iPhone app, no work on my part except some hacking of the javascript so it would actually hit the PHP script. I am sure there are other, newer, better ones, but this was simple, no CMS, just a flat HTML file, all the work is done in one PHP script, and no AJAX, keeping it simple. I may explore using a more updated jquery script, but right now this works.
That's it. There is nothing fancy. For "security", I can have an .htaccess file, or have a php login, or something, but right now it's wide open, but it's behind a firewall and router, so you'd have to hack through my firewall, or connect to my non-broadcasted BSID, and also get an entry in my mac address table, just to get to the app, and I think I have kept it so simple that while not impossible to hack, it would be a giant pain, I am sure. Both my wife and me have iphones, and have an icon on our home screens, that go directly to the "app". I can also reach the "app" via web browser on my laptop, just in case I wanted to.
iPhone Web App Javascript Hack
In the iphonenav.js script I used from here, I replaced this:
if (link && link.hash)
{
var page = document.getElementById(link.hash.substr(1));
showPage(page);
}
with this:
if(link.hash == "#")
{
this.location.href=link;
}
else if (link && link.hash)
{
var page = document.getElementById(link.hash.substr(1));
showPage(page);
}
It's a little backwards, but rather than re-engineer how all the iphone app emulation goodness is working, I just did that work around. The way the iphone app is working, it's shifting around divs, and animating them, and using page anchors to accomplish that. This is common practice by things like jquery, I am actually pretty certain this template is using some jquery bits. The problem occured when I was trying to point the user to a php script, outside of the page, and it wouldn't do anything, this makes it actually go to that script. I could have just done some sort of ajax goodness, but I decided to keep it simple for now. In the end I might do that, so that I can give feedback to the web app.
PHP HTPC App Launcher Script
Now before everyone gets all worried about my script, please understand I an not a programmer, I just hack stuff, I admit it. If you have some pointers please let me know, but right now the security concerns are thwarted by simple security through obscurity. This is a intrAnet web app, not an honest to goodness, accesible via the web, application. And for good reason. I know it may look scary, but it's not intended for anyone except me.
<?php
$u = "username";
$p = "userpassword";
if($_GET["a"] == "boxee") {
$APP = "Boxee";
} else if($_GET["a"] == "itunes") {
$APP = "iTunes";
} else if($_GET["a"] == "utorrent") {
$APP = "uTorrent";
} else if($_GET["a"] == "simplify") {
$APP = "SimplifyMedia";
}
if($_GET["a"] != "system") {
if ($_GET["c"] == "r") {
shell_exec('sudo -u '.$u.' -p '.$p.' killall '.$APP);
// this is to give the application enough time to shutdown
sleep(10);
shell_exec('sudo -u '.$u.' -p '.$p.' open /Applications/'.$APP.'.app');
} else if ($_GET["c"] == "s") {
shell_exec('sudo -u '.$u.' -p '.$p.' open /Applications/'.$APP.'.app');
} else if ( $_GET["c"] == "p") {
shell_exec('sudo -u '.$u.' -p '.$p.' killall '.$APP);
} else if ($_GET["c"] == "b") {
// do some applescript to bring application into focus, working on this
shell_exec('sudo -u '.$u.' -p '.$p.' osascript -e \'tell application "System Events" to set visible of process "'.$APP.'" to false\'');
} else if ($_GET["c"] == "f") {
// do some applescript to bring application into focus, working on this
shell_exec('sudo -u '.$u.' -p '.$p.' osascript -e \'tell application "System Events" to set visible of process "'.$APP.'" to true\'');
}
header("Location: http://10.0.0.1:8888");
} else {
if ($_GET["c"] == "r") {
//reboot
shell_exec('sudo -p '.$p.' shutdown -r NOW');
$output = "The system is rebooting, you will be redirected in 60 seconds.
";
} else if ( $_GET["c"] == "p") {
//shutdown
shell_exec('sudo -p '.$p.' shutdown NOW');
$output = "The system is shutting down, you will need to push the power button on the computer to restart it.";
}
}
?>
I decided to not just let the HTML URL inject whatever app it was going to restart, but instead "modify" what the actual application name was, so that only a subset of applications could be controlled, until I add more down the road. This may not be best method, but it's simple. If I want to add an app down the road, I will have to add the proper HTML to the iphone web app, as well as the proper switch for it's actual name in the php script. But if that's all I have to edit, I am fine.
Hope this helps someone, please comment, as I am curious how broken my php script REALLY is, and how I can improve it.
Post new comment