2
* idle.cpp - detect desktop idle time
3
* Copyright (C) 2003 Justin Karneges
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
#include <qdatetime.h>
27
static IdlePlatform *platform = 0;
28
static int platform_ref = 0;
50
// try to use platform idle
52
IdlePlatform *p = new IdlePlatform;
61
connect(&d->checkTimer, SIGNAL(timeout()), SLOT(doCheck()));
68
if(platform_ref == 0) {
76
bool Idle::isActive() const
81
bool Idle::usingPlatform() const
83
return (platform ? true: false);
88
d->startTime = QDateTime::currentDateTime();
92
d->lastMousePos = QCursor::pos();
93
d->idleSince = QDateTime::currentDateTime();
96
// poll every second (use a lower value if you need more accuracy)
97
d->checkTimer.start(1000);
102
d->checkTimer.stop();
109
i = platform->secondsIdle();
111
QPoint curMousePos = QCursor::pos();
112
QDateTime curDateTime = QDateTime::currentDateTime();
113
if(d->lastMousePos != curMousePos) {
114
d->lastMousePos = curMousePos;
115
d->idleSince = curDateTime;
117
i = d->idleSince.secsTo(curDateTime);
120
// set 'beginIdle' to the beginning of the idle time (by backtracking 'i' seconds from now)
121
QDateTime beginIdle = QDateTime::currentDateTime().addSecs(-i);
123
// set 't' to hold the number of seconds between 'beginIdle' and 'startTime'
124
int t = beginIdle.secsTo(d->startTime);
126
// beginIdle later than (or equal to) startTime?
128
// scoot ourselves up to the new idle start
129
d->startTime = beginIdle;
131
// beginIdle earlier than startTime?
136
// how long have we been idle?
137
int idleTime = d->startTime.secsTo(QDateTime::currentDateTime());
139
secondsIdle(idleTime);