00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "mmsinput/mmsinputmapper.h"
00034 #include "mmstools/mmstools.h"
00035 #include <iostream>
00036 #include <stdlib.h>
00037 #include <cerrno>
00038
00039 MMSInputMapper::MMSInputMapper(string mapfile, string name) {
00040 xmlNode *walkNode = NULL;
00041 xmlNode *curNode = NULL;
00042
00043
00044 try {
00045 LIBXML_TEST_VERSION
00046
00047 parser = xmlReadFile((char *)mapfile.c_str(), NULL, 0);
00048
00049 if(parser == NULL) {
00050 throw MMSError(1, "Could not parse file:" + mapfile);
00051 }
00052 else {
00053
00054 xmlNode* pNode = xmlDocGetRootElement(parser);
00055
00056
00057 if(xmlStrcmp(pNode->name, (const xmlChar *) "mmsinputmaps")) {
00058 std::cout << "invalid mapfile (" << mapfile << ") - does not contain mmsinputmaps root node" << std::endl;
00059 throw MMSError(1, "invalid file");
00060 }
00061
00062 pNode = pNode->xmlChildrenNode;
00063
00064
00065 xmlChar *keyName;
00066 xmlChar *mapTo;
00067
00068
00069 for (walkNode = pNode; walkNode; walkNode = walkNode->next) {
00070 xmlChar *mapName;
00071
00072 if(xmlStrcmp(walkNode->name, (const xmlChar*) "map"))
00073 continue;
00074
00075 mapName = xmlGetProp(walkNode, (const xmlChar*)"name");
00076 if(!mapName)
00077 continue;
00078
00079 if(!xmlStrcmp(mapName, (const xmlChar *) name.c_str())) {
00080 DEBUGMSG("MMSINPUTMANAGER", "using mapping set of " + name + " node");
00081
00082 walkNode = walkNode->xmlChildrenNode;
00083 for (curNode = walkNode; curNode; curNode = curNode->next) {
00084 if(xmlStrcmp(curNode->name, (const xmlChar *) "key"))
00085 continue;
00086
00087 keyName = xmlGetProp(curNode, (const xmlChar*)"name");
00088 mapTo = xmlGetProp(curNode, (const xmlChar*)"mapto");
00089
00090 this->keyMap.insert(pair<string, string> (strToUpr(string((const char *) keyName)), strToUpr(string((const char *)mapTo))));
00091
00092 xmlFree(keyName);
00093 xmlFree(mapTo);
00094 }
00095 }
00096 else {
00097
00098 DEBUGMSG("MMSINPUTMANAGER", "Ignore mapping set of " + string((const char *) mapName) + " node");
00099 }
00100
00101 xmlFree(mapName);
00102 }
00103 }
00104 }
00105 catch(MMSError &error) {
00106 DEBUGMSG("MMSINPUTMANAGER", "Error loading inputmaps (" + mapfile + "." + name + ") [" + error.getMessage() + "]");
00107 }
00108
00109 DEBUGMSG("MMSINPUTMANAGER", "Parsing inputmap finished.");
00110 }
00111
00112 MMSInputMapper::~MMSInputMapper() {
00113
00114 this->keyMap.clear();
00115 delete this->parser;
00116 }
00117
00118 void MMSInputMapper::mapkey(MMSInputEvent *inputevent) {
00119 string symbol = mmskeys[inputevent->key];
00120
00121
00122 typedef multimap<string, string>::iterator MI;
00123 pair<MI,MI> iter = this->keyMap.equal_range(symbol);
00124
00125 for( MI run = iter.first; run != iter.second; ++run ) {
00126 string foundkeyname = run->second;
00127 MMSKeySymbol foundkey = mmskeys[foundkeyname];
00128 if(foundkey) {
00129 DEBUGMSG("MMSINPUTMANAGER", "mapped to key '" + foundkeyname + "', id: " + iToStr(foundkey));
00130
00131 inputevent->key = foundkey;
00132 return;
00133 }
00134 }
00135 }
00136