aboutsummaryrefslogtreecommitdiff
path: root/tools/EventClients/Clients/OSXRemote/xbmchelper_main.mm
diff options
context:
space:
mode:
authorAlTheKiller <AlTheKiller@svn>2009-09-23 01:49:50 +0000
committerAlTheKiller <AlTheKiller@svn>2009-09-23 01:49:50 +0000
commit45285e8a9300cd754a760560640b75b09f98035e (patch)
treead9f093885ad5c98e9dd4156674e7691c22ed0a2 /tools/EventClients/Clients/OSXRemote/xbmchelper_main.mm
step 3/4: Move linuxport to trunk. How'd I get roped into this?
git-svn-id: https://xbmc.svn.sourceforge.net/svnroot/xbmc/trunk@23097 568bbfeb-2a22-0410-94d2-cc84cf5bfa90
Diffstat (limited to 'tools/EventClients/Clients/OSXRemote/xbmchelper_main.mm')
-rw-r--r--tools/EventClients/Clients/OSXRemote/xbmchelper_main.mm210
1 files changed, 210 insertions, 0 deletions
diff --git a/tools/EventClients/Clients/OSXRemote/xbmchelper_main.mm b/tools/EventClients/Clients/OSXRemote/xbmchelper_main.mm
new file mode 100644
index 0000000000..d4af31501a
--- /dev/null
+++ b/tools/EventClients/Clients/OSXRemote/xbmchelper_main.mm
@@ -0,0 +1,210 @@
+#include "Carbon/Carbon.h"
+#import "XBMCHelper.h"
+#include <getopt.h>
+#include <string>
+#include <vector>
+#include <sstream>
+#include <fstream>
+#include <iterator>
+
+using namespace std;
+
+//instantiate XBMCHelper which registers itself to IR handling stuff
+XBMCHelper* gp_xbmchelper;
+eRemoteMode g_mode = DEFAULT_MODE;
+std::string g_server_address="localhost";
+std::string g_app_path = "";
+std::string g_app_home = "";
+double g_universal_timeout = 0.500;
+bool g_verbose_mode = false;
+
+//
+const char* PROGNAME="XBMCHelper";
+const char* PROGVERS="0.5";
+
+void ParseOptions(int argc, char** argv);
+void ReadConfig();
+
+static struct option long_options[] = {
+{ "help", no_argument, 0, 'h' },
+{ "server", required_argument, 0, 's' },
+{ "universal", no_argument, 0, 'u' },
+{ "multiremote", no_argument, 0, 'm' },
+{ "timeout", required_argument, 0, 't' },
+{ "verbose", no_argument, 0, 'v' },
+{ "externalConfig", no_argument, 0, 'x' },
+{ "appPath", required_argument, 0, 'a' },
+{ "appHome", required_argument, 0, 'z' },
+{ 0, 0, 0, 0 },
+};
+
+static const char *options = "hs:umt:vxa:z:";
+
+//----------------------------------------------------------------------------
+//----------------------------------------------------------------------------
+void usage(void)
+{
+ printf("%s (version %s)\n", PROGNAME, PROGVERS);
+ printf(" Sends Apple Remote events to XBMC.\n\n");
+ printf("Usage: %s [OPTIONS...]\n\nOptions:\n", PROGNAME);
+ printf(" -h, --help print this help message and exit.\n");
+ printf(" -s, --server <addr> send events to the specified IP.\n");
+ printf(" -u, --universal runs in Universal Remote mode.\n");
+ printf(" -t, --timeout <ms> timeout length for sequences (default: 500ms).\n");
+ printf(" -m, --multiremote runs in Multi-Remote mode (adds remote identifier as additional idenfier to buttons)\n");
+ printf(" -a, --appPath path to XBMC.app (MenuPress launch support).\n");
+ printf(" -z, --appHome path to XBMC.app/Content/Resources/XBMX \n");
+ printf(" -v, --verbose prints lots of debugging information.\n");
+}
+
+//----------------------------------------------------------------------------
+void ReadConfig()
+{
+ // Compute filename.
+ std::string strFile = getenv("HOME");
+ strFile += "/Library/Application Support/XBMC/XBMCHelper.conf";
+
+ // Open file.
+ std::ifstream ifs(strFile.c_str());
+ if (!ifs)
+ return;
+
+ // Read file.
+ stringstream oss;
+ oss << ifs.rdbuf();
+
+ if (!ifs && !ifs.eof())
+ return;
+
+ // Tokenize.
+ string strData(oss.str());
+ istringstream is(strData);
+ vector<string> args = vector<string>(istream_iterator<string>(is), istream_iterator<string>());
+
+ // Convert to char**.
+ int argc = args.size() + 1;
+ char** argv = new char*[argc + 1];
+ int i = 0;
+ argv[i++] = "XBMCHelper";
+
+ for (vector<string>::iterator it = args.begin(); it != args.end(); ){
+ //fixup the arguments, here: remove '"' like bash would normally do
+ std::string::size_type j = 0;
+ while ((j = it->find("\"", j)) != std::string::npos )
+ it->replace(j, 1, "");
+ argv[i++] = (char* )(*it++).c_str();
+ }
+
+ argv[i] = 0;
+
+ // Parse the arguments.
+ ParseOptions(argc, argv);
+
+ delete[] argv;
+}
+
+//----------------------------------------------------------------------------
+void ParseOptions(int argc, char** argv)
+{
+ int c, option_index = 0;
+ //set the defaults
+ bool readExternal = false;
+ g_server_address = "localhost";
+ g_mode = DEFAULT_MODE;
+ g_app_path = "";
+ g_app_home = "";
+ g_universal_timeout = 0.5;
+ g_verbose_mode = false;
+
+ while ((c = getopt_long(argc, argv, options, long_options, &option_index)) != -1)
+ {
+ switch (c) {
+ case 'h':
+ usage();
+ exit(0);
+ break;
+ case 'v':
+ g_verbose_mode = true;
+ break;
+ case 's':
+ g_server_address = optarg;
+ break;
+ case 'u':
+ g_mode = UNIVERSAL_MODE;
+ break;
+ case 'm':
+ g_mode = MULTIREMOTE_MODE;
+ break;
+ case 't':
+ g_universal_timeout = atof(optarg) * 0.001;
+ break;
+ case 'x':
+ readExternal = true;
+ break;
+ case 'a':
+ g_app_path = optarg;
+ break;
+ case 'z':
+ g_app_home = optarg;
+ break;
+ default:
+ usage();
+ exit(1);
+ break;
+ }
+ }
+ //reset getopts state
+ optreset = 1;
+ optind = 0;
+
+ if (readExternal == true)
+ ReadConfig();
+
+}
+
+//----------------------------------------------------------------------------
+void StartHelper(){
+ [gp_xbmchelper enableVerboseMode:g_verbose_mode];
+
+ //set apppath to startup when pressing Menu
+ [gp_xbmchelper setApplicationPath:[NSString stringWithCString:g_app_path.c_str()]];
+ //set apppath to startup when pressing Menu
+ [gp_xbmchelper setApplicationHome:[NSString stringWithCString:g_app_home.c_str()]];
+ //connect to specified server
+ [gp_xbmchelper connectToServer:[NSString stringWithCString:g_server_address.c_str()] withMode:g_mode withTimeout: g_universal_timeout];
+}
+
+//----------------------------------------------------------------------------
+void Reconfigure(int nSignal)
+{
+ if (nSignal == SIGHUP){
+ ReadConfig();
+ StartHelper();
+ }
+ else {
+ QuitEventLoop(GetMainEventLoop());
+ }
+}
+
+//----------------------------------------------------------------------------
+int main (int argc, char * argv[]) {
+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+
+ NSLog(@"%s %s starting up...", PROGNAME, PROGVERS);
+ gp_xbmchelper = [[XBMCHelper alloc] init];
+
+ signal(SIGHUP, Reconfigure);
+ signal(SIGINT, Reconfigure);
+ signal(SIGTERM, Reconfigure);
+
+ ParseOptions(argc,argv);
+ StartHelper();
+
+ //run event loop in this thread
+ RunCurrentEventLoop(kEventDurationForever);
+ NSLog(@"%s %s exiting...", PROGNAME, PROGVERS);
+ //cleanup
+ [gp_xbmchelper release];
+ [pool drain];
+ return 0;
+}