mykbdplugin.h //插件的实现文件
#ifndef MYKBDPLUGIN_H
#define MYKBDPLUGIN_H
#include <QtGui/QWSKeyboardHandlerFactoryInterface>
#include "mykeyinterface.h" //插件接口头文件
class MyKbdPlugin : public QKbdDriverPlugin ,public MyKeyInterface
{
Q_OBJECT
Q_INTERFACES(MyKeyInterface)
public:
MyKbdPlugin(QObject *parent = 0);
~MyKbdPlugin();
QWSKeyboardHandler* create(const QString& driver, const QString& device);
QStringList keys()const;
};
#endif // MYKBDPLUGIN_H
mykbdplugin.cpp //插件实现文件
#include"mykbdhandler.h"
#include "mykbdplugin.h"
MyKbdPlugin::MyKbdPlugin ( QObject *parent)
: QKbdDriverPlugin( parent )
{
}
MyKbdPlugin::~MyKbdPlugin()
{
}
QWSKeyboardHandler* MyKbdPlugin::create(const QString &driver, const QString &device)
{
if( driver.toLower() == "mykeyboard" ) {
printf("Before call MyKbdHandler %s",device);
return new MyKbdHandler(device);
}
return 0;
}
QStringList MyKbdPlugin::keys() const
{
return QStringList() << "mykeyboard";
}
Q_EXPORT_PLUGIN2(mykbd, MyKbdPlugin)
mykbdhandler.h //插件处理头文件
#ifndef MYKBDHANDLER_H
#define MYKBDHANDLER_H
#include <QObject>
#include <QWSKeyboardHandler>
class QSocketNotifier;
class MyKbdHandler : public QObject,public QWSKeyboardHandler
{
Q_OBJECT
public:
MyKbdHandler(const QString &device);
~MyKbdHandler();
private:
QSocketNotifier *m_notify;
int kbdFD;
private Q_SLOTS:
void readKbdData();
};
#endif // MYKBDHANDLER_H
mykbdhandler.cpp //插件处理实现
#include"mykbdhandler.h"
#include <QSocketNotifier>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <linux/input.h>
#include <linux/kd.h>
MyKbdHandler::MyKbdHandler(const QString &device)
{
printf("loaded mykbd plugin!\n");
setObjectName( "mykbd Handler" );
kbdFD=::open(device.toLocal8Bit().constData(),O_RDONLY,0);
if(kbdFD==-1)
{
qWarning("Cannot open '%s' for keypad (%s)",
device.toLocal8Bit().constData(), strerror(errno));
}
else
{
printf("Opend %s as mykbd",device.toLocal8Bit().constData());
m_notify = new QSocketNotifier( kbdFD, QSocketNotifier::Read, this );
connect( m_notify, SIGNAL(activated(int)), this, SLOT(readKbdData()));
}
return;
}
MyKbdHandler::~MyKbdHandler()
{
if(kbdFD>=0)
{
close(kbdFD);
kbdFD=-1;
}
return;
}
void MyKbdHandler::readKbdData()
{
int key_value[4];
int i;
fd_set rds;
int ret;
FD_ZERO(&rds);
FD_SET(kbdFD,&rds);
ret=select(kbdFD+1,&rds,NULL,NULL,NULL);
if (ret <0)
{
perror("select");
exit(1);
}
if (ret ==0)
{
printf("Timeout.\n");
}
else if(FD_ISSET(kbdFD,&rds))
{
ret = read(kbdFD, key_value, sizeof(key_value));
if (ret != sizeof(key_value))
{
if(errno!=EAGAIN)
perror("read buttons:\n");
exit(1);
}
else
{
Qt::KeyboardModifiers modifiers=Qt::NoModifier;
int unicode=0xffff;
int key_code=0;
for(i=0;i<4;i++)
{
switch(key_value[i])
{
case 1:
key_code=Qt::Key_Up;
unicode=0xffff;
break;
case 2:
key_code=Qt::Key_Down;
unicode=0xffff;
break;
case 3:
key_code=Qt::Key_Left;
unicode=0xffff;
break;
case 4:
key_code=Qt::Key_Right;
unicode=0xffff;
break;
default:
break;
}
processKeyEvent(unicode, key_code, (Qt::KeyboardModifiers)modifiers,
key_value[i]!=0, false);
}
}
}
return ;
}
生成libkbd.so
实现文件
mykeyinterface.h //插件接口头文件代码
#ifndef MYKEYINTERFACE_H
#define MYKEYINTERFACE_H
#include <QString>
#include <QtGui/QWSKeyboardHandlerFactoryInterface>
#include <QObject>
#include <QWSKeyboardHandler>
#include <QtPlugin>
class QWSKeyboardHandler;
class QStringList;
class MyKeyInterface
{
public:
virtual ~MyKeyInterface();
virtual QStringList keys() const=0;
virtual QWSKeyboardHandler* create(const QString& driver, const QString& device);
};
QT_BEGIN_NAMESPACE
Q_DECLARE_INTERFACE(MyKeyInterface,
"wujianmei.myKey.MyKeyInterface/1.0");
QT_END_NAMESPACE
#endif // MYKEYINTERFACE_H
mykey.h //使用插件的程序
#ifndef MYKEY_H
#define MYKEY_H
#include <QWidget>
#include "mykeyinterface.h"
namespace Ui {
class MyKey;
}
class MyKey : public QWidget {
Q_OBJECT
public:
MyKey(QWidget *parent = 0);
~MyKey();
protected:
void changeEvent(QEvent *e);
private:
Ui::MyKey *ui;
MyKeyInterface *mykeyinterface;
bool loadPlugin();
private slots:
void achieveKey();
};
#endif // MYKEY_H
mykey.cpp
#include "mykey.h"
#include "ui_mykey.h"
#include <QTimer>
#include <QtGui>
#include <qdebug.h>
MyKey::MyKey(QWidget *parent) :
QWidget(parent),
ui(new Ui::MyKey)
{
ui->setupUi(this);
ui->pushButton->setFocus();
printf("inital!\n");
if (!loadPlugin()) {
QMessageBox::information(this, "Error", "Could not load the plugin");
}
QTimer *timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(achieveKey()));
timer->start(10000);
}
MyKey::~MyKey()
{
delete ui;
}
void MyKey::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
bool MyKey::loadPlugin()
{
QDir pluginsDir(qApp->applicationDirPath());
pluginsDir.cd("/opt/qt-4.5/plugins");
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
printf("fileNmae is %s!\n",fileName.toLocal8Bit().constData());
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
qDebug()<<pluginsDir.absoluteFilePath(fileName);
QObject *plugin = pluginLoader.instance();
qDebug()<<plugin;
if (plugin){
mykeyinterface = qobject_cast<MyKeyInterface *>(plugin);
if (mykeyinterface)
return true;
}
}
return false;
}
void MyKey::achieveKey()
{
const QString &device = QString("/dev/myself-button1") ;
foreach (QString key,mykeyinterface->keys())
{
mykeyinterface->create(key,device);
printf("key is %s!\n",key.toLocal8Bit().constData());
}
}