1
// This file is part of BOINC.
2
// http://boinc.berkeley.edu
3
// Copyright (C) 2008 University of California
5
// BOINC is free software; you can redistribute it and/or modify it
6
// under the terms of the GNU Lesser General Public License
7
// as published by the Free Software Foundation,
8
// either version 3 of the License, or (at your option) any later version.
10
// BOINC 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.
13
// See the GNU Lesser General Public License for more details.
15
// You should have received a copy of the GNU Lesser General Public License
16
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
18
#if defined(_WIN32) && !defined(__STDWX_H__)
19
#include "boinc_win.h"
20
#elif defined(_WIN32) && defined(__STDWX_H__)
27
* The following global data is only shared in this process
29
HANDLE g_hMemoryMappedData = NULL;
32
* The following global data is SHARED among all (processes); i.e., these are system-wide globals.
34
struct SystemWideIdleData {
35
DWORD dwLastTick; // tick time of last input event
38
struct SystemWideIdleData* g_pSystemWideIdleData = NULL;
41
* Get tick count of last keyboard or mouse event
43
long get_idle_tick_count() {
44
DWORD dwCurrentTickCount = GetTickCount();
45
DWORD dwLastTickCount = 0;
47
if ( g_pSystemWideIdleData )
50
ZeroMemory( &lii, sizeof(lii) );
51
lii.cbSize = sizeof(lii);
52
GetLastInputInfo( &lii );
55
* If both values are greater than the system tick count then
56
* the system must have looped back to the beginning.
58
if ( ( dwCurrentTickCount < lii.dwTime ) &&
59
( dwCurrentTickCount < g_pSystemWideIdleData->dwLastTick ) )
61
lii.dwTime = dwCurrentTickCount;
62
g_pSystemWideIdleData->dwLastTick = dwCurrentTickCount;
65
if ( lii.dwTime > g_pSystemWideIdleData->dwLastTick )
66
g_pSystemWideIdleData->dwLastTick = lii.dwTime;
68
dwLastTickCount = g_pSystemWideIdleData->dwLastTick;
71
return (dwCurrentTickCount - dwLastTickCount);
75
bool startup_idle_monitor() {
78
SECURITY_ATTRIBUTES sec_attr;
79
SECURITY_DESCRIPTOR sd;
83
* Create a security descriptor that will allow
84
* everyone full access.
86
InitializeSecurityDescriptor( &sd, SECURITY_DESCRIPTOR_REVISION );
87
SetSecurityDescriptorDacl( &sd, TRUE, NULL, FALSE );
89
sec_attr.nLength = sizeof(sec_attr);
90
sec_attr.bInheritHandle = TRUE;
91
sec_attr.lpSecurityDescriptor = &sd;
94
* Create a filemap object that is global for everyone,
95
* including users logged in via terminal services.
104
_T("Global\\BoincIdleTracker")
106
if( NULL == g_hMemoryMappedData )
108
g_hMemoryMappedData =
110
INVALID_HANDLE_VALUE,
115
_T("BoincIdleTracker")
119
if( NULL != g_hMemoryMappedData )
121
if( ERROR_ALREADY_EXISTS == GetLastError() )
124
g_pSystemWideIdleData = (struct SystemWideIdleData*)
134
if( !bExists && g_pSystemWideIdleData )
136
g_pSystemWideIdleData->dwLastTick = GetTickCount();
140
if (!g_hMemoryMappedData || !g_pSystemWideIdleData )
150
bool attach_idle_monitor() {
151
BOOL bExists = FALSE;
152
bool bResult = false;
153
SECURITY_ATTRIBUTES sec_attr;
154
SECURITY_DESCRIPTOR sd;
158
* Create a security descriptor that will allow
159
* everyone full access.
161
InitializeSecurityDescriptor( &sd, SECURITY_DESCRIPTOR_REVISION );
162
SetSecurityDescriptorDacl( &sd, TRUE, NULL, FALSE );
164
sec_attr.nLength = sizeof(sec_attr);
165
sec_attr.bInheritHandle = TRUE;
166
sec_attr.lpSecurityDescriptor = &sd;
169
* Create a filemap object that is global for everyone,
170
* including users logged in via terminal services.
172
g_hMemoryMappedData =
174
FILE_MAP_READ | FILE_MAP_WRITE,
176
_T("Global\\BoincIdleTracker")
178
if( NULL == g_hMemoryMappedData )
180
g_hMemoryMappedData =
182
FILE_MAP_READ | FILE_MAP_WRITE,
184
_T("BoincIdleTracker")
188
if( NULL != g_hMemoryMappedData )
190
if( ERROR_ALREADY_EXISTS == GetLastError() )
193
g_pSystemWideIdleData = (struct SystemWideIdleData*)
203
if( !bExists && g_pSystemWideIdleData )
205
g_pSystemWideIdleData->dwLastTick = GetTickCount();
209
if (!g_hMemoryMappedData || !g_pSystemWideIdleData )
218
void shutdown_idle_monitor()
220
if( NULL != g_pSystemWideIdleData )
222
UnmapViewOfFile(g_pSystemWideIdleData);
223
CloseHandle(g_hMemoryMappedData);
228
void detach_idle_monitor()
230
shutdown_idle_monitor();