2009-07-15 20:54:46 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2008 University of California
|
|
|
|
//
|
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// BOINC 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#if defined(_WIN32) && !defined(__STDWX_H__)
|
2009-11-13 21:23:15 +00:00
|
|
|
#include "boinc_win.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#elif defined(_WIN32) && defined(__STDWX_H__)
|
|
|
|
#include "stdwx.h"
|
2009-11-13 21:23:15 +00:00
|
|
|
#endif
|
|
|
|
|
2006-12-14 18:15:22 +00:00
|
|
|
#include "win_util.h"
|
2006-01-23 08:47:05 +00:00
|
|
|
|
|
|
|
/**
|
2009-11-13 21:23:15 +00:00
|
|
|
* The following global data is only shared in this process
|
2006-01-23 08:47:05 +00:00
|
|
|
**/
|
|
|
|
HANDLE g_hMemoryMappedData = NULL;
|
|
|
|
|
|
|
|
/**
|
2009-11-13 21:23:15 +00:00
|
|
|
* The following global data is SHARED among all (processes); i.e., these are system-wide globals.
|
2006-01-23 08:47:05 +00:00
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
struct SystemWideIdleData {
|
2006-01-23 08:47:05 +00:00
|
|
|
DWORD dwLastTick; // tick time of last input event
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SystemWideIdleData* g_pSystemWideIdleData = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get tick count of last keyboard or mouse event
|
|
|
|
**/
|
2009-11-13 21:23:15 +00:00
|
|
|
long get_idle_tick_count() {
|
2006-01-23 08:47:05 +00:00
|
|
|
DWORD dwCurrentTickCount = GetTickCount();
|
|
|
|
DWORD dwLastTickCount = 0;
|
|
|
|
|
2009-07-15 20:54:46 +00:00
|
|
|
if ( g_pSystemWideIdleData )
|
2006-01-23 08:47:05 +00:00
|
|
|
{
|
2009-07-15 20:54:46 +00:00
|
|
|
LASTINPUTINFO lii;
|
|
|
|
ZeroMemory( &lii, sizeof(lii) );
|
|
|
|
lii.cbSize = sizeof(lii);
|
|
|
|
GetLastInputInfo( &lii );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If both values are greater than the system tick count then
|
|
|
|
* the system must have looped back to the beginning.
|
|
|
|
**/
|
|
|
|
if ( ( dwCurrentTickCount < lii.dwTime ) &&
|
|
|
|
( dwCurrentTickCount < g_pSystemWideIdleData->dwLastTick ) )
|
2006-01-23 08:47:05 +00:00
|
|
|
{
|
2009-07-15 20:54:46 +00:00
|
|
|
lii.dwTime = dwCurrentTickCount;
|
|
|
|
g_pSystemWideIdleData->dwLastTick = dwCurrentTickCount;
|
2006-01-23 08:47:05 +00:00
|
|
|
}
|
2009-07-15 20:54:46 +00:00
|
|
|
|
|
|
|
if ( lii.dwTime > g_pSystemWideIdleData->dwLastTick )
|
|
|
|
g_pSystemWideIdleData->dwLastTick = lii.dwTime;
|
|
|
|
|
|
|
|
dwLastTickCount = g_pSystemWideIdleData->dwLastTick;
|
2006-01-23 08:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (dwCurrentTickCount - dwLastTickCount);
|
|
|
|
}
|
|
|
|
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2009-11-13 21:23:15 +00:00
|
|
|
bool startup_idle_monitor() {
|
2006-01-23 08:47:05 +00:00
|
|
|
BOOL bExists = FALSE;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bResult = false;
|
2006-01-23 08:47:05 +00:00
|
|
|
SECURITY_ATTRIBUTES sec_attr;
|
|
|
|
SECURITY_DESCRIPTOR sd;
|
|
|
|
|
|
|
|
|
2009-07-15 20:54:46 +00:00
|
|
|
/*
|
|
|
|
* Create a security descriptor that will allow
|
|
|
|
* everyone full access.
|
|
|
|
*/
|
|
|
|
InitializeSecurityDescriptor( &sd, SECURITY_DESCRIPTOR_REVISION );
|
|
|
|
SetSecurityDescriptorDacl( &sd, TRUE, NULL, FALSE );
|
|
|
|
|
|
|
|
sec_attr.nLength = sizeof(sec_attr);
|
|
|
|
sec_attr.bInheritHandle = TRUE;
|
|
|
|
sec_attr.lpSecurityDescriptor = &sd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a filemap object that is global for everyone,
|
|
|
|
* including users logged in via terminal services.
|
|
|
|
*/
|
|
|
|
g_hMemoryMappedData =
|
|
|
|
CreateFileMapping(
|
|
|
|
INVALID_HANDLE_VALUE,
|
|
|
|
&sec_attr,
|
|
|
|
PAGE_READWRITE,
|
|
|
|
0,
|
|
|
|
4096,
|
2009-11-13 21:23:15 +00:00
|
|
|
_T("Global\\BoincIdleTracker")
|
2009-07-15 20:54:46 +00:00
|
|
|
);
|
|
|
|
if( NULL == g_hMemoryMappedData )
|
|
|
|
{
|
|
|
|
g_hMemoryMappedData =
|
|
|
|
CreateFileMapping(
|
|
|
|
INVALID_HANDLE_VALUE,
|
|
|
|
&sec_attr,
|
|
|
|
PAGE_READWRITE,
|
|
|
|
0,
|
|
|
|
4096,
|
2009-11-13 21:23:15 +00:00
|
|
|
_T("BoincIdleTracker")
|
2006-01-23 08:47:05 +00:00
|
|
|
);
|
|
|
|
}
|
2009-07-15 20:54:46 +00:00
|
|
|
|
|
|
|
if( NULL != g_hMemoryMappedData )
|
2006-01-23 08:47:05 +00:00
|
|
|
{
|
2009-07-15 20:54:46 +00:00
|
|
|
if( ERROR_ALREADY_EXISTS == GetLastError() )
|
|
|
|
bExists = TRUE;
|
|
|
|
|
|
|
|
g_pSystemWideIdleData = (struct SystemWideIdleData*)
|
|
|
|
MapViewOfFile(
|
|
|
|
g_hMemoryMappedData,
|
|
|
|
FILE_MAP_ALL_ACCESS,
|
2007-10-18 18:53:56 +00:00
|
|
|
0,
|
2009-07-15 20:54:46 +00:00
|
|
|
0,
|
|
|
|
0
|
2007-10-18 18:53:56 +00:00
|
|
|
);
|
2008-08-07 16:59:52 +00:00
|
|
|
}
|
|
|
|
|
2009-07-15 20:54:46 +00:00
|
|
|
if( !bExists && g_pSystemWideIdleData )
|
2008-08-07 16:59:52 +00:00
|
|
|
{
|
2009-07-15 20:54:46 +00:00
|
|
|
g_pSystemWideIdleData->dwLastTick = GetTickCount();
|
2008-08-07 16:59:52 +00:00
|
|
|
}
|
2009-07-15 20:54:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (!g_hMemoryMappedData || !g_pSystemWideIdleData )
|
2009-11-13 21:23:15 +00:00
|
|
|
bResult = false;
|
2008-08-07 16:59:52 +00:00
|
|
|
else
|
2009-11-13 21:23:15 +00:00
|
|
|
bResult = true;
|
2009-07-15 20:54:46 +00:00
|
|
|
|
2008-08-07 16:59:52 +00:00
|
|
|
|
|
|
|
return bResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-13 21:23:15 +00:00
|
|
|
bool attach_idle_monitor() {
|
2008-08-07 16:59:52 +00:00
|
|
|
BOOL bExists = FALSE;
|
2009-11-13 21:23:15 +00:00
|
|
|
bool bResult = false;
|
2008-08-07 16:59:52 +00:00
|
|
|
SECURITY_ATTRIBUTES sec_attr;
|
|
|
|
SECURITY_DESCRIPTOR sd;
|
|
|
|
|
|
|
|
|
2009-07-15 20:54:46 +00:00
|
|
|
/*
|
|
|
|
* Create a security descriptor that will allow
|
|
|
|
* everyone full access.
|
|
|
|
*/
|
|
|
|
InitializeSecurityDescriptor( &sd, SECURITY_DESCRIPTOR_REVISION );
|
|
|
|
SetSecurityDescriptorDacl( &sd, TRUE, NULL, FALSE );
|
|
|
|
|
|
|
|
sec_attr.nLength = sizeof(sec_attr);
|
|
|
|
sec_attr.bInheritHandle = TRUE;
|
|
|
|
sec_attr.lpSecurityDescriptor = &sd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a filemap object that is global for everyone,
|
|
|
|
* including users logged in via terminal services.
|
|
|
|
*/
|
|
|
|
g_hMemoryMappedData =
|
|
|
|
OpenFileMapping(
|
|
|
|
FILE_MAP_READ | FILE_MAP_WRITE,
|
|
|
|
FALSE,
|
2009-11-13 21:23:15 +00:00
|
|
|
_T("Global\\BoincIdleTracker")
|
2009-07-15 20:54:46 +00:00
|
|
|
);
|
|
|
|
if( NULL == g_hMemoryMappedData )
|
|
|
|
{
|
|
|
|
g_hMemoryMappedData =
|
|
|
|
OpenFileMapping(
|
|
|
|
FILE_MAP_READ | FILE_MAP_WRITE,
|
|
|
|
FALSE,
|
2009-11-13 21:23:15 +00:00
|
|
|
_T("BoincIdleTracker")
|
2009-07-15 20:54:46 +00:00
|
|
|
);
|
|
|
|
}
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2009-07-15 20:54:46 +00:00
|
|
|
if( NULL != g_hMemoryMappedData )
|
2008-08-07 16:59:52 +00:00
|
|
|
{
|
2009-07-15 20:54:46 +00:00
|
|
|
if( ERROR_ALREADY_EXISTS == GetLastError() )
|
|
|
|
bExists = TRUE;
|
|
|
|
|
|
|
|
g_pSystemWideIdleData = (struct SystemWideIdleData*)
|
|
|
|
MapViewOfFile(
|
|
|
|
g_hMemoryMappedData,
|
|
|
|
FILE_MAP_ALL_ACCESS,
|
|
|
|
0,
|
|
|
|
0,
|
2008-08-07 16:59:52 +00:00
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
2006-01-23 08:47:05 +00:00
|
|
|
|
2009-07-15 20:54:46 +00:00
|
|
|
if( !bExists && g_pSystemWideIdleData )
|
|
|
|
{
|
|
|
|
g_pSystemWideIdleData->dwLastTick = GetTickCount();
|
2006-01-23 08:47:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-15 20:54:46 +00:00
|
|
|
if (!g_hMemoryMappedData || !g_pSystemWideIdleData )
|
2009-11-13 21:23:15 +00:00
|
|
|
bResult = false;
|
2006-01-23 08:47:05 +00:00
|
|
|
else
|
2009-11-13 21:23:15 +00:00
|
|
|
bResult = true;
|
2006-01-23 08:47:05 +00:00
|
|
|
|
|
|
|
return bResult;
|
|
|
|
}
|
|
|
|
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2009-11-13 21:23:15 +00:00
|
|
|
void shutdown_idle_monitor()
|
2006-01-23 08:47:05 +00:00
|
|
|
{
|
2009-07-15 20:54:46 +00:00
|
|
|
if( NULL != g_pSystemWideIdleData )
|
2006-01-23 08:47:05 +00:00
|
|
|
{
|
2009-07-15 20:54:46 +00:00
|
|
|
UnmapViewOfFile(g_pSystemWideIdleData);
|
|
|
|
CloseHandle(g_hMemoryMappedData);
|
2006-01-23 08:47:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-07 16:59:52 +00:00
|
|
|
|
2009-11-13 21:23:15 +00:00
|
|
|
void detach_idle_monitor()
|
2008-08-07 16:59:52 +00:00
|
|
|
{
|
2009-11-13 21:23:15 +00:00
|
|
|
shutdown_idle_monitor();
|
2008-08-07 16:59:52 +00:00
|
|
|
}
|