00001 #include <xercesc/util/PlatformUtils.hpp>
00002 #include <xercesc/dom/DOM.hpp>
00003 #include <xercesc/parsers/XercesDOMParser.hpp>
00004 #include <xercesc/util/XMLChar.hpp>
00005 #include <string>
00006 #include <iostream>
00007 #include <Logger.H>
00008 #include <WURDEConfiguration.H>
00009 #include "GameController.H"
00010
00011
00012 XERCES_CPP_NAMESPACE_USE
00013
00014 using namespace WURDE;
00015 using namespace std;
00016
00017 void configureJoystick(std::string config, GameController &joystick){
00018
00019 try {
00020 XMLPlatformUtils::Initialize();
00021 }
00022 catch (const XMLException& toCatch) {
00023 cout << "Something went wrong.\n";
00024 return;
00025 }
00026
00027
00028 static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
00029 DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(gLS);
00030 DOMBuilder *parser = ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
00031 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
00032 DOMElement *root;
00033 DOMNode *curr,*name,*child, *item;
00034 DOMNodeList *theNodes,*children;
00035 DOMNamedNodeMap *attr;
00036
00037 doc = parser->parseURI(config.c_str());
00038 if(!doc){
00039 g_logfatal << "Config file " << config.c_str() << " not found!"<< endl;
00040 exit(1);
00041 }
00042
00043 root=doc->getDocumentElement();
00044 if(!root){
00045 g_logfatal << "Unable to parse the config file for some reason. Make sure the file is well-formed. Exiting. " << endl;
00046 exit(1);
00047 }
00048
00049 string currentname, temp;
00050
00051
00052 temp=XMLString::transcode(root->getTagName());
00053 if(temp!="joystick-config"){
00054 cout << temp << " is not a proper joystick-config file.\n";
00055 exit(1);
00056 }
00057
00058
00059 theNodes=doc->getElementsByTagName(XMLString::transcode("joystick"));
00060
00061 for(unsigned int i=0;i<theNodes->getLength();i++){
00062 string id;
00063
00064 curr=theNodes->item(i);
00065 attr=curr->getAttributes();
00066
00067 name=attr->getNamedItem(XMLString::transcode("name"));
00068 currentname=XMLString::transcode(name->getNodeValue());
00069
00070 if(currentname!=joystick.name()){
00071 continue;
00072 }
00073
00074 children=curr->getChildNodes();
00075
00076 for(unsigned int j=0;j<children->getLength();j++){
00077 string tmpstring;
00078
00079 child=children->item(j);
00080 temp=XMLString::transcode(child->getNodeName());
00081
00082 if(temp=="axis"){
00083 string axisid,axisname;
00084 attr=child->getAttributes();
00085
00086 item=attr->getNamedItem(XMLString::transcode("id"));
00087 axisid=XMLString::transcode(item->getNodeValue());
00088
00089 item=attr->getNamedItem(XMLString::transcode("name"));
00090 axisname=XMLString::transcode(item->getNodeValue());
00091
00092 joystick.setAxisMap(strtol(axisid.c_str(),NULL,10),axisname);
00093 }else if(temp=="button"){
00094 string buttonid,buttonname;
00095 attr=child->getAttributes();
00096
00097 item=attr->getNamedItem(XMLString::transcode("id"));
00098 buttonid=XMLString::transcode(item->getNodeValue());
00099
00100 item=attr->getNamedItem(XMLString::transcode("name"));
00101 buttonname=XMLString::transcode(item->getNodeValue());
00102
00103 joystick.setButtonMap(strtol(buttonid.c_str(),NULL,10),buttonname);
00104 }
00105 }
00106
00107 }
00108
00109
00110 XMLPlatformUtils::Terminate();
00111
00112 }
00113
00114