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"
10 #include "ui/base/win/lock_state.h"
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
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
;
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() {
42 if (::SystemParametersInfo(SPI_GETSCREENSAVERRUNNING
, 0, &result
, 0))
43 return result
!= FALSE
;
49 void CalculateIdleTime(IdleTimeCallback notify
) {
50 notify
.Run(static_cast<int>(CalculateIdleTimeInternal()));
53 bool CheckIdleStateIsLocked() {
54 return ui::IsWorkstationLocked() || IsScreensaverRunning();