一、 對於每一種類型的插件,通常至少需要兩個類:
(1.) 一個是
插件封裝器類,它實現了插件通用的API函數;
比如QWSMouseHandler,其定義如下:
- class Q_GUI_EXPORT QWSMouseHandler
- {
- public:
- explicit QWSMouseHandler(const QString &driver = QString(),
- const QString &device = QString());
- virtual ~QWSMouseHandler();
-
-
- virtual void clearCalibration() {}
- virtual void calibrate(const QWSPointerCalibrationData *) {}
- virtual void getCalibration(QWSPointerCalibrationData *) const {}
-
-
- virtual void resume() = 0;
- virtual void suspend() = 0;
-
-
- void limitToScreen(QPoint &pt);
- void mouseChanged(const QPoint& pos, int bstate, int wheel = 0);
- const QPoint &pos() const { return mousePos; }
-
-
- void setScreen(const QScreen *screen);
-
-
- protected:
- QPoint &mousePos;
- QWSMouseHandlerPrivate *d_ptr;
- }
該類實現了在嵌入式平台上,一個鼠標設備所具有的屬性和方法。
(2.)一個是一個或多個
處理器類,每個處理器類都實現了一種用於特殊類型插件的API;
比如QWSLinuxInputMouseHandler,兩者的關系如下:
QWSLinuxInputMouseHandler --> QWSCalibratedMouseHandler --> QWSMouseHandler
通過封裝器類才能訪問這些處理器類。 除此之外往往還會有一個名稱含有"Factory"的類來在程序運行的時候自動創建合適的插件對象。
比如,QMouseDriverFactory,其定義如下:
- class Q_GUI_EXPORT QMouseDriverFactory
- {
- public:
- static QStringList keys();
- static QWSMouseHandler *create(const QString&, const QString &);
- };
create的實現如下:
- QWSMouseHandler *QMouseDriverFactory::create(const QString& key, const QString &device){
- QString driver = key.toLower();
- #if defined(Q_OS_QNX) && !defined(QT_NO_QWS_MOUSE_QNX)
- if (driver == QLatin1String("qnx") || driver.isEmpty())
- return new QQnxMouseHandler(key, device);
- #endif
- #if defined(Q_OS_INTEGRITY) && !defined(QT_NO_MOUSE_INTEGRITY)
- if (driver == QLatin1String("integrity") || driver.isEmpty())
- return new QIntMouseHandler(key, device);
- #endif
- #ifndef QT_NO_QWS_MOUSE_LINUXTP
- if (driver == QLatin1String("linuxtp") || driver.isEmpty())
- return new QWSLinuxTPMouseHandler(key, device);
- #endif
- #ifndef QT_NO_QWS_MOUSE_PC
- if (driver == QLatin1String("auto")
- || driver == QLatin1String("intellimouse")
- || driver == QLatin1String("microsoft")
- || driver == QLatin1String("mousesystems")
- || driver == QLatin1String("mouseman")
- || driver.isEmpty()) {
- return new QWSPcMouseHandler(key, device);
- }
- #endif
- #ifndef QT_NO_QWS_MOUSE_TSLIB
- if (driver == QLatin1String("tslib") || driver.isEmpty())
- return new QWSTslibMouseHandler(key, device);
- #endif
- # ifndef QT_NO_QWS_MOUSE_LINUXINPUT
- if (driver == QLatin1String("linuxinput") || \
- driver == QLatin1String("usb") || \
- driver == QLatin1String("linuxis"))
- return new QWSLinuxInputMouseHandler(device);
- # endif
- #ifndef QT_NO_QWS_MOUSE_QVFB
- if (driver == QLatin1String("qvfbmouse") || driver == QLatin1String("qvfb"))
- return new QVFbMouseHandler(key, device);
- #endif
-
- #if !defined(Q_OS_WIN32) || defined(QT_MAKEDLL)
- #ifndef QT_NO_LIBRARY
- if (QWSMouseHandlerFactoryInterface *factory = qobject_cast<QWSMouseHandlerFactoryInterface*>(loader()->instance(driver)))
- return factory->create(driver, device); /*注意:這個地方就會創建QMouseDriverPlugin實現的自定義插件類*/
- #endif
- #endif
- return 0;
- }
注意:QMouseDriverFactory用於探測並且實例化可用的鼠標驅動,Embeded QT for Linux 在server application運行的時候加載合適的鼠標驅動。 Embeded QT for Linux提供幾個內置的類來實現某些鼠標驅動,比如QWSLinuxInputMouseHandler ;另外,也支持使用Qt的插件機制來實現自定義的驅動程序,驅動程序的編寫也要繼承QWSMouseHandler來具體的進行實現。
二、使用QMouseDriverPlugin來創建自定義的鼠標驅動程序
下面的文字來自於Qt的源文檔:
- /*!
- \class QMouseDriverPlugin
- \ingroup plugins
- \ingroup qws
-
- \brief The QMouseDriverPlugin class is an abstract base class for
- mouse driver plugins in Qt for Embedded Linux.
-
- Note that this class is only available in \l{Qt for Embedded Linux}.
-
- \l{Qt for Embedded Linux} provides ready-made drivers for several mouse
- protocols, see the \l{Qt for Embedded Linux Pointer Handling}{pointer
- handling} documentation for details. Custom mouse drivers can be
- implemented by subclassing the QWSMouseHandler class and creating
- a mouse driver plugin.
-
- A mouse driver plugin can be created by subclassing
- QMouseDriverPlugin and reimplementing the pure virtual keys() and
- create() functions. By exporting the derived class using the
- Q_EXPORT_PLUGIN2() macro, The default implementation of the
- QMouseDriverFactory class will automatically detect the plugin and
- load the driver into the server application at run-time. See \l
- {How to Create Qt Plugins} for details.
-
- \sa QWSMouseHandler, QMouseDriverFactory
- */
- /*!
- \fn QStringList QMouseDriverPlugin::keys() const
-
- Implement this function to return the list of valid keys, i.e. the
- mouse drivers supported by this plugin.
-
- \l{Qt for Embedded Linux} provides ready-made drivers for several mouse
- protocols, see the \l {Qt for Embedded Linux Pointer Handling}{pointer
- handling} documentation for details.
-
- \sa create()
- */
- /*!
- Constructs a mouse driver plugin with the given \a parent.
-
- Note that this constructor is invoked automatically by the
- Q_EXPORT_PLUGIN2() macro, so there is no need for calling it
- explicitly.
- */
具體的創建plugin的實例參見:
《How to Create Qt Plugins》
《Qt for Embedded Linux Pointer Handling》