From 5be965e2a54b639df60017247469781c0282425b Mon Sep 17 00:00:00 2001 From: Konrad Rosenbaum Date: Wed, 18 Nov 2015 21:54:05 +0100 Subject: [PATCH] update indenting of barcode plugin, add some debugging --- plugins/bcs-usb/bcs-plugin.cpp | 188 ++++++++-------- plugins/bcs-usb/bcs-plugin.h | 60 +++--- plugins/bcs-usb/bcskeyboard.cpp | 456 +++++++++++++++++++------------------- plugins/bcs-usb/bcskeyboard.h | 300 +++++++++++++------------- plugins/bcs-usb/configwidget.cpp | 32 ++-- plugins/bcs-usb/configwidget.h | 54 +++--- plugins/bcs-usb/hidscanner.cpp | 141 ++++++------ plugins/bcs-usb/hidscanner.h | 50 ++-- 8 files changed, 642 insertions(+), 639 deletions(-) diff --git a/plugins/bcs-usb/bcs-plugin.cpp b/plugins/bcs-usb/bcs-plugin.cpp index 0cf921d..c070d5f 100644 --- a/plugins/bcs-usb/bcs-plugin.cpp +++ b/plugins/bcs-usb/bcs-plugin.cpp @@ -20,132 +20,132 @@ MHidBarcodePlugin::MHidBarcodePlugin(): QObject() { - qDebug()<<"Initializing USB Barcode Plugin"; - misopen = (hid_init()==0); - if(misopen) - qDebug()<<" ...success."; - else{ - qDebug()<<" ...failed to initialize USB-HID."; - return; - } - mdetecttmr=new QTimer(this); - connect(mdetecttmr,SIGNAL(timeout()),this,SLOT(detectScanners())); - mdetecttmr->setSingleShot(false); - restartDetect(); + qDebug()<<"Initializing USB Barcode Plugin"; + misopen = (hid_init()==0); + if(misopen) + qDebug()<<" ...success."; + else{ + qDebug()<<" ...failed to initialize USB-HID."; + return; + } + mdetecttmr=new QTimer(this); + connect(mdetecttmr,SIGNAL(timeout()),this,SLOT(detectScanners())); + mdetecttmr->setSingleShot(false); + restartDetect(); } MHidBarcodePlugin::~MHidBarcodePlugin() { - qDebug()<<"Unloading USB Barcode Plugin"; - if(mdetecttmr)mdetecttmr->stop(); - for(MHidBarcodeScanner*sc:mscanners){ - if(sc)delete sc; - } - mscanners.clear(); - if(misopen)hid_exit(); + qDebug()<<"Unloading USB Barcode Plugin"; + if(mdetecttmr)mdetecttmr->stop(); + for(MHidBarcodeScanner*sc:mscanners){ + if(sc)delete sc; + } + mscanners.clear(); + if(misopen)hid_exit(); } void MHidBarcodePlugin::configure(MBarcodeConfiguration* cfg) { - //suspend detection until dialog closed - if(mdetecttmr)mdetecttmr->stop(); - //main tab - new MUsbBarcodeConfig(cfg); - QSettings set;set.beginGroup(MUsbBarcodeConfig::settingsGroup); - for(const QString s:set.childGroups()) - new MUsbScannerConfig(cfg,s); - //restart detection - connect(cfg,SIGNAL(destroyed(QObject*)),this,SLOT(restartDetect())); + //suspend detection until dialog closed + if(mdetecttmr)mdetecttmr->stop(); + //main tab + new MUsbBarcodeConfig(cfg); + QSettings set;set.beginGroup(MUsbBarcodeConfig::settingsGroup); + for(const QString s:set.childGroups()) + new MUsbScannerConfig(cfg,s); + //restart detection + connect(cfg,SIGNAL(destroyed(QObject*)),this,SLOT(restartDetect())); } MHidBarcodePlugin::Config::Config(QSettings& set,QString grp) { - set.beginGroup(grp); - isactive=set.value(MUsbBarcodeConfig::activeKey,false).toBool(); - vendor=set.value(MUsbBarcodeConfig::vendorKey,0).toInt(); - product=set.value(MUsbBarcodeConfig::productKey,0).toInt(); - iface=set.value(MUsbBarcodeConfig::interfaceKey,0).toInt(); - useserial=set.value(MUsbBarcodeConfig::useSerialKey,false).toBool(); - serial=set.value(MUsbBarcodeConfig::serialKey).toString(); - group=grp; - set.endGroup(); + set.beginGroup(grp); + isactive=set.value(MUsbBarcodeConfig::activeKey,false).toBool(); + vendor=set.value(MUsbBarcodeConfig::vendorKey,0).toInt(); + product=set.value(MUsbBarcodeConfig::productKey,0).toInt(); + iface=set.value(MUsbBarcodeConfig::interfaceKey,0).toInt(); + useserial=set.value(MUsbBarcodeConfig::useSerialKey,false).toBool(); + serial=set.value(MUsbBarcodeConfig::serialKey).toString(); + group=grp; + set.endGroup(); } void MHidBarcodePlugin::restartDetect() { - if(!misopen){ - if(mdetecttmr){ - delete mdetecttmr; - mdetecttmr=0; - } - return; - } - const int interv=QSettings().value(MUsbBarcodeConfig::intervalKey,10).toInt(); - if(interv>0) - mdetecttmr->start(interv*1000); - else - mdetecttmr->stop(); - //re-read config - QSettings set; - set.beginGroup(MUsbBarcodeConfig::settingsGroup); - mconfig.clear(); - for(const QString grp:set.childGroups()){ - Config c(set,grp); - if(!c.isactive){ - continue; - } - mconfig.append(c); - } - //TODO: go through running scanners and kill unknowns + if(!misopen){ + if(mdetecttmr){ + delete mdetecttmr; + mdetecttmr=0; + } + return; + } + const int interv=QSettings().value(MUsbBarcodeConfig::intervalKey,10).toInt(); + if(interv>0) + mdetecttmr->start(interv*1000); + else + mdetecttmr->stop(); + //re-read config + QSettings set; + set.beginGroup(MUsbBarcodeConfig::settingsGroup); + mconfig.clear(); + for(const QString grp:set.childGroups()){ + Config c(set,grp); + if(!c.isactive){ + continue; + } + mconfig.append(c); + } + //TODO: go through running scanners and kill unknowns } void MHidBarcodePlugin::detectScanners() { - if(!misopen)return; - qDebug()<<"scanning for scanners..."; - hid_device_info *info=hid_enumerate(0,0); - for(hid_device_info*dev=info;dev;dev=dev->next){ - //is it known? - Config cfg=findCfgMatch(dev->vendor_id,dev->product_id,dev->interface_number, - QString::fromWCharArray(dev->serial_number)); - if(cfg.vendor<=0)continue; - //is it already open? - MHidBarcodeScanner*scanner=findScanner(dev->path); - if(scanner)continue; - //open it - qDebug()<<"instantiating scanner"; - scanner=new MHidBarcodeScanner(dev->path,cfg.group); - mscanners.append(scanner); - connect(scanner,SIGNAL(destroyed(QObject*)),this,SLOT(removeScanner(QObject*))); - //register with parent - registerScanner(scanner); - } - hid_free_enumeration(info); + if(!misopen)return; + qDebug()<<"scanning for scanners..."; + hid_device_info *info=hid_enumerate(0,0); + for(hid_device_info*dev=info;dev;dev=dev->next){ + //is it known? + Config cfg=findCfgMatch(dev->vendor_id,dev->product_id,dev->interface_number, + QString::fromWCharArray(dev->serial_number)); + if(cfg.vendor<=0)continue; + //is it already open? + MHidBarcodeScanner*scanner=findScanner(dev->path); + if(scanner)continue; + //open it + qDebug()<<"instantiating scanner"; + scanner=new MHidBarcodeScanner(dev->path,cfg.group); + mscanners.append(scanner); + connect(scanner,SIGNAL(destroyed(QObject*)),this,SLOT(removeScanner(QObject*))); + //register with parent + registerScanner(scanner); + } + hid_free_enumeration(info); } MHidBarcodePlugin::Config MHidBarcodePlugin::findCfgMatch(int vendor, int product, int iface, QString serial) { - for(Config c:mconfig){ - if(c.vendor!=vendor || c.product!=product || c.iface!=iface)continue; - if(c.useserial && c.serial!=serial)continue; - return c; - } - return Config(); + for(Config c:mconfig){ + if(c.vendor!=vendor || c.product!=product || c.iface!=iface)continue; + if(c.useserial && c.serial!=serial)continue; + return c; + } + return Config(); } MHidBarcodeScanner* MHidBarcodePlugin::findScanner(QString path) { - for(MHidBarcodeScanner*scanner:mscanners) - if(scanner->matchPath(path)) - return scanner; - return nullptr; + for(MHidBarcodeScanner*scanner:mscanners) + if(scanner->matchPath(path)) + return scanner; + return nullptr; } void MHidBarcodePlugin::removeScanner(QObject* o) { - MHidBarcodeScanner*s=qobject_cast< MHidBarcodeScanner* >(o); - if(s) - mscanners.removeAll(s); + MHidBarcodeScanner*s=qobject_cast< MHidBarcodeScanner* >(o); + if(s) + mscanners.removeAll(s); } diff --git a/plugins/bcs-usb/bcs-plugin.h b/plugins/bcs-usb/bcs-plugin.h index 47f97d4..f09e732 100644 --- a/plugins/bcs-usb/bcs-plugin.h +++ b/plugins/bcs-usb/bcs-plugin.h @@ -1,7 +1,7 @@ // // C++ Interface: plugin for USB barcode scanners // -// Description: +// Description: // // // Author: Konrad Rosenbaum , (C) 2013-2014 @@ -20,35 +20,35 @@ class MHidBarcodeScanner; class QSettings; class MHidBarcodePlugin:public QObject,public MBarcodePlugin { - Q_OBJECT - Q_PLUGIN_METADATA(IID MBarcodePlugin_IID) - Q_INTERFACES(MBarcodePlugin) - public: - MHidBarcodePlugin(); - virtual ~MHidBarcodePlugin(); - virtual void configure(MBarcodeConfiguration*)override; - private slots: - void detectScanners(); - void restartDetect(); - void removeScanner(QObject*); - private: - bool misopen; - QTimer*mdetecttmr=nullptr; - struct Config{ - int vendor=0,product=0,iface=0; - bool useserial=false,isactive=false; - QString serial,group; - Config(){} - Config(const Config&)=default; - Config(Config&&)=default; - Config(QSettings&,QString); - Config& operator=(const Config&)=default; - Config& operator=(Config&&)=default; - }; - QListmconfig; - Config findCfgMatch(int vendor,int product,int iface,QString serial); - QListmscanners; - MHidBarcodeScanner*findScanner(QString path); + Q_OBJECT + Q_PLUGIN_METADATA(IID MBarcodePlugin_IID) + Q_INTERFACES(MBarcodePlugin) + public: + MHidBarcodePlugin(); + virtual ~MHidBarcodePlugin(); + virtual void configure(MBarcodeConfiguration*)override; + private slots: + void detectScanners(); + void restartDetect(); + void removeScanner(QObject*); + private: + bool misopen; + QTimer*mdetecttmr=nullptr; + struct Config{ + int vendor=0,product=0,iface=0; + bool useserial=false,isactive=false; + QString serial,group; + Config(){} + Config(const Config&)=default; + Config(Config&&)=default; + Config(QSettings&,QString); + Config& operator=(const Config&)=default; + Config& operator=(Config&&)=default; + }; + QListmconfig; + Config findCfgMatch(int vendor,int product,int iface,QString serial); + QListmscanners; + MHidBarcodeScanner*findScanner(QString path); }; #endif diff --git a/plugins/bcs-usb/bcskeyboard.cpp b/plugins/bcs-usb/bcskeyboard.cpp index 9546563..64ba258 100644 --- a/plugins/bcs-usb/bcskeyboard.cpp +++ b/plugins/bcs-usb/bcskeyboard.cpp @@ -17,320 +17,320 @@ MHidKeyLayout::MHidKeyLayout() { - loadLayout("base"); + loadLayout("base"); } MHidKeyLayout::MHidKeyLayout(QString name) { - loadLayout(name); + loadLayout(name); } QString MHidKeyLayout::layoutCodeName() const { - if(mfilename.isEmpty())return QString(); - return QFileInfo(mfilename).baseName(); + if(mfilename.isEmpty())return QString(); + return QFileInfo(mfilename).baseName(); } void MHidKeyLayout::clearKeyMapping() { - mkeymap.clear(); + mkeymap.clear(); } void MHidKeyLayout::resetLayout() { - clearKeyMapping(); - loadLayout("base"); + clearKeyMapping(); + loadLayout("base"); } void MHidKeyLayout::setKeyMapping(unsigned char keycode, const QList &c) { - if(!c.isEmpty())mkeymap.insert(keycode,c); - else mkeymap.remove(keycode); + if(!c.isEmpty())mkeymap.insert(keycode,c); + else mkeymap.remove(keycode); } QString MHidKeyLayout::toLayoutDoc() const { - QString ret="#This is a generated keyboard layout\n"; - if(!mlname.isEmpty())ret+="title "+mlname+"\n"; - for(unsigned char kc:mkeymap.keys()){ - if(mkeymap[kc].isEmpty())continue; - ret+=QString("0x%1").arg((int)kc,0,16); - for(const MKey&k:mkeymap[kc]) - ret+=QString(" %1").arg(k.toCharName()); - ret+="\n"; - } - return ret; + QString ret="#This is a generated keyboard layout\n"; + if(!mlname.isEmpty())ret+="title "+mlname+"\n"; + for(unsigned char kc:mkeymap.keys()){ + if(mkeymap[kc].isEmpty())continue; + ret+=QString("0x%1").arg((int)kc,0,16); + for(const MKey&k:mkeymap[kc]) + ret+=QString(" %1").arg(k.toCharName()); + ret+="\n"; + } + return ret; } bool MHidKeyLayout::loadLayout(QString filename) { - //no slash: try to find a match - if(!filename.contains('/')){ - if(!filename.endsWith(".kbl"))filename+=".kbl"; - //search for file in app dir - if(QFileInfo(QCoreApplication::applicationDirPath()+"/kbdlayouts/"+filename).exists()) - filename=QCoreApplication::applicationDirPath()+"/kbdlayouts/"+filename; - //search in resources - else if(QFileInfo(":/kbdlayouts/"+filename).exists()) - filename=":/kbdlayouts/"+filename; - else{ - qDebug()<<"Unable to find a match for keyboard layout"<255){ - qDebug()<<"Error: illegal key code"<keys; - for(int i=1;i255){ + qDebug()<<"Error: illegal key code"<keys; + for(int i=1;i MHidKeyLayout::findLayouts() { - static QMaplaymap; - if(laymap.size()>0)return laymap; - //search all files - for(QString d:QStringList()<<":/kbdlayouts"<laymap; + if(laymap.size()>0)return laymap; + //search all files + for(QString d:QStringList()<<":/kbdlayouts"<=0){ - altseq*=10; - altseq+=d; - } + //enable alt mode? + if(key.modifiers()==MKey::Alt){ + altmode=true; + const int d=key.toChar(true).digitValue(); + if(d>=0){ + altseq*=10; + altseq+=d; + } // qDebug()<<"Alt: "<0) - ret+=QChar::fromLatin1(altseq); + continue; + } + if(altmode && !key.modifiers().testFlag(MKey::Alt)){ + altmode=false; + if(altseq>0) + ret+=QChar::fromLatin1(altseq); // qDebug()<<"Alt compl:"<0)ret+=QChar::fromLatin1(altseq); - //done - return ret; + altseq=0; + //fall through and process remainder + } + //printable? + if(key.isPrintable())ret+=key.toChar(); + } + //residual alt mode? + if(altmode && altseq>0)ret+=QChar::fromLatin1(altseq); + //done + return ret; } void MHidKeyEvent::reduce() { - QByteArray r; - for(unsigned char c:mkeys) - if(c>1)r.append(c); - mkeys=r; + QByteArray r; + for(unsigned char c:mkeys) + if(c>1)r.append(c); + mkeys=r; } qint64 MHidKeySequence::msSinceLastKey() const { - if(isEmpty())return 0; - return at(size()-1).timestamp().msecsTo(QDateTime::currentDateTime()); + if(isEmpty())return 0; + return at(size()-1).timestamp().msecsTo(QDateTime::currentDateTime()); } diff --git a/plugins/bcs-usb/bcskeyboard.h b/plugins/bcs-usb/bcskeyboard.h index b9e6d30..3d0c99f 100644 --- a/plugins/bcs-usb/bcskeyboard.h +++ b/plugins/bcs-usb/bcskeyboard.h @@ -1,7 +1,7 @@ // // C++ Interface: plugin for USB barcode scanners: Scanner Implementation // -// Description: +// Description: // // // Author: Konrad Rosenbaum , (C) 2013-2014 @@ -21,173 +21,173 @@ class MKey { - public: - enum Modifier { - None = 0, - Ctrl = 1, - Shift = 2, - Alt = 4, - AltGr = 8, - GUI = 16, - }; - Q_DECLARE_FLAGS(Modifiers,Modifier); - - enum KeyType{ - AllKeyRelease = 0, - CharKey = 1, - CapsLock = 2, - NumLock = 3, - Backspace = 4, - Return = 5, - Tabulator = 6, - Space = 7, - ModifierOnly =8, - Escape = 9, - }; - - MKey(){} - MKey(const MKey&k,Modifiers m); - MKey(const MKey&)=default; - MKey(MKey&&)=default; - - MKey(QChar,Modifiers m=None); - MKey(KeyType t,Modifiers m=None):mmod(m),mtype(t){} - - MKey& operator=(const MKey&)=default; - MKey& operator=(MKey&&)=default; - - bool operator==(const MKey&); - bool operator!=(const MKey&k){return !operator==(k);} - - bool isValid()const{return mtype!=AllKeyRelease;} - - Modifiers modifiers()const{return mmod;} - QChar toChar(bool ignoremodifier=false)const; - bool isPrintable(bool ignoremodifier=false)const; - - ///returns the encoded character name as used in layout files - QString toCharName()const; - ///returns a key object matching the encoded character name from a layout file - static MKey fromCharName(QString); - - MKey toLower()const{MKey r(*this);r.mchar=r.mchar.toLower();return r;} - MKey toUpper()const{MKey r(*this);r.mchar=r.mchar.toUpper();return r;} - private: - Modifiers mmod=None; - KeyType mtype=AllKeyRelease; - QChar mchar; + public: + enum Modifier { + None = 0, + Ctrl = 1, + Shift = 2, + Alt = 4, + AltGr = 8, + GUI = 16, + }; + Q_DECLARE_FLAGS(Modifiers,Modifier); + + enum KeyType{ + AllKeyRelease = 0, + CharKey = 1, + CapsLock = 2, + NumLock = 3, + Backspace = 4, + Return = 5, + Tabulator = 6, + Space = 7, + ModifierOnly =8, + Escape = 9, + }; + + MKey(){} + MKey(const MKey&k,Modifiers m); + MKey(const MKey&)=default; + MKey(MKey&&)=default; + + MKey(QChar,Modifiers m=None); + MKey(KeyType t,Modifiers m=None):mmod(m),mtype(t){} + + MKey& operator=(const MKey&)=default; + MKey& operator=(MKey&&)=default; + + bool operator==(const MKey&); + bool operator!=(const MKey&k){return !operator==(k);} + + bool isValid()const{return mtype!=AllKeyRelease;} + + Modifiers modifiers()const{return mmod;} + QChar toChar(bool ignoremodifier=false)const; + bool isPrintable(bool ignoremodifier=false)const; + + ///returns the encoded character name as used in layout files + QString toCharName()const; + ///returns a key object matching the encoded character name from a layout file + static MKey fromCharName(QString); + + MKey toLower()const{MKey r(*this);r.mchar=r.mchar.toLower();return r;} + MKey toUpper()const{MKey r(*this);r.mchar=r.mchar.toUpper();return r;} + private: + Modifiers mmod=None; + KeyType mtype=AllKeyRelease; + QChar mchar; }; Q_DECLARE_OPERATORS_FOR_FLAGS(MKey::Modifiers) class MKeySequence:public QList { - public: - MKeySequence(){} - MKeySequence(const QList< MKey >& l):QList< MKey >(l){} - MKeySequence(const MKeySequence& other)=default; - MKeySequence(MKeySequence&&)=default; - - MKeySequence& operator=(const QList&l){QList::operator=(l);return *this;} - MKeySequence& operator=(const MKeySequence&)=default; - MKeySequence& operator=(MKeySequence&&)=default; - - QString toString()const; + public: + MKeySequence(){} + MKeySequence(const QList< MKey >& l):QList< MKey >(l){} + MKeySequence(const MKeySequence& other)=default; + MKeySequence(MKeySequence&&)=default; + + MKeySequence& operator=(const QList&l){QList::operator=(l);return *this;} + MKeySequence& operator=(const MKeySequence&)=default; + MKeySequence& operator=(MKeySequence&&)=default; + + QString toString()const; }; class MHidKeyEvent { - inline static MKey::Modifiers modconv(unsigned char m){ - MKey::Modifiers r=MKey::None; - if(m&0x11)r=MKey::Ctrl; - if(m&0x22)r|=MKey::Shift; - if(m&0x04)r|=MKey::Alt; - if(m&0x40)r|=MKey::AltGr; - if(m&0x88)r|=MKey::GUI; - return r; - } - public: - - MHidKeyEvent(){} - MHidKeyEvent(unsigned char mod,QByteArray keys):mmod(modconv(mod)),mkeys(keys){reduce();} - MHidKeyEvent(const MHidKeyEvent&)=default; - MHidKeyEvent(MHidKeyEvent&&)=default; - - MHidKeyEvent& operator=(const MHidKeyEvent&)=default; - MHidKeyEvent& operator=(MHidKeyEvent&&)=default; - - MKey::Modifiers modifiers()const{return mmod;} - QByteArray keycodes()const{return mkeys;} - QDateTime timestamp()const{return mtime;} - int lastkeycode()const{if(mkeys.size()>0)return mkeys[mkeys.size()-1];else return 0;} - - private: - QDateTime mtime=QDateTime::currentDateTime(); - MKey::Modifiers mmod=MKey::None; - QByteArray mkeys; - void reduce(); + inline static MKey::Modifiers modconv(unsigned char m){ + MKey::Modifiers r=MKey::None; + if(m&0x11)r=MKey::Ctrl; + if(m&0x22)r|=MKey::Shift; + if(m&0x04)r|=MKey::Alt; + if(m&0x40)r|=MKey::AltGr; + if(m&0x88)r|=MKey::GUI; + return r; + } + public: + + MHidKeyEvent(){} + MHidKeyEvent(unsigned char mod,QByteArray keys):mmod(modconv(mod)),mkeys(keys){reduce();} + MHidKeyEvent(const MHidKeyEvent&)=default; + MHidKeyEvent(MHidKeyEvent&&)=default; + + MHidKeyEvent& operator=(const MHidKeyEvent&)=default; + MHidKeyEvent& operator=(MHidKeyEvent&&)=default; + + MKey::Modifiers modifiers()const{return mmod;} + QByteArray keycodes()const{return mkeys;} + QDateTime timestamp()const{return mtime;} + int lastkeycode()const{if(mkeys.size()>0)return mkeys[mkeys.size()-1];else return 0;} + + private: + QDateTime mtime=QDateTime::currentDateTime(); + MKey::Modifiers mmod=MKey::None; + QByteArray mkeys; + void reduce(); }; class MHidKeySequence:public QList { - public: - MHidKeySequence(){} - MHidKeySequence(QList< MHidKeyEvent >& other):QList< MHidKeyEvent >(other){} - MHidKeySequence(const MHidKeySequence&)=default; - MHidKeySequence(MHidKeySequence&&)=default; - - MHidKeySequence& operator=(const MHidKeySequence&)=default; - MHidKeySequence& operator=(MHidKeySequence&&)=default; - - qint64 msSinceLastKey()const; + public: + MHidKeySequence(){} + MHidKeySequence(QList< MHidKeyEvent >& other):QList< MHidKeyEvent >(other){} + MHidKeySequence(const MHidKeySequence&)=default; + MHidKeySequence(MHidKeySequence&&)=default; + + MHidKeySequence& operator=(const MHidKeySequence&)=default; + MHidKeySequence& operator=(MHidKeySequence&&)=default; + + qint64 msSinceLastKey()const; }; class MHidKeyLayout { - public: - ///loads the base layout only - MHidKeyLayout(); - ///loads the specified layout - explicit MHidKeyLayout(QString name); - MHidKeyLayout(const MHidKeyLayout&)=default; - MHidKeyLayout(MHidKeyLayout&&)=default; - - MHidKeyLayout& operator=(const MHidKeyLayout&)=default; - MHidKeyLayout& operator=(MHidKeyLayout&&)=default; - - MKeySequence toKeys(const MHidKeySequence&)const; - QString toString(const MHidKeySequence&)const; - - ///attempts to load a layout file, normally the name mentioned - ///should not contain a complete path or the suffix .kbl - the - ///implementation will automatically search for a matching file; - ///if it does contain a slash it is assumed to be a complete file name - bool loadLayout(QString filename); - bool parseLayout(QString content,QString filename=QString()); - void resetLayout(); - - QString toLayoutDoc()const; - - void setKeyMapping(unsigned char keycode,const QList &c); - void clearKeyMapping(); - - ///returns the human readable name of the layout - QString layoutName()const{return mlname;} - ///returns the iso code of the layout (base of the file name) - ///or an empty string if this is a custom layout - QString layoutCodeName()const; - ///returns the full file name of the layout - QString layoutFileName()const{return mfilename;} - - void setLayoutName(QString l){mlname=l;} - - ///returns true if this is a populated layout - bool isValid()const{return mkeymap.size()>0;} - - static QMap findLayouts(); - private: - QString mfilename,mlname; - QMap>mkeymap; + public: + ///loads the base layout only + MHidKeyLayout(); + ///loads the specified layout + explicit MHidKeyLayout(QString name); + MHidKeyLayout(const MHidKeyLayout&)=default; + MHidKeyLayout(MHidKeyLayout&&)=default; + + MHidKeyLayout& operator=(const MHidKeyLayout&)=default; + MHidKeyLayout& operator=(MHidKeyLayout&&)=default; + + MKeySequence toKeys(const MHidKeySequence&)const; + QString toString(const MHidKeySequence&)const; + + ///attempts to load a layout file, normally the name mentioned + ///should not contain a complete path or the suffix .kbl - the + ///implementation will automatically search for a matching file; + ///if it does contain a slash it is assumed to be a complete file name + bool loadLayout(QString filename); + bool parseLayout(QString content,QString filename=QString()); + void resetLayout(); + + QString toLayoutDoc()const; + + void setKeyMapping(unsigned char keycode,const QList &c); + void clearKeyMapping(); + + ///returns the human readable name of the layout + QString layoutName()const{return mlname;} + ///returns the iso code of the layout (base of the file name) + ///or an empty string if this is a custom layout + QString layoutCodeName()const; + ///returns the full file name of the layout + QString layoutFileName()const{return mfilename;} + + void setLayoutName(QString l){mlname=l;} + + ///returns true if this is a populated layout + bool isValid()const{return mkeymap.size()>0;} + + static QMap findLayouts(); + private: + QString mfilename,mlname; + QMap>mkeymap; }; #endif diff --git a/plugins/bcs-usb/configwidget.cpp b/plugins/bcs-usb/configwidget.cpp index 4a6350d..7abff83 100644 --- a/plugins/bcs-usb/configwidget.cpp +++ b/plugins/bcs-usb/configwidget.cpp @@ -30,20 +30,20 @@ const QString MUsbBarcodeConfig::interfaceKey="interfaceid"; MUsbBarcodeConfig::MUsbBarcodeConfig(MBarcodeConfiguration* cfg) { - QFormLayout *fl; - setLayout(fl=new QFormLayout); - fl->addRow(tr("Detection Interval"),minterval=new QComboBox); - minterval->setEditable(false); - minterval->addItem(tr("Driver Inactive"),0); - minterval->addItem(tr("5 seconds"),5); - minterval->addItem(tr("10 seconds"),10); - minterval->addItem(tr("20 seconds"),20); - minterval->addItem(tr("30 seconds"),30); - QPushButton*p; - fl->addRow(tr("Add Scanner"),p=new QPushButton(tr("add..."))); - connect(p,SIGNAL(clicked(bool)),this,SLOT(addScanner())); - - cfg->addTab(this,tr("USB Settings")); + QFormLayout *fl; + setLayout(fl=new QFormLayout); + fl->addRow(tr("Detection Interval"),minterval=new QComboBox); + minterval->setEditable(false); + minterval->addItem(tr("Driver Inactive"),0); + minterval->addItem(tr("5 seconds"),5); + minterval->addItem(tr("10 seconds"),10); + minterval->addItem(tr("20 seconds"),20); + minterval->addItem(tr("30 seconds"),30); + QPushButton*p; + fl->addRow(tr("Add Scanner"),p=new QPushButton(tr("add..."))); + connect(p,SIGNAL(clicked(bool)),this,SLOT(addScanner())); + + cfg->addTab(this,tr("USB Settings")); } void MUsbBarcodeConfig::saveConfig() @@ -59,8 +59,8 @@ void MUsbBarcodeConfig::addScanner() MUsbScannerConfig::MUsbScannerConfig(MBarcodeConfiguration* cfg, QString id) { - //TODO: use a better name for the tab - cfg->addTab(this,tr("USB Scanner: %1").arg(id)); + //TODO: use a better name for the tab + cfg->addTab(this,tr("USB Scanner: %1").arg(id)); } void MUsbScannerConfig::saveConfig() diff --git a/plugins/bcs-usb/configwidget.h b/plugins/bcs-usb/configwidget.h index b47483b..2a5f7d7 100644 --- a/plugins/bcs-usb/configwidget.h +++ b/plugins/bcs-usb/configwidget.h @@ -1,7 +1,7 @@ // // C++ Interface: plugin for USB barcode scanners // -// Description: +// Description: // // // Author: Konrad Rosenbaum , (C) 2013-2014 @@ -19,37 +19,37 @@ class QComboBox; class MBarcodeConfiguration; class MUsbBarcodeConfig:public QWidget { - Q_OBJECT - public: - MUsbBarcodeConfig(MBarcodeConfiguration*); - - static const QString settingsGroup; - static const QString intervalKey; - static const QString timeoutKey; - static const QString terminatorKey; - static const QString layoutKey; - static const QString nameKey; - static const QString activeKey; - static const QString vendorKey; - static const QString productKey; - static const QString serialKey; - static const QString useSerialKey; - static const QString interfaceKey; + Q_OBJECT + public: + MUsbBarcodeConfig(MBarcodeConfiguration*); - public slots: - void saveConfig(); - void addScanner(); - private: - QComboBox*minterval; + static const QString settingsGroup; + static const QString intervalKey; + static const QString timeoutKey; + static const QString terminatorKey; + static const QString layoutKey; + static const QString nameKey; + static const QString activeKey; + static const QString vendorKey; + static const QString productKey; + static const QString serialKey; + static const QString useSerialKey; + static const QString interfaceKey; + + public slots: + void saveConfig(); + void addScanner(); + private: + QComboBox*minterval; }; class MUsbScannerConfig:public QWidget { - Q_OBJECT - public: - MUsbScannerConfig(MBarcodeConfiguration*,QString); - public slots: - void saveConfig(); + Q_OBJECT + public: + MUsbScannerConfig(MBarcodeConfiguration*,QString); + public slots: + void saveConfig(); }; #endif diff --git a/plugins/bcs-usb/hidscanner.cpp b/plugins/bcs-usb/hidscanner.cpp index 78c5168..7835b68 100644 --- a/plugins/bcs-usb/hidscanner.cpp +++ b/plugins/bcs-usb/hidscanner.cpp @@ -16,98 +16,101 @@ #include MHidBarcodeScanner::MHidBarcodeScanner(QString path,QString setGrp, QObject* parent) - : MBarcodeScanner(parent),mpath(path) + : MBarcodeScanner(parent),mpath(path) { - qDebug()<<"Trying to open device at"<setSingleShot(false); - mrdtmr->start(20); - connect(mrdtmr,SIGNAL(timeout()),this,SLOT(readData())); - emit activated(); + if(mdev!=nullptr && mrdtmr!=nullptr)return; + mdev=hid_open_path(mpath.toLocal8Bit().data()); + if(!mdev){ + qDebug()<<"Unable to open device."; + deleteLater(); + return; + } + hid_set_nonblocking(mdev,1); + mrdtmr=new QTimer(this); + mrdtmr->setSingleShot(false); + mrdtmr->start(20); + connect(mrdtmr,SIGNAL(timeout()),this,SLOT(readData())); + qDebug()<<"Successfully opened scanner"<0)processData(); - return; - } - //read data - unsigned char buf[64]; - memset(buf,0,sizeof(buf)); - while(true){ - const int rs=hid_read(mdev,buf,sizeof(buf)); - if(rs<0){ - qDebug()<<"Error reading from USB device"<=3 bytes, got"<0)processData(); + return; + } + //read data + unsigned char buf[64]; + memset(buf,0,sizeof(buf)); + while(true){ + const int rs=hid_read(mdev,buf,sizeof(buf)); + if(rs<0){ + qDebug()<<"Error reading from USB device"<=3 bytes, got"<0 && mseq.msSinceLastKey()>=mtimeout) - force=true; - //can we see one of the termination codes? - if(!force && !mterminator.isValid())return; - MKeySequence kseq=mlayout.toKeys(mseq); - if(!force && !kseq.contains(mterminator))return; - //convert - QString bc=kseq.toString(); - mseq.clear(); - //remove superfluous chars - //verify checksum and remove it - //emit barcode - qDebug()<<"scanned barcode"<0 && mseq.msSinceLastKey()>=mtimeout) + force=true; + //can we see one of the termination codes? + if(!force && !mterminator.isValid())return; + MKeySequence kseq=mlayout.toKeys(mseq); + if(!force && !kseq.contains(mterminator))return; + //convert + QString bc=kseq.toString(); + mseq.clear(); + //remove superfluous chars + //verify checksum and remove it + //emit barcode + qDebug()<<"scanned barcode"<, (C) 2013-2014 @@ -22,30 +22,30 @@ class QTimer; class MHidBarcodeScanner:public MBarcodeScanner { - Q_OBJECT - public: - explicit MHidBarcodeScanner(QString path,QString setGrp,QObject* parent = 0); - virtual ~MHidBarcodeScanner(); - - QString readableName()const override; - bool isActive()const override{return mdev!=nullptr;} - - void activate() override; - void deactivate() override; - - bool matchPath(const QString &p){return mpath==p;} - - private slots: - void readData(); - void processData(); - private: - hid_device*mdev=nullptr; - QTimer*mrdtmr=nullptr; - QString mpath,mname; - MHidKeySequence mseq; - MHidKeyLayout mlayout; - int mtimeout=0; - MKey mterminator; + Q_OBJECT + public: + explicit MHidBarcodeScanner(QString path,QString setGrp,QObject* parent = 0); + virtual ~MHidBarcodeScanner(); + + QString readableName()const override; + bool isActive()const override{return mdev!=nullptr;} + + void activate() override; + void deactivate() override; + + bool matchPath(const QString &p){return mpath==p;} + + private slots: + void readData(); + void processData(); + private: + hid_device*mdev=nullptr; + QTimer*mrdtmr=nullptr; + QString mpath,mname; + MHidKeySequence mseq; + MHidKeyLayout mlayout; + int mtimeout=0; + MKey mterminator; }; #endif -- 1.7.2.5