1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/idle.h"
6
7 #include <limits.h>
8 #include <windows.h>
9
10 static bool IsScreensaverRunning();
11 static bool IsWorkstationLocked();
12
CalculateIdleState(unsigned int idle_threshold)13 IdleState CalculateIdleState(unsigned int idle_threshold) {
14 if (IsScreensaverRunning() || IsWorkstationLocked())
15 return IDLE_STATE_LOCKED;
16
17 LASTINPUTINFO last_input_info = {0};
18 last_input_info.cbSize = sizeof(LASTINPUTINFO);
19 DWORD current_idle_time = 0;
20 if (::GetLastInputInfo(&last_input_info)) {
21 current_idle_time = ::GetTickCount() - last_input_info.dwTime;
22 // Will go -ve if we have been idle for a long time (2gb seconds).
23 if (current_idle_time < 0)
24 current_idle_time = INT_MAX;
25 // Convert from ms to seconds.
26 current_idle_time /= 1000;
27 }
28
29 if (current_idle_time >= idle_threshold)
30 return IDLE_STATE_IDLE;
31 return IDLE_STATE_ACTIVE;
32 }
33
IsScreensaverRunning()34 bool IsScreensaverRunning() {
35 DWORD result = 0;
36 if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &result, 0))
37 return result != FALSE;
38 return false;
39 }
40
IsWorkstationLocked()41 bool IsWorkstationLocked() {
42 bool is_locked = true;
43 HDESK input_desk = ::OpenInputDesktop(0, 0, GENERIC_READ);
44 if (input_desk) {
45 wchar_t name[256] = {0};
46 DWORD needed = 0;
47 if (::GetUserObjectInformation(input_desk,
48 UOI_NAME,
49 name,
50 sizeof(name),
51 &needed)) {
52 is_locked = lstrcmpi(name, L"default") != 0;
53 }
54 ::CloseDesktop(input_desk);
55 }
56 return is_locked;
57 }
58