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 #include <QDebug> 00025 #ifndef WIN32 00026 #include <fixx11h.h> 00027 #endif 00028 WidgetBasedPoller::WidgetBasedPoller(QWidget *parent) 00029 : AbstractSystemPoller(parent) 00030 { 00031 } 00032 00033 WidgetBasedPoller::~WidgetBasedPoller() 00034 { 00035 } 00036 00037 bool WidgetBasedPoller::isAvailable() 00038 { 00039 return true; 00040 } 00041 00042 bool WidgetBasedPoller::setUpPoller() 00043 { 00044 m_pollTimer = new QTimer(this); 00045 00046 //setup idle timer, with some smart polling 00047 connect(m_pollTimer, SIGNAL(timeout()), this, SLOT(poll())); 00048 00049 // This code was taken from Lithium/KDE4Powersave 00050 m_grabber = new QWidget(0, Qt::X11BypassWindowManagerHint); 00051 m_grabber->move(-1000, -1000); 00052 m_grabber->setMouseTracking(true); 00053 m_grabber->installEventFilter(this); 00054 m_grabber->setObjectName("KIdleGrabberWidget"); 00055 00056 return additionalSetUp(); 00057 } 00058 00059 void WidgetBasedPoller::unloadPoller() 00060 { 00061 m_pollTimer->deleteLater(); 00062 m_grabber->deleteLater(); 00063 } 00064 00065 QList<int> WidgetBasedPoller::timeouts() const 00066 { 00067 return m_timeouts; 00068 } 00069 00070 void WidgetBasedPoller::addTimeout(int nextTimeout) 00071 { 00072 m_timeouts.append(nextTimeout); 00073 poll(); 00074 } 00075 00076 bool WidgetBasedPoller::eventFilter(QObject *object, QEvent *event) 00077 { 00078 if (object == m_grabber 00079 && (event->type() == QEvent::MouseMove || event->type() == QEvent::KeyPress)) { 00080 detectedActivity(); 00081 return true; 00082 } else if (object != m_grabber) { 00083 // If it's not the grabber, fallback to default event filter 00084 return false; 00085 } 00086 00087 // Otherwise, simply ignore it 00088 return false; 00089 00090 } 00091 00092 void WidgetBasedPoller::waitForActivity() 00093 { 00094 // This code was taken from Lithium/KDE4Powersave 00095 00096 m_grabber->show(); 00097 m_grabber->grabMouse(); 00098 m_grabber->grabKeyboard(); 00099 00100 } 00101 00102 void WidgetBasedPoller::detectedActivity() 00103 { 00104 stopCatchingIdleEvents(); 00105 emit resumingFromIdle(); 00106 } 00107 00108 void WidgetBasedPoller::releaseInputLock() 00109 { 00110 m_grabber->releaseMouse(); 00111 m_grabber->releaseKeyboard(); 00112 m_grabber->hide(); 00113 } 00114 00115 int WidgetBasedPoller::poll() 00116 { 00117 int idle = getIdleTime(); 00118 00119 // Check if we reached a timeout.. 00120 foreach(int i, m_timeouts) { 00121 if ((i - idle < 300 && i > idle) || (idle - i < 300 && idle > i)) { 00122 // Bingo! 00123 emit timeoutReached(i); 00124 } 00125 } 00126 00127 // Let's check the timer now! 00128 int mintime = 0; 00129 00130 foreach(int i, m_timeouts) { 00131 if (i > idle && (i < mintime || mintime == 0)) { 00132 mintime = i; 00133 } 00134 } 00135 00136 //qDebug() << "mintime " << mintime << "idle " << idle; 00137 00138 if (mintime != 0) { 00139 m_pollTimer->start(mintime - idle); 00140 } else { 00141 m_pollTimer->stop(); 00142 } 00143 00144 return idle; 00145 } 00146 00147 int WidgetBasedPoller::forcePollRequest() 00148 { 00149 return poll(); 00150 } 00151 00152 void WidgetBasedPoller::removeTimeout(int timeout) 00153 { 00154 m_timeouts.removeOne(timeout); 00155 poll(); 00156 } 00157 00158 void WidgetBasedPoller::catchIdleEvent() 00159 { 00160 waitForActivity(); 00161 } 00162 00163 void WidgetBasedPoller::stopCatchingIdleEvents() 00164 { 00165 releaseInputLock(); 00166 } 00167 00168 #include "widgetbasedpoller.moc"
KDE 4.4 API Reference