1 | /*
|
---|
2 | * idle.cpp - detect desktop idle time
|
---|
3 | * Copyright (C) 2003 Justin Karneges
|
---|
4 | *
|
---|
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.
|
---|
9 | *
|
---|
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.
|
---|
14 | *
|
---|
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
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include"idle.h"
|
---|
22 |
|
---|
23 | #include<qcursor.h>
|
---|
24 | #include<qdatetime.h>
|
---|
25 | #include<qtimer.h>
|
---|
26 |
|
---|
27 | static IdlePlatform *platform = 0;
|
---|
28 | static int platform_ref = 0;
|
---|
29 |
|
---|
30 | class Idle::Private
|
---|
31 | {
|
---|
32 | public:
|
---|
33 | Private() {}
|
---|
34 |
|
---|
35 | QPoint lastMousePos;
|
---|
36 | QDateTime idleSince;
|
---|
37 |
|
---|
38 | bool active;
|
---|
39 | int idleTime;
|
---|
40 | QDateTime startTime;
|
---|
41 | QTimer checkTimer;
|
---|
42 | };
|
---|
43 |
|
---|
44 | Idle::Idle()
|
---|
45 | {
|
---|
46 | d = new Private;
|
---|
47 | d->active = false;
|
---|
48 | d->idleTime = 0;
|
---|
49 |
|
---|
50 | // try to use platform idle
|
---|
51 | if(!platform) {
|
---|
52 | IdlePlatform *p = new IdlePlatform;
|
---|
53 | if(p->init())
|
---|
54 | platform = p;
|
---|
55 | else
|
---|
56 | delete p;
|
---|
57 | }
|
---|
58 | if(platform)
|
---|
59 | ++platform_ref;
|
---|
60 |
|
---|
61 | connect(&d->checkTimer, SIGNAL(timeout()), SLOT(doCheck()));
|
---|
62 | }
|
---|
63 |
|
---|
64 | Idle::~Idle()
|
---|
65 | {
|
---|
66 | if(platform) {
|
---|
67 | --platform_ref;
|
---|
68 | if(platform_ref == 0) {
|
---|
69 | delete platform;
|
---|
70 | platform = 0;
|
---|
71 | }
|
---|
72 | }
|
---|
73 | delete d;
|
---|
74 | }
|
---|
75 |
|
---|
76 | bool Idle::isActive() const
|
---|
77 | {
|
---|
78 | return d->active;
|
---|
79 | }
|
---|
80 |
|
---|
81 | bool Idle::usingPlatform() const
|
---|
82 | {
|
---|
83 | return (platform ? true: false);
|
---|
84 | }
|
---|
85 |
|
---|
86 | void Idle::start()
|
---|
87 | {
|
---|
88 | d->startTime = QDateTime::currentDateTime();
|
---|
89 |
|
---|
90 | if(!platform) {
|
---|
91 | // generic idle
|
---|
92 | d->lastMousePos = QCursor::pos();
|
---|
93 | d->idleSince = QDateTime::currentDateTime();
|
---|
94 | }
|
---|
95 |
|
---|
96 | // poll every second (use a lower value if you need more accuracy)
|
---|
97 | d->checkTimer.start(1000);
|
---|
98 | }
|
---|
99 |
|
---|
100 | void Idle::stop()
|
---|
101 | {
|
---|
102 | d->checkTimer.stop();
|
---|
103 | }
|
---|
104 |
|
---|
105 | void Idle::doCheck()
|
---|
106 | {
|
---|
107 | int i;
|
---|
108 | if(platform)
|
---|
109 | i = platform->secondsIdle();
|
---|
110 | else {
|
---|
111 | QPoint curMousePos = QCursor::pos();
|
---|
112 | QDateTime curDateTime = QDateTime::currentDateTime();
|
---|
113 | if(d->lastMousePos != curMousePos) {
|
---|
114 | d->lastMousePos = curMousePos;
|
---|
115 | d->idleSince = curDateTime;
|
---|
116 | }
|
---|
117 | i = d->idleSince.secsTo(curDateTime);
|
---|
118 | }
|
---|
119 |
|
---|
120 | // set 'beginIdle' to the beginning of the idle time (by backtracking 'i' seconds from now)
|
---|
121 | QDateTime beginIdle = QDateTime::currentDateTime().addSecs(-i);
|
---|
122 |
|
---|
123 | // set 't' to hold the number of seconds between 'beginIdle' and 'startTime'
|
---|
124 | int t = beginIdle.secsTo(d->startTime);
|
---|
125 |
|
---|
126 | // beginIdle later than (or equal to) startTime?
|
---|
127 | if(t <= 0) {
|
---|
128 | // scoot ourselves up to the new idle start
|
---|
129 | d->startTime = beginIdle;
|
---|
130 | }
|
---|
131 | // beginIdle earlier than startTime?
|
---|
132 | else if(t > 0) {
|
---|
133 | // do nothing
|
---|
134 | }
|
---|
135 |
|
---|
136 | // how long have we been idle?
|
---|
137 | int idleTime = d->startTime.secsTo(QDateTime::currentDateTime());
|
---|
138 |
|
---|
139 | secondsIdle(idleTime);
|
---|
140 | }
|
---|