1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/***************************************************************************
klineakdef.cpp - description
-------------------
begin : Sun Jun 9 2002
copyright : (C) 2002 by Sheldon Lee Wen
email : leewsb@hotmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// Leave these in this order !!!!!
#include <klocale.h>
#include "klprefs.h"
#include "klineakdef.h"
#include "klmailclient.h"
#include <qpushbutton.h>
#include <qstring.h>
#include <iostream>
#include <lineak/lkbd.h>
#include <lineak/saver.h>
#include <lineak/ldef.h>
#include <lineak/lkey.h>
#include <lineak/defloader.h>
extern "C" {
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBfile.h>
#include <sys/types.h>
#include <unistd.h>
}
KlineakDef::KlineakDef(QWidget *parent, const char *name ) : KlineakDefUI(parent,name) {
lcdKeycode->setMode(QLCDNumber::Dec);
lcdKeycode->setNumDigits(3);
// btnPicture->setEnabled(false);
btnSubmit->setEnabled(false);
btnApply->setEnabled(false);
//cout << "parsing user def" << endl;
parseUserDef();
//cout << "done parsing user def" << endl;
}
KlineakDef::~KlineakDef(){
}
bool KlineakDef::x11Event( XEvent* event) {
XkbEvent xev;
xev.core = *event;
if ( this->isVisible() ) {
if (event->type == KeyPress) {
XKeyEvent *e = (XKeyEvent *) event;
if (isEAK(e)) {
QString text = currentKeyName; //txtKeyName->text();
lastKeyCode = e->keycode;
lcdKeycode->display(lastKeyCode);
if ( text != "" ) {
statusLabel->setText(i18n("Key %1 mapped to key name %2").arg(lastKeyCode).arg(txtKeyName->text()));
keys2codes[text] = lastKeyCode;
if (!lstKeyList->findItem(text))
lstKeyList->insertItem(text);
if (myKeyboard.brand != "" && myKeyboard.model != "") {
//btnPicture->setEnabled(true);
if (!keys2codes.empty()) {
btnApply->setEnabled(true);
btnSubmit->setEnabled(true);
}
}
txtKeyName->setText("");
}
}
//else
//cout << "isEAK() is false" << endl;
}
}
// if (event->type == MappingNotify)
// cout << "MappingNotify Event Keymap changed\n";
return false;
}
/** Try and determine if the key is an EAK */
bool KlineakDef::isEAK(XKeyEvent *e) {
QString ksname, sub;
KeySym ks;
int nbytes;
char str[256+1];
nbytes = XLookupString (e, str, 256, &ks, NULL);
if (ks == NoSymbol)
ksname = "NoSymbol";
else if (!(ksname = XKeysymToString (ks)))
ksname = "(no name)";
if (nbytes < 0) nbytes = 0;
if (nbytes > 256) nbytes = 256;
str[nbytes] = '\0';
//cout << endl;
//cout << "in isEAK()" << endl;
//cout << "keysym = " << ksname << endl;
//cout << "e->keycode = " << e->keycode << endl;
//cout << "nbytes = " << nbytes << endl;
//cout << "sub = " << ksname.left(3) << endl;
//cout << endl;
if (e->keycode == 0)
return false;
if (ksname == "NoSymbol" || ksname == "(no name)" || ksname == "" ) {
//cout << "keycode=" << e->keycode << " keysym=" << ksname << " XLookupString gives " << nbytes << " characters: \"" << str << "\"" << endl;
return true;
}
sub = ksname.left(3);
if (nbytes == 0 && ( sub == "F13" || sub == "F14" || sub == "F15" || sub == "F16" ||
sub == "F17" || sub == "F18" || sub == "F19" || sub == "F20" ||
sub == "F21" || sub == "F22" || sub == "F23" || sub == "F24" ||
sub == "F25" || sub == "F26" || sub == "F27" || sub == "F28" ||
sub == "F29" || sub == "F30" || sub == "F31" || sub == "F32" )) {
//cout << "keycode=" << e->keycode << " keysym=" << ksname << " XLookupString gives " << nbytes << " characters: \"" << str << "\"" << endl;
return true;
}
/* X may know about our keyboard, so just get a decent range. */
if (e->keycode >= 120 && e->keycode <= 255)
return true;
return false;
}
/** Triggered when the user presses the "Close" button */
void KlineakDef::onQuitButtonClicked(){
emit quitting();
//emit keyboardName(hashKbIdent());
hide();
}
/** Triggered when the user hits the Apply button */
void KlineakDef::onApplyButtonClicked(){
/** Applying should send back the name of the file to parse */
emit applying(saveDefFile());
/** Applying sends back the name of the keyboard to set as the default. */
emit keyboardName(myKeyboard.name);
onQuitButtonClicked();
}
QString KlineakDef::hashKbIdent() {
QString hash;
QStringList hMod;
// Set the defaults.
if (myKeyboard.brand == "")
myKeyboard.brand = "User Defined Brand";
if (myKeyboard.model == "")
myKeyboard.model = "User Defined Model";
hash = "USR"; // Start user defined keyboards with USR
hMod = getenv("USER"); // Fourth letter in the hash is the first letter of the userid
hMod += QStringList::split(" ",QString(myKeyboard.brand.c_str())); // Next letter is the first letter of every word in the brandname.
hMod += QStringList::split(" ",QString(myKeyboard.model.c_str())); // Next letter is the first letter of every word in the modelname.
for ( QStringList::Iterator it = hMod.begin(); it != hMod.end(); ++it ) {
hash+= QString(*it)[0];
}
//cout << "hash is : " << hash << endl;
return hash;
}
/** Save the Def File */
QString KlineakDef::saveDefFile() {
QFile userdeffile;
QString userdeffilename = QString("%1%2%3").arg(getenv("HOME")).arg(LINEAKDIR).arg(DEFFILE);
userdeffile.setName(userdeffilename);
// If the hash name is not set the keyboard will not save. It will show up as empty.
myKeyboard.name = hashKbIdent().latin1();
if (userdeffile.exists() && (keys2codes.size() != 0)) {
// show message box that we are overwriting the existing file.
// if that's ok then continue.
// else return QString::null;
switch( QMessageBox::warning( this, "KLineak",
i18n("A user defined keyboard definition file already exists.\n"
"Do you want to replace that definition with a new one?\n"),
i18n("Replace"),
i18n("Don't Replace"), 0, 0, 1 ) ) {
case 0: // The user clicked the "Replace" button or pressed Enter
break;
case 1: // The user clicked the "Don't Replace" or pressed Escape
return ("");
break;
}
}
cout << "Saving deffile\n";
for(map<QString,int>::iterator it = keys2codes.begin();it!=keys2codes.end(); it++) {
LKey *tmp = new LKey((it->first).latin1(), it->second);
if (tmp != NULL) {
myKeyboard.removeObject(it->first.latin1());
myKeyboard.addObject(tmp);
cout << "Added object: " << endl;
cout << *tmp << endl;
}
tmp = NULL;
}
//cout << "Saving the LKbd" << endl;
//cout << myKeyboard << endl;
LDef def(userdeffilename.latin1());
Saver tmp(userdeffilename.latin1());
def.addKeyboard(myKeyboard);
//cout << "Saving definition!" << endl;
//cout << def << endl;
tmp.saveFile(def);
return userdeffile.name();
}
/** When the user clicks on a key in the key list */
void KlineakDef::updateKeyinfo(const QString &key){
//int keycode = (myKeyboard.getObject(key.latin1())->getKeyCode();
int keycode = keys2codes[key];
txtKeyName->setText(key);
lcdKeycode->display(keycode);
currentKeyName = key;
statusLabel->setText(i18n("Key %1 mapped to key name %2").arg(keycode).arg(key));
}
/** Update the brand for our keyboard */
void KlineakDef::updateBrand(const QString &brand){
myKeyboard.brand = brand.latin1();
if (myKeyboard.brand != "" && myKeyboard.model != "") {
//btnPicture->setEnabled(true);
if (!keys2codes.empty())
btnApply->setEnabled(true);
}
}
/** Update the name of the current key */
void KlineakDef::updateKeyname(const QString &keyname){
currentKeyName = keyname;
}
/** Update the model text of our keyboard. */
void KlineakDef::updateModel(const QString &model){
myKeyboard.model = model.latin1();
if (myKeyboard.brand != "" && myKeyboard.model != "") {
//btnPicture->setEnabled(true);
if (!keys2codes.empty())
btnApply->setEnabled(true);
}
}
/** Triggered when the user hits the Clear Key button */
void KlineakDef::onClearKeyButtonClicked(){
//map<QString,int>::iterator it = keys2codes.find(lstKeyList->currentText());
txtKeyName->setText("");
lcdKeycode->display(0);
currentKeyName = "";
statusLabel->setText("");
keys2codes.erase(lstKeyList->currentText());
//LObject * obj = myKeyboard.getObject(lstKeyList->currentItem());
myKeyboard.removeObject(lstKeyList->currentText().latin1());
//if (obj->getType() == SYM || obj->getType() == CODE) {
// LKey *key = static_cast<LKey*>(*obj);
// key->clear();
lstKeyList->removeItem(lstKeyList->currentItem());
}
/** Triggered when the user hits the Clear All button */
void KlineakDef::onClearAllButtonClicked(){
txtKeyName->setText("");
lcdKeycode->display(0);
currentKeyName = "";
statusLabel->setText("");
lstKeyList->clear();
keys2codes.clear();
const map<string,LObject*> &objects = myKeyboard.getObjects();
map<string,LObject*>::iterator it = const_cast<map<string,LObject*> &>(objects).begin();
for (; it != objects.end(); it++) {
myKeyboard.removeObject(it->first);
}
}
/** Triggered when the user hits the "Submit my Keyboard" button */
void KlineakDef::onSubmitButtonClicked() {
/** Write out the config file so we can attach it. */
onApplyButtonClicked();
const QString dest_email = KLPrefs::instance()->mContribEmail;
const QString source_email = KLPrefs::instance()->email();
const QString subject = "lineakd user defined keyboard contribution.";
const KURL attach(QString("%1%2%3").arg(getenv("HOME")).arg(LINEAKDIR).arg(DEFFILE));
KLMailClient mail;
mail.mailTo(source_email,dest_email,subject,attach);
}
/** Triggered when the user hits the "Choose Picture" button
void KlineakDef::onPictureButtonClicked() {
KURL desturl;
KURL url = KFileDialog::getOpenURL( ":load_image", KImageIO::pattern( KImageIO::Reading ), this );
QString destname;
QDir lineakdir;
QString picsdir = QString(QDir::homeDirPath()) + PICSDIR;
lineakdir.setPath(picsdir);
destname = lineakdir.path() + "/";
destname += hashKbIdent() + ".";
destname += QString(QPixmap::imageFormat (url.path()) ).lower();
desturl.setPath(destname);
if ( !url.isValid() )
kdWarning( 4610 ) << "malformed URL " << url.prettyURL() << endl;
if ( !desturl.isValid() )
kdWarning( 4610 ) << "malformed URL " << desturl.prettyURL() << endl;
cout << "copying " << url.path() << " to " << desturl.path() << endl;
KIO::file_copy(url,desturl, 0644, true);
picture = desturl;
emit setPicture(desturl);
} */
/** Parse the user defined keyboard file if it exists and load it into the gui. */
void KlineakDef::parseUserDef(){
QFile userdeffile;
QString userdeffilename = QString("%1%2%3").arg(getenv("HOME")).arg(LINEAKDIR).arg(DEFFILE);
userdeffile.setName(userdeffilename);
//cout << "Parsing userdef\n";
if (userdeffile.exists() && userdeffile.size() != 0) {
LDef udef;
DefLoader defldr(userdeffilename.latin1());
udef = defldr.loadDef();
if (udef.isEmpty() ) {
cerr << "*** Error occured while loading the user keyboard definition file from ";
cerr << userdeffilename << endl;
}
/** Clear the existing map and add the complete data */
keys2codes.clear();
/* Fill the keys2codes map */
QString kbtype, kbname, model;
map<string,LKbd*> table = udef.getTable();
map<string,LKbd*>::iterator it = table.begin();
myKeyboard = *(it->second);
// Update the brand and model fields.
QString qbrand = myKeyboard.brand.c_str();
QString qmodel = myKeyboard.model.c_str();
txtBrand->setText(qbrand);
updateBrand(qbrand);
txtModel->setText(qmodel);
updateModel(qmodel);
// Get and update the keys
QString text;
int code = 0;
vector<string> names = myKeyboard.getNames();
for (vector<string>::iterator it = names.begin(); it != names.end(); it++) {
text = QString(it->c_str());
code = (static_cast<LKey*>(myKeyboard.getObject(*it)))->getKeyCode();
keys2codes[text] = code;
// Add the key item to the gui
if (!lstKeyList->findItem(text))
lstKeyList->insertItem(text);
// Update the keyinfo
updateKeyinfo(text);
}
// If keys are defined then enable the apply and submit buttons.
if (!keys2codes.empty()) {
btnApply->setEnabled(true);
btnSubmit->setEnabled(true);
}
}
userdeffile.close();
//cout << "Parsed userdef\n";
}
|