summaryrefslogtreecommitdiffstats
path: root/src/inactivity.cpp
blob: e432fb7c0b8d9f2fdd63b5417e3d11ebf1d2aeca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/***************************************************************************
 *   Copyright (C) 2006-2007 by Danny Kukawka                              *
 *                           <[email protected]>, <[email protected]>    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License     *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

/*! \file 	inactivity.cpp
 * \brief 	In this file can be found the inactivity related code. 
 * \author 	Danny Kukawka, <[email protected]>, <[email protected]>
 * \date    	2006-2007
 */

// own header
#include "inactivity.h"

/* needed for lXext C library linkage */
extern "C" {
	#include <X11/Xproto.h>
	#include <X11/extensions/dpms.h>
	#include <X11/extensions/scrnsaver.h>
}
 
// KDE Headers
#include <klocale.h>

/*! The default constructor of the class autosuspend */
inactivity::inactivity() {
	kdDebugFuncIn(trace);

	proc = NULL;

	timeToInactivity = 0;
	blacklisted_running_last = 0;
	
	pidof_call_failed = false;
	pidof_call_started = false;
	pidof_call_returned = false;
	blacklisted_running = false;
	
	int dummy = 0;
	has_XSC_Extension = XScreenSaverQueryExtension( qt_xdisplay(), &dummy, &dummy );
	
	checkInactivity =  new QTimer( this );
	connect( checkInactivity, SIGNAL(timeout()), this, SLOT(check()));

	kdDebugFuncOut(trace);
}

/*! The default destructor of the class autosuspend */
inactivity::~inactivity() {
	kdDebugFuncIn(trace);

	delete proc;
	proc = NULL;

	kdDebugFuncOut(trace);
}

/*!
 * This function start the monitoring of inactivity of user on the X-Server.
 * Here wee set the time for the signal \ref inactivityTimeExpired() and start
 * the needed QTimer.
 * \param timeToExpire  Integer value representing the time of inactivity which need 
 *			to elapse befor send signal. The time is in seconds.
 * \param blacked	QStringList with blacklisted programs which if detected with 
 *			pidof() as running prevent the autosuspend.
 */
void inactivity::start( int timeToExpire, QStringList blacked ) {
	kdDebugFuncIn(trace);

	blacklist = blacked;
 
	if(timeToExpire > 0 && has_XSC_Extension){
		stop();
		timeToInactivity = (unsigned long) (timeToExpire * 1000);
		checkInactivity->start(CHECK_for_INACTIVITY, true);
	}

	kdDebugFuncOut(trace);
}

/*!
 * \b SLOT to call check as recheck inactivity if befor a running PID 
 * request was detected.
 */
void inactivity::recheck() {
	kdDebugFuncIn(trace);

	check(true);
	
	kdDebugFuncOut(trace);
}

/*!
 * \b SLOT to call check without a recheck.
 */
void inactivity::check() {
	check(false);
}

/*!
 * \b SLOT to check the current idle-time of the X-Server and if there
 * are blacklisted programs are running. If the through \ref timeToInactivity 
 * defined time is expired, this function emit signal \ref inactivityTimeExpired() .
 * \param recheck   boolean which define if this is a recheck or not. 
 *                  \li true, if this is a recheck. In this case we didn't call
 *                      \ref checkBlacklisted() again
 *                  \li false, if this is normal check
 */
void inactivity::check( bool recheck ) {
	kdDebugFuncIn(trace);

	if (timeToInactivity > 0) {	
		checkXInactivity();
		if (!pidof_call_started && !recheck) checkBlacklisted();
		
		if( idleTime < blacklisted_running_last ) {
			blacklisted_running_last = idleTime;
		}
	
		if((idleTime - blacklisted_running_last ) >= timeToInactivity) {
			if (!pidof_call_started) {
				if (( pidof_call_returned && !blacklisted_running ) ||
				( pidof_call_returned && pidof_call_failed )) {
					emit inactivityTimeExpired();
				}
				else {
					checkInactivity->start(CHECK_for_INACTIVITY, true);
				}
			}
			else {
				//called if there is a getPIDs() is running
				QTimer::singleShot(500, this, SLOT(recheck()));
			}
		}
		else checkInactivity->start(CHECK_for_INACTIVITY, true);
	} else {
		kdWarning() <<  "timeToInactivity <= 0, stoped autosuspend checks!" << endl;
	}

	kdDebugFuncOut(trace);
}

/*!
 * This function stop the monitoring and reset all variables and Timer.
 */
void inactivity::stop() {
	kdDebugFuncIn(trace);

	if (checkInactivity->isActive()) checkInactivity->stop();
	timeToInactivity = 0;
	idleTime = 0;
	blacklisted_running_last = 0;
	
	pidof_call_failed = false;
	pidof_call_started = false;
	pidof_call_returned = false;
	blacklisted_running = false;

	kdDebugFuncOut(trace);
}

/*!
 * This function query the idle-time of user-imput from the X-Server and set
 * the return value to \ref idleTime.
 */
void inactivity::checkXInactivity(){
	kdDebugFuncIn(trace);

	idleTime = getXInactivity();
	kdDebug() << "autosuspend::checkXInactivity - idleTime: " << idleTime << endl;

	kdDebugFuncOut(trace);
}

/*!
 * This function query the idle-time of user-imput from the X-Server and
 * return the current idle-time. 
 */
