Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsinputmapper.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
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     /* map the keys */
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             // check if incorrent input mapfile found
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             // need char buffers fpr mapping information
00065             xmlChar *keyName;
00066             xmlChar *mapTo;
00067 
00068             // walk trough the child node, until found the correct map node
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                     //ignore this node
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     /* parse the result nodes */
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 //              evt.type = MMSINPUTEVENTTYPE_KEYPRESS;
00131             inputevent->key = foundkey;
00132             return;
00133         }
00134     }
00135 }
00136 

Generated by doxygen