1 /*
2  * idle_win.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 <qlibrary.h>
24 #include <windows.h>
25 
26 #if defined(Q_OS_WIN) && !defined(Q_CC_GNU) && (_WIN32_WINNT < 0x0500)
27 typedef struct tagLASTINPUTINFO
28 {
29 	UINT cbSize;
30 	DWORD dwTime;
31 } LASTINPUTINFO, *PLASTINPUTINFO;
32 #endif
33 
34 BOOL (__stdcall * GetLastInputInfoFun)(PLASTINPUTINFO);
35 DWORD (__stdcall * IdleUIGetLastInputTime)(void);
36 QLibrary *lib;
37 
Idle(QObject * parent)38 Idle::Idle(QObject *parent) :
39 		QObject(parent)
40 {
41 	QFunctionPointer p;
42 
43 	GetLastInputInfoFun = 0;
44 	IdleUIGetLastInputTime = 0;
45 	lib = 0;
46 
47 	if (lib == 0)
48 	{
49 		// try to find the built-in Windows 2000 function
50 		lib = new QLibrary("user32");
51 		if(lib->load() && (p = lib->resolve("GetLastInputInfo")))
52 		{
53 			GetLastInputInfoFun = (BOOL (__stdcall *)(PLASTINPUTINFO))p;
54 		}
55 		else
56 		{
57 			delete lib;
58 
59 			// fall back on idleui
60 			lib = new QLibrary("idleui");
61 			if(lib->load() && (p = lib->resolve("IdleUIGetLastInputTime")))
62 			{
63 				IdleUIGetLastInputTime = (DWORD (__stdcall *)(void))p;
64 			}
65 			else
66 			{
67 				delete lib;
68 				lib = 0;
69 			}
70 		}
71 	}
72 }
73 
~Idle()74 Idle::~Idle()
75 {
76 	delete lib;
77 	lib = 0;
78 }
79 
secondsIdle()80 long Idle::secondsIdle()
81 {
82 	int i;
83 	if (GetLastInputInfoFun != 0)
84 	{
85 		LASTINPUTINFO li;
86 		li.cbSize = sizeof(LASTINPUTINFO);
87 		bool ok = GetLastInputInfoFun(&li);
88 		if (!ok)
89 			return -1;
90 		i = li.dwTime;
91 	}
92 	else if (IdleUIGetLastInputTime)
93 	{
94 		i = IdleUIGetLastInputTime();
95 	}
96 	else
97 		return -1;
98 
99 	return (GetTickCount() - i) / 1000;
100 }
101 
102 #include "moc_idle.cpp"
103