unsigned long inactivity::getXInactivity(){
	kdDebugFuncIn(trace);

	if(has_XSC_Extension) {
		static XScreenSaverInfo* mitInfo = 0;
		if (!mitInfo) mitInfo = XScreenSaverAllocInfo ();
		XScreenSaverQueryInfo (qt_xdisplay(), DefaultRootWindow (qt_xdisplay()), mitInfo);
		kdDebugFuncOut(trace);
		return workaroundCreepyXServer(mitInfo->idle);
	}
	else {
		kdDebugFuncOut(trace);
		return 0;
	}
	
}

/*!
 * This function workaround a fucking XServer idleTime bug in the 
 * XScreenSaverExtension, if dpms is running. In this case always the
 * current dpms-state time is extracted from the current idletime.
 * This mean: XScreenSaverInfo->idle is not the time since the last
 * user activity, as descriped in the header file of the extension.
 * This result in SUSE bug # and sf.net bug #
 *
 * Workaround: check if if XServer is in a dpms state, check the 
 *             current timeout for this state and add this value to 
 * 	       the current idle time and return.
 *
 * \param _idleTime a unsigned long value with the current ideletime fromm
 *                  XScreenSaverInfo->idle
 * \return a unsigned long with the corrected idletime
 */
unsigned long inactivity::workaroundCreepyXServer( unsigned long _idleTime ){
	kdDebugFuncOut(trace);

	int dummy;
	CARD16 standby, suspend, off;
	CARD16 state;
	BOOL onoff;

	Display *dpy = qt_xdisplay();

	kdDebug() << "Current idleTime: " << _idleTime << endl;

	if (DPMSQueryExtension(dpy, &dummy, &dummy)) {
		if (DPMSCapable(dpy)) {
			DPMSGetTimeouts(dpy, &standby, &suspend, &off);
			DPMSInfo(dpy, &state, &onoff);

			if (onoff) {
				switch (state) {
					case DPMSModeStandby:
						kdDebug() << "DPMS enabled. Monitor in Standby. Standby: "
							  << standby << " sec" << endl;
						// this check is a littlebit paranoid, but be sure
						if (_idleTime < (unsigned) (standby * 1000))
							_idleTime += (standby * 1000);
						break;
					case DPMSModeSuspend:
						kdDebug() << "DPMS enabled. Monitor in Suspend. Suspend: "
							  << suspend << " sec" << endl;
						if (_idleTime < (unsigned) ((suspend + standby) * 1000))
							_idleTime += ((suspend + standby) * 1000);
						break;
					case DPMSModeOff:
						kdDebug() << "DPMS enabled. Monitor is Off. Off: "
							  << off << " sec" << endl;
						if (_idleTime < (unsigned) ((off + suspend + standby) * 1000))
							_idleTime += ((off + suspend + standby) * 1000);
						break;
					case DPMSModeOn:
					default:
						break;
				}
			}
		} 
	}

	kdDebug() << "Corrected idleTime: " << _idleTime << endl;
	kdDebugFuncOut(trace);
	return _idleTime;
}

/*!
 * This funtion starts the monitoring of blacklisted processes.
 */
void inactivity::checkBlacklisted(){
	kdDebugFuncIn(trace);

	if (proc != NULL) {
		delete proc;
		proc = NULL;
	}

	proc = new KProcess;
	*proc << "pidof" << blacklist;

	connect( proc, SIGNAL(receivedStdout(KProcess *, char *, int)),this,
		 SLOT(getPIDs(KProcess *, char *, int)));
	connect( proc, SIGNAL(processExited(KProcess *)),
		 SLOT(getPIDsExited(KProcess *)));
	
	if (!proc->start(KProcess::NotifyOnExit, KProcess::AllOutput))
	{
		emit displayErrorMsg(i18n("Could not start 'pidof'. "
					  "Could not autosuspend the machine.\n"
					  "Please check your installation."));
	}

	pidof_call_started = true;
	pidof_call_returned = false;
	pidof_call_failed = false;

	kdDebugFuncOut(trace);
}


/*!
 * \b SLOT to get the return of the command pidof and parse this to set
 * \ref blacklisted_running .
 * \param *proc     pointer to the sending KProcess
 * \param *buffer   the char pointer to the output of the process to stdout
 * \param *lenght   the length of the buffer
 */
void inactivity::getPIDs(KProcess */*proc*/, char *buffer, int /*lenght*/) {
	kdDebugFuncIn(trace);

	QString pids(buffer);
	pids.remove(" ");
	if(pids.isEmpty() || pids == "\n" ) {
		kdDebug() << "NO! BLACKLISTED IS RUNNING" << endl;
		blacklisted_running = false;
	} 
	else {
		if (pids.contains(QRegExp::QRegExp("[0-9]"))) {
			kdDebug() << "BLACKLISTED IS RUNNING" << endl;
			blacklisted_running = true;
			blacklisted_running_last = idleTime;
		}
		else {
			kdError() << "GET BLACKLISTED FAILED - WRONG RETURN" << endl;
			blacklisted_running = false;
			pidof_call_failed = true;
		}
	}

	kdDebugFuncOut(trace);
}


/*!
 * \b SLOT which called if the call of pidof is exited
 * \param proc the KPocess which called this SLOT
 */
void inactivity::getPIDsExited(KProcess *proc){
	kdDebugFuncIn(trace);

	pidof_call_returned = true;
	pidof_call_started = false;
	
	
	if (proc->normalExit()){
		// if returned some pids or if pid returned nothing
		if (proc->exitStatus() == 1 || proc->exitStatus() == 0){
			pidof_call_failed = false;
			kdDebugFuncOut(trace);
			return;
		}
	}	
	// if something crashed/failed
	pidof_call_failed = true;
	kdDebugFuncOut(trace);
}

#include "inactivity.moc"