dtor first
[personal-kdebase.git] / workspace / powerdevil / daemon / WidgetBasedPoller.cpp
blobe0eb38b442e42d18ef3d29452f499ee8d3db156d
1 /***************************************************************************
2 * Copyright (C) 2008 by Dario Freddi <drf@kdemod.ath.cx> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 **************************************************************************/
20 #include "WidgetBasedPoller.h"
22 #include <QWidget>
23 #include <QTimer>
24 #include <QX11Info>
25 #include <QEvent>
27 #include <klocalizedstring.h>
29 WidgetBasedPoller::WidgetBasedPoller(QObject *parent)
30 : AbstractSystemPoller(parent)
34 WidgetBasedPoller::~WidgetBasedPoller()
38 QString WidgetBasedPoller::name()
40 return i18n("Grabber Widget Based");
43 bool WidgetBasedPoller::isAvailable()
45 #ifdef HAVE_XSCREENSAVER
46 return true;
47 #else
48 return false;
49 #endif
52 bool WidgetBasedPoller::setUpPoller()
54 m_pollTimer = new QTimer(this);
56 //setup idle timer, with some smart polling
57 connect(m_pollTimer, SIGNAL(timeout()), this, SLOT(poll()));
59 // This code was taken from Lithium/KDE4Powersave
60 m_grabber = new QWidget(0, Qt::X11BypassWindowManagerHint);
61 m_grabber->move(-1000, -1000);
62 m_grabber->setMouseTracking(true);
63 m_grabber->installEventFilter(this);
64 m_grabber->setObjectName("PowerDevilGrabberWidget");
66 m_screenSaverIface = new OrgFreedesktopScreenSaverInterface("org.freedesktop.ScreenSaver", "/ScreenSaver",
67 QDBusConnection::sessionBus(), this);
69 connect(m_screenSaverIface, SIGNAL(ActiveChanged(bool)), SLOT(screensaverActivated(bool)));
71 return true;
74 void WidgetBasedPoller::unloadPoller()
76 m_pollTimer->deleteLater();
77 m_grabber->deleteLater();
80 void WidgetBasedPoller::setNextTimeout(int nextTimeout)
82 m_pollTimer->start(nextTimeout);
85 void WidgetBasedPoller::screensaverActivated(bool activated)
87 // We care only if it has been disactivated
89 if (!activated) {
90 m_screenSaverIface->SimulateUserActivity();
91 emit resumingFromIdle();
95 bool WidgetBasedPoller::eventFilter(QObject * object, QEvent * event)
97 if (object == m_grabber
98 && (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress)) {
99 detectedActivity();
100 return true;
101 } else if (object != m_grabber) {
102 // If it's not the grabber, fallback to default event filter
103 return false;
106 // Otherwise, simply ignore it
107 return false;
111 void WidgetBasedPoller::waitForActivity()
113 // This code was taken from Lithium/KDE4Powersave
115 m_grabber->show();
116 m_grabber->grabMouse();
117 m_grabber->grabKeyboard();
121 void WidgetBasedPoller::detectedActivity()
123 emit resumingFromIdle();
126 void WidgetBasedPoller::releaseInputLock()
128 m_grabber->releaseMouse();
129 m_grabber->releaseKeyboard();
130 m_grabber->hide();
133 #ifdef HAVE_XSCREENSAVER
134 #include <X11/extensions/scrnsaver.h>
135 #endif
137 void WidgetBasedPoller::poll()
139 /* Hack! Since KRunner still doesn't behave properly, the
140 * correct way to go doesn't work (yet), and it's this one:
141 ------------------------------------------------------------
142 int idle = m_screenSaverIface->GetSessionIdleTime();
143 ------------------------------------------------------------
145 /// In the meanwhile, this X11 hackish way gets its job done.
146 //----------------------------------------------------------
148 int idle = 0;
150 #ifdef HAVE_XSCREENSAVER
151 XScreenSaverInfo * mitInfo = 0;
152 mitInfo = XScreenSaverAllocInfo();
153 XScreenSaverQueryInfo(QX11Info::display(), DefaultRootWindow(QX11Info::display()), mitInfo);
154 idle = mitInfo->idle / 1000;
155 //----------------------------------------------------------
156 #endif
158 emit pollRequest(idle);
161 void WidgetBasedPoller::forcePollRequest()
163 poll();
166 void WidgetBasedPoller::stopCatchingTimeouts()
168 m_pollTimer->stop();
171 void WidgetBasedPoller::catchIdleEvent()
173 waitForActivity();
176 void WidgetBasedPoller::stopCatchingIdleEvents()
178 releaseInputLock();
181 #include "WidgetBasedPoller.moc"