Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / idle / idle_win.cc
blobf151537477d9ca35e1a5179df98e446c759acb16
1 // Copyright (c) 2011 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.
5 #include "ui/base/idle/idle.h"
7 #include <limits.h>
8 #include <windows.h>
10 #include "ui/base/win/lock_state.h"
12 namespace ui {
13 namespace {
15 DWORD CalculateIdleTimeInternal() {
16 LASTINPUTINFO last_input_info = {0};
17 last_input_info.cbSize = sizeof(LASTINPUTINFO);
18 DWORD current_idle_time = 0;
19 if (::GetLastInputInfo(&last_input_info)) {
20 DWORD now = ::GetTickCount();
21 if (now < last_input_info.dwTime) {
22 // GetTickCount() wraps around every 49.7 days -- assume it wrapped just
23 // once.
24 const DWORD kMaxDWORD = static_cast<DWORD>(-1);
25 DWORD time_before_wrap = kMaxDWORD - last_input_info.dwTime;
26 DWORD time_after_wrap = now;
27 // The sum is always smaller than kMaxDWORD.
28 current_idle_time = time_before_wrap + time_after_wrap;
29 } else {
30 current_idle_time = now - last_input_info.dwTime;
33 // Convert from ms to seconds.
34 current_idle_time /= 1000;
37 return current_idle_time;
40 bool IsScreensaverRunning() {
41 DWORD result = 0;
42 if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &result, 0))
43 return result != FALSE;
44 return false;
47 } // namespace
49 void CalculateIdleTime(IdleTimeCallback notify) {
50 notify.Run(static_cast<int>(CalculateIdleTimeInternal()));
53 bool CheckIdleStateIsLocked() {
54 return ui::IsWorkstationLocked() || IsScreensaverRunning();
57 } // namespace ui