~ubuntu-branches/ubuntu/precise/boinc/precise

« back to all changes in this revision

Viewing changes to lib/idlemon_win.cpp

Tags: 6.12.8+dfsg-1
* New upstream release.
* Simplified debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// This file is part of BOINC.
 
2
// http://boinc.berkeley.edu
 
3
// Copyright (C) 2008 University of California
 
4
//
 
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.
 
9
//
 
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.
 
14
//
 
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/>.
 
17
 
 
18
#if   defined(_WIN32) && !defined(__STDWX_H__)
 
19
#include "boinc_win.h"
 
20
#elif defined(_WIN32) && defined(__STDWX_H__)
 
21
#include "stdwx.h"
 
22
#endif
 
23
 
 
24
#include "win_util.h"
 
25
 
 
26
/**
 
27
 * The following global data is only shared in this process
 
28
 **/ 
 
29
HANDLE    g_hMemoryMappedData = NULL;
 
30
 
 
31
/**
 
32
 * The following global data is SHARED among all (processes); i.e., these are system-wide globals.
 
33
 **/
 
34
struct SystemWideIdleData {
 
35
        DWORD   dwLastTick;         // tick time of last input event
 
36
};
 
37
 
 
38
struct SystemWideIdleData* g_pSystemWideIdleData = NULL;
 
39
 
 
40
/**
 
41
 * Get tick count of last keyboard or mouse event
 
42
 **/
 
43
long get_idle_tick_count() {
 
44
    DWORD dwCurrentTickCount = GetTickCount();
 
45
    DWORD dwLastTickCount = 0;
 
46
 
 
47
    if ( g_pSystemWideIdleData )
 
48
    {
 
49
        LASTINPUTINFO lii;
 
50
        ZeroMemory( &lii, sizeof(lii) );
 
51
        lii.cbSize = sizeof(lii);
 
52
        GetLastInputInfo( &lii );
 
53
 
 
54
        /**
 
55
         * If both values are greater than the system tick count then
 
56
         *   the system must have looped back to the beginning.
 
57
         **/
 
58
        if ( ( dwCurrentTickCount < lii.dwTime ) &&
 
59
             ( dwCurrentTickCount < g_pSystemWideIdleData->dwLastTick ) )
 
60
        {
 
61
            lii.dwTime = dwCurrentTickCount;
 
62
            g_pSystemWideIdleData->dwLastTick = dwCurrentTickCount;
 
63
        }
 
64
 
 
65
        if ( lii.dwTime > g_pSystemWideIdleData->dwLastTick )
 
66
            g_pSystemWideIdleData->dwLastTick = lii.dwTime;
 
67
 
 
68
        dwLastTickCount = g_pSystemWideIdleData->dwLastTick;
 
69
    }
 
70
 
 
71
        return (dwCurrentTickCount - dwLastTickCount);
 
72
}
 
73
 
 
74
 
 
75
bool startup_idle_monitor() {
 
76
        BOOL                bExists = FALSE;
 
77
        bool                bResult = false;
 
78
        SECURITY_ATTRIBUTES     sec_attr;
 
79
        SECURITY_DESCRIPTOR sd;
 
80
 
 
81
 
 
82
    /*
 
83
    * Create a security descriptor that will allow
 
84
    * everyone full access.
 
85
    */
 
86
    InitializeSecurityDescriptor( &sd, SECURITY_DESCRIPTOR_REVISION );
 
87
    SetSecurityDescriptorDacl( &sd, TRUE, NULL, FALSE );
 
88
 
 
89
    sec_attr.nLength = sizeof(sec_attr);
 
90
    sec_attr.bInheritHandle = TRUE;
 
91
    sec_attr.lpSecurityDescriptor = &sd;
 
92
 
 
93
    /*
 
94
    * Create a filemap object that is global for everyone,
 
95
    * including users logged in via terminal services.
 
96
    */
 
97
    g_hMemoryMappedData = 
 
98
        CreateFileMapping(
 
99
            INVALID_HANDLE_VALUE,
 
100
                    &sec_attr,
 
101
                    PAGE_READWRITE,
 
102
                    0,
 
103
                    4096,
 
104
                    _T("Global\\BoincIdleTracker")
 
105
        );
 
106
    if( NULL == g_hMemoryMappedData )
 
107
    {
 
108
        g_hMemoryMappedData = 
 
109
            CreateFileMapping(
 
110
                INVALID_HANDLE_VALUE,
 
111
                        &sec_attr,
 
112
                        PAGE_READWRITE,
 
113
                        0,
 
114
                        4096,
 
115
                        _T("BoincIdleTracker")
 
116
            );
 
117
    }
 
118
 
 
119
    if( NULL != g_hMemoryMappedData )
 
120
    {
 
121
            if( ERROR_ALREADY_EXISTS == GetLastError() )
 
122
                    bExists = TRUE;
 
123
 
 
124
        g_pSystemWideIdleData = (struct SystemWideIdleData*) 
 
125
            MapViewOfFile(
 
126
                g_hMemoryMappedData, 
 
127
                FILE_MAP_ALL_ACCESS,
 
128
                            0,
 
129
                0,
 
130
                0
 
131
            );
 
132
    }
 
133
 
 
134
    if( !bExists && g_pSystemWideIdleData )
 
135
    {
 
136
            g_pSystemWideIdleData->dwLastTick = GetTickCount();
 
137
    }
 
138
 
 
139
 
 
140
    if (!g_hMemoryMappedData || !g_pSystemWideIdleData )
 
141
            bResult = false;
 
142
    else
 
143
            bResult = true;
 
144
 
 
145
 
 
146
    return bResult;
 
147
}
 
