KUtils
widgetbasedpoller.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2009 Dario Freddi <drf at kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "widgetbasedpoller.h" 00020 00021 #include <QWidget> 00022 #include <QTimer> 00023 #include <QEvent> 00024 00025 #ifndef WIN32 00026 #include <fixx11h.h> 00027 #endif 00028 00029 WidgetBasedPoller::WidgetBasedPoller(QWidget *parent) 00030 : AbstractSystemPoller(parent) 00031 { 00032 } 00033 00034 WidgetBasedPoller::~WidgetBasedPoller() 00035 { 00036 } 00037 00038 bool WidgetBasedPoller::isAvailable() 00039 { 00040 return true; 00041 } 00042 00043 bool WidgetBasedPoller::setUpPoller() 00044 { 00045 m_pollTimer = new QTimer(this); 00046 00047 //setup idle timer, with some smart polling 00048 connect(m_pollTimer, SIGNAL(timeout()), this, SLOT(poll())); 00049 00050 // This code was taken from Lithium/KDE4Powersave 00051 m_grabber = new QWidget(0, Qt::X11BypassWindowManagerHint); 00052 m_grabber->move(-1000, -1000); 00053 m_grabber->setMouseTracking(true); 00054 m_grabber->installEventFilter(this); 00055 m_grabber->setObjectName("KIdleGrabberWidget"); 00056 00057 return additionalSetUp(); 00058 } 00059 00060 void WidgetBasedPoller::unloadPoller() 00061 { 00062 m_pollTimer->deleteLater(); 00063 m_grabber->deleteLater(); 00064 } 00065 00066 QList<int> WidgetBasedPoller::timeouts() const 00067 { 00068 return m_timeouts; 00069 } 00070 00071 void WidgetBasedPoller::addTimeout(int nextTimeout) 00072 { 00073 m_timeouts.append(nextTimeout); 00074 poll(); 00075 } 00076 00077 bool WidgetBasedPoller::eventFilter(QObject *object, QEvent *event) 00078 { 00079 if (object == m_grabber 00080 && (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress)) { 00081 detectedActivity(); 00082 return true; 00083 } else if (object != m_grabber) { 00084 // If it's not the grabber, fallback to default event filter 00085 return false; 00086 } 00087 00088 // Otherwise, simply ignore it 00089 return false; 00090 00091 } 00092 00093 void WidgetBasedPoller::waitForActivity() 00094 { 00095 // This code was taken from Lithium/KDE4Powersave 00096 00097 m_grabber->show(); 00098 m_grabber->grabMouse(); 00099 m_grabber->grabKeyboard(); 00100 00101 } 00102 00103 void WidgetBasedPoller::detectedActivity() 00104 { 00105 stopCatchingIdleEvents(); 00106 emit resumingFromIdle(); 00107 } 00108 00109 void WidgetBasedPoller::releaseInputLock() 00110 { 00111 m_grabber->releaseMouse(); 00112 m_grabber->releaseKeyboard(); 00113 m_grabber->hide(); 00114 } 00115 00116 int WidgetBasedPoller::poll() 00117 { 00118 int idle = getIdleTime(); 00119 00120 // Check if we reached a timeout.. 00121 foreach(int i, m_timeouts) { 00122 if ((i - idle < 300 && i > idle) || (idle - i < 300 && idle > i)) { 00123 // Bingo! 00124 emit timeoutReached(i); 00125 } 00126 } 00127 00128 // Let's check the timer now! 00129 int mintime = 0; 00130 00131 foreach(int i, m_timeouts) { 00132 if (i > idle && (i < mintime || mintime == 0)) { 00133 mintime = i; 00134 } 00135 } 00136 00137 //qDebug() << "mintime " << mintime << "idle " << idle; 00138 00139 if (mintime != 0) { 00140 m_pollTimer->start(mintime - idle); 00141 } else { 00142 m_pollTimer->stop(); 00143 } 00144 00145 return idle; 00146 } 00147 00148 int WidgetBasedPoller::forcePollRequest() 00149 { 00150 return poll(); 00151 } 00152 00153 void WidgetBasedPoller::removeTimeout(int timeout) 00154 { 00155 m_timeouts.removeOne(timeout); 00156 poll(); 00157 } 00158 00159 void WidgetBasedPoller::catchIdleEvent() 00160 { 00161 waitForActivity(); 00162 } 00163 00164 void WidgetBasedPoller::stopCatchingIdleEvents() 00165 { 00166 releaseInputLock(); 00167 } 00168 00169 #include "widgetbasedpoller.moc"
KDE 4.6 API Reference