Permalink
Browse files

Kb indicator plugin simplified

  • Loading branch information...
1 parent 6f48e0c commit d0004b3ffb5df7e3c050379bd60756a0402c44e2 @kuzmas kuzmas committed Mar 11, 2013
@@ -5,19 +5,16 @@ set(PLUGIN "panelkbindicator")
set(HEADERS
razorkbindicator.h
- razorkbindicatoreventfilter.h
razorkbindicatorconfiguration.h
)
set(SOURCES
razorkbindicator.cpp
- razorkbindicatoreventfilter.cpp
razorkbindicatorconfiguration.cpp
)
set(MOCS
razorkbindicator.h
- razorkbindicatoreventfilter.h
razorkbindicatorconfiguration.h
)
@@ -27,27 +27,38 @@
#include "razorkbindicator.h"
-#include <QtGui/QLabel>
+#include <X11/XKBlib.h>
-#include "razorkbindicatoreventfilter.h"
+#include <QtGui/QLabel>
+#include <QtGui/QX11Info>
EXPORT_RAZOR_PANEL_PLUGIN_CPP(RazorKbIndicator)
RazorKbIndicator::RazorKbIndicator(const RazorPanelPluginStartInfo *startInfo, QWidget *parent):
RazorPanelPlugin(startInfo, parent),
- content(new QLabel(this)),
- eventFilter(RazorKbIndicatorEventFilter::instance())
+ mContent(new QLabel(this))
{
setObjectName("KbIndicator");
- connect(eventFilter, SIGNAL(indicatorsChanged(uint,uint)), this, SLOT(setIndicators(uint,uint)));
+ connect(this, SIGNAL(indicatorsChanged(uint,uint)), this, SLOT(setIndicators(uint,uint)));
+
+ addWidget(mContent);
- addWidget(content);
+ int code;
+ int major = XkbMajorVersion;
+ int minor = XkbMinorVersion;
+ int XkbErrorBase;
+
+ mDisplay = QX11Info::display();
+
+ if (XkbLibraryVersion(&major, &minor))
+ if (XkbQueryExtension(mDisplay, &code, &mXkbEventBase, &XkbErrorBase, &major, &minor))
+ if (XkbUseExtension(mDisplay, &major, &minor))
+ XkbSelectEvents(mDisplay, XkbUseCoreKbd, XkbIndicatorStateNotifyMask, XkbIndicatorStateNotifyMask);
settingsChanged();
- content->setEnabled(eventFilter->getLockStatus(bit));
}
RazorKbIndicator::~RazorKbIndicator()
@@ -56,9 +67,9 @@ RazorKbIndicator::~RazorKbIndicator()
void RazorKbIndicator::settingsChanged()
{
- bit = settings().value("bit", 0).toInt();
- content->setText(settings().value("text", QString("C")).toString());
- content->setEnabled(eventFilter->getLockStatus(bit));
+ mBit = settings().value("bit", 0).toInt();
+ mContent->setText(settings().value("text", QString("C")).toString());
+ mContent->setEnabled(getLockStatus(mBit));
}
void RazorKbIndicator::showConfigureDialog()
@@ -75,6 +86,27 @@ void RazorKbIndicator::showConfigureDialog()
void RazorKbIndicator::setIndicators(unsigned int changed, unsigned int state)
{
- if (changed & (1 << bit))
- content->setEnabled(state & (1 << bit));
+ if (changed & (1 << mBit))
+ mContent->setEnabled(state & (1 << mBit));
+}
+
+bool RazorKbIndicator::getLockStatus(int bit)
+{
+ bool state = false;
+ if (mDisplay)
+ {
+ unsigned n;
+ XkbGetIndicatorState(mDisplay, XkbUseCoreKbd, &n);
+ state = (n & (1 << bit));
+ }
+ return state;
+}
+
+void RazorKbIndicator::x11EventFilter(XEvent* event)
+{
+ XkbEvent* xkbEvent = reinterpret_cast<XkbEvent*>(event);
+
+ if (xkbEvent->type == mXkbEventBase + XkbEventCode)
+ if (xkbEvent->any.xkb_type == XkbIndicatorStateNotify)
+ emit indicatorsChanged(xkbEvent->indicators.changed, xkbEvent->indicators.state);
}
@@ -33,7 +33,6 @@
class QLabel;
-class RazorKbIndicatorEventFilter;
class RazorKbIndicator : public RazorPanelPlugin
{
@@ -44,18 +43,26 @@ class RazorKbIndicator : public RazorPanelPlugin
virtual RazorPanelPlugin::Flags flags() const { return PreferRightAlignment | HaveConfigDialog; }
+signals:
+ void indicatorsChanged(unsigned int, unsigned int);
+
protected slots:
virtual void settingsChanged();
virtual void showConfigureDialog();
void setIndicators(unsigned int, unsigned int);
+protected:
+ bool getLockStatus(int mBit);
+ virtual void x11EventFilter(XEvent* event);
+
private:
- QLabel *content;
+ QLabel *mContent;
- int bit;
+ int mBit;
- RazorKbIndicatorEventFilter *eventFilter;
+ Display *mDisplay;
+ int mXkbEventBase;
};
@@ -1,104 +0,0 @@
-/* BEGIN_COMMON_COPYRIGHT_HEADER
- * (c)LGPL2+
- *
- * Razor - a lightweight, Qt based, desktop toolset
- * http://razor-qt.org
- *
- * Copyright: 2012 Razor team
- * Authors:
- * Kuzma Shapran <kuzma.shapran@gmail.com>
- *
- * This program or library is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
-
- * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA
- *
- * END_COMMON_COPYRIGHT_HEADER */
-
-#include "razorkbindicatoreventfilter.h"
-
-#include <X11/XKBlib.h>
-
-
-static RazorKbIndicatorEventFilter *filterObject = NULL;
-
-
-bool X11_eventFilter(void *message)
-{
- return filterObject ? filterObject->X11_eventFilter(message) : false;
-}
-
-RazorKbIndicatorEventFilter* RazorKbIndicatorEventFilter::instance()
-{
- if (!filterObject)
- new RazorKbIndicatorEventFilter();
- return filterObject;
-}
-
-RazorKbIndicatorEventFilter::RazorKbIndicatorEventFilter(QObject *parent):
- QObject(parent),
- oldEventFilter(0)
-{
- if (!filterObject)
- {
- filterObject = this;
-
- display = QX11Info::display();
-
- if (initXkbExtension())
- if (XkbSelectEvents(display, XkbUseCoreKbd, XkbIndicatorStateNotifyMask, XkbIndicatorStateNotifyMask))
- oldEventFilter = QAbstractEventDispatcher::instance()->setEventFilter(::X11_eventFilter);
- }
-}
-
-bool RazorKbIndicatorEventFilter::initXkbExtension()
-{
- int code;
- int major = XkbMajorVersion;
- int minor = XkbMinorVersion;
- int XkbErrorBase;
-
- if (!XkbLibraryVersion(&major, &minor))
- return false;
- if (!XkbQueryExtension(display, &code, &XkbEventBase, &XkbErrorBase, &major, &minor))
- return false;
- if (!XkbUseExtension(display, &major, &minor))
- return false;
- return true;
-}
-
-bool RazorKbIndicatorEventFilter::X11_eventFilter(void *message)
-{
- XkbEvent* event = static_cast<XkbEvent*>(message);
-
- if (event->type == XkbEventBase + XkbEventCode)
- if (event->any.xkb_type == XkbIndicatorStateNotify)
- emit indicatorsChanged(event->indicators.changed, event->indicators.state);
-
- if (oldEventFilter)
- return oldEventFilter(message);
-
- return false;
-}
-
-bool RazorKbIndicatorEventFilter::getLockStatus(int bit)
-{
- bool state = false;
- if (display)
- {
- unsigned n;
- XkbGetIndicatorState(display, XkbUseCoreKbd, &n);
- state = (n & (1 << bit));
- }
- return state;
-}
@@ -1,61 +0,0 @@
-/* BEGIN_COMMON_COPYRIGHT_HEADER
- * (c)LGPL2+
- *
- * Razor - a lightweight, Qt based, desktop toolset
- * http://razor-qt.org
- *
- * Copyright: 2012 Razor team
- * Authors:
- * Kuzma Shapran <kuzma.shapran@gmail.com>
- *
- * This program or library is free software; you can redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
-
- * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA
- *
- * END_COMMON_COPYRIGHT_HEADER */
-
-#ifndef RAZORPANELKBINDICATOREVENTFILTER_H
-#define RAZORPANELKBINDICATOREVENTFILTER_H
-
-#include <QtCore/QAbstractEventDispatcher>
-
-#include <QtGui/QX11Info>
-
-
-class RazorKbIndicatorEventFilter : public QObject
-{
- Q_OBJECT
-public:
- RazorKbIndicatorEventFilter(QObject *parent = NULL);
-
- bool getLockStatus(int bit);
-
- static RazorKbIndicatorEventFilter* instance();
-
-signals:
- void indicatorsChanged(unsigned int, unsigned int);
-
-private:
- friend bool X11_eventFilter(void*);
-
- Display *display;
- int XkbEventBase;
- QAbstractEventDispatcher::EventFilter oldEventFilter;
-
- bool initXkbExtension();
- bool X11_eventFilter(void *message);
-};
-
-
-#endif // RAZORPANELKBINDICATOREVENTFILTER_H

0 comments on commit d0004b3

Please sign in to comment.