summaryrefslogtreecommitdiff
path: root/core/launcher/suspendmonitor.cpp
Unidiff
Diffstat (limited to 'core/launcher/suspendmonitor.cpp') (more/less context) (show whitespace changes)
-rw-r--r--core/launcher/suspendmonitor.cpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/core/launcher/suspendmonitor.cpp b/core/launcher/suspendmonitor.cpp
new file mode 100644
index 0000000..f555e84
--- a/dev/null
+++ b/core/launcher/suspendmonitor.cpp
@@ -0,0 +1,167 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include <qtopia/qcopenvelope_qws.h>
21#include <qtopia/qpeapplication.h>
22#include "suspendmonitor.h"
23
24
25#ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
26#define QTOPIA_MIN_SCREEN_DISABLE_TIME ((int) 300) // min 5 minutes before forced suspend kicks in
27#endif
28
29
30TempScreenSaverMonitor::TempScreenSaverMonitor(QObject *parent, const char *name)
31 : QObject(parent, name)
32{
33 currentMode = QPEApplication::Enable;
34 timerId = 0;
35}
36
37void TempScreenSaverMonitor::setTempMode(int mode, int pid)
38{
39 removeOld(pid);
40 switch(mode) {
41 case QPEApplication::Disable:
42 sStatus[0].append(pid);
43 break;
44 case QPEApplication::DisableLightOff:
45 sStatus[1].append(pid);
46 break;
47 case QPEApplication::DisableSuspend:
48 sStatus[2].append(pid);
49 break;
50 case QPEApplication::Enable:
51 break;
52 default:
53 qWarning("Unrecognized temp power setting. Ignored");
54 return;
55 }
56 updateAll();
57}
58
59// Returns true if app had set a temp Mode earlier
60bool TempScreenSaverMonitor::removeOld(int pid)
61{
62 QValueList<int>::Iterator it;
63 for (int i = 0; i < 3; i++) {
64 it = sStatus[i].find(pid);
65 if ( it != sStatus[i].end() ) {
66 sStatus[i].remove( it );
67 return TRUE;
68 }
69 }
70 return FALSE;
71}
72
73void TempScreenSaverMonitor::updateAll()
74{
75 int mode = QPEApplication::Enable;
76 if ( sStatus[0].count() ) {
77 mode = QPEApplication::Disable;
78 } else if ( sStatus[1].count() ) {
79 mode = QPEApplication::DisableLightOff;
80 } else if ( sStatus[2].count() ) {
81 mode = QPEApplication::DisableSuspend;
82 }
83
84 if ( mode != currentMode ) {
85#ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
86 if ( currentMode == QPEApplication::Enable) {
87 int tid = timerValue();
88 if ( tid )
89 timerId = startTimer( tid * 1000 );
90 } else if ( mode == QPEApplication::Enable ) {
91 if ( timerId ) {
92 killTimer(timerId);
93 timerId = 0;
94 }
95 }
96#endif
97 currentMode = mode;
98 QCopEnvelope("QPE/System", "setScreenSaverMode(int)") << mode;
99 }
100}
101
102void TempScreenSaverMonitor::applicationTerminated(int pid)
103{
104 if ( removeOld(pid) )
105 updateAll();
106}
107
108int TempScreenSaverMonitor::timerValue()
109{
110 int tid = 0;
111#ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
112 tid = QTOPIA_MAX_SCREEN_DISABLE_TIME;
113
114 char *env = getenv("QTOPIA_DISABLED_APM_TIMEOUT");
115 if ( !env )
116 return tid;
117
118 QString strEnv = env;
119 bool ok = FALSE;
120 int envTime = strEnv.toInt(&ok);
121
122 if ( ok ) {
123 if ( envTime < 0 )
124 return 0;
125 else if ( envTime <= QTOPIA_MIN_SCREEN_DISABLE_TIME )
126 return tid;
127 else
128 return envTime;
129 }
130#endif
131
132 return tid;
133}
134
135void TempScreenSaverMonitor::timerEvent(QTimerEvent *t)
136{
137#ifdef QTOPIA_MAX_SCREEN_DISABLE_TIME
138 if ( timerId && (t->timerId() == timerId) ) {
139
140 /* Clean up*/
141 killTimer(timerId);
142 timerId = 0;
143 currentMode = QPEApplication::Enable;
144 QCopEnvelope("QPE/System", "setScreenSaverMode(int)") << currentMode;
145
146 // signal starts on a merry-go-round, which ends up in Desktop::togglePower()
147 emit forceSuspend();
148 // if we have apm we are asleep at this point, next line will be executed when we
149 // awake from suspend.
150 if ( QFile::exists( "/proc/apm" ) ) {
151 QTime t;
152 t = t.addSecs( timerValue() );
153 QString str = tr("<qt>The running applications disabled the screen saver for more than the allowed time (%1).<p>The system was forced to suspend</p></qt>").arg( t.toString() );
154 QMessageBox::information(0, tr("Forced suspend"), str);
155 }
156
157 // Reset all requests.
158 for (int i = 0; i < 3; i++)
159 sStatus[i].clear();
160
161 updateAll();
162 }
163#else
164 Q_UNUSED(t);
165#endif
166}
167