00001 #include "zkbcfg.h"
00002
00003
00004 #include <opie2/odebug.h>
00005 #include <opie2/oapplication.h>
00006 using namespace Opie::Core;
00007
00008
00009 #include <qfileinfo.h>
00010
00011
00012 ZkbConfig::ZkbConfig(const QString& dir):path(dir) {
00013 }
00014
00015 ZkbConfig::~ZkbConfig() {
00016 }
00017
00018 bool ZkbConfig::load(const QString& file, Keymap& keymap, const QString& prefix) {
00019 bool ret;
00020 QFile *f = new QFile(path+"/"+file);
00021 QFileInfo fi(*f);
00022
00023
00024 if ( !fi.exists() && !path.contains( QPEApplication::qpeDir()) ) {
00025 delete f;
00026 f = new QFile( QPEApplication::qpeDir() + "share/zkb/" + file );
00027 fi = QFileInfo( *f );
00028 }
00029
00030 odebug << "start loading file=" << file << "\n" << oendl;
00031 if (includedFiles.find(fi.absFilePath()) != includedFiles.end()) {
00032 return false;
00033 }
00034
00035 includedFiles.insert(fi.absFilePath(), 1);
00036 QXmlInputSource is(*f);
00037 QXmlSimpleReader reader;
00038 ZkbHandler h(*this, keymap, prefix);
00039
00040 reader.setContentHandler(&h);
00041 reader.setErrorHandler(this);
00042 ret = reader.parse(is);
00043 includedFiles.remove(fi.absFilePath());
00044
00045 odebug << "end loading file=" << file << ": status=" << err << oendl;
00046 delete f;
00047 return ret;
00048 }
00049
00050 bool ZkbConfig::warning(const QXmlParseException& e) {
00051 QString tmp;
00052
00053 tmp.sprintf("%d: warning: %s\n", e.lineNumber(),
00054 (const char*) e.message().utf8());
00055
00056 err += tmp;
00057
00058 return true;
00059 }
00060
00061 bool ZkbConfig::error(const QXmlParseException& e) {
00062 QString tmp;
00063
00064 tmp.sprintf("%d: error: %s\n", e.lineNumber(),
00065 (const char*) e.message().utf8());
00066
00067 err += tmp;
00068
00069 return true;
00070 }
00071
00072 bool ZkbConfig::fatalError(const QXmlParseException& e) {
00073 QString tmp;
00074
00075 tmp.sprintf("%d: fatal error: %s\n", e.lineNumber(),
00076 (const char*) e.message().utf8());
00077
00078 err += tmp;
00079
00080 return false;
00081 }
00082
00083 QString ZkbConfig::errorString() {
00084 return err;
00085 }
00086
00087
00088 ZkbHandler::ZkbHandler(ZkbConfig& z, Keymap& k, const QString& p):zkc(z), keymap(k),
00089 prefix(p), ardelay(-1), arperiod(-1), currentState(0), currentAction(0) {
00090 }
00091
00092 ZkbHandler::~ZkbHandler() {
00093 }
00094
00095 bool ZkbHandler::startKeymapElement(int ard, int arp, const QString&) {
00096 ardelay = ard;
00097 arperiod = arp;
00098
00099 return true;
00100 }
00101
00102 bool ZkbHandler::startIncludeElement(const QString& file,
00103 const QString& pref) {
00104
00105 QString p = prefix;
00106
00107 if (!pref.isNull()) {
00108 p += pref + ":";
00109 }
00110
00111
00112 bool ret = zkc.load(file, keymap, p);
00113 if (!ret) {
00114 setError("Error including file: " + file);
00115 }
00116
00117 return ret;
00118 }
00119
00120 bool ZkbHandler::startLabelElement(const QString& label,
00121 const QString& state) {
00122
00123 if (!keymap.addLabel(label, prefix + state)) {
00124 err = "label " + label + " already defined";
00125 return false;
00126 }
00127
00128 return true;
00129 }
00130
00131 bool ZkbHandler::startStateElement(const QString& name,
00132 const QString& parentName, bool dflt) {
00133
00134 currentStateName = prefix + name;
00135 currentState = keymap.getStateByName(currentStateName);
00136
00137
00138
00139 State* parent = 0;
00140 if (!parentName.isEmpty()) {
00141 QString pn = prefix + parentName;
00142 parent = keymap.getStateByName(pn);
00143 if (parent == 0) {
00144 err = currentStateName +
00145 ": undefined parent state: " + pn;
00146 return false;
00147 }
00148 }
00149
00150 if (currentState == 0) {
00151 currentState = new State(parent);
00152 keymap.addState(currentStateName, currentState);
00153 } else {
00154 if (parent!=0) {
00155 currentState->setParent(parent);
00156 }
00157 }
00158
00159 if (dflt) {
00160 keymap.setCurrentState(currentState);
00161 }
00162
00163 return true;
00164 }
00165
00166 bool ZkbHandler::startMapElement(int keycode, bool pressed) {
00167 currentAction = currentState->get(keycode, pressed);
00168 if (currentAction == 0) {
00169 setError("keycode " + QString::number(keycode) + " not supported");
00170 return false;
00171 }
00172
00173 currentAction->setEvent(false);
00174 currentAction->setState(0);
00175
00176 return true;
00177 }
00178
00179 bool ZkbHandler::startEventElement(int keycode, int unicode, int modifiers,
00180 bool pressed, bool autorepeat) {
00181
00182 currentAction->setEvent(true);
00183 currentAction->setKeycode(keycode);
00184 currentAction->setUnicode(unicode);
00185 currentAction->setModifiers(modifiers);
00186 currentAction->setPressed(pressed);
00187 currentAction->setAutorepeat(autorepeat);
00188
00189 return true;
00190 }
00191
00192 bool ZkbHandler::startNextStateElement(const QString& state) {
00193 State* s = keymap.getStateByName(prefix + state);
00194 if (s == 0) {
00195 setError("undefine state: " + prefix + state);
00196 return false;
00197 }
00198
00199 currentAction->setState(s);
00200 return true;
00201 }
00202
00203
00204 bool ZkbHandler::endKeymapElement() {
00205 if (ardelay > 0) {
00206 keymap.setAutorepeatDelay(ardelay);
00207 }
00208
00209 if (arperiod > 0) {
00210 keymap.setAutorepeatPeriod(arperiod);
00211 }
00212
00213 return true;
00214 }
00215
00216 bool ZkbHandler::endIncludeElement() {
00217 return true;
00218 }
00219
00220 bool ZkbHandler::endLabelElement() {
00221 return true;
00222 }
00223
00224 bool ZkbHandler::endStateElement() {
00225 currentState = 0;
00226 return true;
00227 }
00228
00229 bool ZkbHandler::endMapElement() {
00230 currentAction = 0;
00231 return true;
00232 }
00233
00234 bool ZkbHandler::endEventElement() {
00235 return true;
00236 }
00237
00238 bool ZkbHandler::endNextStateElement() {
00239 return true;
00240 }