148
 
 
149
 
 
150
bool attach_idle_monitor() {
 
151
        BOOL                bExists = FALSE;
 
152
        bool                bResult = false;
 
153
        SECURITY_ATTRIBUTES     sec_attr;
 
154
        SECURITY_DESCRIPTOR sd;
 
155
 
 
156
 
 
157
    /*
 
158
    * Create a security descriptor that will allow
 
159
    * everyone full access.
 
160
    */
 
161
    InitializeSecurityDescriptor( &sd, SECURITY_DESCRIPTOR_REVISION );
 
162
    SetSecurityDescriptorDacl( &sd, TRUE, NULL, FALSE );
 
163
 
 
164
    sec_attr.nLength = sizeof(sec_attr);
 
165
    sec_attr.bInheritHandle = TRUE;
 
166
    sec_attr.lpSecurityDescriptor = &sd;
 
167
 
 
168
    /*
 
169
    * Create a filemap object that is global for everyone,
 
170
    * including users logged in via terminal services.
 
171
    */
 
172
    g_hMemoryMappedData = 
 
173
        OpenFileMapping(
 
174
            FILE_MAP_READ | FILE_MAP_WRITE,
 
175
                    FALSE,
 
176
                    _T("Global\\BoincIdleTracker")
 
177
        );
 
178
    if( NULL == g_hMemoryMappedData )
 
179
    {
 
180
        g_hMemoryMappedData = 
 
181
            OpenFileMapping(
 
182
                FILE_MAP_READ | FILE_MAP_WRITE,
 
183
                        FALSE,
 
184
                        _T("BoincIdleTracker")
 
185
            );
 
186
    }
 
187
 
 
188
    if( NULL != g_hMemoryMappedData )
 
189
    {
 
190
            if( ERROR_ALREADY_EXISTS == GetLastError() )
 
191
                    bExists = TRUE;
 
192
 
 
193
        g_pSystemWideIdleData = (struct SystemWideIdleData*) 
 
194
            MapViewOfFile(
 
195
                g_hMemoryMappedData, 
 
196
                FILE_MAP_ALL_ACCESS,
 
197
                            0,
 
198
                0,
 
199
                0
 
200
            );
 
201
    }
 
202
 
 
203
    if( !bExists && g_pSystemWideIdleData )
 
204
    {
 
205
            g_pSystemWideIdleData->dwLastTick = GetTickCount();
 
206
    }
 
207
 
 
208
 
 
209
    if (!g_hMemoryMappedData || !g_pSystemWideIdleData )
 
210
            bResult = false;
 
211
    else
 
212
            bResult = true;
 
213
 
 
214
    return bResult;
 
215
}
 
216
 
 
217
 
 
218
void shutdown_idle_monitor()
 
219
{
 
220
    if( NULL != g_pSystemWideIdleData )
 
221
    {
 
222
            UnmapViewOfFile(g_pSystemWideIdleData);
 
223
            CloseHandle(g_hMemoryMappedData);
 
224
    }
 
225
}
 
226
 
 
227
 
 
228
void detach_idle_monitor()
 
229
{
 
230
        shutdown_idle_monitor();
 
231
}