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